xref: /aosp_15_r20/external/armnn/third-party/fmt/format-inl.h (revision 89c4ff92f2867872bb9e2354d150bf0c8c502810)
1 // Formatting library for C++ - implementation
2 //
3 // Copyright (c) 2012 - 2016, Victor Zverovich
4 // All rights reserved.
5 // SPDX-License-Identifier: MIT
6 //
7 // For the license information refer to format.h.
8 
9 #ifndef FMT_FORMAT_INL_H_
10 #define FMT_FORMAT_INL_H_
11 
12 #include <cassert>
13 #include <cctype>
14 #include <climits>
15 #include <cmath>
16 #include <cstdarg>
17 #include <cstring>  // for std::memmove
18 #include <cwchar>
19 #include <exception>
20 
21 #include "format.h"
22 #if !defined(FMT_STATIC_THOUSANDS_SEPARATOR)
23 #  include <locale>
24 #endif
25 
26 #ifdef _WIN32
27 #  if !defined(NOMINMAX) && !defined(WIN32_LEAN_AND_MEAN)
28 #    define NOMINMAX
29 #    define WIN32_LEAN_AND_MEAN
30 #    include <windows.h>
31 #    undef WIN32_LEAN_AND_MEAN
32 #    undef NOMINMAX
33 #  else
34 #    include <windows.h>
35 #  endif
36 #  include <io.h>
37 #endif
38 
39 #ifdef _MSC_VER
40 #  pragma warning(push)
41 #  pragma warning(disable : 4702)  // unreachable code
42 #endif
43 
44 // Dummy implementations of strerror_r and strerror_s called if corresponding
45 // system functions are not available.
strerror_r(int,char *,...)46 inline fmt::detail::null<> strerror_r(int, char*, ...) { return {}; }
strerror_s(char *,size_t,...)47 inline fmt::detail::null<> strerror_s(char*, size_t, ...) { return {}; }
48 
49 FMT_BEGIN_NAMESPACE
50 namespace detail {
51 
assert_fail(const char * file,int line,const char * message)52 FMT_FUNC void assert_fail(const char* file, int line, const char* message) {
53   // Use unchecked std::fprintf to avoid triggering another assertion when
54   // writing to stderr fails
55   std::fprintf(stderr, "%s:%d: assertion failed: %s", file, line, message);
56   // Chosen instead of std::abort to satisfy Clang in CUDA mode during device
57   // code pass.
58   std::terminate();
59 }
60 
61 #ifndef _MSC_VER
62 #  define FMT_SNPRINTF snprintf
63 #else  // _MSC_VER
fmt_snprintf(char * buffer,size_t size,const char * format,...)64 inline int fmt_snprintf(char* buffer, size_t size, const char* format, ...) {
65   va_list args;
66   va_start(args, format);
67   int result = vsnprintf_s(buffer, size, _TRUNCATE, format, args);
68   va_end(args);
69   return result;
70 }
71 #  define FMT_SNPRINTF fmt_snprintf
72 #endif  // _MSC_VER
73 
74 // A portable thread-safe version of strerror.
75 // Sets buffer to point to a string describing the error code.
76 // This can be either a pointer to a string stored in buffer,
77 // or a pointer to some static immutable string.
78 // Returns one of the following values:
79 //   0      - success
80 //   ERANGE - buffer is not large enough to store the error message
81 //   other  - failure
82 // Buffer should be at least of size 1.
safe_strerror(int error_code,char * & buffer,size_t buffer_size)83 FMT_FUNC int safe_strerror(int error_code, char*& buffer,
84                            size_t buffer_size) FMT_NOEXCEPT {
85   FMT_ASSERT(buffer != nullptr && buffer_size != 0, "invalid buffer");
86 
87   class dispatcher {
88    private:
89     int error_code_;
90     char*& buffer_;
91     size_t buffer_size_;
92 
93     // A noop assignment operator to avoid bogus warnings.
94     void operator=(const dispatcher&) {}
95 
96     // Handle the result of XSI-compliant version of strerror_r.
97     int handle(int result) {
98       // glibc versions before 2.13 return result in errno.
99       return result == -1 ? errno : result;
100     }
101 
102     // Handle the result of GNU-specific version of strerror_r.
103     FMT_MAYBE_UNUSED
104     int handle(char* message) {
105       // If the buffer is full then the message is probably truncated.
106       if (message == buffer_ && strlen(buffer_) == buffer_size_ - 1)
107         return ERANGE;
108       buffer_ = message;
109       return 0;
110     }
111 
112     // Handle the case when strerror_r is not available.
113     FMT_MAYBE_UNUSED
114     int handle(detail::null<>) {
115       return fallback(strerror_s(buffer_, buffer_size_, error_code_));
116     }
117 
118     // Fallback to strerror_s when strerror_r is not available.
119     FMT_MAYBE_UNUSED
120     int fallback(int result) {
121       // If the buffer is full then the message is probably truncated.
122       return result == 0 && strlen(buffer_) == buffer_size_ - 1 ? ERANGE
123                                                                 : result;
124     }
125 
126 #if !FMT_MSC_VER
127     // Fallback to strerror if strerror_r and strerror_s are not available.
128     int fallback(detail::null<>) {
129       errno = 0;
130       buffer_ = strerror(error_code_);
131       return errno;
132     }
133 #endif
134 
135    public:
136     dispatcher(int err_code, char*& buf, size_t buf_size)
137         : error_code_(err_code), buffer_(buf), buffer_size_(buf_size) {}
138 
139     int run() { return handle(strerror_r(error_code_, buffer_, buffer_size_)); }
140   };
141   return dispatcher(error_code, buffer, buffer_size).run();
142 }
143 
format_error_code(detail::buffer<char> & out,int error_code,string_view message)144 FMT_FUNC void format_error_code(detail::buffer<char>& out, int error_code,
145                                 string_view message) FMT_NOEXCEPT {
146   // Report error code making sure that the output fits into
147   // inline_buffer_size to avoid dynamic memory allocation and potential
148   // bad_alloc.
149   out.try_resize(0);
150   static const char SEP[] = ": ";
151   static const char ERROR_STR[] = "error ";
152   // Subtract 2 to account for terminating null characters in SEP and ERROR_STR.
153   size_t error_code_size = sizeof(SEP) + sizeof(ERROR_STR) - 2;
154   auto abs_value = static_cast<uint32_or_64_or_128_t<int>>(error_code);
155   if (detail::is_negative(error_code)) {
156     abs_value = 0 - abs_value;
157     ++error_code_size;
158   }
159   error_code_size += detail::to_unsigned(detail::count_digits(abs_value));
160   auto it = buffer_appender<char>(out);
161   if (message.size() <= inline_buffer_size - error_code_size)
162     format_to(it, "{}{}", message, SEP);
163   format_to(it, "{}{}", ERROR_STR, error_code);
164   assert(out.size() <= inline_buffer_size);
165 }
166 
report_error(format_func func,int error_code,string_view message)167 FMT_FUNC void report_error(format_func func, int error_code,
168                            string_view message) FMT_NOEXCEPT {
169   memory_buffer full_message;
170   func(full_message, error_code, message);
171   // Don't use fwrite_fully because the latter may throw.
172   (void)std::fwrite(full_message.data(), full_message.size(), 1, stderr);
173   std::fputc('\n', stderr);
174 }
175 
176 // A wrapper around fwrite that throws on error.
fwrite_fully(const void * ptr,size_t size,size_t count,FILE * stream)177 FMT_FUNC void fwrite_fully(const void* ptr, size_t size, size_t count,
178                            FILE* stream) {
179   size_t written = std::fwrite(ptr, size, count, stream);
180   if (written < count) FMT_THROW(system_error(errno, "cannot write to file"));
181 }
182 }  // namespace detail
183 
184 #if !defined(FMT_STATIC_THOUSANDS_SEPARATOR)
185 namespace detail {
186 
187 template <typename Locale>
locale_ref(const Locale & loc)188 locale_ref::locale_ref(const Locale& loc) : locale_(&loc) {
189   static_assert(std::is_same<Locale, std::locale>::value, "");
190 }
191 
get()192 template <typename Locale> Locale locale_ref::get() const {
193   static_assert(std::is_same<Locale, std::locale>::value, "");
194   return locale_ ? *static_cast<const std::locale*>(locale_) : std::locale();
195 }
196 
grouping_impl(locale_ref loc)197 template <typename Char> FMT_FUNC std::string grouping_impl(locale_ref loc) {
198   return std::use_facet<std::numpunct<Char>>(loc.get<std::locale>()).grouping();
199 }
thousands_sep_impl(locale_ref loc)200 template <typename Char> FMT_FUNC Char thousands_sep_impl(locale_ref loc) {
201   return std::use_facet<std::numpunct<Char>>(loc.get<std::locale>())
202       .thousands_sep();
203 }
decimal_point_impl(locale_ref loc)204 template <typename Char> FMT_FUNC Char decimal_point_impl(locale_ref loc) {
205   return std::use_facet<std::numpunct<Char>>(loc.get<std::locale>())
206       .decimal_point();
207 }
208 }  // namespace detail
209 #else
210 template <typename Char>
grouping_impl(locale_ref)211 FMT_FUNC std::string detail::grouping_impl(locale_ref) {
212   return "\03";
213 }
thousands_sep_impl(locale_ref)214 template <typename Char> FMT_FUNC Char detail::thousands_sep_impl(locale_ref) {
215   return FMT_STATIC_THOUSANDS_SEPARATOR;
216 }
decimal_point_impl(locale_ref)217 template <typename Char> FMT_FUNC Char detail::decimal_point_impl(locale_ref) {
218   return '.';
219 }
220 #endif
221 
222 FMT_API FMT_FUNC format_error::~format_error() FMT_NOEXCEPT = default;
223 FMT_API FMT_FUNC system_error::~system_error() FMT_NOEXCEPT = default;
224 
init(int err_code,string_view format_str,format_args args)225 FMT_FUNC void system_error::init(int err_code, string_view format_str,
226                                  format_args args) {
227   error_code_ = err_code;
228   memory_buffer buffer;
229   format_system_error(buffer, err_code, vformat(format_str, args));
230   std::runtime_error& base = *this;
231   base = std::runtime_error(to_string(buffer));
232 }
233 
234 namespace detail {
235 
236 template <> FMT_FUNC int count_digits<4>(detail::fallback_uintptr n) {
237   // fallback_uintptr is always stored in little endian.
238   int i = static_cast<int>(sizeof(void*)) - 1;
239   while (i > 0 && n.value[i] == 0) --i;
240   auto char_digits = std::numeric_limits<unsigned char>::digits / 4;
241   return i >= 0 ? i * char_digits + count_digits<4, unsigned>(n.value[i]) : 1;
242 }
243 
244 template <typename T>
245 const typename basic_data<T>::digit_pair basic_data<T>::digits[] = {
246     {'0', '0'}, {'0', '1'}, {'0', '2'}, {'0', '3'}, {'0', '4'}, {'0', '5'},
247     {'0', '6'}, {'0', '7'}, {'0', '8'}, {'0', '9'}, {'1', '0'}, {'1', '1'},
248     {'1', '2'}, {'1', '3'}, {'1', '4'}, {'1', '5'}, {'1', '6'}, {'1', '7'},
249     {'1', '8'}, {'1', '9'}, {'2', '0'}, {'2', '1'}, {'2', '2'}, {'2', '3'},
250     {'2', '4'}, {'2', '5'}, {'2', '6'}, {'2', '7'}, {'2', '8'}, {'2', '9'},
251     {'3', '0'}, {'3', '1'}, {'3', '2'}, {'3', '3'}, {'3', '4'}, {'3', '5'},
252     {'3', '6'}, {'3', '7'}, {'3', '8'}, {'3', '9'}, {'4', '0'}, {'4', '1'},
253     {'4', '2'}, {'4', '3'}, {'4', '4'}, {'4', '5'}, {'4', '6'}, {'4', '7'},
254     {'4', '8'}, {'4', '9'}, {'5', '0'}, {'5', '1'}, {'5', '2'}, {'5', '3'},
255     {'5', '4'}, {'5', '5'}, {'5', '6'}, {'5', '7'}, {'5', '8'}, {'5', '9'},
256     {'6', '0'}, {'6', '1'}, {'6', '2'}, {'6', '3'}, {'6', '4'}, {'6', '5'},
257     {'6', '6'}, {'6', '7'}, {'6', '8'}, {'6', '9'}, {'7', '0'}, {'7', '1'},
258     {'7', '2'}, {'7', '3'}, {'7', '4'}, {'7', '5'}, {'7', '6'}, {'7', '7'},
259     {'7', '8'}, {'7', '9'}, {'8', '0'}, {'8', '1'}, {'8', '2'}, {'8', '3'},
260     {'8', '4'}, {'8', '5'}, {'8', '6'}, {'8', '7'}, {'8', '8'}, {'8', '9'},
261     {'9', '0'}, {'9', '1'}, {'9', '2'}, {'9', '3'}, {'9', '4'}, {'9', '5'},
262     {'9', '6'}, {'9', '7'}, {'9', '8'}, {'9', '9'}};
263 
264 template <typename T>
265 const char basic_data<T>::hex_digits[] = "0123456789abcdef";
266 
267 #define FMT_POWERS_OF_10(factor)                                             \
268   factor * 10, (factor)*100, (factor)*1000, (factor)*10000, (factor)*100000, \
269       (factor)*1000000, (factor)*10000000, (factor)*100000000,               \
270       (factor)*1000000000
271 
272 template <typename T>
273 const uint64_t basic_data<T>::powers_of_10_64[] = {
274     1, FMT_POWERS_OF_10(1), FMT_POWERS_OF_10(1000000000ULL),
275     10000000000000000000ULL};
276 
277 template <typename T>
278 const uint32_t basic_data<T>::zero_or_powers_of_10_32[] = {0,
279                                                            FMT_POWERS_OF_10(1)};
280 
281 template <typename T>
282 const uint64_t basic_data<T>::zero_or_powers_of_10_64[] = {
283     0, FMT_POWERS_OF_10(1), FMT_POWERS_OF_10(1000000000ULL),
284     10000000000000000000ULL};
285 
286 // Normalized 64-bit significands of pow(10, k), for k = -348, -340, ..., 340.
287 // These are generated by support/compute-powers.py.
288 template <typename T>
289 const uint64_t basic_data<T>::pow10_significands[] = {
290     0xfa8fd5a0081c0288, 0xbaaee17fa23ebf76, 0x8b16fb203055ac76,
291     0xcf42894a5dce35ea, 0x9a6bb0aa55653b2d, 0xe61acf033d1a45df,
292     0xab70fe17c79ac6ca, 0xff77b1fcbebcdc4f, 0xbe5691ef416bd60c,
293     0x8dd01fad907ffc3c, 0xd3515c2831559a83, 0x9d71ac8fada6c9b5,
294     0xea9c227723ee8bcb, 0xaecc49914078536d, 0x823c12795db6ce57,
295     0xc21094364dfb5637, 0x9096ea6f3848984f, 0xd77485cb25823ac7,
296     0xa086cfcd97bf97f4, 0xef340a98172aace5, 0xb23867fb2a35b28e,
297     0x84c8d4dfd2c63f3b, 0xc5dd44271ad3cdba, 0x936b9fcebb25c996,
298     0xdbac6c247d62a584, 0xa3ab66580d5fdaf6, 0xf3e2f893dec3f126,
299     0xb5b5ada8aaff80b8, 0x87625f056c7c4a8b, 0xc9bcff6034c13053,
300     0x964e858c91ba2655, 0xdff9772470297ebd, 0xa6dfbd9fb8e5b88f,
301     0xf8a95fcf88747d94, 0xb94470938fa89bcf, 0x8a08f0f8bf0f156b,
302     0xcdb02555653131b6, 0x993fe2c6d07b7fac, 0xe45c10c42a2b3b06,
303     0xaa242499697392d3, 0xfd87b5f28300ca0e, 0xbce5086492111aeb,
304     0x8cbccc096f5088cc, 0xd1b71758e219652c, 0x9c40000000000000,
305     0xe8d4a51000000000, 0xad78ebc5ac620000, 0x813f3978f8940984,
306     0xc097ce7bc90715b3, 0x8f7e32ce7bea5c70, 0xd5d238a4abe98068,
307     0x9f4f2726179a2245, 0xed63a231d4c4fb27, 0xb0de65388cc8ada8,
308     0x83c7088e1aab65db, 0xc45d1df942711d9a, 0x924d692ca61be758,
309     0xda01ee641a708dea, 0xa26da3999aef774a, 0xf209787bb47d6b85,
310     0xb454e4a179dd1877, 0x865b86925b9bc5c2, 0xc83553c5c8965d3d,
311     0x952ab45cfa97a0b3, 0xde469fbd99a05fe3, 0xa59bc234db398c25,
312     0xf6c69a72a3989f5c, 0xb7dcbf5354e9bece, 0x88fcf317f22241e2,
313     0xcc20ce9bd35c78a5, 0x98165af37b2153df, 0xe2a0b5dc971f303a,
314     0xa8d9d1535ce3b396, 0xfb9b7cd9a4a7443c, 0xbb764c4ca7a44410,
315     0x8bab8eefb6409c1a, 0xd01fef10a657842c, 0x9b10a4e5e9913129,
316     0xe7109bfba19c0c9d, 0xac2820d9623bf429, 0x80444b5e7aa7cf85,
317     0xbf21e44003acdd2d, 0x8e679c2f5e44ff8f, 0xd433179d9c8cb841,
318     0x9e19db92b4e31ba9, 0xeb96bf6ebadf77d9, 0xaf87023b9bf0ee6b,
319 };
320 
321 // Binary exponents of pow(10, k), for k = -348, -340, ..., 340, corresponding
322 // to significands above.
323 template <typename T>
324 const int16_t basic_data<T>::pow10_exponents[] = {
325     -1220, -1193, -1166, -1140, -1113, -1087, -1060, -1034, -1007, -980, -954,
326     -927,  -901,  -874,  -847,  -821,  -794,  -768,  -741,  -715,  -688, -661,
327     -635,  -608,  -582,  -555,  -529,  -502,  -475,  -449,  -422,  -396, -369,
328     -343,  -316,  -289,  -263,  -236,  -210,  -183,  -157,  -130,  -103, -77,
329     -50,   -24,   3,     30,    56,    83,    109,   136,   162,   189,  216,
330     242,   269,   295,   322,   348,   375,   402,   428,   455,   481,  508,
331     534,   561,   588,   614,   641,   667,   694,   720,   747,   774,  800,
332     827,   853,   880,   907,   933,   960,   986,   1013,  1039,  1066};
333 
334 template <typename T>
335 const char basic_data<T>::foreground_color[] = "\x1b[38;2;";
336 template <typename T>
337 const char basic_data<T>::background_color[] = "\x1b[48;2;";
338 template <typename T> const char basic_data<T>::reset_color[] = "\x1b[0m";
339 template <typename T> const wchar_t basic_data<T>::wreset_color[] = L"\x1b[0m";
340 template <typename T> const char basic_data<T>::signs[] = {0, '-', '+', ' '};
341 template <typename T>
342 const char basic_data<T>::left_padding_shifts[] = {31, 31, 0, 1, 0};
343 template <typename T>
344 const char basic_data<T>::right_padding_shifts[] = {0, 31, 0, 1, 0};
345 
346 template <typename T> struct bits {
347   static FMT_CONSTEXPR_DECL const int value =
348       static_cast<int>(sizeof(T) * std::numeric_limits<unsigned char>::digits);
349 };
350 
351 class fp;
352 template <int SHIFT = 0> fp normalize(fp value);
353 
354 // Lower (upper) boundary is a value half way between a floating-point value
355 // and its predecessor (successor). Boundaries have the same exponent as the
356 // value so only significands are stored.
357 struct boundaries {
358   uint64_t lower;
359   uint64_t upper;
360 };
361 
362 // A handmade floating-point number f * pow(2, e).
363 class fp {
364  private:
365   using significand_type = uint64_t;
366 
367  public:
368   significand_type f;
369   int e;
370 
371   // All sizes are in bits.
372   // Subtract 1 to account for an implicit most significant bit in the
373   // normalized form.
374   static FMT_CONSTEXPR_DECL const int double_significand_size =
375       std::numeric_limits<double>::digits - 1;
376   static FMT_CONSTEXPR_DECL const uint64_t implicit_bit =
377       1ULL << double_significand_size;
378   static FMT_CONSTEXPR_DECL const int significand_size =
379       bits<significand_type>::value;
380 
fp()381   fp() : f(0), e(0) {}
fp(uint64_t f_val,int e_val)382   fp(uint64_t f_val, int e_val) : f(f_val), e(e_val) {}
383 
384   // Constructs fp from an IEEE754 double. It is a template to prevent compile
385   // errors on platforms where double is not IEEE754.
fp(Double d)386   template <typename Double> explicit fp(Double d) { assign(d); }
387 
388   // Assigns d to this and return true iff predecessor is closer than successor.
389   template <typename Double, FMT_ENABLE_IF(sizeof(Double) == sizeof(uint64_t))>
assign(Double d)390   bool assign(Double d) {
391     // Assume double is in the format [sign][exponent][significand].
392     using limits = std::numeric_limits<Double>;
393     const int exponent_size =
394         bits<Double>::value - double_significand_size - 1;  // -1 for sign
395     const uint64_t significand_mask = implicit_bit - 1;
396     const uint64_t exponent_mask = (~0ULL >> 1) & ~significand_mask;
397     const int exponent_bias = (1 << exponent_size) - limits::max_exponent - 1;
398     auto u = bit_cast<uint64_t>(d);
399     f = u & significand_mask;
400     int biased_e =
401         static_cast<int>((u & exponent_mask) >> double_significand_size);
402     // Predecessor is closer if d is a normalized power of 2 (f == 0) other than
403     // the smallest normalized number (biased_e > 1).
404     bool is_predecessor_closer = f == 0 && biased_e > 1;
405     if (biased_e != 0)
406       f += implicit_bit;
407     else
408       biased_e = 1;  // Subnormals use biased exponent 1 (min exponent).
409     e = biased_e - exponent_bias - double_significand_size;
410     return is_predecessor_closer;
411   }
412 
413   template <typename Double, FMT_ENABLE_IF(sizeof(Double) != sizeof(uint64_t))>
assign(Double)414   bool assign(Double) {
415     *this = fp();
416     return false;
417   }
418 
419   // Assigns d to this together with computing lower and upper boundaries,
420   // where a boundary is a value half way between the number and its predecessor
421   // (lower) or successor (upper). The upper boundary is normalized and lower
422   // has the same exponent but may be not normalized.
assign_with_boundaries(Double d)423   template <typename Double> boundaries assign_with_boundaries(Double d) {
424     bool is_lower_closer = assign(d);
425     fp lower =
426         is_lower_closer ? fp((f << 2) - 1, e - 2) : fp((f << 1) - 1, e - 1);
427     // 1 in normalize accounts for the exponent shift above.
428     fp upper = normalize<1>(fp((f << 1) + 1, e - 1));
429     lower.f <<= lower.e - upper.e;
430     return boundaries{lower.f, upper.f};
431   }
432 
assign_float_with_boundaries(Double d)433   template <typename Double> boundaries assign_float_with_boundaries(Double d) {
434     assign(d);
435     constexpr int min_normal_e = std::numeric_limits<float>::min_exponent -
436                                  std::numeric_limits<double>::digits;
437     significand_type half_ulp = 1 << (std::numeric_limits<double>::digits -
438                                       std::numeric_limits<float>::digits - 1);
439     if (min_normal_e > e) half_ulp <<= min_normal_e - e;
440     fp upper = normalize<0>(fp(f + half_ulp, e));
441     fp lower = fp(
442         f - (half_ulp >> ((f == implicit_bit && e > min_normal_e) ? 1 : 0)), e);
443     lower.f <<= lower.e - upper.e;
444     return boundaries{lower.f, upper.f};
445   }
446 };
447 
448 // Normalizes the value converted from double and multiplied by (1 << SHIFT).
normalize(fp value)449 template <int SHIFT> fp normalize(fp value) {
450   // Handle subnormals.
451   const auto shifted_implicit_bit = fp::implicit_bit << SHIFT;
452   while ((value.f & shifted_implicit_bit) == 0) {
453     value.f <<= 1;
454     --value.e;
455   }
456   // Subtract 1 to account for hidden bit.
457   const auto offset =
458       fp::significand_size - fp::double_significand_size - SHIFT - 1;
459   value.f <<= offset;
460   value.e -= offset;
461   return value;
462 }
463 
464 inline bool operator==(fp x, fp y) { return x.f == y.f && x.e == y.e; }
465 
466 // Computes lhs * rhs / pow(2, 64) rounded to nearest with half-up tie breaking.
multiply(uint64_t lhs,uint64_t rhs)467 inline uint64_t multiply(uint64_t lhs, uint64_t rhs) {
468 #if FMT_USE_INT128
469   auto product = static_cast<__uint128_t>(lhs) * rhs;
470   auto f = static_cast<uint64_t>(product >> 64);
471   return (static_cast<uint64_t>(product) & (1ULL << 63)) != 0 ? f + 1 : f;
472 #else
473   // Multiply 32-bit parts of significands.
474   uint64_t mask = (1ULL << 32) - 1;
475   uint64_t a = lhs >> 32, b = lhs & mask;
476   uint64_t c = rhs >> 32, d = rhs & mask;
477   uint64_t ac = a * c, bc = b * c, ad = a * d, bd = b * d;
478   // Compute mid 64-bit of result and round.
479   uint64_t mid = (bd >> 32) + (ad & mask) + (bc & mask) + (1U << 31);
480   return ac + (ad >> 32) + (bc >> 32) + (mid >> 32);
481 #endif
482 }
483 
484 inline fp operator*(fp x, fp y) { return {multiply(x.f, y.f), x.e + y.e + 64}; }
485 
486 // Returns a cached power of 10 `c_k = c_k.f * pow(2, c_k.e)` such that its
487 // (binary) exponent satisfies `min_exponent <= c_k.e <= min_exponent + 28`.
get_cached_power(int min_exponent,int & pow10_exponent)488 inline fp get_cached_power(int min_exponent, int& pow10_exponent) {
489   const int64_t one_over_log2_10 = 0x4d104d42;  // round(pow(2, 32) / log2(10))
490   int index = static_cast<int>(
491       ((min_exponent + fp::significand_size - 1) * one_over_log2_10 +
492        ((int64_t(1) << 32) - 1))  // ceil
493       >> 32                       // arithmetic shift
494   );
495   // Decimal exponent of the first (smallest) cached power of 10.
496   const int first_dec_exp = -348;
497   // Difference between 2 consecutive decimal exponents in cached powers of 10.
498   const int dec_exp_step = 8;
499   index = (index - first_dec_exp - 1) / dec_exp_step + 1;
500   pow10_exponent = first_dec_exp + index * dec_exp_step;
501   return {data::pow10_significands[index], data::pow10_exponents[index]};
502 }
503 
504 // A simple accumulator to hold the sums of terms in bigint::square if uint128_t
505 // is not available.
506 struct accumulator {
507   uint64_t lower;
508   uint64_t upper;
509 
accumulatoraccumulator510   accumulator() : lower(0), upper(0) {}
uint32_taccumulator511   explicit operator uint32_t() const { return static_cast<uint32_t>(lower); }
512 
513   void operator+=(uint64_t n) {
514     lower += n;
515     if (lower < n) ++upper;
516   }
517   void operator>>=(int shift) {
518     assert(shift == 32);
519     (void)shift;
520     lower = (upper << 32) | (lower >> 32);
521     upper >>= 32;
522   }
523 };
524 
525 class bigint {
526  private:
527   // A bigint is stored as an array of bigits (big digits), with bigit at index
528   // 0 being the least significant one.
529   using bigit = uint32_t;
530   using double_bigit = uint64_t;
531   enum { bigits_capacity = 32 };
532   basic_memory_buffer<bigit, bigits_capacity> bigits_;
533   int exp_;
534 
535   bigit operator[](int index) const { return bigits_[to_unsigned(index)]; }
536   bigit& operator[](int index) { return bigits_[to_unsigned(index)]; }
537 
538   static FMT_CONSTEXPR_DECL const int bigit_bits = bits<bigit>::value;
539 
540   friend struct formatter<bigint>;
541 
542   void subtract_bigits(int index, bigit other, bigit& borrow) {
543     auto result = static_cast<double_bigit>((*this)[index]) - other - borrow;
544     (*this)[index] = static_cast<bigit>(result);
545     borrow = static_cast<bigit>(result >> (bigit_bits * 2 - 1));
546   }
547 
548   void remove_leading_zeros() {
549     int num_bigits = static_cast<int>(bigits_.size()) - 1;
550     while (num_bigits > 0 && (*this)[num_bigits] == 0) --num_bigits;
551     bigits_.resize(to_unsigned(num_bigits + 1));
552   }
553 
554   // Computes *this -= other assuming aligned bigints and *this >= other.
555   void subtract_aligned(const bigint& other) {
556     FMT_ASSERT(other.exp_ >= exp_, "unaligned bigints");
557     FMT_ASSERT(compare(*this, other) >= 0, "");
558     bigit borrow = 0;
559     int i = other.exp_ - exp_;
560     for (size_t j = 0, n = other.bigits_.size(); j != n; ++i, ++j) {
561       subtract_bigits(i, other.bigits_[j], borrow);
562     }
563     while (borrow > 0) subtract_bigits(i, 0, borrow);
564     remove_leading_zeros();
565   }
566 
567   void multiply(uint32_t value) {
568     const double_bigit wide_value = value;
569     bigit carry = 0;
570     for (size_t i = 0, n = bigits_.size(); i < n; ++i) {
571       double_bigit result = bigits_[i] * wide_value + carry;
572       bigits_[i] = static_cast<bigit>(result);
573       carry = static_cast<bigit>(result >> bigit_bits);
574     }
575     if (carry != 0) bigits_.push_back(carry);
576   }
577 
578   void multiply(uint64_t value) {
579     const bigit mask = ~bigit(0);
580     const double_bigit lower = value & mask;
581     const double_bigit upper = value >> bigit_bits;
582     double_bigit carry = 0;
583     for (size_t i = 0, n = bigits_.size(); i < n; ++i) {
584       double_bigit result = bigits_[i] * lower + (carry & mask);
585       carry =
586           bigits_[i] * upper + (result >> bigit_bits) + (carry >> bigit_bits);
587       bigits_[i] = static_cast<bigit>(result);
588     }
589     while (carry != 0) {
590       bigits_.push_back(carry & mask);
591       carry >>= bigit_bits;
592     }
593   }
594 
595  public:
596   bigint() : exp_(0) {}
597   explicit bigint(uint64_t n) { assign(n); }
598   ~bigint() { assert(bigits_.capacity() <= bigits_capacity); }
599 
600   bigint(const bigint&) = delete;
601   void operator=(const bigint&) = delete;
602 
603   void assign(const bigint& other) {
604     auto size = other.bigits_.size();
605     bigits_.resize(size);
606     auto data = other.bigits_.data();
607     std::copy(data, data + size, make_checked(bigits_.data(), size));
608     exp_ = other.exp_;
609   }
610 
611   void assign(uint64_t n) {
612     size_t num_bigits = 0;
613     do {
614       bigits_[num_bigits++] = n & ~bigit(0);
615       n >>= bigit_bits;
616     } while (n != 0);
617     bigits_.resize(num_bigits);
618     exp_ = 0;
619   }
620 
621   int num_bigits() const { return static_cast<int>(bigits_.size()) + exp_; }
622 
623   FMT_NOINLINE bigint& operator<<=(int shift) {
624     assert(shift >= 0);
625     exp_ += shift / bigit_bits;
626     shift %= bigit_bits;
627     if (shift == 0) return *this;
628     bigit carry = 0;
629     for (size_t i = 0, n = bigits_.size(); i < n; ++i) {
630       bigit c = bigits_[i] >> (bigit_bits - shift);
631       bigits_[i] = (bigits_[i] << shift) + carry;
632       carry = c;
633     }
634     if (carry != 0) bigits_.push_back(carry);
635     return *this;
636   }
637 
638   template <typename Int> bigint& operator*=(Int value) {
639     FMT_ASSERT(value > 0, "");
640     multiply(uint32_or_64_or_128_t<Int>(value));
641     return *this;
642   }
643 
644   friend int compare(const bigint& lhs, const bigint& rhs) {
645     int num_lhs_bigits = lhs.num_bigits(), num_rhs_bigits = rhs.num_bigits();
646     if (num_lhs_bigits != num_rhs_bigits)
647       return num_lhs_bigits > num_rhs_bigits ? 1 : -1;
648     int i = static_cast<int>(lhs.bigits_.size()) - 1;
649     int j = static_cast<int>(rhs.bigits_.size()) - 1;
650     int end = i - j;
651     if (end < 0) end = 0;
652     for (; i >= end; --i, --j) {
653       bigit lhs_bigit = lhs[i], rhs_bigit = rhs[j];
654       if (lhs_bigit != rhs_bigit) return lhs_bigit > rhs_bigit ? 1 : -1;
655     }
656     if (i != j) return i > j ? 1 : -1;
657     return 0;
658   }
659 
660   // Returns compare(lhs1 + lhs2, rhs).
661   friend int add_compare(const bigint& lhs1, const bigint& lhs2,
662                          const bigint& rhs) {
663     int max_lhs_bigits = (std::max)(lhs1.num_bigits(), lhs2.num_bigits());
664     int num_rhs_bigits = rhs.num_bigits();
665     if (max_lhs_bigits + 1 < num_rhs_bigits) return -1;
666     if (max_lhs_bigits > num_rhs_bigits) return 1;
667     auto get_bigit = [](const bigint& n, int i) -> bigit {
668       return i >= n.exp_ && i < n.num_bigits() ? n[i - n.exp_] : 0;
669     };
670     double_bigit borrow = 0;
671     int min_exp = (std::min)((std::min)(lhs1.exp_, lhs2.exp_), rhs.exp_);
672     for (int i = num_rhs_bigits - 1; i >= min_exp; --i) {
673       double_bigit sum =
674           static_cast<double_bigit>(get_bigit(lhs1, i)) + get_bigit(lhs2, i);
675       bigit rhs_bigit = get_bigit(rhs, i);
676       if (sum > rhs_bigit + borrow) return 1;
677       borrow = rhs_bigit + borrow - sum;
678       if (borrow > 1) return -1;
679       borrow <<= bigit_bits;
680     }
681     return borrow != 0 ? -1 : 0;
682   }
683 
684   // Assigns pow(10, exp) to this bigint.
685   void assign_pow10(int exp) {
686     assert(exp >= 0);
687     if (exp == 0) return assign(1);
688     // Find the top bit.
689     int bitmask = 1;
690     while (exp >= bitmask) bitmask <<= 1;
691     bitmask >>= 1;
692     // pow(10, exp) = pow(5, exp) * pow(2, exp). First compute pow(5, exp) by
693     // repeated squaring and multiplication.
694     assign(5);
695     bitmask >>= 1;
696     while (bitmask != 0) {
697       square();
698       if ((exp & bitmask) != 0) *this *= 5;
699       bitmask >>= 1;
700     }
701     *this <<= exp;  // Multiply by pow(2, exp) by shifting.
702   }
703 
704   void square() {
705     basic_memory_buffer<bigit, bigits_capacity> n(std::move(bigits_));
706     int num_bigits = static_cast<int>(bigits_.size());
707     int num_result_bigits = 2 * num_bigits;
708     bigits_.resize(to_unsigned(num_result_bigits));
709     using accumulator_t = conditional_t<FMT_USE_INT128, uint128_t, accumulator>;
710     auto sum = accumulator_t();
711     for (int bigit_index = 0; bigit_index < num_bigits; ++bigit_index) {
712       // Compute bigit at position bigit_index of the result by adding
713       // cross-product terms n[i] * n[j] such that i + j == bigit_index.
714       for (int i = 0, j = bigit_index; j >= 0; ++i, --j) {
715         // Most terms are multiplied twice which can be optimized in the future.
716         sum += static_cast<double_bigit>(n[i]) * n[j];
717       }
718       (*this)[bigit_index] = static_cast<bigit>(sum);
719       sum >>= bits<bigit>::value;  // Compute the carry.
720     }
721     // Do the same for the top half.
722     for (int bigit_index = num_bigits; bigit_index < num_result_bigits;
723          ++bigit_index) {
724       for (int j = num_bigits - 1, i = bigit_index - j; i < num_bigits;)
725         sum += static_cast<double_bigit>(n[i++]) * n[j--];
726       (*this)[bigit_index] = static_cast<bigit>(sum);
727       sum >>= bits<bigit>::value;
728     }
729     --num_result_bigits;
730     remove_leading_zeros();
731     exp_ *= 2;
732   }
733 
734   // Divides this bignum by divisor, assigning the remainder to this and
735   // returning the quotient.
736   int divmod_assign(const bigint& divisor) {
737     FMT_ASSERT(this != &divisor, "");
738     if (compare(*this, divisor) < 0) return 0;
739     int num_bigits = static_cast<int>(bigits_.size());
740     FMT_ASSERT(divisor.bigits_[divisor.bigits_.size() - 1u] != 0, "");
741     int exp_difference = exp_ - divisor.exp_;
742     if (exp_difference > 0) {
743       // Align bigints by adding trailing zeros to simplify subtraction.
744       bigits_.resize(to_unsigned(num_bigits + exp_difference));
745       for (int i = num_bigits - 1, j = i + exp_difference; i >= 0; --i, --j)
746         bigits_[j] = bigits_[i];
747       std::uninitialized_fill_n(bigits_.data(), exp_difference, 0);
748       exp_ -= exp_difference;
749     }
750     int quotient = 0;
751     do {
752       subtract_aligned(divisor);
753       ++quotient;
754     } while (compare(*this, divisor) >= 0);
755     return quotient;
756   }
757 };
758 
759 enum class round_direction { unknown, up, down };
760 
761 // Given the divisor (normally a power of 10), the remainder = v % divisor for
762 // some number v and the error, returns whether v should be rounded up, down, or
763 // whether the rounding direction can't be determined due to error.
764 // error should be less than divisor / 2.
765 inline round_direction get_round_direction(uint64_t divisor, uint64_t remainder,
766                                            uint64_t error) {
767   FMT_ASSERT(remainder < divisor, "");  // divisor - remainder won't overflow.
768   FMT_ASSERT(error < divisor, "");      // divisor - error won't overflow.
769   FMT_ASSERT(error < divisor - error, "");  // error * 2 won't overflow.
770   // Round down if (remainder + error) * 2 <= divisor.
771   if (remainder <= divisor - remainder && error * 2 <= divisor - remainder * 2)
772     return round_direction::down;
773   // Round up if (remainder - error) * 2 >= divisor.
774   if (remainder >= error &&
775       remainder - error >= divisor - (remainder - error)) {
776     return round_direction::up;
777   }
778   return round_direction::unknown;
779 }
780 
781 namespace digits {
782 enum result {
783   more,  // Generate more digits.
784   done,  // Done generating digits.
785   error  // Digit generation cancelled due to an error.
786 };
787 }
788 
789 // A version of count_digits optimized for grisu_gen_digits.
790 inline int grisu_count_digits(uint32_t n) {
791   if (n < 10) return 1;
792   if (n < 100) return 2;
793   if (n < 1000) return 3;
794   if (n < 10000) return 4;
795   if (n < 100000) return 5;
796   if (n < 1000000) return 6;
797   if (n < 10000000) return 7;
798   if (n < 100000000) return 8;
799   if (n < 1000000000) return 9;
800   return 10;
801 }
802 
803 // Generates output using the Grisu digit-gen algorithm.
804 // error: the size of the region (lower, upper) outside of which numbers
805 // definitely do not round to value (Delta in Grisu3).
806 template <typename Handler>
807 FMT_ALWAYS_INLINE digits::result grisu_gen_digits(fp value, uint64_t error,
808                                                   int& exp, Handler& handler) {
809   const fp one(1ULL << -value.e, value.e);
810   // The integral part of scaled value (p1 in Grisu) = value / one. It cannot be
811   // zero because it contains a product of two 64-bit numbers with MSB set (due
812   // to normalization) - 1, shifted right by at most 60 bits.
813   auto integral = static_cast<uint32_t>(value.f >> -one.e);
814   FMT_ASSERT(integral != 0, "");
815   FMT_ASSERT(integral == value.f >> -one.e, "");
816   // The fractional part of scaled value (p2 in Grisu) c = value % one.
817   uint64_t fractional = value.f & (one.f - 1);
818   exp = grisu_count_digits(integral);  // kappa in Grisu.
819   // Divide by 10 to prevent overflow.
820   auto result = handler.on_start(data::powers_of_10_64[exp - 1] << -one.e,
821                                  value.f / 10, error * 10, exp);
822   if (result != digits::more) return result;
823   // Generate digits for the integral part. This can produce up to 10 digits.
824   do {
825     uint32_t digit = 0;
826     auto divmod_integral = [&](uint32_t divisor) {
827       digit = integral / divisor;
828       integral %= divisor;
829     };
830     // This optimization by Milo Yip reduces the number of integer divisions by
831     // one per iteration.
832     switch (exp) {
833     case 10:
834       divmod_integral(1000000000);
835       break;
836     case 9:
837       divmod_integral(100000000);
838       break;
839     case 8:
840       divmod_integral(10000000);
841       break;
842     case 7:
843       divmod_integral(1000000);
844       break;
845     case 6:
846       divmod_integral(100000);
847       break;
848     case 5:
849       divmod_integral(10000);
850       break;
851     case 4:
852       divmod_integral(1000);
853       break;
854     case 3:
855       divmod_integral(100);
856       break;
857     case 2:
858       divmod_integral(10);
859       break;
860     case 1:
861       digit = integral;
862       integral = 0;
863       break;
864     default:
865       FMT_ASSERT(false, "invalid number of digits");
866     }
867     --exp;
868     uint64_t remainder =
869         (static_cast<uint64_t>(integral) << -one.e) + fractional;
870     result = handler.on_digit(static_cast<char>('0' + digit),
871                               data::powers_of_10_64[exp] << -one.e, remainder,
872                               error, exp, true);
873     if (result != digits::more) return result;
874   } while (exp > 0);
875   // Generate digits for the fractional part.
876   for (;;) {
877     fractional *= 10;
878     error *= 10;
879     char digit =
880         static_cast<char>('0' + static_cast<char>(fractional >> -one.e));
881     fractional &= one.f - 1;
882     --exp;
883     result = handler.on_digit(digit, one.f, fractional, error, exp, false);
884     if (result != digits::more) return result;
885   }
886 }
887 
888 // The fixed precision digit handler.
889 struct fixed_handler {
890   char* buf;
891   int size;
892   int precision;
893   int exp10;
894   bool fixed;
895 
896   digits::result on_start(uint64_t divisor, uint64_t remainder, uint64_t error,
897                           int& exp) {
898     // Non-fixed formats require at least one digit and no precision adjustment.
899     if (!fixed) return digits::more;
900     // Adjust fixed precision by exponent because it is relative to decimal
901     // point.
902     precision += exp + exp10;
903     // Check if precision is satisfied just by leading zeros, e.g.
904     // format("{:.2f}", 0.001) gives "0.00" without generating any digits.
905     if (precision > 0) return digits::more;
906     if (precision < 0) return digits::done;
907     auto dir = get_round_direction(divisor, remainder, error);
908     if (dir == round_direction::unknown) return digits::error;
909     buf[size++] = dir == round_direction::up ? '1' : '0';
910     return digits::done;
911   }
912 
913   digits::result on_digit(char digit, uint64_t divisor, uint64_t remainder,
914                           uint64_t error, int, bool integral) {
915     FMT_ASSERT(remainder < divisor, "");
916     buf[size++] = digit;
917     if (size < precision) return digits::more;
918     if (!integral) {
919       // Check if error * 2 < divisor with overflow prevention.
920       // The check is not needed for the integral part because error = 1
921       // and divisor > (1 << 32) there.
922       if (error >= divisor || error >= divisor - error) return digits::error;
923     } else {
924       FMT_ASSERT(error == 1 && divisor > 2, "");
925     }
926     auto dir = get_round_direction(divisor, remainder, error);
927     if (dir != round_direction::up)
928       return dir == round_direction::down ? digits::done : digits::error;
929     ++buf[size - 1];
930     for (int i = size - 1; i > 0 && buf[i] > '9'; --i) {
931       buf[i] = '0';
932       ++buf[i - 1];
933     }
934     if (buf[0] > '9') {
935       buf[0] = '1';
936       buf[size++] = '0';
937     }
938     return digits::done;
939   }
940 };
941 
942 // The shortest representation digit handler.
943 struct grisu_shortest_handler {
944   char* buf;
945   int size;
946   // Distance between scaled value and upper bound (wp_W in Grisu3).
947   uint64_t diff;
948 
949   digits::result on_start(uint64_t, uint64_t, uint64_t, int&) {
950     return digits::more;
951   }
952 
953   // Decrement the generated number approaching value from above.
954   void round(uint64_t d, uint64_t divisor, uint64_t& remainder,
955              uint64_t error) {
956     while (
957         remainder < d && error - remainder >= divisor &&
958         (remainder + divisor < d || d - remainder >= remainder + divisor - d)) {
959       --buf[size - 1];
960       remainder += divisor;
961     }
962   }
963 
964   // Implements Grisu's round_weed.
965   digits::result on_digit(char digit, uint64_t divisor, uint64_t remainder,
966                           uint64_t error, int exp, bool integral) {
967     buf[size++] = digit;
968     if (remainder >= error) return digits::more;
969     uint64_t unit = integral ? 1 : data::powers_of_10_64[-exp];
970     uint64_t up = (diff - 1) * unit;  // wp_Wup
971     round(up, divisor, remainder, error);
972     uint64_t down = (diff + 1) * unit;  // wp_Wdown
973     if (remainder < down && error - remainder >= divisor &&
974         (remainder + divisor < down ||
975          down - remainder > remainder + divisor - down)) {
976       return digits::error;
977     }
978     return 2 * unit <= remainder && remainder <= error - 4 * unit
979                ? digits::done
980                : digits::error;
981   }
982 };
983 
984 // Formats value using a variation of the Fixed-Precision Positive
985 // Floating-Point Printout ((FPP)^2) algorithm by Steele & White:
986 // https://fmt.dev/p372-steele.pdf.
987 template <typename Double>
988 void fallback_format(Double d, buffer<char>& buf, int& exp10) {
989   bigint numerator;    // 2 * R in (FPP)^2.
990   bigint denominator;  // 2 * S in (FPP)^2.
991   // lower and upper are differences between value and corresponding boundaries.
992   bigint lower;             // (M^- in (FPP)^2).
993   bigint upper_store;       // upper's value if different from lower.
994   bigint* upper = nullptr;  // (M^+ in (FPP)^2).
995   fp value;
996   // Shift numerator and denominator by an extra bit or two (if lower boundary
997   // is closer) to make lower and upper integers. This eliminates multiplication
998   // by 2 during later computations.
999   // TODO: handle float
1000   int shift = value.assign(d) ? 2 : 1;
1001   uint64_t significand = value.f << shift;
1002   if (value.e >= 0) {
1003     numerator.assign(significand);
1004     numerator <<= value.e;
1005     lower.assign(1);
1006     lower <<= value.e;
1007     if (shift != 1) {
1008       upper_store.assign(1);
1009       upper_store <<= value.e + 1;
1010       upper = &upper_store;
1011     }
1012     denominator.assign_pow10(exp10);
1013     denominator <<= 1;
1014   } else if (exp10 < 0) {
1015     numerator.assign_pow10(-exp10);
1016     lower.assign(numerator);
1017     if (shift != 1) {
1018       upper_store.assign(numerator);
1019       upper_store <<= 1;
1020       upper = &upper_store;
1021     }
1022     numerator *= significand;
1023     denominator.assign(1);
1024     denominator <<= shift - value.e;
1025   } else {
1026     numerator.assign(significand);
1027     denominator.assign_pow10(exp10);
1028     denominator <<= shift - value.e;
1029     lower.assign(1);
1030     if (shift != 1) {
1031       upper_store.assign(1ULL << 1);
1032       upper = &upper_store;
1033     }
1034   }
1035   if (!upper) upper = &lower;
1036   // Invariant: value == (numerator / denominator) * pow(10, exp10).
1037   bool even = (value.f & 1) == 0;
1038   int num_digits = 0;
1039   char* data = buf.data();
1040   for (;;) {
1041     int digit = numerator.divmod_assign(denominator);
1042     bool low = compare(numerator, lower) - even < 0;  // numerator <[=] lower.
1043     // numerator + upper >[=] pow10:
1044     bool high = add_compare(numerator, *upper, denominator) + even > 0;
1045     data[num_digits++] = static_cast<char>('0' + digit);
1046     if (low || high) {
1047       if (!low) {
1048         ++data[num_digits - 1];
1049       } else if (high) {
1050         int result = add_compare(numerator, numerator, denominator);
1051         // Round half to even.
1052         if (result > 0 || (result == 0 && (digit % 2) != 0))
1053           ++data[num_digits - 1];
1054       }
1055       buf.try_resize(to_unsigned(num_digits));
1056       exp10 -= num_digits - 1;
1057       return;
1058     }
1059     numerator *= 10;
1060     lower *= 10;
1061     if (upper != &lower) *upper *= 10;
1062   }
1063 }
1064 
1065 // Formats value using the Grisu algorithm
1066 // (https://www.cs.tufts.edu/~nr/cs257/archive/florian-loitsch/printf.pdf)
1067 // if T is a IEEE754 binary32 or binary64 and snprintf otherwise.
1068 template <typename T>
1069 int format_float(T value, int precision, float_specs specs, buffer<char>& buf) {
1070   static_assert(!std::is_same<T, float>::value, "");
1071   FMT_ASSERT(value >= 0, "value is negative");
1072 
1073   const bool fixed = specs.format == float_format::fixed;
1074   if (value <= 0) {  // <= instead of == to silence a warning.
1075     if (precision <= 0 || !fixed) {
1076       buf.push_back('0');
1077       return 0;
1078     }
1079     buf.try_resize(to_unsigned(precision));
1080     std::uninitialized_fill_n(buf.data(), precision, '0');
1081     return -precision;
1082   }
1083 
1084   if (!specs.use_grisu) return snprintf_float(value, precision, specs, buf);
1085 
1086   int exp = 0;
1087   const int min_exp = -60;  // alpha in Grisu.
1088   int cached_exp10 = 0;     // K in Grisu.
1089   if (precision < 0) {
1090     fp fp_value;
1091     auto boundaries = specs.binary32
1092                           ? fp_value.assign_float_with_boundaries(value)
1093                           : fp_value.assign_with_boundaries(value);
1094     fp_value = normalize(fp_value);
1095     // Find a cached power of 10 such that multiplying value by it will bring
1096     // the exponent in the range [min_exp, -32].
1097     const fp cached_pow = get_cached_power(
1098         min_exp - (fp_value.e + fp::significand_size), cached_exp10);
1099     // Multiply value and boundaries by the cached power of 10.
1100     fp_value = fp_value * cached_pow;
1101     boundaries.lower = multiply(boundaries.lower, cached_pow.f);
1102     boundaries.upper = multiply(boundaries.upper, cached_pow.f);
1103     assert(min_exp <= fp_value.e && fp_value.e <= -32);
1104     --boundaries.lower;  // \tilde{M}^- - 1 ulp -> M^-_{\downarrow}.
1105     ++boundaries.upper;  // \tilde{M}^+ + 1 ulp -> M^+_{\uparrow}.
1106     // Numbers outside of (lower, upper) definitely do not round to value.
1107     grisu_shortest_handler handler{buf.data(), 0,
1108                                    boundaries.upper - fp_value.f};
1109     auto result =
1110         grisu_gen_digits(fp(boundaries.upper, fp_value.e),
1111                          boundaries.upper - boundaries.lower, exp, handler);
1112     if (result == digits::error) {
1113       exp += handler.size - cached_exp10 - 1;
1114       fallback_format(value, buf, exp);
1115       return exp;
1116     }
1117     buf.try_resize(to_unsigned(handler.size));
1118   } else {
1119     if (precision > 17) return snprintf_float(value, precision, specs, buf);
1120     fp normalized = normalize(fp(value));
1121     const auto cached_pow = get_cached_power(
1122         min_exp - (normalized.e + fp::significand_size), cached_exp10);
1123     normalized = normalized * cached_pow;
1124     fixed_handler handler{buf.data(), 0, precision, -cached_exp10, fixed};
1125     if (grisu_gen_digits(normalized, 1, exp, handler) == digits::error)
1126       return snprintf_float(value, precision, specs, buf);
1127     int num_digits = handler.size;
1128     if (!fixed) {
1129       // Remove trailing zeros.
1130       while (num_digits > 0 && buf[num_digits - 1] == '0') {
1131         --num_digits;
1132         ++exp;
1133       }
1134     }
1135     buf.try_resize(to_unsigned(num_digits));
1136   }
1137   return exp - cached_exp10;
1138 }
1139 
1140 template <typename T>
1141 int snprintf_float(T value, int precision, float_specs specs,
1142                    buffer<char>& buf) {
1143   // Buffer capacity must be non-zero, otherwise MSVC's vsnprintf_s will fail.
1144   FMT_ASSERT(buf.capacity() > buf.size(), "empty buffer");
1145   static_assert(!std::is_same<T, float>::value, "");
1146 
1147   // Subtract 1 to account for the difference in precision since we use %e for
1148   // both general and exponent format.
1149   if (specs.format == float_format::general ||
1150       specs.format == float_format::exp)
1151     precision = (precision >= 0 ? precision : 6) - 1;
1152 
1153   // Build the format string.
1154   enum { max_format_size = 7 };  // The longest format is "%#.*Le".
1155   char format[max_format_size];
1156   char* format_ptr = format;
1157   *format_ptr++ = '%';
1158   if (specs.showpoint && specs.format == float_format::hex) *format_ptr++ = '#';
1159   if (precision >= 0) {
1160     *format_ptr++ = '.';
1161     *format_ptr++ = '*';
1162   }
1163   if (std::is_same<T, long double>()) *format_ptr++ = 'L';
1164   *format_ptr++ = specs.format != float_format::hex
1165                       ? (specs.format == float_format::fixed ? 'f' : 'e')
1166                       : (specs.upper ? 'A' : 'a');
1167   *format_ptr = '\0';
1168 
1169   // Format using snprintf.
1170   auto offset = buf.size();
1171   for (;;) {
1172     auto begin = buf.data() + offset;
1173     auto capacity = buf.capacity() - offset;
1174 #ifdef FMT_FUZZ
1175     if (precision > 100000)
1176       throw std::runtime_error(
1177           "fuzz mode - avoid large allocation inside snprintf");
1178 #endif
1179     // Suppress the warning about a nonliteral format string.
1180     // Cannot use auto because of a bug in MinGW (#1532).
1181     int (*snprintf_ptr)(char*, size_t, const char*, ...) = FMT_SNPRINTF;
1182     int result = precision >= 0
1183                      ? snprintf_ptr(begin, capacity, format, precision, value)
1184                      : snprintf_ptr(begin, capacity, format, value);
1185     if (result < 0) {
1186       // The buffer will grow exponentially.
1187       buf.try_reserve(buf.capacity() + 1);
1188       continue;
1189     }
1190     auto size = to_unsigned(result);
1191     // Size equal to capacity means that the last character was truncated.
1192     if (size >= capacity) {
1193       buf.try_reserve(size + offset + 1);  // Add 1 for the terminating '\0'.
1194       continue;
1195     }
1196     auto is_digit = [](char c) { return c >= '0' && c <= '9'; };
1197     if (specs.format == float_format::fixed) {
1198       if (precision == 0) {
1199         buf.try_resize(size);
1200         return 0;
1201       }
1202       // Find and remove the decimal point.
1203       auto end = begin + size, p = end;
1204       do {
1205         --p;
1206       } while (is_digit(*p));
1207       int fraction_size = static_cast<int>(end - p - 1);
1208       std::memmove(p, p + 1, to_unsigned(fraction_size));
1209       buf.try_resize(size - 1);
1210       return -fraction_size;
1211     }
1212     if (specs.format == float_format::hex) {
1213       buf.try_resize(size + offset);
1214       return 0;
1215     }
1216     // Find and parse the exponent.
1217     auto end = begin + size, exp_pos = end;
1218     do {
1219       --exp_pos;
1220     } while (*exp_pos != 'e');
1221     char sign = exp_pos[1];
1222     assert(sign == '+' || sign == '-');
1223     int exp = 0;
1224     auto p = exp_pos + 2;  // Skip 'e' and sign.
1225     do {
1226       assert(is_digit(*p));
1227       exp = exp * 10 + (*p++ - '0');
1228     } while (p != end);
1229     if (sign == '-') exp = -exp;
1230     int fraction_size = 0;
1231     if (exp_pos != begin + 1) {
1232       // Remove trailing zeros.
1233       auto fraction_end = exp_pos - 1;
1234       while (*fraction_end == '0') --fraction_end;
1235       // Move the fractional part left to get rid of the decimal point.
1236       fraction_size = static_cast<int>(fraction_end - begin - 1);
1237       std::memmove(begin + 1, begin + 2, to_unsigned(fraction_size));
1238     }
1239     buf.try_resize(to_unsigned(fraction_size) + offset + 1);
1240     return exp - fraction_size;
1241   }
1242 }
1243 
1244 // A public domain branchless UTF-8 decoder by Christopher Wellons:
1245 // https://github.com/skeeto/branchless-utf8
1246 /* Decode the next character, c, from buf, reporting errors in e.
1247  *
1248  * Since this is a branchless decoder, four bytes will be read from the
1249  * buffer regardless of the actual length of the next character. This
1250  * means the buffer _must_ have at least three bytes of zero padding
1251  * following the end of the data stream.
1252  *
1253  * Errors are reported in e, which will be non-zero if the parsed
1254  * character was somehow invalid: invalid byte sequence, non-canonical
1255  * encoding, or a surrogate half.
1256  *
1257  * The function returns a pointer to the next character. When an error
1258  * occurs, this pointer will be a guess that depends on the particular
1259  * error, but it will always advance at least one byte.
1260  */
1261 FMT_FUNC const char* utf8_decode(const char* buf, uint32_t* c, int* e) {
1262   static const char lengths[] = {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1263                                  1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0,
1264                                  0, 0, 2, 2, 2, 2, 3, 3, 4, 0};
1265   static const int masks[] = {0x00, 0x7f, 0x1f, 0x0f, 0x07};
1266   static const uint32_t mins[] = {4194304, 0, 128, 2048, 65536};
1267   static const int shiftc[] = {0, 18, 12, 6, 0};
1268   static const int shifte[] = {0, 6, 4, 2, 0};
1269 
1270   auto s = reinterpret_cast<const unsigned char*>(buf);
1271   int len = lengths[s[0] >> 3];
1272 
1273   // Compute the pointer to the next character early so that the next
1274   // iteration can start working on the next character. Neither Clang
1275   // nor GCC figure out this reordering on their own.
1276   const char* next = buf + len + !len;
1277 
1278   // Assume a four-byte character and load four bytes. Unused bits are
1279   // shifted out.
1280   *c = uint32_t(s[0] & masks[len]) << 18;
1281   *c |= uint32_t(s[1] & 0x3f) << 12;
1282   *c |= uint32_t(s[2] & 0x3f) << 6;
1283   *c |= uint32_t(s[3] & 0x3f) << 0;
1284   *c >>= shiftc[len];
1285 
1286   // Accumulate the various error conditions.
1287   *e = (*c < mins[len]) << 6;       // non-canonical encoding
1288   *e |= ((*c >> 11) == 0x1b) << 7;  // surrogate half?
1289   *e |= (*c > 0x10FFFF) << 8;       // out of range?
1290   *e |= (s[1] & 0xc0) >> 2;
1291   *e |= (s[2] & 0xc0) >> 4;
1292   *e |= (s[3]) >> 6;
1293   *e ^= 0x2a;  // top two bits of each tail byte correct?
1294   *e >>= shifte[len];
1295 
1296   return next;
1297 }
1298 
1299 struct stringifier {
1300   template <typename T> FMT_INLINE std::string operator()(T value) const {
1301     return to_string(value);
1302   }
1303   std::string operator()(basic_format_arg<format_context>::handle h) const {
1304     memory_buffer buf;
1305     format_parse_context parse_ctx({});
1306     format_context format_ctx(buffer_appender<char>(buf), {}, {});
1307     h.format(parse_ctx, format_ctx);
1308     return to_string(buf);
1309   }
1310 };
1311 }  // namespace detail
1312 
1313 template <> struct formatter<detail::bigint> {
1314   format_parse_context::iterator parse(format_parse_context& ctx) {
1315     return ctx.begin();
1316   }
1317 
1318   format_context::iterator format(const detail::bigint& n,
1319                                   format_context& ctx) {
1320     auto out = ctx.out();
1321     bool first = true;
1322     for (auto i = n.bigits_.size(); i > 0; --i) {
1323       auto value = n.bigits_[i - 1u];
1324       if (first) {
1325         out = format_to(out, "{:x}", value);
1326         first = false;
1327         continue;
1328       }
1329       out = format_to(out, "{:08x}", value);
1330     }
1331     if (n.exp_ > 0)
1332       out = format_to(out, "p{}", n.exp_ * detail::bigint::bigit_bits);
1333     return out;
1334   }
1335 };
1336 
1337 FMT_FUNC detail::utf8_to_utf16::utf8_to_utf16(string_view s) {
1338   auto transcode = [this](const char* p) {
1339     auto cp = uint32_t();
1340     auto error = 0;
1341     p = utf8_decode(p, &cp, &error);
1342     if (error != 0) FMT_THROW(std::runtime_error("invalid utf8"));
1343     if (cp <= 0xFFFF) {
1344       buffer_.push_back(static_cast<wchar_t>(cp));
1345     } else {
1346       cp -= 0x10000;
1347       buffer_.push_back(static_cast<wchar_t>(0xD800 + (cp >> 10)));
1348       buffer_.push_back(static_cast<wchar_t>(0xDC00 + (cp & 0x3FF)));
1349     }
1350     return p;
1351   };
1352   auto p = s.data();
1353   const size_t block_size = 4;  // utf8_decode always reads blocks of 4 chars.
1354   if (s.size() >= block_size) {
1355     for (auto end = p + s.size() - block_size + 1; p < end;) p = transcode(p);
1356   }
1357   if (auto num_chars_left = s.data() + s.size() - p) {
1358     char buf[2 * block_size - 1] = {};
1359     memcpy(buf, p, to_unsigned(num_chars_left));
1360     p = buf;
1361     do {
1362       p = transcode(p);
1363     } while (p - buf < num_chars_left);
1364   }
1365   buffer_.push_back(0);
1366 }
1367 
1368 FMT_FUNC void format_system_error(detail::buffer<char>& out, int error_code,
1369                                   string_view message) FMT_NOEXCEPT {
1370   FMT_TRY {
1371     memory_buffer buf;
1372     buf.resize(inline_buffer_size);
1373     for (;;) {
1374       char* system_message = &buf[0];
1375       int result =
1376           detail::safe_strerror(error_code, system_message, buf.size());
1377       if (result == 0) {
1378         format_to(detail::buffer_appender<char>(out), "{}: {}", message,
1379                   system_message);
1380         return;
1381       }
1382       if (result != ERANGE)
1383         break;  // Can't get error message, report error code instead.
1384       buf.resize(buf.size() * 2);
1385     }
1386   }
1387   FMT_CATCH(...) {}
1388   format_error_code(out, error_code, message);
1389 }
1390 
1391 FMT_FUNC void detail::error_handler::on_error(const char* message) {
1392   FMT_THROW(format_error(message));
1393 }
1394 
1395 FMT_FUNC void report_system_error(int error_code,
1396                                   fmt::string_view message) FMT_NOEXCEPT {
1397   report_error(format_system_error, error_code, message);
1398 }
1399 
1400 FMT_FUNC std::string detail::vformat(string_view format_str, format_args args) {
1401   if (format_str.size() == 2 && equal2(format_str.data(), "{}")) {
1402     auto arg = args.get(0);
1403     if (!arg) error_handler().on_error("argument not found");
1404     return visit_format_arg(stringifier(), arg);
1405   }
1406   memory_buffer buffer;
1407   detail::vformat_to(buffer, format_str, args);
1408   return to_string(buffer);
1409 }
1410 
1411 FMT_FUNC void vprint(std::FILE* f, string_view format_str, format_args args) {
1412   memory_buffer buffer;
1413   detail::vformat_to(buffer, format_str,
1414                      basic_format_args<buffer_context<char>>(args));
1415 #ifdef _WIN32
1416   auto fd = _fileno(f);
1417   if (_isatty(fd)) {
1418     detail::utf8_to_utf16 u16(string_view(buffer.data(), buffer.size()));
1419     auto written = DWORD();
1420     if (!WriteConsoleW(reinterpret_cast<HANDLE>(_get_osfhandle(fd)),
1421                        u16.c_str(), static_cast<DWORD>(u16.size()), &written,
1422                        nullptr)) {
1423       FMT_THROW(format_error("failed to write to console"));
1424     }
1425     return;
1426   }
1427 #endif
1428   detail::fwrite_fully(buffer.data(), 1, buffer.size(), f);
1429 }
1430 
1431 #ifdef _WIN32
1432 // Print assuming legacy (non-Unicode) encoding.
1433 FMT_FUNC void detail::vprint_mojibake(std::FILE* f, string_view format_str,
1434                                       format_args args) {
1435   memory_buffer buffer;
1436   detail::vformat_to(buffer, format_str,
1437                      basic_format_args<buffer_context<char>>(args));
1438   fwrite_fully(buffer.data(), 1, buffer.size(), f);
1439 }
1440 #endif
1441 
1442 FMT_FUNC void vprint(string_view format_str, format_args args) {
1443   vprint(stdout, format_str, args);
1444 }
1445 
1446 FMT_END_NAMESPACE
1447 
1448 #ifdef _MSC_VER
1449 #  pragma warning(pop)
1450 #endif
1451 
1452 #endif  // FMT_FORMAT_INL_H_
1453