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"
15 #pragma clang diagnostic ignored "-Wtautological-constant-out-of-range-compare"
18 _LIBCPP_BEGIN_NAMESPACE_STD
22 // handle all next_prime(i) for i in [1, 210), special case 0
23 const unsigned small_primes
[] =
75 // potential primes = 210*k + indices[i], k >= 1
76 // these numbers are not divisible by 2, 3, 5 or 7
77 // (or any integer 2 <= j <= 10 for that matter).
78 const unsigned indices
[] =
132 // Returns: If n == 0, returns 0. Else returns the lowest prime number that
133 // is greater than or equal to n.
135 // The algorithm creates a list of small primes, plus an open-ended list of
136 // potential primes. All prime numbers are potential prime numbers. However
137 // some potential prime numbers are not prime. In an ideal world, all potential
138 // prime numbers would be prime. Candidate prime numbers are chosen as the next
139 // highest potential prime. Then this number is tested for prime by dividing it
140 // by all potential prime numbers less than the sqrt of the candidate.
142 // This implementation defines potential primes as those numbers not divisible
143 // by 2, 3, 5, and 7. Other (common) implementations define potential primes
144 // as those not divisible by 2. A few other implementations define potential
145 // primes as those not divisible by 2 or 3. By raising the number of small
146 // primes which the potential prime is not divisible by, the set of potential
147 // primes more closely approximates the set of prime numbers. And thus there
148 // are fewer potential primes to search, and fewer potential primes to divide
151 template <size_t _Sz
= sizeof(size_t)>
152 inline _LIBCPP_INLINE_VISIBILITY
153 typename enable_if
<_Sz
== 4, void>::type
154 __check_for_overflow(size_t N
)
157 __throw_overflow_error("__next_prime overflow");
160 template <size_t _Sz
= sizeof(size_t)>
161 inline _LIBCPP_INLINE_VISIBILITY
162 typename enable_if
<_Sz
== 8, void>::type
163 __check_for_overflow(size_t N
)
165 if (N
> 0xFFFFFFFFFFFFFFC5ull
)
166 __throw_overflow_error("__next_prime overflow");
170 __next_prime(size_t n
)
172 const size_t L
= 210;
173 const size_t N
= sizeof(small_primes
) / sizeof(small_primes
[0]);
174 // If n is small enough, search in small_primes
175 if (n
<= small_primes
[N
-1])
176 return *std::lower_bound(small_primes
, small_primes
+ N
, n
);
177 // Else n > largest small_primes
178 // Check for overflow
179 __check_for_overflow(n
);
180 // Start searching list of potential primes: L * k0 + indices[in]
181 const size_t M
= sizeof(indices
) / sizeof(indices
[0]);
182 // Select first potential prime >= n
183 // Known a-priori n >= L
185 size_t in
= static_cast<size_t>(std::lower_bound(indices
, indices
+ M
, n
- k0
* L
)
187 n
= L
* k0
+ indices
[in
];
190 // Divide n by all primes or potential primes (i) until:
191 // 1. The division is even, so try next potential prime.
192 // 2. The i > sqrt(n), in which case n is prime.
193 // It is known a-priori that n is not divisible by 2, 3, 5 or 7,
194 // so don't test those (j == 5 -> divide by 11 first). And the
195 // potential primes start with 211, so don't test against the last
197 for (size_t j
= 5; j
< N
- 1; ++j
)
199 const std::size_t p
= small_primes
[j
];
200 const std::size_t q
= n
/ p
;
206 // n wasn't divisible by small primes, try potential primes
211 std::size_t q
= n
/ i
;
546 // This will loop i to the next "plane" of potential primes
551 // n is not prime. Increment n to next potential prime.
557 n
= L
* k0
+ indices
[in
];
561 _LIBCPP_END_NAMESPACE_STD