I pushed these to master and branch-2.5. I'm not sure how concerned we are with platforms where CHAR_BIT != 8, but this at least makes the semantics of the code a little more obvious, I think.
>From 175620d3c65209ce72e451bd75756f6bb67e33a1 Mon Sep 17 00:00:00 2001 From: Joel E. Denny <[email protected]> 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. --- ChangeLog | 11 +++++++++++ src/Sbitset.h | 18 +++++++++++------- 2 files changed, 22 insertions(+), 7 deletions(-) diff --git a/ChangeLog b/ChangeLog index a0e138d..f281abb 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,14 @@ +2009-10-16 Joel E. Denny <[email protected]> + + 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 <[email protected]> portability: use va_start and va_end in the same function. diff --git a/src/Sbitset.h b/src/Sbitset.h index a025040..0d0aabf 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) -- 1.5.4.3 >From 5297ebb3bcdf4f957bdab9c5535c2c9c47f7dc07 Mon Sep 17 00:00:00 2001 From: Joel E. Denny <[email protected]> Date: Fri, 16 Oct 2009 19:27:12 -0400 Subject: [PATCH] cleanup. * src/Sbitset.c (Sbitset__new_on_obstack): Use Sbitset instead of char*. (Sbitset__isEmpty): Use Sbitset instead of char*. * src/Sbitset.h (Sbitset): Make it a pointer to unsigned char instead of char. This helps to avoid casting errors. (Sbitset__or): Use Sbitset instead of char*. --- ChangeLog | 10 ++++++++++ src/Sbitset.c | 8 ++++---- src/Sbitset.h | 10 +++++----- 3 files changed, 19 insertions(+), 9 deletions(-) diff --git a/ChangeLog b/ChangeLog index f281abb..b1425e3 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,5 +1,15 @@ 2009-10-16 Joel E. Denny <[email protected]> + cleanup. + * src/Sbitset.c (Sbitset__new_on_obstack): Use Sbitset instead + of char*. + (Sbitset__isEmpty): Use Sbitset instead of char*. + * src/Sbitset.h (Sbitset): Make it a pointer to unsigned char + instead of char. This helps to avoid casting errors. + (Sbitset__or): Use Sbitset instead of char*. + +2009-10-16 Joel E. Denny <[email protected]> + 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. diff --git a/src/Sbitset.c b/src/Sbitset.c index af8600b..742b565 100644 --- a/src/Sbitset.c +++ b/src/Sbitset.c @@ -33,9 +33,9 @@ Sbitset__new (Sbitset__Index nbits) Sbitset Sbitset__new_on_obstack (Sbitset__Index nbits, struct obstack *obstackp) { - char *result; - char *ptr; - char *end; + Sbitset result; + Sbitset ptr; + Sbitset end; aver (nbits); result = obstack_alloc (obstackp, Sbitset__nbytes (nbits)); for (ptr = result, end = result + Sbitset__nbytes (nbits); ptr < end; ++ptr) @@ -52,7 +52,7 @@ Sbitset__delete (Sbitset self) bool Sbitset__isEmpty (Sbitset self, Sbitset__Index nbits) { - char *last = self + Sbitset__nbytes (nbits) - 1; + Sbitset last = self + Sbitset__nbytes (nbits) - 1; for (; self < last; ++self) if (*self != 0) return false; diff --git a/src/Sbitset.h b/src/Sbitset.h index 0d0aabf..e379398 100644 --- a/src/Sbitset.h +++ b/src/Sbitset.h @@ -20,7 +20,7 @@ #ifndef SBITSET_H_ # define SBITSET_H_ -typedef char *Sbitset; +typedef unsigned char *Sbitset; typedef size_t Sbitset__Index; #define SBITSET__INDEX__CONVERSION_SPEC "zu" @@ -73,10 +73,10 @@ do { \ /* NBITS is the size of every bitset. More than NBITS bits might be set. */ #define Sbitset__or(SELF, OTHER1, OTHER2, NBITS) \ do { \ - char *ptr_self = (SELF); \ - char *ptr_other1 = (OTHER1); \ - char *ptr_other2 = (OTHER2); \ - char *end_self = ptr_self + Sbitset__nbytes (NBITS); \ + Sbitset ptr_self = (SELF); \ + Sbitset ptr_other1 = (OTHER1); \ + Sbitset ptr_other2 = (OTHER2); \ + Sbitset end_self = ptr_self + Sbitset__nbytes (NBITS); \ for (; ptr_self < end_self; ++ptr_self, ++ptr_other1, ++ptr_other2) \ *ptr_self = *ptr_other1 | *ptr_other2; \ } while(0) -- 1.5.4.3
