| ... | @@ -1,57 +1,54 @@ | ... | @@ -1,57 +1,54 @@ |
| 1 | const std = @import("../std.zig"); | 1 | const std = @import("../std.zig"); |
| 2 | const math = std.math; | 2 | const math = std.math; |
| 3 | const expect = std.testing.expect; | 3 | const expect = std.testing.expect; |
| 4 | const maxInt = std.math.maxInt; | | |
| 5 | | 4 | |
| 6 | // Returns whether x has a normalized representation (i.e. integer part of mantissa is 1). | 5 | /// Returns whether x is neither zero, subnormal, infinity, or NaN. |
| 7 | pub fn isNormal(x: anytype) bool { | 6 | pub fn isNormal(x: anytype) bool { |
| 8 | const T = @TypeOf(x); | 7 | const T = @TypeOf(x); |
| 9 | switch (T) { | 8 | const TBits = std.meta.Int(.unsigned, @bitSizeOf(T)); |
| 10 | f16 => { | 9 | if (@typeInfo(T) != .Float) { |
| 11 | const bits = @bitCast(u16, x); | 10 | @compileError("isNormal not implemented for " ++ @typeName(T)); |
| 12 | return (bits +% (1 << 10)) & (maxInt(u16) >> 1) >= (1 << 11); | | |
| 13 | }, | | |
| 14 | f32 => { | | |
| 15 | const bits = @bitCast(u32, x); | | |
| 16 | return (bits +% (1 << 23)) & (maxInt(u32) >> 1) >= (1 << 24); | | |
| 17 | }, | | |
| 18 | f64 => { | | |
| 19 | const bits = @bitCast(u64, x); | | |
| 20 | return (bits +% (1 << 52)) & (maxInt(u64) >> 1) >= (1 << 53); | | |
| 21 | }, | | |
| 22 | f128 => { | | |
| 23 | const bits = @bitCast(u128, x); | | |
| 24 | return (bits +% (1 << 112)) & (maxInt(u128) >> 1) >= (1 << 113); | | |
| 25 | }, | | |
| 26 | else => { | | |
| 27 | @compileError("isNormal not implemented for " ++ @typeName(T)); | | |
| 28 | }, | | |
| 29 | } | 11 | } |
| | 12 | |
| | 13 | const increment_exp = 1 << math.floatMantissaBits(T); |
| | 14 | const remove_sign = ~@as(TBits, 0) >> 1; |
| | 15 | |
| | 16 | // We add 1 to the exponent, and if it overflows to 0 or becomes 1, |
| | 17 | // then it was all zeroes (subnormal) or all ones (special, inf/nan). |
| | 18 | // The sign bit is removed because all ones would overflow into it. |
| | 19 | // For f80, even though it has an explicit integer part stored, |
| | 20 | // the exponent effectively takes priority if mismatching. |
| | 21 | const value = @bitCast(TBits, x) +% increment_exp; |
| | 22 | return value & remove_sign >= (increment_exp << 1); |
| 30 | } | 23 | } |
| 31 | | 24 | |
| 32 | test "math.isNormal" { | 25 | test "math.isNormal" { |
| 33 | try expect(!isNormal(math.nan(f16))); | 26 | // TODO remove when #11391 is resolved |
| 34 | try expect(!isNormal(math.nan(f32))); | 27 | if (@import("builtin").os.tag == .freebsd) return error.SkipZigTest; |
| 35 | try expect(!isNormal(math.nan(f64))); | 28 | |
| 36 | try expect(!isNormal(math.nan(f128))); | 29 | // TODO add `c_longdouble' when math.inf(T) supports it |
| 37 | try expect(!isNormal(-math.nan(f16))); | 30 | inline for ([_]type{ f16, f32, f64, f80, f128 }) |T| { |
| 38 | try expect(!isNormal(-math.nan(f32))); | 31 | const TBits = std.meta.Int(.unsigned, @bitSizeOf(T)); |
| 39 | try expect(!isNormal(-math.nan(f64))); | 32 | |
| 40 | try expect(!isNormal(-math.nan(f128))); | 33 | // normals |
| 41 | try expect(!isNormal(math.inf(f16))); | 34 | try expect(isNormal(@as(T, 1.0))); |
| 42 | try expect(!isNormal(math.inf(f32))); | 35 | try expect(isNormal(math.floatMin(T))); |
| 43 | try expect(!isNormal(math.inf(f64))); | 36 | try expect(isNormal(math.floatMax(T))); |
| 44 | try expect(!isNormal(math.inf(f128))); | 37 | |
| 45 | try expect(!isNormal(-math.inf(f16))); | 38 | // subnormals |
| 46 | try expect(!isNormal(-math.inf(f32))); | 39 | try expect(!isNormal(@as(T, -0.0))); |
| 47 | try expect(!isNormal(-math.inf(f64))); | 40 | try expect(!isNormal(@as(T, 0.0))); |
| 48 | try expect(!isNormal(-math.inf(f128))); | 41 | try expect(!isNormal(@as(T, math.floatTrueMin(T)))); |
| 49 | try expect(!isNormal(@as(f16, 0))); | 42 | |
| 50 | try expect(!isNormal(@as(f32, 0))); | 43 | // largest subnormal |
| 51 | try expect(!isNormal(@as(f64, 0))); | 44 | try expect(!isNormal(@bitCast(T, ~(~@as(TBits, 0) << math.floatMantissaDigits(T) - 1)))); |
| 52 | try expect(!isNormal(@as(f128, 0))); | 45 | |
| 53 | try expect(isNormal(@as(f16, 1.0))); | 46 | // non-finite numbers |
| 54 | try expect(isNormal(@as(f32, 1.0))); | 47 | try expect(!isNormal(-math.inf(T))); |
| 55 | try expect(isNormal(@as(f64, 1.0))); | 48 | try expect(!isNormal(math.inf(T))); |
| 56 | try expect(isNormal(@as(f128, 1.0))); | 49 | try expect(!isNormal(math.nan(T))); |
| | 50 | |
| | 51 | // overflow edge-case (described in implementation, also see #10133) |
| | 52 | try expect(!isNormal(@bitCast(T, ~@as(TBits, 0)))); |
| | 53 | } |
| 57 | } | 54 | } |