1 //===----------------------------------------------------------------------===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 #include <__hash_table>
12 #include <type_traits>
14 _LIBCPP_CLANG_DIAGNOSTIC_IGNORED("-Wtautological-constant-out-of-range-compare")
16 _LIBCPP_BEGIN_NAMESPACE_STD
20 // handle all next_prime(i) for i in [1, 210), special case 0
21 const unsigned small_primes
[] = {
22 0, 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47,
23 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101, 103, 107, 109, 113, 127,
24 131, 137, 139, 149, 151, 157, 163, 167, 173, 179, 181, 191, 193, 197, 199, 211};
26 // potential primes = 210*k + indices[i], k >= 1
27 // these numbers are not divisible by 2, 3, 5 or 7
28 // (or any integer 2 <= j <= 10 for that matter).
29 const unsigned indices
[] = {
30 1, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67,
31 71, 73, 79, 83, 89, 97, 101, 103, 107, 109, 113, 121, 127, 131, 137, 139,
32 143, 149, 151, 157, 163, 167, 169, 173, 179, 181, 187, 191, 193, 197, 199, 209};
36 // Returns: If n == 0, returns 0. Else returns the lowest prime number that
37 // is greater than or equal to n.
39 // The algorithm creates a list of small primes, plus an open-ended list of
40 // potential primes. All prime numbers are potential prime numbers. However
41 // some potential prime numbers are not prime. In an ideal world, all potential
42 // prime numbers would be prime. Candidate prime numbers are chosen as the next
43 // highest potential prime. Then this number is tested for prime by dividing it
44 // by all potential prime numbers less than the sqrt of the candidate.
46 // This implementation defines potential primes as those numbers not divisible
47 // by 2, 3, 5, and 7. Other (common) implementations define potential primes
48 // as those not divisible by 2. A few other implementations define potential
49 // primes as those not divisible by 2 or 3. By raising the number of small
50 // primes which the potential prime is not divisible by, the set of potential
51 // primes more closely approximates the set of prime numbers. And thus there
52 // are fewer potential primes to search, and fewer potential primes to divide
55 template <size_t _Sz
= sizeof(size_t)>
56 inline _LIBCPP_HIDE_FROM_ABI typename enable_if
<_Sz
== 4, void>::type
__check_for_overflow(size_t N
) {
58 __throw_overflow_error("__next_prime overflow");
61 template <size_t _Sz
= sizeof(size_t)>
62 inline _LIBCPP_HIDE_FROM_ABI typename enable_if
<_Sz
== 8, void>::type
__check_for_overflow(size_t N
) {
63 if (N
> 0xFFFFFFFFFFFFFFC5ull
)
64 __throw_overflow_error("__next_prime overflow");
67 size_t __next_prime(size_t n
) {
69 const size_t N
= sizeof(small_primes
) / sizeof(small_primes
[0]);
70 // If n is small enough, search in small_primes
71 if (n
<= small_primes
[N
- 1])
72 return *std::lower_bound(small_primes
, small_primes
+ N
, n
);
73 // Else n > largest small_primes
75 __check_for_overflow(n
);
76 // Start searching list of potential primes: L * k0 + indices[in]
77 const size_t M
= sizeof(indices
) / sizeof(indices
[0]);
78 // Select first potential prime >= n
79 // Known a-priori n >= L
81 size_t in
= static_cast<size_t>(std::lower_bound(indices
, indices
+ M
, n
- k0
* L
) - indices
);
82 n
= L
* k0
+ indices
[in
];
84 // Divide n by all primes or potential primes (i) until:
85 // 1. The division is even, so try next potential prime.
86 // 2. The i > sqrt(n), in which case n is prime.
87 // It is known a-priori that n is not divisible by 2, 3, 5 or 7,
88 // so don't test those (j == 5 -> divide by 11 first). And the
89 // potential primes start with 211, so don't test against the last
91 for (size_t j
= 5; j
< N
- 1; ++j
) {
92 const std::size_t p
= small_primes
[j
];
93 const std::size_t q
= n
/ p
;
99 // n wasn't divisible by small primes, try potential primes
103 std::size_t q
= n
/ i
;
438 // This will loop i to the next "plane" of potential primes
443 // n is not prime. Increment n to next potential prime.
448 n
= L
* k0
+ indices
[in
];
452 _LIBCPP_END_NAMESPACE_STD