aboutsummaryrefslogtreecommitdiff
path: root/test
diff options
context:
space:
mode:
authorAndrew Kelley <andrew@ziglang.org>2021-07-26 19:15:27 -0400
committerGitHub <noreply@github.com>2021-07-26 19:15:27 -0400
commitfc105f268149b195ea4a4189da59d40e96e455b4 (patch)
tree8047cbd1e68974cac6de857424d930d650edcc9a /test
parenta9a4fd3200f6f40c1e66a5f8c9e3742639098b9c (diff)
parentdd796154be57da69c0b8467495e9029bb52cb897 (diff)
downloadzig-fc105f268149b195ea4a4189da59d40e96e455b4.tar.gz
zig-fc105f268149b195ea4a4189da59d40e96e455b4.zip
Merge pull request #9458 from SuperAuguste/popcount-ctz-clz
Vector support for `@popCount`, `@ctz`, and `@clz`
Diffstat (limited to 'test')
-rw-r--r--test/behavior/math.zig48
-rw-r--r--test/behavior/popcount.zig32
2 files changed, 61 insertions, 19 deletions
diff --git a/test/behavior/math.zig b/test/behavior/math.zig
index 5c746816e2..7a5c31f67a 100644
--- a/test/behavior/math.zig
+++ b/test/behavior/math.zig
@@ -123,16 +123,27 @@ test "@clz" {
}
fn testClz() !void {
- try expect(clz(u8, 0b10001010) == 0);
- try expect(clz(u8, 0b00001010) == 4);
- try expect(clz(u8, 0b00011010) == 3);
- try expect(clz(u8, 0b00000000) == 8);
- try expect(clz(u128, 0xffffffffffffffff) == 64);
- try expect(clz(u128, 0x10000000000000000) == 63);
+ try expect(@clz(u8, 0b10001010) == 0);
+ try expect(@clz(u8, 0b00001010) == 4);
+ try expect(@clz(u8, 0b00011010) == 3);
+ try expect(@clz(u8, 0b00000000) == 8);
+ try expect(@clz(u128, 0xffffffffffffffff) == 64);
+ try expect(@clz(u128, 0x10000000000000000) == 63);
}
-fn clz(comptime T: type, x: T) usize {
- return @clz(T, x);
+test "@clz vectors" {
+ try testClzVectors();
+ comptime try testClzVectors();
+}
+
+fn testClzVectors() !void {
+ @setEvalBranchQuota(10_000);
+ try expectEqual(@clz(u8, @splat(64, @as(u8, 0b10001010))), @splat(64, @as(u4, 0)));
+ try expectEqual(@clz(u8, @splat(64, @as(u8, 0b00001010))), @splat(64, @as(u4, 4)));
+ try expectEqual(@clz(u8, @splat(64, @as(u8, 0b00011010))), @splat(64, @as(u4, 3)));
+ try expectEqual(@clz(u8, @splat(64, @as(u8, 0b00000000))), @splat(64, @as(u4, 8)));
+ try expectEqual(@clz(u128, @splat(64, @as(u128, 0xffffffffffffffff))), @splat(64, @as(u8, 64)));
+ try expectEqual(@clz(u128, @splat(64, @as(u128, 0x10000000000000000))), @splat(64, @as(u8, 63)));
}
test "@ctz" {
@@ -141,14 +152,23 @@ test "@ctz" {
}
fn testCtz() !void {
- try expect(ctz(u8, 0b10100000) == 5);
- try expect(ctz(u8, 0b10001010) == 1);
- try expect(ctz(u8, 0b00000000) == 8);
- try expect(ctz(u16, 0b00000000) == 16);
+ try expect(@ctz(u8, 0b10100000) == 5);
+ try expect(@ctz(u8, 0b10001010) == 1);
+ try expect(@ctz(u8, 0b00000000) == 8);
+ try expect(@ctz(u16, 0b00000000) == 16);
+}
+
+test "@ctz vectors" {
+ try testClzVectors();
+ comptime try testClzVectors();
}
-fn ctz(comptime T: type, x: T) usize {
- return @ctz(T, x);
+fn testCtzVectors() !void {
+ @setEvalBranchQuota(10_000);
+ try expectEqual(@ctz(u8, @splat(64, @as(u8, 0b10100000))), @splat(64, @as(u4, 5)));
+ try expectEqual(@ctz(u8, @splat(64, @as(u8, 0b10001010))), @splat(64, @as(u4, 1)));
+ try expectEqual(@ctz(u8, @splat(64, @as(u8, 0b00000000))), @splat(64, @as(u4, 8)));
+ try expectEqual(@ctz(u16, @splat(64, @as(u16, 0b00000000))), @splat(64, @as(u5, 16)));
}
test "assignment operators" {
diff --git a/test/behavior/popcount.zig b/test/behavior/popcount.zig
index 99a1849d80..0a2352340d 100644
--- a/test/behavior/popcount.zig
+++ b/test/behavior/popcount.zig
@@ -1,11 +1,14 @@
-const expect = @import("std").testing.expect;
+const std = @import("std");
+const expect = std.testing.expect;
+const expectEqual = std.testing.expectEqual;
+const Vector = std.meta.Vector;
-test "@popCount" {
- comptime try testPopCount();
- try testPopCount();
+test "@popCount integers" {
+ comptime try testPopCountIntegers();
+ try testPopCountIntegers();
}
-fn testPopCount() !void {
+fn testPopCountIntegers() !void {
{
var x: u32 = 0xffffffff;
try expect(@popCount(u32, x) == 32);
@@ -41,3 +44,22 @@ fn testPopCount() !void {
try expect(@popCount(i128, 0b11111111000110001100010000100001000011000011100101010001) == 24);
}
}
+
+test "@popCount vectors" {
+ // https://github.com/ziglang/zig/issues/3317
+ if (std.Target.current.cpu.arch == .mipsel or std.Target.current.cpu.arch == .mips) return error.SkipZigTest;
+
+ comptime try testPopCountVectors();
+ try testPopCountVectors();
+}
+
+fn testPopCountVectors() !void {
+ {
+ var x: Vector(8, u32) = [1]u32{0xffffffff} ** 8;
+ try expectEqual([1]u6{32} ** 8, @as([8]u6, @popCount(u32, x)));
+ }
+ {
+ var x: Vector(8, i16) = [1]i16{-1} ** 8;
+ try expectEqual([1]u5{16} ** 8, @as([8]u5, @popCount(i16, x)));
+ }
+}