Skip to content

Commit

Permalink
stage2: implement alignment calculation of vectors
Browse files Browse the repository at this point in the history
closes #11856
  • Loading branch information
andrewrk committed Jul 1, 2022
1 parent d3542be commit 095e24e
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 4 deletions.
1 change: 0 additions & 1 deletion lib/std/simd.zig
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,6 @@ pub fn extract(
}

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

Expand Down
10 changes: 7 additions & 3 deletions src/type.zig
Original file line number Diff line number Diff line change
Expand Up @@ -2906,9 +2906,13 @@ pub const Type = extern union {

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

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

.i16, .u16 => return AbiAlignmentAdvanced{ .scalar = intAbiAlignment(16, target) },
.u29 => return AbiAlignmentAdvanced{ .scalar = intAbiAlignment(29, target) },
Expand Down
24 changes: 24 additions & 0 deletions test/behavior/vector.zig
Original file line number Diff line number Diff line change
Expand Up @@ -1048,3 +1048,27 @@ test "@shlWithOverflow" {
try S.doTheTest();
comptime try S.doTheTest();
}

test "alignment of vectors" {
try expect(@alignOf(@Vector(2, u8)) == 2);
try expect(@alignOf(@Vector(2, u1)) == 2);
try expect(@alignOf(@Vector(1, u1)) == 1);
try expect(@alignOf(@Vector(2, u16)) == 4);
}

test "loading the second vector from a slice of vectors" {
if (builtin.zig_backend == .stage2_wasm) return error.SkipZigTest; // TODO
if (builtin.zig_backend == .stage2_c) return error.SkipZigTest; // TODO
if (builtin.zig_backend == .stage2_x86_64) return error.SkipZigTest; // TODO
if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest; // TODO
if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; // TODO

@setRuntimeSafety(false);
var small_bases = [2]@Vector(2, u8){
@Vector(2, u8){ 0, 1 },
@Vector(2, u8){ 2, 3 },
};
var a: []const @Vector(2, u8) = &small_bases;
var a4 = a[1][1];
try expect(a4 == 3);
}

0 comments on commit 095e24e

Please sign in to comment.