BigInt/Lib.pm

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