authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2022-07-12 23:29:21-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2022-07-13 11:14:46-07:00
logefe34243c674a06ead171adcce67a71efdf057e3
treec78340f97422ff6dc6d25c60606a5bb1f41505e6
parent1fee9eac8bb5d2e3e78c098b9cebe2cda332e7cf

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.

2 files changed, 15 insertions(+), 15 deletions(-)

lib/std/math/float.zig+12-12
...@@ -3,19 +3,19 @@ const assert = std.debug.assert;...@@ -3,19 +3,19 @@ const assert = std.debug.assert;
3const expect = std.testing.expect;3const 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 {6inline fn mantissaOne(comptime T: type) comptime_int {
7 return if (@typeInfo(T).Float.bits == 80) 1 << floatFractionalBits(T) else 0;7 return if (@typeInfo(T).Float.bits == 80) 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.
11fn reconstructFloat(comptime T: type, exponent: comptime_int, mantissa: comptime_int) T {11inline fn reconstructFloat(comptime T: type, exponent: comptime_int, mantissa: comptime_int) T {
12 const TBits = std.meta.Int(.unsigned, @bitSizeOf(T));12 const TBits = std.meta.Int(.unsigned, @bitSizeOf(T));
13 const biased_exponent = @as(TBits, exponent + floatExponentMax(T));13 const biased_exponent = @as(TBits, exponent + floatExponentMax(T));
14 return @bitCast(T, (biased_exponent << floatMantissaBits(T)) | @as(TBits, mantissa));14 return @bitCast(T, (biased_exponent << floatMantissaBits(T)) | @as(TBits, mantissa));
15}15}
1616
17/// Returns the number of bits in the exponent of floating point type T.17/// Returns the number of bits in the exponent of floating point type T.
18pub fn floatExponentBits(comptime T: type) comptime_int {18pub inline fn floatExponentBits(comptime T: type) comptime_int {
19 assert(@typeInfo(T) == .Float);19 assert(@typeInfo(T) == .Float);
2020
21 return switch (@typeInfo(T).Float.bits) {21 return switch (@typeInfo(T).Float.bits) {
...@@ -29,7 +29,7 @@ pub fn floatExponentBits(comptime T: type) comptime_int {...@@ -29,7 +29,7 @@ pub fn floatExponentBits(comptime T: type) comptime_int {
29}29}
3030
31/// Returns the number of bits in the mantissa of floating point type T.31/// Returns the number of bits in the mantissa of floating point type T.
32pub fn floatMantissaBits(comptime T: type) comptime_int {32pub inline fn floatMantissaBits(comptime T: type) comptime_int {
33 assert(@typeInfo(T) == .Float);33 assert(@typeInfo(T) == .Float);
3434
35 return switch (@typeInfo(T).Float.bits) {35 return switch (@typeInfo(T).Float.bits) {
...@@ -43,7 +43,7 @@ pub fn floatMantissaBits(comptime T: type) comptime_int {...@@ -43,7 +43,7 @@ pub fn floatMantissaBits(comptime T: type) comptime_int {
43}43}
4444
45/// Returns the number of fractional bits 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 floatFractionalBits(comptime T: type) comptime_int {46pub inline 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
...@@ -61,39 +61,39 @@ pub fn floatFractionalBits(comptime T: type) comptime_int {...@@ -61,39 +61,39 @@ pub fn floatFractionalBits(comptime T: type) comptime_int {
6161
62/// Returns the minimum exponent that can represent62/// Returns the minimum exponent that can represent
63/// a normalised value in floating point type T.63/// a normalised value in floating point type T.
64pub fn floatExponentMin(comptime T: type) comptime_int {64pub inline fn floatExponentMin(comptime T: type) comptime_int {
65 return -floatExponentMax(T) + 1;65 return -floatExponentMax(T) + 1;
66}66}
6767
68/// Returns the maximum exponent that can represent68/// Returns the maximum exponent that can represent
69/// a normalised value in floating point type T.69/// a normalised value in floating point type T.
70pub fn floatExponentMax(comptime T: type) comptime_int {70pub inline fn floatExponentMax(comptime T: type) comptime_int {
71 return (1 << (floatExponentBits(T) - 1)) - 1;71 return (1 << (floatExponentBits(T) - 1)) - 1;
72}72}
7373
74/// Returns the smallest subnormal number representable in floating point type T.74/// Returns the smallest subnormal number representable in floating point type T.
75pub fn floatTrueMin(comptime T: type) T {75pub inline fn floatTrueMin(comptime T: type) T {
76 return reconstructFloat(T, floatExponentMin(T) - 1, 1);76 return reconstructFloat(T, floatExponentMin(T) - 1, 1);
77}77}
7878
79/// Returns the smallest normal number representable in floating point type T.79/// Returns the smallest normal number representable in floating point type T.
80pub fn floatMin(comptime T: type) T {80pub inline fn floatMin(comptime T: type) T {
81 return reconstructFloat(T, floatExponentMin(T), mantissaOne(T));81 return reconstructFloat(T, floatExponentMin(T), mantissaOne(T));
82}82}
8383
84/// Returns the largest normal number representable in floating point type T.84/// Returns the largest normal number representable in floating point type T.
85pub fn floatMax(comptime T: type) T {85pub inline fn floatMax(comptime T: type) T {
86 const all1s_mantissa = (1 << floatMantissaBits(T)) - 1;86 const all1s_mantissa = (1 << floatMantissaBits(T)) - 1;
87 return reconstructFloat(T, floatExponentMax(T), all1s_mantissa);87 return reconstructFloat(T, floatExponentMax(T), all1s_mantissa);
88}88}
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 inline fn floatEps(comptime T: type) T {
92 return reconstructFloat(T, -floatFractionalBits(T), 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.
96pub fn inf(comptime T: type) T {96pub inline fn inf(comptime T: type) T {
97 return reconstructFloat(T, floatExponentMax(T) + 1, mantissaOne(T));97 return reconstructFloat(T, floatExponentMax(T) + 1, mantissaOne(T));
98}98}
9999
lib/std/math/isinf.zig+3-3
...@@ -3,7 +3,7 @@ const math = std.math;...@@ -3,7 +3,7 @@ const math = std.math;
3const expect = std.testing.expect;3const expect = std.testing.expect;
44
5/// Returns whether x is an infinity, ignoring sign.5/// Returns whether x is an infinity, ignoring sign.
6pub fn isInf(x: anytype) bool {6pub inline fn isInf(x: anytype) bool {
7 const T = @TypeOf(x);7 const T = @TypeOf(x);
8 const TBits = std.meta.Int(.unsigned, @typeInfo(T).Float.bits);8 const TBits = std.meta.Int(.unsigned, @typeInfo(T).Float.bits);
9 const remove_sign = ~@as(TBits, 0) >> 1;9 const remove_sign = ~@as(TBits, 0) >> 1;
...@@ -11,12 +11,12 @@ pub fn isInf(x: anytype) bool {...@@ -11,12 +11,12 @@ pub fn isInf(x: anytype) bool {
11}11}
1212
13/// Returns whether x is an infinity with a positive sign.13/// Returns whether x is an infinity with a positive sign.
14pub fn isPositiveInf(x: anytype) bool {14pub inline fn isPositiveInf(x: anytype) bool {
15 return x == math.inf(@TypeOf(x));15 return x == math.inf(@TypeOf(x));
16}16}
1717
18/// Returns whether x is an infinity with a negative sign.18/// Returns whether x is an infinity with a negative sign.
19pub fn isNegativeInf(x: anytype) bool {19pub inline fn isNegativeInf(x: anytype) bool {
20 return x == -math.inf(@TypeOf(x));20 return x == -math.inf(@TypeOf(x));
21}21}
2222