aboutsummaryrefslogtreecommitdiff
path: root/lib/std/special/compiler_rt/popcountdi2_test.zig
diff options
context:
space:
mode:
Diffstat (limited to 'lib/std/special/compiler_rt/popcountdi2_test.zig')
-rw-r--r--lib/std/special/compiler_rt/popcountdi2_test.zig27
1 files changed, 27 insertions, 0 deletions
diff --git a/lib/std/special/compiler_rt/popcountdi2_test.zig b/lib/std/special/compiler_rt/popcountdi2_test.zig
new file mode 100644
index 0000000000..bedcbcd1de
--- /dev/null
+++ b/lib/std/special/compiler_rt/popcountdi2_test.zig
@@ -0,0 +1,27 @@
+const __popcountdi2 = @import("popcountdi2.zig").__popcountdi2;
+const testing = @import("std").testing;
+
+fn naive_popcount(a_param: i64) i32 {
+ var a = a_param;
+ var r: i32 = 0;
+ while (a != 0) : (a = @bitCast(i64, @bitCast(u64, a) >> 1)) {
+ r += @intCast(i32, a & 1);
+ }
+ return r;
+}
+
+fn test__popcountdi2(a: i64) void {
+ const x = __popcountdi2(a);
+ const expected = naive_popcount(a);
+ testing.expect(expected == x);
+}
+
+test "popcountdi2" {
+ test__popcountdi2(0);
+ test__popcountdi2(1);
+ test__popcountdi2(2);
+ test__popcountdi2(@bitCast(i64, u64(0xFFFFFFFFFFFFFFFD)));
+ test__popcountdi2(@bitCast(i64, u64(0xFFFFFFFFFFFFFFFE)));
+ test__popcountdi2(@bitCast(i64, u64(0xFFFFFFFFFFFFFFFF)));
+ // TODO some fuzz testing
+}