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