From eb5ea45ba88bc79f678be3ec6d98737079abc7ed Mon Sep 17 00:00:00 2001 From: Colin Percival Date: Sat, 27 Sep 2014 09:00:38 +0000 Subject: [PATCH] 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. --- games/factor/factor.6 | 3 +-- games/primes/primes.c | 21 +++++++++++---------- games/primes/primes.h | 4 +++- 3 files changed, 15 insertions(+), 13 deletions(-) diff --git a/games/factor/factor.6 b/games/factor/factor.6 index 508a98b2c4d7..ba82f1407237 100644 --- a/games/factor/factor.6 +++ b/games/factor/factor.6 @@ -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 diff --git a/games/primes/primes.c b/games/primes/primes.c index f5f1faf5eb0f..a1c95c2a8a02 100644 --- a/games/primes/primes.c +++ b/games/primes/primes.c @@ -64,6 +64,7 @@ static const char rcsid[] = #include #include #include +#include #include #include #include @@ -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); } } } diff --git a/games/primes/primes.h b/games/primes/primes.h index 7dcef52d0c64..3a18fc7dd127 100644 --- a/games/primes/primes.h +++ b/games/primes/primes.h @@ -41,8 +41,10 @@ * chongo /\oo/\ */ +#include + /* 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) */