summaryrefslogtreecommitdiffstats
path: root/games
diff options
context:
space:
mode:
authorcperciva <cperciva@FreeBSD.org>2014-09-27 09:00:38 +0000
committercperciva <cperciva@FreeBSD.org>2014-09-27 09:00:38 +0000
commita742fc39b4772b875eb540b6934da3350e9086e2 (patch)
tree8e2df76b0bdde221c9133d127b4506787e35922e /games
parent0d3437c89c0362a97db627be97564083305c763f (diff)
downloadFreeBSD-src-a742fc39b4772b875eb540b6934da3350e9086e2.zip
FreeBSD-src-a742fc39b4772b875eb540b6934da3350e9086e2.tar.gz
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.
Diffstat (limited to 'games')
-rw-r--r--games/factor/factor.63
-rw-r--r--games/primes/primes.c21
-rw-r--r--games/primes/primes.h4
3 files changed, 15 insertions, 13 deletions
diff --git a/games/factor/factor.6 b/games/factor/factor.6
index 508a98b..ba82f14 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 f5f1faf..a1c95c2 100644
--- a/games/primes/primes.c
+++ b/games/primes/primes.c
@@ -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);
}
}
}
diff --git a/games/primes/primes.h b/games/primes/primes.h
index 7dcef52..3a18fc7 100644
--- a/games/primes/primes.h
+++ b/games/primes/primes.h
@@ -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) */
OpenPOWER on IntegriCloud