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GE_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. */
if (BITS_PER_LONG > 32)
#endif
x >>= 16;
i += 16;
}
- if (x >= 0x100)
- {
- x >>= 8;
- i += 8;
- }
- if (x >= 0x10)
- {
- x >>= 4;
- i += 4;
- }
- return i + table[x & 0xf];
+ return i + table[128 + (x & 0xff) - (x >> 8)];
}
/* Returns size if there's no more bits set */
return EXIT_FAILURE;
case 'b':
{
- long arg = atoi(optarg);
+ long arg = atol(optarg);
if (arg <= 10)
{
usage();
limit = 1000;
else if (argc == 1)
{
- limit = atoi(argv[0]);
+ limit = atol(argv[0]);
if (limit < 2)
return EXIT_SUCCESS;
}
if (!block_size || block_size > size)
block_size = size;
- vector = vector_alloc (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);
if (!vector)
{
fprintf(stderr, "Insufficient memory.\n");