| ... | ... | @@ -3,19 +3,19 @@ const assert = std.debug.assert; |
| 3 | 3 | const expect = std.testing.expect; |
| 4 | 4 | |
| 5 | 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 | inline fn mantissaOne(comptime T: type) comptime_int { |
| 7 | 7 | return if (@typeInfo(T).Float.bits == 80) 1 << floatFractionalBits(T) else 0; |
| 8 | 8 | } |
| 9 | 9 | |
| 10 | 10 | /// Creates floating point type T from an unbiased exponent and raw mantissa. |
| 11 | | fn reconstructFloat(comptime T: type, exponent: comptime_int, mantissa: comptime_int) T { |
| 11 | inline fn reconstructFloat(comptime T: type, exponent: comptime_int, mantissa: comptime_int) T { |
| 12 | 12 | const TBits = std.meta.Int(.unsigned, @bitSizeOf(T)); |
| 13 | 13 | const biased_exponent = @as(TBits, exponent + floatExponentMax(T)); |
| 14 | 14 | return @bitCast(T, (biased_exponent << floatMantissaBits(T)) | @as(TBits, mantissa)); |
| 15 | 15 | } |
| 16 | 16 | |
| 17 | 17 | /// Returns the number of bits in the exponent of floating point type T. |
| 18 | | pub fn floatExponentBits(comptime T: type) comptime_int { |
| 18 | pub inline fn floatExponentBits(comptime T: type) comptime_int { |
| 19 | 19 | assert(@typeInfo(T) == .Float); |
| 20 | 20 | |
| 21 | 21 | return switch (@typeInfo(T).Float.bits) { |
| ... | ... | @@ -29,7 +29,7 @@ pub fn floatExponentBits(comptime T: type) comptime_int { |
| 29 | 29 | } |
| 30 | 30 | |
| 31 | 31 | /// Returns the number of bits in the mantissa of floating point type T. |
| 32 | | pub fn floatMantissaBits(comptime T: type) comptime_int { |
| 32 | pub inline fn floatMantissaBits(comptime T: type) comptime_int { |
| 33 | 33 | assert(@typeInfo(T) == .Float); |
| 34 | 34 | |
| 35 | 35 | return switch (@typeInfo(T).Float.bits) { |
| ... | ... | @@ -43,7 +43,7 @@ pub fn floatMantissaBits(comptime T: type) comptime_int { |
| 43 | 43 | } |
| 44 | 44 | |
| 45 | 45 | /// Returns the number of fractional bits in the mantissa of floating point type T. |
| 46 | | pub fn floatFractionalBits(comptime T: type) comptime_int { |
| 46 | pub inline fn floatFractionalBits(comptime T: type) comptime_int { |
| 47 | 47 | assert(@typeInfo(T) == .Float); |
| 48 | 48 | |
| 49 | 49 | // standard IEEE floats have an implicit 0.m or 1.m integer part |
| ... | ... | @@ -61,39 +61,39 @@ pub fn floatFractionalBits(comptime T: type) comptime_int { |
| 61 | 61 | |
| 62 | 62 | /// Returns the minimum exponent that can represent |
| 63 | 63 | /// a normalised value in floating point type T. |
| 64 | | pub fn floatExponentMin(comptime T: type) comptime_int { |
| 64 | pub inline fn floatExponentMin(comptime T: type) comptime_int { |
| 65 | 65 | return -floatExponentMax(T) + 1; |
| 66 | 66 | } |
| 67 | 67 | |
| 68 | 68 | /// Returns the maximum exponent that can represent |
| 69 | 69 | /// a normalised value in floating point type T. |
| 70 | | pub fn floatExponentMax(comptime T: type) comptime_int { |
| 70 | pub inline fn floatExponentMax(comptime T: type) comptime_int { |
| 71 | 71 | return (1 << (floatExponentBits(T) - 1)) - 1; |
| 72 | 72 | } |
| 73 | 73 | |
| 74 | 74 | /// Returns the smallest subnormal number representable in floating point type T. |
| 75 | | pub fn floatTrueMin(comptime T: type) T { |
| 75 | pub inline fn floatTrueMin(comptime T: type) T { |
| 76 | 76 | return reconstructFloat(T, floatExponentMin(T) - 1, 1); |
| 77 | 77 | } |
| 78 | 78 | |
| 79 | 79 | /// Returns the smallest normal number representable in floating point type T. |
| 80 | | pub fn floatMin(comptime T: type) T { |
| 80 | pub inline fn floatMin(comptime T: type) T { |
| 81 | 81 | return reconstructFloat(T, floatExponentMin(T), mantissaOne(T)); |
| 82 | 82 | } |
| 83 | 83 | |
| 84 | 84 | /// Returns the largest normal number representable in floating point type T. |
| 85 | | pub fn floatMax(comptime T: type) T { |
| 85 | pub inline fn floatMax(comptime T: type) T { |
| 86 | 86 | const all1s_mantissa = (1 << floatMantissaBits(T)) - 1; |
| 87 | 87 | return reconstructFloat(T, floatExponentMax(T), all1s_mantissa); |
| 88 | 88 | } |
| 89 | 89 | |
| 90 | 90 | /// Returns the machine epsilon of floating point type T. |
| 91 | | pub fn floatEps(comptime T: type) T { |
| 91 | pub inline fn floatEps(comptime T: type) T { |
| 92 | 92 | return reconstructFloat(T, -floatFractionalBits(T), mantissaOne(T)); |
| 93 | 93 | } |
| 94 | 94 | |
| 95 | 95 | /// Returns the value inf for floating point type T. |
| 96 | | pub fn inf(comptime T: type) T { |
| 96 | pub inline fn inf(comptime T: type) T { |
| 97 | 97 | return reconstructFloat(T, floatExponentMax(T) + 1, mantissaOne(T)); |
| 98 | 98 | } |
| 99 | 99 | |