Skip to content

Commit b945767

Browse files
committed
rv64 implement muldi3 intrinsic
Implement the __muldi3 intrinsic to prevent infinite recursion during multiplication on rv64 without the 'm' extension.
1 parent 19d53ba commit b945767

File tree

3 files changed

+20
-2
lines changed

3 files changed

+20
-2
lines changed

src/int/mul.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,7 @@ impl_signed_mulo!(i128_overflowing_mul, i128, u128);
100100
intrinsics! {
101101
#[maybe_use_optimized_c_shim]
102102
#[arm_aeabi_alias = __aeabi_lmul]
103+
#[cfg(not(target_arch = "riscv64"))]
103104
pub extern "C" fn __muldi3(a: u64, b: u64) -> u64 {
104105
a.mul(b)
105106
}

src/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,8 @@ pub mod arm;
6060
))]
6161
pub mod arm_linux;
6262

63-
#[cfg(any(target_arch = "riscv32"))]
64-
pub mod riscv32;
63+
#[cfg(any(target_arch = "riscv32", target_arch = "riscv64"))]
64+
pub mod riscv;
6565

6666
#[cfg(target_arch = "x86")]
6767
pub mod x86;

src/riscv32.rs renamed to src/riscv.rs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
intrinsics! {
22
// Implementation from gcc
33
// https://raw.githubusercontent.com/gcc-mirror/gcc/master/libgcc/config/epiphany/mulsi3.c
4+
#[cfg(target_arch = "riscv32")]
45
pub extern "C" fn __mulsi3(a: u32, b: u32) -> u32 {
56
let (mut a, mut b) = (a, b);
67
let mut r = 0;
@@ -15,4 +16,20 @@ intrinsics! {
1516

1617
r
1718
}
19+
20+
#[cfg(target_arch = "riscv64")]
21+
pub extern "C" fn __muldi3(a: u64, b: u64) -> u64 {
22+
let (mut a, mut b) = (a, b);
23+
let mut r = 0;
24+
25+
while a > 0 {
26+
if a & 1 > 0 {
27+
r += b;
28+
}
29+
a >>= 1;
30+
b <<= 1;
31+
}
32+
33+
r
34+
}
1835
}

0 commit comments

Comments
 (0)