authorgravatar for thatlemon@gmail.comLemonBoy <thatlemon@gmail.com> 2019-04-29 09:01:57+02:00
committergravatar for thatlemon@gmail.comLemonBoy <thatlemon@gmail.com> 2019-04-29 19:17:48+02:00
log4e241263ff0081a756d4d331f1e78e571342bea4
treecceafbc2c3820080653633aac1d1c279f42d180c
parent738562e99071e31bf3a7d9227d34415252556737

compiler-rt: Add __divmodsi4, __aeabi_idivmod


1 files changed, 39 insertions(+), 1 deletions(-)

std/special/compiler_rt.zig+39-1
......@@ -106,6 +106,7 @@ comptime {
106106 @export("__udivsi3", __udivsi3, linkage);
107107 @export("__udivdi3", __udivdi3, linkage);
108108 @export("__umoddi3", __umoddi3, linkage);
109 @export("__divmodsi4", __divmodsi4, linkage);
109110 @export("__udivmodsi4", __udivmodsi4, linkage);
110111
111112 @export("__negsf2", @import("compiler_rt/negXf2.zig").__negsf2, linkage);
......@@ -113,10 +114,11 @@ comptime {
113114
114115 if (is_arm_arch and !is_arm_64) {
115116 @export("__aeabi_uldivmod", __aeabi_uldivmod, linkage);
116 @export("__aeabi_uidivmod", __aeabi_uidivmod, linkage);
117117
118118 @export("__aeabi_idiv", __divsi3, linkage);
119 @export("__aeabi_idivmod", __divmodsi4, linkage);
119120 @export("__aeabi_uidiv", __udivsi3, linkage);
121 @export("__aeabi_uidivmod", __aeabi_uidivmod, linkage);
120122
121123 @export("__aeabi_memcpy", __aeabi_memcpy, linkage);
122124 @export("__aeabi_memcpy4", __aeabi_memcpy, linkage);
......@@ -561,6 +563,14 @@ nakedcc fn ___chkstk_ms() align(4) void {
561563 );
562564}
563565
566extern fn __divmodsi4(a: i32, b: i32, rem: *i32) i32 {
567 @setRuntimeSafety(is_test);
568
569 const d = __divsi3(a, b);
570 rem.* = a -% (d * b);
571 return d;
572}
573
564574extern fn __udivmodsi4(a: u32, b: u32, rem: *u32) u32 {
565575 @setRuntimeSafety(is_test);
566576
......@@ -1335,3 +1345,31 @@ fn test_one_divsi3(a: i32, b: i32, expected_q: i32) void {
13351345 const q: i32 = __divsi3(a, b);
13361346 testing.expect(q == expected_q);
13371347}
1348
1349test "test_divmodsi4" {
1350 const cases = [][4]i32{
1351 []i32{ 0, 1, 0, 0},
1352 []i32{ 0, -1, 0, 0},
1353 []i32{ 2, 1, 2, 0},
1354 []i32{ 2, -1, -2, 0},
1355 []i32{-2, 1, -2, 0},
1356 []i32{-2, -1, 2, 0},
1357 []i32{ 7, 5, 1, 2},
1358 []i32{-7, 5, -1, -2},
1359 []i32{19, 5, 3, 4},
1360 []i32{19, -5, -3, 4},
1361
1362 []i32{@bitCast(i32, u32(0x80000000)), 8, @bitCast(i32, u32(0xf0000000)), 0},
1363 []i32{@bitCast(i32, u32(0x80000007)), 8, @bitCast(i32, u32(0xf0000001)), -1},
1364 };
1365
1366 for (cases) |case| {
1367 test_one_divmodsi4(case[0], case[1], case[2], case[3]);
1368 }
1369}
1370
1371fn test_one_divmodsi4(a: i32, b: i32, expected_q: i32, expected_r: i32) void {
1372 var r: i32 = undefined;
1373 const q: i32 = __divmodsi4(a, b, &r);
1374 testing.expect(q == expected_q and r == expected_r);
1375}