File Coverage

d2s.c
Criterion Covered Total %
statement 152 222 68.4
branch 58 120 48.3
condition n/a
subroutine n/a
pod n/a
total 210 342 61.4


line stmt bran cond sub pod time code
1             // Copyright 2018 Ulf Adams
2             //
3             // The contents of this file may be used under the terms of the Apache License,
4             // Version 2.0.
5             //
6             // (See accompanying file LICENSE-Apache or copy at
7             // http://www.apache.org/licenses/LICENSE-2.0)
8             //
9             // Alternatively, the contents of this file may be used under the terms of
10             // the Boost Software License, Version 1.0.
11             // (See accompanying file LICENSE-Boost or copy at
12             // https://www.boost.org/LICENSE_1_0.txt)
13             //
14             // Unless required by applicable law or agreed to in writing, this software
15             // is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16             // KIND, either express or implied.
17              
18             // Runtime compiler options:
19             // -DRYU_DEBUG Generate verbose debugging output to stdout.
20             //
21             // -DRYU_ONLY_64_BIT_OPS Avoid using uint128_t or 64-bit intrinsics. Slower,
22             // depending on your compiler.
23             //
24             // -DRYU_OPTIMIZE_SIZE Use smaller lookup tables. Instead of storing every
25             // required power of 5, only store every 26th entry, and compute
26             // intermediate values with a multiplication. This reduces the lookup table
27             // size by about 10x (only one case, and only double) at the cost of some
28             // performance. Currently requires MSVC intrinsics.
29              
30             /* The location of the header files, relative to this file, has been changed by
31             * Sisyphus */
32              
33             #include "ryu_headers/ryu.h"
34              
35             #include
36             #include
37             #include
38             #include
39             #include
40              
41             #ifdef RYU_DEBUG
42             #include
43             #include
44             #endif
45              
46             #include "ryu_headers/common.h"
47             #include "ryu_headers/digit_table.h"
48             #include "ryu_headers/d2s_intrinsics.h"
49              
50             // Include either the small or the full lookup tables depending on the mode.
51             #if defined(RYU_OPTIMIZE_SIZE)
52             #include "ryu_headers/d2s_small_table.h"
53             #else
54             #include "ryu_headers/d2s_full_table.h"
55             #endif
56              
57             #define DOUBLE_MANTISSA_BITS 52
58             #define DOUBLE_EXPONENT_BITS 11
59             #define DOUBLE_BIAS 1023
60              
61 35           static inline uint32_t decimalLength17(const uint64_t v) {
62             // This is slightly faster than a loop.
63             // The average output length is 16.38 digits, so we check high-to-low.
64             // Function precondition: v is not an 18, 19, or 20-digit number.
65             // (17 digits are sufficient for round-tripping.)
66 35 50         assert(v < 100000000000000000L);
67 35 100         if (v >= 10000000000000000L) { return 17; }
68 20 50         if (v >= 1000000000000000L) { return 16; }
69 20 50         if (v >= 100000000000000L) { return 15; }
70 20 50         if (v >= 10000000000000L) { return 14; }
71 20 50         if (v >= 1000000000000L) { return 13; }
72 20 50         if (v >= 100000000000L) { return 12; }
73 20 50         if (v >= 10000000000L) { return 11; }
74 20 50         if (v >= 1000000000L) { return 10; }
75 20 50         if (v >= 100000000L) { return 9; }
76 20 50         if (v >= 10000000L) { return 8; }
77 20 50         if (v >= 1000000L) { return 7; }
78 20 50         if (v >= 100000L) { return 6; }
79 20 50         if (v >= 10000L) { return 5; }
80 20 50         if (v >= 1000L) { return 4; }
81 20 50         if (v >= 100L) { return 3; }
82 20 50         if (v >= 10L) { return 2; }
83 20           return 1;
84             }
85              
86             // A floating decimal representing m * 10^e.
87             typedef struct floating_decimal_64 {
88             uint64_t mantissa;
89             // Decimal exponent's range is -324 to 308
90             // inclusive, and can fit in a short if needed.
91             int32_t exponent;
92             } floating_decimal_64;
93              
94 35           static inline floating_decimal_64 d2d(const uint64_t ieeeMantissa, const uint32_t ieeeExponent) {
95             int32_t e2;
96             uint64_t m2;
97 35 100         if (ieeeExponent == 0) {
98             // We subtract 2 so that the bounds computation has 2 additional bits.
99 10           e2 = 1 - DOUBLE_BIAS - DOUBLE_MANTISSA_BITS - 2;
100 10           m2 = ieeeMantissa;
101             } else {
102 25           e2 = (int32_t) ieeeExponent - DOUBLE_BIAS - DOUBLE_MANTISSA_BITS - 2;
103 25           m2 = (1ull << DOUBLE_MANTISSA_BITS) | ieeeMantissa;
104             }
105 35           const bool even = (m2 & 1) == 0;
106 35           const bool acceptBounds = even;
107              
108             #ifdef RYU_DEBUG
109             printf("-> %" PRIu64 " * 2^%d\n", m2, e2 + 2);
110             #endif
111              
112             // Step 2: Determine the interval of valid decimal representations.
113 35           const uint64_t mv = 4 * m2;
114             // Implicit bool -> int conversion. True is 1, false is 0.
115 35 50         const uint32_t mmShift = ieeeMantissa != 0 || ieeeExponent <= 1;
    0          
116             // We would compute mp and mm like this:
117             // uint64_t mp = 4 * m2 + 2;
118             // uint64_t mm = mv - 1 - mmShift;
119              
120             // Step 3: Convert to a decimal power base using 128-bit arithmetic.
121             uint64_t vr, vp, vm;
122             int32_t e10;
123 35           bool vmIsTrailingZeros = false;
124 35           bool vrIsTrailingZeros = false;
125 35 50         if (e2 >= 0) {
126             // I tried special-casing q == 0, but there was no effect on performance.
127             // This expression is slightly faster than max(0, log10Pow2(e2) - 1).
128 0           const uint32_t q = log10Pow2(e2) - (e2 > 3);
129 0           e10 = (int32_t) q;
130 0           const int32_t k = DOUBLE_POW5_INV_BITCOUNT + pow5bits((int32_t) q) - 1;
131 0           const int32_t i = -e2 + (int32_t) q + k;
132             #if defined(RYU_OPTIMIZE_SIZE)
133             uint64_t pow5[2];
134             double_computeInvPow5(q, pow5);
135             vr = mulShiftAll64(m2, pow5, i, &vp, &vm, mmShift);
136             #else
137 0           vr = mulShiftAll64(m2, DOUBLE_POW5_INV_SPLIT[q], i, &vp, &vm, mmShift);
138             #endif
139             #ifdef RYU_DEBUG
140             printf("%" PRIu64 " * 2^%d / 10^%u\n", mv, e2, q);
141             printf("V+=%" PRIu64 "\nV =%" PRIu64 "\nV-=%" PRIu64 "\n", vp, vr, vm);
142             #endif
143 0 0         if (q <= 21) {
144             // This should use q <= 22, but I think 21 is also safe. Smaller values
145             // may still be safe, but it's more difficult to reason about them.
146             // Only one of mp, mv, and mm can be a multiple of 5, if any.
147 0           const uint32_t mvMod5 = ((uint32_t) mv) - 5 * ((uint32_t) div5(mv));
148 0 0         if (mvMod5 == 0) {
149 0           vrIsTrailingZeros = multipleOfPowerOf5(mv, q);
150 0 0         } else if (acceptBounds) {
151             // Same as min(e2 + (~mm & 1), pow5Factor(mm)) >= q
152             // <=> e2 + (~mm & 1) >= q && pow5Factor(mm) >= q
153             // <=> true && pow5Factor(mm) >= q, since e2 >= q.
154 0           vmIsTrailingZeros = multipleOfPowerOf5(mv - 1 - mmShift, q);
155             } else {
156             // Same as min(e2 + 1, pow5Factor(mp)) >= q.
157 0           vp -= multipleOfPowerOf5(mv + 2, q);
158             }
159             }
160             } else {
161             // This expression is slightly faster than max(0, log10Pow5(-e2) - 1).
162 35           const uint32_t q = log10Pow5(-e2) - (-e2 > 1);
163 35           e10 = (int32_t) q + e2;
164 35           const int32_t i = -e2 - (int32_t) q;
165 35           const int32_t k = pow5bits(i) - DOUBLE_POW5_BITCOUNT;
166 35           const int32_t j = (int32_t) q - k;
167             #if defined(RYU_OPTIMIZE_SIZE)
168             uint64_t pow5[2];
169             double_computePow5(i, pow5);
170             vr = mulShiftAll64(m2, pow5, j, &vp, &vm, mmShift);
171             #else
172 35           vr = mulShiftAll64(m2, DOUBLE_POW5_SPLIT[i], j, &vp, &vm, mmShift);
173             #endif
174             #ifdef RYU_DEBUG
175             printf("%" PRIu64 " * 5^%d / 10^%u\n", mv, -e2, q);
176             printf("%u %d %d %d\n", q, i, k, j);
177             printf("V+=%" PRIu64 "\nV =%" PRIu64 "\nV-=%" PRIu64 "\n", vp, vr, vm);
178             #endif
179 35 50         if (q <= 1) {
180             // {vr,vp,vm} is trailing zeros if {mv,mp,mm} has at least q trailing 0 bits.
181             // mv = 4 * m2, so it always has at least two trailing 0 bits.
182 0           vrIsTrailingZeros = true;
183 0 0         if (acceptBounds) {
184             // mm = mv - 1 - mmShift, so it has 1 trailing 0 bit iff mmShift == 1.
185 0           vmIsTrailingZeros = mmShift == 1;
186             } else {
187             // mp = mv + 2, so it always has at least one trailing 0 bit.
188 0           --vp;
189             }
190 35 100         } else if (q < 63) { // TODO(ulfjack): Use a tighter bound here.
191             // We want to know if the full product has at least q trailing zeros.
192             // We need to compute min(p2(mv), p5(mv) - e2) >= q
193             // <=> p2(mv) >= q && p5(mv) - e2 >= q
194             // <=> p2(mv) >= q (because -e2 >= q)
195 25           vrIsTrailingZeros = multipleOfPowerOf2(mv, q);
196             #ifdef RYU_DEBUG
197             printf("vr is trailing zeros=%s\n", vrIsTrailingZeros ? "true" : "false");
198             #endif
199             }
200             }
201             #ifdef RYU_DEBUG
202             printf("e10=%d\n", e10);
203             printf("V+=%" PRIu64 "\nV =%" PRIu64 "\nV-=%" PRIu64 "\n", vp, vr, vm);
204             printf("vm is trailing zeros=%s\n", vmIsTrailingZeros ? "true" : "false");
205             printf("vr is trailing zeros=%s\n", vrIsTrailingZeros ? "true" : "false");
206             #endif
207              
208             // Step 4: Find the shortest decimal representation in the interval of valid representations.
209 35           int32_t removed = 0;
210 35           uint8_t lastRemovedDigit = 0;
211             uint64_t output;
212             // On average, we remove ~2 digits.
213 35 50         if (vmIsTrailingZeros || vrIsTrailingZeros) {
    50          
214             // General case, which happens rarely (~0.7%).
215             for (;;) {
216 0           const uint64_t vpDiv10 = div10(vp);
217 0           const uint64_t vmDiv10 = div10(vm);
218 0 0         if (vpDiv10 <= vmDiv10) {
219 0           break;
220             }
221 0           const uint32_t vmMod10 = ((uint32_t) vm) - 10 * ((uint32_t) vmDiv10);
222 0           const uint64_t vrDiv10 = div10(vr);
223 0           const uint32_t vrMod10 = ((uint32_t) vr) - 10 * ((uint32_t) vrDiv10);
224 0           vmIsTrailingZeros &= vmMod10 == 0;
225 0           vrIsTrailingZeros &= lastRemovedDigit == 0;
226 0           lastRemovedDigit = (uint8_t) vrMod10;
227 0           vr = vrDiv10;
228 0           vp = vpDiv10;
229 0           vm = vmDiv10;
230 0           ++removed;
231 0           }
232             #ifdef RYU_DEBUG
233             printf("V+=%" PRIu64 "\nV =%" PRIu64 "\nV-=%" PRIu64 "\n", vp, vr, vm);
234             printf("d-10=%s\n", vmIsTrailingZeros ? "true" : "false");
235             #endif
236 0 0         if (vmIsTrailingZeros) {
237             for (;;) {
238 0           const uint64_t vmDiv10 = div10(vm);
239 0           const uint32_t vmMod10 = ((uint32_t) vm) - 10 * ((uint32_t) vmDiv10);
240 0 0         if (vmMod10 != 0) {
241 0           break;
242             }
243 0           const uint64_t vpDiv10 = div10(vp);
244 0           const uint64_t vrDiv10 = div10(vr);
245 0           const uint32_t vrMod10 = ((uint32_t) vr) - 10 * ((uint32_t) vrDiv10);
246 0           vrIsTrailingZeros &= lastRemovedDigit == 0;
247 0           lastRemovedDigit = (uint8_t) vrMod10;
248 0           vr = vrDiv10;
249 0           vp = vpDiv10;
250 0           vm = vmDiv10;
251 0           ++removed;
252 0           }
253             }
254             #ifdef RYU_DEBUG
255             printf("%" PRIu64 " %d\n", vr, lastRemovedDigit);
256             printf("vr is trailing zeros=%s\n", vrIsTrailingZeros ? "true" : "false");
257             #endif
258 0 0         if (vrIsTrailingZeros && lastRemovedDigit == 5 && vr % 2 == 0) {
    0          
    0          
259             // Round even if the exact number is .....50..0.
260 0           lastRemovedDigit = 4;
261             }
262             // We need to take vr + 1 if vr is outside bounds or we need to round up.
263 0 0         output = vr + ((vr == vm && (!acceptBounds || !vmIsTrailingZeros)) || lastRemovedDigit >= 5);
    0          
    0          
    0          
264             } else {
265             // Specialized for the common case (~99.3%). Percentages below are relative to this.
266 35           bool roundUp = false;
267 35           const uint64_t vpDiv100 = div100(vp);
268 35           const uint64_t vmDiv100 = div100(vm);
269 35 100         if (vpDiv100 > vmDiv100) { // Optimization: remove two digits at a time (~86.2%).
270 25           const uint64_t vrDiv100 = div100(vr);
271 25           const uint32_t vrMod100 = ((uint32_t) vr) - 100 * ((uint32_t) vrDiv100);
272 25           roundUp = vrMod100 >= 50;
273 25           vr = vrDiv100;
274 25           vp = vpDiv100;
275 25           vm = vmDiv100;
276 25           removed += 2;
277             }
278             // Loop iterations below (approximately), without optimization above:
279             // 0: 0.03%, 1: 13.8%, 2: 70.6%, 3: 14.0%, 4: 1.40%, 5: 0.14%, 6+: 0.02%
280             // Loop iterations below (approximately), with optimization above:
281             // 0: 70.6%, 1: 27.8%, 2: 1.40%, 3: 0.14%, 4+: 0.02%
282             for (;;) {
283 205           const uint64_t vpDiv10 = div10(vp);
284 205           const uint64_t vmDiv10 = div10(vm);
285 205 100         if (vpDiv10 <= vmDiv10) {
286 35           break;
287             }
288 170           const uint64_t vrDiv10 = div10(vr);
289 170           const uint32_t vrMod10 = ((uint32_t) vr) - 10 * ((uint32_t) vrDiv10);
290 170           roundUp = vrMod10 >= 5;
291 170           vr = vrDiv10;
292 170           vp = vpDiv10;
293 170           vm = vmDiv10;
294 170           ++removed;
295 170           }
296             #ifdef RYU_DEBUG
297             printf("%" PRIu64 " roundUp=%s\n", vr, roundUp ? "true" : "false");
298             printf("vr is trailing zeros=%s\n", vrIsTrailingZeros ? "true" : "false");
299             #endif
300             // We need to take vr + 1 if vr is outside bounds or we need to round up.
301 35 50         output = vr + (vr == vm || roundUp);
    100          
302             }
303 35           const int32_t exp = e10 + removed;
304              
305             #ifdef RYU_DEBUG
306             printf("V+=%" PRIu64 "\nV =%" PRIu64 "\nV-=%" PRIu64 "\n", vp, vr, vm);
307             printf("O=%" PRIu64 "\n", output);
308             printf("EXP=%d\n", exp);
309             #endif
310              
311             floating_decimal_64 fd;
312 35           fd.exponent = exp;
313 35           fd.mantissa = output;
314 35           return fd;
315             }
316              
317 35           static inline int to_chars(const floating_decimal_64 v, const bool sign, char* const result) {
318             // Step 5: Print the decimal representation.
319 35           int index = 0;
320 35 100         if (sign) {
321 10           result[index++] = '-';
322             }
323              
324 35           uint64_t output = v.mantissa;
325 35           const uint32_t olength = decimalLength17(output);
326              
327             #ifdef RYU_DEBUG
328             printf("DIGITS=%" PRIu64 "\n", v.mantissa);
329             printf("OLEN=%u\n", olength);
330             printf("EXP=%u\n", v.exponent + olength);
331             #endif
332              
333             // Print the decimal digits.
334             // The following code is equivalent to:
335             // for (uint32_t i = 0; i < olength - 1; ++i) {
336             // const uint32_t c = output % 10; output /= 10;
337             // result[index + olength - i] = (char) ('0' + c);
338             // }
339             // result[index] = '0' + output % 10;
340              
341 35           uint32_t i = 0;
342             // We prefer 32-bit operations, even on 64-bit platforms.
343             // We have at most 17 digits, and uint32_t can store 9 digits.
344             // If output doesn't fit into uint32_t, we cut off 8 digits,
345             // so the rest will fit into uint32_t.
346 35 100         if ((output >> 32) != 0) {
347             // Expensive 64-bit division.
348 15           const uint64_t q = div1e8(output);
349 15           uint32_t output2 = ((uint32_t) output) - 100000000 * ((uint32_t) q);
350 15           output = q;
351              
352 15           const uint32_t c = output2 % 10000;
353 15           output2 /= 10000;
354 15           const uint32_t d = output2 % 10000;
355 15           const uint32_t c0 = (c % 100) << 1;
356 15           const uint32_t c1 = (c / 100) << 1;
357 15           const uint32_t d0 = (d % 100) << 1;
358 15           const uint32_t d1 = (d / 100) << 1;
359 15           memcpy(result + index + olength - i - 1, DIGIT_TABLE + c0, 2);
360 15           memcpy(result + index + olength - i - 3, DIGIT_TABLE + c1, 2);
361 15           memcpy(result + index + olength - i - 5, DIGIT_TABLE + d0, 2);
362 15           memcpy(result + index + olength - i - 7, DIGIT_TABLE + d1, 2);
363 15           i += 8;
364             }
365 35           uint32_t output2 = (uint32_t) output;
366 65 100         while (output2 >= 10000) {
367             #ifdef __clang__ // https://bugs.llvm.org/show_bug.cgi?id=38217
368             const uint32_t c = output2 - 10000 * (output2 / 10000);
369             #else
370 30           const uint32_t c = output2 % 10000;
371             #endif
372 30           output2 /= 10000;
373 30           const uint32_t c0 = (c % 100) << 1;
374 30           const uint32_t c1 = (c / 100) << 1;
375 30           memcpy(result + index + olength - i - 1, DIGIT_TABLE + c0, 2);
376 30           memcpy(result + index + olength - i - 3, DIGIT_TABLE + c1, 2);
377 30           i += 4;
378             }
379 35 50         if (output2 >= 100) {
380 0           const uint32_t c = (output2 % 100) << 1;
381 0           output2 /= 100;
382 0           memcpy(result + index + olength - i - 1, DIGIT_TABLE + c, 2);
383 0           i += 2;
384             }
385 35 50         if (output2 >= 10) {
386 0           const uint32_t c = output2 << 1;
387             // We can't use memcpy here: the decimal dot goes between these two digits.
388 0           result[index + olength - i] = DIGIT_TABLE[c + 1];
389 0           result[index] = DIGIT_TABLE[c];
390             } else {
391 35           result[index] = (char) ('0' + output2);
392             }
393              
394             // Print decimal point if needed.
395 35 100         if (olength > 1) {
396 15           result[index + 1] = '.';
397 15           index += olength + 1;
398             } else {
399 20           ++index;
400             }
401              
402             // Print the exponent.
403 35           result[index++] = 'E';
404 35           int32_t exp = v.exponent + (int32_t) olength - 1;
405 35 100         if (exp < 0) {
406 25           result[index++] = '-';
407 25           exp = -exp;
408             }
409              
410 35 100         if (exp >= 100) {
411 10           const int32_t c = exp % 10;
412 10           memcpy(result + index, DIGIT_TABLE + 2 * (exp / 10), 2);
413 10           result[index + 2] = (char) ('0' + c);
414 10           index += 3;
415 25 50         } else if (exp >= 10) {
416 0           memcpy(result + index, DIGIT_TABLE + 2 * exp, 2);
417 0           index += 2;
418             } else {
419 25           result[index++] = (char) ('0' + exp);
420             }
421              
422 35           return index;
423             }
424              
425 35           static inline bool d2d_small_int(const uint64_t ieeeMantissa, const uint32_t ieeeExponent,
426             floating_decimal_64* const v) {
427 35           const uint64_t m2 = (1ull << DOUBLE_MANTISSA_BITS) | ieeeMantissa;
428 35           const int32_t e2 = (int32_t) ieeeExponent - DOUBLE_BIAS - DOUBLE_MANTISSA_BITS;
429              
430 35 50         if (e2 > 0) {
431             // f = m2 * 2^e2 >= 2^53 is an integer.
432             // Ignore this case for now.
433 0           return false;
434             }
435              
436 35 100         if (e2 < -52) {
437             // f < 1.
438 25           return false;
439             }
440              
441             // Since 2^52 <= m2 < 2^53 and 0 <= -e2 <= 52: 1 <= f = m2 / 2^-e2 < 2^53.
442             // Test if the lower -e2 bits of the significand are 0, i.e. whether the fraction is 0.
443 10           const uint64_t mask = (1ull << -e2) - 1;
444 10           const uint64_t fraction = m2 & mask;
445 10 50         if (fraction != 0) {
446 10           return false;
447             }
448              
449             // f is an integer in the range [1, 2^53).
450             // Note: mantissa might contain trailing (decimal) 0's.
451             // Note: since 2^53 < 10^16, there is no need to adjust decimalLength17().
452 0           v->mantissa = m2 >> -e2;
453 0           v->exponent = 0;
454 0           return true;
455             }
456              
457 35           int d2s_buffered_n(double f, char* result) {
458             // Step 1: Decode the floating-point number, and unify normalized and subnormal cases.
459 35           const uint64_t bits = double_to_bits(f);
460              
461             #ifdef RYU_DEBUG
462             printf("IN=");
463             for (int32_t bit = 63; bit >= 0; --bit) {
464             printf("%d", (int) ((bits >> bit) & 1));
465             }
466             printf("\n");
467             #endif
468              
469             // Decode bits into sign, mantissa, and exponent.
470 35           const bool ieeeSign = ((bits >> (DOUBLE_MANTISSA_BITS + DOUBLE_EXPONENT_BITS)) & 1) != 0;
471 35           const uint64_t ieeeMantissa = bits & ((1ull << DOUBLE_MANTISSA_BITS) - 1);
472 35           const uint32_t ieeeExponent = (uint32_t) ((bits >> DOUBLE_MANTISSA_BITS) & ((1u << DOUBLE_EXPONENT_BITS) - 1));
473             // Case distinction; exit early for the easy cases.
474 35 50         if (ieeeExponent == ((1u << DOUBLE_EXPONENT_BITS) - 1u) || (ieeeExponent == 0 && ieeeMantissa == 0)) {
    100          
    50          
475 0           return copy_special_str(result, ieeeSign, ieeeExponent, ieeeMantissa);
476             }
477              
478             floating_decimal_64 v;
479 35           const bool isSmallInt = d2d_small_int(ieeeMantissa, ieeeExponent, &v);
480 35 50         if (isSmallInt) {
481             // For small integers in the range [1, 2^53), v.mantissa might contain trailing (decimal) zeros.
482             // For scientific notation we need to move these zeros into the exponent.
483             // (This is not needed for fixed-point notation, so it might be beneficial to trim
484             // trailing zeros in to_chars only if needed - once fixed-point notation output is implemented.)
485             for (;;) {
486 0           const uint64_t q = div10(v.mantissa);
487 0           const uint32_t r = ((uint32_t) v.mantissa) - 10 * ((uint32_t) q);
488 0 0         if (r != 0) {
489 0           break;
490             }
491 0           v.mantissa = q;
492 0           ++v.exponent;
493 0           }
494             } else {
495 35           v = d2d(ieeeMantissa, ieeeExponent);
496             }
497              
498 35           return to_chars(v, ieeeSign, result);
499             }
500              
501 28           void d2s_buffered(double f, char* result) {
502 28           const int index = d2s_buffered_n(f, result);
503              
504             // Terminate the string.
505 28           result[index] = '\0';
506 28           }
507              
508 21           char* d2s(double f) {
509 21           char* const result = (char*) malloc(25);
510 21           d2s_buffered(f, result);
511 21           return result;
512             }