std.math: add inline to some functions

These functions semantically benefit from being inline; it makes sense
that `isInf(x)` where `x` is comptime-known should have a
comptime-known result.
This commit is contained in:
Andrew Kelley 2022-07-12 23:29:21 -07:00
parent 1fee9eac8b
commit efe34243c6
2 changed files with 15 additions and 15 deletions

View File

@ -3,19 +3,19 @@ const assert = std.debug.assert;
const expect = std.testing.expect; const expect = std.testing.expect;
/// Creates a raw "1.0" mantissa for floating point type T. Used to dedupe f80 logic. /// Creates a raw "1.0" mantissa for floating point type T. Used to dedupe f80 logic.
fn mantissaOne(comptime T: type) comptime_int { inline fn mantissaOne(comptime T: type) comptime_int {
return if (@typeInfo(T).Float.bits == 80) 1 << floatFractionalBits(T) else 0; return if (@typeInfo(T).Float.bits == 80) 1 << floatFractionalBits(T) else 0;
} }
/// Creates floating point type T from an unbiased exponent and raw mantissa. /// Creates floating point type T from an unbiased exponent and raw mantissa.
fn reconstructFloat(comptime T: type, exponent: comptime_int, mantissa: comptime_int) T { inline fn reconstructFloat(comptime T: type, exponent: comptime_int, mantissa: comptime_int) T {
const TBits = std.meta.Int(.unsigned, @bitSizeOf(T)); const TBits = std.meta.Int(.unsigned, @bitSizeOf(T));
const biased_exponent = @as(TBits, exponent + floatExponentMax(T)); const biased_exponent = @as(TBits, exponent + floatExponentMax(T));
return @bitCast(T, (biased_exponent << floatMantissaBits(T)) | @as(TBits, mantissa)); return @bitCast(T, (biased_exponent << floatMantissaBits(T)) | @as(TBits, mantissa));
} }
/// Returns the number of bits in the exponent of floating point type T. /// Returns the number of bits in the exponent of floating point type T.
pub fn floatExponentBits(comptime T: type) comptime_int { pub inline fn floatExponentBits(comptime T: type) comptime_int {
assert(@typeInfo(T) == .Float); assert(@typeInfo(T) == .Float);
return switch (@typeInfo(T).Float.bits) { return switch (@typeInfo(T).Float.bits) {
@ -29,7 +29,7 @@ pub fn floatExponentBits(comptime T: type) comptime_int {
} }
/// Returns the number of bits in the mantissa of floating point type T. /// Returns the number of bits in the mantissa of floating point type T.
pub fn floatMantissaBits(comptime T: type) comptime_int { pub inline fn floatMantissaBits(comptime T: type) comptime_int {
assert(@typeInfo(T) == .Float); assert(@typeInfo(T) == .Float);
return switch (@typeInfo(T).Float.bits) { return switch (@typeInfo(T).Float.bits) {
@ -43,7 +43,7 @@ pub fn floatMantissaBits(comptime T: type) comptime_int {
} }
/// Returns the number of fractional bits in the mantissa of floating point type T. /// Returns the number of fractional bits in the mantissa of floating point type T.
pub fn floatFractionalBits(comptime T: type) comptime_int { pub inline fn floatFractionalBits(comptime T: type) comptime_int {
assert(@typeInfo(T) == .Float); assert(@typeInfo(T) == .Float);
// standard IEEE floats have an implicit 0.m or 1.m integer part // 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 {
/// Returns the minimum exponent that can represent /// Returns the minimum exponent that can represent
/// a normalised value in floating point type T. /// a normalised value in floating point type T.
pub fn floatExponentMin(comptime T: type) comptime_int { pub inline fn floatExponentMin(comptime T: type) comptime_int {
return -floatExponentMax(T) + 1; return -floatExponentMax(T) + 1;
} }
/// Returns the maximum exponent that can represent /// Returns the maximum exponent that can represent
/// a normalised value in floating point type T. /// a normalised value in floating point type T.
pub fn floatExponentMax(comptime T: type) comptime_int { pub inline fn floatExponentMax(comptime T: type) comptime_int {
return (1 << (floatExponentBits(T) - 1)) - 1; return (1 << (floatExponentBits(T) - 1)) - 1;
} }
/// Returns the smallest subnormal number representable in floating point type T. /// Returns the smallest subnormal number representable in floating point type T.
pub fn floatTrueMin(comptime T: type) T { pub inline fn floatTrueMin(comptime T: type) T {
return reconstructFloat(T, floatExponentMin(T) - 1, 1); return reconstructFloat(T, floatExponentMin(T) - 1, 1);
} }
/// Returns the smallest normal number representable in floating point type T. /// Returns the smallest normal number representable in floating point type T.
pub fn floatMin(comptime T: type) T { pub inline fn floatMin(comptime T: type) T {
return reconstructFloat(T, floatExponentMin(T), mantissaOne(T)); return reconstructFloat(T, floatExponentMin(T), mantissaOne(T));
} }
/// Returns the largest normal number representable in floating point type T. /// Returns the largest normal number representable in floating point type T.
pub fn floatMax(comptime T: type) T { pub inline fn floatMax(comptime T: type) T {
const all1s_mantissa = (1 << floatMantissaBits(T)) - 1; const all1s_mantissa = (1 << floatMantissaBits(T)) - 1;
return reconstructFloat(T, floatExponentMax(T), all1s_mantissa); return reconstructFloat(T, floatExponentMax(T), all1s_mantissa);
} }
/// Returns the machine epsilon of floating point type T. /// Returns the machine epsilon of floating point type T.
pub fn floatEps(comptime T: type) T { pub inline fn floatEps(comptime T: type) T {
return reconstructFloat(T, -floatFractionalBits(T), mantissaOne(T)); return reconstructFloat(T, -floatFractionalBits(T), mantissaOne(T));
} }
/// Returns the value inf for floating point type T. /// Returns the value inf for floating point type T.
pub fn inf(comptime T: type) T { pub inline fn inf(comptime T: type) T {
return reconstructFloat(T, floatExponentMax(T) + 1, mantissaOne(T)); return reconstructFloat(T, floatExponentMax(T) + 1, mantissaOne(T));
} }

View File

@ -3,7 +3,7 @@ const math = std.math;
const expect = std.testing.expect; const expect = std.testing.expect;
/// Returns whether x is an infinity, ignoring sign. /// Returns whether x is an infinity, ignoring sign.
pub fn isInf(x: anytype) bool { pub inline fn isInf(x: anytype) bool {
const T = @TypeOf(x); const T = @TypeOf(x);
const TBits = std.meta.Int(.unsigned, @typeInfo(T).Float.bits); const TBits = std.meta.Int(.unsigned, @typeInfo(T).Float.bits);
const remove_sign = ~@as(TBits, 0) >> 1; const remove_sign = ~@as(TBits, 0) >> 1;
@ -11,12 +11,12 @@ pub fn isInf(x: anytype) bool {
} }
/// Returns whether x is an infinity with a positive sign. /// Returns whether x is an infinity with a positive sign.
pub fn isPositiveInf(x: anytype) bool { pub inline fn isPositiveInf(x: anytype) bool {
return x == math.inf(@TypeOf(x)); return x == math.inf(@TypeOf(x));
} }
/// Returns whether x is an infinity with a negative sign. /// Returns whether x is an infinity with a negative sign.
pub fn isNegativeInf(x: anytype) bool { pub inline fn isNegativeInf(x: anytype) bool {
return x == -math.inf(@TypeOf(x)); return x == -math.inf(@TypeOf(x));
} }