compiler-builtins-zynq/src/int/mul.rs

96 lines
3.2 KiB
Rust
Raw Normal View History

2016-10-11 09:21:11 +08:00
use int::LargeInt;
use int::Int;
2016-08-13 16:51:54 +08:00
macro_rules! mul {
($(#[$attr:meta])+ |
$abi:tt, $intrinsic:ident: $ty:ty) => {
2016-08-13 16:51:54 +08:00
/// Returns `a * b`
$(#[$attr])+
pub extern $abi fn $intrinsic(a: $ty, b: $ty) -> $ty {
2016-08-13 16:51:54 +08:00
let half_bits = <$ty>::bits() / 4;
let lower_mask = !0 >> half_bits;
let mut low = (a.low() & lower_mask).wrapping_mul(b.low() & lower_mask);
2016-08-13 16:51:54 +08:00
let mut t = low >> half_bits;
low &= lower_mask;
t += (a.low() >> half_bits).wrapping_mul(b.low() & lower_mask);
2016-08-13 16:51:54 +08:00
low += (t & lower_mask) << half_bits;
2017-01-05 09:22:44 +08:00
let mut high = (t >> half_bits) as hty!($ty);
2016-08-13 16:51:54 +08:00
t = low >> half_bits;
low &= lower_mask;
t += (b.low() >> half_bits).wrapping_mul(a.low() & lower_mask);
2016-08-13 16:51:54 +08:00
low += (t & lower_mask) << half_bits;
2017-01-05 09:22:44 +08:00
high += (t >> half_bits) as hty!($ty);
high += (a.low() >> half_bits).wrapping_mul(b.low() >> half_bits) as hty!($ty);
high = high.wrapping_add(a.high().wrapping_mul(b.low() as hty!($ty)))
.wrapping_add((a.low() as hty!($ty)).wrapping_mul(b.high()));
2016-08-13 16:51:54 +08:00
<$ty>::from_parts(low, high)
}
}
}
macro_rules! mulo {
($intrinsic:ident: $ty:ty) => {
2017-01-04 09:42:03 +08:00
// Default is "C" ABI
mulo!($intrinsic: $ty, "C");
};
($intrinsic:ident: $ty:ty, $abi:tt) => {
2016-08-13 16:51:54 +08:00
/// Returns `a * b` and sets `*overflow = 1` if `a * b` overflows
#[cfg_attr(not(test), no_mangle)]
2017-01-04 09:42:03 +08:00
pub extern $abi fn $intrinsic(a: $ty, b: $ty, overflow: &mut i32) -> $ty {
2016-08-13 16:51:54 +08:00
*overflow = 0;
let result = a.wrapping_mul(b);
if a == <$ty>::min_value() {
if b != 0 && b != 1 {
*overflow = 1;
}
return result;
}
if b == <$ty>::min_value() {
if a != 0 && a != 1 {
*overflow = 1;
}
return result;
}
let sa = a >> (<$ty>::bits() - 1);
let abs_a = (a ^ sa) - sa;
let sb = b >> (<$ty>::bits() - 1);
let abs_b = (b ^ sb) - sb;
if abs_a < 2 || abs_b < 2 {
return result;
}
if sa == sb {
if abs_a > <$ty>::max_value() / abs_b {
*overflow = 1;
}
} else {
if abs_a > <$ty>::min_value() / -abs_b {
*overflow = 1;
}
}
result
}
}
}
#[cfg(not(all(feature = "c", target_arch = "x86")))]
mul!(#[cfg_attr(all(not(test), not(target_arch = "arm")), no_mangle)]
#[cfg_attr(all(not(test), target_arch = "arm"), inline(always))]
| "C", __muldi3: u64);
#[cfg(not(target_arch = "arm"))]
mul!(#[cfg_attr(not(test), no_mangle)]
| "C", __multi3: i128);
2017-01-04 09:42:03 +08:00
#[cfg(target_arch = "arm")]
mul!(#[cfg_attr(not(test), no_mangle)]
| "aapcs", __multi3: i128);
2016-08-13 16:51:54 +08:00
mulo!(__mulosi4: i32);
mulo!(__mulodi4: i64);
2017-01-04 09:42:03 +08:00
#[cfg(all(windows, target_pointer_width="64"))]
mulo!(__muloti4: i128, "unadjusted");
#[cfg(not(all(windows, target_pointer_width="64")))]
mulo!(__muloti4: i128);