summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorKaz Kylheku <kaz@kylheku.com>2019-01-23 06:58:38 -0800
committerKaz Kylheku <kaz@kylheku.com>2019-01-23 06:58:38 -0800
commitb333ea2900a2e7c10d7b5cd35bf250c8e2d8d40f (patch)
treef0cd73c36dd467cefe1506326e6d0c23bda28a8d
parentf64d0bb5e0bfff833936d63849f86510a2328fee (diff)
downloadtxr-b333ea2900a2e7c10d7b5cd35bf250c8e2d8d40f.tar.gz
txr-b333ea2900a2e7c10d7b5cd35bf250c8e2d8d40f.tar.bz2
txr-b333ea2900a2e7c10d7b5cd35bf250c8e2d8d40f.zip
Fix some instances of 4 bytes = 32 bits assumption.
* hash.c (equal_hash, eql_hash, cobj_eq_hash_op, hash_hash_op): Multiply object size by CHAR_BIT and switch on number of bits, rather than bytes. * sysif.c (off_t_num): Likewise. * arith.c, ffi.c, itypes.c, rand.c: In numerous #if directive, fix size tests from bytes to bits. * configure: in the test that detects integer types, and in the test for enabling large file offsets, detect one more variable from the system: the value of CHAR_BIT. This turns into SIZEOF_BYTE. We use that value instead of a hard-coded 8.
-rw-r--r--arith.c10
-rwxr-xr-xconfigure14
-rw-r--r--ffi.c16
-rw-r--r--hash.c30
-rw-r--r--itypes.c2
-rw-r--r--rand.c12
-rw-r--r--sysif.c6
7 files changed, 48 insertions, 42 deletions
diff --git a/arith.c b/arith.c
index e45ad050..bd66bfbc 100644
--- a/arith.c
+++ b/arith.c
@@ -244,7 +244,7 @@ val bignum_len(val num)
int highest_bit(int_ptr_t n)
{
-#if SIZEOF_PTR == 8
+#if CHAR_BIT * SIZEOF_PTR == 64
if (n & 0x7FFFFFFF00000000) {
if (n & 0x7FFF000000000000) {
if (n & 0x7F00000000000000) {
@@ -354,7 +354,7 @@ int highest_bit(int_ptr_t n)
}
}
}
-#elif SIZEOF_PTR == 4
+#elif CHAR_BIT * SIZEOF_PTR == 32
if (n & 0x7FFF0000) {
if (n & 0x7F000000) {
if (n & 0x70000000) {
@@ -2914,14 +2914,14 @@ val logcount(val n)
uint_ptr_t d = c;
if (c < 0)
d = ~d;
-#if SIZEOF_PTR == 8
+#if CHAR_BIT * SIZEOF_PTR == 64
d = ((d & 0xAAAAAAAAAAAAAAAA) >> 1) + (d & 0x5555555555555555);
d = ((d & 0xCCCCCCCCCCCCCCCC) >> 2) + (d & 0x3333333333333333);
d = ((d & 0xF0F0F0F0F0F0F0F0) >> 4) + (d & 0x0F0F0F0F0F0F0F0F);
d = ((d & 0xFF00FF00FF00FF00) >> 8) + (d & 0x00FF00FF00FF00FF);
d = ((d & 0xFFFF0000FFFF0000) >> 16) + (d & 0x0000FFFF0000FFFF);
d = ((d & 0xFFFFFFFF00000000) >> 32) + (d & 0x00000000FFFFFFFF);
-#elif SIZEOF_PTR == 4
+#elif CHAR_BIT * SIZEOF_PTR == 32
d = ((d & 0xAAAAAAAA) >> 1) + (d & 0x55555555);
d = ((d & 0xCCCCCCCC) >> 2) + (d & 0x33333333);
d = ((d & 0xF0F0F0F0) >> 4) + (d & 0x0F0F0F0F);
@@ -2929,7 +2929,7 @@ val logcount(val n)
d = ((d & 0xFFFF0000) >> 16) + (d & 0x0000FFFF);
return unum(d);
#else
-#error fixme: only 4 or 8 byte pointers supported
+#error portme
#endif
}
case BGNUM:
diff --git a/configure b/configure
index 3f3edd78..b9bbd9a4 100755
--- a/configure
+++ b/configure
@@ -1183,7 +1183,9 @@ read_syms()
if [ -z "$intptr" ] ; then
cat > conftest.c <<!
#include <stddef.h>
+#include <limits.h>
#include "config.h"
+char SIZEOF_BYTE[CHAR_BIT];
#ifdef HAVE_SUPERLONG_T
char SIZEOF_SUPERLONG_T[sizeof (superlong_t)];
#endif
@@ -1205,6 +1207,7 @@ char DUMMY;
exit 1
fi
+ SIZEOF_BYTE=0
SIZEOF_PTR=0
SIZEOF_SHORT=0
SIZEOF_INT=0
@@ -1214,7 +1217,7 @@ char DUMMY;
read_syms y
- if [ $SIZEOF_PTR -eq 0 ] ; then
+ if [ $SIZEOF_PTR -eq 0 -o $SIZEOF_BYTE -eq 0 ] ; then
printf "failed\n"
exit 1
fi
@@ -1238,7 +1241,7 @@ fi
printf '"%s"\n' "$intptr"
printf "typedef $intptr int_ptr_t;\n" >> config.h
printf "typedef unsigned $intptr uint_ptr_t;\n" >> config.h
-intptr_max_expr="((((convert(int_ptr_t, 1) << $((SIZEOF_PTR * 8 - 2))) - 1) << 1) + 1)"
+intptr_max_expr="((((convert(int_ptr_t, 1) << $((SIZEOF_PTR * SIZEOF_BYTE - 2))) - 1) << 1) + 1)"
printf "#define INT_PTR_MAX %s\n" "$intptr_max_expr" >> config.h
printf "#define INT_PTR_MIN (-INT_PTR_MAX)\n" >> config.h
printf "#define UINT_PTR_MAX (convert(uint_ptr_t, -1))\n" >> config.h
@@ -2712,7 +2715,9 @@ file_offset_define=none
for try in NOTHING _LARGE_FILES=1 _FILE_OFFSET_BITS=64 ; do
cat > conftest.c <<!
+#include <limits.h>
#include <sys/types.h>
+char SIZEOF_BYTE[CHAR_BIT];
char SIZEOF_OFF_T[sizeof (off_t)];
char DUMMY;
!
@@ -2724,16 +2729,17 @@ char DUMMY;
exit 1
fi
+ SIZEOF_BYTE=0
SIZEOF_OFF_T=0
read_syms
- if [ $SIZEOF_OFF_T -eq 0 ] ; then
+ if [ $SIZEOF_OFF_T -eq 0 -o $SIZEOF_BYTE -eq 0 ] ; then
printf "failed\n"
exit 1
fi
- if [ $SIZEOF_OFF_T -eq 8 ] ; then
+ if [ $(( SIZEOF_BYTE * SIZEOF_OFF_T )) -eq 64 ] ; then
if [ $try = NOTHING ] ; then
printf "default\n"
file_offset_define=
diff --git a/ffi.c b/ffi.c
index 07e361ae..17936396 100644
--- a/ffi.c
+++ b/ffi.c
@@ -926,7 +926,7 @@ static val ffi_le_u32_get(struct txr_ffi_type *tft, mem_t *src, val self)
static void ffi_be_i64_put(struct txr_ffi_type *tft, val n,
mem_t *dst, val self)
{
-#if SIZEOF_PTR >= 8
+#if CHAR_BIT * SIZEOF_PTR >= 64
cnum v = c_num(n);
if (v < -convert(cnum, 0x7FFFFFFFFFFFFFFF - 1) || v > 0x7FFFFFFFFFFFFFFF)
@@ -964,7 +964,7 @@ range:
static val ffi_be_i64_get(struct txr_ffi_type *tft, mem_t *src, val self)
{
-#if SIZEOF_PTR >= 8
+#if CHAR_BIT * SIZEOF_PTR >= 64
cnum n = (convert(cnum, src[0]) << 56 | convert(cnum, src[1]) << 48 |
convert(cnum, src[2]) << 40 | convert(cnum, src[3]) << 32 |
convert(cnum, src[4]) << 24 | convert(cnum, src[5]) << 16 |
@@ -984,7 +984,7 @@ static val ffi_be_i64_get(struct txr_ffi_type *tft, mem_t *src, val self)
static void ffi_be_u64_put(struct txr_ffi_type *tft, val n,
mem_t *dst, val self)
{
-#if SIZEOF_PTR >= 8
+#if CHAR_BIT * SIZEOF_PTR >= 64
ucnum v = c_unum(n);
if (v > 0xFFFFFFFFFFFFFFFF)
@@ -1022,7 +1022,7 @@ range:
static val ffi_be_u64_get(struct txr_ffi_type *tft, mem_t *src, val self)
{
-#if SIZEOF_PTR >= 8
+#if CHAR_BIT * SIZEOF_PTR >= 64
ucnum n = (convert(ucnum, src[0]) << 56 | convert(ucnum, src[1]) << 48 |
convert(ucnum, src[2]) << 40 | convert(ucnum, src[3]) << 32 |
convert(ucnum, src[4]) << 24 | convert(ucnum, src[5]) << 16 |
@@ -1040,7 +1040,7 @@ static val ffi_be_u64_get(struct txr_ffi_type *tft, mem_t *src, val self)
static void ffi_le_i64_put(struct txr_ffi_type *tft, val n,
mem_t *dst, val self)
{
-#if SIZEOF_PTR >= 8
+#if CHAR_BIT * SIZEOF_PTR >= 64
cnum v = c_num(n);
if (v < -convert(cnum, 0x7FFFFFFFFFFFFFFF - 1) || v > 0x7FFFFFFFFFFFFFFF)
@@ -1078,7 +1078,7 @@ range:
static val ffi_le_i64_get(struct txr_ffi_type *tft, mem_t *src, val self)
{
-#if SIZEOF_PTR >= 8
+#if CHAR_BIT * SIZEOF_PTR >= 64
cnum n = (convert(cnum, src[7]) << 56 | convert(cnum, src[6]) << 48 |
convert(cnum, src[5]) << 40 | convert(cnum, src[4]) << 32 |
convert(cnum, src[3]) << 24 | convert(cnum, src[2]) << 16 |
@@ -1098,7 +1098,7 @@ static val ffi_le_i64_get(struct txr_ffi_type *tft, mem_t *src, val self)
static void ffi_le_u64_put(struct txr_ffi_type *tft, val n,
mem_t *dst, val self)
{
-#if SIZEOF_PTR >= 8
+#if CHAR_BIT * SIZEOF_PTR >= 64
ucnum v = c_unum(n);
if (v > 0xFFFFFFFFFFFFFFFF)
@@ -1136,7 +1136,7 @@ range:
static val ffi_le_u64_get(struct txr_ffi_type *tft, mem_t *src, val self)
{
-#if SIZEOF_PTR >= 8
+#if CHAR_BIT * SIZEOF_PTR >= 64
ucnum n = (convert(ucnum, src[7]) << 56 | convert(ucnum, src[6]) << 48 |
convert(ucnum, src[5]) << 40 | convert(ucnum, src[4]) << 32 |
convert(ucnum, src[3]) << 24 | convert(ucnum, src[2]) << 16 |
diff --git a/hash.c b/hash.c
index 767c224a..5ff3a5a4 100644
--- a/hash.c
+++ b/hash.c
@@ -216,10 +216,10 @@ ucnum equal_hash(val obj, int *count, ucnum seed)
case SYM:
case PKG:
case ENV:
- switch (sizeof (mem_t *)) {
- case 4:
+ switch (CHAR_BIT * sizeof (mem_t *)) {
+ case 32:
return coerce(ucnum, obj) >> 4;
- case 8: default:
+ case 64: default:
return coerce(ucnum, obj) >> 5;
}
break;
@@ -281,10 +281,10 @@ static ucnum eql_hash(val obj, int *count)
case RNG:
return eql_hash(obj->rn.from, count) + 2 * eql_hash(obj->rn.to, count);
default:
- switch (sizeof (mem_t *)) {
- case 4:
+ switch (CHAR_BIT * sizeof (mem_t *)) {
+ case 32:
return coerce(ucnum, obj) >> 4;
- case 8: default:
+ case 64: default:
return coerce(ucnum, obj) >> 5;
}
}
@@ -293,10 +293,10 @@ static ucnum eql_hash(val obj, int *count)
case TAG_NUM:
return c_num(obj);
case TAG_LIT:
- switch (sizeof (mem_t *)) {
- case 4:
+ switch (CHAR_BIT * sizeof (mem_t *)) {
+ case 32:
return coerce(ucnum, obj) >> 2;
- case 8: default:
+ case 64: default:
return coerce(ucnum, obj) >> 3;
}
}
@@ -315,10 +315,10 @@ ucnum cobj_eq_hash_op(val obj, int *count, ucnum seed)
(void) count;
(void) seed;
- switch (sizeof (mem_t *)) {
- case 4:
+ switch (CHAR_BIT * sizeof (mem_t *)) {
+ case 32:
return coerce(ucnum, obj) >> 4;
- case 8: default:
+ case 64: default:
return coerce(ucnum, obj) >> 5;
}
/* notreached */
@@ -421,10 +421,10 @@ static ucnum hash_hash_op(val obj, int *count, ucnum seed)
if ((*count)-- <= 0)
return 0;
- switch (sizeof (mem_t *)) {
- case 4:
+ switch (CHAR_BIT * sizeof (mem_t *)) {
+ case 32:
out += coerce(ucnum, h->hops) >> 4;
- case 8: default:
+ case 64: default:
out += coerce(ucnum, h->hops) >> 5;
}
diff --git a/itypes.c b/itypes.c
index a0a10e4e..5c1e3e69 100644
--- a/itypes.c
+++ b/itypes.c
@@ -98,7 +98,7 @@ u32_t c_u32(val n, val self)
#endif
#if HAVE_I64
-#if SIZEOF_PTR == 8
+#if CHAR_BIT * SIZEOF_PTR == 64
i64_t c_i64(val n, val self)
{
cnum v = c_num(n);
diff --git a/rand.c b/rand.c
index d5780d78..91d122ec 100644
--- a/rand.c
+++ b/rand.c
@@ -44,9 +44,9 @@
#define random_warmup (deref(lookup_var_l(nil, random_warmup_s)))
-#if SIZEOF_INT == 4
+#if CHAR_BIT * SIZEOF_INT == 32
typedef unsigned int rand32_t;
-#elif SIZEOF_LONG == 4
+#elif CHAR_BIT * SIZEOF_LONG == 32
typedef unsigned long rand32_t;
#endif
@@ -136,11 +136,11 @@ val make_random_state(val seed, val warmup)
r->state[0] = s & 0xFFFFFFFFul;
i = 1;
-#if SIZEOF_PTR == 8
+#if CHAR_BIT * SIZEOF_PTR == 64
s >>= 32;
r->state[1] = s & 0xFFFFFFFFul;
i = 2;
-#elif SIZEOF_PTR > 8
+#elif CHAR_BIT * SIZEOF_PTR > 64
#error port me!
#endif
} else if (nilp(seed)) {
@@ -304,14 +304,14 @@ val random(val state, val modulus)
return zero;
} else if (m > 1) {
int bits = highest_bit(m - 1);
-#if SIZEOF_PTR >= 8
+#if CHAR_BIT * SIZEOF_PTR >= 64
ucnum rands_needed = (bits + 32 - 1) / 32;
#endif
ucnum msb_rand_bits = bits % 32;
rand32_t msb_rand_mask = convert(rand32_t, -1) >> (32 - msb_rand_bits);
for (;;) {
cnum out = 0;
-#if SIZEOF_PTR >= 8
+#if CHAR_BIT * SIZEOF_PTR >= 64
ucnum i;
for (i = 0; i < rands_needed; i++) {
diff --git a/sysif.c b/sysif.c
index 43f8fe80..ccca25cf 100644
--- a/sysif.c
+++ b/sysif.c
@@ -1332,10 +1332,10 @@ static val crypt_wrap(val wkey, val wsalt)
off_t off_t_num(val num, val self)
{
- switch (sizeof(off_t)) {
- case 4:
+ switch (CHAR_BIT * sizeof(off_t)) {
+ case 32:
return c_i32(num, self);
- case 8:
+ case 64:
return c_i64(num, self);
default:
internal_error("portme: unsupported off_t size");