annotate src/fftw-3.3.3/kernel/primes.c @ 23:619f715526df sv_v2.1

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