aboutsummaryrefslogtreecommitdiff
path: root/test
diff options
context:
space:
mode:
authorShawn Landden <shawn@git.icu>2019-06-29 11:32:26 -0500
committerAndrew Kelley <andrew@ziglang.org>2019-09-18 11:26:45 -0400
commit193604c837df75ab0c3fa5860f8b234263fe5b50 (patch)
treed96c52e9fd95d3e964fcc298505effc6bf3bffb3 /test
parent9e4065fa738f040dd338c613409fc1089cc33580 (diff)
downloadzig-193604c837df75ab0c3fa5860f8b234263fe5b50.tar.gz
zig-193604c837df75ab0c3fa5860f8b234263fe5b50.zip
stage1: add @shuffle() shufflevector support
I change the semantics of the mask operand, to make it a little more flexible. There is no real danger in this because it is a compile-error if you do it the LLVM way (and there is an appropiate error to tell you this). v2: avoid problems with double-free
Diffstat (limited to 'test')
-rw-r--r--test/compile_errors.zig13
-rw-r--r--test/stage1/behavior/shuffle.zig57
2 files changed, 70 insertions, 0 deletions
diff --git a/test/compile_errors.zig b/test/compile_errors.zig
index 9d96d6f948..d9b4ee6a95 100644
--- a/test/compile_errors.zig
+++ b/test/compile_errors.zig
@@ -6485,6 +6485,19 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
);
cases.addTest(
+ "using LLVM syntax for @shuffle",
+ \\export fn entry() void {
+ \\ const v: @Vector(4, u32) = [4]u32{0, 1, 2, 3};
+ \\ const x: @Vector(4, u32) = [4]u32{4, 5, 6, 7};
+ \\ var z = @shuffle(u32, v, x, [8]i32{0, 1, 2, 3, 4, 5, 6, 7});
+ \\}
+ ,
+ "tmp.zig:4:39: error: mask index out of bounds",
+ "tmp.zig:4:39: note: when computing vector element at index 4",
+ "tmp.zig:4:39: note: selections from the second vector are specified with negative numbers",
+ );
+
+ cases.addTest(
"nested vectors",
\\export fn entry() void {
\\ const V = @Vector(4, @Vector(4, u8));
diff --git a/test/stage1/behavior/shuffle.zig b/test/stage1/behavior/shuffle.zig
new file mode 100644
index 0000000000..70bff5991e
--- /dev/null
+++ b/test/stage1/behavior/shuffle.zig
@@ -0,0 +1,57 @@
+const std = @import("std");
+const mem = std.mem;
+const expect = std.testing.expect;
+
+test "@shuffle" {
+ const S = struct {
+ fn doTheTest() void {
+ var v: @Vector(4, i32) = [4]i32{ 2147483647, -2, 30, 40 };
+ var x: @Vector(4, i32) = [4]i32{ 1, 2147483647, 3, 4 };
+ const mask: @Vector(4, i32) = [4]i32{ 0, ~i32(2), 3, ~i32(3)};
+ var res = @shuffle(i32, v, x, mask);
+ expect(mem.eql(i32, ([4]i32)(res), [4]i32{ 2147483647, 3, 40, 4 }));
+
+ // Implicit cast from array (of mask)
+ res = @shuffle(i32, v, x, [4]i32{ 0, ~i32(2), 3, ~i32(3)});
+ expect(mem.eql(i32, ([4]i32)(res), [4]i32{ 2147483647, 3, 40, 4 }));
+
+ // Undefined
+ const mask2: @Vector(4, i32) = [4]i32{ 3, 1, 2, 0};
+ res = @shuffle(i32, v, undefined, mask2);
+ expect(mem.eql(i32, ([4]i32)(res), [4]i32{ 40, -2, 30, 2147483647}));
+
+ // Upcasting of b
+ var v2: @Vector(2, i32) = [2]i32{ 2147483647, undefined};
+ const mask3: @Vector(4, i32) = [4]i32{ ~i32(0), 2, ~i32(0), 3};
+ res = @shuffle(i32, x, v2, mask3);
+ expect(mem.eql(i32, ([4]i32)(res), [4]i32{ 2147483647, 3, 2147483647, 4 }));
+
+ // Upcasting of a
+ var v3: @Vector(2, i32) = [2]i32{ 2147483647, -2};
+ const mask4: @Vector(4, i32) = [4]i32{ 0, ~i32(2), 1, ~i32(3)};
+ res = @shuffle(i32, v3, x, mask4);
+ expect(mem.eql(i32, ([4]i32)(res), [4]i32{ 2147483647, 3, -2, 4 }));
+
+ // bool
+ {
+ var x2: @Vector(4, bool) = [4]bool{ false, true, false, true};
+ var v4: @Vector(2, bool) = [2]bool{ true, false};
+ const mask5: @Vector(4, i32) = [4]i32{ 0, ~i32(1), 1, 2};
+ var res2 = @shuffle(bool, x2, v4, mask5);
+ expect(mem.eql(bool, ([4]bool)(res2), [4]bool{ false, false, true, false }));
+ }
+
+ // FIXME re-enable when LLVM codegen is fixed
+ // https://bugs.llvm.org/show_bug.cgi?id=42803
+ if (false) {
+ var x2: @Vector(3, bool) = [3]bool{ false, true, false};
+ var v4: @Vector(2, bool) = [2]bool{ true, false};
+ const mask5: @Vector(4, i32) = [4]i32{ 0, ~i32(1), 1, 2};
+ var res2 = @shuffle(bool, x2, v4, mask5);
+ expect(mem.eql(bool, ([4]bool)(res2), [4]bool{ false, false, true, false }));
+ }
+ }
+ };
+ S.doTheTest();
+ comptime S.doTheTest();
+}