3 An implementation of the sieve of Eratosthenes, to generate a list of primes.
5 Copyright (C) 2007 Niels Möller
7 This file is part of GNU Nettle.
9 GNU Nettle is free software: you can redistribute it and/or
10 modify it under the terms of either:
12 * the GNU Lesser General Public License as published by the Free
13 Software Foundation; either version 3 of the License, or (at your
14 option) any later version.
18 * the GNU General Public License as published by the Free
19 Software Foundation; either version 2 of the License, or (at your
20 option) any later version.
22 or both in parallel, as here.
24 GNU Nettle is distributed in the hope that it will be useful,
25 but WITHOUT ANY WARRANTY; without even the implied warranty of
26 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
27 General Public License for more details.
29 You should have received copies of the GNU General Public License and
30 the GNU Lesser General Public License along with this program. If
31 not, see http://www.gnu.org/licenses/.
46 # define BITS_PER_LONG (CHAR_BIT * SIZEOF_LONG)
47 # if BITS_PER_LONG > 32
48 # define NEED_HANDLE_LARGE_LONG 1
50 # define NEED_HANDLE_LARGE_LONG 0
53 # define BITS_PER_LONG (CHAR_BIT * sizeof(unsigned long))
54 # define NEED_HANDLE_LARGE_LONG 1
61 fprintf(stderr, "Usage: erathostenes [OPTIONS] [LIMIT]\n\n"
63 " -? Display this message.\n"
64 " -b SIZE Block size.\n"
65 " -v Verbose output.\n"
70 isqrt(unsigned long n)
74 /* FIXME: Better initialization. */
78 /* Must avoid overflow in the first step. */
83 unsigned long y = (x + n/x) / 2;
92 static unsigned long *
93 vector_alloc(unsigned long size)
95 unsigned long end = (size + BITS_PER_LONG - 1) / BITS_PER_LONG;
96 unsigned long *vector = malloc (end * sizeof(*vector));
100 fprintf(stderr, "Insufficient memory.\n");
107 vector_init(unsigned long *vector, unsigned long size)
109 unsigned long end = (size + BITS_PER_LONG - 1) / BITS_PER_LONG;
112 for (i = 0; i < end; i++)
117 vector_clear_bits (unsigned long *vector, unsigned long step,
118 unsigned long start, unsigned long size)
122 for (bit = start; bit < size; bit += step)
124 unsigned long i = bit / BITS_PER_LONG;
125 unsigned long mask = 1L << (bit % BITS_PER_LONG);
132 find_first_one (unsigned long x)
134 static const unsigned char table[0x101] =
136 15, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
137 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
138 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
139 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
140 14, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
141 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
142 13, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
143 12, 0, 0, 0, 0, 0, 0, 0,11, 0, 0, 0,10, 0, 9, 8,
144 0, 0, 1, 0, 2, 0, 0, 0, 3, 0, 0, 0, 0, 0, 0, 0,
145 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
146 5, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
147 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
148 6, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
149 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
150 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
151 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
157 /* Isolate least significant bit */
160 #if NEED_HANDLE_LARGE_LONG
162 /* Can not be tested by the preprocessor. May generate warnings
163 when long is 32 bits. */
164 if (BITS_PER_LONG > 32)
166 while (x >= 0x100000000L)
171 #endif /* NEED_HANDLE_LARGE_LONG */
178 return i + table[128 + (x & 0xff) - (x >> 8)];
181 /* Returns size if there's no more bits set */
183 vector_find_next (const unsigned long *vector, unsigned long bit, unsigned long size)
185 unsigned long end = (size + BITS_PER_LONG - 1) / BITS_PER_LONG;
186 unsigned long i = bit / BITS_PER_LONG;
187 unsigned long mask = 1L << (bit % BITS_PER_LONG);
193 for (word = vector[i] & ~(mask - 1); !word; word = vector[i])
197 /* Next bit is the least significant bit of word */
198 return i * BITS_PER_LONG + find_first_one(word);
201 /* For benchmarking, define to do nothing (otherwise, most of the time
202 will be spent converting the output to decimal). */
203 #define OUTPUT(n) printf("%lu\n", (n))
206 atosize(const char *s)
209 long value = strtol(s, &end, 10);
214 /* FIXME: Doesn't check for overflow. */
232 main (int argc, char **argv)
234 /* Generate all primes p <= limit */
238 unsigned long limit_nbits;
240 /* Represents numbers up to sqrt(limit) */
241 unsigned long sieve_nbits;
242 unsigned long *sieve;
243 /* Block for the rest of the sieving. Size should match the cache,
244 the default value corresponds to 64 KB. */
245 unsigned long block_nbits = 64L << 13;
246 unsigned long block_start_bit;
247 unsigned long *block;
254 enum { OPT_HELP = 300 };
255 static const struct option options[] =
257 /* Name, args, flag, val */
258 { "help", no_argument, NULL, OPT_HELP },
259 { "verbose", no_argument, NULL, 'v' },
260 { "block-size", required_argument, NULL, 'b' },
261 { "quiet", required_argument, NULL, 'q' },
265 while ( (c = getopt_long(argc, argv, "svb:", options, NULL)) != -1)
272 block_nbits = CHAR_BIT * atosize(optarg);
302 limit = atol(argv[0]);
313 /* Round down to odd */
314 root = (root - 1) | 1;
315 /* Represents odd numbers from 3 up. */
316 sieve_nbits = (root - 1) / 2;
317 sieve = vector_alloc(sieve_nbits );
318 vector_init(sieve, sieve_nbits);
321 fprintf(stderr, "Initial sieve using %lu bits.\n", sieve_nbits);
331 bit = vector_find_next(sieve, bit + 1, sieve_nbits))
333 unsigned long n = 3 + 2 * bit;
334 /* First bit to clear corresponds to n^2, which is bit
336 (n^2 - 3) / 2 = (n + 3) * bit + 3
338 unsigned long n2_bit = (n+3)*bit + 3;
343 vector_clear_bits (sieve, n, n2_bit, sieve_nbits);
346 limit_nbits = (limit - 1) / 2;
348 if (sieve_nbits + block_nbits > limit_nbits)
349 block_nbits = limit_nbits - sieve_nbits;
353 double storage = block_nbits / 8.0;
355 const char prefix[] = " KMG";
357 while (storage > 1024 && shift < 3)
362 fprintf(stderr, "Blockwise sieving using blocks of %lu bits (%.3g %cByte)\n",
363 block_nbits, storage, prefix[shift]);
366 block = vector_alloc(block_nbits);
368 for (block_start_bit = bit; block_start_bit < limit_nbits; block_start_bit += block_nbits)
370 unsigned long block_start;
372 if (block_start_bit + block_nbits > limit_nbits)
373 block_nbits = limit_nbits - block_start_bit;
375 vector_init(block, block_nbits);
377 block_start = 3 + 2*block_start_bit;
380 fprintf(stderr, "Next block, n = %lu\n", block_start);
383 for (bit = 0; bit < sieve_nbits;
384 bit = vector_find_next(sieve, bit + 1, sieve_nbits))
386 unsigned long n = 3 + 2 * bit;
387 unsigned long sieve_start_bit = (n + 3) * bit + 3;
389 if (sieve_start_bit < block_start_bit)
391 unsigned long k = (block_start + n - 1) / (2*n);
392 sieve_start_bit = n * k + bit;
394 assert(sieve_start_bit < block_start_bit + n);
396 assert(sieve_start_bit >= block_start_bit);
398 vector_clear_bits(block, n, sieve_start_bit - block_start_bit, block_nbits);
400 for (bit = vector_find_next(block, 0, block_nbits);
402 bit = vector_find_next(block, bit + 1, block_nbits))
404 unsigned long n = block_start + 2 * bit;