Coverage Report

Created: 2026-11-10 14:14

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
src/double.c
Line
Count
Source
1
#include "prism/internal/double.h"
2
3
#include "prism/internal/assert.h"
4
#include "prism/internal/bignum.h"
5
#include "prism/internal/bit.h"
6
7
#include <float.h>
8
#include <stdbool.h>
9
#include <stdint.h>
10
#include <string.h>
11
12
/* The conversion builds the bits of the result directly, so it depends on the
13
 * layout of a binary64 double. */
14
#if FLT_RADIX != 2 || DBL_MANT_DIG != 53 || DBL_MIN_EXP != -1021 || DBL_MAX_EXP != 1024
15
#error "prism requires IEEE 754 binary64 doubles"
16
#endif
17
18
/**
19
 * The number of leading significant digits that are converted exactly.
20
 */
21
234
#define PM_DOUBLE_DIGITS 768
22
23
/**
24
 * The number of words in each side of the division in the exact conversion.
25
 */
26
#define PM_DOUBLE_WORDS 84
27
28
/**
29
 * The explicit exponent stops growing once it reaches this. The literal's
30
 * digits can only shift the exponent by its length, so past this the value is
31
 * already infinity or zero, and capping it keeps the arithmetic far from
32
 * overflowing an int64_t.
33
 */
34
3.36k
#define PM_DOUBLE_EXPONENT_MAXIMUM 100000000000000000
35
36
/**
37
 * The bits of positive infinity.
38
 */
39
1.24k
#define PM_DOUBLE_INFINITY ((uint64_t) 0x7FF0000000000000)
40
41
/**
42
 * The largest power of five that fits in a word.
43
 */
44
1.28k
#define PM_DOUBLE_POWER5_13 1220703125
45
46
/**
47
 * The powers of ten that are exactly representable as doubles.
48
 */
49
static const double pm_double_powers[] = {
50
    1e0, 1e1, 1e2, 1e3, 1e4, 1e5, 1e6, 1e7, 1e8, 1e9, 1e10, 1e11,
51
    1e12, 1e13, 1e14, 1e15, 1e16, 1e17, 1e18, 1e19, 1e20, 1e21, 1e22
52
};
53
54
/**
55
 * Return the number of bits in a nonzero number.
56
 */
57
static uint64_t
58
378
pm_double_bit_length(const pm_bignum_t *bignum) {
59
378
    PRISM_ASSERT(bignum->length > 0);
  Branch (59:5): [True: 0, False: 378]
  Branch (59:5): [True: 378, False: 0]
60
378
    return (uint64_t) (bignum->length - 1) * 32 + pm_bsrll(bignum->words[bignum->length - 1]) + 1;
61
378
}
62
63
/**
64
 * Multiply the number by five to the given power, in place.
65
 */
66
static void
67
236
pm_double_multiply_powof5(pm_bignum_t *bignum, uint64_t power) {
68
1.51k
    for (; power >= 13; 
power -= 131.28k
)
pm_bignum_mul_add(bignum, 1.28k
PM_DOUBLE_POWER5_131.28k
, 0);
  Branch (68:12): [True: 1.28k, False: 236]
69
70
236
    uint32_t multiplier = 1;
71
1.64k
    for (; power > 0; 
power--1.41k
)
multiplier *= 51.41k
;
  Branch (71:12): [True: 1.41k, False: 236]
72
236
    if (multiplier > 1) 
pm_bignum_mul_add(bignum, multiplier, 0)219
;
  Branch (72:9): [True: 219, False: 17]
73
236
}
74
75
/**
76
 * Divide the numerator by the divisor, where the quotient is known to fit in 64
77
 * bits. The divisor must have at least two words and its top bit set, and the
78
 * numerator must have room for one more word above its length.
79
 *
80
 * Returns the quotient, sets inexact if the remainder is nonzero, and leaves
81
 * the remainder in the numerator's low words, untrimmed.
82
 */
83
static uint64_t
84
142
pm_double_divide(pm_bignum_t *numerator, const pm_bignum_t *divisor, bool *inexact) {
85
142
    uint32_t *numerator_words = numerator->words;
86
142
    const uint32_t *divisor_words = divisor->words;
87
142
    uint32_t numerator_length = numerator->length;
88
142
    uint32_t divisor_length = divisor->length;
89
90
142
    PRISM_ASSERT(divisor_length >= 2 && numerator_length >= divisor_length && (divisor_words[divisor_length - 1] >> 31) == 1);
  Branch (90:5): [True: 0, False: 142]
  Branch (90:5): [True: 0, False: 0]
  Branch (90:5): [True: 0, False: 0]
  Branch (90:5): [True: 142, False: 0]
  Branch (90:5): [True: 142, False: 0]
  Branch (90:5): [True: 142, False: 0]
91
92
142
    uint64_t top = divisor_words[divisor_length - 1];
93
142
    uint64_t next = divisor_words[divisor_length - 2];
94
142
    uint64_t quotient = 0;
95
142
    numerator_words[numerator_length] = 0;
96
97
568
    for (uint32_t index = numerator_length - divisor_length + 1; index > 0; 
index--426
) {
  Branch (97:66): [True: 426, False: 142]
98
426
        uint32_t *window = numerator_words + index - 1;
99
426
        uint64_t current = ((uint64_t) window[divisor_length] << 32) | window[divisor_length - 1];
100
426
        uint64_t estimate = current / top;
101
426
        uint64_t remainder = current % top;
102
103
502
        while (estimate > UINT32_MAX || 
estimate * next > ((remainder << 32) | window[divisor_length - 2])496
) {
  Branch (103:16): [True: 6, False: 496]
  Branch (103:41): [True: 91, False: 405]
104
97
            estimate--;
105
97
            remainder += top;
106
97
            if (remainder > UINT32_MAX) 
break21
;
  Branch (106:17): [True: 21, False: 76]
107
97
        }
108
109
        /* A difference below zero wraps around, which sets its top bit. */
110
426
        uint64_t carry = 0;
111
426
        uint64_t borrow = 0;
112
113
3.53k
        for (uint32_t offset = 0; offset < divisor_length; 
offset++3.10k
) {
  Branch (113:35): [True: 3.10k, False: 426]
114
3.10k
            uint64_t product = estimate * divisor_words[offset] + carry;
115
3.10k
            carry = product >> 32;
116
117
3.10k
            uint64_t difference = (uint64_t) window[offset] - (uint32_t) product - borrow;
118
3.10k
            window[offset] = (uint32_t) difference;
119
3.10k
            borrow = difference >> 63;
120
3.10k
        }
121
122
426
        uint64_t difference = (uint64_t) window[divisor_length] - carry - borrow;
123
426
        window[divisor_length] = (uint32_t) difference;
124
125
426
        if ((difference >> 63) != 0) {
  Branch (125:13): [True: 4, False: 422]
126
4
            estimate--;
127
4
            carry = 0;
128
129
21
            for (uint32_t offset = 0; offset < divisor_length; 
offset++17
) {
  Branch (129:39): [True: 17, False: 4]
130
17
                uint64_t sum = (uint64_t) window[offset] + divisor_words[offset] + carry;
131
17
                window[offset] = (uint32_t) sum;
132
17
                carry = sum >> 32;
133
17
            }
134
135
4
            window[divisor_length] += (uint32_t) carry;
136
4
        }
137
138
426
        PRISM_ASSERT((quotient >> 32) == 0);
  Branch (138:9): [True: 0, False: 426]
  Branch (138:9): [True: 426, False: 0]
139
426
        quotient = (quotient << 32) | estimate;
140
426
    }
141
142
168
    
for (uint32_t index = 0; 142
index < divisor_length;
index++26
) {
  Branch (142:30): [True: 167, False: 1]
143
167
        if (numerator_words[index] != 0) {
  Branch (143:13): [True: 141, False: 26]
144
141
            *inexact = true;
145
141
            break;
146
141
        }
147
167
    }
148
149
142
    return quotient;
150
142
}
151
152
/**
153
 * Return the bits of the double nearest to (mantissa + fraction) * 2**exponent,
154
 * rounding ties to even, where the fraction is in [0, 1) and inexact says
155
 * whether it is nonzero. When inexact is set the mantissa must have more bits
156
 * than a double keeps, so that the fraction only ever breaks ties.
157
 */
158
static uint64_t
159
236
pm_double_round(uint64_t mantissa, int64_t exponent, bool inexact) {
160
236
    PRISM_ASSERT(mantissa != 0);
  Branch (160:5): [True: 0, False: 236]
  Branch (160:5): [True: 236, False: 0]
161
162
    /* Drop the bits below the 53 that a double keeps, or below the smallest
163
     * subnormal if the value is that small. */
164
236
    int64_t shift = (int64_t) pm_bsrll(mantissa) + 1 - 53;
165
236
    if (shift < -1074 - exponent) 
shift = -1074 - exponent0
;
  Branch (165:9): [True: 0, False: 236]
166
167
236
    uint64_t bits;
168
236
    if (shift <= 0) {
  Branch (168:9): [True: 0, False: 236]
169
0
        PRISM_ASSERT(!inexact);
  Branch (169:9): [True: 0, False: 0]
  Branch (169:9): [True: 0, False: 0]
170
0
        bits = mantissa << -shift;
171
236
    } else if (shift > 64) {
  Branch (171:16): [True: 0, False: 236]
172
0
        return 0;
173
236
    } else {
174
236
        uint64_t half = (uint64_t) 1 << (shift - 1);
175
236
        uint64_t rest = mantissa & (half - 1 + half);
176
236
        bits = shift == 64 ? 
00
: mantissa >> shift;
  Branch (176:16): [True: 0, False: 236]
177
236
        if (rest > half || 
(76
rest == half76
&&
(5
inexact5
||
(bits & 1)3
)))
bits++165
;
  Branch (177:13): [True: 160, False: 76]
  Branch (177:29): [True: 5, False: 71]
  Branch (177:46): [True: 2, False: 3]
  Branch (177:57): [True: 3, False: 0]
178
236
    }
179
180
    /* The bits hold the significand with its implicit bit, so adding the
181
     * exponent field on top makes that bit carry into it. That also moves a
182
     * subnormal that rounded up into the normal range, and a significand that
183
     * rounded up to 2**53 into the next binade. */
184
236
    int64_t scale = exponent + shift;
185
236
    if (scale > 971) 
return 10
PM_DOUBLE_INFINITY10
;
  Branch (185:9): [True: 10, False: 226]
186
187
226
    bits += (uint64_t) (scale + 1074) << 52;
188
226
    return bits < PM_DOUBLE_INFINITY ? bits : 
PM_DOUBLE_INFINITY0
;
  Branch (188:12): [True: 226, False: 0]
189
236
}
190
191
/**
192
 * Return the bits of the double nearest to the significant digits at start
193
 * times 10**exponent. When there are at most 19 digits, value holds them.
194
 */
195
static uint64_t
196
236
pm_double_exact(const uint8_t *start, uint32_t digits, int64_t exponent, uint64_t value) {
197
236
    uint32_t numerator_words[PM_DOUBLE_WORDS] = { 0 };
198
236
    pm_bignum_t numerator = { .length = 0, .words = numerator_words };
199
200
236
    if (digits <= 19) {
  Branch (200:9): [True: 119, False: 117]
201
119
        numerator_words[0] = (uint32_t) value;
202
119
        numerator_words[1] = (uint32_t) (value >> 32);
203
119
        numerator.length = value > UINT32_MAX ? 
216
:
1103
;
  Branch (203:28): [True: 16, False: 103]
204
119
    } else {
205
117
        uint32_t remaining = digits > PM_DOUBLE_DIGITS ? 
PM_DOUBLE_DIGITS0
: digits;
  Branch (205:30): [True: 0, False: 117]
206
117
        uint32_t chunk = 0;
207
117
        uint32_t multiplier = 1;
208
209
10.5k
        for (const uint8_t *cursor = start; remaining > 0; 
cursor++10.4k
) {
  Branch (209:45): [True: 10.4k, False: 117]
210
10.4k
            if (*cursor < '0' || 
*cursor > '9'10.3k
)
continue1.23k
;
  Branch (210:17): [True: 109, False: 10.3k]
  Branch (210:34): [True: 1.12k, False: 9.24k]
211
212
9.24k
            chunk = chunk * 10 + (uint32_t) (*cursor - '0');
213
9.24k
            multiplier *= 10;
214
9.24k
            remaining--;
215
216
9.24k
            if (multiplier == 1000000000) {
  Branch (216:17): [True: 979, False: 8.26k]
217
979
                pm_bignum_mul_add(&numerator, multiplier, chunk);
218
979
                chunk = 0;
219
979
                multiplier = 1;
220
979
            }
221
9.24k
        }
222
223
117
        if (digits > PM_DOUBLE_DIGITS) {
  Branch (223:13): [True: 0, False: 117]
224
0
            chunk = chunk * 10 + 1;
225
0
            multiplier *= 10;
226
0
            exponent += (int64_t) (digits - PM_DOUBLE_DIGITS) - 1;
227
0
        }
228
229
117
        if (multiplier > 1) 
pm_bignum_mul_add(&numerator, multiplier, chunk)103
;
  Branch (229:13): [True: 103, False: 14]
230
117
    }
231
232
236
    bool inexact = false;
233
236
    uint64_t mantissa;
234
236
    int64_t binary;
235
236
    /* Since 10**exp is 5**exp * 2**exp, only the power of five needs
237
     * arithmetic; the power of 2 goes into the binary exponent. */
238
236
    if (exponent >= 0) {
  Branch (238:9): [True: 94, False: 142]
239
        /* Multiply by the power of five and round the top 64 bits. */
240
94
        pm_double_multiply_powof5(&numerator, (uint64_t) exponent);
241
242
94
        uint64_t length = pm_double_bit_length(&numerator);
243
94
        binary = exponent;
244
245
94
        if (length > 64) {
  Branch (245:13): [True: 91, False: 3]
246
91
            uint64_t dropped = length - 64;
247
91
            inexact = pm_bignum_trailing_zeros(&numerator) < dropped;
248
91
            pm_bignum_shift_right(&numerator, (size_t) dropped);
249
91
            binary += (int64_t) dropped;
250
91
        }
251
252
94
        mantissa = pm_bignum_uint64(&numerator);
253
142
    } else {
254
        /* Divide by the power of five and round the 64-bit quotient. */
255
142
        uint32_t denominator_words[PM_DOUBLE_WORDS];
256
142
        pm_bignum_t denominator = { .length = 1, .words = denominator_words };
257
258
142
        denominator_words[0] = 1;
259
142
        pm_double_multiply_powof5(&denominator, (uint64_t) -exponent);
260
261
        /* Shift the denominator so that its top bit is set and it has at least
262
         * two words, and the numerator so that it has 63 more bits than the
263
         * denominator. If the numerator is already longer than that, the
264
         * denominator shifts by more whole words instead. */
265
142
        int64_t numerator_bits = (int64_t) pm_double_bit_length(&numerator);
266
142
        int64_t denominator_bits = (int64_t) pm_double_bit_length(&denominator);
267
268
142
        int64_t denominator_shift = (32 - denominator_bits % 32) % 32;
269
142
        if (denominator_bits + denominator_shift == 32) 
denominator_shift += 3227
;
  Branch (269:13): [True: 27, False: 115]
270
271
142
        int64_t numerator_shift = 63 + denominator_bits + denominator_shift - numerator_bits;
272
142
        if (numerator_shift < 0) {
  Branch (272:13): [True: 56, False: 86]
273
56
            denominator_shift += (31 - numerator_shift) / 32 * 32;
274
56
            numerator_shift = 63 + denominator_bits + denominator_shift - numerator_bits;
275
56
        }
276
277
142
        pm_bignum_shift_left(&numerator, (size_t) numerator_shift);
278
142
        pm_bignum_shift_left(&denominator, (size_t) denominator_shift);
279
142
        PRISM_ASSERT(numerator.length < PM_DOUBLE_WORDS && denominator.length <= PM_DOUBLE_WORDS);
  Branch (279:9): [True: 0, False: 142]
  Branch (279:9): [True: 0, False: 0]
  Branch (279:9): [True: 142, False: 0]
  Branch (279:9): [True: 142, False: 0]
280
281
142
        mantissa = pm_double_divide(&numerator, &denominator, &inexact);
282
142
        binary = exponent + denominator_shift - numerator_shift;
283
142
    }
284
285
236
    return pm_double_round(mantissa, binary, inexact);
286
236
}
287
288
/**
289
 * Parse a decimal float literal into the nearest double, rounding ties to even.
290
 */
291
double
292
3.31k
pm_double_parse(const uint8_t *start, const uint8_t *end, bool *out_of_range) {
293
3.31k
    *out_of_range = false;
294
3.31k
    if (start < end && *start == '+') 
start++3
;
  Branch (294:9): [True: 3.31k, False: 0]
  Branch (294:24): [True: 3, False: 3.31k]
295
296
3.31k
    const uint8_t *first = NULL;
297
3.31k
    const uint8_t *cursor = start;
298
299
3.31k
    uint32_t digits = 0;
300
3.31k
    uint32_t zeros = 0;
301
302
3.31k
    uint64_t value = 0;
303
3.31k
    int64_t exponent = 0;
304
305
3.31k
    bool fraction = false;
306
3.31k
    bool e = false;
307
308
24.6k
    for (; !e && 
cursor < end24.2k
;
cursor++21.3k
) {
  Branch (308:12): [True: 24.2k, False: 482]
  Branch (308:18): [True: 21.3k, False: 2.83k]
309
21.3k
        switch (*cursor) {
310
1.38k
            case '_':
  Branch (310:13): [True: 1.38k, False: 19.9k]
311
1.38k
                break;
312
2.91k
            case '.':
  Branch (312:13): [True: 2.91k, False: 18.4k]
313
2.91k
                fraction = true;
314
2.91k
                break;
315
482
            
case 'e': 437
case 'E':
  Branch (315:13): [True: 437, False: 20.9k]
  Branch (315:23): [True: 45, False: 21.3k]
316
482
                e = true;
317
482
                break;
318
3.91k
            case '0':
  Branch (318:13): [True: 3.91k, False: 17.4k]
319
3.91k
                if (fraction) 
exponent--1.90k
;
  Branch (319:21): [True: 1.90k, False: 2.01k]
320
3.91k
                if (first != NULL) 
zeros++1.18k
;
  Branch (320:21): [True: 1.18k, False: 2.73k]
321
3.91k
                break;
322
11.3k
            
case '1': 8.52k
case '2': 9.04k
case '3': 9.36k
case '4': 11.0k
case '5':
  Branch (322:13): [True: 8.52k, False: 12.8k]
  Branch (322:23): [True: 518, False: 20.8k]
  Branch (322:33): [True: 328, False: 21.0k]
  Branch (322:43): [True: 1.70k, False: 19.6k]
  Branch (322:53): [True: 326, False: 21.0k]
323
12.6k
            
case '6': 11.8k
case '7': 12.0k
case '8': 12.3k
case '9':
  Branch (323:13): [True: 498, False: 20.8k]
  Branch (323:23): [True: 115, False: 21.2k]
  Branch (323:33): [True: 354, False: 21.0k]
  Branch (323:43): [True: 307, False: 21.0k]
324
12.6k
                if (fraction) 
exponent--7.42k
;
  Branch (324:21): [True: 7.42k, False: 5.23k]
325
12.6k
                if (first == NULL) 
first = cursor2.27k
;
  Branch (325:21): [True: 2.27k, False: 10.3k]
326
327
12.6k
                digits += zeros + 1;
328
12.6k
                if (digits <= 19) {
  Branch (328:21): [True: 5.61k, False: 7.05k]
329
5.74k
                    for (; zeros > 0; 
zeros--134
)
value *= 10134
;
  Branch (329:28): [True: 134, False: 5.61k]
330
5.61k
                    value = value * 10 + (uint64_t) (*cursor - '0');
331
7.05k
                } else {
332
7.05k
                    zeros = 0;
333
7.05k
                }
334
12.6k
                break;
335
0
            default:
  Branch (335:13): [True: 0, False: 21.3k]
336
0
                PRISM_ASSERT(false && "unreachable");
  Branch (336:17): [Folded, False: 0]
  Branch (336:17): [True: 0, False: 0]
  Branch (336:17): [Folded, False: 0]
  Branch (336:17): [True: 0, False: 0]
337
0
                break;
338
21.3k
        }
339
21.3k
    }
340
341
3.31k
    exponent += (int64_t) zeros;
342
3.31k
    if (digits == 0) 
return 0.01.03k
;
  Branch (342:9): [True: 1.03k, False: 2.27k]
343
344
2.27k
    if (e) {
  Branch (344:9): [True: 446, False: 1.83k]
345
446
        bool positive = true;
346
446
        if (cursor < end && (*cursor == '+' || 
*cursor == '-'440
)) {
  Branch (346:13): [True: 446, False: 0]
  Branch (346:30): [True: 6, False: 440]
  Branch (346:48): [True: 49, False: 391]
347
55
            positive = *cursor == '+';
348
55
            cursor++;
349
55
        }
350
351
446
        int64_t power = 0;
352
3.98k
        for (; cursor < end; 
cursor++3.53k
) {
  Branch (352:16): [True: 3.53k, False: 446]
353
3.53k
            if (*cursor != '_' && 
power < 3.36k
PM_DOUBLE_EXPONENT_MAXIMUM3.36k
)
power = power * 10 + (*cursor - '0')2.39k
;
  Branch (353:17): [True: 3.36k, False: 176]
  Branch (353:35): [True: 2.39k, False: 967]
354
3.53k
        }
355
356
446
        exponent += positive ? 
power397
:
-power49
;
  Branch (356:21): [True: 397, False: 49]
357
446
    }
358
359
2.27k
    int64_t magnitude = (int64_t) digits + exponent;
360
2.27k
    uint64_t bits;
361
362
2.27k
    if (magnitude > 309) {
  Branch (362:9): [True: 174, False: 2.10k]
363
174
        bits = PM_DOUBLE_INFINITY;
364
2.10k
    } else if (magnitude < -323) {
  Branch (364:16): [True: 15, False: 2.08k]
365
15
        bits = 0;
366
2.08k
    } else {
367
        /* When the digits and the power of ten are both exactly representable,
368
         * a single correctly rounded multiplication or division gives the
369
         * result. When the power is too large but the digits have room, part
370
         * of the power moves into the digits. This depends on the operation
371
         * rounding once, to a double, rather than to a wider type first. */
372
2.08k
#if defined(FLT_EVAL_METHOD) && FLT_EVAL_METHOD == 0
373
2.08k
        if (digits <= 19 && 
value <= ((uint64_t) 1 << 53)1.97k
) {
  Branch (373:13): [True: 1.97k, False: 117]
  Branch (373:29): [True: 1.95k, False: 15]
374
1.95k
            if (exponent >= -22 && 
exponent < 01.93k
) {
  Branch (374:17): [True: 1.93k, False: 23]
  Branch (374:36): [True: 1.04k, False: 894]
375
1.04k
                return (double) value / pm_double_powers[-exponent];
376
1.04k
            } else 
if (917
exponent >= 0917
&&
exponent <= 22894
) {
  Branch (376:24): [True: 894, False: 23]
  Branch (376:41): [True: 793, False: 101]
377
793
                return (double) value * pm_double_powers[exponent];
378
793
            } else 
if (124
exponent > 22124
&&
exponent <= 22 + 15101
) {
  Branch (378:24): [True: 101, False: 23]
  Branch (378:41): [True: 21, False: 80]
379
21
                uint64_t scaled = value;
380
144
                for (int64_t power = exponent; power > 22 && 
scaled <= ((uint64_t) 1 << 53)124
;
power--123
)
scaled *= 10123
;
  Branch (380:48): [True: 124, False: 20]
  Branch (380:62): [True: 123, False: 1]
381
21
                if (scaled <= ((uint64_t) 1 << 53)) 
return (double) scaled * 1e2220
;
  Branch (381:21): [True: 20, False: 1]
382
21
            }
383
1.95k
        }
384
236
#endif
385
386
236
        bits = pm_double_exact(first, digits, exponent, value);
387
236
    }
388
389
425
    *out_of_range = bits == 0 || 
bits == 410
PM_DOUBLE_INFINITY410
;
  Branch (389:21): [True: 15, False: 410]
  Branch (389:34): [True: 184, False: 226]
390
391
425
    double result;
392
425
    memcpy(&result, &bits, sizeof(double));
393
425
    return result;
394
2.27k
}