Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1 | //===-------------------------- hash.cpp ----------------------------------===// |
| 2 | // |
Howard Hinnant | f5256e1 | 2010-05-11 21:36:01 +0000 | [diff] [blame] | 3 | // The LLVM Compiler Infrastructure |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 4 | // |
Howard Hinnant | b64f8b0 | 2010-11-16 22:09:02 +0000 | [diff] [blame] | 5 | // This file is dual licensed under the MIT and the University of Illinois Open |
| 6 | // Source Licenses. See LICENSE.TXT for details. |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | |
| 10 | #include "__hash_table" |
| 11 | #include "algorithm" |
Howard Hinnant | e87ad17 | 2010-10-29 14:10:30 +0000 | [diff] [blame] | 12 | #include "stdexcept" |
Howard Hinnant | 0aa900e | 2012-12-27 18:59:05 +0000 | [diff] [blame] | 13 | #include "type_traits" |
| 14 | |
Joerg Sonnenberger | 006ab1e | 2013-04-27 19:10:15 +0000 | [diff] [blame] | 15 | #ifdef __clang__ |
Howard Hinnant | 0aa900e | 2012-12-27 18:59:05 +0000 | [diff] [blame] | 16 | #pragma clang diagnostic ignored "-Wtautological-constant-out-of-range-compare" |
Joerg Sonnenberger | 006ab1e | 2013-04-27 19:10:15 +0000 | [diff] [blame] | 17 | #endif |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 18 | |
| 19 | _LIBCPP_BEGIN_NAMESPACE_STD |
| 20 | |
| 21 | namespace { |
| 22 | |
| 23 | // handle all next_prime(i) for i in [1, 210), special case 0 |
| 24 | const unsigned small_primes[] = |
| 25 | { |
| 26 | 0, |
| 27 | 2, |
| 28 | 3, |
| 29 | 5, |
| 30 | 7, |
| 31 | 11, |
| 32 | 13, |
| 33 | 17, |
| 34 | 19, |
| 35 | 23, |
| 36 | 29, |
| 37 | 31, |
| 38 | 37, |
| 39 | 41, |
| 40 | 43, |
| 41 | 47, |
| 42 | 53, |
| 43 | 59, |
| 44 | 61, |
| 45 | 67, |
| 46 | 71, |
| 47 | 73, |
| 48 | 79, |
| 49 | 83, |
| 50 | 89, |
| 51 | 97, |
| 52 | 101, |
| 53 | 103, |
| 54 | 107, |
| 55 | 109, |
| 56 | 113, |
| 57 | 127, |
| 58 | 131, |
| 59 | 137, |
| 60 | 139, |
| 61 | 149, |
| 62 | 151, |
| 63 | 157, |
| 64 | 163, |
| 65 | 167, |
| 66 | 173, |
| 67 | 179, |
| 68 | 181, |
| 69 | 191, |
| 70 | 193, |
| 71 | 197, |
| 72 | 199, |
| 73 | 211 |
| 74 | }; |
| 75 | |
| 76 | // potential primes = 210*k + indices[i], k >= 1 |
| 77 | // these numbers are not divisible by 2, 3, 5 or 7 |
| 78 | // (or any integer 2 <= j <= 10 for that matter). |
| 79 | const unsigned indices[] = |
| 80 | { |
| 81 | 1, |
| 82 | 11, |
| 83 | 13, |
| 84 | 17, |
| 85 | 19, |
| 86 | 23, |
| 87 | 29, |
| 88 | 31, |
| 89 | 37, |
| 90 | 41, |
| 91 | 43, |
| 92 | 47, |
| 93 | 53, |
| 94 | 59, |
| 95 | 61, |
| 96 | 67, |
| 97 | 71, |
| 98 | 73, |
| 99 | 79, |
| 100 | 83, |
| 101 | 89, |
| 102 | 97, |
| 103 | 101, |
| 104 | 103, |
| 105 | 107, |
| 106 | 109, |
| 107 | 113, |
| 108 | 121, |
| 109 | 127, |
| 110 | 131, |
| 111 | 137, |
| 112 | 139, |
| 113 | 143, |
| 114 | 149, |
| 115 | 151, |
| 116 | 157, |
| 117 | 163, |
| 118 | 167, |
| 119 | 169, |
| 120 | 173, |
| 121 | 179, |
| 122 | 181, |
| 123 | 187, |
| 124 | 191, |
| 125 | 193, |
| 126 | 197, |
| 127 | 199, |
| 128 | 209 |
| 129 | }; |
| 130 | |
| 131 | } |
| 132 | |
| 133 | // Returns: If n == 0, returns 0. Else returns the lowest prime number that |
| 134 | // is greater than or equal to n. |
Howard Hinnant | 16e6e1d | 2010-08-22 00:03:27 +0000 | [diff] [blame] | 135 | // |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 136 | // The algorithm creates a list of small primes, plus an open-ended list of |
| 137 | // potential primes. All prime numbers are potential prime numbers. However |
| 138 | // some potential prime numbers are not prime. In an ideal world, all potential |
Alp Toker | ec34c48 | 2014-05-15 11:27:39 +0000 | [diff] [blame] | 139 | // prime numbers would be prime. Candidate prime numbers are chosen as the next |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 140 | // highest potential prime. Then this number is tested for prime by dividing it |
| 141 | // by all potential prime numbers less than the sqrt of the candidate. |
Howard Hinnant | 16e6e1d | 2010-08-22 00:03:27 +0000 | [diff] [blame] | 142 | // |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 143 | // This implementation defines potential primes as those numbers not divisible |
| 144 | // by 2, 3, 5, and 7. Other (common) implementations define potential primes |
| 145 | // as those not divisible by 2. A few other implementations define potential |
| 146 | // primes as those not divisible by 2 or 3. By raising the number of small |
| 147 | // primes which the potential prime is not divisible by, the set of potential |
| 148 | // primes more closely approximates the set of prime numbers. And thus there |
| 149 | // are fewer potential primes to search, and fewer potential primes to divide |
| 150 | // against. |
| 151 | |
Howard Hinnant | 0aa900e | 2012-12-27 18:59:05 +0000 | [diff] [blame] | 152 | template <size_t _Sz = sizeof(size_t)> |
Howard Hinnant | e87ad17 | 2010-10-29 14:10:30 +0000 | [diff] [blame] | 153 | inline _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 0aa900e | 2012-12-27 18:59:05 +0000 | [diff] [blame] | 154 | typename enable_if<_Sz == 4, void>::type |
| 155 | __check_for_overflow(size_t N) |
Howard Hinnant | e87ad17 | 2010-10-29 14:10:30 +0000 | [diff] [blame] | 156 | { |
Howard Hinnant | 0aa900e | 2012-12-27 18:59:05 +0000 | [diff] [blame] | 157 | #ifndef _LIBCPP_NO_EXCEPTIONS |
Howard Hinnant | e87ad17 | 2010-10-29 14:10:30 +0000 | [diff] [blame] | 158 | if (N > 0xFFFFFFFB) |
| 159 | throw overflow_error("__next_prime overflow"); |
Howard Hinnant | db4d478 | 2013-03-28 18:56:26 +0000 | [diff] [blame] | 160 | #else |
| 161 | (void)N; |
Howard Hinnant | e87ad17 | 2010-10-29 14:10:30 +0000 | [diff] [blame] | 162 | #endif |
| 163 | } |
| 164 | |
Howard Hinnant | 0aa900e | 2012-12-27 18:59:05 +0000 | [diff] [blame] | 165 | template <size_t _Sz = sizeof(size_t)> |
Howard Hinnant | e87ad17 | 2010-10-29 14:10:30 +0000 | [diff] [blame] | 166 | inline _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 0aa900e | 2012-12-27 18:59:05 +0000 | [diff] [blame] | 167 | typename enable_if<_Sz == 8, void>::type |
| 168 | __check_for_overflow(size_t N) |
Howard Hinnant | e87ad17 | 2010-10-29 14:10:30 +0000 | [diff] [blame] | 169 | { |
Howard Hinnant | 0aa900e | 2012-12-27 18:59:05 +0000 | [diff] [blame] | 170 | #ifndef _LIBCPP_NO_EXCEPTIONS |
Howard Hinnant | e87ad17 | 2010-10-29 14:10:30 +0000 | [diff] [blame] | 171 | if (N > 0xFFFFFFFFFFFFFFC5ull) |
| 172 | throw overflow_error("__next_prime overflow"); |
Howard Hinnant | db4d478 | 2013-03-28 18:56:26 +0000 | [diff] [blame] | 173 | #else |
| 174 | (void)N; |
Howard Hinnant | e87ad17 | 2010-10-29 14:10:30 +0000 | [diff] [blame] | 175 | #endif |
| 176 | } |
| 177 | |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 178 | size_t |
| 179 | __next_prime(size_t n) |
| 180 | { |
| 181 | const size_t L = 210; |
| 182 | const size_t N = sizeof(small_primes) / sizeof(small_primes[0]); |
| 183 | // If n is small enough, search in small_primes |
| 184 | if (n <= small_primes[N-1]) |
| 185 | return *std::lower_bound(small_primes, small_primes + N, n); |
| 186 | // Else n > largest small_primes |
Howard Hinnant | e87ad17 | 2010-10-29 14:10:30 +0000 | [diff] [blame] | 187 | // Check for overflow |
Howard Hinnant | 0aa900e | 2012-12-27 18:59:05 +0000 | [diff] [blame] | 188 | __check_for_overflow(n); |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 189 | // Start searching list of potential primes: L * k0 + indices[in] |
| 190 | const size_t M = sizeof(indices) / sizeof(indices[0]); |
| 191 | // Select first potential prime >= n |
| 192 | // Known a-priori n >= L |
| 193 | size_t k0 = n / L; |
Howard Hinnant | ec3773c | 2011-12-01 20:21:04 +0000 | [diff] [blame] | 194 | size_t in = static_cast<size_t>(std::lower_bound(indices, indices + M, n - k0 * L) |
| 195 | - indices); |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 196 | n = L * k0 + indices[in]; |
| 197 | while (true) |
| 198 | { |
| 199 | // Divide n by all primes or potential primes (i) until: |
| 200 | // 1. The division is even, so try next potential prime. |
| 201 | // 2. The i > sqrt(n), in which case n is prime. |
| 202 | // It is known a-priori that n is not divisible by 2, 3, 5 or 7, |
| 203 | // so don't test those (j == 5 -> divide by 11 first). And the |
| 204 | // potential primes start with 211, so don't test against the last |
| 205 | // small prime. |
| 206 | for (size_t j = 5; j < N - 1; ++j) |
| 207 | { |
Howard Hinnant | d2a9251 | 2010-09-13 01:43:27 +0000 | [diff] [blame] | 208 | const std::size_t p = small_primes[j]; |
| 209 | const std::size_t q = n / p; |
| 210 | if (q < p) |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 211 | return n; |
Howard Hinnant | d2a9251 | 2010-09-13 01:43:27 +0000 | [diff] [blame] | 212 | if (n == q * p) |
| 213 | goto next; |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 214 | } |
| 215 | // n wasn't divisible by small primes, try potential primes |
| 216 | { |
| 217 | size_t i = 211; |
| 218 | while (true) |
| 219 | { |
Howard Hinnant | d2a9251 | 2010-09-13 01:43:27 +0000 | [diff] [blame] | 220 | std::size_t q = n / i; |
| 221 | if (q < i) |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 222 | return n; |
Howard Hinnant | d2a9251 | 2010-09-13 01:43:27 +0000 | [diff] [blame] | 223 | if (n == q * i) |
| 224 | break; |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 225 | |
| 226 | i += 10; |
Howard Hinnant | d2a9251 | 2010-09-13 01:43:27 +0000 | [diff] [blame] | 227 | q = n / i; |
| 228 | if (q < i) |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 229 | return n; |
Howard Hinnant | d2a9251 | 2010-09-13 01:43:27 +0000 | [diff] [blame] | 230 | if (n == q * i) |
| 231 | break; |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 232 | |
| 233 | i += 2; |
Howard Hinnant | d2a9251 | 2010-09-13 01:43:27 +0000 | [diff] [blame] | 234 | q = n / i; |
| 235 | if (q < i) |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 236 | return n; |
Howard Hinnant | d2a9251 | 2010-09-13 01:43:27 +0000 | [diff] [blame] | 237 | if (n == q * i) |
| 238 | break; |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 239 | |
| 240 | i += 4; |
Howard Hinnant | d2a9251 | 2010-09-13 01:43:27 +0000 | [diff] [blame] | 241 | q = n / i; |
| 242 | if (q < i) |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 243 | return n; |
Howard Hinnant | d2a9251 | 2010-09-13 01:43:27 +0000 | [diff] [blame] | 244 | if (n == q * i) |
| 245 | break; |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 246 | |
| 247 | i += 2; |
Howard Hinnant | d2a9251 | 2010-09-13 01:43:27 +0000 | [diff] [blame] | 248 | q = n / i; |
| 249 | if (q < i) |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 250 | return n; |
Howard Hinnant | d2a9251 | 2010-09-13 01:43:27 +0000 | [diff] [blame] | 251 | if (n == q * i) |
| 252 | break; |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 253 | |
| 254 | i += 4; |
Howard Hinnant | d2a9251 | 2010-09-13 01:43:27 +0000 | [diff] [blame] | 255 | q = n / i; |
| 256 | if (q < i) |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 257 | return n; |
Howard Hinnant | d2a9251 | 2010-09-13 01:43:27 +0000 | [diff] [blame] | 258 | if (n == q * i) |
| 259 | break; |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 260 | |
| 261 | i += 6; |
Howard Hinnant | d2a9251 | 2010-09-13 01:43:27 +0000 | [diff] [blame] | 262 | q = n / i; |
| 263 | if (q < i) |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 264 | return n; |
Howard Hinnant | d2a9251 | 2010-09-13 01:43:27 +0000 | [diff] [blame] | 265 | if (n == q * i) |
| 266 | break; |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 267 | |
| 268 | i += 2; |
Howard Hinnant | d2a9251 | 2010-09-13 01:43:27 +0000 | [diff] [blame] | 269 | q = n / i; |
| 270 | if (q < i) |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 271 | return n; |
Howard Hinnant | d2a9251 | 2010-09-13 01:43:27 +0000 | [diff] [blame] | 272 | if (n == q * i) |
| 273 | break; |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 274 | |
| 275 | i += 6; |
Howard Hinnant | d2a9251 | 2010-09-13 01:43:27 +0000 | [diff] [blame] | 276 | q = n / i; |
| 277 | if (q < i) |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 278 | return n; |
Howard Hinnant | d2a9251 | 2010-09-13 01:43:27 +0000 | [diff] [blame] | 279 | if (n == q * i) |
| 280 | break; |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 281 | |
| 282 | i += 4; |
Howard Hinnant | d2a9251 | 2010-09-13 01:43:27 +0000 | [diff] [blame] | 283 | q = n / i; |
| 284 | if (q < i) |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 285 | return n; |
Howard Hinnant | d2a9251 | 2010-09-13 01:43:27 +0000 | [diff] [blame] | 286 | if (n == q * i) |
| 287 | break; |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 288 | |
| 289 | i += 2; |
Howard Hinnant | d2a9251 | 2010-09-13 01:43:27 +0000 | [diff] [blame] | 290 | q = n / i; |
| 291 | if (q < i) |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 292 | return n; |
Howard Hinnant | d2a9251 | 2010-09-13 01:43:27 +0000 | [diff] [blame] | 293 | if (n == q * i) |
| 294 | break; |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 295 | |
| 296 | i += 4; |
Howard Hinnant | d2a9251 | 2010-09-13 01:43:27 +0000 | [diff] [blame] | 297 | q = n / i; |
| 298 | if (q < i) |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 299 | return n; |
Howard Hinnant | d2a9251 | 2010-09-13 01:43:27 +0000 | [diff] [blame] | 300 | if (n == q * i) |
| 301 | break; |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 302 | |
| 303 | i += 6; |
Howard Hinnant | d2a9251 | 2010-09-13 01:43:27 +0000 | [diff] [blame] | 304 | q = n / i; |
| 305 | if (q < i) |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 306 | return n; |
Howard Hinnant | d2a9251 | 2010-09-13 01:43:27 +0000 | [diff] [blame] | 307 | if (n == q * i) |
| 308 | break; |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 309 | |
| 310 | i += 6; |
Howard Hinnant | d2a9251 | 2010-09-13 01:43:27 +0000 | [diff] [blame] | 311 | q = n / i; |
| 312 | if (q < i) |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 313 | return n; |
Howard Hinnant | d2a9251 | 2010-09-13 01:43:27 +0000 | [diff] [blame] | 314 | if (n == q * i) |
| 315 | break; |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 316 | |
| 317 | i += 2; |
Howard Hinnant | d2a9251 | 2010-09-13 01:43:27 +0000 | [diff] [blame] | 318 | q = n / i; |
| 319 | if (q < i) |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 320 | return n; |
Howard Hinnant | d2a9251 | 2010-09-13 01:43:27 +0000 | [diff] [blame] | 321 | if (n == q * i) |
| 322 | break; |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 323 | |
| 324 | i += 6; |
Howard Hinnant | d2a9251 | 2010-09-13 01:43:27 +0000 | [diff] [blame] | 325 | q = n / i; |
| 326 | if (q < i) |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 327 | return n; |
Howard Hinnant | d2a9251 | 2010-09-13 01:43:27 +0000 | [diff] [blame] | 328 | if (n == q * i) |
| 329 | break; |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 330 | |
| 331 | i += 4; |
Howard Hinnant | d2a9251 | 2010-09-13 01:43:27 +0000 | [diff] [blame] | 332 | q = n / i; |
| 333 | if (q < i) |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 334 | return n; |
Howard Hinnant | d2a9251 | 2010-09-13 01:43:27 +0000 | [diff] [blame] | 335 | if (n == q * i) |
| 336 | break; |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 337 | |
| 338 | i += 2; |
Howard Hinnant | d2a9251 | 2010-09-13 01:43:27 +0000 | [diff] [blame] | 339 | q = n / i; |
| 340 | if (q < i) |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 341 | return n; |
Howard Hinnant | d2a9251 | 2010-09-13 01:43:27 +0000 | [diff] [blame] | 342 | if (n == q * i) |
| 343 | break; |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 344 | |
| 345 | i += 6; |
Howard Hinnant | d2a9251 | 2010-09-13 01:43:27 +0000 | [diff] [blame] | 346 | q = n / i; |
| 347 | if (q < i) |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 348 | return n; |
Howard Hinnant | d2a9251 | 2010-09-13 01:43:27 +0000 | [diff] [blame] | 349 | if (n == q * i) |
| 350 | break; |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 351 | |
| 352 | i += 4; |
Howard Hinnant | d2a9251 | 2010-09-13 01:43:27 +0000 | [diff] [blame] | 353 | q = n / i; |
| 354 | if (q < i) |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 355 | return n; |
Howard Hinnant | d2a9251 | 2010-09-13 01:43:27 +0000 | [diff] [blame] | 356 | if (n == q * i) |
| 357 | break; |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 358 | |
| 359 | i += 6; |
Howard Hinnant | d2a9251 | 2010-09-13 01:43:27 +0000 | [diff] [blame] | 360 | q = n / i; |
| 361 | if (q < i) |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 362 | return n; |
Howard Hinnant | d2a9251 | 2010-09-13 01:43:27 +0000 | [diff] [blame] | 363 | if (n == q * i) |
| 364 | break; |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 365 | |
| 366 | i += 8; |
Howard Hinnant | d2a9251 | 2010-09-13 01:43:27 +0000 | [diff] [blame] | 367 | q = n / i; |
| 368 | if (q < i) |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 369 | return n; |
Howard Hinnant | d2a9251 | 2010-09-13 01:43:27 +0000 | [diff] [blame] | 370 | if (n == q * i) |
| 371 | break; |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 372 | |
| 373 | i += 4; |
Howard Hinnant | d2a9251 | 2010-09-13 01:43:27 +0000 | [diff] [blame] | 374 | q = n / i; |
| 375 | if (q < i) |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 376 | return n; |
Howard Hinnant | d2a9251 | 2010-09-13 01:43:27 +0000 | [diff] [blame] | 377 | if (n == q * i) |
| 378 | break; |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 379 | |
| 380 | i += 2; |
Howard Hinnant | d2a9251 | 2010-09-13 01:43:27 +0000 | [diff] [blame] | 381 | q = n / i; |
| 382 | if (q < i) |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 383 | return n; |
Howard Hinnant | d2a9251 | 2010-09-13 01:43:27 +0000 | [diff] [blame] | 384 | if (n == q * i) |
| 385 | break; |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 386 | |
| 387 | i += 4; |
Howard Hinnant | d2a9251 | 2010-09-13 01:43:27 +0000 | [diff] [blame] | 388 | q = n / i; |
| 389 | if (q < i) |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 390 | return n; |
Howard Hinnant | d2a9251 | 2010-09-13 01:43:27 +0000 | [diff] [blame] | 391 | if (n == q * i) |
| 392 | break; |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 393 | |
| 394 | i += 2; |
Howard Hinnant | d2a9251 | 2010-09-13 01:43:27 +0000 | [diff] [blame] | 395 | q = n / i; |
| 396 | if (q < i) |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 397 | return n; |
Howard Hinnant | d2a9251 | 2010-09-13 01:43:27 +0000 | [diff] [blame] | 398 | if (n == q * i) |
| 399 | break; |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 400 | |
| 401 | i += 4; |
Howard Hinnant | d2a9251 | 2010-09-13 01:43:27 +0000 | [diff] [blame] | 402 | q = n / i; |
| 403 | if (q < i) |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 404 | return n; |
Howard Hinnant | d2a9251 | 2010-09-13 01:43:27 +0000 | [diff] [blame] | 405 | if (n == q * i) |
| 406 | break; |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 407 | |
| 408 | i += 8; |
Howard Hinnant | d2a9251 | 2010-09-13 01:43:27 +0000 | [diff] [blame] | 409 | q = n / i; |
| 410 | if (q < i) |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 411 | return n; |
Howard Hinnant | d2a9251 | 2010-09-13 01:43:27 +0000 | [diff] [blame] | 412 | if (n == q * i) |
| 413 | break; |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 414 | |
| 415 | i += 6; |
Howard Hinnant | d2a9251 | 2010-09-13 01:43:27 +0000 | [diff] [blame] | 416 | q = n / i; |
| 417 | if (q < i) |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 418 | return n; |
Howard Hinnant | d2a9251 | 2010-09-13 01:43:27 +0000 | [diff] [blame] | 419 | if (n == q * i) |
| 420 | break; |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 421 | |
| 422 | i += 4; |
Howard Hinnant | d2a9251 | 2010-09-13 01:43:27 +0000 | [diff] [blame] | 423 | q = n / i; |
| 424 | if (q < i) |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 425 | return n; |
Howard Hinnant | d2a9251 | 2010-09-13 01:43:27 +0000 | [diff] [blame] | 426 | if (n == q * i) |
| 427 | break; |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 428 | |
| 429 | i += 6; |
Howard Hinnant | d2a9251 | 2010-09-13 01:43:27 +0000 | [diff] [blame] | 430 | q = n / i; |
| 431 | if (q < i) |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 432 | return n; |
Howard Hinnant | d2a9251 | 2010-09-13 01:43:27 +0000 | [diff] [blame] | 433 | if (n == q * i) |
| 434 | break; |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 435 | |
| 436 | i += 2; |
Howard Hinnant | d2a9251 | 2010-09-13 01:43:27 +0000 | [diff] [blame] | 437 | q = n / i; |
| 438 | if (q < i) |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 439 | return n; |
Howard Hinnant | d2a9251 | 2010-09-13 01:43:27 +0000 | [diff] [blame] | 440 | if (n == q * i) |
| 441 | break; |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 442 | |
| 443 | i += 4; |
Howard Hinnant | d2a9251 | 2010-09-13 01:43:27 +0000 | [diff] [blame] | 444 | q = n / i; |
| 445 | if (q < i) |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 446 | return n; |
Howard Hinnant | d2a9251 | 2010-09-13 01:43:27 +0000 | [diff] [blame] | 447 | if (n == q * i) |
| 448 | break; |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 449 | |
| 450 | i += 6; |
Howard Hinnant | d2a9251 | 2010-09-13 01:43:27 +0000 | [diff] [blame] | 451 | q = n / i; |
| 452 | if (q < i) |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 453 | return n; |
Howard Hinnant | d2a9251 | 2010-09-13 01:43:27 +0000 | [diff] [blame] | 454 | if (n == q * i) |
| 455 | break; |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 456 | |
| 457 | i += 2; |
Howard Hinnant | d2a9251 | 2010-09-13 01:43:27 +0000 | [diff] [blame] | 458 | q = n / i; |
| 459 | if (q < i) |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 460 | return n; |
Howard Hinnant | d2a9251 | 2010-09-13 01:43:27 +0000 | [diff] [blame] | 461 | if (n == q * i) |
| 462 | break; |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 463 | |
| 464 | i += 6; |
Howard Hinnant | d2a9251 | 2010-09-13 01:43:27 +0000 | [diff] [blame] | 465 | q = n / i; |
| 466 | if (q < i) |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 467 | return n; |
Howard Hinnant | d2a9251 | 2010-09-13 01:43:27 +0000 | [diff] [blame] | 468 | if (n == q * i) |
| 469 | break; |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 470 | |
| 471 | i += 6; |
Howard Hinnant | d2a9251 | 2010-09-13 01:43:27 +0000 | [diff] [blame] | 472 | q = n / i; |
| 473 | if (q < i) |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 474 | return n; |
Howard Hinnant | d2a9251 | 2010-09-13 01:43:27 +0000 | [diff] [blame] | 475 | if (n == q * i) |
| 476 | break; |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 477 | |
| 478 | i += 4; |
Howard Hinnant | d2a9251 | 2010-09-13 01:43:27 +0000 | [diff] [blame] | 479 | q = n / i; |
| 480 | if (q < i) |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 481 | return n; |
Howard Hinnant | d2a9251 | 2010-09-13 01:43:27 +0000 | [diff] [blame] | 482 | if (n == q * i) |
| 483 | break; |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 484 | |
| 485 | i += 2; |
Howard Hinnant | d2a9251 | 2010-09-13 01:43:27 +0000 | [diff] [blame] | 486 | q = n / i; |
| 487 | if (q < i) |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 488 | return n; |
Howard Hinnant | d2a9251 | 2010-09-13 01:43:27 +0000 | [diff] [blame] | 489 | if (n == q * i) |
| 490 | break; |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 491 | |
| 492 | i += 4; |
Howard Hinnant | d2a9251 | 2010-09-13 01:43:27 +0000 | [diff] [blame] | 493 | q = n / i; |
| 494 | if (q < i) |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 495 | return n; |
Howard Hinnant | d2a9251 | 2010-09-13 01:43:27 +0000 | [diff] [blame] | 496 | if (n == q * i) |
| 497 | break; |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 498 | |
| 499 | i += 6; |
Howard Hinnant | d2a9251 | 2010-09-13 01:43:27 +0000 | [diff] [blame] | 500 | q = n / i; |
| 501 | if (q < i) |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 502 | return n; |
Howard Hinnant | d2a9251 | 2010-09-13 01:43:27 +0000 | [diff] [blame] | 503 | if (n == q * i) |
| 504 | break; |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 505 | |
| 506 | i += 2; |
Howard Hinnant | d2a9251 | 2010-09-13 01:43:27 +0000 | [diff] [blame] | 507 | q = n / i; |
| 508 | if (q < i) |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 509 | return n; |
Howard Hinnant | d2a9251 | 2010-09-13 01:43:27 +0000 | [diff] [blame] | 510 | if (n == q * i) |
| 511 | break; |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 512 | |
| 513 | i += 6; |
Howard Hinnant | d2a9251 | 2010-09-13 01:43:27 +0000 | [diff] [blame] | 514 | q = n / i; |
| 515 | if (q < i) |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 516 | return n; |
Howard Hinnant | d2a9251 | 2010-09-13 01:43:27 +0000 | [diff] [blame] | 517 | if (n == q * i) |
| 518 | break; |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 519 | |
| 520 | i += 4; |
Howard Hinnant | d2a9251 | 2010-09-13 01:43:27 +0000 | [diff] [blame] | 521 | q = n / i; |
| 522 | if (q < i) |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 523 | return n; |
Howard Hinnant | d2a9251 | 2010-09-13 01:43:27 +0000 | [diff] [blame] | 524 | if (n == q * i) |
| 525 | break; |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 526 | |
| 527 | i += 2; |
Howard Hinnant | d2a9251 | 2010-09-13 01:43:27 +0000 | [diff] [blame] | 528 | q = n / i; |
| 529 | if (q < i) |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 530 | return n; |
Howard Hinnant | d2a9251 | 2010-09-13 01:43:27 +0000 | [diff] [blame] | 531 | if (n == q * i) |
| 532 | break; |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 533 | |
| 534 | i += 4; |
Howard Hinnant | d2a9251 | 2010-09-13 01:43:27 +0000 | [diff] [blame] | 535 | q = n / i; |
| 536 | if (q < i) |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 537 | return n; |
Howard Hinnant | d2a9251 | 2010-09-13 01:43:27 +0000 | [diff] [blame] | 538 | if (n == q * i) |
| 539 | break; |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 540 | |
| 541 | i += 2; |
Howard Hinnant | d2a9251 | 2010-09-13 01:43:27 +0000 | [diff] [blame] | 542 | q = n / i; |
| 543 | if (q < i) |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 544 | return n; |
Howard Hinnant | d2a9251 | 2010-09-13 01:43:27 +0000 | [diff] [blame] | 545 | if (n == q * i) |
| 546 | break; |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 547 | |
| 548 | i += 10; |
Howard Hinnant | d2a9251 | 2010-09-13 01:43:27 +0000 | [diff] [blame] | 549 | q = n / i; |
| 550 | if (q < i) |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 551 | return n; |
Howard Hinnant | d2a9251 | 2010-09-13 01:43:27 +0000 | [diff] [blame] | 552 | if (n == q * i) |
| 553 | break; |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 554 | |
| 555 | // This will loop i to the next "plane" of potential primes |
| 556 | i += 2; |
| 557 | } |
| 558 | } |
| 559 | next: |
| 560 | // n is not prime. Increment n to next potential prime. |
| 561 | if (++in == M) |
| 562 | { |
| 563 | ++k0; |
| 564 | in = 0; |
| 565 | } |
| 566 | n = L * k0 + indices[in]; |
| 567 | } |
| 568 | } |
| 569 | |
| 570 | _LIBCPP_END_NAMESPACE_STD |