Skip to content

Commit

Permalink
Avoid undefined behavior in FMA.
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
hvdijk committed Jan 9, 2024
1 parent ce793d3 commit 14ed452
Showing 1 changed file with 1 addition and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 14ed452

Please sign in to comment.