|
| 1 | +#![allow(dead_code)] // FIXME(f16_f128): remove once constants are used |
| 2 | + |
| 3 | +// We run out of precision pretty quickly with f16 |
| 4 | +const F16_APPROX_L1: f16 = 0.001; |
| 5 | +const F16_APPROX_L2: f16 = 0.01; |
| 6 | +const F16_APPROX_L3: f16 = 0.1; |
| 7 | +const F16_APPROX_L4: f16 = 0.5; |
| 8 | + |
| 9 | +/// Smallest number |
| 10 | +const TINY_BITS: u16 = 0x1; |
| 11 | +/// Next smallest number |
| 12 | +const TINY_UP_BITS: u16 = 0x2; |
| 13 | +/// Exponent = 0b11...10, Sifnificand 0b1111..10. Min val > 0 |
| 14 | +const MAX_DOWN_BITS: u16 = 0x7bfe; |
| 15 | +/// Zeroed exponent, full significant |
| 16 | +const LARGEST_SUBNORMAL_BITS: u16 = 0x03ff; |
| 17 | +/// Exponent = 0b1, zeroed significand |
| 18 | +const SMALLEST_NORMAL_BITS: u16 = 0x0400; |
| 19 | +/// First pattern over the mantissa |
| 20 | +const NAN_MASK1: u16 = 0x02aa; |
| 21 | +/// Second pattern over the mantissa |
| 22 | +const NAN_MASK2: u16 = 0x0155; |
| 23 | + |
| 24 | +/// Compare by value |
| 25 | +#[allow(unused_macros)] |
| 26 | +macro_rules! assert_f16_eq { |
| 27 | + ($a:expr, $b:expr) => { |
| 28 | + let (l, r): (&f16, &f16) = (&$a, &$b); |
| 29 | + assert_eq!(*l, *r, "\na: {:#018x}\nb: {:#018x}", l.to_bits(), r.to_bits()) |
| 30 | + }; |
| 31 | +} |
| 32 | + |
| 33 | +/// Compare by representation |
| 34 | +#[allow(unused_macros)] |
| 35 | +macro_rules! assert_f16_biteq { |
| 36 | + ($a:expr, $b:expr) => { |
| 37 | + let (l, r): (&f16, &f16) = (&$a, &$b); |
| 38 | + let lb = l.to_bits(); |
| 39 | + let rb = r.to_bits(); |
| 40 | + assert_eq!( |
| 41 | + lb, rb, |
| 42 | + "float {:?} is not bitequal to {:?}.\na: {:#018x}\nb: {:#018x}", |
| 43 | + *l, *r, lb, rb |
| 44 | + ); |
| 45 | + }; |
| 46 | +} |
0 commit comments