1 //===-------------------------- hash.cpp ----------------------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is dual licensed under the MIT and the University of Illinois Open
6 // Source Licenses. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #include "__hash_table"
14 _LIBCPP_BEGIN_NAMESPACE_STD
18 // handle all next_prime(i) for i in [1, 210), special case 0
19 const unsigned small_primes[] =
71 // potential primes = 210*k + indices[i], k >= 1
72 // these numbers are not divisible by 2, 3, 5 or 7
73 // (or any integer 2 <= j <= 10 for that matter).
74 const unsigned indices[] =
128 // Returns: If n == 0, returns 0. Else returns the lowest prime number that
129 // is greater than or equal to n.
131 // The algorithm creates a list of small primes, plus an open-ended list of
132 // potential primes. All prime numbers are potential prime numbers. However
133 // some potential prime numbers are not prime. In an ideal world, all potential
134 // prime numbers would be prime. Candiate prime numbers are chosen as the next
135 // highest potential prime. Then this number is tested for prime by dividing it
136 // by all potential prime numbers less than the sqrt of the candidate.
138 // This implementation defines potential primes as those numbers not divisible
139 // by 2, 3, 5, and 7. Other (common) implementations define potential primes
140 // as those not divisible by 2. A few other implementations define potential
141 // primes as those not divisible by 2 or 3. By raising the number of small
142 // primes which the potential prime is not divisible by, the set of potential
143 // primes more closely approximates the set of prime numbers. And thus there
144 // are fewer potential primes to search, and fewer potential primes to divide
147 inline _LIBCPP_INLINE_VISIBILITY
149 __check_for_overflow(size_t N, integral_constant<size_t, 32>)
151 #ifndef _LIBCPP_NO_EXCEPTIONS
153 throw overflow_error("__next_prime overflow");
157 inline _LIBCPP_INLINE_VISIBILITY
159 __check_for_overflow(size_t N, integral_constant<size_t, 64>)
161 #ifndef _LIBCPP_NO_EXCEPTIONS
162 if (N > 0xFFFFFFFFFFFFFFC5ull)
163 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, integral_constant<size_t,
178 sizeof(n) * __CHAR_BIT__>());
179 // Start searching list of potential primes: L * k0 + indices[in]
180 const size_t M = sizeof(indices) / sizeof(indices[0]);
181 // Select first potential prime >= n
182 // Known a-priori n >= L
184 size_t in = static_cast<size_t>(std::lower_bound(indices, indices + M, n - k0 * L)
186 n = L * k0 + indices[in];
189 // Divide n by all primes or potential primes (i) until:
190 // 1. The division is even, so try next potential prime.
191 // 2. The i > sqrt(n), in which case n is prime.
192 // It is known a-priori that n is not divisible by 2, 3, 5 or 7,
193 // so don't test those (j == 5 -> divide by 11 first). And the
194 // potential primes start with 211, so don't test against the last
196 for (size_t j = 5; j < N - 1; ++j)
198 const std::size_t p = small_primes[j];
199 const std::size_t q = n / p;
205 // n wasn't divisible by small primes, try potential primes
210 std::size_t q = n / i;
545 // This will loop i to the next "plane" of potential primes
550 // n is not prime. Increment n to next potential prime.
556 n = L * k0 + indices[in];
560 _LIBCPP_END_NAMESPACE_STD