Skip to content

Commit 3b8dedd

Browse files
author
Jorge Aparicio
committed
Revert "Merge pull request #48 from mattico/add_float_quickcheck"
This reverts commit e34a605, reversing changes made to cab88e6.
1 parent 36a437a commit 3b8dedd

File tree

3 files changed

+118
-161
lines changed

3 files changed

+118
-161
lines changed

src/float/add.rs

+117-28
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 = 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;
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;
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::{F32, F64};
191+
use qc::{U32, U64};
192192

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

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

223225
fn __adddf3(f: extern fn(f64, f64) -> f64,
224-
a: F64,
225-
b: F64) -> Option<FRepr<f64> > {
226-
Some(FRepr(f(a.0, b.0)))
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)))
227230
}
228231
}
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+
}
229318
}

src/float/mod.rs

+1-78
Original file line numberDiff line numberDiff line change
@@ -10,45 +10,21 @@ 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-
1613
/// Returns the bitwidth of the significand
1714
fn significand_bits() -> u32;
1815

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-
3716
/// Returns `self` transmuted to `Self::Int`
3817
fn repr(self) -> Self::Int;
3918

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

4625
/// Returns a `Self::Int` transmuted back to `Self`
4726
fn from_repr(a: Self::Int) -> Self;
4827

49-
/// Constructs a `Self` from its parts
50-
fn from_parts(sign: bool, exponent: Self::Int, significand: Self::Int) -> Self;
51-
5228
/// Returns (normalized exponent, normalized significand)
5329
fn normalize(significand: Self::Int) -> (i32, Self::Int);
5430
}
@@ -58,21 +34,9 @@ impl Float for f32 {
5834
fn bits() -> u32 {
5935
32
6036
}
61-
fn exponent_bits() -> u32 {
62-
8
63-
}
6437
fn significand_bits() -> u32 {
6538
23
6639
}
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-
}
7640
fn repr(self) -> Self::Int {
7741
unsafe { mem::transmute(self) }
7842
}
@@ -87,21 +51,6 @@ impl Float for f32 {
8751
fn from_repr(a: Self::Int) -> Self {
8852
unsafe { mem::transmute(a) }
8953
}
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-
}
10554
fn normalize(significand: Self::Int) -> (i32, Self::Int) {
10655
let shift = significand.leading_zeros()
10756
.wrapping_sub((1u32 << Self::significand_bits()).leading_zeros());
@@ -113,21 +62,9 @@ impl Float for f64 {
11362
fn bits() -> u32 {
11463
64
11564
}
116-
fn exponent_bits() -> u32 {
117-
11
118-
}
11965
fn significand_bits() -> u32 {
12066
52
12167
}
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-
}
13168
fn repr(self) -> Self::Int {
13269
unsafe { mem::transmute(self) }
13370
}
@@ -142,20 +79,6 @@ impl Float for f64 {
14279
fn from_repr(a: Self::Int) -> Self {
14380
unsafe { mem::transmute(a) }
14481
}
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-
}
15982
fn normalize(significand: Self::Int) -> (i32, Self::Int) {
16083
let shift = significand.leading_zeros()
16184
.wrapping_sub((1u64 << Self::significand_bits()).leading_zeros());

src/qc.rs

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

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

109
use quickcheck::{Arbitrary, Gen};
1110

1211
use int::LargeInt;
13-
use float::Float;
1412

1513
// Generates values in the full range of the integer type
1614
macro_rules! arbitrary {
@@ -73,7 +71,6 @@ macro_rules! arbitrary {
7371
arbitrary!(I32: i32);
7472
arbitrary!(U32: u32);
7573

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

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-
200146
// Convenience macro to test intrinsics against their reference implementations.
201147
//
202148
// Each intrinsic is tested against both the `gcc_s` library as well as
@@ -317,4 +263,3 @@ macro_rules! check {
317263
}
318264
)
319265
}
320-

0 commit comments

Comments
 (0)