Coverage Report

Created: 2025-08-26 06:35

/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.83k
{
66
7.83k
     A(p > 0);
67
7.83k
     if (m == 0)
68
2.33k
    return 1;
69
5.49k
     else if (m % 2 == 0) {
70
3.36k
    INT x = X(power_mod)(n, m / 2, p);
71
3.36k
    return MULMOD(x, x, p);
72
3.36k
     }
73
2.13k
     else
74
2.13k
    return MULMOD(n, X(power_mod)(n, m - 1, p), p);
75
7.83k
}
76
77
/* the following two routines were contributed by Greg Dionne. */
78
static INT get_prime_factors(INT n, INT *primef)
79
40
{
80
40
     INT i;
81
40
     INT size = 0;
82
83
40
     A(n % 2 == 0); /* this routine is designed only for even n */
84
40
     primef[size++] = (INT)2;
85
98
     do {
86
98
    n >>= 1;
87
98
     } while ((n & 1) == 0);
88
89
40
     if (n == 1)
90
0
    return size;
91
92
77
     for (i = 3; i * i <= n; i += 2)
93
37
    if (!(n % i)) {
94
34
         primef[size++] = i;
95
67
         do {
96
67
        n /= i;
97
67
         } while (!(n % i));
98
34
    }
99
40
     if (n == 1)
100
23
    return size;
101
17
     primef[size++] = n;
102
17
     return size;
103
40
}
104
105
INT X(find_generator)(INT p)
106
40
{
107
40
    INT n, i, size;
108
40
    INT primef[16];     /* smallest number = 32589158477190044730 > 2^64 */
109
40
    INT pm1 = p - 1;
110
111
40
    if (p == 2)
112
0
   return 1;
113
114
40
    size = get_prime_factors(pm1, primef);
115
40
    n = 2;
116
202
    for (i = 0; i < size; i++)
117
162
        if (X(power_mod)(n, pm1 / primef[i], p) == 1) {
118
62
            i = -1;
119
62
            n++;
120
62
        }
121
40
    return n;
122
40
}
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.26k
{
128
4.26k
     INT i;
129
4.26k
     if (n <= 1)
130
0
    return n;
131
4.26k
     if (n % 2 == 0)
132
1.76k
    return 2;
133
5.85k
     for (i = 3; i*i <= n; i += 2)
134
4.22k
    if (n % i == 0)
135
864
         return i;
136
1.63k
     return n;
137
2.50k
}
138
139
int X(is_prime)(INT n)
140
3.25k
{
141
3.25k
     return(n > 1 && X(first_divisor)(n) == n);
142
3.25k
}
143
144
INT X(next_prime)(INT n)
145
1.25k
{
146
2.50k
     while (!X(is_prime)(n)) ++n;
147
1.25k
     return n;
148
1.25k
}
149
150
int X(factors_into)(INT n, const INT *primes)
151
924
{
152
3.69k
     for (; *primes != 0; ++primes) 
153
4.72k
    while ((n % *primes) == 0) 
154
1.94k
         n /= *primes;
155
924
     return (n == 1);
156
924
}
157
158
/* integer square root.  Return floor(sqrt(N)) */
159
INT X(isqrt)(INT n)
160
4.79k
{
161
4.79k
     INT guess, iguess;
162
163
4.79k
     A(n >= 0);
164
4.79k
     if (n == 0) return 0;
165
166
4.79k
     guess = n; iguess = 1;
167
168
15.3k
     do {
169
15.3k
          guess = (guess + iguess) / 2;
170
15.3k
    iguess = n / guess;
171
15.3k
     } while (guess > iguess);
172
173
4.79k
     return guess;
174
4.79k
}
175
176
static INT isqrt_maybe(INT n)
177
4.68k
{
178
4.68k
     INT guess = X(isqrt)(n);
179
4.68k
     return guess * guess == n ? guess : 0;
180
4.68k
}
181
182
79.0k
#define divides(a, b) (((b) % (a)) == 0)
183
INT X(choose_radix)(INT r, INT n)
184
84.7k
{
185
84.7k
     if (r > 0) {
186
63.4k
    if (divides(r, n)) return r;
187
53.3k
    return 0;
188
63.4k
     } else if (r == 0) {
189
1.00k
    return X(first_divisor)(n);
190
20.2k
     } else {
191
    /* r is negative.  If n = (-r) * q^2, take q as the radix */
192
20.2k
    r = 0 - r;
193
20.2k
    return (n > r && divides(r, n)) ? isqrt_maybe(n / r) : 0;
194
20.2k
     }
195
84.7k
}
196
197
/* return A mod N, works for all A including A < 0 */
198
INT X(modulo)(INT a, INT n)
199
200
{
200
200
     A(n > 0);
201
200
     if (a >= 0)
202
48
    return a % n;
203
152
     else
204
152
    return (n - 1) - ((-(a + (INT)1)) % n);
205
200
}
206
207
/* TRUE if N factors into small primes */
208
int X(factors_into_small_primes)(INT n)
209
924
{
210
924
     static const INT primes[] = { 2, 3, 5, 0 };
211
924
     return X(factors_into)(n, primes);
212
924
}