2022-02-05 01:23:27 +00:00
|
|
|
/*
|
2024-11-01 11:14:53 +00:00
|
|
|
* Copyright (c) 2022, Jelle Raaijmakers <jelle@ladybird.org>
|
2022-02-05 01:23:27 +00:00
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
|
|
|
*/
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <AK/Types.h>
|
|
|
|
|
|
|
|
namespace AK {
|
|
|
|
|
2022-10-28 16:02:03 +00:00
|
|
|
template<typename T>
|
|
|
|
union FloatExtractor;
|
|
|
|
|
2022-11-25 17:15:55 +00:00
|
|
|
#ifdef AK_HAS_FLOAT_128
|
|
|
|
template<>
|
|
|
|
union FloatExtractor<f128> {
|
2024-01-07 23:00:35 +00:00
|
|
|
using ComponentType = unsigned __int128;
|
2022-11-25 17:15:55 +00:00
|
|
|
static constexpr int mantissa_bits = 112;
|
2024-01-07 23:00:35 +00:00
|
|
|
static constexpr ComponentType mantissa_max = (((ComponentType)1) << 112) - 1;
|
2022-11-25 17:15:55 +00:00
|
|
|
static constexpr int exponent_bias = 16383;
|
|
|
|
static constexpr int exponent_bits = 15;
|
|
|
|
static constexpr unsigned exponent_max = 32767;
|
2023-10-31 22:29:31 +00:00
|
|
|
struct [[gnu::packed]] {
|
2024-07-04 09:09:20 +00:00
|
|
|
# if __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
|
|
|
|
ComponentType sign : 1;
|
|
|
|
ComponentType exponent : 15;
|
|
|
|
ComponentType mantissa : 112;
|
|
|
|
# else
|
2024-01-07 23:00:35 +00:00
|
|
|
ComponentType mantissa : 112;
|
|
|
|
ComponentType exponent : 15;
|
|
|
|
ComponentType sign : 1;
|
2024-07-04 09:09:20 +00:00
|
|
|
# endif
|
2022-11-25 17:15:55 +00:00
|
|
|
};
|
|
|
|
f128 d;
|
|
|
|
};
|
|
|
|
// Validate that f128 and the FloatExtractor union are 128 bits.
|
2023-10-31 22:29:31 +00:00
|
|
|
static_assert(AssertSize<f128, 16>());
|
|
|
|
static_assert(AssertSize<FloatExtractor<f128>, sizeof(f128)>());
|
2022-11-25 17:15:55 +00:00
|
|
|
#endif
|
|
|
|
|
2022-11-04 15:19:27 +00:00
|
|
|
#ifdef AK_HAS_FLOAT_80
|
2022-10-28 16:02:03 +00:00
|
|
|
template<>
|
2022-11-04 15:19:27 +00:00
|
|
|
union FloatExtractor<f80> {
|
2024-01-07 23:00:35 +00:00
|
|
|
using ComponentType = unsigned long long;
|
2022-10-28 16:02:03 +00:00
|
|
|
static constexpr int mantissa_bits = 64;
|
2024-01-07 23:00:35 +00:00
|
|
|
static constexpr ComponentType mantissa_max = ~0ull;
|
2022-10-28 16:02:03 +00:00
|
|
|
static constexpr int exponent_bias = 16383;
|
|
|
|
static constexpr int exponent_bits = 15;
|
|
|
|
static constexpr unsigned exponent_max = 32767;
|
2023-10-31 22:29:31 +00:00
|
|
|
struct [[gnu::packed]] {
|
2024-01-07 23:00:35 +00:00
|
|
|
// This is technically wrong: Extended floating point values really only have 63 bits of mantissa
|
|
|
|
// and an "integer bit" that behaves in various strange, unintuitive and non-IEEE-754 ways.
|
|
|
|
// However, since all bit-fiddling float code assumes IEEE floats, it cannot handle this properly.
|
|
|
|
// If we pretend that 80-bit floats are IEEE floats with 64-bit mantissas, almost everything works correctly
|
|
|
|
// and we just need a few special cases.
|
2024-07-04 09:09:20 +00:00
|
|
|
# if __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
|
|
|
|
ComponentType sign : 1;
|
|
|
|
ComponentType exponent : 15;
|
|
|
|
ComponentType mantissa : 64;
|
|
|
|
# else
|
2024-01-07 23:00:35 +00:00
|
|
|
ComponentType mantissa : 64;
|
|
|
|
ComponentType exponent : 15;
|
|
|
|
ComponentType sign : 1;
|
2024-07-04 09:09:20 +00:00
|
|
|
# endif
|
2022-10-28 16:02:03 +00:00
|
|
|
};
|
2022-11-04 15:19:27 +00:00
|
|
|
f80 d;
|
2022-10-28 16:02:03 +00:00
|
|
|
};
|
2023-10-31 22:29:31 +00:00
|
|
|
static_assert(AssertSize<FloatExtractor<f80>, sizeof(f80)>());
|
2022-10-28 16:02:03 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
template<>
|
2022-11-04 15:19:27 +00:00
|
|
|
union FloatExtractor<f64> {
|
2024-01-07 23:00:35 +00:00
|
|
|
using ComponentType = unsigned long long;
|
2022-10-28 16:02:03 +00:00
|
|
|
static constexpr int mantissa_bits = 52;
|
2024-01-07 23:00:35 +00:00
|
|
|
static constexpr ComponentType mantissa_max = (1ull << 52) - 1;
|
2022-10-28 16:02:03 +00:00
|
|
|
static constexpr int exponent_bias = 1023;
|
|
|
|
static constexpr int exponent_bits = 11;
|
|
|
|
static constexpr unsigned exponent_max = 2047;
|
2023-10-31 22:29:31 +00:00
|
|
|
struct [[gnu::packed]] {
|
|
|
|
// FIXME: These types have to all be the same, otherwise this struct
|
|
|
|
// goes from being a bitfield describing the layout of an f64
|
|
|
|
// into being a multibyte mess on windows.
|
|
|
|
// Technically, '-mno-ms-bitfields' is supposed to disable this
|
|
|
|
// very intuitive and portable behaviour on windows, but it doesn't
|
|
|
|
// work with the msvc ABI.
|
|
|
|
// See <https://github.com/llvm/llvm-project/issues/24757>
|
2024-07-04 09:09:20 +00:00
|
|
|
#if __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
|
|
|
|
ComponentType sign : 1;
|
|
|
|
ComponentType exponent : 11;
|
|
|
|
ComponentType mantissa : 52;
|
|
|
|
#else
|
2024-01-07 23:00:35 +00:00
|
|
|
ComponentType mantissa : 52;
|
|
|
|
ComponentType exponent : 11;
|
|
|
|
ComponentType sign : 1;
|
2024-07-04 09:09:20 +00:00
|
|
|
#endif
|
2022-10-28 16:02:03 +00:00
|
|
|
};
|
2022-11-04 15:19:27 +00:00
|
|
|
f64 d;
|
2022-10-28 16:02:03 +00:00
|
|
|
};
|
2023-10-31 22:29:31 +00:00
|
|
|
static_assert(AssertSize<FloatExtractor<f64>, sizeof(f64)>());
|
2022-10-28 16:02:03 +00:00
|
|
|
|
|
|
|
template<>
|
2022-11-04 15:19:27 +00:00
|
|
|
union FloatExtractor<f32> {
|
2024-01-07 23:00:35 +00:00
|
|
|
using ComponentType = unsigned;
|
2022-10-28 16:02:03 +00:00
|
|
|
static constexpr int mantissa_bits = 23;
|
2024-01-07 23:00:35 +00:00
|
|
|
static constexpr ComponentType mantissa_max = (1 << 23) - 1;
|
2022-10-28 16:02:03 +00:00
|
|
|
static constexpr int exponent_bias = 127;
|
|
|
|
static constexpr int exponent_bits = 8;
|
2024-01-07 23:00:35 +00:00
|
|
|
static constexpr ComponentType exponent_max = 255;
|
2023-10-31 22:29:31 +00:00
|
|
|
struct [[gnu::packed]] {
|
2024-07-04 09:09:20 +00:00
|
|
|
#if __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
|
|
|
|
ComponentType sign : 1;
|
|
|
|
ComponentType exponent : 8;
|
|
|
|
ComponentType mantissa : 23;
|
|
|
|
#else
|
2024-01-07 23:00:35 +00:00
|
|
|
ComponentType mantissa : 23;
|
|
|
|
ComponentType exponent : 8;
|
|
|
|
ComponentType sign : 1;
|
2024-07-04 09:09:20 +00:00
|
|
|
#endif
|
2022-10-28 16:02:03 +00:00
|
|
|
};
|
2022-11-04 15:19:27 +00:00
|
|
|
f32 d;
|
2022-10-28 16:02:03 +00:00
|
|
|
};
|
2023-10-31 22:29:31 +00:00
|
|
|
static_assert(AssertSize<FloatExtractor<f32>, sizeof(f32)>());
|
2022-10-28 16:02:03 +00:00
|
|
|
|
2022-02-05 01:23:27 +00:00
|
|
|
}
|
|
|
|
|
2022-11-26 11:18:30 +00:00
|
|
|
#if USING_AK_GLOBALLY
|
2022-10-28 16:02:03 +00:00
|
|
|
using AK::FloatExtractor;
|
2022-11-26 11:18:30 +00:00
|
|
|
#endif
|