Coverage Report

Created: 2026-08-31 06:25

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/fftw3/kernel/primes.c
Line
Count
Source
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.58k
{
66
7.58k
     A(p > 0);
67
7.58k
     if (m == 0)
68
2.24k
    return 1;
69
5.34k
     else if (m % 2 == 0) {
70
3.30k
    INT x = X(power_mod)(n, m / 2, p);
71
3.30k
    return MULMOD(x, x, p);
72
3.30k
     }
73
2.03k
     else
74
2.03k
    return MULMOD(n, X(power_mod)(n, m - 1, p), p);
75
7.58k
}
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
99
     do {
86
99
    n >>= 1;
87
99
     } while ((n & 1) == 0);
88
89
40
     if (n == 1)
90
0
    return size;
91
92
75
     for (i = 3; i * i <= n; i += 2)
93
35
    if (!(n % i)) {
94
33
         primef[size++] = i;
95
67
         do {
96
67
        n /= i;
97
67
         } while (!(n % i));
98
33
    }
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
204
    for (i = 0; i < size; i++)
117
164
        if (X(power_mod)(n, pm1 / primef[i], p) == 1) {
118
65
            i = -1;
119
65
            n++;
120
65
        }
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.28k
{
128
4.28k
     INT i;
129
4.28k
     if (n <= 1)
130
0
    return n;
131
4.28k
     if (n % 2 == 0)
132
1.77k
    return 2;
133
5.84k
     for (i = 3; i*i <= n; i += 2)
134
4.19k
    if (n % i == 0)
135
866
         return i;
136
1.64k
     return n;
137
2.51k
}
138
139
int X(is_prime)(INT n)
140
3.27k
{
141
3.27k
     return(n > 1 && X(first_divisor)(n) == n);
142
3.27k
}
143
144
INT X(next_prime)(INT n)
145
1.26k
{
146
2.52k
     while (!X(is_prime)(n)) ++n;
147
1.26k
     return n;
148
1.26k
}
149
150
int X(factors_into)(INT n, const INT *primes)
151
891
{
152
3.56k
     for (; *primes != 0; ++primes) 
153
4.56k
    while ((n % *primes) == 0) 
154
1.88k
         n /= *primes;
155
891
     return (n == 1);
156
891
}
157
158
/* integer square root.  Return floor(sqrt(N)) */
159
INT X(isqrt)(INT n)
160
4.80k
{
161
4.80k
     INT guess, iguess;
162
163
4.80k
     A(n >= 0);
164
4.80k
     if (n == 0) return 0;
165
166
4.80k
     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.80k
     return guess;
174
4.80k
}
175
176
static INT isqrt_maybe(INT n)
177
4.69k
{
178
4.69k
     INT guess = X(isqrt)(n);
179
4.69k
     return guess * guess == n ? guess : 0;
180
4.69k
}
181
182
79.6k
#define divides(a, b) (((b) % (a)) == 0)
183
INT X(choose_radix)(INT r, INT n)
184
85.4k
{
185
85.4k
     if (r > 0) {
186
64.0k
    if (divides(r, n)) return r;
187
53.8k
    return 0;
188
64.0k
     } else if (r == 0) {
189
1.00k
    return X(first_divisor)(n);
190
20.3k
     } else {
191
    /* r is negative.  If n = (-r) * q^2, take q as the radix */
192
20.3k
    r = 0 - r;
193
20.3k
    return (n > r && divides(r, n)) ? isqrt_maybe(n / r) : 0;
194
20.3k
     }
195
85.4k
}
196
197
/* return A mod N, works for all A including A < 0 */
198
INT X(modulo)(INT a, INT n)
199
198
{
200
198
     A(n > 0);
201
198
     if (a >= 0)
202
46
    return a % n;
203
152
     else
204
152
    return (n - 1) - ((-(a + (INT)1)) % n);
205
198
}
206
207
/* TRUE if N factors into small primes */
208
int X(factors_into_small_primes)(INT n)
209
891
{
210
891
     static const INT primes[] = { 2, 3, 5, 0 };
211
891
     return X(factors_into)(n, primes);
212
891
}