decimal.c

filter: Statementstmt 98.8% 170 / 172 Branchbran 77.9% 184 / 236 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 86.7% 354 / 408 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« decode.c ↑ Summary encode.c »
1 #define PERL_NO_GET_CONTEXT
2 #include "EXTERN.h"
3 #include "perl.h"
4 #include "XSUB.h"
5  
6 #include <math.h>
7 #include <stdint.h>
8  
9 #if defined(__SIZEOF_INT128__) && __SIZEOF_INT128__ == 16
10 #define HAVE_INT128 1
11 typedef unsigned __int128 u128_t;
12 #endif
13  
14 #include "decimal.h"
15  
16 const uint64_t pow10_u64[20] = {
17 1ULL, 10ULL, 100ULL, 1000ULL, 10000ULL, 100000ULL,
18 1000000ULL, 10000000ULL, 100000000ULL, 1000000000ULL,
19 10000000000ULL, 100000000000ULL, 1000000000000ULL,
20 10000000000000ULL, 100000000000000ULL, 1000000000000000ULL,
21 10000000000000000ULL, 100000000000000000ULL,
22 1000000000000000000ULL, 10000000000000000000ULL
23 };
24  
25 static const double decimal_pow10_table[] = {
26 1e0, 1e1, 1e2, 1e3, 1e4, 1e5, 1e6, 1e7, 1e8, 1e9,
27 1e10, 1e11, 1e12, 1e13, 1e14, 1e15, 1e16, 1e17, 1e18, 1e19,
28 1e20, 1e21, 1e22, 1e23, 1e24, 1e25, 1e26, 1e27, 1e28, 1e29,
29 1e30, 1e31, 1e32, 1e33, 1e34, 1e35, 1e36, 1e37, 1e38
30 };
31  
32 25 double decimal_pow10(int n) {
33 25 ▶ if (n >= 0 && n <= 38) return decimal_pow10_table[n];
Branch: 2 branches
BranchTrueFalseTotal
if (n >= 0 && n <= 38) return decimal_pow10_table[n]; 25 0 25
if (n >= 0 && n <= 38) return decimal_pow10_table[n]; 25 0 25
34 0 return pow(10.0, n);
35 }
36  
37 /* Long-double powers of 10 for Decimal128/256 float-path multiplication.
38 * Computed at extended precision so the scale doesn't double-round. */
39 8 long double decimal_pow10l(int n) {
40 8 ▶ if (n < 0) return powl(10.0L, n);
Branch: 1 branch
BranchTrueFalseTotal
if (n < 0) return powl(10.0L, n); 0 8 8
41 8 long double r = 1.0L;
42 int i;
43 14 ▶ for (i = 0; i < n; i++) r *= 10.0L;
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) r *= 10.0L; 6 8 14
44 8 return r;
45 }
46  
47 12030 static int mul10_128(uint64_t *hi, uint64_t *lo) {
48 #ifdef HAVE_INT128
49 12030 u128_t prod_lo = (u128_t)*lo * 10;
50 12030 u128_t prod_hi = (u128_t)*hi * 10 + (uint64_t)(prod_lo >> 64);
51 12030 *lo = (uint64_t)prod_lo;
52 12030 *hi = (uint64_t)prod_hi;
53 12030 return (prod_hi >> 64) == 0;
54 #else
55 /* 32-bit limb fallback. */
56 uint64_t lo_lo = *lo & 0xFFFFFFFFULL;
57 uint64_t lo_hi = *lo >> 32;
58 uint64_t p_lo = lo_lo * 10;
59 uint64_t p_hi = lo_hi * 10 + (p_lo >> 32);
60 uint64_t carry = p_hi >> 32;
61 *lo = (p_hi << 32) | (p_lo & 0xFFFFFFFFULL);
62  
63 uint64_t hi_lo = *hi & 0xFFFFFFFFULL;
64 uint64_t hi_hi = *hi >> 32;
65 uint64_t q_lo = hi_lo * 10 + (carry & 0xFFFFFFFFULL);
66 uint64_t q_hi = hi_hi * 10 + (q_lo >> 32) + (carry >> 32);
67 *hi = (q_hi << 32) | (q_lo & 0xFFFFFFFFULL);
68 return (q_hi >> 32) == 0;
69 #endif
70 }
71  
72 101536 static int mul10_256(uint64_t limbs[4]) {
73 101536 uint64_t carry = 0;
74 int i;
75 507680 ▶ for (i = 0; i < 4; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < 4; i++) { 406144 101536 507680
76 #ifdef HAVE_INT128
77 406144 u128_t prod = (u128_t)limbs[i] * 10 + carry;
78 406144 limbs[i] = (uint64_t)prod;
79 406144 carry = (uint64_t)(prod >> 64);
80 #else
81 uint64_t lo_lo = limbs[i] & 0xFFFFFFFFULL;
82 uint64_t lo_hi = limbs[i] >> 32;
83 uint64_t p_lo = lo_lo * 10 + (carry & 0xFFFFFFFFULL);
84 uint64_t p_hi = lo_hi * 10 + (p_lo >> 32) + (carry >> 32);
85 limbs[i] = (p_hi << 32) | (p_lo & 0xFFFFFFFFULL);
86 carry = p_hi >> 32;
87 #endif
88 }
89 101536 return carry == 0;
90 }
91  
92 3421 int add_digit_256(uint64_t limbs[4], uint64_t digit) {
93 3421 uint64_t carry = digit;
94 int i;
95 6593 ▶ for (i = 0; i < 4 && carry; i++) {
Branch: 2 branches
BranchTrueFalseTotal
for (i = 0; i < 4 && carry; i++) { 6588 5 6593
for (i = 0; i < 4 && carry; i++) { 3172 3416 6588
96 3172 uint64_t old = limbs[i];
97 3172 limbs[i] = old + carry;
98 3172 carry = (limbs[i] < old) ? 1 : 0;
99 }
100 3421 return (int)carry;
101 }
102  
103 76 static int expand_exponent(const char *s, STRLEN len, int scale, int precision,
104 int max_digits, char *out, STRLEN *out_len) {
105 76 STRLEN i = 0, start, end, digits = 0, fractional = 0, first = len, j, n = 0;
106 76 int negative = 0, dot = 0, exp_negative = 0;
107 76 int64_t exponent = 0, keep;
108 76 ▶ while (i < len && (s[i] == ' ' || s[i] == '\t')) i++;
Branch: 3 branches
BranchTrueFalseTotal
while (i < len && (s[i] == ' ' || s[i] == '\t')) i++; 76 0 76
while (i < len && (s[i] == ' ' || s[i] == '\t')) i++; 0 76 76
while (i < len && (s[i] == ' ' || s[i] == '\t')) i++; 0 76 76
109 76 ▶ if (i < len && (s[i] == '+' || s[i] == '-')) negative = s[i++] == '-';
Branch: 3 branches
BranchTrueFalseTotal
if (i < len && (s[i] == '+' || s[i] == '-')) negative = s[i++] == '-'; 76 0 76
if (i < len && (s[i] == '+' || s[i] == '-')) negative = s[i++] == '-'; 76 0 76
if (i < len && (s[i] == '+' || s[i] == '-')) negative = s[i++] == '-'; 8 68 76
110 76 start = i;
111 250 ▶ for (; i < len && s[i] != 'e' && s[i] != 'E'; i++) {
Branch: 3 branches
BranchTrueFalseTotal
for (; i < len && s[i] != 'e' && s[i] != 'E'; i++) { 250 0 250
for (; i < len && s[i] != 'e' && s[i] != 'E'; i++) { 178 72 250
for (; i < len && s[i] != 'e' && s[i] != 'E'; i++) { 174 4 178
112 174 ▶ if (s[i] == '.' && !dot) { dot = 1; continue; }
Branch: 2 branches
BranchTrueFalseTotal
if (s[i] == '.' && !dot) { dot = 1; continue; } 23 151 174
if (s[i] == '.' && !dot) { dot = 1; continue; } 23 0 23
113 151 ▶ if (s[i] < '0' || s[i] > '9') return 0;
Branch: 2 branches
BranchTrueFalseTotal
if (s[i] < '0' || s[i] > '9') return 0; 151 0 151
if (s[i] < '0' || s[i] > '9') return 0; 0 151 151
114 151 ▶ if (s[i] != '0' && first == len) first = digits;
Branch: 2 branches
BranchTrueFalseTotal
if (s[i] != '0' && first == len) first = digits; 118 33 151
if (s[i] != '0' && first == len) first = digits; 59 59 118
115 151 digits++;
116 151 ▶ if (dot) fractional++;
Branch: 1 branch
BranchTrueFalseTotal
if (dot) fractional++; 22 129 151
117 }
118 76 end = i;
119 76 ▶ if (!digits || i == len) return 0;
Branch: 2 branches
BranchTrueFalseTotal
if (!digits || i == len) return 0; 76 0 76
if (!digits || i == len) return 0; 0 76 76
120 76 i++;
121 76 ▶ if (i < len && (s[i] == '+' || s[i] == '-')) exp_negative = s[i++] == '-';
Branch: 3 branches
BranchTrueFalseTotal
if (i < len && (s[i] == '+' || s[i] == '-')) exp_negative = s[i++] == '-'; 72 4 76
if (i < len && (s[i] == '+' || s[i] == '-')) exp_negative = s[i++] == '-'; 64 8 72
if (i < len && (s[i] == '+' || s[i] == '-')) exp_negative = s[i++] == '-'; 23 41 64
122 76 ▶ if (i == len) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (i == len) return 0; 8 68 76
123 223 ▶ for (; i < len; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (; i < len; i++) { 159 64 223
124 159 ▶ if (s[i] < '0' || s[i] > '9') return 0;
Branch: 2 branches
BranchTrueFalseTotal
if (s[i] < '0' || s[i] > '9') return 0; 159 0 159
if (s[i] < '0' || s[i] > '9') return 0; 4 155 159
125 155 ▶ if (exponent < 1000000000) exponent = exponent * 10 + s[i] - '0';
Branch: 1 branch
BranchTrueFalseTotal
if (exponent < 1000000000) exponent = exponent * 10 + s[i] - '0'; 155 0 155
126 }
127 64 ▶ if (exp_negative) exponent = -exponent;
Branch: 1 branch
BranchTrueFalseTotal
if (exp_negative) exponent = -exponent; 23 41 64
128 64 ▶ if (exponent + scale > precision) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (exponent + scale > precision) return 0; 17 47 64
129 47 keep = (int64_t)digits + scale + exponent - (int64_t)fractional;
130 47 ▶ if (keep - (int64_t)(first == len ? digits : first) > max_digits) return 0;
Branch: 2 branches
BranchTrueFalseTotal
if (keep - (int64_t)(first == len ? digits : first) > max_digits) return 0; 4 43 47
if (keep - (int64_t)(first == len ? digits : first) > max_digits) return 0; 0 47 47
131 47 ▶ if (negative) out[n++] = '-';
Branch: 1 branch
BranchTrueFalseTotal
if (negative) out[n++] = '-'; 8 39 47
132 47 j = 0;
133 192 ▶ for (i = start; i < end; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = start; i < end; i++) { 145 47 192
134 145 ▶ if (s[i] == '.') continue;
Branch: 1 branch
BranchTrueFalseTotal
if (s[i] == '.') continue; 23 122 145
135 122 ▶ if (j >= first && (int64_t)j < keep) out[n++] = s[i];
Branch: 2 branches
BranchTrueFalseTotal
if (j >= first && (int64_t)j < keep) out[n++] = s[i]; 118 4 122
if (j >= first && (int64_t)j < keep) out[n++] = s[i]; 106 12 118
136 122 j++;
137 }
138 47 ▶ if (first != len) {
Branch: 1 branch
BranchTrueFalseTotal
if (first != len) { 43 4 47
139 165 ▶ while ((int64_t)j < keep) { out[n++] = '0'; j++; }
Branch: 1 branch
BranchTrueFalseTotal
while ((int64_t)j < keep) { out[n++] = '0'; j++; } 122 43 165
140 }
141 47 ▶ if (n == (STRLEN)negative) out[n++] = '0';
Branch: 1 branch
BranchTrueFalseTotal
if (n == (STRLEN)negative) out[n++] = '0'; 8 39 47
142 47 *out_len = n;
143 47 return 1;
144 }
145  
146 3008 int parse_decimal128_str(const char *s, STRLEN len, int scale, int precision,
147 uint64_t *hi_out, uint64_t *lo_out) {
148 3008 STRLEN i = 0;
149 3008 int neg = 0, seen_digit = 0, seen_dot = 0, frac_used = 0;
150 3008 uint64_t lo = 0, hi = 0;
151  
152 3008 ▶ if (memchr(s, 'e', len) || memchr(s, 'E', len)) {
Branch: 2 branches
BranchTrueFalseTotal
if (memchr(s, 'e', len) || memchr(s, 'E', len)) { 2954 54 3008
if (memchr(s, 'e', len) || memchr(s, 'E', len)) { 3 2951 2954
153 char expanded[41];
154 STRLEN expanded_len;
155 57 ▶ if (!expand_exponent(s, len, scale, precision, 39, expanded, &expanded_len)) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!expand_exponent(s, len, scale, precision, 39, expanded, &expanded_len)) return 0; 22 35 57
156 35 return parse_decimal128_str(expanded, expanded_len, 0, precision, hi_out, lo_out);
157 }
158  
159 2951 ▶ while (i < len && (s[i] == ' ' || s[i] == '\t')) i++;
Branch: 3 branches
BranchTrueFalseTotal
while (i < len && (s[i] == ' ' || s[i] == '\t')) i++; 2951 0 2951
while (i < len && (s[i] == ' ' || s[i] == '\t')) i++; 0 2951 2951
while (i < len && (s[i] == ' ' || s[i] == '\t')) i++; 0 2951 2951
160 2951 ▶ if (i < len && (s[i] == '+' || s[i] == '-')) {
Branch: 3 branches
BranchTrueFalseTotal
if (i < len && (s[i] == '+' || s[i] == '-')) { 2951 0 2951
if (i < len && (s[i] == '+' || s[i] == '-')) { 2951 0 2951
if (i < len && (s[i] == '+' || s[i] == '-')) { 34 2917 2951
161 34 ▶ if (s[i] == '-') neg = 1;
Branch: 1 branch
BranchTrueFalseTotal
if (s[i] == '-') neg = 1; 34 0 34
162 34 i++;
163 }
164  
165 13439 ▶ while (i < len) {
Branch: 1 branch
BranchTrueFalseTotal
while (i < len) { 10489 2950 13439
166 10489 char c = s[i++];
167 10489 ▶ if (c == '.') {
Branch: 1 branch
BranchTrueFalseTotal
if (c == '.') { 1852 8637 10489
168 1852 ▶ if (seen_dot) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (seen_dot) return 0; 0 1852 1852
169 1852 seen_dot = 1;
170 1852 continue;
171 }
172 8637 ▶ if (c < '0' || c > '9') return 0;
Branch: 2 branches
BranchTrueFalseTotal
if (c < '0' || c > '9') return 0; 8637 0 8637
if (c < '0' || c > '9') return 0; 0 8637 8637
173 8637 seen_digit = 1;
174 8637 ▶ if (seen_dot) {
Branch: 1 branch
BranchTrueFalseTotal
if (seen_dot) { 3468 5169 8637
175 3468 ▶ if (frac_used >= scale) continue; /* truncate */
Branch: 1 branch
BranchTrueFalseTotal
if (frac_used >= scale) continue; /* truncate */ 6 3462 3468
176 3462 frac_used++;
177 }
178 8631 ▶ if (!mul10_128(&hi, &lo)) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!mul10_128(&hi, &lo)) return 0; 0 8631 8631
179 8631 uint64_t digit = (uint64_t)(c - '0');
180 /* (hi:lo) + digit overflows iff hi == UINT64_MAX and lo wraps. */
181 8631 ▶ if (hi == 0xFFFFFFFFFFFFFFFFULL && lo > 0xFFFFFFFFFFFFFFFFULL - digit)
Branch: 2 branches
BranchTrueFalseTotal
if (hi == 0xFFFFFFFFFFFFFFFFULL && lo > 0xFFFFFFFFFFFFFFFFULL - digit) 2 8629 8631
if (hi == 0xFFFFFFFFFFFFFFFFULL && lo > 0xFFFFFFFFFFFFFFFFULL - digit) 1 1 2
182 1 return 0;
183 8630 uint64_t newlo = lo + digit;
184 8630 ▶ if (newlo < lo) hi++;
Branch: 1 branch
BranchTrueFalseTotal
if (newlo < lo) hi++; 3 8627 8630
185 8630 lo = newlo;
186 }
187  
188 2950 ▶ if (!seen_digit) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!seen_digit) return 0; 0 2950 2950
189  
190 6349 ▶ while (frac_used < scale) {
Branch: 1 branch
BranchTrueFalseTotal
while (frac_used < scale) { 3399 2950 6349
191 3399 ▶ if (!mul10_128(&hi, &lo)) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!mul10_128(&hi, &lo)) return 0; 0 3399 3399
192 3399 frac_used++;
193 }
194  
195 /* Reject magnitudes that don't fit signed 128-bit (Decimal128 storage).
196 * The asymmetric extreme -2^127 has hi=0x80..., lo=0 with neg=1; allow it. */
197 2950 ▶ if (hi > 0x8000000000000000ULL) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (hi > 0x8000000000000000ULL) return 0; 1 2949 2950
198 2949 ▶ if (hi == 0x8000000000000000ULL && (!neg || lo != 0)) return 0;
Branch: 3 branches
BranchTrueFalseTotal
if (hi == 0x8000000000000000ULL && (!neg || lo != 0)) return 0; 3 2946 2949
if (hi == 0x8000000000000000ULL && (!neg || lo != 0)) return 0; 1 2 3
if (hi == 0x8000000000000000ULL && (!neg || lo != 0)) return 0; 0 1 1
199  
200 2947 ▶ if (neg) {
Branch: 1 branch
BranchTrueFalseTotal
if (neg) { 34 2913 2947
201 34 lo = ~lo + 1;
202 34 ▶ hi = ~hi + (lo == 0 ? 1 : 0);
Branch: 1 branch
BranchTrueFalseTotal
hi = ~hi + (lo == 0 ? 1 : 0); 1 33 34
203 }
204  
205 2947 *lo_out = lo;
206 2947 *hi_out = hi;
207 2947 return 1;
208 }
209  
210 867 int parse_decimal256_str(const char *s, STRLEN len, int scale, int precision,
211 uint64_t limbs_out[4]) {
212 867 STRLEN i = 0;
213 867 int neg = 0, seen_digit = 0, seen_dot = 0, frac_used = 0;
214 867 uint64_t limbs[4] = {0,0,0,0};
215  
216 867 ▶ if (memchr(s, 'e', len) || memchr(s, 'E', len)) {
Branch: 2 branches
BranchTrueFalseTotal
if (memchr(s, 'e', len) || memchr(s, 'E', len)) { 849 18 867
if (memchr(s, 'e', len) || memchr(s, 'E', len)) { 1 848 849
217 char expanded[80];
218 STRLEN expanded_len;
219 19 ▶ if (!expand_exponent(s, len, scale, precision, 78, expanded, &expanded_len)) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!expand_exponent(s, len, scale, precision, 78, expanded, &expanded_len)) return 0; 7 12 19
220 12 return parse_decimal256_str(expanded, expanded_len, 0, precision, limbs_out);
221 }
222  
223 848 ▶ while (i < len && (s[i] == ' ' || s[i] == '\t')) i++;
Branch: 3 branches
BranchTrueFalseTotal
while (i < len && (s[i] == ' ' || s[i] == '\t')) i++; 848 0 848
while (i < len && (s[i] == ' ' || s[i] == '\t')) i++; 0 848 848
while (i < len && (s[i] == ' ' || s[i] == '\t')) i++; 0 848 848
224 848 ▶ if (i < len && (s[i] == '+' || s[i] == '-')) {
Branch: 3 branches
BranchTrueFalseTotal
if (i < len && (s[i] == '+' || s[i] == '-')) { 848 0 848
if (i < len && (s[i] == '+' || s[i] == '-')) { 848 0 848
if (i < len && (s[i] == '+' || s[i] == '-')) { 13 835 848
225 13 ▶ if (s[i] == '-') neg = 1;
Branch: 1 branch
BranchTrueFalseTotal
if (s[i] == '-') neg = 1; 13 0 13
226 13 i++;
227 }
228 5039 ▶ while (i < len) {
Branch: 1 branch
BranchTrueFalseTotal
while (i < len) { 4192 847 5039
229 4192 char c = s[i++];
230 4192 ▶ if (c == '.') {
Branch: 1 branch
BranchTrueFalseTotal
if (c == '.') { 814 3378 4192
231 814 ▶ if (seen_dot) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (seen_dot) return 0; 0 814 814
232 814 seen_dot = 1;
233 814 continue;
234 }
235 3378 ▶ if (c < '0' || c > '9') return 0;
Branch: 2 branches
BranchTrueFalseTotal
if (c < '0' || c > '9') return 0; 3378 0 3378
if (c < '0' || c > '9') return 0; 0 3378 3378
236 3378 seen_digit = 1;
237 3378 ▶ if (seen_dot) {
Branch: 1 branch
BranchTrueFalseTotal
if (seen_dot) { 1260 2118 3378
238 1260 ▶ if (frac_used >= scale) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (frac_used >= scale) continue; 0 1260 1260
239 1260 frac_used++;
240 }
241 3378 ▶ if (!mul10_256(limbs)) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!mul10_256(limbs)) return 0; 0 3378 3378
242 3378 ▶ if (add_digit_256(limbs, (uint64_t)(c - '0'))) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (add_digit_256(limbs, (uint64_t)(c - '0'))) return 0; 1 3377 3378
243 }
244 847 ▶ if (!seen_digit) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!seen_digit) return 0; 0 847 847
245 2105 ▶ while (frac_used < scale) {
Branch: 1 branch
BranchTrueFalseTotal
while (frac_used < scale) { 1258 847 2105
246 1258 ▶ if (!mul10_256(limbs)) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!mul10_256(limbs)) return 0; 0 1258 1258
247 1258 frac_used++;
248 }
249 /* Reject magnitudes that don't fit signed 256-bit. Allow only the
250 * asymmetric extreme -2^255 (top bit set, rest zero, negative). */
251 847 ▶ if (limbs[3] > 0x8000000000000000ULL) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (limbs[3] > 0x8000000000000000ULL) return 0; 0 847 847
252 847 ▶ if (limbs[3] == 0x8000000000000000ULL
Branch: 1 branch
BranchTrueFalseTotal
if (limbs[3] == 0x8000000000000000ULL 2 845 847
253 2 ▶ && (!neg || limbs[0] != 0 || limbs[1] != 0 || limbs[2] != 0))
Branch: 4 branches
BranchTrueFalseTotal
&& (!neg || limbs[0] != 0 || limbs[1] != 0 || limbs[2] != 0)) 1 1 2
&& (!neg || limbs[0] != 0 || limbs[1] != 0 || limbs[2] != 0)) 1 0 1
&& (!neg || limbs[0] != 0 || limbs[1] != 0 || limbs[2] != 0)) 1 0 1
&& (!neg || limbs[0] != 0 || limbs[1] != 0 || limbs[2] != 0)) 0 1 1
254 1 return 0;
255 846 ▶ if (neg) {
Branch: 1 branch
BranchTrueFalseTotal
if (neg) { 13 833 846
256 int j;
257 65 ▶ for (j = 0; j < 4; j++) limbs[j] = ~limbs[j];
Branch: 1 branch
BranchTrueFalseTotal
for (j = 0; j < 4; j++) limbs[j] = ~limbs[j]; 52 13 65
258 13 add_digit_256(limbs, 1);
259 }
260 846 limbs_out[0] = limbs[0];
261 846 limbs_out[1] = limbs[1];
262 846 limbs_out[2] = limbs[2];
263 846 limbs_out[3] = limbs[3];
264 846 return 1;
265 }
266  
267 1703 static int pow10_256(uint64_t limbs[4], int n) {
268 1703 limbs[0] = 1; limbs[1] = limbs[2] = limbs[3] = 0;
269 98603 ▶ while (n-- > 0) if (!mul10_256(limbs)) return 0;
Branch: 2 branches
BranchTrueFalseTotal
while (n-- > 0) if (!mul10_256(limbs)) return 0; 0 96900 96900
while (n-- > 0) if (!mul10_256(limbs)) return 0; 96900 1703 98603
270 1703 return 1;
271 }
272  
273 1703 int decimal256_within_precision(const uint64_t limbs[4], int precision) {
274 uint64_t magnitude[4], bound[4];
275 int j;
276 1703 memcpy(magnitude, limbs, sizeof(magnitude));
277 1703 ▶ if (magnitude[3] >> 63) {
Branch: 1 branch
BranchTrueFalseTotal
if (magnitude[3] >> 63) { 30 1673 1703
278 150 ▶ for (j = 0; j < 4; j++) magnitude[j] = ~magnitude[j];
Branch: 1 branch
BranchTrueFalseTotal
for (j = 0; j < 4; j++) magnitude[j] = ~magnitude[j]; 120 30 150
279 30 add_digit_256(magnitude, 1);
280 }
281 1703 ▶ if (!pow10_256(bound, precision)) return 1;
Branch: 1 branch
BranchTrueFalseTotal
if (!pow10_256(bound, precision)) return 1; 0 1703 1703
282 3449 ▶ for (j = 3; j >= 0; j--)
Branch: 1 branch
BranchTrueFalseTotal
for (j = 3; j >= 0; j--) 3444 5 3449
283 3444 ▶ if (magnitude[j] != bound[j]) return magnitude[j] < bound[j];
Branch: 1 branch
BranchTrueFalseTotal
if (magnitude[j] != bound[j]) return magnitude[j] < bound[j]; 1698 1746 3444
284 5 return 0;
285 }
286  
287 856 int decimal128_within_precision(uint64_t hi, uint64_t lo, int precision) {
288 uint64_t limbs[4];
289 856 limbs[0] = lo;
290 856 limbs[1] = hi;
291 856 ▶ limbs[2] = limbs[3] = hi >> 63 ? ~(uint64_t)0 : 0;
Branch: 1 branch
BranchTrueFalseTotal
limbs[2] = limbs[3] = hi >> 63 ? ~(uint64_t)0 : 0; 17 839 856
292 856 return decimal256_within_precision(limbs, precision);
293 }
294  
295 2108 int parse_decimal_int64_str(const char *s, STRLEN len, int scale, int precision,
296 int64_t *out) {
297 uint64_t lo, hi;
298 2108 ▶ if (!parse_decimal128_str(s, len, scale, precision, &hi, &lo)) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!parse_decimal128_str(s, len, scale, precision, &hi, &lo)) return 0; 15 2093 2108
299 /* Fits in int64 iff hi is sign extension of lo's high bit. */
300 2093 ▶ if (hi == 0 && (lo & 0x8000000000000000ULL) == 0) {
Branch: 2 branches
BranchTrueFalseTotal
if (hi == 0 && (lo & 0x8000000000000000ULL) == 0) { 2075 18 2093
if (hi == 0 && (lo & 0x8000000000000000ULL) == 0) { 2075 0 2075
301 2075 *out = (int64_t)lo;
302 2075 return 1;
303 }
304 18 ▶ if (hi == ~(uint64_t)0 && (lo & 0x8000000000000000ULL)) {
Branch: 2 branches
BranchTrueFalseTotal
if (hi == ~(uint64_t)0 && (lo & 0x8000000000000000ULL)) { 18 0 18
if (hi == ~(uint64_t)0 && (lo & 0x8000000000000000ULL)) { 18 0 18
305 18 *out = (int64_t)lo;
306 18 return 1;
307 }
308 0 return 0;
309 }
« decode.c ↑ Summary encode.c »