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

filter: Statementstmt 100% 182 / 182 Branchbran 88.8% 48 / 54 Conditioncond 100% 3 / 3 MC/DCmcdc 100% 2 / 2 Subroutinesub 100% 15 / 15 Podpod 15.3% 2 / 13 totaltotal 93.6% 252 / 269 SCAR 34.0
CC
30
Cov
97%
useFROMbaseShortcuts
19.1
useTObaseShortcuts
10.0
longshiftright
2.0
CRAP 30.0
« blib/lib/Math/Base/Convert.pm ↑ Summary blib/lib/Math/Base/Convert/CalcPP.pm »
1 package Math::Base::Convert::Shortcuts;
2  
3 20 ▶use vars qw($VERSION);
Subroutine BEGIN: called CC=1 SCAR=0.0
4 20 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 20 $VERSION = do { my @r = (q$Revision: 0.05 $ =~ /\d+/g); sprintf "%d."."%02d" x $#r, @r };
7  
8 # load bitmaps
9  
10 20 my $xlt = require Math::Base::Convert::Bitmaps;
11  
12 #
13 # base 2 4 8 16 32 64
14 # base power 1 2 3 4 5 6
15 # xlt = [ \@standardbases, undef, \%_2wide, undef, undef, \%_5wide, \%_6wide ];
16 #
17 # base 2 maps directly to lookup key
18 # base 3 maps directly to standard lookup value
19 # base 4 converts directly to hex
20 #
21 # where @standardbases = (\{
22 # dna => {
23 # '00' => 'a',
24 # '01' => 'c',
25 # '10' => 't',
26 # '11' => 'g',
27 # },
28 # b64 => {
29 # '000000' => 0,
30 # '000001' => 1,
31 # * -
32 # * -
33 # '001010' => 'A',
34 # '001011' => 'B',
35 # * -
36 # * -
37 # '111111' => '_',
38 # },
39 # m64 => etc....
40 # iru
41 # url
42 # rex
43 # id0
44 # id1
45 # xnt
46 # xid
47 # });
48 #
49 # .... and
50 #
51 # hash arrays are bit to value maps of the form
52 #
53 # %_3wide = {
54 # '000' => 0,
55 # '001' => 1,
56 # '010' => 2,
57 # * -
58 # * -
59 # etc...
60 # };
61 #
62  
63 20 my @srindx = ( # accomodate up to 31 bit shifts
64 0, # 0 unused
65 1, # 1
66 3, # 2
67 7, # 3
68 0xf, # 4
69 0x1f, # 5
70 0x3f, # 6
71 0x7f, # 7
72 0xff, # 8
73 0x1ff, # 9
74 0x3ff, # 10
75 0x7ff, # 11
76 0xfff, # 12
77 0x1fff, # 13
78 0x3fff, # 14
79 0x7fff, # 15
80 0xffff, # 16
81 0x1ffff, # 17
82 0x3ffff, # 18
83 0x7ffff, # 19
84 0xfffff, # 20
85 0x1fffff, # 21
86 0x3fffff, # 22
87 0x7fffff, # 23
88 0xffffff, # 24
89 0x1ffffff, # 25
90 0x3ffffff, # 26
91 0x7ffffff, # 27
92 0xfffffff, # 28
93 0x1fffffff, # 29
94 0x3fffffff, # 30
95 0x7fffffff # 31
96 );
97  
98 20 my @srindx2 = ( # accomodate up to 31 bit shifts
99 0xffffffff, # 0 unused
100 0xfffffffe, # 1
101 0xfffffffc, # 2
102 0xfffffff8, # 3
103 0xfffffff0, # 4
104 0xffffffe0, # 5
105 0xffffffc0, # 6
106 0xffffff80, # 7
107 0xffffff00, # 8
108 0xfffffe00, # 9
109 0xfffffc00, # 10
110 0xfffff800, # 11
111 0xfffff000, # 12
112 0xffffe000, # 13
113 0xffffc000, # 14
114 0xffff8000, # 15
115 0xffff0000, # 16
116 0xfffe0000, # 17
117 0xfffc0000, # 18
118 0xfff80000, # 19
119 0xfff00000, # 20
120 0xffe00000, # 21
121 0xffc00000, # 22
122 0xff800000, # 23
123 0xff000000, # 24
124 0xfe000000, # 25
125 0xfc000000, # 26
126 0xf8000000, # 27
127 0xf0000000, # 28
128 0xe0000000, # 29
129 0xc0000000, # 30
130 0x80000000 # 31
131 );
132  
133 #
134 # $arraypointer, $shiftright, $mask, $shiftleft
135 #
136 sub longshiftright {
137 609 ▶ my $ap = $_[0]; # perl appears to optimize these variables into registers
Subroutine longshiftright: called CC=2 SCAR=6.9
Pod undocumented
138 609 my $sr = $_[1]; # when they are set in this manner -- much faster!!
139 609 my $msk = $_[2];
140 609 my $sl = $_[3];
141 609 my $al = $#$ap -1;
142 609 my $i = 1;
143 609 foreach (0..$al) {
144 975 $ap->[$_] >>= $sr;
145 # $ap->[$_] |= ($ap->[$i] & $msk) << $sl;
146 975 $ap->[$_] |= ($ap->[$i] << $sl) & $msk;
147 975 $i++;
148 }
149 609 $ap->[$#$ap] >>= $sr;
150 }
151  
152 # see the comments at "longshiftright" about the
153 # integration of calculations into the local subroutine
154 #
155 sub shiftright {
156 609 ▶ my($ap,$n) = @_;
Subroutine shiftright: called CC=1 SCAR=0.0
Pod undocumented
157 609 longshiftright($ap,$n,$srindx2[$n],32 -$n);
158 }
159  
160 #
161 # fast direct conversion of base power of 2 sets to base 2^32
162 #
163 sub bx1 { # base 2, 1 bit wide x32 = 32 bits - 111 32 1's 111111111111111
164 66 ▶ my($ss,$d32p) = @_;
Subroutine bx1: called CC=1 SCAR=0.0
Pod undocumented
165 66 unshift @$d32p, unpack('N1',pack('B32',$ss));
166 }
167  
168 20 my %dna= ('AA', 0, 'AC', 1, 'AT', 2, 'AG', 3, 'CA', 4, 'CC', 5, 'CT', 6, 'CG', 7, 'TA', 8, 'TC', 9, 'TT', 10, 'TG', 11, 'GA', 12, 'GC', 13, 'GT', 14, 'GG', 15,
169 'Aa', 0, 'Ac', 1, 'At', 2, 'Ag', 3, 'Ca', 4, 'Cc', 5, 'Ct', 6, 'Cg', 7, 'Ta', 8, 'Tc', 9, 'Tt', 10, 'Tg', 11, 'Ga', 12, 'Gc', 13, 'Gt', 14, 'Gg', 15,
170 'aA', 0, 'aC', 1, 'aT', 2, 'aG', 3, 'cA', 4, 'cC', 5, 'cT', 6, 'cG', 7, 'tA', 8, 'tC', 9, 'tT', 10, 'tG', 11, 'gA', 12, 'gC', 13, 'gT', 14, 'gG', 15,
171 'aa', 0, 'ac', 1, 'at', 2, 'ag', 3, 'ca', 4, 'cc', 5, 'ct', 6, 'cg', 7, 'ta', 8, 'tc', 9, 'tt', 10, 'tg', 11, 'ga', 12, 'gc', 13, 'gt', 14, 'gg', 15,
172  
173 );
174  
175 # substr 4x faster than array lookup
176 #
177 sub bx2 { # base 4, 2 bits wide x16 = 32 bits - 3333333333333333
178 54 ▶ my($ss,$d32p) = @_;
Subroutine bx2: called CC=1 SCAR=0.0
Pod undocumented
179 54 my $bn = $dna{substr($ss,0,2)}; # 2 digits as a time => base 16
180 54 $bn <<= 4;
181 54 $bn += $dna{substr($ss,2,2)};
182 54 $bn <<= 4;
183 54 $bn += $dna{substr($ss,4,2)};
184 54 $bn <<= 4;
185 54 $bn += $dna{substr($ss,6,2)};
186 54 $bn <<= 4;
187 54 $bn += $dna{substr($ss,8,2)};
188 54 $bn <<= 4;
189 54 $bn += $dna{substr($ss,10,2)};
190 54 $bn <<= 4;
191 54 $bn += $dna{substr($ss,12,2)};
192 54 $bn <<= 4;
193 54 $bn += $dna{substr($ss,14,2)};
194 54 unshift @$d32p, $bn;
195 }
196  
197 sub bx3 { # base 8, 3 bits wide x10 = 30 bits - 07777777777
198 57 ▶ my($ss,$d32p) = @_;
Subroutine bx3: called CC=1 SCAR=0.0
Pod undocumented
199 57 unshift @$d32p, CORE::oct($ss) << 2;
200 57 shiftright($d32p,2);
201 }
202  
203 sub bx4 { # base 16, 4 bits wide x8 = 32 bits - 0xffffffff
204 58 ▶ my($ss,$d32p) = @_;
Subroutine bx4: called CC=1 SCAR=0.0
Pod undocumented
205 58 unshift @$d32p, CORE::hex($ss);
206 }
207  
208 sub bx5 { # base 32, 5 bits wide x6 = 30 bits - 555555
209 58 ▶ my($ss,$d32p,$hsh) = @_;
Subroutine bx5: called CC=1 SCAR=0.0
Pod undocumented
210 58 my $bn = $hsh->{substr($ss,0,1)};
211 58 $bn <<= 5;
212 58 $bn += $hsh->{substr($ss,1,1)};
213 58 $bn <<= 5;
214 58 $bn += $hsh->{substr($ss,2,1)};
215 58 $bn <<= 5;
216 58 $bn += $hsh->{substr($ss,3,1)};
217 58 $bn <<= 5;
218 58 $bn += $hsh->{substr($ss,4,1)};
219 58 $bn <<= 5;
220 58 unshift @$d32p, ($bn += $hsh->{substr($ss,5,1)}) << 2;
221 58 shiftright($d32p,2);
222 }
223  
224 sub bx6 { # base 64, 6 bits wide x5 = 30 bits - 66666
225 419 ▶ my($ss,$d32p,$hsh) = @_;
Subroutine bx6: called CC=1 SCAR=0.0
Pod undocumented
226 419 my $bn = $hsh->{substr($ss,0,1)};
227 419 $bn <<= 6;
228 419 $bn += $hsh->{substr($ss,1,1)};
229 419 $bn <<= 6;
230 419 $bn += $hsh->{substr($ss,2,1)};
231 419 $bn <<= 6;
232 419 $bn += $hsh->{substr($ss,3,1)};
233 419 $bn <<= 6;
234 419 unshift @$d32p, ($bn += $hsh->{substr($ss,4,1)}) << 2;
235 419 shiftright($d32p,2);
236 }
237  
238 sub bx7 { # base 128, 7 bits wide x4 = 28 bits - 7777
239 66 ▶ my($ss,$d32p,$hsh) = @_;
Subroutine bx7: called CC=1 SCAR=0.0
Pod undocumented
240 66 my $bn = $hsh->{substr($ss,0,1)};
241 66 $bn <<= 7;
242 66 $bn += $hsh->{substr($ss,1,1)};
243 66 $bn <<= 7;
244 66 $bn += $hsh->{substr($ss,2,1)};
245 66 $bn <<= 7;
246 66 unshift @$d32p, ($bn += $hsh->{substr($ss,3,1)}) << 4;
247 66 shiftright($d32p,4);
248 }
249  
250 sub bx8 { # base 256, 8 bits wide x4 = 32 bits - 8888
251 58 ▶ my($ss,$d32p,$hsh) = @_;
Subroutine bx8: called CC=1 SCAR=0.0
Pod undocumented
252 58 my $bn = $hsh->{substr($ss,0,1)};
253 58 $bn *= 256;
254 58 $bn += $hsh->{substr($ss,1,1)};
255 58 $bn *= 256;
256 58 $bn += $hsh->{substr($ss,2,1)};
257 58 $bn *= 256;
258 58 unshift @$d32p, $bn += $hsh->{substr($ss,3,1)};
259 }
260  
261 20 my @useFROMbaseShortcuts = ( 0, # unused
262 \&bx1, # base 2, 1 bit wide x32 = 32 bits - 111 32 1's 111111111111111
263 \&bx2, # base 4, 2 bits wide x16 = 32 bits - 3333333333333333
264 \&bx3, # base 8, 3 bits wide x10 = 30 bits - 07777777777
265 \&bx4, # base 16, 4 bits wide x8 = 32 bits - 0xffffffff
266 \&bx5, # base 32, 5 bits wide x6 = 30 bits - 555555
267 \&bx6, # base 64, 6 bits wide x5 = 30 bits - 66666
268 \&bx7, # base 128, 7 bits wide x4 = 28 bits - 7777
269 \&bx8, # and base 256, 8 bits wide x4 = 32 bits - 8888
270 );
271  
272 # 1) find number of digits of base that will fit in 2^32
273 # 2) pad msb's
274 # 3) substr digit groups and get value
275  
276 sub useFROMbaseShortcuts {
277 467 ▶ my $bc = shift;
Subroutine useFROMbaseShortcuts: called CC=19 SCAR=29.5
Pod documented
278 467 my($ary,$hsh,$base,$str) = @{$bc}{qw(from fhsh fbase nstr)};
279 467 my $bp = int(log($base)/log(2) +0.5);
280 467 my $len = length($str);
281 467 ▶ return ($bp,[0]) unless $len; # no value in zero length string
Branch: 1 branch
BranchTrueFalseTotal
unless $len 0 467 467
282  
283 467 my $shrink = 32 % ($bp * $base); # bits short of 16 bits
284  
285 # convert any strings in standard convertable bases that are NOT standard strings to the standard
286 467 my $basnam = ref $ary;
287 467 my $padchar = $ary->[0];
288 467 ▶ if ($base == 16) { # should be hex
Branch: 4 branches
BranchTrueFalseTotal
if ($base == 16) { } 14 453 467
elsif ($base == 8) { } 13 440 453
elsif ($base == 4) { } 13 427 440
elsif ($base == 2) { } 15 412 427
289 14 ▶ if ($basnam !~ /HEX$/i) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($basnam =~ /HEX$/i) 2 12 14
290 2 ▶ $bc->{fHEX} = $bc->HEX() unless exists $bc->{fHEX};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $bc->{"fHEX"} 2 0 2
291 2 my @h = @{$bc->{fHEX}};
292 2 $str =~ s/(.)/$h[$hsh->{$1}]/g; # translate string to HEX
293 2 $padchar = 0;
294 }
295 }
296 elsif ($base == 8) {
297 13 ▶ if ($basnam !~ /OCT$/i) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($basnam =~ /OCT$/i) 2 11 13
298 2 ▶ $bc->{foct} = $bc->ocT() unless exists $bc->{foct};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $bc->{"foct"} 2 0 2
299 2 my @o = @{$bc->{foct}};
300 2 $str =~ s/(.)/$o[$hsh->{$1}]/g;
301 2 $padchar = '0';
302 }
303 }
304 elsif ($base == 4) { # will map to hex
305 13 ▶ if ($basnam !~ /dna$/i) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($basnam =~ /dna$/i) 2 11 13
306 2 ▶ $bc->{fDNA} = $bc->DNA() unless exists $bc->{fDNA};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $bc->{"fDNA"} 2 0 2
307 2 my @d = @{$bc->{fDNA}};
308 2 $str =~ s/(.)/$d[$hsh->{$1}]/g;
309 2 $padchar = 'A';
310 }
311 }
312 elsif ($base == 2) { # will map to binary
313 15 ▶ if ($basnam !~ /bin$/) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($basnam =~ /bin$/) 1 14 15
314 1 ▶ $bc->{fbin} = $bc->bin() unless exists $bc->{fbin};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $bc->{"fbin"} 1 0 1
315 1 my @b = @{$bc->{fbin}};
316 1 $str =~ s/(.)/$b[$hsh->{$1}]/g;
317 1 $padchar = '0';
318 }
319 }
320  
321 # digits per 32 bit register - $dpr
322 # $dpr = int(32 / $bp) = 32 / digit bit width
323 #
324 # number of digits to pad string so the last digit fits exactly in a 32 bit register
325 # $pad = digits_per_reg - (string_length % $dpr)
326 467 my $dpr = int (32 / $bp);
327 467 my $pad = $dpr - ($len % $dpr);
328 467 ▶ $pad = 0 if $pad == $dpr;
Branch: 1 branch
BranchTrueFalseTotal
if $pad == $dpr 10 457 467
329 467 ▶ if ($pad) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pad) 457 10 467
330 457 $str = ($padchar x $pad) . $str; # pad string with zero value digit
331 }
332  
333 # number of iterations % digits/register
334 467 $len += $pad;
335 467 my $i = 0;
336 467 my @d32;
337 467 ▶ while ($i < $len) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $len) 836 467 1303
338 #
339 # base16 digit = sub bx[base power](string fragment )
340 # where base power is the width of each nibble and
341 # base is the symbol value width in bits
342  
343 836 $useFROMbaseShortcuts[$bp]->(substr($str,$i,$dpr),\@d32,$hsh);
344 836 $i += $dpr;
345 }
346 467 ▶ while($#d32 && ! $d32[$#d32]) { # waste leading zeros
Branch: 1 branch
BranchTrueFalseTotal
if ($#d32 and not $d32[$#d32]) 18 467 485
Condition100%3/3
$#d32 and not $d32[$#d32]
!ll&&!rl&&r
3848318
MC/DC100%2/2
$#d32 and not $d32[$#d32]
$#d32 not $d32[$#d32]
Truth table100%3/3
A and not(B)
A= $#d32B= not $d32[$#d32]
ABresult
0X0
100
111
347 18 pop @d32;
348 }
349 467 $bc->{b32str} = \@d32;
350 }
351  
352 # map non-standard user base to bitstream lookup
353 #
354 sub usrmap {
355 49 ▶ my($to,$map) = @_;
Subroutine usrmap: called CC=2 SCAR=6.9
Pod undocumented
356 49 my %map;
357 49 ▶ while (my($key,$val) = each %$map) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %$map) 6168 49 6217
358 6168 $map{$key} = $to->[$val];
359 }
360 49 \%map;
361 }
362  
363 sub useTObaseShortcuts {
364 486 ▶ my $bc = shift;
Subroutine useTObaseShortcuts: called CC=10 SCAR=23.0
Pod documented
365 486 my($base,$b32p,$to) = @{$bc}{qw( tbase b32str to )};
366 486 my $bp = int(log($base)/log(2) +0.5); # base power
367 486 my $L = @$b32p;
368 486 my $packed = pack("N$L", reverse @{$b32p});
369 486 ref($to) =~ /([^:]+)$/; # extract to base name
370 486 my $bname = $1;
371 486 my $str;
372 486 ▶ if ($bp == 1) { # binary
Branch: 2 branches
BranchTrueFalseTotal
if ($bp == 1) { } 121 365 486
elsif ($bp == 4) { } 122 243 365
373 121 $L *= 32;
374 121 ($str = unpack("B$L",$packed)) =~ s/^0+//; # suppress leading zeros
375 121 ▶ $str =~ s/(.)/$to->[$1]/g if $bname eq 'user';
Branch: 1 branch
BranchTrueFalseTotal
if $bname eq "user" 56 65 121
376 }
377 elsif ($bp == 4) { # hex / base 16
378 122 $L *= 8;
379 122 ($str = unpack("H$L",$packed)) =~ s/^0+//; # suppress leading zeros
380 122 ▶ $str =~ s/(.)/$to->[CORE::hex($1)]/g if $bname eq 'user';
Branch: 1 branch
BranchTrueFalseTotal
if $bname eq "user" 3 119 122
381 }
382 else { # the rest
383 243 my $map;
384 243 ▶ if ($bname eq 'user') { # special map request
Branch: 2 branches
BranchTrueFalseTotal
if ($bname eq 'user') { } 49 194 243
elsif ($bp == 3) { } 65 129 194
385 49 ▶ unless (exists $bc->{tmap}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $bc->{"tmap"}) 49 0 49
386 49 $bc->{tmap} = usrmap($to,$xlt->[$bp]); # cache the map for speed
387 }
388 49 $map = $bc->{tmap};
389 }
390 elsif ($bp == 3) { # octal variant?
391 65 $map = $xlt->[$bp];
392 } else {
393 129 $map = $xlt->[0]->{$bname}; # standard map
394 }
395 243 $L *= 32;
396 243 (my $bits = unpack("B$L",$packed)) =~ s/^0+//; # suppress leading zeros
397 #print "bp = $bp, BITS=\n$bits\n";
398 243 my $len = length($bits);
399 243 my $m = $len % $bp; # pad to even multiple base power
400 #my $z = $m;
401 243 ▶ if ($m) {
Branch: 1 branch
BranchTrueFalseTotal
if ($m) 67 176 243
402 67 $m = $bp - $m;
403 67 $bits = ('0' x $m) . $bits;
404 67 $len += $m;
405 }
406 #print "len = $len, m_init = $z, m = $m, BITS PADDED\n$bits\n";
407 243 $str = '';
408 243 ▶ for (my $i = 0; $i < $len; $i += $bp) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $len) 1829 243 2072
409 1829 $str .= $map->{substr($bits,$i,$bp)};
410 #print "MAPPED i=$i, str=$str\n";
411 }
412 }
413 486 $str;
414 }
415  
416 20 1;
417  
418 __END__
« blib/lib/Math/Base/Convert.pm ↑ Summary blib/lib/Math/Base/Convert/CalcPP.pm »