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