annotate fft/fftw/fftw-3.3.4/kernel/primes.c @ 40:223f770b5341 kissfft-double tip

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