authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2019-07-18 13:54:22-04:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2019-07-22 12:49:26-04:00
log16be70cbbf8e6dc658b9fcacd3366df8f83fffa8
tree27dd2a2a5db292ed20e4722b66e39620a77ca8c9
parentbc31c1280e012377c3a2b00356a5876919a72775
signaturelock-open Commit is signed but in an unrecognized format.

compiler-rt: add __muldi3


6 files changed, 116 insertions(+), 5 deletions(-)

build.zig+6-3
...@@ -122,16 +122,19 @@ pub fn build(b: *Builder) !void {...@@ -122,16 +122,19 @@ pub fn build(b: *Builder) !void {
122 }122 }
123 const modes = chosen_modes[0..chosen_mode_index];123 const modes = chosen_modes[0..chosen_mode_index];
124124
125 const multi_and_single = [_]bool{ false, true };
126 const just_multi = [_]bool{false};
127
125 // run stage1 `zig fmt` on this build.zig file just to make sure it works128 // run stage1 `zig fmt` on this build.zig file just to make sure it works
126 test_step.dependOn(&fmt_build_zig.step);129 test_step.dependOn(&fmt_build_zig.step);
127 const fmt_step = b.step("test-fmt", "Run zig fmt against build.zig to make sure it works");130 const fmt_step = b.step("test-fmt", "Run zig fmt against build.zig to make sure it works");
128 fmt_step.dependOn(&fmt_build_zig.step);131 fmt_step.dependOn(&fmt_build_zig.step);
129132
130 test_step.dependOn(tests.addPkgTests(b, test_filter, "test/stage1/behavior.zig", "behavior", "Run the behavior tests", modes, skip_non_native));133 test_step.dependOn(tests.addPkgTests(b, test_filter, "test/stage1/behavior.zig", "behavior", "Run the behavior tests", modes, multi_and_single, skip_non_native));
131134
132 test_step.dependOn(tests.addPkgTests(b, test_filter, "std/std.zig", "std", "Run the standard library tests", modes, skip_non_native));135 test_step.dependOn(tests.addPkgTests(b, test_filter, "std/std.zig", "std", "Run the standard library tests", modes, multi_and_single, skip_non_native));
133136
134 test_step.dependOn(tests.addPkgTests(b, test_filter, "std/special/compiler_rt.zig", "compiler-rt", "Run the compiler_rt tests", modes, skip_non_native));137 test_step.dependOn(tests.addPkgTests(b, test_filter, "std/special/compiler_rt.zig", "compiler-rt", "Run the compiler_rt tests", modes, just_multi, skip_non_native));
135138
136 test_step.dependOn(tests.addCompareOutputTests(b, test_filter, modes));139 test_step.dependOn(tests.addCompareOutputTests(b, test_filter, modes));
137 test_step.dependOn(tests.addStandaloneTests(b, test_filter, modes));140 test_step.dependOn(tests.addStandaloneTests(b, test_filter, modes));
std/special/compiler_rt.zig+3
...@@ -127,6 +127,7 @@ comptime {...@@ -127,6 +127,7 @@ comptime {
127 @export("__udivmoddi4", @import("compiler_rt/udivmoddi4.zig").__udivmoddi4, linkage);127 @export("__udivmoddi4", @import("compiler_rt/udivmoddi4.zig").__udivmoddi4, linkage);
128 @export("__popcountdi2", @import("compiler_rt/popcountdi2.zig").__popcountdi2, linkage);128 @export("__popcountdi2", @import("compiler_rt/popcountdi2.zig").__popcountdi2, linkage);
129129
130 @export("__muldi3", @import("compiler_rt/muldi3.zig").__muldi3, linkage);
130 @export("__divmoddi4", __divmoddi4, linkage);131 @export("__divmoddi4", __divmoddi4, linkage);
131 @export("__divsi3", __divsi3, linkage);132 @export("__divsi3", __divsi3, linkage);
132 @export("__divdi3", __divdi3, linkage);133 @export("__divdi3", __divdi3, linkage);
...@@ -147,6 +148,8 @@ comptime {...@@ -147,6 +148,8 @@ comptime {
147 @export("__aeabi_unwind_cpp_pr1", __aeabi_unwind_cpp_pr1, linkage);148 @export("__aeabi_unwind_cpp_pr1", __aeabi_unwind_cpp_pr1, linkage);
148 @export("__aeabi_unwind_cpp_pr2", __aeabi_unwind_cpp_pr2, linkage);149 @export("__aeabi_unwind_cpp_pr2", __aeabi_unwind_cpp_pr2, linkage);
149150
151 @export("__aeabi_lmul", @import("compiler_rt/muldi3.zig").__muldi3, linkage);
152
150 @export("__aeabi_ldivmod", __aeabi_ldivmod, linkage);153 @export("__aeabi_ldivmod", __aeabi_ldivmod, linkage);
151 @export("__aeabi_uldivmod", __aeabi_uldivmod, linkage);154 @export("__aeabi_uldivmod", __aeabi_uldivmod, linkage);
152155
std/special/compiler_rt/divti3.zig-1
...@@ -1,6 +1,5 @@...@@ -1,6 +1,5 @@
1const udivmod = @import("udivmod.zig").udivmod;1const udivmod = @import("udivmod.zig").udivmod;
2const builtin = @import("builtin");2const builtin = @import("builtin");
3const compiler_rt = @import("../compiler_rt.zig");
43
5pub extern fn __divti3(a: i128, b: i128) i128 {4pub extern fn __divti3(a: i128, b: i128) i128 {
6 @setRuntimeSafety(builtin.is_test);5 @setRuntimeSafety(builtin.is_test);
std/special/compiler_rt/muldi3.zig created+54
...@@ -0,0 +1,54 @@
1const builtin = @import("builtin");
2
3// Ported from
4// https://github.com/llvm/llvm-project/blob/552c2c09d354a3ad9c1c9647e0a3bb5099c31088/compiler-rt/lib/builtins/muldi3.c
5
6const dwords = extern union {
7 all: i64,
8 s: switch (builtin.endian) {
9 .Little => extern struct {
10 low: u32,
11 high: u32,
12 },
13 .Big => extern struct {
14 high: u32,
15 low: u32,
16 },
17 },
18};
19
20fn __muldsi3(a: u32, b: u32) i64 {
21 @setRuntimeSafety(builtin.is_test);
22
23 const bits_in_word_2 = @sizeOf(i32) * 8 / 2;
24 const lower_mask = (~u32(0)) >> bits_in_word_2;
25
26 var r: dwords = undefined;
27 r.s.low = (a & lower_mask) *% (b & lower_mask);
28 var t: u32 = r.s.low >> bits_in_word_2;
29 r.s.low &= lower_mask;
30 t += (a >> bits_in_word_2) *% (b & lower_mask);
31 r.s.low +%= (t & lower_mask) << bits_in_word_2;
32 r.s.high = t >> bits_in_word_2;
33 t = r.s.low >> bits_in_word_2;
34 r.s.low &= lower_mask;
35 t +%= (b >> bits_in_word_2) *% (a & lower_mask);
36 r.s.low +%= (t & lower_mask) << bits_in_word_2;
37 r.s.high +%= t >> bits_in_word_2;
38 r.s.high +%= (a >> bits_in_word_2) *% (b >> bits_in_word_2);
39 return r.all;
40}
41
42pub extern fn __muldi3(a: i64, b: i64) i64 {
43 @setRuntimeSafety(builtin.is_test);
44
45 const x = dwords{ .all = a };
46 const y = dwords{ .all = b };
47 var r = dwords{ .all = __muldsi3(x.s.low, y.s.low) };
48 r.s.high +%= x.s.high *% y.s.low +% x.s.low *% y.s.high;
49 return r.all;
50}
51
52test "import muldi3" {
53 _ = @import("muldi3_test.zig");
54}
std/special/compiler_rt/muldi3_test.zig created+51
...@@ -0,0 +1,51 @@
1const __muldi3 = @import("muldi3.zig").__muldi3;
2const testing = @import("std").testing;
3
4fn test__muldi3(a: i64, b: i64, expected: i64) void {
5 const x = __muldi3(a, b);
6 testing.expect(x == expected);
7}
8
9test "muldi3" {
10 test__muldi3(0, 0, 0);
11 test__muldi3(0, 1, 0);
12 test__muldi3(1, 0, 0);
13 test__muldi3(0, 10, 0);
14 test__muldi3(10, 0, 0);
15 test__muldi3(0, 81985529216486895, 0);
16 test__muldi3(81985529216486895, 0, 0);
17
18 test__muldi3(0, -1, 0);
19 test__muldi3(-1, 0, 0);
20 test__muldi3(0, -10, 0);
21 test__muldi3(-10, 0, 0);
22 test__muldi3(0, -81985529216486895, 0);
23 test__muldi3(-81985529216486895, 0, 0);
24
25 test__muldi3(1, 1, 1);
26 test__muldi3(1, 10, 10);
27 test__muldi3(10, 1, 10);
28 test__muldi3(1, 81985529216486895, 81985529216486895);
29 test__muldi3(81985529216486895, 1, 81985529216486895);
30
31 test__muldi3(1, -1, -1);
32 test__muldi3(1, -10, -10);
33 test__muldi3(-10, 1, -10);
34 test__muldi3(1, -81985529216486895, -81985529216486895);
35 test__muldi3(-81985529216486895, 1, -81985529216486895);
36
37 test__muldi3(3037000499, 3037000499, 9223372030926249001);
38 test__muldi3(-3037000499, 3037000499, -9223372030926249001);
39 test__muldi3(3037000499, -3037000499, -9223372030926249001);
40 test__muldi3(-3037000499, -3037000499, 9223372030926249001);
41
42 test__muldi3(4398046511103, 2097152, 9223372036852678656);
43 test__muldi3(-4398046511103, 2097152, -9223372036852678656);
44 test__muldi3(4398046511103, -2097152, -9223372036852678656);
45 test__muldi3(-4398046511103, -2097152, 9223372036852678656);
46
47 test__muldi3(2097152, 4398046511103, 9223372036852678656);
48 test__muldi3(-2097152, 4398046511103, -9223372036852678656);
49 test__muldi3(2097152, -4398046511103, -9223372036852678656);
50 test__muldi3(-2097152, -4398046511103, 9223372036852678656);
51}
test/tests.zig+2-1
...@@ -170,6 +170,7 @@ pub fn addPkgTests(...@@ -170,6 +170,7 @@ pub fn addPkgTests(
170 name: []const u8,170 name: []const u8,
171 desc: []const u8,171 desc: []const u8,
172 modes: []const Mode,172 modes: []const Mode,
173 single_threaded_list: []const bool,
173 skip_non_native: bool,174 skip_non_native: bool,
174) *build.Step {175) *build.Step {
175 const step = b.step(b.fmt("test-{}", name), desc);176 const step = b.step(b.fmt("test-{}", name), desc);
...@@ -179,7 +180,7 @@ pub fn addPkgTests(...@@ -179,7 +180,7 @@ pub fn addPkgTests(
179 continue;180 continue;
180 for (modes) |mode| {181 for (modes) |mode| {
181 for ([_]bool{ false, true }) |link_libc| {182 for ([_]bool{ false, true }) |link_libc| {
182 for ([_]bool{ false, true }) |single_threaded| {183 for (single_threaded_list) |single_threaded| {
183 if (link_libc and !is_native) {184 if (link_libc and !is_native) {
184 // don't assume we have a cross-compiling libc set up185 // don't assume we have a cross-compiling libc set up
185 continue;186 continue;