Skip to content

Commit 095e24e

Browse files
committed
stage2: implement alignment calculation of vectors
closes #11856
1 parent d3542be commit 095e24e

File tree

3 files changed

+31
-4
lines changed

3 files changed

+31
-4
lines changed

lib/std/simd.zig

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,6 @@ pub fn extract(
160160
}
161161

162162
test "vector patterns" {
163-
if (@import("builtin").zig_backend != .stage1) return error.SkipZigTest;
164163
const base = @Vector(4, u32){ 10, 20, 30, 40 };
165164
const other_base = @Vector(4, u32){ 55, 66, 77, 88 };
166165

src/type.zig

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2906,9 +2906,13 @@ pub const Type = extern union {
29062906

29072907
.array, .array_sentinel => return ty.elemType().abiAlignmentAdvanced(target, strat),
29082908

2909-
// TODO audit this - is there any more complicated logic to determine
2910-
// ABI alignment of vectors?
2911-
.vector => return AbiAlignmentAdvanced{ .scalar = 16 },
2909+
.vector => {
2910+
const len = ty.arrayLen();
2911+
const bits = try bitSizeAdvanced(ty.elemType(), target, sema_kit);
2912+
const bytes = (bits + 7) / 8;
2913+
const alignment = std.math.ceilPowerOfTwoAssert(u64, bytes * len);
2914+
return AbiAlignmentAdvanced{ .scalar = @intCast(u32, alignment) };
2915+
},
29122916

29132917
.i16, .u16 => return AbiAlignmentAdvanced{ .scalar = intAbiAlignment(16, target) },
29142918
.u29 => return AbiAlignmentAdvanced{ .scalar = intAbiAlignment(29, target) },

test/behavior/vector.zig

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1048,3 +1048,27 @@ test "@shlWithOverflow" {
10481048
try S.doTheTest();
10491049
comptime try S.doTheTest();
10501050
}
1051+
1052+
test "alignment of vectors" {
1053+
try expect(@alignOf(@Vector(2, u8)) == 2);
1054+
try expect(@alignOf(@Vector(2, u1)) == 2);
1055+
try expect(@alignOf(@Vector(1, u1)) == 1);
1056+
try expect(@alignOf(@Vector(2, u16)) == 4);
1057+
}
1058+
1059+
test "loading the second vector from a slice of vectors" {
1060+
if (builtin.zig_backend == .stage2_wasm) return error.SkipZigTest; // TODO
1061+
if (builtin.zig_backend == .stage2_c) return error.SkipZigTest; // TODO
1062+
if (builtin.zig_backend == .stage2_x86_64) return error.SkipZigTest; // TODO
1063+
if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest; // TODO
1064+
if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; // TODO
1065+
1066+
@setRuntimeSafety(false);
1067+
var small_bases = [2]@Vector(2, u8){
1068+
@Vector(2, u8){ 0, 1 },
1069+
@Vector(2, u8){ 2, 3 },
1070+
};
1071+
var a: []const @Vector(2, u8) = &small_bases;
1072+
var a4 = a[1][1];
1073+
try expect(a4 == 3);
1074+
}

0 commit comments

Comments
 (0)