authorgravatar for hi@dnydxn.moealice <hi@dnydxn.moe> 2022-05-17 22:04:12+01:00
committergravatar for hi@dnydxn.moealice <hi@dnydxn.moe> 2022-05-17 22:04:12+01:00
log951ab802a38ede9f5329aeae1fed00161321d558
treeb72445012f44c57c5e4670ebfc9e675777958fc4
parent70b6b98e91fb9d1d5575da4d59c5c523864a8f0e
signaturelock-open Commit is signed but in an unrecognized format.

std.math: simpler error handling


4 files changed, 4 insertions(+), 16 deletions(-)

lib/std/math/isfinite.zig+1-4
...@@ -5,10 +5,7 @@ const expect = std.testing.expect;...@@ -5,10 +5,7 @@ const expect = std.testing.expect;
5/// Returns whether x is a finite value.5/// Returns whether x is a finite value.
6pub fn isFinite(x: anytype) bool {6pub fn isFinite(x: anytype) bool {
7 const T = @TypeOf(x);7 const T = @TypeOf(x);
8 const TBits = std.meta.Int(.unsigned, @bitSizeOf(T));8 const TBits = std.meta.Int(.unsigned, @typeInfo(T).Float.bits);
9 if (@typeInfo(T) != .Float) {
10 @compileError("isFinite not implemented for " ++ @typeName(T));
11 }
12 const remove_sign = ~@as(TBits, 0) >> 1;9 const remove_sign = ~@as(TBits, 0) >> 1;
13 return @bitCast(TBits, x) & remove_sign < @bitCast(TBits, math.inf(T));10 return @bitCast(TBits, x) & remove_sign < @bitCast(TBits, math.inf(T));
14}11}
lib/std/math/isinf.zig+1-4
...@@ -5,10 +5,7 @@ const expect = std.testing.expect;...@@ -5,10 +5,7 @@ const expect = std.testing.expect;
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 fn isInf(x: anytype) bool {
7 const T = @TypeOf(x);7 const T = @TypeOf(x);
8 const TBits = std.meta.Int(.unsigned, @bitSizeOf(T));8 const TBits = std.meta.Int(.unsigned, @typeInfo(T).Float.bits);
9 if (@typeInfo(T) != .Float) {
10 @compileError("isInf not implemented for " ++ @typeName(T));
11 }
12 const remove_sign = ~@as(TBits, 0) >> 1;9 const remove_sign = ~@as(TBits, 0) >> 1;
13 return @bitCast(TBits, x) & remove_sign == @bitCast(TBits, math.inf(T));10 return @bitCast(TBits, x) & remove_sign == @bitCast(TBits, math.inf(T));
14}11}
lib/std/math/isnormal.zig+1-4
...@@ -5,10 +5,7 @@ const expect = std.testing.expect;...@@ -5,10 +5,7 @@ const expect = std.testing.expect;
5/// Returns whether x is neither zero, subnormal, infinity, or NaN.5/// Returns whether x is neither zero, subnormal, infinity, or NaN.
6pub fn isNormal(x: anytype) bool {6pub fn isNormal(x: anytype) bool {
7 const T = @TypeOf(x);7 const T = @TypeOf(x);
8 const TBits = std.meta.Int(.unsigned, @bitSizeOf(T));8 const TBits = std.meta.Int(.unsigned, @typeInfo(T).Float.bits);
9 if (@typeInfo(T) != .Float) {
10 @compileError("isNormal not implemented for " ++ @typeName(T));
11 }
129
13 const increment_exp = 1 << math.floatMantissaBits(T);10 const increment_exp = 1 << math.floatMantissaBits(T);
14 const remove_sign = ~@as(TBits, 0) >> 1;11 const remove_sign = ~@as(TBits, 0) >> 1;
lib/std/math/ldexp.zig+1-4
...@@ -15,10 +15,7 @@ pub fn ldexp(x: anytype, n: i32) @TypeOf(x) {...@@ -15,10 +15,7 @@ pub fn ldexp(x: anytype, n: i32) @TypeOf(x) {
15 var shift = n;15 var shift = n;
1616
17 const T = @TypeOf(base);17 const T = @TypeOf(base);
18 const TBits = std.meta.Int(.unsigned, @bitSizeOf(T));18 const TBits = std.meta.Int(.unsigned, @typeInfo(T).Float.bits);
19 if (@typeInfo(T) != .Float) {
20 @compileError("ldexp not implemented for " ++ @typeName(T));
21 }
2219
23 const mantissa_bits = math.floatMantissaBits(T);20 const mantissa_bits = math.floatMantissaBits(T);
24 const exponent_min = math.floatExponentMin(T);21 const exponent_min = math.floatExponentMin(T);