From 61747047da2bb7e64c560f9bbdc5f194c81e3702 Mon Sep 17 00:00:00 2001 From: "Joel E. Denny" Date: Fri, 16 Oct 2009 19:20:43 -0400 Subject: [PATCH] portability: don't assume 8-bit bytes. That is, use CHAR_BIT and UCHAR_MAX instead of 8 and 0xff. * src/Sbitset.h (Sbitset__nbytes): Here. (Sbitset__byteAddress): Here. (Sbitset__bit_mask): Here. (Sbitset__last_byte_mask): Here. (Sbitset__ones): Here. (SBITSET__FOR_EACH): Here. (cherry picked from commit 175620d3c65209ce72e451bd75756f6bb67e33a1) --- ChangeLog | 11 +++++++++++ src/Sbitset.h | 18 +++++++++++------- 2 files changed, 22 insertions(+), 7 deletions(-) diff --git a/ChangeLog b/ChangeLog index 0a15c5a0..00343fc8 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,14 @@ +2009-10-16 Joel E. Denny + + portability: don't assume 8-bit bytes. + That is, use CHAR_BIT and UCHAR_MAX instead of 8 and 0xff. + * src/Sbitset.h (Sbitset__nbytes): Here. + (Sbitset__byteAddress): Here. + (Sbitset__bit_mask): Here. + (Sbitset__last_byte_mask): Here. + (Sbitset__ones): Here. + (SBITSET__FOR_EACH): Here. + 2009-10-11 Joel E. Denny portability: use va_start and va_end in the same function. diff --git a/src/Sbitset.h b/src/Sbitset.h index a0250404..0d0aabf8 100644 --- a/src/Sbitset.h +++ b/src/Sbitset.h @@ -24,10 +24,14 @@ typedef char *Sbitset; typedef size_t Sbitset__Index; #define SBITSET__INDEX__CONVERSION_SPEC "zu" -#define Sbitset__nbytes(NBITS) (((NBITS)+7)/8) -#define Sbitset__byteAddress(SELF, INDEX) (((SELF) + (INDEX)/8)) -#define Sbitset__bit_mask(INDEX) (0x1 << (7 - (INDEX)%8)) -#define Sbitset__last_byte_mask(NBITS) (0xff << (7 - ((NBITS)-1)%8)) +#define Sbitset__nbytes(NBITS) \ + (((NBITS) + CHAR_BIT - 1) / CHAR_BIT) +#define Sbitset__byteAddress(SELF, INDEX) \ + (((SELF) + (INDEX) / CHAR_BIT)) +#define Sbitset__bit_mask(INDEX) \ + (1 << (CHAR_BIT - 1 - (INDEX) % CHAR_BIT)) +#define Sbitset__last_byte_mask(NBITS) \ + (UCHAR_MAX << (CHAR_BIT - 1 - ((NBITS) - 1) % CHAR_BIT)) /* nbits must not be 0. */ Sbitset Sbitset__new (Sbitset__Index nbits); @@ -63,7 +67,7 @@ do { \ /* NBITS is the size of the bitset. More than NBITS bits might be set. */ #define Sbitset__ones(SELF, NBITS) \ do { \ - memset (SELF, 0xff, Sbitset__nbytes (NBITS)); \ + memset (SELF, UCHAR_MAX, Sbitset__nbytes (NBITS)); \ } while(0) /* NBITS is the size of every bitset. More than NBITS bits might be set. */ @@ -80,8 +84,8 @@ do { \ #define SBITSET__FOR_EACH(SELF, NBITS, ITER, INDEX) \ for ((ITER) = (SELF); (ITER) < (SELF) + Sbitset__nbytes (NBITS); ++(ITER)) \ if (*(ITER) != 0) \ - for ((INDEX) = ((ITER)-(SELF))*8; \ - (INDEX) < (NBITS) && (SELF)+(INDEX)/8 < (ITER)+1; \ + for ((INDEX) = ((ITER)-(SELF))*CHAR_BIT; \ + (INDEX) < (NBITS) && (SELF)+(INDEX)/CHAR_BIT < (ITER)+1; \ ++(INDEX)) \ if (((*ITER) & Sbitset__bit_mask (INDEX)) != 0) -- 2.45.2