Auto merge of #192 - est31:master, r=alexcrichton
Refactor float implementation Refactors the float implementation. Fixes #169. Parts of the PR were inspired a previous PR by @mattico .
This commit is contained in:
commit
35dec6bd8a
316
src/float/add.rs
316
src/float/add.rs
|
@ -1,196 +1,196 @@
|
||||||
use core::num::Wrapping;
|
use int::{Int, CastInto};
|
||||||
|
|
||||||
use float::Float;
|
use float::Float;
|
||||||
|
|
||||||
/// Returns `a + b`
|
/// Returns `a + b`
|
||||||
macro_rules! add {
|
fn add<F: Float>(a: F, b: F) -> F where
|
||||||
($a:expr, $b:expr, $ty:ty) => ({
|
u32: CastInto<F::Int>,
|
||||||
let a = $a;
|
F::Int: CastInto<u32>,
|
||||||
let b = $b;
|
i32: CastInto<F::Int>,
|
||||||
let one = Wrapping(1 as <$ty as Float>::Int);
|
F::Int: CastInto<i32>,
|
||||||
let zero = Wrapping(0 as <$ty as Float>::Int);
|
{
|
||||||
|
let one = F::Int::ONE;
|
||||||
|
let zero = F::Int::ZERO;
|
||||||
|
|
||||||
let bits = Wrapping(<$ty>::BITS as <$ty as Float>::Int);
|
let bits = F::BITS.cast();
|
||||||
let significand_bits = Wrapping(<$ty>::SIGNIFICAND_BITS as <$ty as Float>::Int);
|
let significand_bits = F::SIGNIFICAND_BITS;
|
||||||
let exponent_bits = bits - significand_bits - one;
|
let max_exponent = F::EXPONENT_MAX;
|
||||||
let max_exponent = (one << exponent_bits.0 as usize) - one;
|
|
||||||
|
|
||||||
let implicit_bit = one << significand_bits.0 as usize;
|
let implicit_bit = F::IMPLICIT_BIT;
|
||||||
let significand_mask = implicit_bit - one;
|
let significand_mask = F::SIGNIFICAND_MASK;
|
||||||
let sign_bit = one << (significand_bits + exponent_bits).0 as usize;
|
let sign_bit = F::SIGN_MASK as F::Int;
|
||||||
let abs_mask = sign_bit - one;
|
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 inf_rep = exponent_mask;
|
||||||
let quiet_bit = implicit_bit >> 1;
|
let quiet_bit = implicit_bit >> 1;
|
||||||
let qnan_rep = exponent_mask | quiet_bit;
|
let qnan_rep = exponent_mask | quiet_bit;
|
||||||
|
|
||||||
let mut a_rep = Wrapping(a.repr());
|
let mut a_rep = a.repr();
|
||||||
let mut b_rep = Wrapping(b.repr());
|
let mut b_rep = b.repr();
|
||||||
let a_abs = a_rep & abs_mask;
|
let a_abs = a_rep & abs_mask;
|
||||||
let b_abs = b_rep & abs_mask;
|
let b_abs = b_rep & abs_mask;
|
||||||
|
|
||||||
// Detect if a or b is zero, infinity, or NaN.
|
// Detect if a or b is zero, infinity, or NaN.
|
||||||
if a_abs - one >= inf_rep - one ||
|
if a_abs.wrapping_sub(one) >= inf_rep - one ||
|
||||||
b_abs - one >= inf_rep - one {
|
b_abs.wrapping_sub(one) >= inf_rep - one {
|
||||||
// NaN + anything = qNaN
|
// NaN + anything = qNaN
|
||||||
if a_abs > inf_rep {
|
if a_abs > inf_rep {
|
||||||
return <$ty as Float>::from_repr((a_abs | quiet_bit).0);
|
return F::from_repr(a_abs | quiet_bit);
|
||||||
}
|
}
|
||||||
// anything + NaN = qNaN
|
// anything + NaN = qNaN
|
||||||
if b_abs > inf_rep {
|
if b_abs > inf_rep {
|
||||||
return <$ty as Float>::from_repr((b_abs | quiet_bit).0);
|
return F::from_repr(b_abs | quiet_bit);
|
||||||
|
}
|
||||||
|
|
||||||
|
if a_abs == inf_rep {
|
||||||
|
// +/-infinity + -/+infinity = qNaN
|
||||||
|
if (a.repr() ^ b.repr()) == sign_bit {
|
||||||
|
return F::from_repr(qnan_rep);
|
||||||
|
} else {
|
||||||
|
// +/-infinity + anything remaining = +/- infinity
|
||||||
|
return a;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if a_abs == inf_rep {
|
// anything remaining + +/-infinity = +/-infinity
|
||||||
// +/-infinity + -/+infinity = qNaN
|
if b_abs == inf_rep {
|
||||||
if (a.repr() ^ b.repr()) == sign_bit.0 {
|
return b;
|
||||||
return <$ty as Float>::from_repr(qnan_rep.0);
|
}
|
||||||
} else {
|
|
||||||
// +/-infinity + anything remaining = +/- infinity
|
|
||||||
return a;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// anything remaining + +/-infinity = +/-infinity
|
// zero + anything = anything
|
||||||
if b_abs == inf_rep {
|
if a_abs == Int::ZERO {
|
||||||
|
// but we need to get the sign right for zero + zero
|
||||||
|
if b_abs == Int::ZERO {
|
||||||
|
return F::from_repr(a.repr() & b.repr());
|
||||||
|
} else {
|
||||||
return b;
|
return b;
|
||||||
}
|
}
|
||||||
|
|
||||||
// zero + anything = anything
|
|
||||||
if a_abs.0 == 0 {
|
|
||||||
// but we need to get the sign right for zero + zero
|
|
||||||
if b_abs.0 == 0 {
|
|
||||||
return <$ty as Float>::from_repr(a.repr() & b.repr());
|
|
||||||
} else {
|
|
||||||
return b;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// anything + zero = anything
|
|
||||||
if b_abs.0 == 0 {
|
|
||||||
return a;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Swap a and b if necessary so that a has the larger absolute value.
|
// anything + zero = anything
|
||||||
if b_abs > a_abs {
|
if b_abs == Int::ZERO {
|
||||||
// Don't use mem::swap because it may generate references to memcpy in unoptimized code.
|
return a;
|
||||||
let tmp = a_rep;
|
}
|
||||||
a_rep = b_rep;
|
}
|
||||||
b_rep = tmp;
|
|
||||||
|
// Swap a and b if necessary so that a has the larger absolute value.
|
||||||
|
if b_abs > a_abs {
|
||||||
|
// Don't use mem::swap because it may generate references to memcpy in unoptimized code.
|
||||||
|
let tmp = a_rep;
|
||||||
|
a_rep = b_rep;
|
||||||
|
b_rep = tmp;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Extract the exponent and significand from the (possibly swapped) a and b.
|
||||||
|
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) = F::normalize(a_significand);
|
||||||
|
a_exponent = exponent;
|
||||||
|
a_significand = significand;
|
||||||
|
}
|
||||||
|
if b_exponent == 0 {
|
||||||
|
let (exponent, significand) = F::normalize(b_significand);
|
||||||
|
b_exponent = exponent;
|
||||||
|
b_significand = significand;
|
||||||
|
}
|
||||||
|
|
||||||
|
// The sign of the result is the sign of the larger operand, a. If they
|
||||||
|
// have opposite signs, we are performing a subtraction; otherwise addition.
|
||||||
|
let result_sign = a_rep & sign_bit;
|
||||||
|
let subtraction = ((a_rep ^ b_rep) & sign_bit) != zero;
|
||||||
|
|
||||||
|
// Shift the significands to give us round, guard and sticky, and or in the
|
||||||
|
// implicit significand bit. (If we fell through from the denormal path it
|
||||||
|
// was already set by normalize(), but setting it twice won't hurt
|
||||||
|
// anything.)
|
||||||
|
a_significand = (a_significand | implicit_bit) << 3;
|
||||||
|
b_significand = (b_significand | implicit_bit) << 3;
|
||||||
|
|
||||||
|
// 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).cast();
|
||||||
|
if align != Int::ZERO {
|
||||||
|
if align < bits {
|
||||||
|
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.
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if subtraction {
|
||||||
|
a_significand = a_significand.wrapping_sub(b_significand);
|
||||||
|
// If a == -b, return +zero.
|
||||||
|
if a_significand == Int::ZERO {
|
||||||
|
return F::from_repr(Int::ZERO);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Extract the exponent and significand from the (possibly swapped) a and b.
|
// If partial cancellation occured, we need to left-shift the result
|
||||||
let mut a_exponent = Wrapping((a_rep >> significand_bits.0 as usize & max_exponent).0 as i32);
|
// and adjust the exponent:
|
||||||
let mut b_exponent = Wrapping((b_rep >> significand_bits.0 as usize & max_exponent).0 as i32);
|
if a_significand < implicit_bit << 3 {
|
||||||
let mut a_significand = a_rep & significand_mask;
|
let shift = a_significand.leading_zeros() as i32
|
||||||
let mut b_significand = b_rep & significand_mask;
|
- (implicit_bit << 3).leading_zeros() as i32;
|
||||||
|
a_significand <<= shift;
|
||||||
// normalize any denormals, and adjust the exponent accordingly.
|
a_exponent -= shift;
|
||||||
if a_exponent.0 == 0 {
|
|
||||||
let (exponent, significand) = <$ty>::normalize(a_significand.0);
|
|
||||||
a_exponent = Wrapping(exponent);
|
|
||||||
a_significand = Wrapping(significand);
|
|
||||||
}
|
}
|
||||||
if b_exponent.0 == 0 {
|
} else /* addition */ {
|
||||||
let (exponent, significand) = <$ty>::normalize(b_significand.0);
|
a_significand += b_significand;
|
||||||
b_exponent = Wrapping(exponent);
|
|
||||||
b_significand = Wrapping(significand);
|
// If the addition carried up, we need to right-shift the result and
|
||||||
|
// adjust the exponent:
|
||||||
|
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;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// The sign of the result is the sign of the larger operand, a. If they
|
// If we have overflowed the type, return +/- infinity:
|
||||||
// have opposite signs, we are performing a subtraction; otherwise addition.
|
if a_exponent >= max_exponent as i32 {
|
||||||
let result_sign = a_rep & sign_bit;
|
return F::from_repr(inf_rep | result_sign);
|
||||||
let subtraction = ((a_rep ^ b_rep) & sign_bit) != zero;
|
}
|
||||||
|
|
||||||
// Shift the significands to give us round, guard and sticky, and or in the
|
if a_exponent <= 0 {
|
||||||
// implicit significand bit. (If we fell through from the denormal path it
|
// Result is denormal before rounding; the exponent is zero and we
|
||||||
// was already set by normalize(), but setting it twice won't hurt
|
// need to shift the significand.
|
||||||
// anything.)
|
let shift = (1 - a_exponent).cast();
|
||||||
a_significand = (a_significand | implicit_bit) << 3;
|
let sticky = F::Int::from_bool((a_significand << bits.wrapping_sub(shift).cast()) != Int::ZERO);
|
||||||
b_significand = (b_significand | implicit_bit) << 3;
|
a_significand = a_significand >> shift.cast() | sticky;
|
||||||
|
a_exponent = 0;
|
||||||
|
}
|
||||||
|
|
||||||
// Shift the significand of b by the difference in exponents, with a sticky
|
// Low three bits are round, guard, and sticky.
|
||||||
// bottom bit to get rounding correct.
|
let a_significand_i32: i32 = a_significand.cast();
|
||||||
let align = Wrapping((a_exponent - b_exponent).0 as <$ty as Float>::Int);
|
let round_guard_sticky: i32 = a_significand_i32 & 0x7;
|
||||||
if align.0 != 0 {
|
|
||||||
if align < bits {
|
|
||||||
let sticky = ((b_significand << (bits - align).0 as usize).0 != 0) as <$ty as Float>::Int;
|
|
||||||
b_significand = (b_significand >> align.0 as usize) | Wrapping(sticky);
|
|
||||||
} else {
|
|
||||||
b_significand = one; // sticky; b is known to be non-zero.
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if subtraction {
|
|
||||||
a_significand -= b_significand;
|
|
||||||
// If a == -b, return +zero.
|
|
||||||
if a_significand.0 == 0 {
|
|
||||||
return <$ty as Float>::from_repr(0);
|
|
||||||
}
|
|
||||||
|
|
||||||
// If partial cancellation occured, we need to left-shift the result
|
// Shift the significand into place, and mask off the implicit bit.
|
||||||
// and adjust the exponent:
|
let mut result = a_significand >> 3 & significand_mask;
|
||||||
if a_significand < implicit_bit << 3 {
|
|
||||||
let shift = a_significand.0.leading_zeros() as i32
|
|
||||||
- (implicit_bit << 3).0.leading_zeros() as i32;
|
|
||||||
a_significand <<= shift as usize;
|
|
||||||
a_exponent -= Wrapping(shift);
|
|
||||||
}
|
|
||||||
} else /* addition */ {
|
|
||||||
a_significand += b_significand;
|
|
||||||
|
|
||||||
// If the addition carried up, we need to right-shift the result and
|
// Insert the exponent and sign.
|
||||||
// adjust the exponent:
|
result |= a_exponent.cast() << significand_bits;
|
||||||
if (a_significand & implicit_bit << 4).0 != 0 {
|
result |= result_sign;
|
||||||
let sticky = ((a_significand & one).0 != 0) as <$ty as Float>::Int;
|
|
||||||
a_significand = a_significand >> 1 | Wrapping(sticky);
|
|
||||||
a_exponent += Wrapping(1);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// If we have overflowed the type, return +/- infinity:
|
// Final rounding. The result may overflow to infinity, but that is the
|
||||||
if a_exponent >= Wrapping(max_exponent.0 as i32) {
|
// correct result in that case.
|
||||||
return <$ty>::from_repr((inf_rep | result_sign).0);
|
if round_guard_sticky > 0x4 { result += one; }
|
||||||
}
|
if round_guard_sticky == 0x4 { result += result & one; }
|
||||||
|
|
||||||
if a_exponent.0 <= 0 {
|
F::from_repr(result)
|
||||||
// Result is denormal before rounding; the exponent is zero and we
|
|
||||||
// need to shift the significand.
|
|
||||||
let shift = Wrapping((Wrapping(1) - a_exponent).0 as <$ty as Float>::Int);
|
|
||||||
let sticky = ((a_significand << (bits - shift).0 as usize).0 != 0) as <$ty as Float>::Int;
|
|
||||||
a_significand = a_significand >> shift.0 as usize | Wrapping(sticky);
|
|
||||||
a_exponent = Wrapping(0);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Low three bits are round, guard, and sticky.
|
|
||||||
let round_guard_sticky: i32 = (a_significand.0 & 0x7) as i32;
|
|
||||||
|
|
||||||
// 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 |= Wrapping(a_exponent.0 as <$ty as Float>::Int) << significand_bits.0 as usize;
|
|
||||||
result |= result_sign;
|
|
||||||
|
|
||||||
// Final rounding. The result may overflow to infinity, but that is the
|
|
||||||
// correct result in that case.
|
|
||||||
if round_guard_sticky > 0x4 { result += one; }
|
|
||||||
if round_guard_sticky == 0x4 { result += result & one; }
|
|
||||||
|
|
||||||
<$ty>::from_repr(result.0)
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
intrinsics! {
|
intrinsics! {
|
||||||
#[aapcs_on_arm]
|
#[aapcs_on_arm]
|
||||||
#[arm_aeabi_alias = __aeabi_fadd]
|
#[arm_aeabi_alias = __aeabi_fadd]
|
||||||
pub extern "C" fn __addsf3(a: f32, b: f32) -> f32 {
|
pub extern "C" fn __addsf3(a: f32, b: f32) -> f32 {
|
||||||
add!(a, b, f32)
|
add(a, b)
|
||||||
}
|
}
|
||||||
|
|
||||||
#[aapcs_on_arm]
|
#[aapcs_on_arm]
|
||||||
#[arm_aeabi_alias = __aeabi_dadd]
|
#[arm_aeabi_alias = __aeabi_dadd]
|
||||||
pub extern "C" fn __adddf3(a: f64, b: f64) -> f64 {
|
pub extern "C" fn __adddf3(a: f64, b: f64) -> f64 {
|
||||||
add!(a, b, f64)
|
add(a, b)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,83 +1,87 @@
|
||||||
use float::Float;
|
use float::Float;
|
||||||
use int::Int;
|
use int::{Int, CastInto};
|
||||||
|
|
||||||
macro_rules! int_to_float {
|
fn int_to_float<I: Int, F: Float>(i: I) -> F where
|
||||||
($i:expr, $ity:ty, $fty:ty) => ({
|
F::Int: CastInto<u32>,
|
||||||
let i = $i;
|
F::Int: CastInto<I>,
|
||||||
if i == 0 {
|
I::UnsignedInt: CastInto<F::Int>,
|
||||||
return 0.0
|
u32: CastInto<F::Int>,
|
||||||
}
|
{
|
||||||
|
if i == I::ZERO {
|
||||||
|
return F::ZERO;
|
||||||
|
}
|
||||||
|
|
||||||
let mant_dig = <$fty>::SIGNIFICAND_BITS + 1;
|
let two = I::UnsignedInt::ONE + I::UnsignedInt::ONE;
|
||||||
let exponent_bias = <$fty>::EXPONENT_BIAS;
|
let four = two + two;
|
||||||
|
let mant_dig = F::SIGNIFICAND_BITS + 1;
|
||||||
|
let exponent_bias = F::EXPONENT_BIAS;
|
||||||
|
|
||||||
let n = <$ity>::BITS;
|
let n = I::BITS;
|
||||||
let (s, a) = i.extract_sign();
|
let (s, a) = i.extract_sign();
|
||||||
let mut a = a;
|
let mut a = a;
|
||||||
|
|
||||||
// number of significant digits
|
// number of significant digits
|
||||||
let sd = n - a.leading_zeros();
|
let sd = n - a.leading_zeros();
|
||||||
|
|
||||||
// exponent
|
// exponent
|
||||||
let mut e = sd - 1;
|
let mut e = sd - 1;
|
||||||
|
|
||||||
if <$ity>::BITS < mant_dig {
|
if I::BITS < mant_dig {
|
||||||
return <$fty>::from_parts(s,
|
return F::from_parts(s,
|
||||||
(e + exponent_bias) as <$fty as Float>::Int,
|
(e + exponent_bias).cast(),
|
||||||
(a as <$fty as Float>::Int) << (mant_dig - e - 1))
|
a.cast() << (mant_dig - e - 1));
|
||||||
}
|
}
|
||||||
|
|
||||||
a = if sd > mant_dig {
|
a = if sd > mant_dig {
|
||||||
/* start: 0000000000000000000001xxxxxxxxxxxxxxxxxxxxxxPQxxxxxxxxxxxxxxxxxx
|
/* start: 0000000000000000000001xxxxxxxxxxxxxxxxxxxxxxPQxxxxxxxxxxxxxxxxxx
|
||||||
* finish: 000000000000000000000000000000000000001xxxxxxxxxxxxxxxxxxxxxxPQR
|
* finish: 000000000000000000000000000000000000001xxxxxxxxxxxxxxxxxxxxxxPQR
|
||||||
* 12345678901234567890123456
|
* 12345678901234567890123456
|
||||||
* 1 = msb 1 bit
|
* 1 = msb 1 bit
|
||||||
* P = bit MANT_DIG-1 bits to the right of 1
|
* P = bit MANT_DIG-1 bits to the right of 1
|
||||||
* Q = bit MANT_DIG bits to the right of 1
|
* Q = bit MANT_DIG bits to the right of 1
|
||||||
* R = "or" of all bits to the right of Q
|
* R = "or" of all bits to the right of Q
|
||||||
*/
|
*/
|
||||||
let mant_dig_plus_one = mant_dig + 1;
|
let mant_dig_plus_one = mant_dig + 1;
|
||||||
let mant_dig_plus_two = mant_dig + 2;
|
let mant_dig_plus_two = mant_dig + 2;
|
||||||
a = if sd == mant_dig_plus_one {
|
a = if sd == mant_dig_plus_one {
|
||||||
a << 1
|
a << 1
|
||||||
} else if sd == mant_dig_plus_two {
|
} else if sd == mant_dig_plus_two {
|
||||||
a
|
|
||||||
} else {
|
|
||||||
(a >> (sd - mant_dig_plus_two)) as <$ity as Int>::UnsignedInt |
|
|
||||||
((a & <$ity as Int>::UnsignedInt::max_value()).wrapping_shl((n + mant_dig_plus_two) - sd) != 0) as <$ity as Int>::UnsignedInt
|
|
||||||
};
|
|
||||||
|
|
||||||
/* finish: */
|
|
||||||
a |= ((a & 4) != 0) as <$ity as Int>::UnsignedInt; /* Or P into R */
|
|
||||||
a += 1; /* round - this step may add a significant bit */
|
|
||||||
a >>= 2; /* dump Q and R */
|
|
||||||
|
|
||||||
/* a is now rounded to mant_dig or mant_dig+1 bits */
|
|
||||||
if (a & (1 << mant_dig)) != 0 {
|
|
||||||
a >>= 1; e += 1;
|
|
||||||
}
|
|
||||||
a
|
a
|
||||||
/* a is now rounded to mant_dig bits */
|
|
||||||
} else {
|
} else {
|
||||||
a.wrapping_shl(mant_dig - sd)
|
(a >> (sd - mant_dig_plus_two)) |
|
||||||
/* a is now rounded to mant_dig bits */
|
Int::from_bool((a & I::UnsignedInt::max_value()).wrapping_shl((n + mant_dig_plus_two) - sd) != Int::ZERO)
|
||||||
};
|
};
|
||||||
|
|
||||||
<$fty>::from_parts(s,
|
/* finish: */
|
||||||
(e + exponent_bias) as <$fty as Float>::Int,
|
a |= Int::from_bool((a & four) != I::UnsignedInt::ZERO); /* Or P into R */
|
||||||
a as <$fty as Float>::Int)
|
a += Int::ONE; /* round - this step may add a significant bit */
|
||||||
})
|
a >>= 2; /* dump Q and R */
|
||||||
|
|
||||||
|
/* a is now rounded to mant_dig or mant_dig+1 bits */
|
||||||
|
if (a & (I::UnsignedInt::ONE << mant_dig)) != Int::ZERO {
|
||||||
|
a >>= 1; e += 1;
|
||||||
|
}
|
||||||
|
a
|
||||||
|
/* a is now rounded to mant_dig bits */
|
||||||
|
} else {
|
||||||
|
a.wrapping_shl(mant_dig - sd)
|
||||||
|
/* a is now rounded to mant_dig bits */
|
||||||
|
};
|
||||||
|
|
||||||
|
F::from_parts(s,
|
||||||
|
(e + exponent_bias).cast(),
|
||||||
|
a.cast())
|
||||||
}
|
}
|
||||||
|
|
||||||
intrinsics! {
|
intrinsics! {
|
||||||
#[arm_aeabi_alias = __aeabi_i2f]
|
#[arm_aeabi_alias = __aeabi_i2f]
|
||||||
pub extern "C" fn __floatsisf(i: i32) -> f32 {
|
pub extern "C" fn __floatsisf(i: i32) -> f32 {
|
||||||
int_to_float!(i, i32, f32)
|
int_to_float(i)
|
||||||
}
|
}
|
||||||
|
|
||||||
#[arm_aeabi_alias = __aeabi_i2d]
|
#[arm_aeabi_alias = __aeabi_i2d]
|
||||||
pub extern "C" fn __floatsidf(i: i32) -> f64 {
|
pub extern "C" fn __floatsidf(i: i32) -> f64 {
|
||||||
int_to_float!(i, i32, f64)
|
int_to_float(i)
|
||||||
}
|
}
|
||||||
|
|
||||||
#[use_c_shim_if(all(target_arch = "x86", not(target_env = "msvc")))]
|
#[use_c_shim_if(all(target_arch = "x86", not(target_env = "msvc")))]
|
||||||
|
@ -88,28 +92,28 @@ intrinsics! {
|
||||||
if cfg!(target_arch = "x86_64") {
|
if cfg!(target_arch = "x86_64") {
|
||||||
i as f64
|
i as f64
|
||||||
} else {
|
} else {
|
||||||
int_to_float!(i, i64, f64)
|
int_to_float(i)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[unadjusted_on_win64]
|
#[unadjusted_on_win64]
|
||||||
pub extern "C" fn __floattisf(i: i128) -> f32 {
|
pub extern "C" fn __floattisf(i: i128) -> f32 {
|
||||||
int_to_float!(i, i128, f32)
|
int_to_float(i)
|
||||||
}
|
}
|
||||||
|
|
||||||
#[unadjusted_on_win64]
|
#[unadjusted_on_win64]
|
||||||
pub extern "C" fn __floattidf(i: i128) -> f64 {
|
pub extern "C" fn __floattidf(i: i128) -> f64 {
|
||||||
int_to_float!(i, i128, f64)
|
int_to_float(i)
|
||||||
}
|
}
|
||||||
|
|
||||||
#[arm_aeabi_alias = __aeabi_ui2f]
|
#[arm_aeabi_alias = __aeabi_ui2f]
|
||||||
pub extern "C" fn __floatunsisf(i: u32) -> f32 {
|
pub extern "C" fn __floatunsisf(i: u32) -> f32 {
|
||||||
int_to_float!(i, u32, f32)
|
int_to_float(i)
|
||||||
}
|
}
|
||||||
|
|
||||||
#[arm_aeabi_alias = __aeabi_ui2d]
|
#[arm_aeabi_alias = __aeabi_ui2d]
|
||||||
pub extern "C" fn __floatunsidf(i: u32) -> f64 {
|
pub extern "C" fn __floatunsidf(i: u32) -> f64 {
|
||||||
int_to_float!(i, u32, f64)
|
int_to_float(i)
|
||||||
}
|
}
|
||||||
|
|
||||||
#[use_c_shim_if(all(not(target_env = "msvc"),
|
#[use_c_shim_if(all(not(target_env = "msvc"),
|
||||||
|
@ -117,17 +121,17 @@ intrinsics! {
|
||||||
all(not(windows), target_arch = "x86_64"))))]
|
all(not(windows), target_arch = "x86_64"))))]
|
||||||
#[arm_aeabi_alias = __aeabi_ul2d]
|
#[arm_aeabi_alias = __aeabi_ul2d]
|
||||||
pub extern "C" fn __floatundidf(i: u64) -> f64 {
|
pub extern "C" fn __floatundidf(i: u64) -> f64 {
|
||||||
int_to_float!(i, u64, f64)
|
int_to_float(i)
|
||||||
}
|
}
|
||||||
|
|
||||||
#[unadjusted_on_win64]
|
#[unadjusted_on_win64]
|
||||||
pub extern "C" fn __floatuntisf(i: u128) -> f32 {
|
pub extern "C" fn __floatuntisf(i: u128) -> f32 {
|
||||||
int_to_float!(i, u128, f32)
|
int_to_float(i)
|
||||||
}
|
}
|
||||||
|
|
||||||
#[unadjusted_on_win64]
|
#[unadjusted_on_win64]
|
||||||
pub extern "C" fn __floatuntidf(i: u128) -> f64 {
|
pub extern "C" fn __floatuntidf(i: u128) -> f64 {
|
||||||
int_to_float!(i, u128, f64)
|
int_to_float(i)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -137,115 +141,116 @@ enum Sign {
|
||||||
Negative
|
Negative
|
||||||
}
|
}
|
||||||
|
|
||||||
macro_rules! float_to_int {
|
fn float_to_int<F: Float, I: Int>(f: F) -> I where
|
||||||
($f:expr, $fty:ty, $ity:ty) => ({
|
F::Int: CastInto<u32>,
|
||||||
let f = $f;
|
F::Int: CastInto<I>,
|
||||||
let fixint_min = <$ity>::min_value();
|
{
|
||||||
let fixint_max = <$ity>::max_value();
|
let f = f;
|
||||||
let fixint_bits = <$ity>::BITS as usize;
|
let fixint_min = I::min_value();
|
||||||
let fixint_unsigned = fixint_min == 0;
|
let fixint_max = I::max_value();
|
||||||
|
let fixint_bits = I::BITS;
|
||||||
|
let fixint_unsigned = fixint_min == I::ZERO;
|
||||||
|
|
||||||
let sign_bit = <$fty>::SIGN_MASK;
|
let sign_bit = F::SIGN_MASK;
|
||||||
let significand_bits = <$fty>::SIGNIFICAND_BITS as usize;
|
let significand_bits = F::SIGNIFICAND_BITS;
|
||||||
let exponent_bias = <$fty>::EXPONENT_BIAS as usize;
|
let exponent_bias = F::EXPONENT_BIAS;
|
||||||
//let exponent_max = <$fty>::exponent_max() as usize;
|
//let exponent_max = F::exponent_max() as usize;
|
||||||
|
|
||||||
// Break a into sign, exponent, significand
|
// Break a into sign, exponent, significand
|
||||||
let a_rep = <$fty>::repr(f);
|
let a_rep = F::repr(f);
|
||||||
let a_abs = a_rep & !sign_bit;
|
let a_abs = a_rep & !sign_bit;
|
||||||
|
|
||||||
// this is used to work around -1 not being available for unsigned
|
// this is used to work around -1 not being available for unsigned
|
||||||
let sign = if (a_rep & sign_bit) == 0 { Sign::Positive } else { Sign::Negative };
|
let sign = if (a_rep & sign_bit) == F::Int::ZERO { Sign::Positive } else { Sign::Negative };
|
||||||
let mut exponent = (a_abs >> significand_bits) as usize;
|
let mut exponent: u32 = (a_abs >> significand_bits).cast();
|
||||||
let significand = (a_abs & <$fty>::SIGNIFICAND_MASK) | <$fty>::IMPLICIT_BIT;
|
let significand = (a_abs & F::SIGNIFICAND_MASK) | F::IMPLICIT_BIT;
|
||||||
|
|
||||||
// if < 1 or unsigned & negative
|
// if < 1 or unsigned & negative
|
||||||
if exponent < exponent_bias ||
|
if exponent < exponent_bias ||
|
||||||
fixint_unsigned && sign == Sign::Negative {
|
fixint_unsigned && sign == Sign::Negative {
|
||||||
return 0
|
return I::ZERO;
|
||||||
}
|
}
|
||||||
exponent -= exponent_bias;
|
exponent -= exponent_bias;
|
||||||
|
|
||||||
// If the value is infinity, saturate.
|
// If the value is infinity, saturate.
|
||||||
// If the value is too large for the integer type, 0.
|
// If the value is too large for the integer type, 0.
|
||||||
if exponent >= (if fixint_unsigned {fixint_bits} else {fixint_bits -1}) {
|
if exponent >= (if fixint_unsigned {fixint_bits} else {fixint_bits -1}) {
|
||||||
return if sign == Sign::Positive {fixint_max} else {fixint_min}
|
return if sign == Sign::Positive {fixint_max} else {fixint_min}
|
||||||
}
|
}
|
||||||
// If 0 <= exponent < significand_bits, right shift to get the result.
|
// If 0 <= exponent < significand_bits, right shift to get the result.
|
||||||
// Otherwise, shift left.
|
// Otherwise, shift left.
|
||||||
// (sign - 1) will never overflow as negative signs are already returned as 0 for unsigned
|
// (sign - 1) will never overflow as negative signs are already returned as 0 for unsigned
|
||||||
let r = if exponent < significand_bits {
|
let r: I = if exponent < significand_bits {
|
||||||
(significand >> (significand_bits - exponent)) as $ity
|
(significand >> (significand_bits - exponent)).cast()
|
||||||
} else {
|
} else {
|
||||||
(significand as $ity) << (exponent - significand_bits)
|
(significand << (exponent - significand_bits)).cast()
|
||||||
};
|
};
|
||||||
|
|
||||||
if sign == Sign::Negative {
|
if sign == Sign::Negative {
|
||||||
(!r).wrapping_add(1)
|
(!r).wrapping_add(I::ONE)
|
||||||
} else {
|
} else {
|
||||||
r
|
r
|
||||||
}
|
}
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
intrinsics! {
|
intrinsics! {
|
||||||
#[arm_aeabi_alias = __aeabi_f2iz]
|
#[arm_aeabi_alias = __aeabi_f2iz]
|
||||||
pub extern "C" fn __fixsfsi(f: f32) -> i32 {
|
pub extern "C" fn __fixsfsi(f: f32) -> i32 {
|
||||||
float_to_int!(f, f32, i32)
|
float_to_int(f)
|
||||||
}
|
}
|
||||||
|
|
||||||
#[arm_aeabi_alias = __aeabi_f2lz]
|
#[arm_aeabi_alias = __aeabi_f2lz]
|
||||||
pub extern "C" fn __fixsfdi(f: f32) -> i64 {
|
pub extern "C" fn __fixsfdi(f: f32) -> i64 {
|
||||||
float_to_int!(f, f32, i64)
|
float_to_int(f)
|
||||||
}
|
}
|
||||||
|
|
||||||
#[unadjusted_on_win64]
|
#[unadjusted_on_win64]
|
||||||
pub extern "C" fn __fixsfti(f: f32) -> i128 {
|
pub extern "C" fn __fixsfti(f: f32) -> i128 {
|
||||||
float_to_int!(f, f32, i128)
|
float_to_int(f)
|
||||||
}
|
}
|
||||||
|
|
||||||
#[arm_aeabi_alias = __aeabi_d2iz]
|
#[arm_aeabi_alias = __aeabi_d2iz]
|
||||||
pub extern "C" fn __fixdfsi(f: f64) -> i32 {
|
pub extern "C" fn __fixdfsi(f: f64) -> i32 {
|
||||||
float_to_int!(f, f64, i32)
|
float_to_int(f)
|
||||||
}
|
}
|
||||||
|
|
||||||
#[arm_aeabi_alias = __aeabi_d2lz]
|
#[arm_aeabi_alias = __aeabi_d2lz]
|
||||||
pub extern "C" fn __fixdfdi(f: f64) -> i64 {
|
pub extern "C" fn __fixdfdi(f: f64) -> i64 {
|
||||||
float_to_int!(f, f64, i64)
|
float_to_int(f)
|
||||||
}
|
}
|
||||||
|
|
||||||
#[unadjusted_on_win64]
|
#[unadjusted_on_win64]
|
||||||
pub extern "C" fn __fixdfti(f: f64) -> i128 {
|
pub extern "C" fn __fixdfti(f: f64) -> i128 {
|
||||||
float_to_int!(f, f64, i128)
|
float_to_int(f)
|
||||||
}
|
}
|
||||||
|
|
||||||
#[arm_aeabi_alias = __aeabi_f2uiz]
|
#[arm_aeabi_alias = __aeabi_f2uiz]
|
||||||
pub extern "C" fn __fixunssfsi(f: f32) -> u32 {
|
pub extern "C" fn __fixunssfsi(f: f32) -> u32 {
|
||||||
float_to_int!(f, f32, u32)
|
float_to_int(f)
|
||||||
}
|
}
|
||||||
|
|
||||||
#[arm_aeabi_alias = __aeabi_f2ulz]
|
#[arm_aeabi_alias = __aeabi_f2ulz]
|
||||||
pub extern "C" fn __fixunssfdi(f: f32) -> u64 {
|
pub extern "C" fn __fixunssfdi(f: f32) -> u64 {
|
||||||
float_to_int!(f, f32, u64)
|
float_to_int(f)
|
||||||
}
|
}
|
||||||
|
|
||||||
#[unadjusted_on_win64]
|
#[unadjusted_on_win64]
|
||||||
pub extern "C" fn __fixunssfti(f: f32) -> u128 {
|
pub extern "C" fn __fixunssfti(f: f32) -> u128 {
|
||||||
float_to_int!(f, f32, u128)
|
float_to_int(f)
|
||||||
}
|
}
|
||||||
|
|
||||||
#[arm_aeabi_alias = __aeabi_d2uiz]
|
#[arm_aeabi_alias = __aeabi_d2uiz]
|
||||||
pub extern "C" fn __fixunsdfsi(f: f64) -> u32 {
|
pub extern "C" fn __fixunsdfsi(f: f64) -> u32 {
|
||||||
float_to_int!(f, f64, u32)
|
float_to_int(f)
|
||||||
}
|
}
|
||||||
|
|
||||||
#[arm_aeabi_alias = __aeabi_d2ulz]
|
#[arm_aeabi_alias = __aeabi_d2ulz]
|
||||||
pub extern "C" fn __fixunsdfdi(f: f64) -> u64 {
|
pub extern "C" fn __fixunsdfdi(f: f64) -> u64 {
|
||||||
float_to_int!(f, f64, u64)
|
float_to_int(f)
|
||||||
}
|
}
|
||||||
|
|
||||||
#[unadjusted_on_win64]
|
#[unadjusted_on_win64]
|
||||||
pub extern "C" fn __fixunsdfti(f: f64) -> u128 {
|
pub extern "C" fn __fixunsdfti(f: f64) -> u128 {
|
||||||
float_to_int!(f, f64, u128)
|
float_to_int(f)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,4 +1,5 @@
|
||||||
use core::mem;
|
use core::mem;
|
||||||
|
use core::ops;
|
||||||
|
|
||||||
use super::int::Int;
|
use super::int::Int;
|
||||||
|
|
||||||
|
@ -8,10 +9,23 @@ pub mod pow;
|
||||||
pub mod sub;
|
pub mod sub;
|
||||||
|
|
||||||
/// Trait for some basic operations on floats
|
/// Trait for some basic operations on floats
|
||||||
pub trait Float: Sized + Copy {
|
pub trait Float:
|
||||||
|
Copy +
|
||||||
|
PartialEq +
|
||||||
|
PartialOrd +
|
||||||
|
ops::AddAssign +
|
||||||
|
ops::MulAssign +
|
||||||
|
ops::Add<Output = Self> +
|
||||||
|
ops::Sub<Output = Self> +
|
||||||
|
ops::Div<Output = Self> +
|
||||||
|
ops::Rem<Output = Self> +
|
||||||
|
{
|
||||||
/// A uint of the same with as the float
|
/// A uint of the same with as the float
|
||||||
type Int: Int;
|
type Int: Int;
|
||||||
|
|
||||||
|
const ZERO: Self;
|
||||||
|
const ONE: Self;
|
||||||
|
|
||||||
/// The bitwidth of the float type
|
/// The bitwidth of the float type
|
||||||
const BITS: u32;
|
const BITS: u32;
|
||||||
|
|
||||||
|
@ -64,6 +78,9 @@ macro_rules! float_impl {
|
||||||
($ty:ident, $ity:ident, $bits:expr, $significand_bits:expr) => {
|
($ty:ident, $ity:ident, $bits:expr, $significand_bits:expr) => {
|
||||||
impl Float for $ty {
|
impl Float for $ty {
|
||||||
type Int = $ity;
|
type Int = $ity;
|
||||||
|
const ZERO: Self = 0.0;
|
||||||
|
const ONE: Self = 1.0;
|
||||||
|
|
||||||
const BITS: u32 = $bits;
|
const BITS: u32 = $bits;
|
||||||
const SIGNIFICAND_BITS: u32 = $significand_bits;
|
const SIGNIFICAND_BITS: u32 = $significand_bits;
|
||||||
|
|
||||||
|
|
|
@ -1,11 +1,12 @@
|
||||||
use int::Int;
|
use int::Int;
|
||||||
|
use float::Float;
|
||||||
|
|
||||||
/// Returns `a` raised to the power `b`
|
trait Pow: Float {
|
||||||
macro_rules! pow {
|
/// Returns `a` raised to the power `b`
|
||||||
($a: expr, $b: expr) => ({
|
fn pow(self, mut b: i32) -> Self {
|
||||||
let (mut a, mut b) = ($a, $b);
|
let mut a = self;
|
||||||
let recip = b < 0;
|
let recip = b < 0;
|
||||||
let mut r = 1.0;
|
let mut r = Self::ONE;
|
||||||
loop {
|
loop {
|
||||||
if (b & 1) != 0 {
|
if (b & 1) != 0 {
|
||||||
r *= a;
|
r *= a;
|
||||||
|
@ -18,19 +19,22 @@ macro_rules! pow {
|
||||||
}
|
}
|
||||||
|
|
||||||
if recip {
|
if recip {
|
||||||
1.0 / r
|
Self::ONE / r
|
||||||
} else {
|
} else {
|
||||||
r
|
r
|
||||||
}
|
}
|
||||||
})
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl Pow for f32 {}
|
||||||
|
impl Pow for f64 {}
|
||||||
|
|
||||||
intrinsics! {
|
intrinsics! {
|
||||||
pub extern "C" fn __powisf2(a: f32, b: i32) -> f32 {
|
pub extern "C" fn __powisf2(a: f32, b: i32) -> f32 {
|
||||||
pow!(a, b)
|
a.pow(b)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub extern "C" fn __powidf2(a: f64, b: i32) -> f64 {
|
pub extern "C" fn __powidf2(a: f64, b: i32) -> f64 {
|
||||||
pow!(a, b)
|
a.pow(b)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
117
src/int/mod.rs
117
src/int/mod.rs
|
@ -23,6 +23,10 @@ pub trait Int:
|
||||||
PartialEq +
|
PartialEq +
|
||||||
PartialOrd +
|
PartialOrd +
|
||||||
ops::AddAssign +
|
ops::AddAssign +
|
||||||
|
ops::BitAndAssign +
|
||||||
|
ops::BitOrAssign +
|
||||||
|
ops::ShlAssign<i32> +
|
||||||
|
ops::ShrAssign<u32> +
|
||||||
ops::Add<Output = Self> +
|
ops::Add<Output = Self> +
|
||||||
ops::Sub<Output = Self> +
|
ops::Sub<Output = Self> +
|
||||||
ops::Div<Output = Self> +
|
ops::Div<Output = Self> +
|
||||||
|
@ -31,7 +35,6 @@ pub trait Int:
|
||||||
ops::BitOr<Output = Self> +
|
ops::BitOr<Output = Self> +
|
||||||
ops::BitXor<Output = Self> +
|
ops::BitXor<Output = Self> +
|
||||||
ops::BitAnd<Output = Self> +
|
ops::BitAnd<Output = Self> +
|
||||||
ops::BitAndAssign +
|
|
||||||
ops::Not<Output = Self> +
|
ops::Not<Output = Self> +
|
||||||
{
|
{
|
||||||
/// Type with the same width but other signedness
|
/// Type with the same width but other signedness
|
||||||
|
@ -60,14 +63,18 @@ pub trait Int:
|
||||||
fn unsigned(self) -> Self::UnsignedInt;
|
fn unsigned(self) -> Self::UnsignedInt;
|
||||||
fn from_unsigned(unsigned: Self::UnsignedInt) -> Self;
|
fn from_unsigned(unsigned: Self::UnsignedInt) -> Self;
|
||||||
|
|
||||||
|
fn from_bool(b: bool) -> Self;
|
||||||
|
|
||||||
// copied from primitive integers, but put in a trait
|
// copied from primitive integers, but put in a trait
|
||||||
fn max_value() -> Self;
|
fn max_value() -> Self;
|
||||||
fn min_value() -> Self;
|
fn min_value() -> Self;
|
||||||
fn wrapping_add(self, other: Self) -> Self;
|
fn wrapping_add(self, other: Self) -> Self;
|
||||||
fn wrapping_mul(self, other: Self) -> Self;
|
fn wrapping_mul(self, other: Self) -> Self;
|
||||||
fn wrapping_sub(self, other: Self) -> Self;
|
fn wrapping_sub(self, other: Self) -> Self;
|
||||||
|
fn wrapping_shl(self, other: u32) -> Self;
|
||||||
fn aborting_div(self, other: Self) -> Self;
|
fn aborting_div(self, other: Self) -> Self;
|
||||||
fn aborting_rem(self, other: Self) -> Self;
|
fn aborting_rem(self, other: Self) -> Self;
|
||||||
|
fn leading_zeros(self) -> u32;
|
||||||
}
|
}
|
||||||
|
|
||||||
fn unwrap<T>(t: Option<T>) -> T {
|
fn unwrap<T>(t: Option<T>) -> T {
|
||||||
|
@ -77,27 +84,15 @@ fn unwrap<T>(t: Option<T>) -> T {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
macro_rules! int_impl {
|
macro_rules! int_impl_common {
|
||||||
($ity:ty, $uty:ty, $bits:expr) => {
|
($ty:ty, $bits:expr) => {
|
||||||
impl Int for $uty {
|
|
||||||
type OtherSign = $ity;
|
|
||||||
type UnsignedInt = $uty;
|
|
||||||
|
|
||||||
const BITS: u32 = $bits;
|
const BITS: u32 = $bits;
|
||||||
|
|
||||||
const ZERO: Self = 0;
|
const ZERO: Self = 0;
|
||||||
const ONE: Self = 1;
|
const ONE: Self = 1;
|
||||||
|
|
||||||
fn extract_sign(self) -> (bool, $uty) {
|
fn from_bool(b: bool) -> Self {
|
||||||
(false, self)
|
b as $ty
|
||||||
}
|
|
||||||
|
|
||||||
fn unsigned(self) -> $uty {
|
|
||||||
self
|
|
||||||
}
|
|
||||||
|
|
||||||
fn from_unsigned(me: $uty) -> Self {
|
|
||||||
me
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn max_value() -> Self {
|
fn max_value() -> Self {
|
||||||
|
@ -120,6 +115,10 @@ macro_rules! int_impl {
|
||||||
<Self>::wrapping_sub(self, other)
|
<Self>::wrapping_sub(self, other)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn wrapping_shl(self, other: u32) -> Self {
|
||||||
|
<Self>::wrapping_shl(self, other)
|
||||||
|
}
|
||||||
|
|
||||||
fn aborting_div(self, other: Self) -> Self {
|
fn aborting_div(self, other: Self) -> Self {
|
||||||
unwrap(<Self>::checked_div(self, other))
|
unwrap(<Self>::checked_div(self, other))
|
||||||
}
|
}
|
||||||
|
@ -127,17 +126,38 @@ macro_rules! int_impl {
|
||||||
fn aborting_rem(self, other: Self) -> Self {
|
fn aborting_rem(self, other: Self) -> Self {
|
||||||
unwrap(<Self>::checked_rem(self, other))
|
unwrap(<Self>::checked_rem(self, other))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn leading_zeros(self) -> u32 {
|
||||||
|
<Self>::leading_zeros(self)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
macro_rules! int_impl {
|
||||||
|
($ity:ty, $uty:ty, $bits:expr) => {
|
||||||
|
impl Int for $uty {
|
||||||
|
type OtherSign = $ity;
|
||||||
|
type UnsignedInt = $uty;
|
||||||
|
|
||||||
|
fn extract_sign(self) -> (bool, $uty) {
|
||||||
|
(false, self)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn unsigned(self) -> $uty {
|
||||||
|
self
|
||||||
|
}
|
||||||
|
|
||||||
|
fn from_unsigned(me: $uty) -> Self {
|
||||||
|
me
|
||||||
|
}
|
||||||
|
|
||||||
|
int_impl_common!($uty, $bits);
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Int for $ity {
|
impl Int for $ity {
|
||||||
type OtherSign = $uty;
|
type OtherSign = $uty;
|
||||||
type UnsignedInt = $uty;
|
type UnsignedInt = $uty;
|
||||||
|
|
||||||
const BITS: u32 = $bits;
|
|
||||||
|
|
||||||
const ZERO: Self = 0;
|
|
||||||
const ONE: Self = 1;
|
|
||||||
|
|
||||||
fn extract_sign(self) -> (bool, $uty) {
|
fn extract_sign(self) -> (bool, $uty) {
|
||||||
if self < 0 {
|
if self < 0 {
|
||||||
(true, (!(self as $uty)).wrapping_add(1))
|
(true, (!(self as $uty)).wrapping_add(1))
|
||||||
|
@ -154,33 +174,7 @@ macro_rules! int_impl {
|
||||||
me as $ity
|
me as $ity
|
||||||
}
|
}
|
||||||
|
|
||||||
fn max_value() -> Self {
|
int_impl_common!($ity, $bits);
|
||||||
<Self>::max_value()
|
|
||||||
}
|
|
||||||
|
|
||||||
fn min_value() -> Self {
|
|
||||||
<Self>::min_value()
|
|
||||||
}
|
|
||||||
|
|
||||||
fn wrapping_add(self, other: Self) -> Self {
|
|
||||||
<Self>::wrapping_add(self, other)
|
|
||||||
}
|
|
||||||
|
|
||||||
fn wrapping_mul(self, other: Self) -> Self {
|
|
||||||
<Self>::wrapping_mul(self, other)
|
|
||||||
}
|
|
||||||
|
|
||||||
fn wrapping_sub(self, other: Self) -> Self {
|
|
||||||
<Self>::wrapping_sub(self, other)
|
|
||||||
}
|
|
||||||
|
|
||||||
fn aborting_div(self, other: Self) -> Self {
|
|
||||||
unwrap(<Self>::checked_div(self, other))
|
|
||||||
}
|
|
||||||
|
|
||||||
fn aborting_rem(self, other: Self) -> Self {
|
|
||||||
unwrap(<Self>::checked_rem(self, other))
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -230,3 +224,28 @@ large_int!(u64, u32, u32, 32);
|
||||||
large_int!(i64, u32, i32, 32);
|
large_int!(i64, u32, i32, 32);
|
||||||
large_int!(u128, u64, u64, 64);
|
large_int!(u128, u64, u64, 64);
|
||||||
large_int!(i128, u64, i64, 64);
|
large_int!(i128, u64, i64, 64);
|
||||||
|
|
||||||
|
/// Trait to express (possibly lossy) casting of integers
|
||||||
|
pub trait CastInto<T: Copy>: Copy {
|
||||||
|
fn cast(self) -> T;
|
||||||
|
}
|
||||||
|
|
||||||
|
macro_rules! cast_into {
|
||||||
|
($ty:ty) => {
|
||||||
|
cast_into!($ty; usize, isize, u32, i32, u64, i64, u128, i128);
|
||||||
|
};
|
||||||
|
($ty:ty; $($into:ty),*) => {$(
|
||||||
|
impl CastInto<$into> for $ty {
|
||||||
|
fn cast(self) -> $into {
|
||||||
|
self as $into
|
||||||
|
}
|
||||||
|
}
|
||||||
|
)*};
|
||||||
|
}
|
||||||
|
|
||||||
|
cast_into!(u32);
|
||||||
|
cast_into!(i32);
|
||||||
|
cast_into!(u64);
|
||||||
|
cast_into!(i64);
|
||||||
|
cast_into!(u128);
|
||||||
|
cast_into!(i128);
|
||||||
|
|
Loading…
Reference in New Issue