Int128.xs

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