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;...@@ -38,7 +38,7 @@ pub const sqrt1_2 = 0.707106781186547524400844362104849039;
3838
39pub const floatExponentBits = @import("math/float.zig").floatExponentBits;39pub const floatExponentBits = @import("math/float.zig").floatExponentBits;
40pub const floatMantissaBits = @import("math/float.zig").floatMantissaBits;40pub const floatMantissaBits = @import("math/float.zig").floatMantissaBits;
41pub const floatMantissaDigits = @import("math/float.zig").floatMantissaDigits;41pub const floatFractionalBits = @import("math/float.zig").floatFractionalBits;
42pub const floatExponentMin = @import("math/float.zig").floatExponentMin;42pub const floatExponentMin = @import("math/float.zig").floatExponentMin;
43pub const floatExponentMax = @import("math/float.zig").floatExponentMax;43pub const floatExponentMax = @import("math/float.zig").floatExponentMax;
44pub const floatTrueMin = @import("math/float.zig").floatTrueMin;44pub const floatTrueMin = @import("math/float.zig").floatTrueMin;
lib/std/math/float.zig+12-12
...@@ -4,7 +4,7 @@ const expect = std.testing.expect;...@@ -4,7 +4,7 @@ const expect = std.testing.expect;
44
5/// Creates a raw "1.0" mantissa for floating point type T. Used to dedupe f80 logic.5/// Creates a raw "1.0" mantissa for floating point type T. Used to dedupe f80 logic.
6fn mantissaOne(comptime T: type) comptime_int {6fn 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;
8}8}
99
10/// Creates floating point type T from an unbiased exponent and raw mantissa.10/// Creates floating point type T from an unbiased exponent and raw mantissa.
...@@ -42,19 +42,19 @@ pub fn floatMantissaBits(comptime T: type) comptime_int {...@@ -42,19 +42,19 @@ pub fn floatMantissaBits(comptime T: type) comptime_int {
42 };42 };
43}43}
4444
45/// Returns the number of binary digits in the mantissa of floating point type T.45/// Returns the number of fractional bits in the mantissa of floating point type T.
46pub fn floatMantissaDigits(comptime T: type) comptime_int {46pub fn floatFractionalBits(comptime T: type) comptime_int {
47 assert(@typeInfo(T) == .Float);47 assert(@typeInfo(T) == .Float);
4848
49 // standard IEEE floats have an implicit 0.m or 1.m integer part49 // standard IEEE floats have an implicit 0.m or 1.m integer part
50 // f80 is special and has an explicitly stored bit in the MSB50 // f80 is special and has an explicitly stored bit in the MSB
51 // this function corresponds to `MANT_DIG' constants from C51 // this function corresponds to `MANT_DIG - 1' from C
52 return switch (@typeInfo(T).Float.bits) {52 return switch (@typeInfo(T).Float.bits) {
53 16 => 11,53 16 => 10,
54 32 => 24,54 32 => 23,
55 64 => 53,55 64 => 52,
56 80 => 64,56 80 => 63,
57 128 => 113,57 128 => 112,
58 else => @compileError("unknown floating point type " ++ @typeName(T)),58 else => @compileError("unknown floating point type " ++ @typeName(T)),
59 };59 };
60}60}
...@@ -89,7 +89,7 @@ pub fn floatMax(comptime T: type) T {...@@ -89,7 +89,7 @@ pub fn floatMax(comptime T: type) T {
8989
90/// Returns the machine epsilon of floating point type T.90/// Returns the machine epsilon of floating point type T.
91pub fn floatEps(comptime T: type) T {91pub fn floatEps(comptime T: type) T {
92 return reconstructFloat(T, -(floatMantissaDigits(T) - 1), mantissaOne(T));92 return reconstructFloat(T, -floatFractionalBits(T), mantissaOne(T));
93}93}
9494
95/// Returns the value inf for floating point type T.95/// Returns the value inf for floating point type T.
...@@ -104,7 +104,7 @@ test "std.math.float" {...@@ -104,7 +104,7 @@ test "std.math.float" {
104 try expect(@bitSizeOf(T) == size);104 try expect(@bitSizeOf(T) == size);
105105
106 // for machine epsilon, assert expmin <= -prec <= expmax106 // for machine epsilon, assert expmin <= -prec <= expmax
107 try expect(floatExponentMin(T) <= -(floatMantissaDigits(T) - 1));107 try expect(floatExponentMin(T) <= -floatFractionalBits(T));
108 try expect(-(floatMantissaDigits(T) - 1) <= floatExponentMax(T));108 try expect(-floatFractionalBits(T) <= floatExponentMax(T));
109 }109 }
110}110}
lib/std/math/isnormal.zig+1-1
...@@ -41,7 +41,7 @@ test "math.isNormal" {...@@ -41,7 +41,7 @@ test "math.isNormal" {
41 try expect(!isNormal(@as(T, math.floatTrueMin(T))));41 try expect(!isNormal(@as(T, math.floatTrueMin(T))));
4242
43 // largest subnormal43 // 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
46 // non-finite numbers46 // non-finite numbers
47 try expect(!isNormal(-math.inf(T)));47 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 {...@@ -12,7 +12,7 @@ pub inline fn fixXfYi(comptime I: type, a: anytype) I {
12 const rep_t = std.meta.Int(.unsigned, float_bits);12 const rep_t = std.meta.Int(.unsigned, float_bits);
13 const sig_bits = math.floatMantissaBits(F);13 const sig_bits = math.floatMantissaBits(F);
14 const exp_bits = math.floatExponentBits(F);14 const exp_bits = math.floatExponentBits(F);
15 const fractional_sig_bits = math.floatMantissaDigits(F) - 1;15 const fractional_bits = math.floatFractionalBits(F);
1616
17 const implicit_bit = if (F != f80) (@as(rep_t, 1) << sig_bits) else 0;17 const implicit_bit = if (F != f80) (@as(rep_t, 1) << sig_bits) else 0;
18 const max_exp = (1 << (exp_bits - 1));18 const max_exp = (1 << (exp_bits - 1));
...@@ -42,10 +42,10 @@ pub inline fn fixXfYi(comptime I: type, a: anytype) I {...@@ -42,10 +42,10 @@ pub inline fn fixXfYi(comptime I: type, a: anytype) I {
42 // If 0 <= exponent < sig_bits, right shift to get the result.42 // If 0 <= exponent < sig_bits, right shift to get the result.
43 // Otherwise, shift left.43 // Otherwise, shift left.
44 var result: I = undefined;44 var result: I = undefined;
45 if (exponent < fractional_sig_bits) {45 if (exponent < fractional_bits) {
46 result = @intCast(I, significand >> @intCast(Log2Int(rep_t), fractional_sig_bits - exponent));46 result = @intCast(I, significand >> @intCast(Log2Int(rep_t), fractional_bits - exponent));
47 } else {47 } else {
48 result = @intCast(I, significand) << @intCast(Log2Int(I), exponent - fractional_sig_bits);48 result = @intCast(I, significand) << @intCast(Log2Int(I), exponent - fractional_bits);
49 }49 }
5050
51 if ((@typeInfo(I).Int.signedness == .signed) and negative)51 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 {...@@ -17,9 +17,9 @@ pub fn floatXiYf(comptime T: type, x: anytype) T {
17 const float_bits = @bitSizeOf(T);17 const float_bits = @bitSizeOf(T);
18 const int_bits = @bitSizeOf(@TypeOf(x));18 const int_bits = @bitSizeOf(@TypeOf(x));
19 const exp_bits = math.floatExponentBits(T);19 const exp_bits = math.floatExponentBits(T);
20 const sig_bits = math.floatMantissaDigits(T) - 1; // Only counts the fractional bits20 const fractional_bits = math.floatFractionalBits(T);
21 const exp_bias = math.maxInt(std.meta.Int(.unsigned, exp_bits - 1));21 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;
23 const max_exp = exp_bias;23 const max_exp = exp_bias;
2424
25 // Sign25 // Sign
...@@ -29,14 +29,14 @@ pub fn floatXiYf(comptime T: type, x: anytype) T {...@@ -29,14 +29,14 @@ pub fn floatXiYf(comptime T: type, x: anytype) T {
2929
30 // Compute significand30 // Compute significand
31 var exp = int_bits - @clz(Z, abs_val) - 1;31 var exp = int_bits - @clz(Z, abs_val) - 1;
32 if (int_bits <= sig_bits or exp <= sig_bits) {32 if (int_bits <= fractional_bits or exp <= fractional_bits) {
33 const shift_amt = sig_bits - @intCast(math.Log2Int(uT), exp);33 const shift_amt = fractional_bits - @intCast(math.Log2Int(uT), exp);
3434
35 // Shift up result to line up with the significand - no rounding required35 // Shift up result to line up with the significand - no rounding required
36 result = (@intCast(uT, abs_val) << shift_amt);36 result = (@intCast(uT, abs_val) << shift_amt);
37 result ^= implicit_bit; // Remove implicit integer bit37 result ^= implicit_bit; // Remove implicit integer bit
38 } else {38 } else {
39 var shift_amt = @intCast(math.Log2Int(Z), exp - sig_bits);39 var shift_amt = @intCast(math.Log2Int(Z), exp - fractional_bits);
40 const exact_tie: bool = @ctz(Z, abs_val) == shift_amt - 1;40 const exact_tie: bool = @ctz(Z, abs_val) == shift_amt - 1;
4141
42 // Shift down result and remove implicit integer bit42 // Shift down result and remove implicit integer bit
...@@ -53,7 +53,7 @@ pub fn floatXiYf(comptime T: type, x: anytype) T {...@@ -53,7 +53,7 @@ pub fn floatXiYf(comptime T: type, x: anytype) T {
53 result += (@as(uT, exp) + exp_bias) << math.floatMantissaBits(T);53 result += (@as(uT, exp) + exp_bias) << math.floatMantissaBits(T);
5454
55 // If the result included a carry, we need to restore the explicit integer bit55 // 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
58 return @bitCast(T, sign_bit | result);58 return @bitCast(T, sign_bit | result);
59}59}