BigInt/Lib.pm

filter: Statementstmt 22.8% 231 / 1012 Branchbran 19.7% 101 / 512 Conditioncond 21.4% 9 / 42 MC/DCmcdc 7.4% 2 / 27 Subroutinesub 20.1% 21 / 104 Podpod n/a totaltotal 21.4% 364 / 1697 CC 273 SCAR 41.4
CC
273
Cov
22%
_root
462.0
_log_int
306.0
_sqrt
110.0
CRAP 63.0
« BigInt.pm ↑ Summary
1 package Math::BigInt::Lib;
2  
3 50 ▶use 5.006001;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 50 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 50 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 50 our $VERSION = '2.005003';
8 50 $VERSION =~ tr/_//d;
9  
10 50 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 use overload
13  
14 # overload key: with_assign
15  
16 50 ▶ '+' => sub {
Subroutine BEGIN: called CC=1 SCAR=6.4
Subroutine __ANON__: not called CC=2 SCAR=17.9
17 0 my $class = ref $_[0];
18 0 my $x = $class -> _copy($_[0]);
19 0 ▶ my $y = ref($_[1]) ? $_[1] : $class -> _new($_[1]);
Branch: 1 branch
BranchTrueFalseTotal
ref $_[1] ? : 0 0 0
20 0 return $class -> _add($x, $y);
21 },
22  
23 0 ▶ '-' => sub {
Subroutine __ANON__: not called CC=4 SCAR=30.0
24 0 my $class = ref $_[0];
25 0 my ($x, $y);
26 0 ▶ if ($_[2]) { # if swapped
Branch: 1 branch
BranchTrueFalseTotal
if ($_[2]) { } 0 0 0
27 0 $y = $_[0];
28 0 ▶ $x = ref($_[1]) ? $_[1] : $class -> _new($_[1]);
Branch: 1 branch
BranchTrueFalseTotal
ref $_[1] ? : 0 0 0
29 } else {
30 0 $x = $class -> _copy($_[0]);
31 0 ▶ $y = ref($_[1]) ? $_[1] : $class -> _new($_[1]);
Branch: 1 branch
BranchTrueFalseTotal
ref $_[1] ? : 0 0 0
32 }
33 0 return $class -> _sub($x, $y);
34 },
35  
36 0 ▶ '*' => sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
37 0 my $class = ref $_[0];
38 0 my $x = $class -> _copy($_[0]);
39 0 ▶ my $y = ref($_[1]) ? $_[1] : $class -> _new($_[1]);
Branch: 1 branch
BranchTrueFalseTotal
ref $_[1] ? : 0 0 0
40 0 return $class -> _mul($x, $y);
41 },
42  
43 0 ▶ '/' => sub {
Subroutine __ANON__: not called CC=4 SCAR=30.0
44 0 my $class = ref $_[0];
45 0 my ($x, $y);
46 0 ▶ if ($_[2]) { # if swapped
Branch: 1 branch
BranchTrueFalseTotal
if ($_[2]) { } 0 0 0
47 0 $y = $_[0];
48 0 ▶ $x = ref($_[1]) ? $_[1] : $class -> _new($_[1]);
Branch: 1 branch
BranchTrueFalseTotal
ref $_[1] ? : 0 0 0
49 } else {
50 0 $x = $class -> _copy($_[0]);
51 0 ▶ $y = ref($_[1]) ? $_[1] : $class -> _new($_[1]);
Branch: 1 branch
BranchTrueFalseTotal
ref $_[1] ? : 0 0 0
52 }
53 0 return $class -> _div($x, $y);
54 },
55  
56 0 ▶ '%' => sub {
Subroutine __ANON__: not called CC=4 SCAR=30.0
57 0 my $class = ref $_[0];
58 0 my ($x, $y);
59 0 ▶ if ($_[2]) { # if swapped
Branch: 1 branch
BranchTrueFalseTotal
if ($_[2]) { } 0 0 0
60 0 $y = $_[0];
61 0 ▶ $x = ref($_[1]) ? $_[1] : $class -> _new($_[1]);
Branch: 1 branch
BranchTrueFalseTotal
ref $_[1] ? : 0 0 0
62 } else {
63 0 $x = $class -> _copy($_[0]);
64 0 ▶ $y = ref($_[1]) ? $_[1] : $class -> _new($_[1]);
Branch: 1 branch
BranchTrueFalseTotal
ref $_[1] ? : 0 0 0
65 }
66 0 return $class -> _mod($x, $y);
67 },
68  
69 0 ▶ '**' => sub {
Subroutine __ANON__: not called CC=4 SCAR=30.0
70 0 my $class = ref $_[0];
71 0 my ($x, $y);
72 0 ▶ if ($_[2]) { # if swapped
Branch: 1 branch
BranchTrueFalseTotal
if ($_[2]) { } 0 0 0
73 0 $y = $_[0];
74 0 ▶ $x = ref($_[1]) ? $_[1] : $class -> _new($_[1]);
Branch: 1 branch
BranchTrueFalseTotal
ref $_[1] ? : 0 0 0
75 } else {
76 0 $x = $class -> _copy($_[0]);
77 0 ▶ $y = ref($_[1]) ? $_[1] : $class -> _new($_[1]);
Branch: 1 branch
BranchTrueFalseTotal
ref $_[1] ? : 0 0 0
78 }
79 0 return $class -> _pow($x, $y);
80 },
81  
82 0 ▶ '<<' => sub {
Subroutine __ANON__: not called CC=4 SCAR=30.0
83 0 my $class = ref $_[0];
84 0 my ($x, $y);
85 0 ▶ if ($_[2]) { # if swapped
Branch: 1 branch
BranchTrueFalseTotal
if ($_[2]) { } 0 0 0
86 0 $y = $class -> _num($_[0]);
87 0 ▶ $x = ref($_[1]) ? $_[1] : $class -> _new($_[1]);
Branch: 1 branch
BranchTrueFalseTotal
ref $_[1] ? : 0 0 0
88 } else {
89 0 $x = $_[0];
90 0 ▶ $y = ref($_[1]) ? $class -> _num($_[1]) : $_[1];
Branch: 1 branch
BranchTrueFalseTotal
ref $_[1] ? : 0 0 0
91 }
92 0 return $class -> _lsft($x, $y);
93 },
94  
95 0 ▶ '>>' => sub {
Subroutine __ANON__: not called CC=4 SCAR=30.0
96 0 my $class = ref $_[0];
97 0 my ($x, $y);
98 0 ▶ if ($_[2]) { # if swapped
Branch: 1 branch
BranchTrueFalseTotal
if ($_[2]) { } 0 0 0
99 0 $y = $_[0];
100 0 ▶ $x = ref($_[1]) ? $_[1] : $class -> _new($_[1]);
Branch: 1 branch
BranchTrueFalseTotal
ref $_[1] ? : 0 0 0
101 } else {
102 0 $x = $class -> _copy($_[0]);
103 0 ▶ $y = ref($_[1]) ? $_[1] : $class -> _new($_[1]);
Branch: 1 branch
BranchTrueFalseTotal
ref $_[1] ? : 0 0 0
104 }
105 0 return $class -> _rsft($x, $y);
106 },
107  
108 # overload key: num_comparison
109  
110 0 ▶ '<' => sub {
Subroutine __ANON__: not called CC=4 SCAR=30.0
111 0 my $class = ref $_[0];
112 0 my ($x, $y);
113 0 ▶ if ($_[2]) { # if swapped
Branch: 1 branch
BranchTrueFalseTotal
if ($_[2]) { } 0 0 0
114 0 $y = $_[0];
115 0 ▶ $x = ref($_[1]) ? $_[1] : $class -> _new($_[1]);
Branch: 1 branch
BranchTrueFalseTotal
ref $_[1] ? : 0 0 0
116 } else {
117 0 $x = $class -> _copy($_[0]);
118 0 ▶ $y = ref($_[1]) ? $_[1] : $class -> _new($_[1]);
Branch: 1 branch
BranchTrueFalseTotal
ref $_[1] ? : 0 0 0
119 }
120 0 return $class -> _acmp($x, $y) < 0;
121 },
122  
123 0 ▶ '<=' => sub {
Subroutine __ANON__: not called CC=4 SCAR=30.0
124 0 my $class = ref $_[0];
125 0 my ($x, $y);
126 0 ▶ if ($_[2]) { # if swapped
Branch: 1 branch
BranchTrueFalseTotal
if ($_[2]) { } 0 0 0
127 0 $y = $_[0];
128 0 ▶ $x = ref($_[1]) ? $_[1] : $class -> _new($_[1]);
Branch: 1 branch
BranchTrueFalseTotal
ref $_[1] ? : 0 0 0
129 } else {
130 0 $x = $class -> _copy($_[0]);
131 0 ▶ $y = ref($_[1]) ? $_[1] : $class -> _new($_[1]);
Branch: 1 branch
BranchTrueFalseTotal
ref $_[1] ? : 0 0 0
132 }
133 0 return $class -> _acmp($x, $y) <= 0;
134 },
135  
136 0 ▶ '>' => sub {
Subroutine __ANON__: not called CC=4 SCAR=30.0
137 0 my $class = ref $_[0];
138 0 my ($x, $y);
139 0 ▶ if ($_[2]) { # if swapped
Branch: 1 branch
BranchTrueFalseTotal
if ($_[2]) { } 0 0 0
140 0 $y = $_[0];
141 0 ▶ $x = ref($_[1]) ? $_[1] : $class -> _new($_[1]);
Branch: 1 branch
BranchTrueFalseTotal
ref $_[1] ? : 0 0 0
142 } else {
143 0 $x = $class -> _copy($_[0]);
144 0 ▶ $y = ref($_[1]) ? $_[1] : $class -> _new($_[1]);
Branch: 1 branch
BranchTrueFalseTotal
ref $_[1] ? : 0 0 0
145 }
146 0 return $class -> _acmp($x, $y) > 0;
147 },
148  
149 0 ▶ '>=' => sub {
Subroutine __ANON__: not called CC=4 SCAR=30.0
150 0 my $class = ref $_[0];
151 0 my ($x, $y);
152 0 ▶ if ($_[2]) { # if swapped
Branch: 1 branch
BranchTrueFalseTotal
if ($_[2]) { } 0 0 0
153 0 $y = $_[0];
154 0 ▶ $x = ref($_[1]) ? $_[1] : $class -> _new($_[1]);
Branch: 1 branch
BranchTrueFalseTotal
ref $_[1] ? : 0 0 0
155 } else {
156 0 $x = $class -> _copy($_[0]);
157 0 ▶ $y = ref($_[1]) ? $_[1] : $class -> _new($_[1]);
Branch: 1 branch
BranchTrueFalseTotal
ref $_[1] ? : 0 0 0
158 }
159 0 return $class -> _acmp($x, $y) >= 0;
160 },
161  
162 12697 ▶ '==' => sub {
Subroutine __ANON__: called CC=2 SCAR=7.0
163 12697 my $class = ref $_[0];
164 12697 my $x = $class -> _copy($_[0]);
165 12697 ▶ my $y = ref($_[1]) ? $_[1] : $class -> _new($_[1]);
Branch: 1 branch
BranchTrueFalseTotal
ref $_[1] ? : 12697 0 12697
166 12697 return $class -> _acmp($x, $y) == 0;
167 },
168  
169 0 ▶ '!=' => sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
170 0 my $class = ref $_[0];
171 0 my $x = $class -> _copy($_[0]);
172 0 ▶ my $y = ref($_[1]) ? $_[1] : $class -> _new($_[1]);
Branch: 1 branch
BranchTrueFalseTotal
ref $_[1] ? : 0 0 0
173 0 return $class -> _acmp($x, $y) != 0;
174 },
175  
176 # overload key: 3way_comparison
177  
178 0 ▶ '<=>' => sub {
Subroutine __ANON__: not called CC=4 SCAR=30.0
179 0 my $class = ref $_[0];
180 0 my ($x, $y);
181 0 ▶ if ($_[2]) { # if swapped
Branch: 1 branch
BranchTrueFalseTotal
if ($_[2]) { } 0 0 0
182 0 $y = $_[0];
183 0 ▶ $x = ref($_[1]) ? $_[1] : $class -> _new($_[1]);
Branch: 1 branch
BranchTrueFalseTotal
ref $_[1] ? : 0 0 0
184 } else {
185 0 $x = $class -> _copy($_[0]);
186 0 ▶ $y = ref($_[1]) ? $_[1] : $class -> _new($_[1]);
Branch: 1 branch
BranchTrueFalseTotal
ref $_[1] ? : 0 0 0
187 }
188 0 return $class -> _acmp($x, $y);
189 },
190  
191 # overload key: binary
192  
193 0 ▶ '&' => sub {
Subroutine __ANON__: not called CC=4 SCAR=30.0
194 0 my $class = ref $_[0];
195 0 my ($x, $y);
196 0 ▶ if ($_[2]) { # if swapped
Branch: 1 branch
BranchTrueFalseTotal
if ($_[2]) { } 0 0 0
197 0 $y = $_[0];
198 0 ▶ $x = ref($_[1]) ? $_[1] : $class -> _new($_[1]);
Branch: 1 branch
BranchTrueFalseTotal
ref $_[1] ? : 0 0 0
199 } else {
200 0 $x = $class -> _copy($_[0]);
201 0 ▶ $y = ref($_[1]) ? $_[1] : $class -> _new($_[1]);
Branch: 1 branch
BranchTrueFalseTotal
ref $_[1] ? : 0 0 0
202 }
203 0 return $class -> _and($x, $y);
204 },
205  
206 0 ▶ '|' => sub {
Subroutine __ANON__: not called CC=4 SCAR=30.0
207 0 my $class = ref $_[0];
208 0 my ($x, $y);
209 0 ▶ if ($_[2]) { # if swapped
Branch: 1 branch
BranchTrueFalseTotal
if ($_[2]) { } 0 0 0
210 0 $y = $_[0];
211 0 ▶ $x = ref($_[1]) ? $_[1] : $class -> _new($_[1]);
Branch: 1 branch
BranchTrueFalseTotal
ref $_[1] ? : 0 0 0
212 } else {
213 0 $x = $class -> _copy($_[0]);
214 0 ▶ $y = ref($_[1]) ? $_[1] : $class -> _new($_[1]);
Branch: 1 branch
BranchTrueFalseTotal
ref $_[1] ? : 0 0 0
215 }
216 0 return $class -> _or($x, $y);
217 },
218  
219 0 ▶ '^' => sub {
Subroutine __ANON__: not called CC=4 SCAR=30.0
220 0 my $class = ref $_[0];
221 0 my ($x, $y);
222 0 ▶ if ($_[2]) { # if swapped
Branch: 1 branch
BranchTrueFalseTotal
if ($_[2]) { } 0 0 0
223 0 $y = $_[0];
224 0 ▶ $x = ref($_[1]) ? $_[1] : $class -> _new($_[1]);
Branch: 1 branch
BranchTrueFalseTotal
ref $_[1] ? : 0 0 0
225 } else {
226 0 $x = $class -> _copy($_[0]);
227 0 ▶ $y = ref($_[1]) ? $_[1] : $class -> _new($_[1]);
Branch: 1 branch
BranchTrueFalseTotal
ref $_[1] ? : 0 0 0
228 }
229 0 return $class -> _xor($x, $y);
230 },
231  
232 # overload key: func
233  
234 0 ▶ 'abs' => sub { $_[0] },
Subroutine __ANON__: not called CC=1 SCAR=6.9
235  
236 0 ▶ 'sqrt' => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
237 0 my $class = ref $_[0];
238 0 return $class -> _sqrt($class -> _copy($_[0]));
239 },
240  
241 0 ▶ 'int' => sub { $_[0] },
Subroutine __ANON__: not called CC=1 SCAR=6.9
242  
243 # overload key: conversion
244  
245 0 ▶ 'bool' => sub { ref($_[0]) -> _is_zero($_[0]) ? '' : 1; },
Subroutine __ANON__: not called CC=2 SCAR=17.9
Branch: 1 branch
BranchTrueFalseTotal
ref($_[0])->_is_zero($_[0]) ? : 0 0 0
246  
247 4 ▶ '""' => sub { ref($_[0]) -> _str($_[0]); },
Subroutine __ANON__: called CC=1 SCAR=0.0
248  
249 0 ▶ '0+' => sub { ref($_[0]) -> _num($_[0]); },
Subroutine __ANON__: not called CC=1 SCAR=6.9
250  
251 0 ▶ '=' => sub { ref($_[0]) -> _copy($_[0]); },
Subroutine __ANON__: not called CC=1 SCAR=6.9
252  
253 50 ;
254  
255 0 ▶sub _new {
Subroutine _new: not called CC=1 SCAR=6.9
256 0 croak "@{[(caller 0)[3]]} method not implemented";
257 }
258  
259 0 ▶sub _zero {
Subroutine _zero: not called CC=1 SCAR=6.9
260 0 my $class = shift;
261 0 return $class -> _new("0");
262 }
263  
264 0 ▶sub _one {
Subroutine _one: not called CC=1 SCAR=6.9
265 0 my $class = shift;
266 0 return $class -> _new("1");
267 }
268  
269 0 ▶sub _two {
Subroutine _two: not called CC=1 SCAR=6.9
270 0 my $class = shift;
271 0 return $class -> _new("2");
272  
273 }
274 0 ▶sub _ten {
Subroutine _ten: not called CC=1 SCAR=6.9
275 0 my $class = shift;
276 0 return $class -> _new("10");
277 }
278  
279 0 ▶sub _1ex {
Subroutine _1ex: not called CC=2 SCAR=17.9
280 0 my ($class, $exp) = @_;
281 0 ▶ $exp = $class -> _num($exp) if ref($exp);
Branch: 1 branch
BranchTrueFalseTotal
if ref $exp 0 0 0
282 0 return $class -> _new("1" . ("0" x $exp));
283 }
284  
285 0 ▶sub _copy {
Subroutine _copy: not called CC=1 SCAR=6.9
286 0 my ($class, $x) = @_;
287 0 return $class -> _new($class -> _str($x));
288 }
289  
290 # catch and throw away
291 50 ▶sub import { }
Subroutine import: called CC=1 SCAR=0.0
292  
293 ##############################################################################
294 # convert back to string and number
295  
296 0 ▶sub _str {
Subroutine _str: not called CC=1 SCAR=6.9
297 # Convert number from internal base 1eN format to string format. Internal
298 # format is always normalized, i.e., no leading zeros.
299 0 croak "@{[(caller 0)[3]]} method not implemented";
300 }
301  
302 0 ▶sub _num {
Subroutine _num: not called CC=1 SCAR=6.9
303 0 my ($class, $x) = @_;
304 0 0 + $class -> _str($x);
305 }
306  
307 ##############################################################################
308 # actual math code
309  
310 0 ▶sub _add {
Subroutine _add: not called CC=1 SCAR=6.9
311 0 croak "@{[(caller 0)[3]]} method not implemented";
312 }
313  
314 0 ▶sub _sub {
Subroutine _sub: not called CC=1 SCAR=6.9
315 0 croak "@{[(caller 0)[3]]} method not implemented";
316 }
317  
318 0 ▶sub _mul {
Subroutine _mul: not called CC=2 SCAR=17.9
319 0 my ($class, $x, $y) = @_;
320 0 my $sum = $class -> _zero();
321 0 my $i = $class -> _zero();
322 0 ▶ while ($class -> _acmp($i, $y) < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($class->_acmp($i, $y) < 0) 0 0 0
323 0 $sum = $class -> _add($sum, $x);
324 0 $i = $class -> _inc($i);
325 }
326 0 return $sum;
327 }
328  
329 0 ▶sub _div {
Subroutine _div: not called CC=4 SCAR=30.0
330 0 my ($class, $x, $y) = @_;
331  
332 0 ▶ croak "@{[(caller 0)[3]]} requires non-zero divisor"
Branch: 1 branch
BranchTrueFalseTotal
if $class->_is_zero($y) 0 0 0
333 if $class -> _is_zero($y);
334  
335 0 my $r = $class -> _copy($x);
336 0 my $q = $class -> _zero();
337 0 ▶ while ($class -> _acmp($r, $y) >= 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($class->_acmp($r, $y) >= 0) 0 0 0
338 0 $q = $class -> _inc($q);
339 0 $r = $class -> _sub($r, $y);
340 }
341  
342 0 ▶ return $q, $r if wantarray;
Branch: 1 branch
BranchTrueFalseTotal
if wantarray 0 0 0
343 0 return $q;
344 }
345  
346 0 ▶sub _inc {
Subroutine _inc: not called CC=1 SCAR=6.9
347 0 my ($class, $x) = @_;
348 0 $class -> _add($x, $class -> _one());
349 }
350  
351 0 ▶sub _dec {
Subroutine _dec: not called CC=1 SCAR=6.9
352 0 my ($class, $x) = @_;
353 0 $class -> _sub($x, $class -> _one());
354 }
355  
356 # Signed addition. If the flag is false, $xa might be modified, but not $ya. If
357 # the false is true, $ya might be modified, but not $xa.
358  
359 71798 ▶sub _sadd {
Subroutine _sadd: called CC=6 SCAR=17.9
360 71798 my $class = shift;
361 71798 my ($xa, $xs, $ya, $ys, $flag) = @_;
362 71798 my ($za, $zs);
363  
364 # If the signs are equal we can add them (-5 + -3 => -(5 + 3) => -8)
365  
366 71798 ▶ if ($xs eq $ys) {
Branch: 1 branch
BranchTrueFalseTotal
if ($xs eq $ys) 39548 32250 71798
367 39548 ▶ if ($flag) {
Branch: 1 branch
BranchTrueFalseTotal
if ($flag) { } 0 39548 39548
368 0 $za = $class -> _add($ya, $xa);
369 } else {
370 39548 $za = $class -> _add($xa, $ya);
371 }
372 39548 ▶ $zs = $class -> _is_zero($za) ? '+' : $xs;
Branch: 1 branch
BranchTrueFalseTotal
$class->_is_zero($za) ? : 2036 37512 39548
373 39548 return $za, $zs;
374 }
375  
376 32250 my $acmp = $class -> _acmp($xa, $ya); # abs(x) = abs(y)
377  
378 32250 ▶ if ($acmp == 0) { # x = -y or -x = y
Branch: 1 branch
BranchTrueFalseTotal
if ($acmp == 0) 7379 24871 32250
379 7379 $za = $class -> _zero();
380 7379 $zs = '+';
381 7379 return $za, $zs;
382 }
383  
384 24871 ▶ if ($acmp > 0) { # abs(x) > abs(y)
Branch: 1 branch
BranchTrueFalseTotal
if ($acmp > 0) { } 17495 7376 24871
385 17495 $za = $class -> _sub($xa, $ya, $flag);
386 17495 $zs = $xs;
387 } else { # abs(x) < abs(y)
388 7376 $za = $class -> _sub($ya, $xa, !$flag);
389 7376 $zs = $ys;
390 }
391 24871 return $za, $zs;
392 }
393  
394 # Signed subtraction. If the flag is false, $xa might be modified, but not $ya.
395 # If the false is true, $ya might be modified, but not $xa.
396  
397 38194 ▶sub _ssub {
Subroutine _ssub: called CC=2 SCAR=6.9
398 38194 my $class = shift;
399 38194 my ($xa, $xs, $ya, $ys, $flag) = @_;
400  
401 # Swap sign of second operand and let _sadd() do the job.
402 38194 ▶ $ys = $ys eq '+' ? '-' : '+';
Branch: 1 branch
BranchTrueFalseTotal
$ys eq '+' ? : 25684 12510 38194
403 38194 $class -> _sadd($xa, $xs, $ya, $ys, $flag);
404 }
405  
406 ##############################################################################
407 # testing
408  
409 0 ▶sub _acmp {
Subroutine _acmp: not called CC=2 SCAR=17.9
410 # Compare two (absolute) values. Return -1, 0, or 1.
411 0 my ($class, $x, $y) = @_;
412 0 my $xstr = $class -> _str($x);
413 0 my $ystr = $class -> _str($y);
414  
415 0 ▶ length($xstr) <=> length($ystr) || $xstr cmp $ystr;
Branch: 1 branch
BranchTrueFalseTotal
length $xstr <=> length $ystr or $xstr cmp $ystr 0 0 0
416 }
417  
418 513 ▶sub _scmp {
Subroutine _scmp: called CC=4 SCAR=13.9
419 # Compare two signed values. Return -1, 0, or 1.
420 513 my ($class, $xa, $xs, $ya, $ys) = @_;
421 513 ▶ if ($xs eq '+') {
Branch: 1 branch
BranchTrueFalseTotal
if ($xs eq '+') { } 430 83 513
422 430 ▶ if ($ys eq '+') {
Branch: 1 branch
BranchTrueFalseTotal
if ($ys eq '+') { } 410 20 430
423 410 return $class -> _acmp($xa, $ya);
424 } else {
425 20 return 1;
426 }
427 } else {
428 83 ▶ if ($ys eq '+') {
Branch: 1 branch
BranchTrueFalseTotal
if ($ys eq '+') { } 35 48 83
429 35 return -1;
430 } else {
431 48 return $class -> _acmp($ya, $xa);
432 }
433 }
434 }
435  
436 0 ▶sub _len {
Subroutine _len: not called CC=1 SCAR=6.9
437 0 my ($class, $x) = @_;
438 0 CORE::length($class -> _str($x));
439 }
440  
441 27 ▶sub _alen {
Subroutine _alen: called CC=1 SCAR=0.0
442 27 my ($class, $x) = @_;
443 27 $class -> _len($x);
444 }
445  
446 0 ▶sub _digit {
Subroutine _digit: not called CC=1 SCAR=6.9
447 0 my ($class, $x, $n) = @_;
448 0 substr($class ->_str($x), -($n+1), 1);
449 }
450  
451 0 ▶sub _digitsum {
Subroutine _digitsum: not called CC=2 SCAR=17.9
452 0 my ($class, $x) = @_;
453  
454 0 my $len = $class -> _len($x);
455 0 my $sum = $class -> _zero();
456 0 ▶ for (my $i = 0 ; $i < $len ; ++$i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $len) 0 0 0
457 0 my $digit = $class -> _digit($x, $i);
458 0 $digit = $class -> _new($digit);
459 0 $sum = $class -> _add($sum, $digit);
460 }
461  
462 0 return $sum;
463 }
464  
465 0 ▶sub _zeros {
Subroutine _zeros: not called CC=2 SCAR=17.9
466 0 my ($class, $x) = @_;
467 0 my $str = $class -> _str($x);
468 0 ▶ $str =~ /[^0](0*)\z/ ? CORE::length($1) : 0;
Branch: 1 branch
BranchTrueFalseTotal
$str =~ /[^0](0*)\z/ ? : 0 0 0
469 }
470  
471 ##############################################################################
472 # _is_* routines
473  
474 0 ▶sub _is_zero {
Subroutine _is_zero: not called CC=1 SCAR=6.9
475 # return true if arg is zero
476 0 my ($class, $x) = @_;
477 0 $class -> _str($x) == 0;
478 }
479  
480 0 ▶sub _is_even {
Subroutine _is_even: not called CC=1 SCAR=6.9
481 # return true if arg is even
482 0 my ($class, $x) = @_;
483 0 substr($class -> _str($x), -1, 1) % 2 == 0;
484 }
485  
486 0 ▶sub _is_odd {
Subroutine _is_odd: not called CC=1 SCAR=6.9
487 # return true if arg is odd
488 0 my ($class, $x) = @_;
489 0 substr($class -> _str($x), -1, 1) % 2 != 0;
490 }
491  
492 0 ▶sub _is_one {
Subroutine _is_one: not called CC=1 SCAR=6.9
493 # return true if arg is one
494 0 my ($class, $x) = @_;
495 0 $class -> _str($x) == 1;
496 }
497  
498 0 ▶sub _is_two {
Subroutine _is_two: not called CC=1 SCAR=6.9
499 # return true if arg is two
500 0 my ($class, $x) = @_;
501 0 $class -> _str($x) == 2;
502 }
503  
504 0 ▶sub _is_ten {
Subroutine _is_ten: not called CC=1 SCAR=6.9
505 # return true if arg is ten
506 0 my ($class, $x) = @_;
507 0 $class -> _str($x) == 10;
508 }
509  
510 ###############################################################################
511 # check routine to test internal state for corruptions
512  
513 8106 ▶sub _check {
Subroutine _check: called CC=3 SCAR=11.0
514 # used by the test suite
515 8106 my ($class, $x) = @_;
516 8106 ▶ return "Input is undefined" unless defined $x;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $x 0 8106 8106
517 8106 ▶ return "$x is not a reference" unless ref($x);
Branch: 1 branch
BranchTrueFalseTotal
unless ref $x 1 8105 8106
518 8105 return 0;
519 }
520  
521 ###############################################################################
522  
523 0 ▶sub _mod {
Subroutine _mod: not called CC=4 SCAR=30.0
524 # modulus
525 0 my ($class, $x, $y) = @_;
526  
527 0 ▶ croak "@{[(caller 0)[3]]} requires non-zero second operand"
Branch: 1 branch
BranchTrueFalseTotal
if $class->_is_zero($y) 0 0 0
528 if $class -> _is_zero($y);
529  
530 0 ▶ if ($class -> can('_div')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($class->can('_div')) { } 0 0 0
531 0 $x = $class -> _copy($x);
532 0 my ($q, $r) = $class -> _div($x, $y);
533 0 return $r;
534 } else {
535 0 my $r = $class -> _copy($x);
536 0 ▶ while ($class -> _acmp($r, $y) >= 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($class->_acmp($r, $y) >= 0) 0 0 0
537 0 $r = $class -> _sub($r, $y);
538 }
539 0 return $r;
540 }
541 }
542  
543 ##############################################################################
544 # shifts
545  
546 0 ▶sub _rsft {
Subroutine _rsft: not called CC=2 SCAR=17.9
547 0 my ($class, $x, $n, $b) = @_;
548 0 ▶ $b = $class -> _new($b) unless ref $b;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $b 0 0 0
549 0 return scalar $class -> _div($x, $class -> _pow($class -> _copy($b), $n));
550 }
551  
552 0 ▶sub _lsft {
Subroutine _lsft: not called CC=2 SCAR=17.9
553 0 my ($class, $x, $n, $b) = @_;
554 0 ▶ $b = $class -> _new($b) unless ref $b;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $b 0 0 0
555 0 return $class -> _mul($x, $class -> _pow($class -> _copy($b), $n));
556 }
557  
558 0 ▶sub _pow {
Subroutine _pow: not called CC=7 SCAR=40.3
559 # power of $x to $y
560 0 my ($class, $x, $y) = @_;
561  
562 0 ▶ if ($class -> _is_zero($y)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($class->_is_zero($y)) 0 0 0
563 0 return $class -> _one(); # y == 0 => x => 1
564 }
565  
566 0 ▶ if (($class -> _is_one($x)) || # x == 1
Branch: 1 branch
BranchTrueFalseTotal
if ($class->_is_one($x) or $class->_is_one($y)) 0 0 0
Condition0%0/3
$class->_is_one($x) or $class->_is_one($y)
l!l&&r!l&&!r
000
MC/DC0%0/2
$class->_is_one($x) or $class->_is_one($y)
$class->_is_one($x) $class->_is_one($y)
Truth table0%0/3
A or B
A= $class->_is_one($x)B= $class->_is_one($y)
ABresult
1X1
011
000
567 ($class -> _is_one($y))) # or y == 1
568 {
569 0 return $x;
570 }
571  
572 0 ▶ if ($class -> _is_zero($x)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($class->_is_zero($x)) 0 0 0
573 0 return $class -> _zero(); # 0 ** y => 0 (if not y <= 0)
574 }
575  
576 0 my $pow2 = $class -> _one();
577  
578 0 my $y_bin = $class -> _as_bin($y);
579 0 $y_bin =~ s/^0b//;
580 0 my $len = length($y_bin);
581  
582 0 ▶ while (--$len > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (--$len > 0) 0 0 0
583 0 ▶ $pow2 = $class -> _mul($pow2, $x) if substr($y_bin, $len, 1) eq '1';
Branch: 1 branch
BranchTrueFalseTotal
if substr($y_bin, $len, 1) eq 1 0 0 0
584 0 $x = $class -> _mul($x, $x);
585 }
586  
587 0 $x = $class -> _mul($x, $pow2);
588 0 return $x;
589 }
590  
591 0 ▶sub _nok {
Subroutine _nok: not called CC=4 SCAR=30.0
592 # Return binomial coefficient (n over k).
593 0 my ($class, $n, $k) = @_;
594  
595 # If k > n/2, or, equivalently, 2*k > n, compute nok(n, k) as
596 # nok(n, n-k), to minimize the number if iterations in the loop.
597  
598 {
599 0 my $twok = $class -> _mul($class -> _two(), $class -> _copy($k));
600 0 ▶ if ($class -> _acmp($twok, $n) > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($class->_acmp($twok, $n) > 0) 0 0 0
601 0 $k = $class -> _sub($class -> _copy($n), $k);
602 }
603 }
604  
605 # Example:
606 #
607 # / 7 \ 7! 1*2*3*4 * 5*6*7 5 * 6 * 7
608 # | | = --------- = --------------- = --------- = ((5 * 6) / 2 * 7) / 3
609 # \ 3 / (7-3)! 3! 1*2*3*4 * 1*2*3 1 * 2 * 3
610 #
611 # Equivalently, _nok(11, 5) is computed as
612 #
613 # (((((((7 * 8) / 2) * 9) / 3) * 10) / 4) * 11) / 5
614  
615 0 ▶ if ($class -> _is_zero($k)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($class->_is_zero($k)) 0 0 0
616 0 return $class -> _one();
617 }
618  
619 # Make a copy of the original n, in case the subclass modifies n in-place.
620  
621 0 my $n_orig = $class -> _copy($n);
622  
623 # n = 5, f = 6, d = 2 (cf. example above)
624  
625 0 $n = $class -> _sub($n, $k);
626 0 $n = $class -> _inc($n);
627  
628 0 my $f = $class -> _copy($n);
629 0 $f = $class -> _inc($f);
630  
631 0 my $d = $class -> _two();
632  
633 # while f <= n (the original n, that is) ...
634  
635 0 ▶ while ($class -> _acmp($f, $n_orig) <= 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($class->_acmp($f, $n_orig) <= 0) 0 0 0
636 0 $n = $class -> _mul($n, $f);
637 0 $n = $class -> _div($n, $d);
638 0 $f = $class -> _inc($f);
639 0 $d = $class -> _inc($d);
640 }
641  
642 0 return $n;
643 }
644  
645 #sub _fac {
646 # # factorial
647 # my ($class, $x) = @_;
648 #
649 # my $two = $class -> _two();
650 #
651 # if ($class -> _acmp($x, $two) < 0) {
652 # return $class -> _one();
653 # }
654 #
655 # my $i = $class -> _copy($x);
656 # while ($class -> _acmp($i, $two) > 0) {
657 # $i = $class -> _dec($i);
658 # $x = $class -> _mul($x, $i);
659 # }
660 #
661 # return $x;
662 #}
663  
664 0 ▶sub _fac {
Subroutine _fac: not called CC=5 SCAR=34.0
665 # factorial
666 0 my ($class, $x) = @_;
667  
668 # This is an implementation of the split recursive algorithm. See
669 # http://www.luschny.de/math/factorial/csharp/FactorialSplit.cs.html
670  
671 0 my $p = $class -> _one();
672 0 my $r = $class -> _one();
673 0 my $two = $class -> _two();
674  
675 0 my ($log2n) = $class -> _log_int($class -> _copy($x), $two);
676 0 my $h = $class -> _zero();
677 0 my $shift = $class -> _zero();
678 0 my $k = $class -> _one();
679  
680 0 ▶ while ($class -> _acmp($h, $x)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($class->_acmp($h, $x)) 0 0 0
681 0 $shift = $class -> _add($shift, $h);
682 0 $h = $class -> _rsft($class -> _copy($x), $log2n, $two);
683 0 ▶ $log2n = $class -> _dec($log2n) if !$class -> _is_zero($log2n);
Branch: 1 branch
BranchTrueFalseTotal
unless $class->_is_zero($log2n) 0 0 0
684 0 my $high = $class -> _copy($h);
685 0 ▶ $high = $class -> _dec($high) if $class -> _is_even($h);
Branch: 1 branch
BranchTrueFalseTotal
if $class->_is_even($h) 0 0 0
686 0 ▶ while ($class -> _acmp($k, $high)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($class->_acmp($k, $high)) 0 0 0
687 0 $k = $class -> _add($k, $two);
688 0 $p = $class -> _mul($p, $k);
689 }
690 0 $r = $class -> _mul($r, $p);
691 }
692 0 return $class -> _lsft($r, $shift, $two);
693 }
694  
695 77 ▶sub _dfac {
Subroutine _dfac: called CC=3 SCAR=11.1
696 # double factorial
697 77 my ($class, $x) = @_;
698  
699 77 my $two = $class -> _two();
700  
701 77 ▶ if ($class -> _acmp($x, $two) < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($class->_acmp($x, $two) < 0) 0 77 77
702 0 return $class -> _one();
703 }
704  
705 77 my $i = $class -> _copy($x);
706 77 ▶ while ($class -> _acmp($i, $two) > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($class->_acmp($i, $two) > 0) 210 77 287
707 210 $i = $class -> _sub($i, $two);
708 210 $x = $class -> _mul($x, $i);
709 }
710  
711 77 return $x;
712 }
713  
714 0 ▶sub _log_int {
Subroutine _log_int: not called CC=17 SCAR=57.2
715 # calculate integer log of $x to base $base
716 # calculate integer log of $x to base $base
717 # ref to array, ref to array - return ref to array
718 0 my ($class, $x, $base) = @_;
719  
720 # X == 0 => NaN
721 0 ▶ return if $class -> _is_zero($x);
Branch: 1 branch
BranchTrueFalseTotal
if $class->_is_zero($x) 0 0 0
722  
723 0 ▶ $base = $class -> _new(2) unless defined($base);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $base 0 0 0
724 0 ▶ $base = $class -> _new($base) unless ref($base);
Branch: 1 branch
BranchTrueFalseTotal
unless ref $base 0 0 0
725  
726 # BASE 0 or 1 => NaN
727 0 ▶ return if $class -> _is_zero($base) || $class -> _is_one($base);
Branch: 1 branch
BranchTrueFalseTotal
if $class->_is_zero($base) or $class->_is_one($base) 0 0 0
Condition0%0/3
$class->_is_zero($base) or $class->_is_one($base)
l!l&&r!l&&!r
000
MC/DC0%0/2
$class->_is_zero($base) or $class->_is_one($base)
$class->_is_zero($base) $class->_is_one($base)
Truth table0%0/3
A or B
A= $class->_is_zero($base)B= $class->_is_one($base)
ABresult
1X1
011
000
728  
729 # X == 1 => 0 (is exact)
730 0 ▶ if ($class -> _is_one($x)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($class->_is_one($x)) 0 0 0
731 0 ▶ return $class -> _zero(), 1 if wantarray;
Branch: 1 branch
BranchTrueFalseTotal
if wantarray 0 0 0
732 0 return $class -> _zero();
733 }
734  
735 0 my $cmp = $class -> _acmp($x, $base);
736  
737 # X == BASE => 1 (is exact)
738 0 ▶ if ($cmp == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cmp == 0) 0 0 0
739 0 ▶ return $class -> _one(), 1 if wantarray;
Branch: 1 branch
BranchTrueFalseTotal
if wantarray 0 0 0
740 0 return $class -> _one();
741 }
742  
743 # 1 < X < BASE => 0 (is truncated)
744 0 ▶ if ($cmp < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cmp < 0) 0 0 0
745 0 ▶ return $class -> _zero(), 0 if wantarray;
Branch: 1 branch
BranchTrueFalseTotal
if wantarray 0 0 0
746 0 return $class -> _zero();
747 }
748  
749 0 my $y;
750  
751 # log(x) / log(b) = log(xm * 10^xe) / log(bm * 10^be)
752 # = (log(xm) + xe*(log(10))) / (log(bm) + be*log(10))
753  
754 {
755 0 my $x_str = $class -> _str($x);
756 0 my $b_str = $class -> _str($base);
757 0 my $xm = "." . $x_str;
758 0 my $bm = "." . $b_str;
759 0 my $xe = length($x_str);
760 0 my $be = length($b_str);
761 0 my $log10 = log(10);
762 0 my $guess = int((log($xm) + $xe * $log10) / (log($bm) + $be * $log10));
763 0 $y = $class -> _new($guess);
764 }
765  
766 0 my $trial = $class -> _pow($class -> _copy($base), $y);
767 0 my $acmp = $class -> _acmp($trial, $x);
768  
769 # Too small?
770  
771 0 ▶ while ($acmp < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($acmp < 0) 0 0 0
772 0 $trial = $class -> _mul($trial, $base);
773 0 $y = $class -> _inc($y);
774 0 $acmp = $class -> _acmp($trial, $x);
775 }
776  
777 # Too big?
778  
779 0 ▶ while ($acmp > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($acmp > 0) 0 0 0
780 0 $trial = $class -> _div($trial, $base);
781 0 $y = $class -> _dec($y);
782 0 $acmp = $class -> _acmp($trial, $x);
783 }
784  
785 0 ▶ return wantarray ? ($y, 1) : $y if $acmp == 0; # result is exact
Branch: 2 branches
BranchTrueFalseTotal
if $acmp == 0 0 0 0
wantarray ? : 0 0 0
786 0 ▶ return wantarray ? ($y, 0) : $y; # result is too small
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
787 }
788  
789 0 ▶sub _ilog2 {
Subroutine _ilog2: not called CC=4 SCAR=30.0
790 0 my ($class, $x) = @_;
791  
792 0 ▶ return if $class -> _is_zero($x);
Branch: 1 branch
BranchTrueFalseTotal
if $class->_is_zero($x) 0 0 0
793  
794 0 my $str = $class -> _to_hex($x);
795  
796 # First do the bits in all but the most significant hex digit.
797  
798 0 my $y = $class -> _new(length($str) - 1);
799 0 $y = $class -> _mul($y, $class -> _new(4));
800  
801 # Now add the number of bits in the most significant hex digit.
802  
803 0 my $n = int log(hex(substr($str, 0, 1))) / log(2);
804 0 $y = $class -> _add($y, $class -> _new($n));
805 0 ▶ return $y unless wantarray;
Branch: 1 branch
BranchTrueFalseTotal
unless wantarray 0 0 0
806  
807 0 my $pow2 = $class -> _lsft($class -> _one(), $y, 2);
808 0 ▶ my $is_exact = $class -> _acmp($x, $pow2) == 0 ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$class->_acmp($x, $pow2) == 0 ? : 0 0 0
809 0 return $y, $is_exact;
810 }
811  
812 0 ▶sub _ilog10 {
Subroutine _ilog10: not called CC=4 SCAR=30.0
813 0 my ($class, $x) = @_;
814  
815 0 ▶ return if $class -> _is_zero($x);
Branch: 1 branch
BranchTrueFalseTotal
if $class->_is_zero($x) 0 0 0
816  
817 0 my $str = $class -> _str($x);
818 0 my $len = length($str);
819 0 my $y = $class -> _new($len - 1);
820 0 ▶ return $y unless wantarray;
Branch: 1 branch
BranchTrueFalseTotal
unless wantarray 0 0 0
821  
822 #my $pow10 = $class -> _1ex($y);
823 #my $is_exact = $class -> _acmp($x, $pow10) ? 1 : 0;
824  
825 0 ▶ my $is_exact = $str =~ /^10*$/ ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$str =~ /^10*$/ ? : 0 0 0
826 0 return $y, $is_exact;
827 }
828  
829 0 ▶sub _clog2 {
Subroutine _clog2: not called CC=5 SCAR=34.0
830 0 my ($class, $x) = @_;
831  
832 0 ▶ return if $class -> _is_zero($x);
Branch: 1 branch
BranchTrueFalseTotal
if $class->_is_zero($x) 0 0 0
833  
834 0 my $str = $class -> _to_hex($x);
835  
836 # First do the bits in all but the most significant hex digit.
837  
838 0 my $y = $class -> _new(length($str) - 1);
839 0 $y = $class -> _mul($y, $class -> _new(4));
840  
841 # Now add the number of bits in the most significant hex digit.
842  
843 0 my $n = int log(hex(substr($str, 0, 1))) / log(2);
844 0 $y = $class -> _add($y, $class -> _new($n));
845  
846 # $y is now 1 too small unless $y is an exact power of 2.
847  
848 0 my $pow2 = $class -> _lsft($class -> _one(), $y, 2);
849 0 ▶ my $is_exact = $class -> _acmp($x, $pow2) == 0 ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$class->_acmp($x, $pow2) == 0 ? : 0 0 0
850 0 ▶ $y = $class -> _inc($y) if $is_exact == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $is_exact == 0 0 0 0
851 0 ▶ return $y, $is_exact if wantarray;
Branch: 1 branch
BranchTrueFalseTotal
if wantarray 0 0 0
852 0 return $y;
853 }
854  
855 0 ▶sub _clog10 {
Subroutine _clog10: not called CC=5 SCAR=34.0
856 0 my ($class, $x) = @_;
857  
858 0 ▶ return if $class -> _is_zero($x);
Branch: 1 branch
BranchTrueFalseTotal
if $class->_is_zero($x) 0 0 0
859  
860 0 my $str = $class -> _str($x);
861 0 my $len = length($str);
862  
863 0 ▶ if ($str =~ /^10*$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($str =~ /^10*$/) 0 0 0
864 0 my $y = $class -> _new($len - 1);
865 0 ▶ return $y, 1 if wantarray;
Branch: 1 branch
BranchTrueFalseTotal
if wantarray 0 0 0
866 0 return $y;
867 }
868  
869 0 my $y = $class -> _new($len);
870 0 ▶ return $y, 0 if wantarray;
Branch: 1 branch
BranchTrueFalseTotal
if wantarray 0 0 0
871 0 return $y;
872 }
873  
874 0 ▶sub _sqrt {
Subroutine _sqrt: not called CC=10 SCAR=47.0
875 # square-root of $y in place
876 0 my ($class, $y) = @_;
877  
878 0 ▶ return $y if $class -> _is_zero($y);
Branch: 1 branch
BranchTrueFalseTotal
if $class->_is_zero($y) 0 0 0
879  
880 0 my $y_str = $class -> _str($y);
881 0 my $y_len = length($y_str);
882  
883 # Compute the guess $x.
884  
885 0 my $xm;
886 my $xe;
887 0 ▶ if ($y_len % 2 == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($y_len % 2 == 0) { } 0 0 0
888 0 $xm = sqrt("." . $y_str);
889 0 $xe = $y_len / 2;
890 0 $xm = sprintf "%.0f", int($xm * 1e15);
891 0 $xe -= 15;
892 } else {
893 0 $xm = sqrt(".0" . $y_str);
894 0 $xe = ($y_len + 1) / 2;
895 0 $xm = sprintf "%.0f", int($xm * 1e16);
896 0 $xe -= 16;
897 }
898  
899 0 my $x;
900 0 ▶ if ($xe < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($xe < 0) { } 0 0 0
901 0 $x = substr $xm, 0, length($xm) + $xe;
902 } else {
903 0 $x = $xm . ("0" x $xe);
904 }
905  
906 0 $x = $class -> _new($x);
907  
908 # Newton's method for computing square root of y
909 #
910 # x(i+1) = x(i) - f(x(i)) / f'(x(i))
911 # = x(i) - (x(i)^2 - y) / (2 * x(i)) # use if x(i)^2 > y
912 # = x(i) + (y - x(i)^2) / (2 * x(i)) # use if x(i)^2 < y
913  
914 # Determine if x, our guess, is too small, correct, or too large.
915  
916 0 my $xsq = $class -> _mul($class -> _copy($x), $x); # x(i)^2
917 0 my $acmp = $class -> _acmp($xsq, $y); # x(i)^2 <=> y
918  
919 # Only assign a value to this variable if we will be using it.
920  
921 0 my $two;
922 0 ▶ $two = $class -> _two() if $acmp != 0;
Branch: 1 branch
BranchTrueFalseTotal
if $acmp != 0 0 0 0
923  
924 # If x is too small, do one iteration of Newton's method. Since the
925 # function f(x) = x^2 - y is concave and monotonically increasing, the next
926 # guess for x will either be correct or too large.
927  
928 0 ▶ if ($acmp < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($acmp < 0) 0 0 0
929  
930 # x(i+1) = x(i) + (y - x(i)^2) / (2 * x(i))
931  
932 0 my $numer = $class -> _sub($class -> _copy($y), $xsq); # y - x(i)^2
933 0 my $denom = $class -> _mul($class -> _copy($two), $x); # 2 * x(i)
934 0 my $delta = $class -> _div($numer, $denom);
935  
936 0 ▶ unless ($class -> _is_zero($delta)) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($class->_is_zero($delta)) 0 0 0
937 0 $x = $class -> _add($x, $delta);
938 0 $xsq = $class -> _mul($class -> _copy($x), $x); # x(i)^2
939 0 $acmp = $class -> _acmp($xsq, $y); # x(i)^2 <=> y
940 }
941 }
942  
943 # If our guess for x is too large, apply Newton's method repeatedly until
944 # we either have got the correct value, or the delta is zero.
945  
946 0 ▶ while ($acmp > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($acmp > 0) 0 0 0
947  
948 # x(i+1) = x(i) - (x(i)^2 - y) / (2 * x(i))
949  
950 0 my $numer = $class -> _sub($xsq, $y); # x(i)^2 - y
951 0 my $denom = $class -> _mul($class -> _copy($two), $x); # 2 * x(i)
952 0 my $delta = $class -> _div($numer, $denom);
953 0 ▶ last if $class -> _is_zero($delta);
Branch: 1 branch
BranchTrueFalseTotal
if $class->_is_zero($delta) 0 0 0
954  
955 0 $x = $class -> _sub($x, $delta);
956 0 $xsq = $class -> _mul($class -> _copy($x), $x); # x(i)^2
957 0 $acmp = $class -> _acmp($xsq, $y); # x(i)^2 <=> y
958 }
959  
960 # When the delta is zero, our value for x might still be too large. We
961 # require that the outout is either exact or too small (i.e., rounded down
962 # to the nearest integer), so do a final check.
963  
964 0 ▶ while ($acmp > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($acmp > 0) 0 0 0
965 0 $x = $class -> _dec($x);
966 0 $xsq = $class -> _mul($class -> _copy($x), $x); # x(i)^2
967 0 $acmp = $class -> _acmp($xsq, $y); # x(i)^2 <=> y
968 }
969  
970 0 return $x;
971 }
972  
973 0 ▶sub _root {
Subroutine _root: not called CC=21 SCAR=61.4
974 0 my ($class, $y, $n) = @_;
975  
976 0 ▶ return $y if $class -> _is_zero($y) || $class -> _is_one($y) ||
Branch: 1 branch
BranchTrueFalseTotal
if $class->_is_zero($y) or $class->_is_one($y) or $class->_is_one($n) 0 0 0
Condition0%0/6
$class->_is_zero($y) or $class->_is_one($y) or $class->_is_one($n)
l!l&&r!l&&!r
000
$class->_is_zero($y) or $class->_is_one($y)
l!l&&r!l&&!r
000
MC/DC0%0/3
$class->_is_zero($y) or $class->_is_one($y) or $class->_is_one($n)
$class->_is_zero($y) $class->_is_one($y) $class->_is_one($n)
Truth table0%0/4
A or B or C
A= $class->_is_zero($y)B= $class->_is_one($y)C= $class->_is_one($n)
ABCresult
1XX1
01X1
0011
0000
977 $class -> _is_one($n);
978  
979 # If y <= n, the result is always (truncated to) 1.
980  
981 0 ▶ return $class -> _one() if $class -> _acmp($y, $n) <= 0;
Branch: 1 branch
BranchTrueFalseTotal
if $class->_acmp($y, $n) <= 0 0 0 0
982  
983 # Compute the initial guess x of y^(1/n). When n is large, Newton's method
984 # converges slowly if the "guess" (initial value) is poor, so we need a
985 # good guess. It the guess is too small, the next guess will be too large,
986 # and from then on all guesses are too large.
987  
988 0 my $DEBUG = 0;
989  
990 # Split y into mantissa and exponent in base 10, so that
991 #
992 # y = xm * 10^xe, where 0 < xm < 1 and xe is an integer
993  
994 0 my $y_str = $class -> _str($y);
995 0 my $ym = "." . $y_str;
996 0 my $ye = length($y_str);
997  
998 # From this compute the approximate base 10 logarithm of y
999 #
1000 # log_10(y) = log_10(ym) + log_10(ye^10)
1001 # = log(ym)/log(10) + ye
1002  
1003 0 my $log10y = log($ym) / log(10) + $ye;
1004  
1005 # And from this compute the approximate base 10 logarithm of x, where
1006 # x = y^(1/n)
1007 #
1008 # log_10(x) = log_10(y)/n
1009  
1010 0 my $log10x = $log10y / $class -> _num($n);
1011  
1012 # From this compute xm and xe, the mantissa and exponent (in base 10) of x,
1013 # where 1 < xm <= 10 and xe is an integer.
1014  
1015 0 my $xe = int $log10x;
1016 0 my $xm = 10 ** ($log10x - $xe);
1017  
1018 # Scale the mantissa and exponent to increase the integer part of ym, which
1019 # gives us better accuracy.
1020  
1021 0 ▶ if ($DEBUG) {
Branch: 1 branch
BranchTrueFalseTotal
if ($DEBUG) 0 0 0
1022 0 print "\n";
1023 0 print "y_str = $y_str\n";
1024 0 print "ym = $ym\n";
1025 0 print "ye = $ye\n";
1026 0 print "log10y = $log10y\n";
1027 0 print "log10x = $log10x\n";
1028 0 print "xm = $xm\n";
1029 0 print "xe = $xe\n";
1030 }
1031  
1032 0 ▶ my $d = $xe < 15 ? $xe : 15;
Branch: 1 branch
BranchTrueFalseTotal
$xe < 15 ? : 0 0 0
1033 0 $xm *= 10 ** $d;
1034 0 $xe -= $d;
1035  
1036 0 ▶ if ($DEBUG) {
Branch: 1 branch
BranchTrueFalseTotal
if ($DEBUG) 0 0 0
1037 0 print "\n";
1038 0 print "xm = $xm\n";
1039 0 print "xe = $xe\n";
1040 }
1041  
1042 # If the mantissa is not an integer, round up to nearest integer, and then
1043 # convert the number to a string. It is important to always round up due to
1044 # how Newton's method behaves in this case. If the initial guess is too
1045 # small, the next guess will be too large, after which every succeeding
1046 # guess converges the correct value from above. Now, if the initial guess
1047 # is too small and n is large, the next guess will be much too large and
1048 # require a large number of iterations to get close to the solution.
1049 # Because of this, we are likely to find the solution faster if we make
1050 # sure the initial guess is not too small.
1051  
1052 0 my $xm_int = int($xm);
1053 0 ▶ my $x_str = sprintf '%.0f', $xm > $xm_int ? $xm_int + 1 : $xm_int;
Branch: 1 branch
BranchTrueFalseTotal
$xm > $xm_int ? : 0 0 0
1054 0 $x_str .= "0" x $xe;
1055  
1056 0 my $x = $class -> _new($x_str);
1057  
1058 0 ▶ if ($DEBUG) {
Branch: 1 branch
BranchTrueFalseTotal
if ($DEBUG) 0 0 0
1059 0 print "xm = $xm\n";
1060 0 print "xe = $xe\n";
1061 0 print "\n";
1062 0 print "x_str = $x_str (initial guess)\n";
1063 0 print "\n";
1064 }
1065  
1066 # Use Newton's method for computing n'th root of y.
1067 #
1068 # x(i+1) = x(i) - f(x(i)) / f'(x(i))
1069 # = x(i) - (x(i)^n - y) / (n * x(i)^(n-1)) # use if x(i)^n > y
1070 # = x(i) + (y - x(i)^n) / (n * x(i)^(n-1)) # use if x(i)^n < y
1071  
1072 # Determine if x, our guess, is too small, correct, or too large. Rather
1073 # than computing x(i)^n and x(i)^(n-1) directly, compute x(i)^(n-1) and
1074 # then the same value multiplied by x.
1075  
1076 0 my $nm1 = $class -> _dec($class -> _copy($n)); # n-1
1077 0 my $xpownm1 = $class -> _pow($class -> _copy($x), $nm1); # x(i)^(n-1)
1078 0 my $xpown = $class -> _mul($class -> _copy($xpownm1), $x); # x(i)^n
1079 0 my $acmp = $class -> _acmp($xpown, $y); # x(i)^n <=> y
1080  
1081 0 ▶ if ($DEBUG) {
Branch: 1 branch
BranchTrueFalseTotal
if ($DEBUG) 0 0 0
1082 0 print "\n";
1083 0 print "x = ", $class -> _str($x), "\n";
1084 0 print "x^n = ", $class -> _str($xpown), "\n";
1085 0 print "y = ", $class -> _str($y), "\n";
1086 0 print "acmp = $acmp\n";
1087 }
1088  
1089 # If x is too small, do one iteration of Newton's method. Since the
1090 # function f(x) = x^n - y is concave and monotonically increasing, the next
1091 # guess for x will either be correct or too large.
1092  
1093 0 ▶ if ($acmp < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($acmp < 0) 0 0 0
1094  
1095 # x(i+1) = x(i) + (y - x(i)^n) / (n * x(i)^(n-1))
1096  
1097 0 my $numer = $class -> _sub($class -> _copy($y), $xpown); # y - x(i)^n
1098 0 my $denom = $class -> _mul($class -> _copy($n), $xpownm1); # n * x(i)^(n-1)
1099 0 my $delta = $class -> _div($numer, $denom);
1100  
1101 0 ▶ if ($DEBUG) {
Branch: 1 branch
BranchTrueFalseTotal
if ($DEBUG) 0 0 0
1102 0 print "\n";
1103 0 print "numer = ", $class -> _str($numer), "\n";
1104 0 print "denom = ", $class -> _str($denom), "\n";
1105 0 print "delta = ", $class -> _str($delta), "\n";
1106 }
1107  
1108 0 ▶ unless ($class -> _is_zero($delta)) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($class->_is_zero($delta)) 0 0 0
1109 0 $x = $class -> _add($x, $delta);
1110 0 $xpownm1 = $class -> _pow($class -> _copy($x), $nm1); # x(i)^(n-1)
1111 0 $xpown = $class -> _mul($class -> _copy($xpownm1), $x); # x(i)^n
1112 0 $acmp = $class -> _acmp($xpown, $y); # x(i)^n <=> y
1113  
1114 0 ▶ if ($DEBUG) {
Branch: 1 branch
BranchTrueFalseTotal
if ($DEBUG) 0 0 0
1115 0 print "\n";
1116 0 print "x = ", $class -> _str($x), "\n";
1117 0 print "x^n = ", $class -> _str($xpown), "\n";
1118 0 print "y = ", $class -> _str($y), "\n";
1119 0 print "acmp = $acmp\n";
1120 }
1121 }
1122 }
1123  
1124 # If our guess for x is too large, apply Newton's method repeatedly until
1125 # we either have got the correct value, or the delta is zero.
1126  
1127 0 ▶ while ($acmp > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($acmp > 0) 0 0 0
1128  
1129 # x(i+1) = x(i) - (x(i)^n - y) / (n * x(i)^(n-1))
1130  
1131 0 my $numer = $class -> _sub($class -> _copy($xpown), $y); # x(i)^n - y
1132 0 my $denom = $class -> _mul($class -> _copy($n), $xpownm1); # n * x(i)^(n-1)
1133  
1134 0 ▶ if ($DEBUG) {
Branch: 1 branch
BranchTrueFalseTotal
if ($DEBUG) 0 0 0
1135 0 print "numer = ", $class -> _str($numer), "\n";
1136 0 print "denom = ", $class -> _str($denom), "\n";
1137 }
1138  
1139 0 my $delta = $class -> _div($numer, $denom);
1140  
1141 0 ▶ if ($DEBUG) {
Branch: 1 branch
BranchTrueFalseTotal
if ($DEBUG) 0 0 0
1142 0 print "delta = ", $class -> _str($delta), "\n";
1143 }
1144  
1145 0 ▶ last if $class -> _is_zero($delta);
Branch: 1 branch
BranchTrueFalseTotal
if $class->_is_zero($delta) 0 0 0
1146  
1147 0 $x = $class -> _sub($x, $delta);
1148 0 $xpownm1 = $class -> _pow($class -> _copy($x), $nm1); # x(i)^(n-1)
1149 0 $xpown = $class -> _mul($class -> _copy($xpownm1), $x); # x(i)^n
1150 0 $acmp = $class -> _acmp($xpown, $y); # x(i)^n <=> y
1151  
1152 0 ▶ if ($DEBUG) {
Branch: 1 branch
BranchTrueFalseTotal
if ($DEBUG) 0 0 0
1153 0 print "\n";
1154 0 print "x = ", $class -> _str($x), "\n";
1155 0 print "x^n = ", $class -> _str($xpown), "\n";
1156 0 print "y = ", $class -> _str($y), "\n";
1157 0 print "acmp = $acmp\n";
1158 }
1159 }
1160  
1161 # When the delta is zero, our value for x might still be too large. We
1162 # require that the outout is either exact or too small (i.e., rounded down
1163 # to the nearest integer), so do a final check.
1164  
1165 0 ▶ while ($acmp > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($acmp > 0) 0 0 0
1166 0 $x = $class -> _dec($x);
1167 0 $xpown = $class -> _pow($class -> _copy($x), $n); # x(i)^n
1168 0 $acmp = $class -> _acmp($xpown, $y); # x(i)^n <=> y
1169 }
1170  
1171 0 return $x;
1172 }
1173  
1174 ##############################################################################
1175 # binary stuff
1176  
1177 0 ▶sub _and {
Subroutine _and: not called CC=4 SCAR=30.0
1178 0 my ($class, $x, $y) = @_;
1179  
1180 0 ▶ return $x if $class -> _acmp($x, $y) == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $class->_acmp($x, $y) == 0 0 0 0
1181  
1182 0 my $m = $class -> _one();
1183 0 my $mask = $class -> _new("32768");
1184  
1185 0 my ($xr, $yr); # remainders after division
1186  
1187 0 my $xc = $class -> _copy($x);
1188 0 my $yc = $class -> _copy($y);
1189 0 my $z = $class -> _zero();
1190  
1191 0 ▶ until ($class -> _is_zero($xc) || $class -> _is_zero($yc)) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($class->_is_zero($xc) or $class->_is_zero($yc)) 0 0 0
Condition0%0/3
$class->_is_zero($xc) or $class->_is_zero($yc)
l!l&&r!l&&!r
000
MC/DC0%0/2
$class->_is_zero($xc) or $class->_is_zero($yc)
$class->_is_zero($xc) $class->_is_zero($yc)
Truth table0%0/3
A or B
A= $class->_is_zero($xc)B= $class->_is_zero($yc)
ABresult
1X1
011
000
1192 0 ($xc, $xr) = $class -> _div($xc, $mask);
1193 0 ($yc, $yr) = $class -> _div($yc, $mask);
1194 0 my $bits = $class -> _new($class -> _num($xr) & $class -> _num($yr));
1195 0 $z = $class -> _add($z, $class -> _mul($bits, $m));
1196 0 $m = $class -> _mul($m, $mask);
1197 }
1198  
1199 0 return $z;
1200 }
1201  
1202 0 ▶sub _xor {
Subroutine _xor: not called CC=6 SCAR=37.4
1203 0 my ($class, $x, $y) = @_;
1204  
1205 0 ▶ return $class -> _zero() if $class -> _acmp($x, $y) == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $class->_acmp($x, $y) == 0 0 0 0
1206  
1207 0 my $m = $class -> _one();
1208 0 my $mask = $class -> _new("32768");
1209  
1210 0 my ($xr, $yr); # remainders after division
1211  
1212 0 my $xc = $class -> _copy($x);
1213 0 my $yc = $class -> _copy($y);
1214 0 my $z = $class -> _zero();
1215  
1216 0 ▶ until ($class -> _is_zero($xc) || $class -> _is_zero($yc)) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($class->_is_zero($xc) or $class->_is_zero($yc)) 0 0 0
Condition0%0/3
$class->_is_zero($xc) or $class->_is_zero($yc)
l!l&&r!l&&!r
000
MC/DC0%0/2
$class->_is_zero($xc) or $class->_is_zero($yc)
$class->_is_zero($xc) $class->_is_zero($yc)
Truth table0%0/3
A or B
A= $class->_is_zero($xc)B= $class->_is_zero($yc)
ABresult
1X1
011
000
1217 0 ($xc, $xr) = $class -> _div($xc, $mask);
1218 0 ($yc, $yr) = $class -> _div($yc, $mask);
1219 0 my $bits = $class -> _new($class -> _num($xr) ^ $class -> _num($yr));
1220 0 $z = $class -> _add($z, $class -> _mul($bits, $m));
1221 0 $m = $class -> _mul($m, $mask);
1222 }
1223  
1224 # The loop above stops when the smallest of the two numbers is exhausted.
1225 # The remainder of the longer one will survive bit-by-bit, so we simple
1226 # multiply-add it in.
1227  
1228 0 ▶ $z = $class -> _add($z, $class -> _mul($xc, $m))
Branch: 1 branch
BranchTrueFalseTotal
unless $class->_is_zero($xc) 0 0 0
1229 unless $class -> _is_zero($xc);
1230 0 ▶ $z = $class -> _add($z, $class -> _mul($yc, $m))
Branch: 1 branch
BranchTrueFalseTotal
unless $class->_is_zero($yc) 0 0 0
1231 unless $class -> _is_zero($yc);
1232  
1233 0 return $z;
1234 }
1235  
1236 0 ▶sub _or {
Subroutine _or: not called CC=6 SCAR=37.4
1237 0 my ($class, $x, $y) = @_;
1238  
1239 0 ▶ return $x if $class -> _acmp($x, $y) == 0; # shortcut (see _and)
Branch: 1 branch
BranchTrueFalseTotal
if $class->_acmp($x, $y) == 0 0 0 0
1240  
1241 0 my $m = $class -> _one();
1242 0 my $mask = $class -> _new("32768");
1243  
1244 0 my ($xr, $yr); # remainders after division
1245  
1246 0 my $xc = $class -> _copy($x);
1247 0 my $yc = $class -> _copy($y);
1248 0 my $z = $class -> _zero();
1249  
1250 0 ▶ until ($class -> _is_zero($xc) || $class -> _is_zero($yc)) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($class->_is_zero($xc) or $class->_is_zero($yc)) 0 0 0
Condition0%0/3
$class->_is_zero($xc) or $class->_is_zero($yc)
l!l&&r!l&&!r
000
MC/DC0%0/2
$class->_is_zero($xc) or $class->_is_zero($yc)
$class->_is_zero($xc) $class->_is_zero($yc)
Truth table0%0/3
A or B
A= $class->_is_zero($xc)B= $class->_is_zero($yc)
ABresult
1X1
011
000
1251 0 ($xc, $xr) = $class -> _div($xc, $mask);
1252 0 ($yc, $yr) = $class -> _div($yc, $mask);
1253 0 my $bits = $class -> _new($class -> _num($xr) | $class -> _num($yr));
1254 0 $z = $class -> _add($z, $class -> _mul($bits, $m));
1255 0 $m = $class -> _mul($m, $mask);
1256 }
1257  
1258 # The loop above stops when the smallest of the two numbers is exhausted.
1259 # The remainder of the longer one will survive bit-by-bit, so we simple
1260 # multiply-add it in.
1261  
1262 0 ▶ $z = $class -> _add($z, $class -> _mul($xc, $m))
Branch: 1 branch
BranchTrueFalseTotal
unless $class->_is_zero($xc) 0 0 0
1263 unless $class -> _is_zero($xc);
1264 0 ▶ $z = $class -> _add($z, $class -> _mul($yc, $m))
Branch: 1 branch
BranchTrueFalseTotal
unless $class->_is_zero($yc) 0 0 0
1265 unless $class -> _is_zero($yc);
1266  
1267 0 return $z;
1268 }
1269  
1270 33 ▶sub _sand {
Subroutine _sand: called CC=14 SCAR=26.4
1271 33 my ($class, $x, $sx, $y, $sy) = @_;
1272  
1273 33 ▶ return ($class -> _zero(), '+')
Branch: 1 branch
BranchTrueFalseTotal
if $class->_is_zero($x) or $class->_is_zero($y) 0 33 33
Condition33%1/3
$class->_is_zero($x) or $class->_is_zero($y)
l!l&&r!l&&!r
0033
MC/DC0%0/2
$class->_is_zero($x) or $class->_is_zero($y)
$class->_is_zero($x) $class->_is_zero($y)
Truth table33%1/3
A or B
A= $class->_is_zero($x)B= $class->_is_zero($y)
ABresult
1X1
011
000
1274 if $class -> _is_zero($x) || $class -> _is_zero($y);
1275  
1276 33 ▶ my $sign = $sx eq '-' && $sy eq '-' ? '-' : '+';
Branch: 1 branch
BranchTrueFalseTotal
$sx eq '-' && $sy eq '-' ? : 16 17 33
Condition100%3/3
$sx eq "-" and $sy eq "-"
!ll&&!rl&&r
9816
MC/DC100%2/2
$sx eq "-" and $sy eq "-"
$sx eq "-" $sy eq "-"
Truth table100%3/3
A and B
A= $sx eq "-"B= $sy eq "-"
ABresult
0X0
100
111
1277  
1278 33 my ($bx, $by);
1279  
1280 33 ▶ if ($sx eq '-') { # if x is negative
Branch: 1 branch
BranchTrueFalseTotal
if ($sx eq '-') { } 24 9 33
1281 # two's complement: inc (dec unsigned value) and flip all "bits" in $bx
1282 24 $bx = $class -> _copy($x);
1283 24 $bx = $class -> _dec($bx);
1284 24 $bx = $class -> _as_hex($bx);
1285 24 $bx =~ s/^-?0x//;
1286 24 $bx =~ tr<0123456789abcdef>
1287 <\x0f\x0e\x0d\x0c\x0b\x0a\x09\x08\x07\x06\x05\x04\x03\x02\x01\x00>;
1288 } else { # if x is positive
1289 9 $bx = $class -> _as_hex($x); # get binary representation
1290 9 $bx =~ s/^-?0x//;
1291 9 $bx =~ tr<fedcba9876543210>
1292 <\x0f\x0e\x0d\x0c\x0b\x0a\x09\x08\x07\x06\x05\x04\x03\x02\x01\x00>;
1293 }
1294  
1295 33 ▶ if ($sy eq '-') { # if y is negative
Branch: 1 branch
BranchTrueFalseTotal
if ($sy eq '-') { } 25 8 33
1296 # two's complement: inc (dec unsigned value) and flip all "bits" in $by
1297 25 $by = $class -> _copy($y);
1298 25 $by = $class -> _dec($by);
1299 25 $by = $class -> _as_hex($by);
1300 25 $by =~ s/^-?0x//;
1301 25 $by =~ tr<0123456789abcdef>
1302 <\x0f\x0e\x0d\x0c\x0b\x0a\x09\x08\x07\x06\x05\x04\x03\x02\x01\x00>;
1303 } else {
1304 8 $by = $class -> _as_hex($y); # get binary representation
1305 8 $by =~ s/^-?0x//;
1306 8 $by =~ tr<fedcba9876543210>
1307 <\x0f\x0e\x0d\x0c\x0b\x0a\x09\x08\x07\x06\x05\x04\x03\x02\x01\x00>;
1308 }
1309  
1310 # now we have bit-strings from X and Y, reverse them for padding
1311 33 $bx = reverse $bx;
1312 33 $by = reverse $by;
1313  
1314 # padd the shorter string
1315 33 ▶ my $xx = "\x00"; $xx = "\x0f" if $sx eq '-';
Branch: 1 branch
BranchTrueFalseTotal
if $sx eq "-" 24 9 33
1316 33 ▶ my $yy = "\x00"; $yy = "\x0f" if $sy eq '-';
Branch: 1 branch
BranchTrueFalseTotal
if $sy eq "-" 25 8 33
1317 33 my $diff = CORE::length($bx) - CORE::length($by);
1318 33 ▶ if ($diff > 0) {
Branch: 2 branches
BranchTrueFalseTotal
if ($diff > 0) { } 9 24 33
elsif ($diff < 0) { } 0 24 24
1319 # if $yy eq "\x00", we can cut $bx, otherwise we need to padd $by
1320 9 $by .= $yy x $diff;
1321 } elsif ($diff < 0) {
1322 # if $xx eq "\x00", we can cut $by, otherwise we need to padd $bx
1323 0 $bx .= $xx x abs($diff);
1324 }
1325  
1326 # and the strings together
1327 33 my $r = $bx & $by;
1328  
1329 # and reverse the result again
1330 33 $bx = reverse $r;
1331  
1332 # One of $bx or $by was negative, so need to flip bits in the result. In both
1333 # cases (one or two of them negative, or both positive) we need to get the
1334 # characters back.
1335 33 ▶ if ($sign eq '-') {
Branch: 1 branch
BranchTrueFalseTotal
if ($sign eq '-') { } 16 17 33
1336 16 $bx =~ tr<\x0f\x0e\x0d\x0c\x0b\x0a\x09\x08\x07\x06\x05\x04\x03\x02\x01\x00>
1337 <0123456789abcdef>;
1338 } else {
1339 17 $bx =~ tr<\x0f\x0e\x0d\x0c\x0b\x0a\x09\x08\x07\x06\x05\x04\x03\x02\x01\x00>
1340 <fedcba9876543210>;
1341 }
1342  
1343 # leading zeros will be stripped by _from_hex()
1344 33 $bx = '0x' . $bx;
1345 33 $bx = $class -> _from_hex($bx);
1346  
1347 33 ▶ $bx = $class -> _inc($bx) if $sign eq '-';
Branch: 1 branch
BranchTrueFalseTotal
if $sign eq "-" 16 17 33
1348  
1349 # avoid negative zero
1350 33 ▶ $sign = '+' if $class -> _is_zero($bx);
Branch: 1 branch
BranchTrueFalseTotal
if $class->_is_zero($bx) 4 29 33
1351  
1352 33 return $bx, $sign;
1353 }
1354  
1355 40 ▶sub _sxor {
Subroutine _sxor: called CC=13 SCAR=25.7
1356 40 my ($class, $x, $sx, $y, $sy) = @_;
1357  
1358 40 ▶ return ($class -> _zero(), '+')
Branch: 1 branch
BranchTrueFalseTotal
if $class->_is_zero($x) and $class->_is_zero($y) 0 40 40
Condition33%1/3
$class->_is_zero($x) and $class->_is_zero($y)
!ll&&!rl&&r
4000
MC/DC0%0/2
$class->_is_zero($x) and $class->_is_zero($y)
$class->_is_zero($x) $class->_is_zero($y)
Truth table33%1/3
A and B
A= $class->_is_zero($x)B= $class->_is_zero($y)
ABresult
0X0
100
111
1359 if $class -> _is_zero($x) && $class -> _is_zero($y);
1360  
1361 40 ▶ my $sign = $sx ne $sy ? '-' : '+';
Branch: 1 branch
BranchTrueFalseTotal
$sx ne $sy ? : 24 16 40
1362  
1363 40 my ($bx, $by);
1364  
1365 40 ▶ if ($sx eq '-') { # if x is negative
Branch: 1 branch
BranchTrueFalseTotal
if ($sx eq '-') { } 27 13 40
1366 # two's complement: inc (dec unsigned value) and flip all "bits" in $bx
1367 27 $bx = $class -> _copy($x);
1368 27 $bx = $class -> _dec($bx);
1369 27 $bx = $class -> _as_hex($bx);
1370 27 $bx =~ s/^-?0x//;
1371 27 $bx =~ tr<0123456789abcdef>
1372 <\x0f\x0e\x0d\x0c\x0b\x0a\x09\x08\x07\x06\x05\x04\x03\x02\x01\x00>;
1373 } else { # if x is positive
1374 13 $bx = $class -> _as_hex($x); # get binary representation
1375 13 $bx =~ s/^-?0x//;
1376 13 $bx =~ tr<fedcba9876543210>
1377 <\x0f\x0e\x0d\x0c\x0b\x0a\x09\x08\x07\x06\x05\x04\x03\x02\x01\x00>;
1378 }
1379  
1380 40 ▶ if ($sy eq '-') { # if y is negative
Branch: 1 branch
BranchTrueFalseTotal
if ($sy eq '-') { } 29 11 40
1381 # two's complement: inc (dec unsigned value) and flip all "bits" in $by
1382 29 $by = $class -> _copy($y);
1383 29 $by = $class -> _dec($by);
1384 29 $by = $class -> _as_hex($by);
1385 29 $by =~ s/^-?0x//;
1386 29 $by =~ tr<0123456789abcdef>
1387 <\x0f\x0e\x0d\x0c\x0b\x0a\x09\x08\x07\x06\x05\x04\x03\x02\x01\x00>;
1388 } else {
1389 11 $by = $class -> _as_hex($y); # get binary representation
1390 11 $by =~ s/^-?0x//;
1391 11 $by =~ tr<fedcba9876543210>
1392 <\x0f\x0e\x0d\x0c\x0b\x0a\x09\x08\x07\x06\x05\x04\x03\x02\x01\x00>;
1393 }
1394  
1395 # now we have bit-strings from X and Y, reverse them for padding
1396 40 $bx = reverse $bx;
1397 40 $by = reverse $by;
1398  
1399 # padd the shorter string
1400 40 ▶ my $xx = "\x00"; $xx = "\x0f" if $sx eq '-';
Branch: 1 branch
BranchTrueFalseTotal
if $sx eq "-" 27 13 40
1401 40 ▶ my $yy = "\x00"; $yy = "\x0f" if $sy eq '-';
Branch: 1 branch
BranchTrueFalseTotal
if $sy eq "-" 29 11 40
1402 40 my $diff = CORE::length($bx) - CORE::length($by);
1403 40 ▶ if ($diff > 0) {
Branch: 2 branches
BranchTrueFalseTotal
if ($diff > 0) { } 9 31 40
elsif ($diff < 0) { } 3 28 31
1404 # if $yy eq "\x00", we can cut $bx, otherwise we need to padd $by
1405 9 $by .= $yy x $diff;
1406 } elsif ($diff < 0) {
1407 # if $xx eq "\x00", we can cut $by, otherwise we need to padd $bx
1408 3 $bx .= $xx x abs($diff);
1409 }
1410  
1411 # xor the strings together
1412 40 my $r = $bx ^ $by;
1413  
1414 # and reverse the result again
1415 40 $bx = reverse $r;
1416  
1417 # One of $bx or $by was negative, so need to flip bits in the result. In both
1418 # cases (one or two of them negative, or both positive) we need to get the
1419 # characters back.
1420 40 ▶ if ($sign eq '-') {
Branch: 1 branch
BranchTrueFalseTotal
if ($sign eq '-') { } 24 16 40
1421 24 $bx =~ tr<\x0f\x0e\x0d\x0c\x0b\x0a\x09\x08\x07\x06\x05\x04\x03\x02\x01\x00>
1422 <0123456789abcdef>;
1423 } else {
1424 16 $bx =~ tr<\x0f\x0e\x0d\x0c\x0b\x0a\x09\x08\x07\x06\x05\x04\x03\x02\x01\x00>
1425 <fedcba9876543210>;
1426 }
1427  
1428 # leading zeros will be stripped by _from_hex()
1429 40 $bx = '0x' . $bx;
1430 40 $bx = $class -> _from_hex($bx);
1431  
1432 40 ▶ $bx = $class -> _inc($bx) if $sign eq '-';
Branch: 1 branch
BranchTrueFalseTotal
if $sign eq "-" 24 16 40
1433  
1434 # avoid negative zero
1435 40 ▶ $sign = '+' if $class -> _is_zero($bx);
Branch: 1 branch
BranchTrueFalseTotal
if $class->_is_zero($bx) 8 32 40
1436  
1437 40 return $bx, $sign;
1438 }
1439  
1440 35 ▶sub _sor {
Subroutine _sor: called CC=14 SCAR=26.7
1441 35 my ($class, $x, $sx, $y, $sy) = @_;
1442  
1443 35 ▶ return ($class -> _zero(), '+')
Branch: 1 branch
BranchTrueFalseTotal
if $class->_is_zero($x) and $class->_is_zero($y) 0 35 35
Condition33%1/3
$class->_is_zero($x) and $class->_is_zero($y)
!ll&&!rl&&r
3500
MC/DC0%0/2
$class->_is_zero($x) and $class->_is_zero($y)
$class->_is_zero($x) $class->_is_zero($y)
Truth table33%1/3
A and B
A= $class->_is_zero($x)B= $class->_is_zero($y)
ABresult
0X0
100
111
1444 if $class -> _is_zero($x) && $class -> _is_zero($y);
1445  
1446 35 ▶ my $sign = $sx eq '-' || $sy eq '-' ? '-' : '+';
Branch: 1 branch
BranchTrueFalseTotal
$sx eq '-' || $sy eq '-' ? : 35 0 35
Condition66%2/3
$sx eq "-" or $sy eq "-"
l!l&&r!l&&!r
23120
MC/DC0%0/2
$sx eq "-" or $sy eq "-"
$sx eq "-" $sy eq "-"
Truth table66%2/3
A or B
A= $sx eq "-"B= $sy eq "-"
ABresult
1X1
011
000
1447  
1448 35 my ($bx, $by);
1449  
1450 35 ▶ if ($sx eq '-') { # if x is negative
Branch: 1 branch
BranchTrueFalseTotal
if ($sx eq '-') { } 23 12 35
1451 # two's complement: inc (dec unsigned value) and flip all "bits" in $bx
1452 23 $bx = $class -> _copy($x);
1453 23 $bx = $class -> _dec($bx);
1454 23 $bx = $class -> _as_hex($bx);
1455 23 $bx =~ s/^-?0x//;
1456 23 $bx =~ tr<0123456789abcdef>
1457 <\x0f\x0e\x0d\x0c\x0b\x0a\x09\x08\x07\x06\x05\x04\x03\x02\x01\x00>;
1458 } else { # if x is positive
1459 12 $bx = $class -> _as_hex($x); # get binary representation
1460 12 $bx =~ s/^-?0x//;
1461 12 $bx =~ tr<fedcba9876543210>
1462 <\x0f\x0e\x0d\x0c\x0b\x0a\x09\x08\x07\x06\x05\x04\x03\x02\x01\x00>;
1463 }
1464  
1465 35 ▶ if ($sy eq '-') { # if y is negative
Branch: 1 branch
BranchTrueFalseTotal
if ($sy eq '-') { } 24 11 35
1466 # two's complement: inc (dec unsigned value) and flip all "bits" in $by
1467 24 $by = $class -> _copy($y);
1468 24 $by = $class -> _dec($by);
1469 24 $by = $class -> _as_hex($by);
1470 24 $by =~ s/^-?0x//;
1471 24 $by =~ tr<0123456789abcdef>
1472 <\x0f\x0e\x0d\x0c\x0b\x0a\x09\x08\x07\x06\x05\x04\x03\x02\x01\x00>;
1473 } else {
1474 11 $by = $class -> _as_hex($y); # get binary representation
1475 11 $by =~ s/^-?0x//;
1476 11 $by =~ tr<fedcba9876543210>
1477 <\x0f\x0e\x0d\x0c\x0b\x0a\x09\x08\x07\x06\x05\x04\x03\x02\x01\x00>;
1478 }
1479  
1480 # now we have bit-strings from X and Y, reverse them for padding
1481 35 $bx = reverse $bx;
1482 35 $by = reverse $by;
1483  
1484 # padd the shorter string
1485 35 ▶ my $xx = "\x00"; $xx = "\x0f" if $sx eq '-';
Branch: 1 branch
BranchTrueFalseTotal
if $sx eq "-" 23 12 35
1486 35 ▶ my $yy = "\x00"; $yy = "\x0f" if $sy eq '-';
Branch: 1 branch
BranchTrueFalseTotal
if $sy eq "-" 24 11 35
1487 35 my $diff = CORE::length($bx) - CORE::length($by);
1488 35 ▶ if ($diff > 0) {
Branch: 2 branches
BranchTrueFalseTotal
if ($diff > 0) { } 12 23 35
elsif ($diff < 0) { } 3 20 23
1489 # if $yy eq "\x00", we can cut $bx, otherwise we need to padd $by
1490 12 $by .= $yy x $diff;
1491 } elsif ($diff < 0) {
1492 # if $xx eq "\x00", we can cut $by, otherwise we need to padd $bx
1493 3 $bx .= $xx x abs($diff);
1494 }
1495  
1496 # or the strings together
1497 35 my $r = $bx | $by;
1498  
1499 # and reverse the result again
1500 35 $bx = reverse $r;
1501  
1502 # One of $bx or $by was negative, so need to flip bits in the result. In both
1503 # cases (one or two of them negative, or both positive) we need to get the
1504 # characters back.
1505 35 ▶ if ($sign eq '-') {
Branch: 1 branch
BranchTrueFalseTotal
if ($sign eq '-') { } 35 0 35
1506 35 $bx =~ tr<\x0f\x0e\x0d\x0c\x0b\x0a\x09\x08\x07\x06\x05\x04\x03\x02\x01\x00>
1507 <0123456789abcdef>;
1508 } else {
1509 0 $bx =~ tr<\x0f\x0e\x0d\x0c\x0b\x0a\x09\x08\x07\x06\x05\x04\x03\x02\x01\x00>
1510 <fedcba9876543210>;
1511 }
1512  
1513 # leading zeros will be stripped by _from_hex()
1514 35 $bx = '0x' . $bx;
1515 35 $bx = $class -> _from_hex($bx);
1516  
1517 35 ▶ $bx = $class -> _inc($bx) if $sign eq '-';
Branch: 1 branch
BranchTrueFalseTotal
if $sign eq "-" 35 0 35
1518  
1519 # avoid negative zero
1520 35 ▶ $sign = '+' if $class -> _is_zero($bx);
Branch: 1 branch
BranchTrueFalseTotal
if $class->_is_zero($bx) 0 35 35
1521  
1522 35 return $bx, $sign;
1523 }
1524  
1525 369 ▶sub _to_bin {
Subroutine _to_bin: called CC=4 SCAR=13.9
1526 # convert the number to a string of binary digits without prefix
1527 369 my ($class, $x) = @_;
1528 369 my $str = '';
1529 369 my $tmp = $class -> _copy($x);
1530 369 my $chunk = $class -> _new("16777216"); # 2^24 = 24 binary digits
1531 369 my $rem;
1532 369 ▶ until ($class -> _acmp($tmp, $chunk) < 0) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($class->_acmp($tmp, $chunk) < 0) 83 369 452
1533 83 ($tmp, $rem) = $class -> _div($tmp, $chunk);
1534 83 $str = sprintf("%024b", $class -> _num($rem)) . $str;
1535 }
1536 369 ▶ unless ($class -> _is_zero($tmp)) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($class->_is_zero($tmp)) 318 51 369
1537 318 $str = sprintf("%b", $class -> _num($tmp)) . $str;
1538 }
1539 369 ▶ return length($str) ? $str : '0';
Branch: 1 branch
BranchTrueFalseTotal
length $str ? : 318 51 369
1540 }
1541  
1542 48 ▶sub _to_oct {
Subroutine _to_oct: called CC=4 SCAR=13.9
1543 # convert the number to a string of octal digits without prefix
1544 48 my ($class, $x) = @_;
1545 48 my $str = '';
1546 48 my $tmp = $class -> _copy($x);
1547 48 my $chunk = $class -> _new("16777216"); # 2^24 = 8 octal digits
1548 48 my $rem;
1549 48 ▶ until ($class -> _acmp($tmp, $chunk) < 0) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($class->_acmp($tmp, $chunk) < 0) 24 48 72
1550 24 ($tmp, $rem) = $class -> _div($tmp, $chunk);
1551 24 $str = sprintf("%08o", $class -> _num($rem)) . $str;
1552 }
1553 48 ▶ unless ($class -> _is_zero($tmp)) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($class->_is_zero($tmp)) 40 8 48
1554 40 $str = sprintf("%o", $class -> _num($tmp)) . $str;
1555 }
1556 48 ▶ return length($str) ? $str : '0';
Branch: 1 branch
BranchTrueFalseTotal
length $str ? : 40 8 48
1557 }
1558  
1559 40 ▶sub _to_hex {
Subroutine _to_hex: called CC=4 SCAR=13.9
1560 # convert the number to a string of hexadecimal digits without prefix
1561 40 my ($class, $x) = @_;
1562 40 my $str = '';
1563 40 my $tmp = $class -> _copy($x);
1564 40 my $chunk = $class -> _new("16777216"); # 2^24 = 6 hexadecimal digits
1565 40 my $rem;
1566 40 ▶ until ($class -> _acmp($tmp, $chunk) < 0) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($class->_acmp($tmp, $chunk) < 0) 16 40 56
1567 16 ($tmp, $rem) = $class -> _div($tmp, $chunk);
1568 16 $str = sprintf("%06x", $class -> _num($rem)) . $str;
1569 }
1570 40 ▶ unless ($class -> _is_zero($tmp)) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($class->_is_zero($tmp)) 32 8 40
1571 32 $str = sprintf("%x", $class -> _num($tmp)) . $str;
1572 }
1573 40 ▶ return length($str) ? $str : '0';
Branch: 1 branch
BranchTrueFalseTotal
length $str ? : 32 8 40
1574 }
1575  
1576 0 ▶sub _as_bin {
Subroutine _as_bin: not called CC=1 SCAR=6.9
1577 # convert the number to a string of binary digits with prefix
1578 0 my ($class, $x) = @_;
1579 0 return '0b' . $class -> _to_bin($x);
1580 }
1581  
1582 0 ▶sub _as_oct {
Subroutine _as_oct: not called CC=1 SCAR=6.9
1583 # convert the number to a string of octal digits with prefix
1584 0 my ($class, $x) = @_;
1585 0 return '0' . $class -> _to_oct($x); # yes, 0 becomes "00"
1586 }
1587  
1588 0 ▶sub _as_hex {
Subroutine _as_hex: not called CC=1 SCAR=6.9
1589 # convert the number to a string of hexadecimal digits with prefix
1590 0 my ($class, $x) = @_;
1591 0 return '0x' . $class -> _to_hex($x);
1592 }
1593  
1594 0 ▶sub _to_bytes {
Subroutine _to_bytes: not called CC=3 SCAR=24.8
1595 # convert the number to a string of bytes
1596 0 my ($class, $x) = @_;
1597 0 my $str = '';
1598 0 my $tmp = $class -> _copy($x);
1599 0 my $chunk = $class -> _new("65536");
1600 0 my $rem;
1601 0 ▶ until ($class -> _is_zero($tmp)) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($class->_is_zero($tmp)) 0 0 0
1602 0 ($tmp, $rem) = $class -> _div($tmp, $chunk);
1603 0 $str = pack('n', $class -> _num($rem)) . $str;
1604 }
1605 0 $str =~ s/^\0+//;
1606 0 ▶ return length($str) ? $str : "\x00";
Branch: 1 branch
BranchTrueFalseTotal
length $str ? : 0 0 0
1607 }
1608  
1609 50 *_as_bytes = \&_to_bytes;
1610  
1611 0 ▶sub _to_base {
Subroutine _to_base: not called CC=9 SCAR=45.0
1612 # convert the number to a string of digits in various bases
1613 0 my $class = shift;
1614 0 my $x = shift;
1615 0 my $base = shift;
1616 0 ▶ $base = $class -> _new($base) unless ref($base);
Branch: 1 branch
BranchTrueFalseTotal
unless ref $base 0 0 0
1617  
1618 0 my $collseq;
1619 0 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) { } 0 0 0
1620 0 $collseq = shift;
1621 0 ▶ croak "The collation sequence must be a non-empty string"
Branch: 1 branch
BranchTrueFalseTotal
unless defined $collseq and length $collseq 0 0 0
Condition0%0/3
defined $collseq and length $collseq
!ll&&!rl&&r
000
MC/DC0%0/2
defined $collseq and length $collseq
defined $collseq length $collseq
Truth table0%0/3
A and B
A= defined $collseqB= length $collseq
ABresult
0X0
100
111
1622 unless defined($collseq) && length($collseq);
1623 } else {
1624 0 ▶ if ($class -> _acmp($base, $class -> _new("94")) <= 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($class->_acmp($base, $class->_new('94')) <= 0) { } 0 0 0
1625 0 $collseq = '0123456789' # 48 .. 57
1626 . 'ABCDEFGHIJKLMNOPQRSTUVWXYZ' # 65 .. 90
1627 . 'abcdefghijklmnopqrstuvwxyz' # 97 .. 122
1628 . '!"#$%&\'()*+,-./' # 33 .. 47
1629 . ':;<=>?@' # 58 .. 64
1630 . '[\\]^_`' # 91 .. 96
1631 . '{|}~'; # 123 .. 126
1632 } else {
1633 0 croak "When base > 94, a collation sequence must be given";
1634 }
1635 }
1636  
1637 0 my @collseq = split '', $collseq;
1638  
1639 0 my $str = '';
1640 0 my $tmp = $class -> _copy($x);
1641 0 my $rem;
1642 0 ▶ until ($class -> _is_zero($tmp)) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($class->_is_zero($tmp)) 0 0 0
1643 0 ($tmp, $rem) = $class -> _div($tmp, $base);
1644 0 my $num = $class -> _num($rem);
1645 0 ▶ croak "no character to represent '$num' in collation sequence",
Branch: 1 branch
BranchTrueFalseTotal
if $num > $#collseq 0 0 0
1646 " (collation sequence is too short)" if $num > $#collseq;
1647 0 my $chr = $collseq[$num];
1648 0 $str = $chr . $str;
1649 }
1650 0 ▶ return $collseq[0] unless length $str;
Branch: 1 branch
BranchTrueFalseTotal
unless length $str 0 0 0
1651 0 return $str;
1652 }
1653  
1654 0 ▶sub _to_base_num {
Subroutine _to_base_num: not called CC=6 SCAR=37.4
1655 # Convert the number to an array of integers in any base.
1656 0 my ($class, $x, $base) = @_;
1657  
1658 # Make sure the base is an object and >= 2.
1659 0 ▶ $base = $class -> _new($base) unless ref($base);
Branch: 1 branch
BranchTrueFalseTotal
unless ref $base 0 0 0
1660 0 my $two = $class -> _two();
1661 0 ▶ croak "base must be >= 2" unless $class -> _acmp($base, $two) >= 0;
Branch: 1 branch
BranchTrueFalseTotal
unless $class->_acmp($base, $two) >= 0 0 0 0
1662  
1663 0 my $out = [];
1664 0 my $xcopy = $class -> _copy($x);
1665 0 my $rem;
1666  
1667 # Do all except the last (most significant) element.
1668 0 ▶ until ($class -> _acmp($xcopy, $base) < 0) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($class->_acmp($xcopy, $base) < 0) 0 0 0
1669 0 ($xcopy, $rem) = $class -> _div($xcopy, $base);
1670 0 unshift @$out, $rem;
1671 }
1672  
1673 # Do the last (most significant element).
1674 0 ▶ unless ($class -> _is_zero($xcopy)) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($class->_is_zero($xcopy)) 0 0 0
1675 0 unshift @$out, $xcopy;
1676 }
1677  
1678 # $out is empty if $x is zero.
1679 0 ▶ unshift @$out, $class -> _zero() unless @$out;
Branch: 1 branch
BranchTrueFalseTotal
unless @$out 0 0 0
1680  
1681 0 return $out;
1682 }
1683  
1684 0 ▶sub _from_hex {
Subroutine _from_hex: not called CC=3 SCAR=24.8
1685 # Convert a string of hexadecimal digits to a number.
1686  
1687 0 my ($class, $hex) = @_;
1688 0 $hex =~ s/^0[xX]//;
1689  
1690 # Find the largest number of hexadecimal digits that we can safely use with
1691 # 32 bit integers. There are 4 bits pr hexadecimal digit, and we use only
1692 # 31 bits to play safe. This gives us int(31 / 4) = 7.
1693  
1694 0 my $len = length $hex;
1695 0 my $rem = 1 + ($len - 1) % 7;
1696  
1697 # Do the first chunk.
1698  
1699 0 my $ret = $class -> _new(int hex substr $hex, 0, $rem);
1700 0 ▶ return $ret if $rem == $len;
Branch: 1 branch
BranchTrueFalseTotal
if $rem == $len 0 0 0
1701  
1702 # Do the remaining chunks, if any.
1703  
1704 0 my $shift = $class -> _new(1 << (4 * 7));
1705 0 ▶ for (my $offset = $rem ; $offset < $len ; $offset += 7) {
Branch: 1 branch
BranchTrueFalseTotal
if ($offset < $len) 0 0 0
1706 0 my $part = int hex substr $hex, $offset, 7;
1707 0 $ret = $class -> _mul($ret, $shift);
1708 0 $ret = $class -> _add($ret, $class -> _new($part));
1709 }
1710  
1711 0 return $ret;
1712 }
1713  
1714 0 ▶sub _from_oct {
Subroutine _from_oct: not called CC=3 SCAR=24.8
1715 # Convert a string of octal digits to a number.
1716  
1717 0 my ($class, $oct) = @_;
1718  
1719 # Find the largest number of octal digits that we can safely use with 32
1720 # bit integers. There are 3 bits pr octal digit, and we use only 31 bits to
1721 # play safe. This gives us int(31 / 3) = 10.
1722  
1723 0 my $len = length $oct;
1724 0 my $rem = 1 + ($len - 1) % 10;
1725  
1726 # Do the first chunk.
1727  
1728 0 my $ret = $class -> _new(int oct substr $oct, 0, $rem);
1729 0 ▶ return $ret if $rem == $len;
Branch: 1 branch
BranchTrueFalseTotal
if $rem == $len 0 0 0
1730  
1731 # Do the remaining chunks, if any.
1732  
1733 0 my $shift = $class -> _new(1 << (3 * 10));
1734 0 ▶ for (my $offset = $rem ; $offset < $len ; $offset += 10) {
Branch: 1 branch
BranchTrueFalseTotal
if ($offset < $len) 0 0 0
1735 0 my $part = int oct substr $oct, $offset, 10;
1736 0 $ret = $class -> _mul($ret, $shift);
1737 0 $ret = $class -> _add($ret, $class -> _new($part));
1738 }
1739  
1740 0 return $ret;
1741 }
1742  
1743 0 ▶sub _from_bin {
Subroutine _from_bin: not called CC=3 SCAR=24.8
1744 # Convert a string of binary digits to a number.
1745  
1746 0 my ($class, $bin) = @_;
1747 0 $bin =~ s/^0[bB]//;
1748  
1749 # The largest number of binary digits that we can safely use with 32 bit
1750 # integers is 31. We use only 31 bits to play safe.
1751  
1752 0 my $len = length $bin;
1753 0 my $rem = 1 + ($len - 1) % 31;
1754  
1755 # Do the first chunk.
1756  
1757 0 my $ret = $class -> _new(int oct '0b' . substr $bin, 0, $rem);
1758 0 ▶ return $ret if $rem == $len;
Branch: 1 branch
BranchTrueFalseTotal
if $rem == $len 0 0 0
1759  
1760 # Do the remaining chunks, if any.
1761  
1762 0 my $shift = $class -> _new(1 << 31);
1763 0 ▶ for (my $offset = $rem ; $offset < $len ; $offset += 31) {
Branch: 1 branch
BranchTrueFalseTotal
if ($offset < $len) 0 0 0
1764 0 my $part = int oct '0b' . substr $bin, $offset, 31;
1765 0 $ret = $class -> _mul($ret, $shift);
1766 0 $ret = $class -> _add($ret, $class -> _new($part));
1767 }
1768  
1769 0 return $ret;
1770 }
1771  
1772 0 ▶sub _from_bytes {
Subroutine _from_bytes: not called CC=2 SCAR=17.9
1773 # convert string of bytes to a number
1774 0 my ($class, $str) = @_;
1775 0 my $x = $class -> _zero();
1776 0 my $base = $class -> _new("256");
1777 0 my $n = length($str);
1778 0 ▶ for (my $i = 0 ; $i < $n ; ++$i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $n) 0 0 0
1779 0 $x = $class -> _mul($x, $base);
1780 0 my $byteval = $class -> _new(unpack 'C', substr($str, $i, 1));
1781 0 $x = $class -> _add($x, $byteval);
1782 }
1783 0 return $x;
1784 }
1785  
1786 0 ▶sub _from_base {
Subroutine _from_base: not called CC=9 SCAR=45.0
1787 # convert a string to a decimal number
1788 0 my $class = shift;
1789 0 my $str = shift;
1790 0 my $base = shift;
1791 0 ▶ $base = $class -> _new($base) unless ref($base);
Branch: 1 branch
BranchTrueFalseTotal
unless ref $base 0 0 0
1792  
1793 0 my $n = length($str);
1794 0 my $x = $class -> _zero();
1795  
1796 0 my $collseq;
1797 0 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) { } 0 0 0
1798 0 $collseq = shift();
1799 } else {
1800 0 ▶ if ($class -> _acmp($base, $class -> _new("36")) <= 0) {
Branch: 2 branches
BranchTrueFalseTotal
if ($class->_acmp($base, $class->_new('36')) <= 0) { } 0 0 0
elsif ($class->_acmp($base, $class->_new('94')) <= 0) { } 0 0 0
1801 0 $str = uc $str;
1802 0 $collseq = '0123456789' . 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
1803 } elsif ($class -> _acmp($base, $class -> _new("94")) <= 0) {
1804 0 $collseq = '0123456789' # 48 .. 57
1805 . 'ABCDEFGHIJKLMNOPQRSTUVWXYZ' # 65 .. 90
1806 . 'abcdefghijklmnopqrstuvwxyz' # 97 .. 122
1807 . '!"#$%&\'()*+,-./' # 33 .. 47
1808 . ':;<=>?@' # 58 .. 64
1809 . '[\\]^_`' # 91 .. 96
1810 . '{|}~'; # 123 .. 126
1811 } else {
1812 0 croak "When base > 94, a collation sequence must be given";
1813 }
1814 0 $collseq = substr $collseq, 0, $class -> _num($base);
1815 }
1816  
1817 # Create a mapping from each character in the collation sequence to the
1818 # corresponding integer. Check for duplicates in the collation sequence.
1819  
1820 0 my @collseq = split '', $collseq;
1821 0 my %collseq;
1822 0 for my $num (0 .. $#collseq) {
1823 0 my $chr = $collseq[$num];
1824 die "duplicate character '$chr' in collation sequence"
1825 0 ▶ if exists $collseq{$chr};
Branch: 1 branch
BranchTrueFalseTotal
if exists $collseq{$chr} 0 0 0
1826 0 $collseq{$chr} = $num;
1827 }
1828  
1829 0 ▶ for (my $i = 0 ; $i < $n ; ++$i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $n) 0 0 0
1830 0 my $chr = substr($str, $i, 1);
1831 die "input character '$chr' does not exist in collation sequence"
1832 0 ▶ unless exists $collseq{$chr};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $collseq{$chr} 0 0 0
1833 0 $x = $class -> _mul($x, $base);
1834 0 my $num = $class -> _new($collseq{$chr});
1835 0 $x = $class -> _add($x, $num);
1836 }
1837  
1838 0 return $x;
1839 }
1840  
1841 0 ▶sub _from_base_num {
Subroutine _from_base_num: not called CC=6 SCAR=37.4
1842 # Convert an array in the given base to a number.
1843 0 my ($class, $in, $base) = @_;
1844  
1845 # Make sure the base is an object and >= 2.
1846 0 ▶ $base = $class -> _new($base) unless ref($base);
Branch: 1 branch
BranchTrueFalseTotal
unless ref $base 0 0 0
1847 0 my $two = $class -> _two();
1848 0 ▶ croak "base must be >= 2" unless $class -> _acmp($base, $two) >= 0;
Branch: 1 branch
BranchTrueFalseTotal
unless $class->_acmp($base, $two) >= 0 0 0 0
1849  
1850 # @$in = map { ref($_) ? $_ : $class -> _new($_) } @$in;
1851  
1852 0 my $ele = $in -> [0];
1853  
1854 0 ▶ $ele = $class -> _new($ele) unless ref($ele);
Branch: 1 branch
BranchTrueFalseTotal
unless ref $ele 0 0 0
1855 0 my $x = $class -> _copy($ele);
1856  
1857 0 for my $i (1 .. $#$in) {
1858 0 $x = $class -> _mul($x, $base);
1859 0 $ele = $in -> [$i];
1860 0 ▶ $ele = $class -> _new($ele) unless ref($ele);
Branch: 1 branch
BranchTrueFalseTotal
unless ref $ele 0 0 0
1861 0 $x = $class -> _add($x, $ele);
1862 }
1863  
1864 0 return $x;
1865 }
1866  
1867 ##############################################################################
1868 # special modulus functions
1869  
1870 0 ▶sub _modinv {
Subroutine _modinv: not called CC=6 SCAR=37.4
1871 # modular multiplicative inverse
1872 0 my ($class, $x, $y) = @_;
1873  
1874 # modulo zero
1875 0 ▶ if ($class -> _is_zero($y)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($class->_is_zero($y)) 0 0 0
1876 0 return;
1877 }
1878  
1879 # modulo one
1880 0 ▶ if ($class -> _is_one($y)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($class->_is_one($y)) 0 0 0
1881 0 return ($class -> _zero(), '+');
1882 }
1883  
1884 0 my $u = $class -> _zero();
1885 0 my $v = $class -> _one();
1886 0 my $a = $class -> _copy($y);
1887 0 my $b = $class -> _copy($x);
1888  
1889 # Euclid's Algorithm for bgcd().
1890  
1891 0 my $q;
1892 0 my $sign = 1;
1893 {
1894 0 ($a, $q, $b) = ($b, $class -> _div($a, $b));
1895 0 ▶ last if $class -> _is_zero($b);
Branch: 1 branch
BranchTrueFalseTotal
if $class->_is_zero($b) 0 0 0
1896  
1897 0 my $vq = $class -> _mul($class -> _copy($v), $q);
1898 0 my $t = $class -> _add($vq, $u);
1899 0 $u = $v;
1900 0 $v = $t;
1901 0 $sign = -$sign;
1902 0 redo;
1903 }
1904  
1905 # if the gcd is not 1, there exists no modular multiplicative inverse
1906 0 ▶ return unless $class -> _is_one($a);
Branch: 1 branch
BranchTrueFalseTotal
unless $class->_is_one($a) 0 0 0
1907  
1908 0 ▶ ($v, $sign == 1 ? '+' : '-');
Branch: 1 branch
BranchTrueFalseTotal
$sign == 1 ? : 0 0 0
1909 }
1910  
1911 0 ▶sub _modpow {
Subroutine _modpow: not called CC=6 SCAR=37.4
1912 # modulus of power ($x ** $y) % $z
1913 0 my ($class, $num, $exp, $mod) = @_;
1914  
1915 # a^b (mod 1) = 0 for all a and b
1916 0 ▶ if ($class -> _is_one($mod)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($class->_is_one($mod)) 0 0 0
1917 0 return $class -> _zero();
1918 }
1919  
1920 # 0^a (mod m) = 0 if m != 0, a != 0
1921 # 0^0 (mod m) = 1 if m != 0
1922 0 ▶ if ($class -> _is_zero($num)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($class->_is_zero($num)) 0 0 0
1923 0 ▶ return $class -> _is_zero($exp) ? $class -> _one()
Branch: 1 branch
BranchTrueFalseTotal
$class->_is_zero($exp) ? : 0 0 0
1924 : $class -> _zero();
1925 }
1926  
1927 # We could do the following, but it doesn't actually save any time. The
1928 # _copy() is needed in case $num and $mod are the same object.
1929  
1930 0 $num = $class -> _mod($class -> _copy($num), $mod);
1931  
1932 0 my $acc = $class -> _copy($num);
1933 0 my $t = $class -> _one();
1934  
1935 0 my $expbin = $class -> _to_bin($exp);
1936 0 my $len = length($expbin);
1937  
1938 0 ▶ while ($len--) {
Branch: 1 branch
BranchTrueFalseTotal
if ($len--) 0 0 0
1939 0 ▶ if (substr($expbin, $len, 1) eq '1') { # if odd
Branch: 1 branch
BranchTrueFalseTotal
if (substr($expbin, $len, 1) eq 1) 0 0 0
1940 0 $t = $class -> _mul($t, $acc);
1941 0 $t = $class -> _mod($t, $mod);
1942 }
1943 0 $acc = $class -> _mul($acc, $acc);
1944 0 $acc = $class -> _mod($acc, $mod);
1945 }
1946 0 return $t;
1947 }
1948  
1949 0 ▶sub _gcd {
Subroutine _gcd: not called CC=6 SCAR=37.4
1950 # Greatest common divisor.
1951  
1952 0 my ($class, $x, $y) = @_;
1953  
1954 # gcd(0, 0) = 0
1955 # gcd(0, a) = a, if a != 0
1956  
1957 0 ▶ if ($class -> _acmp($x, $y) == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($class->_acmp($x, $y) == 0) 0 0 0
1958 0 return $class -> _copy($x);
1959 }
1960  
1961 0 ▶ if ($class -> _is_zero($x)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($class->_is_zero($x)) { } 0 0 0
1962 0 ▶ if ($class -> _is_zero($y)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($class->_is_zero($y)) { } 0 0 0
1963 0 return $class -> _zero();
1964 } else {
1965 0 return $class -> _copy($y);
1966 }
1967 } else {
1968 0 ▶ if ($class -> _is_zero($y)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($class->_is_zero($y)) { } 0 0 0
1969 0 return $class -> _copy($x);
1970 } else {
1971  
1972 # Until $y is zero ...
1973  
1974 0 $x = $class -> _copy($x);
1975 0 ▶ until ($class -> _is_zero($y)) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($class->_is_zero($y)) 0 0 0
1976  
1977 # Compute remainder.
1978  
1979 0 $x = $class -> _mod($x, $y);
1980  
1981 # Swap $x and $y.
1982  
1983 0 my $tmp = $x;
1984 0 $x = $class -> _copy($y);
1985 0 $y = $tmp;
1986 }
1987  
1988 0 return $x;
1989 }
1990 }
1991 }
1992  
1993 14 ▶sub _lcm {
Subroutine _lcm: called CC=3 SCAR=11.6
1994 # Least common multiple.
1995  
1996 14 my ($class, $x, $y) = @_;
1997  
1998 # lcm(0, x) = 0 for all x
1999  
2000 14 ▶ return $class -> _zero()
Branch: 1 branch
BranchTrueFalseTotal
if $class->_is_zero($x) or $class->_is_zero($y) 0 14 14
Condition33%1/3
$class->_is_zero($x) or $class->_is_zero($y)
l!l&&r!l&&!r
0014
MC/DC0%0/2
$class->_is_zero($x) or $class->_is_zero($y)
$class->_is_zero($x) $class->_is_zero($y)
Truth table33%1/3
A or B
A= $class->_is_zero($x)B= $class->_is_zero($y)
ABresult
1X1
011
000
2001 if ($class -> _is_zero($x) ||
2002 $class -> _is_zero($y));
2003  
2004 14 my $gcd = $class -> _gcd($class -> _copy($x), $y);
2005 14 $x = $class -> _div($x, $gcd);
2006 14 $x = $class -> _mul($x, $y);
2007 14 return $x;
2008 }
2009  
2010 0 ▶sub _lucas {
Subroutine _lucas: not called CC=7 SCAR=40.3
2011 0 my ($class, $n) = @_;
2012  
2013 0 ▶ $n = $class -> _num($n) if ref $n;
Branch: 1 branch
BranchTrueFalseTotal
if ref $n 0 0 0
2014  
2015 # In list context, use lucas(n) = lucas(n-1) + lucas(n-2)
2016  
2017 0 ▶ if (wantarray) {
Branch: 1 branch
BranchTrueFalseTotal
if (wantarray) 0 0 0
2018 0 my @y;
2019  
2020 0 push @y, $class -> _two();
2021 0 ▶ return @y if $n == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $n == 0 0 0 0
2022  
2023 0 push @y, $class -> _one();
2024 0 ▶ return @y if $n == 1;
Branch: 1 branch
BranchTrueFalseTotal
if $n == 1 0 0 0
2025  
2026 0 ▶ for (my $i = 2 ; $i <= $n ; ++ $i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i <= $n) 0 0 0
2027 0 $y[$i] = $class -> _add($class -> _copy($y[$i - 1]), $y[$i - 2]);
2028 }
2029  
2030 0 return @y;
2031 }
2032  
2033 # In scalar context use that lucas(n) = fib(n-1) + fib(n+1).
2034 #
2035 # Remember that _fib() behaves differently in scalar context and list
2036 # context, so we must add scalar() to get the desired behaviour.
2037  
2038 0 ▶ return $class -> _two() if $n == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $n == 0 0 0 0
2039  
2040 0 return $class -> _add(scalar($class -> _fib($n - 1)),
2041 scalar($class -> _fib($n + 1)));
2042 }
2043  
2044 0 ▶sub _fib {
Subroutine _fib: not called CC=6 SCAR=37.4
2045 0 my ($class, $n) = @_;
2046  
2047 0 ▶ $n = $class -> _num($n) if ref $n;
Branch: 1 branch
BranchTrueFalseTotal
if ref $n 0 0 0
2048  
2049 # In list context, use fib(n) = fib(n-1) + fib(n-2)
2050  
2051 0 ▶ if (wantarray) {
Branch: 1 branch
BranchTrueFalseTotal
if (wantarray) 0 0 0
2052 0 my @y;
2053  
2054 0 push @y, $class -> _zero();
2055 0 ▶ return @y if $n == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $n == 0 0 0 0
2056  
2057 0 push @y, $class -> _one();
2058 0 ▶ return @y if $n == 1;
Branch: 1 branch
BranchTrueFalseTotal
if $n == 1 0 0 0
2059  
2060 0 ▶ for (my $i = 2 ; $i <= $n ; ++ $i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i <= $n) 0 0 0
2061 0 $y[$i] = $class -> _add($class -> _copy($y[$i - 1]), $y[$i - 2]);
2062 }
2063  
2064 0 return @y;
2065 }
2066  
2067 # In scalar context use a fast algorithm that is much faster than the
2068 # recursive algorith used in list context.
2069  
2070 0 my $cache = {};
2071 0 my $two = $class -> _two();
2072 0 my $fib;
2073  
2074 0 ▶ $fib = sub {
Subroutine __ANON__: not called CC=5 SCAR=34.0
2075 0 my $n = shift;
2076 0 ▶ return $class -> _zero() if $n <= 0;
Branch: 1 branch
BranchTrueFalseTotal
if $n <= 0 0 0 0
2077 0 ▶ return $class -> _one() if $n <= 2;
Branch: 1 branch
BranchTrueFalseTotal
if $n <= 2 0 0 0
2078 0 ▶ return $cache -> {$n} if exists $cache -> {$n};
Branch: 1 branch
BranchTrueFalseTotal
if exists $cache->{$n} 0 0 0
2079  
2080 0 my $k = int($n / 2);
2081 0 my $a = $fib -> ($k + 1);
2082 0 my $b = $fib -> ($k);
2083 0 my $y;
2084  
2085 0 ▶ if ($n % 2 == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($n % 2 == 1) { } 0 0 0
2086 # a*a + b*b
2087 0 $y = $class -> _add($class -> _mul($class -> _copy($a), $a),
2088 $class -> _mul($class -> _copy($b), $b));
2089 } else {
2090 # (2*a - b)*b
2091 0 $y = $class -> _mul($class -> _sub($class -> _mul(
2092 $class -> _copy($two), $a), $b), $b);
2093 }
2094  
2095 0 $cache -> {$n} = $y;
2096 0 return $y;
2097 0 };
2098  
2099 0 return $fib -> ($n);
2100 }
2101  
2102 ##############################################################################
2103 ##############################################################################
2104  
2105 50 1;
2106  
2107 __END__
« BigInt.pm ↑ Summary