Skip to content

Commit

Permalink
rv64 implement muldi3 intrinsic
Browse files Browse the repository at this point in the history
Implement the __muldi3 intrinsic to prevent infinite recursion during
multiplication on rv64 without the 'm' extension.
  • Loading branch information
johannst committed May 2, 2022
1 parent 19d53ba commit b945767
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 2 deletions.
1 change: 1 addition & 0 deletions src/int/mul.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ impl_signed_mulo!(i128_overflowing_mul, i128, u128);
intrinsics! {
#[maybe_use_optimized_c_shim]
#[arm_aeabi_alias = __aeabi_lmul]
#[cfg(not(target_arch = "riscv64"))]
pub extern "C" fn __muldi3(a: u64, b: u64) -> u64 {
a.mul(b)
}
Expand Down
4 changes: 2 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,8 @@ pub mod arm;
))]
pub mod arm_linux;

#[cfg(any(target_arch = "riscv32"))]
pub mod riscv32;
#[cfg(any(target_arch = "riscv32", target_arch = "riscv64"))]
pub mod riscv;

#[cfg(target_arch = "x86")]
pub mod x86;
Expand Down
17 changes: 17 additions & 0 deletions src/riscv32.rs → src/riscv.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
intrinsics! {
// Implementation from gcc
// https://raw.githubusercontent.com/gcc-mirror/gcc/master/libgcc/config/epiphany/mulsi3.c
#[cfg(target_arch = "riscv32")]
pub extern "C" fn __mulsi3(a: u32, b: u32) -> u32 {
let (mut a, mut b) = (a, b);
let mut r = 0;
Expand All @@ -15,4 +16,20 @@ intrinsics! {

r
}

#[cfg(target_arch = "riscv64")]
pub extern "C" fn __muldi3(a: u64, b: u64) -> u64 {
let (mut a, mut b) = (a, b);
let mut r = 0;

while a > 0 {
if a & 1 > 0 {
r += b;
}
a >>= 1;
b <<= 1;
}

r
}
}

0 comments on commit b945767

Please sign in to comment.