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
58
#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
331
#define PM_DOUBLE_EXPONENT_MAXIMUM 100000000000000000
35
36
/**
37
 * The bits of positive infinity.
38
 */
39
271
#define PM_DOUBLE_INFINITY ((uint64_t) 0x7FF0000000000000)
40
41
/**
42
 * The largest power of five that fits in a word.
43
 */
44
257
#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
94
pm_double_bit_length(const pm_bignum_t *bignum) {
59
94
    PRISM_ASSERT(bignum->length > 0);
  Branch (59:5): [True: 0, False: 94]
  Branch (59:5): [True: 94, False: 0]
60
94
    return (uint64_t) (bignum->length - 1) * 32 + pm_bsrll(bignum->words[bignum->length - 1]) + 1;
61
94
}
62
63
/**
64
 * Multiply the number by five to the given power, in place.
65
 */
66
static void
67
74
pm_double_multiply_powof5(pm_bignum_t *bignum, uint64_t power) {
68
331
    for (; power >= 13; 
power -= 13257
)
pm_bignum_mul_add(bignum, 257
PM_DOUBLE_POWER5_13257
, 0);
  Branch (68:12): [True: 257, False: 74]
69
70
74
    uint32_t multiplier = 1;
71
523
    for (; power > 0; 
power--449
)
multiplier *= 5449
;
  Branch (71:12): [True: 449, False: 74]
72
74
    if (multiplier > 1) 
pm_bignum_mul_add(bignum, multiplier, 0)59
;
  Branch (72:9): [True: 59, False: 15]
73
74
}
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
20
pm_double_divide(pm_bignum_t *numerator, const pm_bignum_t *divisor, bool *inexact) {
85
20
    uint32_t *numerator_words = numerator->words;
86
20
    const uint32_t *divisor_words = divisor->words;
87
20
    uint32_t numerator_length = numerator->length;
88
20
    uint32_t divisor_length = divisor->length;
89
90
20
    PRISM_ASSERT(divisor_length >= 2 && numerator_length >= divisor_length && (divisor_words[divisor_length - 1] >> 31) == 1);
  Branch (90:5): [True: 0, False: 20]
  Branch (90:5): [True: 0, False: 0]
  Branch (90:5): [True: 0, False: 0]
  Branch (90:5): [True: 20, False: 0]
  Branch (90:5): [True: 20, False: 0]
  Branch (90:5): [True: 20, False: 0]
91
92
20
    uint64_t top = divisor_words[divisor_length - 1];
93
20
    uint64_t next = divisor_words[divisor_length - 2];
94
20
    uint64_t quotient = 0;
95
20
    numerator_words[numerator_length] = 0;
96
97
80
    for (uint32_t index = numerator_length - divisor_length + 1; index > 0; 
index--60
) {
  Branch (97:66): [True: 60, False: 20]
98
60
        uint32_t *window = numerator_words + index - 1;
99
60
        uint64_t current = ((uint64_t) window[divisor_length] << 32) | window[divisor_length - 1];
100
60
        uint64_t estimate = current / top;
101
60
        uint64_t remainder = current % top;
102
103
68
        while (estimate > UINT32_MAX || estimate * next > ((remainder << 32) | window[divisor_length - 2])) {
  Branch (103:16): [True: 0, False: 68]
  Branch (103:41): [True: 12, False: 56]
104
12
            estimate--;
105
12
            remainder += top;
106
12
            if (remainder > UINT32_MAX) 
break4
;
  Branch (106:17): [True: 4, False: 8]
107
12
        }
108
109
        /* A difference below zero wraps around, which sets its top bit. */
110
60
        uint64_t carry = 0;
111
60
        uint64_t borrow = 0;
112
113
195
        for (uint32_t offset = 0; offset < divisor_length; 
offset++135
) {
  Branch (113:35): [True: 135, False: 60]
114
135
            uint64_t product = estimate * divisor_words[offset] + carry;
115
135
            carry = product >> 32;
116
117
135
            uint64_t difference = (uint64_t) window[offset] - (uint32_t) product - borrow;
118
135
            window[offset] = (uint32_t) difference;
119
135
            borrow = difference >> 63;
120
135
        }
121
122
60
        uint64_t difference = (uint64_t) window[divisor_length] - carry - borrow;
123
60
        window[divisor_length] = (uint32_t) difference;
124
125
60
        if ((difference >> 63) != 0) {
  Branch (125:13): [True: 0, False: 60]
126
0
            estimate--;
127
0
            carry = 0;
128
129
0
            for (uint32_t offset = 0; offset < divisor_length; offset++) {
  Branch (129:39): [True: 0, False: 0]
130
0
                uint64_t sum = (uint64_t) window[offset] + divisor_words[offset] + carry;
131
0
                window[offset] = (uint32_t) sum;
132
0
                carry = sum >> 32;
133
0
            }
134
135
0
            window[divisor_length] += (uint32_t) carry;
136
0
        }
137
138
60
        PRISM_ASSERT((quotient >> 32) == 0);
  Branch (138:9): [True: 0, False: 60]
  Branch (138:9): [True: 60, False: 0]
139
60
        quotient = (quotient << 32) | estimate;
140
60
    }
141
142
24
    
for (uint32_t index = 0; 20
index < divisor_length;
index++4
) {
  Branch (142:30): [True: 23, False: 1]
143
23
        if (numerator_words[index] != 0) {
  Branch (143:13): [True: 19, False: 4]
144
19
            *inexact = true;
145
19
            break;
146
19
        }
147
23
    }
148
149
20
    return quotient;
150
20
}
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
74
pm_double_round(uint64_t mantissa, int64_t exponent, bool inexact) {
160
74
    PRISM_ASSERT(mantissa != 0);
  Branch (160:5): [True: 0, False: 74]
  Branch (160:5): [True: 74, 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
74
    int64_t shift = (int64_t) pm_bsrll(mantissa) + 1 - 53;
165
74
    if (shift < -1074 - exponent) 
shift = -1074 - exponent0
;
  Branch (165:9): [True: 0, False: 74]
166
167
74
    uint64_t bits;
168
74
    if (shift <= 0) {
  Branch (168:9): [True: 0, False: 74]
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
74
    } else if (shift > 64) {
  Branch (171:16): [True: 0, False: 74]
172
0
        return 0;
173
74
    } else {
174
74
        uint64_t half = (uint64_t) 1 << (shift - 1);
175
74
        uint64_t rest = mantissa & (half - 1 + half);
176
74
        bits = shift == 64 ? 
00
: mantissa >> shift;
  Branch (176:16): [True: 0, False: 74]
177
74
        if (rest > half || 
(41
rest == half41
&&
(1
inexact1
||
(bits & 1)1
)))
bits++33
;
  Branch (177:13): [True: 33, False: 41]
  Branch (177:29): [True: 1, False: 40]
  Branch (177:46): [True: 0, False: 1]
  Branch (177:57): [True: 0, False: 1]
178
74
    }
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
74
    int64_t scale = exponent + shift;
185
74
    if (scale > 971) 
return 0
PM_DOUBLE_INFINITY0
;
  Branch (185:9): [True: 0, False: 74]
186
187
74
    bits += (uint64_t) (scale + 1074) << 52;
188
74
    return bits < PM_DOUBLE_INFINITY ? bits : 
PM_DOUBLE_INFINITY0
;
  Branch (188:12): [True: 74, False: 0]
189
74
}
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
74
pm_double_exact(const uint8_t *start, uint32_t digits, int64_t exponent, uint64_t value) {
197
74
    uint32_t numerator_words[PM_DOUBLE_WORDS] = { 0 };
198
74
    pm_bignum_t numerator = { .length = 0, .words = numerator_words };
199
200
74
    if (digits <= 19) {
  Branch (200:9): [True: 45, False: 29]
201
45
        numerator_words[0] = (uint32_t) value;
202
45
        numerator_words[1] = (uint32_t) (value >> 32);
203
45
        numerator.length = value > UINT32_MAX ? 
222
:
123
;
  Branch (203:28): [True: 22, False: 23]
204
45
    } else {
205
29
        uint32_t remaining = digits > PM_DOUBLE_DIGITS ? 
PM_DOUBLE_DIGITS0
: digits;
  Branch (205:30): [True: 0, False: 29]
206
29
        uint32_t chunk = 0;
207
29
        uint32_t multiplier = 1;
208
209
948
        for (const uint8_t *cursor = start; remaining > 0; 
cursor++919
) {
  Branch (209:45): [True: 919, False: 29]
210
919
            if (*cursor < '0' || 
*cursor > '9'901
)
continue18
;
  Branch (210:17): [True: 18, False: 901]
  Branch (210:34): [True: 0, False: 901]
211
212
901
            chunk = chunk * 10 + (uint32_t) (*cursor - '0');
213
901
            multiplier *= 10;
214
901
            remaining--;
215
216
901
            if (multiplier == 1000000000) {
  Branch (216:17): [True: 88, False: 813]
217
88
                pm_bignum_mul_add(&numerator, multiplier, chunk);
218
88
                chunk = 0;
219
88
                multiplier = 1;
220
88
            }
221
901
        }
222
223
29
        if (digits > PM_DOUBLE_DIGITS) {
  Branch (223:13): [True: 0, False: 29]
224
0
            chunk = chunk * 10 + 1;
225
0
            multiplier *= 10;
226
0
            exponent += (int64_t) (digits - PM_DOUBLE_DIGITS) - 1;
227
0
        }
228
229
29
        if (multiplier > 1) 
pm_bignum_mul_add(&numerator, multiplier, chunk)28
;
  Branch (229:13): [True: 28, False: 1]
230
29
    }
231
232
74
    bool inexact = false;
233
74
    uint64_t mantissa;
234
74
    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
74
    if (exponent >= 0) {
  Branch (238:9): [True: 54, False: 20]
239
        /* Multiply by the power of five and round the top 64 bits. */
240
54
        pm_double_multiply_powof5(&numerator, (uint64_t) exponent);
241
242
54
        uint64_t length = pm_double_bit_length(&numerator);
243
54
        binary = exponent;
244
245
54
        if (length > 64) {
  Branch (245:13): [True: 50, False: 4]
246
50
            uint64_t dropped = length - 64;
247
50
            inexact = pm_bignum_trailing_zeros(&numerator) < dropped;
248
50
            pm_bignum_shift_right(&numerator, (size_t) dropped);
249
50
            binary += (int64_t) dropped;
250
50
        }
251
252
54
        mantissa = pm_bignum_uint64(&numerator);
253
54
    } else {
254
        /* Divide by the power of five and round the 64-bit quotient. */
255
20
        uint32_t denominator_words[PM_DOUBLE_WORDS];
256
20
        pm_bignum_t denominator = { .length = 1, .words = denominator_words };
257
258
20
        denominator_words[0] = 1;
259
20
        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
20
        int64_t numerator_bits = (int64_t) pm_double_bit_length(&numerator);
266
20
        int64_t denominator_bits = (int64_t) pm_double_bit_length(&denominator);
267
268
20
        int64_t denominator_shift = (32 - denominator_bits % 32) % 32;
269
20
        if (denominator_bits + denominator_shift == 32) 
denominator_shift += 329
;
  Branch (269:13): [True: 9, False: 11]
270
271
20
        int64_t numerator_shift = 63 + denominator_bits + denominator_shift - numerator_bits;
272
20
        if (numerator_shift < 0) {
  Branch (272:13): [True: 4, False: 16]
273
4
            denominator_shift += (31 - numerator_shift) / 32 * 32;
274
4
            numerator_shift = 63 + denominator_bits + denominator_shift - numerator_bits;
275
4
        }
276
277
20
        pm_bignum_shift_left(&numerator, (size_t) numerator_shift);
278
20
        pm_bignum_shift_left(&denominator, (size_t) denominator_shift);
279
20
        PRISM_ASSERT(numerator.length < PM_DOUBLE_WORDS && denominator.length <= PM_DOUBLE_WORDS);
  Branch (279:9): [True: 0, False: 20]
  Branch (279:9): [True: 0, False: 0]
  Branch (279:9): [True: 20, False: 0]
  Branch (279:9): [True: 20, False: 0]
280
281
20
        mantissa = pm_double_divide(&numerator, &denominator, &inexact);
282
20
        binary = exponent + denominator_shift - numerator_shift;
283
20
    }
284
285
74
    return pm_double_round(mantissa, binary, inexact);
286
74
}
287
288
/**
289
 * Parse a decimal float literal into the nearest double, rounding ties to even.
290
 */
291
double
292
305
pm_double_parse(const uint8_t *start, const uint8_t *end, bool *out_of_range) {
293
305
    *out_of_range = false;
294
305
    if (start < end && *start == '+') 
start++0
;
  Branch (294:9): [True: 305, False: 0]
  Branch (294:24): [True: 0, False: 305]
295
296
305
    const uint8_t *first = NULL;
297
305
    const uint8_t *cursor = start;
298
299
305
    uint32_t digits = 0;
300
305
    uint32_t zeros = 0;
301
302
305
    uint64_t value = 0;
303
305
    int64_t exponent = 0;
304
305
305
    bool fraction = false;
306
305
    bool e = false;
307
308
2.79k
    for (; !e && 
cursor < end2.71k
;
cursor++2.48k
) {
  Branch (308:12): [True: 2.71k, False: 74]
  Branch (308:18): [True: 2.48k, False: 231]
309
2.48k
        switch (*cursor) {
310
4
            case '_':
  Branch (310:13): [True: 4, False: 2.48k]
311
4
                break;
312
231
            case '.':
  Branch (312:13): [True: 231, False: 2.25k]
313
231
                fraction = true;
314
231
                break;
315
74
            
case 'e': 10
case 'E':
  Branch (315:13): [True: 10, False: 2.47k]
  Branch (315:23): [True: 64, False: 2.42k]
316
74
                e = true;
317
74
                break;
318
217
            case '0':
  Branch (318:13): [True: 217, False: 2.26k]
319
217
                if (fraction) 
exponent--197
;
  Branch (319:21): [True: 197, False: 20]
320
217
                if (first != NULL) 
zeros++203
;
  Branch (320:21): [True: 203, False: 14]
321
217
                break;
322
1.37k
            
case '1': 134
case '2': 187
case '3': 297
case '4': 404
case '5':
  Branch (322:13): [True: 134, False: 2.35k]
  Branch (322:23): [True: 53, False: 2.43k]
  Branch (322:33): [True: 110, False: 2.37k]
  Branch (322:43): [True: 107, False: 2.37k]
  Branch (322:53): [True: 973, False: 1.51k]
323
1.95k
            
case '6': 1.40k
case '7': 1.40k
case '8': 1.95k
case '9':
  Branch (323:13): [True: 29, False: 2.45k]
  Branch (323:23): [True: 3, False: 2.48k]
  Branch (323:33): [True: 543, False: 1.94k]
  Branch (323:43): [True: 7, False: 2.47k]
324
1.95k
                if (fraction) 
exponent--423
;
  Branch (324:21): [True: 423, False: 1.53k]
325
1.95k
                if (first == NULL) 
first = cursor301
;
  Branch (325:21): [True: 301, False: 1.65k]
326
327
1.95k
                digits += zeros + 1;
328
1.95k
                if (digits <= 19) {
  Branch (328:21): [True: 1.55k, False: 402]
329
1.57k
                    for (; zeros > 0; 
zeros--14
)
value *= 1014
;
  Branch (329:28): [True: 14, False: 1.55k]
330
1.55k
                    value = value * 10 + (uint64_t) (*cursor - '0');
331
1.55k
                } else {
332
402
                    zeros = 0;
333
402
                }
334
1.95k
                break;
335
0
            default:
  Branch (335:13): [True: 0, False: 2.48k]
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
2.48k
        }
339
2.48k
    }
340
341
305
    exponent += (int64_t) zeros;
342
305
    if (digits == 0) 
return 0.04
;
  Branch (342:9): [True: 4, False: 301]
343
344
301
    if (e) {
  Branch (344:9): [True: 71, False: 230]
345
71
        bool positive = true;
346
71
        if (cursor < end && (*cursor == '+' || *cursor == '-')) {
  Branch (346:13): [True: 71, False: 0]
  Branch (346:30): [True: 0, False: 71]
  Branch (346:48): [True: 4, False: 67]
347
4
            positive = *cursor == '+';
348
4
            cursor++;
349
4
        }
350
351
71
        int64_t power = 0;
352
405
        for (; cursor < end; 
cursor++334
) {
  Branch (352:16): [True: 334, False: 71]
353
334
            if (*cursor != '_' && 
power < 331
PM_DOUBLE_EXPONENT_MAXIMUM331
)
power = power * 10 + (*cursor - '0')281
;
  Branch (353:17): [True: 331, False: 3]
  Branch (353:35): [True: 281, False: 50]
354
334
        }
355
356
71
        exponent += positive ? 
power67
:
-power4
;
  Branch (356:21): [True: 67, False: 4]
357
71
    }
358
359
301
    int64_t magnitude = (int64_t) digits + exponent;
360
301
    uint64_t bits;
361
362
301
    if (magnitude > 309) {
  Branch (362:9): [True: 16, False: 285]
363
16
        bits = PM_DOUBLE_INFINITY;
364
285
    } else if (magnitude < -323) {
  Branch (364:16): [True: 1, False: 284]
365
1
        bits = 0;
366
284
    } 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
284
#if defined(FLT_EVAL_METHOD) && FLT_EVAL_METHOD == 0
373
284
        if (digits <= 19 && 
value <= ((uint64_t) 1 << 53)255
) {
  Branch (373:13): [True: 255, False: 29]
  Branch (373:29): [True: 247, False: 8]
374
247
            if (exponent >= -22 && exponent < 0) {
  Branch (374:17): [True: 247, False: 0]
  Branch (374:36): [True: 108, False: 139]
375
108
                return (double) value / pm_double_powers[-exponent];
376
139
            } else if (exponent >= 0 && exponent <= 22) {
  Branch (376:24): [True: 139, False: 0]
  Branch (376:41): [True: 102, False: 37]
377
102
                return (double) value * pm_double_powers[exponent];
378
102
            } else 
if (37
exponent > 2237
&&
exponent <= 22 + 1537
) {
  Branch (378:24): [True: 37, False: 0]
  Branch (378:41): [True: 3, False: 34]
379
3
                uint64_t scaled = value;
380
27
                for (int64_t power = exponent; power > 22 && scaled <= ((uint64_t) 1 << 53); 
power--24
)
scaled *= 1024
;
  Branch (380:48): [True: 27, False: 0]
  Branch (380:62): [True: 24, False: 3]
381
3
                if (scaled <= ((uint64_t) 1 << 53)) 
return (double) scaled * 1e220
;
  Branch (381:21): [True: 0, False: 3]
382
3
            }
383
247
        }
384
74
#endif
385
386
74
        bits = pm_double_exact(first, digits, exponent, value);
387
74
    }
388
389
91
    *out_of_range = bits == 0 || 
bits == 90
PM_DOUBLE_INFINITY90
;
  Branch (389:21): [True: 1, False: 90]
  Branch (389:34): [True: 16, False: 74]
390
391
91
    double result;
392
91
    memcpy(&result, &bits, sizeof(double));
393
91
    return result;
394
301
}