authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2019-03-05 23:09:00-05:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2019-03-05 23:09:00-05:00
logb554f6294f4d1c36856b2749a6ff0e457cf0c1a6
treee612e3d1dcbe8eb2d3f851c61b2c9e4c2dd7bf62
parentba144b366c40790f6d77a68b25d4567502601dc1
signature Commit is signed but in an unrecognized format.

add popcountdi2 to compiler_rt


4 files changed, 53 insertions(+), 0 deletions(-)

CMakeLists.txt+1
......@@ -655,6 +655,7 @@ set(ZIG_STD_FILES
655655 "special/compiler_rt/floatuntitf.zig"
656656 "special/compiler_rt/muloti4.zig"
657657 "special/compiler_rt/multi3.zig"
658 "special/compiler_rt/popcountdi2.zig"
658659 "special/compiler_rt/truncXfYf2.zig"
659660 "special/compiler_rt/udivmod.zig"
660661 "special/compiler_rt/udivmoddi4.zig"
std/special/compiler_rt.zig+1
......@@ -67,6 +67,7 @@ comptime {
6767 @export("__fixtfti", @import("compiler_rt/fixtfti.zig").__fixtfti, linkage);
6868
6969 @export("__udivmoddi4", @import("compiler_rt/udivmoddi4.zig").__udivmoddi4, linkage);
70 @export("__popcountdi2", @import("compiler_rt/popcountdi2.zig").__popcountdi2, linkage);
7071
7172 @export("__udivsi3", __udivsi3, linkage);
7273 @export("__udivdi3", __udivdi3, linkage);
std/special/compiler_rt/popcountdi2.zig created+24
......@@ -0,0 +1,24 @@
1const builtin = @import("builtin");
2const compiler_rt = @import("../compiler_rt.zig");
3
4// ported from llvm compiler-rt 8.0.0rc3 95e1c294cb0415a377a7b1d6c7c7d4f89e1c04e4
5pub extern fn __popcountdi2(a: i64) i32 {
6 var x2 = @bitCast(u64, a);
7 x2 = x2 - ((x2 >> 1) & 0x5555555555555555);
8 // Every 2 bits holds the sum of every pair of bits (32)
9 x2 = ((x2 >> 2) & 0x3333333333333333) + (x2 & 0x3333333333333333);
10 // Every 4 bits holds the sum of every 4-set of bits (3 significant bits) (16)
11 x2 = (x2 + (x2 >> 4)) & 0x0F0F0F0F0F0F0F0F;
12 // Every 8 bits holds the sum of every 8-set of bits (4 significant bits) (8)
13 var x: u32 = @truncate(u32, x2 + (x2 >> 32));
14 // The lower 32 bits hold four 16 bit sums (5 significant bits).
15 // Upper 32 bits are garbage */
16 x = x + (x >> 16);
17 // The lower 16 bits hold two 32 bit sums (6 significant bits).
18 // Upper 16 bits are garbage */
19 return @bitCast(i32, (x + (x >> 8)) & 0x0000007F); // (7 significant bits)
20}
21
22test "import popcountdi2" {
23 _ = @import("popcountdi2_test.zig");
24}
std/special/compiler_rt/popcountdi2_test.zig created+27
......@@ -0,0 +1,27 @@
1const __popcountdi2 = @import("popcountdi2.zig").__popcountdi2;
2const testing = @import("std").testing;
3
4fn naive_popcount(a_param: i64) i32 {
5 var a = a_param;
6 var r: i32 = 0;
7 while (a != 0) : (a = @bitCast(i64, @bitCast(u64, a) >> 1)) {
8 r += @intCast(i32, a & 1);
9 }
10 return r;
11}
12
13fn test__popcountdi2(a: i64) void {
14 const x = __popcountdi2(a);
15 const expected = naive_popcount(a);
16 testing.expect(expected == x);
17}
18
19test "popcountdi2" {
20 test__popcountdi2(0);
21 test__popcountdi2(1);
22 test__popcountdi2(2);
23 test__popcountdi2(@bitCast(i64, u64(0xFFFFFFFFFFFFFFFD)));
24 test__popcountdi2(@bitCast(i64, u64(0xFFFFFFFFFFFFFFFE)));
25 test__popcountdi2(@bitCast(i64, u64(0xFFFFFFFFFFFFFFFF)));
26 // TODO some fuzz testing
27}