From 14ed4520ae62d2429071e176342a1116c05379b3 Mon Sep 17 00:00:00 2001 From: Harald van Dijk Date: Tue, 9 Jan 2024 14:24:19 +0000 Subject: [PATCH] Avoid undefined behavior in FMA. If the result is zero, the result is not denormal, so we do not need to follow the denormal special case logic. The check rounding_mask & ansAsUint would always be zero, so it would never do anything, but the calculation of rounding_mask might have had undefined behavior due to an out-of-range exponent. --- modules/compiler/builtins/abacus/source/abacus_math/fma.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/modules/compiler/builtins/abacus/source/abacus_math/fma.cpp b/modules/compiler/builtins/abacus/source/abacus_math/fma.cpp index 923621092..a67760efa 100644 --- a/modules/compiler/builtins/abacus/source/abacus_math/fma.cpp +++ b/modules/compiler/builtins/abacus/source/abacus_math/fma.cpp @@ -432,7 +432,7 @@ T ABACUS_API __abacus_fma_unsafe(const T x, const T y, const T z) { const SignedType finalExpBiased = (((ansAsUint & exp_mask) >> mantissa_bits)) + 2 * ansExp; - if (finalExpBiased < 1) { + if (ans != 0 && finalExpBiased < 1) { // The answer is a denormal, find what bits will be cut off and see if it's // going to result in a RTE case. const UnsignedType rounding_bits = UnsignedType(0x1)