#ifndef _SYS_BITSTRING_H_
#define _SYS_BITSTRING_H_
#ifdef _KERNEL
#include <sys/libkern.h>
#include <sys/malloc.h>
#endif
#include <sys/types.h>
typedef unsigned long bitstr_t;
#define _BITSTR_MASK (~0UL)
#define _BITSTR_BITS (sizeof(bitstr_t) * 8)
#ifdef roundup2
#define _bit_roundup2 roundup2
#else
#define _bit_roundup2(x, y) (((x)+((y)-1))&(~((y)-1)))
#endif
static inline int
_bit_idx(int _bit)
{
return (_bit / _BITSTR_BITS);
}
static inline int
_bit_offset(int _bit)
{
return (_bit % _BITSTR_BITS);
}
static inline bitstr_t
_bit_mask(int _bit)
{
return (1UL << _bit_offset(_bit));
}
static inline bitstr_t
_bit_make_mask(int _start, int _stop)
{
return ((_BITSTR_MASK << _bit_offset(_start)) &
(_BITSTR_MASK >> (_BITSTR_BITS - _bit_offset(_stop) - 1)));
}
#define bitstr_size(_nbits) (_bit_roundup2(_nbits, _BITSTR_BITS) / 8)
#ifdef _KERNEL
static inline bitstr_t *
bit_alloc(int _nbits, struct malloc_type *type, int flags)
{
return ((bitstr_t *)malloc(bitstr_size(_nbits), type, flags | M_ZERO));
}
#else
static inline bitstr_t *
bit_alloc(int _nbits)
{
return ((bitstr_t *)calloc(bitstr_size(_nbits), 1));
}
#endif
#define bit_decl(name, nbits) \
((name)[bitstr_size(nbits) / sizeof(bitstr_t)])
static inline int
bit_test(const bitstr_t *_bitstr, int _bit)
{
return ((_bitstr[_bit_idx(_bit)] & _bit_mask(_bit)) != 0);
}
static inline void
bit_set(bitstr_t *_bitstr, int _bit)
{
_bitstr[_bit_idx(_bit)] |= _bit_mask(_bit);
}
static inline void
bit_clear(bitstr_t *_bitstr, int _bit)
{
_bitstr[_bit_idx(_bit)] &= ~_bit_mask(_bit);
}
static inline void
bit_nset(bitstr_t *_bitstr, int _start, int _stop)
{
bitstr_t *_stopbitstr;
_stopbitstr = _bitstr + _bit_idx(_stop);
_bitstr += _bit_idx(_start);
if (_bitstr == _stopbitstr) {
*_bitstr |= _bit_make_mask(_start, _stop);
} else {
*_bitstr |= _bit_make_mask(_start, _BITSTR_BITS - 1);
while (++_bitstr < _stopbitstr)
*_bitstr = _BITSTR_MASK;
*_stopbitstr |= _bit_make_mask(0, _stop);
}
}
static inline void
bit_nclear(bitstr_t *_bitstr, int _start, int _stop)
{
bitstr_t *_stopbitstr;
_stopbitstr = _bitstr + _bit_idx(_stop);
_bitstr += _bit_idx(_start);
if (_bitstr == _stopbitstr) {
*_bitstr &= ~_bit_make_mask(_start, _stop);
} else {
*_bitstr &= ~_bit_make_mask(_start, _BITSTR_BITS - 1);
while (++_bitstr < _stopbitstr)
*_bitstr = 0;
*_stopbitstr &= ~_bit_make_mask(0, _stop);
}
}
static inline void
bit_ffs_at(bitstr_t *_bitstr, int _start, int _nbits, int *_result)
{
bitstr_t *_curbitstr;
bitstr_t *_stopbitstr;
bitstr_t _test;
int _value, _offset;
if (_nbits > 0) {
_curbitstr = _bitstr + _bit_idx(_start);
_stopbitstr = _bitstr + _bit_idx(_nbits - 1);
_test = *_curbitstr;
if (_bit_offset(_start) != 0)
_test &= _bit_make_mask(_start, _BITSTR_BITS - 1);
while (_test == 0 && _curbitstr < _stopbitstr)
_test = *(++_curbitstr);
_offset = ffsl(_test);
_value = ((_curbitstr - _bitstr) * _BITSTR_BITS) + _offset - 1;
if (_offset == 0 || _value >= _nbits)
_value = -1;
} else {
_value = -1;
}
*_result = _value;
}
static inline void
bit_ffc_at(bitstr_t *_bitstr, int _start, int _nbits, int *_result)
{
bitstr_t *_curbitstr;
bitstr_t *_stopbitstr;
bitstr_t _test;
int _value, _offset;
if (_nbits > 0) {
_curbitstr = _bitstr + _bit_idx(_start);
_stopbitstr = _bitstr + _bit_idx(_nbits - 1);
_test = *_curbitstr;
if (_bit_offset(_start) != 0)
_test |= _bit_make_mask(0, _start - 1);
while (_test == _BITSTR_MASK && _curbitstr < _stopbitstr)
_test = *(++_curbitstr);
_offset = ffsl(~_test);
_value = ((_curbitstr - _bitstr) * _BITSTR_BITS) + _offset - 1;
if (_offset == 0 || _value >= _nbits)
_value = -1;
} else {
_value = -1;
}
*_result = _value;
}
static inline void
bit_ffs(bitstr_t *_bitstr, int _nbits, int *_result)
{
bit_ffs_at(_bitstr, 0, _nbits, _result);
}
static inline void
bit_ffc(bitstr_t *_bitstr, int _nbits, int *_result)
{
bit_ffc_at(_bitstr, 0, _nbits, _result);
}
static inline void
bit_count(bitstr_t *_bitstr, int _start, int _nbits, int *_result)
{
bitstr_t *_curbitstr, mask;
int _value = 0, curbitstr_len;
if (_start >= _nbits)
goto out;
_curbitstr = _bitstr + _bit_idx(_start);
_nbits -= _BITSTR_BITS * _bit_idx(_start);
_start -= _BITSTR_BITS * _bit_idx(_start);
if (_start > 0) {
curbitstr_len = (int)_BITSTR_BITS < _nbits ?
(int)_BITSTR_BITS : _nbits;
mask = _bit_make_mask(_start, _bit_offset(curbitstr_len - 1));
_value += __bitcountl(*_curbitstr & mask);
_curbitstr++;
_nbits -= _BITSTR_BITS;
}
while (_nbits >= (int)_BITSTR_BITS) {
_value += __bitcountl(*_curbitstr);
_curbitstr++;
_nbits -= _BITSTR_BITS;
}
if (_nbits > 0) {
mask = _bit_make_mask(0, _bit_offset(_nbits - 1));
_value += __bitcountl(*_curbitstr & mask);
}
out:
*_result = _value;
}
#endif