Int128.xs

filter: Statementstmt 50.7% 385 / 759 Branchbran 28.4% 184 / 646 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 40.5% 569 / 1405 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« perl_math_int64.c ↑ Summary strtoint128.h »
1 /* -*- Mode: C -*- */
2  
3 #define PERL_NO_GET_CONTEXT 1
4  
5 #include "EXTERN.h"
6 #include "perl.h"
7 #include "XSUB.h"
8 #include "ppport.h"
9  
10 #include <stdint.h>
11 #include "perl_math_int64.h"
12  
13 #ifdef INT128_TI
14 typedef int int128_t __attribute__ ((__mode__ (TI)));
15 typedef unsigned int uint128_t __attribute__ ((__mode__ (TI)));
16 #define HAVE_INT128
17 #endif
18  
19 #ifdef __INT128
20 typedef __int128 int128_t;
21 typedef unsigned __int128 uint128_t;
22 #define HAVE_INT128
23 #endif
24  
25 #ifndef HAVE_INT128
26 #error "No int128 type define was passed to the compiler!"
27 #endif
28  
29 /* perl memory allocator does not guarantee 16-byte alignment */
30 typedef int128_t int128_t_a8 __attribute__ ((aligned(8)));
31 typedef uint128_t uint128_t_a8 __attribute__ ((aligned(8)));
32  
33 #define I128LEN sizeof(int128_t)
34  
35 #define INT128_MAX ((int128_t)((~(uint128_t)0)>>1))
36 #define INT128_MIN (~INT128_MAX)
37 #define UINT128_MAX (~(int128_t)0)
38  
39 static int may_die_on_overflow = 0;
40  
41 #if (PERL_VERSION >= 10)
42  
43 #ifndef cop_hints_fetch_pvs
44 #define cop_hints_fetch_pvs(cop, key, flags) \
45 Perl_refcounted_he_fetch(aTHX_ (cop)->cop_hints_hash, NULL, STR_WITH_LEN(key), (flags), 0)
46 #endif
47  
48 static int
49 0 check_die_on_overflow_hint(pTHX) {
50 0 SV *hint = cop_hints_fetch_pvs(PL_curcop, "Math::Int128::die_on_overflow", 0);
51 0 ▶ return (hint && SvTRUE(hint));
Branch: 2 branches
BranchTrueFalseTotal
return (hint && SvTRUE(hint)); 0 0 0
return (hint && SvTRUE(hint)); 0 0 0
52 }
53  
54 #else
55  
56 static int
57 check_die_on_overflow_hint(pTHX) {
58 return 1;
59 }
60  
61 #endif
62  
63 static void
64 0 overflow(pTHX_ const char *msg) {
65 0 ▶ if (check_die_on_overflow_hint(aTHX))
Branch: 1 branch
BranchTrueFalseTotal
if (check_die_on_overflow_hint(aTHX)) 0 0 0
66 0 Perl_croak(aTHX_ "Math::Int128 overflow: %s", msg);
67 0 }
68  
69 #define get_int128_stash_uncached() gv_stashpvs("Math::Int128", 1)
70 #define get_uint128_stash_uncached() gv_stashpvs("Math::UInt128", 1)
71  
72 #ifdef MULTIPLICITY
73 # if defined(I_PTHREAD) && defined(PTHREAD_MUTEX_INITIALIZER)
74 # define CACHE_STASHES
75 # endif
76 #else
77 # define CACHE_STASHES
78 #endif
79  
80 #ifdef CACHE_STASHES
81 static HV * volatile int128_stash;
82 static HV * volatile uint128_stash;
83  
84 # ifdef MULTIPLICITY
85 static pthread_mutex_t stash_mutex = PTHREAD_MUTEX_INITIALIZER;
86 static int too_many_threads = 0;
87  
88 static void init_stash_cache(pTHX) {
89 pthread_mutex_lock(&stash_mutex);
90 if (too_many_threads) {
91 int128_stash = NULL;
92 uint128_stash = NULL;
93 }
94 else {
95 too_many_threads = 1;
96 int128_stash = get_int128_stash_uncached();
97 uint128_stash = get_uint128_stash_uncached();
98 }
99 pthread_mutex_unlock(&stash_mutex);
100 }
101  
102 # else
103  
104 3 static void init_stash_cache(pTHX) {
105 3 int128_stash = get_int128_stash_uncached();
106 3 uint128_stash = get_uint128_stash_uncached();
107 3 }
108  
109 # endif
110  
111 #define get_int128_stash() (int128_stash ? int128_stash : get_int128_stash_uncached())
112 #define get_uint128_stash() (uint128_stash ? uint128_stash : get_uint128_stash_uncached())
113  
114 #else
115  
116 static void init_stash_cache(pTHX) { }
117  
118 #define get_int128_stash get_int128_stash_uncached
119 #define get_uint128_stash get_uint128_stash_uncached
120  
121 #endif
122  
123 static char *out_of_bounds_error_s = "Number is out of bounds for int128_t conversion";
124 static char *out_of_bounds_error_u = "Number is out of bounds for uint128_t conversion";
125 static char *mul_error = "Multiplication overflows";
126 static char *pow_error = "Exponentiation overflows";
127 static char *add_error = "Addition overflows";
128 static char *sub_error = "Subtraction overflows";
129 static char *inc_error = "Increment operation wraps";
130 static char *dec_error = "Decrement operation wraps";
131 static char *div_by_0_error = "Illegal division by zero";
132  
133 0 static void croak_string(pTHX_ const char *str) {
134 0 Perl_croak(aTHX_ "%s", str);
135 }
136  
137 #include "strtoint128.h"
138  
139 #define SvI128Y(sv) (*((int128_t_a8*)SvPVX(sv)))
140 #define SvU128Y(sv) (*((uint128_t_a8*)SvPVX(sv)))
141 #define SVt_I128 SVt_PV
142  
143 static SV *
144 4690 new_si128(pTHX) {
145 4690 SV *si128 = newSV(I128LEN);
146 4690 SvPOK_on(si128);
147 4690 SvCUR_set(si128, I128LEN);
148 4690 return si128;
149 }
150  
151 #define new_su128 new_si128
152  
153 static SV *
154 2229 newSVi128(pTHX_ int128_t i128) {
155 2229 ▶ HV *stash = get_int128_stash();
Branch: 1 branch
BranchTrueFalseTotal
HV *stash = get_int128_stash(); 0 2229 2229
156 2229 SV *si128 = new_si128(aTHX);
157 SV *sv;
158 2229 SvI128Y(si128) = i128;
159 2229 sv = newRV_noinc(si128);
160 2229 sv_bless(sv, stash);
161 2229 SvREADONLY_on(si128);
162 2229 return sv;
163 }
164  
165 static SV *
166 2461 newSVu128(pTHX_ uint128_t u128) {
167 2461 ▶ HV *stash = get_uint128_stash();
Branch: 1 branch
BranchTrueFalseTotal
HV *stash = get_uint128_stash(); 0 2461 2461
168 2461 SV *su128 = new_su128(aTHX);
169 SV *sv;
170 2461 SvI128Y(su128) = u128;
171 2461 sv = newRV_noinc(su128);
172 2461 sv_bless(sv, stash);
173 2461 SvREADONLY_on(su128);
174 2461 return sv;
175 }
176  
177 static int
178 0 SvI128OK(pTHX_ SV *sv) {
179 0 ▶ if (SvROK(sv)) {
Branch: 1 branch
BranchTrueFalseTotal
if (SvROK(sv)) { 0 0 0
180 0 SV *si128 = SvRV(sv);
181 0 ▶ return (si128 && (SvTYPE(si128) >= SVt_I128) && sv_isa(sv, "Math::Int128"));
Branch: 3 branches
BranchTrueFalseTotal
return (si128 && (SvTYPE(si128) >= SVt_I128) && sv_isa(sv, "Math::Int128")); 0 0 0
return (si128 && (SvTYPE(si128) >= SVt_I128) && sv_isa(sv, "Math::Int128")); 0 0 0
return (si128 && (SvTYPE(si128) >= SVt_I128) && sv_isa(sv, "Math::Int128")); 0 0 0
182 }
183 0 return 0;
184 }
185  
186 static int
187 0 SvU128OK(pTHX_ SV *sv) {
188 0 ▶ if (SvROK(sv)) {
Branch: 1 branch
BranchTrueFalseTotal
if (SvROK(sv)) { 0 0 0
189 0 SV *su128 = SvRV(sv);
190 0 ▶ return (su128 && (SvTYPE(su128) >= SVt_I128) && sv_isa(sv, "Math::UInt128"));
Branch: 3 branches
BranchTrueFalseTotal
return (su128 && (SvTYPE(su128) >= SVt_I128) && sv_isa(sv, "Math::UInt128")); 0 0 0
return (su128 && (SvTYPE(su128) >= SVt_I128) && sv_isa(sv, "Math::UInt128")); 0 0 0
return (su128 && (SvTYPE(su128) >= SVt_I128) && sv_isa(sv, "Math::UInt128")); 0 0 0
191 }
192 0 return 0;
193 }
194  
195 #define SvI128X(sv) (SvI128Y(SvRV(sv)))
196 #define SvU128X(sv) (SvU128Y(SvRV(sv)))
197  
198 static SV *
199 12116 SvSI128(pTHX_ SV *sv) {
200 12116 ▶ if (SvROK(sv)) {
Branch: 1 branch
BranchTrueFalseTotal
if (SvROK(sv)) { 12116 0 12116
201 12116 SV *si128 = SvRV(sv);
202 12116 ▶ if (SvPOK(si128) && (SvCUR(si128) == I128LEN))
Branch: 2 branches
BranchTrueFalseTotal
if (SvPOK(si128) && (SvCUR(si128) == I128LEN)) 12116 0 12116
if (SvPOK(si128) && (SvCUR(si128) == I128LEN)) 12116 0 12116
203 12116 return si128;
204 }
205 0 croak_string(aTHX_ "internal error: reference to int128_t expected");
206 0 return NULL; /* never happens */
207 }
208  
209 static SV *
210 12613 SvSU128(pTHX_ SV *sv) {
211 12613 ▶ if (SvROK(sv)) {
Branch: 1 branch
BranchTrueFalseTotal
if (SvROK(sv)) { 12613 0 12613
212 12613 SV *su128 = SvRV(sv);
213 12613 ▶ if (SvPOK(su128) && (SvCUR(su128) == I128LEN))
Branch: 2 branches
BranchTrueFalseTotal
if (SvPOK(su128) && (SvCUR(su128) == I128LEN)) 12613 0 12613
if (SvPOK(su128) && (SvCUR(su128) == I128LEN)) 12613 0 12613
214 12613 return su128;
215 }
216 0 croak_string(aTHX_ "internal error: reference to uint128_t expected");
217 0 return NULL; /* never happens */
218 }
219  
220 #define SvI128x(sv) SvI128Y(SvSI128(aTHX_ sv))
221 #define SvU128x(sv) SvU128Y(SvSU128(aTHX_ sv))
222  
223 static int128_t
224 5792 SvI128(pTHX_ SV *sv) {
225 5792 ▶ if (SvROK(sv)) {
Branch: 1 branch
BranchTrueFalseTotal
if (SvROK(sv)) { 169 5623 5792
226 169 SV *si128 = SvRV(sv);
227 169 ▶ if (si128 && SvOBJECT(si128)) {
Branch: 2 branches
BranchTrueFalseTotal
if (si128 && SvOBJECT(si128)) { 169 0 169
if (si128 && SvOBJECT(si128)) { 169 0 169
228 169 HV *stash = SvSTASH(si128);
229 #ifdef CACHE_STASHES
230 169 ▶ if (stash == int128_stash) {
Branch: 1 branch
BranchTrueFalseTotal
if (stash == int128_stash) { 169 0 169
231 169 return SvI128Y(si128);
232 }
233 0 ▶ else if (stash == uint128_stash) {
Branch: 1 branch
BranchTrueFalseTotal
else if (stash == uint128_stash) { 0 0 0
234 0 int128_t u128 = SvU128Y(si128);
235 if (may_die_on_overflow && (u128 > INT128_MAX))
236 overflow(aTHX_ out_of_bounds_error_s);
237 0 return u128;
238 }
239 #else
240 if (0);
241 #endif
242 else {
243 GV *method;
244 0 ▶ char const * classname = HvNAME_get(stash);
Branch: 6 branches
BranchTrueFalseTotal
char const * classname = HvNAME_get(stash); 0 0 0
char const * classname = HvNAME_get(stash); 0 0 0
char const * classname = HvNAME_get(stash); 0 0 0
char const * classname = HvNAME_get(stash); 0 0 0
char const * classname = HvNAME_get(stash); 0 0 0
char const * classname = HvNAME_get(stash); 0 0 0
245 0 ▶ if (memcmp(classname, "Math::", 6) == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (memcmp(classname, "Math::", 6) == 0) { 0 0 0
246 int u;
247 0 ▶ if (classname[6] == 'U') {
Branch: 1 branch
BranchTrueFalseTotal
if (classname[6] == 'U') { 0 0 0
248 0 classname += 7;
249 0 u = 1;
250 }
251 else {
252 0 classname += 6;
253 0 u = 0;
254 }
255 0 ▶ if (memcmp(classname, "Int", 3) == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (memcmp(classname, "Int", 3) == 0) { 0 0 0
256 0 classname += 3;
257 0 ▶ if (memcmp(classname, "128", 4) == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (memcmp(classname, "128", 4) == 0) { 0 0 0
258 0 ▶ if (!SvPOK(si128) || (SvCUR(si128) != I128LEN))
Branch: 2 branches
BranchTrueFalseTotal
if (!SvPOK(si128) || (SvCUR(si128) != I128LEN)) 0 0 0
if (!SvPOK(si128) || (SvCUR(si128) != I128LEN)) 0 0 0
259 0 ▶ Perl_croak(aTHX_ "Wrong internal representation for %s object", HvNAME_get(stash));
Branch: 6 branches
BranchTrueFalseTotal
Perl_croak(aTHX_ "Wrong internal representation for %s object", HvNAME_get(stash)); 0 0 0
Perl_croak(aTHX_ "Wrong internal representation for %s object", HvNAME_get(stash)); 0 0 0
Perl_croak(aTHX_ "Wrong internal representation for %s object", HvNAME_get(stash)); 0 0 0
Perl_croak(aTHX_ "Wrong internal representation for %s object", HvNAME_get(stash)); 0 0 0
Perl_croak(aTHX_ "Wrong internal representation for %s object", HvNAME_get(stash)); 0 0 0
Perl_croak(aTHX_ "Wrong internal representation for %s object", HvNAME_get(stash)); 0 0 0
260 0 ▶ if (u) {
Branch: 1 branch
BranchTrueFalseTotal
if (u) { 0 0 0
261 0 int128_t u128 = SvU128Y(si128);
262 if (may_die_on_overflow && (u128 > INT128_MAX))
263 overflow(aTHX_ out_of_bounds_error_s);
264 0 return u128;
265 }
266 0 return SvI128Y(si128);
267 }
268 0 ▶ if (memcmp(classname, "64", 3) == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (memcmp(classname, "64", 3) == 0) { 0 0 0
269 0 ▶ if (u) {
Branch: 1 branch
BranchTrueFalseTotal
if (u) { 0 0 0
270 0 return SvU64(sv);
271 }
272 0 return SvI64(sv);
273 }
274 }
275 }
276 0 method = gv_fetchmethod(stash, "as_int128");
277 0 ▶ if (method) {
Branch: 1 branch
BranchTrueFalseTotal
if (method) { 0 0 0
278 SV *result;
279 int count;
280 0 dSP;
281 0 ENTER;
282 0 SAVETMPS;
283 0 PUSHSTACKi(PERLSI_MAGIC);
284 0 ▶ PUSHMARK(SP);
Branch: 1 branch
BranchTrueFalseTotal
PUSHMARK(SP); 0 0 0
285 0 ▶ XPUSHs(sv);
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(sv); 0 0 0
286 0 PUTBACK;
287 0 count = perl_call_sv( (SV*)method, G_SCALAR );
288 0 SPAGAIN;
289 0 ▶ if (count != 1)
Branch: 1 branch
BranchTrueFalseTotal
if (count != 1) 0 0 0
290 0 Perl_croak(aTHX_ "internal error: method call returned %d values, 1 expected", count);
291 0 result = newSVsv(POPs);
292 0 PUTBACK;
293 0 POPSTACK;
294 0 SPAGAIN;
295 0 ▶ FREETMPS;
Branch: 1 branch
BranchTrueFalseTotal
FREETMPS; 0 0 0
296 0 LEAVE;
297 0 return SvI128(aTHX_ sv_2mortal(result));
298 }
299 }
300 }
301 }
302 else {
303 5623 SvGETMAGIC(sv);
304 5623 ▶ if (SvIOK(sv)) {
Branch: 1 branch
BranchTrueFalseTotal
if (SvIOK(sv)) { 5493 130 5623
305 5493 ▶ if (SvIOK_UV(sv))
Branch: 1 branch
BranchTrueFalseTotal
if (SvIOK_UV(sv)) 2 5491 5493
306 2 return SvUV(sv);
307 5491 return SvIV(sv);
308 }
309 130 ▶ if (SvNOK(sv)) {
Branch: 1 branch
BranchTrueFalseTotal
if (SvNOK(sv)) { 126 4 130
310 126 NV nv = SvNV(sv);
311 126 ▶ if (may_die_on_overflow &&
Branch: 2 branches
BranchTrueFalseTotal
if (may_die_on_overflow && 0 126 126
if (may_die_on_overflow && 0 0 0
312 0 ▶ ((nv >= 0x1p127) || (nv < -0x1p127))) overflow(aTHX_ out_of_bounds_error_s);
Branch: 1 branch
BranchTrueFalseTotal
((nv >= 0x1p127) || (nv < -0x1p127))) overflow(aTHX_ out_of_bounds_error_s); 0 0 0
313 126 return nv;
314 }
315 }
316 4 return strtoint128(aTHX_ SvPV_nolen(sv), 10, 1);
317 }
318  
319 static uint128_t
320 7583 SvU128(pTHX_ SV *sv) {
321 7583 ▶ if (SvROK(sv)) {
Branch: 1 branch
BranchTrueFalseTotal
if (SvROK(sv)) { 177 7406 7583
322 177 SV *su128 = SvRV(sv);
323 177 ▶ if (su128 && SvOBJECT(su128)) {
Branch: 2 branches
BranchTrueFalseTotal
if (su128 && SvOBJECT(su128)) { 177 0 177
if (su128 && SvOBJECT(su128)) { 177 0 177
324 177 HV *stash = SvSTASH(su128);
325 #ifdef CACHE_STASHES
326 177 ▶ if (stash == uint128_stash)
Branch: 1 branch
BranchTrueFalseTotal
if (stash == uint128_stash) 177 0 177
327 177 return SvU128Y(su128);
328 0 ▶ else if (stash == int128_stash) {
Branch: 1 branch
BranchTrueFalseTotal
else if (stash == int128_stash) { 0 0 0
329 0 int128_t i128 = SvI128Y(su128);
330 0 ▶ if (may_die_on_overflow && (i128 < 0))
Branch: 2 branches
BranchTrueFalseTotal
if (may_die_on_overflow && (i128 < 0)) 0 0 0
if (may_die_on_overflow && (i128 < 0)) 0 0 0
331 0 overflow(aTHX_ out_of_bounds_error_u);
332 0 return i128;
333 }
334 #else
335 if (0);
336 #endif
337 else {
338 GV *method;
339 0 ▶ char const * classname = HvNAME_get(stash);
Branch: 6 branches
BranchTrueFalseTotal
char const * classname = HvNAME_get(stash); 0 0 0
char const * classname = HvNAME_get(stash); 0 0 0
char const * classname = HvNAME_get(stash); 0 0 0
char const * classname = HvNAME_get(stash); 0 0 0
char const * classname = HvNAME_get(stash); 0 0 0
char const * classname = HvNAME_get(stash); 0 0 0
340 0 ▶ if (memcmp(classname, "Math::", 6) == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (memcmp(classname, "Math::", 6) == 0) { 0 0 0
341 int u;
342 0 ▶ if (classname[6] == 'U') {
Branch: 1 branch
BranchTrueFalseTotal
if (classname[6] == 'U') { 0 0 0
343 0 classname += 7;
344 0 u = 1;
345 }
346 else {
347 0 classname += 6;
348 0 u = 0;
349 }
350 0 ▶ if (memcmp(classname, "Int", 3) == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (memcmp(classname, "Int", 3) == 0) { 0 0 0
351 0 classname += 3;
352 0 ▶ if (memcmp(classname, "128", 4) == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (memcmp(classname, "128", 4) == 0) { 0 0 0
353 0 ▶ if (!SvPOK(su128) || (SvCUR(su128) != I128LEN))
Branch: 2 branches
BranchTrueFalseTotal
if (!SvPOK(su128) || (SvCUR(su128) != I128LEN)) 0 0 0
if (!SvPOK(su128) || (SvCUR(su128) != I128LEN)) 0 0 0
354 0 ▶ Perl_croak(aTHX_ "Wrong internal representation for %s object", HvNAME_get(stash));
Branch: 6 branches
BranchTrueFalseTotal
Perl_croak(aTHX_ "Wrong internal representation for %s object", HvNAME_get(stash)); 0 0 0
Perl_croak(aTHX_ "Wrong internal representation for %s object", HvNAME_get(stash)); 0 0 0
Perl_croak(aTHX_ "Wrong internal representation for %s object", HvNAME_get(stash)); 0 0 0
Perl_croak(aTHX_ "Wrong internal representation for %s object", HvNAME_get(stash)); 0 0 0
Perl_croak(aTHX_ "Wrong internal representation for %s object", HvNAME_get(stash)); 0 0 0
Perl_croak(aTHX_ "Wrong internal representation for %s object", HvNAME_get(stash)); 0 0 0
355 0 ▶ if (u)
Branch: 1 branch
BranchTrueFalseTotal
if (u) 0 0 0
356 0 return SvU128Y(su128);
357 else {
358 0 int128_t i128 = SvI128Y(su128);
359 0 ▶ if (may_die_on_overflow && (i128 < 0)) overflow(aTHX_ out_of_bounds_error_u);
Branch: 2 branches
BranchTrueFalseTotal
if (may_die_on_overflow && (i128 < 0)) overflow(aTHX_ out_of_bounds_error_u); 0 0 0
if (may_die_on_overflow && (i128 < 0)) overflow(aTHX_ out_of_bounds_error_u); 0 0 0
360 0 return i128;
361 }
362 }
363 0 ▶ if (memcmp(classname, "64", 3) == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (memcmp(classname, "64", 3) == 0) { 0 0 0
364 0 ▶ if (u) {
Branch: 1 branch
BranchTrueFalseTotal
if (u) { 0 0 0
365 0 return SvU64(sv);
366 }
367 else {
368 0 int64_t i64 = SvI64(sv);
369 0 ▶ if (may_die_on_overflow && (i64 < 0)) overflow(aTHX_ out_of_bounds_error_u);
Branch: 2 branches
BranchTrueFalseTotal
if (may_die_on_overflow && (i64 < 0)) overflow(aTHX_ out_of_bounds_error_u); 0 0 0
if (may_die_on_overflow && (i64 < 0)) overflow(aTHX_ out_of_bounds_error_u); 0 0 0
370 0 return i64;
371 }
372 }
373 }
374 }
375 0 method = gv_fetchmethod(stash, "as_uint128");
376 0 ▶ if (method) {
Branch: 1 branch
BranchTrueFalseTotal
if (method) { 0 0 0
377 SV *result;
378 int count;
379 0 dSP;
380 0 ENTER;
381 0 SAVETMPS;
382 0 PUSHSTACKi(PERLSI_MAGIC);
383 0 ▶ PUSHMARK(SP);
Branch: 1 branch
BranchTrueFalseTotal
PUSHMARK(SP); 0 0 0
384 0 ▶ XPUSHs(sv);
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(sv); 0 0 0
385 0 PUTBACK;
386 0 count = perl_call_sv( (SV*)method, G_SCALAR );
387 0 SPAGAIN;
388 0 ▶ if (count != 1)
Branch: 1 branch
BranchTrueFalseTotal
if (count != 1) 0 0 0
389 0 Perl_croak(aTHX_ "internal error: method call returned %d values, 1 expected", count);
390 0 result = newSVsv(POPs);
391 0 PUTBACK;
392 0 POPSTACK;
393 0 SPAGAIN;
394 0 ▶ FREETMPS;
Branch: 1 branch
BranchTrueFalseTotal
FREETMPS; 0 0 0
395 0 LEAVE;
396 0 return SvU128(aTHX_ sv_2mortal(result));
397 }
398 }
399 }
400 }
401 else {
402 7406 SvGETMAGIC(sv);
403 7406 ▶ if (SvIOK(sv)) {
Branch: 1 branch
BranchTrueFalseTotal
if (SvIOK(sv)) { 7210 196 7406
404 7210 ▶ if (SvIOK_UV(sv))
Branch: 1 branch
BranchTrueFalseTotal
if (SvIOK_UV(sv)) 3 7207 7210
405 3 return SvUV(sv);
406 else {
407 7207 IV iv = SvIV(sv);
408 7207 ▶ if (may_die_on_overflow && (iv < 0)) overflow(aTHX_ out_of_bounds_error_u);
Branch: 2 branches
BranchTrueFalseTotal
if (may_die_on_overflow && (iv < 0)) overflow(aTHX_ out_of_bounds_error_u); 0 7207 7207
if (may_die_on_overflow && (iv < 0)) overflow(aTHX_ out_of_bounds_error_u); 0 0 0
409 7207 return iv;
410 }
411 }
412 196 ▶ if (SvNOK(sv)) {
Branch: 1 branch
BranchTrueFalseTotal
if (SvNOK(sv)) { 191 5 196
413 191 NV nv = SvNV(sv);
414 191 ▶ if (may_die_on_overflow && ((nv < 0) || (nv >= 0x1p128))) overflow(aTHX_ out_of_bounds_error_u);
Branch: 3 branches
BranchTrueFalseTotal
if (may_die_on_overflow && ((nv < 0) || (nv >= 0x1p128))) overflow(aTHX_ out_of_bounds_error_u); 0 191 191
if (may_die_on_overflow && ((nv < 0) || (nv >= 0x1p128))) overflow(aTHX_ out_of_bounds_error_u); 0 0 0
if (may_die_on_overflow && ((nv < 0) || (nv >= 0x1p128))) overflow(aTHX_ out_of_bounds_error_u); 0 0 0
415 191 return nv;
416 }
417 }
418 5 return strtoint128(aTHX_ SvPV_nolen(sv), 10, 0);
419 }
420  
421 static SV *
422 1 si128_to_number(pTHX_ SV *sv) {
423 1 int128_t i128 = SvI128(aTHX_ sv);
424 1 ▶ if (i128 < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (i128 < 0) { 0 1 1
425 0 IV iv = i128;
426 0 ▶ if (iv == i128)
Branch: 1 branch
BranchTrueFalseTotal
if (iv == i128) 0 0 0
427 0 return newSViv(iv);
428 }
429 else {
430 1 UV uv = i128;
431 1 ▶ if (uv == i128)
Branch: 1 branch
BranchTrueFalseTotal
if (uv == i128) 1 0 1
432 1 return newSVuv(uv);
433 }
434 0 return newSVnv(i128);
435 }
436  
437 static SV *
438 1 su128_to_number(pTHX_ SV *sv) {
439 1 uint128_t u128 = SvU128(aTHX_ sv);
440 UV uv;
441 1 uv = u128;
442 1 ▶ if (uv == u128)
Branch: 1 branch
BranchTrueFalseTotal
if (uv == u128) 1 0 1
443 1 return newSVuv(uv);
444 0 return newSVnv(u128);
445 }
446  
447 #define I128STRLEN 44
448  
449 static STRLEN
450 4388 u128_to_string(uint128_t u128, char *to) {
451 char str[I128STRLEN];
452 4388 int i, len = 0;
453 83576 ▶ while (u128) {
Branch: 1 branch
BranchTrueFalseTotal
while (u128) { 79188 4388 83576
454 79188 str[len++] = '0' + u128 % 10;
455 79188 u128 /= 10;
456 }
457 4388 ▶ if (len) {
Branch: 1 branch
BranchTrueFalseTotal
if (len) { 4384 4 4388
458 83572 ▶ for (i = len; i--;) *(to++) = str[i];
Branch: 1 branch
BranchTrueFalseTotal
for (i = len; i--;) *(to++) = str[i]; 79188 4384 83572
459 4384 return len;
460 }
461 else {
462 4 to[0] = '0';
463 4 return 1;
464 }
465 }
466  
467 static STRLEN
468 2063 i128_to_string(int128_t i128, char *to) {
469 2063 ▶ if (i128 < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (i128 < 0) { 0 2063 2063
470 0 *(to++) = '-';
471 0 return u128_to_string(-i128, to) + 1;
472 }
473 2063 return u128_to_string(i128, to);
474 }
475  
476 static void
477 0 u128_to_hex(uint128_t i128, char *to) {
478 0 int i = I128LEN * 2;
479 0 ▶ while (i--) {
Branch: 1 branch
BranchTrueFalseTotal
while (i--) { 0 0 0
480 0 int v = i128 & 15;
481 0 ▶ to[i] = v + ((v > 9) ? ('A' - 10) : '0');
Branch: 1 branch
BranchTrueFalseTotal
to[i] = v + ((v > 9) ? ('A' - 10) : '0'); 0 0 0
482 0 i128 >>= 4;
483 }
484 0 }
485  
486 static void
487 0 mul_check_overflow(pTHX_ uint128_t a, uint128_t b, const char *error_str) {
488 0 ▶ if (a < b) {
Branch: 1 branch
BranchTrueFalseTotal
if (a < b) { 0 0 0
489 0 uint128_t tmp = a;
490 0 a = b; b = tmp;
491 }
492 0 ▶ if (b > UINT64_MAX) overflow(aTHX_ error_str);
Branch: 1 branch
BranchTrueFalseTotal
if (b > UINT64_MAX) overflow(aTHX_ error_str); 0 0 0
493 else {
494 uint128_t rl, rh;
495 0 rl = (a & UINT64_MAX) * b;
496 0 rh = (a >> 64) * b + (rl >> 64);
497 0 ▶ if (rh > UINT64_MAX) overflow(aTHX_ error_str);
Branch: 1 branch
BranchTrueFalseTotal
if (rh > UINT64_MAX) overflow(aTHX_ error_str); 0 0 0
498 }
499 0 }
500  
501 static uint128_t
502 1433 powU128(pTHX_ uint128_t a, uint128_t b) {
503 uint128_t r;
504 1433 int mdoo = may_die_on_overflow;
505 1433 ▶ if (b == 0) return 1;
Branch: 1 branch
BranchTrueFalseTotal
if (b == 0) return 1; 18 1415 1433
506 1415 ▶ if (b == 1) return a;
Branch: 1 branch
BranchTrueFalseTotal
if (b == 1) return a; 19 1396 1415
507 1396 ▶ if (b == 2) {
Branch: 1 branch
BranchTrueFalseTotal
if (b == 2) { 19 1377 1396
508 19 ▶ if (mdoo && (a > UINT64_MAX)) overflow(aTHX_ pow_error);
Branch: 2 branches
BranchTrueFalseTotal
if (mdoo && (a > UINT64_MAX)) overflow(aTHX_ pow_error); 0 19 19
if (mdoo && (a > UINT64_MAX)) overflow(aTHX_ pow_error); 0 0 0
509 19 return a*a;
510 }
511 1377 ▶ if (a == 0) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (a == 0) return 0; 0 1377 1377
512 1377 ▶ if (a == 1) return 1;
Branch: 1 branch
BranchTrueFalseTotal
if (a == 1) return 1; 0 1377 1377
513 1377 ▶ if (a == 2) {
Branch: 1 branch
BranchTrueFalseTotal
if (a == 2) { 874 503 1377
514 874 ▶ if (b > 127) {
Branch: 1 branch
BranchTrueFalseTotal
if (b > 127) { 1 873 874
515 1 ▶ if (mdoo) overflow(aTHX_ pow_error);
Branch: 1 branch
BranchTrueFalseTotal
if (mdoo) overflow(aTHX_ pow_error); 0 1 1
516 1 return 0;
517 }
518 873 return (((uint128_t)1) << b);
519 }
520 503 ▶ if (mdoo) {
Branch: 1 branch
BranchTrueFalseTotal
if (mdoo) { 0 503 503
521 0 ▶ r = ((b & 1) ? a : 1);
Branch: 1 branch
BranchTrueFalseTotal
r = ((b & 1) ? a : 1); 0 0 0
522 0 ▶ while ((b >>= 1)) {
Branch: 1 branch
BranchTrueFalseTotal
while ((b >>= 1)) { 0 0 0
523 0 ▶ if (a > UINT64_MAX) overflow(aTHX_ pow_error);
Branch: 1 branch
BranchTrueFalseTotal
if (a > UINT64_MAX) overflow(aTHX_ pow_error); 0 0 0
524 0 a *= a;
525 0 ▶ if (b & 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (b & 1) { 0 0 0
526 0 mul_check_overflow(aTHX_ r, a, pow_error);
527 0 r *= a;
528 }
529 }
530 }
531 else {
532 503 r = 1;
533 3067 ▶ while (b) {
Branch: 1 branch
BranchTrueFalseTotal
while (b) { 2564 503 3067
534 2564 ▶ if (b & 1) r *= a;
Branch: 1 branch
BranchTrueFalseTotal
if (b & 1) r *= a; 1508 1056 2564
535 2564 a *= a;
536 2564 b >>= 1;
537 }
538 }
539 503 return r;
540 }
541  
542 #include "c_api.h"
543  
544 MODULE = Math::Int128 PACKAGE = Math::Int128 PREFIX=miu128_
545  
546 BOOT:
547 3 init_stash_cache(aTHX);
548 3 ▶ PERL_MATH_INT64_LOAD_OR_CROAK;
Branch: 1 branch
BranchTrueFalseTotal
PERL_MATH_INT64_LOAD_OR_CROAK; 0 3 3
549 3 INIT_C_API;
550  
551 void
552 CLONE()
553 CODE:
554 0 init_stash_cache(aTHX);
555  
556 void
557 miu128__set_may_die_on_overflow(v)
558 int v
559 CODE:
560 0 may_die_on_overflow = v;
561  
562 SV *
563 miu128_int128(value=0)
564 SV *value;
565 CODE:
566 683 ▶ RETVAL = newSVi128(aTHX_ (value ? SvI128(aTHX_ value) : 0));
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = newSVi128(aTHX_ (value ? SvI128(aTHX_ value) : 0)); 683 0 683
567 OUTPUT:
568 RETVAL
569  
570 SV *
571 miu128_uint128(value=0)
572 SV *value;
573 CODE:
574 925 ▶ RETVAL = newSVu128(aTHX_ (value ? SvU128(aTHX_ value) : 0));
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = newSVu128(aTHX_ (value ? SvU128(aTHX_ value) : 0)); 925 0 925
575 OUTPUT:
576 RETVAL
577  
578 SV *
579 miu128_int128_to_number(self)
580 SV *self
581 CODE:
582 0 RETVAL = si128_to_number(aTHX_ self);
583 OUTPUT:
584 RETVAL
585  
586 SV *
587 miu128_uint128_to_number(self)
588 SV *self
589 CODE:
590 0 RETVAL = su128_to_number(aTHX_ self);
591 OUTPUT:
592 RETVAL
593  
594 SV *
595 miu128_net_to_int128(net)
596 SV *net;
597 PREINIT:
598 STRLEN len;
599 4 unsigned char *pv = (unsigned char *)SvPV(net, len);
600 CODE:
601 4 ▶ if (len != 16)
Branch: 1 branch
BranchTrueFalseTotal
if (len != 16) 0 4 4
602 0 croak_string(aTHX_ "Invalid length for int128_t");
603 4 RETVAL = newSVi128(aTHX_
604 4 (((((((((((((((((((((((((((((((int128_t)pv[0]) << 8)
605 4 + (int128_t)pv[1]) << 8)
606 4 + (int128_t)pv[2]) << 8)
607 4 + (int128_t)pv[3]) << 8)
608 4 + (int128_t)pv[4]) << 8)
609 4 + (int128_t)pv[5]) << 8)
610 4 + (int128_t)pv[6]) << 8)
611 4 + (int128_t)pv[7]) << 8)
612 4 + (int128_t)pv[8]) << 8)
613 4 + (int128_t)pv[9]) << 8)
614 4 + (int128_t)pv[10]) << 8)
615 4 + (int128_t)pv[11]) << 8)
616 4 + (int128_t)pv[12]) << 8)
617 4 + (int128_t)pv[13]) << 8)
618 4 + (int128_t)pv[14]) << 8)
619 4 + (int128_t)pv[15]);
620 OUTPUT:
621 RETVAL
622  
623 SV *
624 miu128_net_to_uint128(net)
625 SV *net;
626 PREINIT:
627 STRLEN len;
628 6 unsigned char *pv = (unsigned char *)SvPV(net, len);
629 CODE:
630 6 ▶ if (len != 16)
Branch: 1 branch
BranchTrueFalseTotal
if (len != 16) 0 6 6
631 0 croak_string(aTHX_ "Invalid length for uint128_t");
632 6 RETVAL = newSVu128(aTHX_
633 6 (((((((((((((((((((((((((((((((uint128_t)pv[0]) << 8)
634 6 + (uint128_t)pv[1]) << 8)
635 6 + (uint128_t)pv[2]) << 8)
636 6 + (uint128_t)pv[3]) << 8)
637 6 + (uint128_t)pv[4]) << 8)
638 6 + (uint128_t)pv[5]) << 8)
639 6 + (uint128_t)pv[6]) << 8)
640 6 + (uint128_t)pv[7]) << 8)
641 6 + (uint128_t)pv[8]) << 8)
642 6 + (uint128_t)pv[9]) << 8)
643 6 + (uint128_t)pv[10]) << 8)
644 6 + (uint128_t)pv[11]) << 8)
645 6 + (uint128_t)pv[12]) << 8)
646 6 + (uint128_t)pv[13]) << 8)
647 6 + (uint128_t)pv[14]) << 8)
648 6 + (uint128_t)pv[15]);
649 OUTPUT:
650 RETVAL
651  
652 SV *
653 miu128_int128_to_net(self)
654 SV *self
655 PREINIT:
656 char *pv;
657 1 int128_t i128 = SvI128(aTHX_ self);
658 int i;
659 CODE:
660 1 RETVAL = newSV(I128LEN);
661 1 SvPOK_on(RETVAL);
662 1 SvCUR_set(RETVAL, I128LEN);
663 1 pv = SvPVX(RETVAL);
664 1 pv[I128LEN] = '\0';
665 17 ▶ for (i = I128LEN - 1; i >= 0; i--, i128 >>= 8)
Branch: 1 branch
BranchTrueFalseTotal
for (i = I128LEN - 1; i >= 0; i--, i128 >>= 8) 16 1 17
666 16 pv[i] = i128;
667 OUTPUT:
668 RETVAL
669  
670 SV *
671 miu128_uint128_to_net(self)
672 SV *self
673 PREINIT:
674 char *pv;
675 6 uint128_t u128 = SvU128(aTHX_ self);
676 int i;
677 CODE:
678 6 RETVAL = newSV(I128LEN);
679 6 SvPOK_on(RETVAL);
680 6 SvCUR_set(RETVAL, I128LEN);
681 6 pv = SvPVX(RETVAL);
682 6 pv[I128LEN] = '\0';
683 102 ▶ for (i = I128LEN - 1; i >= 0; i--, u128 >>= 8)
Branch: 1 branch
BranchTrueFalseTotal
for (i = I128LEN - 1; i >= 0; i--, u128 >>= 8) 96 6 102
684 96 pv[i] = u128;
685 OUTPUT:
686 RETVAL
687  
688 SV *
689 miu128_native_to_int128(native)
690 SV *native
691 PREINIT:
692 STRLEN len;
693 3 char *pv = SvPV(native, len);
694 CODE:
695 3 ▶ if (len != I128LEN)
Branch: 1 branch
BranchTrueFalseTotal
if (len != I128LEN) 0 3 3
696 0 croak_string(aTHX_ "Invalid length for int128_t");
697 3 RETVAL = newSVi128(aTHX_ 0);
698 3 Copy(pv, &(SvI128X(RETVAL)), I128LEN, char);
699 OUTPUT:
700 RETVAL
701  
702 SV *
703 miu128_native_to_uint128(native)
704 SV *native
705 PREINIT:
706 STRLEN len;
707 6 char *pv = SvPV(native, len);
708 CODE:
709 6 ▶ if (len != I128LEN)
Branch: 1 branch
BranchTrueFalseTotal
if (len != I128LEN) 0 6 6
710 0 croak_string(aTHX_ "Invalid length for uint128_t");
711 6 RETVAL = newSVu128(aTHX_ 0);
712 6 Copy(pv, &(SvU128X(RETVAL)), I128LEN, char);
713 OUTPUT:
714 RETVAL
715  
716 SV *
717 miu128_int128_to_native(self)
718 SV *self
719 PREINIT:
720 char *pv;
721 3 int128_t i128 = SvI128(aTHX_ self);
722 CODE:
723 3 RETVAL = newSV(I128LEN);
724 3 SvPOK_on(RETVAL);
725 3 SvCUR_set(RETVAL, I128LEN);
726 3 pv = SvPVX(RETVAL);
727 3 Copy(&i128, pv, I128LEN, char);
728 3 pv[I128LEN] = '\0';
729 OUTPUT:
730 RETVAL
731  
732 SV *
733 miu128_uint128_to_native(self)
734 SV *self
735 PREINIT:
736 char *pv;
737 6 uint128_t u128 = SvU128(aTHX_ self);
738 CODE:
739 6 RETVAL = newSV(I128LEN);
740 6 SvPOK_on(RETVAL);
741 6 SvCUR_set(RETVAL, I128LEN);
742 6 pv = SvPVX(RETVAL);
743 6 Copy(&u128, pv, I128LEN, char);
744 6 pv[I128LEN] = '\0';
745 OUTPUT:
746 RETVAL
747  
748 SV *
749 miu128_uint128_to_hex(self)
750 SV *self
751 PREINIT:
752 char *pv;
753 0 uint128_t u128 = SvU128(aTHX_ self);
754 CODE:
755 0 RETVAL = newSV(I128LEN * 2);
756 0 SvPOK_on(RETVAL);
757 0 SvCUR_set(RETVAL, I128LEN * 2);
758 0 pv = SvPVX(RETVAL);
759 0 u128_to_hex(u128, pv);
760 OUTPUT:
761 RETVAL
762  
763 SV *
764 miu128_int128_to_hex(self)
765 SV *self
766 PREINIT:
767 char *pv;
768 0 uint128_t u128 = SvI128(aTHX_ self);
769 CODE:
770 0 RETVAL = newSV(I128LEN * 2);
771 0 SvPOK_on(RETVAL);
772 0 SvCUR_set(RETVAL, I128LEN * 2);
773 0 pv = SvPVX(RETVAL);
774 0 u128_to_hex(u128, pv);
775 OUTPUT:
776 RETVAL
777  
778 SV *
779 miu128_string_to_int128(str, base = 0)
780 const char *str;
781 int base;
782 CODE:
783 6 RETVAL = newSVi128(aTHX_ strtoint128(aTHX_ str, base, 1));
784 OUTPUT:
785 RETVAL
786  
787 SV *
788 miu128_string_to_uint128(str, base = 0)
789 const char *str;
790 int base;
791 CODE:
792 3 RETVAL = newSVu128(aTHX_ strtoint128(aTHX_ str, base, 0));
793 OUTPUT:
794 RETVAL
795  
796 MODULE = Math::Int128 PACKAGE = Math::Int128 PREFIX=mi128
797 PROTOTYPES: DISABLE
798  
799 SV *
800 mi128_inc(self, ...)
801 SV *self
802 PREINIT:
803 0 int128_t i128 = SvI128x(self);
804 CODE:
805 0 ▶ if (may_die_on_overflow && (i128 == INT128_MAX)) overflow(aTHX_ inc_error);
Branch: 2 branches
BranchTrueFalseTotal
if (may_die_on_overflow && (i128 == INT128_MAX)) overflow(aTHX_ inc_error); 0 0 0
if (may_die_on_overflow && (i128 == INT128_MAX)) overflow(aTHX_ inc_error); 0 0 0
806 0 SvI128x(self) = i128 + 1;
807 0 RETVAL = self;
808 0 SvREFCNT_inc(RETVAL);
809 OUTPUT:
810 RETVAL
811  
812 SV *
813 mi128_dec(self, ...)
814 SV *self
815 PREINIT:
816 0 int128_t i128 = SvI128x(self);
817 CODE:
818 0 ▶ if (may_die_on_overflow && (i128 == 0)) overflow(aTHX_ dec_error);
Branch: 2 branches
BranchTrueFalseTotal
if (may_die_on_overflow && (i128 == 0)) overflow(aTHX_ dec_error); 0 0 0
if (may_die_on_overflow && (i128 == 0)) overflow(aTHX_ dec_error); 0 0 0
819 0 SvI128x(self) = i128 - 1;
820 0 RETVAL = self;
821 0 SvREFCNT_inc(RETVAL);
822 OUTPUT:
823 RETVAL
824  
825 SV *
826 mi128_add(self, other, rev)
827 SV *self
828 SV *other
829 SV *rev
830 PREINIT:
831 13 int128_t a = SvI128x(self);
832 13 int128_t b = SvI128(aTHX_ other);
833 CODE:
834 13 ▶ if ( may_die_on_overflow &&
Branch: 3 branches
BranchTrueFalseTotal
if ( may_die_on_overflow && 0 13 13
if ( may_die_on_overflow && 0 0 0
if ( may_die_on_overflow && 0 0 0
835 ( a > 0
836 0 ▶ ? ( (b > 0) && (INT128_MAX - a < b) )
Branch: 2 branches
BranchTrueFalseTotal
? ( (b > 0) && (INT128_MAX - a < b) ) 0 0 0
? ( (b > 0) && (INT128_MAX - a < b) ) 0 0 0
837 0 ▶ : ( (b < 0) && (INT128_MIN - a > b) ) ) ) overflow(aTHX_ add_error);
Branch: 2 branches
BranchTrueFalseTotal
: ( (b < 0) && (INT128_MIN - a > b) ) ) ) overflow(aTHX_ add_error); 0 0 0
: ( (b < 0) && (INT128_MIN - a > b) ) ) ) overflow(aTHX_ add_error); 0 0 0
838 13 ▶ if (SvOK(rev))
Branch: 1 branch
BranchTrueFalseTotal
if (SvOK(rev)) 12 1 13
839 12 RETVAL = newSVi128(aTHX_ a + b);
840 else {
841 1 RETVAL = self;
842 1 SvREFCNT_inc(RETVAL);
843 1 SvI128x(self) = a + b;
844 }
845 OUTPUT:
846 RETVAL
847  
848 SV *
849 mi128_sub(self, other, rev)
850 SV *self
851 SV *other
852 SV *rev
853 PREINIT:
854 8 int128_t a = SvI128x(self);
855 8 int128_t b = SvI128(aTHX_ other);
856 CODE:
857 8 ▶ if (SvTRUE(rev)) {
Branch: 1 branch
BranchTrueFalseTotal
if (SvTRUE(rev)) { 0 8 8
858 0 int128_t tmp = a;
859 0 a = b; b = tmp;
860 }
861 8 ▶ if ( may_die_on_overflow &&
Branch: 3 branches
BranchTrueFalseTotal
if ( may_die_on_overflow && 0 8 8
if ( may_die_on_overflow && 0 0 0
if ( may_die_on_overflow && 0 0 0
862 ( a > 0
863 0 ▶ ? ( (b < 0) && (a - INT128_MAX > b) )
Branch: 2 branches
BranchTrueFalseTotal
? ( (b < 0) && (a - INT128_MAX > b) ) 0 0 0
? ( (b < 0) && (a - INT128_MAX > b) ) 0 0 0
864 0 ▶ : ( (b > 0) && (a - INT128_MIN < b) ) ) ) overflow(aTHX_ sub_error);
Branch: 2 branches
BranchTrueFalseTotal
: ( (b > 0) && (a - INT128_MIN < b) ) ) ) overflow(aTHX_ sub_error); 0 0 0
: ( (b > 0) && (a - INT128_MIN < b) ) ) ) overflow(aTHX_ sub_error); 0 0 0
865 8 ▶ if (SvOK(rev))
Branch: 1 branch
BranchTrueFalseTotal
if (SvOK(rev)) 7 1 8
866 7 RETVAL = newSVi128(aTHX_ a - b);
867 else {
868 1 RETVAL = self;
869 1 SvREFCNT_inc(RETVAL);
870 1 SvI128x(self) = a - b;
871 }
872 OUTPUT:
873 RETVAL
874  
875 SV *
876 mi128_mul(self, other, rev)
877 SV *self
878 SV *other
879 SV *rev
880 PREINIT:
881 4111 int128_t a1 = SvI128x(self);
882 4111 int128_t b1 = SvI128(aTHX_ other);
883 CODE:
884 4111 ▶ if (may_die_on_overflow) {
Branch: 1 branch
BranchTrueFalseTotal
if (may_die_on_overflow) { 0 4111 4111
885 0 int neg = 0;
886 uint128_t a, b;
887 0 ▶ if (a1 < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (a1 < 0) { 0 0 0
888 0 a = -a1;
889 0 neg ^= 1;
890 }
891 0 else a = a1;
892 0 ▶ if (b1 < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (b1 < 0) { 0 0 0
893 0 b = -b1;
894 0 neg ^= 1;
895 }
896 0 else b = b1;
897 0 mul_check_overflow(aTHX_ a, b, mul_error);
898 0 ▶ if (a * b > (neg ? (~(uint128_t)INT128_MIN + 1) : INT128_MAX)) overflow(aTHX_ mul_error);
Branch: 2 branches
BranchTrueFalseTotal
if (a * b > (neg ? (~(uint128_t)INT128_MIN + 1) : INT128_MAX)) overflow(aTHX_ mul_error); 0 0 0
if (a * b > (neg ? (~(uint128_t)INT128_MIN + 1) : INT128_MAX)) overflow(aTHX_ mul_error); 0 0 0
899 }
900 4111 ▶ if (SvOK(rev))
Branch: 1 branch
BranchTrueFalseTotal
if (SvOK(rev)) 10 4101 4111
901 10 RETVAL = newSVi128(aTHX_ a1 * b1);
902 else {
903 4101 RETVAL = self;
904 4101 SvREFCNT_inc(RETVAL);
905 4101 SvI128x(self) = a1 * b1;
906 }
907 OUTPUT:
908 RETVAL
909  
910 SV *
911 mi128_div(self, other, rev)
912 SV *self
913 SV *other
914 SV *rev
915 PREINIT:
916 int128_t up;
917 int128_t down;
918 CODE:
919 255 ▶ if (SvOK(rev)) {
Branch: 1 branch
BranchTrueFalseTotal
if (SvOK(rev)) { 255 0 255
920 255 ▶ if (SvTRUE(rev)) {
Branch: 1 branch
BranchTrueFalseTotal
if (SvTRUE(rev)) { 0 255 255
921 0 up = SvI128(aTHX_ other);
922 0 down = SvI128x(self);
923 }
924 else {
925 255 up = SvI128x(self);
926 255 down = SvI128(aTHX_ other);
927 }
928 255 ▶ if (!down)
Branch: 1 branch
BranchTrueFalseTotal
if (!down) 0 255 255
929 0 croak_string(aTHX_ div_by_0_error);
930 255 RETVAL = newSVi128(aTHX_ up/down);
931 }
932 else {
933 0 down = SvI128(aTHX_ other);
934 0 ▶ if (!down)
Branch: 1 branch
BranchTrueFalseTotal
if (!down) 0 0 0
935 0 croak_string(aTHX_ div_by_0_error);
936 0 RETVAL = self;
937 0 SvREFCNT_inc(RETVAL);
938 0 SvI128x(self) /= down;
939 }
940 OUTPUT:
941 RETVAL
942  
943 SV *
944 mi128_remainder(self, other, rev)
945 SV *self
946 SV *other
947 SV *rev
948 PREINIT:
949 int128_t up;
950 int128_t down;
951 CODE:
952 3 ▶ if (SvOK(rev)) {
Branch: 1 branch
BranchTrueFalseTotal
if (SvOK(rev)) { 2 1 3
953 2 ▶ if (SvTRUE(rev)) {
Branch: 1 branch
BranchTrueFalseTotal
if (SvTRUE(rev)) { 0 2 2
954 0 up = SvI128(aTHX_ other);
955 0 down = SvI128x(self);
956 }
957 else {
958 2 up = SvI128x(self);
959 2 down = SvI128(aTHX_ other);
960 }
961 2 ▶ if (!down)
Branch: 1 branch
BranchTrueFalseTotal
if (!down) 0 2 2
962 0 croak_string(aTHX_ div_by_0_error);
963 2 RETVAL = newSVi128(aTHX_ up % down);
964 }
965 else {
966 1 down = SvI128(aTHX_ other);
967 1 ▶ if (!down)
Branch: 1 branch
BranchTrueFalseTotal
if (!down) 0 1 1
968 0 croak_string(aTHX_ div_by_0_error);
969 1 RETVAL = self;
970 1 SvREFCNT_inc(RETVAL);
971 1 SvI128x(self) %= down;
972 }
973 OUTPUT:
974 RETVAL
975  
976 SV *mi128_left(self, other, rev)
977 SV *self
978 SV *other
979 SV *rev
980 CODE:
981 456 ▶ if (SvOK(rev)) {
Branch: 1 branch
BranchTrueFalseTotal
if (SvOK(rev)) { 326 130 456
982 uint128_t a, b;
983 326 ▶ if (SvTRUE(rev)) {
Branch: 1 branch
BranchTrueFalseTotal
if (SvTRUE(rev)) { 0 326 326
984 0 a = SvU128(aTHX_ other);
985 0 b = SvU128x(self);
986 }
987 else {
988 326 b = SvU128(aTHX_ other);
989 326 a = SvI128x(self);
990 }
991 326 ▶ RETVAL = newSVi128(aTHX_ (uint128_t)(b > 127 ? 0 : a << b));
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = newSVi128(aTHX_ (uint128_t)(b > 127 ? 0 : a << b)); 324 2 326
992 }
993 else {
994 130 uint128_t b = SvU128(aTHX_ other);
995 130 RETVAL = SvREFCNT_inc(self);
996 130 ▶ SvI128x(self) = (b > 127 ? 0 : SvI128x(self) << b);
Branch: 1 branch
BranchTrueFalseTotal
SvI128x(self) = (b > 127 ? 0 : SvI128x(self) << b); 128 2 130
997 }
998 OUTPUT:
999 RETVAL
1000  
1001 SV *mi128_right(self, other, rev)
1002 SV *self
1003 SV *other
1004 SV *rev
1005 PREINIT:
1006 int128_t a;
1007 uint128_t b;
1008 CODE:
1009 261 ▶ if (SvOK(rev)) {
Branch: 1 branch
BranchTrueFalseTotal
if (SvOK(rev)) { 131 130 261
1010 131 ▶ if (SvTRUE(rev)) {
Branch: 1 branch
BranchTrueFalseTotal
if (SvTRUE(rev)) { 0 131 131
1011 0 a = SvI128(aTHX_ other);
1012 0 b = SvU128x(self);
1013 }
1014 else {
1015 131 b = SvU128(aTHX_ other);
1016 131 a = SvI128x(self);
1017 }
1018 }
1019 else {
1020 130 a = SvI128x(self);
1021 130 b = SvU128(aTHX_ other);
1022 }
1023 261 ▶ RETVAL = newSVi128(aTHX_ (b > 127 ? (a >= 0 ? 0 : -1) : a >> b));
Branch: 2 branches
BranchTrueFalseTotal
RETVAL = newSVi128(aTHX_ (b > 127 ? (a >= 0 ? 0 : -1) : a >> b)); 8 253 261
RETVAL = newSVi128(aTHX_ (b > 127 ? (a >= 0 ? 0 : -1) : a >> b)); 4 4 8
1024 OUTPUT:
1025 RETVAL
1026  
1027 SV *mi128_pow(self, other, rev = &PL_sv_no)
1028 SV *self
1029 SV *other
1030 SV *rev
1031 PREINIT:
1032 int sign;
1033 uint128_t r;
1034 int128_t a, b;
1035 CODE:
1036 653 ▶ if (SvTRUE(rev)) {
Branch: 1 branch
BranchTrueFalseTotal
if (SvTRUE(rev)) { 0 653 653
1037 0 a = SvI128(aTHX_ other);
1038 0 b = SvI128x(self);
1039 }
1040 else {
1041 653 a = SvI128x(self);
1042 653 b = SvI128(aTHX_ other);
1043 }
1044 653 ▶ if (a < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (a < 0) { 0 653 653
1045 0 ▶ sign = ((b & 1) ? -1 : 1);
Branch: 1 branch
BranchTrueFalseTotal
sign = ((b & 1) ? -1 : 1); 0 0 0
1046 0 a = -a;
1047 }
1048 653 else sign = 1;
1049 653 ▶ if (b < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (b < 0) { 2 651 653
1050 2 ▶ if (a == 0) croak_string(aTHX_ div_by_0_error);
Branch: 1 branch
BranchTrueFalseTotal
if (a == 0) croak_string(aTHX_ div_by_0_error); 0 2 2
1051 2 ▶ else if (a == 1) r = sign;
Branch: 1 branch
BranchTrueFalseTotal
else if (a == 1) r = sign; 0 2 2
1052 2 else r = 0;
1053 }
1054 else {
1055 651 uint128_t u = powU128(aTHX_ a, b);
1056 651 ▶ if (may_die_on_overflow && (u > ((sign < 0) ? (~(uint128_t)INT128_MIN + 1) : INT128_MAX))) overflow(aTHX_ pow_error);
Branch: 3 branches
BranchTrueFalseTotal
if (may_die_on_overflow && (u > ((sign < 0) ? (~(uint128_t)INT128_MIN + 1) : INT128_MAX))) overflow(aTHX_ pow_error); 0 651 651
if (may_die_on_overflow && (u > ((sign < 0) ? (~(uint128_t)INT128_MIN + 1) : INT128_MAX))) overflow(aTHX_ pow_error); 0 0 0
if (may_die_on_overflow && (u > ((sign < 0) ? (~(uint128_t)INT128_MIN + 1) : INT128_MAX))) overflow(aTHX_ pow_error); 0 0 0
1057 651 ▶ r = ((sign > 0) ? u : -u);
Branch: 1 branch
BranchTrueFalseTotal
r = ((sign > 0) ? u : -u); 0 651 651
1058 }
1059 653 ▶ if (SvOK(rev))
Branch: 1 branch
BranchTrueFalseTotal
if (SvOK(rev)) 653 0 653
1060 653 RETVAL = newSVi128(aTHX_ r);
1061 else {
1062 0 RETVAL = self;
1063 0 SvREFCNT_inc(RETVAL);
1064 0 SvI128x(self) = r;
1065 }
1066 OUTPUT:
1067 RETVAL
1068  
1069 int
1070 mi128_spaceship(self, other, rev)
1071 SV *self
1072 SV *other
1073 SV *rev
1074 PREINIT:
1075 int128_t left;
1076 int128_t right;
1077 CODE:
1078 4 ▶ if (SvTRUE(rev)) {
Branch: 1 branch
BranchTrueFalseTotal
if (SvTRUE(rev)) { 0 4 4
1079 0 left = SvI128(aTHX_ other);
1080 0 right = SvI128x(self);
1081 }
1082 else {
1083 4 left = SvI128x(self);
1084 4 right = SvI128(aTHX_ other);
1085 }
1086 4 ▶ RETVAL = (left < right ? -1 : left > right ? 1 : 0);
Branch: 2 branches
BranchTrueFalseTotal
RETVAL = (left < right ? -1 : left > right ? 1 : 0); 3 1 4
RETVAL = (left < right ? -1 : left > right ? 1 : 0); 0 4 4
1087 OUTPUT:
1088 RETVAL
1089  
1090 SV *
1091 mi128_eqn(self, other, ...)
1092 SV *self
1093 SV *other
1094 CODE:
1095 78 RETVAL = ( SvI128x(self) == SvI128(aTHX_ other)
1096 ? &PL_sv_yes
1097 39 ▶ : &PL_sv_no );
Branch: 1 branch
BranchTrueFalseTotal
: &PL_sv_no ); 39 0 39
1098 OUTPUT:
1099 RETVAL
1100  
1101 SV *
1102 mi128_nen(self, other, ...)
1103 SV *self
1104 SV *other
1105 CODE:
1106 2 RETVAL = ( SvI128x(self) != SvI128(aTHX_ other)
1107 ? &PL_sv_yes
1108 1 ▶ : &PL_sv_no );
Branch: 1 branch
BranchTrueFalseTotal
: &PL_sv_no ); 1 0 1
1109 OUTPUT:
1110 RETVAL
1111  
1112 SV *
1113 mi128_gtn(self, other, rev)
1114 SV *self
1115 SV *other
1116 SV *rev
1117 CODE:
1118 2 ▶ if (SvTRUE(rev))
Branch: 1 branch
BranchTrueFalseTotal
if (SvTRUE(rev)) 0 2 2
1119 0 ▶ RETVAL = SvI128x(self) < SvI128(aTHX_ other) ? &PL_sv_yes : &PL_sv_no;
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = SvI128x(self) < SvI128(aTHX_ other) ? &PL_sv_yes : &PL_sv_no; 0 0 0
1120 else
1121 2 ▶ RETVAL = SvI128x(self) > SvI128(aTHX_ other) ? &PL_sv_yes : &PL_sv_no;
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = SvI128x(self) > SvI128(aTHX_ other) ? &PL_sv_yes : &PL_sv_no; 1 1 2
1122 OUTPUT:
1123 RETVAL
1124  
1125 SV *
1126 mi128_ltn(self, other, rev)
1127 SV *self
1128 SV *other
1129 SV *rev
1130 CODE:
1131 2 ▶ if (SvTRUE(rev))
Branch: 1 branch
BranchTrueFalseTotal
if (SvTRUE(rev)) 0 2 2
1132 0 ▶ RETVAL = SvI128x(self) > SvI128(aTHX_ other) ? &PL_sv_yes : &PL_sv_no;
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = SvI128x(self) > SvI128(aTHX_ other) ? &PL_sv_yes : &PL_sv_no; 0 0 0
1133 else
1134 2 ▶ RETVAL = SvI128x(self) < SvI128(aTHX_ other) ? &PL_sv_yes : &PL_sv_no;
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = SvI128x(self) < SvI128(aTHX_ other) ? &PL_sv_yes : &PL_sv_no; 1 1 2
1135 OUTPUT:
1136 RETVAL
1137  
1138 SV *
1139 mi128_gen(self, other, rev)
1140 SV *self
1141 SV *other
1142 SV *rev
1143 CODE:
1144 3 ▶ if (SvTRUE(rev))
Branch: 1 branch
BranchTrueFalseTotal
if (SvTRUE(rev)) 0 3 3
1145 0 ▶ RETVAL = SvI128x(self) <= SvI128(aTHX_ other) ? &PL_sv_yes : &PL_sv_no;
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = SvI128x(self) <= SvI128(aTHX_ other) ? &PL_sv_yes : &PL_sv_no; 0 0 0
1146 else
1147 3 ▶ RETVAL = SvI128x(self) >= SvI128(aTHX_ other) ? &PL_sv_yes : &PL_sv_no;
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = SvI128x(self) >= SvI128(aTHX_ other) ? &PL_sv_yes : &PL_sv_no; 2 1 3
1148 OUTPUT:
1149 RETVAL
1150  
1151 SV *
1152 mi128_len(self, other, rev)
1153 SV *self
1154 SV *other
1155 SV *rev
1156 CODE:
1157 3 ▶ if (SvTRUE(rev))
Branch: 1 branch
BranchTrueFalseTotal
if (SvTRUE(rev)) 0 3 3
1158 0 ▶ RETVAL = SvI128x(self) >= SvI128(aTHX_ other) ? &PL_sv_yes : &PL_sv_no;
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = SvI128x(self) >= SvI128(aTHX_ other) ? &PL_sv_yes : &PL_sv_no; 0 0 0
1159 else
1160 3 ▶ RETVAL = SvI128x(self) <= SvI128(aTHX_ other) ? &PL_sv_yes : &PL_sv_no;
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = SvI128x(self) <= SvI128(aTHX_ other) ? &PL_sv_yes : &PL_sv_no; 2 1 3
1161 OUTPUT:
1162 RETVAL
1163  
1164 SV *
1165 mi128_and(self, other, rev)
1166 SV *self
1167 SV *other
1168 SV *rev
1169 CODE:
1170 5 ▶ if (SvOK(rev))
Branch: 1 branch
BranchTrueFalseTotal
if (SvOK(rev)) 5 0 5
1171 5 RETVAL = newSVi128(aTHX_ SvI128x(self) & SvI128(aTHX_ other));
1172 else {
1173 0 RETVAL = self;
1174 0 SvREFCNT_inc(RETVAL);
1175 0 SvI128x(self) &= SvI128(aTHX_ other);
1176 }
1177 OUTPUT:
1178 RETVAL
1179  
1180 SV *
1181 mi128_or(self, other, rev)
1182 SV *self
1183 SV *other
1184 SV *rev
1185 CODE:
1186 2 ▶ if (SvOK(rev))
Branch: 1 branch
BranchTrueFalseTotal
if (SvOK(rev)) 1 1 2
1187 1 RETVAL = newSVi128(aTHX_ SvI128x(self) | SvI128(aTHX_ other));
1188 else {
1189 1 RETVAL = self;
1190 1 SvREFCNT_inc(RETVAL);
1191 1 SvI128x(self) |= SvI128(aTHX_ other);
1192 }
1193 OUTPUT:
1194 RETVAL
1195  
1196 SV *
1197 mi128_xor(self, other, rev)
1198 SV *self
1199 SV *other
1200 SV *rev
1201 CODE:
1202 0 ▶ if (SvOK(rev))
Branch: 1 branch
BranchTrueFalseTotal
if (SvOK(rev)) 0 0 0
1203 0 RETVAL = newSVi128(aTHX_ SvI128x(self) ^ SvI128(aTHX_ other));
1204 else {
1205 0 RETVAL = self;
1206 0 SvREFCNT_inc(RETVAL);
1207 0 SvI128x(self) ^= SvI128(aTHX_ other);
1208 }
1209 OUTPUT:
1210 RETVAL
1211  
1212 SV *
1213 mi128_not(self, ...)
1214 SV *self
1215 CODE:
1216 0 ▶ RETVAL = SvI128x(self) ? &PL_sv_no : &PL_sv_yes;
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = SvI128x(self) ? &PL_sv_no : &PL_sv_yes; 0 0 0
1217 OUTPUT:
1218 RETVAL
1219  
1220 SV *
1221 mi128_bnot(self, ...)
1222 SV *self
1223 CODE:
1224 0 RETVAL = newSVi128(aTHX_ ~SvI128x(self));
1225 OUTPUT:
1226 RETVAL
1227  
1228 SV *
1229 mi128_neg(self, ...)
1230 SV *self
1231 CODE:
1232 0 RETVAL = newSVi128(aTHX_ -SvI128x(self));
1233 OUTPUT:
1234 RETVAL
1235  
1236 SV *
1237 mi128_bool(self, ...)
1238 SV *self
1239 CODE:
1240 0 ▶ RETVAL = SvI128x(self) ? &PL_sv_yes : &PL_sv_no;
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = SvI128x(self) ? &PL_sv_yes : &PL_sv_no; 0 0 0
1241 OUTPUT:
1242 RETVAL
1243  
1244 SV *
1245 mi128_number(self, ...)
1246 SV *self
1247 CODE:
1248 1 RETVAL = si128_to_number(aTHX_ self);
1249 OUTPUT:
1250 RETVAL
1251  
1252 SV *
1253 mi128_clone(self, ...)
1254 SV *self
1255 CODE:
1256 1 RETVAL = newSVi128(aTHX_ SvI128x(self));
1257 OUTPUT:
1258 RETVAL
1259  
1260 SV *
1261 mi128_string(self, ...)
1262 SV *self
1263 CODE:
1264 2063 RETVAL = newSV(I128STRLEN);
1265 2063 SvPOK_on(RETVAL);
1266 2063 SvCUR_set(RETVAL, i128_to_string(SvI128x(self), SvPVX(RETVAL)));
1267 OUTPUT:
1268 RETVAL
1269  
1270  
1271 MODULE = Math::Int128 PACKAGE = Math::UInt128 PREFIX=mu128
1272 PROTOTYPES: DISABLE
1273  
1274 SV *
1275 mu128_inc(self, ...)
1276 SV *self
1277 CODE:
1278 0 ▶ if (may_die_on_overflow && (SvU128x(self) == UINT128_MAX)) overflow(aTHX_ inc_error);
Branch: 2 branches
BranchTrueFalseTotal
if (may_die_on_overflow && (SvU128x(self) == UINT128_MAX)) overflow(aTHX_ inc_error); 0 0 0
if (may_die_on_overflow && (SvU128x(self) == UINT128_MAX)) overflow(aTHX_ inc_error); 0 0 0
1279 0 SvU128x(self)++;
1280 0 RETVAL = SvREFCNT_inc(self);
1281 OUTPUT:
1282 RETVAL
1283  
1284 SV *
1285 mu128_dec(self, ...)
1286 SV *self
1287 CODE:
1288 0 ▶ if (may_die_on_overflow && (SvU128x(self) == 0)) overflow(aTHX_ dec_error);
Branch: 2 branches
BranchTrueFalseTotal
if (may_die_on_overflow && (SvU128x(self) == 0)) overflow(aTHX_ dec_error); 0 0 0
if (may_die_on_overflow && (SvU128x(self) == 0)) overflow(aTHX_ dec_error); 0 0 0
1289 0 SvU128x(self)--;
1290 0 RETVAL = SvREFCNT_inc(self);
1291 OUTPUT:
1292 RETVAL
1293  
1294 SV *
1295 mu128_add(self, other, rev)
1296 SV *self
1297 SV *other
1298 SV *rev
1299 PREINIT:
1300 10 uint128_t a = SvU128x(self);
1301 10 uint128_t b = SvU128(aTHX_ other);
1302 CODE:
1303 10 ▶ if (may_die_on_overflow && (UINT128_MAX - a < b)) overflow(aTHX_ add_error);
Branch: 2 branches
BranchTrueFalseTotal
if (may_die_on_overflow && (UINT128_MAX - a < b)) overflow(aTHX_ add_error); 0 10 10
if (may_die_on_overflow && (UINT128_MAX - a < b)) overflow(aTHX_ add_error); 0 0 0
1304 10 ▶ if (SvOK(rev))
Branch: 1 branch
BranchTrueFalseTotal
if (SvOK(rev)) 9 1 10
1305 9 RETVAL = newSVu128(aTHX_ a + b);
1306 else {
1307 1 RETVAL = self;
1308 1 SvREFCNT_inc(RETVAL);
1309 1 SvU128x(self) = a + b;
1310 }
1311 OUTPUT:
1312 RETVAL
1313  
1314 SV *
1315 mu128_sub(self, other, rev)
1316 SV *self
1317 SV *other
1318 SV *rev
1319 PREINIT:
1320 uint128_t a, b;
1321 CODE:
1322 8 ▶ if (SvTRUE(rev)) {
Branch: 1 branch
BranchTrueFalseTotal
if (SvTRUE(rev)) { 0 8 8
1323 0 a = SvU128(aTHX_ other);
1324 0 b = SvU128x(self);
1325 }
1326 else {
1327 8 a = SvU128x(self);
1328 8 b = SvU128(aTHX_ other);
1329 }
1330 8 ▶ if (may_die_on_overflow && (b > a)) overflow(aTHX_ sub_error);
Branch: 2 branches
BranchTrueFalseTotal
if (may_die_on_overflow && (b > a)) overflow(aTHX_ sub_error); 0 8 8
if (may_die_on_overflow && (b > a)) overflow(aTHX_ sub_error); 0 0 0
1331 8 ▶ if (SvOK(rev))
Branch: 1 branch
BranchTrueFalseTotal
if (SvOK(rev)) 7 1 8
1332 7 RETVAL = newSVu128(aTHX_ a - b);
1333 else {
1334 1 RETVAL = SvREFCNT_inc(self);
1335 1 SvU128x(self) = a - b;
1336 }
1337 OUTPUT:
1338 RETVAL
1339  
1340 SV *
1341 mu128_mul(self, other, rev)
1342 SV *self
1343 SV *other
1344 SV *rev
1345 PREINIT:
1346 4111 uint128_t a = SvU128x(self);
1347 4111 uint128_t b = SvU128(aTHX_ other);
1348 CODE:
1349 4111 ▶ if (may_die_on_overflow)
Branch: 1 branch
BranchTrueFalseTotal
if (may_die_on_overflow) 0 4111 4111
1350 0 mul_check_overflow(aTHX_ a, b, mul_error);
1351 4111 ▶ if (SvOK(rev))
Branch: 1 branch
BranchTrueFalseTotal
if (SvOK(rev)) 10 4101 4111
1352 10 RETVAL = newSVu128(aTHX_ a * b);
1353 else {
1354 4101 RETVAL = SvREFCNT_inc(self);
1355 4101 SvU128x(self) = a * b;
1356 }
1357 OUTPUT:
1358 RETVAL
1359  
1360 SV *
1361 mu128_div(self, other, rev)
1362 SV *self
1363 SV *other
1364 SV *rev
1365 PREINIT:
1366 uint128_t up;
1367 uint128_t down;
1368 CODE:
1369 257 ▶ if (SvOK(rev)) {
Branch: 1 branch
BranchTrueFalseTotal
if (SvOK(rev)) { 257 0 257
1370 257 ▶ if (SvTRUE(rev)) {
Branch: 1 branch
BranchTrueFalseTotal
if (SvTRUE(rev)) { 0 257 257
1371 0 up = SvU128(aTHX_ other);
1372 0 down = SvU128x(self);
1373 }
1374 else {
1375 257 up = SvU128x(self);
1376 257 down = SvU128(aTHX_ other);
1377 }
1378 257 ▶ if (!down)
Branch: 1 branch
BranchTrueFalseTotal
if (!down) 0 257 257
1379 0 croak_string(aTHX_ div_by_0_error);
1380 257 RETVAL = newSVu128(aTHX_ up/down);
1381 }
1382 else {
1383 0 down = SvU128(aTHX_ other);
1384 0 ▶ if (!down)
Branch: 1 branch
BranchTrueFalseTotal
if (!down) 0 0 0
1385 0 croak_string(aTHX_ div_by_0_error);
1386 0 RETVAL = self;
1387 0 SvREFCNT_inc(RETVAL);
1388 0 SvU128x(self) /= down;
1389 }
1390 OUTPUT:
1391 RETVAL
1392  
1393 SV *
1394 mu128_remainder(self, other, rev)
1395 SV *self
1396 SV *other
1397 SV *rev
1398 PREINIT:
1399 uint128_t up;
1400 uint128_t down;
1401 CODE:
1402 3 ▶ if (SvOK(rev)) {
Branch: 1 branch
BranchTrueFalseTotal
if (SvOK(rev)) { 2 1 3
1403 2 ▶ if (SvTRUE(rev)) {
Branch: 1 branch
BranchTrueFalseTotal
if (SvTRUE(rev)) { 0 2 2
1404 0 up = SvU128(aTHX_ other);
1405 0 down = SvU128x(self);
1406 }
1407 else {
1408 2 up = SvU128x(self);
1409 2 down = SvU128(aTHX_ other);
1410 }
1411 2 ▶ if (!down)
Branch: 1 branch
BranchTrueFalseTotal
if (!down) 0 2 2
1412 0 croak_string(aTHX_ div_by_0_error);
1413 2 RETVAL = newSVu128(aTHX_ up % down);
1414 }
1415 else {
1416 1 down = SvU128(aTHX_ other);
1417 1 ▶ if (!down)
Branch: 1 branch
BranchTrueFalseTotal
if (!down) 0 1 1
1418 0 croak_string(aTHX_ div_by_0_error);
1419 1 RETVAL = self;
1420 1 SvREFCNT_inc(RETVAL);
1421 1 SvU128x(self) %= down;
1422 }
1423 OUTPUT:
1424 RETVAL
1425  
1426 SV *mu128_left(self, other, rev)
1427 SV *self
1428 SV *other
1429 SV *rev
1430 CODE:
1431 452 ▶ if (SvOK(rev)) {
Branch: 1 branch
BranchTrueFalseTotal
if (SvOK(rev)) { 323 129 452
1432 uint128_t a, b;
1433 323 ▶ if (SvTRUE(rev)) {
Branch: 1 branch
BranchTrueFalseTotal
if (SvTRUE(rev)) { 0 323 323
1434 0 a = SvU128(aTHX_ other);
1435 0 b = SvU128x(self);
1436 }
1437 else {
1438 323 b = SvU128(aTHX_ other);
1439 323 a = SvU128x(self);
1440 }
1441 323 ▶ RETVAL = newSVu128(aTHX_ (b > 128 ? 0 : a << b));
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = newSVu128(aTHX_ (b > 128 ? 0 : a << b)); 323 0 323
1442 }
1443 else {
1444 129 uint128_t b = SvU128(aTHX_ other);
1445 129 RETVAL = SvREFCNT_inc(self);
1446 129 ▶ SvU128x(self) = (b > 127 ? 0 : SvU128x(self) << b);
Branch: 1 branch
BranchTrueFalseTotal
SvU128x(self) = (b > 127 ? 0 : SvU128x(self) << b); 129 0 129
1447 }
1448 OUTPUT:
1449 RETVAL
1450  
1451 SV *mu128_right(self, other, rev)
1452 SV *self
1453 SV *other
1454 SV *rev
1455 CODE:
1456 255 ▶ if (SvOK(rev)) {
Branch: 1 branch
BranchTrueFalseTotal
if (SvOK(rev)) { 128 127 255
1457 uint128_t a, b;
1458 128 ▶ if (SvTRUE(rev)) {
Branch: 1 branch
BranchTrueFalseTotal
if (SvTRUE(rev)) { 0 128 128
1459 0 a = SvU128(aTHX_ other);
1460 0 b = SvU128x(self);
1461 }
1462 else {
1463 128 b = SvU128(aTHX_ other);
1464 128 a = SvU128x(self);
1465 }
1466 128 ▶ RETVAL = newSVu128(aTHX_ (b > 127 ? 0 : a >> b));
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = newSVu128(aTHX_ (b > 127 ? 0 : a >> b)); 128 0 128
1467 }
1468 else {
1469 127 uint128_t b = SvU128(aTHX_ other);
1470 127 RETVAL = SvREFCNT_inc(self);
1471 127 ▶ SvU128x(self) = (b > 127 ? 0 : SvU128x(self) >> b);
Branch: 1 branch
BranchTrueFalseTotal
SvU128x(self) = (b > 127 ? 0 : SvU128x(self) >> b); 127 0 127
1472 }
1473 OUTPUT:
1474 RETVAL
1475  
1476 SV *mu128_pow(self, other, rev = &PL_sv_no)
1477 SV *self
1478 SV *other
1479 SV *rev
1480 PREINIT:
1481 uint128_t r;
1482 int128_t a, b;
1483 CODE:
1484 782 ▶ if (SvTRUE(rev)) {
Branch: 1 branch
BranchTrueFalseTotal
if (SvTRUE(rev)) { 0 782 782
1485 0 a = SvU128(aTHX_ other);
1486 0 b = SvU128x(self);
1487 }
1488 else {
1489 782 a = SvU128x(self);
1490 782 b = SvU128(aTHX_ other);
1491 }
1492 782 r = powU128(aTHX_ a, b);
1493 782 ▶ if (SvOK(rev))
Branch: 1 branch
BranchTrueFalseTotal
if (SvOK(rev)) 782 0 782
1494 782 RETVAL = newSVu128(aTHX_ r);
1495 else {
1496 0 RETVAL = self;
1497 0 SvREFCNT_inc(RETVAL);
1498 0 SvU128x(self) = r;
1499 }
1500 OUTPUT:
1501 RETVAL
1502  
1503 int
1504 mu128_spaceship(self, other, rev)
1505 SV *self
1506 SV *other
1507 SV *rev
1508 PREINIT:
1509 uint128_t left;
1510 uint128_t right;
1511 CODE:
1512 4 ▶ if (SvTRUE(rev)) {
Branch: 1 branch
BranchTrueFalseTotal
if (SvTRUE(rev)) { 0 4 4
1513 0 left = SvU128(aTHX_ other);
1514 0 right = SvU128x(self);
1515 }
1516 else {
1517 4 left = SvU128x(self);
1518 4 right = SvU128(aTHX_ other);
1519 }
1520 4 ▶ RETVAL = (left < right ? -1 : left > right ? 1 : 0);
Branch: 2 branches
BranchTrueFalseTotal
RETVAL = (left < right ? -1 : left > right ? 1 : 0); 3 1 4
RETVAL = (left < right ? -1 : left > right ? 1 : 0); 0 4 4
1521 OUTPUT:
1522 RETVAL
1523  
1524 SV *
1525 mu128_eqn(self, other, ...)
1526 SV *self
1527 SV *other
1528 CODE:
1529 66 RETVAL = ( SvU128x(self) == SvU128(aTHX_ other)
1530 ? &PL_sv_yes
1531 33 ▶ : &PL_sv_no );
Branch: 1 branch
BranchTrueFalseTotal
: &PL_sv_no ); 33 0 33
1532 OUTPUT:
1533 RETVAL
1534  
1535 SV *
1536 mu128_nen(self, other, ...)
1537 SV *self
1538 SV *other
1539 CODE:
1540 0 RETVAL = ( SvU128x(self) != SvU128(aTHX_ other)
1541 ? &PL_sv_yes
1542 0 ▶ : &PL_sv_no );
Branch: 1 branch
BranchTrueFalseTotal
: &PL_sv_no ); 0 0 0
1543 OUTPUT:
1544 RETVAL
1545  
1546 SV *
1547 mu128_gtn(self, other, rev)
1548 SV *self
1549 SV *other
1550 SV *rev
1551 CODE:
1552 2 ▶ if (SvTRUE(rev))
Branch: 1 branch
BranchTrueFalseTotal
if (SvTRUE(rev)) 0 2 2
1553 0 ▶ RETVAL = SvU128x(self) < SvU128(aTHX_ other) ? &PL_sv_yes : &PL_sv_no;
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = SvU128x(self) < SvU128(aTHX_ other) ? &PL_sv_yes : &PL_sv_no; 0 0 0
1554 else
1555 2 ▶ RETVAL = SvU128x(self) > SvU128(aTHX_ other) ? &PL_sv_yes : &PL_sv_no;
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = SvU128x(self) > SvU128(aTHX_ other) ? &PL_sv_yes : &PL_sv_no; 1 1 2
1556 OUTPUT:
1557 RETVAL
1558  
1559 SV *
1560 mu128_ltn(self, other, rev)
1561 SV *self
1562 SV *other
1563 SV *rev
1564 CODE:
1565 2 ▶ if (SvTRUE(rev))
Branch: 1 branch
BranchTrueFalseTotal
if (SvTRUE(rev)) 0 2 2
1566 0 ▶ RETVAL = SvU128x(self) > SvU128(aTHX_ other) ? &PL_sv_yes : &PL_sv_no;
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = SvU128x(self) > SvU128(aTHX_ other) ? &PL_sv_yes : &PL_sv_no; 0 0 0
1567 else
1568 2 ▶ RETVAL = SvU128x(self) < SvU128(aTHX_ other) ? &PL_sv_yes : &PL_sv_no;
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = SvU128x(self) < SvU128(aTHX_ other) ? &PL_sv_yes : &PL_sv_no; 1 1 2
1569 OUTPUT:
1570 RETVAL
1571  
1572 SV *
1573 mu128_gen(self, other, rev)
1574 SV *self
1575 SV *other
1576 SV *rev
1577 CODE:
1578 3 ▶ if (SvTRUE(rev))
Branch: 1 branch
BranchTrueFalseTotal
if (SvTRUE(rev)) 0 3 3
1579 0 ▶ RETVAL = SvU128x(self) <= SvU128(aTHX_ other) ? &PL_sv_yes : &PL_sv_no;
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = SvU128x(self) <= SvU128(aTHX_ other) ? &PL_sv_yes : &PL_sv_no; 0 0 0
1580 else
1581 3 ▶ RETVAL = SvU128x(self) >= SvU128(aTHX_ other) ? &PL_sv_yes : &PL_sv_no;
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = SvU128x(self) >= SvU128(aTHX_ other) ? &PL_sv_yes : &PL_sv_no; 2 1 3
1582 OUTPUT:
1583 RETVAL
1584  
1585 SV *
1586 mu128_len(self, other, rev)
1587 SV *self
1588 SV *other
1589 SV *rev
1590 CODE:
1591 3 ▶ if (SvTRUE(rev))
Branch: 1 branch
BranchTrueFalseTotal
if (SvTRUE(rev)) 0 3 3
1592 0 ▶ RETVAL = SvU128x(self) >= SvU128(aTHX_ other) ? &PL_sv_yes : &PL_sv_no;
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = SvU128x(self) >= SvU128(aTHX_ other) ? &PL_sv_yes : &PL_sv_no; 0 0 0
1593 else
1594 3 ▶ RETVAL = SvU128x(self) <= SvU128(aTHX_ other) ? &PL_sv_yes : &PL_sv_no;
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = SvU128x(self) <= SvU128(aTHX_ other) ? &PL_sv_yes : &PL_sv_no; 2 1 3
1595 OUTPUT:
1596 RETVAL
1597  
1598 SV *
1599 mu128_and(self, other, rev)
1600 SV *self
1601 SV *other
1602 SV *rev
1603 CODE:
1604 2 ▶ if (SvOK(rev))
Branch: 1 branch
BranchTrueFalseTotal
if (SvOK(rev)) 2 0 2
1605 2 RETVAL = newSVu128(aTHX_ SvU128x(self) & SvU128(aTHX_ other));
1606 else {
1607 0 RETVAL = self;
1608 0 SvREFCNT_inc(RETVAL);
1609 0 SvU128x(self) &= SvU128(aTHX_ other);
1610 }
1611 OUTPUT:
1612 RETVAL
1613  
1614 SV *
1615 mu128_or(self, other, rev)
1616 SV *self
1617 SV *other
1618 SV *rev
1619 CODE:
1620 1 ▶ if (SvOK(rev))
Branch: 1 branch
BranchTrueFalseTotal
if (SvOK(rev)) 0 1 1
1621 0 RETVAL = newSVu128(aTHX_ SvU128x(self) | SvU128(aTHX_ other));
1622 else {
1623 1 RETVAL = self;
1624 1 SvREFCNT_inc(RETVAL);
1625 1 SvU128x(self) |= SvU128(aTHX_ other);
1626 }
1627 OUTPUT:
1628 RETVAL
1629  
1630 SV *
1631 mu128_xor(self, other, rev)
1632 SV *self
1633 SV *other
1634 SV *rev
1635 CODE:
1636 0 ▶ if (SvOK(rev))
Branch: 1 branch
BranchTrueFalseTotal
if (SvOK(rev)) 0 0 0
1637 0 RETVAL = newSVu128(aTHX_ SvU128x(self) ^ SvU128(aTHX_ other));
1638 else {
1639 0 RETVAL = self;
1640 0 SvREFCNT_inc(RETVAL);
1641 0 SvU128x(self) ^= SvU128(aTHX_ other);
1642 }
1643 OUTPUT:
1644 RETVAL
1645  
1646 SV *
1647 mu128_not(self, ...)
1648 SV *self
1649 CODE:
1650 0 ▶ RETVAL = SvU128x(self) ? &PL_sv_no : &PL_sv_yes;
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = SvU128x(self) ? &PL_sv_no : &PL_sv_yes; 0 0 0
1651 OUTPUT:
1652 RETVAL
1653  
1654 SV *
1655 mu128_bnot(self, ...)
1656 SV *self
1657 CODE:
1658 0 RETVAL = newSVu128(aTHX_ ~SvU128x(self));
1659 OUTPUT:
1660 RETVAL
1661  
1662 SV *
1663 mu128_neg(self, ...)
1664 SV *self
1665 CODE:
1666 0 RETVAL = newSVu128(aTHX_ -SvU128x(self));
1667 OUTPUT:
1668 RETVAL
1669  
1670 SV *
1671 mu128_bool(self, ...)
1672 SV *self
1673 CODE:
1674 0 ▶ RETVAL = SvU128x(self) ? &PL_sv_yes : &PL_sv_no;
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = SvU128x(self) ? &PL_sv_yes : &PL_sv_no; 0 0 0
1675 OUTPUT:
1676 RETVAL
1677  
1678 SV *
1679 mu128_number(self, ...)
1680 SV *self
1681 CODE:
1682 1 RETVAL = su128_to_number(aTHX_ self);
1683 OUTPUT:
1684 RETVAL
1685  
1686 SV *
1687 mu128_clone(self, ...)
1688 SV *self
1689 CODE:
1690 1 RETVAL = newSVu128(aTHX_ SvU128x(self));
1691 OUTPUT:
1692 RETVAL
1693  
1694 SV *
1695 mu128_string(self, ...)
1696 SV *self
1697 CODE:
1698 2325 RETVAL = newSV(I128STRLEN);
1699 2325 SvPOK_on(RETVAL);
1700 2325 SvCUR_set(RETVAL, u128_to_string(SvU128x(self), SvPVX(RETVAL)));
1701 OUTPUT:
1702 RETVAL
1703  
1704  
1705 MODULE = Math::Int128 PACKAGE = Math::Int128 PREFIX=mi128_
1706 PROTOTYPES: DISABLE
1707  
1708 void
1709 mi128_int128_set(self, a=NULL)
1710 SV *self
1711 SV *a
1712 CODE:
1713 0 ▶ SvI128x(self) = (a ? SvI128(aTHX_ a) : 0);
Branch: 1 branch
BranchTrueFalseTotal
SvI128x(self) = (a ? SvI128(aTHX_ a) : 0); 0 0 0
1714  
1715 void
1716 mi128_int128_inc(self, a)
1717 SV *self
1718 int128_t a
1719 CODE:
1720 0 ▶ if ( may_die_on_overflow && (a == INT128_MAX)) overflow(aTHX_ inc_error);
Branch: 2 branches
BranchTrueFalseTotal
if ( may_die_on_overflow && (a == INT128_MAX)) overflow(aTHX_ inc_error); 0 0 0
if ( may_die_on_overflow && (a == INT128_MAX)) overflow(aTHX_ inc_error); 0 0 0
1721 0 SvI128x(self) = a + 1;
1722  
1723 void
1724 mi128_int128_dec(self, a)
1725 SV *self
1726 int128_t a
1727 CODE:
1728 0 ▶ if ( may_die_on_overflow && (a == INT128_MIN)) overflow(aTHX_ dec_error);
Branch: 2 branches
BranchTrueFalseTotal
if ( may_die_on_overflow && (a == INT128_MIN)) overflow(aTHX_ dec_error); 0 0 0
if ( may_die_on_overflow && (a == INT128_MIN)) overflow(aTHX_ dec_error); 0 0 0
1729 0 SvI128x(self) = a - 1;
1730  
1731 void
1732 mi128_int128_add(self, a, b)
1733 SV *self
1734 int128_t a
1735 int128_t b
1736 CODE:
1737 0 ▶ if ( may_die_on_overflow &&
Branch: 3 branches
BranchTrueFalseTotal
if ( may_die_on_overflow && 0 0 0
if ( may_die_on_overflow && 0 0 0
if ( may_die_on_overflow && 0 0 0
1738 ( a > 0
1739 0 ▶ ? ( (b > 0) && (INT128_MAX - a < b) )
Branch: 2 branches
BranchTrueFalseTotal
? ( (b > 0) && (INT128_MAX - a < b) ) 0 0 0
? ( (b > 0) && (INT128_MAX - a < b) ) 0 0 0
1740 0 ▶ : ( (b < 0) && (INT128_MIN - a > b) ) ) ) overflow(aTHX_ add_error);
Branch: 2 branches
BranchTrueFalseTotal
: ( (b < 0) && (INT128_MIN - a > b) ) ) ) overflow(aTHX_ add_error); 0 0 0
: ( (b < 0) && (INT128_MIN - a > b) ) ) ) overflow(aTHX_ add_error); 0 0 0
1741 0 SvI128x(self) = a + b;
1742  
1743 void
1744 mi128_int128_sub(self, a, b)
1745 SV *self
1746 int128_t a
1747 int128_t b
1748 CODE:
1749 0 ▶ if ( may_die_on_overflow &&
Branch: 3 branches
BranchTrueFalseTotal
if ( may_die_on_overflow && 0 0 0
if ( may_die_on_overflow && 0 0 0
if ( may_die_on_overflow && 0 0 0
1750 ( a > 0
1751 0 ▶ ? ( (b < 0) && (a - INT128_MAX > b) )
Branch: 2 branches
BranchTrueFalseTotal
? ( (b < 0) && (a - INT128_MAX > b) ) 0 0 0
? ( (b < 0) && (a - INT128_MAX > b) ) 0 0 0
1752 0 ▶ : ( (b > 0) && (a - INT128_MIN < b) ) ) ) overflow(aTHX_ sub_error);
Branch: 2 branches
BranchTrueFalseTotal
: ( (b > 0) && (a - INT128_MIN < b) ) ) ) overflow(aTHX_ sub_error); 0 0 0
: ( (b > 0) && (a - INT128_MIN < b) ) ) ) overflow(aTHX_ sub_error); 0 0 0
1753 0 SvI128x(self) = a - b;
1754  
1755 void
1756 mi128_int128_mul(self, a1, b1)
1757 SV *self
1758 int128_t a1
1759 int128_t b1
1760 CODE:
1761 0 ▶ if (may_die_on_overflow) {
Branch: 1 branch
BranchTrueFalseTotal
if (may_die_on_overflow) { 0 0 0
1762 0 int neg = 0;
1763 uint128_t a, b;
1764 0 ▶ if (a1 < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (a1 < 0) { 0 0 0
1765 0 a = -a1;
1766 0 neg ^= 1;
1767 }
1768 0 else a = a1;
1769 0 ▶ if (b1 < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (b1 < 0) { 0 0 0
1770 0 b = -b1;
1771 0 neg ^= 1;
1772 }
1773 0 else b = b1;
1774 0 ▶ if (a < b) {
Branch: 1 branch
BranchTrueFalseTotal
if (a < b) { 0 0 0
1775 0 uint128_t tmp = a;
1776 0 a = b; b = tmp;
1777 }
1778 0 mul_check_overflow(aTHX_ a, b, mul_error);
1779 0 ▶ if (a * b > (neg ? (~(uint128_t)INT128_MIN + 1) : INT128_MAX)) overflow(aTHX_ mul_error);
Branch: 2 branches
BranchTrueFalseTotal
if (a * b > (neg ? (~(uint128_t)INT128_MIN + 1) : INT128_MAX)) overflow(aTHX_ mul_error); 0 0 0
if (a * b > (neg ? (~(uint128_t)INT128_MIN + 1) : INT128_MAX)) overflow(aTHX_ mul_error); 0 0 0
1780 }
1781 0 SvI128x(self) = a1 * b1;
1782  
1783 void
1784 mi128_int128_pow(self, a, b)
1785 SV *self
1786 int128_t a
1787 int128_t b
1788 PREINIT:
1789 int sign;
1790 uint128_t r;
1791 CODE:
1792 0 ▶ if (a < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (a < 0) { 0 0 0
1793 0 ▶ sign = ((b & 1) ? -1 : 1);
Branch: 1 branch
BranchTrueFalseTotal
sign = ((b & 1) ? -1 : 1); 0 0 0
1794 0 a = -a;
1795 }
1796 0 else sign = 1;
1797 0 ▶ if (b < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (b < 0) { 0 0 0
1798 0 ▶ if (a == 0) croak_string(aTHX_ div_by_0_error);
Branch: 1 branch
BranchTrueFalseTotal
if (a == 0) croak_string(aTHX_ div_by_0_error); 0 0 0
1799 0 ▶ else if (a == 1) r = sign;
Branch: 1 branch
BranchTrueFalseTotal
else if (a == 1) r = sign; 0 0 0
1800 0 else r = 0;
1801 }
1802 else {
1803 0 uint128_t u = powU128(aTHX_ a, b);
1804 0 ▶ if (may_die_on_overflow && (u > ((sign < 0) ? (~(uint128_t)INT128_MIN + 1) : INT128_MAX))) overflow(aTHX_ pow_error);
Branch: 3 branches
BranchTrueFalseTotal
if (may_die_on_overflow && (u > ((sign < 0) ? (~(uint128_t)INT128_MIN + 1) : INT128_MAX))) overflow(aTHX_ pow_error); 0 0 0
if (may_die_on_overflow && (u > ((sign < 0) ? (~(uint128_t)INT128_MIN + 1) : INT128_MAX))) overflow(aTHX_ pow_error); 0 0 0
if (may_die_on_overflow && (u > ((sign < 0) ? (~(uint128_t)INT128_MIN + 1) : INT128_MAX))) overflow(aTHX_ pow_error); 0 0 0
1805 0 ▶ r = ((sign > 0) ? u : -u);
Branch: 1 branch
BranchTrueFalseTotal
r = ((sign > 0) ? u : -u); 0 0 0
1806 }
1807 0 SvI128x(self) = r;
1808  
1809 void
1810 mi128_int128_div(self, a, b)
1811 SV *self
1812 int128_t a
1813 int128_t b
1814 CODE:
1815 0 ▶ if (!b) croak_string(aTHX_ div_by_0_error);
Branch: 1 branch
BranchTrueFalseTotal
if (!b) croak_string(aTHX_ div_by_0_error); 0 0 0
1816 0 SvI128x(self) = a / b;
1817  
1818 void
1819 mi128_int128_mod(self, a, b)
1820 SV *self
1821 int128_t a
1822 int128_t b
1823 CODE:
1824 0 ▶ if (!b) croak_string(aTHX_ div_by_0_error);
Branch: 1 branch
BranchTrueFalseTotal
if (!b) croak_string(aTHX_ div_by_0_error); 0 0 0
1825 0 SvI128x(self) = a % b;
1826  
1827 void
1828 mi128_int128_divmod(self, rem, a, b)
1829 SV *self
1830 SV *rem
1831 int128_t a
1832 int128_t b
1833 PREINIT:
1834 int128_t d;
1835 CODE:
1836 0 ▶ if (!b) croak_string(aTHX_ div_by_0_error);
Branch: 1 branch
BranchTrueFalseTotal
if (!b) croak_string(aTHX_ div_by_0_error); 0 0 0
1837 0 SvI128x(self) = d = a / b;
1838 0 SvI128x(rem) = a - b * d;
1839  
1840 void
1841 mi128_int128_not(self, a)
1842 SV *self
1843 int128_t a
1844 CODE:
1845 0 SvI128x(self) = ~a;
1846  
1847 void
1848 mi128_int128_neg(self, a)
1849 SV *self
1850 int128_t a
1851 CODE:
1852 0 SvI128x(self) = -a;
1853  
1854 void
1855 mi128_int128_and(self, a, b)
1856 SV *self
1857 int128_t a
1858 int128_t b
1859 CODE:
1860 0 SvI128x(self) = a & b;
1861  
1862 void
1863 mi128_int128_or(self, a, b)
1864 SV *self
1865 int128_t a
1866 int128_t b
1867 CODE:
1868 0 SvI128x(self) = a | b;
1869  
1870 void
1871 mi128_int128_xor(self, a, b)
1872 SV *self
1873 int128_t a
1874 int128_t b
1875 CODE:
1876 0 SvI128x(self) = a ^ b;
1877  
1878 void
1879 mi128_int128_left(self, a, b)
1880 SV *self
1881 int128_t a
1882 uint128_t b
1883 CODE:
1884 0 ▶ SvI128x(self) = (b > 127 ? 0 : a << b);
Branch: 1 branch
BranchTrueFalseTotal
SvI128x(self) = (b > 127 ? 0 : a << b); 0 0 0
1885  
1886 void
1887 mi128_int128_right(self, a, b)
1888 SV *self
1889 int128_t a
1890 uint128_t b
1891 CODE:
1892 0 ▶ SvI128x(self) = (b > 127 ? (a < 0 ? - 1 : 0) : a >> b);
Branch: 2 branches
BranchTrueFalseTotal
SvI128x(self) = (b > 127 ? (a < 0 ? - 1 : 0) : a >> b); 0 0 0
SvI128x(self) = (b > 127 ? (a < 0 ? - 1 : 0) : a >> b); 0 0 0
1893
1894 void
1895 mi128_int128_average(self, a, b)
1896 SV *self
1897 int128_t a
1898 int128_t b
1899 CODE:
1900 0 SvI128x(self) = (a & b) + ((a ^ b) / 2);
1901  
1902 void
1903 mi128_int128_min(self, a, b)
1904 SV *self
1905 int128_t a
1906 int128_t b
1907 CODE:
1908 0 SvI128x(self) = (a > b ? b : a);
1909  
1910 void
1911 mi128_int128_max(self, a, b)
1912 SV *self
1913 int128_t a
1914 int128_t b
1915 CODE:
1916 0 SvI128x(self) = (a > b ? a : b);
1917  
1918  
1919 MODULE = Math::Int128 PACKAGE = Math::Int128 PREFIX=mu128_
1920 PROTOTYPES: DISABLE
1921  
1922 void
1923 mu128_uint128_set(self, a=0)
1924 SV *self
1925 uint128_t a;
1926 CODE:
1927 0 SvU128x(self) = a;
1928  
1929 void
1930 mu128_uint128_inc(self, a)
1931 SV *self
1932 uint128_t a
1933 CODE:
1934 0 ▶ if ( may_die_on_overflow && (a == INT128_MAX)) overflow(aTHX_ inc_error);
Branch: 2 branches
BranchTrueFalseTotal
if ( may_die_on_overflow && (a == INT128_MAX)) overflow(aTHX_ inc_error); 0 0 0
if ( may_die_on_overflow && (a == INT128_MAX)) overflow(aTHX_ inc_error); 0 0 0
1935 0 SvU128x(self) = a + 1;
1936  
1937 void
1938 mu128_uint128_dec(self, a)
1939 SV *self
1940 uint128_t a
1941 CODE:
1942 0 ▶ if ( may_die_on_overflow && (a == 0)) overflow(aTHX_ dec_error);
Branch: 2 branches
BranchTrueFalseTotal
if ( may_die_on_overflow && (a == 0)) overflow(aTHX_ dec_error); 0 0 0
if ( may_die_on_overflow && (a == 0)) overflow(aTHX_ dec_error); 0 0 0
1943 0 SvU128x(self) = a - 1;
1944  
1945 void
1946 mu128_uint128_add(self, a, b)
1947 SV *self
1948 uint128_t a
1949 uint128_t b
1950 CODE:
1951 0 ▶ if (may_die_on_overflow && (UINT128_MAX - a < b)) overflow(aTHX_ add_error);
Branch: 2 branches
BranchTrueFalseTotal
if (may_die_on_overflow && (UINT128_MAX - a < b)) overflow(aTHX_ add_error); 0 0 0
if (may_die_on_overflow && (UINT128_MAX - a < b)) overflow(aTHX_ add_error); 0 0 0
1952 0 SvU128x(self) = a + b;
1953  
1954 void
1955 mu128_uint128_sub(self, a, b)
1956 SV *self
1957 uint128_t a
1958 uint128_t b
1959 CODE:
1960 0 ▶ if (may_die_on_overflow && (b > a)) overflow(aTHX_ sub_error);
Branch: 2 branches
BranchTrueFalseTotal
if (may_die_on_overflow && (b > a)) overflow(aTHX_ sub_error); 0 0 0
if (may_die_on_overflow && (b > a)) overflow(aTHX_ sub_error); 0 0 0
1961 0 SvU128x(self) = a - b;
1962  
1963 void
1964 mu128_uint128_mul(self, a, b)
1965 SV *self
1966 uint128_t a
1967 uint128_t b
1968 CODE:
1969 0 ▶ if (may_die_on_overflow)
Branch: 1 branch
BranchTrueFalseTotal
if (may_die_on_overflow) 0 0 0
1970 0 mul_check_overflow(aTHX_ a, b, mul_error);
1971 0 SvU128x(self) = a * b;
1972  
1973 void
1974 mu128_uint128_pow(self, a, b)
1975 SV *self
1976 uint128_t a
1977 uint128_t b
1978 CODE:
1979 0 SvU128x(self) = powU128(aTHX_ a, b);
1980  
1981 void
1982 mu128_uint128_div(self, a, b)
1983 SV *self
1984 uint128_t a
1985 uint128_t b
1986 CODE:
1987 0 ▶ if (!b) croak_string(aTHX_ div_by_0_error);
Branch: 1 branch
BranchTrueFalseTotal
if (!b) croak_string(aTHX_ div_by_0_error); 0 0 0
1988 0 SvU128x(self) = a / b;
1989  
1990 void
1991 mu128_uint128_mod(self, a, b)
1992 SV *self
1993 uint128_t a
1994 uint128_t b
1995 CODE:
1996 0 ▶ if (!b) croak_string(aTHX_ div_by_0_error);
Branch: 1 branch
BranchTrueFalseTotal
if (!b) croak_string(aTHX_ div_by_0_error); 0 0 0
1997 0 SvU128x(self) = a % b;
1998  
1999 void
2000 mu128_uint128_divmod(self, rem, a, b)
2001 SV *self
2002 SV *rem
2003 uint128_t a
2004 uint128_t b
2005 PREINIT:
2006 uint128_t d;
2007 CODE:
2008 0 ▶ if (!b) croak_string(aTHX_ div_by_0_error);
Branch: 1 branch
BranchTrueFalseTotal
if (!b) croak_string(aTHX_ div_by_0_error); 0 0 0
2009 0 SvU128x(self) = d = a / b;
2010 0 SvU128x(rem) = a - b * d;
2011  
2012 void
2013 mu128_uint128_not(self, a)
2014 SV *self
2015 uint128_t a
2016 CODE:
2017 0 SvU128x(self) = ~a;
2018  
2019 void
2020 mu128_uint128_neg(self, a)
2021 SV *self
2022 uint128_t a
2023 CODE:
2024 0 SvU128x(self) = -a;
2025  
2026 void
2027 mu128_uint128_and(self, a, b)
2028 SV *self
2029 uint128_t a
2030 uint128_t b
2031 CODE:
2032 0 SvU128x(self) = a & b;
2033  
2034 void
2035 mu128_uint128_or(self, a, b)
2036 SV *self
2037 uint128_t a
2038 uint128_t b
2039 CODE:
2040 0 SvU128x(self) = a | b;
2041  
2042 void
2043 mu128_uint128_xor(self, a, b)
2044 SV *self
2045 uint128_t a
2046 uint128_t b
2047 CODE:
2048 0 SvU128x(self) = a ^ b;
2049  
2050 void
2051 mu128_uint128_left(self, a, b)
2052 SV *self
2053 uint128_t a
2054 uint128_t b
2055 CODE:
2056 0 ▶ SvU128x(self) = (b > 127 ? 0 : a << b);
Branch: 1 branch
BranchTrueFalseTotal
SvU128x(self) = (b > 127 ? 0 : a << b); 0 0 0
2057  
2058 void
2059 mu128_uint128_right(self, a, b)
2060 SV *self
2061 uint128_t a
2062 uint128_t b
2063 CODE:
2064 0 ▶ SvU128x(self) = (b > 127 ? 0 : a >> b);
Branch: 1 branch
BranchTrueFalseTotal
SvU128x(self) = (b > 127 ? 0 : a >> b); 0 0 0
2065  
2066 void
2067 mu128_uint128_average(self, a, b)
2068 SV *self
2069 uint128_t a
2070 uint128_t b
2071 CODE:
2072 0 SvU128x(self) = (a & b) + ((a ^ b) >> 1);
2073  
2074 void
2075 mi128_uint128_min(self, a, b)
2076 SV *self
2077 uint128_t a
2078 uint128_t b
2079 CODE:
2080 0 SvU128x(self) = (a > b ? b : a);
2081  
2082 void
2083 mi128_uint128_max(self, a, b)
2084 SV *self
2085 uint128_t a
2086 uint128_t b
2087 CODE:
2088 0 SvU128x(self) = (a > b ? a : b);
« perl_math_int64.c ↑ Summary strtoint128.h »