impl (unsigned/signed) int to single/double precision float conversion based on llvm algorithms.
This commit is contained in:
parent
0507842b24
commit
293fef5ebe
|
@ -62,6 +62,12 @@ fn main() {
|
||||||
"powisf2.c",
|
"powisf2.c",
|
||||||
"subdf3.c",
|
"subdf3.c",
|
||||||
"subsf3.c",
|
"subsf3.c",
|
||||||
|
"floatsisf.c",
|
||||||
|
"floatsidf.c",
|
||||||
|
"floatdidf.c",
|
||||||
|
"floatunsisf.c",
|
||||||
|
"floatunsidf.c",
|
||||||
|
"floatundidf.c",
|
||||||
// 128 bit integers
|
// 128 bit integers
|
||||||
"lshrti3.c",
|
"lshrti3.c",
|
||||||
"modti3.c",
|
"modti3.c",
|
||||||
|
|
|
@ -26,6 +26,12 @@ extern {
|
||||||
fn __powidf2();
|
fn __powidf2();
|
||||||
fn __subsf3();
|
fn __subsf3();
|
||||||
fn __subdf3();
|
fn __subdf3();
|
||||||
|
fn __floatsisf();
|
||||||
|
fn __floatsidf();
|
||||||
|
fn __floatdidf();
|
||||||
|
fn __floatunsisf();
|
||||||
|
fn __floatunsidf();
|
||||||
|
fn __floatundidf();
|
||||||
}
|
}
|
||||||
|
|
||||||
macro_rules! declare {
|
macro_rules! declare {
|
||||||
|
@ -61,6 +67,12 @@ declare!(___powisf2, __powisf2);
|
||||||
declare!(___powidf2, __powidf2);
|
declare!(___powidf2, __powidf2);
|
||||||
declare!(___subsf3, __subsf3);
|
declare!(___subsf3, __subsf3);
|
||||||
declare!(___subdf3, __subdf3);
|
declare!(___subdf3, __subdf3);
|
||||||
|
declare!(___floatsisf, __floatsisf);
|
||||||
|
declare!(___floatsidf, __floatsidf);
|
||||||
|
declare!(___floatdidf, __floatdidf);
|
||||||
|
declare!(___floatunsisf, __floatunsisf);
|
||||||
|
declare!(___floatunsidf, __floatunsidf);
|
||||||
|
declare!(___floatundidf, __floatundidf);
|
||||||
|
|
||||||
#[cfg(all(not(windows),
|
#[cfg(all(not(windows),
|
||||||
not(target_arch = "mips64"),
|
not(target_arch = "mips64"),
|
||||||
|
|
|
@ -112,6 +112,12 @@ pub extern "aapcs" fn __aeabi_uidiv(a: u32, b: u32) -> u32 {
|
||||||
::int::udiv::__udivsi3(a, b)
|
::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)
|
||||||
|
}
|
||||||
|
|
||||||
// TODO: These aeabi_* functions should be defined as aliases
|
// TODO: These aeabi_* functions should be defined as aliases
|
||||||
#[cfg(not(feature = "mem"))]
|
#[cfg(not(feature = "mem"))]
|
||||||
extern "C" {
|
extern "C" {
|
||||||
|
|
|
@ -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<F32> {
|
||||||
|
Some(F32(f(a.0)))
|
||||||
|
}
|
||||||
|
fn __floatsidf(f: extern fn(i32) -> f64,
|
||||||
|
a: I32)
|
||||||
|
-> Option<F64> {
|
||||||
|
Some(F64(f(a.0)))
|
||||||
|
}
|
||||||
|
fn __floatdidf(f: extern fn(i64) -> f64,
|
||||||
|
a: I64)
|
||||||
|
-> Option<F64> {
|
||||||
|
Some(F64(f(a.0)))
|
||||||
|
}
|
||||||
|
fn __floatunsisf(f: extern fn(u32) -> f32,
|
||||||
|
a: U32)
|
||||||
|
-> Option<F32> {
|
||||||
|
Some(F32(f(a.0)))
|
||||||
|
}
|
||||||
|
fn __floatunsidf(f: extern fn(u32) -> f64,
|
||||||
|
a: U32)
|
||||||
|
-> Option<F64> {
|
||||||
|
Some(F64(f(a.0)))
|
||||||
|
}
|
||||||
|
fn __floatundidf(f: extern fn(u64) -> f64,
|
||||||
|
a: U64)
|
||||||
|
-> Option<F64> {
|
||||||
|
Some(F64(f(a.0)))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -1,5 +1,6 @@
|
||||||
use core::mem;
|
use core::mem;
|
||||||
|
|
||||||
|
pub mod conv;
|
||||||
pub mod add;
|
pub mod add;
|
||||||
pub mod pow;
|
pub mod pow;
|
||||||
pub mod sub;
|
pub mod sub;
|
||||||
|
@ -19,6 +20,15 @@ pub trait Float: Sized + Copy {
|
||||||
fn exponent_bits() -> u32 {
|
fn exponent_bits() -> u32 {
|
||||||
Self::bits() - Self::significand_bits() - 1
|
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
|
/// Returns a mask for the sign bit
|
||||||
fn sign_mask() -> Self::Int;
|
fn sign_mask() -> Self::Int;
|
||||||
|
|
|
@ -19,30 +19,85 @@ pub mod udiv;
|
||||||
pub trait Int {
|
pub trait Int {
|
||||||
/// Type with the same width but other signedness
|
/// Type with the same width but other signedness
|
||||||
type OtherSign;
|
type OtherSign;
|
||||||
|
/// Unsigned version of Self
|
||||||
|
type UnsignedInt;
|
||||||
|
|
||||||
/// Returns the bitwidth of the int type
|
/// Returns the bitwidth of the int type
|
||||||
fn bits() -> u32;
|
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);
|
||||||
}
|
}
|
||||||
|
|
||||||
macro_rules! int_impl {
|
// TODO: Once i128/u128 support lands, we'll want to add impls for those as well
|
||||||
($ity:ty, $sty:ty, $bits:expr) => {
|
impl Int for u32 {
|
||||||
impl Int for $ity {
|
type OtherSign = i32;
|
||||||
type OtherSign = $sty;
|
type UnsignedInt = u32;
|
||||||
|
|
||||||
fn bits() -> u32 {
|
fn bits() -> u32 {
|
||||||
$bits
|
32
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn extract_sign(self) -> (bool, u32) {
|
||||||
|
(false, self)
|
||||||
}
|
}
|
||||||
impl Int for $sty {
|
}
|
||||||
type OtherSign = $ity;
|
|
||||||
|
impl Int for i32 {
|
||||||
|
type OtherSign = u32;
|
||||||
|
type UnsignedInt = u32;
|
||||||
|
|
||||||
fn bits() -> u32 {
|
fn bits() -> u32 {
|
||||||
$bits
|
32
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn extract_sign(self) -> (bool, u32) {
|
||||||
|
if self < 0 {
|
||||||
|
(true, !(self as u32) + 1)
|
||||||
|
} else {
|
||||||
|
(false, self as u32)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
int_impl!(i32, u32, 32);
|
impl Int for u64 {
|
||||||
int_impl!(i64, u64, 64);
|
type OtherSign = i64;
|
||||||
int_impl!(i128, u128, 128);
|
type UnsignedInt = u64;
|
||||||
|
|
||||||
|
fn bits() -> u32 {
|
||||||
|
64
|
||||||
|
}
|
||||||
|
|
||||||
|
fn extract_sign(self) -> (bool, u64) {
|
||||||
|
(false, self)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Int for i64 {
|
||||||
|
type OtherSign = u64;
|
||||||
|
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
|
/// Trait to convert an integer to/from smaller parts
|
||||||
pub trait LargeInt {
|
pub trait LargeInt {
|
||||||
|
|
Loading…
Reference in New Issue