Convert float_to_int! into a function

master
est31 2017-09-14 14:59:32 +02:00
parent a272d753f5
commit ff63f7b80f
2 changed files with 82 additions and 56 deletions

View File

@ -1,5 +1,5 @@
use float::Float; use float::Float;
use int::Int; use int::{Int, CastInto};
macro_rules! int_to_float { macro_rules! int_to_float {
($i:expr, $ity:ty, $fty:ty) => ({ ($i:expr, $ity:ty, $fty:ty) => ({
@ -137,115 +137,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)
} }
} }

View File

@ -230,3 +230,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);