Skip to content

Commit e871d09

Browse files
authored
Auto merge of #111 - mattico:new_float_quickcheck, r=japaric
Add float quickcheck
2 parents 33dc132 + 6504950 commit e871d09

File tree

6 files changed

+144
-139
lines changed

6 files changed

+144
-139
lines changed

build.rs

+6
Original file line numberDiff line numberDiff line change
@@ -360,6 +360,10 @@ fn main() {
360360

361361
}
362362

363+
if llvm_target.last().unwrap().ends_with("gnueabi") {
364+
sources.extend(&["addsf3.c", "adddf3.c"]);
365+
}
366+
363367
if target_arch == "aarch64" {
364368
sources.extend(&["comparetf2.c",
365369
"extenddftf2.c",
@@ -426,6 +430,8 @@ fn main() {
426430
// To filter away some flaky test (see src/float/add.rs for details)
427431
if llvm_target.last() == Some(&"gnueabihf") {
428432
println!("cargo:rustc-cfg=gnueabihf")
433+
} else if llvm_target.last() == Some(&"gnueabi") {
434+
println!("cargo:rustc-cfg=gnueabi")
429435
}
430436

431437
// To compile intrinsics.rs for thumb targets, where there is no libc

src/arm.rs

+6
Original file line numberDiff line numberDiff line change
@@ -58,11 +58,17 @@ pub unsafe fn __aeabi_ldivmod() {
5858
}
5959

6060
// TODO: These aeabi_* functions should be defined as aliases
61+
62+
// NOTE(cfg) The gnueabi targets use the C implementation for now
63+
// https://github.com/rust-lang-nursery/compiler-builtins/issues/90
64+
65+
#[cfg(not(gnueabi))]
6166
#[cfg_attr(not(test), no_mangle)]
6267
pub extern "C" fn __aeabi_dadd(a: f64, b: f64) -> f64 {
6368
::float::add::__adddf3(a, b)
6469
}
6570

71+
#[cfg(not(gnueabi))]
6672
#[cfg_attr(not(test), no_mangle)]
6773
pub extern "C" fn __aeabi_fadd(a: f32, b: f32) -> f32 {
6874
::float::add::__addsf3(a, b)

src/float/add.rs

+8-99
Original file line numberDiff line numberDiff line change
@@ -187,111 +187,20 @@ add!(__adddf3: f64);
187187
#[cfg(test)]
188188
mod tests {
189189
use core::{f32, f64};
190+
use qc::{F32, F64};
190191

191-
use float::{Float, FRepr};
192-
use qc::{U32, U64};
193-
194-
// TODO: Add F32/F64 to qc so that they print the right values (at the very least)
195192
check! {
196193
fn __addsf3(f: extern fn(f32, f32) -> f32,
197-
a: U32,
198-
b: U32)
199-
-> Option<FRepr<f32> > {
200-
let (a, b) = (f32::from_repr(a.0), f32::from_repr(b.0));
201-
Some(FRepr(f(a, b)))
194+
a: F32,
195+
b: F32)
196+
-> Option<F32> {
197+
Some(F32(f(a.0, b.0)))
202198
}
203199

204200
fn __adddf3(f: extern fn(f64, f64) -> f64,
205-
a: U64,
206-
b: U64) -> Option<FRepr<f64> > {
207-
let (a, b) = (f64::from_repr(a.0), f64::from_repr(b.0));
208-
Some(FRepr(f(a, b)))
201+
a: F64,
202+
b: F64) -> Option<F64> {
203+
Some(F64(f(a.0, b.0)))
209204
}
210205
}
211-
212-
// More tests for special float values
213-
214-
#[test]
215-
fn test_float_tiny_plus_tiny() {
216-
let tiny = f32::from_repr(1);
217-
let r = super::__addsf3(tiny, tiny);
218-
assert!(r.eq_repr(tiny + tiny));
219-
}
220-
221-
#[test]
222-
fn test_double_tiny_plus_tiny() {
223-
let tiny = f64::from_repr(1);
224-
let r = super::__adddf3(tiny, tiny);
225-
assert!(r.eq_repr(tiny + tiny));
226-
}
227-
228-
#[test]
229-
fn test_float_small_plus_small() {
230-
let a = f32::from_repr(327);
231-
let b = f32::from_repr(256);
232-
let r = super::__addsf3(a, b);
233-
assert!(r.eq_repr(a + b));
234-
}
235-
236-
#[test]
237-
fn test_double_small_plus_small() {
238-
let a = f64::from_repr(327);
239-
let b = f64::from_repr(256);
240-
let r = super::__adddf3(a, b);
241-
assert!(r.eq_repr(a + b));
242-
}
243-
244-
#[test]
245-
fn test_float_one_plus_one() {
246-
let r = super::__addsf3(1f32, 1f32);
247-
assert!(r.eq_repr(1f32 + 1f32));
248-
}
249-
250-
#[test]
251-
fn test_double_one_plus_one() {
252-
let r = super::__adddf3(1f64, 1f64);
253-
assert!(r.eq_repr(1f64 + 1f64));
254-
}
255-
256-
#[test]
257-
fn test_float_different_nan() {
258-
let a = f32::from_repr(1);
259-
let b = f32::from_repr(0b11111111100100010001001010101010);
260-
let x = super::__addsf3(a, b);
261-
let y = a + b;
262-
assert!(x.eq_repr(y));
263-
}
264-
265-
#[test]
266-
fn test_double_different_nan() {
267-
let a = f64::from_repr(1);
268-
let b = f64::from_repr(0b1111111111110010001000100101010101001000101010000110100011101011);
269-
let x = super::__adddf3(a, b);
270-
let y = a + b;
271-
assert!(x.eq_repr(y));
272-
}
273-
274-
#[test]
275-
fn test_float_nan() {
276-
let r = super::__addsf3(f32::NAN, 1.23);
277-
assert_eq!(r.repr(), f32::NAN.repr());
278-
}
279-
280-
#[test]
281-
fn test_double_nan() {
282-
let r = super::__adddf3(f64::NAN, 1.23);
283-
assert_eq!(r.repr(), f64::NAN.repr());
284-
}
285-
286-
#[test]
287-
fn test_float_inf() {
288-
let r = super::__addsf3(f32::INFINITY, -123.4);
289-
assert_eq!(r, f32::INFINITY);
290-
}
291-
292-
#[test]
293-
fn test_double_inf() {
294-
let r = super::__adddf3(f64::INFINITY, -123.4);
295-
assert_eq!(r, f64::INFINITY);
296-
}
297206
}

src/float/mod.rs

+52-30
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
use core::mem;
2-
#[cfg(test)]
3-
use core::fmt;
42

3+
// NOTE(cfg) add is disabled for gnueabi due to
4+
// https://github.com/rust-lang-nursery/compiler-builtins/issues/90
5+
6+
#[cfg(not(gnueabi))]
57
pub mod add;
68
pub mod pow;
79

@@ -16,22 +18,41 @@ pub trait Float: Sized + Copy {
1618
/// Returns the bitwidth of the significand
1719
fn significand_bits() -> u32;
1820

21+
/// Returns the bitwidth of the exponent
22+
fn exponent_bits() -> u32 {
23+
Self::bits() - Self::significand_bits() - 1
24+
}
25+
26+
/// Returns a mask for the sign bit
27+
fn sign_mask() -> Self::Int;
28+
29+
/// Returns a mask for the significand
30+
fn significand_mask() -> Self::Int;
31+
32+
/// Returns a mask for the exponent
33+
fn exponent_mask() -> Self::Int;
34+
1935
/// Returns `self` transmuted to `Self::Int`
2036
fn repr(self) -> Self::Int;
2137

2238
#[cfg(test)]
2339
/// Checks if two floats have the same bit representation. *Except* for NaNs! NaN can be
24-
/// represented in multiple different ways. This methods returns `true` if two NaNs are
40+
/// represented in multiple different ways. This method returns `true` if two NaNs are
2541
/// compared.
2642
fn eq_repr(self, rhs: Self) -> bool;
2743

2844
/// Returns a `Self::Int` transmuted back to `Self`
2945
fn from_repr(a: Self::Int) -> Self;
3046

47+
/// Constructs a `Self` from its parts. Inputs are treated as bits and shifted into position.
48+
fn from_parts(sign: bool, exponent: Self::Int, significand: Self::Int) -> Self;
49+
3150
/// Returns (normalized exponent, normalized significand)
3251
fn normalize(significand: Self::Int) -> (i32, Self::Int);
3352
}
3453

54+
// FIXME: Some of this can be removed if RFC Issue #1424 is resolved
55+
// https://github.com/rust-lang/rfcs/issues/1424
3556
impl Float for f32 {
3657
type Int = u32;
3758
fn bits() -> u32 {
@@ -40,6 +61,15 @@ impl Float for f32 {
4061
fn significand_bits() -> u32 {
4162
23
4263
}
64+
fn sign_mask() -> Self::Int {
65+
1 << (Self::bits() - 1)
66+
}
67+
fn significand_mask() -> Self::Int {
68+
(1 << Self::significand_bits()) - 1
69+
}
70+
fn exponent_mask() -> Self::Int {
71+
!(Self::sign_mask() | Self::significand_mask())
72+
}
4373
fn repr(self) -> Self::Int {
4474
unsafe { mem::transmute(self) }
4575
}
@@ -54,6 +84,11 @@ impl Float for f32 {
5484
fn from_repr(a: Self::Int) -> Self {
5585
unsafe { mem::transmute(a) }
5686
}
87+
fn from_parts(sign: bool, exponent: Self::Int, significand: Self::Int) -> Self {
88+
Self::from_repr(((sign as Self::Int) << (Self::bits() - 1)) |
89+
((exponent << Self::significand_bits()) & Self::exponent_mask()) |
90+
(significand & Self::significand_mask()))
91+
}
5792
fn normalize(significand: Self::Int) -> (i32, Self::Int) {
5893
let shift = significand.leading_zeros()
5994
.wrapping_sub((1u32 << Self::significand_bits()).leading_zeros());
@@ -68,6 +103,15 @@ impl Float for f64 {
68103
fn significand_bits() -> u32 {
69104
52
70105
}
106+
fn sign_mask() -> Self::Int {
107+
1 << (Self::bits() - 1)
108+
}
109+
fn significand_mask() -> Self::Int {
110+
(1 << Self::significand_bits()) - 1
111+
}
112+
fn exponent_mask() -> Self::Int {
113+
!(Self::sign_mask() | Self::significand_mask())
114+
}
71115
fn repr(self) -> Self::Int {
72116
unsafe { mem::transmute(self) }
73117
}
@@ -82,36 +126,14 @@ impl Float for f64 {
82126
fn from_repr(a: Self::Int) -> Self {
83127
unsafe { mem::transmute(a) }
84128
}
129+
fn from_parts(sign: bool, exponent: Self::Int, significand: Self::Int) -> Self {
130+
Self::from_repr(((sign as Self::Int) << (Self::bits() - 1)) |
131+
((exponent << Self::significand_bits()) & Self::exponent_mask()) |
132+
(significand & Self::significand_mask()))
133+
}
85134
fn normalize(significand: Self::Int) -> (i32, Self::Int) {
86135
let shift = significand.leading_zeros()
87136
.wrapping_sub((1u64 << Self::significand_bits()).leading_zeros());
88137
(1i32.wrapping_sub(shift as i32), significand << shift as Self::Int)
89138
}
90139
}
91-
92-
// TODO: Move this to F32/F64 in qc.rs
93-
#[cfg(test)]
94-
#[derive(Copy, Clone)]
95-
pub struct FRepr<F>(F);
96-
97-
#[cfg(test)]
98-
impl<F: Float> PartialEq for FRepr<F> {
99-
fn eq(&self, other: &FRepr<F>) -> bool {
100-
// NOTE(cfg) for some reason, on hard float targets, our implementation doesn't
101-
// match the output of its gcc_s counterpart. Until we investigate further, we'll
102-
// just avoid testing against gcc_s on those targets. Do note that our
103-
// implementation matches the output of the FPU instruction on *hard* float targets
104-
// and matches its gcc_s counterpart on *soft* float targets.
105-
if cfg!(gnueabihf) {
106-
return true
107-
}
108-
self.0.eq_repr(other.0)
109-
}
110-
}
111-
112-
#[cfg(test)]
113-
impl<F: fmt::Debug> fmt::Debug for FRepr<F> {
114-
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
115-
self.0.fmt(f)
116-
}
117-
}

src/float/pow.rs

+7-10
Original file line numberDiff line numberDiff line change
@@ -31,22 +31,19 @@ pow!(__powidf2: f64, i32);
3131

3232
#[cfg(test)]
3333
mod tests {
34-
use float::{Float, FRepr};
35-
use qc::{I32, U32, U64};
34+
use qc::{I32, F32, F64};
3635

3736
check! {
3837
fn __powisf2(f: extern fn(f32, i32) -> f32,
39-
a: U32,
40-
b: I32) -> Option<FRepr<f32> > {
41-
let (a, b) = (f32::from_repr(a.0), b.0);
42-
Some(FRepr(f(a, b)))
38+
a: F32,
39+
b: I32) -> Option<F32> {
40+
Some(F32(f(a.0, b.0)))
4341
}
4442

4543
fn __powidf2(f: extern fn(f64, i32) -> f64,
46-
a: U64,
47-
b: I32) -> Option<FRepr<f64> > {
48-
let (a, b) = (f64::from_repr(a.0), b.0);
49-
Some(FRepr(f(a, b)))
44+
a: F64,
45+
b: I32) -> Option<F64> {
46+
Some(F64(f(a.0, b.0)))
5047
}
5148
}
5249
}

0 commit comments

Comments
 (0)