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 12 long double decimal_pow10l(int n) {
40 12 ▶ if (n < 0) return powl(10.0L, n);
Branch: 1 branch
BranchTrueFalseTotal
if (n < 0) return powl(10.0L, n); 0 12 12
41 12 long double r = 1.0L;
42 int i;
43 34 ▶ for (i = 0; i < n; i++) r *= 10.0L;
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) r *= 10.0L; 22 12 34
44 12 return r;
45 }
46  
47 12106 static int mul10_128(uint64_t *hi, uint64_t *lo) {
48 #ifdef HAVE_INT128
49 12106 u128_t prod_lo = (u128_t)*lo * 10;
50 12106 u128_t prod_hi = (u128_t)*hi * 10 + (uint64_t)(prod_lo >> 64);
51 12106 *lo = (uint64_t)prod_lo;
52 12106 *hi = (uint64_t)prod_hi;
53 12106 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 102078 static int mul10_256(uint64_t limbs[4]) {
73 102078 uint64_t carry = 0;
74 int i;
75 510390 ▶ for (i = 0; i < 4; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < 4; i++) { 408312 102078 510390
76 #ifdef HAVE_INT128
77 408312 u128_t prod = (u128_t)limbs[i] * 10 + carry;
78 408312 limbs[i] = (uint64_t)prod;
79 408312 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 102078 return carry == 0;
90 }
91  
92 3423 int add_digit_256(uint64_t limbs[4], uint64_t digit) {
93 3423 uint64_t carry = digit;
94 int i;
95 6595 ▶ for (i = 0; i < 4 && carry; i++) {
Branch: 2 branches
BranchTrueFalseTotal
for (i = 0; i < 4 && carry; i++) { 6590 5 6595
for (i = 0; i < 4 && carry; i++) { 3172 3418 6590
96 3172 uint64_t old = limbs[i];
97 3172 limbs[i] = old + carry;
98 3172 carry = (limbs[i] < old) ? 1 : 0;
99 }
100 3423 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 3016 int parse_decimal128_str(const char *s, STRLEN len, int scale, int precision,
147 uint64_t *hi_out, uint64_t *lo_out) {
148 3016 STRLEN i = 0;
149 3016 int neg = 0, seen_digit = 0, seen_dot = 0, frac_used = 0;
150 3016 uint64_t lo = 0, hi = 0;
151  
152 3016 ▶ if (memchr(s, 'e', len) || memchr(s, 'E', len)) {
Branch: 2 branches
BranchTrueFalseTotal
if (memchr(s, 'e', len) || memchr(s, 'E', len)) { 2962 54 3016
if (memchr(s, 'e', len) || memchr(s, 'E', len)) { 3 2959 2962
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 2959 ▶ while (i < len && (s[i] == ' ' || s[i] == '\t')) i++;
Branch: 3 branches
BranchTrueFalseTotal
while (i < len && (s[i] == ' ' || s[i] == '\t')) i++; 2959 0 2959
while (i < len && (s[i] == ' ' || s[i] == '\t')) i++; 0 2959 2959
while (i < len && (s[i] == ' ' || s[i] == '\t')) i++; 0 2959 2959
160 2959 ▶ if (i < len && (s[i] == '+' || s[i] == '-')) {
Branch: 3 branches
BranchTrueFalseTotal
if (i < len && (s[i] == '+' || s[i] == '-')) { 2959 0 2959
if (i < len && (s[i] == '+' || s[i] == '-')) { 2959 0 2959
if (i < len && (s[i] == '+' || s[i] == '-')) { 34 2925 2959
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 13531 ▶ while (i < len) {
Branch: 1 branch
BranchTrueFalseTotal
while (i < len) { 10573 2958 13531
166 10573 char c = s[i++];
167 10573 ▶ if (c == '.') {
Branch: 1 branch
BranchTrueFalseTotal
if (c == '.') { 1860 8713 10573
168 1860 ▶ if (seen_dot) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (seen_dot) return 0; 0 1860 1860
169 1860 seen_dot = 1;
170 1860 continue;
171 }
172 8713 ▶ if (c < '0' || c > '9') return 0;
Branch: 2 branches
BranchTrueFalseTotal
if (c < '0' || c > '9') return 0; 8713 0 8713
if (c < '0' || c > '9') return 0; 0 8713 8713
173 8713 seen_digit = 1;
174 8713 ▶ if (seen_dot) {
Branch: 1 branch
BranchTrueFalseTotal
if (seen_dot) { 3498 5215 8713
175 3498 ▶ if (frac_used >= scale) continue; /* truncate */
Branch: 1 branch
BranchTrueFalseTotal
if (frac_used >= scale) continue; /* truncate */ 6 3492 3498
176 3492 frac_used++;
177 }
178 8707 ▶ if (!mul10_128(&hi, &lo)) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!mul10_128(&hi, &lo)) return 0; 0 8707 8707
179 8707 uint64_t digit = (uint64_t)(c - '0');
180 /* (hi:lo) + digit overflows iff hi == UINT64_MAX and lo wraps. */
181 8707 ▶ if (hi == 0xFFFFFFFFFFFFFFFFULL && lo > 0xFFFFFFFFFFFFFFFFULL - digit)
Branch: 2 branches
BranchTrueFalseTotal
if (hi == 0xFFFFFFFFFFFFFFFFULL && lo > 0xFFFFFFFFFFFFFFFFULL - digit) 2 8705 8707
if (hi == 0xFFFFFFFFFFFFFFFFULL && lo > 0xFFFFFFFFFFFFFFFFULL - digit) 1 1 2
182 1 return 0;
183 8706 uint64_t newlo = lo + digit;
184 8706 ▶ if (newlo < lo) hi++;
Branch: 1 branch
BranchTrueFalseTotal
if (newlo < lo) hi++; 3 8703 8706
185 8706 lo = newlo;
186 }
187  
188 2958 ▶ if (!seen_digit) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!seen_digit) return 0; 0 2958 2958
189  
190 6357 ▶ while (frac_used < scale) {
Branch: 1 branch
BranchTrueFalseTotal
while (frac_used < scale) { 3399 2958 6357
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 2958 ▶ if (hi > 0x8000000000000000ULL) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (hi > 0x8000000000000000ULL) return 0; 1 2957 2958
198 2957 ▶ if (hi == 0x8000000000000000ULL && (!neg || lo != 0)) return 0;
Branch: 3 branches
BranchTrueFalseTotal
if (hi == 0x8000000000000000ULL && (!neg || lo != 0)) return 0; 3 2954 2957
if (hi == 0x8000000000000000ULL && (!neg || lo != 0)) return 0; 1 2 3
if (hi == 0x8000000000000000ULL && (!neg || lo != 0)) return 0; 0 1 1
199  
200 2955 ▶ if (neg) {
Branch: 1 branch
BranchTrueFalseTotal
if (neg) { 34 2921 2955
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 2955 *lo_out = lo;
206 2955 *hi_out = hi;
207 2955 return 1;
208 }
209  
210 869 int parse_decimal256_str(const char *s, STRLEN len, int scale, int precision,
211 uint64_t limbs_out[4]) {
212 869 STRLEN i = 0;
213 869 int neg = 0, seen_digit = 0, seen_dot = 0, frac_used = 0;
214 869 uint64_t limbs[4] = {0,0,0,0};
215  
216 869 ▶ if (memchr(s, 'e', len) || memchr(s, 'E', len)) {
Branch: 2 branches
BranchTrueFalseTotal
if (memchr(s, 'e', len) || memchr(s, 'E', len)) { 851 18 869
if (memchr(s, 'e', len) || memchr(s, 'E', len)) { 1 850 851
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 850 ▶ while (i < len && (s[i] == ' ' || s[i] == '\t')) i++;
Branch: 3 branches
BranchTrueFalseTotal
while (i < len && (s[i] == ' ' || s[i] == '\t')) i++; 850 0 850
while (i < len && (s[i] == ' ' || s[i] == '\t')) i++; 0 850 850
while (i < len && (s[i] == ' ' || s[i] == '\t')) i++; 0 850 850
224 850 ▶ if (i < len && (s[i] == '+' || s[i] == '-')) {
Branch: 3 branches
BranchTrueFalseTotal
if (i < len && (s[i] == '+' || s[i] == '-')) { 850 0 850
if (i < len && (s[i] == '+' || s[i] == '-')) { 850 0 850
if (i < len && (s[i] == '+' || s[i] == '-')) { 13 837 850
225 13 ▶ if (s[i] == '-') neg = 1;
Branch: 1 branch
BranchTrueFalseTotal
if (s[i] == '-') neg = 1; 13 0 13
226 13 i++;
227 }
228 5043 ▶ while (i < len) {
Branch: 1 branch
BranchTrueFalseTotal
while (i < len) { 4194 849 5043
229 4194 char c = s[i++];
230 4194 ▶ if (c == '.') {
Branch: 1 branch
BranchTrueFalseTotal
if (c == '.') { 814 3380 4194
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 3380 ▶ if (c < '0' || c > '9') return 0;
Branch: 2 branches
BranchTrueFalseTotal
if (c < '0' || c > '9') return 0; 3380 0 3380
if (c < '0' || c > '9') return 0; 0 3380 3380
236 3380 seen_digit = 1;
237 3380 ▶ if (seen_dot) {
Branch: 1 branch
BranchTrueFalseTotal
if (seen_dot) { 1260 2120 3380
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 3380 ▶ if (!mul10_256(limbs)) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!mul10_256(limbs)) return 0; 0 3380 3380
242 3380 ▶ 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 3379 3380
243 }
244 849 ▶ if (!seen_digit) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!seen_digit) return 0; 0 849 849
245 2115 ▶ while (frac_used < scale) {
Branch: 1 branch
BranchTrueFalseTotal
while (frac_used < scale) { 1266 849 2115
246 1266 ▶ if (!mul10_256(limbs)) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!mul10_256(limbs)) return 0; 0 1266 1266
247 1266 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 849 ▶ if (limbs[3] > 0x8000000000000000ULL) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (limbs[3] > 0x8000000000000000ULL) return 0; 0 849 849
252 849 ▶ if (limbs[3] == 0x8000000000000000ULL
Branch: 1 branch
BranchTrueFalseTotal
if (limbs[3] == 0x8000000000000000ULL 2 847 849
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 848 ▶ if (neg) {
Branch: 1 branch
BranchTrueFalseTotal
if (neg) { 13 835 848
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 848 limbs_out[0] = limbs[0];
261 848 limbs_out[1] = limbs[1];
262 848 limbs_out[2] = limbs[2];
263 848 limbs_out[3] = limbs[3];
264 848 return 1;
265 }
266  
267 1711 static int pow10_256(uint64_t limbs[4], int n) {
268 1711 limbs[0] = 1; limbs[1] = limbs[2] = limbs[3] = 0;
269 99143 ▶ while (n-- > 0) if (!mul10_256(limbs)) return 0;
Branch: 2 branches
BranchTrueFalseTotal
while (n-- > 0) if (!mul10_256(limbs)) return 0; 0 97432 97432
while (n-- > 0) if (!mul10_256(limbs)) return 0; 97432 1711 99143
270 1711 return 1;
271 }
272  
273 1711 int decimal256_within_precision(const uint64_t limbs[4], int precision) {
274 uint64_t magnitude[4], bound[4];
275 int j;
276 1711 memcpy(magnitude, limbs, sizeof(magnitude));
277 1711 ▶ if (magnitude[3] >> 63) {
Branch: 1 branch
BranchTrueFalseTotal
if (magnitude[3] >> 63) { 30 1681 1711
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 1711 ▶ if (!pow10_256(bound, precision)) return 1;
Branch: 1 branch
BranchTrueFalseTotal
if (!pow10_256(bound, precision)) return 1; 0 1711 1711
282 3461 ▶ for (j = 3; j >= 0; j--)
Branch: 1 branch
BranchTrueFalseTotal
for (j = 3; j >= 0; j--) 3456 5 3461
283 3456 ▶ if (magnitude[j] != bound[j]) return magnitude[j] < bound[j];
Branch: 1 branch
BranchTrueFalseTotal
if (magnitude[j] != bound[j]) return magnitude[j] < bound[j]; 1706 1750 3456
284 5 return 0;
285 }
286  
287 858 int decimal128_within_precision(uint64_t hi, uint64_t lo, int precision) {
288 uint64_t limbs[4];
289 858 limbs[0] = lo;
290 858 limbs[1] = hi;
291 858 ▶ 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 841 858
292 858 return decimal256_within_precision(limbs, precision);
293 }
294  
295 2114 int parse_decimal_int64_str(const char *s, STRLEN len, int scale, int precision,
296 int64_t *out) {
297 uint64_t lo, hi;
298 2114 ▶ 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 2099 2114
299 /* Fits in int64 iff hi is sign extension of lo's high bit. */
300 2099 ▶ if (hi == 0 && (lo & 0x8000000000000000ULL) == 0) {
Branch: 2 branches
BranchTrueFalseTotal
if (hi == 0 && (lo & 0x8000000000000000ULL) == 0) { 2081 18 2099
if (hi == 0 && (lo & 0x8000000000000000ULL) == 0) { 2081 0 2081
301 2081 *out = (int64_t)lo;
302 2081 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 »