X-Git-Url: https://git.saurik.com/bison.git/blobdiff_plain/166366b28f579dfb991f8f6fca323847bcc1eb65..63951be29fcb8e21146a9377176d53d509c575ca:/src/Sbitset.h?ds=sidebyside diff --git a/src/Sbitset.h b/src/Sbitset.h index 4b4b7508..4a32402e 100644 --- a/src/Sbitset.h +++ b/src/Sbitset.h @@ -1,6 +1,6 @@ /* A simple, memory-efficient bitset implementation. - Copyright (C) 2009 Free Software Foundation, Inc. + Copyright (C) 2009-2012 Free Software Foundation, Inc. This file is part of Bison, the GNU Compiler Compiler. @@ -20,13 +20,18 @@ #ifndef SBITSET_H_ # define SBITSET_H_ -typedef char *Sbitset; +typedef unsigned 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); @@ -62,16 +67,16 @@ 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. */ #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) @@ -79,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)