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