const std = @import("../std.zig");
const math = std.math;
const expect = std.testing.expect;
pub fn isNormal(x: anytype) bool {
const T = @TypeOf(x);
const TBits = std.meta.Int(.unsigned, @typeInfo(T).Float.bits);
const increment_exp = 1 << math.floatMantissaBits(T);
const remove_sign = ~@as(TBits, 0) >> 1;
const value = @bitCast(TBits, x) +% increment_exp;
return value & remove_sign >= (increment_exp << 1);
}
test "math.isNormal" {
inline for ([_]type{ f16, f32, f64, f80, f128 }) |T| {
const TBits = std.meta.Int(.unsigned, @bitSizeOf(T));
try expect(isNormal(@as(T, 1.0)));
try expect(isNormal(math.floatMin(T)));
try expect(isNormal(math.floatMax(T)));
try expect(!isNormal(@as(T, -0.0)));
try expect(!isNormal(@as(T, 0.0)));
try expect(!isNormal(@as(T, math.floatTrueMin(T))));
try expect(!isNormal(@bitCast(T, ~(~@as(TBits, 0) << math.floatFractionalBits(T)))));
try expect(!isNormal(-math.inf(T)));
try expect(!isNormal(math.inf(T)));
try expect(!isNormal(math.nan(T)));
try expect(!isNormal(@bitCast(T, ~@as(TBits, 0))));
}
}