1*e886a842Srillig /* $NetBSD: bitstring.h,v 1.15 2024/05/12 10:41:23 rillig Exp $ */ 24d2cbfceScgd 361f28255Scgd /* 4b7b7322cSperry * Copyright (c) 1989, 1993 5b7b7322cSperry * The Regents of the University of California. All rights reserved. 661f28255Scgd * 761f28255Scgd * This code is derived from software contributed to Berkeley by 861f28255Scgd * Paul Vixie. 961f28255Scgd * 10b7b7322cSperry * Redistribution and use in source and binary forms, with or without 11b7b7322cSperry * modification, are permitted provided that the following conditions 12b7b7322cSperry * are met: 13b7b7322cSperry * 1. Redistributions of source code must retain the above copyright 14b7b7322cSperry * notice, this list of conditions and the following disclaimer. 15b7b7322cSperry * 2. Redistributions in binary form must reproduce the above copyright 16b7b7322cSperry * notice, this list of conditions and the following disclaimer in the 17b7b7322cSperry * documentation and/or other materials provided with the distribution. 18039cc956Sagc * 3. Neither the name of the University nor the names of its contributors 19b7b7322cSperry * may be used to endorse or promote products derived from this software 20b7b7322cSperry * without specific prior written permission. 2161f28255Scgd * 22b7b7322cSperry * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 23b7b7322cSperry * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 24b7b7322cSperry * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 25b7b7322cSperry * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 26b7b7322cSperry * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 27b7b7322cSperry * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 28b7b7322cSperry * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 29b7b7322cSperry * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 30b7b7322cSperry * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 31b7b7322cSperry * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 32b7b7322cSperry * SUCH DAMAGE. 33b7b7322cSperry * 34b7b7322cSperry * @(#)bitstring.h 8.1 (Berkeley) 7/19/93 3561f28255Scgd */ 3661f28255Scgd 3761f28255Scgd #ifndef _BITSTRING_H_ 3861f28255Scgd #define _BITSTRING_H_ 3961f28255Scgd 405e82abdeScgd /* modified for SV/AT and bitstring bugfix by M.R.Murphy, 11oct91 415e82abdeScgd * bitstr_size changed gratuitously, but shorter 425e82abdeScgd * bit_alloc spelling error fixed 435e82abdeScgd * the following were efficient, but didn't work, they've been made to 445e82abdeScgd * work, but are no longer as efficient :-) 455e82abdeScgd * bit_nclear, bit_nset, bit_ffc, bit_ffs 465e82abdeScgd */ 47b7b7322cSperry /* 48b7b7322cSperry * The comment above may or may not bear any resemblance to reality. 49b7b7322cSperry * This code has been maintained in a confusing way, with little 50b7b7322cSperry * information available on the provenance of much of it. "At least it 51b7b7322cSperry * works." 52b7b7322cSperry * /s/ Perry E. Metzger, 2 Feb 98 53b7b7322cSperry */ 5461f28255Scgd typedef unsigned char bitstr_t; 5561f28255Scgd 5661f28255Scgd /* internal macros */ 5761f28255Scgd /* byte of the bitstring bit is in */ 5861f28255Scgd #define _bit_byte(bit) \ 59b04439f4Schristos (uint32_t)((bit) >> 3) 6061f28255Scgd 6161f28255Scgd /* mask for the bit within its byte */ 6261f28255Scgd #define _bit_mask(bit) \ 63b04439f4Schristos (uint32_t)((1 << (uint32_t)((bit)&0x7))) 6461f28255Scgd 6561f28255Scgd /* external macros */ 6661f28255Scgd /* bytes in a bitstring of nbits bits */ 6761f28255Scgd #define bitstr_size(nbits) \ 68b04439f4Schristos (size_t)((uint32_t)((nbits) + 7) >> 3) 6961f28255Scgd 7061f28255Scgd /* allocate a bitstring */ 7161f28255Scgd #define bit_alloc(nbits) \ 72b04439f4Schristos calloc(bitstr_size(nbits), sizeof(bitstr_t)) 7361f28255Scgd 7461f28255Scgd /* allocate a bitstring on the stack */ 7561f28255Scgd #define bit_decl(name, nbits) \ 760df98a62Spk ((name)[bitstr_size(nbits)]) 7761f28255Scgd 7861f28255Scgd /* is bit N of bitstring name set? */ 7961f28255Scgd #define bit_test(name, bit) \ 80b04439f4Schristos /*LINTED bitwise on signed*/((name)[_bit_byte(bit)] & _bit_mask(bit)) 8161f28255Scgd 8261f28255Scgd /* set bit N of bitstring name */ 8361f28255Scgd #define bit_set(name, bit) \ 8466e7b261Schristos /*LINTED bitwise on signed*/ \ 858aeecea3Schristos ((name)[_bit_byte(bit)] = \ 868aeecea3Schristos (unsigned char)(_bit_mask(bit) | (name)[_bit_byte(bit)])) 8761f28255Scgd 8861f28255Scgd /* clear bit N of bitstring name */ 8961f28255Scgd #define bit_clear(name, bit) \ 908aeecea3Schristos /*LINTED bitwise on signed*/ \ 918aeecea3Schristos ((name)[_bit_byte(bit)] &= (unsigned char)~_bit_mask(bit)) 9261f28255Scgd 9361f28255Scgd /* clear bits start ... stop in bitstring */ 940df98a62Spk #define bit_nclear(name, start, stop) do { \ 95ee5442c7Sperry bitstr_t *_name = name; \ 96a5c147c8Schristos size_t _start = start, _stop = stop; \ 975e82abdeScgd while (_start <= _stop) { \ 985e82abdeScgd bit_clear(_name, _start); \ 995e82abdeScgd _start++; \ 1005e82abdeScgd } \ 101*e886a842Srillig } while (0) 10261f28255Scgd 10361f28255Scgd /* set bits start ... stop in bitstring */ 1040df98a62Spk #define bit_nset(name, start, stop) do { \ 105ee5442c7Sperry bitstr_t *_name = name; \ 106a5c147c8Schristos size_t _start = start, _stop = stop; \ 1075e82abdeScgd while (_start <= _stop) { \ 1085e82abdeScgd bit_set(_name, _start); \ 1095e82abdeScgd _start++; \ 1105e82abdeScgd } \ 111*e886a842Srillig } while (0) 11261f28255Scgd 11361f28255Scgd /* find first bit clear in name */ 1140df98a62Spk #define bit_ffc(name, nbits, value) do { \ 1153a9f77c2Schristos const bitstr_t *_name = name; \ 116d4321930Schristos size_t _bit, _nbits = nbits; \ 117d4321930Schristos int _value = -1; \ 1185e82abdeScgd for (_bit = 0; _bit < _nbits; ++_bit) \ 1195e82abdeScgd if (!bit_test(_name, _bit)) { \ 1205e82abdeScgd _value = _bit; \ 12161f28255Scgd break; \ 12261f28255Scgd } \ 12361f28255Scgd *(value) = _value; \ 124*e886a842Srillig } while (0) 12561f28255Scgd 12661f28255Scgd /* find first bit set in name */ 1270df98a62Spk #define bit_ffs(name, nbits, value) do { \ 1283a9f77c2Schristos const bitstr_t *_name = name; \ 129d4321930Schristos size_t _bit, _nbits = nbits; \ 130d4321930Schristos int _value = -1; \ 1315e82abdeScgd for (_bit = 0; _bit < _nbits; ++_bit) \ 1325e82abdeScgd if (bit_test(_name, _bit)) { \ 1335e82abdeScgd _value = _bit; \ 13461f28255Scgd break; \ 13561f28255Scgd } \ 13661f28255Scgd *(value) = _value; \ 137*e886a842Srillig } while (0) 13861f28255Scgd 13961f28255Scgd #endif /* !_BITSTRING_H_ */ 140