Coverage Report

Created: 2025-07-18 06:52

/src/fftw3/kernel/primes.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * Copyright (c) 2003, 2007-14 Matteo Frigo
3
 * Copyright (c) 2003, 2007-14 Massachusetts Institute of Technology
4
 *
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.
9
 *
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.
14
 *
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., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
18
 *
19
 */
20
21
22
#include "kernel/ifftw.h"
23
24
/***************************************************************************/
25
26
/* Rader's algorithm requires lots of modular arithmetic, and if we
27
   aren't careful we can have errors due to integer overflows. */
28
29
/* Compute (x * y) mod p, but watch out for integer overflows; we must
30
   have 0 <= {x, y} < p.
31
32
   If overflow is common, this routine is somewhat slower than
33
   e.g. using 'long long' arithmetic.  However, it has the advantage
34
   of working when INT is 64 bits, and is also faster when overflow is
35
   rare.  FFTW calls this via the MULMOD macro, which further
36
   optimizes for the case of small integers. 
37
*/
38
39
0
#define ADD_MOD(x, y, p) ((x) >= (p) - (y)) ? ((x) + ((y) - (p))) : ((x) + (y))
40
41
INT X(safe_mulmod)(INT x, INT y, INT p)
42
0
{
43
0
     INT r;
44
45
0
     if (y > x) 
46
0
    return X(safe_mulmod)(y, x, p);
47
48
0
     A(0 <= y && x < p);
49
50
0
     r = 0;
51
0
     while (y) {
52
0
    r = ADD_MOD(r, x*(y&1), p); y >>= 1;
53
0
    x = ADD_MOD(x, x, p);
54
0
     }
55
56
0
     return r;
57
0
}
58
59
/***************************************************************************/
60
61
/* Compute n^m mod p, where m >= 0 and p > 0.  If we really cared, we
62
   could make this tail-recursive. */
63
64
INT X(power_mod)(INT n, INT m, INT p)
65
7.25k
{
66
7.25k
     A(p > 0);
67
7.25k
     if (m == 0)
68
2.13k
    return 1;
69
5.12k
     else if (m % 2 == 0) {
70
3.19k
    INT x = X(power_mod)(n, m / 2, p);
71
3.19k
    return MULMOD(x, x, p);
72
3.19k
     }
73
1.93k
     else
74
1.93k
    return MULMOD(n, X(power_mod)(n, m - 1, p), p);
75
7.25k
}
76
77
/* the following two routines were contributed by Greg Dionne. */
78
static INT get_prime_factors(INT n, INT *primef)
79
41
{
80
41
     INT i;
81
41
     INT size = 0;
82
83
41
     A(n % 2 == 0); /* this routine is designed only for even n */
84
41
     primef[size++] = (INT)2;
85
102
     do {
86
102
    n >>= 1;
87
102
     } while ((n & 1) == 0);
88
89
41
     if (n == 1)
90
0
    return size;
91
92
78
     for (i = 3; i * i <= n; i += 2)
93
37
    if (!(n % i)) {
94
34
         primef[size++] = i;
95
68
         do {
96
68
        n /= i;
97
68
         } while (!(n % i));
98
34
    }
99
41
     if (n == 1)
100
25
    return size;
101
16
     primef[size++] = n;
102
16
     return size;
103
41
}
104
105
INT X(find_generator)(INT p)
106
41
{
107
41
    INT n, i, size;
108
41
    INT primef[16];     /* smallest number = 32589158477190044730 > 2^64 */
109
41
    INT pm1 = p - 1;
110
111
41
    if (p == 2)
112
0
   return 1;
113
114
41
    size = get_prime_factors(pm1, primef);
115
41
    n = 2;
116
204
    for (i = 0; i < size; i++)
117
163
        if (X(power_mod)(n, pm1 / primef[i], p) == 1) {
118
64
            i = -1;
119
64
            n++;
120
64
        }
121
41
    return n;
122
41
}
123
124
/* Return first prime divisor of n  (It would be at best slightly faster to
125
   search a static table of primes; there are 6542 primes < 2^16.)  */
126
INT X(first_divisor)(INT n)
127
4.23k
{
128
4.23k
     INT i;
129
4.23k
     if (n <= 1)
130
0
    return n;
131
4.23k
     if (n % 2 == 0)
132
1.73k
    return 2;
133
5.81k
     for (i = 3; i*i <= n; i += 2)
134
4.19k
    if (n % i == 0)
135
875
         return i;
136
1.61k
     return n;
137
2.49k
}
138
139
int X(is_prime)(INT n)
140
3.23k
{
141
3.23k
     return(n > 1 && X(first_divisor)(n) == n);
142
3.23k
}
143
144
INT X(next_prime)(INT n)
145
1.24k
{
146
2.49k
     while (!X(is_prime)(n)) ++n;
147
1.24k
     return n;
148
1.24k
}
149
150
int X(factors_into)(INT n, const INT *primes)
151
896
{
152
3.58k
     for (; *primes != 0; ++primes) 
153
4.59k
    while ((n % *primes) == 0) 
154
1.90k
         n /= *primes;
155
896
     return (n == 1);
156
896
}
157
158
/* integer square root.  Return floor(sqrt(N)) */
159
INT X(isqrt)(INT n)
160
4.72k
{
161
4.72k
     INT guess, iguess;
162
163
4.72k
     A(n >= 0);
164
4.72k
     if (n == 0) return 0;
165
166
4.72k
     guess = n; iguess = 1;
167
168
15.1k
     do {
169
15.1k
          guess = (guess + iguess) / 2;
170
15.1k
    iguess = n / guess;
171
15.1k
     } while (guess > iguess);
172
173
4.72k
     return guess;
174
4.72k
}
175
176
static INT isqrt_maybe(INT n)
177
4.61k
{
178
4.61k
     INT guess = X(isqrt)(n);
179
4.61k
     return guess * guess == n ? guess : 0;
180
4.61k
}
181
182
78.6k
#define divides(a, b) (((b) % (a)) == 0)
183
INT X(choose_radix)(INT r, INT n)
184
84.2k
{
185
84.2k
     if (r > 0) {
186
63.2k
    if (divides(r, n)) return r;
187
53.1k
    return 0;
188
63.2k
     } else if (r == 0) {
189
998
    return X(first_divisor)(n);
190
20.0k
     } else {
191
    /* r is negative.  If n = (-r) * q^2, take q as the radix */
192
20.0k
    r = 0 - r;
193
20.0k
    return (n > r && divides(r, n)) ? isqrt_maybe(n / r) : 0;
194
20.0k
     }
195
84.2k
}
196
197
/* return A mod N, works for all A including A < 0 */
198
INT X(modulo)(INT a, INT n)
199
196
{
200
196
     A(n > 0);
201
196
     if (a >= 0)
202
48
    return a % n;
203
148
     else
204
148
    return (n - 1) - ((-(a + (INT)1)) % n);
205
196
}
206
207
/* TRUE if N factors into small primes */
208
int X(factors_into_small_primes)(INT n)
209
896
{
210
896
     static const INT primes[] = { 2, 3, 5, 0 };
211
896
     return X(factors_into)(n, primes);
212
896
}