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
[] =
73 // potential primes = 210*k + indices[i], k >= 1
74 // these numbers are not divisible by 2, 3, 5 or 7
75 // (or any integer 2 <= j <= 10 for that matter).
76 const unsigned indices
[] =
130 // Returns: If n == 0, returns 0. Else returns the lowest prime number that
131 // is greater than or equal to n.
133 // The algorithm creates a list of small primes, plus an open-ended list of
134 // potential primes. All prime numbers are potential prime numbers. However
135 // some potential prime numbers are not prime. In an ideal world, all potential
136 // prime numbers would be prime. Candidate prime numbers are chosen as the next
137 // highest potential prime. Then this number is tested for prime by dividing it
138 // by all potential prime numbers less than the sqrt of the candidate.
140 // This implementation defines potential primes as those numbers not divisible
141 // by 2, 3, 5, and 7. Other (common) implementations define potential primes
142 // as those not divisible by 2. A few other implementations define potential
143 // primes as those not divisible by 2 or 3. By raising the number of small
144 // primes which the potential prime is not divisible by, the set of potential
145 // primes more closely approximates the set of prime numbers. And thus there
146 // are fewer potential primes to search, and fewer potential primes to divide
149 template <size_t _Sz
= sizeof(size_t)>
150 inline _LIBCPP_INLINE_VISIBILITY
151 typename enable_if
<_Sz
== 4, void>::type
152 __check_for_overflow(size_t N
)
155 __throw_overflow_error("__next_prime overflow");
158 template <size_t _Sz
= sizeof(size_t)>
159 inline _LIBCPP_INLINE_VISIBILITY
160 typename enable_if
<_Sz
== 8, void>::type
161 __check_for_overflow(size_t N
)
163 if (N
> 0xFFFFFFFFFFFFFFC5ull
)
164 __throw_overflow_error("__next_prime overflow");
168 __next_prime(size_t n
)
170 const size_t L
= 210;
171 const size_t N
= sizeof(small_primes
) / sizeof(small_primes
[0]);
172 // If n is small enough, search in small_primes
173 if (n
<= small_primes
[N
-1])
174 return *std::lower_bound(small_primes
, small_primes
+ N
, n
);
175 // Else n > largest small_primes
176 // Check for overflow
177 __check_for_overflow(n
);
178 // Start searching list of potential primes: L * k0 + indices[in]
179 const size_t M
= sizeof(indices
) / sizeof(indices
[0]);
180 // Select first potential prime >= n
181 // Known a-priori n >= L
183 size_t in
= static_cast<size_t>(std::lower_bound(indices
, indices
+ M
, n
- k0
* L
)
185 n
= L
* k0
+ indices
[in
];
188 // Divide n by all primes or potential primes (i) until:
189 // 1. The division is even, so try next potential prime.
190 // 2. The i > sqrt(n), in which case n is prime.
191 // It is known a-priori that n is not divisible by 2, 3, 5 or 7,
192 // so don't test those (j == 5 -> divide by 11 first). And the
193 // potential primes start with 211, so don't test against the last
195 for (size_t j
= 5; j
< N
- 1; ++j
)
197 const std::size_t p
= small_primes
[j
];
198 const std::size_t q
= n
/ p
;
204 // n wasn't divisible by small primes, try potential primes
209 std::size_t q
= n
/ i
;
544 // This will loop i to the next "plane" of potential primes
549 // n is not prime. Increment n to next potential prime.
555 n
= L
* k0
+ indices
[in
];
559 _LIBCPP_END_NAMESPACE_STD