annotate src/fftw-3.3.5/kernel/primes.c @ 82:d0c2a83c1364

Add FFTW 3.3.8 source, and a Linux build
author Chris Cannam
date Tue, 19 Nov 2019 14:52:55 +0000
parents 2cd0e3b3e1fd
children
rev   line source
Chris@42 1 /*
Chris@42 2 * Copyright (c) 2003, 2007-14 Matteo Frigo
Chris@42 3 * Copyright (c) 2003, 2007-14 Massachusetts Institute of Technology
Chris@42 4 *
Chris@42 5 * This program is free software; you can redistribute it and/or modify
Chris@42 6 * it under the terms of the GNU General Public License as published by
Chris@42 7 * the Free Software Foundation; either version 2 of the License, or
Chris@42 8 * (at your option) any later version.
Chris@42 9 *
Chris@42 10 * This program is distributed in the hope that it will be useful,
Chris@42 11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
Chris@42 12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
Chris@42 13 * GNU General Public License for more details.
Chris@42 14 *
Chris@42 15 * You should have received a copy of the GNU General Public License
Chris@42 16 * along with this program; if not, write to the Free Software
Chris@42 17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
Chris@42 18 *
Chris@42 19 */
Chris@42 20
Chris@42 21
Chris@42 22 #include "ifftw.h"
Chris@42 23
Chris@42 24 /***************************************************************************/
Chris@42 25
Chris@42 26 /* Rader's algorithm requires lots of modular arithmetic, and if we
Chris@42 27 aren't careful we can have errors due to integer overflows. */
Chris@42 28
Chris@42 29 /* Compute (x * y) mod p, but watch out for integer overflows; we must
Chris@42 30 have 0 <= {x, y} < p.
Chris@42 31
Chris@42 32 If overflow is common, this routine is somewhat slower than
Chris@42 33 e.g. using 'long long' arithmetic. However, it has the advantage
Chris@42 34 of working when INT is 64 bits, and is also faster when overflow is
Chris@42 35 rare. FFTW calls this via the MULMOD macro, which further
Chris@42 36 optimizes for the case of small integers.
Chris@42 37 */
Chris@42 38
Chris@42 39 #define ADD_MOD(x, y, p) ((x) >= (p) - (y)) ? ((x) + ((y) - (p))) : ((x) + (y))
Chris@42 40
Chris@42 41 INT X(safe_mulmod)(INT x, INT y, INT p)
Chris@42 42 {
Chris@42 43 INT r;
Chris@42 44
Chris@42 45 if (y > x)
Chris@42 46 return X(safe_mulmod)(y, x, p);
Chris@42 47
Chris@42 48 A(0 <= y && x < p);
Chris@42 49
Chris@42 50 r = 0;
Chris@42 51 while (y) {
Chris@42 52 r = ADD_MOD(r, x*(y&1), p); y >>= 1;
Chris@42 53 x = ADD_MOD(x, x, p);
Chris@42 54 }
Chris@42 55
Chris@42 56 return r;
Chris@42 57 }
Chris@42 58
Chris@42 59 /***************************************************************************/
Chris@42 60
Chris@42 61 /* Compute n^m mod p, where m >= 0 and p > 0. If we really cared, we
Chris@42 62 could make this tail-recursive. */
Chris@42 63
Chris@42 64 INT X(power_mod)(INT n, INT m, INT p)
Chris@42 65 {
Chris@42 66 A(p > 0);
Chris@42 67 if (m == 0)
Chris@42 68 return 1;
Chris@42 69 else if (m % 2 == 0) {
Chris@42 70 INT x = X(power_mod)(n, m / 2, p);
Chris@42 71 return MULMOD(x, x, p);
Chris@42 72 }
Chris@42 73 else
Chris@42 74 return MULMOD(n, X(power_mod)(n, m - 1, p), p);
Chris@42 75 }
Chris@42 76
Chris@42 77 /* the following two routines were contributed by Greg Dionne. */
Chris@42 78 static INT get_prime_factors(INT n, INT *primef)
Chris@42 79 {
Chris@42 80 INT i;
Chris@42 81 INT size = 0;
Chris@42 82
Chris@42 83 A(n % 2 == 0); /* this routine is designed only for even n */
Chris@42 84 primef[size++] = (INT)2;
Chris@42 85 do {
Chris@42 86 n >>= 1;
Chris@42 87 } while ((n & 1) == 0);
Chris@42 88
Chris@42 89 if (n == 1)
Chris@42 90 return size;
Chris@42 91
Chris@42 92 for (i = 3; i * i <= n; i += 2)
Chris@42 93 if (!(n % i)) {
Chris@42 94 primef[size++] = i;
Chris@42 95 do {
Chris@42 96 n /= i;
Chris@42 97 } while (!(n % i));
Chris@42 98 }
Chris@42 99 if (n == 1)
Chris@42 100 return size;
Chris@42 101 primef[size++] = n;
Chris@42 102 return size;
Chris@42 103 }
Chris@42 104
Chris@42 105 INT X(find_generator)(INT p)
Chris@42 106 {
Chris@42 107 INT n, i, size;
Chris@42 108 INT primef[16]; /* smallest number = 32589158477190044730 > 2^64 */
Chris@42 109 INT pm1 = p - 1;
Chris@42 110
Chris@42 111 if (p == 2)
Chris@42 112 return 1;
Chris@42 113
Chris@42 114 size = get_prime_factors(pm1, primef);
Chris@42 115 n = 2;
Chris@42 116 for (i = 0; i < size; i++)
Chris@42 117 if (X(power_mod)(n, pm1 / primef[i], p) == 1) {
Chris@42 118 i = -1;
Chris@42 119 n++;
Chris@42 120 }
Chris@42 121 return n;
Chris@42 122 }
Chris@42 123
Chris@42 124 /* Return first prime divisor of n (It would be at best slightly faster to
Chris@42 125 search a static table of primes; there are 6542 primes < 2^16.) */
Chris@42 126 INT X(first_divisor)(INT n)
Chris@42 127 {
Chris@42 128 INT i;
Chris@42 129 if (n <= 1)
Chris@42 130 return n;
Chris@42 131 if (n % 2 == 0)
Chris@42 132 return 2;
Chris@42 133 for (i = 3; i*i <= n; i += 2)
Chris@42 134 if (n % i == 0)
Chris@42 135 return i;
Chris@42 136 return n;
Chris@42 137 }
Chris@42 138
Chris@42 139 int X(is_prime)(INT n)
Chris@42 140 {
Chris@42 141 return(n > 1 && X(first_divisor)(n) == n);
Chris@42 142 }
Chris@42 143
Chris@42 144 INT X(next_prime)(INT n)
Chris@42 145 {
Chris@42 146 while (!X(is_prime)(n)) ++n;
Chris@42 147 return n;
Chris@42 148 }
Chris@42 149
Chris@42 150 int X(factors_into)(INT n, const INT *primes)
Chris@42 151 {
Chris@42 152 for (; *primes != 0; ++primes)
Chris@42 153 while ((n % *primes) == 0)
Chris@42 154 n /= *primes;
Chris@42 155 return (n == 1);
Chris@42 156 }
Chris@42 157
Chris@42 158 /* integer square root. Return floor(sqrt(N)) */
Chris@42 159 INT X(isqrt)(INT n)
Chris@42 160 {
Chris@42 161 INT guess, iguess;
Chris@42 162
Chris@42 163 A(n >= 0);
Chris@42 164 if (n == 0) return 0;
Chris@42 165
Chris@42 166 guess = n; iguess = 1;
Chris@42 167
Chris@42 168 do {
Chris@42 169 guess = (guess + iguess) / 2;
Chris@42 170 iguess = n / guess;
Chris@42 171 } while (guess > iguess);
Chris@42 172
Chris@42 173 return guess;
Chris@42 174 }
Chris@42 175
Chris@42 176 static INT isqrt_maybe(INT n)
Chris@42 177 {
Chris@42 178 INT guess = X(isqrt)(n);
Chris@42 179 return guess * guess == n ? guess : 0;
Chris@42 180 }
Chris@42 181
Chris@42 182 #define divides(a, b) (((b) % (a)) == 0)
Chris@42 183 INT X(choose_radix)(INT r, INT n)
Chris@42 184 {
Chris@42 185 if (r > 0) {
Chris@42 186 if (divides(r, n)) return r;
Chris@42 187 return 0;
Chris@42 188 } else if (r == 0) {
Chris@42 189 return X(first_divisor)(n);
Chris@42 190 } else {
Chris@42 191 /* r is negative. If n = (-r) * q^2, take q as the radix */
Chris@42 192 r = 0 - r;
Chris@42 193 return (n > r && divides(r, n)) ? isqrt_maybe(n / r) : 0;
Chris@42 194 }
Chris@42 195 }
Chris@42 196
Chris@42 197 /* return A mod N, works for all A including A < 0 */
Chris@42 198 INT X(modulo)(INT a, INT n)
Chris@42 199 {
Chris@42 200 A(n > 0);
Chris@42 201 if (a >= 0)
Chris@42 202 return a % n;
Chris@42 203 else
Chris@42 204 return (n - 1) - ((-(a + (INT)1)) % n);
Chris@42 205 }
Chris@42 206
Chris@42 207 /* TRUE if N factors into small primes */
Chris@42 208 int X(factors_into_small_primes)(INT n)
Chris@42 209 {
Chris@42 210 static const INT primes[] = { 2, 3, 5, 0 };
Chris@42 211 return X(factors_into)(n, primes);
Chris@42 212 }