2016-08-18 04:50:24 +08:00
|
|
|
use int::Int;
|
2016-08-19 00:20:24 +08:00
|
|
|
|
|
|
|
macro_rules! div {
|
|
|
|
($intrinsic:ident: $ty:ty, $uty:ty) => {
|
2017-01-05 22:23:45 +08:00
|
|
|
div!($intrinsic: $ty, $uty, $ty, |i| {i});
|
|
|
|
};
|
|
|
|
($intrinsic:ident: $ty:ty, $uty:ty, $tyret:ty, $conv:expr) => {
|
2016-08-19 00:20:24 +08:00
|
|
|
/// Returns `a / b`
|
|
|
|
#[cfg_attr(not(test), no_mangle)]
|
2017-01-05 22:23:45 +08:00
|
|
|
pub extern "C" fn $intrinsic(a: $ty, b: $ty) -> $tyret {
|
2016-08-19 00:20:24 +08:00
|
|
|
let s_a = a >> (<$ty>::bits() - 1);
|
|
|
|
let s_b = b >> (<$ty>::bits() - 1);
|
2017-03-07 06:46:11 +08:00
|
|
|
// NOTE it's OK to overflow here because of the `as $uty` cast below
|
|
|
|
// This whole operation is computing the absolute value of the inputs
|
|
|
|
// So some overflow will happen when dealing with e.g. `i64::MIN`
|
|
|
|
// where the absolute value is `(-i64::MIN) as u64`
|
|
|
|
let a = (a ^ s_a).wrapping_sub(s_a);
|
|
|
|
let b = (b ^ s_b).wrapping_sub(s_b);
|
2016-08-19 00:20:24 +08:00
|
|
|
let s = s_a ^ s_b;
|
2016-10-06 09:45:40 +08:00
|
|
|
|
2016-10-08 06:48:37 +08:00
|
|
|
let r = udiv!(a as $uty, b as $uty);
|
2017-01-05 22:23:45 +08:00
|
|
|
($conv)((r as $ty ^ s) - s)
|
2016-08-19 00:20:24 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
macro_rules! mod_ {
|
|
|
|
($intrinsic:ident: $ty:ty, $uty:ty) => {
|
2017-01-05 22:23:45 +08:00
|
|
|
mod_!($intrinsic: $ty, $uty, $ty, |i| {i});
|
|
|
|
};
|
|
|
|
($intrinsic:ident: $ty:ty, $uty:ty, $tyret:ty, $conv:expr) => {
|
2016-08-19 00:20:24 +08:00
|
|
|
/// Returns `a % b`
|
|
|
|
#[cfg_attr(not(test), no_mangle)]
|
2017-01-05 22:23:45 +08:00
|
|
|
pub extern "C" fn $intrinsic(a: $ty, b: $ty) -> $tyret {
|
2016-08-19 00:20:24 +08:00
|
|
|
let s = b >> (<$ty>::bits() - 1);
|
2017-03-14 09:40:44 +08:00
|
|
|
// NOTE(wrapping_sub) see comment in the `div` macro
|
|
|
|
let b = (b ^ s).wrapping_sub(s);
|
2016-08-19 00:20:24 +08:00
|
|
|
let s = a >> (<$ty>::bits() - 1);
|
2017-03-14 09:40:44 +08:00
|
|
|
let a = (a ^ s).wrapping_sub(s);
|
2016-10-06 09:45:40 +08:00
|
|
|
|
2016-10-08 06:48:37 +08:00
|
|
|
let r = urem!(a as $uty, b as $uty);
|
2017-01-05 22:23:45 +08:00
|
|
|
($conv)((r as $ty ^ s) - s)
|
2016-08-19 00:20:24 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
macro_rules! divmod {
|
2017-02-08 01:51:18 +08:00
|
|
|
($abi:tt, $intrinsic:ident, $div:ident: $ty:ty) => {
|
2016-08-19 00:20:24 +08:00
|
|
|
/// Returns `a / b` and sets `*rem = n % d`
|
2017-02-08 01:51:18 +08:00
|
|
|
#[cfg_attr(not(test), no_mangle)]
|
|
|
|
pub extern $abi fn $intrinsic(a: $ty, b: $ty, rem: &mut $ty) -> $ty {
|
2016-09-27 04:55:11 +08:00
|
|
|
#[cfg(all(feature = "c", any(target_arch = "x86")))]
|
|
|
|
extern {
|
|
|
|
fn $div(a: $ty, b: $ty) -> $ty;
|
|
|
|
}
|
|
|
|
|
2016-10-11 08:45:34 +08:00
|
|
|
let r = match () {
|
|
|
|
#[cfg(not(all(feature = "c", any(target_arch = "x86"))))]
|
|
|
|
() => $div(a, b),
|
|
|
|
#[cfg(all(feature = "c", any(target_arch = "x86")))]
|
|
|
|
() => unsafe { $div(a, b) },
|
|
|
|
};
|
2017-03-06 23:34:27 +08:00
|
|
|
// NOTE won't overflow because it's using the result from the
|
|
|
|
// previous division
|
|
|
|
*rem = a - r.wrapping_mul(b);
|
2016-08-19 00:20:24 +08:00
|
|
|
r
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-27 04:55:11 +08:00
|
|
|
#[cfg(not(all(feature = "c", target_arch = "arm", not(target_os = "ios"), not(thumbv6m))))]
|
2016-08-19 00:20:24 +08:00
|
|
|
div!(__divsi3: i32, u32);
|
2016-09-27 04:55:11 +08:00
|
|
|
|
|
|
|
#[cfg(not(all(feature = "c", target_arch = "x86")))]
|
2016-08-19 00:20:24 +08:00
|
|
|
div!(__divdi3: i64, u64);
|
2016-09-27 04:55:11 +08:00
|
|
|
|
2017-01-05 22:23:45 +08:00
|
|
|
#[cfg(not(all(windows, target_pointer_width="64")))]
|
|
|
|
div!(__divti3: i128, u128);
|
|
|
|
|
|
|
|
#[cfg(all(windows, target_pointer_width="64"))]
|
|
|
|
div!(__divti3: i128, u128, ::U64x2, ::sconv);
|
|
|
|
|
2016-09-27 04:55:11 +08:00
|
|
|
#[cfg(not(all(feature = "c", target_arch = "arm", not(target_os = "ios"))))]
|
2016-08-19 00:20:24 +08:00
|
|
|
mod_!(__modsi3: i32, u32);
|
2016-09-27 04:55:11 +08:00
|
|
|
|
|
|
|
#[cfg(not(all(feature = "c", target_arch = "x86")))]
|
2016-08-19 00:20:24 +08:00
|
|
|
mod_!(__moddi3: i64, u64);
|
2016-09-27 04:55:11 +08:00
|
|
|
|
2017-01-05 22:23:45 +08:00
|
|
|
#[cfg(not(all(windows, target_pointer_width="64")))]
|
|
|
|
mod_!(__modti3: i128, u128);
|
|
|
|
|
|
|
|
#[cfg(all(windows, target_pointer_width="64"))]
|
|
|
|
mod_!(__modti3: i128, u128, ::U64x2, ::sconv);
|
|
|
|
|
2016-09-27 04:55:11 +08:00
|
|
|
#[cfg(not(all(feature = "c", target_arch = "arm", not(target_os = "ios"))))]
|
2017-02-08 01:51:18 +08:00
|
|
|
divmod!("C", __divmodsi4, __divsi3: i32);
|
|
|
|
|
|
|
|
#[cfg(target_arch = "arm")]
|
|
|
|
divmod!("aapcs", __divmoddi4, __divdi3: i64);
|
2016-09-27 04:55:11 +08:00
|
|
|
|
2017-02-08 01:51:18 +08:00
|
|
|
#[cfg(not(target_arch = "arm"))]
|
|
|
|
divmod!("C", __divmoddi4, __divdi3: i64);
|