| ... | ... | @@ -5,6 +5,10 @@ const assert = std.debug.assert; |
| 5 | 5 | pub fn isNormal(x: var) bool { |
| 6 | 6 | const T = @typeOf(x); |
| 7 | 7 | switch (T) { |
| 8 | f16 => { |
| 9 | const bits = @bitCast(u16, x); |
| 10 | return (bits + 1024) & 0x7FFF >= 2048; |
| 11 | }, |
| 8 | 12 | f32 => { |
| 9 | 13 | const bits = @bitCast(u32, x); |
| 10 | 14 | return (bits + 0x00800000) & 0x7FFFFFFF >= 0x01000000; |
| ... | ... | @@ -20,8 +24,13 @@ pub fn isNormal(x: var) bool { |
| 20 | 24 | } |
| 21 | 25 | |
| 22 | 26 | test "math.isNormal" { |
| 27 | assert(!isNormal(math.nan(f16))); |
| 23 | 28 | assert(!isNormal(math.nan(f32))); |
| 24 | 29 | assert(!isNormal(math.nan(f64))); |
| 30 | assert(!isNormal(f16(0))); |
| 31 | assert(!isNormal(f32(0))); |
| 32 | assert(!isNormal(f64(0))); |
| 33 | assert(isNormal(f16(1.0))); |
| 25 | 34 | assert(isNormal(f32(1.0))); |
| 26 | 35 | assert(isNormal(f64(1.0))); |
| 27 | 36 | } |