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