Skip to content

Division tweaks #380

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Oct 10, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/int/leading_zeros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
// Compilers will insert the check for zero in cases where it is needed.

/// Returns the number of leading binary zeros in `x`.
#[doc(hidden)]
pub fn usize_leading_zeros_default(x: usize) -> usize {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can this be pub(crate) instead of#[doc(hidden)]?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is being used by the testcrate (the CI does not have a RISCV target at the moment, so we want to make sure that they both work)

// The basic idea is to test if the higher bits of `x` are zero and bisect the number
// of leading zeros. It is possible for all branches of the bisection to use the same
Expand Down Expand Up @@ -75,6 +76,7 @@ pub fn usize_leading_zeros_default(x: usize) -> usize {
// RISC-V that allows `(x >= power-of-two) as usize` to be branchless.

/// Returns the number of leading binary zeros in `x`.
#[doc(hidden)]
pub fn usize_leading_zeros_riscv(x: usize) -> usize {
let mut x = x;
// the number of potential leading zeros
Expand Down
207 changes: 154 additions & 53 deletions src/int/sdiv.rs
Original file line number Diff line number Diff line change
@@ -1,65 +1,166 @@
use int::specialized_div_rem::*;
use int::udiv::*;

intrinsics! {
#[maybe_use_optimized_c_shim]
#[arm_aeabi_alias = __aeabi_idiv]
/// Returns `n / d`
pub extern "C" fn __divsi3(a: i32, b: i32) -> i32 {
i32_div_rem(a, b).0
}

#[maybe_use_optimized_c_shim]
/// Returns `n % d`
pub extern "C" fn __modsi3(a: i32, b: i32) -> i32 {
i32_div_rem(a, b).1
}

#[maybe_use_optimized_c_shim]
/// Returns `n / d` and sets `*rem = n % d`
pub extern "C" fn __divmodsi4(a: i32, b: i32, rem: &mut i32) -> i32 {
let quo_rem = i32_div_rem(a, b);
*rem = quo_rem.1;
quo_rem.0
macro_rules! sdivmod {
(
$unsigned_fn:ident, // name of the unsigned division function
$signed_fn:ident, // name of the signed division function
$uX:ident, // unsigned integer type for the inputs and outputs of `$unsigned_name`
$iX:ident, // signed integer type for the inputs and outputs of `$signed_name`
$($attr:tt),* // attributes
) => {
intrinsics! {
$(
#[$attr]
)*
/// Returns `n / d` and sets `*rem = n % d`
pub extern "C" fn $signed_fn(a: $iX, b: $iX, rem: &mut $iX) -> $iX {
let a_neg = a < 0;
let b_neg = b < 0;
let mut a = a;
let mut b = b;
if a_neg {
a = a.wrapping_neg();
}
if b_neg {
b = b.wrapping_neg();
}
let mut r = *rem as $uX;
let t = $unsigned_fn(a as $uX, b as $uX, Some(&mut r)) as $iX;
let mut r = r as $iX;
if a_neg {
r = r.wrapping_neg();
}
*rem = r;
if a_neg != b_neg {
t.wrapping_neg()
} else {
t
}
}
}
}
}

#[maybe_use_optimized_c_shim]
/// Returns `n / d`
pub extern "C" fn __divdi3(a: i64, b: i64) -> i64 {
i64_div_rem(a, b).0
macro_rules! sdiv {
(
$unsigned_fn:ident, // name of the unsigned division function
$signed_fn:ident, // name of the signed division function
$uX:ident, // unsigned integer type for the inputs and outputs of `$unsigned_name`
$iX:ident, // signed integer type for the inputs and outputs of `$signed_name`
$($attr:tt),* // attributes
) => {
intrinsics! {
$(
#[$attr]
)*
/// Returns `n / d`
pub extern "C" fn $signed_fn(a: $iX, b: $iX) -> $iX {
let a_neg = a < 0;
let b_neg = b < 0;
let mut a = a;
let mut b = b;
if a_neg {
a = a.wrapping_neg();
}
if b_neg {
b = b.wrapping_neg();
}
let t = $unsigned_fn(a as $uX, b as $uX) as $iX;
if a_neg != b_neg {
t.wrapping_neg()
} else {
t
}
}
}
}
}

#[maybe_use_optimized_c_shim]
/// Returns `n % d`
pub extern "C" fn __moddi3(a: i64, b: i64) -> i64 {
i64_div_rem(a, b).1
macro_rules! smod {
(
$unsigned_fn:ident, // name of the unsigned division function
$signed_fn:ident, // name of the signed division function
$uX:ident, // unsigned integer type for the inputs and outputs of `$unsigned_name`
$iX:ident, // signed integer type for the inputs and outputs of `$signed_name`
$($attr:tt),* // attributes
) => {
intrinsics! {
$(
#[$attr]
)*
/// Returns `n % d`
pub extern "C" fn $signed_fn(a: $iX, b: $iX) -> $iX {
let a_neg = a < 0;
let b_neg = b < 0;
let mut a = a;
let mut b = b;
if a_neg {
a = a.wrapping_neg();
}
if b_neg {
b = b.wrapping_neg();
}
let r = $unsigned_fn(a as $uX, b as $uX) as $iX;
if a_neg {
r.wrapping_neg()
} else {
r
}
}
}
}
}

sdivmod!(
__udivmodsi4,
__divmodsi4,
u32,
i32,
maybe_use_optimized_c_shim
);
// The `#[arm_aeabi_alias = __aeabi_idiv]` attribute cannot be made to work with `intrinsics!` in macros
intrinsics! {
#[maybe_use_optimized_c_shim]
/// Returns `n / d` and sets `*rem = n % d`
pub extern "C" fn __divmoddi4(a: i64, b: i64, rem: &mut i64) -> i64 {
let quo_rem = i64_div_rem(a, b);
*rem = quo_rem.1;
quo_rem.0
}

#[win64_128bit_abi_hack]
#[arm_aeabi_alias = __aeabi_idiv]
/// Returns `n / d`
pub extern "C" fn __divti3(a: i128, b: i128) -> i128 {
i128_div_rem(a, b).0
pub extern "C" fn __divsi3(a: i32, b: i32) -> i32 {
let a_neg = a < 0;
let b_neg = b < 0;
let mut a = a;
let mut b = b;
if a_neg {
a = a.wrapping_neg();
}
if b_neg {
b = b.wrapping_neg();
}
let t = __udivsi3(a as u32, b as u32) as i32;
if a_neg != b_neg {
t.wrapping_neg()
} else {
t
}
}
}
smod!(__umodsi3, __modsi3, u32, i32, maybe_use_optimized_c_shim);

#[win64_128bit_abi_hack]
/// Returns `n % d`
pub extern "C" fn __modti3(a: i128, b: i128) -> i128 {
i128_div_rem(a, b).1
}
sdivmod!(
__udivmoddi4,
__divmoddi4,
u64,
i64,
maybe_use_optimized_c_shim
);
sdiv!(__udivdi3, __divdi3, u64, i64, maybe_use_optimized_c_shim);
smod!(__umoddi3, __moddi3, u64, i64, maybe_use_optimized_c_shim);

// LLVM does not currently have a `__divmodti4` function, but GCC does
#[maybe_use_optimized_c_shim]
/// Returns `n / d` and sets `*rem = n % d`
pub extern "C" fn __divmodti4(a: i128, b: i128, rem: &mut i128) -> i128 {
let quo_rem = i128_div_rem(a, b);
*rem = quo_rem.1;
quo_rem.0
}
}
// LLVM does not currently have a `__divmodti4` function, but GCC does
sdivmod!(
__udivmodti4,
__divmodti4,
u128,
i128,
maybe_use_optimized_c_shim
);
sdiv!(__udivti3, __divti3, u128, i128, win64_128bit_abi_hack);
smod!(__umodti3, __modti3, u128, i128, win64_128bit_abi_hack);
Loading