| ... | ... | @@ -5,6 +5,10 @@ const assert = std.debug.assert; |
| 5 | 5 | pub fn isInf(x: var) bool { |
| 6 | 6 | const T = @typeOf(x); |
| 7 | 7 | switch (T) { |
| 8 | f16 => { |
| 9 | const bits = @bitCast(u16, x); |
| 10 | return bits & 0x7FFF == 0x7C00; |
| 11 | }, |
| 8 | 12 | f32 => { |
| 9 | 13 | const bits = @bitCast(u32, x); |
| 10 | 14 | return bits & 0x7FFFFFFF == 0x7F800000; |
| ... | ... | @@ -22,6 +26,9 @@ pub fn isInf(x: var) bool { |
| 22 | 26 | pub fn isPositiveInf(x: var) bool { |
| 23 | 27 | const T = @typeOf(x); |
| 24 | 28 | switch (T) { |
| 29 | f16 => { |
| 30 | return @bitCast(u16, x) == 0x7C00; |
| 31 | }, |
| 25 | 32 | f32 => { |
| 26 | 33 | return @bitCast(u32, x) == 0x7F800000; |
| 27 | 34 | }, |
| ... | ... | @@ -37,6 +44,9 @@ pub fn isPositiveInf(x: var) bool { |
| 37 | 44 | pub fn isNegativeInf(x: var) bool { |
| 38 | 45 | const T = @typeOf(x); |
| 39 | 46 | switch (T) { |
| 47 | f16 => { |
| 48 | return @bitCast(u16, x) == 0xFC00; |
| 49 | }, |
| 40 | 50 | f32 => { |
| 41 | 51 | return @bitCast(u32, x) == 0xFF800000; |
| 42 | 52 | }, |
| ... | ... | @@ -50,10 +60,14 @@ pub fn isNegativeInf(x: var) bool { |
| 50 | 60 | } |
| 51 | 61 | |
| 52 | 62 | test "math.isInf" { |
| 63 | assert(!isInf(f16(0.0))); |
| 64 | assert(!isInf(f16(-0.0))); |
| 53 | 65 | assert(!isInf(f32(0.0))); |
| 54 | 66 | assert(!isInf(f32(-0.0))); |
| 55 | 67 | assert(!isInf(f64(0.0))); |
| 56 | 68 | assert(!isInf(f64(-0.0))); |
| 69 | assert(isInf(math.inf(f16))); |
| 70 | assert(isInf(-math.inf(f16))); |
| 57 | 71 | assert(isInf(math.inf(f32))); |
| 58 | 72 | assert(isInf(-math.inf(f32))); |
| 59 | 73 | assert(isInf(math.inf(f64))); |
| ... | ... | @@ -61,10 +75,14 @@ test "math.isInf" { |
| 61 | 75 | } |
| 62 | 76 | |
| 63 | 77 | test "math.isPositiveInf" { |
| 78 | assert(!isPositiveInf(f16(0.0))); |
| 79 | assert(!isPositiveInf(f16(-0.0))); |
| 64 | 80 | assert(!isPositiveInf(f32(0.0))); |
| 65 | 81 | assert(!isPositiveInf(f32(-0.0))); |
| 66 | 82 | assert(!isPositiveInf(f64(0.0))); |
| 67 | 83 | assert(!isPositiveInf(f64(-0.0))); |
| 84 | assert(isPositiveInf(math.inf(f16))); |
| 85 | assert(!isPositiveInf(-math.inf(f16))); |
| 68 | 86 | assert(isPositiveInf(math.inf(f32))); |
| 69 | 87 | assert(!isPositiveInf(-math.inf(f32))); |
| 70 | 88 | assert(isPositiveInf(math.inf(f64))); |
| ... | ... | @@ -72,10 +90,14 @@ test "math.isPositiveInf" { |
| 72 | 90 | } |
| 73 | 91 | |
| 74 | 92 | test "math.isNegativeInf" { |
| 93 | assert(!isNegativeInf(f16(0.0))); |
| 94 | assert(!isNegativeInf(f16(-0.0))); |
| 75 | 95 | assert(!isNegativeInf(f32(0.0))); |
| 76 | 96 | assert(!isNegativeInf(f32(-0.0))); |
| 77 | 97 | assert(!isNegativeInf(f64(0.0))); |
| 78 | 98 | assert(!isNegativeInf(f64(-0.0))); |
| 99 | assert(!isNegativeInf(math.inf(f16))); |
| 100 | assert(isNegativeInf(-math.inf(f16))); |
| 79 | 101 | assert(!isNegativeInf(math.inf(f32))); |
| 80 | 102 | assert(isNegativeInf(-math.inf(f32))); |
| 81 | 103 | assert(!isNegativeInf(math.inf(f64))); |