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.
This commit is contained in:
Lioncash 2019-04-01 19:51:45 -04:00 committed by MerryMage
parent 51fe05a443
commit 46eae8cf2f

View file

@ -48,8 +48,9 @@ FPT FPRecipExponent(FPT op, FPCR fpcr, FPSR& fpsr) {
} }
// Infinities and normals // Infinities and normals
const auto negated_exponent = (~exponent << FPInfo<FPT>::explicit_mantissa_width) & FPInfo<FPT>::exponent_mask; const FPT negated_exponent = FPT(~exponent);
return FPT(sign_bits | negated_exponent); const FPT adjusted_exponent = FPT(negated_exponent << FPInfo<FPT>::explicit_mantissa_width) & FPInfo<FPT>::exponent_mask;
return FPT(sign_bits | adjusted_exponent);
} }
template u16 FPRecipExponent<u16>(u16 op, FPCR fpcr, FPSR& fpsr); template u16 FPRecipExponent<u16>(u16 op, FPCR fpcr, FPSR& fpsr);