lib/Math/BigInt/Pari.pm

filter: Statementstmt 96.4% 163 / 169 Branchbran 90.2% 65 / 72 Conditioncond 100% 2 / 2 MC/DCmcdc 100% 1 / 1 Subroutinesub 95.0% 57 / 60 Podpod n/a totaltotal 94.7% 288 / 304 SCAR 36.4
CC
38
Cov
95%
_modpow
7.0
_to_bytes
6.0
_mp2os
6.0
CRAP 38.2
« blib/lib/Math/BigInt/Pari.pm ↑ Summary
1 package Math::BigInt::Pari;
2  
3 8 ▶use 5.006002;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 8 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 8 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 8 ▶use Math::BigInt::Lib 1.999801;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 8 our @ISA = qw< Math::BigInt::Lib >;
10  
11 8 our $VERSION = '1.3012';
12  
13 8 use Math::Pari qw(PARI pari2pv gdivent bittest
14 gcmp gcmp0 gcmp1 gcd ifact gpui gmul
15 binomial lcm
16 8 ▶ );
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 # MBI will call this, so catch it and throw it away
19 ▶sub import { }
Subroutine import: called CC=1 SCAR=0.0
20 sub api_version() { 2; } # we are compatible with MBI v1.83 and up
21  
22 8 my $zero = PARI(0); # for _copy
23 8 my $one = PARI(1); # for _inc and _dec
24 8 my $two = PARI(2); # for _is_two
25 8 my $ten = PARI(10); # for _digit
26  
27 sub _new {
28 # the . '' is because new($2) will give a magical scalar to us, and PARI
29 # does not like this at all :/
30 # use Devel::Peek; print Dump($_[1]);
31 48957 ▶ PARI($_[1] . '')
Subroutine _new: called CC=1 SCAR=0.0
32 }
33  
34 sub _from_hex {
35 321 ▶ my $hex = $_[1];
Subroutine _from_hex: called CC=2 SCAR=7.1
36 321 ▶ $hex = '0x' . $hex unless substr($hex, 0, 2) eq '0x';
Branch: 1 branch
BranchTrueFalseTotal
unless substr($hex, 0, 2) eq "0x" 0 321 321
37 321 Math::Pari::_hex_cvt($hex);
38 }
39  
40 sub _from_oct {
41 6 ▶ Math::Pari::_hex_cvt('0' . $_[1]);
Subroutine _from_oct: called CC=1 SCAR=0.0
42 }
43  
44 sub _from_bin {
45 68 ▶ my $bin = $_[1];
Subroutine _from_bin: called CC=2 SCAR=7.1
46 68 ▶ $bin = '0b' . $bin unless substr($bin, 0, 2) eq '0b';
Branch: 1 branch
BranchTrueFalseTotal
unless substr($bin, 0, 2) eq "0b" 0 68 68
47 68 Math::Pari::_hex_cvt($bin);
48 }
49  
50 sub _as_bin {
51 17 ▶ my $v = unpack('B*', _mp2os($_[1]));
Subroutine _as_bin: called CC=2 SCAR=6.9
52 17 ▶ return "0b0" if $v eq '';
Branch: 1 branch
BranchTrueFalseTotal
if $v eq "" 3 14 17
53 14 $v =~ s/^0*/0b/;
54 14 $v;
55 }
56  
57 sub _as_oct {
58 15 ▶ my $v = _mp2oct($_[1]);
Subroutine _as_oct: called CC=2 SCAR=6.9
59 15 ▶ return "00" if $v eq '';
Branch: 1 branch
BranchTrueFalseTotal
if $v eq "" 3 12 15
60 12 $v =~ s/^0*/0/;
61 12 $v;
62 }
63  
64 sub _as_hex {
65 75 ▶ my $v = unpack('H*', _mp2os($_[1]));
Subroutine _as_hex: called CC=2 SCAR=6.9
66 75 ▶ return "0x0" if $v eq '';
Branch: 1 branch
BranchTrueFalseTotal
if $v eq "" 13 62 75
67 62 $v =~ s/^0*/0x/;
68 62 $v;
69 }
70  
71 sub _to_bin {
72 83 ▶ my $v = unpack('B*', _mp2os($_[1]));
Subroutine _to_bin: called CC=2 SCAR=6.9
73 83 $v =~ s/^0+//;
74 83 ▶ return "0" if $v eq '';
Branch: 1 branch
BranchTrueFalseTotal
if $v eq "" 10 73 83
75 73 $v;
76 }
77  
78 sub _to_oct {
79 12 ▶ my $v = _mp2oct($_[1]);
Subroutine _to_oct: called CC=2 SCAR=6.9
80 12 $v =~ s/^0+//;
81 12 ▶ return "0" if $v eq '';
Branch: 1 branch
BranchTrueFalseTotal
if $v eq "" 2 10 12
82 10 $v;
83 }
84  
85 sub _to_hex {
86 10 ▶ my $v = unpack('H*', _mp2os($_[1]));
Subroutine _to_hex: called CC=2 SCAR=6.9
87 10 $v =~ s/^0+//;
88 10 ▶ return "0" if $v eq '';
Branch: 1 branch
BranchTrueFalseTotal
if $v eq "" 2 8 10
89 8 $v;
90 }
91  
92 sub _to_bytes {
93 0 ▶ my $bytes = _mp2os($_[1]);
Subroutine _to_bytes: not called CC=2 SCAR=17.9
94 0 ▶ return $bytes eq '' ? "\x00" : $bytes;
Branch: 1 branch
BranchTrueFalseTotal
$bytes eq '' ? : 0 0 0
95 }
96  
97 sub _mp2os {
98 185 ▶ my($p) = @_;
Subroutine _mp2os: called CC=6 SCAR=17.9
99 185 $p = PARI($p);
100 185 my $base = PARI(1) << PARI(4*8);
101 185 my $res = '';
102 185 ▶ while ($p != 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($p != 0) 184 185 369
103 184 my $r = $p % $base;
104 184 $p = ($p - $r) / $base;
105 184 my $buf = pack 'V', $r;
106 184 ▶ if ($p == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($p == 0) 157 27 184
107 157 ▶ $buf = $r >= 16777216 ? $buf
Branch: 3 branches
BranchTrueFalseTotal
$r >= 16777216 ? : 4 153 157
$r >= 65536 ? : 2 151 153
$r >= 256 ? : 5 146 151
108 : $r >= 65536 ? substr($buf, 0, 3)
109 : $r >= 256 ? substr($buf, 0, 2)
110 : substr($buf, 0, 1);
111 }
112 184 $res .= $buf;
113 }
114 185 scalar reverse $res;
115 }
116  
117 sub _mp2oct {
118 27 ▶ my($p) = @_;
Subroutine _mp2oct: called CC=2 SCAR=6.9
119 27 $p = PARI($p);
120 27 my $base = PARI(8);
121 27 my $res = '';
122 27 ▶ while ($p != 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($p != 0) 181 27 208
123 181 my $r = $p % $base;
124 181 $p = ($p - $r) / $base;
125 181 $res .= $r;
126 }
127 27 scalar reverse $res;
128 }
129  
130 11215 ▶sub _zero { PARI(0) }
Subroutine _zero: called CC=1 SCAR=0.0
131 767 ▶sub _one { PARI(1) }
Subroutine _one: called CC=1 SCAR=0.0
132 119 ▶sub _two { PARI(2) }
Subroutine _two: called CC=1 SCAR=0.0
133 2 ▶sub _ten { PARI(10) }
Subroutine _ten: called CC=1 SCAR=0.0
134  
135 5 ▶sub _1ex { gpui(PARI(10), $_[1]) }
Subroutine _1ex: called CC=1 SCAR=0.0
136  
137 35112 ▶sub _copy { $_[1] + $zero; }
Subroutine _copy: called CC=1 SCAR=0.0
138  
139 53308 ▶sub _str { pari2pv($_[1]) }
Subroutine _str: called CC=1 SCAR=0.0
140  
141 10086 ▶sub _num { 0 + pari2pv($_[1]) }
Subroutine _num: called CC=1 SCAR=0.0
142  
143 24443 ▶sub _add { $_[1] += $_[2] }
Subroutine _add: called CC=1 SCAR=0.0
144  
145 sub _sub {
146 24723 ▶ if ($_[3]) {
Subroutine _sub: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
if ($_[3]) 2506 22217 24723
147 2506 $_[2] = $_[1] - $_[2];
148 2506 return $_[2];
149 }
150 22217 $_[1] -= $_[2];
151 }
152  
153 11257 ▶sub _mul { $_[1] = gmul($_[1], $_[2]) }
Subroutine _mul: called CC=1 SCAR=0.0
154  
155 sub _div {
156 5095 ▶ if (wantarray) {
Subroutine _div: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
if (wantarray) 874 4221 5095
157 874 my $r = $_[1] % $_[2];
158 874 $_[1] = gdivent($_[1], $_[2]);
159 874 return ($_[1], $r);
160 }
161 4221 $_[1] = gdivent($_[1], $_[2]);
162 }
163  
164 722 ▶sub _mod { $_[1] %= $_[2]; }
Subroutine _mod: called CC=1 SCAR=0.0
165  
166 sub _nok {
167 16 ▶ my ($class, $n, $k) = @_;
Subroutine _nok: called CC=2 SCAR=7.2
168  
169 # Math::Pari doesn't seem to be able to handle the case when n is large and
170 # k is almost as big as n. For instance, the following returns zero (at
171 # least for certain versions and configurations):
172 #
173 # $n = PARI("10000000000000000000");
174 # $k = PARI("9999999999999999999");
175 # print Math::Pari::binomial($n, $k);'
176  
177 # If k > n/2, or, equivalently, 2*k > n, compute nok(n, k) as nok(n, n-k).
178  
179 {
180 16 my $twok = $class -> _mul($class -> _two(), $class -> _copy($k));
181 16 ▶ if ($class -> _acmp($twok, $n) > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($class->_acmp($twok, $n) > 0) 0 16 16
182 0 $k = $class -> _sub($class -> _copy($n), $k);
183 }
184 }
185  
186 16 binomial($n, $k);
187 }
188  
189 2344 ▶sub _inc { ++$_[1]; }
Subroutine _inc: called CC=1 SCAR=0.0
190 84 ▶sub _dec { --$_[1]; }
Subroutine _dec: called CC=1 SCAR=0.0
191  
192 36 ▶sub _and { $_[1] &= $_[2] }
Subroutine _and: called CC=1 SCAR=0.0
193  
194 53 ▶sub _xor { $_[1] ^= $_[2] }
Subroutine _xor: called CC=1 SCAR=0.0
195  
196 51 ▶sub _or { $_[1] |= $_[2] }
Subroutine _or: called CC=1 SCAR=0.0
197  
198 276 ▶sub _pow { gpui($_[1], $_[2]) }
Subroutine _pow: called CC=1 SCAR=0.0
199  
200 25 ▶sub _gcd { gcd($_[1], $_[2]) }
Subroutine _gcd: called CC=1 SCAR=0.0
201  
202 8 ▶sub _lcm { lcm($_[1], $_[2]) }
Subroutine _lcm: called CC=1 SCAR=0.0
203  
204 49207 ▶sub _len { length(pari2pv($_[1])) } # costly!
Subroutine _len: called CC=1 SCAR=0.0
205  
206 # XXX TODO: calc len in base 2 then appr. in base 10
207 0 ▶sub _alen { length(pari2pv($_[1])) }
Subroutine _alen: not called CC=1 SCAR=6.9
208  
209 sub _zeros {
210 36148 ▶ my ($class, $x) = @_;
Subroutine _zeros: called CC=2 SCAR=6.9
211 36148 ▶ return 0 if gcmp0($x); # 0 has no trailing zeros
Branch: 1 branch
BranchTrueFalseTotal
if &gcmp0($x) 617 35531 36148
212  
213 35531 my $u = $class -> _str($x);
214 35531 $u =~ /(0*)\z/;
215 35531 return length($1);
216  
217 #my $s = pari2pv($_[1]);
218 #my $i = length($s);
219 #my $zeros = 0;
220 #while (--$i >= 0) {
221 # substr($s, $i, 1) eq '0' ? $zeros ++ : last;
222 #}
223 #$zeros;
224 }
225  
226 sub _digit {
227 # if $n < 0, we need to count from left and thus can't use the other method:
228 27 ▶ if ($_[2] < 0) {
Subroutine _digit: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
if ($_[2] < 0) 10 17 27
229 10 return substr(pari2pv($_[1]), -1 - $_[2], 1);
230 }
231 # else this is faster (except for very short numbers)
232 # shift the number right by $n digits, then extract last digit via % 10
233 17 pari2pv(gdivent($_[1], $ten ** $_[2]) % $ten);
234 }
235  
236 117104 ▶sub _is_zero { gcmp0($_[1]) }
Subroutine _is_zero: called CC=1 SCAR=0.0
237  
238 1312 ▶sub _is_one { gcmp1($_[1]) }
Subroutine _is_one: called CC=1 SCAR=0.0
239  
240 58 ▶sub _is_two { gcmp($_[1], $two) ? 0 : 1 }
Subroutine _is_two: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
&gcmp($_[1], $two) ? : 35 23 58
241  
242 2 ▶sub _is_ten { gcmp($_[1], $ten) ? 0 : 1 }
Subroutine _is_ten: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
&gcmp($_[1], $ten) ? : 1 1 2
243  
244 23 ▶sub _is_even { bittest($_[1], 0) ? 0 : 1 }
Subroutine _is_even: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
&bittest($_[1], 0) ? : 12 11 23
245  
246 207 ▶sub _is_odd { bittest($_[1], 0) ? 1 : 0 }
Subroutine _is_odd: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
&bittest($_[1], 0) ? : 97 110 207
247  
248 sub _acmp {
249 30885 ▶ my $i = gcmp($_[1], $_[2]) || 0;
Subroutine _acmp: called CC=3 SCAR=11.0
Condition100%2/2
&gcmp($_[1], $_[2]) || 0
l!l
267194166
MC/DC100%1/1
&gcmp($_[1], $_[2]) || 0
&gcmp($_[1], $_[2])
Truth table100%2/2
A
A= &gcmp($_[1], $_[2])
Aresult
11
00
250 # work around bug in Pari (on 64bit systems?)
251 30885 ▶ $i = -1 if $i == 4294967295;
Branch: 1 branch
BranchTrueFalseTotal
if $i == 4294967295 2958 27927 30885
252 30885 $i;
253 }
254  
255 sub _check {
256 1830 ▶ my ($class, $x) = @_;
Subroutine _check: called CC=3 SCAR=11.0
257 1830 ▶ return "Undefined" unless defined $x;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $x 0 1830 1830
258 1830 ▶ return "$x is not a reference to Math::Pari"
Branch: 1 branch
BranchTrueFalseTotal
unless ref $x eq "Math::Pari" 1 1829 1830
259 unless ref($x) eq 'Math::Pari';
260 1829 return 0;
261 }
262  
263 sub _sqrt {
264 # square root of $x
265 139 ▶ my ($class, $x) = @_;
Subroutine _sqrt: called CC=2 SCAR=7.1
266 139 my $y = Math::Pari::sqrtint($x);
267 139 ▶ return $y * $y > $x ? $y - $one : $y; # bug in sqrtint()?
Branch: 1 branch
BranchTrueFalseTotal
$y * $y > $x ? : 0 139 139
268 }
269  
270 sub _root {
271 # n'th root
272  
273 # Native version:
274 47 ▶ return $_[1] = int(Math::Pari::sqrtn($_[1] + 0.5, $_[2]));
Subroutine _root: called CC=1 SCAR=0.0
275 }
276  
277 sub _modpow {
278 # modulus of power ($x ** $y) % $z
279 144 ▶ my ($c, $num, $exp, $mod) = @_;
Subroutine _modpow: called CC=7 SCAR=19.5
280  
281 # in the trivial case,
282 144 ▶ if (gcmp1($mod)) {
Branch: 1 branch
BranchTrueFalseTotal
if (&gcmp1($mod)) 50 94 144
283 50 $num = PARI(0);
284 50 return $num;
285 }
286  
287 94 ▶ if (gcmp1($num)) {
Branch: 1 branch
BranchTrueFalseTotal
if (&gcmp1($num)) 29 65 94
288 29 $num = PARI(1);
289 29 return $num;
290 }
291  
292 65 ▶ if (gcmp0($num)) {
Branch: 1 branch
BranchTrueFalseTotal
if (&gcmp0($num)) 12 53 65
293 12 ▶ if (gcmp0($exp)) {
Branch: 1 branch
BranchTrueFalseTotal
if (&gcmp0($exp)) { } 3 9 12
294 3 return PARI(1);
295 } else {
296 9 return PARI(0);
297 }
298 }
299  
300 53 my $acc = $c -> _copy($num);
301 53 my $t = $c -> _one();
302  
303 53 my $expbin = $c -> _to_bin($exp);
304 53 my $len = length($expbin);
305 53 ▶ while (--$len >= 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (--$len >= 0) 268 53 321
306 268 ▶ if (substr($expbin, $len, 1) eq '1') { # is_odd
Branch: 1 branch
BranchTrueFalseTotal
if (substr($expbin, $len, 1) eq 1) 141 127 268
307 141 $c -> _mul($t, $acc);
308 141 $t = $c -> _mod($t, $mod);
309 }
310 268 $c -> _mul($acc, $acc);
311 268 $acc = $c -> _mod($acc, $mod);
312 }
313 53 $num = $t;
314 53 $num;
315 }
316  
317 sub _rsft {
318 # (X, Y, N) = @_; means X >> Y in base N
319  
320 14684 ▶ if ($_[3] != 2) {
Subroutine _rsft: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
if ($_[3] != 2) 14669 15 14684
321 14669 return $_[1] = gdivent($_[1], PARI($_[3]) ** $_[2]);
322 }
323 15 $_[1] >>= $_[2];
324 }
325  
326 sub _lsft {
327 # (X, Y, N) = @_; means X >> Y in base N
328  
329 9189 ▶ if ($_[3] != 2) {
Subroutine _lsft: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
if ($_[3] != 2) 9171 18 9189
330 9171 return $_[1] *= PARI($_[3]) ** $_[2];
331 }
332 18 $_[1] <<= $_[2];
333 }
334  
335 sub _fac {
336 # factorial of argument
337 46 ▶ $_[1] = ifact($_[1]);
Subroutine _fac: called CC=1 SCAR=0.0
338 }
339  
340 # _set() - set an already existing object to the given scalar value
341  
342 sub _set {
343 0 ▶ my ($c, $x, $y) = @_;
Subroutine _set: not called CC=1 SCAR=6.9
344 0 *x = \PARI($y . '');
345 }
346  
347 8 1;
348  
349 __END__
« blib/lib/Math/BigInt/Pari.pm ↑ Summary