| ... | ... | @@ -1,59 +1,43 @@ |
| 1 | 1 | const std = @import("../std.zig"); |
| 2 | 2 | const math = std.math; |
| 3 | 3 | const expect = std.testing.expect; |
| 4 | | const maxInt = std.math.maxInt; |
| 5 | 4 | |
| 6 | 5 | /// Returns whether x is a finite value. |
| 7 | 6 | pub fn isFinite(x: anytype) bool { |
| 8 | 7 | const T = @TypeOf(x); |
| 9 | | switch (T) { |
| 10 | | f16 => { |
| 11 | | const bits = @bitCast(u16, x); |
| 12 | | return bits & 0x7FFF < 0x7C00; |
| 13 | | }, |
| 14 | | f32 => { |
| 15 | | const bits = @bitCast(u32, x); |
| 16 | | return bits & 0x7FFFFFFF < 0x7F800000; |
| 17 | | }, |
| 18 | | f64 => { |
| 19 | | const bits = @bitCast(u64, x); |
| 20 | | return bits & (maxInt(u64) >> 1) < (0x7FF << 52); |
| 21 | | }, |
| 22 | | f128 => { |
| 23 | | const bits = @bitCast(u128, x); |
| 24 | | return bits & (maxInt(u128) >> 1) < (0x7FFF << 112); |
| 25 | | }, |
| 26 | | else => { |
| 27 | | @compileError("isFinite not implemented for " ++ @typeName(T)); |
| 28 | | }, |
| 8 | const TBits = std.meta.Int(.unsigned, @bitSizeOf(T)); |
| 9 | if (@typeInfo(T) != .Float) { |
| 10 | @compileError("isFinite not implemented for " ++ @typeName(T)); |
| 29 | 11 | } |
| 12 | const exponent_bits = math.floatExponentBits(T); |
| 13 | const mantissa_bits = math.floatMantissaBits(T); |
| 14 | const all1s_exponent = ((1 << exponent_bits) - 1) << mantissa_bits; |
| 15 | const remove_sign = ~@as(TBits, 0) >> 1; |
| 16 | return @bitCast(TBits, x) & remove_sign < all1s_exponent; |
| 30 | 17 | } |
| 31 | 18 | |
| 32 | 19 | test "math.isFinite" { |
| 33 | | try expect(isFinite(@as(f16, 0.0))); |
| 34 | | try expect(isFinite(@as(f16, -0.0))); |
| 35 | | try expect(isFinite(@as(f32, 0.0))); |
| 36 | | try expect(isFinite(@as(f32, -0.0))); |
| 37 | | try expect(isFinite(@as(f64, 0.0))); |
| 38 | | try expect(isFinite(@as(f64, -0.0))); |
| 39 | | try expect(isFinite(@as(f128, 0.0))); |
| 40 | | try expect(isFinite(@as(f128, -0.0))); |
| 20 | // TODO remove when #11391 is resolved |
| 21 | if (@import("builtin").os.tag == .freebsd) return error.SkipZigTest; |
| 41 | 22 | |
| 42 | | try expect(!isFinite(math.inf(f16))); |
| 43 | | try expect(!isFinite(-math.inf(f16))); |
| 44 | | try expect(!isFinite(math.inf(f32))); |
| 45 | | try expect(!isFinite(-math.inf(f32))); |
| 46 | | try expect(!isFinite(math.inf(f64))); |
| 47 | | try expect(!isFinite(-math.inf(f64))); |
| 48 | | try expect(!isFinite(math.inf(f128))); |
| 49 | | try expect(!isFinite(-math.inf(f128))); |
| 23 | inline for ([_]type{ f16, f32, f64, f80, f128 }) |T| { |
| 24 | // normals |
| 25 | try expect(isFinite(@as(T, 1.0))); |
| 26 | try expect(isFinite(-@as(T, 1.0))); |
| 50 | 27 | |
| 51 | | try expect(!isFinite(math.nan(f16))); |
| 52 | | try expect(!isFinite(-math.nan(f16))); |
| 53 | | try expect(!isFinite(math.nan(f32))); |
| 54 | | try expect(!isFinite(-math.nan(f32))); |
| 55 | | try expect(!isFinite(math.nan(f64))); |
| 56 | | try expect(!isFinite(-math.nan(f64))); |
| 57 | | try expect(!isFinite(math.nan(f128))); |
| 58 | | try expect(!isFinite(-math.nan(f128))); |
| 28 | // zero & subnormals |
| 29 | try expect(isFinite(@as(T, 0.0))); |
| 30 | try expect(isFinite(@as(T, -0.0))); |
| 31 | try expect(isFinite(math.floatTrueMin(T))); |
| 32 | |
| 33 | // other float limits |
| 34 | try expect(isFinite(math.floatMin(T))); |
| 35 | try expect(isFinite(math.floatMax(T))); |
| 36 | |
| 37 | // inf & nan |
| 38 | try expect(!isFinite(math.inf(T))); |
| 39 | try expect(!isFinite(-math.inf(T))); |
| 40 | try expect(!isFinite(math.nan(T))); |
| 41 | try expect(!isFinite(-math.nan(T))); |
| 42 | } |
| 59 | 43 | } |