From 47eef2805b8a388de95fd692d96a8938b0646d80 Mon Sep 17 00:00:00 2001 From: Wilfried Chauveau Date: Tue, 6 Dec 2016 05:16:19 +0100 Subject: [PATCH 1/3] impl (unsigned/signed) int to single/double precision float conversion based on llvm algorithms. --- compiler-rt/compiler-rt-cdylib/build.rs | 6 + compiler-rt/compiler-rt-cdylib/src/lib.rs | 12 ++ src/arm.rs | 6 + src/float/conv.rs | 133 ++++++++++++++++++++++ src/float/mod.rs | 10 ++ src/int/mod.rs | 69 +++++++++-- 6 files changed, 224 insertions(+), 12 deletions(-) mode change 100644 => 100755 src/arm.rs create mode 100755 src/float/conv.rs mode change 100644 => 100755 src/int/mod.rs diff --git a/compiler-rt/compiler-rt-cdylib/build.rs b/compiler-rt/compiler-rt-cdylib/build.rs index 4eb763895..c78cccf64 100644 --- a/compiler-rt/compiler-rt-cdylib/build.rs +++ b/compiler-rt/compiler-rt-cdylib/build.rs @@ -60,6 +60,12 @@ fn main() { "addsf3.c", "powidf2.c", "powisf2.c", + "floatsisf.c", + "floatsidf.c", + "floatdidf.c", + "floatunsisf.c", + "floatunsidf.c", + "floatundidf.c", ]); for src in sources.files.iter() { diff --git a/compiler-rt/compiler-rt-cdylib/src/lib.rs b/compiler-rt/compiler-rt-cdylib/src/lib.rs index 81affa242..d1eede2cc 100644 --- a/compiler-rt/compiler-rt-cdylib/src/lib.rs +++ b/compiler-rt/compiler-rt-cdylib/src/lib.rs @@ -24,6 +24,12 @@ extern { fn __adddf3(); fn __powisf2(); fn __powidf2(); + fn __floatsisf(); + fn __floatsidf(); + fn __floatdidf(); + fn __floatunsisf(); + fn __floatunsidf(); + fn __floatundidf(); } macro_rules! declare { @@ -57,6 +63,12 @@ declare!(___addsf3, __addsf3); declare!(___adddf3, __adddf3); declare!(___powisf2, __powisf2); declare!(___powidf2, __powidf2); +declare!(___floatsisf, __floatsisf); +declare!(___floatsidf, __floatsidf); +declare!(___floatdidf, __floatdidf); +declare!(___floatunsisf, __floatunsisf); +declare!(___floatunsidf, __floatunsidf); +declare!(___floatundidf, __floatundidf); #[lang = "eh_personality"] fn eh_personality() {} diff --git a/src/arm.rs b/src/arm.rs old mode 100644 new mode 100755 index b74458f11..9d56cb657 --- a/src/arm.rs +++ b/src/arm.rs @@ -100,6 +100,12 @@ pub extern "C" fn __aeabi_uidiv(a: u32, b: u32) -> u32 { ::int::udiv::__udivsi3(a, b) } +#[cfg(not(feature = "c"))] +#[cfg_attr(not(test), no_mangle)] +pub extern "C" fn __aeabi_ui2d(a: u32) -> f64 { + ::float::conv::__floatunsidf(a) +} + extern "C" { fn memcpy(dest: *mut u8, src: *const u8, n: usize) -> *mut u8; fn memmove(dest: *mut u8, src: *const u8, n: usize) -> *mut u8; diff --git a/src/float/conv.rs b/src/float/conv.rs new file mode 100755 index 000000000..629af3c63 --- /dev/null +++ b/src/float/conv.rs @@ -0,0 +1,133 @@ +use float::Float; +use int::Int; + +macro_rules! fp_overflow { + (infinity, $fty:ty, $sign: expr) => { + return { + <$fty as Float>::from_parts( + $sign, + <$fty as Float>::exponent_max() as <$fty as Float>::Int, + 0 as <$fty as Float>::Int) + } + } +} + +macro_rules! fp_convert { + ($intrinsic:ident: $ity:ty, $fty:ty) => { + + pub extern "C" fn $intrinsic(i: $ity) -> $fty { + if i == 0 { + return 0.0 + } + + let mant_dig = <$fty>::significand_bits() + 1; + let exponent_bias = <$fty>::exponent_bias(); + + let n = <$ity>::bits(); + let (s, a) = i.extract_sign(); + let mut a = a; + + // number of significant digits + let sd = n - a.leading_zeros(); + + // exponent + let mut e = sd - 1; + + if <$ity>::bits() < mant_dig { + return <$fty>::from_parts(s, + (e + exponent_bias) as <$fty as Float>::Int, + (a as <$fty as Float>::Int) << (mant_dig - e - 1)) + } + + a = if sd > mant_dig { + /* start: 0000000000000000000001xxxxxxxxxxxxxxxxxxxxxxPQxxxxxxxxxxxxxxxxxx + * finish: 000000000000000000000000000000000000001xxxxxxxxxxxxxxxxxxxxxxPQR + * 12345678901234567890123456 + * 1 = msb 1 bit + * P = bit MANT_DIG-1 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 + */ + let mant_dig_plus_one = mant_dig + 1; + let mant_dig_plus_two = mant_dig + 2; + a = if sd == mant_dig_plus_one { + a << 1 + } 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 is now rounded to mant_dig bits */ + } else { + a.wrapping_shl(mant_dig - sd) + /* a is now rounded to mant_dig bits */ + }; + + <$fty>::from_parts(s, + (e + exponent_bias) as <$fty as Float>::Int, + a as <$fty as Float>::Int) + } + } +} + +fp_convert!(__floatsisf: i32, f32); +fp_convert!(__floatsidf: i32, f64); +fp_convert!(__floatdidf: i64, f64); +fp_convert!(__floatunsisf: u32, f32); +fp_convert!(__floatunsidf: u32, f64); +fp_convert!(__floatundidf: u64, f64); + +// NOTE(cfg) for some reason, on arm*-unknown-linux-gnueabihf, our implementation doesn't +// match the output of its gcc_s or compiler-rt counterpart. Until we investigate further, we'll +// just avoid testing against them on those targets. Do note that our implementation gives the +// correct answer; gcc_s and compiler-rt are incorrect in this case. +// +#[cfg(all(test, not(arm_linux)))] +mod tests { + use qc::{I32, U32, I64, U64, F32, F64}; + + check! { + fn __floatsisf(f: extern fn(i32) -> f32, + a: I32) + -> Option { + Some(F32(f(a.0))) + } + fn __floatsidf(f: extern fn(i32) -> f64, + a: I32) + -> Option { + Some(F64(f(a.0))) + } + fn __floatdidf(f: extern fn(i64) -> f64, + a: I64) + -> Option { + Some(F64(f(a.0))) + } + fn __floatunsisf(f: extern fn(u32) -> f32, + a: U32) + -> Option { + Some(F32(f(a.0))) + } + fn __floatunsidf(f: extern fn(u32) -> f64, + a: U32) + -> Option { + Some(F64(f(a.0))) + } + fn __floatundidf(f: extern fn(u64) -> f64, + a: U64) + -> Option { + Some(F64(f(a.0))) + } + } +} diff --git a/src/float/mod.rs b/src/float/mod.rs index 134c32dc7..2bba37dea 100644 --- a/src/float/mod.rs +++ b/src/float/mod.rs @@ -1,5 +1,6 @@ use core::mem; +pub mod conv; pub mod add; pub mod pow; @@ -18,6 +19,15 @@ pub trait Float: Sized + Copy { fn exponent_bits() -> u32 { Self::bits() - Self::significand_bits() - 1 } + /// Returns the maximum value of the exponent + fn exponent_max() -> u32 { + (1 << Self::exponent_bits()) - 1 + } + + /// Returns the exponent bias value + fn exponent_bias() -> u32 { + Self::exponent_max() >> 1 + } /// Returns a mask for the sign bit fn sign_mask() -> Self::Int; diff --git a/src/int/mod.rs b/src/int/mod.rs old mode 100644 new mode 100755 index 37e05377a..fb7ed8fc8 --- a/src/int/mod.rs +++ b/src/int/mod.rs @@ -6,30 +6,75 @@ pub mod udiv; /// Trait for some basic operations on integers pub trait Int { + /// Unsigned version of Self + type UnsignedInt; /// Returns the bitwidth of the int type fn bits() -> u32; + + /// Extracts the sign from self and returns a tuple. + /// + /// # Examples + /// + /// ```rust,ignore + /// let i = -25_i32; + /// let (sign, u) = i.extract_sign(); + /// assert_eq!(sign, true); + /// assert_eq!(u, 25_u32); + /// ``` + fn extract_sign(self) -> (bool, Self::UnsignedInt); } // TODO: Once i128/u128 support lands, we'll want to add impls for those as well impl Int for u32 { - fn bits() -> u32 { - 32 - } + type UnsignedInt = u32; + fn bits() -> u32 { + 32 + } + + fn extract_sign(self) -> (bool, u32) { + (false, self) + } } impl Int for i32 { - fn bits() -> u32 { - 32 - } + type UnsignedInt = u32; + + fn bits() -> u32 { + 32 + } + + fn extract_sign(self) -> (bool, u32) { + if self < 0 { + (true, !(self as u32) + 1) + } else { + (false, self as u32) + } + } } impl Int for u64 { - fn bits() -> u32 { - 64 - } + type UnsignedInt = u64; + + fn bits() -> u32 { + 64 + } + + fn extract_sign(self) -> (bool, u64) { + (false, self) + } } impl Int for i64 { - fn bits() -> u32 { - 64 - } + type UnsignedInt = u64; + + fn bits() -> u32 { + 64 + } + + fn extract_sign(self) -> (bool, u64) { + if self < 0 { + (true, !(self as u64) + 1) + } else { + (false, self as u64) + } + } } /// Trait to convert an integer to/from smaller parts From 2e9c80bb21c83e0e1ade2935623165a4ddc27b21 Mon Sep 17 00:00:00 2001 From: Wilfried Chauveau Date: Sun, 5 Feb 2017 10:57:36 +0100 Subject: [PATCH 2/3] implement float/double to (u)int conversion. --- compiler-rt/compiler-rt-cdylib/build.rs | 8 ++ compiler-rt/compiler-rt-cdylib/src/lib.rs | 16 ++++ src/float/conv.rs | 109 ++++++++++++++++++++++ src/float/mod.rs | 10 ++ src/qc.rs | 4 +- 5 files changed, 145 insertions(+), 2 deletions(-) diff --git a/compiler-rt/compiler-rt-cdylib/build.rs b/compiler-rt/compiler-rt-cdylib/build.rs index c78cccf64..0fdafb41e 100644 --- a/compiler-rt/compiler-rt-cdylib/build.rs +++ b/compiler-rt/compiler-rt-cdylib/build.rs @@ -66,6 +66,14 @@ fn main() { "floatunsisf.c", "floatunsidf.c", "floatundidf.c", + "fixsfsi.c", + "fixsfdi.c", + "fixdfsi.c", + "fixdfdi.c", + "fixunssfsi.c", + "fixunssfdi.c", + "fixunsdfsi.c", + "fixunsdfdi.c", ]); for src in sources.files.iter() { diff --git a/compiler-rt/compiler-rt-cdylib/src/lib.rs b/compiler-rt/compiler-rt-cdylib/src/lib.rs index d1eede2cc..9befb6430 100644 --- a/compiler-rt/compiler-rt-cdylib/src/lib.rs +++ b/compiler-rt/compiler-rt-cdylib/src/lib.rs @@ -30,6 +30,14 @@ extern { fn __floatunsisf(); fn __floatunsidf(); fn __floatundidf(); + fn __fixsfsi(); + fn __fixsfdi(); + fn __fixdfsi(); + fn __fixdfdi(); + fn __fixunssfsi(); + fn __fixunssfdi(); + fn __fixunsdfsi(); + fn __fixunsdfdi(); } macro_rules! declare { @@ -69,6 +77,14 @@ declare!(___floatdidf, __floatdidf); declare!(___floatunsisf, __floatunsisf); declare!(___floatunsidf, __floatunsidf); declare!(___floatundidf, __floatundidf); +declare!(___fixsfsi, __fixsfsi); +declare!(___fixsfdi, __fixsfdi); +declare!(___fixdfsi, __fixdfsi); +declare!(___fixdfdi, __fixdfdi); +declare!(___fixunssfsi, __fixunssfsi); +declare!(___fixunssfdi, __fixunssfdi); +declare!(___fixunsdfsi, __fixunsdfsi); +declare!(___fixunsdfdi, __fixunsdfdi); #[lang = "eh_personality"] fn eh_personality() {} diff --git a/src/float/conv.rs b/src/float/conv.rs index 629af3c63..93437c840 100755 --- a/src/float/conv.rs +++ b/src/float/conv.rs @@ -89,6 +89,73 @@ fp_convert!(__floatunsisf: u32, f32); fp_convert!(__floatunsidf: u32, f64); fp_convert!(__floatundidf: u64, f64); +#[derive(PartialEq, Debug)] +enum Sign { + Positive, + Negative +} +macro_rules! fp_fix { + ($intrinsic:ident: $fty:ty, $ity:ty) => { + pub extern "C" fn $intrinsic(f: $fty) -> $ity { + let fixint_min = <$ity>::min_value(); + let fixint_max = <$ity>::max_value(); + let fixint_bits = <$ity>::bits() as usize; + let fixint_unsigned = fixint_min == 0; + + let sign_bit = <$fty>::sign_mask(); + let significand_bits = <$fty>::significand_bits() as usize; + let exponent_bias = <$fty>::exponent_bias() as usize; + //let exponent_max = <$fty>::exponent_max() as usize; + + // Break a into sign, exponent, significand + let a_rep = <$fty>::repr(f); + let a_abs = a_rep & !sign_bit; + + // 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 mut exponent = (a_abs >> significand_bits) as usize; + let significand = (a_abs & <$fty>::significand_mask()) | <$fty>::implicit_bit(); + + // if < 1 or unsigned & negative + if exponent < exponent_bias || + fixint_unsigned && sign == Sign::Negative { + return 0 + } + exponent -= exponent_bias; + + // If the value is infinity, saturate. + // If the value is too large for the integer type, 0. + if exponent >= (if fixint_unsigned {fixint_bits} else {fixint_bits -1}) { + return if sign == Sign::Positive {fixint_max} else {fixint_min} + } + // If 0 <= exponent < significand_bits, right shift to get the result. + // Otherwise, shift left. + // (sign - 1) will never overflow as negative signs are already returned as 0 for unsigned + let r = if exponent < significand_bits { + (significand >> (significand_bits - exponent)) as $ity + } else { + (significand as $ity) << (exponent - significand_bits) + }; + + if sign == Sign::Negative { + (!r).wrapping_add(1) + } else { + r + } + } + } +} + +fp_fix!(__fixsfsi: f32, i32); +fp_fix!(__fixsfdi: f32, i64); +fp_fix!(__fixdfsi: f64, i32); +fp_fix!(__fixdfdi: f64, i64); + +fp_fix!(__fixunssfsi: f32, u32); +fp_fix!(__fixunssfdi: f32, u64); +fp_fix!(__fixunsdfsi: f64, u32); +fp_fix!(__fixunsdfdi: f64, u64); + // NOTE(cfg) for some reason, on arm*-unknown-linux-gnueabihf, our implementation doesn't // match the output of its gcc_s or compiler-rt counterpart. Until we investigate further, we'll // just avoid testing against them on those targets. Do note that our implementation gives the @@ -129,5 +196,47 @@ mod tests { -> Option { Some(F64(f(a.0))) } + + fn __fixsfsi(f: extern fn(f32) -> i32, + a: F32) + -> Option { + Some(I32(f(a.0))) + } + fn __fixsfdi(f: extern fn(f32) -> i64, + a: F32) + -> Option { + Some(I64(f(a.0))) + } + fn __fixdfsi(f: extern fn(f64) -> i32, + a: F64) + -> Option { + Some(I32(f(a.0))) + } + fn __fixdfdi(f: extern fn(f64) -> i64, + a: F64) + -> Option { + Some(I64(f(a.0))) + } + + fn __fixunssfsi(f: extern fn(f32) -> u32, + a: F32) + -> Option { + Some(U32(f(a.0))) + } + fn __fixunssfdi(f: extern fn(f32) -> u64, + a: F32) + -> Option { + Some(U64(f(a.0))) + } + fn __fixunsdfsi(f: extern fn(f64) -> u32, + a: F64) + -> Option { + Some(U32(f(a.0))) + } + fn __fixunsdfdi(f: extern fn(f64) -> u64, + a: F64) + -> Option { + Some(U64(f(a.0))) + } } } diff --git a/src/float/mod.rs b/src/float/mod.rs index 2bba37dea..8c664c630 100644 --- a/src/float/mod.rs +++ b/src/float/mod.rs @@ -35,6 +35,9 @@ pub trait Float: Sized + Copy { /// Returns a mask for the significand fn significand_mask() -> Self::Int; + // Returns the implicit bit of the float format + fn implicit_bit() -> Self::Int; + /// Returns a mask for the exponent fn exponent_mask() -> Self::Int; @@ -67,6 +70,9 @@ impl Float for f32 { fn significand_bits() -> u32 { 23 } + fn implicit_bit() -> Self::Int { + 1 << Self::significand_bits() + } fn sign_mask() -> Self::Int { 1 << (Self::bits() - 1) } @@ -109,6 +115,10 @@ impl Float for f64 { fn significand_bits() -> u32 { 52 } + // Returns the implicit bit of the float format + fn implicit_bit() -> Self::Int { + 1 << Self::significand_bits() + } fn sign_mask() -> Self::Int { 1 << (Self::bits() - 1) } diff --git a/src/qc.rs b/src/qc.rs index 5dbc56f24..2b8591b8d 100644 --- a/src/qc.rs +++ b/src/qc.rs @@ -15,7 +15,7 @@ use float::Float; // Generates values in the full range of the integer type macro_rules! arbitrary { ($TY:ident : $ty:ident) => { - #[derive(Clone, Copy)] + #[derive(Clone, Copy, PartialEq)] pub struct $TY(pub $ty); impl Arbitrary for $TY { @@ -82,7 +82,7 @@ arbitrary!(U32: u32); // intrinsics. macro_rules! arbitrary_large { ($TY:ident : $ty:ident) => { - #[derive(Clone, Copy)] + #[derive(Clone, Copy, PartialEq)] pub struct $TY(pub $ty); impl Arbitrary for $TY { From a02eb075756a5c20b2c5ad977b6b5d7e8df093df Mon Sep 17 00:00:00 2001 From: Wilfried Chauveau Date: Mon, 6 Feb 2017 06:26:55 +0100 Subject: [PATCH 3/3] exclude from tests values out of integer range (and NaN). --- src/float/conv.rs | 42 +++++++++++++++++++++++++++++++++--------- 1 file changed, 33 insertions(+), 9 deletions(-) diff --git a/src/float/conv.rs b/src/float/conv.rs index 5808093f3..8753b8860 100755 --- a/src/float/conv.rs +++ b/src/float/conv.rs @@ -199,47 +199,71 @@ mod tests { -> Option { Some(F64(f(a.0))) } - + fn __fixsfsi(f: extern fn(f32) -> i32, a: F32) -> Option { - Some(I32(f(a.0))) + if a.0 > (i32::max_value() as f32) || + a.0 < (i32::min_value() as f32) || a.0.is_nan() { + None + } else { Some(I32(f(a.0))) } } fn __fixsfdi(f: extern fn(f32) -> i64, a: F32) -> Option { - Some(I64(f(a.0))) + if a.0 > (i64::max_value() as f32) || + a.0 < (i64::min_value() as f32) || a.0.is_nan() { + None + } else { Some(I64(f(a.0))) } } fn __fixdfsi(f: extern fn(f64) -> i32, a: F64) -> Option { - Some(I32(f(a.0))) + if a.0 > (i32::max_value() as f64) || + a.0 < (i32::min_value() as f64) || a.0.is_nan() { + None + } else { Some(I32(f(a.0))) } } fn __fixdfdi(f: extern fn(f64) -> i64, a: F64) -> Option { - Some(I64(f(a.0))) + if a.0 > (i64::max_value() as f64) || + a.0 < (i64::min_value() as f64) || a.0.is_nan() { + None + } else { Some(I64(f(a.0))) } } fn __fixunssfsi(f: extern fn(f32) -> u32, a: F32) -> Option { - Some(U32(f(a.0))) + if a.0 > (u32::max_value() as f32) || + a.0 < (u32::min_value() as f32) || a.0.is_nan() { + None + } else { Some(U32(f(a.0))) } } fn __fixunssfdi(f: extern fn(f32) -> u64, a: F32) -> Option { - Some(U64(f(a.0))) + if a.0 > (u64::max_value() as f32) || + a.0 < (u64::min_value() as f32) || a.0.is_nan() { + None + } else { Some(U64(f(a.0))) } } fn __fixunsdfsi(f: extern fn(f64) -> u32, a: F64) -> Option { - Some(U32(f(a.0))) + if a.0 > (u32::max_value() as f64) || + a.0 < (u32::min_value() as f64) || a.0.is_nan() { + None + } else { Some(U32(f(a.0))) } } fn __fixunsdfdi(f: extern fn(f64) -> u64, a: F64) -> Option { - Some(U64(f(a.0))) + if a.0 <= (u64::max_value() as f64) || + a.0 >= (u64::min_value() as f64) || a.0.is_nan() { + None + } else { Some(U64(f(a.0))) } } } }