aboutsummaryrefslogtreecommitdiff
path: root/std
diff options
context:
space:
mode:
Diffstat (limited to 'std')
-rw-r--r--std/special/compiler_rt.zig3
-rw-r--r--std/special/compiler_rt/divti3.zig1
-rw-r--r--std/special/compiler_rt/muldi3.zig54
-rw-r--r--std/special/compiler_rt/muldi3_test.zig51
4 files changed, 108 insertions, 1 deletions
diff --git a/std/special/compiler_rt.zig b/std/special/compiler_rt.zig
index 5064e9db29..a5bbefa1db 100644
--- a/std/special/compiler_rt.zig
+++ b/std/special/compiler_rt.zig
@@ -127,6 +127,7 @@ comptime {
@export("__udivmoddi4", @import("compiler_rt/udivmoddi4.zig").__udivmoddi4, linkage);
@export("__popcountdi2", @import("compiler_rt/popcountdi2.zig").__popcountdi2, linkage);
+ @export("__muldi3", @import("compiler_rt/muldi3.zig").__muldi3, linkage);
@export("__divmoddi4", __divmoddi4, linkage);
@export("__divsi3", __divsi3, linkage);
@export("__divdi3", __divdi3, linkage);
@@ -147,6 +148,8 @@ comptime {
@export("__aeabi_unwind_cpp_pr1", __aeabi_unwind_cpp_pr1, linkage);
@export("__aeabi_unwind_cpp_pr2", __aeabi_unwind_cpp_pr2, linkage);
+ @export("__aeabi_lmul", @import("compiler_rt/muldi3.zig").__muldi3, linkage);
+
@export("__aeabi_ldivmod", __aeabi_ldivmod, linkage);
@export("__aeabi_uldivmod", __aeabi_uldivmod, linkage);
diff --git a/std/special/compiler_rt/divti3.zig b/std/special/compiler_rt/divti3.zig
index d5b2778a34..477ce2cb98 100644
--- a/std/special/compiler_rt/divti3.zig
+++ b/std/special/compiler_rt/divti3.zig
@@ -1,6 +1,5 @@
const udivmod = @import("udivmod.zig").udivmod;
const builtin = @import("builtin");
-const compiler_rt = @import("../compiler_rt.zig");
pub extern fn __divti3(a: i128, b: i128) i128 {
@setRuntimeSafety(builtin.is_test);
diff --git a/std/special/compiler_rt/muldi3.zig b/std/special/compiler_rt/muldi3.zig
new file mode 100644
index 0000000000..7a955120f5
--- /dev/null
+++ b/std/special/compiler_rt/muldi3.zig
@@ -0,0 +1,54 @@
+const builtin = @import("builtin");
+
+// Ported from
+// https://github.com/llvm/llvm-project/blob/552c2c09d354a3ad9c1c9647e0a3bb5099c31088/compiler-rt/lib/builtins/muldi3.c
+
+const dwords = extern union {
+ all: i64,
+ s: switch (builtin.endian) {
+ .Little => extern struct {
+ low: u32,
+ high: u32,
+ },
+ .Big => extern struct {
+ high: u32,
+ low: u32,
+ },
+ },
+};
+
+fn __muldsi3(a: u32, b: u32) i64 {
+ @setRuntimeSafety(builtin.is_test);
+
+ const bits_in_word_2 = @sizeOf(i32) * 8 / 2;
+ const lower_mask = (~u32(0)) >> bits_in_word_2;
+
+ var r: dwords = undefined;
+ r.s.low = (a & lower_mask) *% (b & lower_mask);
+ var t: u32 = r.s.low >> bits_in_word_2;
+ r.s.low &= lower_mask;
+ t += (a >> bits_in_word_2) *% (b & lower_mask);
+ r.s.low +%= (t & lower_mask) << bits_in_word_2;
+ r.s.high = t >> bits_in_word_2;
+ t = r.s.low >> bits_in_word_2;
+ r.s.low &= lower_mask;
+ t +%= (b >> bits_in_word_2) *% (a & lower_mask);
+ r.s.low +%= (t & lower_mask) << bits_in_word_2;
+ r.s.high +%= t >> bits_in_word_2;
+ r.s.high +%= (a >> bits_in_word_2) *% (b >> bits_in_word_2);
+ return r.all;
+}
+
+pub extern fn __muldi3(a: i64, b: i64) i64 {
+ @setRuntimeSafety(builtin.is_test);
+
+ const x = dwords{ .all = a };
+ const y = dwords{ .all = b };
+ var r = dwords{ .all = __muldsi3(x.s.low, y.s.low) };
+ r.s.high +%= x.s.high *% y.s.low +% x.s.low *% y.s.high;
+ return r.all;
+}
+
+test "import muldi3" {
+ _ = @import("muldi3_test.zig");
+}
diff --git a/std/special/compiler_rt/muldi3_test.zig b/std/special/compiler_rt/muldi3_test.zig
new file mode 100644
index 0000000000..db4daf1e1e
--- /dev/null
+++ b/std/special/compiler_rt/muldi3_test.zig
@@ -0,0 +1,51 @@
+const __muldi3 = @import("muldi3.zig").__muldi3;
+const testing = @import("std").testing;
+
+fn test__muldi3(a: i64, b: i64, expected: i64) void {
+ const x = __muldi3(a, b);
+ testing.expect(x == expected);
+}
+
+test "muldi3" {
+ test__muldi3(0, 0, 0);
+ test__muldi3(0, 1, 0);
+ test__muldi3(1, 0, 0);
+ test__muldi3(0, 10, 0);
+ test__muldi3(10, 0, 0);
+ test__muldi3(0, 81985529216486895, 0);
+ test__muldi3(81985529216486895, 0, 0);
+
+ test__muldi3(0, -1, 0);
+ test__muldi3(-1, 0, 0);
+ test__muldi3(0, -10, 0);
+ test__muldi3(-10, 0, 0);
+ test__muldi3(0, -81985529216486895, 0);
+ test__muldi3(-81985529216486895, 0, 0);
+
+ test__muldi3(1, 1, 1);
+ test__muldi3(1, 10, 10);
+ test__muldi3(10, 1, 10);
+ test__muldi3(1, 81985529216486895, 81985529216486895);
+ test__muldi3(81985529216486895, 1, 81985529216486895);
+
+ test__muldi3(1, -1, -1);
+ test__muldi3(1, -10, -10);
+ test__muldi3(-10, 1, -10);
+ test__muldi3(1, -81985529216486895, -81985529216486895);
+ test__muldi3(-81985529216486895, 1, -81985529216486895);
+
+ test__muldi3(3037000499, 3037000499, 9223372030926249001);
+ test__muldi3(-3037000499, 3037000499, -9223372030926249001);
+ test__muldi3(3037000499, -3037000499, -9223372030926249001);
+ test__muldi3(-3037000499, -3037000499, 9223372030926249001);
+
+ test__muldi3(4398046511103, 2097152, 9223372036852678656);
+ test__muldi3(-4398046511103, 2097152, -9223372036852678656);
+ test__muldi3(4398046511103, -2097152, -9223372036852678656);
+ test__muldi3(-4398046511103, -2097152, 9223372036852678656);
+
+ test__muldi3(2097152, 4398046511103, 9223372036852678656);
+ test__muldi3(-2097152, 4398046511103, -9223372036852678656);
+ test__muldi3(2097152, -4398046511103, -9223372036852678656);
+ test__muldi3(-2097152, -4398046511103, 9223372036852678656);
+}