| author | |
| committer | |
| log | 319555a6690a43de143698f13d6d107a7873dce0 |
| tree | 17ac027a86b3874746838ff286f98bbf8d24504c |
| parent | 319b5cbce5d4c43de8a6848ac5f5c8879ab9806e |
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; |
| 38 | 38 | ||
| 39 | pub const floatExponentBits = @import("math/float.zig").floatExponentBits; | 39 | pub const floatExponentBits = @import("math/float.zig").floatExponentBits; |
| 40 | pub const floatMantissaBits = @import("math/float.zig").floatMantissaBits; | 40 | pub const floatMantissaBits = @import("math/float.zig").floatMantissaBits; |
| 41 | pub const floatMantissaDigits = @import("math/float.zig").floatMantissaDigits; | 41 | pub const floatFractionalBits = @import("math/float.zig").floatFractionalBits; |
| 42 | pub const floatExponentMin = @import("math/float.zig").floatExponentMin; | 42 | pub const floatExponentMin = @import("math/float.zig").floatExponentMin; |
| 43 | pub const floatExponentMax = @import("math/float.zig").floatExponentMax; | 43 | pub const floatExponentMax = @import("math/float.zig").floatExponentMax; |
| 44 | pub const floatTrueMin = @import("math/float.zig").floatTrueMin; | 44 | pub 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; |
| 4 | 4 | ||
| 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. |
| 6 | fn mantissaOne(comptime T: type) comptime_int { | 6 | fn 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 | } |
| 9 | 9 | ||
| 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 | } |
| 44 | 44 | ||
| 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. |
| 46 | pub fn floatMantissaDigits(comptime T: type) comptime_int { | 46 | pub fn floatFractionalBits(comptime T: type) comptime_int { |
| 47 | assert(@typeInfo(T) == .Float); | 47 | assert(@typeInfo(T) == .Float); |
| 48 | 48 | ||
| 49 | // standard IEEE floats have an implicit 0.m or 1.m integer part | 49 | // 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 MSB | 50 | // 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 |
| 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 { |
| 89 | 89 | ||
| 90 | /// Returns the machine epsilon of floating point type T. | 90 | /// Returns the machine epsilon of floating point type T. |
| 91 | pub fn floatEps(comptime T: type) T { | 91 | pub 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 | } |
| 94 | 94 | ||
| 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); |
| 105 | 105 | ||
| 106 | // for machine epsilon, assert expmin <= -prec <= expmax | 106 | // 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)))); |
| 42 | 42 | ||
| 43 | // largest subnormal | 43 | // 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))))); |
| 45 | 45 | ||
| 46 | // non-finite numbers | 46 | // 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); |
| 16 | 16 | ||
| 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 | } |
| 50 | 50 | ||
| 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 bits | 20 | 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; |
| 24 | 24 | ||
| 25 | // Sign | 25 | // 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 { |
| 29 | 29 | ||
| 30 | // Compute significand | 30 | // 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); |
| 34 | 34 | ||
| 35 | // Shift up result to line up with the significand - no rounding required | 35 | // 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 bit | 37 | 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; |
| 41 | 41 | ||
| 42 | // Shift down result and remove implicit integer bit | 42 | // 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); |
| 54 | 54 | ||
| 55 | // If the result included a carry, we need to restore the explicit integer bit | 55 | // 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; |
| 57 | 57 | ||
| 58 | return @bitCast(T, sign_bit | result); | 58 | return @bitCast(T, sign_bit | result); |
| 59 | } | 59 | } |