Skip to content

Commit 26fe6ff

Browse files
committed
Add __divmodti4
1 parent bc06465 commit 26fe6ff

File tree

3 files changed

+22
-8
lines changed

3 files changed

+22
-8
lines changed

src/int/sdiv.rs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,5 +54,12 @@ intrinsics! {
5454
i128_div_rem(a, b).1
5555
}
5656

57-
// LLVM does not currently have a `__divmodti4` function
57+
// LLVM does not currently have a `__divmodti4` function, but GCC does
58+
#[maybe_use_optimized_c_shim]
59+
/// Returns `n / d` and sets `*rem = n % d`
60+
pub extern "C" fn __divmodti4(a: i128, b: i128, rem: &mut i128) -> i128 {
61+
let quo_rem = i128_div_rem(a, b);
62+
*rem = quo_rem.1;
63+
quo_rem.0
64+
}
5865
}

testcrate/build.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -805,6 +805,19 @@ fn main() {
805805
(builtins::int::sdiv::__divmodsi4(a, b, &mut r), r)
806806
}",
807807
);
808+
gen(
809+
|(a, b): (MyI128, MyI128)| {
810+
if b.0 == 0 {
811+
None
812+
} else {
813+
Some((a.0 / b.0, a.0 % b.0))
814+
}
815+
},
816+
"{
817+
let mut r = 0;
818+
(builtins::int::sdiv::__divmodti4(a, b, &mut r), r)
819+
}",
820+
);
808821
gen(
809822
|(a, b): (MyI32, MyI32)| {
810823
if b.0 == 0 {

testcrate/tests/div_rem.rs

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,9 @@
11
use rand_xoshiro::rand_core::{RngCore, SeedableRng};
22
use rand_xoshiro::Xoshiro128StarStar;
33

4-
use compiler_builtins::int::sdiv::{__divmoddi4, __divmodsi4, __divti3, __modti3};
4+
use compiler_builtins::int::sdiv::{__divmoddi4, __divmodsi4, __divmodti4};
55
use compiler_builtins::int::udiv::{__udivmoddi4, __udivmodsi4, __udivmodti4};
66

7-
// because `__divmodti4` does not exist, we synthesize it
8-
fn __divmodti4(a: i128, b: i128, rem: &mut i128) -> i128 {
9-
*rem = __modti3(a, b);
10-
__divti3(a, b)
11-
}
12-
137
/// Creates intensive test functions for division functions of a certain size
148
macro_rules! test {
159
(

0 commit comments

Comments
 (0)