Convert int_to_float! to a function
This commit is contained in:
parent
ff63f7b80f
commit
482d98318f
@ -1,17 +1,22 @@
|
|||||||
use float::Float;
|
use float::Float;
|
||||||
use int::{Int, CastInto};
|
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;
|
||||||
|
|
||||||
@ -21,10 +26,10 @@ macro_rules! int_to_float {
|
|||||||
// 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 {
|
||||||
@ -43,17 +48,17 @@ macro_rules! int_to_float {
|
|||||||
} else if sd == mant_dig_plus_two {
|
} else if sd == mant_dig_plus_two {
|
||||||
a
|
a
|
||||||
} else {
|
} else {
|
||||||
(a >> (sd - mant_dig_plus_two)) as <$ity as Int>::UnsignedInt |
|
(a >> (sd - mant_dig_plus_two)) |
|
||||||
((a & <$ity as Int>::UnsignedInt::max_value()).wrapping_shl((n + mant_dig_plus_two) - sd) != 0) as <$ity as Int>::UnsignedInt
|
Int::from_bool((a & I::UnsignedInt::max_value()).wrapping_shl((n + mant_dig_plus_two) - sd) != Int::ZERO)
|
||||||
};
|
};
|
||||||
|
|
||||||
/* finish: */
|
/* finish: */
|
||||||
a |= ((a & 4) != 0) as <$ity as Int>::UnsignedInt; /* Or P into R */
|
a |= Int::from_bool((a & four) != I::UnsignedInt::ZERO); /* Or P into R */
|
||||||
a += 1; /* round - this step may add a significant bit */
|
a += Int::ONE; /* 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 & (1 << mant_dig)) != 0 {
|
if (a & (I::UnsignedInt::ONE << mant_dig)) != Int::ZERO {
|
||||||
a >>= 1; e += 1;
|
a >>= 1; e += 1;
|
||||||
}
|
}
|
||||||
a
|
a
|
||||||
@ -63,21 +68,20 @@ macro_rules! int_to_float {
|
|||||||
/* a is now rounded to mant_dig bits */
|
/* a is now rounded to mant_dig bits */
|
||||||
};
|
};
|
||||||
|
|
||||||
<$fty>::from_parts(s,
|
F::from_parts(s,
|
||||||
(e + exponent_bias) as <$fty as Float>::Int,
|
(e + exponent_bias).cast(),
|
||||||
a as <$fty as Float>::Int)
|
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)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -23,6 +23,9 @@ pub trait Int:
|
|||||||
PartialEq +
|
PartialEq +
|
||||||
PartialOrd +
|
PartialOrd +
|
||||||
ops::AddAssign +
|
ops::AddAssign +
|
||||||
|
ops::BitAndAssign +
|
||||||
|
ops::BitOrAssign +
|
||||||
|
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 +34,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 +62,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 {
|
||||||
@ -100,6 +106,10 @@ macro_rules! int_impl {
|
|||||||
me
|
me
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn from_bool(b: bool) -> Self {
|
||||||
|
b as $uty
|
||||||
|
}
|
||||||
|
|
||||||
fn max_value() -> Self {
|
fn max_value() -> Self {
|
||||||
<Self>::max_value()
|
<Self>::max_value()
|
||||||
}
|
}
|
||||||
@ -120,6 +130,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,6 +141,10 @@ 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)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Int for $ity {
|
impl Int for $ity {
|
||||||
@ -154,6 +172,10 @@ macro_rules! int_impl {
|
|||||||
me as $ity
|
me as $ity
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn from_bool(b: bool) -> Self {
|
||||||
|
b as $ity
|
||||||
|
}
|
||||||
|
|
||||||
fn max_value() -> Self {
|
fn max_value() -> Self {
|
||||||
<Self>::max_value()
|
<Self>::max_value()
|
||||||
}
|
}
|
||||||
@ -174,6 +196,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))
|
||||||
}
|
}
|
||||||
@ -181,6 +207,10 @@ 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)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user