xref: /OK3568_Linux_fs/kernel/include/linux/prime_numbers.h (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun /* SPDX-License-Identifier: GPL-2.0 */
2*4882a593Smuzhiyun #ifndef __LINUX_PRIME_NUMBERS_H
3*4882a593Smuzhiyun #define __LINUX_PRIME_NUMBERS_H
4*4882a593Smuzhiyun 
5*4882a593Smuzhiyun #include <linux/types.h>
6*4882a593Smuzhiyun 
7*4882a593Smuzhiyun bool is_prime_number(unsigned long x);
8*4882a593Smuzhiyun unsigned long next_prime_number(unsigned long x);
9*4882a593Smuzhiyun 
10*4882a593Smuzhiyun /**
11*4882a593Smuzhiyun  * for_each_prime_number - iterate over each prime upto a value
12*4882a593Smuzhiyun  * @prime: the current prime number in this iteration
13*4882a593Smuzhiyun  * @max: the upper limit
14*4882a593Smuzhiyun  *
15*4882a593Smuzhiyun  * Starting from the first prime number 2 iterate over each prime number up to
16*4882a593Smuzhiyun  * the @max value. On each iteration, @prime is set to the current prime number.
17*4882a593Smuzhiyun  * @max should be less than ULONG_MAX to ensure termination. To begin with
18*4882a593Smuzhiyun  * @prime set to 1 on the first iteration use for_each_prime_number_from()
19*4882a593Smuzhiyun  * instead.
20*4882a593Smuzhiyun  */
21*4882a593Smuzhiyun #define for_each_prime_number(prime, max) \
22*4882a593Smuzhiyun 	for_each_prime_number_from((prime), 2, (max))
23*4882a593Smuzhiyun 
24*4882a593Smuzhiyun /**
25*4882a593Smuzhiyun  * for_each_prime_number_from - iterate over each prime upto a value
26*4882a593Smuzhiyun  * @prime: the current prime number in this iteration
27*4882a593Smuzhiyun  * @from: the initial value
28*4882a593Smuzhiyun  * @max: the upper limit
29*4882a593Smuzhiyun  *
30*4882a593Smuzhiyun  * Starting from @from iterate over each successive prime number up to the
31*4882a593Smuzhiyun  * @max value. On each iteration, @prime is set to the current prime number.
32*4882a593Smuzhiyun  * @max should be less than ULONG_MAX, and @from less than @max, to ensure
33*4882a593Smuzhiyun  * termination.
34*4882a593Smuzhiyun  */
35*4882a593Smuzhiyun #define for_each_prime_number_from(prime, from, max) \
36*4882a593Smuzhiyun 	for (prime = (from); prime <= (max); prime = next_prime_number(prime))
37*4882a593Smuzhiyun 
38*4882a593Smuzhiyun #endif /* !__LINUX_PRIME_NUMBERS_H */
39