-
Notifications
You must be signed in to change notification settings - Fork 25
Expand file tree
/
Copy pathto_chars.cpp
More file actions
755 lines (647 loc) · 30.4 KB
/
to_chars.cpp
File metadata and controls
755 lines (647 loc) · 30.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
// Copyright 2020-2023 Junekey Jeon
// Copyright 2022 Peter Dimov
// Copyright 2023 Matt Borland
// Distributed under the Boost Software License, Version 1.0.
// https://www.boost.org/LICENSE_1_0.txt
#include "float128_impl.hpp"
#include "to_chars_float_impl.hpp"
#include <boost/charconv/to_chars.hpp>
#include <boost/charconv/chars_format.hpp>
#include <limits>
#include <cstring>
#include <cstdio>
#include <cstdint>
#include <cmath>
namespace boost { namespace charconv { namespace detail { namespace to_chars_detail {
#ifdef BOOST_MSVC
# pragma warning(push)
# pragma warning(disable: 4127) // Conditional expression is constant (e.g. BOOST_IF_CONSTEXPR statements)
#endif
// These "//"'s are to prevent clang-format to ruin this nice alignment.
// Thanks to reddit user u/mcmcc:
// https://www.reddit.com/r/cpp/comments/so3wx9/dragonbox_110_is_released_a_fast_floattostring/hw8z26r/?context=3
static constexpr char radix_100_head_table[] = {
'0', '.', '1', '.', '2', '.', '3', '.', '4', '.', //
'5', '.', '6', '.', '7', '.', '8', '.', '9', '.', //
'1', '.', '1', '.', '1', '.', '1', '.', '1', '.', //
'1', '.', '1', '.', '1', '.', '1', '.', '1', '.', //
'2', '.', '2', '.', '2', '.', '2', '.', '2', '.', //
'2', '.', '2', '.', '2', '.', '2', '.', '2', '.', //
'3', '.', '3', '.', '3', '.', '3', '.', '3', '.', //
'3', '.', '3', '.', '3', '.', '3', '.', '3', '.', //
'4', '.', '4', '.', '4', '.', '4', '.', '4', '.', //
'4', '.', '4', '.', '4', '.', '4', '.', '4', '.', //
'5', '.', '5', '.', '5', '.', '5', '.', '5', '.', //
'5', '.', '5', '.', '5', '.', '5', '.', '5', '.', //
'6', '.', '6', '.', '6', '.', '6', '.', '6', '.', //
'6', '.', '6', '.', '6', '.', '6', '.', '6', '.', //
'7', '.', '7', '.', '7', '.', '7', '.', '7', '.', //
'7', '.', '7', '.', '7', '.', '7', '.', '7', '.', //
'8', '.', '8', '.', '8', '.', '8', '.', '8', '.', //
'8', '.', '8', '.', '8', '.', '8', '.', '8', '.', //
'9', '.', '9', '.', '9', '.', '9', '.', '9', '.', //
'9', '.', '9', '.', '9', '.', '9', '.', '9', '.' //
};
static void print_1_digit(std::uint32_t n, char* buffer) noexcept
{
*buffer = char('0' + n);
}
static void print_2_digits(std::uint32_t n, char* buffer) noexcept
{
std::memcpy(buffer, radix_table + n * 2, 2);
}
// These digit generation routines are inspired by James Anhalt's itoa algorithm:
// https://github.com/jeaiii/itoa
// The main idea is for given n, find y such that floor(10^k * y / 2^32) = n holds,
// where k is an appropriate integer depending on the length of n.
// For example, if n = 1234567, we set k = 6. In this case, we have
// floor(y / 2^32) = 1,
// floor(10^2 * ((10^0 * y) mod 2^32) / 2^32) = 23,
// floor(10^2 * ((10^2 * y) mod 2^32) / 2^32) = 45, and
// floor(10^2 * ((10^4 * y) mod 2^32) / 2^32) = 67.
// See https://jk-jeon.github.io/posts/2022/02/jeaiii-algorithm/ for more explanation.
BOOST_FORCEINLINE static void print_9_digits(std::uint32_t s32, int& exponent,
char*& buffer) noexcept
{
// -- IEEE-754 binary32
// Since we do not cut trailing zeros in advance, s32 must be of 6~9 digits
// unless the original input was subnormal.
// In particular, when it is of 9 digits it shouldn't have any trailing zeros.
// -- IEEE-754 binary64
// In this case, s32 must be of 7~9 digits unless the input is subnormal,
// and it shouldn't have any trailing zeros if it is of 9 digits.
if (s32 >= 100000000)
{
// 9 digits.
// 1441151882 = ceil(2^57 / 1'0000'0000) + 1
auto prod = s32 * std::uint64_t(1441151882);
prod >>= 25;
std::memcpy(buffer, radix_100_head_table + std::uint32_t(prod >> 32) * 2, 2);
prod = std::uint32_t(prod) * std::uint64_t(100);
print_2_digits(std::uint32_t(prod >> 32), buffer + 2);
prod = std::uint32_t(prod) * std::uint64_t(100);
print_2_digits(std::uint32_t(prod >> 32), buffer + 4);
prod = std::uint32_t(prod) * std::uint64_t(100);
print_2_digits(std::uint32_t(prod >> 32), buffer + 6);
prod = std::uint32_t(prod) * std::uint64_t(100);
print_2_digits(std::uint32_t(prod >> 32), buffer + 8);
exponent += 8;
buffer += 10;
}
else if (s32 >= 1000000)
{
// 7 or 8 digits.
// 281474978 = ceil(2^48 / 100'0000) + 1
auto prod = s32 * std::uint64_t(281474978);
prod >>= 16;
const auto head_digits = std::uint32_t(prod >> 32);
// If s32 is of 8 digits, increase the exponent by 7.
// Otherwise, increase it by 6.
exponent += static_cast<int>(6 + unsigned(head_digits >= 10));
// Write the first digit and the decimal point.
std::memcpy(buffer, radix_100_head_table + head_digits * 2, 2);
// This third character may be overwritten later, but we don't care.
buffer[2] = radix_table[head_digits * 2 + 1];
// Remaining 6 digits are all zero?
if (std::uint32_t(prod) <= std::uint32_t((std::uint64_t(1) << 32) / 1000000))
{
// The number of characters actually need to be written is:
// 1, if only the first digit is nonzero, which means that either s32 is of 7
// digits or it is of 8 digits but the second digit is zero, or
// 3, otherwise.
// Note that buffer[2] is never '0' if s32 is of 7 digits, because the input is
// never zero.
buffer += (1 + (unsigned(head_digits >= 10) & unsigned(buffer[2] > '0')) * 2);
}
else
{
// At least one of the remaining 6 digits are nonzero.
// After this adjustment, now the first destination becomes buffer + 2.
buffer += unsigned(head_digits >= 10);
// Obtain the next two digits.
prod = std::uint32_t(prod) * std::uint64_t(100);
print_2_digits(std::uint32_t(prod >> 32), buffer + 2);
// Remaining 4 digits are all zero?
if (std::uint32_t(prod) <= std::uint32_t((std::uint64_t(1) << 32) / 10000))
{
buffer += (3 + unsigned(buffer[3] > '0'));
}
else
{
// At least one of the remaining 4 digits are nonzero.
// Obtain the next two digits.
prod = std::uint32_t(prod) * std::uint64_t(100);
print_2_digits(std::uint32_t(prod >> 32), buffer + 4);
// Remaining 2 digits are all zero?
if (std::uint32_t(prod) <= std::uint32_t((std::uint64_t(1) << 32) / 100))
{
buffer += (5 + unsigned(buffer[5] > '0'));
}
else
{
// Obtain the last two digits.
prod = std::uint32_t(prod) * std::uint64_t(100);
print_2_digits(std::uint32_t(prod >> 32), buffer + 6);
buffer += (7 + unsigned(buffer[7] > '0'));
}
}
}
}
else if (s32 >= 10000)
{
// 5 or 6 digits.
// 429497 = ceil(2^32 / 1'0000)
auto prod = s32 * std::uint64_t(429497);
const auto head_digits = std::uint32_t(prod >> 32);
// If s32 is of 6 digits, increase the exponent by 5.
// Otherwise, increase it by 4.
exponent += static_cast<int>(4 + unsigned(head_digits >= 10));
// Write the first digit and the decimal point.
std::memcpy(buffer, radix_100_head_table + head_digits * 2, 2);
// This third character may be overwritten later but we don't care.
buffer[2] = radix_table[head_digits * 2 + 1];
// Remaining 4 digits are all zero?
if (std::uint32_t(prod) <= std::uint32_t((std::uint64_t(1) << 32) / 10000))
{
// The number of characters actually written is 1 or 3, similarly to the case of
// 7 or 8 digits.
buffer += (1 + (unsigned(head_digits >= 10) & unsigned(buffer[2] > '0')) * 2);
}
else
{
// At least one of the remaining 4 digits are nonzero.
// After this adjustment, now the first destination becomes buffer + 2.
buffer += unsigned(head_digits >= 10);
// Obtain the next two digits.
prod = std::uint32_t(prod) * std::uint64_t(100);
print_2_digits(std::uint32_t(prod >> 32), buffer + 2);
// Remaining 2 digits are all zero?
if (std::uint32_t(prod) <= std::uint32_t((std::uint64_t(1) << 32) / 100))
{
buffer += (3 + unsigned(buffer[3] > '0'));
}
else
{
// Obtain the last two digits.
prod = std::uint32_t(prod) * std::uint64_t(100);
print_2_digits(std::uint32_t(prod >> 32), buffer + 4);
buffer += (5 + unsigned(buffer[5] > '0'));
}
}
}
else if (s32 >= 100)
{
// 3 or 4 digits.
// 42949673 = ceil(2^32 / 100)
auto prod = s32 * std::uint64_t(42949673);
const auto head_digits = std::uint32_t(prod >> 32);
// If s32 is of 4 digits, increase the exponent by 3.
// Otherwise, increase it by 2.
exponent += (2 + int(head_digits >= 10));
// Write the first digit and the decimal point.
std::memcpy(buffer, radix_100_head_table + head_digits * 2, 2);
// This third character may be overwritten later but we don't care.
buffer[2] = radix_table[head_digits * 2 + 1];
// Remaining 2 digits are all zero?
if (std::uint32_t(prod) <= std::uint32_t((std::uint64_t(1) << 32) / 100))
{
// The number of characters actually written is 1 or 3, similarly to the case of
// 7 or 8 digits.
buffer += (1 + (unsigned(head_digits >= 10) & unsigned(buffer[2] > '0')) * 2);
}
else
{
// At least one of the remaining 2 digits are nonzero.
// After this adjustment, now the first destination becomes buffer + 2.
buffer += unsigned(head_digits >= 10);
// Obtain the last two digits.
prod = std::uint32_t(prod) * std::uint64_t(100);
print_2_digits(std::uint32_t(prod >> 32), buffer + 2);
buffer += (3 + unsigned(buffer[3] > '0'));
}
}
else
{
// 1 or 2 digits.
// If s32 is of 2 digits, increase the exponent by 1.
exponent += int(s32 >= 10);
// Write the first digit and the decimal point.
std::memcpy(buffer, radix_100_head_table + s32 * 2, 2);
// This third character may be overwritten later but we don't care.
buffer[2] = radix_table[s32 * 2 + 1];
// The number of characters actually written is 1 or 3, similarly to the case of
// 7 or 8 digits.
buffer += (1 + (unsigned(s32 >= 10) & unsigned(buffer[2] > '0')) * 2);
}
}
template <>
to_chars_result dragon_box_print_chars<float, dragonbox_float_traits<float>>(std::uint32_t s32, int exponent, char* first, char* last, chars_format fmt) noexcept
{
auto buffer = first;
const std::ptrdiff_t total_length = total_buffer_length(9, exponent, false);
if (total_length > (last - first))
{
return {last, std::errc::value_too_large};
}
// Print significand.
print_9_digits(s32, exponent, buffer);
// Print exponent and return
if (exponent < 0)
{
std::memcpy(buffer, "e-", 2);
buffer += 2;
exponent = -exponent;
}
else if (exponent == 0)
{
if (fmt == chars_format::scientific)
{
std::memcpy(buffer, "e+00", 4);
buffer += 4;
}
return {buffer, std::errc()};
}
else
{
std::memcpy(buffer, "e+", 2);
buffer += 2;
}
print_2_digits(std::uint32_t(exponent), buffer);
buffer += 2;
return {buffer, std::errc()};
}
template <>
to_chars_result dragon_box_print_chars<double, dragonbox_float_traits<double>>(const std::uint64_t significand, int exponent, char* first, char* last, chars_format fmt) noexcept
{
auto buffer = first;
const std::ptrdiff_t total_length = total_buffer_length(17, exponent, false);
if (total_length > (last - first))
{
return {last, std::errc::value_too_large};
}
// Print significand by decomposing it into a 9-digit block and a 8-digit block.
std::uint32_t first_block;
std::uint32_t second_block {};
bool no_second_block;
if (significand >= 100000000)
{
first_block = std::uint32_t(significand / 100000000);
second_block = std::uint32_t(significand) - first_block * 100000000;
exponent += 8;
no_second_block = (second_block == 0);
}
else
{
first_block = std::uint32_t(significand);
no_second_block = true;
}
if (no_second_block)
{
print_9_digits(first_block, exponent, buffer);
}
else
{
// We proceed similarly to print_9_digits(), but since we do not need to remove
// trailing zeros, the procedure is a bit simpler.
if (first_block >= 100000000)
{
// The input is of 17 digits, thus there should be no trailing zero at all.
// The first block is of 9 digits.
// 1441151882 = ceil(2^57 / 1'0000'0000) + 1
auto prod = first_block * std::uint64_t(1441151882);
prod >>= 25;
std::memcpy(buffer, radix_100_head_table + std::uint32_t(prod >> 32) * 2, 2);
prod = std::uint32_t(prod) * std::uint64_t(100);
print_2_digits(std::uint32_t(prod >> 32), buffer + 2);
prod = std::uint32_t(prod) * std::uint64_t(100);
print_2_digits(std::uint32_t(prod >> 32), buffer + 4);
prod = std::uint32_t(prod) * std::uint64_t(100);
print_2_digits(std::uint32_t(prod >> 32), buffer + 6);
prod = std::uint32_t(prod) * std::uint64_t(100);
print_2_digits(std::uint32_t(prod >> 32), buffer + 8);
// The second block is of 8 digits.
// 281474978 = ceil(2^48 / 100'0000) + 1
prod = second_block * std::uint64_t(281474978);
prod >>= 16;
prod += 1;
print_2_digits(std::uint32_t(prod >> 32), buffer + 10);
prod = std::uint32_t(prod) * std::uint64_t(100);
print_2_digits(std::uint32_t(prod >> 32), buffer + 12);
prod = std::uint32_t(prod) * std::uint64_t(100);
print_2_digits(std::uint32_t(prod >> 32), buffer + 14);
prod = std::uint32_t(prod) * std::uint64_t(100);
print_2_digits(std::uint32_t(prod >> 32), buffer + 16);
exponent += 8;
buffer += 18;
}
else
{
if (first_block >= 1000000)
{
// 7 or 8 digits.
// 281474978 = ceil(2^48 / 100'0000) + 1
auto prod = first_block * std::uint64_t(281474978);
prod >>= 16;
const auto head_digits = std::uint32_t(prod >> 32);
std::memcpy(buffer, radix_100_head_table + head_digits * 2, 2);
buffer[2] = radix_table[head_digits * 2 + 1];
exponent += static_cast<int>(6 + unsigned(head_digits >= 10));
buffer += unsigned(head_digits >= 10);
// Print remaining 6 digits.
prod = std::uint32_t(prod) * std::uint64_t(100);
print_2_digits(std::uint32_t(prod >> 32), buffer + 2);
prod = std::uint32_t(prod) * std::uint64_t(100);
print_2_digits(std::uint32_t(prod >> 32), buffer + 4);
prod = std::uint32_t(prod) * std::uint64_t(100);
print_2_digits(std::uint32_t(prod >> 32), buffer + 6);
buffer += 8;
}
else if (first_block >= 10000)
{
// 5 or 6 digits.
// 429497 = ceil(2^32 / 1'0000)
auto prod = first_block * std::uint64_t(429497);
const auto head_digits = std::uint32_t(prod >> 32);
std::memcpy(buffer, radix_100_head_table + head_digits * 2, 2);
buffer[2] = radix_table[head_digits * 2 + 1];
exponent += static_cast<int>(4 + unsigned(head_digits >= 10));
buffer += unsigned(head_digits >= 10);
// Print remaining 4 digits.
prod = std::uint32_t(prod) * std::uint64_t(100);
print_2_digits(std::uint32_t(prod >> 32), buffer + 2);
prod = std::uint32_t(prod) * std::uint64_t(100);
print_2_digits(std::uint32_t(prod >> 32), buffer + 4);
buffer += 6;
}
else if (first_block >= 100)
{
// 3 or 4 digits.
// 42949673 = ceil(2^32 / 100)
auto prod = first_block * std::uint64_t(42949673);
const auto head_digits = std::uint32_t(prod >> 32);
std::memcpy(buffer, radix_100_head_table + head_digits * 2, 2);
buffer[2] = radix_table[head_digits * 2 + 1];
exponent += static_cast<int>(2 + unsigned(head_digits >= 10));
buffer += unsigned(head_digits >= 10);
// Print remaining 2 digits.
prod = std::uint32_t(prod) * std::uint64_t(100);
print_2_digits(std::uint32_t(prod >> 32), buffer + 2);
buffer += 4;
}
else
{
// 1 or 2 digits.
std::memcpy(buffer, radix_100_head_table + first_block * 2, 2);
buffer[2] = radix_table[first_block * 2 + 1];
exponent += (first_block >= 10);
buffer += (2 + unsigned(first_block >= 10));
}
// Next, print the second block.
// The second block is of 8 digits, but we may have trailing zeros.
// 281474978 = ceil(2^48 / 100'0000) + 1
auto prod = second_block * std::uint64_t(281474978);
prod >>= 16;
prod += 1;
print_2_digits(std::uint32_t(prod >> 32), buffer);
// Remaining 6 digits are all zero?
if (std::uint32_t(prod) <= std::uint32_t((std::uint64_t(1) << 32) / 1000000))
{
buffer += (1 + unsigned(buffer[1] > '0'));
}
else
{
// Obtain the next two digits.
prod = std::uint32_t(prod) * std::uint64_t(100);
print_2_digits(std::uint32_t(prod >> 32), buffer + 2);
// Remaining 4 digits are all zero?
if (std::uint32_t(prod) <= std::uint32_t((std::uint64_t(1) << 32) / 10000))
{
buffer += (3 + unsigned(buffer[3] > '0'));
}
else
{
// Obtain the next two digits.
prod = std::uint32_t(prod) * std::uint64_t(100);
print_2_digits(std::uint32_t(prod >> 32), buffer + 4);
// Remaining 2 digits are all zero?
if (std::uint32_t(prod) <= std::uint32_t((std::uint64_t(1) << 32) / 100))
{
buffer += (5 + unsigned(buffer[5] > '0'));
}
else
{
// Obtain the last two digits.
prod = std::uint32_t(prod) * std::uint64_t(100);
print_2_digits(std::uint32_t(prod >> 32), buffer + 6);
buffer += (7 + unsigned(buffer[7] > '0'));
}
}
}
}
}
if (exponent < 0)
{
std::memcpy(buffer, "e-", 2);
buffer += 2;
exponent = -exponent;
}
else if (exponent == 0)
{
if (fmt == chars_format::scientific)
{
std::memcpy(buffer, "e+00", 4);
buffer += 4;
}
return {buffer, std::errc()};
}
else
{
std::memcpy(buffer, "e+", 2);
buffer += 2;
}
if (exponent >= 100)
{
// d1 = exponent / 10; d2 = exponent % 10;
// 6554 = ceil(2^16 / 10)
auto prod = std::uint32_t(exponent) * std::uint32_t(6554);
auto d1 = prod >> 16;
prod = std::uint16_t(prod) * std::uint32_t(5); // * 10
auto d2 = prod >> 15; // >> 16
print_2_digits(d1, buffer);
print_1_digit(d2, buffer + 2);
buffer += 3;
}
else
{
print_2_digits(static_cast<std::uint32_t>(exponent), buffer);
buffer += 2;
}
return {buffer, std::errc()};
}
#ifdef BOOST_MSVC
# pragma warning(pop)
#endif
}}}} // Namespaces
boost::charconv::to_chars_result boost::charconv::to_chars(char* first, char* last, float value,
boost::charconv::chars_format fmt) noexcept
{
return boost::charconv::detail::to_chars_float_impl(first, last, value, fmt, -1);
}
boost::charconv::to_chars_result boost::charconv::to_chars(char* first, char* last, float value,
boost::charconv::chars_format fmt, int precision) noexcept
{
if (precision < 0)
{
precision = 6;
}
return boost::charconv::detail::to_chars_float_impl(first, last, value, fmt, precision);
}
boost::charconv::to_chars_result boost::charconv::to_chars(char* first, char* last, double value,
boost::charconv::chars_format fmt) noexcept
{
return boost::charconv::detail::to_chars_float_impl(first, last, value, fmt, -1);
}
boost::charconv::to_chars_result boost::charconv::to_chars(char* first, char* last, double value,
boost::charconv::chars_format fmt, int precision) noexcept
{
if (precision < 0)
{
precision = 6;
}
return boost::charconv::detail::to_chars_float_impl(first, last, value, fmt, precision);
}
#if BOOST_CHARCONV_LDBL_BITS == 64 || defined(BOOST_MSVC)
boost::charconv::to_chars_result boost::charconv::to_chars(char* first, char* last, long double value,
boost::charconv::chars_format fmt) noexcept
{
return boost::charconv::detail::to_chars_float_impl(first, last, static_cast<double>(value), fmt, -1);
}
boost::charconv::to_chars_result boost::charconv::to_chars(char* first, char* last, long double value,
boost::charconv::chars_format fmt, int precision) noexcept
{
if (precision < 0)
{
precision = 6;
}
return boost::charconv::detail::to_chars_float_impl(first, last, static_cast<double>(value), fmt, precision);
}
#elif !defined(BOOST_CHARCONV_UNSUPPORTED_LONG_DOUBLE) && !defined(BOOST_CHARCONV_LDBL_IS_FLOAT128)
boost::charconv::to_chars_result boost::charconv::to_chars(char* first, char* last, long double value,
boost::charconv::chars_format fmt) noexcept
{
return boost::charconv::detail::to_chars_float_impl(first, last, value, fmt, -1);
}
boost::charconv::to_chars_result boost::charconv::to_chars(char* first, char* last, long double value,
boost::charconv::chars_format fmt, int precision) noexcept
{
if (precision < 0)
{
precision = 6;
}
return boost::charconv::detail::to_chars_float_impl(first, last, value, fmt, precision);
}
#endif
#ifdef BOOST_CHARCONV_HAS_QUADMATH
boost::charconv::to_chars_result boost::charconv::to_chars(char* first, char* last, __float128 value, boost::charconv::chars_format fmt) noexcept
{
return boost::charconv::detail::to_chars_float_impl(first, last, value, fmt, -1);
}
boost::charconv::to_chars_result boost::charconv::to_chars(char* first, char* last, __float128 value, boost::charconv::chars_format fmt, int precision) noexcept
{
if (precision < 0)
{
precision = 6;
}
return boost::charconv::detail::to_chars_float_impl(first, last, value, fmt, precision);
}
#endif
#ifdef BOOST_CHARCONV_HAS_FLOAT16
boost::charconv::to_chars_result boost::charconv::to_chars(char* first, char* last, std::float16_t value,
boost::charconv::chars_format fmt) noexcept
{
return boost::charconv::detail::to_chars_16_bit_float_impl(first, last, value, fmt, -1);
}
boost::charconv::to_chars_result boost::charconv::to_chars(char* first, char* last, std::float16_t value,
boost::charconv::chars_format fmt, int precision) noexcept
{
if (precision < 0)
{
precision = 6;
return boost::charconv::detail::to_chars_16_bit_float_impl(first, last, value, fmt, precision);
}
// If the precision is specified it is better to use our exisiting methods for float
return boost::charconv::detail::to_chars_float_impl(first, last, static_cast<float>(value), fmt, precision);
}
#endif
#ifdef BOOST_CHARCONV_HAS_FLOAT32
boost::charconv::to_chars_result boost::charconv::to_chars(char* first, char* last, std::float32_t value,
boost::charconv::chars_format fmt) noexcept
{
return boost::charconv::detail::to_chars_float_impl(first, last, static_cast<float>(value), fmt, -1);
}
boost::charconv::to_chars_result boost::charconv::to_chars(char* first, char* last, std::float32_t value,
boost::charconv::chars_format fmt, int precision) noexcept
{
static_assert(std::numeric_limits<std::float32_t>::digits == FLT_MANT_DIG &&
std::numeric_limits<std::float32_t>::min_exponent == FLT_MIN_EXP,
"float and std::float32_t are not the same layout like they should be");
if (precision < 0)
{
precision = 6;
}
return boost::charconv::detail::to_chars_float_impl(first, last, static_cast<float>(value), fmt, precision);
}
#endif
#ifdef BOOST_CHARCONV_HAS_FLOAT64
boost::charconv::to_chars_result boost::charconv::to_chars(char* first, char* last, std::float64_t value,
boost::charconv::chars_format fmt) noexcept
{
return boost::charconv::detail::to_chars_float_impl(first, last, static_cast<double>(value), fmt, -1);
}
boost::charconv::to_chars_result boost::charconv::to_chars(char* first, char* last, std::float64_t value,
boost::charconv::chars_format fmt, int precision) noexcept
{
static_assert(std::numeric_limits<std::float64_t>::digits == DBL_MANT_DIG &&
std::numeric_limits<std::float64_t>::min_exponent == DBL_MIN_EXP,
"double and std::float64_t are not the same layout like they should be");
if (precision < 0)
{
precision = 6;
}
return boost::charconv::detail::to_chars_float_impl(first, last, static_cast<double>(value), fmt, precision);
}
#endif
#if defined(BOOST_CHARCONV_HAS_STDFLOAT128) && defined(BOOST_CHARCONV_HAS_QUADMATH)
boost::charconv::to_chars_result boost::charconv::to_chars(char* first, char* last, std::float128_t value,
boost::charconv::chars_format fmt) noexcept
{
return boost::charconv::detail::to_chars_float_impl(first, last, static_cast<__float128>(value), fmt, -1);
}
boost::charconv::to_chars_result boost::charconv::to_chars(char* first, char* last, std::float128_t value,
boost::charconv::chars_format fmt, int precision) noexcept
{
if (precision < 0)
{
precision = 6;
}
return boost::charconv::detail::to_chars_float_impl(first, last, static_cast<__float128>(value), fmt, precision);
}
#endif
#ifdef BOOST_CHARCONV_HAS_BRAINFLOAT16
boost::charconv::to_chars_result boost::charconv::to_chars(char* first, char* last, std::bfloat16_t value,
boost::charconv::chars_format fmt) noexcept
{
return boost::charconv::detail::to_chars_16_bit_float_impl(first, last, value, fmt, -1);
}
boost::charconv::to_chars_result boost::charconv::to_chars(char* first, char* last, std::bfloat16_t value,
boost::charconv::chars_format fmt, int precision) noexcept
{
if (precision < 0)
{
precision = 6;
return boost::charconv::detail::to_chars_16_bit_float_impl(first, last, value, fmt, precision);
}
// If the precision is specified it is better to use our existing methods for float
return boost::charconv::detail::to_chars_float_impl(first, last, static_cast<float>(value), fmt, precision);
}
#endif