From 10971912e87e0ab10e1f7c6e9da8086f11ced68a Mon Sep 17 00:00:00 2001 From: Johannes Stoelp Date: Mon, 2 May 2022 23:00:12 +0200 Subject: [PATCH] rv64 implement muldi3 intrinsic Implement the __muldi3 intrinsic to prevent infinite recursion during multiplication on rv64 without the 'm' extension. --- library/compiler-builtins/src/int/mul.rs | 1 + library/compiler-builtins/src/lib.rs | 4 ++-- .../src/{riscv32.rs => riscv.rs} | 17 +++++++++++++++++ 3 files changed, 20 insertions(+), 2 deletions(-) rename library/compiler-builtins/src/{riscv32.rs => riscv.rs} (54%) diff --git a/library/compiler-builtins/src/int/mul.rs b/library/compiler-builtins/src/int/mul.rs index a5238eeac70e..37eb03ec054f 100644 --- a/library/compiler-builtins/src/int/mul.rs +++ b/library/compiler-builtins/src/int/mul.rs @@ -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) } diff --git a/library/compiler-builtins/src/lib.rs b/library/compiler-builtins/src/lib.rs index 9ca72bc20f4e..009923d27e5b 100644 --- a/library/compiler-builtins/src/lib.rs +++ b/library/compiler-builtins/src/lib.rs @@ -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; diff --git a/library/compiler-builtins/src/riscv32.rs b/library/compiler-builtins/src/riscv.rs similarity index 54% rename from library/compiler-builtins/src/riscv32.rs rename to library/compiler-builtins/src/riscv.rs index 9a3c1714c1f0..d9a65e200745 100644 --- a/library/compiler-builtins/src/riscv32.rs +++ b/library/compiler-builtins/src/riscv.rs @@ -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; @@ -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 + } }