-
Notifications
You must be signed in to change notification settings - Fork 1.5k
/
xcharconv_ryu.h
2425 lines (2147 loc) · 99.7 KB
/
xcharconv_ryu.h
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
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// xcharconv_ryu.h internal header
// Copyright (c) Microsoft Corporation.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
// Copyright 2018 Ulf Adams
// Copyright (c) Microsoft Corporation. All rights reserved.
// Boost Software License - Version 1.0 - August 17th, 2003
// Permission is hereby granted, free of charge, to any person or organization
// obtaining a copy of the software and accompanying documentation covered by
// this license (the "Software") to use, reproduce, display, distribute,
// execute, and transmit the Software, and to prepare derivative works of the
// Software, and to permit third-parties to whom the Software is furnished to
// do so, all subject to the following:
// The copyright notices in the Software and this entire statement, including
// the above license grant, this restriction and the following disclaimer,
// must be included in all copies of the Software, in whole or in part, and
// all derivative works of the Software, unless such copies or derivative
// works are solely in the form of machine-executable object code generated by
// a source language processor.
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
// SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
// FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.
#ifndef _XCHARCONV_RYU_H
#define _XCHARCONV_RYU_H
#include <yvals_core.h>
#if _STL_COMPILER_PREPROCESSOR
#if !_HAS_CXX17
#error The contents of <charconv> are only available with C++17. (Also, you should not include this internal header.)
#endif // !_HAS_CXX17
#include <cstring>
#include <type_traits>
#include <utility>
#include <xcharconv.h>
#include <xcharconv_ryu_tables.h>
#include <xutility>
#if defined(_M_X64) || defined(_M_ARM64) || defined(_M_ARM64EC) || defined(_M_HYBRID_X86_ARM64)
#define _HAS_CHARCONV_INTRINSICS 1
#else // ^^^ intrinsics available / intrinsics unavailable vvv
#define _HAS_CHARCONV_INTRINSICS 0
#endif // ^^^ intrinsics unavailable ^^^
#if _HAS_CHARCONV_INTRINSICS
#if defined(_M_ARM64) || defined(_M_ARM64EC) || defined(_M_HYBRID_X86_ARM64)
#include <intrin.h> // TRANSITION, VSO-1918426
#else // ^^^ defined(_M_ARM64) || defined(_M_ARM64EC) || defined(_M_HYBRID_X86_ARM64) / defined(_M_X64) vvv
#include _STL_INTRIN_HEADER // for _umul128(), __umulh(), and __shiftright128()
#endif // ^^^ defined(_M_X64) ^^^
#endif // ^^^ intrinsics available ^^^
#pragma pack(push, _CRT_PACKING)
#pragma warning(push, _STL_WARNING_LEVEL)
#pragma warning(disable : _STL_DISABLED_WARNINGS)
_STL_DISABLE_CLANG_WARNINGS
#pragma push_macro("new")
#undef new
_STD_BEGIN
// https://github.com/ulfjack/ryu/tree/59661c3/ryu
// (Keep the cgmanifest.json commitHash in sync.)
// clang-format off
// vvvvvvvvvv DERIVED FROM common.h vvvvvvvvvv
_NODISCARD inline uint32_t __decimalLength9(const uint32_t __v) {
// Function precondition: __v is not a 10-digit number.
// (f2s: 9 digits are sufficient for round-tripping.)
// (d2fixed: We print 9-digit blocks.)
_STL_INTERNAL_CHECK(__v < 1000000000);
if (__v >= 100000000) { return 9; }
if (__v >= 10000000) { return 8; }
if (__v >= 1000000) { return 7; }
if (__v >= 100000) { return 6; }
if (__v >= 10000) { return 5; }
if (__v >= 1000) { return 4; }
if (__v >= 100) { return 3; }
if (__v >= 10) { return 2; }
return 1;
}
// Returns __e == 0 ? 1 : ceil(log_2(5^__e)).
_NODISCARD inline int32_t __pow5bits(const int32_t __e) {
// This approximation works up to the point that the multiplication overflows at __e = 3529.
// If the multiplication were done in 64 bits, it would fail at 5^4004 which is just greater
// than 2^9297.
_STL_INTERNAL_CHECK(__e >= 0);
_STL_INTERNAL_CHECK(__e <= 3528);
return static_cast<int32_t>(((static_cast<uint32_t>(__e) * 1217359) >> 19) + 1);
}
// Returns floor(log_10(2^__e)).
_NODISCARD inline uint32_t __log10Pow2(const int32_t __e) {
// The first value this approximation fails for is 2^1651 which is just greater than 10^297.
_STL_INTERNAL_CHECK(__e >= 0);
_STL_INTERNAL_CHECK(__e <= 1650);
return (static_cast<uint32_t>(__e) * 78913) >> 18;
}
// Returns floor(log_10(5^__e)).
_NODISCARD inline uint32_t __log10Pow5(const int32_t __e) {
// The first value this approximation fails for is 5^2621 which is just greater than 10^1832.
_STL_INTERNAL_CHECK(__e >= 0);
_STL_INTERNAL_CHECK(__e <= 2620);
return (static_cast<uint32_t>(__e) * 732923) >> 20;
}
_NODISCARD inline uint32_t __float_to_bits(const float __f) {
uint32_t __bits = 0;
_CSTD memcpy(&__bits, &__f, sizeof(float));
return __bits;
}
_NODISCARD inline uint64_t __double_to_bits(const double __d) {
uint64_t __bits = 0;
_CSTD memcpy(&__bits, &__d, sizeof(double));
return __bits;
}
// ^^^^^^^^^^ DERIVED FROM common.h ^^^^^^^^^^
// vvvvvvvvvv DERIVED FROM d2s.h vvvvvvvvvv
inline constexpr int __DOUBLE_MANTISSA_BITS = 52;
inline constexpr int __DOUBLE_BIAS = 1023;
inline constexpr int __DOUBLE_POW5_INV_BITCOUNT = 122;
inline constexpr int __DOUBLE_POW5_BITCOUNT = 121;
// ^^^^^^^^^^ DERIVED FROM d2s.h ^^^^^^^^^^
// vvvvvvvvvv DERIVED FROM d2s_intrinsics.h vvvvvvvvvv
#if _HAS_CHARCONV_INTRINSICS
_NODISCARD inline uint64_t __ryu_umul128(const uint64_t __a, const uint64_t __b, uint64_t* const __productHi) {
#if defined(_M_ARM64) || defined(_M_ARM64EC) || defined(_M_HYBRID_X86_ARM64)
*__productHi = __umulh(__a, __b);
return __a * __b;
#else // ^^^ not native X64 / native X64 vvv
return _umul128(__a, __b, __productHi);
#endif // defined(_M_ARM64) || defined(_M_ARM64EC) || defined(_M_HYBRID_X86_ARM64)
}
#else // ^^^ intrinsics available / intrinsics unavailable vvv
_NODISCARD __forceinline uint64_t __ryu_umul128(const uint64_t __a, const uint64_t __b, uint64_t* const __productHi) {
// TRANSITION, VSO-634761
// The casts here help MSVC to avoid calls to the __allmul library function.
const uint32_t __aLo = static_cast<uint32_t>(__a);
const uint32_t __aHi = static_cast<uint32_t>(__a >> 32);
const uint32_t __bLo = static_cast<uint32_t>(__b);
const uint32_t __bHi = static_cast<uint32_t>(__b >> 32);
const uint64_t __b00 = static_cast<uint64_t>(__aLo) * __bLo;
const uint64_t __b01 = static_cast<uint64_t>(__aLo) * __bHi;
const uint64_t __b10 = static_cast<uint64_t>(__aHi) * __bLo;
const uint64_t __b11 = static_cast<uint64_t>(__aHi) * __bHi;
const uint32_t __b00Lo = static_cast<uint32_t>(__b00);
const uint32_t __b00Hi = static_cast<uint32_t>(__b00 >> 32);
const uint64_t __mid1 = __b10 + __b00Hi;
const uint32_t __mid1Lo = static_cast<uint32_t>(__mid1);
const uint32_t __mid1Hi = static_cast<uint32_t>(__mid1 >> 32);
const uint64_t __mid2 = __b01 + __mid1Lo;
const uint32_t __mid2Lo = static_cast<uint32_t>(__mid2);
const uint32_t __mid2Hi = static_cast<uint32_t>(__mid2 >> 32);
const uint64_t __pHi = __b11 + __mid1Hi + __mid2Hi;
const uint64_t __pLo = (static_cast<uint64_t>(__mid2Lo) << 32) | __b00Lo;
*__productHi = __pHi;
return __pLo;
}
#endif // ^^^ intrinsics unavailable ^^^
_NODISCARD inline uint64_t __ryu_shiftright128(const uint64_t __lo, const uint64_t __hi, const uint32_t __dist) {
// In the current implementation, the shift value is always < 64.
// If larger shift values are ever required, this function will need to be adjusted.
_STL_INTERNAL_CHECK(__dist < 64);
#if defined(_M_X64) && !defined(_M_ARM64EC)
return __shiftright128(__lo, __hi, static_cast<unsigned char>(__dist));
#else // ^^^ __shiftright128 intrinsic available / __shiftright128 intrinsic unavailable vvv
if (__dist == 0) {
return __lo;
}
return (__hi << (64 - __dist)) | (__lo >> __dist);
#endif // ^^^ __shiftright128 intrinsic unavailable ^^^
}
#ifndef _WIN64
#if !defined(_M_HYBRID_X86_ARM64)
// Returns the high 64 bits of the 128-bit product of __a and __b.
_NODISCARD inline uint64_t __umulh(const uint64_t __a, const uint64_t __b) {
// Reuse the __ryu_umul128 implementation.
// Optimizers will likely eliminate the instructions used to compute the
// low part of the product.
uint64_t __hi;
(void) __ryu_umul128(__a, __b, &__hi);
return __hi;
}
#endif // ^^^ !defined(_M_HYBRID_X86_ARM64) ^^^
// On 32-bit platforms, compilers typically generate calls to library
// functions for 64-bit divisions, even if the divisor is a constant.
//
// TRANSITION, LLVM-37932
//
// The functions here perform division-by-constant using multiplications
// in the same way as 64-bit compilers would do.
//
// NB:
// The multipliers and shift values are the ones generated by clang x64
// for expressions like x/5, x/10, etc.
_NODISCARD inline uint64_t __div5(const uint64_t __x) {
return __umulh(__x, 0xCCCCCCCCCCCCCCCDu) >> 2;
}
_NODISCARD inline uint64_t __div10(const uint64_t __x) {
return __umulh(__x, 0xCCCCCCCCCCCCCCCDu) >> 3;
}
_NODISCARD inline uint64_t __div100(const uint64_t __x) {
return __umulh(__x >> 2, 0x28F5C28F5C28F5C3u) >> 2;
}
_NODISCARD inline uint64_t __div1e8(const uint64_t __x) {
return __umulh(__x, 0xABCC77118461CEFDu) >> 26;
}
_NODISCARD inline uint64_t __div1e9(const uint64_t __x) {
return __umulh(__x >> 9, 0x44B82FA09B5A53u) >> 11;
}
_NODISCARD inline uint32_t __mod1e9(const uint64_t __x) {
// Avoid 64-bit math as much as possible.
// Returning static_cast<uint32_t>(__x - 1000000000 * __div1e9(__x)) would
// perform 32x64-bit multiplication and 64-bit subtraction.
// __x and 1000000000 * __div1e9(__x) are guaranteed to differ by
// less than 10^9, so their highest 32 bits must be identical,
// so we can truncate both sides to uint32_t before subtracting.
// We can also simplify static_cast<uint32_t>(1000000000 * __div1e9(__x)).
// We can truncate before multiplying instead of after, as multiplying
// the highest 32 bits of __div1e9(__x) can't affect the lowest 32 bits.
return static_cast<uint32_t>(__x) - 1000000000 * static_cast<uint32_t>(__div1e9(__x));
}
#else // ^^^ 32-bit / 64-bit vvv
_NODISCARD inline uint64_t __div5(const uint64_t __x) {
return __x / 5;
}
_NODISCARD inline uint64_t __div10(const uint64_t __x) {
return __x / 10;
}
_NODISCARD inline uint64_t __div100(const uint64_t __x) {
return __x / 100;
}
_NODISCARD inline uint64_t __div1e8(const uint64_t __x) {
return __x / 100000000;
}
_NODISCARD inline uint64_t __div1e9(const uint64_t __x) {
return __x / 1000000000;
}
_NODISCARD inline uint32_t __mod1e9(const uint64_t __x) {
return static_cast<uint32_t>(__x - 1000000000 * __div1e9(__x));
}
#endif // ^^^ 64-bit ^^^
_NODISCARD inline uint32_t __pow5Factor(uint64_t __value) {
uint32_t __count = 0;
for (;;) {
_STL_INTERNAL_CHECK(__value != 0);
const uint64_t __q = __div5(__value);
const uint32_t __r = static_cast<uint32_t>(__value) - 5 * static_cast<uint32_t>(__q);
if (__r != 0) {
break;
}
__value = __q;
++__count;
}
return __count;
}
// Returns true if __value is divisible by 5^__p.
_NODISCARD inline bool __multipleOfPowerOf5(const uint64_t __value, const uint32_t __p) {
// I tried a case distinction on __p, but there was no performance difference.
return __pow5Factor(__value) >= __p;
}
// Returns true if __value is divisible by 2^__p.
_NODISCARD inline bool __multipleOfPowerOf2(const uint64_t __value, const uint32_t __p) {
_STL_INTERNAL_CHECK(__value != 0);
_STL_INTERNAL_CHECK(__p < 64);
// return __builtin_ctzll(__value) >= __p;
return (__value & ((1ull << __p) - 1)) == 0;
}
// ^^^^^^^^^^ DERIVED FROM d2s_intrinsics.h ^^^^^^^^^^
// vvvvvvvvvv DERIVED FROM d2fixed.c vvvvvvvvvv
inline constexpr int __POW10_ADDITIONAL_BITS = 120;
#if _HAS_CHARCONV_INTRINSICS
// Returns the low 64 bits of the high 128 bits of the 256-bit product of a and b.
_NODISCARD inline uint64_t __umul256_hi128_lo64(
const uint64_t __aHi, const uint64_t __aLo, const uint64_t __bHi, const uint64_t __bLo) {
uint64_t __b00Hi;
const uint64_t __b00Lo = __ryu_umul128(__aLo, __bLo, &__b00Hi);
uint64_t __b01Hi;
const uint64_t __b01Lo = __ryu_umul128(__aLo, __bHi, &__b01Hi);
uint64_t __b10Hi;
const uint64_t __b10Lo = __ryu_umul128(__aHi, __bLo, &__b10Hi);
uint64_t __b11Hi;
const uint64_t __b11Lo = __ryu_umul128(__aHi, __bHi, &__b11Hi);
(void) __b00Lo; // unused
(void) __b11Hi; // unused
const uint64_t __temp1Lo = __b10Lo + __b00Hi;
const uint64_t __temp1Hi = __b10Hi + (__temp1Lo < __b10Lo);
const uint64_t __temp2Lo = __b01Lo + __temp1Lo;
const uint64_t __temp2Hi = __b01Hi + (__temp2Lo < __b01Lo);
return __b11Lo + __temp1Hi + __temp2Hi;
}
_NODISCARD inline uint32_t __uint128_mod1e9(const uint64_t __vHi, const uint64_t __vLo) {
// After multiplying, we're going to shift right by 29, then truncate to uint32_t.
// This means that we need only 29 + 32 = 61 bits, so we can truncate to uint64_t before shifting.
const uint64_t __multiplied = __umul256_hi128_lo64(__vHi, __vLo, 0x89705F4136B4A597u, 0x31680A88F8953031u);
// For uint32_t truncation, see the __mod1e9() comment in d2s_intrinsics.h.
const uint32_t __shifted = static_cast<uint32_t>(__multiplied >> 29);
return static_cast<uint32_t>(__vLo) - 1000000000 * __shifted;
}
#endif // ^^^ intrinsics available ^^^
_NODISCARD inline uint32_t __mulShift_mod1e9(const uint64_t __m, const uint64_t* const __mul, const int32_t __j) {
uint64_t __high0; // 64
const uint64_t __low0 = __ryu_umul128(__m, __mul[0], &__high0); // 0
uint64_t __high1; // 128
const uint64_t __low1 = __ryu_umul128(__m, __mul[1], &__high1); // 64
uint64_t __high2; // 192
const uint64_t __low2 = __ryu_umul128(__m, __mul[2], &__high2); // 128
const uint64_t __s0low = __low0; // 0
(void) __s0low; // unused
const uint64_t __s0high = __low1 + __high0; // 64
const uint32_t __c1 = __s0high < __low1;
const uint64_t __s1low = __low2 + __high1 + __c1; // 128
const uint32_t __c2 = __s1low < __low2; // __high1 + __c1 can't overflow, so compare against __low2
const uint64_t __s1high = __high2 + __c2; // 192
_STL_INTERNAL_CHECK(__j >= 128);
_STL_INTERNAL_CHECK(__j <= 180);
#if _HAS_CHARCONV_INTRINSICS
const uint32_t __dist = static_cast<uint32_t>(__j - 128); // __dist: [0, 52]
const uint64_t __shiftedhigh = __s1high >> __dist;
const uint64_t __shiftedlow = __ryu_shiftright128(__s1low, __s1high, __dist);
return __uint128_mod1e9(__shiftedhigh, __shiftedlow);
#else // ^^^ intrinsics available / intrinsics unavailable vvv
if (__j < 160) { // __j: [128, 160)
const uint64_t __r0 = __mod1e9(__s1high);
const uint64_t __r1 = __mod1e9((__r0 << 32) | (__s1low >> 32));
const uint64_t __r2 = ((__r1 << 32) | (__s1low & 0xffffffff));
return __mod1e9(__r2 >> (__j - 128));
} else { // __j: [160, 192)
const uint64_t __r0 = __mod1e9(__s1high);
const uint64_t __r1 = ((__r0 << 32) | (__s1low >> 32));
return __mod1e9(__r1 >> (__j - 160));
}
#endif // ^^^ intrinsics unavailable ^^^
}
#define _WIDEN(_TYPE, _CHAR) static_cast<_TYPE>(is_same_v<_TYPE, char> ? _CHAR : L##_CHAR)
template <class _CharT>
void __append_n_digits(const uint32_t __olength, uint32_t __digits, _CharT* const __result) {
uint32_t __i = 0;
while (__digits >= 10000) {
#ifdef __clang__ // TRANSITION, LLVM-38217
const uint32_t __c = __digits - 10000 * (__digits / 10000);
#else
const uint32_t __c = __digits % 10000;
#endif
__digits /= 10000;
const uint32_t __c0 = (__c % 100) << 1;
const uint32_t __c1 = (__c / 100) << 1;
_CSTD memcpy(__result + __olength - __i - 2, __DIGIT_TABLE<_CharT> + __c0, 2 * sizeof(_CharT));
_CSTD memcpy(__result + __olength - __i - 4, __DIGIT_TABLE<_CharT> + __c1, 2 * sizeof(_CharT));
__i += 4;
}
if (__digits >= 100) {
const uint32_t __c = (__digits % 100) << 1;
__digits /= 100;
_CSTD memcpy(__result + __olength - __i - 2, __DIGIT_TABLE<_CharT> + __c, 2 * sizeof(_CharT));
__i += 2;
}
if (__digits >= 10) {
const uint32_t __c = __digits << 1;
_CSTD memcpy(__result + __olength - __i - 2, __DIGIT_TABLE<_CharT> + __c, 2 * sizeof(_CharT));
} else {
__result[0] = static_cast<_CharT>(_WIDEN(_CharT, '0') + __digits);
}
}
inline void __append_d_digits(const uint32_t __olength, uint32_t __digits, char* const __result) {
uint32_t __i = 0;
while (__digits >= 10000) {
#ifdef __clang__ // TRANSITION, LLVM-38217
const uint32_t __c = __digits - 10000 * (__digits / 10000);
#else
const uint32_t __c = __digits % 10000;
#endif
__digits /= 10000;
const uint32_t __c0 = (__c % 100) << 1;
const uint32_t __c1 = (__c / 100) << 1;
_CSTD memcpy(__result + __olength + 1 - __i - 2, __DIGIT_TABLE<char> + __c0, 2);
_CSTD memcpy(__result + __olength + 1 - __i - 4, __DIGIT_TABLE<char> + __c1, 2);
__i += 4;
}
if (__digits >= 100) {
const uint32_t __c = (__digits % 100) << 1;
__digits /= 100;
_CSTD memcpy(__result + __olength + 1 - __i - 2, __DIGIT_TABLE<char> + __c, 2);
__i += 2;
}
if (__digits >= 10) {
const uint32_t __c = __digits << 1;
__result[2] = __DIGIT_TABLE<char>[__c + 1];
__result[1] = '.';
__result[0] = __DIGIT_TABLE<char>[__c];
} else {
__result[1] = '.';
__result[0] = static_cast<char>('0' + __digits);
}
}
template <class _CharT>
void __append_c_digits(const uint32_t __count, uint32_t __digits, _CharT* const __result) {
uint32_t __i = 0;
for (; __i < __count - 1; __i += 2) {
const uint32_t __c = (__digits % 100) << 1;
__digits /= 100;
_CSTD memcpy(__result + __count - __i - 2, __DIGIT_TABLE<_CharT> + __c, 2 * sizeof(_CharT));
}
if (__i < __count) {
const _CharT __c = static_cast<_CharT>(_WIDEN(_CharT, '0') + (__digits % 10));
__result[__count - __i - 1] = __c;
}
}
template <class _CharT>
void __append_nine_digits(uint32_t __digits, _CharT* const __result) {
if (__digits == 0) {
_STD fill_n(__result, 9, _WIDEN(_CharT, '0'));
return;
}
for (uint32_t __i = 0; __i < 5; __i += 4) {
#ifdef __clang__ // TRANSITION, LLVM-38217
const uint32_t __c = __digits - 10000 * (__digits / 10000);
#else
const uint32_t __c = __digits % 10000;
#endif
__digits /= 10000;
const uint32_t __c0 = (__c % 100) << 1;
const uint32_t __c1 = (__c / 100) << 1;
_CSTD memcpy(__result + 7 - __i, __DIGIT_TABLE<_CharT> + __c0, 2 * sizeof(_CharT));
_CSTD memcpy(__result + 5 - __i, __DIGIT_TABLE<_CharT> + __c1, 2 * sizeof(_CharT));
}
__result[0] = static_cast<_CharT>(_WIDEN(_CharT, '0') + __digits);
}
_NODISCARD inline uint32_t __indexForExponent(const uint32_t __e) {
return (__e + 15) / 16;
}
_NODISCARD inline uint32_t __pow10BitsForIndex(const uint32_t __idx) {
return 16 * __idx + __POW10_ADDITIONAL_BITS;
}
_NODISCARD inline uint32_t __lengthForIndex(const uint32_t __idx) {
// +1 for ceil, +16 for mantissa, +8 to round up when dividing by 9
return (__log10Pow2(16 * static_cast<int32_t>(__idx)) + 1 + 16 + 8) / 9;
}
template <class _CharT>
_NODISCARD pair<_CharT*, errc> __d2fixed_buffered_n(_CharT* _First, _CharT* const _Last, const double __d,
const uint32_t __precision) {
_CharT* const _Original_first = _First;
const uint64_t __bits = __double_to_bits(__d);
// Case distinction; exit early for the easy cases.
if (__bits == 0) {
const int32_t _Total_zero_length = 1 // leading zero
+ static_cast<int32_t>(__precision != 0) // possible decimal point
+ static_cast<int32_t>(__precision); // zeroes after decimal point
if (_Last - _First < _Total_zero_length) {
return { _Last, errc::value_too_large };
}
*_First++ = _WIDEN(_CharT, '0');
if (__precision > 0) {
*_First++ = _WIDEN(_CharT, '.');
_STD fill_n(_First, __precision, _WIDEN(_CharT, '0'));
_First += __precision;
}
return { _First, errc{} };
}
// Decode __bits into mantissa and exponent.
const uint64_t __ieeeMantissa = __bits & ((1ull << __DOUBLE_MANTISSA_BITS) - 1);
const uint32_t __ieeeExponent = static_cast<uint32_t>(__bits >> __DOUBLE_MANTISSA_BITS);
int32_t __e2;
uint64_t __m2;
if (__ieeeExponent == 0) {
__e2 = 1 - __DOUBLE_BIAS - __DOUBLE_MANTISSA_BITS;
__m2 = __ieeeMantissa;
} else {
__e2 = static_cast<int32_t>(__ieeeExponent) - __DOUBLE_BIAS - __DOUBLE_MANTISSA_BITS;
__m2 = (1ull << __DOUBLE_MANTISSA_BITS) | __ieeeMantissa;
}
bool __nonzero = false;
if (__e2 >= -52) {
const uint32_t __idx = __e2 < 0 ? 0 : __indexForExponent(static_cast<uint32_t>(__e2));
const uint32_t __p10bits = __pow10BitsForIndex(__idx);
const int32_t __len = static_cast<int32_t>(__lengthForIndex(__idx));
for (int32_t __i = __len - 1; __i >= 0; --__i) {
const uint32_t __j = __p10bits - __e2;
// Temporary: __j is usually around 128, and by shifting a bit, we push it to 128 or above, which is
// a slightly faster code path in __mulShift_mod1e9. Instead, we can just increase the multipliers.
const uint32_t __digits = __mulShift_mod1e9(__m2 << 8, __POW10_SPLIT[__POW10_OFFSET[__idx] + __i],
static_cast<int32_t>(__j + 8));
if (__nonzero) {
if (_Last - _First < 9) {
return { _Last, errc::value_too_large };
}
__append_nine_digits(__digits, _First);
_First += 9;
} else if (__digits != 0) {
const uint32_t __olength = __decimalLength9(__digits);
if (_Last - _First < static_cast<ptrdiff_t>(__olength)) {
return { _Last, errc::value_too_large };
}
__append_n_digits(__olength, __digits, _First);
_First += __olength;
__nonzero = true;
}
}
}
if (!__nonzero) {
if (_First == _Last) {
return { _Last, errc::value_too_large };
}
*_First++ = _WIDEN(_CharT, '0');
}
if (__precision > 0) {
if (_First == _Last) {
return { _Last, errc::value_too_large };
}
*_First++ = _WIDEN(_CharT, '.');
}
if (__e2 < 0) {
const int32_t __idx = -__e2 / 16;
const uint32_t __blocks = __precision / 9 + 1;
// 0 = don't round up; 1 = round up unconditionally; 2 = round up if odd.
int __roundUp = 0;
uint32_t __i = 0;
if (__blocks <= __MIN_BLOCK_2[__idx]) {
__i = __blocks;
if (_Last - _First < static_cast<ptrdiff_t>(__precision)) {
return { _Last, errc::value_too_large };
}
_STD fill_n(_First, __precision, _WIDEN(_CharT, '0'));
_First += __precision;
} else if (__i < __MIN_BLOCK_2[__idx]) {
__i = __MIN_BLOCK_2[__idx];
if (_Last - _First < static_cast<ptrdiff_t>(9 * __i)) {
return { _Last, errc::value_too_large };
}
_STD fill_n(_First, 9 * __i, _WIDEN(_CharT, '0'));
_First += 9 * __i;
}
for (; __i < __blocks; ++__i) {
const int32_t __j = __ADDITIONAL_BITS_2 + (-__e2 - 16 * __idx);
const uint32_t __p = __POW10_OFFSET_2[__idx] + __i - __MIN_BLOCK_2[__idx];
if (__p >= __POW10_OFFSET_2[__idx + 1]) {
// If the remaining digits are all 0, then we might as well use memset.
// No rounding required in this case.
const uint32_t __fill = __precision - 9 * __i;
if (_Last - _First < static_cast<ptrdiff_t>(__fill)) {
return { _Last, errc::value_too_large };
}
_STD fill_n(_First, __fill, _WIDEN(_CharT, '0'));
_First += __fill;
break;
}
// Temporary: __j is usually around 128, and by shifting a bit, we push it to 128 or above, which is
// a slightly faster code path in __mulShift_mod1e9. Instead, we can just increase the multipliers.
uint32_t __digits = __mulShift_mod1e9(__m2 << 8, __POW10_SPLIT_2[__p], __j + 8);
if (__i < __blocks - 1) {
if (_Last - _First < 9) {
return { _Last, errc::value_too_large };
}
__append_nine_digits(__digits, _First);
_First += 9;
} else {
const uint32_t __maximum = __precision - 9 * __i;
uint32_t __lastDigit = 0;
for (uint32_t __k = 0; __k < 9 - __maximum; ++__k) {
__lastDigit = __digits % 10;
__digits /= 10;
}
if (__lastDigit != 5) {
__roundUp = __lastDigit > 5;
} else {
// Is m * 10^(additionalDigits + 1) / 2^(-__e2) integer?
const int32_t __requiredTwos = -__e2 - static_cast<int32_t>(__precision) - 1;
const bool __trailingZeros = __requiredTwos <= 0
|| (__requiredTwos < 60 && __multipleOfPowerOf2(__m2, static_cast<uint32_t>(__requiredTwos)));
__roundUp = __trailingZeros ? 2 : 1;
}
if (__maximum > 0) {
if (_Last - _First < static_cast<ptrdiff_t>(__maximum)) {
return { _Last, errc::value_too_large };
}
__append_c_digits(__maximum, __digits, _First);
_First += __maximum;
}
break;
}
}
if (__roundUp != 0) {
_CharT* _Round = _First;
_CharT* _Dot = _Last;
while (true) {
if (_Round == _Original_first) {
_Round[0] = _WIDEN(_CharT, '1');
if (_Dot != _Last) {
_Dot[0] = _WIDEN(_CharT, '0');
_Dot[1] = _WIDEN(_CharT, '.');
}
if (_First == _Last) {
return { _Last, errc::value_too_large };
}
*_First++ = _WIDEN(_CharT, '0');
break;
}
--_Round;
const _CharT __c = _Round[0];
if (__c == _WIDEN(_CharT, '.')) {
_Dot = _Round;
} else if (__c == _WIDEN(_CharT, '9')) {
_Round[0] = _WIDEN(_CharT, '0');
__roundUp = 1;
} else {
if (__roundUp == 1 || __c % 2 != 0) {
_Round[0] = static_cast<_CharT>(__c + 1);
}
break;
}
}
}
} else {
if (_Last - _First < static_cast<ptrdiff_t>(__precision)) {
return { _Last, errc::value_too_large };
}
_STD fill_n(_First, __precision, _WIDEN(_CharT, '0'));
_First += __precision;
}
return { _First, errc{} };
}
_NODISCARD inline to_chars_result __d2exp_buffered_n(char* _First, char* const _Last, const double __d,
uint32_t __precision) {
char* const _Original_first = _First;
const uint64_t __bits = __double_to_bits(__d);
// Case distinction; exit early for the easy cases.
if (__bits == 0) {
const int32_t _Total_zero_length = 1 // leading zero
+ static_cast<int32_t>(__precision != 0) // possible decimal point
+ static_cast<int32_t>(__precision) // zeroes after decimal point
+ 4; // "e+00"
if (_Last - _First < _Total_zero_length) {
return { _Last, errc::value_too_large };
}
*_First++ = '0';
if (__precision > 0) {
*_First++ = '.';
_CSTD memset(_First, '0', __precision);
_First += __precision;
}
_CSTD memcpy(_First, "e+00", 4);
_First += 4;
return { _First, errc{} };
}
// Decode __bits into mantissa and exponent.
const uint64_t __ieeeMantissa = __bits & ((1ull << __DOUBLE_MANTISSA_BITS) - 1);
const uint32_t __ieeeExponent = static_cast<uint32_t>(__bits >> __DOUBLE_MANTISSA_BITS);
int32_t __e2;
uint64_t __m2;
if (__ieeeExponent == 0) {
__e2 = 1 - __DOUBLE_BIAS - __DOUBLE_MANTISSA_BITS;
__m2 = __ieeeMantissa;
} else {
__e2 = static_cast<int32_t>(__ieeeExponent) - __DOUBLE_BIAS - __DOUBLE_MANTISSA_BITS;
__m2 = (1ull << __DOUBLE_MANTISSA_BITS) | __ieeeMantissa;
}
const bool __printDecimalPoint = __precision > 0;
++__precision;
uint32_t __digits = 0;
uint32_t __printedDigits = 0;
uint32_t __availableDigits = 0;
int32_t __exp = 0;
if (__e2 >= -52) {
const uint32_t __idx = __e2 < 0 ? 0 : __indexForExponent(static_cast<uint32_t>(__e2));
const uint32_t __p10bits = __pow10BitsForIndex(__idx);
const int32_t __len = static_cast<int32_t>(__lengthForIndex(__idx));
for (int32_t __i = __len - 1; __i >= 0; --__i) {
const uint32_t __j = __p10bits - __e2;
// Temporary: __j is usually around 128, and by shifting a bit, we push it to 128 or above, which is
// a slightly faster code path in __mulShift_mod1e9. Instead, we can just increase the multipliers.
__digits = __mulShift_mod1e9(__m2 << 8, __POW10_SPLIT[__POW10_OFFSET[__idx] + __i],
static_cast<int32_t>(__j + 8));
if (__printedDigits != 0) {
if (__printedDigits + 9 > __precision) {
__availableDigits = 9;
break;
}
if (_Last - _First < 9) {
return { _Last, errc::value_too_large };
}
__append_nine_digits(__digits, _First);
_First += 9;
__printedDigits += 9;
} else if (__digits != 0) {
__availableDigits = __decimalLength9(__digits);
__exp = __i * 9 + static_cast<int32_t>(__availableDigits) - 1;
if (__availableDigits > __precision) {
break;
}
if (__printDecimalPoint) {
if (_Last - _First < static_cast<ptrdiff_t>(__availableDigits + 1)) {
return { _Last, errc::value_too_large };
}
__append_d_digits(__availableDigits, __digits, _First);
_First += __availableDigits + 1; // +1 for decimal point
} else {
if (_First == _Last) {
return { _Last, errc::value_too_large };
}
*_First++ = static_cast<char>('0' + __digits);
}
__printedDigits = __availableDigits;
__availableDigits = 0;
}
}
}
if (__e2 < 0 && __availableDigits == 0) {
const int32_t __idx = -__e2 / 16;
for (int32_t __i = __MIN_BLOCK_2[__idx]; __i < 200; ++__i) {
const int32_t __j = __ADDITIONAL_BITS_2 + (-__e2 - 16 * __idx);
const uint32_t __p = __POW10_OFFSET_2[__idx] + static_cast<uint32_t>(__i) - __MIN_BLOCK_2[__idx];
// Temporary: __j is usually around 128, and by shifting a bit, we push it to 128 or above, which is
// a slightly faster code path in __mulShift_mod1e9. Instead, we can just increase the multipliers.
__digits = (__p >= __POW10_OFFSET_2[__idx + 1]) ? 0 : __mulShift_mod1e9(__m2 << 8, __POW10_SPLIT_2[__p], __j + 8);
if (__printedDigits != 0) {
if (__printedDigits + 9 > __precision) {
__availableDigits = 9;
break;
}
if (_Last - _First < 9) {
return { _Last, errc::value_too_large };
}
__append_nine_digits(__digits, _First);
_First += 9;
__printedDigits += 9;
} else if (__digits != 0) {
__availableDigits = __decimalLength9(__digits);
__exp = -(__i + 1) * 9 + static_cast<int32_t>(__availableDigits) - 1;
if (__availableDigits > __precision) {
break;
}
if (__printDecimalPoint) {
if (_Last - _First < static_cast<ptrdiff_t>(__availableDigits + 1)) {
return { _Last, errc::value_too_large };
}
__append_d_digits(__availableDigits, __digits, _First);
_First += __availableDigits + 1; // +1 for decimal point
} else {
if (_First == _Last) {
return { _Last, errc::value_too_large };
}
*_First++ = static_cast<char>('0' + __digits);
}
__printedDigits = __availableDigits;
__availableDigits = 0;
}
}
}
const uint32_t __maximum = __precision - __printedDigits;
if (__availableDigits == 0) {
__digits = 0;
}
uint32_t __lastDigit = 0;
if (__availableDigits > __maximum) {
for (uint32_t __k = 0; __k < __availableDigits - __maximum; ++__k) {
__lastDigit = __digits % 10;
__digits /= 10;
}
}
// 0 = don't round up; 1 = round up unconditionally; 2 = round up if odd.
int __roundUp = 0;
if (__lastDigit != 5) {
__roundUp = __lastDigit > 5;
} else {
// Is m * 2^__e2 * 10^(__precision + 1 - __exp) integer?
// __precision was already increased by 1, so we don't need to write + 1 here.
const int32_t __rexp = static_cast<int32_t>(__precision) - __exp;
const int32_t __requiredTwos = -__e2 - __rexp;
bool __trailingZeros = __requiredTwos <= 0
|| (__requiredTwos < 60 && __multipleOfPowerOf2(__m2, static_cast<uint32_t>(__requiredTwos)));
if (__rexp < 0) {
const int32_t __requiredFives = -__rexp;
__trailingZeros = __trailingZeros && __multipleOfPowerOf5(__m2, static_cast<uint32_t>(__requiredFives));
}
__roundUp = __trailingZeros ? 2 : 1;
}
if (__printedDigits != 0) {
if (_Last - _First < static_cast<ptrdiff_t>(__maximum)) {
return { _Last, errc::value_too_large };
}
if (__digits == 0) {
_CSTD memset(_First, '0', __maximum);
} else {
__append_c_digits(__maximum, __digits, _First);
}
_First += __maximum;
} else {
if (__printDecimalPoint) {
if (_Last - _First < static_cast<ptrdiff_t>(__maximum + 1)) {
return { _Last, errc::value_too_large };
}
__append_d_digits(__maximum, __digits, _First);
_First += __maximum + 1; // +1 for decimal point
} else {
if (_First == _Last) {
return { _Last, errc::value_too_large };
}
*_First++ = static_cast<char>('0' + __digits);
}
}
if (__roundUp != 0) {
char* _Round = _First;
while (true) {
if (_Round == _Original_first) {
_Round[0] = '1';
++__exp;
break;
}
--_Round;
const char __c = _Round[0];
if (__c == '.') {
// Keep going.
} else if (__c == '9') {
_Round[0] = '0';
__roundUp = 1;
} else {
if (__roundUp == 1 || __c % 2 != 0) {
_Round[0] = static_cast<char>(__c + 1);
}
break;
}
}
}
char _Sign_character;
if (__exp < 0) {
_Sign_character = '-';
__exp = -__exp;
} else {
_Sign_character = '+';
}
const int _Exponent_part_length = __exp >= 100
? 5 // "e+NNN"
: 4; // "e+NN"
if (_Last - _First < _Exponent_part_length) {
return { _Last, errc::value_too_large };
}
*_First++ = 'e';
*_First++ = _Sign_character;
if (__exp >= 100) {
const int32_t __c = __exp % 10;
_CSTD memcpy(_First, __DIGIT_TABLE<char> + 2 * (__exp / 10), 2);
_First[2] = static_cast<char>('0' + __c);
_First += 3;
} else {
_CSTD memcpy(_First, __DIGIT_TABLE<char> + 2 * __exp, 2);
_First += 2;
}
return { _First, errc{} };
}
// ^^^^^^^^^^ DERIVED FROM d2fixed.c ^^^^^^^^^^
// vvvvvvvvvv DERIVED FROM f2s.c vvvvvvvvvv
inline constexpr int __FLOAT_MANTISSA_BITS = 23;
inline constexpr int __FLOAT_BIAS = 127;
// This table is generated by PrintFloatLookupTable.
inline constexpr int __FLOAT_POW5_INV_BITCOUNT = 59;
inline constexpr uint64_t __FLOAT_POW5_INV_SPLIT[31] = {
576460752303423489u, 461168601842738791u, 368934881474191033u, 295147905179352826u,
472236648286964522u, 377789318629571618u, 302231454903657294u, 483570327845851670u,
386856262276681336u, 309485009821345069u, 495176015714152110u, 396140812571321688u,
316912650057057351u, 507060240091291761u, 405648192073033409u, 324518553658426727u,
519229685853482763u, 415383748682786211u, 332306998946228969u, 531691198313966350u,
425352958651173080u, 340282366920938464u, 544451787073501542u, 435561429658801234u,
348449143727040987u, 557518629963265579u, 446014903970612463u, 356811923176489971u,
570899077082383953u, 456719261665907162u, 365375409332725730u
};
inline constexpr int __FLOAT_POW5_BITCOUNT = 61;
inline constexpr uint64_t __FLOAT_POW5_SPLIT[47] = {
1152921504606846976u, 1441151880758558720u, 1801439850948198400u, 2251799813685248000u,
1407374883553280000u, 1759218604441600000u, 2199023255552000000u, 1374389534720000000u,
1717986918400000000u, 2147483648000000000u, 1342177280000000000u, 1677721600000000000u,
2097152000000000000u, 1310720000000000000u, 1638400000000000000u, 2048000000000000000u,
1280000000000000000u, 1600000000000000000u, 2000000000000000000u, 1250000000000000000u,
1562500000000000000u, 1953125000000000000u, 1220703125000000000u, 1525878906250000000u,
1907348632812500000u, 1192092895507812500u, 1490116119384765625u, 1862645149230957031u,
1164153218269348144u, 1455191522836685180u, 1818989403545856475u, 2273736754432320594u,
1421085471520200371u, 1776356839400250464u, 2220446049250313080u, 1387778780781445675u,
1734723475976807094u, 2168404344971008868u, 1355252715606880542u, 1694065894508600678u,
2117582368135750847u, 1323488980084844279u, 1654361225106055349u, 2067951531382569187u,
1292469707114105741u, 1615587133892632177u, 2019483917365790221u
};
_NODISCARD inline uint32_t __pow5Factor(uint32_t __value) {
uint32_t __count = 0;
for (;;) {
_STL_INTERNAL_CHECK(__value != 0);
const uint32_t __q = __value / 5;
const uint32_t __r = __value % 5;
if (__r != 0) {
break;
}
__value = __q;
++__count;
}
return __count;
}
// Returns true if __value is divisible by 5^__p.
_NODISCARD inline bool __multipleOfPowerOf5(const uint32_t __value, const uint32_t __p) {