Skip to content

Commit 8cee025

Browse files
committed
add powers_of_ten/max_mantissa for float16_t/bfloat16_t
1 parent 6f8fd67 commit 8cee025

File tree

1 file changed

+29
-8
lines changed

1 file changed

+29
-8
lines changed

include/fast_float/float_common.h

Lines changed: 29 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -713,8 +713,17 @@ inline constexpr uint64_t binary_format<double>::max_mantissa_fast_path() {
713713
// credit: Jakub Jelínek
714714
#ifdef __STDCPP_FLOAT16_T__
715715
template <typename U> struct binary_format_lookup_tables<std::float16_t, U> {
716-
static constexpr std::float16_t powers_of_ten[] = {1}; // todo: fix this
717-
static constexpr uint64_t max_mantissa[] = {1}; // todo: fix this
716+
static constexpr std::float16_t powers_of_ten[] = {1e0f16, 1e1f16, 1e2f16,
717+
1e3f16, 1e4f16};
718+
719+
// Largest integer value v so that (5**index * v) <= 1<<11.
720+
// 0x800 == 1<<11
721+
static constexpr uint64_t max_mantissa[] = {0x800,
722+
0x800 / 5,
723+
0x800 / (5 * 5),
724+
0x800 / (5 * 5 * 5),
725+
0x800 / (5 * 5 * 5 * 5),
726+
0x800 / (constant_55555)};
718727
};
719728

720729
#if FASTFLOAT_DETAIL_MUST_DEFINE_CONSTEXPR_VARIABLE
@@ -756,18 +765,21 @@ binary_format<std::float16_t>::hidden_bit_mask() {
756765

757766
template <>
758767
inline constexpr int binary_format<std::float16_t>::max_exponent_fast_path() {
759-
return 0;
768+
return 4;
760769
}
761770

762771
template <>
763772
inline constexpr uint64_t
764773
binary_format<std::float16_t>::max_mantissa_fast_path() {
765-
return 0;
774+
return uint64_t(2) << mantissa_explicit_bits();
766775
}
767776

768777
template <>
769778
inline constexpr uint64_t
770779
binary_format<std::float16_t>::max_mantissa_fast_path(int64_t power) {
780+
// caller is responsible to ensure that
781+
// power >= 0 && power <= 4
782+
//
771783
// Work around clang bug https://godbolt.org/z/zedh7rrhc
772784
return (void)max_mantissa[0], max_mantissa[power];
773785
}
@@ -823,8 +835,14 @@ template <> constexpr size_t binary_format<std::float16_t>::max_digits() {
823835
// credit: Jakub Jelínek
824836
#ifdef __STDCPP_BFLOAT16_T__
825837
template <typename U> struct binary_format_lookup_tables<std::bfloat16_t, U> {
826-
static constexpr std::bfloat16_t powers_of_ten[] = {1}; // todo: fix this
827-
static constexpr uint64_t max_mantissa[] = {1}; // todo: fix this
838+
static constexpr std::bfloat16_t powers_of_ten[] = {1e0bf16, 1e1bf16, 1e2bf16,
839+
1e3bf16};
840+
841+
// Largest integer value v so that (5**index * v) <= 1<<8.
842+
// 0x100 == 1<<8
843+
static constexpr uint64_t max_mantissa[] = {0x100, 0x100 / 5, 0x100 / (5 * 5),
844+
0x100 / (5 * 5 * 5),
845+
0x100 / (5 * 5 * 5 * 5)};
828846
};
829847

830848
#if FASTFLOAT_DETAIL_MUST_DEFINE_CONSTEXPR_VARIABLE
@@ -848,7 +866,7 @@ binary_format<std::bfloat16_t>::exact_power_of_ten(int64_t power) {
848866

849867
template <>
850868
inline constexpr int binary_format<std::bfloat16_t>::max_exponent_fast_path() {
851-
return 0;
869+
return 3;
852870
}
853871

854872
template <>
@@ -872,12 +890,15 @@ binary_format<std::bfloat16_t>::hidden_bit_mask() {
872890
template <>
873891
inline constexpr uint64_t
874892
binary_format<std::bfloat16_t>::max_mantissa_fast_path() {
875-
return 0;
893+
return uint64_t(2) << mantissa_explicit_bits();
876894
}
877895

878896
template <>
879897
inline constexpr uint64_t
880898
binary_format<std::bfloat16_t>::max_mantissa_fast_path(int64_t power) {
899+
// caller is responsible to ensure that
900+
// power >= 0 && power <= 3
901+
//
881902
// Work around clang bug https://godbolt.org/z/zedh7rrhc
882903
return (void)max_mantissa[0], max_mantissa[power];
883904
}

0 commit comments

Comments
 (0)