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
parent 89581c0c8c
commit f4d63ff07d
No known key found for this signature in database
GPG Key ID: 4E3C3CC1031BA9C7

View File

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