aboutsummaryrefslogtreecommitdiff
path: root/test/behavior/shuffle.zig
diff options
context:
space:
mode:
authorRobin Voetter <robin@voetter.nl>2024-06-04 22:09:15 +0200
committerRobin Voetter <robin@voetter.nl>2024-06-10 20:32:50 +0200
commita567f3871ec06f3e6a8c0e6424aba556f1069ccc (patch)
treee319b045727eeaa9b701c06bc7fcde198525365f /test/behavior/shuffle.zig
parenta3b1ba82f57d5d8981a471850cbbb0db29c3a479 (diff)
downloadzig-a567f3871ec06f3e6a8c0e6424aba556f1069ccc.tar.gz
zig-a567f3871ec06f3e6a8c0e6424aba556f1069ccc.zip
spirv: improve shuffle codegen
Diffstat (limited to 'test/behavior/shuffle.zig')
-rw-r--r--test/behavior/shuffle.zig83
1 files changed, 83 insertions, 0 deletions
diff --git a/test/behavior/shuffle.zig b/test/behavior/shuffle.zig
index fb16f3fbb3..2bcdbd1581 100644
--- a/test/behavior/shuffle.zig
+++ b/test/behavior/shuffle.zig
@@ -2,6 +2,7 @@ const std = @import("std");
const builtin = @import("builtin");
const mem = std.mem;
const expect = std.testing.expect;
+const expectEqual = std.testing.expectEqual;
test "@shuffle int" {
if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest; // TODO
@@ -49,6 +50,88 @@ test "@shuffle int" {
try comptime S.doTheTest();
}
+test "@shuffle int strange sizes" {
+ if (builtin.zig_backend == .stage2_wasm) 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
+ if (builtin.zig_backend == .stage2_sparc64) return error.SkipZigTest; // TODO
+ if (builtin.zig_backend == .stage2_riscv64) return error.SkipZigTest;
+
+ try comptime testShuffle(2, 2, 2);
+ try testShuffle(2, 2, 2);
+ try comptime testShuffle(4, 4, 4);
+ try testShuffle(4, 4, 4);
+ try comptime testShuffle(7, 4, 4);
+ try testShuffle(7, 4, 4);
+ try comptime testShuffle(8, 6, 4);
+ try testShuffle(8, 6, 4);
+ try comptime testShuffle(2, 7, 5);
+ try testShuffle(2, 7, 5);
+ try comptime testShuffle(13, 16, 12);
+ try testShuffle(13, 16, 12);
+ try comptime testShuffle(19, 3, 17);
+ try testShuffle(19, 3, 17);
+ try comptime testShuffle(1, 10, 1);
+ try testShuffle(1, 10, 1);
+}
+
+fn testShuffle(
+ comptime x_len: comptime_int,
+ comptime a_len: comptime_int,
+ comptime b_len: comptime_int,
+) !void {
+ const T = i32;
+ const XT = @Vector(x_len, T);
+ const AT = @Vector(a_len, T);
+ const BT = @Vector(b_len, T);
+
+ const a_elems = comptime blk: {
+ var elems: [a_len]T = undefined;
+ for (&elems, 0..) |*elem, i| elem.* = @intCast(100 + i);
+ break :blk elems;
+ };
+ var a: AT = a_elems;
+ _ = &a;
+
+ const b_elems = comptime blk: {
+ var elems: [b_len]T = undefined;
+ for (&elems, 0..) |*elem, i| elem.* = @intCast(1000 + i);
+ break :blk elems;
+ };
+ var b: BT = b_elems;
+ _ = &b;
+
+ const mask_seed: []const i32 = &.{ -14, -31, 23, 1, 21, 13, 17, -21, -10, -27, -16, -5, 15, 14, -2, 26, 2, -31, -24, -16 };
+
+ const mask = comptime blk: {
+ var elems: [x_len]i32 = undefined;
+ for (&elems, 0..) |*elem, i| {
+ const mask_val = mask_seed[i];
+ if (mask_val >= 0) {
+ elem.* = @mod(mask_val, a_len);
+ } else {
+ elem.* = @mod(mask_val, -b_len);
+ }
+ }
+
+ break :blk elems;
+ };
+
+ const x: XT = @shuffle(T, a, b, mask);
+
+ const x_elems: [x_len]T = x;
+ for (mask, x_elems) |m, x_elem| {
+ if (m >= 0) {
+ // Element from A
+ try expectEqual(x_elem, a_elems[@intCast(m)]);
+ } else {
+ // Element from B
+ try expectEqual(x_elem, b_elems[@intCast(~m)]);
+ }
+ }
+}
+
test "@shuffle bool 1" {
if (builtin.zig_backend == .stage2_wasm) return error.SkipZigTest; // TODO
if (builtin.zig_backend == .stage2_x86_64) return error.SkipZigTest; // TODO