authorgravatar for jan.hafer@rwth-aachen.deJan Philipp Hafer <jan.hafer@rwth-aachen.de> 2021-12-01 11:27:40+01:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2021-12-01 13:35:19-08:00
loge4c053f04737d852766b6577e7b1fbfb4a8a3096
tree9bef4ca4a82acdd0336808617fa0033b76420967
parent02a1f838e67edc4107df19ba194a925958f1c669

compiler_rt: add __paritysi2, __paritydi2, __parityti2

- use Bit Twiddling Hacks: Compute parity in parallel - test cases derived from popcount.zig - tests: compare naive approach 10_000 times with random numbers created from naive seed 42 - compiler_rt.zig: sort by LLVM builtin order and add comments to improve structure See #1290

6 files changed, 194 insertions(+), 21 deletions(-)

CMakeLists.txt+1
......@@ -498,6 +498,7 @@ set(ZIG_STAGE2_SOURCES
498498 "${CMAKE_SOURCE_DIR}/lib/std/special/compiler_rt/multi3.zig"
499499 "${CMAKE_SOURCE_DIR}/lib/std/special/compiler_rt/negXf2.zig"
500500 "${CMAKE_SOURCE_DIR}/lib/std/special/compiler_rt/os_version_check.zig"
501 "${CMAKE_SOURCE_DIR}/lib/std/special/compiler_rt/parity.zig"
501502 "${CMAKE_SOURCE_DIR}/lib/std/special/compiler_rt/popcount.zig"
502503 "${CMAKE_SOURCE_DIR}/lib/std/special/compiler_rt/shift.zig"
503504 "${CMAKE_SOURCE_DIR}/lib/std/special/compiler_rt/stack_probe.zig"
lib/std/special/compiler_rt.zig+48-21
......@@ -76,10 +76,11 @@ comptime {
7676
7777 @export(__extendhfsf2, .{ .name = "__gnu_h2f_ieee", .linkage = linkage });
7878
79 const __muloti4 = @import("compiler_rt/muloti4.zig").__muloti4;
80 @export(__muloti4, .{ .name = "__muloti4", .linkage = linkage });
79 // Integral arithmetic which returns if overflow
8180 const __mulodi4 = @import("compiler_rt/mulodi4.zig").__mulodi4;
8281 @export(__mulodi4, .{ .name = "__mulodi4", .linkage = linkage });
82 const __muloti4 = @import("compiler_rt/muloti4.zig").__muloti4;
83 @export(__muloti4, .{ .name = "__muloti4", .linkage = linkage });
8384 }
8485
8586 if (builtin.os.tag == .windows) {
......@@ -217,6 +218,7 @@ comptime {
217218 const __divtf3 = @import("compiler_rt/divtf3.zig").__divtf3;
218219 @export(__divtf3, .{ .name = "__divtf3", .linkage = linkage });
219220
221 // Integral bit manipulation
220222 const __ashldi3 = @import("compiler_rt/shift.zig").__ashldi3;
221223 @export(__ashldi3, .{ .name = "__ashldi3", .linkage = linkage });
222224 const __ashlti3 = @import("compiler_rt/shift.zig").__ashlti3;
......@@ -230,6 +232,33 @@ comptime {
230232 const __lshrti3 = @import("compiler_rt/shift.zig").__lshrti3;
231233 @export(__lshrti3, .{ .name = "__lshrti3", .linkage = linkage });
232234
235 const __clzsi2 = @import("compiler_rt/count0bits.zig").__clzsi2;
236 @export(__clzsi2, .{ .name = "__clzsi2", .linkage = linkage });
237 const __clzdi2 = @import("compiler_rt/count0bits.zig").__clzdi2;
238 @export(__clzdi2, .{ .name = "__clzdi2", .linkage = linkage });
239 const __clzti2 = @import("compiler_rt/count0bits.zig").__clzti2;
240 @export(__clzti2, .{ .name = "__clzti2", .linkage = linkage });
241 const __ctzsi2 = @import("compiler_rt/count0bits.zig").__ctzsi2;
242 @export(__ctzsi2, .{ .name = "__ctzsi2", .linkage = linkage });
243 const __ctzdi2 = @import("compiler_rt/count0bits.zig").__ctzdi2;
244 @export(__ctzdi2, .{ .name = "__ctzdi2", .linkage = linkage });
245 const __ctzti2 = @import("compiler_rt/count0bits.zig").__ctzti2;
246 @export(__ctzti2, .{ .name = "__ctzti2", .linkage = linkage });
247
248 const __paritysi2 = @import("compiler_rt/parity.zig").__paritysi2;
249 @export(__paritysi2, .{ .name = "__paritysi2", .linkage = linkage });
250 const __paritydi2 = @import("compiler_rt/parity.zig").__paritydi2;
251 @export(__paritydi2, .{ .name = "__paritydi2", .linkage = linkage });
252 const __parityti2 = @import("compiler_rt/parity.zig").__parityti2;
253 @export(__parityti2, .{ .name = "__parityti2", .linkage = linkage });
254 const __popcountsi2 = @import("compiler_rt/popcount.zig").__popcountsi2;
255 @export(__popcountsi2, .{ .name = "__popcountsi2", .linkage = linkage });
256 const __popcountdi2 = @import("compiler_rt/popcount.zig").__popcountdi2;
257 @export(__popcountdi2, .{ .name = "__popcountdi2", .linkage = linkage });
258 const __popcountti2 = @import("compiler_rt/popcount.zig").__popcountti2;
259 @export(__popcountti2, .{ .name = "__popcountti2", .linkage = linkage });
260
261 // Integral / floating point conversion (part 1/2)
233262 const __floatsidf = @import("compiler_rt/floatsiXf.zig").__floatsidf;
234263 @export(__floatsidf, .{ .name = "__floatsidf", .linkage = linkage });
235264 const __floatsisf = @import("compiler_rt/floatsiXf.zig").__floatsisf;
......@@ -291,6 +320,7 @@ comptime {
291320 const __extendsfdf2 = @import("compiler_rt/extendXfYf2.zig").__extendsfdf2;
292321 @export(__extendsfdf2, .{ .name = "__extendsfdf2", .linkage = linkage });
293322
323 // Integral / floating point conversion (part 2/2)
294324 const __fixunssfsi = @import("compiler_rt/fixunssfsi.zig").__fixunssfsi;
295325 @export(__fixunssfsi, .{ .name = "__fixunssfsi", .linkage = linkage });
296326 const __fixunssfdi = @import("compiler_rt/fixunssfdi.zig").__fixunssfdi;
......@@ -333,18 +363,13 @@ comptime {
333363
334364 const __udivmoddi4 = @import("compiler_rt/int.zig").__udivmoddi4;
335365 @export(__udivmoddi4, .{ .name = "__udivmoddi4", .linkage = linkage });
336 const __popcountsi2 = @import("compiler_rt/popcount.zig").__popcountsi2;
337 @export(__popcountsi2, .{ .name = "__popcountsi2", .linkage = linkage });
338 const __popcountdi2 = @import("compiler_rt/popcount.zig").__popcountdi2;
339 @export(__popcountdi2, .{ .name = "__popcountdi2", .linkage = linkage });
340 const __popcountti2 = @import("compiler_rt/popcount.zig").__popcountti2;
341 @export(__popcountti2, .{ .name = "__popcountti2", .linkage = linkage });
342366
343367 if (is_darwin) {
344368 const __isPlatformVersionAtLeast = @import("compiler_rt/os_version_check.zig").__isPlatformVersionAtLeast;
345369 @export(__isPlatformVersionAtLeast, .{ .name = "__isPlatformVersionAtLeast", .linkage = linkage });
346370 }
347371
372 // Integral arithmetic
348373 const __mulsi3 = @import("compiler_rt/int.zig").__mulsi3;
349374 @export(__mulsi3, .{ .name = "__mulsi3", .linkage = linkage });
350375 const __muldi3 = @import("compiler_rt/muldi3.zig").__muldi3;
......@@ -372,24 +397,26 @@ comptime {
372397 const __udivmodsi4 = @import("compiler_rt/int.zig").__udivmodsi4;
373398 @export(__udivmodsi4, .{ .name = "__udivmodsi4", .linkage = linkage });
374399
400 // missing: Integral arithmetic with trapping overflow
401
402 // missing: Integral arithmetic which returns if overflow
403
404 // missing: Integral comparison
405 // (a < b) => 0
406 // (a == b) => 1
407 // (a > b) => 2
408
409 // missing: Floating point raised to integer power
410
411 // missing: Complex arithmetic
412 // (a + ib) * (c + id)
413 // (a + ib) / (c + id)
414
375415 const __negsf2 = @import("compiler_rt/negXf2.zig").__negsf2;
376416 @export(__negsf2, .{ .name = "__negsf2", .linkage = linkage });
377417 const __negdf2 = @import("compiler_rt/negXf2.zig").__negdf2;
378418 @export(__negdf2, .{ .name = "__negdf2", .linkage = linkage });
379419
380 const __clzsi2 = @import("compiler_rt/count0bits.zig").__clzsi2;
381 @export(__clzsi2, .{ .name = "__clzsi2", .linkage = linkage });
382 const __clzdi2 = @import("compiler_rt/count0bits.zig").__clzdi2;
383 @export(__clzdi2, .{ .name = "__clzdi2", .linkage = linkage });
384 const __clzti2 = @import("compiler_rt/count0bits.zig").__clzti2;
385 @export(__clzti2, .{ .name = "__clzti2", .linkage = linkage });
386 const __ctzsi2 = @import("compiler_rt/count0bits.zig").__ctzsi2;
387 @export(__ctzsi2, .{ .name = "__ctzsi2", .linkage = linkage });
388 const __ctzdi2 = @import("compiler_rt/count0bits.zig").__ctzdi2;
389 @export(__ctzdi2, .{ .name = "__ctzdi2", .linkage = linkage });
390 const __ctzti2 = @import("compiler_rt/count0bits.zig").__ctzti2;
391 @export(__ctzti2, .{ .name = "__ctzti2", .linkage = linkage });
392
393420 if (builtin.link_libc and os_tag == .openbsd) {
394421 const __emutls_get_address = @import("compiler_rt/emutls.zig").__emutls_get_address;
395422 @export(__emutls_get_address, .{ .name = "__emutls_get_address", .linkage = linkage });
lib/std/special/compiler_rt/parity.zig created+40
......@@ -0,0 +1,40 @@
1const std = @import("std");
2const builtin = @import("builtin");
3
4// parity - if number of bits set is even => 0, else => 1
5// - pariytXi2_generic for big and little endian
6
7fn parityXi2_generic(comptime T: type) fn (a: T) callconv(.C) i32 {
8 return struct {
9 fn f(a: T) callconv(.C) i32 {
10 @setRuntimeSafety(builtin.is_test);
11
12 var x = switch (@bitSizeOf(T)) {
13 32 => @bitCast(u32, a),
14 64 => @bitCast(u64, a),
15 128 => @bitCast(u128, a),
16 else => unreachable,
17 };
18 // Bit Twiddling Hacks: Compute parity in parallel
19 comptime var shift: u8 = @bitSizeOf(T) / 2;
20 inline while (shift > 2) {
21 x ^= x >> shift;
22 shift = shift >> 1;
23 }
24 x &= 0xf;
25 return (@intCast(u16, 0x6996) >> @intCast(u4, x)) & 1; // optimization for >>2 and >>1
26 }
27 }.f;
28}
29
30pub const __paritysi2 = parityXi2_generic(i32);
31
32pub const __paritydi2 = parityXi2_generic(i64);
33
34pub const __parityti2 = parityXi2_generic(i128);
35
36test {
37 _ = @import("paritysi2_test.zig");
38 _ = @import("paritydi2_test.zig");
39 _ = @import("parityti2_test.zig");
40}
lib/std/special/compiler_rt/paritydi2_test.zig created+35
......@@ -0,0 +1,35 @@
1const parity = @import("parity.zig");
2const testing = @import("std").testing;
3
4fn paritydi2Naive(a: i64) i32 {
5 var x = @bitCast(u64, a);
6 var has_parity: bool = false;
7 while (x > 0) {
8 has_parity = !has_parity;
9 x = x & (x - 1);
10 }
11 return @intCast(i32, @boolToInt(has_parity));
12}
13
14fn test__paritydi2(a: i64) !void {
15 var x = parity.__paritydi2(a);
16 var expected: i64 = paritydi2Naive(a);
17 try testing.expectEqual(expected, x);
18}
19
20test "paritydi2" {
21 try test__paritydi2(0);
22 try test__paritydi2(1);
23 try test__paritydi2(2);
24 try test__paritydi2(@bitCast(i64, @as(u64, 0xffffffff_fffffffd)));
25 try test__paritydi2(@bitCast(i64, @as(u64, 0xffffffff_fffffffe)));
26 try test__paritydi2(@bitCast(i64, @as(u64, 0xffffffff_ffffffff)));
27
28 const RndGen = @import("std").rand.DefaultPrng;
29 var rnd = RndGen.init(42);
30 var i: u32 = 0;
31 while (i < 10_000) : (i += 1) {
32 var rand_num = rnd.random().int(i64);
33 try test__paritydi2(rand_num);
34 }
35}
lib/std/special/compiler_rt/paritysi2_test.zig created+35
......@@ -0,0 +1,35 @@
1const parity = @import("parity.zig");
2const testing = @import("std").testing;
3
4fn paritysi2Naive(a: i32) i32 {
5 var x = @bitCast(u32, a);
6 var has_parity: bool = false;
7 while (x > 0) {
8 has_parity = !has_parity;
9 x = x & (x - 1);
10 }
11 return @intCast(i32, @boolToInt(has_parity));
12}
13
14fn test__paritysi2(a: i32) !void {
15 var x = parity.__paritysi2(a);
16 var expected: i32 = paritysi2Naive(a);
17 try testing.expectEqual(expected, x);
18}
19
20test "paritysi2" {
21 try test__paritysi2(0);
22 try test__paritysi2(1);
23 try test__paritysi2(2);
24 try test__paritysi2(@bitCast(i32, @as(u32, 0xfffffffd)));
25 try test__paritysi2(@bitCast(i32, @as(u32, 0xfffffffe)));
26 try test__paritysi2(@bitCast(i32, @as(u32, 0xffffffff)));
27
28 const RndGen = @import("std").rand.DefaultPrng;
29 var rnd = RndGen.init(42);
30 var i: u32 = 0;
31 while (i < 10_000) : (i += 1) {
32 var rand_num = rnd.random().int(i32);
33 try test__paritysi2(rand_num);
34 }
35}
lib/std/special/compiler_rt/parityti2_test.zig created+35
......@@ -0,0 +1,35 @@
1const parity = @import("parity.zig");
2const testing = @import("std").testing;
3
4fn parityti2Naive(a: i128) i32 {
5 var x = @bitCast(u128, a);
6 var has_parity: bool = false;
7 while (x > 0) {
8 has_parity = !has_parity;
9 x = x & (x - 1);
10 }
11 return @intCast(i32, @boolToInt(has_parity));
12}
13
14fn test__parityti2(a: i128) !void {
15 var x = parity.__parityti2(a);
16 var expected: i128 = parityti2Naive(a);
17 try testing.expectEqual(expected, x);
18}
19
20test "parityti2" {
21 try test__parityti2(0);
22 try test__parityti2(1);
23 try test__parityti2(2);
24 try test__parityti2(@bitCast(i128, @as(u128, 0xffffffff_ffffffff_ffffffff_fffffffd)));
25 try test__parityti2(@bitCast(i128, @as(u128, 0xffffffff_ffffffff_ffffffff_fffffffe)));
26 try test__parityti2(@bitCast(i128, @as(u128, 0xffffffff_ffffffff_ffffffff_ffffffff)));
27
28 const RndGen = @import("std").rand.DefaultPrng;
29 var rnd = RndGen.init(42);
30 var i: u32 = 0;
31 while (i < 10_000) : (i += 1) {
32 var rand_num = rnd.random().int(i128);
33 try test__parityti2(rand_num);
34 }
35}