1 use core
::num
::dec2flt
::rawfp
::RawFloat
;
2 use core
::num
::dec2flt
::rawfp
::{fp_to_float, next_float, prev_float, round_normal}
;
3 use core
::num
::diy_float
::Fp
;
5 fn integer_decode(f
: f64) -> (u64, i16, i8) {
6 RawFloat
::integer_decode(f
)
10 fn fp_to_float_half_to_even() {
11 fn is_normalized(sig
: u64) -> bool
{
12 // intentionally written without {min,max}_sig() as a sanity check
13 sig
>> 52 == 1 && sig
>> 53 == 0
16 fn conv(sig
: u64) -> u64 {
17 // The significands are perfectly in range, so the exponent should not matter
18 let (m1
, e1
, _
) = integer_decode(fp_to_float
::<f64>(Fp { f: sig, e: 0 }
));
19 assert_eq
!(e1
, 0 + 64 - 53);
20 let (m2
, e2
, _
) = integer_decode(fp_to_float
::<f64>(Fp { f: sig, e: 55 }
));
21 assert_eq
!(e2
, 55 + 64 - 53);
23 let (m3
, e3
, _
) = integer_decode(fp_to_float
::<f64>(Fp { f: sig, e: -78 }
));
24 assert_eq
!(e3
, -78 + 64 - 53);
29 let odd
= 0x1F_EDCB_A012_345F;
31 assert
!(is_normalized(odd
));
32 assert
!(is_normalized(even
));
33 assert_eq
!(conv(odd
<< 11), odd
);
34 assert_eq
!(conv(even
<< 11), even
);
35 assert_eq
!(conv(odd
<< 11 | 1 << 10), odd
+ 1);
36 assert_eq
!(conv(even
<< 11 | 1 << 10), even
);
37 assert_eq
!(conv(even
<< 11 | 1 << 10 | 1), even
+ 1);
38 assert_eq
!(conv(odd
<< 11 | 1 << 9), odd
);
39 assert_eq
!(conv(even
<< 11 | 1 << 9), even
);
40 assert_eq
!(conv(odd
<< 11 | 0x7FF), odd
+ 1);
41 assert_eq
!(conv(even
<< 11 | 0x7FF), even
+ 1);
42 assert_eq
!(conv(odd
<< 11 | 0x3FF), odd
);
43 assert_eq
!(conv(even
<< 11 | 0x3FF), even
);
47 fn integers_to_f64() {
48 assert_eq
!(fp_to_float
::<f64>(Fp { f: 1, e: 0 }
), 1.0);
49 assert_eq
!(fp_to_float
::<f64>(Fp { f: 42, e: 7 }
), (42 << 7) as f64);
50 assert_eq
!(fp_to_float
::<f64>(Fp { f: 1 << 20, e: 30 }
), (1u64 << 50) as f64);
51 assert_eq
!(fp_to_float
::<f64>(Fp { f: 4, e: -3 }
), 0.5);
54 const SOME_FLOATS
: [f64; 9] = [
67 fn human_f64_roundtrip() {
68 for &x
in &SOME_FLOATS
{
69 let (f
, e
, _
) = integer_decode(x
);
70 let fp
= Fp { f: f, e: e }
;
71 assert_eq
!(fp_to_float
::<f64>(fp
), x
);
76 fn rounding_overflow() {
77 let x
= Fp { f: 0xFF_FF_FF_FF_FF_FF_FF_00u64, e: 42 }
;
78 let rounded
= round_normal
::<f64>(x
);
79 let adjusted_k
= x
.e
+ 64 - 53;
80 assert_eq
!(rounded
.sig
, 1 << 52);
81 assert_eq
!(rounded
.k
, adjusted_k
+ 1);
85 fn prev_float_monotonic() {
88 let x1
= prev_float(x
);
90 assert
!(x
- x1
< 1e
-15);
95 const MIN_SUBNORMAL
: f64 = 5e
-324;
98 fn next_float_zero() {
99 let tiny
= next_float(0.0);
100 assert_eq
!(tiny
, MIN_SUBNORMAL
);
101 assert
!(tiny
!= 0.0);
105 fn next_float_subnormal() {
106 let second
= next_float(MIN_SUBNORMAL
);
107 // For subnormals, MIN_SUBNORMAL is the ULP
108 assert
!(second
!= MIN_SUBNORMAL
);
109 assert
!(second
> 0.0);
110 assert_eq
!(second
- MIN_SUBNORMAL
, MIN_SUBNORMAL
);
114 fn next_float_inf() {
115 assert_eq
!(next_float(f64::MAX
), f64::INFINITY
);
116 assert_eq
!(next_float(f64::INFINITY
), f64::INFINITY
);
120 fn next_prev_identity() {
121 for &x
in &SOME_FLOATS
{
122 assert_eq
!(prev_float(next_float(x
)), x
);
123 assert_eq
!(prev_float(prev_float(next_float(next_float(x
)))), x
);
124 assert_eq
!(next_float(prev_float(x
)), x
);
125 assert_eq
!(next_float(next_float(prev_float(prev_float(x
)))), x
);
130 fn next_float_monotonic() {
131 let mut x
= 0.49999999999999;
134 let x1
= next_float(x
);
136 assert
!(x1
- x
< 1e
-15, "next_float_monotonic: delta = {:?}", x1
- x
);
143 fn test_f32_integer_decode() {
144 assert_eq
!(3.14159265359f32.integer_decode(), (13176795, -22, 1));
145 assert_eq
!((-8573.5918555f32).integer_decode(), (8779358, -10, -1));
146 assert_eq
!(2f32.powf(100.0).integer_decode(), (8388608, 77, 1));
147 assert_eq
!(0f32.integer_decode(), (0, -150, 1));
148 assert_eq
!((-0f32).integer_decode(), (0, -150, -1));
149 assert_eq
!(f32::INFINITY
.integer_decode(), (8388608, 105, 1));
150 assert_eq
!(f32::NEG_INFINITY
.integer_decode(), (8388608, 105, -1));
152 // Ignore the "sign" (quiet / signalling flag) of NAN.
153 // It can vary between runtime operations and LLVM folding.
154 let (nan_m
, nan_e
, _nan_s
) = f32::NAN
.integer_decode();
155 assert_eq
!((nan_m
, nan_e
), (12582912, 105));
159 fn test_f64_integer_decode() {
160 assert_eq
!(3.14159265359f64.integer_decode(), (7074237752028906, -51, 1));
161 assert_eq
!((-8573.5918555f64).integer_decode(), (4713381968463931, -39, -1));
162 assert_eq
!(2f64.powf(100.0).integer_decode(), (4503599627370496, 48, 1));
163 assert_eq
!(0f64.integer_decode(), (0, -1075, 1));
164 assert_eq
!((-0f64).integer_decode(), (0, -1075, -1));
165 assert_eq
!(f64::INFINITY
.integer_decode(), (4503599627370496, 972, 1));
166 assert_eq
!(f64::NEG_INFINITY
.integer_decode(), (4503599627370496, 972, -1));
168 // Ignore the "sign" (quiet / signalling flag) of NAN.
169 // It can vary between runtime operations and LLVM folding.
170 let (nan_m
, nan_e
, _nan_s
) = f64::NAN
.integer_decode();
171 assert_eq
!((nan_m
, nan_e
), (6755399441055744, 972));