-
Notifications
You must be signed in to change notification settings - Fork 229
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
Division tweaks #380
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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)]
?There was a problem hiding this comment.
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)