Skip to content

Commit e34a605

Browse files
author
Jorge Aparicio
authored
Merge pull request #48 from mattico/add_float_quickcheck
Add float quickcheck implementations (fixes #45)
2 parents cab88e6 + 010d153 commit e34a605

File tree

3 files changed

+161
-118
lines changed

3 files changed

+161
-118
lines changed

src/float/add.rs

+28-117
Original file line numberDiff line numberDiff line change
@@ -9,27 +9,27 @@ macro_rules! add {
99
#[allow(unused_parens)]
1010
#[cfg_attr(not(test), no_mangle)]
1111
pub extern fn $intrinsic(a: $ty, b: $ty) -> $ty {
12-
let one = Wrapping(1 as <$ty as Float>::Int);
13-
let zero = Wrapping(0 as <$ty as Float>::Int);
14-
15-
let bits = Wrapping(<$ty>::bits() as <$ty as Float>::Int);
16-
let significand_bits = Wrapping(<$ty>::significand_bits() as <$ty as Float>::Int);
17-
let exponent_bits = bits - significand_bits - one;
18-
let max_exponent = (one << exponent_bits.0 as usize) - one;
19-
20-
let implicit_bit = one << significand_bits.0 as usize;
21-
let significand_mask = implicit_bit - one;
22-
let sign_bit = one << (significand_bits + exponent_bits).0 as usize;
23-
let abs_mask = sign_bit - one;
24-
let exponent_mask = abs_mask ^ significand_mask;
25-
let inf_rep = exponent_mask;
26-
let quiet_bit = implicit_bit >> 1;
27-
let qnan_rep = exponent_mask | quiet_bit;
28-
29-
let mut a_rep = Wrapping(a.repr());
30-
let mut b_rep = Wrapping(b.repr());
31-
let a_abs = a_rep & abs_mask;
32-
let b_abs = b_rep & abs_mask;
12+
let one = Wrapping(1 as <$ty as Float>::Int);
13+
let zero = Wrapping(0 as <$ty as Float>::Int);
14+
15+
let bits = Wrapping(<$ty>::bits() as <$ty as Float>::Int);
16+
let significand_bits = Wrapping(<$ty>::significand_bits() as <$ty as Float>::Int);
17+
let exponent_bits = Wrapping(<$ty>::exponent_bits() as <$ty as Float>::Int);
18+
let max_exponent = (one << exponent_bits.0 as usize) - one;
19+
20+
let implicit_bit = one << significand_bits.0 as usize;
21+
let significand_mask = implicit_bit - one;
22+
let sign_bit = one << (significand_bits + exponent_bits).0 as usize;
23+
let abs_mask = sign_bit - one;
24+
let exponent_mask = abs_mask ^ significand_mask;
25+
let inf_rep = exponent_mask;
26+
let quiet_bit = implicit_bit >> 1;
27+
let qnan_rep = exponent_mask | quiet_bit;
28+
29+
let mut a_rep = Wrapping(a.repr());
30+
let mut b_rep = Wrapping(b.repr());
31+
let a_abs = a_rep & abs_mask;
32+
let b_abs = b_rep & abs_mask;
3333

3434
// Detect if a or b is zero, infinity, or NaN.
3535
if a_abs - one >= inf_rep - one ||
@@ -188,7 +188,7 @@ mod tests {
188188
use core::{f32, f64};
189189

190190
use float::Float;
191-
use qc::{U32, U64};
191+
use qc::{F32, F64};
192192

193193
// NOTE The tests below have special handing for NaN values.
194194
// Because NaN != NaN, the floating-point representations must be used
@@ -212,107 +212,18 @@ mod tests {
212212
}
213213
}
214214

215-
// TODO: Add F32/F64 to qc so that they print the right values (at the very least)
216215
check! {
217216
fn __addsf3(f: extern fn(f32, f32) -> f32,
218-
a: U32,
219-
b: U32)
217+
a: F32,
218+
b: F32)
220219
-> Option<FRepr<f32> > {
221-
let (a, b) = (f32::from_repr(a.0), f32::from_repr(b.0));
222-
Some(FRepr(f(a, b)))
220+
Some(FRepr(f(a.0, b.0)))
223221
}
224222

225223
fn __adddf3(f: extern fn(f64, f64) -> f64,
226-
a: U64,
227-
b: U64) -> Option<FRepr<f64> > {
228-
let (a, b) = (f64::from_repr(a.0), f64::from_repr(b.0));
229-
Some(FRepr(f(a, b)))
224+
a: F64,
225+
b: F64) -> Option<FRepr<f64> > {
226+
Some(FRepr(f(a.0, b.0)))
230227
}
231228
}
232-
233-
// More tests for special float values
234-
235-
#[test]
236-
fn test_float_tiny_plus_tiny() {
237-
let tiny = f32::from_repr(1);
238-
let r = super::__addsf3(tiny, tiny);
239-
assert!(r.eq_repr(tiny + tiny));
240-
}
241-
242-
#[test]
243-
fn test_double_tiny_plus_tiny() {
244-
let tiny = f64::from_repr(1);
245-
let r = super::__adddf3(tiny, tiny);
246-
assert!(r.eq_repr(tiny + tiny));
247-
}
248-
249-
#[test]
250-
fn test_float_small_plus_small() {
251-
let a = f32::from_repr(327);
252-
let b = f32::from_repr(256);
253-
let r = super::__addsf3(a, b);
254-
assert!(r.eq_repr(a + b));
255-
}
256-
257-
#[test]
258-
fn test_double_small_plus_small() {
259-
let a = f64::from_repr(327);
260-
let b = f64::from_repr(256);
261-
let r = super::__adddf3(a, b);
262-
assert!(r.eq_repr(a + b));
263-
}
264-
265-
#[test]
266-
fn test_float_one_plus_one() {
267-
let r = super::__addsf3(1f32, 1f32);
268-
assert!(r.eq_repr(1f32 + 1f32));
269-
}
270-
271-
#[test]
272-
fn test_double_one_plus_one() {
273-
let r = super::__adddf3(1f64, 1f64);
274-
assert!(r.eq_repr(1f64 + 1f64));
275-
}
276-
277-
#[test]
278-
fn test_float_different_nan() {
279-
let a = f32::from_repr(1);
280-
let b = f32::from_repr(0b11111111100100010001001010101010);
281-
let x = super::__addsf3(a, b);
282-
let y = a + b;
283-
assert!(x.eq_repr(y));
284-
}
285-
286-
#[test]
287-
fn test_double_different_nan() {
288-
let a = f64::from_repr(1);
289-
let b = f64::from_repr(0b1111111111110010001000100101010101001000101010000110100011101011);
290-
let x = super::__adddf3(a, b);
291-
let y = a + b;
292-
assert!(x.eq_repr(y));
293-
}
294-
295-
#[test]
296-
fn test_float_nan() {
297-
let r = super::__addsf3(f32::NAN, 1.23);
298-
assert_eq!(r.repr(), f32::NAN.repr());
299-
}
300-
301-
#[test]
302-
fn test_double_nan() {
303-
let r = super::__adddf3(f64::NAN, 1.23);
304-
assert_eq!(r.repr(), f64::NAN.repr());
305-
}
306-
307-
#[test]
308-
fn test_float_inf() {
309-
let r = super::__addsf3(f32::INFINITY, -123.4);
310-
assert_eq!(r, f32::INFINITY);
311-
}
312-
313-
#[test]
314-
fn test_double_inf() {
315-
let r = super::__adddf3(f64::INFINITY, -123.4);
316-
assert_eq!(r, f64::INFINITY);
317-
}
318229
}

src/float/mod.rs

+78-1
Original file line numberDiff line numberDiff line change
@@ -10,21 +10,45 @@ pub trait Float: Sized + Copy {
1010
/// Returns the bitwidth of the float type
1111
fn bits() -> u32;
1212

13+
/// Returns the bitwidth of the exponent
14+
fn exponent_bits() -> u32;
15+
1316
/// Returns the bitwidth of the significand
1417
fn significand_bits() -> u32;
1518

19+
/// Returns a mask for the sign bit of `self`
20+
fn sign_mask() -> Self::Int;
21+
22+
/// Returns a mask for the exponent portion of `self`
23+
fn exponent_mask() -> Self::Int;
24+
25+
/// Returns a mask for the significand portion of `self`
26+
fn significand_mask() -> Self::Int;
27+
28+
/// Returns the sign bit of `self`
29+
fn sign(self) -> bool;
30+
31+
/// Returns the exponent portion of `self`, shifted to the right
32+
fn exponent(self) -> Self::Int;
33+
34+
/// Returns the significand portion of `self`
35+
fn significand(self) -> Self::Int;
36+
1637
/// Returns `self` transmuted to `Self::Int`
1738
fn repr(self) -> Self::Int;
1839

1940
#[cfg(test)]
2041
/// Checks if two floats have the same bit representation. *Except* for NaNs! NaN can be
21-
/// represented in multiple different ways. This methods returns `true` if two NaNs are
42+
/// represented in multiple different ways. This method returns `true` if two NaNs are
2243
/// compared.
2344
fn eq_repr(self, rhs: Self) -> bool;
2445

2546
/// Returns a `Self::Int` transmuted back to `Self`
2647
fn from_repr(a: Self::Int) -> Self;
2748

49+
/// Constructs a `Self` from its parts
50+
fn from_parts(sign: bool, exponent: Self::Int, significand: Self::Int) -> Self;
51+
2852
/// Returns (normalized exponent, normalized significand)
2953
fn normalize(significand: Self::Int) -> (i32, Self::Int);
3054
}
@@ -34,9 +58,21 @@ impl Float for f32 {
3458
fn bits() -> u32 {
3559
32
3660
}
61+
fn exponent_bits() -> u32 {
62+
8
63+
}
3764
fn significand_bits() -> u32 {
3865
23
3966
}
67+
fn sign_mask() -> Self::Int {
68+
1 << (Self::bits() - 1)
69+
}
70+
fn exponent_mask() -> Self::Int {
71+
((1 << Self::exponent_bits()) - 1) << Self::significand_bits()
72+
}
73+
fn significand_mask() -> Self::Int {
74+
(1 << Self::significand_bits()) - 1
75+
}
4076
fn repr(self) -> Self::Int {
4177
unsafe { mem::transmute(self) }
4278
}
@@ -51,6 +87,21 @@ impl Float for f32 {
5187
fn from_repr(a: Self::Int) -> Self {
5288
unsafe { mem::transmute(a) }
5389
}
90+
91+
fn from_parts(sign: bool, exponent: Self::Int, significand: Self::Int) -> Self {
92+
Self::from_repr(((sign as Self::Int) << (Self::bits() - 1)) |
93+
exponent & Self::exponent_mask() |
94+
significand & Self::significand_mask())
95+
}
96+
fn sign(self) -> bool {
97+
(self.repr() & Self::sign_mask()) != 0
98+
}
99+
fn exponent(self) -> Self::Int {
100+
self.repr() >> Self::significand_bits() & Self::exponent_mask()
101+
}
102+
fn significand(self) -> Self::Int {
103+
self.repr() & Self::significand_mask()
104+
}
54105
fn normalize(significand: Self::Int) -> (i32, Self::Int) {
55106
let shift = significand.leading_zeros()
56107
.wrapping_sub((1u32 << Self::significand_bits()).leading_zeros());
@@ -62,9 +113,21 @@ impl Float for f64 {
62113
fn bits() -> u32 {
63114
64
64115
}
116+
fn exponent_bits() -> u32 {
117+
11
118+
}
65119
fn significand_bits() -> u32 {
66120
52
67121
}
122+
fn sign_mask() -> Self::Int {
123+
1 << (Self::bits() - 1)
124+
}
125+
fn exponent_mask() -> Self::Int {
126+
((1 << Self::exponent_bits()) - 1) << Self::significand_bits()
127+
}
128+
fn significand_mask() -> Self::Int {
129+
(1 << Self::significand_bits()) - 1
130+
}
68131
fn repr(self) -> Self::Int {
69132
unsafe { mem::transmute(self) }
70133
}
@@ -79,6 +142,20 @@ impl Float for f64 {
79142
fn from_repr(a: Self::Int) -> Self {
80143
unsafe { mem::transmute(a) }
81144
}
145+
fn from_parts(sign: bool, exponent: Self::Int, significand: Self::Int) -> Self {
146+
Self::from_repr(((sign as Self::Int) << (Self::bits() - 1)) |
147+
exponent & Self::exponent_mask() |
148+
significand & Self::significand_mask())
149+
}
150+
fn sign(self) -> bool {
151+
(self.repr() & Self::sign_mask()) != 0
152+
}
153+
fn exponent(self) -> Self::Int {
154+
self.repr() >> Self::significand_bits() & Self::exponent_mask()
155+
}
156+
fn significand(self) -> Self::Int {
157+
self.repr() & Self::significand_mask()
158+
}
82159
fn normalize(significand: Self::Int) -> (i32, Self::Int) {
83160
let shift = significand.leading_zeros()
84161
.wrapping_sub((1u64 << Self::significand_bits()).leading_zeros());

src/qc.rs

+55
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 {
@@ -71,6 +73,7 @@ macro_rules! arbitrary {
7173
arbitrary!(I32: i32);
7274
arbitrary!(U32: u32);
7375

76+
7477
// These integers are "too large". If we generate e.g. `u64` values in the full range then there's
7578
// only `1 / 2^32` chance of seeing a value smaller than `2^32` (i.e. whose higher "word" (32-bits)
7679
// is `0`)! But this is an important group of values to tests because we have special code paths for
@@ -143,6 +146,57 @@ macro_rules! arbitrary_large {
143146
arbitrary_large!(I64: i64);
144147
arbitrary_large!(U64: u64);
145148

149+
150+
macro_rules! arbitrary_float {
151+
($TY:ident : $ty:ident) => {
152+
#[derive(Clone, Copy)]
153+
pub struct $TY(pub $ty);
154+
155+
impl Arbitrary for $TY {
156+
fn arbitrary<G>(g: &mut G) -> $TY
157+
where G: Gen
158+
{
159+
let special = [
160+
-0.0, 0.0, $ty::NAN, $ty::INFINITY, -$ty::INFINITY
161+
];
162+
163+
if g.gen_weighted_bool(10) { // Random special case
164+
$TY(*g.choose(&special).unwrap())
165+
} else if g.gen_weighted_bool(10) { // NaN variants
166+
let sign: bool = g.gen();
167+
let exponent: <$ty as Float>::Int = g.gen();
168+
let significand: <$ty as Float>::Int = 0;
169+
$TY($ty::from_parts(sign, exponent, significand))
170+
} else if g.gen() { // Denormalized
171+
let sign: bool = g.gen();
172+
let exponent: <$ty as Float>::Int = 0;
173+
let significand: <$ty as Float>::Int = g.gen();
174+
$TY($ty::from_parts(sign, exponent, significand))
175+
} else { // Random anything
176+
let sign: bool = g.gen();
177+
let exponent: <$ty as Float>::Int = g.gen();
178+
let significand: <$ty as Float>::Int = g.gen();
179+
$TY($ty::from_parts(sign, exponent, significand))
180+
}
181+
}
182+
183+
fn shrink(&self) -> Box<Iterator<Item=$TY>> {
184+
::quickcheck::empty_shrinker()
185+
}
186+
}
187+
188+
impl fmt::Debug for $TY {
189+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
190+
fmt::Debug::fmt(&self.0, f)
191+
}
192+
}
193+
}
194+
}
195+
196+
arbitrary_float!(F32: f32);
197+
arbitrary_float!(F64: f64);
198+
199+
146200
// Convenience macro to test intrinsics against their reference implementations.
147201
//
148202
// Each intrinsic is tested against both the `gcc_s` library as well as
@@ -263,3 +317,4 @@ macro_rules! check {
263317
}
264318
)
265319
}
320+

0 commit comments

Comments
 (0)