authorgravatar for topolarity@tapscott.meCody Tapscott <topolarity@tapscott.me> 2022-04-12 12:23:18-07:00
committergravatar for topolarity@tapscott.meCody Tapscott <topolarity@tapscott.me> 2022-04-12 12:33:16-07:00
log319555a6690a43de143698f13d6d107a7873dce0
tree17ac027a86b3874746838ff286f98bbf8d24504c
parent319b5cbce5d4c43de8a6848ac5f5c8879ab9806e

Add `floatFractionalBits` to replace `floatMantissaDigits`


5 files changed, 24 insertions(+), 24 deletions(-)

lib/std/math.zig+1-1
......@@ -38,7 +38,7 @@ pub const sqrt1_2 = 0.707106781186547524400844362104849039;
3838
3939pub const floatExponentBits = @import("math/float.zig").floatExponentBits;
4040pub const floatMantissaBits = @import("math/float.zig").floatMantissaBits;
41pub const floatMantissaDigits = @import("math/float.zig").floatMantissaDigits;
41pub const floatFractionalBits = @import("math/float.zig").floatFractionalBits;
4242pub const floatExponentMin = @import("math/float.zig").floatExponentMin;
4343pub const floatExponentMax = @import("math/float.zig").floatExponentMax;
4444pub const floatTrueMin = @import("math/float.zig").floatTrueMin;
lib/std/math/float.zig+12-12
......@@ -4,7 +4,7 @@ const expect = std.testing.expect;
44
55/// Creates a raw "1.0" mantissa for floating point type T. Used to dedupe f80 logic.
66fn mantissaOne(comptime T: type) comptime_int {
7 return if (floatMantissaDigits(T) == 64) 1 << 63 else 0;
7 return if (T == f80) 1 << floatFractionalBits(T) else 0;
88}
99
1010/// Creates floating point type T from an unbiased exponent and raw mantissa.
......@@ -42,19 +42,19 @@ pub fn floatMantissaBits(comptime T: type) comptime_int {
4242 };
4343}
4444
45/// Returns the number of binary digits in the mantissa of floating point type T.
46pub fn floatMantissaDigits(comptime T: type) comptime_int {
45/// Returns the number of fractional bits in the mantissa of floating point type T.
46pub fn floatFractionalBits(comptime T: type) comptime_int {
4747 assert(@typeInfo(T) == .Float);
4848
4949 // standard IEEE floats have an implicit 0.m or 1.m integer part
5050 // f80 is special and has an explicitly stored bit in the MSB
51 // this function corresponds to `MANT_DIG' constants from C
51 // this function corresponds to `MANT_DIG - 1' from C
5252 return switch (@typeInfo(T).Float.bits) {
53 16 => 11,
54 32 => 24,
55 64 => 53,
56 80 => 64,
57 128 => 113,
53 16 => 10,
54 32 => 23,
55 64 => 52,
56 80 => 63,
57 128 => 112,
5858 else => @compileError("unknown floating point type " ++ @typeName(T)),
5959 };
6060}
......@@ -89,7 +89,7 @@ pub fn floatMax(comptime T: type) T {
8989
9090/// Returns the machine epsilon of floating point type T.
9191pub fn floatEps(comptime T: type) T {
92 return reconstructFloat(T, -(floatMantissaDigits(T) - 1), mantissaOne(T));
92 return reconstructFloat(T, -floatFractionalBits(T), mantissaOne(T));
9393}
9494
9595/// Returns the value inf for floating point type T.
......@@ -104,7 +104,7 @@ test "std.math.float" {
104104 try expect(@bitSizeOf(T) == size);
105105
106106 // for machine epsilon, assert expmin <= -prec <= expmax
107 try expect(floatExponentMin(T) <= -(floatMantissaDigits(T) - 1));
108 try expect(-(floatMantissaDigits(T) - 1) <= floatExponentMax(T));
107 try expect(floatExponentMin(T) <= -floatFractionalBits(T));
108 try expect(-floatFractionalBits(T) <= floatExponentMax(T));
109109 }
110110}
lib/std/math/isnormal.zig+1-1
......@@ -41,7 +41,7 @@ test "math.isNormal" {
4141 try expect(!isNormal(@as(T, math.floatTrueMin(T))));
4242
4343 // largest subnormal
44 try expect(!isNormal(@bitCast(T, ~(~@as(TBits, 0) << math.floatMantissaDigits(T) - 1))));
44 try expect(!isNormal(@bitCast(T, ~(~@as(TBits, 0) << math.floatFractionalBits(T)))));
4545
4646 // non-finite numbers
4747 try expect(!isNormal(-math.inf(T)));
lib/std/special/compiler_rt/fixXfYi.zig+4-4
......@@ -12,7 +12,7 @@ pub inline fn fixXfYi(comptime I: type, a: anytype) I {
1212 const rep_t = std.meta.Int(.unsigned, float_bits);
1313 const sig_bits = math.floatMantissaBits(F);
1414 const exp_bits = math.floatExponentBits(F);
15 const fractional_sig_bits = math.floatMantissaDigits(F) - 1;
15 const fractional_bits = math.floatFractionalBits(F);
1616
1717 const implicit_bit = if (F != f80) (@as(rep_t, 1) << sig_bits) else 0;
1818 const max_exp = (1 << (exp_bits - 1));
......@@ -42,10 +42,10 @@ pub inline fn fixXfYi(comptime I: type, a: anytype) I {
4242 // If 0 <= exponent < sig_bits, right shift to get the result.
4343 // Otherwise, shift left.
4444 var result: I = undefined;
45 if (exponent < fractional_sig_bits) {
46 result = @intCast(I, significand >> @intCast(Log2Int(rep_t), fractional_sig_bits - exponent));
45 if (exponent < fractional_bits) {
46 result = @intCast(I, significand >> @intCast(Log2Int(rep_t), fractional_bits - exponent));
4747 } else {
48 result = @intCast(I, significand) << @intCast(Log2Int(I), exponent - fractional_sig_bits);
48 result = @intCast(I, significand) << @intCast(Log2Int(I), exponent - fractional_bits);
4949 }
5050
5151 if ((@typeInfo(I).Int.signedness == .signed) and negative)
lib/std/special/compiler_rt/floatXiYf.zig+6-6
......@@ -17,9 +17,9 @@ pub fn floatXiYf(comptime T: type, x: anytype) T {
1717 const float_bits = @bitSizeOf(T);
1818 const int_bits = @bitSizeOf(@TypeOf(x));
1919 const exp_bits = math.floatExponentBits(T);
20 const sig_bits = math.floatMantissaDigits(T) - 1; // Only counts the fractional bits
20 const fractional_bits = math.floatFractionalBits(T);
2121 const exp_bias = math.maxInt(std.meta.Int(.unsigned, exp_bits - 1));
22 const implicit_bit = if (T != f80) @as(uT, 1) << sig_bits else 0;
22 const implicit_bit = if (T != f80) @as(uT, 1) << fractional_bits else 0;
2323 const max_exp = exp_bias;
2424
2525 // Sign
......@@ -29,14 +29,14 @@ pub fn floatXiYf(comptime T: type, x: anytype) T {
2929
3030 // Compute significand
3131 var exp = int_bits - @clz(Z, abs_val) - 1;
32 if (int_bits <= sig_bits or exp <= sig_bits) {
33 const shift_amt = sig_bits - @intCast(math.Log2Int(uT), exp);
32 if (int_bits <= fractional_bits or exp <= fractional_bits) {
33 const shift_amt = fractional_bits - @intCast(math.Log2Int(uT), exp);
3434
3535 // Shift up result to line up with the significand - no rounding required
3636 result = (@intCast(uT, abs_val) << shift_amt);
3737 result ^= implicit_bit; // Remove implicit integer bit
3838 } else {
39 var shift_amt = @intCast(math.Log2Int(Z), exp - sig_bits);
39 var shift_amt = @intCast(math.Log2Int(Z), exp - fractional_bits);
4040 const exact_tie: bool = @ctz(Z, abs_val) == shift_amt - 1;
4141
4242 // Shift down result and remove implicit integer bit
......@@ -53,7 +53,7 @@ pub fn floatXiYf(comptime T: type, x: anytype) T {
5353 result += (@as(uT, exp) + exp_bias) << math.floatMantissaBits(T);
5454
5555 // If the result included a carry, we need to restore the explicit integer bit
56 if (T == f80) result |= 1 << sig_bits;
56 if (T == f80) result |= 1 << fractional_bits;
5757
5858 return @bitCast(T, sign_bit | result);
5959}