Skip to content

Implement floatunsisf (WIP) #106

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,6 @@ fn main() {
"floatundisf.c",
"floatundixf.c",
"floatunsidf.c",
"floatunsisf.c",
"int_util.c",
"muldc3.c",
"muldf3.c",
Expand Down
2 changes: 1 addition & 1 deletion ci/run.sh
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ case $TRAVIS_OS_NAME in
esac

# NOTE On i586, It's normal that the get_pc_thunk symbol appears several times so ignore it
if [ $TRAVIS_OS_NAME = osx ]; then
if [ "$TRAVIS_OS_NAME" = "osx" ]; then
path=target/${1}/debug/libcompiler_builtins.rlib
else
path=/target/${1}/debug/libcompiler_builtins.rlib
Expand Down
1 change: 1 addition & 0 deletions compiler-rt/compiler-rt-cdylib/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ fn main() {
"addsf3.c",
"powidf2.c",
"powisf2.c",
"floatunsisf.c",
]);

for src in sources.files.iter() {
Expand Down
2 changes: 2 additions & 0 deletions compiler-rt/compiler-rt-cdylib/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ extern {
fn __adddf3();
fn __powisf2();
fn __powidf2();
fn __floatunsisf();
}

macro_rules! declare {
Expand Down Expand Up @@ -57,6 +58,7 @@ declare!(___addsf3, __addsf3);
declare!(___adddf3, __adddf3);
declare!(___powisf2, __powisf2);
declare!(___powidf2, __powidf2);
declare!(___floatunsisf, __floatunsisf);

#[lang = "eh_personality"]
fn eh_personality() {}
Expand Down
5 changes: 5 additions & 0 deletions src/arm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,11 @@ pub extern "C" fn __aeabi_fadd(a: f32, b: f32) -> f32 {
::float::add::__addsf3(a, b)
}

#[cfg_attr(not(test), no_mangle)]
pub extern "C" fn __aeabi_ui2f(a: u32) -> f32 {
::float::conv::__floatunsisf(a)
}

#[cfg(not(all(feature = "c", target_arch = "arm", not(target_os = "ios"), not(thumbv6m))))]
#[cfg_attr(not(test), no_mangle)]
pub extern "C" fn __aeabi_idiv(a: i32, b: i32) -> i32 {
Expand Down
94 changes: 94 additions & 0 deletions src/float/conv.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
use core::num::Wrapping;
use float::Float;
use int::Int;


macro_rules! floatunsisf {
($intrinsic:ident: $ity:ty => $fty:ty) => {
/// Returns `a as f32` or `a as f64`
#[cfg_attr(not(test), no_mangle)]
pub extern "C" fn $intrinsic(a: $ity) -> $fty {
// CC This file implements unsigned integer to single-precision conversion for the
// CC compiler-rt library in the IEEE-754 default round-to-nearest, ties-to-even
// CC mode.

// CC const int aWidth = sizeof a * CHAR_BIT;
let a_width : usize = <$ity>::bits() as usize;
// CC
// CC // Handle zero as a special case to protect clz
// CC if (a == 0) return fromRep(0);
if a == 0 { return (<$fty as Float>::from_repr)(<$fty>::zero().0); }
// CC
// CC // Exponent of (fp_t)a is the width of abs(a).
// CC const int exponent = (aWidth - 1) - __builtin_clz(a);
let exponent : usize = (a_width - 1usize) - a.leading_zeros() as usize;
// CC rep_t result;
// CC
let mut result =
// CC // Shift a into the significand field, rounding if it is a right-shift
// CC if (exponent <= significandBits) {
if exponent <= <$fty>::significand_bits() {
// CC const int shift = significandBits - exponent;
let shift = <$fty>::significand_bits() - exponent;
// CC result = (rep_t)a << shift ^ implicitBit;
(Wrapping(a as <$fty as Float>::Int) << shift) ^ <$fty>::implicit_bit()
// CC } else {
} else {
// CC const int shift = exponent - significandBits;
let shift = exponent - <$fty>::significand_bits();
// CC result = (rep_t)a >> shift ^ implicitBit;
let mut result = (Wrapping(a as <$fty as Float>::Int) >> shift) ^ <$fty>::implicit_bit();
// CC rep_t round = (rep_t)a << (typeWidth - shift);
let round = Wrapping(a as <$fty as Float>::Int) << (<$fty>::bits() - shift);
// CC if (round > signBit) result++;
if round > <$fty>::sign_bit() { result += <$fty>::one() }
// CC if (round == signBit) result += result & 1;
if round == <$fty>::sign_bit() { result += result & <$fty>::one() }
result
// CC }
};
// CC
// CC // Insert the exponent
// CC result += (rep_t)(exponent + exponentBias) << significandBits;
result += Wrapping((exponent + <$fty>::exponent_bias()) as <$fty as Float>::Int) << <$fty>::significand_bits();
// CC return fromRep(result);
<$fty as Float>::from_repr(result.0)
}
}
}

// __floatunsisf implements unsigned integer to single-precision conversion
// (IEEE-754 default round-to-nearest, ties-to-even mode)
floatunsisf!(__floatunsisf: u32 => f32);

#[cfg(test)]
mod tests {
use qc::{U32};
use float::{Float};

check! {
fn __floatunsisf(f: extern fn(u32) -> f32, a: U32)
-> Option<f32> {
Some(f(a.0))
}
}

#[test]
fn test_floatunsisf_conv_zero() {
let r = super::__floatunsisf(0);
assert!(r.eq_repr(0.0f32));
}

use std::mem;

#[test]
fn test_floatunsisf_libcompilerrt() {
let compiler_rt_fn = ::compiler_rt::get("__floatunsisf");
let compiler_rt_fn : extern fn(u32) -> f32 = unsafe { mem::transmute(compiler_rt_fn) };
//println!("1231515 {:?}", compiler_rt_fn);
let ans = compiler_rt_fn(0);
println!("{:?}", ans);
assert!(ans.eq_repr(0.0f32));

}
}
86 changes: 78 additions & 8 deletions src/float/mod.rs
Original file line number Diff line number Diff line change
@@ -1,20 +1,83 @@
use core::mem;
use core::num::Wrapping;
use core::ops;
use core::convert;
#[cfg(test)]
use core::fmt;

pub mod add;
pub mod pow;
pub mod conv;

/// Trait for some basic operations on floats
pub trait Float: Sized + Copy {
pub trait Float: Sized + Copy
where Wrapping<Self::Int> : ops::Shl<usize, Output = Wrapping<Self::Int>>
+ ops::Shr<usize, Output = Wrapping<Self::Int>>
+ ops::Sub<Wrapping<Self::Int>, Output = Wrapping<Self::Int>>
+ ops::BitOr<Wrapping<Self::Int>, Output = Wrapping<Self::Int>>
+ ops::BitXor<Wrapping<Self::Int>, Output = Wrapping<Self::Int>>,
Self::Int : convert::From<u32>
{
/// A uint of the same with as the float
type Int;

fn one() -> Wrapping<Self::Int> {
Wrapping(Self::Int::from(1u32))
}

fn zero() -> Wrapping<Self::Int> {
Wrapping(Self::Int::from(0u32))
}

/// Returns the bitwidth of the float type
fn bits() -> u32;
fn bits() -> usize;

/// Returns the bitwidth of the significand
fn significand_bits() -> u32;
fn significand_bits() -> usize;

fn exponent_bits() -> usize {
Self::bits() - Self::significand_bits() - 1
}

fn max_exponent() -> usize {
(1usize << Self::exponent_bits()).wrapping_sub(1)
}

fn exponent_bias() -> usize {
Self::max_exponent() >> 1
}

fn implicit_bit() -> Wrapping<Self::Int> {
Self::one() << Self::significand_bits()
}

fn significand_mask() -> Wrapping<Self::Int> {
Self::implicit_bit() - Self::one()
}

fn sign_bit() -> Wrapping<Self::Int> {
Self::one() << (Self::significand_bits() + Self::exponent_bits())
}

fn abs_mask() -> Wrapping<Self::Int> {
Self::sign_bit() - Self::one()
}

fn exponent_mask() -> Wrapping<Self::Int> {
Self::abs_mask() ^ Self::significand_mask()
}

fn inf_rep() -> Wrapping<Self::Int> {
Self::exponent_mask()
}

fn quiet_bit() -> Wrapping<Self::Int> {
Self::implicit_bit() >> 1
}

fn qnan_rep() -> Wrapping<Self::Int> {
Self::exponent_mask() | Self::quiet_bit()
}

/// Returns `self` transmuted to `Self::Int`
fn repr(self) -> Self::Int;
Expand All @@ -34,10 +97,10 @@ pub trait Float: Sized + Copy {

impl Float for f32 {
type Int = u32;
fn bits() -> u32 {
fn bits() -> usize {
32
}
fn significand_bits() -> u32 {
fn significand_bits() -> usize {
23
}
fn repr(self) -> Self::Int {
Expand All @@ -60,12 +123,13 @@ impl Float for f32 {
(1i32.wrapping_sub(shift as i32), significand << shift as Self::Int)
}
}

impl Float for f64 {
type Int = u64;
fn bits() -> u32 {
fn bits() -> usize {
64
}
fn significand_bits() -> u32 {
fn significand_bits() -> usize {
52
}
fn repr(self) -> Self::Int {
Expand Down Expand Up @@ -95,7 +159,13 @@ impl Float for f64 {
pub struct FRepr<F>(F);

#[cfg(test)]
impl<F: Float> PartialEq for FRepr<F> {
impl<F: Float> PartialEq for FRepr<F>
where Wrapping<F::Int> : ops::Shl<usize, Output = Wrapping<F::Int>>
+ ops::Shr<usize, Output = Wrapping<F::Int>>
+ ops::Sub<Wrapping<F::Int>, Output = Wrapping<F::Int>>
+ ops::BitOr<Wrapping<F::Int>, Output = Wrapping<F::Int>>
+ ops::BitXor<Wrapping<F::Int>, Output = Wrapping<F::Int>>
{
fn eq(&self, other: &FRepr<F>) -> bool {
// NOTE(cfg) for some reason, on hard float targets, our implementation doesn't
// match the output of its gcc_s counterpart. Until we investigate further, we'll
Expand Down