authorgravatar for thatlemon@gmail.comLemonBoy <thatlemon@gmail.com> 2019-04-29 19:11:37+02:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2019-04-29 18:55:20-04:00
logb1a61a6d51919cb8d5d87c53d495406491c53ebf
tree59ca2f376d40c6b4f798abb17394a48aeaad1383
parentd2328ac71a927df3ce9b7cec09c90dbb04ec3018

compiler-rt: Add __mulodi4


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

CMakeLists.txt+1
...@@ -676,6 +676,7 @@ set(ZIG_STD_FILES...@@ -676,6 +676,7 @@ set(ZIG_STD_FILES
676 "special/compiler_rt/modti3.zig"676 "special/compiler_rt/modti3.zig"
677 "special/compiler_rt/mulXf3.zig"677 "special/compiler_rt/mulXf3.zig"
678 "special/compiler_rt/muloti4.zig"678 "special/compiler_rt/muloti4.zig"
679 "special/compiler_rt/mulodi4.zig"
679 "special/compiler_rt/multi3.zig"680 "special/compiler_rt/multi3.zig"
680 "special/compiler_rt/negXf2.zig"681 "special/compiler_rt/negXf2.zig"
681 "special/compiler_rt/popcountdi2.zig"682 "special/compiler_rt/popcountdi2.zig"
std/special/compiler_rt.zig+1
...@@ -221,6 +221,7 @@ comptime {...@@ -221,6 +221,7 @@ comptime {
221 @export("__umodti3", @import("compiler_rt/umodti3.zig").__umodti3, linkage);221 @export("__umodti3", @import("compiler_rt/umodti3.zig").__umodti3, linkage);
222 }222 }
223 @export("__muloti4", @import("compiler_rt/muloti4.zig").__muloti4, linkage);223 @export("__muloti4", @import("compiler_rt/muloti4.zig").__muloti4, linkage);
224 @export("__mulodi4", @import("compiler_rt/mulodi4.zig").__mulodi4, linkage);
224}225}
225226
226const std = @import("std");227const std = @import("std");
std/special/compiler_rt/mulodi4.zig created+44
...@@ -0,0 +1,44 @@
1const builtin = @import("builtin");
2const compiler_rt = @import("../compiler_rt.zig");
3const maxInt = std.math.maxInt;
4const minInt = std.math.minInt;
5
6pub extern fn __mulodi4(a: i64, b: i64, overflow: *c_int) i64 {
7 @setRuntimeSafety(builtin.is_test);
8
9 const min = @bitCast(i64, u64(1 << (i64.bit_count - 1)));
10 const max = ~min;
11
12 overflow.* = 0;
13 const result = a *% b;
14
15 // Edge cases
16 if (a == min) {
17 if (b != 0 and b != 1) overflow.* = 1;
18 return result;
19 }
20 if (b == min) {
21 if (a != 0 and a != 1) overflow.* = 1;
22 return result;
23 }
24
25 // Take absolute value of a and b via abs(x) = (x^(x >> 63)) - (x >> 63).
26 const abs_a = (a ^ (a >> 63)) -% (a >> 63);
27 const abs_b = (b ^ (b >> 63)) -% (b >> 63);
28
29 // Unitary magnitude, cannot have overflow
30 if (abs_a < 2 or abs_b < 2) return result;
31
32 // Compare the signs of the operands
33 if ((a ^ b) >> 63 != 0) {
34 if (abs_a > @divTrunc(max, abs_b)) overflow.* = 1;
35 } else {
36 if (abs_a > @divTrunc(min, -abs_b)) overflow.* = 1;
37 }
38
39 return result;
40}
41
42test "import mulodi4" {
43 _ = @import("mulodi4_test.zig");
44}
std/special/compiler_rt/mulodi4_test.zig created+85
...@@ -0,0 +1,85 @@
1const __mulodi4 = @import("mulodi4.zig").__mulodi4;
2const testing = @import("std").testing;
3
4fn test__mulodi4(a: i64, b: i64, expected: i64, expected_overflow: c_int) void {
5 var overflow: c_int = undefined;
6 const x = __mulodi4(a, b, &overflow);
7 testing.expect(overflow == expected_overflow and (expected_overflow != 0 or x == expected));
8}
9
10test "mulodi4" {
11 test__mulodi4(0, 0, 0, 0);
12 test__mulodi4(0, 1, 0, 0);
13 test__mulodi4(1, 0, 0, 0);
14 test__mulodi4(0, 10, 0, 0);
15 test__mulodi4(10, 0, 0, 0);
16 test__mulodi4(0, 81985529216486895, 0, 0);
17 test__mulodi4(81985529216486895, 0, 0, 0);
18
19 test__mulodi4(0, -1, 0, 0);
20 test__mulodi4(-1, 0, 0, 0);
21 test__mulodi4(0, -10, 0, 0);
22 test__mulodi4(-10, 0, 0, 0);
23 test__mulodi4(0, -81985529216486895, 0, 0);
24 test__mulodi4(-81985529216486895, 0, 0, 0);
25
26 test__mulodi4(1, 1, 1, 0);
27 test__mulodi4(1, 10, 10, 0);
28 test__mulodi4(10, 1, 10, 0);
29 test__mulodi4(1, 81985529216486895, 81985529216486895, 0);
30 test__mulodi4(81985529216486895, 1, 81985529216486895, 0);
31
32 test__mulodi4(1, -1, -1, 0);
33 test__mulodi4(1, -10, -10, 0);
34 test__mulodi4(-10, 1, -10, 0);
35 test__mulodi4(1, -81985529216486895, -81985529216486895, 0);
36 test__mulodi4(-81985529216486895, 1, -81985529216486895, 0);
37
38 test__mulodi4(3037000499, 3037000499, 9223372030926249001, 0);
39 test__mulodi4(-3037000499, 3037000499, -9223372030926249001, 0);
40 test__mulodi4(3037000499, -3037000499, -9223372030926249001, 0);
41 test__mulodi4(-3037000499, -3037000499, 9223372030926249001, 0);
42
43 test__mulodi4(4398046511103, 2097152, 9223372036852678656, 0);
44 test__mulodi4(-4398046511103, 2097152, -9223372036852678656, 0);
45 test__mulodi4(4398046511103, -2097152, -9223372036852678656, 0);
46 test__mulodi4(-4398046511103, -2097152, 9223372036852678656, 0);
47
48 test__mulodi4(2097152, 4398046511103, 9223372036852678656, 0);
49 test__mulodi4(-2097152, 4398046511103, -9223372036852678656, 0);
50 test__mulodi4(2097152, -4398046511103, -9223372036852678656, 0);
51 test__mulodi4(-2097152, -4398046511103, 9223372036852678656, 0);
52
53 test__mulodi4(0x7FFFFFFFFFFFFFFF, -2, 2, 1);
54 test__mulodi4(-2, 0x7FFFFFFFFFFFFFFF, 2, 1);
55 test__mulodi4(0x7FFFFFFFFFFFFFFF, -1, @bitCast(i64, u64(0x8000000000000001)), 0);
56 test__mulodi4(-1, 0x7FFFFFFFFFFFFFFF, @bitCast(i64, u64(0x8000000000000001)), 0);
57 test__mulodi4(0x7FFFFFFFFFFFFFFF, 0, 0, 0);
58 test__mulodi4(0, 0x7FFFFFFFFFFFFFFF, 0, 0);
59 test__mulodi4(0x7FFFFFFFFFFFFFFF, 1, 0x7FFFFFFFFFFFFFFF, 0);
60 test__mulodi4(1, 0x7FFFFFFFFFFFFFFF, 0x7FFFFFFFFFFFFFFF, 0);
61 test__mulodi4(0x7FFFFFFFFFFFFFFF, 2, @bitCast(i64, u64(0x8000000000000001)), 1);
62 test__mulodi4(2, 0x7FFFFFFFFFFFFFFF, @bitCast(i64, u64(0x8000000000000001)), 1);
63
64 test__mulodi4(@bitCast(i64, u64(0x8000000000000000)), -2, @bitCast(i64, u64(0x8000000000000000)), 1);
65 test__mulodi4(-2, @bitCast(i64, u64(0x8000000000000000)), @bitCast(i64, u64(0x8000000000000000)), 1);
66 test__mulodi4(@bitCast(i64, u64(0x8000000000000000)), -1, @bitCast(i64, u64(0x8000000000000000)), 1);
67 test__mulodi4(-1, @bitCast(i64, u64(0x8000000000000000)), @bitCast(i64, u64(0x8000000000000000)), 1);
68 test__mulodi4(@bitCast(i64, u64(0x8000000000000000)), 0, 0, 0);
69 test__mulodi4(0, @bitCast(i64, u64(0x8000000000000000)), 0, 0);
70 test__mulodi4(@bitCast(i64, u64(0x8000000000000000)), 1, @bitCast(i64, u64(0x8000000000000000)), 0);
71 test__mulodi4(1, @bitCast(i64, u64(0x8000000000000000)), @bitCast(i64, u64(0x8000000000000000)), 0);
72 test__mulodi4(@bitCast(i64, u64(0x8000000000000000)), 2, @bitCast(i64, u64(0x8000000000000000)), 1);
73 test__mulodi4(2, @bitCast(i64, u64(0x8000000000000000)), @bitCast(i64, u64(0x8000000000000000)), 1);
74
75 test__mulodi4(@bitCast(i64, u64(0x8000000000000001)), -2, @bitCast(i64, u64(0x8000000000000001)), 1);
76 test__mulodi4(-2, @bitCast(i64, u64(0x8000000000000001)), @bitCast(i64, u64(0x8000000000000001)), 1);
77 test__mulodi4(@bitCast(i64, u64(0x8000000000000001)), -1, 0x7FFFFFFFFFFFFFFF, 0);
78 test__mulodi4(-1, @bitCast(i64, u64(0x8000000000000001)), 0x7FFFFFFFFFFFFFFF, 0);
79 test__mulodi4(@bitCast(i64, u64(0x8000000000000001)), 0, 0, 0);
80 test__mulodi4(0, @bitCast(i64, u64(0x8000000000000001)), 0, 0);
81 test__mulodi4(@bitCast(i64, u64(0x8000000000000001)), 1, @bitCast(i64, u64(0x8000000000000001)), 0);
82 test__mulodi4(1, @bitCast(i64, u64(0x8000000000000001)), @bitCast(i64, u64(0x8000000000000001)), 0);
83 test__mulodi4(@bitCast(i64, u64(0x8000000000000001)), 2, @bitCast(i64, u64(0x8000000000000000)), 1);
84 test__mulodi4(2, @bitCast(i64, u64(0x8000000000000001)), @bitCast(i64, u64(0x8000000000000000)), 1);
85}