2
* Copyright (c) 2003, 2006 Matteo Frigo
3
* Copyright (c) 2003, 2006 Massachusetts Institute of Technology
5
* This program is free software; you can redistribute it and/or modify
6
* it under the terms of the GNU General Public License as published by
7
* the Free Software Foundation; either version 2 of the License, or
8
* (at your option) any later version.
10
* This program is distributed in the hope that it will be useful,
11
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
* GNU General Public License for more details.
15
* You should have received a copy of the GNU General Public License
16
* along with this program; if not, write to the Free Software
17
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21
/* $Id: primes.c,v 1.25 2006-02-05 23:19:55 athena Exp $ */
25
/***************************************************************************/
27
/* Rader's algorithm requires lots of modular arithmetic, and if we
28
aren't careful we can have errors due to integer overflows. */
30
/* Compute (x * y) mod p, but watch out for integer overflows; we must
33
If overflow is common, this routine is somewhat slower than
34
e.g. using 'long long' arithmetic. However, it has the advantage
35
of working when INT is 64 bits, and is also faster when overflow is
36
rare. FFTW calls this via the MULMOD macro, which further
37
optimizes for the case of small integers.
40
#define ADD_MOD(x, y, p) ((x) >= (p) - (y)) ? ((x) + ((y) - (p))) : ((x) + (y))
42
INT X(safe_mulmod)(INT x, INT y, INT p)
47
return X(safe_mulmod)(y, x, p);
53
r = ADD_MOD(r, x*(y&1), p); y >>= 1;
60
/***************************************************************************/
62
/* Compute n^m mod p, where m >= 0 and p > 0. If we really cared, we
63
could make this tail-recursive. */
65
INT X(power_mod)(INT n, INT m, INT p)
70
else if (m % 2 == 0) {
71
INT x = X(power_mod)(n, m / 2, p);
72
return MULMOD(x, x, p);
75
return MULMOD(n, X(power_mod)(n, m - 1, p), p);
78
/* the following two routines were contributed by Greg Dionne. */
79
static INT get_prime_factors(INT n, INT *primef)
84
A(n % 2 == 0); /* this routine is designed only for even n */
85
primef[size++] = (INT)2;
93
for (i = 3; i * i <= n; i += 2)
106
INT X(find_generator)(INT p)
109
INT primef[16]; /* smallest number = 32589158477190044730 > 2^64 */
115
size = get_prime_factors(pm1, primef);
117
for (i = 0; i < size; i++)
118
if (X(power_mod)(n, pm1 / primef[i], p) == 1) {
125
/* Return first prime divisor of n (It would be at best slightly faster to
126
search a static table of primes; there are 6542 primes < 2^16.) */
127
INT X(first_divisor)(INT n)
134
for (i = 3; i*i <= n; i += 2)
140
int X(is_prime)(INT n)
142
return(n > 1 && X(first_divisor)(n) == n);
145
INT X(next_prime)(INT n)
147
while (!X(is_prime)(n)) ++n;
151
int X(factors_into)(INT n, const INT *primes)
153
for (; *primes != 0; ++primes)
154
while ((n % *primes) == 0)
159
/* integer square root. Return floor(sqrt(N)) */
165
if (n == 0) return 0;
167
guess = n; iguess = 1;
170
guess = (guess + iguess) / 2;
172
} while (guess > iguess);
177
static INT isqrt_maybe(INT n)
179
INT guess = X(isqrt)(n);
180
return guess * guess == n ? guess : 0;
183
#define divides(a, b) (((b) % (a)) == 0)
184
INT X(choose_radix)(INT r, INT n)
187
if (divides(r, n)) return r;
190
return X(first_divisor)(n);
192
/* r is negative. If n = (-r) * q^2, take q as the radix */
194
return (n > r && divides(r, n)) ? isqrt_maybe(n / r) : 0;