Debian Codegraph @

podofo/3rdparty/fastfloat/fast_float/fast_float.h

raw
1 // fast_float by Daniel Lemire
2 // fast_float by João Paulo Magalhaes
3 //
4 //
5 // with contributions from Eugene Golushkov
6 // with contributions from Maksim Kita
7 // with contributions from Marcin Wojdyr
8 // with contributions from Neal Richardson
9 // with contributions from Tim Paine
10 // with contributions from Fabio Pellacini
11 // with contributions from Lénárd Szolnoki
12 // with contributions from Jan Pharago
13 // with contributions from Maya Warrier
14 // with contributions from Taha Khokhar
15 // with contributions from Anders Dalvander
16 //
17 //
18 // Licensed under the Apache License, Version 2.0, or the
19 // MIT License or the Boost License. This file may not be copied,
20 // modified, or distributed except according to those terms.
21 //
22 // MIT License Notice
23 //
24 // MIT License
25 //
26 // Copyright (c) 2021 The fast_float authors
27 //
28 // Permission is hereby granted, free of charge, to any
29 // person obtaining a copy of this software and associated
30 // documentation files (the "Software"), to deal in the
31 // Software without restriction, including without
32 // limitation the rights to use, copy, modify, merge,
33 // publish, distribute, sublicense, and/or sell copies of
34 // the Software, and to permit persons to whom the Software
35 // is furnished to do so, subject to the following
36 // conditions:
37 //
38 // The above copyright notice and this permission notice
39 // shall be included in all copies or substantial portions
40 // of the Software.
41 //
42 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF
43 // ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
44 // TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
45 // PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT
46 // SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
47 // CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
48 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR
49 // IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
50 // DEALINGS IN THE SOFTWARE.
51 //
52 // Apache License (Version 2.0) Notice
53 //
54 // Copyright 2021 The fast_float authors
55 // Licensed under the Apache License, Version 2.0 (the "License");
56 // you may not use this file except in compliance with the License.
57 // You may obtain a copy of the License at
58 //
59 // http://www.apache.org/licenses/LICENSE-2.0
60 //
61 // Unless required by applicable law or agreed to in writing, software
62 // distributed under the License is distributed on an "AS IS" BASIS,
63 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
64 // See the License for the specific language governing permissions and
65 //
66 // BOOST License Notice
67 //
68 // Boost Software License - Version 1.0 - August 17th, 2003
69 //
70 // Permission is hereby granted, free of charge, to any person or organization
71 // obtaining a copy of the software and accompanying documentation covered by
72 // this license (the "Software") to use, reproduce, display, distribute,
73 // execute, and transmit the Software, and to prepare derivative works of the
74 // Software, and to permit third-parties to whom the Software is furnished to
75 // do so, all subject to the following:
76 //
77 // The copyright notices in the Software and this entire statement, including
78 // the above license grant, this restriction and the following disclaimer,
79 // must be included in all copies of the Software, in whole or in part, and
80 // all derivative works of the Software, unless such copies or derivative
81 // works are solely in the form of machine-executable object code generated by
82 // a source language processor.
83 //
84 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
85 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
86 // FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
87 // SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
88 // FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
89 // ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
90 // DEALINGS IN THE SOFTWARE.
91 //
92
93 #ifndef FASTFLOAT_CONSTEXPR_FEATURE_DETECT_H
94 #define FASTFLOAT_CONSTEXPR_FEATURE_DETECT_H
95
96 #ifdef __has_include
97 #if __has_include(<version>)
98 #include <version>
99 #endif
100 #endif
101
102 // Testing for https://wg21.link/N3652, adopted in C++14
103 #if defined(__cpp_constexpr) && __cpp_constexpr >= 201304
104 #define FASTFLOAT_CONSTEXPR14 constexpr
105 #else
106 #define FASTFLOAT_CONSTEXPR14
107 #endif
108
109 #if defined(__cpp_lib_bit_cast) && __cpp_lib_bit_cast >= 201806L
110 #define FASTFLOAT_HAS_BIT_CAST 1
111 #else
112 #define FASTFLOAT_HAS_BIT_CAST 0
113 #endif
114
115 #if defined(__cpp_lib_is_constant_evaluated) && \
116 __cpp_lib_is_constant_evaluated >= 201811L
117 #define FASTFLOAT_HAS_IS_CONSTANT_EVALUATED 1
118 #else
119 #define FASTFLOAT_HAS_IS_CONSTANT_EVALUATED 0
120 #endif
121
122 #if defined(__cpp_if_constexpr) && __cpp_if_constexpr >= 201606L
123 #define FASTFLOAT_IF_CONSTEXPR17(x) if constexpr (x)
124 #else
125 #define FASTFLOAT_IF_CONSTEXPR17(x) if (x)
126 #endif
127
128 // Testing for relevant C++20 constexpr library features
129 #if FASTFLOAT_HAS_IS_CONSTANT_EVALUATED && FASTFLOAT_HAS_BIT_CAST && \
130 defined(__cpp_lib_constexpr_algorithms) && \
131 __cpp_lib_constexpr_algorithms >= 201806L /*For std::copy and std::fill*/
132 #define FASTFLOAT_CONSTEXPR20 constexpr
133 #define FASTFLOAT_IS_CONSTEXPR 1
134 #else
135 #define FASTFLOAT_CONSTEXPR20
136 #define FASTFLOAT_IS_CONSTEXPR 0
137 #endif
138
139 #if __cplusplus >= 201703L || (defined(_MSVC_LANG) && _MSVC_LANG >= 201703L)
140 #define FASTFLOAT_DETAIL_MUST_DEFINE_CONSTEXPR_VARIABLE 0
141 #else
142 #define FASTFLOAT_DETAIL_MUST_DEFINE_CONSTEXPR_VARIABLE 1
143 #endif
144
145 #endif // FASTFLOAT_CONSTEXPR_FEATURE_DETECT_H
146
147 #ifndef FASTFLOAT_FLOAT_COMMON_H
148 #define FASTFLOAT_FLOAT_COMMON_H
149
150 #include <cfloat>
151 #include <cstddef>
152 #include <cstdint>
153 #include <cassert>
154 #include <cstring>
155 #include <limits>
156 #include <type_traits>
157 #include <system_error>
158 #ifdef __has_include
159 #if __has_include(<stdfloat>) && (__cplusplus > 202002L || (defined(_MSVC_LANG) && (_MSVC_LANG > 202002L)))
160 #include <stdfloat>
161 #endif
162 #endif
163
164 #define FASTFLOAT_VERSION_MAJOR 8
165 #define FASTFLOAT_VERSION_MINOR 2
166 #define FASTFLOAT_VERSION_PATCH 4
167
168 #define FASTFLOAT_STRINGIZE_IMPL(x) #x
169 #define FASTFLOAT_STRINGIZE(x) FASTFLOAT_STRINGIZE_IMPL(x)
170
171 #define FASTFLOAT_VERSION_STR \
172 FASTFLOAT_STRINGIZE(FASTFLOAT_VERSION_MAJOR) \
173 "." FASTFLOAT_STRINGIZE(FASTFLOAT_VERSION_MINOR) "." FASTFLOAT_STRINGIZE( \
174 FASTFLOAT_VERSION_PATCH)
175
176 #define FASTFLOAT_VERSION \
177 (FASTFLOAT_VERSION_MAJOR * 10000 + FASTFLOAT_VERSION_MINOR * 100 + \
178 FASTFLOAT_VERSION_PATCH)
179
180 namespace fast_float {
181
182 enum class chars_format : uint64_t;
183
184 namespace detail {
185 constexpr chars_format basic_json_fmt = chars_format(1 << 5);
186 constexpr chars_format basic_fortran_fmt = chars_format(1 << 6);
187 } // namespace detail
188
189 enum class chars_format : uint64_t {
190 scientific = 1 << 0,
191 fixed = 1 << 2,
192 hex = 1 << 3,
193 no_infnan = 1 << 4,
194 // RFC 8259: https://datatracker.ietf.org/doc/html/rfc8259#section-6
195 json = uint64_t(detail::basic_json_fmt) | fixed | scientific | no_infnan,
196 // Extension of RFC 8259 where, e.g., "inf" and "nan" are allowed.
197 json_or_infnan = uint64_t(detail::basic_json_fmt) | fixed | scientific,
198 fortran = uint64_t(detail::basic_fortran_fmt) | fixed | scientific,
199 general = fixed | scientific,
200 allow_leading_plus = 1 << 7,
201 skip_white_space = 1 << 8,
202 };
203
204 template <typename UC> struct from_chars_result_t {
205 UC const *ptr;
206 std::errc ec;
207
208 // https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2023/p2497r0.html
209 constexpr explicit operator bool() const noexcept {
210 return ec == std::errc();
211 }
212 };
213
214 using from_chars_result = from_chars_result_t<char>;
215
216 template <typename UC> struct parse_options_t {
217 constexpr explicit parse_options_t(chars_format fmt = chars_format::general,
218 UC dot = UC('.'), int b = 10)
219 : format(fmt), decimal_point(dot), base(b) {}
220
221 /** Which number formats are accepted */
222 chars_format format;
223 /** The character used as decimal point */
224 UC decimal_point;
225 /** The base used for integers */
226 int base;
227 };
228
229 using parse_options = parse_options_t<char>;
230
231 } // namespace fast_float
232
233 #if FASTFLOAT_HAS_BIT_CAST
234 #include <bit>
235 #endif
236
237 #if (defined(__x86_64) || defined(__x86_64__) || defined(_M_X64) || \
238 defined(__amd64) || defined(__aarch64__) || defined(_M_ARM64) || \
239 defined(__MINGW64__) || defined(__s390x__) || \
240 (defined(__ppc64__) || defined(__PPC64__) || defined(__ppc64le__) || \
241 defined(__PPC64LE__)) || \
242 defined(__loongarch64) || (defined(__riscv) && __riscv_xlen == 64))
243 #define FASTFLOAT_64BIT 1
244 #elif (defined(__i386) || defined(__i386__) || defined(_M_IX86) || \
245 defined(__arm__) || defined(_M_ARM) || defined(__ppc__) || \
246 defined(__MINGW32__) || defined(__EMSCRIPTEN__) || \
247 (defined(__riscv) && __riscv_xlen == 32))
248 #define FASTFLOAT_32BIT 1
249 #else
250 // Need to check incrementally, since SIZE_MAX is a size_t, avoid overflow.
251 // We can never tell the register width, but the SIZE_MAX is a good
252 // approximation. UINTPTR_MAX and INTPTR_MAX are optional, so avoid them for max
253 // portability.
254 #if SIZE_MAX == 0xffff
255 #error Unknown platform (16-bit, unsupported)
256 #elif SIZE_MAX == 0xffffffff
257 #define FASTFLOAT_32BIT 1
258 #elif SIZE_MAX == 0xffffffffffffffff
259 #define FASTFLOAT_64BIT 1
260 #else
261 #error Unknown platform (not 32-bit, not 64-bit?)
262 #endif
263 #endif
264
265 #if ((defined(_WIN32) || defined(_WIN64)) && !defined(__clang__)) || \
266 (defined(_M_ARM64) && !defined(__MINGW32__))
267 #include <intrin.h>
268 #endif
269
270 #if defined(_MSC_VER) && !defined(__clang__)
271 #define FASTFLOAT_VISUAL_STUDIO 1
272 #endif
273
274 #if defined __BYTE_ORDER__ && defined __ORDER_BIG_ENDIAN__
275 #define FASTFLOAT_IS_BIG_ENDIAN (__BYTE_ORDER__ == __ORDER_BIG_ENDIAN__)
276 #elif defined _WIN32
277 #define FASTFLOAT_IS_BIG_ENDIAN 0
278 #else
279 #if defined(__APPLE__) || defined(__FreeBSD__)
280 #include <machine/endian.h>
281 #elif defined(sun) || defined(__sun)
282 #include <sys/byteorder.h>
283 #elif defined(__MVS__)
284 #include <sys/endian.h>
285 #else
286 #ifdef __has_include
287 #if __has_include(<endian.h>)
288 #include <endian.h>
289 #endif //__has_include(<endian.h>)
290 #endif //__has_include
291 #endif
292 #
293 #ifndef __BYTE_ORDER__
294 // safe choice
295 #define FASTFLOAT_IS_BIG_ENDIAN 0
296 #endif
297 #
298 #ifndef __ORDER_LITTLE_ENDIAN__
299 // safe choice
300 #define FASTFLOAT_IS_BIG_ENDIAN 0
301 #endif
302 #
303 #if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
304 #define FASTFLOAT_IS_BIG_ENDIAN 0
305 #else
306 #define FASTFLOAT_IS_BIG_ENDIAN 1
307 #endif
308 #endif
309
310 #if defined(__SSE2__) || (defined(FASTFLOAT_VISUAL_STUDIO) && \
311 (defined(_M_AMD64) || defined(_M_X64) || \
312 (defined(_M_IX86_FP) && _M_IX86_FP == 2)))
313 #define FASTFLOAT_SSE2 1
314 #endif
315
316 #if defined(__aarch64__) || defined(_M_ARM64)
317 #define FASTFLOAT_NEON 1
318 #endif
319
320 #if defined(FASTFLOAT_SSE2) || defined(FASTFLOAT_NEON)
321 #define FASTFLOAT_HAS_SIMD 1
322 #endif
323
324 #if defined(__GNUC__)
325 // disable -Wcast-align=strict (GCC only)
326 #define FASTFLOAT_SIMD_DISABLE_WARNINGS \
327 _Pragma("GCC diagnostic push") \
328 _Pragma("GCC diagnostic ignored \"-Wcast-align\"")
329 #else
330 #define FASTFLOAT_SIMD_DISABLE_WARNINGS
331 #endif
332
333 #if defined(__GNUC__)
334 #define FASTFLOAT_SIMD_RESTORE_WARNINGS _Pragma("GCC diagnostic pop")
335 #else
336 #define FASTFLOAT_SIMD_RESTORE_WARNINGS
337 #endif
338
339 #ifdef FASTFLOAT_VISUAL_STUDIO
340 #define fastfloat_really_inline __forceinline
341 #else
342 #define fastfloat_really_inline inline __attribute__((always_inline))
343 #endif
344
345 #ifndef FASTFLOAT_ASSERT
346 #define FASTFLOAT_ASSERT(x) \
347 { ((void)(x)); }
348 #endif
349
350 #ifndef FASTFLOAT_DEBUG_ASSERT
351 #define FASTFLOAT_DEBUG_ASSERT(x) \
352 { ((void)(x)); }
353 #endif
354
355 // rust style `try!()` macro, or `?` operator
356 #define FASTFLOAT_TRY(x) \
357 { \
358 if (!(x)) \
359 return false; \
360 }
361
362 #define FASTFLOAT_ENABLE_IF(...) \
363 typename std::enable_if<(__VA_ARGS__), int>::type
364
365 namespace fast_float {
366
367 fastfloat_really_inline constexpr bool cpp20_and_in_constexpr() {
368 #if FASTFLOAT_HAS_IS_CONSTANT_EVALUATED
369 return std::is_constant_evaluated();
370 #else
371 return false;
372 #endif
373 }
374
375 template <typename T>
376 struct is_supported_float_type
377 : std::integral_constant<
378 bool, std::is_same<T, double>::value || std::is_same<T, float>::value
379 #ifdef __STDCPP_FLOAT64_T__
380 || std::is_same<T, std::float64_t>::value
381 #endif
382 #ifdef __STDCPP_FLOAT32_T__
383 || std::is_same<T, std::float32_t>::value
384 #endif
385 #ifdef __STDCPP_FLOAT16_T__
386 || std::is_same<T, std::float16_t>::value
387 #endif
388 #ifdef __STDCPP_BFLOAT16_T__
389 || std::is_same<T, std::bfloat16_t>::value
390 #endif
391 > {
392 };
393
394 template <typename T>
395 using equiv_uint_t = typename std::conditional<
396 sizeof(T) == 1, uint8_t,
397 typename std::conditional<
398 sizeof(T) == 2, uint16_t,
399 typename std::conditional<sizeof(T) == 4, uint32_t,
400 uint64_t>::type>::type>::type;
401
402 template <typename T> struct is_supported_integer_type : std::is_integral<T> {};
403
404 template <typename UC>
405 struct is_supported_char_type
406 : std::integral_constant<bool, std::is_same<UC, char>::value ||
407 std::is_same<UC, wchar_t>::value ||
408 std::is_same<UC, char16_t>::value ||
409 std::is_same<UC, char32_t>::value
410 #ifdef __cpp_char8_t
411 || std::is_same<UC, char8_t>::value
412 #endif
413 > {
414 };
415
416 template <typename UC>
417 inline FASTFLOAT_CONSTEXPR14 bool
418 fastfloat_strncasecmp3(UC const *actual_mixedcase,
419 UC const *expected_lowercase) {
420 uint64_t mask{0};
421 FASTFLOAT_IF_CONSTEXPR17(sizeof(UC) == 1) { mask = 0x2020202020202020; }
422 else FASTFLOAT_IF_CONSTEXPR17(sizeof(UC) == 2) {
423 mask = 0x0020002000200020;
424 }
425 else FASTFLOAT_IF_CONSTEXPR17(sizeof(UC) == 4) {
426 mask = 0x0000002000000020;
427 }
428 else {
429 return false;
430 }
431
432 uint64_t val1{0}, val2{0};
433 if (cpp20_and_in_constexpr()) {
434 for (size_t i = 0; i < 3; i++) {
435 if ((actual_mixedcase[i] | 32) != expected_lowercase[i]) {
436 return false;
437 }
438 }
439 return true;
440 } else {
441 FASTFLOAT_IF_CONSTEXPR17(sizeof(UC) == 1 || sizeof(UC) == 2) {
442 ::memcpy(&val1, actual_mixedcase, 3 * sizeof(UC));
443 ::memcpy(&val2, expected_lowercase, 3 * sizeof(UC));
444 val1 |= mask;
445 val2 |= mask;
446 return val1 == val2;
447 }
448 else FASTFLOAT_IF_CONSTEXPR17(sizeof(UC) == 4) {
449 ::memcpy(&val1, actual_mixedcase, 2 * sizeof(UC));
450 ::memcpy(&val2, expected_lowercase, 2 * sizeof(UC));
451 val1 |= mask;
452 if (val1 != val2) {
453 return false;
454 }
455 return (actual_mixedcase[2] | 32) == (expected_lowercase[2]);
456 }
457 else {
458 return false;
459 }
460 }
461 }
462
463 template <typename UC>
464 inline FASTFLOAT_CONSTEXPR14 bool
465 fastfloat_strncasecmp5(UC const *actual_mixedcase,
466 UC const *expected_lowercase) {
467 uint64_t mask{0};
468 uint64_t val1{0}, val2{0};
469 if (cpp20_and_in_constexpr()) {
470 for (size_t i = 0; i < 5; i++) {
471 if ((actual_mixedcase[i] | 32) != expected_lowercase[i]) {
472 return false;
473 }
474 }
475 return true;
476 } else {
477 FASTFLOAT_IF_CONSTEXPR17(sizeof(UC) == 1) {
478 mask = 0x2020202020202020;
479 ::memcpy(&val1, actual_mixedcase, 5 * sizeof(UC));
480 ::memcpy(&val2, expected_lowercase, 5 * sizeof(UC));
481 val1 |= mask;
482 val2 |= mask;
483 return val1 == val2;
484 }
485 else FASTFLOAT_IF_CONSTEXPR17(sizeof(UC) == 2) {
486 mask = 0x0020002000200020;
487 ::memcpy(&val1, actual_mixedcase, 4 * sizeof(UC));
488 ::memcpy(&val2, expected_lowercase, 4 * sizeof(UC));
489 val1 |= mask;
490 if (val1 != val2) {
491 return false;
492 }
493 return (actual_mixedcase[4] | 32) == (expected_lowercase[4]);
494 }
495 else FASTFLOAT_IF_CONSTEXPR17(sizeof(UC) == 4) {
496 mask = 0x0000002000000020;
497 ::memcpy(&val1, actual_mixedcase, 2 * sizeof(UC));
498 ::memcpy(&val2, expected_lowercase, 2 * sizeof(UC));
499 val1 |= mask;
500 if (val1 != val2) {
501 return false;
502 }
503 ::memcpy(&val1, actual_mixedcase + 2, 2 * sizeof(UC));
504 ::memcpy(&val2, expected_lowercase + 2, 2 * sizeof(UC));
505 val1 |= mask;
506 if (val1 != val2) {
507 return false;
508 }
509 return (actual_mixedcase[4] | 32) == (expected_lowercase[4]);
510 }
511 else {
512 return false;
513 }
514 }
515 }
516
517 // Compares two ASCII strings in a case insensitive manner.
518 template <typename UC>
519 inline FASTFLOAT_CONSTEXPR14 bool
520 fastfloat_strncasecmp(UC const *actual_mixedcase, UC const *expected_lowercase,
521 size_t length) {
522 uint64_t mask{0};
523 FASTFLOAT_IF_CONSTEXPR17(sizeof(UC) == 1) { mask = 0x2020202020202020; }
524 else FASTFLOAT_IF_CONSTEXPR17(sizeof(UC) == 2) {
525 mask = 0x0020002000200020;
526 }
527 else FASTFLOAT_IF_CONSTEXPR17(sizeof(UC) == 4) {
528 mask = 0x0000002000000020;
529 }
530 else {
531 return false;
532 }
533
534 if (cpp20_and_in_constexpr()) {
535 for (size_t i = 0; i < length; i++) {
536 if ((actual_mixedcase[i] | 32) != expected_lowercase[i]) {
537 return false;
538 }
539 }
540 return true;
541 } else {
542 uint64_t val1{0}, val2{0};
543 size_t sz{8 / (sizeof(UC))};
544 for (size_t i = 0; i < length; i += sz) {
545 val1 = val2 = 0;
546 sz = std::min(sz, length - i);
547 ::memcpy(&val1, actual_mixedcase + i, sz * sizeof(UC));
548 ::memcpy(&val2, expected_lowercase + i, sz * sizeof(UC));
549 val1 |= mask;
550 val2 |= mask;
551 if (val1 != val2) {
552 return false;
553 }
554 }
555 return true;
556 }
557 }
558
559 #ifndef FLT_EVAL_METHOD
560 #error "FLT_EVAL_METHOD should be defined, please include cfloat."
561 #endif
562
563 // a pointer and a length to a contiguous block of memory
564 template <typename T> struct span {
565 T const *ptr;
566 size_t length;
567
568 constexpr span(T const *_ptr, size_t _length) : ptr(_ptr), length(_length) {}
569
570 constexpr span() : ptr(nullptr), length(0) {}
571
572 constexpr size_t len() const noexcept { return length; }
573
574 FASTFLOAT_CONSTEXPR14 const T &operator[](size_t index) const noexcept {
575 FASTFLOAT_DEBUG_ASSERT(index < length);
576 return ptr[index];
577 }
578 };
579
580 struct value128 {
581 uint64_t low;
582 uint64_t high;
583
584 constexpr value128(uint64_t _low, uint64_t _high) : low(_low), high(_high) {}
585
586 constexpr value128() : low(0), high(0) {}
587 };
588
589 /* Helper C++14 constexpr generic implementation of leading_zeroes */
590 fastfloat_really_inline FASTFLOAT_CONSTEXPR14 int
591 leading_zeroes_generic(uint64_t input_num, int last_bit = 0) {
592 if (input_num & uint64_t(0xffffffff00000000)) {
593 input_num >>= 32;
594 last_bit |= 32;
595 }
596 if (input_num & uint64_t(0xffff0000)) {
597 input_num >>= 16;
598 last_bit |= 16;
599 }
600 if (input_num & uint64_t(0xff00)) {
601 input_num >>= 8;
602 last_bit |= 8;
603 }
604 if (input_num & uint64_t(0xf0)) {
605 input_num >>= 4;
606 last_bit |= 4;
607 }
608 if (input_num & uint64_t(0xc)) {
609 input_num >>= 2;
610 last_bit |= 2;
611 }
612 if (input_num & uint64_t(0x2)) { /* input_num >>= 1; */
613 last_bit |= 1;
614 }
615 return 63 - last_bit;
616 }
617
618 /* result might be undefined when input_num is zero */
619 fastfloat_really_inline FASTFLOAT_CONSTEXPR20 int
620 leading_zeroes(uint64_t input_num) {
621 assert(input_num > 0);
622 if (cpp20_and_in_constexpr()) {
623 return leading_zeroes_generic(input_num);
624 }
625 #ifdef FASTFLOAT_VISUAL_STUDIO
626 #if defined(_M_X64) || defined(_M_ARM64)
627 unsigned long leading_zero = 0;
628 // Search the mask data from most significant bit (MSB)
629 // to least significant bit (LSB) for a set bit (1).
630 _BitScanReverse64(&leading_zero, input_num);
631 return (int)(63 - leading_zero);
632 #else
633 return leading_zeroes_generic(input_num);
634 #endif
635 #else
636 return __builtin_clzll(input_num);
637 #endif
638 }
639
640 /* Helper C++14 constexpr generic implementation of countr_zero for 32-bit */
641 fastfloat_really_inline FASTFLOAT_CONSTEXPR14 int
642 countr_zero_generic_32(uint32_t input_num) {
643 if (input_num == 0) {
644 return 32;
645 }
646 int last_bit = 0;
647 if (!(input_num & 0x0000FFFF)) {
648 input_num >>= 16;
649 last_bit |= 16;
650 }
651 if (!(input_num & 0x00FF)) {
652 input_num >>= 8;
653 last_bit |= 8;
654 }
655 if (!(input_num & 0x0F)) {
656 input_num >>= 4;
657 last_bit |= 4;
658 }
659 if (!(input_num & 0x3)) {
660 input_num >>= 2;
661 last_bit |= 2;
662 }
663 if (!(input_num & 0x1)) {
664 last_bit |= 1;
665 }
666 return last_bit;
667 }
668
669 /* count trailing zeroes for 32-bit integers */
670 fastfloat_really_inline FASTFLOAT_CONSTEXPR20 int
671 countr_zero_32(uint32_t input_num) {
672 if (cpp20_and_in_constexpr()) {
673 return countr_zero_generic_32(input_num);
674 }
675 #ifdef FASTFLOAT_VISUAL_STUDIO
676 unsigned long trailing_zero = 0;
677 if (_BitScanForward(&trailing_zero, input_num)) {
678 return (int)trailing_zero;
679 }
680 return 32;
681 #else
682 return input_num == 0 ? 32 : __builtin_ctz(input_num);
683 #endif
684 }
685
686 // slow emulation routine for 32-bit
687 fastfloat_really_inline constexpr uint64_t emulu(uint32_t x, uint32_t y) {
688 return x * (uint64_t)y;
689 }
690
691 fastfloat_really_inline FASTFLOAT_CONSTEXPR14 uint64_t
692 umul128_generic(uint64_t ab, uint64_t cd, uint64_t *hi) {
693 uint64_t ad = emulu((uint32_t)(ab >> 32), (uint32_t)cd);
694 uint64_t bd = emulu((uint32_t)ab, (uint32_t)cd);
695 uint64_t adbc = ad + emulu((uint32_t)ab, (uint32_t)(cd >> 32));
696 uint64_t adbc_carry = (uint64_t)(adbc < ad);
697 uint64_t lo = bd + (adbc << 32);
698 *hi = emulu((uint32_t)(ab >> 32), (uint32_t)(cd >> 32)) + (adbc >> 32) +
699 (adbc_carry << 32) + (uint64_t)(lo < bd);
700 return lo;
701 }
702
703 #ifdef FASTFLOAT_32BIT
704
705 // slow emulation routine for 32-bit
706 #if !defined(__MINGW64__)
707 fastfloat_really_inline FASTFLOAT_CONSTEXPR14 uint64_t _umul128(uint64_t ab,
708 uint64_t cd,
709 uint64_t *hi) {
710 return umul128_generic(ab, cd, hi);
711 }
712 #endif // !__MINGW64__
713
714 #endif // FASTFLOAT_32BIT
715
716 // compute 64-bit a*b
717 fastfloat_really_inline FASTFLOAT_CONSTEXPR20 value128
718 full_multiplication(uint64_t a, uint64_t b) {
719 if (cpp20_and_in_constexpr()) {
720 value128 answer;
721 answer.low = umul128_generic(a, b, &answer.high);
722 return answer;
723 }
724 value128 answer;
725 #if defined(_M_ARM64) && !defined(__MINGW32__)
726 // ARM64 has native support for 64-bit multiplications, no need to emulate
727 // But MinGW on ARM64 doesn't have native support for 64-bit multiplications
728 answer.high = __umulh(a, b);
729 answer.low = a * b;
730 #elif defined(FASTFLOAT_32BIT) || (defined(_WIN64) && !defined(__clang__) && \
731 !defined(_M_ARM64) && !defined(__GNUC__))
732 answer.low = _umul128(a, b, &answer.high); // _umul128 not available on ARM64
733 #elif defined(FASTFLOAT_64BIT) && defined(__SIZEOF_INT128__)
734 __uint128_t r = ((__uint128_t)a) * b;
735 answer.low = uint64_t(r);
736 answer.high = uint64_t(r >> 64);
737 #else
738 answer.low = umul128_generic(a, b, &answer.high);
739 #endif
740 return answer;
741 }
742
743 struct adjusted_mantissa {
744 uint64_t mantissa{0};
745 int32_t power2{0}; // a negative value indicates an invalid result
746 adjusted_mantissa() = default;
747
748 constexpr bool operator==(adjusted_mantissa const &o) const {
749 return mantissa == o.mantissa && power2 == o.power2;
750 }
751
752 constexpr bool operator!=(adjusted_mantissa const &o) const {
753 return mantissa != o.mantissa || power2 != o.power2;
754 }
755 };
756
757 // Bias so we can get the real exponent with an invalid adjusted_mantissa.
758 constexpr static int32_t invalid_am_bias = -0x8000;
759
760 // used for binary_format_lookup_tables<T>::max_mantissa
761 constexpr uint64_t constant_55555 = 5 * 5 * 5 * 5 * 5;
762
763 template <typename T, typename U = void> struct binary_format_lookup_tables;
764
765 template <typename T> struct binary_format : binary_format_lookup_tables<T> {
766 using equiv_uint = equiv_uint_t<T>;
767
768 static constexpr int mantissa_explicit_bits();
769 static constexpr int minimum_exponent();
770 static constexpr int infinite_power();
771 static constexpr int sign_index();
772 static constexpr int
773 min_exponent_fast_path(); // used when fegetround() == FE_TONEAREST
774 static constexpr int max_exponent_fast_path();
775 static constexpr int max_exponent_round_to_even();
776 static constexpr int min_exponent_round_to_even();
777 static constexpr uint64_t max_mantissa_fast_path(int64_t power);
778 static constexpr uint64_t
779 max_mantissa_fast_path(); // used when fegetround() == FE_TONEAREST
780 static constexpr int largest_power_of_ten();
781 static constexpr int smallest_power_of_ten();
782 static constexpr T exact_power_of_ten(int64_t power);
783 static constexpr size_t max_digits();
784 static constexpr equiv_uint exponent_mask();
785 static constexpr equiv_uint mantissa_mask();
786 static constexpr equiv_uint hidden_bit_mask();
787 };
788
789 template <typename U> struct binary_format_lookup_tables<double, U> {
790 static constexpr double powers_of_ten[] = {
791 1e0, 1e1, 1e2, 1e3, 1e4, 1e5, 1e6, 1e7, 1e8, 1e9, 1e10, 1e11,
792 1e12, 1e13, 1e14, 1e15, 1e16, 1e17, 1e18, 1e19, 1e20, 1e21, 1e22};
793
794 // Largest integer value v so that (5**index * v) <= 1<<53.
795 // 0x20000000000000 == 1 << 53
796 static constexpr uint64_t max_mantissa[] = {
797 0x20000000000000,
798 0x20000000000000 / 5,
799 0x20000000000000 / (5 * 5),
800 0x20000000000000 / (5 * 5 * 5),
801 0x20000000000000 / (5 * 5 * 5 * 5),
802 0x20000000000000 / (constant_55555),
803 0x20000000000000 / (constant_55555 * 5),
804 0x20000000000000 / (constant_55555 * 5 * 5),
805 0x20000000000000 / (constant_55555 * 5 * 5 * 5),
806 0x20000000000000 / (constant_55555 * 5 * 5 * 5 * 5),
807 0x20000000000000 / (constant_55555 * constant_55555),
808 0x20000000000000 / (constant_55555 * constant_55555 * 5),
809 0x20000000000000 / (constant_55555 * constant_55555 * 5 * 5),
810 0x20000000000000 / (constant_55555 * constant_55555 * 5 * 5 * 5),
811 0x20000000000000 / (constant_55555 * constant_55555 * constant_55555),
812 0x20000000000000 / (constant_55555 * constant_55555 * constant_55555 * 5),
813 0x20000000000000 /
814 (constant_55555 * constant_55555 * constant_55555 * 5 * 5),
815 0x20000000000000 /
816 (constant_55555 * constant_55555 * constant_55555 * 5 * 5 * 5),
817 0x20000000000000 /
818 (constant_55555 * constant_55555 * constant_55555 * 5 * 5 * 5 * 5),
819 0x20000000000000 /
820 (constant_55555 * constant_55555 * constant_55555 * constant_55555),
821 0x20000000000000 / (constant_55555 * constant_55555 * constant_55555 *
822 constant_55555 * 5),
823 0x20000000000000 / (constant_55555 * constant_55555 * constant_55555 *
824 constant_55555 * 5 * 5),
825 0x20000000000000 / (constant_55555 * constant_55555 * constant_55555 *
826 constant_55555 * 5 * 5 * 5),
827 0x20000000000000 / (constant_55555 * constant_55555 * constant_55555 *
828 constant_55555 * 5 * 5 * 5 * 5)};
829 };
830
831 #if FASTFLOAT_DETAIL_MUST_DEFINE_CONSTEXPR_VARIABLE
832
833 template <typename U>
834 constexpr double binary_format_lookup_tables<double, U>::powers_of_ten[];
835
836 template <typename U>
837 constexpr uint64_t binary_format_lookup_tables<double, U>::max_mantissa[];
838
839 #endif
840
841 template <typename U> struct binary_format_lookup_tables<float, U> {
842 static constexpr float powers_of_ten[] = {1e0f, 1e1f, 1e2f, 1e3f, 1e4f, 1e5f,
843 1e6f, 1e7f, 1e8f, 1e9f, 1e10f};
844
845 // Largest integer value v so that (5**index * v) <= 1<<24.
846 // 0x1000000 == 1<<24
847 static constexpr uint64_t max_mantissa[] = {
848 0x1000000,
849 0x1000000 / 5,
850 0x1000000 / (5 * 5),
851 0x1000000 / (5 * 5 * 5),
852 0x1000000 / (5 * 5 * 5 * 5),
853 0x1000000 / (constant_55555),
854 0x1000000 / (constant_55555 * 5),
855 0x1000000 / (constant_55555 * 5 * 5),
856 0x1000000 / (constant_55555 * 5 * 5 * 5),
857 0x1000000 / (constant_55555 * 5 * 5 * 5 * 5),
858 0x1000000 / (constant_55555 * constant_55555),
859 0x1000000 / (constant_55555 * constant_55555 * 5)};
860 };
861
862 #if FASTFLOAT_DETAIL_MUST_DEFINE_CONSTEXPR_VARIABLE
863
864 template <typename U>
865 constexpr float binary_format_lookup_tables<float, U>::powers_of_ten[];
866
867 template <typename U>
868 constexpr uint64_t binary_format_lookup_tables<float, U>::max_mantissa[];
869
870 #endif
871
872 template <>
873 inline constexpr int binary_format<double>::min_exponent_fast_path() {
874 #if (FLT_EVAL_METHOD != 1) && (FLT_EVAL_METHOD != 0)
875 return 0;
876 #else
877 return -22;
878 #endif
879 }
880
881 template <>
882 inline constexpr int binary_format<float>::min_exponent_fast_path() {
883 #if (FLT_EVAL_METHOD != 1) && (FLT_EVAL_METHOD != 0)
884 return 0;
885 #else
886 return -10;
887 #endif
888 }
889
890 template <>
891 inline constexpr int binary_format<double>::mantissa_explicit_bits() {
892 return 52;
893 }
894
895 template <>
896 inline constexpr int binary_format<float>::mantissa_explicit_bits() {
897 return 23;
898 }
899
900 template <>
901 inline constexpr int binary_format<double>::max_exponent_round_to_even() {
902 return 23;
903 }
904
905 template <>
906 inline constexpr int binary_format<float>::max_exponent_round_to_even() {
907 return 10;
908 }
909
910 template <>
911 inline constexpr int binary_format<double>::min_exponent_round_to_even() {
912 return -4;
913 }
914
915 template <>
916 inline constexpr int binary_format<float>::min_exponent_round_to_even() {
917 return -17;
918 }
919
920 template <> inline constexpr int binary_format<double>::minimum_exponent() {
921 return -1023;
922 }
923
924 template <> inline constexpr int binary_format<float>::minimum_exponent() {
925 return -127;
926 }
927
928 template <> inline constexpr int binary_format<double>::infinite_power() {
929 return 0x7FF;
930 }
931
932 template <> inline constexpr int binary_format<float>::infinite_power() {
933 return 0xFF;
934 }
935
936 template <> inline constexpr int binary_format<double>::sign_index() {
937 return 63;
938 }
939
940 template <> inline constexpr int binary_format<float>::sign_index() {
941 return 31;
942 }
943
944 template <>
945 inline constexpr int binary_format<double>::max_exponent_fast_path() {
946 return 22;
947 }
948
949 template <>
950 inline constexpr int binary_format<float>::max_exponent_fast_path() {
951 return 10;
952 }
953
954 template <>
955 inline constexpr uint64_t binary_format<double>::max_mantissa_fast_path() {
956 return uint64_t(2) << mantissa_explicit_bits();
957 }
958
959 template <>
960 inline constexpr uint64_t binary_format<float>::max_mantissa_fast_path() {
961 return uint64_t(2) << mantissa_explicit_bits();
962 }
963
964 // credit: Jakub Jelínek
965 #ifdef __STDCPP_FLOAT16_T__
966 template <typename U> struct binary_format_lookup_tables<std::float16_t, U> {
967 static constexpr std::float16_t powers_of_ten[] = {1e0f16, 1e1f16, 1e2f16,
968 1e3f16, 1e4f16};
969
970 // Largest integer value v so that (5**index * v) <= 1<<11.
971 // 0x800 == 1<<11
972 static constexpr uint64_t max_mantissa[] = {0x800,
973 0x800 / 5,
974 0x800 / (5 * 5),
975 0x800 / (5 * 5 * 5),
976 0x800 / (5 * 5 * 5 * 5),
977 0x800 / (constant_55555)};
978 };
979
980 #if FASTFLOAT_DETAIL_MUST_DEFINE_CONSTEXPR_VARIABLE
981
982 template <typename U>
983 constexpr std::float16_t
984 binary_format_lookup_tables<std::float16_t, U>::powers_of_ten[];
985
986 template <typename U>
987 constexpr uint64_t
988 binary_format_lookup_tables<std::float16_t, U>::max_mantissa[];
989
990 #endif
991
992 template <>
993 inline constexpr std::float16_t
994 binary_format<std::float16_t>::exact_power_of_ten(int64_t power) {
995 // Work around clang bug https://godbolt.org/z/zedh7rrhc
996 return (void)powers_of_ten[0], powers_of_ten[power];
997 }
998
999 template <>
1000 inline constexpr binary_format<std::float16_t>::equiv_uint
1001 binary_format<std::float16_t>::exponent_mask() {
1002 return 0x7C00;
1003 }
1004
1005 template <>
1006 inline constexpr binary_format<std::float16_t>::equiv_uint
1007 binary_format<std::float16_t>::mantissa_mask() {
1008 return 0x03FF;
1009 }
1010
1011 template <>
1012 inline constexpr binary_format<std::float16_t>::equiv_uint
1013 binary_format<std::float16_t>::hidden_bit_mask() {
1014 return 0x0400;
1015 }
1016
1017 template <>
1018 inline constexpr int binary_format<std::float16_t>::max_exponent_fast_path() {
1019 return 4;
1020 }
1021
1022 template <>
1023 inline constexpr int binary_format<std::float16_t>::mantissa_explicit_bits() {
1024 return 10;
1025 }
1026
1027 template <>
1028 inline constexpr uint64_t
1029 binary_format<std::float16_t>::max_mantissa_fast_path() {
1030 return uint64_t(2) << mantissa_explicit_bits();
1031 }
1032
1033 template <>
1034 inline constexpr uint64_t
1035 binary_format<std::float16_t>::max_mantissa_fast_path(int64_t power) {
1036 // caller is responsible to ensure that
1037 // power >= 0 && power <= 4
1038 //
1039 // Work around clang bug https://godbolt.org/z/zedh7rrhc
1040 return (void)max_mantissa[0], max_mantissa[power];
1041 }
1042
1043 template <>
1044 inline constexpr int binary_format<std::float16_t>::min_exponent_fast_path() {
1045 return 0;
1046 }
1047
1048 template <>
1049 inline constexpr int
1050 binary_format<std::float16_t>::max_exponent_round_to_even() {
1051 return 5;
1052 }
1053
1054 template <>
1055 inline constexpr int
1056 binary_format<std::float16_t>::min_exponent_round_to_even() {
1057 return -22;
1058 }
1059
1060 template <>
1061 inline constexpr int binary_format<std::float16_t>::minimum_exponent() {
1062 return -15;
1063 }
1064
1065 template <>
1066 inline constexpr int binary_format<std::float16_t>::infinite_power() {
1067 return 0x1F;
1068 }
1069
1070 template <> inline constexpr int binary_format<std::float16_t>::sign_index() {
1071 return 15;
1072 }
1073
1074 template <>
1075 inline constexpr int binary_format<std::float16_t>::largest_power_of_ten() {
1076 return 4;
1077 }
1078
1079 template <>
1080 inline constexpr int binary_format<std::float16_t>::smallest_power_of_ten() {
1081 return -27;
1082 }
1083
1084 template <>
1085 inline constexpr size_t binary_format<std::float16_t>::max_digits() {
1086 return 22;
1087 }
1088 #endif // __STDCPP_FLOAT16_T__
1089
1090 // credit: Jakub Jelínek
1091 #ifdef __STDCPP_BFLOAT16_T__
1092 template <typename U> struct binary_format_lookup_tables<std::bfloat16_t, U> {
1093 static constexpr std::bfloat16_t powers_of_ten[] = {1e0bf16, 1e1bf16, 1e2bf16,
1094 1e3bf16};
1095
1096 // Largest integer value v so that (5**index * v) <= 1<<8.
1097 // 0x100 == 1<<8
1098 static constexpr uint64_t max_mantissa[] = {0x100, 0x100 / 5, 0x100 / (5 * 5),
1099 0x100 / (5 * 5 * 5),
1100 0x100 / (5 * 5 * 5 * 5)};
1101 };
1102
1103 #if FASTFLOAT_DETAIL_MUST_DEFINE_CONSTEXPR_VARIABLE
1104
1105 template <typename U>
1106 constexpr std::bfloat16_t
1107 binary_format_lookup_tables<std::bfloat16_t, U>::powers_of_ten[];
1108
1109 template <typename U>
1110 constexpr uint64_t
1111 binary_format_lookup_tables<std::bfloat16_t, U>::max_mantissa[];
1112
1113 #endif
1114
1115 template <>
1116 inline constexpr std::bfloat16_t
1117 binary_format<std::bfloat16_t>::exact_power_of_ten(int64_t power) {
1118 // Work around clang bug https://godbolt.org/z/zedh7rrhc
1119 return (void)powers_of_ten[0], powers_of_ten[power];
1120 }
1121
1122 template <>
1123 inline constexpr int binary_format<std::bfloat16_t>::max_exponent_fast_path() {
1124 return 3;
1125 }
1126
1127 template <>
1128 inline constexpr binary_format<std::bfloat16_t>::equiv_uint
1129 binary_format<std::bfloat16_t>::exponent_mask() {
1130 return 0x7F80;
1131 }
1132
1133 template <>
1134 inline constexpr binary_format<std::bfloat16_t>::equiv_uint
1135 binary_format<std::bfloat16_t>::mantissa_mask() {
1136 return 0x007F;
1137 }
1138
1139 template <>
1140 inline constexpr binary_format<std::bfloat16_t>::equiv_uint
1141 binary_format<std::bfloat16_t>::hidden_bit_mask() {
1142 return 0x0080;
1143 }
1144
1145 template <>
1146 inline constexpr int binary_format<std::bfloat16_t>::mantissa_explicit_bits() {
1147 return 7;
1148 }
1149
1150 template <>
1151 inline constexpr uint64_t
1152 binary_format<std::bfloat16_t>::max_mantissa_fast_path() {
1153 return uint64_t(2) << mantissa_explicit_bits();
1154 }
1155
1156 template <>
1157 inline constexpr uint64_t
1158 binary_format<std::bfloat16_t>::max_mantissa_fast_path(int64_t power) {
1159 // caller is responsible to ensure that
1160 // power >= 0 && power <= 3
1161 //
1162 // Work around clang bug https://godbolt.org/z/zedh7rrhc
1163 return (void)max_mantissa[0], max_mantissa[power];
1164 }
1165
1166 template <>
1167 inline constexpr int binary_format<std::bfloat16_t>::min_exponent_fast_path() {
1168 return 0;
1169 }
1170
1171 template <>
1172 inline constexpr int
1173 binary_format<std::bfloat16_t>::max_exponent_round_to_even() {
1174 return 3;
1175 }
1176
1177 template <>
1178 inline constexpr int
1179 binary_format<std::bfloat16_t>::min_exponent_round_to_even() {
1180 return -24;
1181 }
1182
1183 template <>
1184 inline constexpr int binary_format<std::bfloat16_t>::minimum_exponent() {
1185 return -127;
1186 }
1187
1188 template <>
1189 inline constexpr int binary_format<std::bfloat16_t>::infinite_power() {
1190 return 0xFF;
1191 }
1192
1193 template <> inline constexpr int binary_format<std::bfloat16_t>::sign_index() {
1194 return 15;
1195 }
1196
1197 template <>
1198 inline constexpr int binary_format<std::bfloat16_t>::largest_power_of_ten() {
1199 return 38;
1200 }
1201
1202 template <>
1203 inline constexpr int binary_format<std::bfloat16_t>::smallest_power_of_ten() {
1204 return -60;
1205 }
1206
1207 template <>
1208 inline constexpr size_t binary_format<std::bfloat16_t>::max_digits() {
1209 return 98;
1210 }
1211 #endif // __STDCPP_BFLOAT16_T__
1212
1213 template <>
1214 inline constexpr uint64_t
1215 binary_format<double>::max_mantissa_fast_path(int64_t power) {
1216 // caller is responsible to ensure that
1217 // power >= 0 && power <= 22
1218 //
1219 // Work around clang bug https://godbolt.org/z/zedh7rrhc
1220 return (void)max_mantissa[0], max_mantissa[power];
1221 }
1222
1223 template <>
1224 inline constexpr uint64_t
1225 binary_format<float>::max_mantissa_fast_path(int64_t power) {
1226 // caller is responsible to ensure that
1227 // power >= 0 && power <= 10
1228 //
1229 // Work around clang bug https://godbolt.org/z/zedh7rrhc
1230 return (void)max_mantissa[0], max_mantissa[power];
1231 }
1232
1233 template <>
1234 inline constexpr double
1235 binary_format<double>::exact_power_of_ten(int64_t power) {
1236 // Work around clang bug https://godbolt.org/z/zedh7rrhc
1237 return (void)powers_of_ten[0], powers_of_ten[power];
1238 }
1239
1240 template <>
1241 inline constexpr float binary_format<float>::exact_power_of_ten(int64_t power) {
1242 // Work around clang bug https://godbolt.org/z/zedh7rrhc
1243 return (void)powers_of_ten[0], powers_of_ten[power];
1244 }
1245
1246 template <> inline constexpr int binary_format<double>::largest_power_of_ten() {
1247 return 308;
1248 }
1249
1250 template <> inline constexpr int binary_format<float>::largest_power_of_ten() {
1251 return 38;
1252 }
1253
1254 template <>
1255 inline constexpr int binary_format<double>::smallest_power_of_ten() {
1256 return -342;
1257 }
1258
1259 template <> inline constexpr int binary_format<float>::smallest_power_of_ten() {
1260 return -64;
1261 }
1262
1263 template <> inline constexpr size_t binary_format<double>::max_digits() {
1264 return 769;
1265 }
1266
1267 template <> inline constexpr size_t binary_format<float>::max_digits() {
1268 return 114;
1269 }
1270
1271 template <>
1272 inline constexpr binary_format<float>::equiv_uint
1273 binary_format<float>::exponent_mask() {
1274 return 0x7F800000;
1275 }
1276
1277 template <>
1278 inline constexpr binary_format<double>::equiv_uint
1279 binary_format<double>::exponent_mask() {
1280 return 0x7FF0000000000000;
1281 }
1282
1283 template <>
1284 inline constexpr binary_format<float>::equiv_uint
1285 binary_format<float>::mantissa_mask() {
1286 return 0x007FFFFF;
1287 }
1288
1289 template <>
1290 inline constexpr binary_format<double>::equiv_uint
1291 binary_format<double>::mantissa_mask() {
1292 return 0x000FFFFFFFFFFFFF;
1293 }
1294
1295 template <>
1296 inline constexpr binary_format<float>::equiv_uint
1297 binary_format<float>::hidden_bit_mask() {
1298 return 0x00800000;
1299 }
1300
1301 template <>
1302 inline constexpr binary_format<double>::equiv_uint
1303 binary_format<double>::hidden_bit_mask() {
1304 return 0x0010000000000000;
1305 }
1306
1307 template <typename T>
1308 fastfloat_really_inline FASTFLOAT_CONSTEXPR20 void
1309 to_float(bool negative, adjusted_mantissa am, T &value) {
1310 using equiv_uint = equiv_uint_t<T>;
1311 equiv_uint word = equiv_uint(am.mantissa);
1312 word = equiv_uint(word | equiv_uint(am.power2)
1313 << binary_format<T>::mantissa_explicit_bits());
1314 word =
1315 equiv_uint(word | equiv_uint(negative) << binary_format<T>::sign_index());
1316 #if FASTFLOAT_HAS_BIT_CAST
1317 value = std::bit_cast<T>(word);
1318 #else
1319 ::memcpy(&value, &word, sizeof(T));
1320 #endif
1321 }
1322
1323 template <typename = void> struct space_lut {
1324 static constexpr bool value[] = {
1325 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1326 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1327 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1328 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1329 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1330 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1331 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1332 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1333 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1334 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1335 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
1336 };
1337
1338 #if FASTFLOAT_DETAIL_MUST_DEFINE_CONSTEXPR_VARIABLE
1339
1340 template <typename T> constexpr bool space_lut<T>::value[];
1341
1342 #endif
1343
1344 template <typename UC> constexpr bool is_space(UC c) {
1345 return c < 256 && space_lut<>::value[uint8_t(c)];
1346 }
1347
1348 template <typename UC> static constexpr uint64_t int_cmp_zeros() {
1349 static_assert((sizeof(UC) == 1) || (sizeof(UC) == 2) || (sizeof(UC) == 4),
1350 "Unsupported character size");
1351 return (sizeof(UC) == 1) ? 0x3030303030303030
1352 : (sizeof(UC) == 2)
1353 ? (uint64_t(UC('0')) << 48 | uint64_t(UC('0')) << 32 |
1354 uint64_t(UC('0')) << 16 | UC('0'))
1355 : (uint64_t(UC('0')) << 32 | UC('0'));
1356 }
1357
1358 template <typename UC> static constexpr int int_cmp_len() {
1359 return sizeof(uint64_t) / sizeof(UC);
1360 }
1361
1362 template <typename UC> constexpr UC const *str_const_nan();
1363
1364 template <> constexpr char const *str_const_nan<char>() { return "nan"; }
1365
1366 template <> constexpr wchar_t const *str_const_nan<wchar_t>() { return L"nan"; }
1367
1368 template <> constexpr char16_t const *str_const_nan<char16_t>() {
1369 return u"nan";
1370 }
1371
1372 template <> constexpr char32_t const *str_const_nan<char32_t>() {
1373 return U"nan";
1374 }
1375
1376 #ifdef __cpp_char8_t
1377 template <> constexpr char8_t const *str_const_nan<char8_t>() {
1378 return u8"nan";
1379 }
1380 #endif
1381
1382 template <typename UC> constexpr UC const *str_const_inf();
1383
1384 template <> constexpr char const *str_const_inf<char>() { return "infinity"; }
1385
1386 template <> constexpr wchar_t const *str_const_inf<wchar_t>() {
1387 return L"infinity";
1388 }
1389
1390 template <> constexpr char16_t const *str_const_inf<char16_t>() {
1391 return u"infinity";
1392 }
1393
1394 template <> constexpr char32_t const *str_const_inf<char32_t>() {
1395 return U"infinity";
1396 }
1397
1398 #ifdef __cpp_char8_t
1399 template <> constexpr char8_t const *str_const_inf<char8_t>() {
1400 return u8"infinity";
1401 }
1402 #endif
1403
1404 template <typename = void> struct int_luts {
1405 static constexpr uint8_t chdigit[] = {
1406 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
1407 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
1408 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
1409 255, 255, 255, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 255, 255,
1410 255, 255, 255, 255, 255, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19,
1411 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34,
1412 35, 255, 255, 255, 255, 255, 255, 10, 11, 12, 13, 14, 15, 16, 17,
1413 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32,
1414 33, 34, 35, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
1415 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
1416 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
1417 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
1418 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
1419 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
1420 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
1421 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
1422 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
1423 255};
1424
1425 static constexpr size_t maxdigits_u64[] = {
1426 64, 41, 32, 28, 25, 23, 22, 21, 20, 19, 18, 18, 17, 17, 16, 16, 16, 16,
1427 15, 15, 15, 15, 14, 14, 14, 14, 14, 14, 14, 13, 13, 13, 13, 13, 13};
1428
1429 static constexpr uint64_t min_safe_u64[] = {
1430 9223372036854775808ull, 12157665459056928801ull, 4611686018427387904,
1431 7450580596923828125, 4738381338321616896, 3909821048582988049,
1432 9223372036854775808ull, 12157665459056928801ull, 10000000000000000000ull,
1433 5559917313492231481, 2218611106740436992, 8650415919381337933,
1434 2177953337809371136, 6568408355712890625, 1152921504606846976,
1435 2862423051509815793, 6746640616477458432, 15181127029874798299ull,
1436 1638400000000000000, 3243919932521508681, 6221821273427820544,
1437 11592836324538749809ull, 876488338465357824, 1490116119384765625,
1438 2481152873203736576, 4052555153018976267, 6502111422497947648,
1439 10260628712958602189ull, 15943230000000000000ull, 787662783788549761,
1440 1152921504606846976, 1667889514952984961, 2386420683693101056,
1441 3379220508056640625, 4738381338321616896};
1442 };
1443
1444 #if FASTFLOAT_DETAIL_MUST_DEFINE_CONSTEXPR_VARIABLE
1445
1446 template <typename T> constexpr uint8_t int_luts<T>::chdigit[];
1447
1448 template <typename T> constexpr size_t int_luts<T>::maxdigits_u64[];
1449
1450 template <typename T> constexpr uint64_t int_luts<T>::min_safe_u64[];
1451
1452 #endif
1453
1454 template <typename UC>
1455 fastfloat_really_inline constexpr uint8_t ch_to_digit(UC c) {
1456 // wchar_t and char can be signed, so we need to be careful.
1457 using UnsignedUC = typename std::make_unsigned<UC>::type;
1458 return int_luts<>::chdigit[static_cast<unsigned char>(
1459 static_cast<UnsignedUC>(c) &
1460 static_cast<UnsignedUC>(
1461 -((static_cast<UnsignedUC>(c) & ~0xFFull) == 0)))];
1462 }
1463
1464 fastfloat_really_inline constexpr size_t max_digits_u64(int base) {
1465 return int_luts<>::maxdigits_u64[base - 2];
1466 }
1467
1468 // If a u64 is exactly max_digits_u64() in length, this is
1469 // the value below which it has definitely overflowed.
1470 fastfloat_really_inline constexpr uint64_t min_safe_u64(int base) {
1471 return int_luts<>::min_safe_u64[base - 2];
1472 }
1473
1474 static_assert(std::is_same<equiv_uint_t<double>, uint64_t>::value,
1475 "equiv_uint should be uint64_t for double");
1476 static_assert(std::numeric_limits<double>::is_iec559,
1477 "double must fulfill the requirements of IEC 559 (IEEE 754)");
1478
1479 static_assert(std::is_same<equiv_uint_t<float>, uint32_t>::value,
1480 "equiv_uint should be uint32_t for float");
1481 static_assert(std::numeric_limits<float>::is_iec559,
1482 "float must fulfill the requirements of IEC 559 (IEEE 754)");
1483
1484 #ifdef __STDCPP_FLOAT64_T__
1485 static_assert(std::is_same<equiv_uint_t<std::float64_t>, uint64_t>::value,
1486 "equiv_uint should be uint64_t for std::float64_t");
1487 static_assert(
1488 std::numeric_limits<std::float64_t>::is_iec559,
1489 "std::float64_t must fulfill the requirements of IEC 559 (IEEE 754)");
1490
1491 template <>
1492 struct binary_format<std::float64_t> : public binary_format<double> {};
1493 #endif // __STDCPP_FLOAT64_T__
1494
1495 #ifdef __STDCPP_FLOAT32_T__
1496 static_assert(std::is_same<equiv_uint_t<std::float32_t>, uint32_t>::value,
1497 "equiv_uint should be uint32_t for std::float32_t");
1498 static_assert(
1499 std::numeric_limits<std::float32_t>::is_iec559,
1500 "std::float32_t must fulfill the requirements of IEC 559 (IEEE 754)");
1501
1502 template <>
1503 struct binary_format<std::float32_t> : public binary_format<float> {};
1504 #endif // __STDCPP_FLOAT32_T__
1505
1506 #ifdef __STDCPP_FLOAT16_T__
1507 static_assert(
1508 std::is_same<binary_format<std::float16_t>::equiv_uint, uint16_t>::value,
1509 "equiv_uint should be uint16_t for std::float16_t");
1510 static_assert(
1511 std::numeric_limits<std::float16_t>::is_iec559,
1512 "std::float16_t must fulfill the requirements of IEC 559 (IEEE 754)");
1513 #endif // __STDCPP_FLOAT16_T__
1514
1515 #ifdef __STDCPP_BFLOAT16_T__
1516 static_assert(
1517 std::is_same<binary_format<std::bfloat16_t>::equiv_uint, uint16_t>::value,
1518 "equiv_uint should be uint16_t for std::bfloat16_t");
1519 static_assert(
1520 std::numeric_limits<std::bfloat16_t>::is_iec559,
1521 "std::bfloat16_t must fulfill the requirements of IEC 559 (IEEE 754)");
1522 #endif // __STDCPP_BFLOAT16_T__
1523
1524 constexpr chars_format operator~(chars_format rhs) noexcept {
1525 using int_type = std::underlying_type<chars_format>::type;
1526 return static_cast<chars_format>(~static_cast<int_type>(rhs));
1527 }
1528
1529 constexpr chars_format operator&(chars_format lhs, chars_format rhs) noexcept {
1530 using int_type = std::underlying_type<chars_format>::type;
1531 return static_cast<chars_format>(static_cast<int_type>(lhs) &
1532 static_cast<int_type>(rhs));
1533 }
1534
1535 constexpr chars_format operator|(chars_format lhs, chars_format rhs) noexcept {
1536 using int_type = std::underlying_type<chars_format>::type;
1537 return static_cast<chars_format>(static_cast<int_type>(lhs) |
1538 static_cast<int_type>(rhs));
1539 }
1540
1541 constexpr chars_format operator^(chars_format lhs, chars_format rhs) noexcept {
1542 using int_type = std::underlying_type<chars_format>::type;
1543 return static_cast<chars_format>(static_cast<int_type>(lhs) ^
1544 static_cast<int_type>(rhs));
1545 }
1546
1547 fastfloat_really_inline FASTFLOAT_CONSTEXPR14 chars_format &
1548 operator&=(chars_format &lhs, chars_format rhs) noexcept {
1549 return lhs = (lhs & rhs);
1550 }
1551
1552 fastfloat_really_inline FASTFLOAT_CONSTEXPR14 chars_format &
1553 operator|=(chars_format &lhs, chars_format rhs) noexcept {
1554 return lhs = (lhs | rhs);
1555 }
1556
1557 fastfloat_really_inline FASTFLOAT_CONSTEXPR14 chars_format &
1558 operator^=(chars_format &lhs, chars_format rhs) noexcept {
1559 return lhs = (lhs ^ rhs);
1560 }
1561
1562 namespace detail {
1563 // adjust for deprecated feature macros
1564 constexpr chars_format adjust_for_feature_macros(chars_format fmt) {
1565 return fmt
1566 #ifdef FASTFLOAT_ALLOWS_LEADING_PLUS
1567 | chars_format::allow_leading_plus
1568 #endif
1569 #ifdef FASTFLOAT_SKIP_WHITE_SPACE
1570 | chars_format::skip_white_space
1571 #endif
1572 ;
1573 }
1574 } // namespace detail
1575 } // namespace fast_float
1576
1577 #endif
1578
1579
1580 #ifndef FASTFLOAT_FAST_FLOAT_H
1581 #define FASTFLOAT_FAST_FLOAT_H
1582
1583
1584 namespace fast_float {
1585 /**
1586 * This function parses the character sequence [first,last) for a number. It
1587 * parses floating-point numbers expecting a locale-indepent format equivalent
1588 * to what is used by std::strtod in the default ("C") locale. The resulting
1589 * floating-point value is the closest floating-point values (using either float
1590 * or double), using the "round to even" convention for values that would
1591 * otherwise fall right in-between two values. That is, we provide exact parsing
1592 * according to the IEEE standard.
1593 *
1594 * Given a successful parse, the pointer (`ptr`) in the returned value is set to
1595 * point right after the parsed number, and the `value` referenced is set to the
1596 * parsed value. In case of error, the returned `ec` contains a representative
1597 * error, otherwise the default (`std::errc()`) value is stored.
1598 *
1599 * The implementation does not throw and does not allocate memory (e.g., with
1600 * `new` or `malloc`).
1601 *
1602 * Like the C++17 standard, the `fast_float::from_chars` functions take an
1603 * optional last argument of the type `fast_float::chars_format`. It is a bitset
1604 * value: we check whether `fmt & fast_float::chars_format::fixed` and `fmt &
1605 * fast_float::chars_format::scientific` are set to determine whether we allow
1606 * the fixed point and scientific notation respectively. The default is
1607 * `fast_float::chars_format::general` which allows both `fixed` and
1608 * `scientific`.
1609 */
1610 template <typename T, typename UC = char,
1611 typename = FASTFLOAT_ENABLE_IF(is_supported_float_type<T>::value)>
1612 FASTFLOAT_CONSTEXPR20 from_chars_result_t<UC>
1613 from_chars(UC const *first, UC const *last, T &value,
1614 chars_format fmt = chars_format::general) noexcept;
1615
1616 /**
1617 * Like from_chars, but accepts an `options` argument to govern number parsing.
1618 * Both for floating-point types and integer types.
1619 */
1620 template <typename T, typename UC = char>
1621 FASTFLOAT_CONSTEXPR20 from_chars_result_t<UC>
1622 from_chars_advanced(UC const *first, UC const *last, T &value,
1623 parse_options_t<UC> options) noexcept;
1624
1625 /**
1626 * This function multiplies an integer number by a power of 10 and returns
1627 * the result as a double precision floating-point value that is correctly
1628 * rounded. The resulting floating-point value is the closest floating-point
1629 * value, using the "round to nearest, tie to even" convention for values that
1630 * would otherwise fall right in-between two values. That is, we provide exact
1631 * conversion according to the IEEE standard.
1632 *
1633 * On overflow infinity is returned, on underflow 0 is returned.
1634 *
1635 * The implementation does not throw and does not allocate memory (e.g., with
1636 * `new` or `malloc`).
1637 */
1638 FASTFLOAT_CONSTEXPR20 inline double
1639 integer_times_pow10(uint64_t mantissa, int decimal_exponent) noexcept;
1640 FASTFLOAT_CONSTEXPR20 inline double
1641 integer_times_pow10(int64_t mantissa, int decimal_exponent) noexcept;
1642
1643 /**
1644 * This function is a template overload of `integer_times_pow10()`
1645 * that returns a floating-point value of type `T` that is one of
1646 * supported floating-point types (e.g. `double`, `float`).
1647 */
1648 template <typename T>
1649 FASTFLOAT_CONSTEXPR20
1650 typename std::enable_if<is_supported_float_type<T>::value, T>::type
1651 integer_times_pow10(uint64_t mantissa, int decimal_exponent) noexcept;
1652 template <typename T>
1653 FASTFLOAT_CONSTEXPR20
1654 typename std::enable_if<is_supported_float_type<T>::value, T>::type
1655 integer_times_pow10(int64_t mantissa, int decimal_exponent) noexcept;
1656
1657 /**
1658 * from_chars for integer types.
1659 */
1660 template <typename T, typename UC = char,
1661 typename = FASTFLOAT_ENABLE_IF(is_supported_integer_type<T>::value)>
1662 FASTFLOAT_CONSTEXPR20 from_chars_result_t<UC>
1663 from_chars(UC const *first, UC const *last, T &value, int base = 10) noexcept;
1664
1665 } // namespace fast_float
1666
1667 #endif // FASTFLOAT_FAST_FLOAT_H
1668
1669 #ifndef FASTFLOAT_ASCII_NUMBER_H
1670 #define FASTFLOAT_ASCII_NUMBER_H
1671
1672 #include <cctype>
1673 #include <cstdint>
1674 #include <cstring>
1675 #include <iterator>
1676 #include <limits>
1677 #include <type_traits>
1678
1679
1680 #ifdef FASTFLOAT_SSE2
1681 #include <emmintrin.h>
1682 #endif
1683
1684 #ifdef FASTFLOAT_NEON
1685 #include <arm_neon.h>
1686 #endif
1687
1688 namespace fast_float {
1689
1690 template <typename UC> fastfloat_really_inline constexpr bool has_simd_opt() {
1691 #ifdef FASTFLOAT_HAS_SIMD
1692 return std::is_same<UC, char16_t>::value;
1693 #else
1694 return false;
1695 #endif
1696 }
1697
1698 // Next function can be micro-optimized, but compilers are entirely
1699 // able to optimize it well.
1700 template <typename UC>
1701 fastfloat_really_inline constexpr bool is_integer(UC c) noexcept {
1702 return (unsigned)(c - UC('0')) <= 9u;
1703 }
1704
1705 fastfloat_really_inline constexpr uint64_t byteswap(uint64_t val) {
1706 return (val & 0xFF00000000000000) >> 56 | (val & 0x00FF000000000000) >> 40 |
1707 (val & 0x0000FF0000000000) >> 24 | (val & 0x000000FF00000000) >> 8 |
1708 (val & 0x00000000FF000000) << 8 | (val & 0x0000000000FF0000) << 24 |
1709 (val & 0x000000000000FF00) << 40 | (val & 0x00000000000000FF) << 56;
1710 }
1711
1712 fastfloat_really_inline constexpr uint32_t byteswap_32(uint32_t val) {
1713 return (val >> 24) | ((val >> 8) & 0x0000FF00u) | ((val << 8) & 0x00FF0000u) |
1714 (val << 24);
1715 }
1716
1717 // Read 8 UC into a u64. Truncates UC if not char.
1718 template <typename UC>
1719 fastfloat_really_inline FASTFLOAT_CONSTEXPR20 uint64_t
1720 read8_to_u64(UC const *chars) {
1721 if (cpp20_and_in_constexpr() || !std::is_same<UC, char>::value) {
1722 uint64_t val = 0;
1723 for (int i = 0; i < 8; ++i) {
1724 val |= uint64_t(uint8_t(*chars)) << (i * 8);
1725 ++chars;
1726 }
1727 return val;
1728 }
1729 uint64_t val;
1730 ::memcpy(&val, chars, sizeof(uint64_t));
1731 #if FASTFLOAT_IS_BIG_ENDIAN == 1
1732 // Need to read as-if the number was in little-endian order.
1733 val = byteswap(val);
1734 #endif
1735 return val;
1736 }
1737
1738 // Read 4 UC into a u32. Truncates UC if not char.
1739 template <typename UC>
1740 fastfloat_really_inline FASTFLOAT_CONSTEXPR20 uint32_t
1741 read4_to_u32(UC const *chars) {
1742 if (cpp20_and_in_constexpr() || !std::is_same<UC, char>::value) {
1743 uint32_t val = 0;
1744 for (int i = 0; i < 4; ++i) {
1745 val |= uint32_t(uint8_t(*chars)) << (i * 8);
1746 ++chars;
1747 }
1748 return val;
1749 }
1750 uint32_t val;
1751 ::memcpy(&val, chars, sizeof(uint32_t));
1752 #if FASTFLOAT_IS_BIG_ENDIAN == 1
1753 val = byteswap_32(val);
1754 #endif
1755 return val;
1756 }
1757 #ifdef FASTFLOAT_SSE2
1758
1759 fastfloat_really_inline uint64_t simd_read8_to_u64(__m128i const data) {
1760 FASTFLOAT_SIMD_DISABLE_WARNINGS
1761 __m128i const packed = _mm_packus_epi16(data, data);
1762 #ifdef FASTFLOAT_64BIT
1763 return uint64_t(_mm_cvtsi128_si64(packed));
1764 #else
1765 uint64_t value;
1766 // Visual Studio + older versions of GCC don't support _mm_storeu_si64
1767 _mm_storel_epi64(reinterpret_cast<__m128i *>(&value), packed);
1768 return value;
1769 #endif
1770 FASTFLOAT_SIMD_RESTORE_WARNINGS
1771 }
1772
1773 fastfloat_really_inline uint64_t simd_read8_to_u64(char16_t const *chars) {
1774 FASTFLOAT_SIMD_DISABLE_WARNINGS
1775 return simd_read8_to_u64(
1776 _mm_loadu_si128(reinterpret_cast<__m128i const *>(chars)));
1777 FASTFLOAT_SIMD_RESTORE_WARNINGS
1778 }
1779
1780 #elif defined(FASTFLOAT_NEON)
1781
1782 fastfloat_really_inline uint64_t simd_read8_to_u64(uint16x8_t const data) {
1783 FASTFLOAT_SIMD_DISABLE_WARNINGS
1784 uint8x8_t utf8_packed = vmovn_u16(data);
1785 return vget_lane_u64(vreinterpret_u64_u8(utf8_packed), 0);
1786 FASTFLOAT_SIMD_RESTORE_WARNINGS
1787 }
1788
1789 fastfloat_really_inline uint64_t simd_read8_to_u64(char16_t const *chars) {
1790 FASTFLOAT_SIMD_DISABLE_WARNINGS
1791 return simd_read8_to_u64(
1792 vld1q_u16(reinterpret_cast<uint16_t const *>(chars)));
1793 FASTFLOAT_SIMD_RESTORE_WARNINGS
1794 }
1795
1796 #endif // FASTFLOAT_SSE2
1797
1798 // MSVC SFINAE is broken pre-VS2017
1799 #if defined(_MSC_VER) && _MSC_VER <= 1900
1800 template <typename UC>
1801 #else
1802 template <typename UC, FASTFLOAT_ENABLE_IF(!has_simd_opt<UC>()) = 0>
1803 #endif
1804 // dummy for compile
1805 uint64_t simd_read8_to_u64(UC const *) {
1806 return 0;
1807 }
1808
1809 // credit @aqrit
1810 fastfloat_really_inline FASTFLOAT_CONSTEXPR14 uint32_t
1811 parse_eight_digits_unrolled(uint64_t val) {
1812 uint64_t const mask = 0x000000FF000000FF;
1813 uint64_t const mul1 = 0x000F424000000064; // 100 + (1000000ULL << 32)
1814 uint64_t const mul2 = 0x0000271000000001; // 1 + (10000ULL << 32)
1815 val -= 0x3030303030303030;
1816 val = (val * 10) + (val >> 8); // val = (val * 2561) >> 8;
1817 val = (((val & mask) * mul1) + (((val >> 16) & mask) * mul2)) >> 32;
1818 return uint32_t(val);
1819 }
1820
1821 // Call this if chars are definitely 8 digits.
1822 template <typename UC>
1823 fastfloat_really_inline FASTFLOAT_CONSTEXPR20 uint32_t
1824 parse_eight_digits_unrolled(UC const *chars) noexcept {
1825 if (cpp20_and_in_constexpr() || !has_simd_opt<UC>()) {
1826 return parse_eight_digits_unrolled(read8_to_u64(chars)); // truncation okay
1827 }
1828 return parse_eight_digits_unrolled(simd_read8_to_u64(chars));
1829 }
1830
1831 // credit @aqrit
1832 fastfloat_really_inline constexpr bool
1833 is_made_of_eight_digits_fast(uint64_t val) noexcept {
1834 return !((((val + 0x4646464646464646) | (val - 0x3030303030303030)) &
1835 0x8080808080808080));
1836 }
1837
1838 fastfloat_really_inline constexpr bool
1839 is_made_of_four_digits_fast(uint32_t val) noexcept {
1840 return !((((val + 0x46464646) | (val - 0x30303030)) & 0x80808080));
1841 }
1842
1843 fastfloat_really_inline FASTFLOAT_CONSTEXPR14 uint32_t
1844 parse_four_digits_unrolled(uint32_t val) noexcept {
1845 val -= 0x30303030;
1846 val = (val * 10) + (val >> 8);
1847 return (((val & 0x00FF00FF) * 0x00640001) >> 16) & 0xFFFF;
1848 }
1849
1850 #ifdef FASTFLOAT_HAS_SIMD
1851
1852 // Call this if chars might not be 8 digits.
1853 // Using this style (instead of is_made_of_eight_digits_fast() then
1854 // parse_eight_digits_unrolled()) ensures we don't load SIMD registers twice.
1855 fastfloat_really_inline FASTFLOAT_CONSTEXPR20 bool
1856 simd_parse_if_eight_digits_unrolled(char16_t const *chars,
1857 uint64_t &i) noexcept {
1858 if (cpp20_and_in_constexpr()) {
1859 return false;
1860 }
1861 #ifdef FASTFLOAT_SSE2
1862 FASTFLOAT_SIMD_DISABLE_WARNINGS
1863 __m128i const data =
1864 _mm_loadu_si128(reinterpret_cast<__m128i const *>(chars));
1865
1866 // (x - '0') <= 9
1867 // http://0x80.pl/articles/simd-parsing-int-sequences.html
1868 __m128i const t0 = _mm_add_epi16(data, _mm_set1_epi16(32720));
1869 __m128i const t1 = _mm_cmpgt_epi16(t0, _mm_set1_epi16(-32759));
1870
1871 if (_mm_movemask_epi8(t1) == 0) {
1872 i = i * 100000000 + parse_eight_digits_unrolled(simd_read8_to_u64(data));
1873 return true;
1874 } else
1875 return false;
1876 FASTFLOAT_SIMD_RESTORE_WARNINGS
1877 #elif defined(FASTFLOAT_NEON)
1878 FASTFLOAT_SIMD_DISABLE_WARNINGS
1879 uint16x8_t const data = vld1q_u16(reinterpret_cast<uint16_t const *>(chars));
1880
1881 // (x - '0') <= 9
1882 // http://0x80.pl/articles/simd-parsing-int-sequences.html
1883 uint16x8_t const t0 = vsubq_u16(data, vmovq_n_u16('0'));
1884 uint16x8_t const mask = vcltq_u16(t0, vmovq_n_u16('9' - '0' + 1));
1885
1886 if (vminvq_u16(mask) == 0xFFFF) {
1887 i = i * 100000000 + parse_eight_digits_unrolled(simd_read8_to_u64(data));
1888 return true;
1889 } else
1890 return false;
1891 FASTFLOAT_SIMD_RESTORE_WARNINGS
1892 #else
1893 (void)chars;
1894 (void)i;
1895 return false;
1896 #endif // FASTFLOAT_SSE2
1897 }
1898
1899 #endif // FASTFLOAT_HAS_SIMD
1900
1901 // MSVC SFINAE is broken pre-VS2017
1902 #if defined(_MSC_VER) && _MSC_VER <= 1900
1903 template <typename UC>
1904 #else
1905 template <typename UC, FASTFLOAT_ENABLE_IF(!has_simd_opt<UC>()) = 0>
1906 #endif
1907 // dummy for compile
1908 bool simd_parse_if_eight_digits_unrolled(UC const *, uint64_t &) {
1909 return 0;
1910 }
1911
1912 template <typename UC, FASTFLOAT_ENABLE_IF(!std::is_same<UC, char>::value) = 0>
1913 fastfloat_really_inline FASTFLOAT_CONSTEXPR20 void
1914 loop_parse_if_eight_digits(UC const *&p, UC const *const pend, uint64_t &i) {
1915 if (!has_simd_opt<UC>()) {
1916 return;
1917 }
1918 while ((std::distance(p, pend) >= 8) &&
1919 simd_parse_if_eight_digits_unrolled(
1920 p, i)) { // in rare cases, this will overflow, but that's ok
1921 p += 8;
1922 }
1923 }
1924
1925 fastfloat_really_inline FASTFLOAT_CONSTEXPR20 void
1926 loop_parse_if_eight_digits(char const *&p, char const *const pend,
1927 uint64_t &i) {
1928 // optimizes better than parse_if_eight_digits_unrolled() for UC = char.
1929 while ((std::distance(p, pend) >= 8) &&
1930 is_made_of_eight_digits_fast(read8_to_u64(p))) {
1931 i = i * 100000000 +
1932 parse_eight_digits_unrolled(read8_to_u64(
1933 p)); // in rare cases, this will overflow, but that's ok
1934 p += 8;
1935 }
1936 }
1937
1938 enum class parse_error {
1939 no_error,
1940 // [JSON-only] The minus sign must be followed by an integer.
1941 missing_integer_after_sign,
1942 // A sign must be followed by an integer or dot.
1943 missing_integer_or_dot_after_sign,
1944 // [JSON-only] The integer part must not have leading zeros.
1945 leading_zeros_in_integer_part,
1946 // [JSON-only] The integer part must have at least one digit.
1947 no_digits_in_integer_part,
1948 // [JSON-only] If there is a decimal point, there must be digits in the
1949 // fractional part.
1950 no_digits_in_fractional_part,
1951 // The mantissa must have at least one digit.
1952 no_digits_in_mantissa,
1953 // Scientific notation requires an exponential part.
1954 missing_exponential_part,
1955 };
1956
1957 template <typename UC> struct parsed_number_string_t {
1958 int64_t exponent{0};
1959 uint64_t mantissa{0};
1960 UC const *lastmatch{nullptr};
1961 bool negative{false};
1962 bool valid{false};
1963 bool too_many_digits{false};
1964 // contains the range of the significant digits
1965 span<UC const> integer{}; // non-nullable
1966 span<UC const> fraction{}; // nullable
1967 parse_error error{parse_error::no_error};
1968 };
1969
1970 using byte_span = span<char const>;
1971 using parsed_number_string = parsed_number_string_t<char>;
1972
1973 template <typename UC>
1974 fastfloat_really_inline FASTFLOAT_CONSTEXPR20 parsed_number_string_t<UC>
1975 report_parse_error(UC const *p, parse_error error) {
1976 parsed_number_string_t<UC> answer;
1977 answer.valid = false;
1978 answer.lastmatch = p;
1979 answer.error = error;
1980 return answer;
1981 }
1982
1983 // Assuming that you use no more than 19 digits, this will
1984 // parse an ASCII string.
1985 template <bool basic_json_fmt, typename UC>
1986 fastfloat_really_inline FASTFLOAT_CONSTEXPR20 parsed_number_string_t<UC>
1987 parse_number_string(UC const *p, UC const *pend,
1988 parse_options_t<UC> options) noexcept {
1989 chars_format const fmt = detail::adjust_for_feature_macros(options.format);
1990 UC const decimal_point = options.decimal_point;
1991
1992 parsed_number_string_t<UC> answer;
1993 answer.valid = false;
1994 answer.too_many_digits = false;
1995 // assume p < pend, so dereference without checks;
1996 answer.negative = (*p == UC('-'));
1997 // C++17 20.19.3.(7.1) explicitly forbids '+' sign here
1998 if ((*p == UC('-')) || (uint64_t(fmt & chars_format::allow_leading_plus) &&
1999 !basic_json_fmt && *p == UC('+'))) {
2000 ++p;
2001 if (p == pend) {
2002 return report_parse_error<UC>(
2003 p, parse_error::missing_integer_or_dot_after_sign);
2004 }
2005 FASTFLOAT_IF_CONSTEXPR17(basic_json_fmt) {
2006 if (!is_integer(*p)) { // a sign must be followed by an integer
2007 return report_parse_error<UC>(p,
2008 parse_error::missing_integer_after_sign);
2009 }
2010 }
2011 else {
2012 if (!is_integer(*p) &&
2013 (*p !=
2014 decimal_point)) { // a sign must be followed by an integer or the dot
2015 return report_parse_error<UC>(
2016 p, parse_error::missing_integer_or_dot_after_sign);
2017 }
2018 }
2019 }
2020 UC const *const start_digits = p;
2021
2022 uint64_t i = 0; // an unsigned int avoids signed overflows (which are bad)
2023
2024 while ((p != pend) && is_integer(*p)) {
2025 // a multiplication by 10 is cheaper than an arbitrary integer
2026 // multiplication
2027 i = 10 * i +
2028 uint64_t(*p -
2029 UC('0')); // might overflow, we will handle the overflow later
2030 ++p;
2031 }
2032 UC const *const end_of_integer_part = p;
2033 int64_t digit_count = int64_t(end_of_integer_part - start_digits);
2034 answer.integer = span<UC const>(start_digits, size_t(digit_count));
2035 FASTFLOAT_IF_CONSTEXPR17(basic_json_fmt) {
2036 // at least 1 digit in integer part, without leading zeros
2037 if (digit_count == 0) {
2038 return report_parse_error<UC>(p, parse_error::no_digits_in_integer_part);
2039 }
2040 if ((start_digits[0] == UC('0') && digit_count > 1)) {
2041 return report_parse_error<UC>(start_digits,
2042 parse_error::leading_zeros_in_integer_part);
2043 }
2044 }
2045
2046 int64_t exponent = 0;
2047 bool const has_decimal_point = (p != pend) && (*p == decimal_point);
2048 if (has_decimal_point) {
2049 ++p;
2050 UC const *before = p;
2051 // can occur at most twice without overflowing, but let it occur more, since
2052 // for integers with many digits, digit parsing is the primary bottleneck.
2053 loop_parse_if_eight_digits(p, pend, i);
2054
2055 while ((p != pend) && is_integer(*p)) {
2056 uint8_t digit = uint8_t(*p - UC('0'));
2057 ++p;
2058 i = i * 10 + digit; // in rare cases, this will overflow, but that's ok
2059 }
2060 exponent = before - p;
2061 answer.fraction = span<UC const>(before, size_t(p - before));
2062 digit_count -= exponent;
2063 }
2064 FASTFLOAT_IF_CONSTEXPR17(basic_json_fmt) {
2065 // at least 1 digit in fractional part
2066 if (has_decimal_point && exponent == 0) {
2067 return report_parse_error<UC>(p,
2068 parse_error::no_digits_in_fractional_part);
2069 }
2070 }
2071 else if (digit_count == 0) { // we must have encountered at least one integer!
2072 return report_parse_error<UC>(p, parse_error::no_digits_in_mantissa);
2073 }
2074 int64_t exp_number = 0; // explicit exponential part
2075 if ((uint64_t(fmt & chars_format::scientific) && (p != pend) &&
2076 ((UC('e') == *p) || (UC('E') == *p))) ||
2077 (uint64_t(fmt & detail::basic_fortran_fmt) && (p != pend) &&
2078 ((UC('+') == *p) || (UC('-') == *p) || (UC('d') == *p) ||
2079 (UC('D') == *p)))) {
2080 UC const *location_of_e = p;
2081 if ((UC('e') == *p) || (UC('E') == *p) || (UC('d') == *p) ||
2082 (UC('D') == *p)) {
2083 ++p;
2084 }
2085 bool neg_exp = false;
2086 if ((p != pend) && (UC('-') == *p)) {
2087 neg_exp = true;
2088 ++p;
2089 } else if ((p != pend) &&
2090 (UC('+') ==
2091 *p)) { // '+' on exponent is allowed by C++17 20.19.3.(7.1)
2092 ++p;
2093 }
2094 if ((p == pend) || !is_integer(*p)) {
2095 if (!uint64_t(fmt & chars_format::fixed)) {
2096 // The exponential part is invalid for scientific notation, so it must
2097 // be a trailing token for fixed notation. However, fixed notation is
2098 // disabled, so report a scientific notation error.
2099 return report_parse_error<UC>(p, parse_error::missing_exponential_part);
2100 }
2101 // Otherwise, we will be ignoring the 'e'.
2102 p = location_of_e;
2103 } else {
2104 while ((p != pend) && is_integer(*p)) {
2105 uint8_t digit = uint8_t(*p - UC('0'));
2106 if (exp_number < 0x10000000) {
2107 exp_number = 10 * exp_number + digit;
2108 }
2109 ++p;
2110 }
2111 if (neg_exp) {
2112 exp_number = -exp_number;
2113 }
2114 exponent += exp_number;
2115 }
2116 } else {
2117 // If it scientific and not fixed, we have to bail out.
2118 if (uint64_t(fmt & chars_format::scientific) &&
2119 !uint64_t(fmt & chars_format::fixed)) {
2120 return report_parse_error<UC>(p, parse_error::missing_exponential_part);
2121 }
2122 }
2123 answer.lastmatch = p;
2124 answer.valid = true;
2125
2126 // If we frequently had to deal with long strings of digits,
2127 // we could extend our code by using a 128-bit integer instead
2128 // of a 64-bit integer. However, this is uncommon.
2129 //
2130 // We can deal with up to 19 digits.
2131 if (digit_count > 19) { // this is uncommon
2132 // It is possible that the integer had an overflow.
2133 // We have to handle the case where we have 0.0000somenumber.
2134 // We need to be mindful of the case where we only have zeroes...
2135 // E.g., 0.000000000...000.
2136 UC const *start = start_digits;
2137 while ((start != pend) && (*start == UC('0') || *start == decimal_point)) {
2138 if (*start == UC('0')) {
2139 digit_count--;
2140 }
2141 start++;
2142 }
2143
2144 if (digit_count > 19) {
2145 answer.too_many_digits = true;
2146 // Let us start again, this time, avoiding overflows.
2147 // We don't need to call if is_integer, since we use the
2148 // pre-tokenized spans from above.
2149 i = 0;
2150 p = answer.integer.ptr;
2151 UC const *int_end = p + answer.integer.len();
2152 uint64_t const minimal_nineteen_digit_integer{1000000000000000000};
2153 while ((i < minimal_nineteen_digit_integer) && (p != int_end)) {
2154 i = i * 10 + uint64_t(*p - UC('0'));
2155 ++p;
2156 }
2157 if (i >= minimal_nineteen_digit_integer) { // We have a big integer
2158 exponent = end_of_integer_part - p + exp_number;
2159 } else { // We have a value with a fractional component.
2160 p = answer.fraction.ptr;
2161 UC const *frac_end = p + answer.fraction.len();
2162 while ((i < minimal_nineteen_digit_integer) && (p != frac_end)) {
2163 i = i * 10 + uint64_t(*p - UC('0'));
2164 ++p;
2165 }
2166 exponent = answer.fraction.ptr - p + exp_number;
2167 }
2168 // We have now corrected both exponent and i, to a truncated value
2169 }
2170 }
2171 answer.exponent = exponent;
2172 answer.mantissa = i;
2173 return answer;
2174 }
2175
2176 template <typename T, typename UC>
2177 fastfloat_really_inline FASTFLOAT_CONSTEXPR20 from_chars_result_t<UC>
2178 parse_int_string(UC const *p, UC const *pend, T &value,
2179 parse_options_t<UC> options) {
2180 chars_format const fmt = detail::adjust_for_feature_macros(options.format);
2181 int const base = options.base;
2182
2183 from_chars_result_t<UC> answer;
2184
2185 UC const *const first = p;
2186
2187 bool const negative = (*p == UC('-'));
2188 #ifdef FASTFLOAT_VISUAL_STUDIO
2189 #pragma warning(push)
2190 #pragma warning(disable : 4127)
2191 #endif
2192 if (!std::is_signed<T>::value && negative) {
2193 #ifdef FASTFLOAT_VISUAL_STUDIO
2194 #pragma warning(pop)
2195 #endif
2196 answer.ec = std::errc::invalid_argument;
2197 answer.ptr = first;
2198 return answer;
2199 }
2200 if ((*p == UC('-')) ||
2201 (uint64_t(fmt & chars_format::allow_leading_plus) && (*p == UC('+')))) {
2202 ++p;
2203 }
2204
2205 UC const *const start_num = p;
2206
2207 while (p != pend && *p == UC('0')) {
2208 ++p;
2209 }
2210
2211 bool const has_leading_zeros = p > start_num;
2212
2213 UC const *const start_digits = p;
2214
2215 FASTFLOAT_IF_CONSTEXPR17((std::is_same<T, std::uint8_t>::value)) {
2216 if (base == 10) {
2217 const size_t len = (size_t)(pend - p);
2218 if (len == 0) {
2219 if (has_leading_zeros) {
2220 value = 0;
2221 answer.ec = std::errc();
2222 answer.ptr = p;
2223 } else {
2224 answer.ec = std::errc::invalid_argument;
2225 answer.ptr = first;
2226 }
2227 return answer;
2228 }
2229
2230 uint32_t digits;
2231
2232 #if FASTFLOAT_HAS_IS_CONSTANT_EVALUATED && FASTFLOAT_HAS_BIT_CAST
2233 if (std::is_constant_evaluated()) {
2234 uint8_t str[4]{};
2235 for (size_t j = 0; j < 4 && j < len; ++j) {
2236 str[j] = static_cast<uint8_t>(p[j]);
2237 }
2238 digits = std::bit_cast<uint32_t>(str);
2239 #if FASTFLOAT_IS_BIG_ENDIAN
2240 digits = byteswap_32(digits);
2241 #endif
2242 }
2243 #else
2244 if (false) {
2245 }
2246 #endif
2247 else if (len >= 4) {
2248 ::memcpy(&digits, p, 4);
2249 #if FASTFLOAT_IS_BIG_ENDIAN
2250 digits = byteswap_32(digits);
2251 #endif
2252 } else {
2253 uint32_t b0 = static_cast<uint8_t>(p[0]);
2254 uint32_t b1 = (len > 1) ? static_cast<uint8_t>(p[1]) : 0xFFu;
2255 uint32_t b2 = (len > 2) ? static_cast<uint8_t>(p[2]) : 0xFFu;
2256 uint32_t b3 = 0xFFu;
2257 digits = b0 | (b1 << 8) | (b2 << 16) | (b3 << 24);
2258 }
2259
2260 uint32_t magic =
2261 ((digits + 0x46464646u) | (digits - 0x30303030u)) & 0x80808080u;
2262 uint32_t tz = (uint32_t)countr_zero_32(magic); // 7, 15, 23, 31, or 32
2263 uint32_t nd = (tz == 32) ? 4 : (tz >> 3);
2264 nd = (uint32_t)std::min((size_t)nd, len);
2265 if (nd == 0) {
2266 if (has_leading_zeros) {
2267 value = 0;
2268 answer.ec = std::errc();
2269 answer.ptr = p;
2270 return answer;
2271 }
2272 answer.ec = std::errc::invalid_argument;
2273 answer.ptr = first;
2274 return answer;
2275 }
2276 if (nd > 3) {
2277 const UC *q = p + nd;
2278 size_t rem = len - nd;
2279 while (rem) {
2280 if (*q < UC('0') || *q > UC('9'))
2281 break;
2282 ++q;
2283 --rem;
2284 }
2285 answer.ec = std::errc::result_out_of_range;
2286 answer.ptr = q;
2287 return answer;
2288 }
2289
2290 digits ^= 0x30303030u;
2291 digits <<= ((4 - nd) * 8);
2292
2293 uint32_t check = ((digits >> 24) & 0xff) | ((digits >> 8) & 0xff00) |
2294 ((digits << 8) & 0xff0000);
2295 if (check > 0x00020505) {
2296 answer.ec = std::errc::result_out_of_range;
2297 answer.ptr = p + nd;
2298 return answer;
2299 }
2300 value = (uint8_t)((0x640a01 * digits) >> 24);
2301 answer.ec = std::errc();
2302 answer.ptr = p + nd;
2303 return answer;
2304 }
2305 }
2306
2307 FASTFLOAT_IF_CONSTEXPR17((std::is_same<T, std::uint16_t>::value)) {
2308 if (base == 10) {
2309 const size_t len = size_t(pend - p);
2310 if (len == 0) {
2311 if (has_leading_zeros) {
2312 value = 0;
2313 answer.ec = std::errc();
2314 answer.ptr = p;
2315 } else {
2316 answer.ec = std::errc::invalid_argument;
2317 answer.ptr = first;
2318 }
2319 return answer;
2320 }
2321
2322 if (len >= 4) {
2323 uint32_t digits = read4_to_u32(p);
2324 if (is_made_of_four_digits_fast(digits)) {
2325 uint32_t v = parse_four_digits_unrolled(digits);
2326 if (len >= 5 && is_integer(p[4])) {
2327 v = v * 10 + uint32_t(p[4] - '0');
2328 if (len >= 6 && is_integer(p[5])) {
2329 answer.ec = std::errc::result_out_of_range;
2330 const UC *q = p + 5;
2331 while (q != pend && is_integer(*q)) {
2332 q++;
2333 }
2334 answer.ptr = q;
2335 return answer;
2336 }
2337 if (v > 65535) {
2338 answer.ec = std::errc::result_out_of_range;
2339 answer.ptr = p + 5;
2340 return answer;
2341 }
2342 value = uint16_t(v);
2343 answer.ec = std::errc();
2344 answer.ptr = p + 5;
2345 return answer;
2346 }
2347 // 4 digits
2348 value = uint16_t(v);
2349 answer.ec = std::errc();
2350 answer.ptr = p + 4;
2351 return answer;
2352 }
2353 }
2354 }
2355 }
2356
2357 uint64_t i = 0;
2358 if (base == 10) {
2359 loop_parse_if_eight_digits(p, pend, i); // use SIMD if possible
2360 }
2361 while (p != pend) {
2362 uint8_t digit = ch_to_digit(*p);
2363 if (digit >= base) {
2364 break;
2365 }
2366 i = uint64_t(base) * i + digit; // might overflow, check this later
2367 p++;
2368 }
2369
2370 size_t digit_count = size_t(p - start_digits);
2371
2372 if (digit_count == 0) {
2373 if (has_leading_zeros) {
2374 value = 0;
2375 answer.ec = std::errc();
2376 answer.ptr = p;
2377 } else {
2378 answer.ec = std::errc::invalid_argument;
2379 answer.ptr = first;
2380 }
2381 return answer;
2382 }
2383
2384 answer.ptr = p;
2385
2386 // check u64 overflow
2387 size_t max_digits = max_digits_u64(base);
2388 if (digit_count > max_digits) {
2389 answer.ec = std::errc::result_out_of_range;
2390 return answer;
2391 }
2392 // this check can be eliminated for all other types, but they will all require
2393 // a max_digits(base) equivalent
2394 if (digit_count == max_digits && i < min_safe_u64(base)) {
2395 answer.ec = std::errc::result_out_of_range;
2396 return answer;
2397 }
2398
2399 // check other types overflow
2400 if (!std::is_same<T, uint64_t>::value) {
2401 if (i > uint64_t(std::numeric_limits<T>::max()) + uint64_t(negative)) {
2402 answer.ec = std::errc::result_out_of_range;
2403 return answer;
2404 }
2405 }
2406
2407 if (negative) {
2408 #ifdef FASTFLOAT_VISUAL_STUDIO
2409 #pragma warning(push)
2410 #pragma warning(disable : 4146)
2411 #endif
2412 // this weird workaround is required because:
2413 // - converting unsigned to signed when its value is greater than signed max
2414 // is UB pre-C++23.
2415 // - reinterpret_casting (~i + 1) would work, but it is not constexpr
2416 // this is always optimized into a neg instruction (note: T is an integer
2417 // type)
2418 value = T(-std::numeric_limits<T>::max() -
2419 T(i - uint64_t(std::numeric_limits<T>::max())));
2420 #ifdef FASTFLOAT_VISUAL_STUDIO
2421 #pragma warning(pop)
2422 #endif
2423 } else {
2424 value = T(i);
2425 }
2426
2427 answer.ec = std::errc();
2428 return answer;
2429 }
2430
2431 } // namespace fast_float
2432
2433 #endif
2434
2435 #ifndef FASTFLOAT_FAST_TABLE_H
2436 #define FASTFLOAT_FAST_TABLE_H
2437
2438 #include <cstdint>
2439
2440 namespace fast_float {
2441
2442 /**
2443 * When mapping numbers from decimal to binary,
2444 * we go from w * 10^q to m * 2^p but we have
2445 * 10^q = 5^q * 2^q, so effectively
2446 * we are trying to match
2447 * w * 2^q * 5^q to m * 2^p. Thus the powers of two
2448 * are not a concern since they can be represented
2449 * exactly using the binary notation, only the powers of five
2450 * affect the binary significand.
2451 */
2452
2453 /**
2454 * The smallest non-zero float (binary64) is 2^-1074.
2455 * We take as input numbers of the form w x 10^q where w < 2^64.
2456 * We have that w * 10^-343 < 2^(64-344) 5^-343 < 2^-1076.
2457 * However, we have that
2458 * (2^64-1) * 10^-342 = (2^64-1) * 2^-342 * 5^-342 > 2^-1074.
2459 * Thus it is possible for a number of the form w * 10^-342 where
2460 * w is a 64-bit value to be a non-zero floating-point number.
2461 *********
2462 * Any number of form w * 10^309 where w>= 1 is going to be
2463 * infinite in binary64 so we never need to worry about powers
2464 * of 5 greater than 308.
2465 */
2466 template <class unused = void> struct powers_template {
2467
2468 constexpr static int smallest_power_of_five =
2469 binary_format<double>::smallest_power_of_ten();
2470 constexpr static int largest_power_of_five =
2471 binary_format<double>::largest_power_of_ten();
2472 constexpr static int number_of_entries =
2473 2 * (largest_power_of_five - smallest_power_of_five + 1);
2474 // Powers of five from 5^-342 all the way to 5^308 rounded toward one.
2475 constexpr static uint64_t power_of_five_128[number_of_entries] = {
2476 0xeef453d6923bd65a, 0x113faa2906a13b3f,
2477 0x9558b4661b6565f8, 0x4ac7ca59a424c507,
2478 0xbaaee17fa23ebf76, 0x5d79bcf00d2df649,
2479 0xe95a99df8ace6f53, 0xf4d82c2c107973dc,
2480 0x91d8a02bb6c10594, 0x79071b9b8a4be869,
2481 0xb64ec836a47146f9, 0x9748e2826cdee284,
2482 0xe3e27a444d8d98b7, 0xfd1b1b2308169b25,
2483 0x8e6d8c6ab0787f72, 0xfe30f0f5e50e20f7,
2484 0xb208ef855c969f4f, 0xbdbd2d335e51a935,
2485 0xde8b2b66b3bc4723, 0xad2c788035e61382,
2486 0x8b16fb203055ac76, 0x4c3bcb5021afcc31,
2487 0xaddcb9e83c6b1793, 0xdf4abe242a1bbf3d,
2488 0xd953e8624b85dd78, 0xd71d6dad34a2af0d,
2489 0x87d4713d6f33aa6b, 0x8672648c40e5ad68,
2490 0xa9c98d8ccb009506, 0x680efdaf511f18c2,
2491 0xd43bf0effdc0ba48, 0x212bd1b2566def2,
2492 0x84a57695fe98746d, 0x14bb630f7604b57,
2493 0xa5ced43b7e3e9188, 0x419ea3bd35385e2d,
2494 0xcf42894a5dce35ea, 0x52064cac828675b9,
2495 0x818995ce7aa0e1b2, 0x7343efebd1940993,
2496 0xa1ebfb4219491a1f, 0x1014ebe6c5f90bf8,
2497 0xca66fa129f9b60a6, 0xd41a26e077774ef6,
2498 0xfd00b897478238d0, 0x8920b098955522b4,
2499 0x9e20735e8cb16382, 0x55b46e5f5d5535b0,
2500 0xc5a890362fddbc62, 0xeb2189f734aa831d,
2501 0xf712b443bbd52b7b, 0xa5e9ec7501d523e4,
2502 0x9a6bb0aa55653b2d, 0x47b233c92125366e,
2503 0xc1069cd4eabe89f8, 0x999ec0bb696e840a,
2504 0xf148440a256e2c76, 0xc00670ea43ca250d,
2505 0x96cd2a865764dbca, 0x380406926a5e5728,
2506 0xbc807527ed3e12bc, 0xc605083704f5ecf2,
2507 0xeba09271e88d976b, 0xf7864a44c633682e,
2508 0x93445b8731587ea3, 0x7ab3ee6afbe0211d,
2509 0xb8157268fdae9e4c, 0x5960ea05bad82964,
2510 0xe61acf033d1a45df, 0x6fb92487298e33bd,
2511 0x8fd0c16206306bab, 0xa5d3b6d479f8e056,
2512 0xb3c4f1ba87bc8696, 0x8f48a4899877186c,
2513 0xe0b62e2929aba83c, 0x331acdabfe94de87,
2514 0x8c71dcd9ba0b4925, 0x9ff0c08b7f1d0b14,
2515 0xaf8e5410288e1b6f, 0x7ecf0ae5ee44dd9,
2516 0xdb71e91432b1a24a, 0xc9e82cd9f69d6150,
2517 0x892731ac9faf056e, 0xbe311c083a225cd2,
2518 0xab70fe17c79ac6ca, 0x6dbd630a48aaf406,
2519 0xd64d3d9db981787d, 0x92cbbccdad5b108,
2520 0x85f0468293f0eb4e, 0x25bbf56008c58ea5,
2521 0xa76c582338ed2621, 0xaf2af2b80af6f24e,
2522 0xd1476e2c07286faa, 0x1af5af660db4aee1,
2523 0x82cca4db847945ca, 0x50d98d9fc890ed4d,
2524 0xa37fce126597973c, 0xe50ff107bab528a0,
2525 0xcc5fc196fefd7d0c, 0x1e53ed49a96272c8,
2526 0xff77b1fcbebcdc4f, 0x25e8e89c13bb0f7a,
2527 0x9faacf3df73609b1, 0x77b191618c54e9ac,
2528 0xc795830d75038c1d, 0xd59df5b9ef6a2417,
2529 0xf97ae3d0d2446f25, 0x4b0573286b44ad1d,
2530 0x9becce62836ac577, 0x4ee367f9430aec32,
2531 0xc2e801fb244576d5, 0x229c41f793cda73f,
2532 0xf3a20279ed56d48a, 0x6b43527578c1110f,
2533 0x9845418c345644d6, 0x830a13896b78aaa9,
2534 0xbe5691ef416bd60c, 0x23cc986bc656d553,
2535 0xedec366b11c6cb8f, 0x2cbfbe86b7ec8aa8,
2536 0x94b3a202eb1c3f39, 0x7bf7d71432f3d6a9,
2537 0xb9e08a83a5e34f07, 0xdaf5ccd93fb0cc53,
2538 0xe858ad248f5c22c9, 0xd1b3400f8f9cff68,
2539 0x91376c36d99995be, 0x23100809b9c21fa1,
2540 0xb58547448ffffb2d, 0xabd40a0c2832a78a,
2541 0xe2e69915b3fff9f9, 0x16c90c8f323f516c,
2542 0x8dd01fad907ffc3b, 0xae3da7d97f6792e3,
2543 0xb1442798f49ffb4a, 0x99cd11cfdf41779c,
2544 0xdd95317f31c7fa1d, 0x40405643d711d583,
2545 0x8a7d3eef7f1cfc52, 0x482835ea666b2572,
2546 0xad1c8eab5ee43b66, 0xda3243650005eecf,
2547 0xd863b256369d4a40, 0x90bed43e40076a82,
2548 0x873e4f75e2224e68, 0x5a7744a6e804a291,
2549 0xa90de3535aaae202, 0x711515d0a205cb36,
2550 0xd3515c2831559a83, 0xd5a5b44ca873e03,
2551 0x8412d9991ed58091, 0xe858790afe9486c2,
2552 0xa5178fff668ae0b6, 0x626e974dbe39a872,
2553 0xce5d73ff402d98e3, 0xfb0a3d212dc8128f,
2554 0x80fa687f881c7f8e, 0x7ce66634bc9d0b99,
2555 0xa139029f6a239f72, 0x1c1fffc1ebc44e80,
2556 0xc987434744ac874e, 0xa327ffb266b56220,
2557 0xfbe9141915d7a922, 0x4bf1ff9f0062baa8,
2558 0x9d71ac8fada6c9b5, 0x6f773fc3603db4a9,
2559 0xc4ce17b399107c22, 0xcb550fb4384d21d3,
2560 0xf6019da07f549b2b, 0x7e2a53a146606a48,
2561 0x99c102844f94e0fb, 0x2eda7444cbfc426d,
2562 0xc0314325637a1939, 0xfa911155fefb5308,
2563 0xf03d93eebc589f88, 0x793555ab7eba27ca,
2564 0x96267c7535b763b5, 0x4bc1558b2f3458de,
2565 0xbbb01b9283253ca2, 0x9eb1aaedfb016f16,
2566 0xea9c227723ee8bcb, 0x465e15a979c1cadc,
2567 0x92a1958a7675175f, 0xbfacd89ec191ec9,
2568 0xb749faed14125d36, 0xcef980ec671f667b,
2569 0xe51c79a85916f484, 0x82b7e12780e7401a,
2570 0x8f31cc0937ae58d2, 0xd1b2ecb8b0908810,
2571 0xb2fe3f0b8599ef07, 0x861fa7e6dcb4aa15,
2572 0xdfbdcece67006ac9, 0x67a791e093e1d49a,
2573 0x8bd6a141006042bd, 0xe0c8bb2c5c6d24e0,
2574 0xaecc49914078536d, 0x58fae9f773886e18,
2575 0xda7f5bf590966848, 0xaf39a475506a899e,
2576 0x888f99797a5e012d, 0x6d8406c952429603,
2577 0xaab37fd7d8f58178, 0xc8e5087ba6d33b83,
2578 0xd5605fcdcf32e1d6, 0xfb1e4a9a90880a64,
2579 0x855c3be0a17fcd26, 0x5cf2eea09a55067f,
2580 0xa6b34ad8c9dfc06f, 0xf42faa48c0ea481e,
2581 0xd0601d8efc57b08b, 0xf13b94daf124da26,
2582 0x823c12795db6ce57, 0x76c53d08d6b70858,
2583 0xa2cb1717b52481ed, 0x54768c4b0c64ca6e,
2584 0xcb7ddcdda26da268, 0xa9942f5dcf7dfd09,
2585 0xfe5d54150b090b02, 0xd3f93b35435d7c4c,
2586 0x9efa548d26e5a6e1, 0xc47bc5014a1a6daf,
2587 0xc6b8e9b0709f109a, 0x359ab6419ca1091b,
2588 0xf867241c8cc6d4c0, 0xc30163d203c94b62,
2589 0x9b407691d7fc44f8, 0x79e0de63425dcf1d,
2590 0xc21094364dfb5636, 0x985915fc12f542e4,
2591 0xf294b943e17a2bc4, 0x3e6f5b7b17b2939d,
2592 0x979cf3ca6cec5b5a, 0xa705992ceecf9c42,
2593 0xbd8430bd08277231, 0x50c6ff782a838353,
2594 0xece53cec4a314ebd, 0xa4f8bf5635246428,
2595 0x940f4613ae5ed136, 0x871b7795e136be99,
2596 0xb913179899f68584, 0x28e2557b59846e3f,
2597 0xe757dd7ec07426e5, 0x331aeada2fe589cf,
2598 0x9096ea6f3848984f, 0x3ff0d2c85def7621,
2599 0xb4bca50b065abe63, 0xfed077a756b53a9,
2600 0xe1ebce4dc7f16dfb, 0xd3e8495912c62894,
2601 0x8d3360f09cf6e4bd, 0x64712dd7abbbd95c,
2602 0xb080392cc4349dec, 0xbd8d794d96aacfb3,
2603 0xdca04777f541c567, 0xecf0d7a0fc5583a0,
2604 0x89e42caaf9491b60, 0xf41686c49db57244,
2605 0xac5d37d5b79b6239, 0x311c2875c522ced5,
2606 0xd77485cb25823ac7, 0x7d633293366b828b,
2607 0x86a8d39ef77164bc, 0xae5dff9c02033197,
2608 0xa8530886b54dbdeb, 0xd9f57f830283fdfc,
2609 0xd267caa862a12d66, 0xd072df63c324fd7b,
2610 0x8380dea93da4bc60, 0x4247cb9e59f71e6d,
2611 0xa46116538d0deb78, 0x52d9be85f074e608,
2612 0xcd795be870516656, 0x67902e276c921f8b,
2613 0x806bd9714632dff6, 0xba1cd8a3db53b6,
2614 0xa086cfcd97bf97f3, 0x80e8a40eccd228a4,
2615 0xc8a883c0fdaf7df0, 0x6122cd128006b2cd,
2616 0xfad2a4b13d1b5d6c, 0x796b805720085f81,
2617 0x9cc3a6eec6311a63, 0xcbe3303674053bb0,
2618 0xc3f490aa77bd60fc, 0xbedbfc4411068a9c,
2619 0xf4f1b4d515acb93b, 0xee92fb5515482d44,
2620 0x991711052d8bf3c5, 0x751bdd152d4d1c4a,
2621 0xbf5cd54678eef0b6, 0xd262d45a78a0635d,
2622 0xef340a98172aace4, 0x86fb897116c87c34,
2623 0x9580869f0e7aac0e, 0xd45d35e6ae3d4da0,
2624 0xbae0a846d2195712, 0x8974836059cca109,
2625 0xe998d258869facd7, 0x2bd1a438703fc94b,
2626 0x91ff83775423cc06, 0x7b6306a34627ddcf,
2627 0xb67f6455292cbf08, 0x1a3bc84c17b1d542,
2628 0xe41f3d6a7377eeca, 0x20caba5f1d9e4a93,
2629 0x8e938662882af53e, 0x547eb47b7282ee9c,
2630 0xb23867fb2a35b28d, 0xe99e619a4f23aa43,
2631 0xdec681f9f4c31f31, 0x6405fa00e2ec94d4,
2632 0x8b3c113c38f9f37e, 0xde83bc408dd3dd04,
2633 0xae0b158b4738705e, 0x9624ab50b148d445,
2634 0xd98ddaee19068c76, 0x3badd624dd9b0957,
2635 0x87f8a8d4cfa417c9, 0xe54ca5d70a80e5d6,
2636 0xa9f6d30a038d1dbc, 0x5e9fcf4ccd211f4c,
2637 0xd47487cc8470652b, 0x7647c3200069671f,
2638 0x84c8d4dfd2c63f3b, 0x29ecd9f40041e073,
2639 0xa5fb0a17c777cf09, 0xf468107100525890,
2640 0xcf79cc9db955c2cc, 0x7182148d4066eeb4,
2641 0x81ac1fe293d599bf, 0xc6f14cd848405530,
2642 0xa21727db38cb002f, 0xb8ada00e5a506a7c,
2643 0xca9cf1d206fdc03b, 0xa6d90811f0e4851c,
2644 0xfd442e4688bd304a, 0x908f4a166d1da663,
2645 0x9e4a9cec15763e2e, 0x9a598e4e043287fe,
2646 0xc5dd44271ad3cdba, 0x40eff1e1853f29fd,
2647 0xf7549530e188c128, 0xd12bee59e68ef47c,
2648 0x9a94dd3e8cf578b9, 0x82bb74f8301958ce,
2649 0xc13a148e3032d6e7, 0xe36a52363c1faf01,
2650 0xf18899b1bc3f8ca1, 0xdc44e6c3cb279ac1,
2651 0x96f5600f15a7b7e5, 0x29ab103a5ef8c0b9,
2652 0xbcb2b812db11a5de, 0x7415d448f6b6f0e7,
2653 0xebdf661791d60f56, 0x111b495b3464ad21,
2654 0x936b9fcebb25c995, 0xcab10dd900beec34,
2655 0xb84687c269ef3bfb, 0x3d5d514f40eea742,
2656 0xe65829b3046b0afa, 0xcb4a5a3112a5112,
2657 0x8ff71a0fe2c2e6dc, 0x47f0e785eaba72ab,
2658 0xb3f4e093db73a093, 0x59ed216765690f56,
2659 0xe0f218b8d25088b8, 0x306869c13ec3532c,
2660 0x8c974f7383725573, 0x1e414218c73a13fb,
2661 0xafbd2350644eeacf, 0xe5d1929ef90898fa,
2662 0xdbac6c247d62a583, 0xdf45f746b74abf39,
2663 0x894bc396ce5da772, 0x6b8bba8c328eb783,
2664 0xab9eb47c81f5114f, 0x66ea92f3f326564,
2665 0xd686619ba27255a2, 0xc80a537b0efefebd,
2666 0x8613fd0145877585, 0xbd06742ce95f5f36,
2667 0xa798fc4196e952e7, 0x2c48113823b73704,
2668 0xd17f3b51fca3a7a0, 0xf75a15862ca504c5,
2669 0x82ef85133de648c4, 0x9a984d73dbe722fb,
2670 0xa3ab66580d5fdaf5, 0xc13e60d0d2e0ebba,
2671 0xcc963fee10b7d1b3, 0x318df905079926a8,
2672 0xffbbcfe994e5c61f, 0xfdf17746497f7052,
2673 0x9fd561f1fd0f9bd3, 0xfeb6ea8bedefa633,
2674 0xc7caba6e7c5382c8, 0xfe64a52ee96b8fc0,
2675 0xf9bd690a1b68637b, 0x3dfdce7aa3c673b0,
2676 0x9c1661a651213e2d, 0x6bea10ca65c084e,
2677 0xc31bfa0fe5698db8, 0x486e494fcff30a62,
2678 0xf3e2f893dec3f126, 0x5a89dba3c3efccfa,
2679 0x986ddb5c6b3a76b7, 0xf89629465a75e01c,
2680 0xbe89523386091465, 0xf6bbb397f1135823,
2681 0xee2ba6c0678b597f, 0x746aa07ded582e2c,
2682 0x94db483840b717ef, 0xa8c2a44eb4571cdc,
2683 0xba121a4650e4ddeb, 0x92f34d62616ce413,
2684 0xe896a0d7e51e1566, 0x77b020baf9c81d17,
2685 0x915e2486ef32cd60, 0xace1474dc1d122e,
2686 0xb5b5ada8aaff80b8, 0xd819992132456ba,
2687 0xe3231912d5bf60e6, 0x10e1fff697ed6c69,
2688 0x8df5efabc5979c8f, 0xca8d3ffa1ef463c1,
2689 0xb1736b96b6fd83b3, 0xbd308ff8a6b17cb2,
2690 0xddd0467c64bce4a0, 0xac7cb3f6d05ddbde,
2691 0x8aa22c0dbef60ee4, 0x6bcdf07a423aa96b,
2692 0xad4ab7112eb3929d, 0x86c16c98d2c953c6,
2693 0xd89d64d57a607744, 0xe871c7bf077ba8b7,
2694 0x87625f056c7c4a8b, 0x11471cd764ad4972,
2695 0xa93af6c6c79b5d2d, 0xd598e40d3dd89bcf,
2696 0xd389b47879823479, 0x4aff1d108d4ec2c3,
2697 0x843610cb4bf160cb, 0xcedf722a585139ba,
2698 0xa54394fe1eedb8fe, 0xc2974eb4ee658828,
2699 0xce947a3da6a9273e, 0x733d226229feea32,
2700 0x811ccc668829b887, 0x806357d5a3f525f,
2701 0xa163ff802a3426a8, 0xca07c2dcb0cf26f7,
2702 0xc9bcff6034c13052, 0xfc89b393dd02f0b5,
2703 0xfc2c3f3841f17c67, 0xbbac2078d443ace2,
2704 0x9d9ba7832936edc0, 0xd54b944b84aa4c0d,
2705 0xc5029163f384a931, 0xa9e795e65d4df11,
2706 0xf64335bcf065d37d, 0x4d4617b5ff4a16d5,
2707 0x99ea0196163fa42e, 0x504bced1bf8e4e45,
2708 0xc06481fb9bcf8d39, 0xe45ec2862f71e1d6,
2709 0xf07da27a82c37088, 0x5d767327bb4e5a4c,
2710 0x964e858c91ba2655, 0x3a6a07f8d510f86f,
2711 0xbbe226efb628afea, 0x890489f70a55368b,
2712 0xeadab0aba3b2dbe5, 0x2b45ac74ccea842e,
2713 0x92c8ae6b464fc96f, 0x3b0b8bc90012929d,
2714 0xb77ada0617e3bbcb, 0x9ce6ebb40173744,
2715 0xe55990879ddcaabd, 0xcc420a6a101d0515,
2716 0x8f57fa54c2a9eab6, 0x9fa946824a12232d,
2717 0xb32df8e9f3546564, 0x47939822dc96abf9,
2718 0xdff9772470297ebd, 0x59787e2b93bc56f7,
2719 0x8bfbea76c619ef36, 0x57eb4edb3c55b65a,
2720 0xaefae51477a06b03, 0xede622920b6b23f1,
2721 0xdab99e59958885c4, 0xe95fab368e45eced,
2722 0x88b402f7fd75539b, 0x11dbcb0218ebb414,
2723 0xaae103b5fcd2a881, 0xd652bdc29f26a119,
2724 0xd59944a37c0752a2, 0x4be76d3346f0495f,
2725 0x857fcae62d8493a5, 0x6f70a4400c562ddb,
2726 0xa6dfbd9fb8e5b88e, 0xcb4ccd500f6bb952,
2727 0xd097ad07a71f26b2, 0x7e2000a41346a7a7,
2728 0x825ecc24c873782f, 0x8ed400668c0c28c8,
2729 0xa2f67f2dfa90563b, 0x728900802f0f32fa,
2730 0xcbb41ef979346bca, 0x4f2b40a03ad2ffb9,
2731 0xfea126b7d78186bc, 0xe2f610c84987bfa8,
2732 0x9f24b832e6b0f436, 0xdd9ca7d2df4d7c9,
2733 0xc6ede63fa05d3143, 0x91503d1c79720dbb,
2734 0xf8a95fcf88747d94, 0x75a44c6397ce912a,
2735 0x9b69dbe1b548ce7c, 0xc986afbe3ee11aba,
2736 0xc24452da229b021b, 0xfbe85badce996168,
2737 0xf2d56790ab41c2a2, 0xfae27299423fb9c3,
2738 0x97c560ba6b0919a5, 0xdccd879fc967d41a,
2739 0xbdb6b8e905cb600f, 0x5400e987bbc1c920,
2740 0xed246723473e3813, 0x290123e9aab23b68,
2741 0x9436c0760c86e30b, 0xf9a0b6720aaf6521,
2742 0xb94470938fa89bce, 0xf808e40e8d5b3e69,
2743 0xe7958cb87392c2c2, 0xb60b1d1230b20e04,
2744 0x90bd77f3483bb9b9, 0xb1c6f22b5e6f48c2,
2745 0xb4ecd5f01a4aa828, 0x1e38aeb6360b1af3,
2746 0xe2280b6c20dd5232, 0x25c6da63c38de1b0,
2747 0x8d590723948a535f, 0x579c487e5a38ad0e,
2748 0xb0af48ec79ace837, 0x2d835a9df0c6d851,
2749 0xdcdb1b2798182244, 0xf8e431456cf88e65,
2750 0x8a08f0f8bf0f156b, 0x1b8e9ecb641b58ff,
2751 0xac8b2d36eed2dac5, 0xe272467e3d222f3f,
2752 0xd7adf884aa879177, 0x5b0ed81dcc6abb0f,
2753 0x86ccbb52ea94baea, 0x98e947129fc2b4e9,
2754 0xa87fea27a539e9a5, 0x3f2398d747b36224,
2755 0xd29fe4b18e88640e, 0x8eec7f0d19a03aad,
2756 0x83a3eeeef9153e89, 0x1953cf68300424ac,
2757 0xa48ceaaab75a8e2b, 0x5fa8c3423c052dd7,
2758 0xcdb02555653131b6, 0x3792f412cb06794d,
2759 0x808e17555f3ebf11, 0xe2bbd88bbee40bd0,
2760 0xa0b19d2ab70e6ed6, 0x5b6aceaeae9d0ec4,
2761 0xc8de047564d20a8b, 0xf245825a5a445275,
2762 0xfb158592be068d2e, 0xeed6e2f0f0d56712,
2763 0x9ced737bb6c4183d, 0x55464dd69685606b,
2764 0xc428d05aa4751e4c, 0xaa97e14c3c26b886,
2765 0xf53304714d9265df, 0xd53dd99f4b3066a8,
2766 0x993fe2c6d07b7fab, 0xe546a8038efe4029,
2767 0xbf8fdb78849a5f96, 0xde98520472bdd033,
2768 0xef73d256a5c0f77c, 0x963e66858f6d4440,
2769 0x95a8637627989aad, 0xdde7001379a44aa8,
2770 0xbb127c53b17ec159, 0x5560c018580d5d52,
2771 0xe9d71b689dde71af, 0xaab8f01e6e10b4a6,
2772 0x9226712162ab070d, 0xcab3961304ca70e8,
2773 0xb6b00d69bb55c8d1, 0x3d607b97c5fd0d22,
2774 0xe45c10c42a2b3b05, 0x8cb89a7db77c506a,
2775 0x8eb98a7a9a5b04e3, 0x77f3608e92adb242,
2776 0xb267ed1940f1c61c, 0x55f038b237591ed3,
2777 0xdf01e85f912e37a3, 0x6b6c46dec52f6688,
2778 0x8b61313bbabce2c6, 0x2323ac4b3b3da015,
2779 0xae397d8aa96c1b77, 0xabec975e0a0d081a,
2780 0xd9c7dced53c72255, 0x96e7bd358c904a21,
2781 0x881cea14545c7575, 0x7e50d64177da2e54,
2782 0xaa242499697392d2, 0xdde50bd1d5d0b9e9,
2783 0xd4ad2dbfc3d07787, 0x955e4ec64b44e864,
2784 0x84ec3c97da624ab4, 0xbd5af13bef0b113e,
2785 0xa6274bbdd0fadd61, 0xecb1ad8aeacdd58e,
2786 0xcfb11ead453994ba, 0x67de18eda5814af2,
2787 0x81ceb32c4b43fcf4, 0x80eacf948770ced7,
2788 0xa2425ff75e14fc31, 0xa1258379a94d028d,
2789 0xcad2f7f5359a3b3e, 0x96ee45813a04330,
2790 0xfd87b5f28300ca0d, 0x8bca9d6e188853fc,
2791 0x9e74d1b791e07e48, 0x775ea264cf55347e,
2792 0xc612062576589dda, 0x95364afe032a819e,
2793 0xf79687aed3eec551, 0x3a83ddbd83f52205,
2794 0x9abe14cd44753b52, 0xc4926a9672793543,
2795 0xc16d9a0095928a27, 0x75b7053c0f178294,
2796 0xf1c90080baf72cb1, 0x5324c68b12dd6339,
2797 0x971da05074da7bee, 0xd3f6fc16ebca5e04,
2798 0xbce5086492111aea, 0x88f4bb1ca6bcf585,
2799 0xec1e4a7db69561a5, 0x2b31e9e3d06c32e6,
2800 0x9392ee8e921d5d07, 0x3aff322e62439fd0,
2801 0xb877aa3236a4b449, 0x9befeb9fad487c3,
2802 0xe69594bec44de15b, 0x4c2ebe687989a9b4,
2803 0x901d7cf73ab0acd9, 0xf9d37014bf60a11,
2804 0xb424dc35095cd80f, 0x538484c19ef38c95,
2805 0xe12e13424bb40e13, 0x2865a5f206b06fba,
2806 0x8cbccc096f5088cb, 0xf93f87b7442e45d4,
2807 0xafebff0bcb24aafe, 0xf78f69a51539d749,
2808 0xdbe6fecebdedd5be, 0xb573440e5a884d1c,
2809 0x89705f4136b4a597, 0x31680a88f8953031,
2810 0xabcc77118461cefc, 0xfdc20d2b36ba7c3e,
2811 0xd6bf94d5e57a42bc, 0x3d32907604691b4d,
2812 0x8637bd05af6c69b5, 0xa63f9a49c2c1b110,
2813 0xa7c5ac471b478423, 0xfcf80dc33721d54,
2814 0xd1b71758e219652b, 0xd3c36113404ea4a9,
2815 0x83126e978d4fdf3b, 0x645a1cac083126ea,
2816 0xa3d70a3d70a3d70a, 0x3d70a3d70a3d70a4,
2817 0xcccccccccccccccc, 0xcccccccccccccccd,
2818 0x8000000000000000, 0x0,
2819 0xa000000000000000, 0x0,
2820 0xc800000000000000, 0x0,
2821 0xfa00000000000000, 0x0,
2822 0x9c40000000000000, 0x0,
2823 0xc350000000000000, 0x0,
2824 0xf424000000000000, 0x0,
2825 0x9896800000000000, 0x0,
2826 0xbebc200000000000, 0x0,
2827 0xee6b280000000000, 0x0,
2828 0x9502f90000000000, 0x0,
2829 0xba43b74000000000, 0x0,
2830 0xe8d4a51000000000, 0x0,
2831 0x9184e72a00000000, 0x0,
2832 0xb5e620f480000000, 0x0,
2833 0xe35fa931a0000000, 0x0,
2834 0x8e1bc9bf04000000, 0x0,
2835 0xb1a2bc2ec5000000, 0x0,
2836 0xde0b6b3a76400000, 0x0,
2837 0x8ac7230489e80000, 0x0,
2838 0xad78ebc5ac620000, 0x0,
2839 0xd8d726b7177a8000, 0x0,
2840 0x878678326eac9000, 0x0,
2841 0xa968163f0a57b400, 0x0,
2842 0xd3c21bcecceda100, 0x0,
2843 0x84595161401484a0, 0x0,
2844 0xa56fa5b99019a5c8, 0x0,
2845 0xcecb8f27f4200f3a, 0x0,
2846 0x813f3978f8940984, 0x4000000000000000,
2847 0xa18f07d736b90be5, 0x5000000000000000,
2848 0xc9f2c9cd04674ede, 0xa400000000000000,
2849 0xfc6f7c4045812296, 0x4d00000000000000,
2850 0x9dc5ada82b70b59d, 0xf020000000000000,
2851 0xc5371912364ce305, 0x6c28000000000000,
2852 0xf684df56c3e01bc6, 0xc732000000000000,
2853 0x9a130b963a6c115c, 0x3c7f400000000000,
2854 0xc097ce7bc90715b3, 0x4b9f100000000000,
2855 0xf0bdc21abb48db20, 0x1e86d40000000000,
2856 0x96769950b50d88f4, 0x1314448000000000,
2857 0xbc143fa4e250eb31, 0x17d955a000000000,
2858 0xeb194f8e1ae525fd, 0x5dcfab0800000000,
2859 0x92efd1b8d0cf37be, 0x5aa1cae500000000,
2860 0xb7abc627050305ad, 0xf14a3d9e40000000,
2861 0xe596b7b0c643c719, 0x6d9ccd05d0000000,
2862 0x8f7e32ce7bea5c6f, 0xe4820023a2000000,
2863 0xb35dbf821ae4f38b, 0xdda2802c8a800000,
2864 0xe0352f62a19e306e, 0xd50b2037ad200000,
2865 0x8c213d9da502de45, 0x4526f422cc340000,
2866 0xaf298d050e4395d6, 0x9670b12b7f410000,
2867 0xdaf3f04651d47b4c, 0x3c0cdd765f114000,
2868 0x88d8762bf324cd0f, 0xa5880a69fb6ac800,
2869 0xab0e93b6efee0053, 0x8eea0d047a457a00,
2870 0xd5d238a4abe98068, 0x72a4904598d6d880,
2871 0x85a36366eb71f041, 0x47a6da2b7f864750,
2872 0xa70c3c40a64e6c51, 0x999090b65f67d924,
2873 0xd0cf4b50cfe20765, 0xfff4b4e3f741cf6d,
2874 0x82818f1281ed449f, 0xbff8f10e7a8921a4,
2875 0xa321f2d7226895c7, 0xaff72d52192b6a0d,
2876 0xcbea6f8ceb02bb39, 0x9bf4f8a69f764490,
2877 0xfee50b7025c36a08, 0x2f236d04753d5b4,
2878 0x9f4f2726179a2245, 0x1d762422c946590,
2879 0xc722f0ef9d80aad6, 0x424d3ad2b7b97ef5,
2880 0xf8ebad2b84e0d58b, 0xd2e0898765a7deb2,
2881 0x9b934c3b330c8577, 0x63cc55f49f88eb2f,
2882 0xc2781f49ffcfa6d5, 0x3cbf6b71c76b25fb,
2883 0xf316271c7fc3908a, 0x8bef464e3945ef7a,
2884 0x97edd871cfda3a56, 0x97758bf0e3cbb5ac,
2885 0xbde94e8e43d0c8ec, 0x3d52eeed1cbea317,
2886 0xed63a231d4c4fb27, 0x4ca7aaa863ee4bdd,
2887 0x945e455f24fb1cf8, 0x8fe8caa93e74ef6a,
2888 0xb975d6b6ee39e436, 0xb3e2fd538e122b44,
2889 0xe7d34c64a9c85d44, 0x60dbbca87196b616,
2890 0x90e40fbeea1d3a4a, 0xbc8955e946fe31cd,
2891 0xb51d13aea4a488dd, 0x6babab6398bdbe41,
2892 0xe264589a4dcdab14, 0xc696963c7eed2dd1,
2893 0x8d7eb76070a08aec, 0xfc1e1de5cf543ca2,
2894 0xb0de65388cc8ada8, 0x3b25a55f43294bcb,
2895 0xdd15fe86affad912, 0x49ef0eb713f39ebe,
2896 0x8a2dbf142dfcc7ab, 0x6e3569326c784337,
2897 0xacb92ed9397bf996, 0x49c2c37f07965404,
2898 0xd7e77a8f87daf7fb, 0xdc33745ec97be906,
2899 0x86f0ac99b4e8dafd, 0x69a028bb3ded71a3,
2900 0xa8acd7c0222311bc, 0xc40832ea0d68ce0c,
2901 0xd2d80db02aabd62b, 0xf50a3fa490c30190,
2902 0x83c7088e1aab65db, 0x792667c6da79e0fa,
2903 0xa4b8cab1a1563f52, 0x577001b891185938,
2904 0xcde6fd5e09abcf26, 0xed4c0226b55e6f86,
2905 0x80b05e5ac60b6178, 0x544f8158315b05b4,
2906 0xa0dc75f1778e39d6, 0x696361ae3db1c721,
2907 0xc913936dd571c84c, 0x3bc3a19cd1e38e9,
2908 0xfb5878494ace3a5f, 0x4ab48a04065c723,
2909 0x9d174b2dcec0e47b, 0x62eb0d64283f9c76,
2910 0xc45d1df942711d9a, 0x3ba5d0bd324f8394,
2911 0xf5746577930d6500, 0xca8f44ec7ee36479,
2912 0x9968bf6abbe85f20, 0x7e998b13cf4e1ecb,
2913 0xbfc2ef456ae276e8, 0x9e3fedd8c321a67e,
2914 0xefb3ab16c59b14a2, 0xc5cfe94ef3ea101e,
2915 0x95d04aee3b80ece5, 0xbba1f1d158724a12,
2916 0xbb445da9ca61281f, 0x2a8a6e45ae8edc97,
2917 0xea1575143cf97226, 0xf52d09d71a3293bd,
2918 0x924d692ca61be758, 0x593c2626705f9c56,
2919 0xb6e0c377cfa2e12e, 0x6f8b2fb00c77836c,
2920 0xe498f455c38b997a, 0xb6dfb9c0f956447,
2921 0x8edf98b59a373fec, 0x4724bd4189bd5eac,
2922 0xb2977ee300c50fe7, 0x58edec91ec2cb657,
2923 0xdf3d5e9bc0f653e1, 0x2f2967b66737e3ed,
2924 0x8b865b215899f46c, 0xbd79e0d20082ee74,
2925 0xae67f1e9aec07187, 0xecd8590680a3aa11,
2926 0xda01ee641a708de9, 0xe80e6f4820cc9495,
2927 0x884134fe908658b2, 0x3109058d147fdcdd,
2928 0xaa51823e34a7eede, 0xbd4b46f0599fd415,
2929 0xd4e5e2cdc1d1ea96, 0x6c9e18ac7007c91a,
2930 0x850fadc09923329e, 0x3e2cf6bc604ddb0,
2931 0xa6539930bf6bff45, 0x84db8346b786151c,
2932 0xcfe87f7cef46ff16, 0xe612641865679a63,
2933 0x81f14fae158c5f6e, 0x4fcb7e8f3f60c07e,
2934 0xa26da3999aef7749, 0xe3be5e330f38f09d,
2935 0xcb090c8001ab551c, 0x5cadf5bfd3072cc5,
2936 0xfdcb4fa002162a63, 0x73d9732fc7c8f7f6,
2937 0x9e9f11c4014dda7e, 0x2867e7fddcdd9afa,
2938 0xc646d63501a1511d, 0xb281e1fd541501b8,
2939 0xf7d88bc24209a565, 0x1f225a7ca91a4226,
2940 0x9ae757596946075f, 0x3375788de9b06958,
2941 0xc1a12d2fc3978937, 0x52d6b1641c83ae,
2942 0xf209787bb47d6b84, 0xc0678c5dbd23a49a,
2943 0x9745eb4d50ce6332, 0xf840b7ba963646e0,
2944 0xbd176620a501fbff, 0xb650e5a93bc3d898,
2945 0xec5d3fa8ce427aff, 0xa3e51f138ab4cebe,
2946 0x93ba47c980e98cdf, 0xc66f336c36b10137,
2947 0xb8a8d9bbe123f017, 0xb80b0047445d4184,
2948 0xe6d3102ad96cec1d, 0xa60dc059157491e5,
2949 0x9043ea1ac7e41392, 0x87c89837ad68db2f,
2950 0xb454e4a179dd1877, 0x29babe4598c311fb,
2951 0xe16a1dc9d8545e94, 0xf4296dd6fef3d67a,
2952 0x8ce2529e2734bb1d, 0x1899e4a65f58660c,
2953 0xb01ae745b101e9e4, 0x5ec05dcff72e7f8f,
2954 0xdc21a1171d42645d, 0x76707543f4fa1f73,
2955 0x899504ae72497eba, 0x6a06494a791c53a8,
2956 0xabfa45da0edbde69, 0x487db9d17636892,
2957 0xd6f8d7509292d603, 0x45a9d2845d3c42b6,
2958 0x865b86925b9bc5c2, 0xb8a2392ba45a9b2,
2959 0xa7f26836f282b732, 0x8e6cac7768d7141e,
2960 0xd1ef0244af2364ff, 0x3207d795430cd926,
2961 0x8335616aed761f1f, 0x7f44e6bd49e807b8,
2962 0xa402b9c5a8d3a6e7, 0x5f16206c9c6209a6,
2963 0xcd036837130890a1, 0x36dba887c37a8c0f,
2964 0x802221226be55a64, 0xc2494954da2c9789,
2965 0xa02aa96b06deb0fd, 0xf2db9baa10b7bd6c,
2966 0xc83553c5c8965d3d, 0x6f92829494e5acc7,
2967 0xfa42a8b73abbf48c, 0xcb772339ba1f17f9,
2968 0x9c69a97284b578d7, 0xff2a760414536efb,
2969 0xc38413cf25e2d70d, 0xfef5138519684aba,
2970 0xf46518c2ef5b8cd1, 0x7eb258665fc25d69,
2971 0x98bf2f79d5993802, 0xef2f773ffbd97a61,
2972 0xbeeefb584aff8603, 0xaafb550ffacfd8fa,
2973 0xeeaaba2e5dbf6784, 0x95ba2a53f983cf38,
2974 0x952ab45cfa97a0b2, 0xdd945a747bf26183,
2975 0xba756174393d88df, 0x94f971119aeef9e4,
2976 0xe912b9d1478ceb17, 0x7a37cd5601aab85d,
2977 0x91abb422ccb812ee, 0xac62e055c10ab33a,
2978 0xb616a12b7fe617aa, 0x577b986b314d6009,
2979 0xe39c49765fdf9d94, 0xed5a7e85fda0b80b,
2980 0x8e41ade9fbebc27d, 0x14588f13be847307,
2981 0xb1d219647ae6b31c, 0x596eb2d8ae258fc8,
2982 0xde469fbd99a05fe3, 0x6fca5f8ed9aef3bb,
2983 0x8aec23d680043bee, 0x25de7bb9480d5854,
2984 0xada72ccc20054ae9, 0xaf561aa79a10ae6a,
2985 0xd910f7ff28069da4, 0x1b2ba1518094da04,
2986 0x87aa9aff79042286, 0x90fb44d2f05d0842,
2987 0xa99541bf57452b28, 0x353a1607ac744a53,
2988 0xd3fa922f2d1675f2, 0x42889b8997915ce8,
2989 0x847c9b5d7c2e09b7, 0x69956135febada11,
2990 0xa59bc234db398c25, 0x43fab9837e699095,
2991 0xcf02b2c21207ef2e, 0x94f967e45e03f4bb,
2992 0x8161afb94b44f57d, 0x1d1be0eebac278f5,
2993 0xa1ba1ba79e1632dc, 0x6462d92a69731732,
2994 0xca28a291859bbf93, 0x7d7b8f7503cfdcfe,
2995 0xfcb2cb35e702af78, 0x5cda735244c3d43e,
2996 0x9defbf01b061adab, 0x3a0888136afa64a7,
2997 0xc56baec21c7a1916, 0x88aaa1845b8fdd0,
2998 0xf6c69a72a3989f5b, 0x8aad549e57273d45,
2999 0x9a3c2087a63f6399, 0x36ac54e2f678864b,
3000 0xc0cb28a98fcf3c7f, 0x84576a1bb416a7dd,
3001 0xf0fdf2d3f3c30b9f, 0x656d44a2a11c51d5,
3002 0x969eb7c47859e743, 0x9f644ae5a4b1b325,
3003 0xbc4665b596706114, 0x873d5d9f0dde1fee,
3004 0xeb57ff22fc0c7959, 0xa90cb506d155a7ea,
3005 0x9316ff75dd87cbd8, 0x9a7f12442d588f2,
3006 0xb7dcbf5354e9bece, 0xc11ed6d538aeb2f,
3007 0xe5d3ef282a242e81, 0x8f1668c8a86da5fa,
3008 0x8fa475791a569d10, 0xf96e017d694487bc,
3009 0xb38d92d760ec4455, 0x37c981dcc395a9ac,
3010 0xe070f78d3927556a, 0x85bbe253f47b1417,
3011 0x8c469ab843b89562, 0x93956d7478ccec8e,
3012 0xaf58416654a6babb, 0x387ac8d1970027b2,
3013 0xdb2e51bfe9d0696a, 0x6997b05fcc0319e,
3014 0x88fcf317f22241e2, 0x441fece3bdf81f03,
3015 0xab3c2fddeeaad25a, 0xd527e81cad7626c3,
3016 0xd60b3bd56a5586f1, 0x8a71e223d8d3b074,
3017 0x85c7056562757456, 0xf6872d5667844e49,
3018 0xa738c6bebb12d16c, 0xb428f8ac016561db,
3019 0xd106f86e69d785c7, 0xe13336d701beba52,
3020 0x82a45b450226b39c, 0xecc0024661173473,
3021 0xa34d721642b06084, 0x27f002d7f95d0190,
3022 0xcc20ce9bd35c78a5, 0x31ec038df7b441f4,
3023 0xff290242c83396ce, 0x7e67047175a15271,
3024 0x9f79a169bd203e41, 0xf0062c6e984d386,
3025 0xc75809c42c684dd1, 0x52c07b78a3e60868,
3026 0xf92e0c3537826145, 0xa7709a56ccdf8a82,
3027 0x9bbcc7a142b17ccb, 0x88a66076400bb691,
3028 0xc2abf989935ddbfe, 0x6acff893d00ea435,
3029 0xf356f7ebf83552fe, 0x583f6b8c4124d43,
3030 0x98165af37b2153de, 0xc3727a337a8b704a,
3031 0xbe1bf1b059e9a8d6, 0x744f18c0592e4c5c,
3032 0xeda2ee1c7064130c, 0x1162def06f79df73,
3033 0x9485d4d1c63e8be7, 0x8addcb5645ac2ba8,
3034 0xb9a74a0637ce2ee1, 0x6d953e2bd7173692,
3035 0xe8111c87c5c1ba99, 0xc8fa8db6ccdd0437,
3036 0x910ab1d4db9914a0, 0x1d9c9892400a22a2,
3037 0xb54d5e4a127f59c8, 0x2503beb6d00cab4b,
3038 0xe2a0b5dc971f303a, 0x2e44ae64840fd61d,
3039 0x8da471a9de737e24, 0x5ceaecfed289e5d2,
3040 0xb10d8e1456105dad, 0x7425a83e872c5f47,
3041 0xdd50f1996b947518, 0xd12f124e28f77719,
3042 0x8a5296ffe33cc92f, 0x82bd6b70d99aaa6f,
3043 0xace73cbfdc0bfb7b, 0x636cc64d1001550b,
3044 0xd8210befd30efa5a, 0x3c47f7e05401aa4e,
3045 0x8714a775e3e95c78, 0x65acfaec34810a71,
3046 0xa8d9d1535ce3b396, 0x7f1839a741a14d0d,
3047 0xd31045a8341ca07c, 0x1ede48111209a050,
3048 0x83ea2b892091e44d, 0x934aed0aab460432,
3049 0xa4e4b66b68b65d60, 0xf81da84d5617853f,
3050 0xce1de40642e3f4b9, 0x36251260ab9d668e,
3051 0x80d2ae83e9ce78f3, 0xc1d72b7c6b426019,
3052 0xa1075a24e4421730, 0xb24cf65b8612f81f,
3053 0xc94930ae1d529cfc, 0xdee033f26797b627,
3054 0xfb9b7cd9a4a7443c, 0x169840ef017da3b1,
3055 0x9d412e0806e88aa5, 0x8e1f289560ee864e,
3056 0xc491798a08a2ad4e, 0xf1a6f2bab92a27e2,
3057 0xf5b5d7ec8acb58a2, 0xae10af696774b1db,
3058 0x9991a6f3d6bf1765, 0xacca6da1e0a8ef29,
3059 0xbff610b0cc6edd3f, 0x17fd090a58d32af3,
3060 0xeff394dcff8a948e, 0xddfc4b4cef07f5b0,
3061 0x95f83d0a1fb69cd9, 0x4abdaf101564f98e,
3062 0xbb764c4ca7a4440f, 0x9d6d1ad41abe37f1,
3063 0xea53df5fd18d5513, 0x84c86189216dc5ed,
3064 0x92746b9be2f8552c, 0x32fd3cf5b4e49bb4,
3065 0xb7118682dbb66a77, 0x3fbc8c33221dc2a1,
3066 0xe4d5e82392a40515, 0xfabaf3feaa5334a,
3067 0x8f05b1163ba6832d, 0x29cb4d87f2a7400e,
3068 0xb2c71d5bca9023f8, 0x743e20e9ef511012,
3069 0xdf78e4b2bd342cf6, 0x914da9246b255416,
3070 0x8bab8eefb6409c1a, 0x1ad089b6c2f7548e,
3071 0xae9672aba3d0c320, 0xa184ac2473b529b1,
3072 0xda3c0f568cc4f3e8, 0xc9e5d72d90a2741e,
3073 0x8865899617fb1871, 0x7e2fa67c7a658892,
3074 0xaa7eebfb9df9de8d, 0xddbb901b98feeab7,
3075 0xd51ea6fa85785631, 0x552a74227f3ea565,
3076 0x8533285c936b35de, 0xd53a88958f87275f,
3077 0xa67ff273b8460356, 0x8a892abaf368f137,
3078 0xd01fef10a657842c, 0x2d2b7569b0432d85,
3079 0x8213f56a67f6b29b, 0x9c3b29620e29fc73,
3080 0xa298f2c501f45f42, 0x8349f3ba91b47b8f,
3081 0xcb3f2f7642717713, 0x241c70a936219a73,
3082 0xfe0efb53d30dd4d7, 0xed238cd383aa0110,
3083 0x9ec95d1463e8a506, 0xf4363804324a40aa,
3084 0xc67bb4597ce2ce48, 0xb143c6053edcd0d5,
3085 0xf81aa16fdc1b81da, 0xdd94b7868e94050a,
3086 0x9b10a4e5e9913128, 0xca7cf2b4191c8326,
3087 0xc1d4ce1f63f57d72, 0xfd1c2f611f63a3f0,
3088 0xf24a01a73cf2dccf, 0xbc633b39673c8cec,
3089 0x976e41088617ca01, 0xd5be0503e085d813,
3090 0xbd49d14aa79dbc82, 0x4b2d8644d8a74e18,
3091 0xec9c459d51852ba2, 0xddf8e7d60ed1219e,
3092 0x93e1ab8252f33b45, 0xcabb90e5c942b503,
3093 0xb8da1662e7b00a17, 0x3d6a751f3b936243,
3094 0xe7109bfba19c0c9d, 0xcc512670a783ad4,
3095 0x906a617d450187e2, 0x27fb2b80668b24c5,
3096 0xb484f9dc9641e9da, 0xb1f9f660802dedf6,
3097 0xe1a63853bbd26451, 0x5e7873f8a0396973,
3098 0x8d07e33455637eb2, 0xdb0b487b6423e1e8,
3099 0xb049dc016abc5e5f, 0x91ce1a9a3d2cda62,
3100 0xdc5c5301c56b75f7, 0x7641a140cc7810fb,
3101 0x89b9b3e11b6329ba, 0xa9e904c87fcb0a9d,
3102 0xac2820d9623bf429, 0x546345fa9fbdcd44,
3103 0xd732290fbacaf133, 0xa97c177947ad4095,
3104 0x867f59a9d4bed6c0, 0x49ed8eabcccc485d,
3105 0xa81f301449ee8c70, 0x5c68f256bfff5a74,
3106 0xd226fc195c6a2f8c, 0x73832eec6fff3111,
3107 0x83585d8fd9c25db7, 0xc831fd53c5ff7eab,
3108 0xa42e74f3d032f525, 0xba3e7ca8b77f5e55,
3109 0xcd3a1230c43fb26f, 0x28ce1bd2e55f35eb,
3110 0x80444b5e7aa7cf85, 0x7980d163cf5b81b3,
3111 0xa0555e361951c366, 0xd7e105bcc332621f,
3112 0xc86ab5c39fa63440, 0x8dd9472bf3fefaa7,
3113 0xfa856334878fc150, 0xb14f98f6f0feb951,
3114 0x9c935e00d4b9d8d2, 0x6ed1bf9a569f33d3,
3115 0xc3b8358109e84f07, 0xa862f80ec4700c8,
3116 0xf4a642e14c6262c8, 0xcd27bb612758c0fa,
3117 0x98e7e9cccfbd7dbd, 0x8038d51cb897789c,
3118 0xbf21e44003acdd2c, 0xe0470a63e6bd56c3,
3119 0xeeea5d5004981478, 0x1858ccfce06cac74,
3120 0x95527a5202df0ccb, 0xf37801e0c43ebc8,
3121 0xbaa718e68396cffd, 0xd30560258f54e6ba,
3122 0xe950df20247c83fd, 0x47c6b82ef32a2069,
3123 0x91d28b7416cdd27e, 0x4cdc331d57fa5441,
3124 0xb6472e511c81471d, 0xe0133fe4adf8e952,
3125 0xe3d8f9e563a198e5, 0x58180fddd97723a6,
3126 0x8e679c2f5e44ff8f, 0x570f09eaa7ea7648,
3127 };
3128 };
3129
3130 #if FASTFLOAT_DETAIL_MUST_DEFINE_CONSTEXPR_VARIABLE
3131
3132 template <class unused>
3133 constexpr uint64_t
3134 powers_template<unused>::power_of_five_128[number_of_entries];
3135
3136 #endif
3137
3138 using powers = powers_template<>;
3139
3140 } // namespace fast_float
3141
3142 #endif
3143
3144 #ifndef FASTFLOAT_DECIMAL_TO_BINARY_H
3145 #define FASTFLOAT_DECIMAL_TO_BINARY_H
3146
3147 #include <cfloat>
3148 #include <cinttypes>
3149 #include <cmath>
3150 #include <cstdint>
3151 #include <cstdlib>
3152 #include <cstring>
3153
3154 namespace fast_float {
3155
3156 // This will compute or rather approximate w * 5**q and return a pair of 64-bit
3157 // words approximating the result, with the "high" part corresponding to the
3158 // most significant bits and the low part corresponding to the least significant
3159 // bits.
3160 //
3161 template <int bit_precision>
3162 fastfloat_really_inline FASTFLOAT_CONSTEXPR20 value128
3163 compute_product_approximation(int64_t q, uint64_t w) {
3164 int const index = 2 * int(q - powers::smallest_power_of_five);
3165 // For small values of q, e.g., q in [0,27], the answer is always exact
3166 // because The line value128 firstproduct = full_multiplication(w,
3167 // power_of_five_128[index]); gives the exact answer.
3168 value128 firstproduct =
3169 full_multiplication(w, powers::power_of_five_128[index]);
3170 static_assert((bit_precision >= 0) && (bit_precision <= 64),
3171 " precision should be in (0,64]");
3172 constexpr uint64_t precision_mask =
3173 (bit_precision < 64) ? (uint64_t(0xFFFFFFFFFFFFFFFF) >> bit_precision)
3174 : uint64_t(0xFFFFFFFFFFFFFFFF);
3175 if ((firstproduct.high & precision_mask) ==
3176 precision_mask) { // could further guard with (lower + w < lower)
3177 // regarding the second product, we only need secondproduct.high, but our
3178 // expectation is that the compiler will optimize this extra work away if
3179 // needed.
3180 value128 secondproduct =
3181 full_multiplication(w, powers::power_of_five_128[index + 1]);
3182 firstproduct.low += secondproduct.high;
3183 if (secondproduct.high > firstproduct.low) {
3184 firstproduct.high++;
3185 }
3186 }
3187 return firstproduct;
3188 }
3189
3190 namespace detail {
3191 /**
3192 * For q in (0,350), we have that
3193 * f = (((152170 + 65536) * q ) >> 16);
3194 * is equal to
3195 * floor(p) + q
3196 * where
3197 * p = log(5**q)/log(2) = q * log(5)/log(2)
3198 *
3199 * For negative values of q in (-400,0), we have that
3200 * f = (((152170 + 65536) * q ) >> 16);
3201 * is equal to
3202 * -ceil(p) + q
3203 * where
3204 * p = log(5**-q)/log(2) = -q * log(5)/log(2)
3205 */
3206 constexpr fastfloat_really_inline int32_t power(int32_t q) noexcept {
3207 return (((152170 + 65536) * q) >> 16) + 63;
3208 }
3209 } // namespace detail
3210
3211 // create an adjusted mantissa, biased by the invalid power2
3212 // for significant digits already multiplied by 10 ** q.
3213 template <typename binary>
3214 fastfloat_really_inline FASTFLOAT_CONSTEXPR14 adjusted_mantissa
3215 compute_error_scaled(int64_t q, uint64_t w, int lz) noexcept {
3216 int hilz = int(w >> 63) ^ 1;
3217 adjusted_mantissa answer;
3218 answer.mantissa = w << hilz;
3219 int bias = binary::mantissa_explicit_bits() - binary::minimum_exponent();
3220 answer.power2 = int32_t(detail::power(int32_t(q)) + bias - hilz - lz - 62 +
3221 invalid_am_bias);
3222 return answer;
3223 }
3224
3225 // w * 10 ** q, without rounding the representation up.
3226 // the power2 in the exponent will be adjusted by invalid_am_bias.
3227 template <typename binary>
3228 fastfloat_really_inline FASTFLOAT_CONSTEXPR20 adjusted_mantissa
3229 compute_error(int64_t q, uint64_t w) noexcept {
3230 int lz = leading_zeroes(w);
3231 w <<= lz;
3232 value128 product =
3233 compute_product_approximation<binary::mantissa_explicit_bits() + 3>(q, w);
3234 return compute_error_scaled<binary>(q, product.high, lz);
3235 }
3236
3237 // Computers w * 10 ** q.
3238 // The returned value should be a valid number that simply needs to be
3239 // packed. However, in some very rare cases, the computation will fail. In such
3240 // cases, we return an adjusted_mantissa with a negative power of 2: the caller
3241 // should recompute in such cases.
3242 template <typename binary>
3243 fastfloat_really_inline FASTFLOAT_CONSTEXPR20 adjusted_mantissa
3244 compute_float(int64_t q, uint64_t w) noexcept {
3245 adjusted_mantissa answer;
3246 if ((w == 0) || (q < binary::smallest_power_of_ten())) {
3247 answer.power2 = 0;
3248 answer.mantissa = 0;
3249 // result should be zero
3250 return answer;
3251 }
3252 if (q > binary::largest_power_of_ten()) {
3253 // we want to get infinity:
3254 answer.power2 = binary::infinite_power();
3255 answer.mantissa = 0;
3256 return answer;
3257 }
3258 // At this point in time q is in [powers::smallest_power_of_five,
3259 // powers::largest_power_of_five].
3260
3261 // We want the most significant bit of i to be 1. Shift if needed.
3262 int lz = leading_zeroes(w);
3263 w <<= lz;
3264
3265 // The required precision is binary::mantissa_explicit_bits() + 3 because
3266 // 1. We need the implicit bit
3267 // 2. We need an extra bit for rounding purposes
3268 // 3. We might lose a bit due to the "upperbit" routine (result too small,
3269 // requiring a shift)
3270
3271 value128 product =
3272 compute_product_approximation<binary::mantissa_explicit_bits() + 3>(q, w);
3273 // The computed 'product' is always sufficient.
3274 // Mathematical proof:
3275 // Noble Mushtak and Daniel Lemire, Fast Number Parsing Without Fallback (to
3276 // appear) See script/mushtak_lemire.py
3277
3278 // The "compute_product_approximation" function can be slightly slower than a
3279 // branchless approach: value128 product = compute_product(q, w); but in
3280 // practice, we can win big with the compute_product_approximation if its
3281 // additional branch is easily predicted. Which is best is data specific.
3282 int upperbit = int(product.high >> 63);
3283 int shift = upperbit + 64 - binary::mantissa_explicit_bits() - 3;
3284
3285 answer.mantissa = product.high >> shift;
3286
3287 answer.power2 = int32_t(detail::power(int32_t(q)) + upperbit - lz -
3288 binary::minimum_exponent());
3289 if (answer.power2 <= 0) { // we have a subnormal?
3290 // Here have that answer.power2 <= 0 so -answer.power2 >= 0
3291 if (-answer.power2 + 1 >=
3292 64) { // if we have more than 64 bits below the minimum exponent, you
3293 // have a zero for sure.
3294 answer.power2 = 0;
3295 answer.mantissa = 0;
3296 // result should be zero
3297 return answer;
3298 }
3299 // next line is safe because -answer.power2 + 1 < 64
3300 answer.mantissa >>= -answer.power2 + 1;
3301 // Thankfully, we can't have both "round-to-even" and subnormals because
3302 // "round-to-even" only occurs for powers close to 0 in the 32-bit and
3303 // and 64-bit case (with no more than 19 digits).
3304 answer.mantissa += (answer.mantissa & 1); // round up
3305 answer.mantissa >>= 1;
3306 // There is a weird scenario where we don't have a subnormal but just.
3307 // Suppose we start with 2.2250738585072013e-308, we end up
3308 // with 0x3fffffffffffff x 2^-1023-53 which is technically subnormal
3309 // whereas 0x40000000000000 x 2^-1023-53 is normal. Now, we need to round
3310 // up 0x3fffffffffffff x 2^-1023-53 and once we do, we are no longer
3311 // subnormal, but we can only know this after rounding.
3312 // So we only declare a subnormal if we are smaller than the threshold.
3313 answer.power2 =
3314 (answer.mantissa < (uint64_t(1) << binary::mantissa_explicit_bits()))
3315 ? 0
3316 : 1;
3317 return answer;
3318 }
3319
3320 // usually, we round *up*, but if we fall right in between and and we have an
3321 // even basis, we need to round down
3322 // We are only concerned with the cases where 5**q fits in single 64-bit word.
3323 if ((product.low <= 1) && (q >= binary::min_exponent_round_to_even()) &&
3324 (q <= binary::max_exponent_round_to_even()) &&
3325 ((answer.mantissa & 3) == 1)) { // we may fall between two floats!
3326 // To be in-between two floats we need that in doing
3327 // answer.mantissa = product.high >> (upperbit + 64 -
3328 // binary::mantissa_explicit_bits() - 3);
3329 // ... we dropped out only zeroes. But if this happened, then we can go
3330 // back!!!
3331 if ((answer.mantissa << shift) == product.high) {
3332 answer.mantissa &= ~uint64_t(1); // flip it so that we do not round up
3333 }
3334 }
3335
3336 answer.mantissa += (answer.mantissa & 1); // round up
3337 answer.mantissa >>= 1;
3338 if (answer.mantissa >= (uint64_t(2) << binary::mantissa_explicit_bits())) {
3339 answer.mantissa = (uint64_t(1) << binary::mantissa_explicit_bits());
3340 answer.power2++; // undo previous addition
3341 }
3342
3343 answer.mantissa &= ~(uint64_t(1) << binary::mantissa_explicit_bits());
3344 if (answer.power2 >= binary::infinite_power()) { // infinity
3345 answer.power2 = binary::infinite_power();
3346 answer.mantissa = 0;
3347 }
3348 return answer;
3349 }
3350
3351 } // namespace fast_float
3352
3353 #endif
3354
3355 #ifndef FASTFLOAT_BIGINT_H
3356 #define FASTFLOAT_BIGINT_H
3357
3358 #include <algorithm>
3359 #include <cstdint>
3360 #include <climits>
3361 #include <cstring>
3362
3363
3364 namespace fast_float {
3365
3366 // the limb width: we want efficient multiplication of double the bits in
3367 // limb, or for 64-bit limbs, at least 64-bit multiplication where we can
3368 // extract the high and low parts efficiently. this is every 64-bit
3369 // architecture except for sparc, which emulates 128-bit multiplication.
3370 // we might have platforms where `CHAR_BIT` is not 8, so let's avoid
3371 // doing `8 * sizeof(limb)`.
3372 #if defined(FASTFLOAT_64BIT) && !defined(__sparc)
3373 #define FASTFLOAT_64BIT_LIMB 1
3374 typedef uint64_t limb;
3375 constexpr size_t limb_bits = 64;
3376 #else
3377 #define FASTFLOAT_32BIT_LIMB
3378 typedef uint32_t limb;
3379 constexpr size_t limb_bits = 32;
3380 #endif
3381
3382 typedef span<limb> limb_span;
3383
3384 // number of bits in a bigint. this needs to be at least the number
3385 // of bits required to store the largest bigint, which is
3386 // `log2(10**(digits + max_exp))`, or `log2(10**(767 + 342))`, or
3387 // ~3600 bits, so we round to 4000.
3388 constexpr size_t bigint_bits = 4000;
3389 constexpr size_t bigint_limbs = bigint_bits / limb_bits;
3390
3391 // vector-like type that is allocated on the stack. the entire
3392 // buffer is pre-allocated, and only the length changes.
3393 template <uint16_t size> struct stackvec {
3394 limb data[size];
3395 // we never need more than 150 limbs
3396 uint16_t length{0};
3397
3398 stackvec() = default;
3399 stackvec(stackvec const &) = delete;
3400 stackvec &operator=(stackvec const &) = delete;
3401 stackvec(stackvec &&) = delete;
3402 stackvec &operator=(stackvec &&other) = delete;
3403
3404 // create stack vector from existing limb span.
3405 FASTFLOAT_CONSTEXPR20 stackvec(limb_span s) {
3406 FASTFLOAT_ASSERT(try_extend(s));
3407 }
3408
3409 FASTFLOAT_CONSTEXPR14 limb &operator[](size_t index) noexcept {
3410 FASTFLOAT_DEBUG_ASSERT(index < length);
3411 return data[index];
3412 }
3413
3414 FASTFLOAT_CONSTEXPR14 const limb &operator[](size_t index) const noexcept {
3415 FASTFLOAT_DEBUG_ASSERT(index < length);
3416 return data[index];
3417 }
3418
3419 // index from the end of the container
3420 FASTFLOAT_CONSTEXPR14 const limb &rindex(size_t index) const noexcept {
3421 FASTFLOAT_DEBUG_ASSERT(index < length);
3422 size_t rindex = length - index - 1;
3423 return data[rindex];
3424 }
3425
3426 // set the length, without bounds checking.
3427 FASTFLOAT_CONSTEXPR14 void set_len(size_t len) noexcept {
3428 length = uint16_t(len);
3429 }
3430
3431 constexpr size_t len() const noexcept { return length; }
3432
3433 constexpr bool is_empty() const noexcept { return length == 0; }
3434
3435 constexpr size_t capacity() const noexcept { return size; }
3436
3437 // append item to vector, without bounds checking
3438 FASTFLOAT_CONSTEXPR14 void push_unchecked(limb value) noexcept {
3439 data[length] = value;
3440 length++;
3441 }
3442
3443 // append item to vector, returning if item was added
3444 FASTFLOAT_CONSTEXPR14 bool try_push(limb value) noexcept {
3445 if (len() < capacity()) {
3446 push_unchecked(value);
3447 return true;
3448 } else {
3449 return false;
3450 }
3451 }
3452
3453 // add items to the vector, from a span, without bounds checking
3454 FASTFLOAT_CONSTEXPR20 void extend_unchecked(limb_span s) noexcept {
3455 limb *ptr = data + length;
3456 std::copy_n(s.ptr, s.len(), ptr);
3457 set_len(len() + s.len());
3458 }
3459
3460 // try to add items to the vector, returning if items were added
3461 FASTFLOAT_CONSTEXPR20 bool try_extend(limb_span s) noexcept {
3462 if (len() + s.len() <= capacity()) {
3463 extend_unchecked(s);
3464 return true;
3465 } else {
3466 return false;
3467 }
3468 }
3469
3470 // resize the vector, without bounds checking
3471 // if the new size is longer than the vector, assign value to each
3472 // appended item.
3473 FASTFLOAT_CONSTEXPR20
3474 void resize_unchecked(size_t new_len, limb value) noexcept {
3475 if (new_len > len()) {
3476 size_t count = new_len - len();
3477 limb *first = data + len();
3478 limb *last = first + count;
3479 ::std::fill(first, last, value);
3480 set_len(new_len);
3481 } else {
3482 set_len(new_len);
3483 }
3484 }
3485
3486 // try to resize the vector, returning if the vector was resized.
3487 FASTFLOAT_CONSTEXPR20 bool try_resize(size_t new_len, limb value) noexcept {
3488 if (new_len > capacity()) {
3489 return false;
3490 } else {
3491 resize_unchecked(new_len, value);
3492 return true;
3493 }
3494 }
3495
3496 // check if any limbs are non-zero after the given index.
3497 // this needs to be done in reverse order, since the index
3498 // is relative to the most significant limbs.
3499 FASTFLOAT_CONSTEXPR14 bool nonzero(size_t index) const noexcept {
3500 while (index < len()) {
3501 if (rindex(index) != 0) {
3502 return true;
3503 }
3504 index++;
3505 }
3506 return false;
3507 }
3508
3509 // normalize the big integer, so most-significant zero limbs are removed.
3510 FASTFLOAT_CONSTEXPR14 void normalize() noexcept {
3511 while (len() > 0 && rindex(0) == 0) {
3512 length--;
3513 }
3514 }
3515 };
3516
3517 fastfloat_really_inline FASTFLOAT_CONSTEXPR14 uint64_t
3518 empty_hi64(bool &truncated) noexcept {
3519 truncated = false;
3520 return 0;
3521 }
3522
3523 fastfloat_really_inline FASTFLOAT_CONSTEXPR20 uint64_t
3524 uint64_hi64(uint64_t r0, bool &truncated) noexcept {
3525 truncated = false;
3526 int shl = leading_zeroes(r0);
3527 return r0 << shl;
3528 }
3529
3530 fastfloat_really_inline FASTFLOAT_CONSTEXPR20 uint64_t
3531 uint64_hi64(uint64_t r0, uint64_t r1, bool &truncated) noexcept {
3532 int shl = leading_zeroes(r0);
3533 if (shl == 0) {
3534 truncated = r1 != 0;
3535 return r0;
3536 } else {
3537 int shr = 64 - shl;
3538 truncated = (r1 << shl) != 0;
3539 return (r0 << shl) | (r1 >> shr);
3540 }
3541 }
3542
3543 fastfloat_really_inline FASTFLOAT_CONSTEXPR20 uint64_t
3544 uint32_hi64(uint32_t r0, bool &truncated) noexcept {
3545 return uint64_hi64(r0, truncated);
3546 }
3547
3548 fastfloat_really_inline FASTFLOAT_CONSTEXPR20 uint64_t
3549 uint32_hi64(uint32_t r0, uint32_t r1, bool &truncated) noexcept {
3550 uint64_t x0 = r0;
3551 uint64_t x1 = r1;
3552 return uint64_hi64((x0 << 32) | x1, truncated);
3553 }
3554
3555 fastfloat_really_inline FASTFLOAT_CONSTEXPR20 uint64_t
3556 uint32_hi64(uint32_t r0, uint32_t r1, uint32_t r2, bool &truncated) noexcept {
3557 uint64_t x0 = r0;
3558 uint64_t x1 = r1;
3559 uint64_t x2 = r2;
3560 return uint64_hi64(x0, (x1 << 32) | x2, truncated);
3561 }
3562
3563 // add two small integers, checking for overflow.
3564 // we want an efficient operation. for msvc, where
3565 // we don't have built-in intrinsics, this is still
3566 // pretty fast.
3567 fastfloat_really_inline FASTFLOAT_CONSTEXPR20 limb
3568 scalar_add(limb x, limb y, bool &overflow) noexcept {
3569 limb z;
3570 // gcc and clang
3571 #if defined(__has_builtin)
3572 #if __has_builtin(__builtin_add_overflow)
3573 if (!cpp20_and_in_constexpr()) {
3574 overflow = __builtin_add_overflow(x, y, &z);
3575 return z;
3576 }
3577 #endif
3578 #endif
3579
3580 // generic, this still optimizes correctly on MSVC.
3581 z = x + y;
3582 overflow = z < x;
3583 return z;
3584 }
3585
3586 // multiply two small integers, getting both the high and low bits.
3587 fastfloat_really_inline FASTFLOAT_CONSTEXPR20 limb
3588 scalar_mul(limb x, limb y, limb &carry) noexcept {
3589 #ifdef FASTFLOAT_64BIT_LIMB
3590 #if defined(__SIZEOF_INT128__)
3591 // GCC and clang both define it as an extension.
3592 __uint128_t z = __uint128_t(x) * __uint128_t(y) + __uint128_t(carry);
3593 carry = limb(z >> limb_bits);
3594 return limb(z);
3595 #else
3596 // fallback, no native 128-bit integer multiplication with carry.
3597 // on msvc, this optimizes identically, somehow.
3598 value128 z = full_multiplication(x, y);
3599 bool overflow;
3600 z.low = scalar_add(z.low, carry, overflow);
3601 z.high += uint64_t(overflow); // cannot overflow
3602 carry = z.high;
3603 return z.low;
3604 #endif
3605 #else
3606 uint64_t z = uint64_t(x) * uint64_t(y) + uint64_t(carry);
3607 carry = limb(z >> limb_bits);
3608 return limb(z);
3609 #endif
3610 }
3611
3612 // add scalar value to bigint starting from offset.
3613 // used in grade school multiplication
3614 template <uint16_t size>
3615 inline FASTFLOAT_CONSTEXPR20 bool small_add_from(stackvec<size> &vec, limb y,
3616 size_t start) noexcept {
3617 size_t index = start;
3618 limb carry = y;
3619 bool overflow;
3620 while (carry != 0 && index < vec.len()) {
3621 vec[index] = scalar_add(vec[index], carry, overflow);
3622 carry = limb(overflow);
3623 index += 1;
3624 }
3625 if (carry != 0) {
3626 FASTFLOAT_TRY(vec.try_push(carry));
3627 }
3628 return true;
3629 }
3630
3631 // add scalar value to bigint.
3632 template <uint16_t size>
3633 fastfloat_really_inline FASTFLOAT_CONSTEXPR20 bool
3634 small_add(stackvec<size> &vec, limb y) noexcept {
3635 return small_add_from(vec, y, 0);
3636 }
3637
3638 // multiply bigint by scalar value.
3639 template <uint16_t size>
3640 inline FASTFLOAT_CONSTEXPR20 bool small_mul(stackvec<size> &vec,
3641 limb y) noexcept {
3642 limb carry = 0;
3643 for (size_t index = 0; index < vec.len(); index++) {
3644 vec[index] = scalar_mul(vec[index], y, carry);
3645 }
3646 if (carry != 0) {
3647 FASTFLOAT_TRY(vec.try_push(carry));
3648 }
3649 return true;
3650 }
3651
3652 // add bigint to bigint starting from index.
3653 // used in grade school multiplication
3654 template <uint16_t size>
3655 FASTFLOAT_CONSTEXPR20 bool large_add_from(stackvec<size> &x, limb_span y,
3656 size_t start) noexcept {
3657 // the effective x buffer is from `xstart..x.len()`, so exit early
3658 // if we can't get that current range.
3659 if (x.len() < start || y.len() > x.len() - start) {
3660 FASTFLOAT_TRY(x.try_resize(y.len() + start, 0));
3661 }
3662
3663 bool carry = false;
3664 for (size_t index = 0; index < y.len(); index++) {
3665 limb xi = x[index + start];
3666 limb yi = y[index];
3667 bool c1 = false;
3668 bool c2 = false;
3669 xi = scalar_add(xi, yi, c1);
3670 if (carry) {
3671 xi = scalar_add(xi, 1, c2);
3672 }
3673 x[index + start] = xi;
3674 carry = c1 | c2;
3675 }
3676
3677 // handle overflow
3678 if (carry) {
3679 FASTFLOAT_TRY(small_add_from(x, 1, y.len() + start));
3680 }
3681 return true;
3682 }
3683
3684 // add bigint to bigint.
3685 template <uint16_t size>
3686 fastfloat_really_inline FASTFLOAT_CONSTEXPR20 bool
3687 large_add_from(stackvec<size> &x, limb_span y) noexcept {
3688 return large_add_from(x, y, 0);
3689 }
3690
3691 // grade-school multiplication algorithm
3692 template <uint16_t size>
3693 FASTFLOAT_CONSTEXPR20 bool long_mul(stackvec<size> &x, limb_span y) noexcept {
3694 limb_span xs = limb_span(x.data, x.len());
3695 stackvec<size> z(xs);
3696 limb_span zs = limb_span(z.data, z.len());
3697
3698 if (y.len() != 0) {
3699 limb y0 = y[0];
3700 FASTFLOAT_TRY(small_mul(x, y0));
3701 for (size_t index = 1; index < y.len(); index++) {
3702 limb yi = y[index];
3703 stackvec<size> zi;
3704 if (yi != 0) {
3705 // re-use the same buffer throughout
3706 zi.set_len(0);
3707 FASTFLOAT_TRY(zi.try_extend(zs));
3708 FASTFLOAT_TRY(small_mul(zi, yi));
3709 limb_span zis = limb_span(zi.data, zi.len());
3710 FASTFLOAT_TRY(large_add_from(x, zis, index));
3711 }
3712 }
3713 }
3714
3715 x.normalize();
3716 return true;
3717 }
3718
3719 // grade-school multiplication algorithm
3720 template <uint16_t size>
3721 FASTFLOAT_CONSTEXPR20 bool large_mul(stackvec<size> &x, limb_span y) noexcept {
3722 if (y.len() == 1) {
3723 FASTFLOAT_TRY(small_mul(x, y[0]));
3724 } else {
3725 FASTFLOAT_TRY(long_mul(x, y));
3726 }
3727 return true;
3728 }
3729
3730 template <typename = void> struct pow5_tables {
3731 static constexpr uint32_t large_step = 135;
3732 static constexpr uint64_t small_power_of_5[] = {
3733 1UL,
3734 5UL,
3735 25UL,
3736 125UL,
3737 625UL,
3738 3125UL,
3739 15625UL,
3740 78125UL,
3741 390625UL,
3742 1953125UL,
3743 9765625UL,
3744 48828125UL,
3745 244140625UL,
3746 1220703125UL,
3747 6103515625UL,
3748 30517578125UL,
3749 152587890625UL,
3750 762939453125UL,
3751 3814697265625UL,
3752 19073486328125UL,
3753 95367431640625UL,
3754 476837158203125UL,
3755 2384185791015625UL,
3756 11920928955078125UL,
3757 59604644775390625UL,
3758 298023223876953125UL,
3759 1490116119384765625UL,
3760 7450580596923828125UL,
3761 };
3762 #ifdef FASTFLOAT_64BIT_LIMB
3763 constexpr static limb large_power_of_5[] = {
3764 1414648277510068013UL, 9180637584431281687UL, 4539964771860779200UL,
3765 10482974169319127550UL, 198276706040285095UL};
3766 #else
3767 constexpr static limb large_power_of_5[] = {
3768 4279965485U, 329373468U, 4020270615U, 2137533757U, 4287402176U,
3769 1057042919U, 1071430142U, 2440757623U, 381945767U, 46164893U};
3770 #endif
3771 };
3772
3773 #if FASTFLOAT_DETAIL_MUST_DEFINE_CONSTEXPR_VARIABLE
3774
3775 template <typename T> constexpr uint32_t pow5_tables<T>::large_step;
3776
3777 template <typename T> constexpr uint64_t pow5_tables<T>::small_power_of_5[];
3778
3779 template <typename T> constexpr limb pow5_tables<T>::large_power_of_5[];
3780
3781 #endif
3782
3783 // big integer type. implements a small subset of big integer
3784 // arithmetic, using simple algorithms since asymptotically
3785 // faster algorithms are slower for a small number of limbs.
3786 // all operations assume the big-integer is normalized.
3787 struct bigint : pow5_tables<> {
3788 // storage of the limbs, in little-endian order.
3789 stackvec<bigint_limbs> vec;
3790
3791 FASTFLOAT_CONSTEXPR20 bigint() : vec() {}
3792
3793 bigint(bigint const &) = delete;
3794 bigint &operator=(bigint const &) = delete;
3795 bigint(bigint &&) = delete;
3796 bigint &operator=(bigint &&other) = delete;
3797
3798 FASTFLOAT_CONSTEXPR20 bigint(uint64_t value) : vec() {
3799 #ifdef FASTFLOAT_64BIT_LIMB
3800 vec.push_unchecked(value);
3801 #else
3802 vec.push_unchecked(uint32_t(value));
3803 vec.push_unchecked(uint32_t(value >> 32));
3804 #endif
3805 vec.normalize();
3806 }
3807
3808 // get the high 64 bits from the vector, and if bits were truncated.
3809 // this is to get the significant digits for the float.
3810 FASTFLOAT_CONSTEXPR20 uint64_t hi64(bool &truncated) const noexcept {
3811 #ifdef FASTFLOAT_64BIT_LIMB
3812 if (vec.len() == 0) {
3813 return empty_hi64(truncated);
3814 } else if (vec.len() == 1) {
3815 return uint64_hi64(vec.rindex(0), truncated);
3816 } else {
3817 uint64_t result = uint64_hi64(vec.rindex(0), vec.rindex(1), truncated);
3818 truncated |= vec.nonzero(2);
3819 return result;
3820 }
3821 #else
3822 if (vec.len() == 0) {
3823 return empty_hi64(truncated);
3824 } else if (vec.len() == 1) {
3825 return uint32_hi64(vec.rindex(0), truncated);
3826 } else if (vec.len() == 2) {
3827 return uint32_hi64(vec.rindex(0), vec.rindex(1), truncated);
3828 } else {
3829 uint64_t result =
3830 uint32_hi64(vec.rindex(0), vec.rindex(1), vec.rindex(2), truncated);
3831 truncated |= vec.nonzero(3);
3832 return result;
3833 }
3834 #endif
3835 }
3836
3837 // compare two big integers, returning the large value.
3838 // assumes both are normalized. if the return value is
3839 // negative, other is larger, if the return value is
3840 // positive, this is larger, otherwise they are equal.
3841 // the limbs are stored in little-endian order, so we
3842 // must compare the limbs in ever order.
3843 FASTFLOAT_CONSTEXPR20 int compare(bigint const &other) const noexcept {
3844 if (vec.len() > other.vec.len()) {
3845 return 1;
3846 } else if (vec.len() < other.vec.len()) {
3847 return -1;
3848 } else {
3849 for (size_t index = vec.len(); index > 0; index--) {
3850 limb xi = vec[index - 1];
3851 limb yi = other.vec[index - 1];
3852 if (xi > yi) {
3853 return 1;
3854 } else if (xi < yi) {
3855 return -1;
3856 }
3857 }
3858 return 0;
3859 }
3860 }
3861
3862 // shift left each limb n bits, carrying over to the new limb
3863 // returns true if we were able to shift all the digits.
3864 FASTFLOAT_CONSTEXPR20 bool shl_bits(size_t n) noexcept {
3865 // Internally, for each item, we shift left by n, and add the previous
3866 // right shifted limb-bits.
3867 // For example, we transform (for u8) shifted left 2, to:
3868 // b10100100 b01000010
3869 // b10 b10010001 b00001000
3870 FASTFLOAT_DEBUG_ASSERT(n != 0);
3871 FASTFLOAT_DEBUG_ASSERT(n < sizeof(limb) * 8);
3872
3873 size_t shl = n;
3874 size_t shr = limb_bits - shl;
3875 limb prev = 0;
3876 for (size_t index = 0; index < vec.len(); index++) {
3877 limb xi = vec[index];
3878 vec[index] = (xi << shl) | (prev >> shr);
3879 prev = xi;
3880 }
3881
3882 limb carry = prev >> shr;
3883 if (carry != 0) {
3884 return vec.try_push(carry);
3885 }
3886 return true;
3887 }
3888
3889 // move the limbs left by `n` limbs.
3890 FASTFLOAT_CONSTEXPR20 bool shl_limbs(size_t n) noexcept {
3891 FASTFLOAT_DEBUG_ASSERT(n != 0);
3892 if (n + vec.len() > vec.capacity()) {
3893 return false;
3894 } else if (!vec.is_empty()) {
3895 // move limbs
3896 limb *dst = vec.data + n;
3897 limb const *src = vec.data;
3898 std::copy_backward(src, src + vec.len(), dst + vec.len());
3899 // fill in empty limbs
3900 limb *first = vec.data;
3901 limb *last = first + n;
3902 ::std::fill(first, last, 0);
3903 vec.set_len(n + vec.len());
3904 return true;
3905 } else {
3906 return true;
3907 }
3908 }
3909
3910 // move the limbs left by `n` bits.
3911 FASTFLOAT_CONSTEXPR20 bool shl(size_t n) noexcept {
3912 size_t rem = n % limb_bits;
3913 size_t div = n / limb_bits;
3914 if (rem != 0) {
3915 FASTFLOAT_TRY(shl_bits(rem));
3916 }
3917 if (div != 0) {
3918 FASTFLOAT_TRY(shl_limbs(div));
3919 }
3920 return true;
3921 }
3922
3923 // get the number of leading zeros in the bigint.
3924 FASTFLOAT_CONSTEXPR20 int ctlz() const noexcept {
3925 if (vec.is_empty()) {
3926 return 0;
3927 } else {
3928 #ifdef FASTFLOAT_64BIT_LIMB
3929 return leading_zeroes(vec.rindex(0));
3930 #else
3931 // no use defining a specialized leading_zeroes for a 32-bit type.
3932 uint64_t r0 = vec.rindex(0);
3933 return leading_zeroes(r0 << 32);
3934 #endif
3935 }
3936 }
3937
3938 // get the number of bits in the bigint.
3939 FASTFLOAT_CONSTEXPR20 int bit_length() const noexcept {
3940 int lz = ctlz();
3941 return int(limb_bits * vec.len()) - lz;
3942 }
3943
3944 FASTFLOAT_CONSTEXPR20 bool mul(limb y) noexcept { return small_mul(vec, y); }
3945
3946 FASTFLOAT_CONSTEXPR20 bool add(limb y) noexcept { return small_add(vec, y); }
3947
3948 // multiply as if by 2 raised to a power.
3949 FASTFLOAT_CONSTEXPR20 bool pow2(uint32_t exp) noexcept { return shl(exp); }
3950
3951 // multiply as if by 5 raised to a power.
3952 FASTFLOAT_CONSTEXPR20 bool pow5(uint32_t exp) noexcept {
3953 // multiply by a power of 5
3954 size_t large_length = sizeof(large_power_of_5) / sizeof(limb);
3955 limb_span large = limb_span(large_power_of_5, large_length);
3956 while (exp >= large_step) {
3957 FASTFLOAT_TRY(large_mul(vec, large));
3958 exp -= large_step;
3959 }
3960 #ifdef FASTFLOAT_64BIT_LIMB
3961 uint32_t small_step = 27;
3962 limb max_native = 7450580596923828125UL;
3963 #else
3964 uint32_t small_step = 13;
3965 limb max_native = 1220703125U;
3966 #endif
3967 while (exp >= small_step) {
3968 FASTFLOAT_TRY(small_mul(vec, max_native));
3969 exp -= small_step;
3970 }
3971 if (exp != 0) {
3972 // Work around clang bug https://godbolt.org/z/zedh7rrhc
3973 // This is similar to https://github.com/llvm/llvm-project/issues/47746,
3974 // except the workaround described there don't work here
3975 FASTFLOAT_TRY(small_mul(
3976 vec, limb(((void)small_power_of_5[0], small_power_of_5[exp]))));
3977 }
3978
3979 return true;
3980 }
3981
3982 // multiply as if by 10 raised to a power.
3983 FASTFLOAT_CONSTEXPR20 bool pow10(uint32_t exp) noexcept {
3984 FASTFLOAT_TRY(pow5(exp));
3985 return pow2(exp);
3986 }
3987 };
3988
3989 } // namespace fast_float
3990
3991 #endif
3992
3993 #ifndef FASTFLOAT_DIGIT_COMPARISON_H
3994 #define FASTFLOAT_DIGIT_COMPARISON_H
3995
3996 #include <algorithm>
3997 #include <cstdint>
3998 #include <cstring>
3999 #include <iterator>
4000
4001
4002 namespace fast_float {
4003
4004 // 1e0 to 1e19
4005 constexpr static uint64_t powers_of_ten_uint64[] = {1UL,
4006 10UL,
4007 100UL,
4008 1000UL,
4009 10000UL,
4010 100000UL,
4011 1000000UL,
4012 10000000UL,
4013 100000000UL,
4014 1000000000UL,
4015 10000000000UL,
4016 100000000000UL,
4017 1000000000000UL,
4018 10000000000000UL,
4019 100000000000000UL,
4020 1000000000000000UL,
4021 10000000000000000UL,
4022 100000000000000000UL,
4023 1000000000000000000UL,
4024 10000000000000000000UL};
4025
4026 // calculate the exponent, in scientific notation, of the number.
4027 // this algorithm is not even close to optimized, but it has no practical
4028 // effect on performance: in order to have a faster algorithm, we'd need
4029 // to slow down performance for faster algorithms, and this is still fast.
4030 fastfloat_really_inline FASTFLOAT_CONSTEXPR14 int32_t
4031 scientific_exponent(uint64_t mantissa, int32_t exponent) noexcept {
4032 while (mantissa >= 10000) {
4033 mantissa /= 10000;
4034 exponent += 4;
4035 }
4036 while (mantissa >= 100) {
4037 mantissa /= 100;
4038 exponent += 2;
4039 }
4040 while (mantissa >= 10) {
4041 mantissa /= 10;
4042 exponent += 1;
4043 }
4044 return exponent;
4045 }
4046
4047 // this converts a native floating-point number to an extended-precision float.
4048 template <typename T>
4049 fastfloat_really_inline FASTFLOAT_CONSTEXPR20 adjusted_mantissa
4050 to_extended(T value) noexcept {
4051 using equiv_uint = equiv_uint_t<T>;
4052 constexpr equiv_uint exponent_mask = binary_format<T>::exponent_mask();
4053 constexpr equiv_uint mantissa_mask = binary_format<T>::mantissa_mask();
4054 constexpr equiv_uint hidden_bit_mask = binary_format<T>::hidden_bit_mask();
4055
4056 adjusted_mantissa am;
4057 int32_t bias = binary_format<T>::mantissa_explicit_bits() -
4058 binary_format<T>::minimum_exponent();
4059 equiv_uint bits;
4060 #if FASTFLOAT_HAS_BIT_CAST
4061 bits = std::bit_cast<equiv_uint>(value);
4062 #else
4063 ::memcpy(&bits, &value, sizeof(T));
4064 #endif
4065 if ((bits & exponent_mask) == 0) {
4066 // denormal
4067 am.power2 = 1 - bias;
4068 am.mantissa = bits & mantissa_mask;
4069 } else {
4070 // normal
4071 am.power2 = int32_t((bits & exponent_mask) >>
4072 binary_format<T>::mantissa_explicit_bits());
4073 am.power2 -= bias;
4074 am.mantissa = (bits & mantissa_mask) | hidden_bit_mask;
4075 }
4076
4077 return am;
4078 }
4079
4080 // get the extended precision value of the halfway point between b and b+u.
4081 // we are given a native float that represents b, so we need to adjust it
4082 // halfway between b and b+u.
4083 template <typename T>
4084 fastfloat_really_inline FASTFLOAT_CONSTEXPR20 adjusted_mantissa
4085 to_extended_halfway(T value) noexcept {
4086 adjusted_mantissa am = to_extended(value);
4087 am.mantissa <<= 1;
4088 am.mantissa += 1;
4089 am.power2 -= 1;
4090 return am;
4091 }
4092
4093 // round an extended-precision float to the nearest machine float.
4094 template <typename T, typename callback>
4095 fastfloat_really_inline FASTFLOAT_CONSTEXPR14 void round(adjusted_mantissa &am,
4096 callback cb) noexcept {
4097 int32_t mantissa_shift = 64 - binary_format<T>::mantissa_explicit_bits() - 1;
4098 if (-am.power2 >= mantissa_shift) {
4099 // have a denormal float
4100 int32_t shift = -am.power2 + 1;
4101 cb(am, std::min<int32_t>(shift, 64));
4102 // check for round-up: if rounding-nearest carried us to the hidden bit.
4103 am.power2 = (am.mantissa <
4104 (uint64_t(1) << binary_format<T>::mantissa_explicit_bits()))
4105 ? 0
4106 : 1;
4107 return;
4108 }
4109
4110 // have a normal float, use the default shift.
4111 cb(am, mantissa_shift);
4112
4113 // check for carry
4114 if (am.mantissa >=
4115 (uint64_t(2) << binary_format<T>::mantissa_explicit_bits())) {
4116 am.mantissa = (uint64_t(1) << binary_format<T>::mantissa_explicit_bits());
4117 am.power2++;
4118 }
4119
4120 // check for infinite: we could have carried to an infinite power
4121 am.mantissa &= ~(uint64_t(1) << binary_format<T>::mantissa_explicit_bits());
4122 if (am.power2 >= binary_format<T>::infinite_power()) {
4123 am.power2 = binary_format<T>::infinite_power();
4124 am.mantissa = 0;
4125 }
4126 }
4127
4128 template <typename callback>
4129 fastfloat_really_inline FASTFLOAT_CONSTEXPR14 void
4130 round_nearest_tie_even(adjusted_mantissa &am, int32_t shift,
4131 callback cb) noexcept {
4132 uint64_t const mask = (shift == 64) ? UINT64_MAX : (uint64_t(1) << shift) - 1;
4133 uint64_t const halfway = (shift == 0) ? 0 : uint64_t(1) << (shift - 1);
4134 uint64_t truncated_bits = am.mantissa & mask;
4135 bool is_above = truncated_bits > halfway;
4136 bool is_halfway = truncated_bits == halfway;
4137
4138 // shift digits into position
4139 if (shift == 64) {
4140 am.mantissa = 0;
4141 } else {
4142 am.mantissa >>= shift;
4143 }
4144 am.power2 += shift;
4145
4146 bool is_odd = (am.mantissa & 1) == 1;
4147 am.mantissa += uint64_t(cb(is_odd, is_halfway, is_above));
4148 }
4149
4150 fastfloat_really_inline FASTFLOAT_CONSTEXPR14 void
4151 round_down(adjusted_mantissa &am, int32_t shift) noexcept {
4152 if (shift == 64) {
4153 am.mantissa = 0;
4154 } else {
4155 am.mantissa >>= shift;
4156 }
4157 am.power2 += shift;
4158 }
4159
4160 template <typename UC>
4161 fastfloat_really_inline FASTFLOAT_CONSTEXPR20 void
4162 skip_zeros(UC const *&first, UC const *last) noexcept {
4163 uint64_t val;
4164 while (!cpp20_and_in_constexpr() &&
4165 std::distance(first, last) >= int_cmp_len<UC>()) {
4166 ::memcpy(&val, first, sizeof(uint64_t));
4167 if (val != int_cmp_zeros<UC>()) {
4168 break;
4169 }
4170 first += int_cmp_len<UC>();
4171 }
4172 while (first != last) {
4173 if (*first != UC('0')) {
4174 break;
4175 }
4176 first++;
4177 }
4178 }
4179
4180 // determine if any non-zero digits were truncated.
4181 // all characters must be valid digits.
4182 template <typename UC>
4183 fastfloat_really_inline FASTFLOAT_CONSTEXPR20 bool
4184 is_truncated(UC const *first, UC const *last) noexcept {
4185 // do 8-bit optimizations, can just compare to 8 literal 0s.
4186 uint64_t val;
4187 while (!cpp20_and_in_constexpr() &&
4188 std::distance(first, last) >= int_cmp_len<UC>()) {
4189 ::memcpy(&val, first, sizeof(uint64_t));
4190 if (val != int_cmp_zeros<UC>()) {
4191 return true;
4192 }
4193 first += int_cmp_len<UC>();
4194 }
4195 while (first != last) {
4196 if (*first != UC('0')) {
4197 return true;
4198 }
4199 ++first;
4200 }
4201 return false;
4202 }
4203
4204 template <typename UC>
4205 fastfloat_really_inline FASTFLOAT_CONSTEXPR20 bool
4206 is_truncated(span<UC const> s) noexcept {
4207 return is_truncated(s.ptr, s.ptr + s.len());
4208 }
4209
4210 template <typename UC>
4211 fastfloat_really_inline FASTFLOAT_CONSTEXPR20 void
4212 parse_eight_digits(UC const *&p, limb &value, size_t &counter,
4213 size_t &count) noexcept {
4214 value = value * 100000000 + parse_eight_digits_unrolled(p);
4215 p += 8;
4216 counter += 8;
4217 count += 8;
4218 }
4219
4220 template <typename UC>
4221 fastfloat_really_inline FASTFLOAT_CONSTEXPR14 void
4222 parse_one_digit(UC const *&p, limb &value, size_t &counter,
4223 size_t &count) noexcept {
4224 value = value * 10 + limb(*p - UC('0'));
4225 p++;
4226 counter++;
4227 count++;
4228 }
4229
4230 fastfloat_really_inline FASTFLOAT_CONSTEXPR20 void
4231 add_native(bigint &big, limb power, limb value) noexcept {
4232 big.mul(power);
4233 big.add(value);
4234 }
4235
4236 fastfloat_really_inline FASTFLOAT_CONSTEXPR20 void
4237 round_up_bigint(bigint &big, size_t &count) noexcept {
4238 // need to round-up the digits, but need to avoid rounding
4239 // ....9999 to ...10000, which could cause a false halfway point.
4240 add_native(big, 10, 1);
4241 count++;
4242 }
4243
4244 // parse the significant digits into a big integer
4245 template <typename UC>
4246 inline FASTFLOAT_CONSTEXPR20 void
4247 parse_mantissa(bigint &result, parsed_number_string_t<UC> &num,
4248 size_t max_digits, size_t &digits) noexcept {
4249 // try to minimize the number of big integer and scalar multiplication.
4250 // therefore, try to parse 8 digits at a time, and multiply by the largest
4251 // scalar value (9 or 19 digits) for each step.
4252 size_t counter = 0;
4253 digits = 0;
4254 limb value = 0;
4255 #ifdef FASTFLOAT_64BIT_LIMB
4256 size_t step = 19;
4257 #else
4258 size_t step = 9;
4259 #endif
4260
4261 // process all integer digits.
4262 UC const *p = num.integer.ptr;
4263 UC const *pend = p + num.integer.len();
4264 skip_zeros(p, pend);
4265 // process all digits, in increments of step per loop
4266 while (p != pend) {
4267 while ((std::distance(p, pend) >= 8) && (step - counter >= 8) &&
4268 (max_digits - digits >= 8)) {
4269 parse_eight_digits(p, value, counter, digits);
4270 }
4271 while (counter < step && p != pend && digits < max_digits) {
4272 parse_one_digit(p, value, counter, digits);
4273 }
4274 if (digits == max_digits) {
4275 // add the temporary value, then check if we've truncated any digits
4276 add_native(result, limb(powers_of_ten_uint64[counter]), value);
4277 bool truncated = is_truncated(p, pend);
4278 if (num.fraction.ptr != nullptr) {
4279 truncated |= is_truncated(num.fraction);
4280 }
4281 if (truncated) {
4282 round_up_bigint(result, digits);
4283 }
4284 return;
4285 } else {
4286 add_native(result, limb(powers_of_ten_uint64[counter]), value);
4287 counter = 0;
4288 value = 0;
4289 }
4290 }
4291
4292 // add our fraction digits, if they're available.
4293 if (num.fraction.ptr != nullptr) {
4294 p = num.fraction.ptr;
4295 pend = p + num.fraction.len();
4296 if (digits == 0) {
4297 skip_zeros(p, pend);
4298 }
4299 // process all digits, in increments of step per loop
4300 while (p != pend) {
4301 while ((std::distance(p, pend) >= 8) && (step - counter >= 8) &&
4302 (max_digits - digits >= 8)) {
4303 parse_eight_digits(p, value, counter, digits);
4304 }
4305 while (counter < step && p != pend && digits < max_digits) {
4306 parse_one_digit(p, value, counter, digits);
4307 }
4308 if (digits == max_digits) {
4309 // add the temporary value, then check if we've truncated any digits
4310 add_native(result, limb(powers_of_ten_uint64[counter]), value);
4311 bool truncated = is_truncated(p, pend);
4312 if (truncated) {
4313 round_up_bigint(result, digits);
4314 }
4315 return;
4316 } else {
4317 add_native(result, limb(powers_of_ten_uint64[counter]), value);
4318 counter = 0;
4319 value = 0;
4320 }
4321 }
4322 }
4323
4324 if (counter != 0) {
4325 add_native(result, limb(powers_of_ten_uint64[counter]), value);
4326 }
4327 }
4328
4329 template <typename T>
4330 inline FASTFLOAT_CONSTEXPR20 adjusted_mantissa
4331 positive_digit_comp(bigint &bigmant, int32_t exponent) noexcept {
4332 FASTFLOAT_ASSERT(bigmant.pow10(uint32_t(exponent)));
4333 adjusted_mantissa answer;
4334 bool truncated;
4335 answer.mantissa = bigmant.hi64(truncated);
4336 int bias = binary_format<T>::mantissa_explicit_bits() -
4337 binary_format<T>::minimum_exponent();
4338 answer.power2 = bigmant.bit_length() - 64 + bias;
4339
4340 round<T>(answer, [truncated](adjusted_mantissa &a, int32_t shift) {
4341 round_nearest_tie_even(
4342 a, shift,
4343 [truncated](bool is_odd, bool is_halfway, bool is_above) -> bool {
4344 return is_above || (is_halfway && truncated) ||
4345 (is_odd && is_halfway);
4346 });
4347 });
4348
4349 return answer;
4350 }
4351
4352 // the scaling here is quite simple: we have, for the real digits `m * 10^e`,
4353 // and for the theoretical digits `n * 2^f`. Since `e` is always negative,
4354 // to scale them identically, we do `n * 2^f * 5^-f`, so we now have `m * 2^e`.
4355 // we then need to scale by `2^(f- e)`, and then the two significant digits
4356 // are of the same magnitude.
4357 template <typename T>
4358 inline FASTFLOAT_CONSTEXPR20 adjusted_mantissa negative_digit_comp(
4359 bigint &bigmant, adjusted_mantissa am, int32_t exponent) noexcept {
4360 bigint &real_digits = bigmant;
4361 int32_t real_exp = exponent;
4362
4363 // get the value of `b`, rounded down, and get a bigint representation of b+h
4364 adjusted_mantissa am_b = am;
4365 // gcc7 buf: use a lambda to remove the noexcept qualifier bug with
4366 // -Wnoexcept-type.
4367 round<T>(am_b,
4368 [](adjusted_mantissa &a, int32_t shift) { round_down(a, shift); });
4369 T b;
4370 to_float(false, am_b, b);
4371 adjusted_mantissa theor = to_extended_halfway(b);
4372 bigint theor_digits(theor.mantissa);
4373 int32_t theor_exp = theor.power2;
4374
4375 // scale real digits and theor digits to be same power.
4376 int32_t pow2_exp = theor_exp - real_exp;
4377 uint32_t pow5_exp = uint32_t(-real_exp);
4378 if (pow5_exp != 0) {
4379 FASTFLOAT_ASSERT(theor_digits.pow5(pow5_exp));
4380 }
4381 if (pow2_exp > 0) {
4382 FASTFLOAT_ASSERT(theor_digits.pow2(uint32_t(pow2_exp)));
4383 } else if (pow2_exp < 0) {
4384 FASTFLOAT_ASSERT(real_digits.pow2(uint32_t(-pow2_exp)));
4385 }
4386
4387 // compare digits, and use it to direct rounding
4388 int ord = real_digits.compare(theor_digits);
4389 adjusted_mantissa answer = am;
4390 round<T>(answer, [ord](adjusted_mantissa &a, int32_t shift) {
4391 round_nearest_tie_even(
4392 a, shift, [ord](bool is_odd, bool _, bool __) -> bool {
4393 (void)_; // not needed, since we've done our comparison
4394 (void)__; // not needed, since we've done our comparison
4395 if (ord > 0) {
4396 return true;
4397 } else if (ord < 0) {
4398 return false;
4399 } else {
4400 return is_odd;
4401 }
4402 });
4403 });
4404
4405 return answer;
4406 }
4407
4408 // parse the significant digits as a big integer to unambiguously round
4409 // the significant digits. here, we are trying to determine how to round
4410 // an extended float representation close to `b+h`, halfway between `b`
4411 // (the float rounded-down) and `b+u`, the next positive float. this
4412 // algorithm is always correct, and uses one of two approaches. when
4413 // the exponent is positive relative to the significant digits (such as
4414 // 1234), we create a big-integer representation, get the high 64-bits,
4415 // determine if any lower bits are truncated, and use that to direct
4416 // rounding. in case of a negative exponent relative to the significant
4417 // digits (such as 1.2345), we create a theoretical representation of
4418 // `b` as a big-integer type, scaled to the same binary exponent as
4419 // the actual digits. we then compare the big integer representations
4420 // of both, and use that to direct rounding.
4421 template <typename T, typename UC>
4422 inline FASTFLOAT_CONSTEXPR20 adjusted_mantissa
4423 digit_comp(parsed_number_string_t<UC> &num, adjusted_mantissa am) noexcept {
4424 // remove the invalid exponent bias
4425 am.power2 -= invalid_am_bias;
4426
4427 int32_t sci_exp =
4428 scientific_exponent(num.mantissa, static_cast<int32_t>(num.exponent));
4429 size_t max_digits = binary_format<T>::max_digits();
4430 size_t digits = 0;
4431 bigint bigmant;
4432 parse_mantissa(bigmant, num, max_digits, digits);
4433 // can't underflow, since digits is at most max_digits.
4434 int32_t exponent = sci_exp + 1 - int32_t(digits);
4435 if (exponent >= 0) {
4436 return positive_digit_comp<T>(bigmant, exponent);
4437 } else {
4438 return negative_digit_comp<T>(bigmant, am, exponent);
4439 }
4440 }
4441
4442 } // namespace fast_float
4443
4444 #endif
4445
4446 #ifndef FASTFLOAT_PARSE_NUMBER_H
4447 #define FASTFLOAT_PARSE_NUMBER_H
4448
4449
4450 #include <cmath>
4451 #include <cstring>
4452 #include <limits>
4453 #include <system_error>
4454
4455 namespace fast_float {
4456
4457 namespace detail {
4458 /**
4459 * Special case +inf, -inf, nan, infinity, -infinity.
4460 * The case comparisons could be made much faster given that we know that the
4461 * strings a null-free and fixed.
4462 **/
4463 template <typename T, typename UC>
4464 from_chars_result_t<UC>
4465 FASTFLOAT_CONSTEXPR14 parse_infnan(UC const *first, UC const *last,
4466 T &value, chars_format fmt) noexcept {
4467 from_chars_result_t<UC> answer{};
4468 answer.ptr = first;
4469 answer.ec = std::errc(); // be optimistic
4470 // assume first < last, so dereference without checks;
4471 bool const minusSign = (*first == UC('-'));
4472 // C++17 20.19.3.(7.1) explicitly forbids '+' sign here
4473 if ((*first == UC('-')) ||
4474 (uint64_t(fmt & chars_format::allow_leading_plus) &&
4475 (*first == UC('+')))) {
4476 ++first;
4477 }
4478 if (last - first >= 3) {
4479 if (fastfloat_strncasecmp3(first, str_const_nan<UC>())) {
4480 answer.ptr = (first += 3);
4481 value = minusSign ? -std::numeric_limits<T>::quiet_NaN()
4482 : std::numeric_limits<T>::quiet_NaN();
4483 // Check for possible nan(n-char-seq-opt), C++17 20.19.3.7,
4484 // C11 7.20.1.3.3. At least MSVC produces nan(ind) and nan(snan).
4485 if (first != last && *first == UC('(')) {
4486 for (UC const *ptr = first + 1; ptr != last; ++ptr) {
4487 if (*ptr == UC(')')) {
4488 answer.ptr = ptr + 1; // valid nan(n-char-seq-opt)
4489 break;
4490 } else if (!((UC('a') <= *ptr && *ptr <= UC('z')) ||
4491 (UC('A') <= *ptr && *ptr <= UC('Z')) ||
4492 (UC('0') <= *ptr && *ptr <= UC('9')) || *ptr == UC('_')))
4493 break; // forbidden char, not nan(n-char-seq-opt)
4494 }
4495 }
4496 return answer;
4497 }
4498 if (fastfloat_strncasecmp3(first, str_const_inf<UC>())) {
4499 if ((last - first >= 8) &&
4500 fastfloat_strncasecmp5(first + 3, str_const_inf<UC>() + 3)) {
4501 answer.ptr = first + 8;
4502 } else {
4503 answer.ptr = first + 3;
4504 }
4505 value = minusSign ? -std::numeric_limits<T>::infinity()
4506 : std::numeric_limits<T>::infinity();
4507 return answer;
4508 }
4509 }
4510 answer.ec = std::errc::invalid_argument;
4511 return answer;
4512 }
4513
4514 /**
4515 * Returns true if the floating-pointing rounding mode is to 'nearest'.
4516 * It is the default on most system. This function is meant to be inexpensive.
4517 * Credit : @mwalcott3
4518 */
4519 fastfloat_really_inline bool rounds_to_nearest() noexcept {
4520 // https://lemire.me/blog/2020/06/26/gcc-not-nearest/
4521 #if (FLT_EVAL_METHOD != 1) && (FLT_EVAL_METHOD != 0)
4522 return false;
4523 #endif
4524 // See
4525 // A fast function to check your floating-point rounding mode
4526 // https://lemire.me/blog/2022/11/16/a-fast-function-to-check-your-floating-point-rounding-mode/
4527 //
4528 // This function is meant to be equivalent to :
4529 // prior: #include <cfenv>
4530 // return fegetround() == FE_TONEAREST;
4531 // However, it is expected to be much faster than the fegetround()
4532 // function call.
4533 //
4534 // The volatile keyword prevents the compiler from computing the function
4535 // at compile-time.
4536 // There might be other ways to prevent compile-time optimizations (e.g.,
4537 // asm). The value does not need to be std::numeric_limits<float>::min(), any
4538 // small value so that 1 + x should round to 1 would do (after accounting for
4539 // excess precision, as in 387 instructions).
4540 static float volatile fmin = std::numeric_limits<float>::min();
4541 float fmini = fmin; // we copy it so that it gets loaded at most once.
4542 //
4543 // Explanation:
4544 // Only when fegetround() == FE_TONEAREST do we have that
4545 // fmin + 1.0f == 1.0f - fmin.
4546 //
4547 // FE_UPWARD:
4548 // fmin + 1.0f > 1
4549 // 1.0f - fmin == 1
4550 //
4551 // FE_DOWNWARD or FE_TOWARDZERO:
4552 // fmin + 1.0f == 1
4553 // 1.0f - fmin < 1
4554 //
4555 // Note: This may fail to be accurate if fast-math has been
4556 // enabled, as rounding conventions may not apply.
4557 #ifdef FASTFLOAT_VISUAL_STUDIO
4558 #pragma warning(push)
4559 // todo: is there a VS warning?
4560 // see
4561 // https://stackoverflow.com/questions/46079446/is-there-a-warning-for-floating-point-equality-checking-in-visual-studio-2013
4562 #elif defined(__clang__)
4563 #pragma clang diagnostic push
4564 #pragma clang diagnostic ignored "-Wfloat-equal"
4565 #elif defined(__GNUC__)
4566 #pragma GCC diagnostic push
4567 #pragma GCC diagnostic ignored "-Wfloat-equal"
4568 #endif
4569 return (fmini + 1.0f == 1.0f - fmini);
4570 #ifdef FASTFLOAT_VISUAL_STUDIO
4571 #pragma warning(pop)
4572 #elif defined(__clang__)
4573 #pragma clang diagnostic pop
4574 #elif defined(__GNUC__)
4575 #pragma GCC diagnostic pop
4576 #endif
4577 }
4578
4579 } // namespace detail
4580
4581 template <typename T> struct from_chars_caller {
4582 template <typename UC>
4583 FASTFLOAT_CONSTEXPR20 static from_chars_result_t<UC>
4584 call(UC const *first, UC const *last, T &value,
4585 parse_options_t<UC> options) noexcept {
4586 return from_chars_advanced(first, last, value, options);
4587 }
4588 };
4589
4590 #ifdef __STDCPP_FLOAT32_T__
4591 template <> struct from_chars_caller<std::float32_t> {
4592 template <typename UC>
4593 FASTFLOAT_CONSTEXPR20 static from_chars_result_t<UC>
4594 call(UC const *first, UC const *last, std::float32_t &value,
4595 parse_options_t<UC> options) noexcept {
4596 // if std::float32_t is defined, and we are in C++23 mode; macro set for
4597 // float32; set value to float due to equivalence between float and
4598 // float32_t
4599 float val;
4600 auto ret = from_chars_advanced(first, last, val, options);
4601 value = val;
4602 return ret;
4603 }
4604 };
4605 #endif
4606
4607 #ifdef __STDCPP_FLOAT64_T__
4608 template <> struct from_chars_caller<std::float64_t> {
4609 template <typename UC>
4610 FASTFLOAT_CONSTEXPR20 static from_chars_result_t<UC>
4611 call(UC const *first, UC const *last, std::float64_t &value,
4612 parse_options_t<UC> options) noexcept {
4613 // if std::float64_t is defined, and we are in C++23 mode; macro set for
4614 // float64; set value as double due to equivalence between double and
4615 // float64_t
4616 double val;
4617 auto ret = from_chars_advanced(first, last, val, options);
4618 value = val;
4619 return ret;
4620 }
4621 };
4622 #endif
4623
4624 template <typename T, typename UC, typename>
4625 FASTFLOAT_CONSTEXPR20 from_chars_result_t<UC>
4626 from_chars(UC const *first, UC const *last, T &value,
4627 chars_format fmt /*= chars_format::general*/) noexcept {
4628 return from_chars_caller<T>::call(first, last, value,
4629 parse_options_t<UC>(fmt));
4630 }
4631
4632 template <typename T>
4633 fastfloat_really_inline FASTFLOAT_CONSTEXPR20 bool
4634 clinger_fast_path_impl(uint64_t mantissa, int64_t exponent, bool is_negative,
4635 T &value) noexcept {
4636 // The implementation of the Clinger's fast path is convoluted because
4637 // we want round-to-nearest in all cases, irrespective of the rounding mode
4638 // selected on the thread.
4639 // We proceed optimistically, assuming that detail::rounds_to_nearest()
4640 // returns true.
4641 if (binary_format<T>::min_exponent_fast_path() <= exponent &&
4642 exponent <= binary_format<T>::max_exponent_fast_path()) {
4643 // Unfortunately, the conventional Clinger's fast path is only possible
4644 // when the system rounds to the nearest float.
4645 //
4646 // We expect the next branch to almost always be selected.
4647 // We could check it first (before the previous branch), but
4648 // there might be performance advantages at having the check
4649 // be last.
4650 if (!cpp20_and_in_constexpr() && detail::rounds_to_nearest()) {
4651 // We have that fegetround() == FE_TONEAREST.
4652 // Next is Clinger's fast path.
4653 if (mantissa <= binary_format<T>::max_mantissa_fast_path()) {
4654 value = T(mantissa);
4655 if (exponent < 0) {
4656 value = value / binary_format<T>::exact_power_of_ten(-exponent);
4657 } else {
4658 value = value * binary_format<T>::exact_power_of_ten(exponent);
4659 }
4660 if (is_negative) {
4661 value = -value;
4662 }
4663 return true;
4664 }
4665 } else {
4666 // We do not have that fegetround() == FE_TONEAREST.
4667 // Next is a modified Clinger's fast path, inspired by Jakub Jelínek's
4668 // proposal
4669 if (exponent >= 0 &&
4670 mantissa <= binary_format<T>::max_mantissa_fast_path(exponent)) {
4671 #if defined(__clang__) || defined(FASTFLOAT_32BIT)
4672 // Clang may map 0 to -0.0 when fegetround() == FE_DOWNWARD
4673 if (mantissa == 0) {
4674 value = is_negative ? T(-0.) : T(0.);
4675 return true;
4676 }
4677 #endif
4678 value = T(mantissa) * binary_format<T>::exact_power_of_ten(exponent);
4679 if (is_negative) {
4680 value = -value;
4681 }
4682 return true;
4683 }
4684 }
4685 }
4686 return false;
4687 }
4688
4689 /**
4690 * This function overload takes parsed_number_string_t structure that is created
4691 * and populated either by from_chars_advanced function taking chars range and
4692 * parsing options or other parsing custom function implemented by user.
4693 */
4694 template <typename T, typename UC>
4695 fastfloat_really_inline FASTFLOAT_CONSTEXPR20 from_chars_result_t<UC>
4696 from_chars_advanced(parsed_number_string_t<UC> &pns, T &value) noexcept {
4697 static_assert(is_supported_float_type<T>::value,
4698 "only some floating-point types are supported");
4699 static_assert(is_supported_char_type<UC>::value,
4700 "only char, wchar_t, char16_t and char32_t are supported");
4701
4702 from_chars_result_t<UC> answer;
4703
4704 answer.ec = std::errc(); // be optimistic
4705 answer.ptr = pns.lastmatch;
4706
4707 if (!pns.too_many_digits &&
4708 clinger_fast_path_impl(pns.mantissa, pns.exponent, pns.negative, value))
4709 return answer;
4710
4711 adjusted_mantissa am =
4712 compute_float<binary_format<T>>(pns.exponent, pns.mantissa);
4713 if (pns.too_many_digits && am.power2 >= 0) {
4714 if (am != compute_float<binary_format<T>>(pns.exponent, pns.mantissa + 1)) {
4715 am = compute_error<binary_format<T>>(pns.exponent, pns.mantissa);
4716 }
4717 }
4718 // If we called compute_float<binary_format<T>>(pns.exponent, pns.mantissa)
4719 // and we have an invalid power (am.power2 < 0), then we need to go the long
4720 // way around again. This is very uncommon.
4721 if (am.power2 < 0) {
4722 am = digit_comp<T>(pns, am);
4723 }
4724 to_float(pns.negative, am, value);
4725 // Test for over/underflow.
4726 if ((pns.mantissa != 0 && am.mantissa == 0 && am.power2 == 0) ||
4727 am.power2 == binary_format<T>::infinite_power()) {
4728 answer.ec = std::errc::result_out_of_range;
4729 }
4730 return answer;
4731 }
4732
4733 template <typename T, typename UC>
4734 fastfloat_really_inline FASTFLOAT_CONSTEXPR20 from_chars_result_t<UC>
4735 from_chars_float_advanced(UC const *first, UC const *last, T &value,
4736 parse_options_t<UC> options) noexcept {
4737
4738 static_assert(is_supported_float_type<T>::value,
4739 "only some floating-point types are supported");
4740 static_assert(is_supported_char_type<UC>::value,
4741 "only char, wchar_t, char16_t and char32_t are supported");
4742
4743 chars_format const fmt = detail::adjust_for_feature_macros(options.format);
4744
4745 from_chars_result_t<UC> answer;
4746 if (uint64_t(fmt & chars_format::skip_white_space)) {
4747 while ((first != last) && fast_float::is_space(*first)) {
4748 first++;
4749 }
4750 }
4751 if (first == last) {
4752 answer.ec = std::errc::invalid_argument;
4753 answer.ptr = first;
4754 return answer;
4755 }
4756 parsed_number_string_t<UC> pns =
4757 uint64_t(fmt & detail::basic_json_fmt)
4758 ? parse_number_string<true, UC>(first, last, options)
4759 : parse_number_string<false, UC>(first, last, options);
4760 if (!pns.valid) {
4761 if (uint64_t(fmt & chars_format::no_infnan)) {
4762 answer.ec = std::errc::invalid_argument;
4763 answer.ptr = first;
4764 return answer;
4765 } else {
4766 return detail::parse_infnan(first, last, value, fmt);
4767 }
4768 }
4769
4770 // call overload that takes parsed_number_string_t directly.
4771 return from_chars_advanced(pns, value);
4772 }
4773
4774 template <typename T, typename UC, typename>
4775 FASTFLOAT_CONSTEXPR20 from_chars_result_t<UC>
4776 from_chars(UC const *first, UC const *last, T &value, int base) noexcept {
4777
4778 static_assert(is_supported_integer_type<T>::value,
4779 "only integer types are supported");
4780 static_assert(is_supported_char_type<UC>::value,
4781 "only char, wchar_t, char16_t and char32_t are supported");
4782
4783 parse_options_t<UC> options;
4784 options.base = base;
4785 return from_chars_advanced(first, last, value, options);
4786 }
4787
4788 template <typename T>
4789 FASTFLOAT_CONSTEXPR20
4790 typename std::enable_if<is_supported_float_type<T>::value, T>::type
4791 integer_times_pow10(uint64_t mantissa, int decimal_exponent) noexcept {
4792 T value;
4793 if (clinger_fast_path_impl(mantissa, decimal_exponent, false, value))
4794 return value;
4795
4796 adjusted_mantissa am =
4797 compute_float<binary_format<T>>(decimal_exponent, mantissa);
4798 to_float(false, am, value);
4799 return value;
4800 }
4801
4802 template <typename T>
4803 FASTFLOAT_CONSTEXPR20
4804 typename std::enable_if<is_supported_float_type<T>::value, T>::type
4805 integer_times_pow10(int64_t mantissa, int decimal_exponent) noexcept {
4806 const bool is_negative = mantissa < 0;
4807 const uint64_t m = static_cast<uint64_t>(is_negative ? -mantissa : mantissa);
4808
4809 T value;
4810 if (clinger_fast_path_impl(m, decimal_exponent, is_negative, value))
4811 return value;
4812
4813 adjusted_mantissa am = compute_float<binary_format<T>>(decimal_exponent, m);
4814 to_float(is_negative, am, value);
4815 return value;
4816 }
4817
4818 FASTFLOAT_CONSTEXPR20 inline double
4819 integer_times_pow10(uint64_t mantissa, int decimal_exponent) noexcept {
4820 return integer_times_pow10<double>(mantissa, decimal_exponent);
4821 }
4822
4823 FASTFLOAT_CONSTEXPR20 inline double
4824 integer_times_pow10(int64_t mantissa, int decimal_exponent) noexcept {
4825 return integer_times_pow10<double>(mantissa, decimal_exponent);
4826 }
4827
4828 // the following overloads are here to avoid surprising ambiguity for int,
4829 // unsigned, etc.
4830 template <typename T, typename Int>
4831 FASTFLOAT_CONSTEXPR20
4832 typename std::enable_if<is_supported_float_type<T>::value &&
4833 std::is_integral<Int>::value &&
4834 !std::is_signed<Int>::value,
4835 T>::type
4836 integer_times_pow10(Int mantissa, int decimal_exponent) noexcept {
4837 return integer_times_pow10<T>(static_cast<uint64_t>(mantissa),
4838 decimal_exponent);
4839 }
4840
4841 template <typename T, typename Int>
4842 FASTFLOAT_CONSTEXPR20
4843 typename std::enable_if<is_supported_float_type<T>::value &&
4844 std::is_integral<Int>::value &&
4845 std::is_signed<Int>::value,
4846 T>::type
4847 integer_times_pow10(Int mantissa, int decimal_exponent) noexcept {
4848 return integer_times_pow10<T>(static_cast<int64_t>(mantissa),
4849 decimal_exponent);
4850 }
4851
4852 template <typename Int>
4853 FASTFLOAT_CONSTEXPR20 typename std::enable_if<
4854 std::is_integral<Int>::value && !std::is_signed<Int>::value, double>::type
4855 integer_times_pow10(Int mantissa, int decimal_exponent) noexcept {
4856 return integer_times_pow10(static_cast<uint64_t>(mantissa), decimal_exponent);
4857 }
4858
4859 template <typename Int>
4860 FASTFLOAT_CONSTEXPR20 typename std::enable_if<
4861 std::is_integral<Int>::value && std::is_signed<Int>::value, double>::type
4862 integer_times_pow10(Int mantissa, int decimal_exponent) noexcept {
4863 return integer_times_pow10(static_cast<int64_t>(mantissa), decimal_exponent);
4864 }
4865
4866 template <typename T, typename UC>
4867 FASTFLOAT_CONSTEXPR20 from_chars_result_t<UC>
4868 from_chars_int_advanced(UC const *first, UC const *last, T &value,
4869 parse_options_t<UC> options) noexcept {
4870
4871 static_assert(is_supported_integer_type<T>::value,
4872 "only integer types are supported");
4873 static_assert(is_supported_char_type<UC>::value,
4874 "only char, wchar_t, char16_t and char32_t are supported");
4875
4876 chars_format const fmt = detail::adjust_for_feature_macros(options.format);
4877 int const base = options.base;
4878
4879 from_chars_result_t<UC> answer;
4880 if (uint64_t(fmt & chars_format::skip_white_space)) {
4881 while ((first != last) && fast_float::is_space(*first)) {
4882 first++;
4883 }
4884 }
4885 if (first == last || base < 2 || base > 36) {
4886 answer.ec = std::errc::invalid_argument;
4887 answer.ptr = first;
4888 return answer;
4889 }
4890
4891 return parse_int_string(first, last, value, options);
4892 }
4893
4894 template <size_t TypeIx> struct from_chars_advanced_caller {
4895 static_assert(TypeIx > 0, "unsupported type");
4896 };
4897
4898 template <> struct from_chars_advanced_caller<1> {
4899 template <typename T, typename UC>
4900 fastfloat_really_inline
4901 FASTFLOAT_CONSTEXPR20 static from_chars_result_t<UC>
4902 call(UC const *first, UC const *last, T &value,
4903 parse_options_t<UC> options) noexcept {
4904 return from_chars_float_advanced(first, last, value, options);
4905 }
4906 };
4907
4908 template <> struct from_chars_advanced_caller<2> {
4909 template <typename T, typename UC>
4910 fastfloat_really_inline FASTFLOAT_CONSTEXPR20 static from_chars_result_t<UC>
4911 call(UC const *first, UC const *last, T &value,
4912 parse_options_t<UC> options) noexcept {
4913 return from_chars_int_advanced(first, last, value, options);
4914 }
4915 };
4916
4917 template <typename T, typename UC>
4918 fastfloat_really_inline FASTFLOAT_CONSTEXPR20 from_chars_result_t<UC>
4919 from_chars_advanced(UC const *first, UC const *last, T &value,
4920 parse_options_t<UC> options) noexcept {
4921 return from_chars_advanced_caller<
4922 size_t(is_supported_float_type<T>::value) +
4923 2 * size_t(is_supported_integer_type<T>::value)>::call(first, last, value,
4924 options);
4925 }
4926
4927 } // namespace fast_float
4928
4929 #endif
4930