1 /* ISC license. */
2 
3 #ifndef BITARRAY_H
4 #define BITARRAY_H
5 
6 #include <string.h>
7 #include <skalibs/gccattributes.h>
8 
9 #define bitarray_div8(n) ((n) ? 1U + (((n) - 1) >> 3) : 0U)
10 
11 extern void bitarray_clearsetn (unsigned char *, size_t, size_t, int) ;
12 #define bitarray_clearn(s, start, len) bitarray_clearsetn(s, start, (len), 0)
13 #define bitarray_setn(s, start, len)   bitarray_clearsetn(s, start, (len), 1)
14 
15 #define bitarray_peek(s, n) (((s)[(n)>>3] & (1U<<((n)&7))) ? 1 : 0)
16 #define bitarray_isset(b, n) bitarray_peek(b, n)
17 #define bitarray_clear(s, n) ((s)[(n)>>3] &= ~(1U << ((n) & 7)))
18 #define bitarray_set(s, n) ((s)[(n)>>3] |= 1U << ((n) & 7))
19 #define bitarray_poke(s, n, h) ((h) ? bitarray_set(s, n) : bitarray_clear(s, n))
20 
21 extern int bitarray_testandpoke (unsigned char *, size_t, int) ;
22 #define bitarray_testandclear(b, n) bitarray_testandpoke(b, n, 0)
23 #define bitarray_testandset(b, n)   bitarray_testandpoke(b, n, 1)
24 
25 extern size_t bitarray_firstclear (unsigned char const *, size_t) gccattr_pure ;
26 extern size_t bitarray_firstset (unsigned char const *, size_t) gccattr_pure ;
27 #define bitarray_first(s, n, h) ((h) ? bitarray_firstset(s, n) : bitarray_firstclear(s, n))
28 
29 extern size_t bitarray_firstclear_skip (unsigned char const *, size_t, size_t) gccattr_pure ;
30 extern size_t bitarray_firstset_skip (unsigned char const *, size_t, size_t) gccattr_pure ;
31 #define bitarray_first_skip(s, n, k, h) ((h) ? bitarray_firstset_skip(s, n, k) : bitarray_firstclear_skip(s, n, k))
32 
33 extern size_t bitarray_countones (unsigned char const *, size_t) gccattr_pure ;
34 
35 extern void bitarray_not (unsigned char *, size_t, size_t) ;
36 extern void bitarray_and (unsigned char *, unsigned char const *, unsigned char const *, size_t) ;
37 extern void bitarray_or (unsigned char *, unsigned char const *, unsigned char const *, size_t) ;
38 extern void bitarray_xor (unsigned char *, unsigned char const *, unsigned char const *, size_t) ;
39 
40 #endif
41