From 834bacfe5e125cf6576587b1701564edf704eb52 Mon Sep 17 00:00:00 2001 From: bjorn3 <17426603+bjorn3@users.noreply.github.com> Date: Thu, 9 Jan 2025 13:25:48 +0100 Subject: [PATCH 1/2] Remove some unused rust specific intrinsics I think they were originally introduced when 128bit int support was added to rustc but before LLVM consistently supported 128bit int operations across platforms. Nowadays LLVM and GCC handle 128bit int operations for us rather than manually adding calls to rust specific intrinsics. The only exception is the Cranelift backend which still uses the __rust_u/i128_mulo intrinsics and as such this commit keeps those around. --- src/int/addsub.rs | 96 --------------------------------------- src/int/mod.rs | 1 - testcrate/tests/addsub.rs | 75 ------------------------------ 3 files changed, 172 deletions(-) delete mode 100644 src/int/addsub.rs diff --git a/src/int/addsub.rs b/src/int/addsub.rs deleted file mode 100644 index e95590d84..000000000 --- a/src/int/addsub.rs +++ /dev/null @@ -1,96 +0,0 @@ -use crate::int::{DInt, Int, MinInt}; - -trait UAddSub: DInt + Int { - fn uadd(self, other: Self) -> Self { - let (lo, carry) = self.lo().overflowing_add(other.lo()); - let hi = self.hi().wrapping_add(other.hi()); - let carry = if carry { Self::H::ONE } else { Self::H::ZERO }; - Self::from_lo_hi(lo, hi.wrapping_add(carry)) - } - fn uadd_one(self) -> Self { - let (lo, carry) = self.lo().overflowing_add(Self::H::ONE); - let carry = if carry { Self::H::ONE } else { Self::H::ZERO }; - Self::from_lo_hi(lo, self.hi().wrapping_add(carry)) - } - fn usub(self, other: Self) -> Self { - let uneg = (!other).uadd_one(); - self.uadd(uneg) - } -} - -impl UAddSub for u128 {} - -trait AddSub: Int -where - ::UnsignedInt: UAddSub, -{ - fn add(self, other: Self) -> Self { - Self::from_unsigned(self.unsigned().uadd(other.unsigned())) - } - fn sub(self, other: Self) -> Self { - Self::from_unsigned(self.unsigned().usub(other.unsigned())) - } -} - -impl AddSub for u128 {} -impl AddSub for i128 {} - -trait Addo: AddSub -where - ::UnsignedInt: UAddSub, -{ - fn addo(self, other: Self) -> (Self, bool) { - let sum = AddSub::add(self, other); - (sum, (other < Self::ZERO) != (sum < self)) - } -} - -impl Addo for i128 {} -impl Addo for u128 {} - -trait Subo: AddSub -where - ::UnsignedInt: UAddSub, -{ - fn subo(self, other: Self) -> (Self, bool) { - let sum = AddSub::sub(self, other); - (sum, (other < Self::ZERO) != (self < sum)) - } -} - -impl Subo for i128 {} -impl Subo for u128 {} - -intrinsics! { - pub extern "C" fn __rust_i128_add(a: i128, b: i128) -> i128 { - AddSub::add(a,b) - } - - pub extern "C" fn __rust_i128_addo(a: i128, b: i128) -> (i128, bool) { - a.addo(b) - } - - pub extern "C" fn __rust_u128_add(a: u128, b: u128) -> u128 { - AddSub::add(a,b) - } - - pub extern "C" fn __rust_u128_addo(a: u128, b: u128) -> (u128, bool) { - a.addo(b) - } - - pub extern "C" fn __rust_i128_sub(a: i128, b: i128) -> i128 { - AddSub::sub(a,b) - } - - pub extern "C" fn __rust_i128_subo(a: i128, b: i128) -> (i128, bool) { - a.subo(b) - } - - pub extern "C" fn __rust_u128_sub(a: u128, b: u128) -> u128 { - AddSub::sub(a,b) - } - - pub extern "C" fn __rust_u128_subo(a: u128, b: u128) -> (u128, bool) { - a.subo(b) - } -} diff --git a/src/int/mod.rs b/src/int/mod.rs index c0d5a6715..a5261134a 100644 --- a/src/int/mod.rs +++ b/src/int/mod.rs @@ -2,7 +2,6 @@ use core::ops; mod specialized_div_rem; -pub mod addsub; mod big; pub mod bswap; pub mod leading_zeros; diff --git a/testcrate/tests/addsub.rs b/testcrate/tests/addsub.rs index 284a2bf5a..f77ce723f 100644 --- a/testcrate/tests/addsub.rs +++ b/testcrate/tests/addsub.rs @@ -3,81 +3,6 @@ use testcrate::*; -mod int_addsub { - use super::*; - - macro_rules! sum { - ($($i:ty, $fn_add:ident, $fn_sub:ident);*;) => { - $( - #[test] - fn $fn_add() { - use compiler_builtins::int::addsub::{$fn_add, $fn_sub}; - - fuzz_2(N, |x: $i, y: $i| { - let add0 = x.wrapping_add(y); - let sub0 = x.wrapping_sub(y); - let add1: $i = $fn_add(x, y); - let sub1: $i = $fn_sub(x, y); - if add0 != add1 { - panic!( - "{}({}, {}): std: {}, builtins: {}", - stringify!($fn_add), x, y, add0, add1 - ); - } - if sub0 != sub1 { - panic!( - "{}({}, {}): std: {}, builtins: {}", - stringify!($fn_sub), x, y, sub0, sub1 - ); - } - }); - } - )* - }; - } - - macro_rules! overflowing_sum { - ($($i:ty, $fn_add:ident, $fn_sub:ident);*;) => { - $( - #[test] - fn $fn_add() { - use compiler_builtins::int::addsub::{$fn_add, $fn_sub}; - - fuzz_2(N, |x: $i, y: $i| { - let add0 = x.overflowing_add(y); - let sub0 = x.overflowing_sub(y); - let add1: ($i, bool) = $fn_add(x, y); - let sub1: ($i, bool) = $fn_sub(x, y); - if add0.0 != add1.0 || add0.1 != add1.1 { - panic!( - "{}({}, {}): std: {:?}, builtins: {:?}", - stringify!($fn_add), x, y, add0, add1 - ); - } - if sub0.0 != sub1.0 || sub0.1 != sub1.1 { - panic!( - "{}({}, {}): std: {:?}, builtins: {:?}", - stringify!($fn_sub), x, y, sub0, sub1 - ); - } - }); - } - )* - }; - } - - // Integer addition and subtraction is very simple, so 100 fuzzing passes should be plenty. - sum! { - u128, __rust_u128_add, __rust_u128_sub; - i128, __rust_i128_add, __rust_i128_sub; - } - - overflowing_sum! { - u128, __rust_u128_addo, __rust_u128_subo; - i128, __rust_i128_addo, __rust_i128_subo; - } -} - macro_rules! float_sum { ($($f:ty, $fn_add:ident, $fn_sub:ident, $apfloat_ty:ident, $sys_available:meta);*;) => { $( From 287c2791df13461bd51b795d2831e3ac80ecb429 Mon Sep 17 00:00:00 2001 From: bjorn3 <17426603+bjorn3@users.noreply.github.com> Date: Thu, 9 Jan 2025 13:26:37 +0100 Subject: [PATCH 2/2] Fix error for cargo check --workspace --all-targets This prevents rust-analyzer from showing an error for the panic-handler crate. --- crates/panic-handler/Cargo.toml | 4 ++++ testcrate/tests/addsub.rs | 1 + 2 files changed, 5 insertions(+) diff --git a/crates/panic-handler/Cargo.toml b/crates/panic-handler/Cargo.toml index 2ad858409..92fbb748a 100644 --- a/crates/panic-handler/Cargo.toml +++ b/crates/panic-handler/Cargo.toml @@ -5,4 +5,8 @@ authors = ["Alex Crichton "] edition = "2021" publish = false +[lib] +test = false +bench = false + [dependencies] diff --git a/testcrate/tests/addsub.rs b/testcrate/tests/addsub.rs index f77ce723f..0ee17f807 100644 --- a/testcrate/tests/addsub.rs +++ b/testcrate/tests/addsub.rs @@ -1,6 +1,7 @@ #![allow(unused_macros)] #![cfg_attr(f128_enabled, feature(f128))] +#[allow(unused_imports)] // Unused on some archs use testcrate::*; macro_rules! float_sum {