jdk/src/share/classes/java/math/BitSieve.java
author bpb
Fri, 23 Aug 2013 14:15:54 -0700
changeset 19585 b57abf89019f
parent 5506 202f599c92aa
permissions -rw-r--r--
6378503: In java.math.BigDecimal, division by one returns zero 6446965: Using BigDecimal.divideToIntegralValue with extreme scales can lead to an incorrect result Summary: Fix overflow of ints and ensure appropriate values passed to checkScaleNonZero() Reviewed-by: darcy, martin Contributed-by: Brian Burkhalter <brian.burkhalter@oracle.com>
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2922
diff changeset
     2
 * Copyright (c) 1999, 2007, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2922
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2922
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2922
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2922
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2922
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package java.math;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
 * A simple bit sieve used for finding prime number candidates. Allows setting
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
 * and clearing of bits in a storage array. The size of the sieve is assumed to
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
 * be constant to reduce overhead. All the bits of a new bitSieve are zero, and
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
 * bits are removed from it by setting them.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 * To reduce storage space and increase efficiency, no even numbers are
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 * represented in the sieve (each bit in the sieve represents an odd number).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * The relationship between the index of a bit and the number it represents is
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * given by
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * N = offset + (2*index + 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * Where N is the integer represented by a bit in the sieve, offset is some
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * even integer offset indicating where the sieve begins, and index is the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * index of a bit in the sieve array.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * @see     BigInteger
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * @author  Michael McCloskey
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * @since   1.3
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
class BitSieve {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
     * Stores the bits in this bitSieve.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
    private long bits[];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
     * Length is how many bits this sieve holds.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
    private int length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
     * A small sieve used to filter out multiples of small primes in a search
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
     * sieve.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
    private static BitSieve smallSieve = new BitSieve();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
     * Construct a "small sieve" with a base of 0.  This constructor is
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
     * used internally to generate the set of "small primes" whose multiples
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
     * are excluded from sieves generated by the main (package private)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
     * constructor, BitSieve(BigInteger base, int searchLen).  The length
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
     * of the sieve generated by this constructor was chosen for performance;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
     * it controls a tradeoff between how much time is spent constructing
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
     * other sieves, and how much time is wasted testing composite candidates
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
     * for primality.  The length was chosen experimentally to yield good
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
     * performance.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
    private BitSieve() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
        length = 150 * 64;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
        bits = new long[(unitIndex(length - 1) + 1)];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
        // Mark 1 as composite
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
        set(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
        int nextIndex = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
        int nextPrime = 3;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
        // Find primes and remove their multiples from sieve
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
        do {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
            sieveSingle(length, nextIndex + nextPrime, nextPrime);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
            nextIndex = sieveSearch(length, nextIndex + 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
            nextPrime = 2*nextIndex + 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
        } while((nextIndex > 0) && (nextPrime < length));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
     * Construct a bit sieve of searchLen bits used for finding prime number
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
     * candidates. The new sieve begins at the specified base, which must
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
     * be even.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
    BitSieve(BigInteger base, int searchLen) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
         * Candidates are indicated by clear bits in the sieve. As a candidates
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
         * nonprimality is calculated, a bit is set in the sieve to eliminate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
         * it. To reduce storage space and increase efficiency, no even numbers
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
         * are represented in the sieve (each bit in the sieve represents an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
         * odd number).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
        bits = new long[(unitIndex(searchLen-1) + 1)];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
        length = searchLen;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        int start = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
        int step = smallSieve.sieveSearch(smallSieve.length, start);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
        int convertedStep = (step *2) + 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
        // Construct the large sieve at an even offset specified by base
2922
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
   113
        MutableBigInteger b = new MutableBigInteger(base);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
        MutableBigInteger q = new MutableBigInteger();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
        do {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
            // Calculate base mod convertedStep
2922
dd6d609861f0 6622432: RFE: Performance improvements to java.math.BigDecimal
xlu
parents: 2
diff changeset
   117
            start = b.divideOneWord(convertedStep, q);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
            // Take each multiple of step out of sieve
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
            start = convertedStep - start;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
            if (start%2 == 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
                start += convertedStep;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
            sieveSingle(searchLen, (start-1)/2, convertedStep);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
            // Find next prime from small sieve
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
            step = smallSieve.sieveSearch(smallSieve.length, step+1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
            convertedStep = (step *2) + 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
        } while (step > 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
     * Given a bit index return unit index containing it.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
    private static int unitIndex(int bitIndex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
        return bitIndex >>> 6;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
     * Return a unit that masks the specified bit in its unit.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
    private static long bit(int bitIndex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
        return 1L << (bitIndex & ((1<<6) - 1));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
     * Get the value of the bit at the specified index.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
    private boolean get(int bitIndex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
        int unitIndex = unitIndex(bitIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
        return ((bits[unitIndex] & bit(bitIndex)) != 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
     * Set the bit at the specified index.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
    private void set(int bitIndex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
        int unitIndex = unitIndex(bitIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
        bits[unitIndex] |= bit(bitIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
     * This method returns the index of the first clear bit in the search
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
     * array that occurs at or after start. It will not search past the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
     * specified limit. It returns -1 if there is no such clear bit.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
    private int sieveSearch(int limit, int start) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
        if (start >= limit)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
            return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
        int index = start;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
        do {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
            if (!get(index))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
                return index;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
            index++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
        } while(index < limit-1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
        return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
     * Sieve a single set of multiples out of the sieve. Begin to remove
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
     * multiples of the specified step starting at the specified start index,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
     * up to the specified limit.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
    private void sieveSingle(int limit, int start, int step) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
        while(start < limit) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
            set(start);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
            start += step;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
     * Test probable primes in the sieve and return successful candidates.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
    BigInteger retrieve(BigInteger initValue, int certainty, java.util.Random random) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
        // Examine the sieve one long at a time to find possible primes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
        int offset = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
        for (int i=0; i<bits.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
            long nextLong = ~bits[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
            for (int j=0; j<64; j++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
                if ((nextLong & 1) == 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
                    BigInteger candidate = initValue.add(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
                                           BigInteger.valueOf(offset));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
                    if (candidate.primeToCertainty(certainty, random))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
                        return candidate;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
                nextLong >>>= 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
                offset+=2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
}