Skip to content

Commit 850f70a

Browse files
committed
Add comptime_float support to std.math.isNormal
1 parent ac0f590 commit 850f70a

File tree

1 file changed

+8
-5
lines changed

1 file changed

+8
-5
lines changed

lib/std/math/isnormal.zig

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ const expect = std.testing.expect;
44

55
/// Returns whether x is neither zero, subnormal, infinity, or NaN.
66
pub fn isNormal(x: anytype) bool {
7-
const T = @TypeOf(x);
7+
const T = if (@TypeOf(x) == comptime_float) f128 else @TypeOf(x);
88
const TBits = std.meta.Int(.unsigned, @typeInfo(T).float.bits);
99

1010
const increment_exp = 1 << math.floatMantissaBits(T);
@@ -15,14 +15,14 @@ pub fn isNormal(x: anytype) bool {
1515
// The sign bit is removed because all ones would overflow into it.
1616
// For f80, even though it has an explicit integer part stored,
1717
// the exponent effectively takes priority if mismatching.
18-
const value = @as(TBits, @bitCast(x)) +% increment_exp;
18+
const value = @as(TBits, @bitCast(@as(T, x))) +% increment_exp;
1919
return value & remove_sign >= (increment_exp << 1);
2020
}
2121

2222
test isNormal {
2323
// TODO add `c_longdouble' when math.inf(T) supports it
24-
inline for ([_]type{ f16, f32, f64, f80, f128 }) |T| {
25-
const TBits = std.meta.Int(.unsigned, @bitSizeOf(T));
24+
inline for ([_]type{ f16, f32, f64, f80, f128, comptime_float }) |T| {
25+
const TBits = if (T == comptime_float) u128 else std.meta.Int(.unsigned, @bitSizeOf(T));
2626

2727
// normals
2828
try expect(isNormal(@as(T, 1.0)));
@@ -35,7 +35,10 @@ test isNormal {
3535
try expect(!isNormal(@as(T, math.floatTrueMin(T))));
3636

3737
// largest subnormal
38-
try expect(!isNormal(@as(T, @bitCast(~(~@as(TBits, 0) << math.floatFractionalBits(T))))));
38+
const large_subnormal: if (T == comptime_float) f128 else T = @bitCast(~(~@as(TBits, 0) << math.floatFractionalBits(T)));
39+
try expect(!isNormal(@as(T, large_subnormal)));
40+
41+
if (T == comptime_float) return;
3942

4043
// non-finite numbers
4144
try expect(!isNormal(-math.inf(T)));

0 commit comments

Comments
 (0)