Coverage Report

Created: 2026-09-14 06:39

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/build-dir/_deps/simdjson-src/src/from_chars.cpp
Line
Count
Source
1
#ifndef SIMDJSON_SRC_FROM_CHARS_CPP
2
#define SIMDJSON_SRC_FROM_CHARS_CPP
3
4
#include <base.h>
5
6
#include <cstdint>
7
#include <cstring>
8
#include <limits>
9
10
namespace simdjson {
11
namespace internal {
12
13
/**
14
 * The code in the internal::from_chars function is meant to handle the floating-point number parsing
15
 * when we have more than 19 digits in the decimal mantissa. This should only be seen
16
 * in adversarial scenarios: we do not expect production systems to even produce
17
 * such floating-point numbers.
18
 *
19
 * The parser is based on work by Nigel Tao (at https://github.com/google/wuffs/)
20
 * who credits Ken Thompson for the design (via a reference to the Go source
21
 * code). See
22
 * https://github.com/google/wuffs/blob/aa46859ea40c72516deffa1b146121952d6dfd3b/internal/cgen/base/floatconv-submodule-data.c
23
 * https://github.com/google/wuffs/blob/46cd8105f47ca07ae2ba8e6a7818ef9c0df6c152/internal/cgen/base/floatconv-submodule-code.c
24
 * It is probably not very fast but it is a fallback that should almost never be
25
 * called in real life. Google Wuffs is published under APL 2.0.
26
 **/
27
28
namespace {
29
constexpr uint32_t max_digits = 768;
30
constexpr int32_t decimal_point_range = 2047;
31
} // namespace
32
33
struct adjusted_mantissa {
34
  uint64_t mantissa;
35
  int power2;
36
0
  adjusted_mantissa() : mantissa(0), power2(0) {}
37
};
38
39
struct decimal {
40
  uint32_t num_digits;
41
  int32_t decimal_point;
42
  bool negative;
43
  bool truncated;
44
  uint8_t digits[max_digits];
45
};
46
47
template <typename T> struct binary_format {
48
  static constexpr int mantissa_explicit_bits();
49
  static constexpr int minimum_exponent();
50
  static constexpr int infinite_power();
51
  static constexpr int sign_index();
52
};
53
54
0
template <> constexpr int binary_format<double>::mantissa_explicit_bits() {
55
0
  return 52;
56
0
}
57
58
0
template <> constexpr int binary_format<double>::minimum_exponent() {
59
0
  return -1023;
60
0
}
61
0
template <> constexpr int binary_format<double>::infinite_power() {
62
0
  return 0x7FF;
63
0
}
64
65
0
template <> constexpr int binary_format<double>::sign_index() { return 63; }
66
67
0
bool is_integer(char c)  noexcept  { return (c >= '0' && c <= '9'); }
68
69
// This should always succeed since it follows a call to parse_number.
70
0
decimal parse_decimal(const char *&p) noexcept {
71
0
  decimal answer;
72
0
  answer.num_digits = 0;
73
0
  answer.decimal_point = 0;
74
0
  answer.truncated = false;
75
0
  answer.negative = (*p == '-');
76
0
  if ((*p == '-') || (*p == '+')) {
77
0
    ++p;
78
0
  }
79
80
0
  while (*p == '0') {
81
0
    ++p;
82
0
  }
83
0
  while (is_integer(*p)) {
84
0
    if (answer.num_digits < max_digits) {
85
0
      answer.digits[answer.num_digits] = uint8_t(*p - '0');
86
0
    }
87
0
    answer.num_digits++;
88
0
    ++p;
89
0
  }
90
0
  if (*p == '.') {
91
0
    ++p;
92
0
    const char *first_after_period = p;
93
    // if we have not yet encountered a zero, we have to skip it as well
94
0
    if (answer.num_digits == 0) {
95
      // skip zeros
96
0
      while (*p == '0') {
97
0
        ++p;
98
0
      }
99
0
    }
100
0
    while (is_integer(*p)) {
101
0
      if (answer.num_digits < max_digits) {
102
0
        answer.digits[answer.num_digits] = uint8_t(*p - '0');
103
0
      }
104
0
      answer.num_digits++;
105
0
      ++p;
106
0
    }
107
0
    answer.decimal_point = int32_t(first_after_period - p);
108
0
  }
109
0
  if(answer.num_digits > 0) {
110
0
    const char *preverse = p - 1;
111
0
    int32_t trailing_zeros = 0;
112
0
    while ((*preverse == '0') || (*preverse == '.')) {
113
0
      if(*preverse == '0') { trailing_zeros++; };
114
0
      --preverse;
115
0
    }
116
0
    answer.decimal_point += int32_t(answer.num_digits);
117
0
    answer.num_digits -= uint32_t(trailing_zeros);
118
0
  }
119
0
  if(answer.num_digits > max_digits ) {
120
0
    answer.num_digits = max_digits;
121
0
    answer.truncated = true;
122
0
  }
123
0
  if (('e' == *p) || ('E' == *p)) {
124
0
    ++p;
125
0
    bool neg_exp = false;
126
0
    if ('-' == *p) {
127
0
      neg_exp = true;
128
0
      ++p;
129
0
    } else if ('+' == *p) {
130
0
      ++p;
131
0
    }
132
0
    int32_t exp_number = 0; // exponential part
133
0
    while (is_integer(*p)) {
134
0
      uint8_t digit = uint8_t(*p - '0');
135
0
      if (exp_number < 0x10000) {
136
0
        exp_number = 10 * exp_number + digit;
137
0
      }
138
0
      ++p;
139
0
    }
140
0
    answer.decimal_point += (neg_exp ? -exp_number : exp_number);
141
0
  }
142
0
  return answer;
143
0
}
144
145
// This should always succeed since it follows a call to parse_number.
146
// Will not read at or beyond the "end" pointer.
147
0
decimal parse_decimal(const char *&p, const char * end) noexcept {
148
0
  decimal answer;
149
0
  answer.num_digits = 0;
150
0
  answer.decimal_point = 0;
151
0
  answer.truncated = false;
152
0
  if(p == end) { return answer; } // should never happen
153
0
  answer.negative = (*p == '-');
154
0
  if ((*p == '-') || (*p == '+')) {
155
0
    ++p;
156
0
  }
157
158
0
  while ((p != end) && (*p == '0')) {
159
0
    ++p;
160
0
  }
161
0
  while ((p != end) && is_integer(*p)) {
162
0
    if (answer.num_digits < max_digits) {
163
0
      answer.digits[answer.num_digits] = uint8_t(*p - '0');
164
0
    }
165
0
    answer.num_digits++;
166
0
    ++p;
167
0
  }
168
0
  if ((p != end) && (*p == '.')) {
169
0
    ++p;
170
0
    if(p == end) { return answer; } // should never happen
171
0
    const char *first_after_period = p;
172
    // if we have not yet encountered a zero, we have to skip it as well
173
0
    if (answer.num_digits == 0) {
174
      // skip zeros
175
0
      while (*p == '0') {
176
0
        ++p;
177
0
      }
178
0
    }
179
0
    while ((p != end) && is_integer(*p)) {
180
0
      if (answer.num_digits < max_digits) {
181
0
        answer.digits[answer.num_digits] = uint8_t(*p - '0');
182
0
      }
183
0
      answer.num_digits++;
184
0
      ++p;
185
0
    }
186
0
    answer.decimal_point = int32_t(first_after_period - p);
187
0
  }
188
0
  if(answer.num_digits > 0) {
189
0
    const char *preverse = p - 1;
190
0
    int32_t trailing_zeros = 0;
191
0
    while ((*preverse == '0') || (*preverse == '.')) {
192
0
      if(*preverse == '0') { trailing_zeros++; };
193
0
      --preverse;
194
0
    }
195
0
    answer.decimal_point += int32_t(answer.num_digits);
196
0
    answer.num_digits -= uint32_t(trailing_zeros);
197
0
  }
198
0
  if(answer.num_digits > max_digits ) {
199
0
    answer.num_digits = max_digits;
200
0
    answer.truncated = true;
201
0
  }
202
0
  if ((p != end) && (('e' == *p) || ('E' == *p))) {
203
0
    ++p;
204
0
    if(p == end) { return answer; } // should never happen
205
0
    bool neg_exp = false;
206
0
    if ('-' == *p) {
207
0
      neg_exp = true;
208
0
      ++p;
209
0
    } else if ('+' == *p) {
210
0
      ++p;
211
0
    }
212
0
    int32_t exp_number = 0; // exponential part
213
0
    while ((p != end) && is_integer(*p)) {
214
0
      uint8_t digit = uint8_t(*p - '0');
215
0
      if (exp_number < 0x10000) {
216
0
        exp_number = 10 * exp_number + digit;
217
0
      }
218
0
      ++p;
219
0
    }
220
0
    answer.decimal_point += (neg_exp ? -exp_number : exp_number);
221
0
  }
222
0
  return answer;
223
0
}
224
225
namespace {
226
227
// remove all final zeroes
228
0
inline void trim(decimal &h) {
229
0
  while ((h.num_digits > 0) && (h.digits[h.num_digits - 1] == 0)) {
230
0
    h.num_digits--;
231
0
  }
232
0
}
233
234
0
uint32_t number_of_digits_decimal_left_shift(decimal &h, uint32_t shift) {
235
0
  shift &= 63;
236
0
  const static uint16_t number_of_digits_decimal_left_shift_table[65] = {
237
0
      0x0000, 0x0800, 0x0801, 0x0803, 0x1006, 0x1009, 0x100D, 0x1812, 0x1817,
238
0
      0x181D, 0x2024, 0x202B, 0x2033, 0x203C, 0x2846, 0x2850, 0x285B, 0x3067,
239
0
      0x3073, 0x3080, 0x388E, 0x389C, 0x38AB, 0x38BB, 0x40CC, 0x40DD, 0x40EF,
240
0
      0x4902, 0x4915, 0x4929, 0x513E, 0x5153, 0x5169, 0x5180, 0x5998, 0x59B0,
241
0
      0x59C9, 0x61E3, 0x61FD, 0x6218, 0x6A34, 0x6A50, 0x6A6D, 0x6A8B, 0x72AA,
242
0
      0x72C9, 0x72E9, 0x7B0A, 0x7B2B, 0x7B4D, 0x8370, 0x8393, 0x83B7, 0x83DC,
243
0
      0x8C02, 0x8C28, 0x8C4F, 0x9477, 0x949F, 0x94C8, 0x9CF2, 0x051C, 0x051C,
244
0
      0x051C, 0x051C,
245
0
  };
246
0
  uint32_t x_a = number_of_digits_decimal_left_shift_table[shift];
247
0
  uint32_t x_b = number_of_digits_decimal_left_shift_table[shift + 1];
248
0
  uint32_t num_new_digits = x_a >> 11;
249
0
  uint32_t pow5_a = 0x7FF & x_a;
250
0
  uint32_t pow5_b = 0x7FF & x_b;
251
0
  const static uint8_t
252
0
      number_of_digits_decimal_left_shift_table_powers_of_5[0x051C] = {
253
0
          5, 2, 5, 1, 2, 5, 6, 2, 5, 3, 1, 2, 5, 1, 5, 6, 2, 5, 7, 8, 1, 2, 5,
254
0
          3, 9, 0, 6, 2, 5, 1, 9, 5, 3, 1, 2, 5, 9, 7, 6, 5, 6, 2, 5, 4, 8, 8,
255
0
          2, 8, 1, 2, 5, 2, 4, 4, 1, 4, 0, 6, 2, 5, 1, 2, 2, 0, 7, 0, 3, 1, 2,
256
0
          5, 6, 1, 0, 3, 5, 1, 5, 6, 2, 5, 3, 0, 5, 1, 7, 5, 7, 8, 1, 2, 5, 1,
257
0
          5, 2, 5, 8, 7, 8, 9, 0, 6, 2, 5, 7, 6, 2, 9, 3, 9, 4, 5, 3, 1, 2, 5,
258
0
          3, 8, 1, 4, 6, 9, 7, 2, 6, 5, 6, 2, 5, 1, 9, 0, 7, 3, 4, 8, 6, 3, 2,
259
0
          8, 1, 2, 5, 9, 5, 3, 6, 7, 4, 3, 1, 6, 4, 0, 6, 2, 5, 4, 7, 6, 8, 3,
260
0
          7, 1, 5, 8, 2, 0, 3, 1, 2, 5, 2, 3, 8, 4, 1, 8, 5, 7, 9, 1, 0, 1, 5,
261
0
          6, 2, 5, 1, 1, 9, 2, 0, 9, 2, 8, 9, 5, 5, 0, 7, 8, 1, 2, 5, 5, 9, 6,
262
0
          0, 4, 6, 4, 4, 7, 7, 5, 3, 9, 0, 6, 2, 5, 2, 9, 8, 0, 2, 3, 2, 2, 3,
263
0
          8, 7, 6, 9, 5, 3, 1, 2, 5, 1, 4, 9, 0, 1, 1, 6, 1, 1, 9, 3, 8, 4, 7,
264
0
          6, 5, 6, 2, 5, 7, 4, 5, 0, 5, 8, 0, 5, 9, 6, 9, 2, 3, 8, 2, 8, 1, 2,
265
0
          5, 3, 7, 2, 5, 2, 9, 0, 2, 9, 8, 4, 6, 1, 9, 1, 4, 0, 6, 2, 5, 1, 8,
266
0
          6, 2, 6, 4, 5, 1, 4, 9, 2, 3, 0, 9, 5, 7, 0, 3, 1, 2, 5, 9, 3, 1, 3,
267
0
          2, 2, 5, 7, 4, 6, 1, 5, 4, 7, 8, 5, 1, 5, 6, 2, 5, 4, 6, 5, 6, 6, 1,
268
0
          2, 8, 7, 3, 0, 7, 7, 3, 9, 2, 5, 7, 8, 1, 2, 5, 2, 3, 2, 8, 3, 0, 6,
269
0
          4, 3, 6, 5, 3, 8, 6, 9, 6, 2, 8, 9, 0, 6, 2, 5, 1, 1, 6, 4, 1, 5, 3,
270
0
          2, 1, 8, 2, 6, 9, 3, 4, 8, 1, 4, 4, 5, 3, 1, 2, 5, 5, 8, 2, 0, 7, 6,
271
0
          6, 0, 9, 1, 3, 4, 6, 7, 4, 0, 7, 2, 2, 6, 5, 6, 2, 5, 2, 9, 1, 0, 3,
272
0
          8, 3, 0, 4, 5, 6, 7, 3, 3, 7, 0, 3, 6, 1, 3, 2, 8, 1, 2, 5, 1, 4, 5,
273
0
          5, 1, 9, 1, 5, 2, 2, 8, 3, 6, 6, 8, 5, 1, 8, 0, 6, 6, 4, 0, 6, 2, 5,
274
0
          7, 2, 7, 5, 9, 5, 7, 6, 1, 4, 1, 8, 3, 4, 2, 5, 9, 0, 3, 3, 2, 0, 3,
275
0
          1, 2, 5, 3, 6, 3, 7, 9, 7, 8, 8, 0, 7, 0, 9, 1, 7, 1, 2, 9, 5, 1, 6,
276
0
          6, 0, 1, 5, 6, 2, 5, 1, 8, 1, 8, 9, 8, 9, 4, 0, 3, 5, 4, 5, 8, 5, 6,
277
0
          4, 7, 5, 8, 3, 0, 0, 7, 8, 1, 2, 5, 9, 0, 9, 4, 9, 4, 7, 0, 1, 7, 7,
278
0
          2, 9, 2, 8, 2, 3, 7, 9, 1, 5, 0, 3, 9, 0, 6, 2, 5, 4, 5, 4, 7, 4, 7,
279
0
          3, 5, 0, 8, 8, 6, 4, 6, 4, 1, 1, 8, 9, 5, 7, 5, 1, 9, 5, 3, 1, 2, 5,
280
0
          2, 2, 7, 3, 7, 3, 6, 7, 5, 4, 4, 3, 2, 3, 2, 0, 5, 9, 4, 7, 8, 7, 5,
281
0
          9, 7, 6, 5, 6, 2, 5, 1, 1, 3, 6, 8, 6, 8, 3, 7, 7, 2, 1, 6, 1, 6, 0,
282
0
          2, 9, 7, 3, 9, 3, 7, 9, 8, 8, 2, 8, 1, 2, 5, 5, 6, 8, 4, 3, 4, 1, 8,
283
0
          8, 6, 0, 8, 0, 8, 0, 1, 4, 8, 6, 9, 6, 8, 9, 9, 4, 1, 4, 0, 6, 2, 5,
284
0
          2, 8, 4, 2, 1, 7, 0, 9, 4, 3, 0, 4, 0, 4, 0, 0, 7, 4, 3, 4, 8, 4, 4,
285
0
          9, 7, 0, 7, 0, 3, 1, 2, 5, 1, 4, 2, 1, 0, 8, 5, 4, 7, 1, 5, 2, 0, 2,
286
0
          0, 0, 3, 7, 1, 7, 4, 2, 2, 4, 8, 5, 3, 5, 1, 5, 6, 2, 5, 7, 1, 0, 5,
287
0
          4, 2, 7, 3, 5, 7, 6, 0, 1, 0, 0, 1, 8, 5, 8, 7, 1, 1, 2, 4, 2, 6, 7,
288
0
          5, 7, 8, 1, 2, 5, 3, 5, 5, 2, 7, 1, 3, 6, 7, 8, 8, 0, 0, 5, 0, 0, 9,
289
0
          2, 9, 3, 5, 5, 6, 2, 1, 3, 3, 7, 8, 9, 0, 6, 2, 5, 1, 7, 7, 6, 3, 5,
290
0
          6, 8, 3, 9, 4, 0, 0, 2, 5, 0, 4, 6, 4, 6, 7, 7, 8, 1, 0, 6, 6, 8, 9,
291
0
          4, 5, 3, 1, 2, 5, 8, 8, 8, 1, 7, 8, 4, 1, 9, 7, 0, 0, 1, 2, 5, 2, 3,
292
0
          2, 3, 3, 8, 9, 0, 5, 3, 3, 4, 4, 7, 2, 6, 5, 6, 2, 5, 4, 4, 4, 0, 8,
293
0
          9, 2, 0, 9, 8, 5, 0, 0, 6, 2, 6, 1, 6, 1, 6, 9, 4, 5, 2, 6, 6, 7, 2,
294
0
          3, 6, 3, 2, 8, 1, 2, 5, 2, 2, 2, 0, 4, 4, 6, 0, 4, 9, 2, 5, 0, 3, 1,
295
0
          3, 0, 8, 0, 8, 4, 7, 2, 6, 3, 3, 3, 6, 1, 8, 1, 6, 4, 0, 6, 2, 5, 1,
296
0
          1, 1, 0, 2, 2, 3, 0, 2, 4, 6, 2, 5, 1, 5, 6, 5, 4, 0, 4, 2, 3, 6, 3,
297
0
          1, 6, 6, 8, 0, 9, 0, 8, 2, 0, 3, 1, 2, 5, 5, 5, 5, 1, 1, 1, 5, 1, 2,
298
0
          3, 1, 2, 5, 7, 8, 2, 7, 0, 2, 1, 1, 8, 1, 5, 8, 3, 4, 0, 4, 5, 4, 1,
299
0
          0, 1, 5, 6, 2, 5, 2, 7, 7, 5, 5, 5, 7, 5, 6, 1, 5, 6, 2, 8, 9, 1, 3,
300
0
          5, 1, 0, 5, 9, 0, 7, 9, 1, 7, 0, 2, 2, 7, 0, 5, 0, 7, 8, 1, 2, 5, 1,
301
0
          3, 8, 7, 7, 7, 8, 7, 8, 0, 7, 8, 1, 4, 4, 5, 6, 7, 5, 5, 2, 9, 5, 3,
302
0
          9, 5, 8, 5, 1, 1, 3, 5, 2, 5, 3, 9, 0, 6, 2, 5, 6, 9, 3, 8, 8, 9, 3,
303
0
          9, 0, 3, 9, 0, 7, 2, 2, 8, 3, 7, 7, 6, 4, 7, 6, 9, 7, 9, 2, 5, 5, 6,
304
0
          7, 6, 2, 6, 9, 5, 3, 1, 2, 5, 3, 4, 6, 9, 4, 4, 6, 9, 5, 1, 9, 5, 3,
305
0
          6, 1, 4, 1, 8, 8, 8, 2, 3, 8, 4, 8, 9, 6, 2, 7, 8, 3, 8, 1, 3, 4, 7,
306
0
          6, 5, 6, 2, 5, 1, 7, 3, 4, 7, 2, 3, 4, 7, 5, 9, 7, 6, 8, 0, 7, 0, 9,
307
0
          4, 4, 1, 1, 9, 2, 4, 4, 8, 1, 3, 9, 1, 9, 0, 6, 7, 3, 8, 2, 8, 1, 2,
308
0
          5, 8, 6, 7, 3, 6, 1, 7, 3, 7, 9, 8, 8, 4, 0, 3, 5, 4, 7, 2, 0, 5, 9,
309
0
          6, 2, 2, 4, 0, 6, 9, 5, 9, 5, 3, 3, 6, 9, 1, 4, 0, 6, 2, 5,
310
0
      };
311
0
  const uint8_t *pow5 =
312
0
      &number_of_digits_decimal_left_shift_table_powers_of_5[pow5_a];
313
0
  uint32_t i = 0;
314
0
  uint32_t n = pow5_b - pow5_a;
315
0
  for (; i < n; i++) {
316
0
    if (i >= h.num_digits) {
317
0
      return num_new_digits - 1;
318
0
    } else if (h.digits[i] == pow5[i]) {
319
0
      continue;
320
0
    } else if (h.digits[i] < pow5[i]) {
321
0
      return num_new_digits - 1;
322
0
    } else {
323
0
      return num_new_digits;
324
0
    }
325
0
  }
326
0
  return num_new_digits;
327
0
}
328
329
} // end of anonymous namespace
330
331
0
uint64_t round(decimal &h) {
332
0
  if ((h.num_digits == 0) || (h.decimal_point < 0)) {
333
0
    return 0;
334
0
  } else if (h.decimal_point > 18) {
335
0
    return UINT64_MAX;
336
0
  }
337
  // at this point, we know that h.decimal_point >= 0
338
0
  uint32_t dp = uint32_t(h.decimal_point);
339
0
  uint64_t n = 0;
340
0
  for (uint32_t i = 0; i < dp; i++) {
341
0
    n = (10 * n) + ((i < h.num_digits) ? h.digits[i] : 0);
342
0
  }
343
0
  bool round_up = false;
344
0
  if (dp < h.num_digits) {
345
0
    round_up = h.digits[dp] >= 5; // normally, we round up
346
    // but we may need to round to even!
347
0
    if ((h.digits[dp] == 5) && (dp + 1 == h.num_digits)) {
348
0
      round_up = h.truncated || ((dp > 0) && (1 & h.digits[dp - 1]));
349
0
    }
350
0
  }
351
0
  if (round_up) {
352
0
    n++;
353
0
  }
354
0
  return n;
355
0
}
356
357
// computes h * 2^-shift
358
0
void decimal_left_shift(decimal &h, uint32_t shift) {
359
0
  if (h.num_digits == 0) {
360
0
    return;
361
0
  }
362
0
  uint32_t num_new_digits = number_of_digits_decimal_left_shift(h, shift);
363
0
  int32_t read_index = int32_t(h.num_digits - 1);
364
0
  uint32_t write_index = h.num_digits - 1 + num_new_digits;
365
0
  uint64_t n = 0;
366
367
0
  while (read_index >= 0) {
368
0
    n += uint64_t(h.digits[read_index]) << shift;
369
0
    uint64_t quotient = n / 10;
370
0
    uint64_t remainder = n - (10 * quotient);
371
0
    if (write_index < max_digits) {
372
0
      h.digits[write_index] = uint8_t(remainder);
373
0
    } else if (remainder > 0) {
374
0
      h.truncated = true;
375
0
    }
376
0
    n = quotient;
377
0
    write_index--;
378
0
    read_index--;
379
0
  }
380
0
  while (n > 0) {
381
0
    uint64_t quotient = n / 10;
382
0
    uint64_t remainder = n - (10 * quotient);
383
0
    if (write_index < max_digits) {
384
0
      h.digits[write_index] = uint8_t(remainder);
385
0
    } else if (remainder > 0) {
386
0
      h.truncated = true;
387
0
    }
388
0
    n = quotient;
389
0
    write_index--;
390
0
  }
391
0
  h.num_digits += num_new_digits;
392
0
  if (h.num_digits > max_digits) {
393
0
    h.num_digits = max_digits;
394
0
  }
395
0
  h.decimal_point += int32_t(num_new_digits);
396
0
  trim(h);
397
0
}
398
399
// computes h * 2^shift
400
0
void decimal_right_shift(decimal &h, uint32_t shift) {
401
0
  uint32_t read_index = 0;
402
0
  uint32_t write_index = 0;
403
404
0
  uint64_t n = 0;
405
406
0
  while ((n >> shift) == 0) {
407
0
    if (read_index < h.num_digits) {
408
0
      n = (10 * n) + h.digits[read_index++];
409
0
    } else if (n == 0) {
410
0
      return;
411
0
    } else {
412
0
      while ((n >> shift) == 0) {
413
0
        n = 10 * n;
414
0
        read_index++;
415
0
      }
416
0
      break;
417
0
    }
418
0
  }
419
0
  h.decimal_point -= int32_t(read_index - 1);
420
0
  if (h.decimal_point < -decimal_point_range) { // it is zero
421
0
    h.num_digits = 0;
422
0
    h.decimal_point = 0;
423
0
    h.negative = false;
424
0
    h.truncated = false;
425
0
    return;
426
0
  }
427
0
  uint64_t mask = (uint64_t(1) << shift) - 1;
428
0
  while (read_index < h.num_digits) {
429
0
    uint8_t new_digit = uint8_t(n >> shift);
430
0
    n = (10 * (n & mask)) + h.digits[read_index++];
431
0
    h.digits[write_index++] = new_digit;
432
0
  }
433
0
  while (n > 0) {
434
0
    uint8_t new_digit = uint8_t(n >> shift);
435
0
    n = 10 * (n & mask);
436
0
    if (write_index < max_digits) {
437
0
      h.digits[write_index++] = new_digit;
438
0
    } else if (new_digit > 0) {
439
0
      h.truncated = true;
440
0
    }
441
0
  }
442
0
  h.num_digits = write_index;
443
0
  trim(h);
444
0
}
445
446
0
template <typename binary> adjusted_mantissa compute_float(decimal &d) {
447
0
  adjusted_mantissa answer;
448
0
  if (d.num_digits == 0) {
449
    // should be zero
450
0
    answer.power2 = 0;
451
0
    answer.mantissa = 0;
452
0
    return answer;
453
0
  }
454
  // At this point, going further, we can assume that d.num_digits > 0.
455
  // We want to guard against excessive decimal point values because
456
  // they can result in long running times. Indeed, we do
457
  // shifts by at most 60 bits. We have that log(10**400)/log(2**60) ~= 22
458
  // which is fine, but log(10**299995)/log(2**60) ~= 16609 which is not
459
  // fine (runs for a long time).
460
  //
461
0
  if(d.decimal_point < -324) {
462
    // We have something smaller than 1e-324 which is always zero
463
    // in binary64 and binary32.
464
    // It should be zero.
465
0
    answer.power2 = 0;
466
0
    answer.mantissa = 0;
467
0
    return answer;
468
0
  } else if(d.decimal_point >= 310) {
469
    // We have something at least as large as 0.1e310 which is
470
    // always infinite.
471
0
    answer.power2 = binary::infinite_power();
472
0
    answer.mantissa = 0;
473
0
    return answer;
474
0
  }
475
476
0
  static const uint32_t max_shift = 60;
477
0
  static const uint32_t num_powers = 19;
478
0
  static const uint8_t powers[19] = {
479
0
      0,  3,  6,  9,  13, 16, 19, 23, 26, 29, //
480
0
      33, 36, 39, 43, 46, 49, 53, 56, 59,     //
481
0
  };
482
0
  int32_t exp2 = 0;
483
0
  while (d.decimal_point > 0) {
484
0
    uint32_t n = uint32_t(d.decimal_point);
485
0
    uint32_t shift = (n < num_powers) ? powers[n] : max_shift;
486
0
    decimal_right_shift(d, shift);
487
0
    if (d.decimal_point < -decimal_point_range) {
488
      // should be zero
489
0
      answer.power2 = 0;
490
0
      answer.mantissa = 0;
491
0
      return answer;
492
0
    }
493
0
    exp2 += int32_t(shift);
494
0
  }
495
  // We shift left toward [1/2 ... 1].
496
0
  while (d.decimal_point <= 0) {
497
0
    uint32_t shift;
498
0
    if (d.decimal_point == 0) {
499
0
      if (d.digits[0] >= 5) {
500
0
        break;
501
0
      }
502
0
      shift = (d.digits[0] < 2) ? 2 : 1;
503
0
    } else {
504
0
      uint32_t n = uint32_t(-d.decimal_point);
505
0
      shift = (n < num_powers) ? powers[n] : max_shift;
506
0
    }
507
0
    decimal_left_shift(d, shift);
508
0
    if (d.decimal_point > decimal_point_range) {
509
      // we want to get infinity:
510
0
      answer.power2 = 0xFF;
511
0
      answer.mantissa = 0;
512
0
      return answer;
513
0
    }
514
0
    exp2 -= int32_t(shift);
515
0
  }
516
  // We are now in the range [1/2 ... 1] but the binary format uses [1 ... 2].
517
0
  exp2--;
518
0
  constexpr int32_t minimum_exponent = binary::minimum_exponent();
519
0
  while ((minimum_exponent + 1) > exp2) {
520
0
    uint32_t n = uint32_t((minimum_exponent + 1) - exp2);
521
0
    if (n > max_shift) {
522
0
      n = max_shift;
523
0
    }
524
0
    decimal_right_shift(d, n);
525
0
    exp2 += int32_t(n);
526
0
  }
527
0
  if ((exp2 - minimum_exponent) >= binary::infinite_power()) {
528
0
    answer.power2 = binary::infinite_power();
529
0
    answer.mantissa = 0;
530
0
    return answer;
531
0
  }
532
533
0
  const int mantissa_size_in_bits = binary::mantissa_explicit_bits() + 1;
534
0
  decimal_left_shift(d, mantissa_size_in_bits);
535
536
0
  uint64_t mantissa = round(d);
537
  // It is possible that we have an overflow, in which case we need
538
  // to shift back.
539
0
  if (mantissa >= (uint64_t(1) << mantissa_size_in_bits)) {
540
0
    decimal_right_shift(d, 1);
541
0
    exp2 += 1;
542
0
    mantissa = round(d);
543
0
    if ((exp2 - minimum_exponent) >= binary::infinite_power()) {
544
0
      answer.power2 = binary::infinite_power();
545
0
      answer.mantissa = 0;
546
0
      return answer;
547
0
    }
548
0
  }
549
0
  answer.power2 = exp2 - binary::minimum_exponent();
550
0
  if (mantissa < (uint64_t(1) << binary::mantissa_explicit_bits())) {
551
0
    answer.power2--;
552
0
  }
553
0
  answer.mantissa =
554
0
      mantissa & ((uint64_t(1) << binary::mantissa_explicit_bits()) - 1);
555
0
  return answer;
556
0
}
557
558
template <typename binary>
559
0
adjusted_mantissa parse_long_mantissa(const char *first) {
560
0
  decimal d = parse_decimal(first);
561
0
  return compute_float<binary>(d);
562
0
}
563
564
template <typename binary>
565
0
adjusted_mantissa parse_long_mantissa(const char *first, const char *end) {
566
0
  decimal d = parse_decimal(first, end);
567
0
  return compute_float<binary>(d);
568
0
}
569
570
0
double from_chars(const char *first) noexcept {
571
0
  bool negative = first[0] == '-';
572
0
  if (negative) {
573
0
    first++;
574
0
  }
575
0
  adjusted_mantissa am = parse_long_mantissa<binary_format<double>>(first);
576
0
  uint64_t word = am.mantissa;
577
0
  word |= uint64_t(am.power2)
578
0
          << binary_format<double>::mantissa_explicit_bits();
579
0
  word = negative ? word | (uint64_t(1) << binary_format<double>::sign_index())
580
0
                  : word;
581
0
  double value;
582
0
  std::memcpy(&value, &word, sizeof(double));
583
0
  return value;
584
0
}
585
586
587
0
double from_chars(const char *first, const char *end) noexcept {
588
0
  bool negative = first[0] == '-';
589
0
  if (negative) {
590
0
    first++;
591
0
  }
592
0
  adjusted_mantissa am = parse_long_mantissa<binary_format<double>>(first, end);
593
0
  uint64_t word = am.mantissa;
594
0
  word |= uint64_t(am.power2)
595
0
          << binary_format<double>::mantissa_explicit_bits();
596
0
  word = negative ? word | (uint64_t(1) << binary_format<double>::sign_index())
597
0
                  : word;
598
0
  double value;
599
0
  std::memcpy(&value, &word, sizeof(double));
600
0
  return value;
601
0
}
602
603
} // internal
604
} // simdjson
605
606
#endif // SIMDJSON_SRC_FROM_CHARS_CPP