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