Skip to content

Commit ebadb12

Browse files
author
Jorge Aparicio
authored
Merge pull request #43 from mattico/add-add_f3-builtins
Add soft float addition builtins
2 parents 8603e64 + da53b70 commit ebadb12

File tree

11 files changed

+474
-80
lines changed

11 files changed

+474
-80
lines changed

README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@ See [rust-lang/rust#35437][0].
2626

2727
## Progress
2828

29-
- [ ] adddf3.c
30-
- [ ] addsf3.c
29+
- [x] adddf3.c
30+
- [x] addsf3.c
3131
- [ ] arm/adddf3vfp.S
3232
- [ ] arm/addsf3vfp.S
3333
- [ ] arm/aeabi_dcmp.S

src/arm.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -60,12 +60,12 @@ pub unsafe fn __aeabi_ldivmod() {
6060
// TODO: These two functions should be defined as aliases
6161
#[cfg_attr(not(test), no_mangle)]
6262
pub extern "C" fn __aeabi_uidiv(a: u32, b: u32) -> u32 {
63-
::udiv::__udivsi3(a, b)
63+
::int::udiv::__udivsi3(a, b)
6464
}
6565

6666
#[cfg_attr(not(test), no_mangle)]
6767
pub extern "C" fn __aeabi_idiv(a: i32, b: i32) -> i32 {
68-
::sdiv::__divsi3(a, b)
68+
::int::sdiv::__divsi3(a, b)
6969
}
7070

7171
extern "C" {

src/float/add.rs

+324
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,324 @@
1+
use core::num::Wrapping;
2+
use float::Float;
3+
4+
macro_rules! add {
5+
($intrinsic:ident: $ty:ty) => {
6+
/// Returns `a + b`
7+
#[allow(unused_parens)]
8+
#[cfg_attr(not(test), no_mangle)]
9+
pub extern fn $intrinsic(a: $ty, b: $ty) -> $ty {
10+
let one = Wrapping(1 as <$ty as Float>::Int);
11+
let zero = Wrapping(0 as <$ty as Float>::Int);
12+
13+
let bits = Wrapping(<$ty>::bits() as <$ty as Float>::Int);
14+
let significand_bits = Wrapping(<$ty>::significand_bits() as <$ty as Float>::Int);
15+
let exponent_bits = bits - significand_bits - one;
16+
let max_exponent = (one << exponent_bits.0 as usize) - one;
17+
18+
let implicit_bit = one << significand_bits.0 as usize;
19+
let significand_mask = implicit_bit - one;
20+
let sign_bit = one << (significand_bits + exponent_bits).0 as usize;
21+
let abs_mask = sign_bit - one;
22+
let exponent_mask = abs_mask ^ significand_mask;
23+
let inf_rep = exponent_mask;
24+
let quiet_bit = implicit_bit >> 1;
25+
let qnan_rep = exponent_mask | quiet_bit;
26+
27+
let mut a_rep = Wrapping(a.repr());
28+
let mut b_rep = Wrapping(b.repr());
29+
let a_abs = a_rep & abs_mask;
30+
let b_abs = b_rep & abs_mask;
31+
32+
// Detect if a or b is zero, infinity, or NaN.
33+
if a_abs - one >= inf_rep - one ||
34+
b_abs - one >= inf_rep - one {
35+
// NaN + anything = qNaN
36+
if a_abs > inf_rep {
37+
return (<$ty as Float>::from_repr((a_abs | quiet_bit).0));
38+
}
39+
// anything + NaN = qNaN
40+
if b_abs > inf_rep {
41+
return (<$ty as Float>::from_repr((b_abs | quiet_bit).0));
42+
}
43+
44+
if a_abs == inf_rep {
45+
// +/-infinity + -/+infinity = qNaN
46+
if (a.repr() ^ b.repr()) == sign_bit.0 {
47+
return (<$ty as Float>::from_repr(qnan_rep.0));
48+
} else {
49+
// +/-infinity + anything remaining = +/- infinity
50+
return a;
51+
}
52+
}
53+
54+
// anything remaining + +/-infinity = +/-infinity
55+
if b_abs == inf_rep {
56+
return b;
57+
}
58+
59+
// zero + anything = anything
60+
if a_abs.0 == 0 {
61+
// but we need to get the sign right for zero + zero
62+
if b_abs.0 == 0 {
63+
return (<$ty as Float>::from_repr(a.repr() & b.repr()));
64+
} else {
65+
return b;
66+
}
67+
}
68+
69+
// anything + zero = anything
70+
if b_abs.0 == 0 {
71+
return a;
72+
}
73+
}
74+
75+
// Swap a and b if necessary so that a has the larger absolute value.
76+
if b_abs > a_abs {
77+
::core::mem::swap(&mut a_rep, &mut b_rep);
78+
}
79+
80+
// Extract the exponent and significand from the (possibly swapped) a and b.
81+
let mut a_exponent = Wrapping((a_rep >> significand_bits.0 as usize & max_exponent).0 as i32);
82+
let mut b_exponent = Wrapping((b_rep >> significand_bits.0 as usize & max_exponent).0 as i32);
83+
let mut a_significand = a_rep & significand_mask;
84+
let mut b_significand = b_rep & significand_mask;
85+
86+
// normalize any denormals, and adjust the exponent accordingly.
87+
if a_exponent.0 == 0 {
88+
let (exponent, significand) = <$ty>::normalize(a_significand.0);
89+
a_exponent = Wrapping(exponent);
90+
a_significand = Wrapping(significand);
91+
}
92+
if b_exponent.0 == 0 {
93+
let (exponent, significand) = <$ty>::normalize(b_significand.0);
94+
b_exponent = Wrapping(exponent);
95+
b_significand = Wrapping(significand);
96+
}
97+
98+
// The sign of the result is the sign of the larger operand, a. If they
99+
// have opposite signs, we are performing a subtraction; otherwise addition.
100+
let result_sign = a_rep & sign_bit;
101+
let subtraction = ((a_rep ^ b_rep) & sign_bit) != zero;
102+
103+
// Shift the significands to give us round, guard and sticky, and or in the
104+
// implicit significand bit. (If we fell through from the denormal path it
105+
// was already set by normalize(), but setting it twice won't hurt
106+
// anything.)
107+
a_significand = (a_significand | implicit_bit) << 3;
108+
b_significand = (b_significand | implicit_bit) << 3;
109+
110+
// Shift the significand of b by the difference in exponents, with a sticky
111+
// bottom bit to get rounding correct.
112+
let align = Wrapping((a_exponent - b_exponent).0 as <$ty as Float>::Int);
113+
if align.0 != 0 {
114+
if align < bits {
115+
let sticky = ((b_significand << (bits - align).0 as usize).0 != 0) as <$ty as Float>::Int;
116+
b_significand = (b_significand >> align.0 as usize) | Wrapping(sticky);
117+
} else {
118+
b_significand = one; // sticky; b is known to be non-zero.
119+
}
120+
}
121+
if subtraction {
122+
a_significand -= b_significand;
123+
// If a == -b, return +zero.
124+
if a_significand.0 == 0 {
125+
return (<$ty as Float>::from_repr(0));
126+
}
127+
128+
// If partial cancellation occured, we need to left-shift the result
129+
// and adjust the exponent:
130+
if a_significand < implicit_bit << 3 {
131+
let shift = a_significand.0.leading_zeros() as i32
132+
- (implicit_bit << 3).0.leading_zeros() as i32;
133+
a_significand <<= shift as usize;
134+
a_exponent -= Wrapping(shift);
135+
}
136+
} else /* addition */ {
137+
a_significand += b_significand;
138+
139+
// If the addition carried up, we need to right-shift the result and
140+
// adjust the exponent:
141+
if (a_significand & implicit_bit << 4).0 != 0 {
142+
let sticky = ((a_significand & one).0 != 0) as <$ty as Float>::Int;
143+
a_significand = a_significand >> 1 | Wrapping(sticky);
144+
a_exponent += Wrapping(1);
145+
}
146+
}
147+
148+
// If we have overflowed the type, return +/- infinity:
149+
if a_exponent >= Wrapping(max_exponent.0 as i32) {
150+
return (<$ty>::from_repr((inf_rep | result_sign).0));
151+
}
152+
153+
if a_exponent.0 <= 0 {
154+
// Result is denormal before rounding; the exponent is zero and we
155+
// need to shift the significand.
156+
let shift = Wrapping((Wrapping(1) - a_exponent).0 as <$ty as Float>::Int);
157+
let sticky = ((a_significand << (bits - shift).0 as usize).0 != 0) as <$ty as Float>::Int;
158+
a_significand = a_significand >> shift.0 as usize | Wrapping(sticky);
159+
a_exponent = Wrapping(0);
160+
}
161+
162+
// Low three bits are round, guard, and sticky.
163+
let round_guard_sticky: i32 = (a_significand.0 & 0x7) as i32;
164+
165+
// Shift the significand into place, and mask off the implicit bit.
166+
let mut result = a_significand >> 3 & significand_mask;
167+
168+
// Insert the exponent and sign.
169+
result |= Wrapping(a_exponent.0 as <$ty as Float>::Int) << significand_bits.0 as usize;
170+
result |= result_sign;
171+
172+
// Final rounding. The result may overflow to infinity, but that is the
173+
// correct result in that case.
174+
if round_guard_sticky > 0x4 { result += one; }
175+
if round_guard_sticky == 0x4 { result += result & one; }
176+
return (<$ty>::from_repr(result.0));
177+
}
178+
}
179+
}
180+
181+
add!(__addsf3: f32);
182+
add!(__adddf3: f64);
183+
184+
// FIXME: Implement these using aliases
185+
#[cfg(target_arch = "arm")]
186+
#[cfg_attr(not(test), no_mangle)]
187+
pub extern fn __aeabi_dadd(a: f64, b: f64) -> f64 {
188+
__adddf3(a, b)
189+
}
190+
191+
#[cfg(target_arch = "arm")]
192+
#[cfg_attr(not(test), no_mangle)]
193+
pub extern fn __aeabi_fadd(a: f32, b: f32) -> f32 {
194+
__addsf3(a, b)
195+
}
196+
197+
#[cfg(test)]
198+
mod tests {
199+
use core::{f32, f64};
200+
use qc::{U32, U64};
201+
use float::Float;
202+
203+
// NOTE The tests below have special handing for NaN values.
204+
// Because NaN != NaN, the floating-point representations must be used
205+
// Because there are many diffferent values of NaN, and the implementation
206+
// doesn't care about calculating the 'correct' one, if both values are NaN
207+
// the values are considered equivalent.
208+
209+
// TODO: Add F32/F64 to qc so that they print the right values (at the very least)
210+
quickcheck! {
211+
fn addsf3(a: U32, b: U32) -> bool {
212+
let (a, b) = (f32::from_repr(a.0), f32::from_repr(b.0));
213+
let x = super::__addsf3(a, b);
214+
let y = a + b;
215+
if !(x.is_nan() && y.is_nan()) {
216+
x.repr() == y.repr()
217+
} else {
218+
true
219+
}
220+
}
221+
222+
fn adddf3(a: U64, b: U64) -> bool {
223+
let (a, b) = (f64::from_repr(a.0), f64::from_repr(b.0));
224+
let x = super::__adddf3(a, b);
225+
let y = a + b;
226+
if !(x.is_nan() && y.is_nan()) {
227+
x.repr() == y.repr()
228+
} else {
229+
true
230+
}
231+
}
232+
}
233+
234+
// More tests for special float values
235+
236+
#[test]
237+
fn test_float_tiny_plus_tiny() {
238+
let tiny = f32::from_repr(1);
239+
let r = super::__addsf3(tiny, tiny);
240+
assert_eq!(r, tiny + tiny);
241+
}
242+
243+
#[test]
244+
fn test_double_tiny_plus_tiny() {
245+
let tiny = f64::from_repr(1);
246+
let r = super::__adddf3(tiny, tiny);
247+
assert_eq!(r, tiny + tiny);
248+
}
249+
250+
#[test]
251+
fn test_float_small_plus_small() {
252+
let a = f32::from_repr(327);
253+
let b = f32::from_repr(256);
254+
let r = super::__addsf3(a, b);
255+
assert_eq!(r, a + b);
256+
}
257+
258+
#[test]
259+
fn test_double_small_plus_small() {
260+
let a = f64::from_repr(327);
261+
let b = f64::from_repr(256);
262+
let r = super::__adddf3(a, b);
263+
assert_eq!(r, a + b);
264+
}
265+
266+
#[test]
267+
fn test_float_one_plus_one() {
268+
let r = super::__addsf3(1f32, 1f32);
269+
assert_eq!(r, 1f32 + 1f32);
270+
}
271+
272+
#[test]
273+
fn test_double_one_plus_one() {
274+
let r = super::__adddf3(1f64, 1f64);
275+
assert_eq!(r, 1f64 + 1f64);
276+
}
277+
278+
#[test]
279+
fn test_float_different_nan() {
280+
let a = f32::from_repr(1);
281+
let b = f32::from_repr(0b11111111100100010001001010101010);
282+
let x = super::__addsf3(a, b);
283+
let y = a + b;
284+
if !(x.is_nan() && y.is_nan()) {
285+
assert_eq!(x.repr(), y.repr());
286+
}
287+
}
288+
289+
#[test]
290+
fn test_double_different_nan() {
291+
let a = f64::from_repr(1);
292+
let b = f64::from_repr(
293+
0b1111111111110010001000100101010101001000101010000110100011101011);
294+
let x = super::__adddf3(a, b);
295+
let y = a + b;
296+
if !(x.is_nan() && y.is_nan()) {
297+
assert_eq!(x.repr(), y.repr());
298+
}
299+
}
300+
301+
#[test]
302+
fn test_float_nan() {
303+
let r = super::__addsf3(f32::NAN, 1.23);
304+
assert_eq!(r.repr(), f32::NAN.repr());
305+
}
306+
307+
#[test]
308+
fn test_double_nan() {
309+
let r = super::__adddf3(f64::NAN, 1.23);
310+
assert_eq!(r.repr(), f64::NAN.repr());
311+
}
312+
313+
#[test]
314+
fn test_float_inf() {
315+
let r = super::__addsf3(f32::INFINITY, -123.4);
316+
assert_eq!(r, f32::INFINITY);
317+
}
318+
319+
#[test]
320+
fn test_double_inf() {
321+
let r = super::__adddf3(f64::INFINITY, -123.4);
322+
assert_eq!(r, f64::INFINITY);
323+
}
324+
}

0 commit comments

Comments
 (0)