Coverage Report

Created: 2026-11-05 07:00

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/open62541_15/deps/dtoa.c
Line
Count
Source
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
37.7M
#define mantissa_bits 52
34
37.5M
#define exponent_bits 11
35
12.4M
#define fracmask  0x000FFFFFFFFFFFFFU
36
12.4M
#define expmask   0x7FF0000000000000U
37
56.3M
#define hiddenbit 0x0010000000000000U
38
//#define signmask  0x8000000000000000U
39
12.4M
#define expbias   (1023 + 52)
40
41
16.4M
#define absv(n) ((n) < 0 ? -(n) : (n))
42
266k
#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
12.4M
#define npowers     87
55
24.8M
#define steppowers  8
56
24.8M
#define firstpower -348 /* 10 ^ -348 */
57
12.4M
#define expmax     -32
58
37.2M
#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
12.4M
find_cachedpow10(int exp, int* k) {
114
12.4M
    const double one_log_ten = 0.30102999566398114;
115
12.4M
    int approx = (int)(-(exp + npowers) * one_log_ten);
116
12.4M
    int idx = (approx - firstpower) / steppowers;
117
37.2M
    while(1) {
118
37.2M
        int current = exp + powers_ten[idx].exp + 64;
119
37.2M
        if(current < expmin) {
120
24.7M
            idx++;
121
24.7M
            continue;
122
24.7M
        }
123
12.4M
        if(current > expmax) {
124
0
            idx--;
125
0
            continue;
126
0
        }
127
12.4M
        *k = (firstpower + idx * steppowers);
128
12.4M
        return powers_ten[idx];
129
12.4M
    }
130
12.4M
}
131
132
12.4M
static Fp build_fp(uint64_t bits) {
133
12.4M
    Fp fp;
134
12.4M
    fp.frac = bits & fracmask;
135
12.4M
    fp.exp = (bits & expmask) >> 52;
136
12.4M
    if(fp.exp) {
137
12.2M
        fp.frac += hiddenbit;
138
12.2M
        fp.exp -= expbias;
139
12.2M
    } else {
140
135k
        fp.exp = -expbias + 1;
141
135k
    }
142
12.4M
    return fp;
143
12.4M
}
144
145
12.4M
static void normalize(Fp* fp) {
146
15.8M
    while((fp->frac & hiddenbit) == 0) {
147
3.39M
        fp->frac <<= 1;
148
3.39M
        fp->exp--;
149
3.39M
    }
150
12.4M
    int shift = 64 - 52 - 1;
151
12.4M
    fp->frac <<= shift;
152
12.4M
    fp->exp -= shift;
153
12.4M
}
154
155
12.4M
static void get_normalized_boundaries(Fp* fp, Fp* lower, Fp* upper) {
156
12.4M
    upper->frac = (fp->frac << 1) + 1;
157
12.4M
    upper->exp  = fp->exp - 1;
158
15.8M
    while ((upper->frac & (hiddenbit << 1)) == 0) {
159
3.39M
        upper->frac <<= 1;
160
3.39M
        upper->exp--;
161
3.39M
    }
162
163
12.4M
    int u_shift = 64 - 52 - 2;
164
12.4M
    upper->frac <<= u_shift;
165
12.4M
    upper->exp = upper->exp - u_shift;
166
167
12.4M
    int l_shift = fp->frac == hiddenbit ? 2 : 1;
168
12.4M
    lower->frac = (fp->frac << l_shift) - 1;
169
12.4M
    lower->exp = fp->exp - l_shift;
170
12.4M
    lower->frac <<= lower->exp - upper->exp;
171
12.4M
    lower->exp = upper->exp;
172
12.4M
}
173
174
37.2M
static Fp multiply(Fp* a, Fp* b) {
175
37.2M
    const uint64_t lomask = 0x00000000FFFFFFFF;
176
37.2M
    uint64_t ah_bl = (a->frac >> 32)    * (b->frac & lomask);
177
37.2M
    uint64_t al_bh = (a->frac & lomask) * (b->frac >> 32);
178
37.2M
    uint64_t al_bl = (a->frac & lomask) * (b->frac & lomask);
179
37.2M
    uint64_t ah_bh = (a->frac >> 32)    * (b->frac >> 32);
180
37.2M
    uint64_t tmp = (ah_bl & lomask) + (al_bh & lomask) + (al_bl >> 32); 
181
    /* round up */
182
37.2M
    tmp += 1U << 31;
183
37.2M
    Fp fp;
184
37.2M
    fp.frac = ah_bh + (ah_bl >> 32) + (al_bh >> 32) + (tmp >> 32);
185
37.2M
    fp.exp = a->exp + b->exp + 64;
186
37.2M
    return fp;
187
37.2M
}
188
189
static void round_digit(char* digits, unsigned ndigits, uint64_t delta,
190
12.4M
                        uint64_t rem, uint64_t kappa, uint64_t frac) {
191
15.1M
    while(rem < frac && delta - rem >= kappa &&
192
3.53M
          (rem + kappa < frac || frac - rem > rem + kappa - frac)) {
193
2.73M
        digits[ndigits - 1]--;
194
2.73M
        rem += kappa;
195
2.73M
    }
196
12.4M
}
197
198
12.4M
static unsigned generate_digits(Fp* fp, Fp* upper, Fp* lower, char* digits, int* K) {
199
12.4M
    uint64_t wfrac = upper->frac - fp->frac;
200
12.4M
    uint64_t delta = upper->frac - lower->frac;
201
202
12.4M
    Fp one;
203
12.4M
    one.frac = 1ULL << -upper->exp;
204
12.4M
    one.exp  = upper->exp;
205
206
12.4M
    uint64_t part1 = upper->frac >> -one.exp;
207
12.4M
    uint64_t part2 = upper->frac & (one.frac - 1);
208
209
12.4M
    unsigned idx = 0;
210
12.4M
    int kappa = 10;
211
12.4M
    uint64_t* divp;
212
213
    /* 1000000000 */
214
94.0M
    for(divp = tens + 10; kappa > 0; divp++) {
215
90.2M
        uint64_t div = *divp;
216
90.2M
        uint64_t digit = part1 / div;
217
90.2M
        if(digit || idx) {
218
28.0M
            digits[idx++] = (char)(digit + '0');
219
28.0M
        }
220
221
90.2M
        part1 -= digit * div;
222
90.2M
        kappa--;
223
224
90.2M
        uint64_t tmp = (part1 <<-one.exp) + part2;
225
90.2M
        if(tmp <= delta) {
226
8.63M
            *K += kappa;
227
8.63M
            round_digit(digits, idx, delta, tmp, div << -one.exp, wfrac);
228
8.63M
            return idx;
229
8.63M
        }
230
90.2M
    }
231
232
    /* 10 */
233
3.78M
    uint64_t* unit = tens + 18;
234
44.0M
    while(true) {
235
44.0M
        part2 *= 10;
236
44.0M
        delta *= 10;
237
44.0M
        kappa--;
238
239
44.0M
        uint64_t digit = part2 >> -one.exp;
240
44.0M
        if(digit || idx) {
241
44.0M
            digits[idx++] = (char)(digit + '0');
242
44.0M
        }
243
244
44.0M
        part2 &= one.frac - 1;
245
44.0M
        if(part2 < delta) {
246
3.78M
            *K += kappa;
247
3.78M
            round_digit(digits, idx, delta, part2, one.frac, wfrac * *unit);
248
3.78M
            break;
249
3.78M
        }
250
40.2M
        unit--;
251
40.2M
    }
252
3.78M
    return idx;
253
12.4M
}
254
255
12.4M
static unsigned grisu2(uint64_t bits, char* digits, int* K) {
256
12.4M
    Fp w = build_fp(bits);
257
12.4M
    Fp lower, upper;
258
12.4M
    get_normalized_boundaries(&w, &lower, &upper);
259
12.4M
    normalize(&w);
260
12.4M
    int k;
261
12.4M
    Fp cp = find_cachedpow10(upper.exp, &k);
262
12.4M
    w     = multiply(&w,     &cp);
263
12.4M
    upper = multiply(&upper, &cp);
264
12.4M
    lower = multiply(&lower, &cp);
265
12.4M
    lower.frac++;
266
12.4M
    upper.frac--;
267
12.4M
    *K = -k;
268
12.4M
    return generate_digits(&w, &upper, &lower, digits, K);
269
12.4M
}
270
271
static unsigned
272
12.4M
emit_digits(char* digits, unsigned ndigits, char* dest, int K, bool neg) {
273
12.4M
    int exp = absv(K + (int)ndigits - 1);
274
275
    /* write plain integer */
276
12.4M
    if(K >= 0 && (exp < (int)ndigits + 7)) {
277
8.17M
        memcpy(dest, digits, ndigits);
278
8.17M
        memset(dest + ndigits, '0', (unsigned)K);
279
8.17M
        memcpy(dest + ndigits + (unsigned)K, ".0", 2); /* always append .0 for naked integers */
280
8.17M
        return (unsigned)(ndigits + (unsigned)K + 2);
281
8.17M
    }
282
283
    /* write decimal w/o scientific notation */
284
4.25M
    if(K < 0 && (K > -7 || exp < 4)) {
285
3.98M
        int offset = (int)ndigits - absv(K);
286
3.98M
        if(offset <= 0) {
287
            /* fp < 1.0 -> write leading zero */
288
766k
            offset = -offset;
289
766k
            dest[0] = '0';
290
766k
            dest[1] = '.';
291
766k
            memset(dest + 2, '0', (size_t)offset);
292
766k
            memcpy(dest + offset + 2, digits, ndigits);
293
766k
            return ndigits + 2 + (unsigned)offset;
294
3.21M
        } else {
295
            /* fp > 1.0 */
296
3.21M
            memcpy(dest, digits, (size_t)offset);
297
3.21M
            dest[offset] = '.';
298
3.21M
            memcpy(dest + offset + 1, digits + offset, ndigits - (unsigned)offset);
299
3.21M
            return ndigits + 1;
300
3.21M
        }
301
3.98M
    }
302
303
    /* write decimal w/ scientific notation */
304
266k
    ndigits = minv(ndigits, (unsigned)(18 - neg));
305
266k
    unsigned idx = 0;
306
266k
    dest[idx++] = digits[0];
307
266k
    if(ndigits > 1) {
308
253k
        dest[idx++] = '.';
309
253k
        memcpy(dest + idx, digits + 1, ndigits - 1);
310
253k
        idx += ndigits - 1;
311
253k
    }
312
313
266k
    dest[idx++] = 'e';
314
315
266k
    char sign = K + (int)ndigits - 1 < 0 ? '-' : '+';
316
266k
    dest[idx++] = sign;
317
318
266k
    int cent = 0;
319
266k
    if(exp > 99) {
320
234k
        cent = exp / 100;
321
234k
        dest[idx++] = (char)(cent + '0');
322
234k
        exp -= cent * 100;
323
234k
    }
324
266k
    if(exp > 9) {
325
164k
        int dec = exp / 10;
326
164k
        dest[idx++] = (char)(dec + '0');
327
164k
        exp -= dec * 10;
328
329
164k
    } else if(cent) {
330
97.0k
        dest[idx++] = '0';
331
97.0k
    }
332
266k
    dest[idx++] = (char)(exp % 10 + '0');
333
266k
    return idx;
334
4.25M
}
335
336
12.6M
unsigned dtoa(double d, char* buffer) {
337
12.6M
    uint64_t bits = 0;
338
12.6M
    memcpy(&bits, &d, sizeof(double));
339
340
12.6M
    uint64_t mantissa = bits & ((1ull << mantissa_bits) - 1);
341
12.6M
    uint32_t exponent = (uint32_t)
342
12.6M
        ((bits >> mantissa_bits) & ((1u << exponent_bits) - 1));
343
344
12.6M
    if(exponent == 0 && mantissa == 0) {
345
244k
        memcpy(buffer, "0.0", 3);
346
244k
        return 3;
347
244k
    }
348
349
12.6M
    bool sign = ((bits >> (mantissa_bits + exponent_bits)) & 1) != 0;
350
12.4M
    unsigned pos = 0;
351
12.4M
    if(sign) {
352
24.3k
        buffer[0] = '-';
353
24.3k
        pos++;
354
24.3k
    }
355
356
12.4M
    if(exponent == ((1u << exponent_bits) - 1u)) {
357
0
        if(mantissa != 0) {
358
0
            memcpy(buffer, "nan", 3);
359
0
            return 3;
360
0
        } else {
361
0
            memcpy(&buffer[pos], "inf", 3);
362
0
            return pos + 3;
363
0
        }
364
0
    }
365
366
12.4M
    int K = 0;
367
12.4M
    char digits[18];
368
12.4M
    memset(digits, 0, 18);
369
12.4M
    unsigned ndigits = grisu2(bits, digits, &K);
370
12.4M
    return pos + emit_digits(digits, ndigits, &buffer[pos], K, sign);
371
12.4M
}