Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-05-03 08:13:50

0001 // -*- C++ -*-
0002 //===----------------------------------------------------------------------===//
0003 //
0004 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
0005 // See https://llvm.org/LICENSE.txt for license information.
0006 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
0007 //
0008 //===----------------------------------------------------------------------===//
0009 
0010 #ifndef _LIBCPP___FORMAT_FORMATTER_FLOATING_POINT_H
0011 #define _LIBCPP___FORMAT_FORMATTER_FLOATING_POINT_H
0012 
0013 #include <__algorithm/copy_n.h>
0014 #include <__algorithm/find.h>
0015 #include <__algorithm/max.h>
0016 #include <__algorithm/min.h>
0017 #include <__algorithm/rotate.h>
0018 #include <__algorithm/transform.h>
0019 #include <__assert>
0020 #include <__charconv/chars_format.h>
0021 #include <__charconv/to_chars_floating_point.h>
0022 #include <__charconv/to_chars_result.h>
0023 #include <__concepts/arithmetic.h>
0024 #include <__concepts/same_as.h>
0025 #include <__config>
0026 #include <__cstddef/ptrdiff_t.h>
0027 #include <__format/concepts.h>
0028 #include <__format/format_parse_context.h>
0029 #include <__format/formatter.h>
0030 #include <__format/formatter_integral.h>
0031 #include <__format/formatter_output.h>
0032 #include <__format/parser_std_format_spec.h>
0033 #include <__iterator/concepts.h>
0034 #include <__memory/allocator.h>
0035 #include <__system_error/errc.h>
0036 #include <__type_traits/conditional.h>
0037 #include <__utility/move.h>
0038 #include <__utility/unreachable.h>
0039 #include <cmath>
0040 
0041 #if _LIBCPP_HAS_LOCALIZATION
0042 #  include <__locale>
0043 #endif
0044 
0045 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
0046 #  pragma GCC system_header
0047 #endif
0048 
0049 _LIBCPP_PUSH_MACROS
0050 #include <__undef_macros>
0051 
0052 _LIBCPP_BEGIN_NAMESPACE_STD
0053 
0054 #if _LIBCPP_STD_VER >= 20
0055 
0056 namespace __formatter {
0057 
0058 template <floating_point _Tp>
0059 _LIBCPP_HIDE_FROM_ABI char* __to_buffer(char* __first, char* __last, _Tp __value) {
0060   to_chars_result __r = std::to_chars(__first, __last, __value);
0061   _LIBCPP_ASSERT_INTERNAL(__r.ec == errc(0), "Internal buffer too small");
0062   return __r.ptr;
0063 }
0064 
0065 template <floating_point _Tp>
0066 _LIBCPP_HIDE_FROM_ABI char* __to_buffer(char* __first, char* __last, _Tp __value, chars_format __fmt) {
0067   to_chars_result __r = std::to_chars(__first, __last, __value, __fmt);
0068   _LIBCPP_ASSERT_INTERNAL(__r.ec == errc(0), "Internal buffer too small");
0069   return __r.ptr;
0070 }
0071 
0072 template <floating_point _Tp>
0073 _LIBCPP_HIDE_FROM_ABI char* __to_buffer(char* __first, char* __last, _Tp __value, chars_format __fmt, int __precision) {
0074   to_chars_result __r = std::to_chars(__first, __last, __value, __fmt, __precision);
0075   _LIBCPP_ASSERT_INTERNAL(__r.ec == errc(0), "Internal buffer too small");
0076   return __r.ptr;
0077 }
0078 
0079 // https://en.cppreference.com/w/cpp/language/types#cite_note-1
0080 // float             min subnormal: +/-0x1p-149   max: +/- 3.402,823,4 10^38
0081 // double            min subnormal: +/-0x1p-1074  max  +/- 1.797,693,134,862,315,7 10^308
0082 // long double (x86) min subnormal: +/-0x1p-16446 max: +/- 1.189,731,495,357,231,765,021 10^4932
0083 //
0084 // The maximum number of digits required for the integral part is based on the
0085 // maximum's value power of 10. Every power of 10 requires one additional
0086 // decimal digit.
0087 // The maximum number of digits required for the fractional part is based on
0088 // the minimal subnormal hexadecimal output's power of 10. Every division of a
0089 // fraction's binary 1 by 2, requires one additional decimal digit.
0090 //
0091 // The maximum size of a formatted value depends on the selected output format.
0092 // Ignoring the fact the format string can request a precision larger than the
0093 // values maximum required, these values are:
0094 //
0095 // sign                    1 code unit
0096 // __max_integral
0097 // radix point             1 code unit
0098 // __max_fractional
0099 // exponent character      1 code unit
0100 // sign                    1 code unit
0101 // __max_fractional_value
0102 // -----------------------------------
0103 // total                   4 code units extra required.
0104 //
0105 // TODO FMT Optimize the storage to avoid storing digits that are known to be zero.
0106 // https://www.exploringbinary.com/maximum-number-of-decimal-digits-in-binary-floating-point-numbers/
0107 
0108 // TODO FMT Add long double specialization when to_chars has proper long double support.
0109 template <class _Tp>
0110 struct __traits;
0111 
0112 template <floating_point _Fp>
0113 _LIBCPP_HIDE_FROM_ABI constexpr size_t __float_buffer_size(int __precision) {
0114   using _Traits = __traits<_Fp>;
0115   return 4 + _Traits::__max_integral + __precision + _Traits::__max_fractional_value;
0116 }
0117 
0118 template <>
0119 struct __traits<float> {
0120   static constexpr int __max_integral         = 38;
0121   static constexpr int __max_fractional       = 149;
0122   static constexpr int __max_fractional_value = 3;
0123   static constexpr size_t __stack_buffer_size = 256;
0124 
0125   static constexpr int __hex_precision_digits = 3;
0126 };
0127 
0128 template <>
0129 struct __traits<double> {
0130   static constexpr int __max_integral         = 308;
0131   static constexpr int __max_fractional       = 1074;
0132   static constexpr int __max_fractional_value = 4;
0133   static constexpr size_t __stack_buffer_size = 1024;
0134 
0135   static constexpr int __hex_precision_digits = 4;
0136 };
0137 
0138 /// Helper class to store the conversion buffer.
0139 ///
0140 /// Depending on the maximum size required for a value, the buffer is allocated
0141 /// on the stack or the heap.
0142 template <floating_point _Fp>
0143 class _LIBCPP_TEMPLATE_VIS __float_buffer {
0144   using _Traits _LIBCPP_NODEBUG = __traits<_Fp>;
0145 
0146 public:
0147   // TODO FMT Improve this constructor to do a better estimate.
0148   // When using a scientific formatting with a precision of 6 a stack buffer
0149   // will always suffice. At the moment that isn't important since floats and
0150   // doubles use a stack buffer, unless the precision used in the format string
0151   // is large.
0152   // When supporting long doubles the __max_integral part becomes 4932 which
0153   // may be too much for some platforms. For these cases a better estimate is
0154   // required.
0155   explicit _LIBCPP_HIDE_FROM_ABI __float_buffer(int __precision)
0156       : __precision_(__precision != -1 ? __precision : _Traits::__max_fractional) {
0157     // When the precision is larger than _Traits::__max_fractional the digits in
0158     // the range (_Traits::__max_fractional, precision] will contain the value
0159     // zero. There's no need to request to_chars to write these zeros:
0160     // - When the value is large a temporary heap buffer needs to be allocated.
0161     // - When to_chars writes the values they need to be "copied" to the output:
0162     //   - char: std::fill on the output iterator is faster than std::copy.
0163     //   - wchar_t: same argument as char, but additional std::copy won't work.
0164     //     The input is always a char buffer, so every char in the buffer needs
0165     //     to be converted from a char to a wchar_t.
0166     if (__precision_ > _Traits::__max_fractional) {
0167       __num_trailing_zeros_ = __precision_ - _Traits::__max_fractional;
0168       __precision_          = _Traits::__max_fractional;
0169     }
0170 
0171     __size_ = __formatter::__float_buffer_size<_Fp>(__precision_);
0172     if (__size_ > _Traits::__stack_buffer_size)
0173       // The allocated buffer's contents don't need initialization.
0174       __begin_ = allocator<char>{}.allocate(__size_);
0175     else
0176       __begin_ = __buffer_;
0177   }
0178 
0179   _LIBCPP_HIDE_FROM_ABI ~__float_buffer() {
0180     if (__size_ > _Traits::__stack_buffer_size)
0181       allocator<char>{}.deallocate(__begin_, __size_);
0182   }
0183   _LIBCPP_HIDE_FROM_ABI __float_buffer(const __float_buffer&)            = delete;
0184   _LIBCPP_HIDE_FROM_ABI __float_buffer& operator=(const __float_buffer&) = delete;
0185 
0186   _LIBCPP_HIDE_FROM_ABI char* begin() const { return __begin_; }
0187   _LIBCPP_HIDE_FROM_ABI char* end() const { return __begin_ + __size_; }
0188 
0189   _LIBCPP_HIDE_FROM_ABI int __precision() const { return __precision_; }
0190   _LIBCPP_HIDE_FROM_ABI int __num_trailing_zeros() const { return __num_trailing_zeros_; }
0191   _LIBCPP_HIDE_FROM_ABI void __remove_trailing_zeros() { __num_trailing_zeros_ = 0; }
0192   _LIBCPP_HIDE_FROM_ABI void __add_trailing_zeros(int __zeros) { __num_trailing_zeros_ += __zeros; }
0193 
0194 private:
0195   int __precision_;
0196   int __num_trailing_zeros_{0};
0197   size_t __size_;
0198   char* __begin_;
0199   char __buffer_[_Traits::__stack_buffer_size];
0200 };
0201 
0202 struct __float_result {
0203   /// Points at the beginning of the integral part in the buffer.
0204   ///
0205   /// When there's no sign character this points at the start of the buffer.
0206   char* __integral;
0207 
0208   /// Points at the radix point, when not present it's the same as \ref __last.
0209   char* __radix_point;
0210 
0211   /// Points at the exponent character, when not present it's the same as \ref __last.
0212   char* __exponent;
0213 
0214   /// Points beyond the last written element in the buffer.
0215   char* __last;
0216 };
0217 
0218 /// Finds the position of the exponent character 'e' at the end of the buffer.
0219 ///
0220 /// Assuming there is an exponent the input will terminate with
0221 /// eSdd and eSdddd (S = sign, d = digit)
0222 ///
0223 /// \returns a pointer to the exponent or __last when not found.
0224 constexpr inline _LIBCPP_HIDE_FROM_ABI char* __find_exponent(char* __first, char* __last) {
0225   ptrdiff_t __size = __last - __first;
0226   if (__size >= 4) {
0227     __first = __last - std::min(__size, ptrdiff_t(6));
0228     for (; __first != __last - 3; ++__first) {
0229       if (*__first == 'e')
0230         return __first;
0231     }
0232   }
0233   return __last;
0234 }
0235 
0236 template <class _Fp, class _Tp>
0237 _LIBCPP_HIDE_FROM_ABI __float_result
0238 __format_buffer_default(const __float_buffer<_Fp>& __buffer, _Tp __value, char* __integral) {
0239   __float_result __result;
0240   __result.__integral = __integral;
0241   __result.__last     = __formatter::__to_buffer(__integral, __buffer.end(), __value);
0242 
0243   __result.__exponent = __formatter::__find_exponent(__result.__integral, __result.__last);
0244 
0245   // Constrains:
0246   // - There's at least one decimal digit before the radix point.
0247   // - The radix point, when present, is placed before the exponent.
0248   __result.__radix_point = std::find(__result.__integral + 1, __result.__exponent, '.');
0249 
0250   // When the radix point isn't found its position is the exponent instead of
0251   // __result.__last.
0252   if (__result.__radix_point == __result.__exponent)
0253     __result.__radix_point = __result.__last;
0254 
0255   // clang-format off
0256   _LIBCPP_ASSERT_INTERNAL((__result.__integral != __result.__last) &&
0257                           (__result.__radix_point == __result.__last || *__result.__radix_point == '.') &&
0258                           (__result.__exponent == __result.__last || *__result.__exponent == 'e'),
0259                           "Post-condition failure.");
0260   // clang-format on
0261 
0262   return __result;
0263 }
0264 
0265 template <class _Fp, class _Tp>
0266 _LIBCPP_HIDE_FROM_ABI __float_result __format_buffer_hexadecimal_lower_case(
0267     const __float_buffer<_Fp>& __buffer, _Tp __value, int __precision, char* __integral) {
0268   __float_result __result;
0269   __result.__integral = __integral;
0270   if (__precision == -1)
0271     __result.__last = __formatter::__to_buffer(__integral, __buffer.end(), __value, chars_format::hex);
0272   else
0273     __result.__last = __formatter::__to_buffer(__integral, __buffer.end(), __value, chars_format::hex, __precision);
0274 
0275   // H = one or more hex-digits
0276   // S = sign
0277   // D = one or more decimal-digits
0278   // When the fractional part is zero and no precision the output is 0p+0
0279   // else the output is                                              0.HpSD
0280   // So testing the second position can differentiate between these two cases.
0281   char* __first = __integral + 1;
0282   if (*__first == '.') {
0283     __result.__radix_point = __first;
0284     // One digit is the minimum
0285     // 0.hpSd
0286     //       ^-- last
0287     //     ^---- integral = end of search
0288     // ^-------- start of search
0289     // 0123456
0290     //
0291     // Four digits is the maximum
0292     // 0.hpSdddd
0293     //          ^-- last
0294     //        ^---- integral = end of search
0295     //    ^-------- start of search
0296     // 0123456789
0297     static_assert(__traits<_Fp>::__hex_precision_digits <= 4, "Guard against possible underflow.");
0298 
0299     char* __last        = __result.__last - 2;
0300     __first             = __last - __traits<_Fp>::__hex_precision_digits;
0301     __result.__exponent = std::find(__first, __last, 'p');
0302   } else {
0303     __result.__radix_point = __result.__last;
0304     __result.__exponent    = __first;
0305   }
0306 
0307   // clang-format off
0308   _LIBCPP_ASSERT_INTERNAL((__result.__integral != __result.__last) &&
0309                           (__result.__radix_point == __result.__last || *__result.__radix_point == '.') &&
0310                           (__result.__exponent != __result.__last && *__result.__exponent == 'p'),
0311                           "Post-condition failure.");
0312   // clang-format on
0313 
0314   return __result;
0315 }
0316 
0317 template <class _Fp, class _Tp>
0318 _LIBCPP_HIDE_FROM_ABI __float_result __format_buffer_hexadecimal_upper_case(
0319     const __float_buffer<_Fp>& __buffer, _Tp __value, int __precision, char* __integral) {
0320   __float_result __result =
0321       __formatter::__format_buffer_hexadecimal_lower_case(__buffer, __value, __precision, __integral);
0322   std::transform(__result.__integral, __result.__exponent, __result.__integral, __hex_to_upper);
0323   *__result.__exponent = 'P';
0324   return __result;
0325 }
0326 
0327 template <class _Fp, class _Tp>
0328 _LIBCPP_HIDE_FROM_ABI __float_result __format_buffer_scientific_lower_case(
0329     const __float_buffer<_Fp>& __buffer, _Tp __value, int __precision, char* __integral) {
0330   __float_result __result;
0331   __result.__integral = __integral;
0332   __result.__last =
0333       __formatter::__to_buffer(__integral, __buffer.end(), __value, chars_format::scientific, __precision);
0334 
0335   char* __first = __integral + 1;
0336   _LIBCPP_ASSERT_INTERNAL(__first != __result.__last, "No exponent present");
0337   if (*__first == '.') {
0338     __result.__radix_point = __first;
0339     __result.__exponent    = __formatter::__find_exponent(__first + 1, __result.__last);
0340   } else {
0341     __result.__radix_point = __result.__last;
0342     __result.__exponent    = __first;
0343   }
0344 
0345   // clang-format off
0346   _LIBCPP_ASSERT_INTERNAL((__result.__integral != __result.__last) &&
0347                           (__result.__radix_point == __result.__last || *__result.__radix_point == '.') &&
0348                           (__result.__exponent != __result.__last && *__result.__exponent == 'e'),
0349                           "Post-condition failure.");
0350   // clang-format on
0351   return __result;
0352 }
0353 
0354 template <class _Fp, class _Tp>
0355 _LIBCPP_HIDE_FROM_ABI __float_result __format_buffer_scientific_upper_case(
0356     const __float_buffer<_Fp>& __buffer, _Tp __value, int __precision, char* __integral) {
0357   __float_result __result =
0358       __formatter::__format_buffer_scientific_lower_case(__buffer, __value, __precision, __integral);
0359   *__result.__exponent = 'E';
0360   return __result;
0361 }
0362 
0363 template <class _Fp, class _Tp>
0364 _LIBCPP_HIDE_FROM_ABI __float_result
0365 __format_buffer_fixed(const __float_buffer<_Fp>& __buffer, _Tp __value, int __precision, char* __integral) {
0366   __float_result __result;
0367   __result.__integral = __integral;
0368   __result.__last     = __formatter::__to_buffer(__integral, __buffer.end(), __value, chars_format::fixed, __precision);
0369 
0370   // When there's no precision there's no radix point.
0371   // Else the radix point is placed at __precision + 1 from the end.
0372   // By converting __precision to a bool the subtraction can be done
0373   // unconditionally.
0374   __result.__radix_point = __result.__last - (__precision + bool(__precision));
0375   __result.__exponent    = __result.__last;
0376 
0377   // clang-format off
0378   _LIBCPP_ASSERT_INTERNAL((__result.__integral != __result.__last) &&
0379                           (__result.__radix_point == __result.__last || *__result.__radix_point == '.') &&
0380                           (__result.__exponent == __result.__last),
0381                           "Post-condition failure.");
0382   // clang-format on
0383   return __result;
0384 }
0385 
0386 template <class _Fp, class _Tp>
0387 _LIBCPP_HIDE_FROM_ABI __float_result
0388 __format_buffer_general_lower_case(__float_buffer<_Fp>& __buffer, _Tp __value, int __precision, char* __integral) {
0389   __buffer.__remove_trailing_zeros();
0390 
0391   __float_result __result;
0392   __result.__integral = __integral;
0393   __result.__last = __formatter::__to_buffer(__integral, __buffer.end(), __value, chars_format::general, __precision);
0394 
0395   char* __first = __integral + 1;
0396   if (__first == __result.__last) {
0397     __result.__radix_point = __result.__last;
0398     __result.__exponent    = __result.__last;
0399   } else {
0400     __result.__exponent = __formatter::__find_exponent(__first, __result.__last);
0401     if (__result.__exponent != __result.__last)
0402       // In scientific mode if there's a radix point it will always be after
0403       // the first digit. (This is the position __first points at).
0404       __result.__radix_point = *__first == '.' ? __first : __result.__last;
0405     else {
0406       // In fixed mode the algorithm truncates trailing spaces and possibly the
0407       // radix point. There's no good guess for the position of the radix point
0408       // therefore scan the output after the first digit.
0409       __result.__radix_point = std::find(__first, __result.__last, '.');
0410     }
0411   }
0412 
0413   // clang-format off
0414   _LIBCPP_ASSERT_INTERNAL((__result.__integral != __result.__last) &&
0415                           (__result.__radix_point == __result.__last || *__result.__radix_point == '.') &&
0416                           (__result.__exponent == __result.__last || *__result.__exponent == 'e'),
0417                           "Post-condition failure.");
0418   // clang-format on
0419 
0420   return __result;
0421 }
0422 
0423 template <class _Fp, class _Tp>
0424 _LIBCPP_HIDE_FROM_ABI __float_result
0425 __format_buffer_general_upper_case(__float_buffer<_Fp>& __buffer, _Tp __value, int __precision, char* __integral) {
0426   __float_result __result = __formatter::__format_buffer_general_lower_case(__buffer, __value, __precision, __integral);
0427   if (__result.__exponent != __result.__last)
0428     *__result.__exponent = 'E';
0429   return __result;
0430 }
0431 
0432 /// Fills the buffer with the data based on the requested formatting.
0433 ///
0434 /// This function, when needed, turns the characters to upper case and
0435 /// determines the "interesting" locations which are returned to the caller.
0436 ///
0437 /// This means the caller never has to convert the contents of the buffer to
0438 /// upper case or search for radix points and the location of the exponent.
0439 /// This gives a bit of overhead. The original code didn't do that, but due
0440 /// to the number of possible additional work needed to turn this number to
0441 /// the proper output the code was littered with tests for upper cases and
0442 /// searches for radix points and exponents.
0443 /// - When a precision larger than the type's precision is selected
0444 ///   additional zero characters need to be written before the exponent.
0445 /// - alternate form needs to add a radix point when not present.
0446 /// - localization needs to do grouping in the integral part.
0447 template <class _Fp, class _Tp>
0448 // TODO FMT _Fp should just be _Tp when to_chars has proper long double support.
0449 _LIBCPP_HIDE_FROM_ABI __float_result __format_buffer(
0450     __float_buffer<_Fp>& __buffer,
0451     _Tp __value,
0452     bool __negative,
0453     bool __has_precision,
0454     __format_spec::__sign __sign,
0455     __format_spec::__type __type) {
0456   char* __first = __formatter::__insert_sign(__buffer.begin(), __negative, __sign);
0457   switch (__type) {
0458   case __format_spec::__type::__default:
0459     if (__has_precision)
0460       return __formatter::__format_buffer_general_lower_case(__buffer, __value, __buffer.__precision(), __first);
0461     else
0462       return __formatter::__format_buffer_default(__buffer, __value, __first);
0463 
0464   case __format_spec::__type::__hexfloat_lower_case:
0465     return __formatter::__format_buffer_hexadecimal_lower_case(
0466         __buffer, __value, __has_precision ? __buffer.__precision() : -1, __first);
0467 
0468   case __format_spec::__type::__hexfloat_upper_case:
0469     return __formatter::__format_buffer_hexadecimal_upper_case(
0470         __buffer, __value, __has_precision ? __buffer.__precision() : -1, __first);
0471 
0472   case __format_spec::__type::__scientific_lower_case:
0473     return __formatter::__format_buffer_scientific_lower_case(__buffer, __value, __buffer.__precision(), __first);
0474 
0475   case __format_spec::__type::__scientific_upper_case:
0476     return __formatter::__format_buffer_scientific_upper_case(__buffer, __value, __buffer.__precision(), __first);
0477 
0478   case __format_spec::__type::__fixed_lower_case:
0479   case __format_spec::__type::__fixed_upper_case:
0480     return __formatter::__format_buffer_fixed(__buffer, __value, __buffer.__precision(), __first);
0481 
0482   case __format_spec::__type::__general_lower_case:
0483     return __formatter::__format_buffer_general_lower_case(__buffer, __value, __buffer.__precision(), __first);
0484 
0485   case __format_spec::__type::__general_upper_case:
0486     return __formatter::__format_buffer_general_upper_case(__buffer, __value, __buffer.__precision(), __first);
0487 
0488   default:
0489     _LIBCPP_ASSERT_INTERNAL(false, "The parser should have validated the type");
0490     __libcpp_unreachable();
0491   }
0492 }
0493 
0494 #  if _LIBCPP_HAS_LOCALIZATION
0495 template <class _OutIt, class _Fp, class _CharT>
0496 _LIBCPP_HIDE_FROM_ABI _OutIt __format_locale_specific_form(
0497     _OutIt __out_it,
0498     const __float_buffer<_Fp>& __buffer,
0499     const __float_result& __result,
0500     std::locale __loc,
0501     __format_spec::__parsed_specifications<_CharT> __specs) {
0502   const auto& __np  = std::use_facet<numpunct<_CharT>>(__loc);
0503   string __grouping = __np.grouping();
0504   char* __first     = __result.__integral;
0505   // When no radix point or exponent are present __last will be __result.__last.
0506   char* __last = std::min(__result.__radix_point, __result.__exponent);
0507 
0508   ptrdiff_t __digits = __last - __first;
0509   if (!__grouping.empty()) {
0510     if (__digits <= __grouping[0])
0511       __grouping.clear();
0512     else
0513       __grouping = __formatter::__determine_grouping(__digits, __grouping);
0514   }
0515 
0516   ptrdiff_t __size =
0517       __result.__last - __buffer.begin() + // Formatted string
0518       __buffer.__num_trailing_zeros() +    // Not yet rendered zeros
0519       __grouping.size() -                  // Grouping contains one
0520       !__grouping.empty();                 // additional character
0521 
0522   __formatter::__padding_size_result __padding = {0, 0};
0523   bool __zero_padding                          = __specs.__alignment_ == __format_spec::__alignment::__zero_padding;
0524   if (__size < __specs.__width_) {
0525     if (__zero_padding) {
0526       __specs.__alignment_      = __format_spec::__alignment::__right;
0527       __specs.__fill_.__data[0] = _CharT('0');
0528     }
0529 
0530     __padding = __formatter::__padding_size(__size, __specs.__width_, __specs.__alignment_);
0531   }
0532 
0533   // sign and (zero padding or alignment)
0534   if (__zero_padding && __first != __buffer.begin())
0535     *__out_it++ = *__buffer.begin();
0536   __out_it = __formatter::__fill(std::move(__out_it), __padding.__before_, __specs.__fill_);
0537   if (!__zero_padding && __first != __buffer.begin())
0538     *__out_it++ = *__buffer.begin();
0539 
0540   // integral part
0541   if (__grouping.empty()) {
0542     __out_it = __formatter::__copy(__first, __digits, std::move(__out_it));
0543   } else {
0544     auto __r     = __grouping.rbegin();
0545     auto __e     = __grouping.rend() - 1;
0546     _CharT __sep = __np.thousands_sep();
0547     // The output is divided in small groups of numbers to write:
0548     // - A group before the first separator.
0549     // - A separator and a group, repeated for the number of separators.
0550     // - A group after the last separator.
0551     // This loop achieves that process by testing the termination condition
0552     // midway in the loop.
0553     while (true) {
0554       __out_it = __formatter::__copy(__first, *__r, std::move(__out_it));
0555       __first += *__r;
0556 
0557       if (__r == __e)
0558         break;
0559 
0560       ++__r;
0561       *__out_it++ = __sep;
0562     }
0563   }
0564 
0565   // fractional part
0566   if (__result.__radix_point != __result.__last) {
0567     *__out_it++ = __np.decimal_point();
0568     __out_it    = __formatter::__copy(__result.__radix_point + 1, __result.__exponent, std::move(__out_it));
0569     __out_it    = __formatter::__fill(std::move(__out_it), __buffer.__num_trailing_zeros(), _CharT('0'));
0570   }
0571 
0572   // exponent
0573   if (__result.__exponent != __result.__last)
0574     __out_it = __formatter::__copy(__result.__exponent, __result.__last, std::move(__out_it));
0575 
0576   // alignment
0577   return __formatter::__fill(std::move(__out_it), __padding.__after_, __specs.__fill_);
0578 }
0579 #  endif // _LIBCPP_HAS_LOCALIZATION
0580 
0581 template <class _OutIt, class _CharT>
0582 _LIBCPP_HIDE_FROM_ABI _OutIt __format_floating_point_non_finite(
0583     _OutIt __out_it, __format_spec::__parsed_specifications<_CharT> __specs, bool __negative, bool __isnan) {
0584   char __buffer[4];
0585   char* __last = __formatter::__insert_sign(__buffer, __negative, __specs.__std_.__sign_);
0586 
0587   // to_chars can return inf, infinity, nan, and nan(n-char-sequence).
0588   // The format library requires inf and nan.
0589   // All in one expression to avoid dangling references.
0590   bool __upper_case =
0591       __specs.__std_.__type_ == __format_spec::__type::__hexfloat_upper_case ||
0592       __specs.__std_.__type_ == __format_spec::__type::__scientific_upper_case ||
0593       __specs.__std_.__type_ == __format_spec::__type::__fixed_upper_case ||
0594       __specs.__std_.__type_ == __format_spec::__type::__general_upper_case;
0595   __last = std::copy_n(&("infnanINFNAN"[6 * __upper_case + 3 * __isnan]), 3, __last);
0596 
0597   // [format.string.std]/13
0598   // A zero (0) character preceding the width field pads the field with
0599   // leading zeros (following any indication of sign or base) to the field
0600   // width, except when applied to an infinity or NaN.
0601   if (__specs.__alignment_ == __format_spec::__alignment::__zero_padding)
0602     __specs.__alignment_ = __format_spec::__alignment::__right;
0603 
0604   return __formatter::__write(__buffer, __last, std::move(__out_it), __specs);
0605 }
0606 
0607 /// Writes additional zero's for the precision before the exponent.
0608 /// This is used when the precision requested in the format string is larger
0609 /// than the maximum precision of the floating-point type. These precision
0610 /// digits are always 0.
0611 ///
0612 /// \param __exponent           The location of the exponent character.
0613 /// \param __num_trailing_zeros The number of 0's to write before the exponent
0614 ///                             character.
0615 template <class _CharT, class _ParserCharT>
0616 _LIBCPP_HIDE_FROM_ABI auto __write_using_trailing_zeros(
0617     const _CharT* __first,
0618     const _CharT* __last,
0619     output_iterator<const _CharT&> auto __out_it,
0620     __format_spec::__parsed_specifications<_ParserCharT> __specs,
0621     size_t __size,
0622     const _CharT* __exponent,
0623     size_t __num_trailing_zeros) -> decltype(__out_it) {
0624   _LIBCPP_ASSERT_INTERNAL(__first <= __last, "Not a valid range");
0625   _LIBCPP_ASSERT_INTERNAL(__num_trailing_zeros > 0, "The overload not writing trailing zeros should have been used");
0626 
0627   __padding_size_result __padding =
0628       __formatter::__padding_size(__size + __num_trailing_zeros, __specs.__width_, __specs.__alignment_);
0629   __out_it = __formatter::__fill(std::move(__out_it), __padding.__before_, __specs.__fill_);
0630   __out_it = __formatter::__copy(__first, __exponent, std::move(__out_it));
0631   __out_it = __formatter::__fill(std::move(__out_it), __num_trailing_zeros, _CharT('0'));
0632   __out_it = __formatter::__copy(__exponent, __last, std::move(__out_it));
0633   return __formatter::__fill(std::move(__out_it), __padding.__after_, __specs.__fill_);
0634 }
0635 
0636 template <floating_point _Tp, class _CharT, class _FormatContext>
0637 _LIBCPP_HIDE_FROM_ABI typename _FormatContext::iterator
0638 __format_floating_point(_Tp __value, _FormatContext& __ctx, __format_spec::__parsed_specifications<_CharT> __specs) {
0639   bool __negative = std::signbit(__value);
0640 
0641   if (!std::isfinite(__value)) [[unlikely]]
0642     return __formatter::__format_floating_point_non_finite(__ctx.out(), __specs, __negative, std::isnan(__value));
0643 
0644   // Depending on the std-format-spec string the sign and the value
0645   // might not be outputted together:
0646   // - zero-padding may insert additional '0' characters.
0647   // Therefore the value is processed as a non negative value.
0648   // The function @ref __insert_sign will insert a '-' when the value was
0649   // negative.
0650 
0651   if (__negative)
0652     __value = -__value;
0653 
0654   // TODO FMT _Fp should just be _Tp when to_chars has proper long double support.
0655   using _Fp = conditional_t<same_as<_Tp, long double>, double, _Tp>;
0656   // Force the type of the precision to avoid -1 to become an unsigned value.
0657   __float_buffer<_Fp> __buffer(__specs.__precision_);
0658   __float_result __result = __formatter::__format_buffer(
0659       __buffer, __value, __negative, (__specs.__has_precision()), __specs.__std_.__sign_, __specs.__std_.__type_);
0660 
0661   if (__specs.__std_.__alternate_form_) {
0662     if (__result.__radix_point == __result.__last) {
0663       *__result.__last++ = '.';
0664 
0665       // When there is an exponent the point needs to be moved before the
0666       // exponent. When there's no exponent the rotate does nothing. Since
0667       // rotate tests whether the operation is a nop, call it unconditionally.
0668       std::rotate(__result.__exponent, __result.__last - 1, __result.__last);
0669       __result.__radix_point = __result.__exponent;
0670 
0671       // The radix point is always placed before the exponent.
0672       // - No exponent needs to point to the new last.
0673       // - An exponent needs to move one position to the right.
0674       // So it's safe to increment the value unconditionally.
0675       ++__result.__exponent;
0676     }
0677 
0678     // [format.string.std]/6
0679     //   In addition, for g and G conversions, trailing zeros are not removed
0680     //   from the result.
0681     //
0682     // If the type option for a floating-point type is none it may use the
0683     // general formatting, but it's not a g or G conversion. So in that case
0684     // the formatting should not append trailing zeros.
0685     bool __is_general = __specs.__std_.__type_ == __format_spec::__type::__general_lower_case ||
0686                         __specs.__std_.__type_ == __format_spec::__type::__general_upper_case;
0687 
0688     if (__is_general) {
0689       // https://en.cppreference.com/w/c/io/fprintf
0690       // Let P equal the precision if nonzero, 6 if the precision is not
0691       // specified, or 1 if the precision is 0. Then, if a conversion with
0692       // style E would have an exponent of X:
0693       int __p = std::max<int>(1, (__specs.__has_precision() ? __specs.__precision_ : 6));
0694       if (__result.__exponent == __result.__last)
0695         // if P > X >= -4, the conversion is with style f or F and precision P - 1 - X.
0696         // By including the radix point it calculates P - (1 + X)
0697         __p -= __result.__radix_point - __result.__integral;
0698       else
0699         // otherwise, the conversion is with style e or E and precision P - 1.
0700         --__p;
0701 
0702       ptrdiff_t __precision = (__result.__exponent - __result.__radix_point) - 1;
0703       if (__precision < __p)
0704         __buffer.__add_trailing_zeros(__p - __precision);
0705     }
0706   }
0707 
0708 #  if _LIBCPP_HAS_LOCALIZATION
0709   if (__specs.__std_.__locale_specific_form_)
0710     return __formatter::__format_locale_specific_form(__ctx.out(), __buffer, __result, __ctx.locale(), __specs);
0711 #  endif
0712 
0713   ptrdiff_t __size         = __result.__last - __buffer.begin();
0714   int __num_trailing_zeros = __buffer.__num_trailing_zeros();
0715   if (__size + __num_trailing_zeros >= __specs.__width_) {
0716     if (__num_trailing_zeros && __result.__exponent != __result.__last)
0717       // Insert trailing zeros before exponent character.
0718       return __formatter::__copy(
0719           __result.__exponent,
0720           __result.__last,
0721           __formatter::__fill(__formatter::__copy(__buffer.begin(), __result.__exponent, __ctx.out()),
0722                               __num_trailing_zeros,
0723                               _CharT('0')));
0724 
0725     return __formatter::__fill(
0726         __formatter::__copy(__buffer.begin(), __result.__last, __ctx.out()), __num_trailing_zeros, _CharT('0'));
0727   }
0728 
0729   auto __out_it = __ctx.out();
0730   char* __first = __buffer.begin();
0731   if (__specs.__alignment_ == __format_spec::__alignment ::__zero_padding) {
0732     // When there is a sign output it before the padding. Note the __size
0733     // doesn't need any adjustment, regardless whether the sign is written
0734     // here or in __formatter::__write.
0735     if (__first != __result.__integral)
0736       *__out_it++ = *__first++;
0737     // After the sign is written, zero padding is the same a right alignment
0738     // with '0'.
0739     __specs.__alignment_      = __format_spec::__alignment::__right;
0740     __specs.__fill_.__data[0] = _CharT('0');
0741   }
0742 
0743   if (__num_trailing_zeros)
0744     return __formatter::__write_using_trailing_zeros(
0745         __first, __result.__last, std::move(__out_it), __specs, __size, __result.__exponent, __num_trailing_zeros);
0746 
0747   return __formatter::__write(__first, __result.__last, std::move(__out_it), __specs, __size);
0748 }
0749 
0750 } // namespace __formatter
0751 
0752 template <__fmt_char_type _CharT>
0753 struct _LIBCPP_TEMPLATE_VIS __formatter_floating_point {
0754 public:
0755   template <class _ParseContext>
0756   _LIBCPP_HIDE_FROM_ABI constexpr typename _ParseContext::iterator parse(_ParseContext& __ctx) {
0757     typename _ParseContext::iterator __result = __parser_.__parse(__ctx, __format_spec::__fields_floating_point);
0758     __format_spec::__process_parsed_floating_point(__parser_, "a floating-point");
0759     return __result;
0760   }
0761 
0762   template <floating_point _Tp, class _FormatContext>
0763   _LIBCPP_HIDE_FROM_ABI typename _FormatContext::iterator format(_Tp __value, _FormatContext& __ctx) const {
0764     return __formatter::__format_floating_point(__value, __ctx, __parser_.__get_parsed_std_specifications(__ctx));
0765   }
0766 
0767   __format_spec::__parser<_CharT> __parser_;
0768 };
0769 
0770 template <__fmt_char_type _CharT>
0771 struct _LIBCPP_TEMPLATE_VIS formatter<float, _CharT> : public __formatter_floating_point<_CharT> {};
0772 template <__fmt_char_type _CharT>
0773 struct _LIBCPP_TEMPLATE_VIS formatter<double, _CharT> : public __formatter_floating_point<_CharT> {};
0774 template <__fmt_char_type _CharT>
0775 struct _LIBCPP_TEMPLATE_VIS formatter<long double, _CharT> : public __formatter_floating_point<_CharT> {};
0776 
0777 #  if _LIBCPP_STD_VER >= 23
0778 template <>
0779 inline constexpr bool enable_nonlocking_formatter_optimization<float> = true;
0780 template <>
0781 inline constexpr bool enable_nonlocking_formatter_optimization<double> = true;
0782 template <>
0783 inline constexpr bool enable_nonlocking_formatter_optimization<long double> = true;
0784 #  endif // _LIBCPP_STD_VER >= 23
0785 #endif   // _LIBCPP_STD_VER >= 20
0786 
0787 _LIBCPP_END_NAMESPACE_STD
0788 
0789 _LIBCPP_POP_MACROS
0790 
0791 #endif // _LIBCPP___FORMAT_FORMATTER_FLOATING_POINT_H