Skip to content

Commit 293fef5

Browse files
ithinuelJorge Aparicio
authored and
Jorge Aparicio
committed
impl (unsigned/signed) int to single/double precision float conversion based on llvm algorithms.
1 parent 0507842 commit 293fef5

File tree

6 files changed

+238
-16
lines changed

6 files changed

+238
-16
lines changed

compiler-rt/compiler-rt-cdylib/build.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,12 @@ fn main() {
6262
"powisf2.c",
6363
"subdf3.c",
6464
"subsf3.c",
65+
"floatsisf.c",
66+
"floatsidf.c",
67+
"floatdidf.c",
68+
"floatunsisf.c",
69+
"floatunsidf.c",
70+
"floatundidf.c",
6571
// 128 bit integers
6672
"lshrti3.c",
6773
"modti3.c",

compiler-rt/compiler-rt-cdylib/src/lib.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,12 @@ extern {
2626
fn __powidf2();
2727
fn __subsf3();
2828
fn __subdf3();
29+
fn __floatsisf();
30+
fn __floatsidf();
31+
fn __floatdidf();
32+
fn __floatunsisf();
33+
fn __floatunsidf();
34+
fn __floatundidf();
2935
}
3036

3137
macro_rules! declare {
@@ -61,6 +67,12 @@ declare!(___powisf2, __powisf2);
6167
declare!(___powidf2, __powidf2);
6268
declare!(___subsf3, __subsf3);
6369
declare!(___subdf3, __subdf3);
70+
declare!(___floatsisf, __floatsisf);
71+
declare!(___floatsidf, __floatsidf);
72+
declare!(___floatdidf, __floatdidf);
73+
declare!(___floatunsisf, __floatunsisf);
74+
declare!(___floatunsidf, __floatunsidf);
75+
declare!(___floatundidf, __floatundidf);
6476

6577
#[cfg(all(not(windows),
6678
not(target_arch = "mips64"),

src/arm.rs

100644100755
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,12 @@ pub extern "aapcs" fn __aeabi_uidiv(a: u32, b: u32) -> u32 {
112112
::int::udiv::__udivsi3(a, b)
113113
}
114114

115+
#[cfg(not(feature = "c"))]
116+
#[cfg_attr(not(test), no_mangle)]
117+
pub extern "C" fn __aeabi_ui2d(a: u32) -> f64 {
118+
::float::conv::__floatunsidf(a)
119+
}
120+
115121
// TODO: These aeabi_* functions should be defined as aliases
116122
#[cfg(not(feature = "mem"))]
117123
extern "C" {

src/float/conv.rs

Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
use float::Float;
2+
use int::Int;
3+
4+
macro_rules! fp_overflow {
5+
(infinity, $fty:ty, $sign: expr) => {
6+
return {
7+
<$fty as Float>::from_parts(
8+
$sign,
9+
<$fty as Float>::exponent_max() as <$fty as Float>::Int,
10+
0 as <$fty as Float>::Int)
11+
}
12+
}
13+
}
14+
15+
macro_rules! fp_convert {
16+
($intrinsic:ident: $ity:ty, $fty:ty) => {
17+
18+
pub extern "C" fn $intrinsic(i: $ity) -> $fty {
19+
if i == 0 {
20+
return 0.0
21+
}
22+
23+
let mant_dig = <$fty>::significand_bits() + 1;
24+
let exponent_bias = <$fty>::exponent_bias();
25+
26+
let n = <$ity>::bits();
27+
let (s, a) = i.extract_sign();
28+
let mut a = a;
29+
30+
// number of significant digits
31+
let sd = n - a.leading_zeros();
32+
33+
// exponent
34+
let mut e = sd - 1;
35+
36+
if <$ity>::bits() < mant_dig {
37+
return <$fty>::from_parts(s,
38+
(e + exponent_bias) as <$fty as Float>::Int,
39+
(a as <$fty as Float>::Int) << (mant_dig - e - 1))
40+
}
41+
42+
a = if sd > mant_dig {
43+
/* start: 0000000000000000000001xxxxxxxxxxxxxxxxxxxxxxPQxxxxxxxxxxxxxxxxxx
44+
* finish: 000000000000000000000000000000000000001xxxxxxxxxxxxxxxxxxxxxxPQR
45+
* 12345678901234567890123456
46+
* 1 = msb 1 bit
47+
* P = bit MANT_DIG-1 bits to the right of 1
48+
* Q = bit MANT_DIG bits to the right of 1
49+
* R = "or" of all bits to the right of Q
50+
*/
51+
let mant_dig_plus_one = mant_dig + 1;
52+
let mant_dig_plus_two = mant_dig + 2;
53+
a = if sd == mant_dig_plus_one {
54+
a << 1
55+
} else if sd == mant_dig_plus_two {
56+
a
57+
} else {
58+
(a >> (sd - mant_dig_plus_two)) as <$ity as Int>::UnsignedInt |
59+
((a & <$ity as Int>::UnsignedInt::max_value()).wrapping_shl((n + mant_dig_plus_two) - sd) != 0) as <$ity as Int>::UnsignedInt
60+
};
61+
62+
/* finish: */
63+
a |= ((a & 4) != 0) as <$ity as Int>::UnsignedInt; /* Or P into R */
64+
a += 1; /* round - this step may add a significant bit */
65+
a >>= 2; /* dump Q and R */
66+
67+
/* a is now rounded to mant_dig or mant_dig+1 bits */
68+
if (a & (1 << mant_dig)) != 0 {
69+
a >>= 1; e += 1;
70+
}
71+
a
72+
/* a is now rounded to mant_dig bits */
73+
} else {
74+
a.wrapping_shl(mant_dig - sd)
75+
/* a is now rounded to mant_dig bits */
76+
};
77+
78+
<$fty>::from_parts(s,
79+
(e + exponent_bias) as <$fty as Float>::Int,
80+
a as <$fty as Float>::Int)
81+
}
82+
}
83+
}
84+
85+
fp_convert!(__floatsisf: i32, f32);
86+
fp_convert!(__floatsidf: i32, f64);
87+
fp_convert!(__floatdidf: i64, f64);
88+
fp_convert!(__floatunsisf: u32, f32);
89+
fp_convert!(__floatunsidf: u32, f64);
90+
fp_convert!(__floatundidf: u64, f64);
91+
92+
// NOTE(cfg) for some reason, on arm*-unknown-linux-gnueabihf, our implementation doesn't
93+
// match the output of its gcc_s or compiler-rt counterpart. Until we investigate further, we'll
94+
// just avoid testing against them on those targets. Do note that our implementation gives the
95+
// correct answer; gcc_s and compiler-rt are incorrect in this case.
96+
//
97+
#[cfg(all(test, not(arm_linux)))]
98+
mod tests {
99+
use qc::{I32, U32, I64, U64, F32, F64};
100+
101+
check! {
102+
fn __floatsisf(f: extern fn(i32) -> f32,
103+
a: I32)
104+
-> Option<F32> {
105+
Some(F32(f(a.0)))
106+
}
107+
fn __floatsidf(f: extern fn(i32) -> f64,
108+
a: I32)
109+
-> Option<F64> {
110+
Some(F64(f(a.0)))
111+
}
112+
fn __floatdidf(f: extern fn(i64) -> f64,
113+
a: I64)
114+
-> Option<F64> {
115+
Some(F64(f(a.0)))
116+
}
117+
fn __floatunsisf(f: extern fn(u32) -> f32,
118+
a: U32)
119+
-> Option<F32> {
120+
Some(F32(f(a.0)))
121+
}
122+
fn __floatunsidf(f: extern fn(u32) -> f64,
123+
a: U32)
124+
-> Option<F64> {
125+
Some(F64(f(a.0)))
126+
}
127+
fn __floatundidf(f: extern fn(u64) -> f64,
128+
a: U64)
129+
-> Option<F64> {
130+
Some(F64(f(a.0)))
131+
}
132+
}
133+
}

src/float/mod.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use core::mem;
22

3+
pub mod conv;
34
pub mod add;
45
pub mod pow;
56
pub mod sub;
@@ -19,6 +20,15 @@ pub trait Float: Sized + Copy {
1920
fn exponent_bits() -> u32 {
2021
Self::bits() - Self::significand_bits() - 1
2122
}
23+
/// Returns the maximum value of the exponent
24+
fn exponent_max() -> u32 {
25+
(1 << Self::exponent_bits()) - 1
26+
}
27+
28+
/// Returns the exponent bias value
29+
fn exponent_bias() -> u32 {
30+
Self::exponent_max() >> 1
31+
}
2232

2333
/// Returns a mask for the sign bit
2434
fn sign_mask() -> Self::Int;

src/int/mod.rs

100644100755
Lines changed: 71 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -19,30 +19,85 @@ pub mod udiv;
1919
pub trait Int {
2020
/// Type with the same width but other signedness
2121
type OtherSign;
22+
/// Unsigned version of Self
23+
type UnsignedInt;
24+
2225
/// Returns the bitwidth of the int type
2326
fn bits() -> u32;
27+
28+
/// Extracts the sign from self and returns a tuple.
29+
///
30+
/// # Examples
31+
///
32+
/// ```rust,ignore
33+
/// let i = -25_i32;
34+
/// let (sign, u) = i.extract_sign();
35+
/// assert_eq!(sign, true);
36+
/// assert_eq!(u, 25_u32);
37+
/// ```
38+
fn extract_sign(self) -> (bool, Self::UnsignedInt);
2439
}
2540

26-
macro_rules! int_impl {
27-
($ity:ty, $sty:ty, $bits:expr) => {
28-
impl Int for $ity {
29-
type OtherSign = $sty;
30-
fn bits() -> u32 {
31-
$bits
32-
}
33-
}
34-
impl Int for $sty {
35-
type OtherSign = $ity;
36-
fn bits() -> u32 {
37-
$bits
38-
}
41+
// TODO: Once i128/u128 support lands, we'll want to add impls for those as well
42+
impl Int for u32 {
43+
type OtherSign = i32;
44+
type UnsignedInt = u32;
45+
46+
fn bits() -> u32 {
47+
32
48+
}
49+
50+
fn extract_sign(self) -> (bool, u32) {
51+
(false, self)
52+
}
53+
}
54+
55+
impl Int for i32 {
56+
type OtherSign = u32;
57+
type UnsignedInt = u32;
58+
59+
fn bits() -> u32 {
60+
32
61+
}
62+
63+
fn extract_sign(self) -> (bool, u32) {
64+
if self < 0 {
65+
(true, !(self as u32) + 1)
66+
} else {
67+
(false, self as u32)
3968
}
4069
}
4170
}
4271

43-
int_impl!(i32, u32, 32);
44-
int_impl!(i64, u64, 64);
45-
int_impl!(i128, u128, 128);
72+
impl Int for u64 {
73+
type OtherSign = i64;
74+
type UnsignedInt = u64;
75+
76+
fn bits() -> u32 {
77+
64
78+
}
79+
80+
fn extract_sign(self) -> (bool, u64) {
81+
(false, self)
82+
}
83+
}
84+
85+
impl Int for i64 {
86+
type OtherSign = u64;
87+
type UnsignedInt = u64;
88+
89+
fn bits() -> u32 {
90+
64
91+
}
92+
93+
fn extract_sign(self) -> (bool, u64) {
94+
if self < 0 {
95+
(true, !(self as u64) + 1)
96+
} else {
97+
(false, self as u64)
98+
}
99+
}
100+
}
46101

47102
/// Trait to convert an integer to/from smaller parts
48103
pub trait LargeInt {

0 commit comments

Comments
 (0)