bitstring.h revision 300544
1139825Simp/*- 21539Srgrimes * Copyright (c) 1989, 1993 31539Srgrimes * The Regents of the University of California. All rights reserved. 41539Srgrimes * 51539Srgrimes * This code is derived from software contributed to Berkeley by 61539Srgrimes * Paul Vixie. 71539Srgrimes * 81539Srgrimes * Redistribution and use in source and binary forms, with or without 91539Srgrimes * modification, are permitted provided that the following conditions 101539Srgrimes * are met: 111539Srgrimes * 1. Redistributions of source code must retain the above copyright 121539Srgrimes * notice, this list of conditions and the following disclaimer. 131539Srgrimes * 2. Redistributions in binary form must reproduce the above copyright 141539Srgrimes * notice, this list of conditions and the following disclaimer in the 151539Srgrimes * documentation and/or other materials provided with the distribution. 161539Srgrimes * 4. Neither the name of the University nor the names of its contributors 171539Srgrimes * may be used to endorse or promote products derived from this software 181539Srgrimes * without specific prior written permission. 191539Srgrimes * 201539Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 211539Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 221539Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 231539Srgrimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 241539Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 251539Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 261539Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 271539Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 281539Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 291539Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 301539Srgrimes * SUCH DAMAGE. 311539Srgrimes * 32299090Sasomers * Copyright (c) 2014 Spectra Logic Corporation 33299090Sasomers * All rights reserved. 34299090Sasomers * 35299090Sasomers * Redistribution and use in source and binary forms, with or without 36299090Sasomers * modification, are permitted provided that the following conditions 37299090Sasomers * are met: 38299090Sasomers * 1. Redistributions of source code must retain the above copyright 39299090Sasomers * notice, this list of conditions, and the following disclaimer, 40299090Sasomers * without modification. 41299090Sasomers * 2. Redistributions in binary form must reproduce at minimum a disclaimer 42299090Sasomers * substantially similar to the "NO WARRANTY" disclaimer below 43299090Sasomers * ("Disclaimer") and any redistribution must be conditioned upon 44299090Sasomers * including a substantially similar Disclaimer requirement for further 45299090Sasomers * binary redistribution. 46299090Sasomers * 47299090Sasomers * NO WARRANTY 48299090Sasomers * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 49299090Sasomers * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 50299090Sasomers * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR 51299090Sasomers * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 52299090Sasomers * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 53299090Sasomers * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 54299090Sasomers * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 55299090Sasomers * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 56299090Sasomers * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING 57299090Sasomers * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 58299090Sasomers * POSSIBILITY OF SUCH DAMAGES. 59299090Sasomers * 6066872Sdwmalone * $FreeBSD: head/sys/sys/bitstring.h 300544 2016-05-24 00:14:58Z asomers $ 611539Srgrimes */ 62116306Sphk#ifndef _SYS_BITSTRING_H_ 63116306Sphk#define _SYS_BITSTRING_H_ 641539Srgrimes 65299090Sasomers#ifdef _KERNEL 66299090Sasomers#include <sys/libkern.h> 67299090Sasomers#include <sys/malloc.h> 68299090Sasomers#endif 691539Srgrimes 70300544Sasomers#include <sys/types.h> 71300544Sasomers 72299090Sasomerstypedef unsigned long bitstr_t; 731539Srgrimes 74299090Sasomers/*---------------------- Private Implementation Details ----------------------*/ 75299090Sasomers#define _BITSTR_MASK (~0UL) 76299090Sasomers#define _BITSTR_BITS (sizeof(bitstr_t) * 8) 771539Srgrimes 78299090Sasomers/* bitstr_t in bit string containing the bit. */ 79299090Sasomersstatic inline int 80299090Sasomers_bit_idx(int _bit) 81299090Sasomers{ 82299090Sasomers return (_bit / _BITSTR_BITS); 83299090Sasomers} 841539Srgrimes 85299090Sasomers/* bit number within bitstr_t at _bit_idx(_bit). */ 86299090Sasomersstatic inline int 87299090Sasomers_bit_offset(int _bit) 88299090Sasomers{ 89299090Sasomers return (_bit % _BITSTR_BITS); 90299090Sasomers} 911539Srgrimes 92299090Sasomers/* Mask for the bit within its long. */ 93299090Sasomersstatic inline bitstr_t 94299090Sasomers_bit_mask(int _bit) 95299090Sasomers{ 96299090Sasomers return (1UL << _bit_offset(_bit)); 97299090Sasomers} 98299090Sasomers 99299090Sasomersstatic inline bitstr_t 100299090Sasomers_bit_make_mask(int _start, int _stop) 101299090Sasomers{ 102299090Sasomers return ((_BITSTR_MASK << _bit_offset(_start)) & 103299090Sasomers (_BITSTR_MASK >> (_BITSTR_BITS - _bit_offset(_stop) - 1))); 104299090Sasomers} 105299090Sasomers 106299090Sasomers/*----------------------------- Public Interface -----------------------------*/ 107299090Sasomers/* Number of bytes consumed by a bit string of nbits bits */ 108299090Sasomers#define bitstr_size(_nbits) \ 109299090Sasomers (((_nbits) + _BITSTR_BITS - 1) / 8) 110299090Sasomers 111299090Sasomers/* Allocate a bit string initialized with no bits set. */ 112299090Sasomers#ifdef _KERNEL 113299090Sasomersstatic inline bitstr_t * 114299090Sasomersbit_alloc(int _nbits, struct malloc_type *type, int flags) 115299090Sasomers{ 116299090Sasomers return ((bitstr_t *)malloc(bitstr_size(_nbits), type, flags | M_ZERO)); 117299090Sasomers} 118299090Sasomers#else 119299090Sasomersstatic inline bitstr_t * 120299090Sasomersbit_alloc(int _nbits) 121299090Sasomers{ 122299090Sasomers return ((bitstr_t *)calloc(bitstr_size(_nbits), 1)); 123299090Sasomers} 124299090Sasomers#endif 125299090Sasomers 126299090Sasomers/* Allocate a bit string on the stack with no bits set. */ 1271539Srgrimes#define bit_decl(name, nbits) \ 128299090Sasomers ((name)[bitstr_size(nbits) / sizeof(bitstr_t)]) 1291539Srgrimes 130299090Sasomers/* Is bit N of bit string set? */ 131299090Sasomersstatic inline int 132299090Sasomersbit_test(const bitstr_t *_bitstr, int _bit) 133299090Sasomers{ 134299090Sasomers return ((_bitstr[_bit_idx(_bit)] & _bit_mask(_bit)) != 0); 135299090Sasomers} 1361539Srgrimes 137299090Sasomers/* Set bit N of bit string. */ 138299090Sasomersstatic inline void 139299090Sasomersbit_set(bitstr_t *_bitstr, int _bit) 140299090Sasomers{ 141299090Sasomers _bitstr[_bit_idx(_bit)] |= _bit_mask(_bit); 142299090Sasomers} 1431539Srgrimes 144299090Sasomers/* clear bit N of bit string name */ 145299090Sasomersstatic inline void 146299090Sasomersbit_clear(bitstr_t *_bitstr, int _bit) 147299090Sasomers{ 148299090Sasomers _bitstr[_bit_idx(_bit)] &= ~_bit_mask(_bit); 149299090Sasomers} 1501539Srgrimes 151299090Sasomers/* Set bits start ... stop inclusive in bit string. */ 152299090Sasomersstatic inline void 153299090Sasomersbit_nset(bitstr_t *_bitstr, int _start, int _stop) 154299090Sasomers{ 155299090Sasomers bitstr_t *_stopbitstr; 1561539Srgrimes 157299090Sasomers _stopbitstr = _bitstr + _bit_idx(_stop); 158299090Sasomers _bitstr += _bit_idx(_start); 1591539Srgrimes 160299090Sasomers if (_bitstr == _stopbitstr) { 161299090Sasomers *_bitstr |= _bit_make_mask(_start, _stop); 162299090Sasomers } else { 163299090Sasomers *_bitstr |= _bit_make_mask(_start, _BITSTR_BITS - 1); 164299090Sasomers while (++_bitstr < _stopbitstr) 165299090Sasomers *_bitstr = _BITSTR_MASK; 166299090Sasomers *_stopbitstr |= _bit_make_mask(0, _stop); 167299090Sasomers } 168299090Sasomers} 1691539Srgrimes 170299090Sasomers/* Clear bits start ... stop inclusive in bit string. */ 171299090Sasomersstatic inline void 172299090Sasomersbit_nclear(bitstr_t *_bitstr, int _start, int _stop) 173299090Sasomers{ 174299090Sasomers bitstr_t *_stopbitstr; 1751539Srgrimes 176299090Sasomers _stopbitstr = _bitstr + _bit_idx(_stop); 177299090Sasomers _bitstr += _bit_idx(_start); 178299090Sasomers 179299090Sasomers if (_bitstr == _stopbitstr) { 180299090Sasomers *_bitstr &= ~_bit_make_mask(_start, _stop); 181299090Sasomers } else { 182299090Sasomers *_bitstr &= ~_bit_make_mask(_start, _BITSTR_BITS - 1); 183299090Sasomers while (++_bitstr < _stopbitstr) 184299090Sasomers *_bitstr = 0; 185299090Sasomers *_stopbitstr &= ~_bit_make_mask(0, _stop); 186299090Sasomers } 187299090Sasomers} 188299090Sasomers 189299090Sasomers/* Find the first bit set in bit string at or after bit start. */ 190299090Sasomersstatic inline void 191299090Sasomersbit_ffs_at(bitstr_t *_bitstr, int _start, int _nbits, int *_result) 192299090Sasomers{ 193299090Sasomers bitstr_t *_curbitstr; 194299090Sasomers bitstr_t *_stopbitstr; 195299090Sasomers bitstr_t _test; 196299090Sasomers int _value, _offset; 197299090Sasomers 198299090Sasomers if (_nbits > 0) { 199299090Sasomers _curbitstr = _bitstr + _bit_idx(_start); 200299090Sasomers _stopbitstr = _bitstr + _bit_idx(_nbits - 1); 201299090Sasomers 202299090Sasomers _test = *_curbitstr; 203299090Sasomers if (_bit_offset(_start) != 0) 204299090Sasomers _test &= _bit_make_mask(_start, _BITSTR_BITS - 1); 205299090Sasomers while (_test == 0 && _curbitstr < _stopbitstr) 206299090Sasomers _test = *(++_curbitstr); 207300539Sasomers 208299090Sasomers _offset = ffsl(_test); 209299090Sasomers _value = ((_curbitstr - _bitstr) * _BITSTR_BITS) + _offset - 1; 210299090Sasomers if (_offset == 0 || _value >= _nbits) 211299090Sasomers _value = -1; 212299090Sasomers } else { 213299090Sasomers _value = -1; 214299090Sasomers } 215299090Sasomers *_result = _value; 216299090Sasomers} 217299090Sasomers 218299090Sasomers/* Find the first bit clear in bit string at or after bit start. */ 219299090Sasomersstatic inline void 220299090Sasomersbit_ffc_at(bitstr_t *_bitstr, int _start, int _nbits, int *_result) 221299090Sasomers{ 222299090Sasomers bitstr_t *_curbitstr; 223299090Sasomers bitstr_t *_stopbitstr; 224299090Sasomers bitstr_t _test; 225299090Sasomers int _value, _offset; 226299090Sasomers 227299090Sasomers if (_nbits > 0) { 228299090Sasomers _curbitstr = _bitstr + _bit_idx(_start); 229299090Sasomers _stopbitstr = _bitstr + _bit_idx(_nbits - 1); 230299090Sasomers 231299090Sasomers _test = *_curbitstr; 232299090Sasomers if (_bit_offset(_start) != 0) 233299090Sasomers _test |= _bit_make_mask(0, _start - 1); 234299090Sasomers while (_test == _BITSTR_MASK && _curbitstr < _stopbitstr) 235299090Sasomers _test = *(++_curbitstr); 236300539Sasomers 237299090Sasomers _offset = ffsl(~_test); 238299090Sasomers _value = ((_curbitstr - _bitstr) * _BITSTR_BITS) + _offset - 1; 239299090Sasomers if (_offset == 0 || _value >= _nbits) 240299090Sasomers _value = -1; 241299090Sasomers } else { 242299090Sasomers _value = -1; 243299090Sasomers } 244299090Sasomers *_result = _value; 245299090Sasomers} 246299090Sasomers 247299090Sasomers/* Find the first bit set in bit string. */ 248299090Sasomersstatic inline void 249299090Sasomersbit_ffs(bitstr_t *_bitstr, int _nbits, int *_result) 250299090Sasomers{ 251299090Sasomers bit_ffs_at(_bitstr, /*start*/0, _nbits, _result); 252299090Sasomers} 253299090Sasomers 254299090Sasomers/* Find the first bit clear in bit string. */ 255299090Sasomersstatic inline void 256299090Sasomersbit_ffc(bitstr_t *_bitstr, int _nbits, int *_result) 257299090Sasomers{ 258299090Sasomers bit_ffc_at(_bitstr, /*start*/0, _nbits, _result); 259299090Sasomers} 260299090Sasomers 261300539Sasomers/* Count the number of bits set in a bitstr of size _nbits at or after _start */ 262300539Sasomersstatic inline void 263300539Sasomersbit_count(bitstr_t *_bitstr, int _start, int _nbits, int *_result) 264300539Sasomers{ 265300539Sasomers bitstr_t *_curbitstr, mask; 266300539Sasomers int _value = 0, curbitstr_len; 267300539Sasomers 268300539Sasomers if (_start >= _nbits) 269300539Sasomers goto out; 270300539Sasomers 271300539Sasomers _curbitstr = _bitstr + _bit_idx(_start); 272300539Sasomers _nbits -= _BITSTR_BITS * _bit_idx(_start); 273300539Sasomers _start -= _BITSTR_BITS * _bit_idx(_start); 274300539Sasomers 275300539Sasomers if (_start > 0) { 276300539Sasomers curbitstr_len = (int)_BITSTR_BITS < _nbits ? 277300539Sasomers (int)_BITSTR_BITS : _nbits; 278300539Sasomers mask = _bit_make_mask(_start, _bit_offset(curbitstr_len - 1)); 279300539Sasomers _value += __bitcountl(*_curbitstr & mask); 280300539Sasomers _curbitstr++; 281300539Sasomers _nbits -= _BITSTR_BITS; 282300539Sasomers } 283300539Sasomers while (_nbits >= (int)_BITSTR_BITS) { 284300539Sasomers _value += __bitcountl(*_curbitstr); 285300539Sasomers _curbitstr++; 286300539Sasomers _nbits -= _BITSTR_BITS; 287300539Sasomers } 288300539Sasomers if (_nbits > 0) { 289300539Sasomers mask = _bit_make_mask(0, _bit_offset(_nbits - 1)); 290300539Sasomers _value += __bitcountl(*_curbitstr & mask); 291300539Sasomers } 292300539Sasomers 293300539Sasomersout: 294300539Sasomers *_result = _value; 295300539Sasomers} 296300539Sasomers 297299090Sasomers#endif /* _SYS_BITSTRING_H_ */ 298