Coverage Report

Created: 2025-08-29 06:10

/src/open62541/deps/dtoa.c
Line
Count
Source (jump to first uncovered line)
1
// Copyright 2013, Andreas Samoljuk
2
// Copyright 2023, Julius Pfrommer
3
//
4
// Boost Software License - Version 1.0 - August 17th, 2003
5
//
6
// Permission is hereby granted, free of charge, to any person or organization
7
// obtaining a copy of the software and accompanying documentation covered by
8
// this license (the "Software") to use, reproduce, display, distribute,
9
// execute, and transmit the Software, and to prepare derivative works of the
10
// Software, and to permit third-parties to whom the Software is furnished to
11
// do so, all subject to the following:
12
//
13
// The copyright notices in the Software and this entire statement, including
14
// the above license grant, this restriction and the following disclaimer,
15
// must be included in all copies of the Software, in whole or in part, and
16
// all derivative works of the Software, unless such copies or derivative
17
// works are solely in the form of machine-executable object code generated by
18
// a source language processor.
19
//
20
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22
// FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
23
// SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
24
// FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
25
// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
26
// DEALINGS IN THE SOFTWARE.
27
28
#include "dtoa.h"
29
#include <stdint.h>
30
#include <stdbool.h>
31
#include <string.h>
32
33
10.1M
#define mantissa_bits 52
34
9.89M
#define exponent_bits 11
35
3.22M
#define fracmask  0x000FFFFFFFFFFFFFU
36
3.22M
#define expmask   0x7FF0000000000000U
37
13.1M
#define hiddenbit 0x0010000000000000U
38
//#define signmask  0x8000000000000000U
39
3.22M
#define expbias   (1023 + 52)
40
41
5.74M
#define absv(n) ((n) < 0 ? -(n) : (n))
42
11.2k
#define minv(a, b) ((a) < (b) ? (a) : (b))
43
44
static uint64_t tens[] = {
45
    10000000000000000000U, 1000000000000000000U, 100000000000000000U,
46
    10000000000000000U, 1000000000000000U, 100000000000000U,
47
    10000000000000U, 1000000000000U, 100000000000U,
48
    10000000000U, 1000000000U, 100000000U,
49
    10000000U, 1000000U, 100000U,
50
    10000U, 1000U, 100U,
51
    10U, 1U
52
};
53
54
3.22M
#define npowers     87
55
6.44M
#define steppowers  8
56
6.44M
#define firstpower -348 /* 10 ^ -348 */
57
3.22M
#define expmax     -32
58
9.64M
#define expmin     -60
59
60
typedef struct Fp {
61
    uint64_t frac;
62
    int exp;
63
} Fp;
64
65
static Fp powers_ten[] = {
66
    { 18054884314459144840U, -1220 }, { 13451937075301367670U, -1193 },
67
    { 10022474136428063862U, -1166 }, { 14934650266808366570U, -1140 },
68
    { 11127181549972568877U, -1113 }, { 16580792590934885855U, -1087 },
69
    { 12353653155963782858U, -1060 }, { 18408377700990114895U, -1034 },
70
    { 13715310171984221708U, -1007 }, { 10218702384817765436U, -980 },
71
    { 15227053142812498563U, -954 },  { 11345038669416679861U, -927 },
72
    { 16905424996341287883U, -901 },  { 12595523146049147757U, -874 },
73
    { 9384396036005875287U,  -847 },  { 13983839803942852151U, -821 },
74
    { 10418772551374772303U, -794 },  { 15525180923007089351U, -768 },
75
    { 11567161174868858868U, -741 },  { 17236413322193710309U, -715 },
76
    { 12842128665889583758U, -688 },  { 9568131466127621947U,  -661 },
77
    { 14257626930069360058U, -635 },  { 10622759856335341974U, -608 },
78
    { 15829145694278690180U, -582 },  { 11793632577567316726U, -555 },
79
    { 17573882009934360870U, -529 },  { 13093562431584567480U, -502 },
80
    { 9755464219737475723U,  -475 },  { 14536774485912137811U, -449 },
81
    { 10830740992659433045U, -422 },  { 16139061738043178685U, -396 },
82
    { 12024538023802026127U, -369 },  { 17917957937422433684U, -343 },
83
    { 13349918974505688015U, -316 },  { 9946464728195732843U,  -289 },
84
    { 14821387422376473014U, -263 },  { 11042794154864902060U, -236 },
85
    { 16455045573212060422U, -210 },  { 12259964326927110867U, -183 },
86
    { 18268770466636286478U, -157 },  { 13611294676837538539U, -130 },
87
    { 10141204801825835212U, -103 },  { 15111572745182864684U, -77 },
88
    { 11258999068426240000U, -50 },   { 16777216000000000000U, -24 },
89
    { 12500000000000000000U,   3 },   { 9313225746154785156U,   30 },
90
    { 13877787807814456755U,  56 },   { 10339757656912845936U,  83 },
91
    { 15407439555097886824U, 109 },   { 11479437019748901445U, 136 },
92
    { 17105694144590052135U, 162 },   { 12744735289059618216U, 189 },
93
    { 9495567745759798747U,  216 },   { 14149498560666738074U, 242 },
94
    { 10542197943230523224U, 269 },   { 15709099088952724970U, 295 },
95
    { 11704190886730495818U, 322 },   { 17440603504673385349U, 348 },
96
    { 12994262207056124023U, 375 },   { 9681479787123295682U,  402 },
97
    { 14426529090290212157U, 428 },   { 10748601772107342003U, 455 },
98
    { 16016664761464807395U, 481 },   { 11933345169920330789U, 508 },
99
    { 17782069995880619868U, 534 },   { 13248674568444952270U, 561 },
100
    { 9871031767461413346U,  588 },   { 14708983551653345445U, 614 },
101
    { 10959046745042015199U, 641 },   { 16330252207878254650U, 667 },
102
    { 12166986024289022870U, 694 },   { 18130221999122236476U, 720 },
103
    { 13508068024458167312U, 747 },   { 10064294952495520794U, 774 },
104
    { 14996968138956309548U, 800 },   { 11173611982879273257U, 827 },
105
    { 16649979327439178909U, 853 },   { 12405201291620119593U, 880 },
106
    { 9242595204427927429U,  907 },   { 13772540099066387757U, 933 },
107
    { 10261342003245940623U, 960 },   { 15290591125556738113U, 986 },
108
    { 11392378155556871081U, 1013 },  { 16975966327722178521U, 1039 },
109
    { 12648080533535911531U, 1066 }
110
};
111
112
static Fp
113
3.22M
find_cachedpow10(int exp, int* k) {
114
3.22M
    const double one_log_ten = 0.30102999566398114;
115
3.22M
    int approx = (int)(-(exp + npowers) * one_log_ten);
116
3.22M
    int idx = (approx - firstpower) / steppowers;
117
9.64M
    while(1) {
118
9.64M
        int current = exp + powers_ten[idx].exp + 64;
119
9.64M
        if(current < expmin) {
120
6.41M
            idx++;
121
6.41M
            continue;
122
6.41M
        }
123
3.22M
        if(current > expmax) {
124
0
            idx--;
125
0
            continue;
126
0
        }
127
3.22M
        *k = (firstpower + idx * steppowers);
128
3.22M
        return powers_ten[idx];
129
3.22M
    }
130
3.22M
}
131
132
3.22M
static Fp build_fp(uint64_t bits) {
133
3.22M
    Fp fp;
134
3.22M
    fp.frac = bits & fracmask;
135
3.22M
    fp.exp = (bits & expmask) >> 52;
136
3.22M
    if(fp.exp) {
137
3.21M
        fp.frac += hiddenbit;
138
3.21M
        fp.exp -= expbias;
139
3.21M
    } else {
140
3.88k
        fp.exp = -expbias + 1;
141
3.88k
    }
142
3.22M
    return fp;
143
3.22M
}
144
145
3.22M
static void normalize(Fp* fp) {
146
3.35M
    while((fp->frac & hiddenbit) == 0) {
147
131k
        fp->frac <<= 1;
148
131k
        fp->exp--;
149
131k
    }
150
3.22M
    int shift = 64 - 52 - 1;
151
3.22M
    fp->frac <<= shift;
152
3.22M
    fp->exp -= shift;
153
3.22M
}
154
155
3.22M
static void get_normalized_boundaries(Fp* fp, Fp* lower, Fp* upper) {
156
3.22M
    upper->frac = (fp->frac << 1) + 1;
157
3.22M
    upper->exp  = fp->exp - 1;
158
3.35M
    while ((upper->frac & (hiddenbit << 1)) == 0) {
159
131k
        upper->frac <<= 1;
160
131k
        upper->exp--;
161
131k
    }
162
163
3.22M
    int u_shift = 64 - 52 - 2;
164
3.22M
    upper->frac <<= u_shift;
165
3.22M
    upper->exp = upper->exp - u_shift;
166
167
3.22M
    int l_shift = fp->frac == hiddenbit ? 2 : 1;
168
3.22M
    lower->frac = (fp->frac << l_shift) - 1;
169
3.22M
    lower->exp = fp->exp - l_shift;
170
3.22M
    lower->frac <<= lower->exp - upper->exp;
171
3.22M
    lower->exp = upper->exp;
172
3.22M
}
173
174
9.67M
static Fp multiply(Fp* a, Fp* b) {
175
9.67M
    const uint64_t lomask = 0x00000000FFFFFFFF;
176
9.67M
    uint64_t ah_bl = (a->frac >> 32)    * (b->frac & lomask);
177
9.67M
    uint64_t al_bh = (a->frac & lomask) * (b->frac >> 32);
178
9.67M
    uint64_t al_bl = (a->frac & lomask) * (b->frac & lomask);
179
9.67M
    uint64_t ah_bh = (a->frac >> 32)    * (b->frac >> 32);
180
9.67M
    uint64_t tmp = (ah_bl & lomask) + (al_bh & lomask) + (al_bl >> 32); 
181
    /* round up */
182
9.67M
    tmp += 1U << 31;
183
9.67M
    Fp fp;
184
9.67M
    fp.frac = ah_bh + (ah_bl >> 32) + (al_bh >> 32) + (tmp >> 32);
185
9.67M
    fp.exp = a->exp + b->exp + 64;
186
9.67M
    return fp;
187
9.67M
}
188
189
static void round_digit(char* digits, unsigned ndigits, uint64_t delta,
190
3.22M
                        uint64_t rem, uint64_t kappa, uint64_t frac) {
191
4.95M
    while(rem < frac && delta - rem >= kappa &&
192
4.95M
          (rem + kappa < frac || frac - rem > rem + kappa - frac)) {
193
1.73M
        digits[ndigits - 1]--;
194
1.73M
        rem += kappa;
195
1.73M
    }
196
3.22M
}
197
198
3.22M
static unsigned generate_digits(Fp* fp, Fp* upper, Fp* lower, char* digits, int* K) {
199
3.22M
    uint64_t wfrac = upper->frac - fp->frac;
200
3.22M
    uint64_t delta = upper->frac - lower->frac;
201
202
3.22M
    Fp one;
203
3.22M
    one.frac = 1ULL << -upper->exp;
204
3.22M
    one.exp  = upper->exp;
205
206
3.22M
    uint64_t part1 = upper->frac >> -one.exp;
207
3.22M
    uint64_t part2 = upper->frac & (one.frac - 1);
208
209
3.22M
    unsigned idx = 0;
210
3.22M
    int kappa = 10;
211
3.22M
    uint64_t* divp;
212
213
    /* 1000000000 */
214
31.9M
    for(divp = tens + 10; kappa > 0; divp++) {
215
29.4M
        uint64_t div = *divp;
216
29.4M
        uint64_t digit = part1 / div;
217
29.4M
        if(digit || idx) {
218
13.2M
            digits[idx++] = (char)(digit + '0');
219
13.2M
        }
220
221
29.4M
        part1 -= digit * div;
222
29.4M
        kappa--;
223
224
29.4M
        uint64_t tmp = (part1 <<-one.exp) + part2;
225
29.4M
        if(tmp <= delta) {
226
703k
            *K += kappa;
227
703k
            round_digit(digits, idx, delta, tmp, div << -one.exp, wfrac);
228
703k
            return idx;
229
703k
        }
230
29.4M
    }
231
232
    /* 10 */
233
2.51M
    uint64_t* unit = tens + 18;
234
29.6M
    while(true) {
235
29.6M
        part2 *= 10;
236
29.6M
        delta *= 10;
237
29.6M
        kappa--;
238
239
29.6M
        uint64_t digit = part2 >> -one.exp;
240
29.6M
        if(digit || idx) {
241
29.6M
            digits[idx++] = (char)(digit + '0');
242
29.6M
        }
243
244
29.6M
        part2 &= one.frac - 1;
245
29.6M
        if(part2 < delta) {
246
2.51M
            *K += kappa;
247
2.51M
            round_digit(digits, idx, delta, part2, one.frac, wfrac * *unit);
248
2.51M
            break;
249
2.51M
        }
250
27.0M
        unit--;
251
27.0M
    }
252
2.51M
    return idx;
253
3.22M
}
254
255
3.22M
static unsigned grisu2(uint64_t bits, char* digits, int* K) {
256
3.22M
    Fp w = build_fp(bits);
257
3.22M
    Fp lower, upper;
258
3.22M
    get_normalized_boundaries(&w, &lower, &upper);
259
3.22M
    normalize(&w);
260
3.22M
    int k;
261
3.22M
    Fp cp = find_cachedpow10(upper.exp, &k);
262
3.22M
    w     = multiply(&w,     &cp);
263
3.22M
    upper = multiply(&upper, &cp);
264
3.22M
    lower = multiply(&lower, &cp);
265
3.22M
    lower.frac++;
266
3.22M
    upper.frac--;
267
3.22M
    *K = -k;
268
3.22M
    return generate_digits(&w, &upper, &lower, digits, K);
269
3.22M
}
270
271
static unsigned
272
3.22M
emit_digits(char* digits, unsigned ndigits, char* dest, int K, bool neg) {
273
3.22M
    int exp = absv(K + (int)ndigits - 1);
274
275
    /* write plain integer */
276
3.22M
    if(K >= 0 && (exp < (int)ndigits + 7)) {
277
693k
        memcpy(dest, digits, ndigits);
278
693k
        memset(dest + ndigits, '0', (unsigned)K);
279
693k
        memcpy(dest + ndigits + (unsigned)K, ".0", 2); /* always append .0 for naked integers */
280
693k
        return (unsigned)(ndigits + (unsigned)K + 2);
281
693k
    }
282
283
    /* write decimal w/o scientific notation */
284
2.52M
    if(K < 0 && (K > -7 || exp < 4)) {
285
2.51M
        int offset = (int)ndigits - absv(K);
286
2.51M
        if(offset <= 0) {
287
            /* fp < 1.0 -> write leading zero */
288
470k
            offset = -offset;
289
470k
            dest[0] = '0';
290
470k
            dest[1] = '.';
291
470k
            memset(dest + 2, '0', (size_t)offset);
292
470k
            memcpy(dest + offset + 2, digits, ndigits);
293
470k
            return ndigits + 2 + (unsigned)offset;
294
2.04M
        } else {
295
            /* fp > 1.0 */
296
2.04M
            memcpy(dest, digits, (size_t)offset);
297
2.04M
            dest[offset] = '.';
298
2.04M
            memcpy(dest + offset + 1, digits + offset, ndigits - (unsigned)offset);
299
2.04M
            return ndigits + 1;
300
2.04M
        }
301
2.51M
    }
302
303
    /* write decimal w/ scientific notation */
304
11.2k
    ndigits = minv(ndigits, (unsigned)(18 - neg));
305
11.2k
    unsigned idx = 0;
306
11.2k
    dest[idx++] = digits[0];
307
11.2k
    if(ndigits > 1) {
308
5.00k
        dest[idx++] = '.';
309
5.00k
        memcpy(dest + idx, digits + 1, ndigits - 1);
310
5.00k
        idx += ndigits - 1;
311
5.00k
    }
312
313
11.2k
    dest[idx++] = 'e';
314
315
11.2k
    char sign = K + (int)ndigits - 1 < 0 ? '-' : '+';
316
11.2k
    dest[idx++] = sign;
317
318
11.2k
    int cent = 0;
319
11.2k
    if(exp > 99) {
320
5.41k
        cent = exp / 100;
321
5.41k
        dest[idx++] = (char)(cent + '0');
322
5.41k
        exp -= cent * 100;
323
5.41k
    }
324
11.2k
    if(exp > 9) {
325
7.89k
        int dec = exp / 10;
326
7.89k
        dest[idx++] = (char)(dec + '0');
327
7.89k
        exp -= dec * 10;
328
329
7.89k
    } else if(cent) {
330
1.11k
        dest[idx++] = '0';
331
1.11k
    }
332
11.2k
    dest[idx++] = (char)(exp % 10 + '0');
333
11.2k
    return idx;
334
2.52M
}
335
336
3.44M
unsigned dtoa(double d, char* buffer) {
337
3.44M
    uint64_t bits = 0;
338
3.44M
    memcpy(&bits, &d, sizeof(double));
339
340
3.44M
    uint64_t mantissa = bits & ((1ull << mantissa_bits) - 1);
341
3.44M
    uint32_t exponent = (uint32_t)
342
3.44M
        ((bits >> mantissa_bits) & ((1u << exponent_bits) - 1));
343
344
3.44M
    if(exponent == 0 && mantissa == 0) {
345
225k
        memcpy(buffer, "0.0", 3);
346
225k
        return 3;
347
225k
    }
348
349
3.22M
    bool sign = ((bits >> (mantissa_bits + exponent_bits)) & 1) != 0;
350
3.22M
    unsigned pos = 0;
351
3.22M
    if(sign) {
352
5.77k
        buffer[0] = '-';
353
5.77k
        pos++;
354
5.77k
        buffer++;
355
5.77k
    }
356
357
3.22M
    if(exponent == ((1u << exponent_bits) - 1u)) {
358
0
        if(mantissa != 0) {
359
0
            memcpy(buffer, "nan", 3);
360
0
            return 3;
361
0
        } else {
362
0
            memcpy(&buffer[pos], "inf", 3);
363
0
            return pos + 3;
364
0
        }
365
0
    }
366
367
3.22M
    int K = 0;
368
3.22M
    char digits[18];
369
3.22M
    memset(digits, 0, 18);
370
3.22M
    unsigned ndigits = grisu2(bits, digits, &K);
371
3.22M
    return pos + emit_digits(digits, ndigits, buffer, K, sign);
372
3.22M
}