diff options
| author | Andrew Kelley <andrew@ziglang.org> | 2022-04-13 01:08:33 -0400 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2022-04-13 01:08:33 -0400 |
| commit | 8ada446b1f69842793321978b4ae796a550d99a9 (patch) | |
| tree | 410426d150157802292d393604b1efd0d479e6e7 /lib/std/math | |
| parent | 7972bc8aa8edb7bd213bd5ccdc90d70fb730efd2 (diff) | |
| parent | 319555a6690a43de143698f13d6d107a7873dce0 (diff) | |
| download | zig-8ada446b1f69842793321978b4ae796a550d99a9.tar.gz zig-8ada446b1f69842793321978b4ae796a550d99a9.zip | |
Merge pull request #11407 from topolarity/float-fix
compiler_rt: Add `f80` support to `floatXiYf`/`fixXfYi`
Diffstat (limited to 'lib/std/math')
| -rw-r--r-- | lib/std/math/float.zig | 24 | ||||
| -rw-r--r-- | lib/std/math/isnormal.zig | 2 |
2 files changed, 13 insertions, 13 deletions
diff --git a/lib/std/math/float.zig b/lib/std/math/float.zig index 6d9c17d2a2..30e2268908 100644 --- a/lib/std/math/float.zig +++ b/lib/std/math/float.zig @@ -4,7 +4,7 @@ const expect = std.testing.expect; /// Creates a raw "1.0" mantissa for floating point type T. Used to dedupe f80 logic. fn mantissaOne(comptime T: type) comptime_int { - return if (floatMantissaDigits(T) == 64) 1 << 63 else 0; + return if (T == f80) 1 << floatFractionalBits(T) else 0; } /// Creates floating point type T from an unbiased exponent and raw mantissa. @@ -42,19 +42,19 @@ pub fn floatMantissaBits(comptime T: type) comptime_int { }; } -/// Returns the number of binary digits in the mantissa of floating point type T. -pub fn floatMantissaDigits(comptime T: type) comptime_int { +/// Returns the number of fractional bits in the mantissa of floating point type T. +pub fn floatFractionalBits(comptime T: type) comptime_int { assert(@typeInfo(T) == .Float); // standard IEEE floats have an implicit 0.m or 1.m integer part // f80 is special and has an explicitly stored bit in the MSB - // this function corresponds to `MANT_DIG' constants from C + // this function corresponds to `MANT_DIG - 1' from C return switch (@typeInfo(T).Float.bits) { - 16 => 11, - 32 => 24, - 64 => 53, - 80 => 64, - 128 => 113, + 16 => 10, + 32 => 23, + 64 => 52, + 80 => 63, + 128 => 112, else => @compileError("unknown floating point type " ++ @typeName(T)), }; } @@ -89,7 +89,7 @@ pub fn floatMax(comptime T: type) T { /// Returns the machine epsilon of floating point type T. pub fn floatEps(comptime T: type) T { - return reconstructFloat(T, -(floatMantissaDigits(T) - 1), mantissaOne(T)); + return reconstructFloat(T, -floatFractionalBits(T), mantissaOne(T)); } /// Returns the value inf for floating point type T. @@ -104,7 +104,7 @@ test "std.math.float" { try expect(@bitSizeOf(T) == size); // for machine epsilon, assert expmin <= -prec <= expmax - try expect(floatExponentMin(T) <= -(floatMantissaDigits(T) - 1)); - try expect(-(floatMantissaDigits(T) - 1) <= floatExponentMax(T)); + try expect(floatExponentMin(T) <= -floatFractionalBits(T)); + try expect(-floatFractionalBits(T) <= floatExponentMax(T)); } } diff --git a/lib/std/math/isnormal.zig b/lib/std/math/isnormal.zig index 7376b86eb9..42b2e1c188 100644 --- a/lib/std/math/isnormal.zig +++ b/lib/std/math/isnormal.zig @@ -41,7 +41,7 @@ test "math.isNormal" { try expect(!isNormal(@as(T, math.floatTrueMin(T)))); // largest subnormal - try expect(!isNormal(@bitCast(T, ~(~@as(TBits, 0) << math.floatMantissaDigits(T) - 1)))); + try expect(!isNormal(@bitCast(T, ~(~@as(TBits, 0) << math.floatFractionalBits(T))))); // non-finite numbers try expect(!isNormal(-math.inf(T))); |
