Convert add! to a function
This commit is contained in:
parent
482d98318f
commit
3efae7f7d9
@ -1,24 +1,25 @@
|
||||
use int::Int;
|
||||
use int::{Int, CastInto};
|
||||
use float::Float;
|
||||
|
||||
/// Returns `a + b`
|
||||
macro_rules! add {
|
||||
($a:expr, $b:expr, $ty:ty) => ({
|
||||
let a = $a;
|
||||
let b = $b;
|
||||
let one = <$ty as Float>::Int::ONE;
|
||||
let zero = <$ty as Float>::Int::ZERO;
|
||||
fn add<F: Float>(a: F, b: F) -> F where
|
||||
u32: CastInto<F::Int>,
|
||||
F::Int: CastInto<u32>,
|
||||
i32: CastInto<F::Int>,
|
||||
F::Int: CastInto<i32>,
|
||||
{
|
||||
let one = F::Int::ONE;
|
||||
let zero = F::Int::ZERO;
|
||||
|
||||
let bits = <$ty>::BITS as <$ty as Float>::Int;
|
||||
let significand_bits = <$ty>::SIGNIFICAND_BITS as <$ty as Float>::Int;
|
||||
let exponent_bits = <$ty>::EXPONENT_BITS as <$ty as Float>::Int;
|
||||
let max_exponent = (one << exponent_bits as usize) - one;
|
||||
let bits = F::BITS.cast();
|
||||
let significand_bits = F::SIGNIFICAND_BITS;
|
||||
let max_exponent = F::EXPONENT_MAX;
|
||||
|
||||
let implicit_bit = one << significand_bits as usize;
|
||||
let significand_mask = implicit_bit - one;
|
||||
let sign_bit = <$ty>::SIGN_MASK as <$ty as Float>::Int;
|
||||
let implicit_bit = F::IMPLICIT_BIT;
|
||||
let significand_mask = F::SIGNIFICAND_MASK;
|
||||
let sign_bit = F::SIGN_MASK as F::Int;
|
||||
let abs_mask = sign_bit - one;
|
||||
let exponent_mask = abs_mask ^ significand_mask;
|
||||
let exponent_mask = F::EXPONENT_MASK;
|
||||
let inf_rep = exponent_mask;
|
||||
let quiet_bit = implicit_bit >> 1;
|
||||
let qnan_rep = exponent_mask | quiet_bit;
|
||||
@ -33,17 +34,17 @@ macro_rules! add {
|
||||
b_abs.wrapping_sub(one) >= inf_rep - one {
|
||||
// NaN + anything = qNaN
|
||||
if a_abs > inf_rep {
|
||||
return <$ty as Float>::from_repr(a_abs | quiet_bit);
|
||||
return F::from_repr(a_abs | quiet_bit);
|
||||
}
|
||||
// anything + NaN = qNaN
|
||||
if b_abs > inf_rep {
|
||||
return <$ty as Float>::from_repr(b_abs | quiet_bit);
|
||||
return F::from_repr(b_abs | quiet_bit);
|
||||
}
|
||||
|
||||
if a_abs == inf_rep {
|
||||
// +/-infinity + -/+infinity = qNaN
|
||||
if (a.repr() ^ b.repr()) == sign_bit {
|
||||
return <$ty as Float>::from_repr(qnan_rep);
|
||||
return F::from_repr(qnan_rep);
|
||||
} else {
|
||||
// +/-infinity + anything remaining = +/- infinity
|
||||
return a;
|
||||
@ -56,17 +57,17 @@ macro_rules! add {
|
||||
}
|
||||
|
||||
// zero + anything = anything
|
||||
if a_abs == 0 {
|
||||
if a_abs == Int::ZERO {
|
||||
// but we need to get the sign right for zero + zero
|
||||
if b_abs == 0 {
|
||||
return <$ty as Float>::from_repr(a.repr() & b.repr());
|
||||
if b_abs == Int::ZERO {
|
||||
return F::from_repr(a.repr() & b.repr());
|
||||
} else {
|
||||
return b;
|
||||
}
|
||||
}
|
||||
|
||||
// anything + zero = anything
|
||||
if b_abs == 0 {
|
||||
if b_abs == Int::ZERO {
|
||||
return a;
|
||||
}
|
||||
}
|
||||
@ -80,19 +81,19 @@ macro_rules! add {
|
||||
}
|
||||
|
||||
// Extract the exponent and significand from the (possibly swapped) a and b.
|
||||
let mut a_exponent = ((a_rep >> significand_bits) & max_exponent) as i32;
|
||||
let mut b_exponent = ((b_rep >> significand_bits) & max_exponent) as i32;
|
||||
let mut a_exponent: i32 = ((a_rep & exponent_mask) >> significand_bits).cast();
|
||||
let mut b_exponent: i32 = ((b_rep & exponent_mask) >> significand_bits).cast();
|
||||
let mut a_significand = a_rep & significand_mask;
|
||||
let mut b_significand = b_rep & significand_mask;
|
||||
|
||||
// normalize any denormals, and adjust the exponent accordingly.
|
||||
if a_exponent == 0 {
|
||||
let (exponent, significand) = <$ty>::normalize(a_significand);
|
||||
let (exponent, significand) = F::normalize(a_significand);
|
||||
a_exponent = exponent;
|
||||
a_significand = significand;
|
||||
}
|
||||
if b_exponent == 0 {
|
||||
let (exponent, significand) = <$ty>::normalize(b_significand);
|
||||
let (exponent, significand) = F::normalize(b_significand);
|
||||
b_exponent = exponent;
|
||||
b_significand = significand;
|
||||
}
|
||||
@ -111,11 +112,11 @@ macro_rules! add {
|
||||
|
||||
// Shift the significand of b by the difference in exponents, with a sticky
|
||||
// bottom bit to get rounding correct.
|
||||
let align = a_exponent.wrapping_sub(b_exponent) as <$ty as Float>::Int;
|
||||
if align != 0 {
|
||||
let align = a_exponent.wrapping_sub(b_exponent).cast();
|
||||
if align != Int::ZERO {
|
||||
if align < bits {
|
||||
let sticky = (b_significand << (bits.wrapping_sub(align) as usize) != 0) as <$ty as Float>::Int;
|
||||
b_significand = (b_significand >> align as usize) | sticky;
|
||||
let sticky = F::Int::from_bool(b_significand << bits.wrapping_sub(align).cast() != Int::ZERO);
|
||||
b_significand = (b_significand >> align.cast()) | sticky;
|
||||
} else {
|
||||
b_significand = one; // sticky; b is known to be non-zero.
|
||||
}
|
||||
@ -123,8 +124,8 @@ macro_rules! add {
|
||||
if subtraction {
|
||||
a_significand = a_significand.wrapping_sub(b_significand);
|
||||
// If a == -b, return +zero.
|
||||
if a_significand == 0 {
|
||||
return <$ty as Float>::from_repr(0);
|
||||
if a_significand == Int::ZERO {
|
||||
return F::from_repr(Int::ZERO);
|
||||
}
|
||||
|
||||
// If partial cancellation occured, we need to left-shift the result
|
||||
@ -132,7 +133,7 @@ macro_rules! add {
|
||||
if a_significand < implicit_bit << 3 {
|
||||
let shift = a_significand.leading_zeros() as i32
|
||||
- (implicit_bit << 3).leading_zeros() as i32;
|
||||
a_significand <<= shift as usize;
|
||||
a_significand <<= shift;
|
||||
a_exponent -= shift;
|
||||
}
|
||||
} else /* addition */ {
|
||||
@ -140,8 +141,8 @@ macro_rules! add {
|
||||
|
||||
// If the addition carried up, we need to right-shift the result and
|
||||
// adjust the exponent:
|
||||
if a_significand & implicit_bit << 4 != 0 {
|
||||
let sticky = (a_significand & one != 0) as <$ty as Float>::Int;
|
||||
if a_significand & implicit_bit << 4 != Int::ZERO {
|
||||
let sticky = F::Int::from_bool(a_significand & one != Int::ZERO);
|
||||
a_significand = a_significand >> 1 | sticky;
|
||||
a_exponent += 1;
|
||||
}
|
||||
@ -149,26 +150,27 @@ macro_rules! add {
|
||||
|
||||
// If we have overflowed the type, return +/- infinity:
|
||||
if a_exponent >= max_exponent as i32 {
|
||||
return <$ty>::from_repr(inf_rep | result_sign);
|
||||
return F::from_repr(inf_rep | result_sign);
|
||||
}
|
||||
|
||||
if a_exponent <= 0 {
|
||||
// Result is denormal before rounding; the exponent is zero and we
|
||||
// need to shift the significand.
|
||||
let shift = (1 - a_exponent) as <$ty as Float>::Int;
|
||||
let sticky = ((a_significand << bits.wrapping_sub(shift) as usize) != 0) as <$ty as Float>::Int;
|
||||
a_significand = a_significand >> shift as usize | sticky;
|
||||
let shift = (1 - a_exponent).cast();
|
||||
let sticky = F::Int::from_bool((a_significand << bits.wrapping_sub(shift).cast()) != Int::ZERO);
|
||||
a_significand = a_significand >> shift.cast() | sticky;
|
||||
a_exponent = 0;
|
||||
}
|
||||
|
||||
// Low three bits are round, guard, and sticky.
|
||||
let round_guard_sticky: i32 = (a_significand & 0x7) as i32;
|
||||
let a_significand_i32: i32 = a_significand.cast();
|
||||
let round_guard_sticky: i32 = a_significand_i32 & 0x7;
|
||||
|
||||
// Shift the significand into place, and mask off the implicit bit.
|
||||
let mut result = a_significand >> 3 & significand_mask;
|
||||
|
||||
// Insert the exponent and sign.
|
||||
result |= (a_exponent as <$ty as Float>::Int) << (significand_bits as usize);
|
||||
result |= a_exponent.cast() << significand_bits;
|
||||
result |= result_sign;
|
||||
|
||||
// Final rounding. The result may overflow to infinity, but that is the
|
||||
@ -176,20 +178,19 @@ macro_rules! add {
|
||||
if round_guard_sticky > 0x4 { result += one; }
|
||||
if round_guard_sticky == 0x4 { result += result & one; }
|
||||
|
||||
<$ty>::from_repr(result)
|
||||
})
|
||||
F::from_repr(result)
|
||||
}
|
||||
|
||||
intrinsics! {
|
||||
#[aapcs_on_arm]
|
||||
#[arm_aeabi_alias = __aeabi_fadd]
|
||||
pub extern "C" fn __addsf3(a: f32, b: f32) -> f32 {
|
||||
add!(a, b, f32)
|
||||
add(a, b)
|
||||
}
|
||||
|
||||
#[aapcs_on_arm]
|
||||
#[arm_aeabi_alias = __aeabi_dadd]
|
||||
pub extern "C" fn __adddf3(a: f64, b: f64) -> f64 {
|
||||
add!(a, b, f64)
|
||||
add(a, b)
|
||||
}
|
||||
}
|
||||
|
@ -25,6 +25,7 @@ pub trait Int:
|
||||
ops::AddAssign +
|
||||
ops::BitAndAssign +
|
||||
ops::BitOrAssign +
|
||||
ops::ShlAssign<i32> +
|
||||
ops::ShrAssign<u32> +
|
||||
ops::Add<Output = Self> +
|
||||
ops::Sub<Output = Self> +
|
||||
|
Loading…
Reference in New Issue
Block a user