//===----------------------------------------------------------------------===// // // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. // See https://llvm.org/LICENSE.txt for license information. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception // //===----------------------------------------------------------------------===// #include <__hash_table> #include <algorithm> #include <stdexcept> #include <type_traits> _LIBCPP_CLANG_DIAGNOSTIC_IGNORED(…) _LIBCPP_BEGIN_NAMESPACE_STD namespace { // handle all next_prime(i) for i in [1, 210), special case 0 const unsigned small_primes[] = …; // potential primes = 210*k + indices[i], k >= 1 // these numbers are not divisible by 2, 3, 5 or 7 // (or any integer 2 <= j <= 10 for that matter). const unsigned indices[] = …; } // namespace // Returns: If n == 0, returns 0. Else returns the lowest prime number that // is greater than or equal to n. // // The algorithm creates a list of small primes, plus an open-ended list of // potential primes. All prime numbers are potential prime numbers. However // some potential prime numbers are not prime. In an ideal world, all potential // prime numbers would be prime. Candidate prime numbers are chosen as the next // highest potential prime. Then this number is tested for prime by dividing it // by all potential prime numbers less than the sqrt of the candidate. // // This implementation defines potential primes as those numbers not divisible // by 2, 3, 5, and 7. Other (common) implementations define potential primes // as those not divisible by 2. A few other implementations define potential // primes as those not divisible by 2 or 3. By raising the number of small // primes which the potential prime is not divisible by, the set of potential // primes more closely approximates the set of prime numbers. And thus there // are fewer potential primes to search, and fewer potential primes to divide // against. template <size_t _Sz = sizeof(size_t)> inline _LIBCPP_HIDE_FROM_ABI typename enable_if<_Sz == 4, void>::type __check_for_overflow(size_t N) { … } template <size_t _Sz = sizeof(size_t)> inline _LIBCPP_HIDE_FROM_ABI typename enable_if<_Sz == 8, void>::type __check_for_overflow(size_t N) { … } size_t __next_prime(size_t n) { … } _LIBCPP_END_NAMESPACE_STD