blib/lib/Math/Base/Convert/CalcPP.pm

filter: Statementstmt 95.5% 65 / 68 Branchbran 86.3% 19 / 22 Conditioncond 66.6% 4 / 6 MC/DCmcdc 50.0% 2 / 4 Subroutinesub 100% 8 / 8 Podpod 83.3% 5 / 6 totaltotal 90.3% 103 / 114 SCAR 28.4
CC
17
Cov
92%
useFROMbaseto32wide
6.0
multiply
4.0
dividebybase
4.0
CRAP 17.2
« blib/lib/Math/Base/Convert/Shortcuts.pm ↑ Summary blib/lib/Math/Base/Convert/Bases.pm »
1 #!/usr/bin/perl
2  
3 package Math::Base::Convert::CalcPP;
4  
5 20 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 20 ▶use vars qw($VERSION);
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 20 $VERSION = do { my @r = (q$Revision: 0.03 $ =~ /\d+/g); sprintf "%d."."%02d" x $#r, @r };
9  
10 # test number < 2^32 is NOT power of 2
11 #
12 sub isnotp2 {
13 62 ▶ my $ref = ref $_[0];
Subroutine isnotp2: called CC=3 SCAR=12.5
Pod undocumented
14 62 ▶ shift if ref $_[0] || $_[0] =~ /\D/; # class?
Branch: 1 branch
BranchTrueFalseTotal
if ref $_[0] or $_[0] =~ /\D/ 62 0 62
Condition33%1/3
ref $_[0] or $_[0] =~ /\D/
l!l&&r!l&&!r
6200
MC/DC0%0/2
ref $_[0] or $_[0] =~ /\D/
ref $_[0] $_[0] =~ /\D/
Truth table33%1/3
A or B
A= ref $_[0]B= $_[0] =~ /\D/
ABresult
1X1
011
000
15 62 $_[0] & $_[0] -1;
16 }
17  
18 # add a long n*32 bit number toa number < 65536
19 # add 'n' to array digits and propagate carry, return carry
20 #
21 sub addbaseno {
22 11483 ▶ my($ap,$n) = @_;
Subroutine addbaseno: called CC=3 SCAR=13.3
Pod documented
23 11483 foreach (@$ap) {
24 11483 $_ += $n;
25 11483 ▶ return 0 unless $_ > 0xffffffff;
Branch: 1 branch
BranchTrueFalseTotal
unless $_ > 4294967295 11483 0 11483
26 0 $n = 1;
27 0 $_ -= 4294967296;
28 }
29 0 1; # carry is one on exit, else would have taken return 0 branch
30 }
31  
32 # multiply a register of indeterminate length by a number < 65535
33 #
34 # ap pointer to multiplicand array
35 # multiplier
36 #
37 sub multiply {
38 11483 ▶ my($ap,$m) = @_;
Subroutine multiply: called CC=4 SCAR=13.9
Pod documented
39 # $m is always 2..65535
40 # $m &= 0xffff; # max value 65535 already done by VETTING
41 #
42 # perl uses doubles for arithmetic, $m << 65536 will fit
43 11483 my $carry = 0;
44 11483 foreach ( @$ap) {
45 41961 $_ *= $m;
46 41961 $_ += $carry;
47 41961 ▶ if ($_ > 0xffffffff) {
Branch: 1 branch
BranchTrueFalseTotal
if ($_ > 4294967295) { } 26967 14994 41961
48 26967 $carry = int($_ / 4294967296);
49 26967 $_ %= 4294967296;
50 } else {
51 14994 $carry = 0;
52 }
53 }
54 11483 ▶ push @$ap, $carry if $carry;
Branch: 1 branch
BranchTrueFalseTotal
if $carry 1073 10410 11483
55 }
56  
57 sub dividebybase {
58 12780 ▶ my($np,$divisor) = @_;
Subroutine dividebybase: called CC=4 SCAR=13.9
Pod documented
59 12780 my @dividend = @$np; # 3% improvement
60 12780 ▶ while ($#dividend) { # 3% improvement
Branch: 1 branch
BranchTrueFalseTotal
if ($#dividend) 10466 3422 13888
61 10466 ▶ last if $dividend[0];
Branch: 1 branch
BranchTrueFalseTotal
if $dividend[0] 9358 1108 10466
62 1108 shift @dividend;
63 }
64 12780 my $remainder = 0;
65 12780 my @quotient;
66 12780 ▶ while (@dividend) {
Branch: 1 branch
BranchTrueFalseTotal
if (@dividend) 46200 12780 58980
67 46200 my $work = ($dividend[0] += ($remainder * 4294967296));
68 46200 push @quotient, int($work / $divisor);
69 46200 $remainder = $work % $divisor;
70 46200 shift @dividend;
71 }
72 12780 return (\@quotient,$remainder);
73 }
74  
75 # simple versions of conversion, works for N < ~2^49 or 10^16
76 #
77 #sub frombase {
78 # my($hsh,$base,$str) = @_;
79 # my $number = 0;
80 # for( $str =~ /./g ) {
81 # $number *= $base;
82 # $number += $hsh->{$_};
83 # }
84 # return $number;
85 #}
86  
87 #sub tobase {
88 #sub to_base
89 # my($bp,$base,$num) = @_;
90 # my $base = shift;
91 # return $bp->[0] if $num == 0;
92 # my $str = '';
93 # while( $num > 0 ) {
94 # $str = $bp->[$num % $base] . $str;
95 # $num = int( $num / $base );
96 # }
97 # return $str;
98 #}
99  
100 # convert a number from its base to 32*N bit representation
101 #
102 sub useFROMbaseto32wide {
103 559 ▶ my $bc = shift;
Subroutine useFROMbaseto32wide: called CC=6 SCAR=17.9
Pod documented
104 559 my($ary,$hsh,$base,$str) = @{$bc}{qw(from fhsh fbase nstr)};
105 # check if decimal and interger from within perl's 32bit double representation
106 # cutoff is 999,999,999,999,999 -- a bit less than 2^50
107 #
108 # convert directly to base 2^32 arrays
109 #
110 559 my @result = (0);
111  
112 559 ▶ if ($base == 10 && length($str) < 16) {
Branch: 1 branch
BranchTrueFalseTotal
if ($base == 10 and length $str < 16) { } 35 524 559
Condition100%3/3
$base == 10 and length $str < 16
!ll&&!rl&&r
4537135
MC/DC100%2/2
$base == 10 and length $str < 16
$base == 10 length $str < 16
Truth table100%3/3
A and B
A= $base == 10B= length $str < 16
ABresult
0X0
100
111
113 # unless ($str > 999999999999999) { # maximum 32 bit double float integer representation
114 35 $result[0] = $str % 4294967296;
115 35 my $quotient = int($str / 4294967296);
116 35 ▶ $result[1] = $quotient if $quotient;
Branch: 1 branch
BranchTrueFalseTotal
if $quotient 9 26 35
117 35 $bc->{b32str} = \@result;
118 }
119 else {
120 524 for ($str =~ /./g) {
121 11483 multiply(\@result,$base);
122 11483 ▶ push @result, 1 if addbaseno(\@result,$hsh->{$_}); # propagate carry
Branch: 1 branch
BranchTrueFalseTotal
if addbaseno(\@result, $hsh->{$_}) 0 11483 11483
123 }
124 # my @rv = reverse @result;
125 524 $bc->{b32str} = \@result;
126 }
127 559 $bc;
128 }
129  
130 #my %used = map {$_,0}(0..255);
131  
132 # convert 32*N bit representation to any base < 65536
133 #
134  
135 sub use32wideTObase {
136 556 ▶ my $bc = shift;
Subroutine use32wideTObase: called CC=2 SCAR=6.9
Pod documented
137 556 my($ary,$base,$rquot) = @{$bc}{qw(to tbase b32str)};
138 556 my @quotient = reverse(@$rquot);
139 556 my $quotient = \@quotient;
140 556 my @answer;
141 my $remainder;
142 do {
143 12780 ($quotient,$remainder) = dividebybase($quotient,$base);
144 # these commented out print statements are for convert.t DO NOT REMOVE!
145 #$used{$remainder} = 1;
146 #print $remainder;
147 #print " *" if $remainder > 86;
148 #print "\n";
149 12780 unshift @answer, $ary->[$remainder];
150 556 ▶ } while grep {$_} @$quotient;
Branch: 1 branch
BranchTrueFalseTotal
grep {$_;} @$quotient and do { do { ($quotient, $remainder) = dividebybase($quotient, $base); unshift @answer, $ary->[$remainder] } } 12224 556 12780
151  
152 #foreach (sort {$b <=> $a} keys %used) {
153 #print " $_,\n" if $used{$_} && $_ > 85;
154 #print "\t$_\t=> \n" if !$used{$_} && $_ < 86;
155 #}
156 556 join '', @answer;
157 }
158  
159 20 1;
160  
161 __END__
« blib/lib/Math/Base/Convert/Shortcuts.pm ↑ Summary blib/lib/Math/Base/Convert/Bases.pm »