|
| 1 | +/* |
| 2 | + * java-math-library is a Java library focused on number theory, but not necessarily limited to it. It is based on the PSIQS 4.0 factoring project. |
| 3 | + * Copyright (C) 2018-2026 Tilman Neumann - tilman.neumann@web.de |
| 4 | + * |
| 5 | + * This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License |
| 6 | + * as published by the Free Software Foundation; either version 3 of the License, or (at your option) any later version. |
| 7 | + * |
| 8 | + * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied |
| 9 | + * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. |
| 10 | + * |
| 11 | + * You should have received a copy of the GNU General Public License along with this program; |
| 12 | + * if not, see <http://www.gnu.org/licenses/>. |
| 13 | + */ |
| 14 | +package de.tilman_neumann.jml.primes.exact; |
| 15 | + |
| 16 | +import org.apache.logging.log4j.Logger; |
| 17 | +import org.apache.logging.log4j.LogManager; |
| 18 | + |
| 19 | +import de.tilman_neumann.jml.BinarySearch; |
| 20 | + |
| 21 | +/** |
| 22 | + * A deterministic prime test for N < 32 bit using Lemire division. |
| 23 | + * |
| 24 | + * Implements the singleton pattern so that the resources will not be allocated twice no |
| 25 | + * matter how often the class is used. |
| 26 | + * |
| 27 | + * @author Tilman Neumann |
| 28 | + */ |
| 29 | +public class LemirePrimeTest { |
| 30 | + @SuppressWarnings("unused") |
| 31 | + private static final Logger LOG = LogManager.getLogger(LemirePrimeTest.class); |
| 32 | + |
| 33 | + private static final int MAX_N_BITS = 36; |
| 34 | + private static final int MAX_PRIME_FACTOR = 1 << ((MAX_N_BITS+1)/2); |
| 35 | + |
| 36 | + private static final BinarySearch binarySeach = new BinarySearch(); |
| 37 | + |
| 38 | + private static int MAX_INDEX; // for N<32 bit this would be 4792 |
| 39 | + |
| 40 | + private int[] primes; |
| 41 | + private long[] modularInverse; |
| 42 | + private long[] limits; |
| 43 | + |
| 44 | + // lazy-initialized singleton |
| 45 | + private static LemirePrimeTest the_instance = null; |
| 46 | + |
| 47 | + /** |
| 48 | + * @return the only TDivPrimeTest instance (singleton) |
| 49 | + */ |
| 50 | + public static synchronized final LemirePrimeTest getInstance() { |
| 51 | + if (the_instance == null) { |
| 52 | + the_instance = new LemirePrimeTest(); |
| 53 | + } |
| 54 | + return the_instance; |
| 55 | + } |
| 56 | + |
| 57 | + private LemirePrimeTest() { |
| 58 | + primes = SmallPrimes.generatePrimes(MAX_PRIME_FACTOR); |
| 59 | + MAX_INDEX = primes.length - 1; |
| 60 | + modularInverse = new long[primes.length]; |
| 61 | + limits = new long[primes.length]; |
| 62 | + for (int i = 0; i < primes.length; i++) { |
| 63 | + long p = primes[i]; |
| 64 | + // compute modular inverses of p (mod 2^64) using Newton's method |
| 65 | + long inverse = modularInverse(p); |
| 66 | + modularInverse[i] = inverse; |
| 67 | + // limit = (2^64 - 1) / p (unsigned) |
| 68 | + limits[i] = Long.divideUnsigned(-1L, p); |
| 69 | + } |
| 70 | + } |
| 71 | + |
| 72 | + private static long modularInverse(long n) { |
| 73 | + long inverse = n; // initial estimate |
| 74 | + for (int i = 0; i < 5; i++) { // 5 iterations are sufficient for 64 bit numbers |
| 75 | + inverse *= 2 - n * inverse; |
| 76 | + } |
| 77 | + return inverse; |
| 78 | + } |
| 79 | + |
| 80 | + // not public because isPrimeUnrolled() is faster |
| 81 | + boolean isPrime_v1(int N) { |
| 82 | + if (N==1) return false; |
| 83 | + if ((N&1)==0) return N==2; |
| 84 | + |
| 85 | + int pmax = (int) Math.sqrt(N); |
| 86 | + |
| 87 | + for (int i = 1; primes[i]<=pmax; i++) { |
| 88 | + // for hard numbers like big semiprimes finding a factor (early) is unlikely and JIT predicts that |
| 89 | + // the return branch is unlikely -> always the same data processing; preloading the arrays |
| 90 | + if (factorFound (N, i)) return false; |
| 91 | + } |
| 92 | + |
| 93 | + return true; |
| 94 | + } |
| 95 | + |
| 96 | + // not public because isPrimeUnrolled() is faster |
| 97 | + boolean isPrime_v2(int N) { |
| 98 | + if (N==1) return false; |
| 99 | + if ((N&1)==0) return N==2; |
| 100 | + |
| 101 | + int pmax = (int) Math.sqrt(N); |
| 102 | + int imax = binarySeach.getInsertPosition(primes, MAX_INDEX, pmax); |
| 103 | + |
| 104 | + for (int i = 1; i<=imax; i++) { |
| 105 | + // for hard numbers like big semiprimes finding a factor (early) is unlikely and JIT predicts that |
| 106 | + // the return branch is unlikely -> always the same data processing; preloading the arrays |
| 107 | + if (factorFound (N, i)) return false; |
| 108 | + } |
| 109 | + |
| 110 | + return true; |
| 111 | + } |
| 112 | + |
| 113 | + public boolean isPrime/*Unrolled*/(int N) { |
| 114 | + if (N==1) return false; |
| 115 | + if ((N&1)==0) return N==2; |
| 116 | + |
| 117 | + int pmax = (int) Math.sqrt(N); |
| 118 | + int imax = binarySeach.getInsertPosition(primes, MAX_INDEX, pmax); |
| 119 | + |
| 120 | + int i=1; |
| 121 | + int unrolledLimit = imax-8; |
| 122 | + for ( ; i<unrolledLimit; i++) { |
| 123 | + if (factorFound (N, i)) return false; |
| 124 | + if (factorFound (N, ++i)) return false; |
| 125 | + if (factorFound (N, ++i)) return false; |
| 126 | + if (factorFound (N, ++i)) return false; |
| 127 | + if (factorFound (N, ++i)) return false; |
| 128 | + if (factorFound (N, ++i)) return false; |
| 129 | + if (factorFound (N, ++i)) return false; |
| 130 | + if (factorFound (N, ++i)) return false; |
| 131 | + } |
| 132 | + for ( ; i<imax; i++) { |
| 133 | + if (factorFound (N, i)) return false; |
| 134 | + } |
| 135 | + |
| 136 | + return true; |
| 137 | + } |
| 138 | + |
| 139 | + private boolean factorFound(long N, int i) { |
| 140 | + // 1. get pre-computed inverse and limit |
| 141 | + long inv = modularInverse[i]; |
| 142 | + long limit = limits[i]; |
| 143 | + |
| 144 | + // 2. multiply number * inverse (overflow is intended!) |
| 145 | + long product = N * inv; |
| 146 | + |
| 147 | + // 3. if the (unsigned) product is less than or equal to the limit, then primes[i] divides N without rest. |
| 148 | + return Long.compareUnsigned(product, limit) <= 0; |
| 149 | + } |
| 150 | +} |
0 commit comments