Chris@42: /* Chris@42: * Copyright (c) 2003, 2007-14 Matteo Frigo Chris@42: * Copyright (c) 2003, 2007-14 Massachusetts Institute of Technology Chris@42: * Chris@42: * This program is free software; you can redistribute it and/or modify Chris@42: * it under the terms of the GNU General Public License as published by Chris@42: * the Free Software Foundation; either version 2 of the License, or Chris@42: * (at your option) any later version. Chris@42: * Chris@42: * This program is distributed in the hope that it will be useful, Chris@42: * but WITHOUT ANY WARRANTY; without even the implied warranty of Chris@42: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the Chris@42: * GNU General Public License for more details. Chris@42: * Chris@42: * You should have received a copy of the GNU General Public License Chris@42: * along with this program; if not, write to the Free Software Chris@42: * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA Chris@42: * Chris@42: */ Chris@42: Chris@42: Chris@42: #include "ifftw.h" Chris@42: Chris@42: /***************************************************************************/ Chris@42: Chris@42: /* Rader's algorithm requires lots of modular arithmetic, and if we Chris@42: aren't careful we can have errors due to integer overflows. */ Chris@42: Chris@42: /* Compute (x * y) mod p, but watch out for integer overflows; we must Chris@42: have 0 <= {x, y} < p. Chris@42: Chris@42: If overflow is common, this routine is somewhat slower than Chris@42: e.g. using 'long long' arithmetic. However, it has the advantage Chris@42: of working when INT is 64 bits, and is also faster when overflow is Chris@42: rare. FFTW calls this via the MULMOD macro, which further Chris@42: optimizes for the case of small integers. Chris@42: */ Chris@42: Chris@42: #define ADD_MOD(x, y, p) ((x) >= (p) - (y)) ? ((x) + ((y) - (p))) : ((x) + (y)) Chris@42: Chris@42: INT X(safe_mulmod)(INT x, INT y, INT p) Chris@42: { Chris@42: INT r; Chris@42: Chris@42: if (y > x) Chris@42: return X(safe_mulmod)(y, x, p); Chris@42: Chris@42: A(0 <= y && x < p); Chris@42: Chris@42: r = 0; Chris@42: while (y) { Chris@42: r = ADD_MOD(r, x*(y&1), p); y >>= 1; Chris@42: x = ADD_MOD(x, x, p); Chris@42: } Chris@42: Chris@42: return r; Chris@42: } Chris@42: Chris@42: /***************************************************************************/ Chris@42: Chris@42: /* Compute n^m mod p, where m >= 0 and p > 0. If we really cared, we Chris@42: could make this tail-recursive. */ Chris@42: Chris@42: INT X(power_mod)(INT n, INT m, INT p) Chris@42: { Chris@42: A(p > 0); Chris@42: if (m == 0) Chris@42: return 1; Chris@42: else if (m % 2 == 0) { Chris@42: INT x = X(power_mod)(n, m / 2, p); Chris@42: return MULMOD(x, x, p); Chris@42: } Chris@42: else Chris@42: return MULMOD(n, X(power_mod)(n, m - 1, p), p); Chris@42: } Chris@42: Chris@42: /* the following two routines were contributed by Greg Dionne. */ Chris@42: static INT get_prime_factors(INT n, INT *primef) Chris@42: { Chris@42: INT i; Chris@42: INT size = 0; Chris@42: Chris@42: A(n % 2 == 0); /* this routine is designed only for even n */ Chris@42: primef[size++] = (INT)2; Chris@42: do { Chris@42: n >>= 1; Chris@42: } while ((n & 1) == 0); Chris@42: Chris@42: if (n == 1) Chris@42: return size; Chris@42: Chris@42: for (i = 3; i * i <= n; i += 2) Chris@42: if (!(n % i)) { Chris@42: primef[size++] = i; Chris@42: do { Chris@42: n /= i; Chris@42: } while (!(n % i)); Chris@42: } Chris@42: if (n == 1) Chris@42: return size; Chris@42: primef[size++] = n; Chris@42: return size; Chris@42: } Chris@42: Chris@42: INT X(find_generator)(INT p) Chris@42: { Chris@42: INT n, i, size; Chris@42: INT primef[16]; /* smallest number = 32589158477190044730 > 2^64 */ Chris@42: INT pm1 = p - 1; Chris@42: Chris@42: if (p == 2) Chris@42: return 1; Chris@42: Chris@42: size = get_prime_factors(pm1, primef); Chris@42: n = 2; Chris@42: for (i = 0; i < size; i++) Chris@42: if (X(power_mod)(n, pm1 / primef[i], p) == 1) { Chris@42: i = -1; Chris@42: n++; Chris@42: } Chris@42: return n; Chris@42: } Chris@42: Chris@42: /* Return first prime divisor of n (It would be at best slightly faster to Chris@42: search a static table of primes; there are 6542 primes < 2^16.) */ Chris@42: INT X(first_divisor)(INT n) Chris@42: { Chris@42: INT i; Chris@42: if (n <= 1) Chris@42: return n; Chris@42: if (n % 2 == 0) Chris@42: return 2; Chris@42: for (i = 3; i*i <= n; i += 2) Chris@42: if (n % i == 0) Chris@42: return i; Chris@42: return n; Chris@42: } Chris@42: Chris@42: int X(is_prime)(INT n) Chris@42: { Chris@42: return(n > 1 && X(first_divisor)(n) == n); Chris@42: } Chris@42: Chris@42: INT X(next_prime)(INT n) Chris@42: { Chris@42: while (!X(is_prime)(n)) ++n; Chris@42: return n; Chris@42: } Chris@42: Chris@42: int X(factors_into)(INT n, const INT *primes) Chris@42: { Chris@42: for (; *primes != 0; ++primes) Chris@42: while ((n % *primes) == 0) Chris@42: n /= *primes; Chris@42: return (n == 1); Chris@42: } Chris@42: Chris@42: /* integer square root. Return floor(sqrt(N)) */ Chris@42: INT X(isqrt)(INT n) Chris@42: { Chris@42: INT guess, iguess; Chris@42: Chris@42: A(n >= 0); Chris@42: if (n == 0) return 0; Chris@42: Chris@42: guess = n; iguess = 1; Chris@42: Chris@42: do { Chris@42: guess = (guess + iguess) / 2; Chris@42: iguess = n / guess; Chris@42: } while (guess > iguess); Chris@42: Chris@42: return guess; Chris@42: } Chris@42: Chris@42: static INT isqrt_maybe(INT n) Chris@42: { Chris@42: INT guess = X(isqrt)(n); Chris@42: return guess * guess == n ? guess : 0; Chris@42: } Chris@42: Chris@42: #define divides(a, b) (((b) % (a)) == 0) Chris@42: INT X(choose_radix)(INT r, INT n) Chris@42: { Chris@42: if (r > 0) { Chris@42: if (divides(r, n)) return r; Chris@42: return 0; Chris@42: } else if (r == 0) { Chris@42: return X(first_divisor)(n); Chris@42: } else { Chris@42: /* r is negative. If n = (-r) * q^2, take q as the radix */ Chris@42: r = 0 - r; Chris@42: return (n > r && divides(r, n)) ? isqrt_maybe(n / r) : 0; Chris@42: } Chris@42: } Chris@42: Chris@42: /* return A mod N, works for all A including A < 0 */ Chris@42: INT X(modulo)(INT a, INT n) Chris@42: { Chris@42: A(n > 0); Chris@42: if (a >= 0) Chris@42: return a % n; Chris@42: else Chris@42: return (n - 1) - ((-(a + (INT)1)) % n); Chris@42: } Chris@42: Chris@42: /* TRUE if N factors into small primes */ Chris@42: int X(factors_into_small_primes)(INT n) Chris@42: { Chris@42: static const INT primes[] = { 2, 3, 5, 0 }; Chris@42: return X(factors_into)(n, primes); Chris@42: }