Partially revert 482d98318f

This partially reverts "Convert int_to_float! to a function".
This commit is contained in:
est31 2017-09-29 02:19:40 +02:00
parent 789e8c159f
commit a20840262e
1 changed files with 66 additions and 70 deletions

View File

@ -1,22 +1,17 @@
use float::Float; use float::Float;
use int::{Int, CastInto}; use int::{Int, CastInto};
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;
@ -26,10 +21,10 @@ fn int_to_float<I: Int, F: Float>(i: I) -> F where
// 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 {
@ -48,17 +43,17 @@ fn int_to_float<I: Int, F: Float>(i: I) -> F where
} else if sd == mant_dig_plus_two { } else if sd == mant_dig_plus_two {
a a
} else { } else {
(a >> (sd - mant_dig_plus_two)) | (a >> (sd - mant_dig_plus_two)) as <$ity as Int>::UnsignedInt |
Int::from_bool((a & I::UnsignedInt::max_value()).wrapping_shl((n + mant_dig_plus_two) - sd) != Int::ZERO) ((a & <$ity as Int>::UnsignedInt::max_value()).wrapping_shl((n + mant_dig_plus_two) - sd) != 0) as <$ity as Int>::UnsignedInt
}; };
/* finish: */ /* finish: */
a |= Int::from_bool((a & four) != I::UnsignedInt::ZERO); /* Or P into R */ a |= ((a & 4) != 0) as <$ity as Int>::UnsignedInt; /* Or P into R */
a += Int::ONE; /* round - this step may add a significant bit */ a += 1; /* round - this step may add a significant bit */
a >>= 2; /* dump Q and R */ a >>= 2; /* dump Q and R */
/* a is now rounded to mant_dig or mant_dig+1 bits */ /* a is now rounded to mant_dig or mant_dig+1 bits */
if (a & (I::UnsignedInt::ONE << mant_dig)) != Int::ZERO { if (a & (1 << mant_dig)) != 0 {
a >>= 1; e += 1; a >>= 1; e += 1;
} }
a a
@ -68,20 +63,21 @@ fn int_to_float<I: Int, F: Float>(i: I) -> F where
/* a is now rounded to mant_dig bits */ /* a is now rounded to mant_dig bits */
}; };
F::from_parts(s, <$fty>::from_parts(s,
(e + exponent_bias).cast(), (e + exponent_bias) as <$fty as Float>::Int,
a.cast()) a as <$fty as Float>::Int)
})
} }
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)
} }
} }