Skip to content

Commit 8992316

Browse files
author
Jorge Aparicio
authored
Merge pull request #111 from mattico/new_float_quickcheck
Add float quickcheck
2 parents 33dc132 + f68475e commit 8992316

File tree

5 files changed

+131
-145
lines changed

5 files changed

+131
-145
lines changed

build.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -424,8 +424,9 @@ fn main() {
424424
}
425425

426426
// To filter away some flaky test (see src/float/add.rs for details)
427-
if llvm_target.last() == Some(&"gnueabihf") {
428-
println!("cargo:rustc-cfg=gnueabihf")
427+
if llvm_target[0].starts_with("arm") &&
428+
llvm_target.last().unwrap().contains("gnueabi") {
429+
println!("cargo:rustc-cfg=arm_linux")
429430
}
430431

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

src/float/add.rs

+13-100
Original file line numberDiff line numberDiff line change
@@ -184,114 +184,27 @@ macro_rules! add {
184184
add!(__addsf3: f32);
185185
add!(__adddf3: f64);
186186

187-
#[cfg(test)]
187+
// NOTE(cfg) for some reason, on arm*-unknown-linux-gnueabi*, our implementation doesn't
188+
// match the output of its gcc_s or compiler-rt counterpart. Until we investigate further, we'll
189+
// just avoid testing against them on those targets. Do note that our implementation gives the
190+
// correct answer; gcc_s and compiler-rt are incorrect in this case.
191+
#[cfg(all(test, not(arm_linux)))]
188192
mod tests {
189193
use core::{f32, f64};
194+
use qc::{F32, F64};
190195

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)
195196
check! {
196197
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)))
198+
a: F32,
199+
b: F32)
200+
-> Option<F32> {
201+
Some(F32(f(a.0, b.0)))
202202
}
203203

204204
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)))
205+
a: F64,
206+
b: F64) -> Option<F64> {
207+
Some(F64(f(a.0, b.0)))
209208
}
210209
}
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-
}
297210
}

src/float/mod.rs

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

53
pub mod add;
64
pub mod pow;
@@ -16,22 +14,41 @@ pub trait Float: Sized + Copy {
1614
/// Returns the bitwidth of the significand
1715
fn significand_bits() -> u32;
1816

17+
/// Returns the bitwidth of the exponent
18+
fn exponent_bits() -> u32 {
19+
Self::bits() - Self::significand_bits() - 1
20+
}
21+
22+
/// Returns a mask for the sign bit
23+
fn sign_mask() -> Self::Int;
24+
25+
/// Returns a mask for the significand
26+
fn significand_mask() -> Self::Int;
27+
28+
/// Returns a mask for the exponent
29+
fn exponent_mask() -> Self::Int;
30+
1931
/// Returns `self` transmuted to `Self::Int`
2032
fn repr(self) -> Self::Int;
2133

2234
#[cfg(test)]
2335
/// 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
36+
/// represented in multiple different ways. This method returns `true` if two NaNs are
2537
/// compared.
2638
fn eq_repr(self, rhs: Self) -> bool;
2739

2840
/// Returns a `Self::Int` transmuted back to `Self`
2941
fn from_repr(a: Self::Int) -> Self;
3042

43+
/// Constructs a `Self` from its parts. Inputs are treated as bits and shifted into position.
44+
fn from_parts(sign: bool, exponent: Self::Int, significand: Self::Int) -> Self;
45+
3146
/// Returns (normalized exponent, normalized significand)
3247
fn normalize(significand: Self::Int) -> (i32, Self::Int);
3348
}
3449

50+
// FIXME: Some of this can be removed if RFC Issue #1424 is resolved
51+
// https://github.com/rust-lang/rfcs/issues/1424
3552
impl Float for f32 {
3653
type Int = u32;
3754
fn bits() -> u32 {
@@ -40,6 +57,15 @@ impl Float for f32 {
4057
fn significand_bits() -> u32 {
4158
23
4259
}
60+
fn sign_mask() -> Self::Int {
61+
1 << (Self::bits() - 1)
62+
}
63+
fn significand_mask() -> Self::Int {
64+
(1 << Self::significand_bits()) - 1
65+
}
66+
fn exponent_mask() -> Self::Int {
67+
!(Self::sign_mask() | Self::significand_mask())
68+
}
4369
fn repr(self) -> Self::Int {
4470
unsafe { mem::transmute(self) }
4571
}
@@ -54,6 +80,11 @@ impl Float for f32 {
5480
fn from_repr(a: Self::Int) -> Self {
5581
unsafe { mem::transmute(a) }
5682
}
83+
fn from_parts(sign: bool, exponent: Self::Int, significand: Self::Int) -> Self {
84+
Self::from_repr(((sign as Self::Int) << (Self::bits() - 1)) |
85+
((exponent << Self::significand_bits()) & Self::exponent_mask()) |
86+
(significand & Self::significand_mask()))
87+
}
5788
fn normalize(significand: Self::Int) -> (i32, Self::Int) {
5889
let shift = significand.leading_zeros()
5990
.wrapping_sub((1u32 << Self::significand_bits()).leading_zeros());
@@ -68,6 +99,15 @@ impl Float for f64 {
6899
fn significand_bits() -> u32 {
69100
52
70101
}
102+
fn sign_mask() -> Self::Int {
103+
1 << (Self::bits() - 1)
104+
}
105+
fn significand_mask() -> Self::Int {
106+
(1 << Self::significand_bits()) - 1
107+
}
108+
fn exponent_mask() -> Self::Int {
109+
!(Self::sign_mask() | Self::significand_mask())
110+
}
71111
fn repr(self) -> Self::Int {
72112
unsafe { mem::transmute(self) }
73113
}
@@ -82,36 +122,14 @@ impl Float for f64 {
82122
fn from_repr(a: Self::Int) -> Self {
83123
unsafe { mem::transmute(a) }
84124
}
125+
fn from_parts(sign: bool, exponent: Self::Int, significand: Self::Int) -> Self {
126+
Self::from_repr(((sign as Self::Int) << (Self::bits() - 1)) |
127+
((exponent << Self::significand_bits()) & Self::exponent_mask()) |
128+
(significand & Self::significand_mask()))
129+
}
85130
fn normalize(significand: Self::Int) -> (i32, Self::Int) {
86131
let shift = significand.leading_zeros()
87132
.wrapping_sub((1u64 << Self::significand_bits()).leading_zeros());
88133
(1i32.wrapping_sub(shift as i32), significand << shift as Self::Int)
89134
}
90135
}
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
}

src/qc.rs

+60-3
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,12 @@
55

66
use std::boxed::Box;
77
use std::fmt;
8+
use core::{f32, f64};
89

910
use quickcheck::{Arbitrary, Gen};
1011

1112
use int::LargeInt;
13+
use float::Float;
1214

1315
// Generates values in the full range of the integer type
1416
macro_rules! arbitrary {
@@ -143,6 +145,61 @@ macro_rules! arbitrary_large {
143145
arbitrary_large!(I64: i64);
144146
arbitrary_large!(U64: u64);
145147

148+
macro_rules! arbitrary_float {
149+
($TY:ident : $ty:ident) => {
150+
#[derive(Clone, Copy)]
151+
pub struct $TY(pub $ty);
152+
153+
impl Arbitrary for $TY {
154+
fn arbitrary<G>(g: &mut G) -> $TY
155+
where G: Gen
156+
{
157+
let special = [
158+
-0.0, 0.0, $ty::NAN, $ty::INFINITY, -$ty::INFINITY
159+
];
160+
161+
if g.gen_weighted_bool(10) { // Random special case
162+
$TY(*g.choose(&special).unwrap())
163+
} else if g.gen_weighted_bool(10) { // NaN variants
164+
let sign: bool = g.gen();
165+
let exponent: <$ty as Float>::Int = g.gen();
166+
let significand: <$ty as Float>::Int = 0;
167+
$TY($ty::from_parts(sign, exponent, significand))
168+
} else if g.gen() { // Denormalized
169+
let sign: bool = g.gen();
170+
let exponent: <$ty as Float>::Int = 0;
171+
let significand: <$ty as Float>::Int = g.gen();
172+
$TY($ty::from_parts(sign, exponent, significand))
173+
} else { // Random anything
174+
let sign: bool = g.gen();
175+
let exponent: <$ty as Float>::Int = g.gen();
176+
let significand: <$ty as Float>::Int = g.gen();
177+
$TY($ty::from_parts(sign, exponent, significand))
178+
}
179+
}
180+
181+
fn shrink(&self) -> Box<Iterator<Item=$TY>> {
182+
::quickcheck::empty_shrinker()
183+
}
184+
}
185+
186+
impl fmt::Debug for $TY {
187+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
188+
fmt::Debug::fmt(&self.0, f)
189+
}
190+
}
191+
192+
impl PartialEq for $TY {
193+
fn eq(&self, other: &$TY) -> bool {
194+
self.0.eq_repr(other.0)
195+
}
196+
}
197+
}
198+
}
199+
200+
arbitrary_float!(F32: f32);
201+
arbitrary_float!(F64: f64);
202+
146203
// Convenience macro to test intrinsics against their reference implementations.
147204
//
148205
// Each intrinsic is tested against both the `gcc_s` library as well as
@@ -224,9 +281,9 @@ macro_rules! check {
224281
print!("{} - Args: ", stringify!($name));
225282
$(print!("{:?} ", $arg);)*
226283
print!("\n");
227-
println!(" rustc-builtins: {:?}", my_answer);
228-
println!(" compiler_rt: {:?}", compiler_rt_answer);
229-
println!(" gcc_s: {:?}", gcc_s_answer);
284+
println!(" compiler-builtins: {:?}", my_answer);
285+
println!(" compiler_rt: {:?}", compiler_rt_answer);
286+
println!(" gcc_s: {:?}", gcc_s_answer);
230287
};
231288

232289
if my_answer != compiler_rt_answer {

0 commit comments

Comments
 (0)