1 #pragma once
2
3 #include <array> // array
4 #include <cmath> // signbit, isfinite
5 #include <cstdint> // intN_t, uintN_t
6 #include <cstring> // memcpy, memmove
7 #include <limits> // numeric_limits
8 #include <type_traits> // conditional
9
10 #include <nlohmann/detail/macro_scope.hpp>
11
12 namespace nlohmann
13 {
14 namespace detail
15 {
16
17 /*!
18 @brief implements the Grisu2 algorithm for binary to decimal floating-point
19 conversion.
20
21 This implementation is a slightly modified version of the reference
22 implementation which may be obtained from
23 http://florian.loitsch.com/publications (bench.tar.gz).
24
25 The code is distributed under the MIT license, Copyright (c) 2009 Florian Loitsch.
26
27 For a detailed description of the algorithm see:
28
29 [1] Loitsch, "Printing Floating-Point Numbers Quickly and Accurately with
30 Integers", Proceedings of the ACM SIGPLAN 2010 Conference on Programming
31 Language Design and Implementation, PLDI 2010
32 [2] Burger, Dybvig, "Printing Floating-Point Numbers Quickly and Accurately",
33 Proceedings of the ACM SIGPLAN 1996 Conference on Programming Language
34 Design and Implementation, PLDI 1996
35 */
36 namespace dtoa_impl
37 {
38
39 template<typename Target, typename Source>
40 Target reinterpret_bits(const Source source)
41 {
42 static_assert(sizeof(Target) == sizeof(Source), "size mismatch");
43
44 Target target;
45 std::memcpy(&target, &source, sizeof(Source));
46 return target;
47 }
48
49 struct diyfp // f * 2^e
50 {
51 static constexpr int kPrecision = 64; // = q
52
53 std::uint64_t f = 0;
54 int e = 0;
55
diyfpnlohmann::detail::dtoa_impl::diyfp56 constexpr diyfp(std::uint64_t f_, int e_) noexcept : f(f_), e(e_) {}
57
58 /*!
59 @brief returns x - y
60 @pre x.e == y.e and x.f >= y.f
61 */
subnlohmann::detail::dtoa_impl::diyfp62 static diyfp sub(const diyfp& x, const diyfp& y) noexcept
63 {
64 JSON_ASSERT(x.e == y.e);
65 JSON_ASSERT(x.f >= y.f);
66
67 return {x.f - y.f, x.e};
68 }
69
70 /*!
71 @brief returns x * y
72 @note The result is rounded. (Only the upper q bits are returned.)
73 */
mulnlohmann::detail::dtoa_impl::diyfp74 static diyfp mul(const diyfp& x, const diyfp& y) noexcept
75 {
76 static_assert(kPrecision == 64, "internal error");
77
78 // Computes:
79 // f = round((x.f * y.f) / 2^q)
80 // e = x.e + y.e + q
81
82 // Emulate the 64-bit * 64-bit multiplication:
83 //
84 // p = u * v
85 // = (u_lo + 2^32 u_hi) (v_lo + 2^32 v_hi)
86 // = (u_lo v_lo ) + 2^32 ((u_lo v_hi ) + (u_hi v_lo )) + 2^64 (u_hi v_hi )
87 // = (p0 ) + 2^32 ((p1 ) + (p2 )) + 2^64 (p3 )
88 // = (p0_lo + 2^32 p0_hi) + 2^32 ((p1_lo + 2^32 p1_hi) + (p2_lo + 2^32 p2_hi)) + 2^64 (p3 )
89 // = (p0_lo ) + 2^32 (p0_hi + p1_lo + p2_lo ) + 2^64 (p1_hi + p2_hi + p3)
90 // = (p0_lo ) + 2^32 (Q ) + 2^64 (H )
91 // = (p0_lo ) + 2^32 (Q_lo + 2^32 Q_hi ) + 2^64 (H )
92 //
93 // (Since Q might be larger than 2^32 - 1)
94 //
95 // = (p0_lo + 2^32 Q_lo) + 2^64 (Q_hi + H)
96 //
97 // (Q_hi + H does not overflow a 64-bit int)
98 //
99 // = p_lo + 2^64 p_hi
100
101 const std::uint64_t u_lo = x.f & 0xFFFFFFFFu;
102 const std::uint64_t u_hi = x.f >> 32u;
103 const std::uint64_t v_lo = y.f & 0xFFFFFFFFu;
104 const std::uint64_t v_hi = y.f >> 32u;
105
106 const std::uint64_t p0 = u_lo * v_lo;
107 const std::uint64_t p1 = u_lo * v_hi;
108 const std::uint64_t p2 = u_hi * v_lo;
109 const std::uint64_t p3 = u_hi * v_hi;
110
111 const std::uint64_t p0_hi = p0 >> 32u;
112 const std::uint64_t p1_lo = p1 & 0xFFFFFFFFu;
113 const std::uint64_t p1_hi = p1 >> 32u;
114 const std::uint64_t p2_lo = p2 & 0xFFFFFFFFu;
115 const std::uint64_t p2_hi = p2 >> 32u;
116
117 std::uint64_t Q = p0_hi + p1_lo + p2_lo;
118
119 // The full product might now be computed as
120 //
121 // p_hi = p3 + p2_hi + p1_hi + (Q >> 32)
122 // p_lo = p0_lo + (Q << 32)
123 //
124 // But in this particular case here, the full p_lo is not required.
125 // Effectively we only need to add the highest bit in p_lo to p_hi (and
126 // Q_hi + 1 does not overflow).
127
128 Q += std::uint64_t{1} << (64u - 32u - 1u); // round, ties up
129
130 const std::uint64_t h = p3 + p2_hi + p1_hi + (Q >> 32u);
131
132 return {h, x.e + y.e + 64};
133 }
134
135 /*!
136 @brief normalize x such that the significand is >= 2^(q-1)
137 @pre x.f != 0
138 */
normalizenlohmann::detail::dtoa_impl::diyfp139 static diyfp normalize(diyfp x) noexcept
140 {
141 JSON_ASSERT(x.f != 0);
142
143 while ((x.f >> 63u) == 0)
144 {
145 x.f <<= 1u;
146 x.e--;
147 }
148
149 return x;
150 }
151
152 /*!
153 @brief normalize x such that the result has the exponent E
154 @pre e >= x.e and the upper e - x.e bits of x.f must be zero.
155 */
normalize_tonlohmann::detail::dtoa_impl::diyfp156 static diyfp normalize_to(const diyfp& x, const int target_exponent) noexcept
157 {
158 const int delta = x.e - target_exponent;
159
160 JSON_ASSERT(delta >= 0);
161 JSON_ASSERT(((x.f << delta) >> delta) == x.f);
162
163 return {x.f << delta, target_exponent};
164 }
165 };
166
167 struct boundaries
168 {
169 diyfp w;
170 diyfp minus;
171 diyfp plus;
172 };
173
174 /*!
175 Compute the (normalized) diyfp representing the input number 'value' and its
176 boundaries.
177
178 @pre value must be finite and positive
179 */
180 template<typename FloatType>
compute_boundaries(FloatType value)181 boundaries compute_boundaries(FloatType value)
182 {
183 JSON_ASSERT(std::isfinite(value));
184 JSON_ASSERT(value > 0);
185
186 // Convert the IEEE representation into a diyfp.
187 //
188 // If v is denormal:
189 // value = 0.F * 2^(1 - bias) = ( F) * 2^(1 - bias - (p-1))
190 // If v is normalized:
191 // value = 1.F * 2^(E - bias) = (2^(p-1) + F) * 2^(E - bias - (p-1))
192
193 static_assert(std::numeric_limits<FloatType>::is_iec559,
194 "internal error: dtoa_short requires an IEEE-754 floating-point implementation");
195
196 constexpr int kPrecision = std::numeric_limits<FloatType>::digits; // = p (includes the hidden bit)
197 constexpr int kBias = std::numeric_limits<FloatType>::max_exponent - 1 + (kPrecision - 1);
198 constexpr int kMinExp = 1 - kBias;
199 constexpr std::uint64_t kHiddenBit = std::uint64_t{1} << (kPrecision - 1); // = 2^(p-1)
200
201 using bits_type = typename std::conditional<kPrecision == 24, std::uint32_t, std::uint64_t >::type;
202
203 const std::uint64_t bits = reinterpret_bits<bits_type>(value);
204 const std::uint64_t E = bits >> (kPrecision - 1);
205 const std::uint64_t F = bits & (kHiddenBit - 1);
206
207 const bool is_denormal = E == 0;
208 const diyfp v = is_denormal
209 ? diyfp(F, kMinExp)
210 : diyfp(F + kHiddenBit, static_cast<int>(E) - kBias);
211
212 // Compute the boundaries m- and m+ of the floating-point value
213 // v = f * 2^e.
214 //
215 // Determine v- and v+, the floating-point predecessor and successor if v,
216 // respectively.
217 //
218 // v- = v - 2^e if f != 2^(p-1) or e == e_min (A)
219 // = v - 2^(e-1) if f == 2^(p-1) and e > e_min (B)
220 //
221 // v+ = v + 2^e
222 //
223 // Let m- = (v- + v) / 2 and m+ = (v + v+) / 2. All real numbers _strictly_
224 // between m- and m+ round to v, regardless of how the input rounding
225 // algorithm breaks ties.
226 //
227 // ---+-------------+-------------+-------------+-------------+--- (A)
228 // v- m- v m+ v+
229 //
230 // -----------------+------+------+-------------+-------------+--- (B)
231 // v- m- v m+ v+
232
233 const bool lower_boundary_is_closer = F == 0 && E > 1;
234 const diyfp m_plus = diyfp(2 * v.f + 1, v.e - 1);
235 const diyfp m_minus = lower_boundary_is_closer
236 ? diyfp(4 * v.f - 1, v.e - 2) // (B)
237 : diyfp(2 * v.f - 1, v.e - 1); // (A)
238
239 // Determine the normalized w+ = m+.
240 const diyfp w_plus = diyfp::normalize(m_plus);
241
242 // Determine w- = m- such that e_(w-) = e_(w+).
243 const diyfp w_minus = diyfp::normalize_to(m_minus, w_plus.e);
244
245 return {diyfp::normalize(v), w_minus, w_plus};
246 }
247
248 // Given normalized diyfp w, Grisu needs to find a (normalized) cached
249 // power-of-ten c, such that the exponent of the product c * w = f * 2^e lies
250 // within a certain range [alpha, gamma] (Definition 3.2 from [1])
251 //
252 // alpha <= e = e_c + e_w + q <= gamma
253 //
254 // or
255 //
256 // f_c * f_w * 2^alpha <= f_c 2^(e_c) * f_w 2^(e_w) * 2^q
257 // <= f_c * f_w * 2^gamma
258 //
259 // Since c and w are normalized, i.e. 2^(q-1) <= f < 2^q, this implies
260 //
261 // 2^(q-1) * 2^(q-1) * 2^alpha <= c * w * 2^q < 2^q * 2^q * 2^gamma
262 //
263 // or
264 //
265 // 2^(q - 2 + alpha) <= c * w < 2^(q + gamma)
266 //
267 // The choice of (alpha,gamma) determines the size of the table and the form of
268 // the digit generation procedure. Using (alpha,gamma)=(-60,-32) works out well
269 // in practice:
270 //
271 // The idea is to cut the number c * w = f * 2^e into two parts, which can be
272 // processed independently: An integral part p1, and a fractional part p2:
273 //
274 // f * 2^e = ( (f div 2^-e) * 2^-e + (f mod 2^-e) ) * 2^e
275 // = (f div 2^-e) + (f mod 2^-e) * 2^e
276 // = p1 + p2 * 2^e
277 //
278 // The conversion of p1 into decimal form requires a series of divisions and
279 // modulos by (a power of) 10. These operations are faster for 32-bit than for
280 // 64-bit integers, so p1 should ideally fit into a 32-bit integer. This can be
281 // achieved by choosing
282 //
283 // -e >= 32 or e <= -32 := gamma
284 //
285 // In order to convert the fractional part
286 //
287 // p2 * 2^e = p2 / 2^-e = d[-1] / 10^1 + d[-2] / 10^2 + ...
288 //
289 // into decimal form, the fraction is repeatedly multiplied by 10 and the digits
290 // d[-i] are extracted in order:
291 //
292 // (10 * p2) div 2^-e = d[-1]
293 // (10 * p2) mod 2^-e = d[-2] / 10^1 + ...
294 //
295 // The multiplication by 10 must not overflow. It is sufficient to choose
296 //
297 // 10 * p2 < 16 * p2 = 2^4 * p2 <= 2^64.
298 //
299 // Since p2 = f mod 2^-e < 2^-e,
300 //
301 // -e <= 60 or e >= -60 := alpha
302
303 constexpr int kAlpha = -60;
304 constexpr int kGamma = -32;
305
306 struct cached_power // c = f * 2^e ~= 10^k
307 {
308 std::uint64_t f;
309 int e;
310 int k;
311 };
312
313 /*!
314 For a normalized diyfp w = f * 2^e, this function returns a (normalized) cached
315 power-of-ten c = f_c * 2^e_c, such that the exponent of the product w * c
316 satisfies (Definition 3.2 from [1])
317
318 alpha <= e_c + e + q <= gamma.
319 */
get_cached_power_for_binary_exponent(int e)320 inline cached_power get_cached_power_for_binary_exponent(int e)
321 {
322 // Now
323 //
324 // alpha <= e_c + e + q <= gamma (1)
325 // ==> f_c * 2^alpha <= c * 2^e * 2^q
326 //
327 // and since the c's are normalized, 2^(q-1) <= f_c,
328 //
329 // ==> 2^(q - 1 + alpha) <= c * 2^(e + q)
330 // ==> 2^(alpha - e - 1) <= c
331 //
332 // If c were an exact power of ten, i.e. c = 10^k, one may determine k as
333 //
334 // k = ceil( log_10( 2^(alpha - e - 1) ) )
335 // = ceil( (alpha - e - 1) * log_10(2) )
336 //
337 // From the paper:
338 // "In theory the result of the procedure could be wrong since c is rounded,
339 // and the computation itself is approximated [...]. In practice, however,
340 // this simple function is sufficient."
341 //
342 // For IEEE double precision floating-point numbers converted into
343 // normalized diyfp's w = f * 2^e, with q = 64,
344 //
345 // e >= -1022 (min IEEE exponent)
346 // -52 (p - 1)
347 // -52 (p - 1, possibly normalize denormal IEEE numbers)
348 // -11 (normalize the diyfp)
349 // = -1137
350 //
351 // and
352 //
353 // e <= +1023 (max IEEE exponent)
354 // -52 (p - 1)
355 // -11 (normalize the diyfp)
356 // = 960
357 //
358 // This binary exponent range [-1137,960] results in a decimal exponent
359 // range [-307,324]. One does not need to store a cached power for each
360 // k in this range. For each such k it suffices to find a cached power
361 // such that the exponent of the product lies in [alpha,gamma].
362 // This implies that the difference of the decimal exponents of adjacent
363 // table entries must be less than or equal to
364 //
365 // floor( (gamma - alpha) * log_10(2) ) = 8.
366 //
367 // (A smaller distance gamma-alpha would require a larger table.)
368
369 // NB:
370 // Actually this function returns c, such that -60 <= e_c + e + 64 <= -34.
371
372 constexpr int kCachedPowersMinDecExp = -300;
373 constexpr int kCachedPowersDecStep = 8;
374
375 static constexpr std::array<cached_power, 79> kCachedPowers =
376 {
377 {
378 { 0xAB70FE17C79AC6CA, -1060, -300 },
379 { 0xFF77B1FCBEBCDC4F, -1034, -292 },
380 { 0xBE5691EF416BD60C, -1007, -284 },
381 { 0x8DD01FAD907FFC3C, -980, -276 },
382 { 0xD3515C2831559A83, -954, -268 },
383 { 0x9D71AC8FADA6C9B5, -927, -260 },
384 { 0xEA9C227723EE8BCB, -901, -252 },
385 { 0xAECC49914078536D, -874, -244 },
386 { 0x823C12795DB6CE57, -847, -236 },
387 { 0xC21094364DFB5637, -821, -228 },
388 { 0x9096EA6F3848984F, -794, -220 },
389 { 0xD77485CB25823AC7, -768, -212 },
390 { 0xA086CFCD97BF97F4, -741, -204 },
391 { 0xEF340A98172AACE5, -715, -196 },
392 { 0xB23867FB2A35B28E, -688, -188 },
393 { 0x84C8D4DFD2C63F3B, -661, -180 },
394 { 0xC5DD44271AD3CDBA, -635, -172 },
395 { 0x936B9FCEBB25C996, -608, -164 },
396 { 0xDBAC6C247D62A584, -582, -156 },
397 { 0xA3AB66580D5FDAF6, -555, -148 },
398 { 0xF3E2F893DEC3F126, -529, -140 },
399 { 0xB5B5ADA8AAFF80B8, -502, -132 },
400 { 0x87625F056C7C4A8B, -475, -124 },
401 { 0xC9BCFF6034C13053, -449, -116 },
402 { 0x964E858C91BA2655, -422, -108 },
403 { 0xDFF9772470297EBD, -396, -100 },
404 { 0xA6DFBD9FB8E5B88F, -369, -92 },
405 { 0xF8A95FCF88747D94, -343, -84 },
406 { 0xB94470938FA89BCF, -316, -76 },
407 { 0x8A08F0F8BF0F156B, -289, -68 },
408 { 0xCDB02555653131B6, -263, -60 },
409 { 0x993FE2C6D07B7FAC, -236, -52 },
410 { 0xE45C10C42A2B3B06, -210, -44 },
411 { 0xAA242499697392D3, -183, -36 },
412 { 0xFD87B5F28300CA0E, -157, -28 },
413 { 0xBCE5086492111AEB, -130, -20 },
414 { 0x8CBCCC096F5088CC, -103, -12 },
415 { 0xD1B71758E219652C, -77, -4 },
416 { 0x9C40000000000000, -50, 4 },
417 { 0xE8D4A51000000000, -24, 12 },
418 { 0xAD78EBC5AC620000, 3, 20 },
419 { 0x813F3978F8940984, 30, 28 },
420 { 0xC097CE7BC90715B3, 56, 36 },
421 { 0x8F7E32CE7BEA5C70, 83, 44 },
422 { 0xD5D238A4ABE98068, 109, 52 },
423 { 0x9F4F2726179A2245, 136, 60 },
424 { 0xED63A231D4C4FB27, 162, 68 },
425 { 0xB0DE65388CC8ADA8, 189, 76 },
426 { 0x83C7088E1AAB65DB, 216, 84 },
427 { 0xC45D1DF942711D9A, 242, 92 },
428 { 0x924D692CA61BE758, 269, 100 },
429 { 0xDA01EE641A708DEA, 295, 108 },
430 { 0xA26DA3999AEF774A, 322, 116 },
431 { 0xF209787BB47D6B85, 348, 124 },
432 { 0xB454E4A179DD1877, 375, 132 },
433 { 0x865B86925B9BC5C2, 402, 140 },
434 { 0xC83553C5C8965D3D, 428, 148 },
435 { 0x952AB45CFA97A0B3, 455, 156 },
436 { 0xDE469FBD99A05FE3, 481, 164 },
437 { 0xA59BC234DB398C25, 508, 172 },
438 { 0xF6C69A72A3989F5C, 534, 180 },
439 { 0xB7DCBF5354E9BECE, 561, 188 },
440 { 0x88FCF317F22241E2, 588, 196 },
441 { 0xCC20CE9BD35C78A5, 614, 204 },
442 { 0x98165AF37B2153DF, 641, 212 },
443 { 0xE2A0B5DC971F303A, 667, 220 },
444 { 0xA8D9D1535CE3B396, 694, 228 },
445 { 0xFB9B7CD9A4A7443C, 720, 236 },
446 { 0xBB764C4CA7A44410, 747, 244 },
447 { 0x8BAB8EEFB6409C1A, 774, 252 },
448 { 0xD01FEF10A657842C, 800, 260 },
449 { 0x9B10A4E5E9913129, 827, 268 },
450 { 0xE7109BFBA19C0C9D, 853, 276 },
451 { 0xAC2820D9623BF429, 880, 284 },
452 { 0x80444B5E7AA7CF85, 907, 292 },
453 { 0xBF21E44003ACDD2D, 933, 300 },
454 { 0x8E679C2F5E44FF8F, 960, 308 },
455 { 0xD433179D9C8CB841, 986, 316 },
456 { 0x9E19DB92B4E31BA9, 1013, 324 },
457 }
458 };
459
460 // This computation gives exactly the same results for k as
461 // k = ceil((kAlpha - e - 1) * 0.30102999566398114)
462 // for |e| <= 1500, but doesn't require floating-point operations.
463 // NB: log_10(2) ~= 78913 / 2^18
464 JSON_ASSERT(e >= -1500);
465 JSON_ASSERT(e <= 1500);
466 const int f = kAlpha - e - 1;
467 const int k = (f * 78913) / (1 << 18) + static_cast<int>(f > 0);
468
469 const int index = (-kCachedPowersMinDecExp + k + (kCachedPowersDecStep - 1)) / kCachedPowersDecStep;
470 JSON_ASSERT(index >= 0);
471 JSON_ASSERT(static_cast<std::size_t>(index) < kCachedPowers.size());
472
473 const cached_power cached = kCachedPowers[static_cast<std::size_t>(index)];
474 JSON_ASSERT(kAlpha <= cached.e + e + 64);
475 JSON_ASSERT(kGamma >= cached.e + e + 64);
476
477 return cached;
478 }
479
480 /*!
481 For n != 0, returns k, such that pow10 := 10^(k-1) <= n < 10^k.
482 For n == 0, returns 1 and sets pow10 := 1.
483 */
find_largest_pow10(const std::uint32_t n,std::uint32_t & pow10)484 inline int find_largest_pow10(const std::uint32_t n, std::uint32_t& pow10)
485 {
486 // LCOV_EXCL_START
487 if (n >= 1000000000)
488 {
489 pow10 = 1000000000;
490 return 10;
491 }
492 // LCOV_EXCL_STOP
493 else if (n >= 100000000)
494 {
495 pow10 = 100000000;
496 return 9;
497 }
498 else if (n >= 10000000)
499 {
500 pow10 = 10000000;
501 return 8;
502 }
503 else if (n >= 1000000)
504 {
505 pow10 = 1000000;
506 return 7;
507 }
508 else if (n >= 100000)
509 {
510 pow10 = 100000;
511 return 6;
512 }
513 else if (n >= 10000)
514 {
515 pow10 = 10000;
516 return 5;
517 }
518 else if (n >= 1000)
519 {
520 pow10 = 1000;
521 return 4;
522 }
523 else if (n >= 100)
524 {
525 pow10 = 100;
526 return 3;
527 }
528 else if (n >= 10)
529 {
530 pow10 = 10;
531 return 2;
532 }
533 else
534 {
535 pow10 = 1;
536 return 1;
537 }
538 }
539
grisu2_round(char * buf,int len,std::uint64_t dist,std::uint64_t delta,std::uint64_t rest,std::uint64_t ten_k)540 inline void grisu2_round(char* buf, int len, std::uint64_t dist, std::uint64_t delta,
541 std::uint64_t rest, std::uint64_t ten_k)
542 {
543 JSON_ASSERT(len >= 1);
544 JSON_ASSERT(dist <= delta);
545 JSON_ASSERT(rest <= delta);
546 JSON_ASSERT(ten_k > 0);
547
548 // <--------------------------- delta ---->
549 // <---- dist --------->
550 // --------------[------------------+-------------------]--------------
551 // M- w M+
552 //
553 // ten_k
554 // <------>
555 // <---- rest ---->
556 // --------------[------------------+----+--------------]--------------
557 // w V
558 // = buf * 10^k
559 //
560 // ten_k represents a unit-in-the-last-place in the decimal representation
561 // stored in buf.
562 // Decrement buf by ten_k while this takes buf closer to w.
563
564 // The tests are written in this order to avoid overflow in unsigned
565 // integer arithmetic.
566
567 while (rest < dist
568 && delta - rest >= ten_k
569 && (rest + ten_k < dist || dist - rest > rest + ten_k - dist))
570 {
571 JSON_ASSERT(buf[len - 1] != '0');
572 buf[len - 1]--;
573 rest += ten_k;
574 }
575 }
576
577 /*!
578 Generates V = buffer * 10^decimal_exponent, such that M- <= V <= M+.
579 M- and M+ must be normalized and share the same exponent -60 <= e <= -32.
580 */
grisu2_digit_gen(char * buffer,int & length,int & decimal_exponent,diyfp M_minus,diyfp w,diyfp M_plus)581 inline void grisu2_digit_gen(char* buffer, int& length, int& decimal_exponent,
582 diyfp M_minus, diyfp w, diyfp M_plus)
583 {
584 static_assert(kAlpha >= -60, "internal error");
585 static_assert(kGamma <= -32, "internal error");
586
587 // Generates the digits (and the exponent) of a decimal floating-point
588 // number V = buffer * 10^decimal_exponent in the range [M-, M+]. The diyfp's
589 // w, M- and M+ share the same exponent e, which satisfies alpha <= e <= gamma.
590 //
591 // <--------------------------- delta ---->
592 // <---- dist --------->
593 // --------------[------------------+-------------------]--------------
594 // M- w M+
595 //
596 // Grisu2 generates the digits of M+ from left to right and stops as soon as
597 // V is in [M-,M+].
598
599 JSON_ASSERT(M_plus.e >= kAlpha);
600 JSON_ASSERT(M_plus.e <= kGamma);
601
602 std::uint64_t delta = diyfp::sub(M_plus, M_minus).f; // (significand of (M+ - M-), implicit exponent is e)
603 std::uint64_t dist = diyfp::sub(M_plus, w ).f; // (significand of (M+ - w ), implicit exponent is e)
604
605 // Split M+ = f * 2^e into two parts p1 and p2 (note: e < 0):
606 //
607 // M+ = f * 2^e
608 // = ((f div 2^-e) * 2^-e + (f mod 2^-e)) * 2^e
609 // = ((p1 ) * 2^-e + (p2 )) * 2^e
610 // = p1 + p2 * 2^e
611
612 const diyfp one(std::uint64_t{1} << -M_plus.e, M_plus.e);
613
614 auto p1 = static_cast<std::uint32_t>(M_plus.f >> -one.e); // p1 = f div 2^-e (Since -e >= 32, p1 fits into a 32-bit int.)
615 std::uint64_t p2 = M_plus.f & (one.f - 1); // p2 = f mod 2^-e
616
617 // 1)
618 //
619 // Generate the digits of the integral part p1 = d[n-1]...d[1]d[0]
620
621 JSON_ASSERT(p1 > 0);
622
623 std::uint32_t pow10;
624 const int k = find_largest_pow10(p1, pow10);
625
626 // 10^(k-1) <= p1 < 10^k, pow10 = 10^(k-1)
627 //
628 // p1 = (p1 div 10^(k-1)) * 10^(k-1) + (p1 mod 10^(k-1))
629 // = (d[k-1] ) * 10^(k-1) + (p1 mod 10^(k-1))
630 //
631 // M+ = p1 + p2 * 2^e
632 // = d[k-1] * 10^(k-1) + (p1 mod 10^(k-1)) + p2 * 2^e
633 // = d[k-1] * 10^(k-1) + ((p1 mod 10^(k-1)) * 2^-e + p2) * 2^e
634 // = d[k-1] * 10^(k-1) + ( rest) * 2^e
635 //
636 // Now generate the digits d[n] of p1 from left to right (n = k-1,...,0)
637 //
638 // p1 = d[k-1]...d[n] * 10^n + d[n-1]...d[0]
639 //
640 // but stop as soon as
641 //
642 // rest * 2^e = (d[n-1]...d[0] * 2^-e + p2) * 2^e <= delta * 2^e
643
644 int n = k;
645 while (n > 0)
646 {
647 // Invariants:
648 // M+ = buffer * 10^n + (p1 + p2 * 2^e) (buffer = 0 for n = k)
649 // pow10 = 10^(n-1) <= p1 < 10^n
650 //
651 const std::uint32_t d = p1 / pow10; // d = p1 div 10^(n-1)
652 const std::uint32_t r = p1 % pow10; // r = p1 mod 10^(n-1)
653 //
654 // M+ = buffer * 10^n + (d * 10^(n-1) + r) + p2 * 2^e
655 // = (buffer * 10 + d) * 10^(n-1) + (r + p2 * 2^e)
656 //
657 JSON_ASSERT(d <= 9);
658 buffer[length++] = static_cast<char>('0' + d); // buffer := buffer * 10 + d
659 //
660 // M+ = buffer * 10^(n-1) + (r + p2 * 2^e)
661 //
662 p1 = r;
663 n--;
664 //
665 // M+ = buffer * 10^n + (p1 + p2 * 2^e)
666 // pow10 = 10^n
667 //
668
669 // Now check if enough digits have been generated.
670 // Compute
671 //
672 // p1 + p2 * 2^e = (p1 * 2^-e + p2) * 2^e = rest * 2^e
673 //
674 // Note:
675 // Since rest and delta share the same exponent e, it suffices to
676 // compare the significands.
677 const std::uint64_t rest = (std::uint64_t{p1} << -one.e) + p2;
678 if (rest <= delta)
679 {
680 // V = buffer * 10^n, with M- <= V <= M+.
681
682 decimal_exponent += n;
683
684 // We may now just stop. But instead look if the buffer could be
685 // decremented to bring V closer to w.
686 //
687 // pow10 = 10^n is now 1 ulp in the decimal representation V.
688 // The rounding procedure works with diyfp's with an implicit
689 // exponent of e.
690 //
691 // 10^n = (10^n * 2^-e) * 2^e = ulp * 2^e
692 //
693 const std::uint64_t ten_n = std::uint64_t{pow10} << -one.e;
694 grisu2_round(buffer, length, dist, delta, rest, ten_n);
695
696 return;
697 }
698
699 pow10 /= 10;
700 //
701 // pow10 = 10^(n-1) <= p1 < 10^n
702 // Invariants restored.
703 }
704
705 // 2)
706 //
707 // The digits of the integral part have been generated:
708 //
709 // M+ = d[k-1]...d[1]d[0] + p2 * 2^e
710 // = buffer + p2 * 2^e
711 //
712 // Now generate the digits of the fractional part p2 * 2^e.
713 //
714 // Note:
715 // No decimal point is generated: the exponent is adjusted instead.
716 //
717 // p2 actually represents the fraction
718 //
719 // p2 * 2^e
720 // = p2 / 2^-e
721 // = d[-1] / 10^1 + d[-2] / 10^2 + ...
722 //
723 // Now generate the digits d[-m] of p1 from left to right (m = 1,2,...)
724 //
725 // p2 * 2^e = d[-1]d[-2]...d[-m] * 10^-m
726 // + 10^-m * (d[-m-1] / 10^1 + d[-m-2] / 10^2 + ...)
727 //
728 // using
729 //
730 // 10^m * p2 = ((10^m * p2) div 2^-e) * 2^-e + ((10^m * p2) mod 2^-e)
731 // = ( d) * 2^-e + ( r)
732 //
733 // or
734 // 10^m * p2 * 2^e = d + r * 2^e
735 //
736 // i.e.
737 //
738 // M+ = buffer + p2 * 2^e
739 // = buffer + 10^-m * (d + r * 2^e)
740 // = (buffer * 10^m + d) * 10^-m + 10^-m * r * 2^e
741 //
742 // and stop as soon as 10^-m * r * 2^e <= delta * 2^e
743
744 JSON_ASSERT(p2 > delta);
745
746 int m = 0;
747 for (;;)
748 {
749 // Invariant:
750 // M+ = buffer * 10^-m + 10^-m * (d[-m-1] / 10 + d[-m-2] / 10^2 + ...) * 2^e
751 // = buffer * 10^-m + 10^-m * (p2 ) * 2^e
752 // = buffer * 10^-m + 10^-m * (1/10 * (10 * p2) ) * 2^e
753 // = buffer * 10^-m + 10^-m * (1/10 * ((10*p2 div 2^-e) * 2^-e + (10*p2 mod 2^-e)) * 2^e
754 //
755 JSON_ASSERT(p2 <= (std::numeric_limits<std::uint64_t>::max)() / 10);
756 p2 *= 10;
757 const std::uint64_t d = p2 >> -one.e; // d = (10 * p2) div 2^-e
758 const std::uint64_t r = p2 & (one.f - 1); // r = (10 * p2) mod 2^-e
759 //
760 // M+ = buffer * 10^-m + 10^-m * (1/10 * (d * 2^-e + r) * 2^e
761 // = buffer * 10^-m + 10^-m * (1/10 * (d + r * 2^e))
762 // = (buffer * 10 + d) * 10^(-m-1) + 10^(-m-1) * r * 2^e
763 //
764 JSON_ASSERT(d <= 9);
765 buffer[length++] = static_cast<char>('0' + d); // buffer := buffer * 10 + d
766 //
767 // M+ = buffer * 10^(-m-1) + 10^(-m-1) * r * 2^e
768 //
769 p2 = r;
770 m++;
771 //
772 // M+ = buffer * 10^-m + 10^-m * p2 * 2^e
773 // Invariant restored.
774
775 // Check if enough digits have been generated.
776 //
777 // 10^-m * p2 * 2^e <= delta * 2^e
778 // p2 * 2^e <= 10^m * delta * 2^e
779 // p2 <= 10^m * delta
780 delta *= 10;
781 dist *= 10;
782 if (p2 <= delta)
783 {
784 break;
785 }
786 }
787
788 // V = buffer * 10^-m, with M- <= V <= M+.
789
790 decimal_exponent -= m;
791
792 // 1 ulp in the decimal representation is now 10^-m.
793 // Since delta and dist are now scaled by 10^m, we need to do the
794 // same with ulp in order to keep the units in sync.
795 //
796 // 10^m * 10^-m = 1 = 2^-e * 2^e = ten_m * 2^e
797 //
798 const std::uint64_t ten_m = one.f;
799 grisu2_round(buffer, length, dist, delta, p2, ten_m);
800
801 // By construction this algorithm generates the shortest possible decimal
802 // number (Loitsch, Theorem 6.2) which rounds back to w.
803 // For an input number of precision p, at least
804 //
805 // N = 1 + ceil(p * log_10(2))
806 //
807 // decimal digits are sufficient to identify all binary floating-point
808 // numbers (Matula, "In-and-Out conversions").
809 // This implies that the algorithm does not produce more than N decimal
810 // digits.
811 //
812 // N = 17 for p = 53 (IEEE double precision)
813 // N = 9 for p = 24 (IEEE single precision)
814 }
815
816 /*!
817 v = buf * 10^decimal_exponent
818 len is the length of the buffer (number of decimal digits)
819 The buffer must be large enough, i.e. >= max_digits10.
820 */
821 JSON_HEDLEY_NON_NULL(1)
grisu2(char * buf,int & len,int & decimal_exponent,diyfp m_minus,diyfp v,diyfp m_plus)822 inline void grisu2(char* buf, int& len, int& decimal_exponent,
823 diyfp m_minus, diyfp v, diyfp m_plus)
824 {
825 JSON_ASSERT(m_plus.e == m_minus.e);
826 JSON_ASSERT(m_plus.e == v.e);
827
828 // --------(-----------------------+-----------------------)-------- (A)
829 // m- v m+
830 //
831 // --------------------(-----------+-----------------------)-------- (B)
832 // m- v m+
833 //
834 // First scale v (and m- and m+) such that the exponent is in the range
835 // [alpha, gamma].
836
837 const cached_power cached = get_cached_power_for_binary_exponent(m_plus.e);
838
839 const diyfp c_minus_k(cached.f, cached.e); // = c ~= 10^-k
840
841 // The exponent of the products is = v.e + c_minus_k.e + q and is in the range [alpha,gamma]
842 const diyfp w = diyfp::mul(v, c_minus_k);
843 const diyfp w_minus = diyfp::mul(m_minus, c_minus_k);
844 const diyfp w_plus = diyfp::mul(m_plus, c_minus_k);
845
846 // ----(---+---)---------------(---+---)---------------(---+---)----
847 // w- w w+
848 // = c*m- = c*v = c*m+
849 //
850 // diyfp::mul rounds its result and c_minus_k is approximated too. w, w- and
851 // w+ are now off by a small amount.
852 // In fact:
853 //
854 // w - v * 10^k < 1 ulp
855 //
856 // To account for this inaccuracy, add resp. subtract 1 ulp.
857 //
858 // --------+---[---------------(---+---)---------------]---+--------
859 // w- M- w M+ w+
860 //
861 // Now any number in [M-, M+] (bounds included) will round to w when input,
862 // regardless of how the input rounding algorithm breaks ties.
863 //
864 // And digit_gen generates the shortest possible such number in [M-, M+].
865 // Note that this does not mean that Grisu2 always generates the shortest
866 // possible number in the interval (m-, m+).
867 const diyfp M_minus(w_minus.f + 1, w_minus.e);
868 const diyfp M_plus (w_plus.f - 1, w_plus.e );
869
870 decimal_exponent = -cached.k; // = -(-k) = k
871
872 grisu2_digit_gen(buf, len, decimal_exponent, M_minus, w, M_plus);
873 }
874
875 /*!
876 v = buf * 10^decimal_exponent
877 len is the length of the buffer (number of decimal digits)
878 The buffer must be large enough, i.e. >= max_digits10.
879 */
880 template<typename FloatType>
881 JSON_HEDLEY_NON_NULL(1)
grisu2(char * buf,int & len,int & decimal_exponent,FloatType value)882 void grisu2(char* buf, int& len, int& decimal_exponent, FloatType value)
883 {
884 static_assert(diyfp::kPrecision >= std::numeric_limits<FloatType>::digits + 3,
885 "internal error: not enough precision");
886
887 JSON_ASSERT(std::isfinite(value));
888 JSON_ASSERT(value > 0);
889
890 // If the neighbors (and boundaries) of 'value' are always computed for double-precision
891 // numbers, all float's can be recovered using strtod (and strtof). However, the resulting
892 // decimal representations are not exactly "short".
893 //
894 // The documentation for 'std::to_chars' (https://en.cppreference.com/w/cpp/utility/to_chars)
895 // says "value is converted to a string as if by std::sprintf in the default ("C") locale"
896 // and since sprintf promotes float's to double's, I think this is exactly what 'std::to_chars'
897 // does.
898 // On the other hand, the documentation for 'std::to_chars' requires that "parsing the
899 // representation using the corresponding std::from_chars function recovers value exactly". That
900 // indicates that single precision floating-point numbers should be recovered using
901 // 'std::strtof'.
902 //
903 // NB: If the neighbors are computed for single-precision numbers, there is a single float
904 // (7.0385307e-26f) which can't be recovered using strtod. The resulting double precision
905 // value is off by 1 ulp.
906 #if 0
907 const boundaries w = compute_boundaries(static_cast<double>(value));
908 #else
909 const boundaries w = compute_boundaries(value);
910 #endif
911
912 grisu2(buf, len, decimal_exponent, w.minus, w.w, w.plus);
913 }
914
915 /*!
916 @brief appends a decimal representation of e to buf
917 @return a pointer to the element following the exponent.
918 @pre -1000 < e < 1000
919 */
920 JSON_HEDLEY_NON_NULL(1)
921 JSON_HEDLEY_RETURNS_NON_NULL
append_exponent(char * buf,int e)922 inline char* append_exponent(char* buf, int e)
923 {
924 JSON_ASSERT(e > -1000);
925 JSON_ASSERT(e < 1000);
926
927 if (e < 0)
928 {
929 e = -e;
930 *buf++ = '-';
931 }
932 else
933 {
934 *buf++ = '+';
935 }
936
937 auto k = static_cast<std::uint32_t>(e);
938 if (k < 10)
939 {
940 // Always print at least two digits in the exponent.
941 // This is for compatibility with printf("%g").
942 *buf++ = '0';
943 *buf++ = static_cast<char>('0' + k);
944 }
945 else if (k < 100)
946 {
947 *buf++ = static_cast<char>('0' + k / 10);
948 k %= 10;
949 *buf++ = static_cast<char>('0' + k);
950 }
951 else
952 {
953 *buf++ = static_cast<char>('0' + k / 100);
954 k %= 100;
955 *buf++ = static_cast<char>('0' + k / 10);
956 k %= 10;
957 *buf++ = static_cast<char>('0' + k);
958 }
959
960 return buf;
961 }
962
963 /*!
964 @brief prettify v = buf * 10^decimal_exponent
965
966 If v is in the range [10^min_exp, 10^max_exp) it will be printed in fixed-point
967 notation. Otherwise it will be printed in exponential notation.
968
969 @pre min_exp < 0
970 @pre max_exp > 0
971 */
972 JSON_HEDLEY_NON_NULL(1)
973 JSON_HEDLEY_RETURNS_NON_NULL
format_buffer(char * buf,int len,int decimal_exponent,int min_exp,int max_exp)974 inline char* format_buffer(char* buf, int len, int decimal_exponent,
975 int min_exp, int max_exp)
976 {
977 JSON_ASSERT(min_exp < 0);
978 JSON_ASSERT(max_exp > 0);
979
980 const int k = len;
981 const int n = len + decimal_exponent;
982
983 // v = buf * 10^(n-k)
984 // k is the length of the buffer (number of decimal digits)
985 // n is the position of the decimal point relative to the start of the buffer.
986
987 if (k <= n && n <= max_exp)
988 {
989 // digits[000]
990 // len <= max_exp + 2
991
992 std::memset(buf + k, '0', static_cast<size_t>(n) - static_cast<size_t>(k));
993 // Make it look like a floating-point number (#362, #378)
994 buf[n + 0] = '.';
995 buf[n + 1] = '0';
996 return buf + (static_cast<size_t>(n) + 2);
997 }
998
999 if (0 < n && n <= max_exp)
1000 {
1001 // dig.its
1002 // len <= max_digits10 + 1
1003
1004 JSON_ASSERT(k > n);
1005
1006 std::memmove(buf + (static_cast<size_t>(n) + 1), buf + n, static_cast<size_t>(k) - static_cast<size_t>(n));
1007 buf[n] = '.';
1008 return buf + (static_cast<size_t>(k) + 1U);
1009 }
1010
1011 if (min_exp < n && n <= 0)
1012 {
1013 // 0.[000]digits
1014 // len <= 2 + (-min_exp - 1) + max_digits10
1015
1016 std::memmove(buf + (2 + static_cast<size_t>(-n)), buf, static_cast<size_t>(k));
1017 buf[0] = '0';
1018 buf[1] = '.';
1019 std::memset(buf + 2, '0', static_cast<size_t>(-n));
1020 return buf + (2U + static_cast<size_t>(-n) + static_cast<size_t>(k));
1021 }
1022
1023 if (k == 1)
1024 {
1025 // dE+123
1026 // len <= 1 + 5
1027
1028 buf += 1;
1029 }
1030 else
1031 {
1032 // d.igitsE+123
1033 // len <= max_digits10 + 1 + 5
1034
1035 std::memmove(buf + 2, buf + 1, static_cast<size_t>(k) - 1);
1036 buf[1] = '.';
1037 buf += 1 + static_cast<size_t>(k);
1038 }
1039
1040 *buf++ = 'e';
1041 return append_exponent(buf, n - 1);
1042 }
1043
1044 } // namespace dtoa_impl
1045
1046 /*!
1047 @brief generates a decimal representation of the floating-point number value in [first, last).
1048
1049 The format of the resulting decimal representation is similar to printf's %g
1050 format. Returns an iterator pointing past-the-end of the decimal representation.
1051
1052 @note The input number must be finite, i.e. NaN's and Inf's are not supported.
1053 @note The buffer must be large enough.
1054 @note The result is NOT null-terminated.
1055 */
1056 template<typename FloatType>
1057 JSON_HEDLEY_NON_NULL(1, 2)
1058 JSON_HEDLEY_RETURNS_NON_NULL
to_chars(char * first,const char * last,FloatType value)1059 char* to_chars(char* first, const char* last, FloatType value)
1060 {
1061 static_cast<void>(last); // maybe unused - fix warning
1062 JSON_ASSERT(std::isfinite(value));
1063
1064 // Use signbit(value) instead of (value < 0) since signbit works for -0.
1065 if (std::signbit(value))
1066 {
1067 value = -value;
1068 *first++ = '-';
1069 }
1070
1071 if (value == 0) // +-0
1072 {
1073 *first++ = '0';
1074 // Make it look like a floating-point number (#362, #378)
1075 *first++ = '.';
1076 *first++ = '0';
1077 return first;
1078 }
1079
1080 JSON_ASSERT(last - first >= std::numeric_limits<FloatType>::max_digits10);
1081
1082 // Compute v = buffer * 10^decimal_exponent.
1083 // The decimal digits are stored in the buffer, which needs to be interpreted
1084 // as an unsigned decimal integer.
1085 // len is the length of the buffer, i.e. the number of decimal digits.
1086 int len = 0;
1087 int decimal_exponent = 0;
1088 dtoa_impl::grisu2(first, len, decimal_exponent, value);
1089
1090 JSON_ASSERT(len <= std::numeric_limits<FloatType>::max_digits10);
1091
1092 // Format the buffer like printf("%.*g", prec, value)
1093 constexpr int kMinExp = -4;
1094 // Use digits10 here to increase compatibility with version 2.
1095 constexpr int kMaxExp = std::numeric_limits<FloatType>::digits10;
1096
1097 JSON_ASSERT(last - first >= kMaxExp + 2);
1098 JSON_ASSERT(last - first >= 2 + (-kMinExp - 1) + std::numeric_limits<FloatType>::max_digits10);
1099 JSON_ASSERT(last - first >= std::numeric_limits<FloatType>::max_digits10 + 6);
1100
1101 return dtoa_impl::format_buffer(first, len, decimal_exponent, kMinExp, kMaxExp);
1102 }
1103
1104 } // namespace detail
1105 } // namespace nlohmann
1106