Skip to content

Commit efe3424

Browse files
committed
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.
1 parent 1fee9ea commit efe3424

File tree

2 files changed

+15
-15
lines changed

2 files changed

+15
-15
lines changed

lib/std/math/float.zig

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,19 @@ const assert = std.debug.assert;
33
const expect = std.testing.expect;
44

55
/// 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 {
77
return if (@typeInfo(T).Float.bits == 80) 1 << floatFractionalBits(T) else 0;
88
}
99

1010
/// 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 {
1212
const TBits = std.meta.Int(.unsigned, @bitSizeOf(T));
1313
const biased_exponent = @as(TBits, exponent + floatExponentMax(T));
1414
return @bitCast(T, (biased_exponent << floatMantissaBits(T)) | @as(TBits, mantissa));
1515
}
1616

1717
/// 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 {
1919
assert(@typeInfo(T) == .Float);
2020

2121
return switch (@typeInfo(T).Float.bits) {
@@ -29,7 +29,7 @@ pub fn floatExponentBits(comptime T: type) comptime_int {
2929
}
3030

3131
/// 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 {
3333
assert(@typeInfo(T) == .Float);
3434

3535
return switch (@typeInfo(T).Float.bits) {
@@ -43,7 +43,7 @@ pub fn floatMantissaBits(comptime T: type) comptime_int {
4343
}
4444

4545
/// 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 {
4747
assert(@typeInfo(T) == .Float);
4848

4949
// 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 {
6161

6262
/// Returns the minimum exponent that can represent
6363
/// 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 {
6565
return -floatExponentMax(T) + 1;
6666
}
6767

6868
/// Returns the maximum exponent that can represent
6969
/// 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 {
7171
return (1 << (floatExponentBits(T) - 1)) - 1;
7272
}
7373

7474
/// 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 {
7676
return reconstructFloat(T, floatExponentMin(T) - 1, 1);
7777
}
7878

7979
/// 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 {
8181
return reconstructFloat(T, floatExponentMin(T), mantissaOne(T));
8282
}
8383

8484
/// 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 {
8686
const all1s_mantissa = (1 << floatMantissaBits(T)) - 1;
8787
return reconstructFloat(T, floatExponentMax(T), all1s_mantissa);
8888
}
8989

9090
/// 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 {
9292
return reconstructFloat(T, -floatFractionalBits(T), mantissaOne(T));
9393
}
9494

9595
/// 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 {
9797
return reconstructFloat(T, floatExponentMax(T) + 1, mantissaOne(T));
9898
}
9999

lib/std/math/isinf.zig

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,20 @@ const math = std.math;
33
const expect = std.testing.expect;
44

55
/// Returns whether x is an infinity, ignoring sign.
6-
pub fn isInf(x: anytype) bool {
6+
pub inline fn isInf(x: anytype) bool {
77
const T = @TypeOf(x);
88
const TBits = std.meta.Int(.unsigned, @typeInfo(T).Float.bits);
99
const remove_sign = ~@as(TBits, 0) >> 1;
1010
return @bitCast(TBits, x) & remove_sign == @bitCast(TBits, math.inf(T));
1111
}
1212

1313
/// Returns whether x is an infinity with a positive sign.
14-
pub fn isPositiveInf(x: anytype) bool {
14+
pub inline fn isPositiveInf(x: anytype) bool {
1515
return x == math.inf(@TypeOf(x));
1616
}
1717

1818
/// Returns whether x is an infinity with a negative sign.
19-
pub fn isNegativeInf(x: anytype) bool {
19+
pub inline fn isNegativeInf(x: anytype) bool {
2020
return x == -math.inf(@TypeOf(x));
2121
}
2222

0 commit comments

Comments
 (0)