prime_numbers.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. #include <linux/module.h>
  3. #include <linux/mutex.h>
  4. #include <linux/prime_numbers.h>
  5. #include <linux/slab.h>
  6. #include "prime_numbers_private.h"
  7. #if BITS_PER_LONG == 64
  8. static const struct primes small_primes = {
  9. .last = 61,
  10. .sz = 64,
  11. .primes = {
  12. BIT(2) |
  13. BIT(3) |
  14. BIT(5) |
  15. BIT(7) |
  16. BIT(11) |
  17. BIT(13) |
  18. BIT(17) |
  19. BIT(19) |
  20. BIT(23) |
  21. BIT(29) |
  22. BIT(31) |
  23. BIT(37) |
  24. BIT(41) |
  25. BIT(43) |
  26. BIT(47) |
  27. BIT(53) |
  28. BIT(59) |
  29. BIT(61)
  30. }
  31. };
  32. #elif BITS_PER_LONG == 32
  33. static const struct primes small_primes = {
  34. .last = 31,
  35. .sz = 32,
  36. .primes = {
  37. BIT(2) |
  38. BIT(3) |
  39. BIT(5) |
  40. BIT(7) |
  41. BIT(11) |
  42. BIT(13) |
  43. BIT(17) |
  44. BIT(19) |
  45. BIT(23) |
  46. BIT(29) |
  47. BIT(31)
  48. }
  49. };
  50. #else
  51. #error "unhandled BITS_PER_LONG"
  52. #endif
  53. static DEFINE_MUTEX(lock);
  54. static const struct primes __rcu *primes = RCU_INITIALIZER(&small_primes);
  55. #if IS_ENABLED(CONFIG_PRIME_NUMBERS_KUNIT_TEST)
  56. /*
  57. * Calls the callback under RCU lock. The callback must not retain
  58. * the primes pointer.
  59. */
  60. void with_primes(void *ctx, primes_fn fn)
  61. {
  62. rcu_read_lock();
  63. fn(ctx, rcu_dereference(primes));
  64. rcu_read_unlock();
  65. }
  66. EXPORT_SYMBOL(with_primes);
  67. EXPORT_SYMBOL(slow_is_prime_number);
  68. #else
  69. static
  70. #endif
  71. bool slow_is_prime_number(unsigned long x)
  72. {
  73. unsigned long y = int_sqrt(x);
  74. while (y > 1) {
  75. if ((x % y) == 0)
  76. break;
  77. y--;
  78. }
  79. return y == 1;
  80. }
  81. static unsigned long slow_next_prime_number(unsigned long x)
  82. {
  83. while (x < ULONG_MAX && !slow_is_prime_number(++x))
  84. ;
  85. return x;
  86. }
  87. static unsigned long clear_multiples(unsigned long x,
  88. unsigned long *p,
  89. unsigned long start,
  90. unsigned long end)
  91. {
  92. unsigned long m;
  93. m = 2 * x;
  94. if (m < start)
  95. m = roundup(start, x);
  96. while (m < end) {
  97. __clear_bit(m, p);
  98. m += x;
  99. }
  100. return x;
  101. }
  102. static bool expand_to_next_prime(unsigned long x)
  103. {
  104. const struct primes *p;
  105. struct primes *new;
  106. unsigned long sz, y;
  107. /* Betrand's Postulate (or Chebyshev's theorem) states that if n > 3,
  108. * there is always at least one prime p between n and 2n - 2.
  109. * Equivalently, if n > 1, then there is always at least one prime p
  110. * such that n < p < 2n.
  111. *
  112. * http://mathworld.wolfram.com/BertrandsPostulate.html
  113. * https://en.wikipedia.org/wiki/Bertrand's_postulate
  114. */
  115. sz = 2 * x;
  116. if (sz < x)
  117. return false;
  118. sz = round_up(sz, BITS_PER_LONG);
  119. new = kmalloc(sizeof(*new) + bitmap_size(sz),
  120. GFP_KERNEL | __GFP_NOWARN);
  121. if (!new)
  122. return false;
  123. mutex_lock(&lock);
  124. p = rcu_dereference_protected(primes, lockdep_is_held(&lock));
  125. if (x < p->last) {
  126. kfree(new);
  127. goto unlock;
  128. }
  129. /* Where memory permits, track the primes using the
  130. * Sieve of Eratosthenes. The sieve is to remove all multiples of known
  131. * primes from the set, what remains in the set is therefore prime.
  132. */
  133. bitmap_fill(new->primes, sz);
  134. bitmap_copy(new->primes, p->primes, p->sz);
  135. for (y = 2UL; y < sz; y = find_next_bit(new->primes, sz, y + 1))
  136. new->last = clear_multiples(y, new->primes, p->sz, sz);
  137. new->sz = sz;
  138. BUG_ON(new->last <= x);
  139. rcu_assign_pointer(primes, new);
  140. if (p != &small_primes)
  141. kfree_rcu((struct primes *)p, rcu);
  142. unlock:
  143. mutex_unlock(&lock);
  144. return true;
  145. }
  146. static void free_primes(void)
  147. {
  148. const struct primes *p;
  149. mutex_lock(&lock);
  150. p = rcu_dereference_protected(primes, lockdep_is_held(&lock));
  151. if (p != &small_primes) {
  152. rcu_assign_pointer(primes, &small_primes);
  153. kfree_rcu((struct primes *)p, rcu);
  154. }
  155. mutex_unlock(&lock);
  156. }
  157. /**
  158. * next_prime_number - return the next prime number
  159. * @x: the starting point for searching to test
  160. *
  161. * A prime number is an integer greater than 1 that is only divisible by
  162. * itself and 1. The set of prime numbers is computed using the Sieve of
  163. * Eratoshenes (on finding a prime, all multiples of that prime are removed
  164. * from the set) enabling a fast lookup of the next prime number larger than
  165. * @x. If the sieve fails (memory limitation), the search falls back to using
  166. * slow trial-divison, up to the value of ULONG_MAX (which is reported as the
  167. * final prime as a sentinel).
  168. *
  169. * Returns: the next prime number larger than @x
  170. */
  171. unsigned long next_prime_number(unsigned long x)
  172. {
  173. const struct primes *p;
  174. rcu_read_lock();
  175. p = rcu_dereference(primes);
  176. while (x >= p->last) {
  177. rcu_read_unlock();
  178. if (!expand_to_next_prime(x))
  179. return slow_next_prime_number(x);
  180. rcu_read_lock();
  181. p = rcu_dereference(primes);
  182. }
  183. x = find_next_bit(p->primes, p->last, x + 1);
  184. rcu_read_unlock();
  185. return x;
  186. }
  187. EXPORT_SYMBOL(next_prime_number);
  188. /**
  189. * is_prime_number - test whether the given number is prime
  190. * @x: the number to test
  191. *
  192. * A prime number is an integer greater than 1 that is only divisible by
  193. * itself and 1. Internally a cache of prime numbers is kept (to speed up
  194. * searching for sequential primes, see next_prime_number()), but if the number
  195. * falls outside of that cache, its primality is tested using trial-divison.
  196. *
  197. * Returns: true if @x is prime, false for composite numbers.
  198. */
  199. bool is_prime_number(unsigned long x)
  200. {
  201. const struct primes *p;
  202. bool result;
  203. rcu_read_lock();
  204. p = rcu_dereference(primes);
  205. while (x >= p->sz) {
  206. rcu_read_unlock();
  207. if (!expand_to_next_prime(x))
  208. return slow_is_prime_number(x);
  209. rcu_read_lock();
  210. p = rcu_dereference(primes);
  211. }
  212. result = test_bit(x, p->primes);
  213. rcu_read_unlock();
  214. return result;
  215. }
  216. EXPORT_SYMBOL(is_prime_number);
  217. static void __exit primes_exit(void)
  218. {
  219. free_primes();
  220. }
  221. module_exit(primes_exit);
  222. MODULE_AUTHOR("Intel Corporation");
  223. MODULE_DESCRIPTION("Prime number library");
  224. MODULE_LICENSE("GPL");