zig/lib/std/special/compiler_rt/popcountti2_test.zig
Jan Philipp Hafer 1ea650bb75 compiler_rt: add __popcountsi2, __popcountdi2 and __popcountti2
- apply simpler approach than LLVM for __popcountdi2
  taken from The Art of Computer Programming and generalized
- rename popcountdi2.zig to popcount.zig
- test cases derived from popcountdi2_test.zig
- tests: compare naive approach 10_000 times with
  random numbers created from naive seed 42

    See #1290
2021-11-29 12:50:25 -08:00

34 lines
1 KiB
Zig

const popcount = @import("popcount.zig");
const testing = @import("std").testing;
fn popcountti2Naive(a: i128) i32 {
var x = a;
var r: i32 = 0;
while (x != 0) : (x = @bitCast(i128, @bitCast(u128, x) >> 1)) {
r += @intCast(i32, x & 1);
}
return r;
}
fn test__popcountti2(a: i128) !void {
const x = popcount.__popcountti2(a);
const expected = popcountti2Naive(a);
try testing.expectEqual(expected, x);
}
test "popcountti2" {
try test__popcountti2(0);
try test__popcountti2(1);
try test__popcountti2(2);
try test__popcountti2(@bitCast(i128, @as(u128, 0xffffffff_ffffffff_ffffffff_fffffffd)));
try test__popcountti2(@bitCast(i128, @as(u128, 0xffffffff_ffffffff_ffffffff_fffffffe)));
try test__popcountti2(@bitCast(i128, @as(u128, 0xffffffff_ffffffff_ffffffff_ffffffff)));
const RndGen = @import("std").rand.DefaultPrng;
var rnd = RndGen.init(42);
var i: u32 = 0;
while (i < 10_000) : (i += 1) {
var rand_num = rnd.random().int(i128);
try test__popcountti2(rand_num);
}
}