master
  1// -*- C++ -*-
  2//===----------------------------------------------------------------------===//
  3//
  4// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  5// See https://llvm.org/LICENSE.txt for license information.
  6// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  7//
  8//===----------------------------------------------------------------------===//
  9
 10#ifndef _LIBCPP___FORMAT_FORMATTER_FLOATING_POINT_H
 11#define _LIBCPP___FORMAT_FORMATTER_FLOATING_POINT_H
 12
 13#include <__algorithm/copy_n.h>
 14#include <__algorithm/find.h>
 15#include <__algorithm/max.h>
 16#include <__algorithm/min.h>
 17#include <__algorithm/rotate.h>
 18#include <__algorithm/transform.h>
 19#include <__assert>
 20#include <__charconv/chars_format.h>
 21#include <__charconv/to_chars_floating_point.h>
 22#include <__charconv/to_chars_integral.h>
 23#include <__charconv/to_chars_result.h>
 24#include <__concepts/arithmetic.h>
 25#include <__concepts/same_as.h>
 26#include <__config>
 27#include <__cstddef/ptrdiff_t.h>
 28#include <__format/concepts.h>
 29#include <__format/format_parse_context.h>
 30#include <__format/formatter.h>
 31#include <__format/formatter_integral.h>
 32#include <__format/formatter_output.h>
 33#include <__format/parser_std_format_spec.h>
 34#include <__iterator/concepts.h>
 35#include <__memory/allocator.h>
 36#include <__system_error/errc.h>
 37#include <__type_traits/conditional.h>
 38#include <__utility/move.h>
 39#include <__utility/unreachable.h>
 40#include <cmath>
 41
 42#if _LIBCPP_HAS_LOCALIZATION
 43#  include <__locale>
 44#endif
 45
 46#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
 47#  pragma GCC system_header
 48#endif
 49
 50_LIBCPP_PUSH_MACROS
 51#include <__undef_macros>
 52
 53_LIBCPP_BEGIN_NAMESPACE_STD
 54
 55#if _LIBCPP_STD_VER >= 20
 56
 57namespace __formatter {
 58
 59template <floating_point _Tp>
 60_LIBCPP_HIDE_FROM_ABI char* __to_buffer(char* __first, char* __last, _Tp __value) {
 61  to_chars_result __r = std::to_chars(__first, __last, __value);
 62  _LIBCPP_ASSERT_INTERNAL(__r.ec == errc(0), "Internal buffer too small");
 63  return __r.ptr;
 64}
 65
 66template <floating_point _Tp>
 67_LIBCPP_HIDE_FROM_ABI char* __to_buffer(char* __first, char* __last, _Tp __value, chars_format __fmt) {
 68  to_chars_result __r = std::to_chars(__first, __last, __value, __fmt);
 69  _LIBCPP_ASSERT_INTERNAL(__r.ec == errc(0), "Internal buffer too small");
 70  return __r.ptr;
 71}
 72
 73template <floating_point _Tp>
 74_LIBCPP_HIDE_FROM_ABI char* __to_buffer(char* __first, char* __last, _Tp __value, chars_format __fmt, int __precision) {
 75  to_chars_result __r = std::to_chars(__first, __last, __value, __fmt, __precision);
 76  _LIBCPP_ASSERT_INTERNAL(__r.ec == errc(0), "Internal buffer too small");
 77  return __r.ptr;
 78}
 79
 80// https://en.cppreference.com/w/cpp/language/types#cite_note-1
 81// float             min subnormal: +/-0x1p-149   max: +/- 3.402,823,4 10^38
 82// double            min subnormal: +/-0x1p-1074  max  +/- 1.797,693,134,862,315,7 10^308
 83// long double (x86) min subnormal: +/-0x1p-16446 max: +/- 1.189,731,495,357,231,765,021 10^4932
 84//
 85// The maximum number of digits required for the integral part is based on the
 86// maximum's value power of 10. Every power of 10 requires one additional
 87// decimal digit.
 88// The maximum number of digits required for the fractional part is based on
 89// the minimal subnormal hexadecimal output's power of 10. Every division of a
 90// fraction's binary 1 by 2, requires one additional decimal digit.
 91//
 92// The maximum size of a formatted value depends on the selected output format.
 93// Ignoring the fact the format string can request a precision larger than the
 94// values maximum required, these values are:
 95//
 96// sign                    1 code unit
 97// __max_integral
 98// radix point             1 code unit
 99// __max_fractional
100// exponent character      1 code unit
101// sign                    1 code unit
102// __max_fractional_value
103// -----------------------------------
104// total                   4 code units extra required.
105//
106// TODO FMT Optimize the storage to avoid storing digits that are known to be zero.
107// https://www.exploringbinary.com/maximum-number-of-decimal-digits-in-binary-floating-point-numbers/
108
109// TODO FMT Add long double specialization when to_chars has proper long double support.
110template <class _Tp>
111struct __traits;
112
113template <floating_point _Fp>
114_LIBCPP_HIDE_FROM_ABI constexpr size_t __float_buffer_size(int __precision) {
115  using _Traits = __traits<_Fp>;
116  return 4 + _Traits::__max_integral + __precision + _Traits::__max_fractional_value;
117}
118
119template <>
120struct __traits<float> {
121  static constexpr int __max_integral         = 38;
122  static constexpr int __max_fractional       = 149;
123  static constexpr int __max_fractional_value = 3;
124  static constexpr size_t __stack_buffer_size = 256;
125
126  static constexpr int __hex_precision_digits = 3;
127};
128
129template <>
130struct __traits<double> {
131  static constexpr int __max_integral         = 308;
132  static constexpr int __max_fractional       = 1074;
133  static constexpr int __max_fractional_value = 4;
134  static constexpr size_t __stack_buffer_size = 1024;
135
136  static constexpr int __hex_precision_digits = 4;
137};
138
139/// Helper class to store the conversion buffer.
140///
141/// Depending on the maximum size required for a value, the buffer is allocated
142/// on the stack or the heap.
143template <floating_point _Fp>
144class __float_buffer {
145  using _Traits _LIBCPP_NODEBUG = __traits<_Fp>;
146
147public:
148  // TODO FMT Improve this constructor to do a better estimate.
149  // When using a scientific formatting with a precision of 6 a stack buffer
150  // will always suffice. At the moment that isn't important since floats and
151  // doubles use a stack buffer, unless the precision used in the format string
152  // is large.
153  // When supporting long doubles the __max_integral part becomes 4932 which
154  // may be too much for some platforms. For these cases a better estimate is
155  // required.
156  explicit _LIBCPP_HIDE_FROM_ABI __float_buffer(int __precision)
157      : __precision_(__precision != -1 ? __precision : _Traits::__max_fractional) {
158    // When the precision is larger than _Traits::__max_fractional the digits in
159    // the range (_Traits::__max_fractional, precision] will contain the value
160    // zero. There's no need to request to_chars to write these zeros:
161    // - When the value is large a temporary heap buffer needs to be allocated.
162    // - When to_chars writes the values they need to be "copied" to the output:
163    //   - char: std::fill on the output iterator is faster than std::copy.
164    //   - wchar_t: same argument as char, but additional std::copy won't work.
165    //     The input is always a char buffer, so every char in the buffer needs
166    //     to be converted from a char to a wchar_t.
167    if (__precision_ > _Traits::__max_fractional) {
168      __num_trailing_zeros_ = __precision_ - _Traits::__max_fractional;
169      __precision_          = _Traits::__max_fractional;
170    }
171
172    __size_ = __formatter::__float_buffer_size<_Fp>(__precision_);
173    if (__size_ > _Traits::__stack_buffer_size)
174      // The allocated buffer's contents don't need initialization.
175      __begin_ = allocator<char>{}.allocate(__size_);
176    else
177      __begin_ = __buffer_;
178  }
179
180  _LIBCPP_HIDE_FROM_ABI ~__float_buffer() {
181    if (__size_ > _Traits::__stack_buffer_size)
182      allocator<char>{}.deallocate(__begin_, __size_);
183  }
184  _LIBCPP_HIDE_FROM_ABI __float_buffer(const __float_buffer&)            = delete;
185  _LIBCPP_HIDE_FROM_ABI __float_buffer& operator=(const __float_buffer&) = delete;
186
187  _LIBCPP_HIDE_FROM_ABI char* begin() const { return __begin_; }
188  _LIBCPP_HIDE_FROM_ABI char* end() const { return __begin_ + __size_; }
189
190  _LIBCPP_HIDE_FROM_ABI int __precision() const { return __precision_; }
191  _LIBCPP_HIDE_FROM_ABI int __num_trailing_zeros() const { return __num_trailing_zeros_; }
192  _LIBCPP_HIDE_FROM_ABI void __remove_trailing_zeros() { __num_trailing_zeros_ = 0; }
193  _LIBCPP_HIDE_FROM_ABI void __add_trailing_zeros(int __zeros) { __num_trailing_zeros_ += __zeros; }
194
195private:
196  int __precision_;
197  int __num_trailing_zeros_{0};
198  size_t __size_;
199  char* __begin_;
200  char __buffer_[_Traits::__stack_buffer_size];
201};
202
203struct __float_result {
204  /// Points at the beginning of the integral part in the buffer.
205  ///
206  /// When there's no sign character this points at the start of the buffer.
207  char* __integral;
208
209  /// Points at the radix point, when not present it's the same as \ref __last.
210  char* __radix_point;
211
212  /// Points at the exponent character, when not present it's the same as \ref __last.
213  char* __exponent;
214
215  /// Points beyond the last written element in the buffer.
216  char* __last;
217};
218
219/// Finds the position of the exponent character 'e' at the end of the buffer.
220///
221/// Assuming there is an exponent the input will terminate with
222/// eSdd and eSdddd (S = sign, d = digit)
223///
224/// \returns a pointer to the exponent or __last when not found.
225constexpr inline _LIBCPP_HIDE_FROM_ABI char* __find_exponent(char* __first, char* __last) {
226  ptrdiff_t __size = __last - __first;
227  if (__size >= 4) {
228    __first = __last - std::min(__size, ptrdiff_t(6));
229    for (; __first != __last - 3; ++__first) {
230      if (*__first == 'e')
231        return __first;
232    }
233  }
234  return __last;
235}
236
237template <class _Fp, class _Tp>
238_LIBCPP_HIDE_FROM_ABI __float_result
239__format_buffer_default(const __float_buffer<_Fp>& __buffer, _Tp __value, char* __integral) {
240  __float_result __result;
241  __result.__integral = __integral;
242  __result.__last     = __formatter::__to_buffer(__integral, __buffer.end(), __value);
243
244  __result.__exponent = __formatter::__find_exponent(__result.__integral, __result.__last);
245
246  // Constrains:
247  // - There's at least one decimal digit before the radix point.
248  // - The radix point, when present, is placed before the exponent.
249  __result.__radix_point = std::find(__result.__integral + 1, __result.__exponent, '.');
250
251  // When the radix point isn't found its position is the exponent instead of
252  // __result.__last.
253  if (__result.__radix_point == __result.__exponent)
254    __result.__radix_point = __result.__last;
255
256  // clang-format off
257  _LIBCPP_ASSERT_INTERNAL((__result.__integral != __result.__last) &&
258                          (__result.__radix_point == __result.__last || *__result.__radix_point == '.') &&
259                          (__result.__exponent == __result.__last || *__result.__exponent == 'e'),
260                          "Post-condition failure.");
261  // clang-format on
262
263  return __result;
264}
265
266template <class _Fp, class _Tp>
267_LIBCPP_HIDE_FROM_ABI __float_result __format_buffer_hexadecimal_lower_case(
268    const __float_buffer<_Fp>& __buffer, _Tp __value, int __precision, char* __integral) {
269  __float_result __result;
270  __result.__integral = __integral;
271  if (__precision == -1)
272    __result.__last = __formatter::__to_buffer(__integral, __buffer.end(), __value, chars_format::hex);
273  else
274    __result.__last = __formatter::__to_buffer(__integral, __buffer.end(), __value, chars_format::hex, __precision);
275
276  // H = one or more hex-digits
277  // S = sign
278  // D = one or more decimal-digits
279  // When the fractional part is zero and no precision the output is 0p+0
280  // else the output is                                              0.HpSD
281  // So testing the second position can differentiate between these two cases.
282  char* __first = __integral + 1;
283  if (*__first == '.') {
284    __result.__radix_point = __first;
285    // One digit is the minimum
286    // 0.hpSd
287    //       ^-- last
288    //     ^---- integral = end of search
289    // ^-------- start of search
290    // 0123456
291    //
292    // Four digits is the maximum
293    // 0.hpSdddd
294    //          ^-- last
295    //        ^---- integral = end of search
296    //    ^-------- start of search
297    // 0123456789
298    static_assert(__traits<_Fp>::__hex_precision_digits <= 4, "Guard against possible underflow.");
299
300    char* __last        = __result.__last - 2;
301    __first             = __last - __traits<_Fp>::__hex_precision_digits;
302    __result.__exponent = std::find(__first, __last, 'p');
303  } else {
304    __result.__radix_point = __result.__last;
305    __result.__exponent    = __first;
306  }
307
308  // clang-format off
309  _LIBCPP_ASSERT_INTERNAL((__result.__integral != __result.__last) &&
310                          (__result.__radix_point == __result.__last || *__result.__radix_point == '.') &&
311                          (__result.__exponent != __result.__last && *__result.__exponent == 'p'),
312                          "Post-condition failure.");
313  // clang-format on
314
315  return __result;
316}
317
318template <class _Fp, class _Tp>
319_LIBCPP_HIDE_FROM_ABI __float_result __format_buffer_hexadecimal_upper_case(
320    const __float_buffer<_Fp>& __buffer, _Tp __value, int __precision, char* __integral) {
321  __float_result __result =
322      __formatter::__format_buffer_hexadecimal_lower_case(__buffer, __value, __precision, __integral);
323  std::transform(__result.__integral, __result.__exponent, __result.__integral, __hex_to_upper);
324  *__result.__exponent = 'P';
325  return __result;
326}
327
328template <class _Fp, class _Tp>
329_LIBCPP_HIDE_FROM_ABI __float_result __format_buffer_scientific_lower_case(
330    const __float_buffer<_Fp>& __buffer, _Tp __value, int __precision, char* __integral) {
331  __float_result __result;
332  __result.__integral = __integral;
333  __result.__last =
334      __formatter::__to_buffer(__integral, __buffer.end(), __value, chars_format::scientific, __precision);
335
336  char* __first = __integral + 1;
337  _LIBCPP_ASSERT_INTERNAL(__first != __result.__last, "No exponent present");
338  if (*__first == '.') {
339    __result.__radix_point = __first;
340    __result.__exponent    = __formatter::__find_exponent(__first + 1, __result.__last);
341  } else {
342    __result.__radix_point = __result.__last;
343    __result.__exponent    = __first;
344  }
345
346  // clang-format off
347  _LIBCPP_ASSERT_INTERNAL((__result.__integral != __result.__last) &&
348                          (__result.__radix_point == __result.__last || *__result.__radix_point == '.') &&
349                          (__result.__exponent != __result.__last && *__result.__exponent == 'e'),
350                          "Post-condition failure.");
351  // clang-format on
352  return __result;
353}
354
355template <class _Fp, class _Tp>
356_LIBCPP_HIDE_FROM_ABI __float_result __format_buffer_scientific_upper_case(
357    const __float_buffer<_Fp>& __buffer, _Tp __value, int __precision, char* __integral) {
358  __float_result __result =
359      __formatter::__format_buffer_scientific_lower_case(__buffer, __value, __precision, __integral);
360  *__result.__exponent = 'E';
361  return __result;
362}
363
364template <class _Fp, class _Tp>
365_LIBCPP_HIDE_FROM_ABI __float_result
366__format_buffer_fixed(const __float_buffer<_Fp>& __buffer, _Tp __value, int __precision, char* __integral) {
367  __float_result __result;
368  __result.__integral = __integral;
369  __result.__last     = __formatter::__to_buffer(__integral, __buffer.end(), __value, chars_format::fixed, __precision);
370
371  // When there's no precision there's no radix point.
372  // Else the radix point is placed at __precision + 1 from the end.
373  // By converting __precision to a bool the subtraction can be done
374  // unconditionally.
375  __result.__radix_point = __result.__last - (__precision + bool(__precision));
376  __result.__exponent    = __result.__last;
377
378  // clang-format off
379  _LIBCPP_ASSERT_INTERNAL((__result.__integral != __result.__last) &&
380                          (__result.__radix_point == __result.__last || *__result.__radix_point == '.') &&
381                          (__result.__exponent == __result.__last),
382                          "Post-condition failure.");
383  // clang-format on
384  return __result;
385}
386
387template <class _Fp, class _Tp>
388_LIBCPP_HIDE_FROM_ABI __float_result
389__format_buffer_general_lower_case(__float_buffer<_Fp>& __buffer, _Tp __value, int __precision, char* __integral) {
390  __buffer.__remove_trailing_zeros();
391
392  __float_result __result;
393  __result.__integral = __integral;
394  __result.__last = __formatter::__to_buffer(__integral, __buffer.end(), __value, chars_format::general, __precision);
395
396  char* __first = __integral + 1;
397  if (__first == __result.__last) {
398    __result.__radix_point = __result.__last;
399    __result.__exponent    = __result.__last;
400  } else {
401    __result.__exponent = __formatter::__find_exponent(__first, __result.__last);
402    if (__result.__exponent != __result.__last)
403      // In scientific mode if there's a radix point it will always be after
404      // the first digit. (This is the position __first points at).
405      __result.__radix_point = *__first == '.' ? __first : __result.__last;
406    else {
407      // In fixed mode the algorithm truncates trailing spaces and possibly the
408      // radix point. There's no good guess for the position of the radix point
409      // therefore scan the output after the first digit.
410      __result.__radix_point = std::find(__first, __result.__last, '.');
411    }
412  }
413
414  // clang-format off
415  _LIBCPP_ASSERT_INTERNAL((__result.__integral != __result.__last) &&
416                          (__result.__radix_point == __result.__last || *__result.__radix_point == '.') &&
417                          (__result.__exponent == __result.__last || *__result.__exponent == 'e'),
418                          "Post-condition failure.");
419  // clang-format on
420
421  return __result;
422}
423
424template <class _Fp, class _Tp>
425_LIBCPP_HIDE_FROM_ABI __float_result
426__format_buffer_general_upper_case(__float_buffer<_Fp>& __buffer, _Tp __value, int __precision, char* __integral) {
427  __float_result __result = __formatter::__format_buffer_general_lower_case(__buffer, __value, __precision, __integral);
428  if (__result.__exponent != __result.__last)
429    *__result.__exponent = 'E';
430  return __result;
431}
432
433/// Fills the buffer with the data based on the requested formatting.
434///
435/// This function, when needed, turns the characters to upper case and
436/// determines the "interesting" locations which are returned to the caller.
437///
438/// This means the caller never has to convert the contents of the buffer to
439/// upper case or search for radix points and the location of the exponent.
440/// This gives a bit of overhead. The original code didn't do that, but due
441/// to the number of possible additional work needed to turn this number to
442/// the proper output the code was littered with tests for upper cases and
443/// searches for radix points and exponents.
444/// - When a precision larger than the type's precision is selected
445///   additional zero characters need to be written before the exponent.
446/// - alternate form needs to add a radix point when not present.
447/// - localization needs to do grouping in the integral part.
448template <class _Fp, class _Tp>
449// TODO FMT _Fp should just be _Tp when to_chars has proper long double support.
450_LIBCPP_HIDE_FROM_ABI __float_result __format_buffer(
451    __float_buffer<_Fp>& __buffer,
452    _Tp __value,
453    bool __negative,
454    bool __has_precision,
455    __format_spec::__sign __sign,
456    __format_spec::__type __type) {
457  char* __first = __formatter::__insert_sign(__buffer.begin(), __negative, __sign);
458  switch (__type) {
459  case __format_spec::__type::__default:
460    if (__has_precision)
461      return __formatter::__format_buffer_general_lower_case(__buffer, __value, __buffer.__precision(), __first);
462    else
463      return __formatter::__format_buffer_default(__buffer, __value, __first);
464
465  case __format_spec::__type::__hexfloat_lower_case:
466    return __formatter::__format_buffer_hexadecimal_lower_case(
467        __buffer, __value, __has_precision ? __buffer.__precision() : -1, __first);
468
469  case __format_spec::__type::__hexfloat_upper_case:
470    return __formatter::__format_buffer_hexadecimal_upper_case(
471        __buffer, __value, __has_precision ? __buffer.__precision() : -1, __first);
472
473  case __format_spec::__type::__scientific_lower_case:
474    return __formatter::__format_buffer_scientific_lower_case(__buffer, __value, __buffer.__precision(), __first);
475
476  case __format_spec::__type::__scientific_upper_case:
477    return __formatter::__format_buffer_scientific_upper_case(__buffer, __value, __buffer.__precision(), __first);
478
479  case __format_spec::__type::__fixed_lower_case:
480  case __format_spec::__type::__fixed_upper_case:
481    return __formatter::__format_buffer_fixed(__buffer, __value, __buffer.__precision(), __first);
482
483  case __format_spec::__type::__general_lower_case:
484    return __formatter::__format_buffer_general_lower_case(__buffer, __value, __buffer.__precision(), __first);
485
486  case __format_spec::__type::__general_upper_case:
487    return __formatter::__format_buffer_general_upper_case(__buffer, __value, __buffer.__precision(), __first);
488
489  default:
490    _LIBCPP_ASSERT_INTERNAL(false, "The parser should have validated the type");
491    __libcpp_unreachable();
492  }
493}
494
495#  if _LIBCPP_HAS_LOCALIZATION
496template <class _OutIt, class _Fp, class _CharT>
497_LIBCPP_HIDE_FROM_ABI _OutIt __format_locale_specific_form(
498    _OutIt __out_it,
499    const __float_buffer<_Fp>& __buffer,
500    const __float_result& __result,
501    std::locale __loc,
502    __format_spec::__parsed_specifications<_CharT> __specs) {
503  const auto& __np  = std::use_facet<numpunct<_CharT>>(__loc);
504  string __grouping = __np.grouping();
505  char* __first     = __result.__integral;
506  // When no radix point or exponent are present __last will be __result.__last.
507  char* __last = std::min(__result.__radix_point, __result.__exponent);
508
509  ptrdiff_t __digits = __last - __first;
510  if (!__grouping.empty()) {
511    if (__digits <= __grouping[0])
512      __grouping.clear();
513    else
514      __grouping = __formatter::__determine_grouping(__digits, __grouping);
515  }
516
517  ptrdiff_t __size =
518      __result.__last - __buffer.begin() + // Formatted string
519      __buffer.__num_trailing_zeros() +    // Not yet rendered zeros
520      __grouping.size() -                  // Grouping contains one
521      !__grouping.empty();                 // additional character
522
523  __formatter::__padding_size_result __padding = {0, 0};
524  bool __zero_padding                          = __specs.__alignment_ == __format_spec::__alignment::__zero_padding;
525  if (__size < __specs.__width_) {
526    if (__zero_padding) {
527      __specs.__alignment_      = __format_spec::__alignment::__right;
528      __specs.__fill_.__data[0] = _CharT('0');
529    }
530
531    __padding = __formatter::__padding_size(__size, __specs.__width_, __specs.__alignment_);
532  }
533
534  // sign and (zero padding or alignment)
535  if (__zero_padding && __first != __buffer.begin())
536    *__out_it++ = *__buffer.begin();
537  __out_it = __formatter::__fill(std::move(__out_it), __padding.__before_, __specs.__fill_);
538  if (!__zero_padding && __first != __buffer.begin())
539    *__out_it++ = *__buffer.begin();
540
541  // integral part
542  if (__grouping.empty()) {
543    __out_it = __formatter::__copy(__first, __digits, std::move(__out_it));
544  } else {
545    auto __r     = __grouping.rbegin();
546    auto __e     = __grouping.rend() - 1;
547    _CharT __sep = __np.thousands_sep();
548    // The output is divided in small groups of numbers to write:
549    // - A group before the first separator.
550    // - A separator and a group, repeated for the number of separators.
551    // - A group after the last separator.
552    // This loop achieves that process by testing the termination condition
553    // midway in the loop.
554    while (true) {
555      __out_it = __formatter::__copy(__first, *__r, std::move(__out_it));
556      __first += *__r;
557
558      if (__r == __e)
559        break;
560
561      ++__r;
562      *__out_it++ = __sep;
563    }
564  }
565
566  // fractional part
567  if (__result.__radix_point != __result.__last) {
568    *__out_it++ = __np.decimal_point();
569    __out_it    = __formatter::__copy(__result.__radix_point + 1, __result.__exponent, std::move(__out_it));
570    __out_it    = __formatter::__fill(std::move(__out_it), __buffer.__num_trailing_zeros(), _CharT('0'));
571  }
572
573  // exponent
574  if (__result.__exponent != __result.__last)
575    __out_it = __formatter::__copy(__result.__exponent, __result.__last, std::move(__out_it));
576
577  // alignment
578  return __formatter::__fill(std::move(__out_it), __padding.__after_, __specs.__fill_);
579}
580#  endif // _LIBCPP_HAS_LOCALIZATION
581
582template <class _OutIt, class _CharT>
583_LIBCPP_HIDE_FROM_ABI _OutIt __format_floating_point_non_finite(
584    _OutIt __out_it, __format_spec::__parsed_specifications<_CharT> __specs, bool __negative, bool __isnan) {
585  char __buffer[4];
586  char* __last = __formatter::__insert_sign(__buffer, __negative, __specs.__std_.__sign_);
587
588  // to_chars can return inf, infinity, nan, and nan(n-char-sequence).
589  // The format library requires inf and nan.
590  // All in one expression to avoid dangling references.
591  bool __upper_case =
592      __specs.__std_.__type_ == __format_spec::__type::__hexfloat_upper_case ||
593      __specs.__std_.__type_ == __format_spec::__type::__scientific_upper_case ||
594      __specs.__std_.__type_ == __format_spec::__type::__fixed_upper_case ||
595      __specs.__std_.__type_ == __format_spec::__type::__general_upper_case;
596  __last = std::copy_n(&("infnanINFNAN"[6 * __upper_case + 3 * __isnan]), 3, __last);
597
598  // [format.string.std]/13
599  // A zero (0) character preceding the width field pads the field with
600  // leading zeros (following any indication of sign or base) to the field
601  // width, except when applied to an infinity or NaN.
602  if (__specs.__alignment_ == __format_spec::__alignment::__zero_padding)
603    __specs.__alignment_ = __format_spec::__alignment::__right;
604
605  return __formatter::__write(__buffer, __last, std::move(__out_it), __specs);
606}
607
608/// Writes additional zero's for the precision before the exponent.
609/// This is used when the precision requested in the format string is larger
610/// than the maximum precision of the floating-point type. These precision
611/// digits are always 0.
612///
613/// \param __exponent           The location of the exponent character.
614/// \param __num_trailing_zeros The number of 0's to write before the exponent
615///                             character.
616template <class _CharT, class _ParserCharT>
617_LIBCPP_HIDE_FROM_ABI auto __write_using_trailing_zeros(
618    const _CharT* __first,
619    const _CharT* __last,
620    output_iterator<const _CharT&> auto __out_it,
621    __format_spec::__parsed_specifications<_ParserCharT> __specs,
622    size_t __size,
623    const _CharT* __exponent,
624    size_t __num_trailing_zeros) -> decltype(__out_it) {
625  _LIBCPP_ASSERT_INTERNAL(__first <= __last, "Not a valid range");
626  _LIBCPP_ASSERT_INTERNAL(__num_trailing_zeros > 0, "The overload not writing trailing zeros should have been used");
627
628  __padding_size_result __padding =
629      __formatter::__padding_size(__size + __num_trailing_zeros, __specs.__width_, __specs.__alignment_);
630  __out_it = __formatter::__fill(std::move(__out_it), __padding.__before_, __specs.__fill_);
631  __out_it = __formatter::__copy(__first, __exponent, std::move(__out_it));
632  __out_it = __formatter::__fill(std::move(__out_it), __num_trailing_zeros, _CharT('0'));
633  __out_it = __formatter::__copy(__exponent, __last, std::move(__out_it));
634  return __formatter::__fill(std::move(__out_it), __padding.__after_, __specs.__fill_);
635}
636
637template <floating_point _Tp, class _CharT, class _FormatContext>
638_LIBCPP_HIDE_FROM_ABI typename _FormatContext::iterator
639__format_floating_point(_Tp __value, _FormatContext& __ctx, __format_spec::__parsed_specifications<_CharT> __specs) {
640  bool __negative = std::signbit(__value);
641
642  if (!std::isfinite(__value)) [[unlikely]]
643    return __formatter::__format_floating_point_non_finite(__ctx.out(), __specs, __negative, std::isnan(__value));
644
645  // Depending on the std-format-spec string the sign and the value
646  // might not be outputted together:
647  // - zero-padding may insert additional '0' characters.
648  // Therefore the value is processed as a non negative value.
649  // The function @ref __insert_sign will insert a '-' when the value was
650  // negative.
651
652  if (__negative)
653    __value = -__value;
654
655  // TODO FMT _Fp should just be _Tp when to_chars has proper long double support.
656  using _Fp = conditional_t<same_as<_Tp, long double>, double, _Tp>;
657  // Force the type of the precision to avoid -1 to become an unsigned value.
658  __float_buffer<_Fp> __buffer(__specs.__precision_);
659  __float_result __result = __formatter::__format_buffer(
660      __buffer, __value, __negative, (__specs.__has_precision()), __specs.__std_.__sign_, __specs.__std_.__type_);
661
662  if (__specs.__std_.__alternate_form_) {
663    if (__result.__radix_point == __result.__last) {
664      *__result.__last++ = '.';
665
666      // When there is an exponent the point needs to be moved before the
667      // exponent. When there's no exponent the rotate does nothing. Since
668      // rotate tests whether the operation is a nop, call it unconditionally.
669      std::rotate(__result.__exponent, __result.__last - 1, __result.__last);
670      __result.__radix_point = __result.__exponent;
671
672      // The radix point is always placed before the exponent.
673      // - No exponent needs to point to the new last.
674      // - An exponent needs to move one position to the right.
675      // So it's safe to increment the value unconditionally.
676      ++__result.__exponent;
677    }
678
679    // [format.string.std]/6
680    //   In addition, for g and G conversions, trailing zeros are not removed
681    //   from the result.
682    //
683    // If the type option for a floating-point type is none it may use the
684    // general formatting, but it's not a g or G conversion. So in that case
685    // the formatting should not append trailing zeros.
686    bool __is_general = __specs.__std_.__type_ == __format_spec::__type::__general_lower_case ||
687                        __specs.__std_.__type_ == __format_spec::__type::__general_upper_case;
688
689    if (__is_general) {
690      // https://en.cppreference.com/w/c/io/fprintf
691      // Let P equal the precision if nonzero, 6 if the precision is not
692      // specified, or 1 if the precision is 0. Then, if a conversion with
693      // style E would have an exponent of X:
694      int __p = std::max<int>(1, (__specs.__has_precision() ? __specs.__precision_ : 6));
695      if (__result.__exponent == __result.__last)
696        // if P > X >= -4, the conversion is with style f or F and precision P - 1 - X.
697        // By including the radix point it calculates P - (1 + X)
698        __p -= __result.__radix_point - __result.__integral;
699      else
700        // otherwise, the conversion is with style e or E and precision P - 1.
701        --__p;
702
703      ptrdiff_t __precision = (__result.__exponent - __result.__radix_point) - 1;
704      if (__precision < __p)
705        __buffer.__add_trailing_zeros(__p - __precision);
706    }
707  }
708
709#  if _LIBCPP_HAS_LOCALIZATION
710  if (__specs.__std_.__locale_specific_form_)
711    return __formatter::__format_locale_specific_form(__ctx.out(), __buffer, __result, __ctx.locale(), __specs);
712#  endif
713
714  ptrdiff_t __size         = __result.__last - __buffer.begin();
715  int __num_trailing_zeros = __buffer.__num_trailing_zeros();
716  if (__size + __num_trailing_zeros >= __specs.__width_) {
717    if (__num_trailing_zeros && __result.__exponent != __result.__last)
718      // Insert trailing zeros before exponent character.
719      return __formatter::__copy(
720          __result.__exponent,
721          __result.__last,
722          __formatter::__fill(__formatter::__copy(__buffer.begin(), __result.__exponent, __ctx.out()),
723                              __num_trailing_zeros,
724                              _CharT('0')));
725
726    return __formatter::__fill(
727        __formatter::__copy(__buffer.begin(), __result.__last, __ctx.out()), __num_trailing_zeros, _CharT('0'));
728  }
729
730  auto __out_it = __ctx.out();
731  char* __first = __buffer.begin();
732  if (__specs.__alignment_ == __format_spec::__alignment ::__zero_padding) {
733    // When there is a sign output it before the padding. Note the __size
734    // doesn't need any adjustment, regardless whether the sign is written
735    // here or in __formatter::__write.
736    if (__first != __result.__integral)
737      *__out_it++ = *__first++;
738    // After the sign is written, zero padding is the same a right alignment
739    // with '0'.
740    __specs.__alignment_      = __format_spec::__alignment::__right;
741    __specs.__fill_.__data[0] = _CharT('0');
742  }
743
744  if (__num_trailing_zeros)
745    return __formatter::__write_using_trailing_zeros(
746        __first, __result.__last, std::move(__out_it), __specs, __size, __result.__exponent, __num_trailing_zeros);
747
748  return __formatter::__write(__first, __result.__last, std::move(__out_it), __specs, __size);
749}
750
751} // namespace __formatter
752
753template <__fmt_char_type _CharT>
754struct __formatter_floating_point {
755public:
756  template <class _ParseContext>
757  _LIBCPP_HIDE_FROM_ABI constexpr typename _ParseContext::iterator parse(_ParseContext& __ctx) {
758    typename _ParseContext::iterator __result = __parser_.__parse(__ctx, __format_spec::__fields_floating_point);
759    __format_spec::__process_parsed_floating_point(__parser_, "a floating-point");
760    return __result;
761  }
762
763  template <floating_point _Tp, class _FormatContext>
764  _LIBCPP_HIDE_FROM_ABI typename _FormatContext::iterator format(_Tp __value, _FormatContext& __ctx) const {
765    return __formatter::__format_floating_point(__value, __ctx, __parser_.__get_parsed_std_specifications(__ctx));
766  }
767
768  __format_spec::__parser<_CharT> __parser_;
769};
770
771template <__fmt_char_type _CharT>
772struct formatter<float, _CharT> : public __formatter_floating_point<_CharT> {};
773template <__fmt_char_type _CharT>
774struct formatter<double, _CharT> : public __formatter_floating_point<_CharT> {};
775template <__fmt_char_type _CharT>
776struct formatter<long double, _CharT> : public __formatter_floating_point<_CharT> {};
777
778#  if _LIBCPP_STD_VER >= 23
779template <>
780inline constexpr bool enable_nonlocking_formatter_optimization<float> = true;
781template <>
782inline constexpr bool enable_nonlocking_formatter_optimization<double> = true;
783template <>
784inline constexpr bool enable_nonlocking_formatter_optimization<long double> = true;
785#  endif // _LIBCPP_STD_VER >= 23
786#endif   // _LIBCPP_STD_VER >= 20
787
788_LIBCPP_END_NAMESPACE_STD
789
790_LIBCPP_POP_MACROS
791
792#endif // _LIBCPP___FORMAT_FORMATTER_FLOATING_POINT_H