From f4d63ff07d92c6479ffe4b22787e5d6cae24eff7 Mon Sep 17 00:00:00 2001 From: Lioncash Date: Mon, 1 Apr 2019 19:51:45 -0400 Subject: [PATCH] common/fp/op/FPRecipExponent: Prevent undefined behavior from shifting a negative value Due to promotion rules (types < int, even if unsigned, get promoted to int when arithmetic is performed on them), this is a potential spot for undefined behavior. --- src/common/fp/op/FPRecipExponent.cpp | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/common/fp/op/FPRecipExponent.cpp b/src/common/fp/op/FPRecipExponent.cpp index 4e0224e7..eafad6a9 100644 --- a/src/common/fp/op/FPRecipExponent.cpp +++ b/src/common/fp/op/FPRecipExponent.cpp @@ -48,8 +48,9 @@ FPT FPRecipExponent(FPT op, FPCR fpcr, FPSR& fpsr) { } // Infinities and normals - const auto negated_exponent = (~exponent << FPInfo::explicit_mantissa_width) & FPInfo::exponent_mask; - return FPT(sign_bits | negated_exponent); + const FPT negated_exponent = FPT(~exponent); + const FPT adjusted_exponent = FPT(negated_exponent << FPInfo::explicit_mantissa_width) & FPInfo::exponent_mask; + return FPT(sign_bits | adjusted_exponent); } template u16 FPRecipExponent(u16 op, FPCR fpcr, FPSR& fpsr);