File: CalcPP.pm

package info (click to toggle)
libmath-base-convert-perl 0.13-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 308 kB
  • sloc: perl: 870; makefile: 2
file content (231 lines) | stat: -rw-r--r-- 5,422 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
#!/usr/bin/perl

package Math::Base::Convert::CalcPP;

use strict;
use vars qw($VERSION);

$VERSION = do { my @r = (q$Revision: 0.03 $ =~ /\d+/g); sprintf "%d."."%02d" x $#r, @r };

# test number < 2^32 is NOT power of 2
#
sub isnotp2 {
  my $ref = ref $_[0];
  shift if ref $_[0] || $_[0] =~ /\D/;	# class?
  $_[0] & $_[0] -1;
}

# add a long n*32 bit number toa number < 65536
# add 'n' to array digits and propagate carry, return carry
#
sub addbaseno {
  my($ap,$n) = @_;
  foreach (@$ap) {
    $_ += $n;
    return 0 unless $_ > 0xffffffff;
    $n = 1;
    $_ -= 4294967296;
  }
  1;	# carry is one on exit, else would have taken return 0 branch
}

# multiply a register of indeterminate length by a number < 65535
#
# ap    pointer to multiplicand array
# multiplier  
#
sub multiply {
  my($ap,$m) = @_;
# $m is always 2..65535
#  $m &= 0xffff;                # max value 65535       already done by VETTING
#
# perl uses doubles for arithmetic, $m << 65536 will fit
  my $carry = 0;
  foreach ( @$ap) {
    $_ *= $m;
    $_ += $carry;
    if ($_ > 0xffffffff) {
      $carry = int($_ / 4294967296);
      $_ %= 4294967296;
    } else {
      $carry = 0;
    }
  }
  push @$ap, $carry if $carry;
}

sub dividebybase {
  my($np,$divisor) = @_;
  my @dividend = @$np;		# 3% improvement
  while ($#dividend) {		# 3% improvement
    last if $dividend[0];
    shift @dividend;
  }    
  my $remainder = 0;
  my @quotient;
  while (@dividend) {
    my $work = ($dividend[0] += ($remainder * 4294967296));     
    push @quotient, int($work / $divisor);
    $remainder = $work % $divisor;
    shift @dividend;
  }
  return (\@quotient,$remainder);
}

# simple versions of conversion, works for N < ~2^49 or 10^16
#
#sub frombase {
#  my($hsh,$base,$str) = @_;
#  my $number = 0;
#  for( $str =~ /./g ) {
#    $number *= $base;
#    $number += $hsh->{$_};
#  }
#  return $number;
#}

#sub tobase {
#sub to_base
#  my($bp,$base,$num) = @_;
#  my $base   = shift;
#  return $bp->[0] if $num == 0;
#  my $str = '';
#  while( $num > 0 ) {
#    $str = $bp->[$num % $base] . $str;
#    $num = int( $num / $base );
#  }
#  return $str;
#}

# convert a number from its base to 32*N bit representation
#
sub useFROMbaseto32wide {
  my $bc = shift;
  my($ary,$hsh,$base,$str) = @{$bc}{qw(from fhsh fbase nstr)};
# check if decimal and interger from within perl's 32bit double representation
# cutoff is 999,999,999,999,999 -- a bit less than 2^50
#
#	convert directly to base 2^32 arrays
#
  my @result = (0);

  if ($base == 10 && length($str) < 16) {
#  unless ($str > 999999999999999) {	# maximum 32 bit double float integer representation
    $result[0] = $str % 4294967296;
    my $quotient = int($str / 4294967296);
    $result[1] = $quotient if $quotient;
    $bc->{b32str} = \@result;
  }
  else {
    for ($str =~ /./g) {
      multiply(\@result,$base);
      push @result, 1 if addbaseno(\@result,$hsh->{$_});	# propagate carry
    }
#    my @rv = reverse @result;
    $bc->{b32str} = \@result;
  }
  $bc;
}

#my %used = map {$_,0}(0..255);

# convert 32*N bit representation to any base < 65536
#

sub use32wideTObase {
  my $bc = shift;
  my($ary,$base,$rquot) = @{$bc}{qw(to tbase b32str)};
  my @quotient = reverse(@$rquot);
  my $quotient = \@quotient;
  my @answer;
  my $remainder;
  do {
    ($quotient,$remainder) = dividebybase($quotient,$base);
# these commented out print statements are for convert.t DO NOT REMOVE!
#$used{$remainder} = 1;
#print $remainder;
#print "    *" if $remainder > 86;
#print "\n";
    unshift @answer, $ary->[$remainder];
  } while grep {$_} @$quotient;

#foreach (sort {$b <=> $a} keys %used) {
#print " $_,\n" if $used{$_} && $_ > 85;
#print "\t$_\t=> \n" if !$used{$_} && $_ < 86;
#}
  join '', @answer;
}

1;

__END__

=head1 NAME

Math::Base::Convert::CalcPP - standard methods used by Math::Base::Convert

=head1 DESCRIPTION

This module contains the standard methods used by B<Math::Base::Convert> to
convert from one base number to another base number.

=over 4

=item * $carry = addbaseno($reg32ptr,$int)

This function adds an integer < 65536 to a long n*32 bit register and
returns the carry.

=item * multiply($reg32ptr,$int)

This function multiplies a long n*32 bit register by an integer < 65536

=item * ($qptr,$remainder) = dividebybase($reg32ptr,$int)

this function divides a long n*32 bit register by an integer < 65536 and
returns a pointer to a long n*32 bit quotient and an integer remainder.

=item * $bc->useFROMbaseto32wide

This method converts FROM an input base string to a long n*32 bit register using
an algorithim like:

	$longnum = 0;
	for $char ( $in_str =~ /./g ) {
	  $longnum *= $base;
	  $longnum += $value{$char)
	}
	return $number;

=item * $output = $bc->use32wideTObase

This method converts a long n*32 bit register TO a base number using an
algorithim like:

    $output = '';
    while( $longnum > 0 ) {
      $output = ( $longnum % $base ) . $output;
      $num = int( $longnum / $base );
    }
    return $output;

=back

=head1 AUTHOR
   
Michael Robinton, michael@bizsystems.com

=head1 COPYRIGHT

Copyright 2012-15, Michael Robinton

This program is free software; you may redistribute it and/or modify it
under the same terms as Perl itself.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 
=cut

1;