1. /*
  2. * @(#)BitSieve.java 1.6 00/02/02
  3. *
  4. * Copyright 1996-2000 Sun Microsystems, Inc. All Rights Reserved.
  5. *
  6. * This software is the proprietary information of Sun Microsystems, Inc.
  7. * Use is subject to license terms.
  8. *
  9. */
  10. package java.math;
  11. /**
  12. * A simple bit sieve used for finding prime number candidates. Allows setting
  13. * and clearing of bits in a storage array. The size of the sieve is assumed to
  14. * be constant to reduce overhead. All the bits of a new bitSieve are zero, and
  15. * bits are removed from it by setting them.
  16. *
  17. * To reduce storage space and increase efficiency, no even numbers are
  18. * represented in the sieve (each bit in the sieve represents an odd number).
  19. * The relationship between the index of a bit and the number it represents is
  20. * given by
  21. * N = offset + (2*index + 1);
  22. * Where N is the integer represented by a bit in the sieve, offset is some
  23. * even integer offset indicating where the sieve begins, and index is the
  24. * index of a bit in the sieve array.
  25. *
  26. * @see BigInteger
  27. * @version 1.6, 02/02/00
  28. * @author Michael McCloskey
  29. * @since 1.3
  30. */
  31. class BitSieve {
  32. /**
  33. * Stores the bits in this bitSieve.
  34. */
  35. private long bits[];
  36. /**
  37. * Length is how many bits this sieve holds.
  38. */
  39. private int length;
  40. /**
  41. * A small sieve used to filter out multiples of small primes in a search
  42. * sieve.
  43. */
  44. private static BitSieve smallSieve = new BitSieve();
  45. /**
  46. * Construct a "small sieve" with a base of 0. This constructor is
  47. * used internally to generate the set of "small primes" whose multiples
  48. * are excluded from sieves generated by the main (package private)
  49. * constructor, BitSieve(BigInteger base, int searchLen). The length
  50. * of the sieve generated by this constructor was chosen for performance;
  51. * it controls a tradeoff between how much time is spent constructing
  52. * other sieves, and how much time is wasted testing composite candidates
  53. * for primality. The length was chosen experimentally to yield good
  54. * performance.
  55. */
  56. private BitSieve() {
  57. length = 150 * 64;
  58. bits = new long[(unitIndex(length - 1) + 1)];
  59. // Mark 1 as composite
  60. set(0);
  61. int nextIndex = 1;
  62. int nextPrime = 3;
  63. // Find primes and remove their multiples from sieve
  64. do {
  65. sieveSingle(length, nextIndex + nextPrime, nextPrime);
  66. nextIndex = sieveSearch(length, nextIndex + 1);
  67. nextPrime = 2*nextIndex + 1;
  68. } while((nextIndex > 0) && (nextPrime < length));
  69. }
  70. /**
  71. * Construct a bit sieve of searchLen bits used for finding prime number
  72. * candidates. The new sieve begins at the specified base, which must
  73. * be even.
  74. */
  75. BitSieve(BigInteger base, int searchLen) {
  76. /*
  77. * Candidates are indicated by clear bits in the sieve. As a candidates
  78. * nonprimality is calculated, a bit is set in the sieve to eliminate
  79. * it. To reduce storage space and increase efficiency, no even numbers
  80. * are represented in the sieve (each bit in the sieve represents an
  81. * odd number).
  82. */
  83. bits = new long[(unitIndex(searchLen-1) + 1)];
  84. length = searchLen;
  85. int start = 0;
  86. int step = smallSieve.sieveSearch(smallSieve.length, start);
  87. int convertedStep = (step *2) + 1;
  88. // Construct the large sieve at an even offset specified by base
  89. MutableBigInteger r = new MutableBigInteger();
  90. MutableBigInteger q = new MutableBigInteger();
  91. do {
  92. // Calculate base mod convertedStep
  93. r.copyValue(base.mag);
  94. r.divideOneWord(convertedStep, q);
  95. start = r.value[r.offset];
  96. // Take each multiple of step out of sieve
  97. start = convertedStep - start;
  98. if (start%2 == 0)
  99. start += convertedStep;
  100. sieveSingle(searchLen, (start-1)/2, convertedStep);
  101. // Find next prime from small sieve
  102. step = smallSieve.sieveSearch(smallSieve.length, step+1);
  103. convertedStep = (step *2) + 1;
  104. } while (step > 0);
  105. }
  106. /**
  107. * Given a bit index return unit index containing it.
  108. */
  109. private static int unitIndex(int bitIndex) {
  110. return bitIndex >>> 6;
  111. }
  112. /**
  113. * Return a unit that masks the specified bit in its unit.
  114. */
  115. private static long bit(int bitIndex) {
  116. return 1L << (bitIndex & ((1<<6) - 1));
  117. }
  118. /**
  119. * Get the value of the bit at the specified index.
  120. */
  121. private boolean get(int bitIndex) {
  122. int unitIndex = unitIndex(bitIndex);
  123. return ((bits[unitIndex] & bit(bitIndex)) != 0);
  124. }
  125. /**
  126. * Set the bit at the specified index.
  127. */
  128. private void set(int bitIndex) {
  129. int unitIndex = unitIndex(bitIndex);
  130. bits[unitIndex] |= bit(bitIndex);
  131. }
  132. /**
  133. * This method returns the index of the first clear bit in the search
  134. * array that occurs at or after start. It will not search past the
  135. * specified limit. It returns -1 if there is no such clear bit.
  136. */
  137. private int sieveSearch(int limit, int start) {
  138. if (start >= limit)
  139. return -1;
  140. int index = start;
  141. do {
  142. if (!get(index))
  143. return index;
  144. index++;
  145. } while(index < limit-1);
  146. return -1;
  147. }
  148. /**
  149. * Sieve a single set of multiples out of the sieve. Begin to remove
  150. * multiples of the specified step starting at the specified start index,
  151. * up to the specified limit.
  152. */
  153. private void sieveSingle(int limit, int start, int step) {
  154. while(start < limit) {
  155. set(start);
  156. start += step;
  157. }
  158. }
  159. /**
  160. * Test probable primes in the sieve and return successful candidates.
  161. */
  162. BigInteger retrieve(BigInteger initValue, int certainty) {
  163. // Examine the sieve one long at a time to find possible primes
  164. int offset = 1;
  165. for (int i=0; i<bits.length; i++) {
  166. long nextLong = ~bits[i];
  167. for (int j=0; j<64; j++) {
  168. if ((nextLong & 1) == 1) {
  169. BigInteger candidate = initValue.add(
  170. BigInteger.valueOf(offset));
  171. if (candidate.isProbablePrime(certainty))
  172. return candidate;
  173. }
  174. nextLong >>>= 1;
  175. offset+=2;
  176. }
  177. }
  178. return null;
  179. }
  180. }