Cleanup of bitstring.h:
1) Add FreeBSD: tag. 2) Add parenthesis around macro args. 3) Add parenthesis around macros which are expressions. 4) Add do { ... } while (0) around macros which are compound statements. 5) Sync bitstr_size and bit_alloc with neater versions from NetBSD. 6) Fix bit_ffs and bit_ffc so that they don't search off the end of the bitstirng. 7) Try to avoid rightshifting signed ints. I didn't take NetBSD's version directly as the macros are significantly slower for long bitstrings. Bruce reviewed a previous version of this patch. PR: 21204 Submitted by: bob@immure.com Reviewed by: bde
This commit is contained in:
parent
3851401454
commit
fc39509245
Notes:
svn2git
2020-12-20 02:59:44 +00:00
svn path=/head/; revision=66872
@ -33,7 +33,7 @@
|
|||||||
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
||||||
* SUCH DAMAGE.
|
* SUCH DAMAGE.
|
||||||
*
|
*
|
||||||
* @(#)bitstring.h 8.1 (Berkeley) 7/19/93
|
* $FreeBSD$
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#ifndef _BITSTRING_H_
|
#ifndef _BITSTRING_H_
|
||||||
@ -53,16 +53,15 @@ typedef unsigned char bitstr_t;
|
|||||||
/* external macros */
|
/* external macros */
|
||||||
/* bytes in a bitstring of nbits bits */
|
/* bytes in a bitstring of nbits bits */
|
||||||
#define bitstr_size(nbits) \
|
#define bitstr_size(nbits) \
|
||||||
((((nbits) - 1) >> 3) + 1)
|
(((nbits) + 7) >> 3)
|
||||||
|
|
||||||
/* allocate a bitstring */
|
/* allocate a bitstring */
|
||||||
#define bit_alloc(nbits) \
|
#define bit_alloc(nbits) \
|
||||||
(bitstr_t *)calloc(1, \
|
(bitstr_t *)calloc((size_t)bitstr_size(nbits), sizeof(bitstr_t))
|
||||||
(unsigned int)bitstr_size(nbits) * sizeof(bitstr_t))
|
|
||||||
|
|
||||||
/* allocate a bitstring on the stack */
|
/* allocate a bitstring on the stack */
|
||||||
#define bit_decl(name, nbits) \
|
#define bit_decl(name, nbits) \
|
||||||
(name)[bitstr_size(nbits)]
|
((name)[bitstr_size(nbits)])
|
||||||
|
|
||||||
/* is bit N of bitstring name set? */
|
/* is bit N of bitstring name set? */
|
||||||
#define bit_test(name, bit) \
|
#define bit_test(name, bit) \
|
||||||
@ -70,16 +69,16 @@ typedef unsigned char bitstr_t;
|
|||||||
|
|
||||||
/* set bit N of bitstring name */
|
/* set bit N of bitstring name */
|
||||||
#define bit_set(name, bit) \
|
#define bit_set(name, bit) \
|
||||||
(name)[_bit_byte(bit)] |= _bit_mask(bit)
|
((name)[_bit_byte(bit)] |= _bit_mask(bit))
|
||||||
|
|
||||||
/* clear bit N of bitstring name */
|
/* clear bit N of bitstring name */
|
||||||
#define bit_clear(name, bit) \
|
#define bit_clear(name, bit) \
|
||||||
(name)[_bit_byte(bit)] &= ~_bit_mask(bit)
|
((name)[_bit_byte(bit)] &= ~_bit_mask(bit))
|
||||||
|
|
||||||
/* clear bits start ... stop in bitstring */
|
/* clear bits start ... stop in bitstring */
|
||||||
#define bit_nclear(name, start, stop) { \
|
#define bit_nclear(name, start, stop) do { \
|
||||||
register bitstr_t *_name = name; \
|
register bitstr_t *_name = (name); \
|
||||||
register int _start = start, _stop = stop; \
|
register int _start = (start), _stop = (stop); \
|
||||||
register int _startbyte = _bit_byte(_start); \
|
register int _startbyte = _bit_byte(_start); \
|
||||||
register int _stopbyte = _bit_byte(_stop); \
|
register int _stopbyte = _bit_byte(_stop); \
|
||||||
if (_startbyte == _stopbyte) { \
|
if (_startbyte == _stopbyte) { \
|
||||||
@ -91,12 +90,12 @@ typedef unsigned char bitstr_t;
|
|||||||
_name[_startbyte] = 0; \
|
_name[_startbyte] = 0; \
|
||||||
_name[_stopbyte] &= 0xff << ((_stop&0x7) + 1); \
|
_name[_stopbyte] &= 0xff << ((_stop&0x7) + 1); \
|
||||||
} \
|
} \
|
||||||
}
|
} while (0)
|
||||||
|
|
||||||
/* set bits start ... stop in bitstring */
|
/* set bits start ... stop in bitstring */
|
||||||
#define bit_nset(name, start, stop) { \
|
#define bit_nset(name, start, stop) do { \
|
||||||
register bitstr_t *_name = name; \
|
register bitstr_t *_name = (name); \
|
||||||
register int _start = start, _stop = stop; \
|
register int _start = (start), _stop = (stop); \
|
||||||
register int _startbyte = _bit_byte(_start); \
|
register int _startbyte = _bit_byte(_start); \
|
||||||
register int _stopbyte = _bit_byte(_stop); \
|
register int _stopbyte = _bit_byte(_stop); \
|
||||||
if (_startbyte == _stopbyte) { \
|
if (_startbyte == _stopbyte) { \
|
||||||
@ -108,36 +107,44 @@ typedef unsigned char bitstr_t;
|
|||||||
_name[_startbyte] = 0xff; \
|
_name[_startbyte] = 0xff; \
|
||||||
_name[_stopbyte] |= 0xff >> (7 - (_stop&0x7)); \
|
_name[_stopbyte] |= 0xff >> (7 - (_stop&0x7)); \
|
||||||
} \
|
} \
|
||||||
}
|
} while (0)
|
||||||
|
|
||||||
/* find first bit clear in name */
|
/* find first bit clear in name */
|
||||||
#define bit_ffc(name, nbits, value) { \
|
#define bit_ffc(name, nbits, value) do { \
|
||||||
register bitstr_t *_name = name; \
|
register bitstr_t *_name = (name); \
|
||||||
register int _byte, _nbits = nbits; \
|
register int _byte, _nbits = (nbits); \
|
||||||
register int _stopbyte = _bit_byte(_nbits), _value = -1; \
|
register int _stopbyte = _bit_byte(_nbits - 1), _value = -1; \
|
||||||
for (_byte = 0; _byte <= _stopbyte; ++_byte) \
|
if (_nbits > 0) \
|
||||||
if (_name[_byte] != 0xff) { \
|
for (_byte = 0; _byte <= _stopbyte; ++_byte) \
|
||||||
_value = _byte << 3; \
|
if (_name[_byte] != 0xff) { \
|
||||||
for (_stopbyte = _name[_byte]; (_stopbyte&0x1); \
|
bitstr_t _lb; \
|
||||||
++_value, _stopbyte >>= 1); \
|
_value = _byte << 3; \
|
||||||
break; \
|
for (_lb = _name[_byte]; (_lb&0x1); \
|
||||||
} \
|
++_value, _lb >>= 1); \
|
||||||
|
break; \
|
||||||
|
} \
|
||||||
|
if (_value >= nbits) \
|
||||||
|
_value = -1; \
|
||||||
*(value) = _value; \
|
*(value) = _value; \
|
||||||
}
|
} while (0)
|
||||||
|
|
||||||
/* find first bit set in name */
|
/* find first bit set in name */
|
||||||
#define bit_ffs(name, nbits, value) { \
|
#define bit_ffs(name, nbits, value) do { \
|
||||||
register bitstr_t *_name = name; \
|
register bitstr_t *_name = (name); \
|
||||||
register int _byte, _nbits = nbits; \
|
register int _byte, _nbits = (nbits); \
|
||||||
register int _stopbyte = _bit_byte(_nbits), _value = -1; \
|
register int _stopbyte = _bit_byte(_nbits - 1), _value = -1; \
|
||||||
for (_byte = 0; _byte <= _stopbyte; ++_byte) \
|
if (_nbits > 0) \
|
||||||
if (_name[_byte]) { \
|
for (_byte = 0; _byte <= _stopbyte; ++_byte) \
|
||||||
_value = _byte << 3; \
|
if (_name[_byte]) { \
|
||||||
for (_stopbyte = _name[_byte]; !(_stopbyte&0x1); \
|
bitstr_t _lb; \
|
||||||
++_value, _stopbyte >>= 1); \
|
_value = _byte << 3; \
|
||||||
break; \
|
for (_lb = _name[_byte]; !(_lb&0x1); \
|
||||||
} \
|
++_value, _lb >>= 1); \
|
||||||
|
break; \
|
||||||
|
} \
|
||||||
|
if (_value >= nbits) \
|
||||||
|
_value = -1; \
|
||||||
*(value) = _value; \
|
*(value) = _value; \
|
||||||
}
|
} while (0)
|
||||||
|
|
||||||
#endif /* !_BITSTRING_H_ */
|
#endif /* !_BITSTRING_H_ */
|
||||||
|
@ -33,7 +33,7 @@
|
|||||||
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
||||||
* SUCH DAMAGE.
|
* SUCH DAMAGE.
|
||||||
*
|
*
|
||||||
* @(#)bitstring.h 8.1 (Berkeley) 7/19/93
|
* $FreeBSD$
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#ifndef _BITSTRING_H_
|
#ifndef _BITSTRING_H_
|
||||||
@ -53,16 +53,15 @@ typedef unsigned char bitstr_t;
|
|||||||
/* external macros */
|
/* external macros */
|
||||||
/* bytes in a bitstring of nbits bits */
|
/* bytes in a bitstring of nbits bits */
|
||||||
#define bitstr_size(nbits) \
|
#define bitstr_size(nbits) \
|
||||||
((((nbits) - 1) >> 3) + 1)
|
(((nbits) + 7) >> 3)
|
||||||
|
|
||||||
/* allocate a bitstring */
|
/* allocate a bitstring */
|
||||||
#define bit_alloc(nbits) \
|
#define bit_alloc(nbits) \
|
||||||
(bitstr_t *)calloc(1, \
|
(bitstr_t *)calloc((size_t)bitstr_size(nbits), sizeof(bitstr_t))
|
||||||
(unsigned int)bitstr_size(nbits) * sizeof(bitstr_t))
|
|
||||||
|
|
||||||
/* allocate a bitstring on the stack */
|
/* allocate a bitstring on the stack */
|
||||||
#define bit_decl(name, nbits) \
|
#define bit_decl(name, nbits) \
|
||||||
(name)[bitstr_size(nbits)]
|
((name)[bitstr_size(nbits)])
|
||||||
|
|
||||||
/* is bit N of bitstring name set? */
|
/* is bit N of bitstring name set? */
|
||||||
#define bit_test(name, bit) \
|
#define bit_test(name, bit) \
|
||||||
@ -70,16 +69,16 @@ typedef unsigned char bitstr_t;
|
|||||||
|
|
||||||
/* set bit N of bitstring name */
|
/* set bit N of bitstring name */
|
||||||
#define bit_set(name, bit) \
|
#define bit_set(name, bit) \
|
||||||
(name)[_bit_byte(bit)] |= _bit_mask(bit)
|
((name)[_bit_byte(bit)] |= _bit_mask(bit))
|
||||||
|
|
||||||
/* clear bit N of bitstring name */
|
/* clear bit N of bitstring name */
|
||||||
#define bit_clear(name, bit) \
|
#define bit_clear(name, bit) \
|
||||||
(name)[_bit_byte(bit)] &= ~_bit_mask(bit)
|
((name)[_bit_byte(bit)] &= ~_bit_mask(bit))
|
||||||
|
|
||||||
/* clear bits start ... stop in bitstring */
|
/* clear bits start ... stop in bitstring */
|
||||||
#define bit_nclear(name, start, stop) { \
|
#define bit_nclear(name, start, stop) do { \
|
||||||
register bitstr_t *_name = name; \
|
register bitstr_t *_name = (name); \
|
||||||
register int _start = start, _stop = stop; \
|
register int _start = (start), _stop = (stop); \
|
||||||
register int _startbyte = _bit_byte(_start); \
|
register int _startbyte = _bit_byte(_start); \
|
||||||
register int _stopbyte = _bit_byte(_stop); \
|
register int _stopbyte = _bit_byte(_stop); \
|
||||||
if (_startbyte == _stopbyte) { \
|
if (_startbyte == _stopbyte) { \
|
||||||
@ -91,12 +90,12 @@ typedef unsigned char bitstr_t;
|
|||||||
_name[_startbyte] = 0; \
|
_name[_startbyte] = 0; \
|
||||||
_name[_stopbyte] &= 0xff << ((_stop&0x7) + 1); \
|
_name[_stopbyte] &= 0xff << ((_stop&0x7) + 1); \
|
||||||
} \
|
} \
|
||||||
}
|
} while (0)
|
||||||
|
|
||||||
/* set bits start ... stop in bitstring */
|
/* set bits start ... stop in bitstring */
|
||||||
#define bit_nset(name, start, stop) { \
|
#define bit_nset(name, start, stop) do { \
|
||||||
register bitstr_t *_name = name; \
|
register bitstr_t *_name = (name); \
|
||||||
register int _start = start, _stop = stop; \
|
register int _start = (start), _stop = (stop); \
|
||||||
register int _startbyte = _bit_byte(_start); \
|
register int _startbyte = _bit_byte(_start); \
|
||||||
register int _stopbyte = _bit_byte(_stop); \
|
register int _stopbyte = _bit_byte(_stop); \
|
||||||
if (_startbyte == _stopbyte) { \
|
if (_startbyte == _stopbyte) { \
|
||||||
@ -108,36 +107,44 @@ typedef unsigned char bitstr_t;
|
|||||||
_name[_startbyte] = 0xff; \
|
_name[_startbyte] = 0xff; \
|
||||||
_name[_stopbyte] |= 0xff >> (7 - (_stop&0x7)); \
|
_name[_stopbyte] |= 0xff >> (7 - (_stop&0x7)); \
|
||||||
} \
|
} \
|
||||||
}
|
} while (0)
|
||||||
|
|
||||||
/* find first bit clear in name */
|
/* find first bit clear in name */
|
||||||
#define bit_ffc(name, nbits, value) { \
|
#define bit_ffc(name, nbits, value) do { \
|
||||||
register bitstr_t *_name = name; \
|
register bitstr_t *_name = (name); \
|
||||||
register int _byte, _nbits = nbits; \
|
register int _byte, _nbits = (nbits); \
|
||||||
register int _stopbyte = _bit_byte(_nbits), _value = -1; \
|
register int _stopbyte = _bit_byte(_nbits - 1), _value = -1; \
|
||||||
for (_byte = 0; _byte <= _stopbyte; ++_byte) \
|
if (_nbits > 0) \
|
||||||
if (_name[_byte] != 0xff) { \
|
for (_byte = 0; _byte <= _stopbyte; ++_byte) \
|
||||||
_value = _byte << 3; \
|
if (_name[_byte] != 0xff) { \
|
||||||
for (_stopbyte = _name[_byte]; (_stopbyte&0x1); \
|
bitstr_t _lb; \
|
||||||
++_value, _stopbyte >>= 1); \
|
_value = _byte << 3; \
|
||||||
break; \
|
for (_lb = _name[_byte]; (_lb&0x1); \
|
||||||
} \
|
++_value, _lb >>= 1); \
|
||||||
|
break; \
|
||||||
|
} \
|
||||||
|
if (_value >= nbits) \
|
||||||
|
_value = -1; \
|
||||||
*(value) = _value; \
|
*(value) = _value; \
|
||||||
}
|
} while (0)
|
||||||
|
|
||||||
/* find first bit set in name */
|
/* find first bit set in name */
|
||||||
#define bit_ffs(name, nbits, value) { \
|
#define bit_ffs(name, nbits, value) do { \
|
||||||
register bitstr_t *_name = name; \
|
register bitstr_t *_name = (name); \
|
||||||
register int _byte, _nbits = nbits; \
|
register int _byte, _nbits = (nbits); \
|
||||||
register int _stopbyte = _bit_byte(_nbits), _value = -1; \
|
register int _stopbyte = _bit_byte(_nbits - 1), _value = -1; \
|
||||||
for (_byte = 0; _byte <= _stopbyte; ++_byte) \
|
if (_nbits > 0) \
|
||||||
if (_name[_byte]) { \
|
for (_byte = 0; _byte <= _stopbyte; ++_byte) \
|
||||||
_value = _byte << 3; \
|
if (_name[_byte]) { \
|
||||||
for (_stopbyte = _name[_byte]; !(_stopbyte&0x1); \
|
bitstr_t _lb; \
|
||||||
++_value, _stopbyte >>= 1); \
|
_value = _byte << 3; \
|
||||||
break; \
|
for (_lb = _name[_byte]; !(_lb&0x1); \
|
||||||
} \
|
++_value, _lb >>= 1); \
|
||||||
|
break; \
|
||||||
|
} \
|
||||||
|
if (_value >= nbits) \
|
||||||
|
_value = -1; \
|
||||||
*(value) = _value; \
|
*(value) = _value; \
|
||||||
}
|
} while (0)
|
||||||
|
|
||||||
#endif /* !_BITSTRING_H_ */
|
#endif /* !_BITSTRING_H_ */
|
||||||
|
Loading…
Reference in New Issue
Block a user