authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2018-06-14 10:59:38-04:00
committergravatar for noreply@github.comGitHub <noreply@github.com> 2018-06-14 10:59:38-04:00
log2219cc061229bdc69314616869ad731b592d9e96
tree4e255416890323a304c909242b56aaf06a9cd15e
parent4ec09ac243afa0b784669e618ec09e9e444a0275
parenta369d69c5144c2a4186e4f8d20bfda0c3f86605a
signaturebadge-question-mark Signed by PGP key 4AEE18F83AFDEB23

Merge pull request #1105 from ziglang/i128-compiler-rt

Add i128 compiler-rt div/mul support

6 files changed, 184 insertions(+), 0 deletions(-)

CMakeLists.txt+2
...@@ -556,6 +556,7 @@ set(ZIG_STD_FILES...@@ -556,6 +556,7 @@ set(ZIG_STD_FILES
556 "special/compiler_rt/aulldiv.zig"556 "special/compiler_rt/aulldiv.zig"
557 "special/compiler_rt/aullrem.zig"557 "special/compiler_rt/aullrem.zig"
558 "special/compiler_rt/comparetf2.zig"558 "special/compiler_rt/comparetf2.zig"
559 "special/compiler_rt/divti3.zig"
559 "special/compiler_rt/fixuint.zig"560 "special/compiler_rt/fixuint.zig"
560 "special/compiler_rt/fixunsdfdi.zig"561 "special/compiler_rt/fixunsdfdi.zig"
561 "special/compiler_rt/fixunsdfsi.zig"562 "special/compiler_rt/fixunsdfsi.zig"
...@@ -566,6 +567,7 @@ set(ZIG_STD_FILES...@@ -566,6 +567,7 @@ set(ZIG_STD_FILES
566 "special/compiler_rt/fixunstfdi.zig"567 "special/compiler_rt/fixunstfdi.zig"
567 "special/compiler_rt/fixunstfsi.zig"568 "special/compiler_rt/fixunstfsi.zig"
568 "special/compiler_rt/fixunstfti.zig"569 "special/compiler_rt/fixunstfti.zig"
570 "special/compiler_rt/muloti4.zig"
569 "special/compiler_rt/index.zig"571 "special/compiler_rt/index.zig"
570 "special/compiler_rt/udivmod.zig"572 "special/compiler_rt/udivmod.zig"
571 "special/compiler_rt/udivmoddi4.zig"573 "special/compiler_rt/udivmoddi4.zig"
std/special/compiler_rt/divti3.zig created+26
...@@ -0,0 +1,26 @@
1const udivmod = @import("udivmod.zig").udivmod;
2const builtin = @import("builtin");
3const compiler_rt = @import("index.zig");
4
5pub extern fn __divti3(a: i128, b: i128) i128 {
6 @setRuntimeSafety(builtin.is_test);
7
8 const s_a = a >> (i128.bit_count - 1);
9 const s_b = b >> (i128.bit_count - 1);
10
11 const an = (a ^ s_a) -% s_a;
12 const bn = (b ^ s_b) -% s_b;
13
14 const r = udivmod(u128, @bitCast(u128, an), @bitCast(u128, bn), null);
15 const s = s_a ^ s_b;
16 return (i128(r) ^ s) -% s;
17}
18
19pub extern fn __divti3_windows_x86_64(a: *const i128, b: *const i128) void {
20 @setRuntimeSafety(builtin.is_test);
21 compiler_rt.setXmm0(i128, __divti3(a.*, b.*));
22}
23
24test "import divti3" {
25 _ = @import("divti3_test.zig");
26}
std/special/compiler_rt/divti3_test.zig created+21
...@@ -0,0 +1,21 @@
1const __divti3 = @import("divti3.zig").__divti3;
2const assert = @import("std").debug.assert;
3
4fn test__divti3(a: i128, b: i128, expected: i128) void {
5 const x = __divti3(a, b);
6 assert(x == expected);
7}
8
9test "divti3" {
10 test__divti3(0, 1, 0);
11 test__divti3(0, -1, 0);
12 test__divti3(2, 1, 2);
13 test__divti3(2, -1, -2);
14 test__divti3(-2, 1, -2);
15 test__divti3(-2, -1, 2);
16
17 test__divti3(@bitCast(i128, u128(0x8 << 124)), 1, @bitCast(i128, u128(0x8 << 124)));
18 test__divti3(@bitCast(i128, u128(0x8 << 124)), -1, @bitCast(i128, u128(0x8 << 124)));
19 test__divti3(@bitCast(i128, u128(0x8 << 124)), -2, @bitCast(i128, u128(0x4 << 124)));
20 test__divti3(@bitCast(i128, u128(0x8 << 124)), 2, @bitCast(i128, u128(0xc << 124)));
21}
std/special/compiler_rt/index.zig+4
...@@ -58,6 +58,8 @@ comptime {...@@ -58,6 +58,8 @@ comptime {
58 @export("__chkstk", __chkstk, strong_linkage);58 @export("__chkstk", __chkstk, strong_linkage);
59 @export("___chkstk_ms", ___chkstk_ms, linkage);59 @export("___chkstk_ms", ___chkstk_ms, linkage);
60 }60 }
61 @export("__divti3", @import("divti3.zig").__divti3_windows_x86_64, linkage);
62 @export("__muloti4", @import("muloti4.zig").__muloti4_windows_x86_64, linkage);
61 @export("__udivti3", @import("udivti3.zig").__udivti3_windows_x86_64, linkage);63 @export("__udivti3", @import("udivti3.zig").__udivti3_windows_x86_64, linkage);
62 @export("__udivmodti4", @import("udivmodti4.zig").__udivmodti4_windows_x86_64, linkage);64 @export("__udivmodti4", @import("udivmodti4.zig").__udivmodti4_windows_x86_64, linkage);
63 @export("__umodti3", @import("umodti3.zig").__umodti3_windows_x86_64, linkage);65 @export("__umodti3", @import("umodti3.zig").__umodti3_windows_x86_64, linkage);
...@@ -65,6 +67,8 @@ comptime {...@@ -65,6 +67,8 @@ comptime {
65 else => {},67 else => {},
66 }68 }
67 } else {69 } else {
70 @export("__divti3", @import("divti3.zig").__divti3, linkage);
71 @export("__muloti4", @import("muloti4.zig").__muloti4, linkage);
68 @export("__udivti3", @import("udivti3.zig").__udivti3, linkage);72 @export("__udivti3", @import("udivti3.zig").__udivti3, linkage);
69 @export("__udivmodti4", @import("udivmodti4.zig").__udivmodti4, linkage);73 @export("__udivmodti4", @import("udivmodti4.zig").__udivmodti4, linkage);
70 @export("__umodti3", @import("umodti3.zig").__umodti3, linkage);74 @export("__umodti3", @import("umodti3.zig").__umodti3, linkage);
std/special/compiler_rt/muloti4.zig created+55
...@@ -0,0 +1,55 @@
1const udivmod = @import("udivmod.zig").udivmod;
2const builtin = @import("builtin");
3const compiler_rt = @import("index.zig");
4
5pub extern fn __muloti4(a: i128, b: i128, overflow: *c_int) i128 {
6 @setRuntimeSafety(builtin.is_test);
7
8 const min = @bitCast(i128, u128(1 << (i128.bit_count - 1)));
9 const max = ~min;
10 overflow.* = 0;
11
12 const r = a *% b;
13 if (a == min) {
14 if (b != 0 and b != 1) {
15 overflow.* = 1;
16 }
17 return r;
18 }
19 if (b == min) {
20 if (a != 0 and a != 1) {
21 overflow.* = 1;
22 }
23 return r;
24 }
25
26 const sa = a >> (i128.bit_count - 1);
27 const abs_a = (a ^ sa) -% sa;
28 const sb = b >> (i128.bit_count - 1);
29 const abs_b = (b ^ sb) -% sb;
30
31 if (abs_a < 2 or abs_b < 2) {
32 return r;
33 }
34
35 if (sa == sb) {
36 if (abs_a > @divFloor(max, abs_b)) {
37 overflow.* = 1;
38 }
39 } else {
40 if (abs_a > @divFloor(min, -abs_b)) {
41 overflow.* = 1;
42 }
43 }
44
45 return r;
46}
47
48pub extern fn __muloti4_windows_x86_64(a: *const i128, b: *const i128, overflow: *c_int) void {
49 @setRuntimeSafety(builtin.is_test);
50 compiler_rt.setXmm0(i128, __muloti4(a.*, b.*, overflow));
51}
52
53test "import muloti4" {
54 _ = @import("muloti4_test.zig");
55}
std/special/compiler_rt/muloti4_test.zig created+76
...@@ -0,0 +1,76 @@
1const __muloti4 = @import("muloti4.zig").__muloti4;
2const assert = @import("std").debug.assert;
3
4fn test__muloti4(a: i128, b: i128, expected: i128, expected_overflow: c_int) void {
5 var overflow: c_int = undefined;
6 const x = __muloti4(a, b, &overflow);
7 assert(overflow == expected_overflow and (expected_overflow != 0 or x == expected));
8}
9
10test "muloti4" {
11 test__muloti4(0, 0, 0, 0);
12 test__muloti4(0, 1, 0, 0);
13 test__muloti4(1, 0, 0, 0);
14 test__muloti4(0, 10, 0, 0);
15 test__muloti4(10, 0, 0, 0);
16
17 test__muloti4(0, 81985529216486895, 0, 0);
18 test__muloti4(81985529216486895, 0, 0, 0);
19
20 test__muloti4(0, -1, 0, 0);
21 test__muloti4(-1, 0, 0, 0);
22 test__muloti4(0, -10, 0, 0);
23 test__muloti4(-10, 0, 0, 0);
24 test__muloti4(0, -81985529216486895, 0, 0);
25 test__muloti4(-81985529216486895, 0, 0, 0);
26
27 test__muloti4(3037000499, 3037000499, 9223372030926249001, 0);
28 test__muloti4(-3037000499, 3037000499, -9223372030926249001, 0);
29 test__muloti4(3037000499, -3037000499, -9223372030926249001, 0);
30 test__muloti4(-3037000499, -3037000499, 9223372030926249001, 0);
31
32 test__muloti4(4398046511103, 2097152, 9223372036852678656, 0);
33 test__muloti4(-4398046511103, 2097152, -9223372036852678656, 0);
34 test__muloti4(4398046511103, -2097152, -9223372036852678656, 0);
35 test__muloti4(-4398046511103, -2097152, 9223372036852678656, 0);
36
37 test__muloti4(2097152, 4398046511103, 9223372036852678656, 0);
38 test__muloti4(-2097152, 4398046511103, -9223372036852678656, 0);
39 test__muloti4(2097152, -4398046511103, -9223372036852678656, 0);
40 test__muloti4(-2097152, -4398046511103, 9223372036852678656, 0);
41
42 test__muloti4(@bitCast(i128, u128(0x00000000000000B504F333F9DE5BE000)), @bitCast(i128, u128(0x000000000000000000B504F333F9DE5B)), @bitCast(i128, u128(0x7FFFFFFFFFFFF328DF915DA296E8A000)), 0);
43 test__muloti4(@bitCast(i128, u128(0x7FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF)), -2, @bitCast(i128, u128(0x80000000000000000000000000000001)), 1);
44 test__muloti4(-2, @bitCast(i128, u128(0x7FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF)), @bitCast(i128, u128(0x80000000000000000000000000000001)), 1);
45
46 test__muloti4(@bitCast(i128, u128(0x7FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF)), -1, @bitCast(i128, u128(0x80000000000000000000000000000001)), 0);
47 test__muloti4(-1, @bitCast(i128, u128(0x7FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF)), @bitCast(i128, u128(0x80000000000000000000000000000001)), 0);
48 test__muloti4(@bitCast(i128, u128(0x7FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF)), 0, 0, 0);
49 test__muloti4(0, @bitCast(i128, u128(0x7FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF)), 0, 0);
50 test__muloti4(@bitCast(i128, u128(0x7FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF)), 1, @bitCast(i128, u128(0x7FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF)), 0);
51 test__muloti4(1, @bitCast(i128, u128(0x7FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF)), @bitCast(i128, u128(0x7FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF)), 0);
52 test__muloti4(@bitCast(i128, u128(0x7FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF)), 2, @bitCast(i128, u128(0x80000000000000000000000000000001)), 1);
53 test__muloti4(2, @bitCast(i128, u128(0x7FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF)), @bitCast(i128, u128(0x80000000000000000000000000000001)), 1);
54
55 test__muloti4(@bitCast(i128, u128(0x80000000000000000000000000000000)), -2, @bitCast(i128, u128(0x80000000000000000000000000000000)), 1);
56 test__muloti4(-2, @bitCast(i128, u128(0x80000000000000000000000000000000)), @bitCast(i128, u128(0x80000000000000000000000000000000)), 1);
57 test__muloti4(@bitCast(i128, u128(0x80000000000000000000000000000000)), -1, @bitCast(i128, u128(0x80000000000000000000000000000000)), 1);
58 test__muloti4(-1, @bitCast(i128, u128(0x80000000000000000000000000000000)), @bitCast(i128, u128(0x80000000000000000000000000000000)), 1);
59 test__muloti4(@bitCast(i128, u128(0x80000000000000000000000000000000)), 0, 0, 0);
60 test__muloti4(0, @bitCast(i128, u128(0x80000000000000000000000000000000)), 0, 0);
61 test__muloti4(@bitCast(i128, u128(0x80000000000000000000000000000000)), 1, @bitCast(i128, u128(0x80000000000000000000000000000000)), 0);
62 test__muloti4(1, @bitCast(i128, u128(0x80000000000000000000000000000000)), @bitCast(i128, u128(0x80000000000000000000000000000000)), 0);
63 test__muloti4(@bitCast(i128, u128(0x80000000000000000000000000000000)), 2, @bitCast(i128, u128(0x80000000000000000000000000000000)), 1);
64 test__muloti4(2, @bitCast(i128, u128(0x80000000000000000000000000000000)), @bitCast(i128, u128(0x80000000000000000000000000000000)), 1);
65
66 test__muloti4(@bitCast(i128, u128(0x80000000000000000000000000000001)), -2, @bitCast(i128, u128(0x80000000000000000000000000000001)), 1);
67 test__muloti4(-2, @bitCast(i128, u128(0x80000000000000000000000000000001)), @bitCast(i128, u128(0x80000000000000000000000000000001)), 1);
68 test__muloti4(@bitCast(i128, u128(0x80000000000000000000000000000001)), -1, @bitCast(i128, u128(0x7FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF)), 0);
69 test__muloti4(-1, @bitCast(i128, u128(0x80000000000000000000000000000001)), @bitCast(i128, u128(0x7FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF)), 0);
70 test__muloti4(@bitCast(i128, u128(0x80000000000000000000000000000001)), 0, 0, 0);
71 test__muloti4(0, @bitCast(i128, u128(0x80000000000000000000000000000001)), 0, 0);
72 test__muloti4(@bitCast(i128, u128(0x80000000000000000000000000000001)), 1, @bitCast(i128, u128(0x80000000000000000000000000000001)), 0);
73 test__muloti4(1, @bitCast(i128, u128(0x80000000000000000000000000000001)), @bitCast(i128, u128(0x80000000000000000000000000000001)), 0);
74 test__muloti4(@bitCast(i128, u128(0x80000000000000000000000000000001)), 2, @bitCast(i128, u128(0x80000000000000000000000000000000)), 1);
75 test__muloti4(2, @bitCast(i128, u128(0x80000000000000000000000000000001)), @bitCast(i128, u128(0x80000000000000000000000000000000)), 1);
76}