Merge pull request #198 from est31/master

Partial revert of the float conversion refactor in #192
This commit is contained in:
Alex Crichton 2017-09-29 01:54:05 -05:00 committed by GitHub
commit 0b9844764e
1 changed files with 122 additions and 127 deletions

View File

@ -1,87 +1,83 @@
use float::Float; use float::Float;
use int::{Int, CastInto}; use int::Int;
fn int_to_float<I: Int, F: Float>(i: I) -> F where macro_rules! int_to_float {
F::Int: CastInto<u32>, ($i:expr, $ity:ty, $fty:ty) => ({
F::Int: CastInto<I>, let i = $i;
I::UnsignedInt: CastInto<F::Int>, if i == 0 {
u32: CastInto<F::Int>, return 0.0
{ }
if i == I::ZERO {
return F::ZERO;
}
let two = I::UnsignedInt::ONE + I::UnsignedInt::ONE; let mant_dig = <$fty>::SIGNIFICAND_BITS + 1;
let four = two + two; let exponent_bias = <$fty>::EXPONENT_BIAS;
let mant_dig = F::SIGNIFICAND_BITS + 1;
let exponent_bias = F::EXPONENT_BIAS;
let n = I::BITS; let n = <$ity>::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 I::BITS < mant_dig { if <$ity>::BITS < mant_dig {
return F::from_parts(s, return <$fty>::from_parts(s,
(e + exponent_bias).cast(), (e + exponent_bias) as <$fty as Float>::Int,
a.cast() << (mant_dig - e - 1)); (a as <$fty as Float>::Int) << (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 >> (sd - mant_dig_plus_two)) | a.wrapping_shl(mant_dig - sd)
Int::from_bool((a & I::UnsignedInt::max_value()).wrapping_shl((n + mant_dig_plus_two) - sd) != Int::ZERO) /* a is now rounded to mant_dig bits */
}; };
/* finish: */ <$fty>::from_parts(s,
a |= Int::from_bool((a & four) != I::UnsignedInt::ZERO); /* Or P into R */ (e + exponent_bias) as <$fty as Float>::Int,
a += Int::ONE; /* round - this step may add a significant bit */ a as <$fty as Float>::Int)
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) int_to_float!(i, i32, f32)
} }
#[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) int_to_float!(i, i32, f64)
} }
#[use_c_shim_if(all(target_arch = "x86", not(target_env = "msvc")))] #[use_c_shim_if(all(target_arch = "x86", not(target_env = "msvc")))]
@ -92,28 +88,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) int_to_float!(i, i64, f64)
} }
} }
#[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) int_to_float!(i, i128, f32)
} }
#[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) int_to_float!(i, i128, f64)
} }
#[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) int_to_float!(i, u32, f32)
} }
#[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) int_to_float!(i, u32, f64)
} }
#[use_c_shim_if(all(not(target_env = "msvc"), #[use_c_shim_if(all(not(target_env = "msvc"),
@ -121,17 +117,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) int_to_float!(i, u64, f64)
} }
#[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) int_to_float!(i, u128, f32)
} }
#[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) int_to_float!(i, u128, f64)
} }
} }
@ -141,116 +137,115 @@ enum Sign {
Negative Negative
} }
fn float_to_int<F: Float, I: Int>(f: F) -> I where macro_rules! float_to_int {
F::Int: CastInto<u32>, ($f:expr, $fty:ty, $ity:ty) => ({
F::Int: CastInto<I>, let f = $f;
{ let fixint_min = <$ity>::min_value();
let f = f; let fixint_max = <$ity>::max_value();
let fixint_min = I::min_value(); let fixint_bits = <$ity>::BITS as usize;
let fixint_max = I::max_value(); let fixint_unsigned = fixint_min == 0;
let fixint_bits = I::BITS;
let fixint_unsigned = fixint_min == I::ZERO;
let sign_bit = F::SIGN_MASK; let sign_bit = <$fty>::SIGN_MASK;
let significand_bits = F::SIGNIFICAND_BITS; let significand_bits = <$fty>::SIGNIFICAND_BITS as usize;
let exponent_bias = F::EXPONENT_BIAS; let exponent_bias = <$fty>::EXPONENT_BIAS as usize;
//let exponent_max = F::exponent_max() as usize; //let exponent_max = <$fty>::exponent_max() as usize;
// Break a into sign, exponent, significand // Break a into sign, exponent, significand
let a_rep = F::repr(f); let a_rep = <$fty>::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) == F::Int::ZERO { Sign::Positive } else { Sign::Negative }; let sign = if (a_rep & sign_bit) == 0 { Sign::Positive } else { Sign::Negative };
let mut exponent: u32 = (a_abs >> significand_bits).cast(); let mut exponent = (a_abs >> significand_bits) as usize;
let significand = (a_abs & F::SIGNIFICAND_MASK) | F::IMPLICIT_BIT; let significand = (a_abs & <$fty>::SIGNIFICAND_MASK) | <$fty>::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 I::ZERO; return 0
} }
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: I = if exponent < significand_bits { let r = if exponent < significand_bits {
(significand >> (significand_bits - exponent)).cast() (significand >> (significand_bits - exponent)) as $ity
} else { } else {
(significand << (exponent - significand_bits)).cast() (significand as $ity) << (exponent - significand_bits)
}; };
if sign == Sign::Negative { if sign == Sign::Negative {
(!r).wrapping_add(I::ONE) (!r).wrapping_add(1)
} 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) float_to_int!(f, f32, i32)
} }
#[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) float_to_int!(f, f32, i64)
} }
#[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) float_to_int!(f, f32, i128)
} }
#[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) float_to_int!(f, f64, i32)
} }
#[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) float_to_int!(f, f64, i64)
} }
#[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) float_to_int!(f, f64, i128)
} }
#[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) float_to_int!(f, f32, u32)
} }
#[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) float_to_int!(f, f32, u64)
} }
#[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) float_to_int!(f, f32, u128)
} }
#[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) float_to_int!(f, f64, u32)
} }
#[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) float_to_int!(f, f64, u64)
} }
#[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) float_to_int!(f, f64, u128)
} }
} }