| ... | ... | @@ -1,57 +1,54 @@ |
| 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 | | // 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 | 6 | pub fn isNormal(x: anytype) bool { |
| 8 | 7 | const T = @TypeOf(x); |
| 9 | | switch (T) { |
| 10 | | f16 => { |
| 11 | | const bits = @bitCast(u16, x); |
| 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 | | }, |
| 8 | const TBits = std.meta.Int(.unsigned, @bitSizeOf(T)); |
| 9 | if (@typeInfo(T) != .Float) { |
| 10 | @compileError("isNormal not implemented for " ++ @typeName(T)); |
| 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 | 25 | test "math.isNormal" { |
| 33 | | try expect(!isNormal(math.nan(f16))); |
| 34 | | try expect(!isNormal(math.nan(f32))); |
| 35 | | try expect(!isNormal(math.nan(f64))); |
| 36 | | try expect(!isNormal(math.nan(f128))); |
| 37 | | try expect(!isNormal(-math.nan(f16))); |
| 38 | | try expect(!isNormal(-math.nan(f32))); |
| 39 | | try expect(!isNormal(-math.nan(f64))); |
| 40 | | try expect(!isNormal(-math.nan(f128))); |
| 41 | | try expect(!isNormal(math.inf(f16))); |
| 42 | | try expect(!isNormal(math.inf(f32))); |
| 43 | | try expect(!isNormal(math.inf(f64))); |
| 44 | | try expect(!isNormal(math.inf(f128))); |
| 45 | | try expect(!isNormal(-math.inf(f16))); |
| 46 | | try expect(!isNormal(-math.inf(f32))); |
| 47 | | try expect(!isNormal(-math.inf(f64))); |
| 48 | | try expect(!isNormal(-math.inf(f128))); |
| 49 | | try expect(!isNormal(@as(f16, 0))); |
| 50 | | try expect(!isNormal(@as(f32, 0))); |
| 51 | | try expect(!isNormal(@as(f64, 0))); |
| 52 | | try expect(!isNormal(@as(f128, 0))); |
| 53 | | try expect(isNormal(@as(f16, 1.0))); |
| 54 | | try expect(isNormal(@as(f32, 1.0))); |
| 55 | | try expect(isNormal(@as(f64, 1.0))); |
| 56 | | try expect(isNormal(@as(f128, 1.0))); |
| 26 | // TODO remove when #11391 is resolved |
| 27 | if (@import("builtin").os.tag == .freebsd) return error.SkipZigTest; |
| 28 | |
| 29 | // TODO add `c_longdouble' when math.inf(T) supports it |
| 30 | inline for ([_]type{ f16, f32, f64, f80, f128 }) |T| { |
| 31 | const TBits = std.meta.Int(.unsigned, @bitSizeOf(T)); |
| 32 | |
| 33 | // normals |
| 34 | try expect(isNormal(@as(T, 1.0))); |
| 35 | try expect(isNormal(math.floatMin(T))); |
| 36 | try expect(isNormal(math.floatMax(T))); |
| 37 | |
| 38 | // subnormals |
| 39 | try expect(!isNormal(@as(T, -0.0))); |
| 40 | try expect(!isNormal(@as(T, 0.0))); |
| 41 | try expect(!isNormal(@as(T, math.floatTrueMin(T)))); |
| 42 | |
| 43 | // largest subnormal |
| 44 | try expect(!isNormal(@bitCast(T, ~(~@as(TBits, 0) << math.floatMantissaDigits(T) - 1)))); |
| 45 | |
| 46 | // non-finite numbers |
| 47 | try expect(!isNormal(-math.inf(T))); |
| 48 | try expect(!isNormal(math.inf(T))); |
| 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 | } |