/[drupal]/contributions/modules/coolfilter/bcmath.php
ViewVC logotype

Contents of /contributions/modules/coolfilter/bcmath.php

Parent Directory Parent Directory | Revision Log Revision Log | View Revision Graph Revision Graph


Revision 1.2 - (show annotations) (download) (as text)
Wed Feb 14 02:51:02 2007 UTC (2 years, 9 months ago) by lllkkk
Branch: MAIN
CVS Tags: HEAD
Branch point for: DRUPAL-5, DRUPAL-5--2
Changes since 1.1: +111 -111 lines
File MIME type: text/x-php
Add .install file and hack for drupal 5.1
1 <?php
2 if (!function_exists('bcpowmod')) {
3 function bcpowmod($x, $y, $modulus, $scale = 0)
4 {
5 $t = '1';
6 while (bccomp($y, '0')) {
7 if (bccomp(bcmod($y, '2'), '0')) {
8 $t = bcmod(bcmul($t, $x), $modulus);
9 $y = bcsub($y, '1');
10 }
11
12 $x = bcmod(bcmul($x, $x), $modulus);
13 $y = bcdiv($y, '2');
14 }
15 return $t;
16 }
17 }
18
19 function bcstr2dec($str) {
20 $len = strlen($str);
21 $result = '0';
22 $m = '1';
23 for ($i = 0; $i < $len; $i++) {
24 $result = bcadd(bcmul($m, ord($str{$len - $i - 1})), $result);
25 $m = bcmul($m, '256');
26 }
27 return $result;
28 }
29
30 function bcdec2str($dec) {
31 $str = "";
32 while (bccomp($dec, '0') == 1) {
33 $str = chr(bcmod($dec, '256')) . $str;
34 $dec = bcdiv($dec, '256');
35 }
36 return $str;
37 }
38
39 function bcrand($n, $s) {
40 $lowBitMasks = array(0x0000, 0x0001, 0x0003, 0x0007,
41 0x000f, 0x001f, 0x003f, 0x007f,
42 0x00ff, 0x01ff, 0x03ff, 0x07ff,
43 0x0fff, 0x1fff, 0x3fff, 0x7fff);
44
45 $r = $n % 16;
46 $q = floor($n / 16);
47 $result = '0';
48 $m = '1';
49 for ($i = 0; $i < $q; $i++) {
50 $rand = mt_rand(0, 0xffff);
51 if (($q - 1 == $i) and ($r == 0) and ($s == 1)) {
52 $rand |= 0x8000;
53 }
54 $result = bcadd(bcmul($m, $rand), $result);
55 $m = bcmul($m, '65536');
56 }
57 if ($r != 0) {
58 $rand = mt_rand(0, $lowBitMasks[$r]);
59 if ($s == 1) {
60 $rand |= 1 << ($r - 1);
61 }
62 $result = bcadd(bcmul($m, $rand), $result);
63 }
64 return $result;
65 }
66
67 function bcnextprime($num) {
68 if (!bccomp(bcmod($num, '2'), '0')) {
69 $num = bcsub($num, '1');
70 }
71 do {
72 $num = bcadd($num, '2');
73 } while (!bcisprime($num));
74 return $num;
75 }
76
77 function bcisprime($num) {
78 $primes = array(2, 3, 5, 7, 11, 13, 17);
79 for ($i = 0; $i < 7; $i++) {
80 if (!bcmillertest($num, $primes[$i])) {
81 return false;
82 }
83 }
84 return true;
85 }
86
87 function bcmillertest($num, $base) {
88 if (!bccomp($num, '1')) {
89 return false;
90 }
91 $tmp = bcsub($num, '1');
92
93 $zero_bits = 0;
94 while (!bccomp(bcmod($tmp, '2'), '0')) {
95 $zero_bits++;
96 $tmp = bcdiv($tmp, '2');
97 }
98
99 $tmp = bcpowmod($base, $tmp, $num);
100 if (!bccomp($tmp, '1')) {
101 return true;
102 }
103
104 while ($zero_bits--) {
105 if (!bccomp(bcadd($tmp, '1'), $num)) {
106 return true;
107 }
108 $tmp = bcpowmod($tmp, '2', $num);
109 }
110 return false;
111 }
112 ?>

  ViewVC Help
Powered by ViewVC 1.1.2