1
0
mirror of https://git.FreeBSD.org/src.git synced 2024-11-24 07:40:52 +00:00

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:
David Malone 2000-10-09 12:34:51 +00:00
parent 3851401454
commit fc39509245
Notes: svn2git 2020-12-20 02:59:44 +00:00
svn path=/head/; revision=66872
2 changed files with 92 additions and 78 deletions

View File

@ -33,7 +33,7 @@
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
* @(#)bitstring.h 8.1 (Berkeley) 7/19/93
* $FreeBSD$
*/
#ifndef _BITSTRING_H_
@ -53,16 +53,15 @@ typedef unsigned char bitstr_t;
/* external macros */
/* bytes in a bitstring of nbits bits */
#define bitstr_size(nbits) \
((((nbits) - 1) >> 3) + 1)
(((nbits) + 7) >> 3)
/* allocate a bitstring */
#define bit_alloc(nbits) \
(bitstr_t *)calloc(1, \
(unsigned int)bitstr_size(nbits) * sizeof(bitstr_t))
(bitstr_t *)calloc((size_t)bitstr_size(nbits), sizeof(bitstr_t))
/* allocate a bitstring on the stack */
#define bit_decl(name, nbits) \
(name)[bitstr_size(nbits)]
((name)[bitstr_size(nbits)])
/* is bit N of bitstring name set? */
#define bit_test(name, bit) \
@ -70,16 +69,16 @@ typedef unsigned char bitstr_t;
/* set bit N of bitstring name */
#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 */
#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 */
#define bit_nclear(name, start, stop) { \
register bitstr_t *_name = name; \
register int _start = start, _stop = stop; \
#define bit_nclear(name, start, stop) do { \
register bitstr_t *_name = (name); \
register int _start = (start), _stop = (stop); \
register int _startbyte = _bit_byte(_start); \
register int _stopbyte = _bit_byte(_stop); \
if (_startbyte == _stopbyte) { \
@ -91,12 +90,12 @@ typedef unsigned char bitstr_t;
_name[_startbyte] = 0; \
_name[_stopbyte] &= 0xff << ((_stop&0x7) + 1); \
} \
}
} while (0)
/* set bits start ... stop in bitstring */
#define bit_nset(name, start, stop) { \
register bitstr_t *_name = name; \
register int _start = start, _stop = stop; \
#define bit_nset(name, start, stop) do { \
register bitstr_t *_name = (name); \
register int _start = (start), _stop = (stop); \
register int _startbyte = _bit_byte(_start); \
register int _stopbyte = _bit_byte(_stop); \
if (_startbyte == _stopbyte) { \
@ -108,36 +107,44 @@ typedef unsigned char bitstr_t;
_name[_startbyte] = 0xff; \
_name[_stopbyte] |= 0xff >> (7 - (_stop&0x7)); \
} \
}
} while (0)
/* find first bit clear in name */
#define bit_ffc(name, nbits, value) { \
register bitstr_t *_name = name; \
register int _byte, _nbits = nbits; \
register int _stopbyte = _bit_byte(_nbits), _value = -1; \
for (_byte = 0; _byte <= _stopbyte; ++_byte) \
if (_name[_byte] != 0xff) { \
_value = _byte << 3; \
for (_stopbyte = _name[_byte]; (_stopbyte&0x1); \
++_value, _stopbyte >>= 1); \
break; \
} \
#define bit_ffc(name, nbits, value) do { \
register bitstr_t *_name = (name); \
register int _byte, _nbits = (nbits); \
register int _stopbyte = _bit_byte(_nbits - 1), _value = -1; \
if (_nbits > 0) \
for (_byte = 0; _byte <= _stopbyte; ++_byte) \
if (_name[_byte] != 0xff) { \
bitstr_t _lb; \
_value = _byte << 3; \
for (_lb = _name[_byte]; (_lb&0x1); \
++_value, _lb >>= 1); \
break; \
} \
if (_value >= nbits) \
_value = -1; \
*(value) = _value; \
}
} while (0)
/* find first bit set in name */
#define bit_ffs(name, nbits, value) { \
register bitstr_t *_name = name; \
register int _byte, _nbits = nbits; \
register int _stopbyte = _bit_byte(_nbits), _value = -1; \
for (_byte = 0; _byte <= _stopbyte; ++_byte) \
if (_name[_byte]) { \
_value = _byte << 3; \
for (_stopbyte = _name[_byte]; !(_stopbyte&0x1); \
++_value, _stopbyte >>= 1); \
break; \
} \
#define bit_ffs(name, nbits, value) do { \
register bitstr_t *_name = (name); \
register int _byte, _nbits = (nbits); \
register int _stopbyte = _bit_byte(_nbits - 1), _value = -1; \
if (_nbits > 0) \
for (_byte = 0; _byte <= _stopbyte; ++_byte) \
if (_name[_byte]) { \
bitstr_t _lb; \
_value = _byte << 3; \
for (_lb = _name[_byte]; !(_lb&0x1); \
++_value, _lb >>= 1); \
break; \
} \
if (_value >= nbits) \
_value = -1; \
*(value) = _value; \
}
} while (0)
#endif /* !_BITSTRING_H_ */

View File

@ -33,7 +33,7 @@
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
* @(#)bitstring.h 8.1 (Berkeley) 7/19/93
* $FreeBSD$
*/
#ifndef _BITSTRING_H_
@ -53,16 +53,15 @@ typedef unsigned char bitstr_t;
/* external macros */
/* bytes in a bitstring of nbits bits */
#define bitstr_size(nbits) \
((((nbits) - 1) >> 3) + 1)
(((nbits) + 7) >> 3)
/* allocate a bitstring */
#define bit_alloc(nbits) \
(bitstr_t *)calloc(1, \
(unsigned int)bitstr_size(nbits) * sizeof(bitstr_t))
(bitstr_t *)calloc((size_t)bitstr_size(nbits), sizeof(bitstr_t))
/* allocate a bitstring on the stack */
#define bit_decl(name, nbits) \
(name)[bitstr_size(nbits)]
((name)[bitstr_size(nbits)])
/* is bit N of bitstring name set? */
#define bit_test(name, bit) \
@ -70,16 +69,16 @@ typedef unsigned char bitstr_t;
/* set bit N of bitstring name */
#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 */
#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 */
#define bit_nclear(name, start, stop) { \
register bitstr_t *_name = name; \
register int _start = start, _stop = stop; \
#define bit_nclear(name, start, stop) do { \
register bitstr_t *_name = (name); \
register int _start = (start), _stop = (stop); \
register int _startbyte = _bit_byte(_start); \
register int _stopbyte = _bit_byte(_stop); \
if (_startbyte == _stopbyte) { \
@ -91,12 +90,12 @@ typedef unsigned char bitstr_t;
_name[_startbyte] = 0; \
_name[_stopbyte] &= 0xff << ((_stop&0x7) + 1); \
} \
}
} while (0)
/* set bits start ... stop in bitstring */
#define bit_nset(name, start, stop) { \
register bitstr_t *_name = name; \
register int _start = start, _stop = stop; \
#define bit_nset(name, start, stop) do { \
register bitstr_t *_name = (name); \
register int _start = (start), _stop = (stop); \
register int _startbyte = _bit_byte(_start); \
register int _stopbyte = _bit_byte(_stop); \
if (_startbyte == _stopbyte) { \
@ -108,36 +107,44 @@ typedef unsigned char bitstr_t;
_name[_startbyte] = 0xff; \
_name[_stopbyte] |= 0xff >> (7 - (_stop&0x7)); \
} \
}
} while (0)
/* find first bit clear in name */
#define bit_ffc(name, nbits, value) { \
register bitstr_t *_name = name; \
register int _byte, _nbits = nbits; \
register int _stopbyte = _bit_byte(_nbits), _value = -1; \
for (_byte = 0; _byte <= _stopbyte; ++_byte) \
if (_name[_byte] != 0xff) { \
_value = _byte << 3; \
for (_stopbyte = _name[_byte]; (_stopbyte&0x1); \
++_value, _stopbyte >>= 1); \
break; \
} \
#define bit_ffc(name, nbits, value) do { \
register bitstr_t *_name = (name); \
register int _byte, _nbits = (nbits); \
register int _stopbyte = _bit_byte(_nbits - 1), _value = -1; \
if (_nbits > 0) \
for (_byte = 0; _byte <= _stopbyte; ++_byte) \
if (_name[_byte] != 0xff) { \
bitstr_t _lb; \
_value = _byte << 3; \
for (_lb = _name[_byte]; (_lb&0x1); \
++_value, _lb >>= 1); \
break; \
} \
if (_value >= nbits) \
_value = -1; \
*(value) = _value; \
}
} while (0)
/* find first bit set in name */
#define bit_ffs(name, nbits, value) { \
register bitstr_t *_name = name; \
register int _byte, _nbits = nbits; \
register int _stopbyte = _bit_byte(_nbits), _value = -1; \
for (_byte = 0; _byte <= _stopbyte; ++_byte) \
if (_name[_byte]) { \
_value = _byte << 3; \
for (_stopbyte = _name[_byte]; !(_stopbyte&0x1); \
++_value, _stopbyte >>= 1); \
break; \
} \
#define bit_ffs(name, nbits, value) do { \
register bitstr_t *_name = (name); \
register int _byte, _nbits = (nbits); \
register int _stopbyte = _bit_byte(_nbits - 1), _value = -1; \
if (_nbits > 0) \
for (_byte = 0; _byte <= _stopbyte; ++_byte) \
if (_name[_byte]) { \
bitstr_t _lb; \
_value = _byte << 3; \
for (_lb = _name[_byte]; !(_lb&0x1); \
++_value, _lb >>= 1); \
break; \
} \
if (_value >= nbits) \
_value = -1; \
*(value) = _value; \
}
} while (0)
#endif /* !_BITSTRING_H_ */