LibC: Prevent undefined shift in internal_to_integer

New tests will hit a dead bit count of 64, leading to an undefined
shift.
This commit is contained in:
kleines Filmröllchen 2024-01-08 16:24:42 +01:00 committed by Andrew Kaster
parent 8443d0a74d
commit 334f5e319c
Notes: sideshowbarker 2024-07-17 22:09:47 +09:00

View file

@ -68,6 +68,9 @@ static FloatType internal_to_integer(FloatType x, RoundingMode rounding_mode)
return x;
using Extractor = FloatExtractor<decltype(x)>;
// Most component types are larger than int.
constexpr auto zero = static_cast<Extractor::ComponentType>(0);
constexpr auto one = static_cast<Extractor::ComponentType>(1);
Extractor extractor;
extractor.d = x;
@ -90,7 +93,8 @@ static FloatType internal_to_integer(FloatType x, RoundingMode rounding_mode)
return x;
auto dead_bitcount = Extractor::mantissa_bits - unbiased_exponent;
auto dead_mask = (1ull << dead_bitcount) - 1;
// Avoid shifting by the integer type's size since that's UB.
auto dead_mask = dead_bitcount == sizeof(typename Extractor::ComponentType) * 8 ? ~zero : (one << dead_bitcount) - 1;
auto dead_bits = extractor.mantissa & dead_mask;
extractor.mantissa &= ~dead_mask;