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) => {
|
|
|
|
/// Returns `a / b`
|
|
|
|
#[cfg_attr(not(test), no_mangle)]
|
|
|
|
pub extern "C" fn $intrinsic(a: $ty, b: $ty) -> $ty {
|
|
|
|
let s_a = a >> (<$ty>::bits() - 1);
|
|
|
|
let s_b = b >> (<$ty>::bits() - 1);
|
|
|
|
let a = (a ^ s_a) - s_a;
|
|
|
|
let b = (b ^ s_b) - s_b;
|
|
|
|
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);
|
2016-08-19 00:20:24 +08:00
|
|
|
(r as $ty ^ s) - s
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
macro_rules! mod_ {
|
|
|
|
($intrinsic:ident: $ty:ty, $uty:ty) => {
|
|
|
|
/// Returns `a % b`
|
|
|
|
#[cfg_attr(not(test), no_mangle)]
|
|
|
|
pub extern "C" fn $intrinsic(a: $ty, b: $ty) -> $ty {
|
|
|
|
let s = b >> (<$ty>::bits() - 1);
|
|
|
|
let b = (b ^ s) - s;
|
|
|
|
let s = a >> (<$ty>::bits() - 1);
|
|
|
|
let a = (a ^ s) - 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);
|
2016-08-19 00:20:24 +08:00
|
|
|
(r as $ty ^ s) - s
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
macro_rules! divmod {
|
|
|
|
($intrinsic:ident, $div:ident: $ty:ty) => {
|
|
|
|
/// Returns `a / b` and sets `*rem = n % d`
|
|
|
|
#[cfg_attr(not(test), no_mangle)]
|
|
|
|
pub extern "C" 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) },
|
|
|
|
};
|
2016-08-19 00:20:24 +08:00
|
|
|
*rem = a - (r * b);
|
|
|
|
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
|
|
|
|
|
|
|
#[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
|
|
|
|
|
|
|
#[cfg(not(all(feature = "c", target_arch = "arm", not(target_os = "ios"))))]
|
2016-08-19 00:20:24 +08:00
|
|
|
divmod!(__divmodsi4, __divsi3: i32);
|
2016-09-27 04:55:11 +08:00
|
|
|
|
2016-08-19 00:20:24 +08:00
|
|
|
divmod!(__divmoddi4, __divdi3: i64);
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
2016-09-22 10:38:06 +08:00
|
|
|
use qc::{U32, U64};
|
|
|
|
|
2016-09-27 13:22:10 +08:00
|
|
|
check! {
|
|
|
|
fn __divdi3(f: extern fn(i64, i64) -> i64, n: U64, d: U64) -> Option<i64> {
|
2016-08-19 00:20:24 +08:00
|
|
|
let (n, d) = (n.0 as i64, d.0 as i64);
|
|
|
|
if d == 0 {
|
2016-09-27 13:22:10 +08:00
|
|
|
None
|
2016-08-19 00:20:24 +08:00
|
|
|
} else {
|
2016-09-27 13:22:10 +08:00
|
|
|
Some(f(n, d))
|
2016-08-19 00:20:24 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-27 13:22:10 +08:00
|
|
|
fn __moddi3(f: extern fn(i64, i64) -> i64, n: U64, d: U64) -> Option<i64> {
|
2016-08-19 00:20:24 +08:00
|
|
|
let (n, d) = (n.0 as i64, d.0 as i64);
|
|
|
|
if d == 0 {
|
2016-09-27 13:22:10 +08:00
|
|
|
None
|
2016-08-19 00:20:24 +08:00
|
|
|
} else {
|
2016-09-27 13:22:10 +08:00
|
|
|
Some(f(n, d))
|
2016-08-19 00:20:24 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-27 13:22:10 +08:00
|
|
|
fn __divmoddi4(f: extern fn(i64, i64, &mut i64) -> i64,
|
|
|
|
n: U64,
|
|
|
|
d: U64) -> Option<(i64, i64)> {
|
2016-08-19 00:20:24 +08:00
|
|
|
let (n, d) = (n.0 as i64, d.0 as i64);
|
|
|
|
if d == 0 {
|
2016-09-27 13:22:10 +08:00
|
|
|
None
|
2016-08-19 00:20:24 +08:00
|
|
|
} else {
|
|
|
|
let mut r = 0;
|
2016-09-27 13:22:10 +08:00
|
|
|
let q = f(n, d, &mut r);
|
|
|
|
Some((q, r))
|
2016-08-19 00:20:24 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-27 13:22:10 +08:00
|
|
|
fn __divsi3(f: extern fn(i32, i32) -> i32,
|
|
|
|
n: U32,
|
|
|
|
d: U32) -> Option<i32> {
|
2016-08-19 00:20:24 +08:00
|
|
|
let (n, d) = (n.0 as i32, d.0 as i32);
|
|
|
|
if d == 0 {
|
2016-09-27 13:22:10 +08:00
|
|
|
None
|
2016-08-19 00:20:24 +08:00
|
|
|
} else {
|
2016-09-27 13:22:10 +08:00
|
|
|
Some(f(n, d))
|
2016-08-19 00:20:24 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-27 13:22:10 +08:00
|
|
|
fn __modsi3(f: extern fn(i32, i32) -> i32,
|
|
|
|
n: U32,
|
|
|
|
d: U32) -> Option<i32> {
|
2016-08-19 00:20:24 +08:00
|
|
|
let (n, d) = (n.0 as i32, d.0 as i32);
|
|
|
|
if d == 0 {
|
2016-09-27 13:22:10 +08:00
|
|
|
None
|
2016-08-19 00:20:24 +08:00
|
|
|
} else {
|
2016-09-27 13:22:10 +08:00
|
|
|
Some(f(n, d))
|
2016-08-19 00:20:24 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-27 13:22:10 +08:00
|
|
|
fn __divmodsi4(f: extern fn(i32, i32, &mut i32) -> i32,
|
|
|
|
n: U32,
|
|
|
|
d: U32) -> Option<(i32, i32)> {
|
2016-08-19 00:20:24 +08:00
|
|
|
let (n, d) = (n.0 as i32, d.0 as i32);
|
|
|
|
if d == 0 {
|
2016-09-27 13:22:10 +08:00
|
|
|
None
|
2016-08-19 00:20:24 +08:00
|
|
|
} else {
|
|
|
|
let mut r = 0;
|
2016-09-27 13:22:10 +08:00
|
|
|
let q = f(n, d, &mut r);
|
|
|
|
Some((q, r))
|
2016-08-19 00:20:24 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|