authorgravatar for gereeter+code@gmail.comJonathan S <gereeter+code@gmail.com> 2022-01-29 10:11:49-06:00
committergravatar for noreply@github.comGitHub <noreply@github.com> 2022-01-29 18:11:49+02:00
logaca665cebd2b6ec9ec3db669cf5446ee45bbb5d0
treed06c19e46393f5ec0ae7a700cf5eb2e3507fbf21
parentba445013c472abd874f7b041d9ad8f3c72197807
signaturebadge-question-mark Signed by PGP key 4AEE18F83AFDEB23

Fix overflow in std.math.isNormal when applied to -Inf or a negative NaN


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

lib/std/math/isnormal.zig+16-4
......@@ -9,19 +9,19 @@ pub fn isNormal(x: anytype) bool {
99 switch (T) {
1010 f16 => {
1111 const bits = @bitCast(u16, x);
12 return (bits + (1 << 10)) & (maxInt(u16) >> 1) >= (1 << 11);
12 return (bits +% (1 << 10)) & (maxInt(u16) >> 1) >= (1 << 11);
1313 },
1414 f32 => {
1515 const bits = @bitCast(u32, x);
16 return (bits + (1 << 23)) & (maxInt(u32) >> 1) >= (1 << 24);
16 return (bits +% (1 << 23)) & (maxInt(u32) >> 1) >= (1 << 24);
1717 },
1818 f64 => {
1919 const bits = @bitCast(u64, x);
20 return (bits + (1 << 52)) & (maxInt(u64) >> 1) >= (1 << 53);
20 return (bits +% (1 << 52)) & (maxInt(u64) >> 1) >= (1 << 53);
2121 },
2222 f128 => {
2323 const bits = @bitCast(u128, x);
24 return (bits + (1 << 112)) & (maxInt(u128) >> 1) >= (1 << 113);
24 return (bits +% (1 << 112)) & (maxInt(u128) >> 1) >= (1 << 113);
2525 },
2626 else => {
2727 @compileError("isNormal not implemented for " ++ @typeName(T));
......@@ -34,6 +34,18 @@ test "math.isNormal" {
3434 try expect(!isNormal(math.nan(f32)));
3535 try expect(!isNormal(math.nan(f64)));
3636 try expect(!isNormal(math.nan(f128)));
37 try expect(!isNormal(-math.nan(f16)));
38 try expect(!isNormal(-math.nan(f32)));
39 try expect(!isNormal(-math.nan(f64)));
40 try expect(!isNormal(-math.nan(f128)));
41 try expect(!isNormal(math.inf(f16)));
42 try expect(!isNormal(math.inf(f32)));
43 try expect(!isNormal(math.inf(f64)));
44 try expect(!isNormal(math.inf(f128)));
45 try expect(!isNormal(-math.inf(f16)));
46 try expect(!isNormal(-math.inf(f32)));
47 try expect(!isNormal(-math.inf(f64)));
48 try expect(!isNormal(-math.inf(f128)));
3749 try expect(!isNormal(@as(f16, 0)));
3850 try expect(!isNormal(@as(f32, 0)));
3951 try expect(!isNormal(@as(f64, 0)));