Skip to content
Snippets Groups Projects
Commit 40d6c668 authored by Niels Möller's avatar Niels Möller
Browse files

(find_first_one): Optimized, using slightly larger table.

(main): Use atol, arther than atoi.

Rev: nettle/examples/eratosthenes.c:1.8
parent eb894161
No related branches found
No related tags found
No related merge requests found
...@@ -113,12 +113,25 @@ vector_clear_bits (unsigned long *vector, unsigned long step, ...@@ -113,12 +113,25 @@ vector_clear_bits (unsigned long *vector, unsigned long step,
static unsigned static unsigned
find_first_one (unsigned long x) find_first_one (unsigned long x)
{ {
unsigned table[0x10] = unsigned table[0x101] =
{ {
/* 0, 1, 2, 3, 4, 5, 6, 7 */ 15, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
-1, 0, 1, 0, 2, 0, 1 , 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
/* 8, 9, 10, 11, 12, 13, 14, 15 */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
3, 0, 1, 0, 2, 0, 1, 0 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
14, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
13, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
12, 0, 0, 0, 0, 0, 0, 0,11, 0, 0, 0,10, 0, 9, 8,
0, 0, 1, 0, 2, 0, 0, 0, 3, 0, 0, 0, 0, 0, 0, 0,
4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
5, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
6, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
7,
}; };
/* Isolate least significant bit */ /* Isolate least significant bit */
...@@ -127,7 +140,7 @@ find_first_one (unsigned long x) ...@@ -127,7 +140,7 @@ find_first_one (unsigned long x)
unsigned i = 0; unsigned i = 0;
#if NEED_HANDLE_LARGE_LONG #if NEED_HANDLE_LARGE_LONG
#ifndef SIZEOF_LONG #ifndef SIZEOF_LONG
/* Can't not be tested by the preprocessor. May generate warnings /* Can not be tested by the preprocessor. May generate warnings
when long is 32 bits. */ when long is 32 bits. */
if (BITS_PER_LONG > 32) if (BITS_PER_LONG > 32)
#endif #endif
...@@ -143,17 +156,7 @@ find_first_one (unsigned long x) ...@@ -143,17 +156,7 @@ find_first_one (unsigned long x)
x >>= 16; x >>= 16;
i += 16; i += 16;
} }
if (x >= 0x100) return i + table[128 + (x & 0xff) - (x >> 8)];
{
x >>= 8;
i += 8;
}
if (x >= 0x10)
{
x >>= 4;
i += 4;
}
return i + table[x & 0xf];
} }
/* Returns size if there's no more bits set */ /* Returns size if there's no more bits set */
...@@ -204,7 +207,7 @@ main (int argc, char **argv) ...@@ -204,7 +207,7 @@ main (int argc, char **argv)
return EXIT_FAILURE; return EXIT_FAILURE;
case 'b': case 'b':
{ {
long arg = atoi(optarg); long arg = atol(optarg);
if (arg <= 10) if (arg <= 10)
{ {
usage(); usage();
...@@ -224,7 +227,7 @@ main (int argc, char **argv) ...@@ -224,7 +227,7 @@ main (int argc, char **argv)
limit = 1000; limit = 1000;
else if (argc == 1) else if (argc == 1)
{ {
limit = atoi(argv[0]); limit = atol(argv[0]);
if (limit < 2) if (limit < 2)
return EXIT_SUCCESS; return EXIT_SUCCESS;
} }
...@@ -239,6 +242,9 @@ main (int argc, char **argv) ...@@ -239,6 +242,9 @@ main (int argc, char **argv)
if (!block_size || block_size > size) if (!block_size || block_size > size)
block_size = size; block_size = size;
/* FIXME: Allocates a bit for all odd numbers up to limit. Could
save a lot of memory by allocating space only for numbers up to
the square root, plus one additional block. */
vector = vector_alloc(size); vector = vector_alloc(size);
if (!vector) if (!vector)
{ {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment