compilation fixes for std::bfloat16_t and std::float16_t. Sorry for this, my compilers don't supports it.

additional type usage fixes and constexpr.
This commit is contained in:
IRainman 2025-05-06 16:53:30 +03:00
parent b2ea7bcaab
commit afbb803aa4
3 changed files with 68 additions and 65 deletions

View File

@ -17,7 +17,7 @@ namespace fast_float {
// most significant bits and the low part corresponding to the least significant // most significant bits and the low part corresponding to the least significant
// bits. // bits.
// //
template <uint_fast8_t bit_precision> template <limb_t bit_precision>
fastfloat_really_inline FASTFLOAT_CONSTEXPR20 value128 fastfloat_really_inline FASTFLOAT_CONSTEXPR20 value128
compute_product_approximation(int64_t q, uint64_t w) noexcept { compute_product_approximation(int64_t q, uint64_t w) noexcept {
int const index = 2 * int(q - powers::smallest_power_of_five); int const index = 2 * int(q - powers::smallest_power_of_five);
@ -62,7 +62,7 @@ namespace detail {
* where * where
* p = log(5**-q)/log(2) = -q * log(5)/log(2) * p = log(5**-q)/log(2) = -q * log(5)/log(2)
*/ */
constexpr fastfloat_really_inline int32_t power(int32_t q) noexcept { constexpr fastfloat_really_inline am_pow_t power(am_pow_t q) noexcept {
return (((152170 + 65536) * q) >> 16) + 63; return (((152170 + 65536) * q) >> 16) + 63;
} }
} // namespace detail } // namespace detail
@ -72,11 +72,12 @@ constexpr fastfloat_really_inline int32_t power(int32_t q) noexcept {
template <typename binary> template <typename binary>
fastfloat_really_inline FASTFLOAT_CONSTEXPR14 adjusted_mantissa fastfloat_really_inline FASTFLOAT_CONSTEXPR14 adjusted_mantissa
compute_error_scaled(int64_t q, uint64_t w, int32_t lz) noexcept { compute_error_scaled(int64_t q, uint64_t w, int32_t lz) noexcept {
int32_t hilz = int32_t(w >> 63) ^ 1; am_pow_t hilz = uint64_t(w >> 63) ^ 1;
adjusted_mantissa answer; adjusted_mantissa answer;
answer.mantissa = w << hilz; answer.mantissa = w << hilz;
int32_t bias = binary::mantissa_explicit_bits() - binary::minimum_exponent(); constexpr am_pow_t bias =
answer.power2 = am_pow_t(detail::power(int32_t(q)) + bias - hilz - lz - 62 + binary::mantissa_explicit_bits() - binary::minimum_exponent();
answer.power2 = am_pow_t(detail::power(am_pow_t(q)) + bias - hilz - lz - 62 +
invalid_am_bias); invalid_am_bias);
return answer; return answer;
} }
@ -86,7 +87,7 @@ compute_error_scaled(int64_t q, uint64_t w, int32_t lz) noexcept {
template <typename binary> template <typename binary>
fastfloat_really_inline FASTFLOAT_CONSTEXPR20 adjusted_mantissa fastfloat_really_inline FASTFLOAT_CONSTEXPR20 adjusted_mantissa
compute_error(int64_t q, uint64_t w) noexcept { compute_error(int64_t q, uint64_t w) noexcept {
int lz = leading_zeroes(w); limb_t lz = leading_zeroes(w);
w <<= lz; w <<= lz;
value128 product = value128 product =
compute_product_approximation<binary::mantissa_explicit_bits() + 3>(q, w); compute_product_approximation<binary::mantissa_explicit_bits() + 3>(q, w);
@ -118,7 +119,7 @@ compute_float(int64_t q, uint64_t w) noexcept {
// powers::largest_power_of_five]. // powers::largest_power_of_five].
// We want the most significant bit of i to be 1. Shift if needed. // We want the most significant bit of i to be 1. Shift if needed.
int lz = leading_zeroes(w); limb_t lz = leading_zeroes(w);
w <<= lz; w <<= lz;
// The required precision is binary::mantissa_explicit_bits() + 3 because // The required precision is binary::mantissa_explicit_bits() + 3 because
@ -138,12 +139,12 @@ compute_float(int64_t q, uint64_t w) noexcept {
// branchless approach: value128 product = compute_product(q, w); but in // branchless approach: value128 product = compute_product(q, w); but in
// practice, we can win big with the compute_product_approximation if its // practice, we can win big with the compute_product_approximation if its
// additional branch is easily predicted. Which is best is data specific. // additional branch is easily predicted. Which is best is data specific.
int upperbit = int(product.high >> 63); limb_t upperbit = limb_t(product.high >> 63);
int shift = upperbit + 64 - binary::mantissa_explicit_bits() - 3; limb_t shift = upperbit + 64 - binary::mantissa_explicit_bits() - 3;
answer.mantissa = product.high >> shift; answer.mantissa = product.high >> shift;
answer.power2 = am_pow_t(detail::power(int32_t(q)) + upperbit - lz - answer.power2 = am_pow_t(detail::power(am_pow_t(q)) + upperbit - lz -
binary::minimum_exponent()); binary::minimum_exponent());
if (answer.power2 <= 0) { // we have a subnormal or very small value. if (answer.power2 <= 0) { // we have a subnormal or very small value.
// Here have that answer.power2 <= 0 so -answer.power2 >= 0 // Here have that answer.power2 <= 0 so -answer.power2 >= 0

View File

@ -68,7 +68,7 @@ to_extended(T const &value) noexcept {
constexpr equiv_uint hidden_bit_mask = binary_format<T>::hidden_bit_mask(); constexpr equiv_uint hidden_bit_mask = binary_format<T>::hidden_bit_mask();
adjusted_mantissa am; adjusted_mantissa am;
am_pow_t bias = binary_format<T>::mantissa_explicit_bits() - constexpr am_pow_t bias = binary_format<T>::mantissa_explicit_bits() -
binary_format<T>::minimum_exponent(); binary_format<T>::minimum_exponent();
equiv_uint bits; equiv_uint bits;
#if FASTFLOAT_HAS_BIT_CAST #if FASTFLOAT_HAS_BIT_CAST
@ -112,7 +112,8 @@ to_extended_halfway(T const &value) noexcept {
template <typename T, typename callback> template <typename T, typename callback>
fastfloat_really_inline FASTFLOAT_CONSTEXPR14 void round(adjusted_mantissa &am, fastfloat_really_inline FASTFLOAT_CONSTEXPR14 void round(adjusted_mantissa &am,
callback cb) noexcept { callback cb) noexcept {
am_pow_t mantissa_shift = 64 - binary_format<T>::mantissa_explicit_bits() - 1; constexpr am_pow_t mantissa_shift =
64 - binary_format<T>::mantissa_explicit_bits() - 1;
if (-am.power2 >= mantissa_shift) { if (-am.power2 >= mantissa_shift) {
// have a denormal float // have a denormal float
am_pow_t shift = -am.power2 + 1; am_pow_t shift = -am.power2 + 1;
@ -352,7 +353,7 @@ inline FASTFLOAT_CONSTEXPR20 adjusted_mantissa positive_digit_comp(
FASTFLOAT_ASSERT(bigmant.pow10(exponent)); FASTFLOAT_ASSERT(bigmant.pow10(exponent));
bool truncated; bool truncated;
am.mantissa = bigmant.hi64(truncated); am.mantissa = bigmant.hi64(truncated);
am_pow_t bias = binary_format<T>::mantissa_explicit_bits() - constexpr am_pow_t bias = binary_format<T>::mantissa_explicit_bits() -
binary_format<T>::minimum_exponent(); binary_format<T>::minimum_exponent();
am.power2 = bigmant.bit_length() - 64 + bias; am.power2 = bigmant.bit_length() - 64 + bias;

View File

@ -445,8 +445,8 @@ full_multiplication(uint64_t a, uint64_t b) noexcept {
// Value of the mantissa. // Value of the mantissa.
typedef uint_fast64_t am_mant_t; typedef uint_fast64_t am_mant_t;
// Size of bits in the mantissa. // Size of bits in the mantissa and path and roundings shifts
typedef uint_fast8_t am_bits_t; typedef int_fast8_t am_bits_t;
// Power bias is signed for handling a denormal float // Power bias is signed for handling a denormal float
// or an invalid mantissa. // or an invalid mantissa.
@ -481,17 +481,17 @@ template <typename T> struct binary_format : binary_format_lookup_tables<T> {
static constexpr am_pow_t minimum_exponent(); static constexpr am_pow_t minimum_exponent();
static constexpr am_pow_t infinite_power(); static constexpr am_pow_t infinite_power();
static constexpr am_bits_t sign_index(); static constexpr am_bits_t sign_index();
static constexpr am_pow_t static constexpr am_bits_t
min_exponent_fast_path(); // used when fegetround() == FE_TONEAREST min_exponent_fast_path(); // used when fegetround() == FE_TONEAREST
static constexpr am_pow_t max_exponent_fast_path(); static constexpr am_bits_t max_exponent_fast_path();
static constexpr am_pow_t max_exponent_round_to_even(); static constexpr am_bits_t max_exponent_round_to_even();
static constexpr am_pow_t min_exponent_round_to_even(); static constexpr am_bits_t min_exponent_round_to_even();
static constexpr equiv_uint max_mantissa_fast_path(int64_t power); static constexpr equiv_uint max_mantissa_fast_path(am_pow_t power);
static constexpr equiv_uint static constexpr equiv_uint
max_mantissa_fast_path(); // used when fegetround() == FE_TONEAREST max_mantissa_fast_path(); // used when fegetround() == FE_TONEAREST
static constexpr am_pow_t largest_power_of_ten(); static constexpr am_pow_t largest_power_of_ten();
static constexpr am_pow_t smallest_power_of_ten(); static constexpr am_pow_t smallest_power_of_ten();
static constexpr T exact_power_of_ten(int64_t power); static constexpr T exact_power_of_ten(am_pow_t power);
static constexpr am_digits max_digits(); static constexpr am_digits max_digits();
static constexpr equiv_uint exponent_mask(); static constexpr equiv_uint exponent_mask();
static constexpr equiv_uint mantissa_mask(); static constexpr equiv_uint mantissa_mask();
@ -582,7 +582,7 @@ constexpr uint32_t binary_format_lookup_tables<float, U>::max_mantissa[];
#endif #endif
template <> template <>
inline constexpr am_pow_t binary_format<double>::min_exponent_fast_path() { inline constexpr am_bits_t binary_format<double>::min_exponent_fast_path() {
#if (FLT_EVAL_METHOD != 1) && (FLT_EVAL_METHOD != 0) #if (FLT_EVAL_METHOD != 1) && (FLT_EVAL_METHOD != 0)
return 0; return 0;
#else #else
@ -591,7 +591,7 @@ inline constexpr am_pow_t binary_format<double>::min_exponent_fast_path() {
} }
template <> template <>
inline constexpr am_pow_t binary_format<float>::min_exponent_fast_path() { inline constexpr am_bits_t binary_format<float>::min_exponent_fast_path() {
#if (FLT_EVAL_METHOD != 1) && (FLT_EVAL_METHOD != 0) #if (FLT_EVAL_METHOD != 1) && (FLT_EVAL_METHOD != 0)
return 0; return 0;
#else #else
@ -610,22 +610,22 @@ inline constexpr am_bits_t binary_format<float>::mantissa_explicit_bits() {
} }
template <> template <>
inline constexpr am_pow_t binary_format<double>::max_exponent_round_to_even() { inline constexpr am_bits_t binary_format<double>::max_exponent_round_to_even() {
return 23; return 23;
} }
template <> template <>
inline constexpr am_pow_t binary_format<float>::max_exponent_round_to_even() { inline constexpr am_bits_t binary_format<float>::max_exponent_round_to_even() {
return 10; return 10;
} }
template <> template <>
inline constexpr am_pow_t binary_format<double>::min_exponent_round_to_even() { inline constexpr am_bits_t binary_format<double>::min_exponent_round_to_even() {
return -4; return -4;
} }
template <> template <>
inline constexpr am_pow_t binary_format<float>::min_exponent_round_to_even() { inline constexpr am_bits_t binary_format<float>::min_exponent_round_to_even() {
return -17; return -17;
} }
@ -659,12 +659,12 @@ template <> inline constexpr am_bits_t binary_format<float>::sign_index() {
#endif #endif
template <> template <>
inline constexpr am_pow_t binary_format<double>::max_exponent_fast_path() { inline constexpr am_bits_t binary_format<double>::max_exponent_fast_path() {
return 22; return 22;
} }
template <> template <>
inline constexpr am_pow_t binary_format<float>::max_exponent_fast_path() { inline constexpr am_bits_t binary_format<float>::max_exponent_fast_path() {
return 10; return 10;
} }
@ -704,7 +704,7 @@ constexpr uint16_t
template <> template <>
inline constexpr std::float16_t inline constexpr std::float16_t
binary_format<std::float16_t>::exact_power_of_ten(int64_t power) { binary_format<std::float16_t>::exact_power_of_ten(am_pow_t power) {
// Work around clang bug https://godbolt.org/z/zedh7rrhc // Work around clang bug https://godbolt.org/z/zedh7rrhc
return (void)powers_of_ten[0], powers_of_ten[power]; return (void)powers_of_ten[0], powers_of_ten[power];
} }
@ -728,52 +728,52 @@ binary_format<std::float16_t>::hidden_bit_mask() {
} }
template <> template <>
inline constexpr int8_t inline constexpr am_bits_t
binary_format<std::float16_t>::max_exponent_fast_path() { binary_format<std::float16_t>::max_exponent_fast_path() {
return 4; return 4;
} }
template <> template <>
inline constexpr uint8_t inline constexpr am_bits_t
binary_format<std::float16_t>::mantissa_explicit_bits() { binary_format<std::float16_t>::mantissa_explicit_bits() {
return 10; return 10;
} }
template <> template <>
inline constexpr uint64_t inline constexpr binary_format<std::float16_t>::equiv_uint
binary_format<std::float16_t>::max_mantissa_fast_path(int64_t power) { binary_format<std::float16_t>::max_mantissa_fast_path(am_pow_t power) {
// caller is responsible to ensure that // caller is responsible to ensure that
// power >= 0 && power <= 4 FASTFLOAT_ASSUME(power >= 0 && power <= 4);
// //
// Work around clang bug https://godbolt.org/z/zedh7rrhc // Work around clang bug https://godbolt.org/z/zedh7rrhc
return (void)max_mantissa[0], max_mantissa[power]; return (void)max_mantissa[0], max_mantissa[power];
} }
template <> template <>
inline constexpr int8_t inline constexpr am_bits_t
binary_format<std::float16_t>::min_exponent_fast_path() { binary_format<std::float16_t>::min_exponent_fast_path() {
return 0; return 0;
} }
template <> template <>
inline constexpr int16_t inline constexpr am_bits_t
binary_format<std::float16_t>::max_exponent_round_to_even() { binary_format<std::float16_t>::max_exponent_round_to_even() {
return 5; return 5;
} }
template <> template <>
inline constexpr int16_t inline constexpr am_bits_t
binary_format<std::float16_t>::min_exponent_round_to_even() { binary_format<std::float16_t>::min_exponent_round_to_even() {
return -22; return -22;
} }
template <> template <>
inline constexpr am_exp_t binary_format<std::float16_t>::minimum_exponent() { inline constexpr am_pow_t binary_format<std::float16_t>::minimum_exponent() {
return -15; return -15;
} }
template <> template <>
inline constexpr am_exp_t binary_format<std::float16_t>::infinite_power() { inline constexpr am_pow_t binary_format<std::float16_t>::infinite_power() {
return 0x1F; return 0x1F;
} }
@ -787,13 +787,13 @@ inline constexpr am_bits_t binary_format<std::float16_t>::sign_index() {
#endif #endif
template <> template <>
inline constexpr am_exp_t inline constexpr am_pow_t
binary_format<std::float16_t>::largest_power_of_ten() { binary_format<std::float16_t>::largest_power_of_ten() {
return 4; return 4;
} }
template <> template <>
inline constexpr am_exp_t inline constexpr am_pow_t
binary_format<std::float16_t>::smallest_power_of_ten() { binary_format<std::float16_t>::smallest_power_of_ten() {
return -27; return -27;
} }
@ -812,7 +812,7 @@ template <typename U> struct binary_format_lookup_tables<std::bfloat16_t, U> {
// Largest integer value v so that (5**index * v) <= 1<<8. // Largest integer value v so that (5**index * v) <= 1<<8.
// 0x100 == 1<<8 // 0x100 == 1<<8
static constexpr uint64_t max_mantissa[] = {0x100, 0x100 / 5, 0x100 / (5 * 5), static constexpr uint16_t max_mantissa[] = {0x100, 0x100 / 5, 0x100 / (5 * 5),
0x100 / (5 * 5 * 5), 0x100 / (5 * 5 * 5),
0x100 / (5 * 5 * 5 * 5)}; 0x100 / (5 * 5 * 5 * 5)};
}; };
@ -831,13 +831,13 @@ constexpr uint64_t
template <> template <>
inline constexpr std::bfloat16_t inline constexpr std::bfloat16_t
binary_format<std::bfloat16_t>::exact_power_of_ten(int64_t power) { binary_format<std::bfloat16_t>::exact_power_of_ten(am_pow_t power) {
// Work around clang bug https://godbolt.org/z/zedh7rrhc // Work around clang bug https://godbolt.org/z/zedh7rrhc
return (void)powers_of_ten[0], powers_of_ten[power]; return (void)powers_of_ten[0], powers_of_ten[power];
} }
template <> template <>
inline constexpr int8_t inline constexpr am_bits_t
binary_format<std::bfloat16_t>::max_exponent_fast_path() { binary_format<std::bfloat16_t>::max_exponent_fast_path() {
return 3; return 3;
} }
@ -861,66 +861,66 @@ binary_format<std::bfloat16_t>::hidden_bit_mask() {
} }
template <> template <>
inline constexpr uint8_t inline constexpr am_bits_t
binary_format<std::bfloat16_t>::mantissa_explicit_bits() { binary_format<std::bfloat16_t>::mantissa_explicit_bits() {
return 7; return 7;
} }
template <> template <>
inline constexpr uint64_t inline constexpr binary_format<std::bfloat16_t>::equiv_uint
binary_format<std::bfloat16_t>::max_mantissa_fast_path(int64_t power) { binary_format<std::bfloat16_t>::max_mantissa_fast_path(am_pow_t power) {
// caller is responsible to ensure that // caller is responsible to ensure that
// power >= 0 && power <= 3 FASTFLOAT_ASSUME(power >= 0 && power <= 3);
// //
// Work around clang bug https://godbolt.org/z/zedh7rrhc // Work around clang bug https://godbolt.org/z/zedh7rrhc
return (void)max_mantissa[0], max_mantissa[power]; return (void)max_mantissa[0], max_mantissa[power];
} }
template <> template <>
inline constexpr int8_t inline constexpr am_bits_t
binary_format<std::bfloat16_t>::min_exponent_fast_path() { binary_format<std::bfloat16_t>::min_exponent_fast_path() {
return 0; return 0;
} }
template <> template <>
inline constexpr am_exp_t inline constexpr am_bits_t
binary_format<std::bfloat16_t>::max_exponent_round_to_even() { binary_format<std::bfloat16_t>::max_exponent_round_to_even() {
return 3; return 3;
} }
template <> template <>
inline constexpr am_exp_t inline constexpr am_bits_t
binary_format<std::bfloat16_t>::min_exponent_round_to_even() { binary_format<std::bfloat16_t>::min_exponent_round_to_even() {
return -24; return -24;
} }
template <> template <>
inline constexpr am_exp_t binary_format<std::bfloat16_t>::minimum_exponent() { inline constexpr am_pow_t binary_format<std::bfloat16_t>::minimum_exponent() {
return -127; return -127;
} }
template <> template <>
inline constexpr am_exp_t binary_format<std::bfloat16_t>::infinite_power() { inline constexpr am_pow_t binary_format<std::bfloat16_t>::infinite_power() {
return 0xFF; return 0xFF;
} }
#ifndef FASTFLOAT_ONLY_POSITIVE_C_NUMBER_WO_INF_NAN #ifndef FASTFLOAT_ONLY_POSITIVE_C_NUMBER_WO_INF_NAN
template <> template <>
inline constexpr uint8_t binary_format<std::bfloat16_t>::sign_index() { inline constexpr am_bits_t binary_format<std::bfloat16_t>::sign_index() {
return 15; return 15;
} }
#endif #endif
template <> template <>
inline constexpr am_exp_t inline constexpr am_pow_t
binary_format<std::bfloat16_t>::largest_power_of_ten() { binary_format<std::bfloat16_t>::largest_power_of_ten() {
return 38; return 38;
} }
template <> template <>
inline constexpr am_exp_t inline constexpr am_pow_t
binary_format<std::bfloat16_t>::smallest_power_of_ten() { binary_format<std::bfloat16_t>::smallest_power_of_ten() {
return -60; return -60;
} }
@ -932,8 +932,8 @@ inline constexpr uint16_t binary_format<std::bfloat16_t>::max_digits() {
#endif // __STDCPP_BFLOAT16_T__ #endif // __STDCPP_BFLOAT16_T__
template <> template <>
inline constexpr uint64_t inline constexpr binary_format<double>::equiv_uint
binary_format<double>::max_mantissa_fast_path(int64_t power) { binary_format<double>::max_mantissa_fast_path(am_pow_t power) {
// caller is responsible to ensure that // caller is responsible to ensure that
FASTFLOAT_ASSUME(power >= 0 && power <= 22); FASTFLOAT_ASSUME(power >= 0 && power <= 22);
// //
@ -942,8 +942,8 @@ binary_format<double>::max_mantissa_fast_path(int64_t power) {
} }
template <> template <>
inline constexpr uint32_t inline constexpr binary_format<float>::equiv_uint
binary_format<float>::max_mantissa_fast_path(int64_t power) { binary_format<float>::max_mantissa_fast_path(am_pow_t power) {
// caller is responsible to ensure that // caller is responsible to ensure that
FASTFLOAT_ASSUME(power >= 0 && power <= 10); FASTFLOAT_ASSUME(power >= 0 && power <= 10);
// //
@ -953,7 +953,7 @@ binary_format<float>::max_mantissa_fast_path(int64_t power) {
template <> template <>
inline constexpr double inline constexpr double
binary_format<double>::exact_power_of_ten(int64_t power) { binary_format<double>::exact_power_of_ten(am_pow_t power) {
// caller is responsible to ensure that // caller is responsible to ensure that
FASTFLOAT_ASSUME(power >= 0 && power <= 22); FASTFLOAT_ASSUME(power >= 0 && power <= 22);
// //
@ -962,7 +962,8 @@ binary_format<double>::exact_power_of_ten(int64_t power) {
} }
template <> template <>
inline constexpr float binary_format<float>::exact_power_of_ten(int64_t power) { inline constexpr float
binary_format<float>::exact_power_of_ten(am_pow_t power) {
// caller is responsible to ensure that // caller is responsible to ensure that
FASTFLOAT_ASSUME(power >= 0 && power <= 10); FASTFLOAT_ASSUME(power >= 0 && power <= 10);
// //