implement float/double to (u)int conversion.
This commit is contained in:
parent
293fef5ebe
commit
d96102d270
@ -68,6 +68,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",
|
||||
// 128 bit integers
|
||||
"lshrti3.c",
|
||||
"modti3.c",
|
||||
|
@ -32,6 +32,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 {
|
||||
@ -73,6 +81,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);
|
||||
|
||||
#[cfg(all(not(windows),
|
||||
not(target_arch = "mips64"),
|
||||
|
@ -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<F64> {
|
||||
Some(F64(f(a.0)))
|
||||
}
|
||||
|
||||
fn __fixsfsi(f: extern fn(f32) -> i32,
|
||||
a: F32)
|
||||
-> Option<I32> {
|
||||
Some(I32(f(a.0)))
|
||||
}
|
||||
fn __fixsfdi(f: extern fn(f32) -> i64,
|
||||
a: F32)
|
||||
-> Option<I64> {
|
||||
Some(I64(f(a.0)))
|
||||
}
|
||||
fn __fixdfsi(f: extern fn(f64) -> i32,
|
||||
a: F64)
|
||||
-> Option<I32> {
|
||||
Some(I32(f(a.0)))
|
||||
}
|
||||
fn __fixdfdi(f: extern fn(f64) -> i64,
|
||||
a: F64)
|
||||
-> Option<I64> {
|
||||
Some(I64(f(a.0)))
|
||||
}
|
||||
|
||||
fn __fixunssfsi(f: extern fn(f32) -> u32,
|
||||
a: F32)
|
||||
-> Option<U32> {
|
||||
Some(U32(f(a.0)))
|
||||
}
|
||||
fn __fixunssfdi(f: extern fn(f32) -> u64,
|
||||
a: F32)
|
||||
-> Option<U64> {
|
||||
Some(U64(f(a.0)))
|
||||
}
|
||||
fn __fixunsdfsi(f: extern fn(f64) -> u32,
|
||||
a: F64)
|
||||
-> Option<U32> {
|
||||
Some(U32(f(a.0)))
|
||||
}
|
||||
fn __fixunsdfdi(f: extern fn(f64) -> u64,
|
||||
a: F64)
|
||||
-> Option<U64> {
|
||||
Some(U64(f(a.0)))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -36,6 +36,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;
|
||||
|
||||
@ -68,6 +71,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)
|
||||
}
|
||||
@ -110,6 +116,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)
|
||||
}
|
||||
|
@ -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 {
|
||||
|
Loading…
Reference in New Issue
Block a user