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;
33const expect = std.testing.expect;
44
55/// 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 {
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.
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 {
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.
18pub fn floatExponentBits(comptime T: type) comptime_int {
18pub 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.
32pub fn floatMantissaBits(comptime T: type) comptime_int {
32pub 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.
46pub fn floatFractionalBits(comptime T: type) comptime_int {
46pub 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.
64pub fn floatExponentMin(comptime T: type) comptime_int {
64pub 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.
70pub fn floatExponentMax(comptime T: type) comptime_int {
70pub 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.
75pub fn floatTrueMin(comptime T: type) T {
75pub 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.
80pub fn floatMin(comptime T: type) T {
80pub 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.
85pub fn floatMax(comptime T: type) T {
85pub 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.
91pub fn floatEps(comptime T: type) T {
91pub 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.
96pub fn inf(comptime T: type) T {
96pub inline fn inf(comptime T: type) T {
9797 return reconstructFloat(T, floatExponentMax(T) + 1, mantissaOne(T));
9898}
9999
lib/std/math/isinf.zig+3-3
......@@ -3,7 +3,7 @@ const math = std.math;
33const expect = std.testing.expect;
44
55/// Returns whether x is an infinity, ignoring sign.
6pub fn isInf(x: anytype) bool {
6pub 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;
......@@ -11,12 +11,12 @@ pub fn isInf(x: anytype) bool {
1111}
1212
1313/// Returns whether x is an infinity with a positive sign.
14pub fn isPositiveInf(x: anytype) bool {
14pub 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.
19pub fn isNegativeInf(x: anytype) bool {
19pub inline fn isNegativeInf(x: anytype) bool {
2020 return x == -math.inf(@TypeOf(x));
2121}
2222