1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-14 08:54:58 +00:00

AK+LibC+LibCrypto: Move FloatExtractor to AK/FloatingPoint.h

This commit is contained in:
Dan Klishch 2022-10-28 12:02:03 -04:00 committed by Andrew Kaster
parent 2d75229192
commit 17c9a3e8d9
3 changed files with 80 additions and 99 deletions

View file

@ -9,6 +9,7 @@
#include <AK/BuiltinWrappers.h>
#include <AK/ExtraMathConstants.h>
#include <AK/FloatingPoint.h>
#ifndef AK_ARCH_AARCH64
# include <AK/FPControl.h>
#endif
@ -61,57 +62,6 @@ enum class RoundingMode {
ToEven = FE_TONEAREST
};
template<typename T>
union FloatExtractor;
#if ARCH(I386) || ARCH(X86_64) || ARCH(AARCH64)
// This assumes long double is 80 bits, which is true with GCC on Intel platforms
template<>
union FloatExtractor<long double> {
static constexpr int mantissa_bits = 64;
static constexpr unsigned long long mantissa_max = ~0u;
static constexpr int exponent_bias = 16383;
static constexpr int exponent_bits = 15;
static constexpr unsigned exponent_max = 32767;
struct {
unsigned long long mantissa;
unsigned exponent : 15;
unsigned sign : 1;
};
long double d;
};
#endif
template<>
union FloatExtractor<double> {
static constexpr int mantissa_bits = 52;
static constexpr unsigned long long mantissa_max = (1ull << 52) - 1;
static constexpr int exponent_bias = 1023;
static constexpr int exponent_bits = 11;
static constexpr unsigned exponent_max = 2047;
struct {
unsigned long long mantissa : 52;
unsigned exponent : 11;
unsigned sign : 1;
};
double d;
};
template<>
union FloatExtractor<float> {
static constexpr int mantissa_bits = 23;
static constexpr unsigned mantissa_max = (1 << 23) - 1;
static constexpr int exponent_bias = 127;
static constexpr int exponent_bits = 8;
static constexpr unsigned exponent_max = 255;
struct {
unsigned long long mantissa : 23;
unsigned exponent : 8;
unsigned sign : 1;
};
float d;
};
// This is much branchier than it really needs to be
template<typename FloatType>
static FloatType internal_to_integer(FloatType x, RoundingMode rounding_mode)