Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

320 lines
9.1 KiB

  1. package bigint;
  2. #
  3. # This library is no longer being maintained, and is included for backward
  4. # compatibility with Perl 4 programs which may require it.
  5. #
  6. # In particular, this should not be used as an example of modern Perl
  7. # programming techniques.
  8. #
  9. # Suggested alternative: Math::BigInt
  10. #
  11. # arbitrary size integer math package
  12. #
  13. # by Mark Biggar
  14. #
  15. # Canonical Big integer value are strings of the form
  16. # /^[+-]\d+$/ with leading zeros suppressed
  17. # Input values to these routines may be strings of the form
  18. # /^\s*[+-]?[\d\s]+$/.
  19. # Examples:
  20. # '+0' canonical zero value
  21. # ' -123 123 123' canonical value '-123123123'
  22. # '1 23 456 7890' canonical value '+1234567890'
  23. # Output values always always in canonical form
  24. #
  25. # Actual math is done in an internal format consisting of an array
  26. # whose first element is the sign (/^[+-]$/) and whose remaining
  27. # elements are base 100000 digits with the least significant digit first.
  28. # The string 'NaN' is used to represent the result when input arguments
  29. # are not numbers, as well as the result of dividing by zero
  30. #
  31. # routines provided are:
  32. #
  33. # bneg(BINT) return BINT negation
  34. # babs(BINT) return BINT absolute value
  35. # bcmp(BINT,BINT) return CODE compare numbers (undef,<0,=0,>0)
  36. # badd(BINT,BINT) return BINT addition
  37. # bsub(BINT,BINT) return BINT subtraction
  38. # bmul(BINT,BINT) return BINT multiplication
  39. # bdiv(BINT,BINT) return (BINT,BINT) division (quo,rem) just quo if scalar
  40. # bmod(BINT,BINT) return BINT modulus
  41. # bgcd(BINT,BINT) return BINT greatest common divisor
  42. # bnorm(BINT) return BINT normalization
  43. #
  44. # overcome a floating point problem on certain osnames (posix-bc, os390)
  45. BEGIN {
  46. my $x = 100000.0;
  47. my $use_mult = int($x*1e-5)*1e5 == $x ? 1 : 0;
  48. }
  49. $zero = 0;
  50. # normalize string form of number. Strip leading zeros. Strip any
  51. # white space and add a sign, if missing.
  52. # Strings that are not numbers result the value 'NaN'.
  53. sub main'bnorm { #(num_str) return num_str
  54. local($_) = @_;
  55. s/\s+//g; # strip white space
  56. if (s/^([+-]?)0*(\d+)$/$1$2/) { # test if number
  57. substr($_,$[,0) = '+' unless $1; # Add missing sign
  58. s/^-0/+0/;
  59. $_;
  60. } else {
  61. 'NaN';
  62. }
  63. }
  64. # Convert a number from string format to internal base 100000 format.
  65. # Assumes normalized value as input.
  66. sub internal { #(num_str) return int_num_array
  67. local($d) = @_;
  68. ($is,$il) = (substr($d,$[,1),length($d)-2);
  69. substr($d,$[,1) = '';
  70. ($is, reverse(unpack("a" . ($il%5+1) . ("a5" x ($il/5)), $d)));
  71. }
  72. # Convert a number from internal base 100000 format to string format.
  73. # This routine scribbles all over input array.
  74. sub external { #(int_num_array) return num_str
  75. $es = shift;
  76. grep($_ > 9999 || ($_ = substr('0000'.$_,-5)), @_); # zero pad
  77. &'bnorm(join('', $es, reverse(@_))); # reverse concat and normalize
  78. }
  79. # Negate input value.
  80. sub main'bneg { #(num_str) return num_str
  81. local($_) = &'bnorm(@_);
  82. vec($_,0,8) ^= ord('+') ^ ord('-') unless $_ eq '+0';
  83. s/^./N/ unless /^[-+]/; # works both in ASCII and EBCDIC
  84. $_;
  85. }
  86. # Returns the absolute value of the input.
  87. sub main'babs { #(num_str) return num_str
  88. &abs(&'bnorm(@_));
  89. }
  90. sub abs { # post-normalized abs for internal use
  91. local($_) = @_;
  92. s/^-/+/;
  93. $_;
  94. }
  95. # Compares 2 values. Returns one of undef, <0, =0, >0. (suitable for sort)
  96. sub main'bcmp { #(num_str, num_str) return cond_code
  97. local($x,$y) = (&'bnorm($_[$[]),&'bnorm($_[$[+1]));
  98. if ($x eq 'NaN') {
  99. undef;
  100. } elsif ($y eq 'NaN') {
  101. undef;
  102. } else {
  103. &cmp($x,$y);
  104. }
  105. }
  106. sub cmp { # post-normalized compare for internal use
  107. local($cx, $cy) = @_;
  108. return 0 if ($cx eq $cy);
  109. local($sx, $sy) = (substr($cx, 0, 1), substr($cy, 0, 1));
  110. local($ld);
  111. if ($sx eq '+') {
  112. return 1 if ($sy eq '-' || $cy eq '+0');
  113. $ld = length($cx) - length($cy);
  114. return $ld if ($ld);
  115. return $cx cmp $cy;
  116. } else { # $sx eq '-'
  117. return -1 if ($sy eq '+');
  118. $ld = length($cy) - length($cx);
  119. return $ld if ($ld);
  120. return $cy cmp $cx;
  121. }
  122. }
  123. sub main'badd { #(num_str, num_str) return num_str
  124. local(*x, *y); ($x, $y) = (&'bnorm($_[$[]),&'bnorm($_[$[+1]));
  125. if ($x eq 'NaN') {
  126. 'NaN';
  127. } elsif ($y eq 'NaN') {
  128. 'NaN';
  129. } else {
  130. @x = &internal($x); # convert to internal form
  131. @y = &internal($y);
  132. local($sx, $sy) = (shift @x, shift @y); # get signs
  133. if ($sx eq $sy) {
  134. &external($sx, &add(*x, *y)); # if same sign add
  135. } else {
  136. ($x, $y) = (&abs($x),&abs($y)); # make abs
  137. if (&cmp($y,$x) > 0) {
  138. &external($sy, &sub(*y, *x));
  139. } else {
  140. &external($sx, &sub(*x, *y));
  141. }
  142. }
  143. }
  144. }
  145. sub main'bsub { #(num_str, num_str) return num_str
  146. &'badd($_[$[],&'bneg($_[$[+1]));
  147. }
  148. # GCD -- Euclids algorithm Knuth Vol 2 pg 296
  149. sub main'bgcd { #(num_str, num_str) return num_str
  150. local($x,$y) = (&'bnorm($_[$[]),&'bnorm($_[$[+1]));
  151. if ($x eq 'NaN' || $y eq 'NaN') {
  152. 'NaN';
  153. } else {
  154. ($x, $y) = ($y,&'bmod($x,$y)) while $y ne '+0';
  155. $x;
  156. }
  157. }
  158. # routine to add two base 1e5 numbers
  159. # stolen from Knuth Vol 2 Algorithm A pg 231
  160. # there are separate routines to add and sub as per Kunth pg 233
  161. sub add { #(int_num_array, int_num_array) return int_num_array
  162. local(*x, *y) = @_;
  163. $car = 0;
  164. for $x (@x) {
  165. last unless @y || $car;
  166. $x -= 1e5 if $car = (($x += shift(@y) + $car) >= 1e5) ? 1 : 0;
  167. }
  168. for $y (@y) {
  169. last unless $car;
  170. $y -= 1e5 if $car = (($y += $car) >= 1e5) ? 1 : 0;
  171. }
  172. (@x, @y, $car);
  173. }
  174. # subtract base 1e5 numbers -- stolen from Knuth Vol 2 pg 232, $x > $y
  175. sub sub { #(int_num_array, int_num_array) return int_num_array
  176. local(*sx, *sy) = @_;
  177. $bar = 0;
  178. for $sx (@sx) {
  179. last unless @y || $bar;
  180. $sx += 1e5 if $bar = (($sx -= shift(@sy) + $bar) < 0);
  181. }
  182. @sx;
  183. }
  184. # multiply two numbers -- stolen from Knuth Vol 2 pg 233
  185. sub main'bmul { #(num_str, num_str) return num_str
  186. local(*x, *y); ($x, $y) = (&'bnorm($_[$[]), &'bnorm($_[$[+1]));
  187. if ($x eq 'NaN') {
  188. 'NaN';
  189. } elsif ($y eq 'NaN') {
  190. 'NaN';
  191. } else {
  192. @x = &internal($x);
  193. @y = &internal($y);
  194. local($signr) = (shift @x ne shift @y) ? '-' : '+';
  195. @prod = ();
  196. for $x (@x) {
  197. ($car, $cty) = (0, $[);
  198. for $y (@y) {
  199. $prod = $x * $y + $prod[$cty] + $car;
  200. if ($use_mult) {
  201. $prod[$cty++] =
  202. $prod - ($car = int($prod * 1e-5)) * 1e5;
  203. }
  204. else {
  205. $prod[$cty++] =
  206. $prod - ($car = int($prod / 1e5)) * 1e5;
  207. }
  208. }
  209. $prod[$cty] += $car if $car;
  210. $x = shift @prod;
  211. }
  212. &external($signr, @x, @prod);
  213. }
  214. }
  215. # modulus
  216. sub main'bmod { #(num_str, num_str) return num_str
  217. (&'bdiv(@_))[$[+1];
  218. }
  219. sub main'bdiv { #(dividend: num_str, divisor: num_str) return num_str
  220. local (*x, *y); ($x, $y) = (&'bnorm($_[$[]), &'bnorm($_[$[+1]));
  221. return wantarray ? ('NaN','NaN') : 'NaN'
  222. if ($x eq 'NaN' || $y eq 'NaN' || $y eq '+0');
  223. return wantarray ? ('+0',$x) : '+0' if (&cmp(&abs($x),&abs($y)) < 0);
  224. @x = &internal($x); @y = &internal($y);
  225. $srem = $y[$[];
  226. $sr = (shift @x ne shift @y) ? '-' : '+';
  227. $car = $bar = $prd = 0;
  228. if (($dd = int(1e5/($y[$#y]+1))) != 1) {
  229. for $x (@x) {
  230. $x = $x * $dd + $car;
  231. if ($use_mult) {
  232. $x -= ($car = int($x * 1e-5)) * 1e5;
  233. }
  234. else {
  235. $x -= ($car = int($x / 1e5)) * 1e5;
  236. }
  237. }
  238. push(@x, $car); $car = 0;
  239. for $y (@y) {
  240. $y = $y * $dd + $car;
  241. if ($use_mult) {
  242. $y -= ($car = int($y * 1e-5)) * 1e5;
  243. }
  244. else {
  245. $y -= ($car = int($y / 1e5)) * 1e5;
  246. }
  247. }
  248. }
  249. else {
  250. push(@x, 0);
  251. }
  252. @q = (); ($v2,$v1) = @y[-2,-1];
  253. while ($#x > $#y) {
  254. ($u2,$u1,$u0) = @x[-3..-1];
  255. $q = (($u0 == $v1) ? 99999 : int(($u0*1e5+$u1)/$v1));
  256. --$q while ($v2*$q > ($u0*1e5+$u1-$q*$v1)*1e5+$u2);
  257. if ($q) {
  258. ($car, $bar) = (0,0);
  259. for ($y = $[, $x = $#x-$#y+$[-1; $y <= $#y; ++$y,++$x) {
  260. $prd = $q * $y[$y] + $car;
  261. if ($use_mult) {
  262. $prd -= ($car = int($prd * 1e-5)) * 1e5;
  263. }
  264. else {
  265. $prd -= ($car = int($prd / 1e5)) * 1e5;
  266. }
  267. $x[$x] += 1e5 if ($bar = (($x[$x] -= $prd + $bar) < 0));
  268. }
  269. if ($x[$#x] < $car + $bar) {
  270. $car = 0; --$q;
  271. for ($y = $[, $x = $#x-$#y+$[-1; $y <= $#y; ++$y,++$x) {
  272. $x[$x] -= 1e5
  273. if ($car = (($x[$x] += $y[$y] + $car) > 1e5));
  274. }
  275. }
  276. }
  277. pop(@x); unshift(@q, $q);
  278. }
  279. if (wantarray) {
  280. @d = ();
  281. if ($dd != 1) {
  282. $car = 0;
  283. for $x (reverse @x) {
  284. $prd = $car * 1e5 + $x;
  285. $car = $prd - ($tmp = int($prd / $dd)) * $dd;
  286. unshift(@d, $tmp);
  287. }
  288. }
  289. else {
  290. @d = @x;
  291. }
  292. (&external($sr, @q), &external($srem, @d, $zero));
  293. } else {
  294. &external($sr, @q);
  295. }
  296. }
  297. 1;