LibC: Fix extended floating point software rounding

Contrary to IEEE formats, x86's 80-bit extended floats need the topmost
mantissa bit to be 1 or else a special kind of NaN is produced. We fix
this by reinserting the special bit if necessary.
This commit is contained in:
kleines Filmröllchen 2024-01-08 14:12:33 +01:00 committed by Andrew Kaster
commit e74b34b69e
Notes: sideshowbarker 2024-07-17 04:09:56 +09:00

View file

@ -99,6 +99,13 @@ static FloatType internal_to_integer(FloatType x, RoundingMode rounding_mode)
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;
#ifdef AK_HAS_FLOAT_80
if constexpr (IsSame<f80, FloatType>) {
// x86 80-bit extended floating point requires the top mantissa bit to always be 1, or we get a special Intel NaN.
if (extractor.mantissa == 0)
extractor.mantissa = one << (Extractor::mantissa_bits - 1);
}
#endif
auto nonhalf_fraction_mask = dead_mask >> 1;
has_nonhalf_fraction = (dead_bits & nonhalf_fraction_mask) != 0;