From 951ab802a38ede9f5329aeae1fed00161321d558 Mon Sep 17 00:00:00 2001 From: alice Date: Tue, 17 May 2022 22:04:12 +0100 Subject: [PATCH] std.math: simpler error handling --- lib/std/math/isfinite.zig | 5 +---- lib/std/math/isinf.zig | 5 +---- lib/std/math/isnormal.zig | 5 +---- lib/std/math/ldexp.zig | 5 +---- 4 files changed, 4 insertions(+), 16 deletions(-) diff --git a/lib/std/math/isfinite.zig b/lib/std/math/isfinite.zig index 67a67a461085d00dcaf0c72a8c272d6403cd09f8..556f8a2378db78ba5beb6789603c671c85449136 100644 --- a/lib/std/math/isfinite.zig +++ b/lib/std/math/isfinite.zig @@ -5,10 +5,7 @@ const expect = std.testing.expect; /// Returns whether x is a finite value. pub fn isFinite(x: anytype) bool { const T = @TypeOf(x); - const TBits = std.meta.Int(.unsigned, @bitSizeOf(T)); - if (@typeInfo(T) != .Float) { - @compileError("isFinite not implemented for " ++ @typeName(T)); - } + const TBits = std.meta.Int(.unsigned, @typeInfo(T).Float.bits); const remove_sign = ~@as(TBits, 0) >> 1; return @bitCast(TBits, x) & remove_sign < @bitCast(TBits, math.inf(T)); } diff --git a/lib/std/math/isinf.zig b/lib/std/math/isinf.zig index 7275740fcc24b2fb0bc8d523b77136befa6f89de..a26332411f85eb5b4b1633edd30b518095bc2d3f 100644 --- a/lib/std/math/isinf.zig +++ b/lib/std/math/isinf.zig @@ -5,10 +5,7 @@ const expect = std.testing.expect; /// Returns whether x is an infinity, ignoring sign. pub fn isInf(x: anytype) bool { const T = @TypeOf(x); - const TBits = std.meta.Int(.unsigned, @bitSizeOf(T)); - if (@typeInfo(T) != .Float) { - @compileError("isInf not implemented for " ++ @typeName(T)); - } + const TBits = std.meta.Int(.unsigned, @typeInfo(T).Float.bits); const remove_sign = ~@as(TBits, 0) >> 1; return @bitCast(TBits, x) & remove_sign == @bitCast(TBits, math.inf(T)); } diff --git a/lib/std/math/isnormal.zig b/lib/std/math/isnormal.zig index e15d8a91cc2dcb8343fc8cb46dc288fc98d6add9..08f848f5dfc7e4a094302a53c9da1e25f88a14c8 100644 --- a/lib/std/math/isnormal.zig +++ b/lib/std/math/isnormal.zig @@ -5,10 +5,7 @@ const expect = std.testing.expect; /// Returns whether x is neither zero, subnormal, infinity, or NaN. pub fn isNormal(x: anytype) bool { const T = @TypeOf(x); - const TBits = std.meta.Int(.unsigned, @bitSizeOf(T)); - if (@typeInfo(T) != .Float) { - @compileError("isNormal not implemented for " ++ @typeName(T)); - } + const TBits = std.meta.Int(.unsigned, @typeInfo(T).Float.bits); const increment_exp = 1 << math.floatMantissaBits(T); const remove_sign = ~@as(TBits, 0) >> 1; diff --git a/lib/std/math/ldexp.zig b/lib/std/math/ldexp.zig index 0934244c65b54607a7259028d4213ee39e16e927..57d8896c9cf59fb9a0e288e77b03ffb10c743162 100644 --- a/lib/std/math/ldexp.zig +++ b/lib/std/math/ldexp.zig @@ -15,10 +15,7 @@ pub fn ldexp(x: anytype, n: i32) @TypeOf(x) { var shift = n; const T = @TypeOf(base); - const TBits = std.meta.Int(.unsigned, @bitSizeOf(T)); - if (@typeInfo(T) != .Float) { - @compileError("ldexp not implemented for " ++ @typeName(T)); - } + const TBits = std.meta.Int(.unsigned, @typeInfo(T).Float.bits); const mantissa_bits = math.floatMantissaBits(T); const exponent_min = math.floatExponentMin(T); -- 2.54.0