Switch primes(6) from using unsigned long to using uint64_t. This fixes

'limited range of type' warnings about comparisons on 32-bit systems, and
allows 32-bit systems to compute the full range of primes.
This commit is contained in:
Colin Percival 2014-09-27 09:00:38 +00:00
parent 137bf249f3
commit eb5ea45ba8
Notes: svn2git 2020-12-20 02:59:44 +00:00
svn path=/head/; revision=272207
3 changed files with 15 additions and 13 deletions

View File

@ -89,8 +89,7 @@ The
value must not be greater than the maximum.
The default and maximum value of
.Ar stop
is 4294967295 on 32-bit architectures
and 3825123056546413050 on 64-bit ones.
is 3825123056546413050.
.Pp
When the
.Nm primes

View File

@ -64,6 +64,7 @@ static const char rcsid[] =
#include <ctype.h>
#include <err.h>
#include <errno.h>
#include <inttypes.h>
#include <limits.h>
#include <math.h>
#include <stdio.h>
@ -111,10 +112,10 @@ main(int argc, char *argv[])
argv += optind;
start = 0;
stop = (sizeof(ubig) > 4) ? SPSPMAX : BIG;
stop = SPSPMAX;
/*
* Convert low and high args. Strtoul(3) sets errno to
* Convert low and high args. Strtoumax(3) sets errno to
* ERANGE if the number is too large, but, if there's
* a leading minus sign it returns the negation of the
* result of the conversion, which we'd rather disallow.
@ -126,19 +127,19 @@ main(int argc, char *argv[])
errx(1, "negative numbers aren't permitted.");
errno = 0;
start = strtoul(argv[0], &p, 0);
start = strtoumax(argv[0], &p, 0);
if (errno)
err(1, "%s", argv[0]);
if (*p != '\0')
errx(1, "%s: illegal numeric format.", argv[0]);
errno = 0;
stop = strtoul(argv[1], &p, 0);
stop = strtoumax(argv[1], &p, 0);
if (errno)
err(1, "%s", argv[1]);
if (*p != '\0')
errx(1, "%s: illegal numeric format.", argv[1]);
if ((uint64_t)stop > SPSPMAX)
if (stop > SPSPMAX)
errx(1, "%s: stop value too large.", argv[1]);
break;
case 1:
@ -147,7 +148,7 @@ main(int argc, char *argv[])
errx(1, "negative numbers aren't permitted.");
errno = 0;
start = strtoul(argv[0], &p, 0);
start = strtoumax(argv[0], &p, 0);
if (errno)
err(1, "%s", argv[0]);
if (*p != '\0')
@ -188,7 +189,7 @@ read_num_buf(void)
if (*p == '-')
errx(1, "negative numbers aren't permitted.");
errno = 0;
val = strtoul(buf, &p, 0);
val = strtoumax(buf, &p, 0);
if (errno)
err(1, "%s", buf);
if (*p != '\n')
@ -243,7 +244,7 @@ primes(ubig start, ubig stop)
for (p = &prime[0], factor = prime[0];
factor < stop && p <= pr_limit; factor = *(++p)) {
if (factor >= start) {
printf(hflag ? "0x%lx\n" : "%lu\n", factor);
printf(hflag ? "%" PRIx64 "\n" : "%" PRIu64 "\n", factor);
}
}
/* return early if we are done */
@ -306,11 +307,11 @@ primes(ubig start, ubig stop)
*/
for (q = table; q < tab_lim; ++q, start+=2) {
if (*q) {
if ((uint64_t)start > SIEVEMAX) {
if (start > SIEVEMAX) {
if (!isprime(start))
continue;
}
printf(hflag ? "0x%lx\n" : "%lu\n", start);
printf(hflag ? "%" PRIx64 "\n" : "%" PRIu64 "\n", start);
}
}
}

View File

@ -41,8 +41,10 @@
* chongo <for a good prime call: 391581 * 2^216193 - 1> /\oo/\
*/
#include <stdint.h>
/* ubig is the type that holds a large unsigned value */
typedef unsigned long ubig; /* must be >=32 bit unsigned value */
typedef uint64_t ubig; /* must be >=32 bit unsigned value */
#define BIG ULONG_MAX /* largest value will sieve */
/* bytes in sieve table (must be > 3*5*7*11) */