mirror of
https://github.com/ziglang/zig.git
synced 2024-11-27 07:32:44 +00:00
f26dda2117
Most of this migration was performed automatically with `zig fmt`. There were a few exceptions which I had to manually fix: * `@alignCast` and `@addrSpaceCast` cannot be automatically rewritten * `@truncate`'s fixup is incorrect for vectors * Test cases are not formatted, and their error locations change
36 lines
1.0 KiB
Zig
36 lines
1.0 KiB
Zig
const std = @import("std");
|
|
const popcount = @import("popcount.zig");
|
|
const testing = std.testing;
|
|
|
|
fn popcountdi2Naive(a: i64) i32 {
|
|
var x = a;
|
|
var r: i32 = 0;
|
|
while (x != 0) : (x = @as(i64, @bitCast(@as(u64, @bitCast(x)) >> 1))) {
|
|
r += @as(i32, @intCast(x & 1));
|
|
}
|
|
return r;
|
|
}
|
|
|
|
fn test__popcountdi2(a: i64) !void {
|
|
const x = popcount.__popcountdi2(a);
|
|
const expected = popcountdi2Naive(a);
|
|
try testing.expectEqual(expected, x);
|
|
}
|
|
|
|
test "popcountdi2" {
|
|
try test__popcountdi2(0);
|
|
try test__popcountdi2(1);
|
|
try test__popcountdi2(2);
|
|
try test__popcountdi2(@as(i64, @bitCast(@as(u64, 0xffffffff_fffffffd))));
|
|
try test__popcountdi2(@as(i64, @bitCast(@as(u64, 0xffffffff_fffffffe))));
|
|
try test__popcountdi2(@as(i64, @bitCast(@as(u64, 0xffffffff_ffffffff))));
|
|
|
|
const RndGen = std.rand.DefaultPrng;
|
|
var rnd = RndGen.init(42);
|
|
var i: u32 = 0;
|
|
while (i < 10_000) : (i += 1) {
|
|
var rand_num = rnd.random().int(i64);
|
|
try test__popcountdi2(rand_num);
|
|
}
|
|
}
|