Skip to content

Commit 2219cc0

Browse files
authoredJun 14, 2018
Merge pull request #1105 from ziglang/i128-compiler-rt
Add i128 compiler-rt div/mul support
·
0.15.20.3.0
2 parents 4ec09ac + a369d69 commit 2219cc0

File tree

6 files changed

+184
-0
lines changed

6 files changed

+184
-0
lines changed
 

‎CMakeLists.txt‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -556,6 +556,7 @@ set(ZIG_STD_FILES
556556
"special/compiler_rt/aulldiv.zig"
557557
"special/compiler_rt/aullrem.zig"
558558
"special/compiler_rt/comparetf2.zig"
559+
"special/compiler_rt/divti3.zig"
559560
"special/compiler_rt/fixuint.zig"
560561
"special/compiler_rt/fixunsdfdi.zig"
561562
"special/compiler_rt/fixunsdfsi.zig"
@@ -566,6 +567,7 @@ set(ZIG_STD_FILES
566567
"special/compiler_rt/fixunstfdi.zig"
567568
"special/compiler_rt/fixunstfsi.zig"
568569
"special/compiler_rt/fixunstfti.zig"
570+
"special/compiler_rt/muloti4.zig"
569571
"special/compiler_rt/index.zig"
570572
"special/compiler_rt/udivmod.zig"
571573
"special/compiler_rt/udivmoddi4.zig"

‎std/special/compiler_rt/divti3.zig‎

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
const udivmod = @import("udivmod.zig").udivmod;
2+
const builtin = @import("builtin");
3+
const compiler_rt = @import("index.zig");
4+
5+
pub 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+
19+
pub 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+
24+
test "import divti3" {
25+
_ = @import("divti3_test.zig");
26+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
const __divti3 = @import("divti3.zig").__divti3;
2+
const assert = @import("std").debug.assert;
3+
4+
fn test__divti3(a: i128, b: i128, expected: i128) void {
5+
const x = __divti3(a, b);
6+
assert(x == expected);
7+
}
8+
9+
test "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‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,13 +58,17 @@ comptime {
5858
@export("__chkstk", __chkstk, strong_linkage);
5959
@export("___chkstk_ms", ___chkstk_ms, linkage);
6060
}
61+
@export("__divti3", @import("divti3.zig").__divti3_windows_x86_64, linkage);
62+
@export("__muloti4", @import("muloti4.zig").__muloti4_windows_x86_64, linkage);
6163
@export("__udivti3", @import("udivti3.zig").__udivti3_windows_x86_64, linkage);
6264
@export("__udivmodti4", @import("udivmodti4.zig").__udivmodti4_windows_x86_64, linkage);
6365
@export("__umodti3", @import("umodti3.zig").__umodti3_windows_x86_64, linkage);
6466
},
6567
else => {},
6668
}
6769
} else {
70+
@export("__divti3", @import("divti3.zig").__divti3, linkage);
71+
@export("__muloti4", @import("muloti4.zig").__muloti4, linkage);
6872
@export("__udivti3", @import("udivti3.zig").__udivti3, linkage);
6973
@export("__udivmodti4", @import("udivmodti4.zig").__udivmodti4, linkage);
7074
@export("__umodti3", @import("umodti3.zig").__umodti3, linkage);
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
const udivmod = @import("udivmod.zig").udivmod;
2+
const builtin = @import("builtin");
3+
const compiler_rt = @import("index.zig");
4+
5+
pub 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+
48+
pub 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+
53+
test "import muloti4" {
54+
_ = @import("muloti4_test.zig");
55+
}
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
const __muloti4 = @import("muloti4.zig").__muloti4;
2+
const assert = @import("std").debug.assert;
3+
4+
fn 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+
10+
test "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+
}

0 commit comments

Comments
 (0)
Please sign in to comment.