@@ -713,8 +713,17 @@ inline constexpr uint64_t binary_format<double>::max_mantissa_fast_path() {
713
713
// credit: Jakub Jelínek
714
714
#ifdef __STDCPP_FLOAT16_T__
715
715
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)};
718
727
};
719
728
720
729
#if FASTFLOAT_DETAIL_MUST_DEFINE_CONSTEXPR_VARIABLE
@@ -756,18 +765,21 @@ binary_format<std::float16_t>::hidden_bit_mask() {
756
765
757
766
template <>
758
767
inline constexpr int binary_format<std::float16_t >::max_exponent_fast_path() {
759
- return 0 ;
768
+ return 4 ;
760
769
}
761
770
762
771
template <>
763
772
inline constexpr uint64_t
764
773
binary_format<std::float16_t >::max_mantissa_fast_path() {
765
- return 0 ;
774
+ return uint64_t ( 2 ) << mantissa_explicit_bits () ;
766
775
}
767
776
768
777
template <>
769
778
inline constexpr uint64_t
770
779
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
+ //
771
783
// Work around clang bug https://godbolt.org/z/zedh7rrhc
772
784
return (void )max_mantissa[0 ], max_mantissa[power];
773
785
}
@@ -823,8 +835,14 @@ template <> constexpr size_t binary_format<std::float16_t>::max_digits() {
823
835
// credit: Jakub Jelínek
824
836
#ifdef __STDCPP_BFLOAT16_T__
825
837
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 )};
828
846
};
829
847
830
848
#if FASTFLOAT_DETAIL_MUST_DEFINE_CONSTEXPR_VARIABLE
@@ -848,7 +866,7 @@ binary_format<std::bfloat16_t>::exact_power_of_ten(int64_t power) {
848
866
849
867
template <>
850
868
inline constexpr int binary_format<std::bfloat16_t >::max_exponent_fast_path() {
851
- return 0 ;
869
+ return 3 ;
852
870
}
853
871
854
872
template <>
@@ -872,12 +890,15 @@ binary_format<std::bfloat16_t>::hidden_bit_mask() {
872
890
template <>
873
891
inline constexpr uint64_t
874
892
binary_format<std::bfloat16_t >::max_mantissa_fast_path() {
875
- return 0 ;
893
+ return uint64_t ( 2 ) << mantissa_explicit_bits () ;
876
894
}
877
895
878
896
template <>
879
897
inline constexpr uint64_t
880
898
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
+ //
881
902
// Work around clang bug https://godbolt.org/z/zedh7rrhc
882
903
return (void )max_mantissa[0 ], max_mantissa[power];
883
904
}
0 commit comments