AK: Make FloatExtractor use bit_cast<>() instead of a union

The motivation is to allow functions that use FloatExtractor to be
constexpr.  Type punning through a union will never work in constexpr.

In practice, bit_cast<>()ing bit fields also does not yet work in clang,
but that's just a bug and it will work eventually (and it does already
work in gcc): https://github.com/llvm/llvm-project/issues/54018

No behavior change.
This commit is contained in:
Nico Weber
2025-01-01 14:10:26 -05:00
parent 39a2356c54
commit 48a28cffd5
5 changed files with 77 additions and 79 deletions

View File

@@ -49,8 +49,7 @@ UnsignedBigInteger::UnsignedBigInteger(double value)
return;
}
FloatExtractor<double> extractor;
extractor.d = value;
auto extractor = FloatExtractor<double>::from_float(value);
VERIFY(!extractor.sign);
i32 real_exponent = extractor.exponent - extractor.exponent_bias;
@@ -350,7 +349,7 @@ double UnsignedBigInteger::to_double(UnsignedBigInteger::RoundingMode rounding_m
VERIFY((mantissa & 0xfff0000000000000) == 0);
extractor.mantissa = mantissa;
return extractor.d;
return extractor.to_float();
}
void UnsignedBigInteger::set_to_0()
@@ -638,8 +637,7 @@ UnsignedBigInteger::CompareResult UnsignedBigInteger::compare_to_double(double v
if (is_zero())
return CompareResult::DoubleGreaterThanBigInt;
FloatExtractor<double> extractor;
extractor.d = value;
auto extractor = FloatExtractor<double>::from_float(value);
// Value cannot be negative at this point.
VERIFY(extractor.sign == 0);