2 * Copyright (c) 2008, Jeffrey Roberson <jeff@freebsd.org>
5 * Copyright (c) 2008 Nokia Corporation
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice unmodified, this list of conditions, and the following
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 #ifndef _SYS_BITSET_H_
33 #define _SYS_BITSET_H_
35 #define BIT_CLR(_s, n, p) \
36 ((p)->__bits[__bitset_word(_s, n)] &= ~__bitset_mask((_s), (n)))
38 #define BIT_COPY(_s, f, t) (void)(*(t) = *(f))
40 #define BIT_ISSET(_s, n, p) \
41 ((((p)->__bits[__bitset_word(_s, n)] & __bitset_mask((_s), (n))) != 0))
43 #define BIT_SET(_s, n, p) \
44 ((p)->__bits[__bitset_word(_s, n)] |= __bitset_mask((_s), (n)))
46 #define BIT_ZERO(_s, p) do { \
48 for (__i = 0; __i < __bitset_words((_s)); __i++) \
49 (p)->__bits[__i] = 0L; \
52 #define BIT_FILL(_s, p) do { \
54 for (__i = 0; __i < __bitset_words((_s)); __i++) \
55 (p)->__bits[__i] = -1L; \
58 #define BIT_SETOF(_s, n, p) do { \
60 (p)->__bits[__bitset_word(_s, n)] = __bitset_mask((_s), (n)); \
64 #define BIT_EMPTY(_s, p) __extension__ ({ \
66 for (__i = 0; __i < __bitset_words((_s)); __i++) \
67 if ((p)->__bits[__i]) \
69 __i == __bitset_words((_s)); \
73 #define BIT_ISFULLSET(_s, p) __extension__ ({ \
75 for (__i = 0; __i < __bitset_words((_s)); __i++) \
76 if ((p)->__bits[__i] != (long)-1) \
78 __i == __bitset_words((_s)); \
81 /* Is c a subset of p. */
82 #define BIT_SUBSET(_s, p, c) __extension__ ({ \
84 for (__i = 0; __i < __bitset_words((_s)); __i++) \
85 if (((c)->__bits[__i] & \
86 (p)->__bits[__i]) != \
89 __i == __bitset_words((_s)); \
92 /* Are there any common bits between b & c? */
93 #define BIT_OVERLAP(_s, p, c) __extension__ ({ \
95 for (__i = 0; __i < __bitset_words((_s)); __i++) \
96 if (((c)->__bits[__i] & \
97 (p)->__bits[__i]) != 0) \
99 __i != __bitset_words((_s)); \
102 /* Compare two sets, returns 0 if equal 1 otherwise. */
103 #define BIT_CMP(_s, p, c) __extension__ ({ \
105 for (__i = 0; __i < __bitset_words((_s)); __i++) \
106 if (((c)->__bits[__i] != \
109 __i != __bitset_words((_s)); \
112 #define BIT_OR(_s, d, s) do { \
114 for (__i = 0; __i < __bitset_words((_s)); __i++) \
115 (d)->__bits[__i] |= (s)->__bits[__i]; \
118 #define BIT_AND(_s, d, s) do { \
120 for (__i = 0; __i < __bitset_words((_s)); __i++) \
121 (d)->__bits[__i] &= (s)->__bits[__i]; \
124 #define BIT_NAND(_s, d, s) do { \
126 for (__i = 0; __i < __bitset_words((_s)); __i++) \
127 (d)->__bits[__i] &= ~(s)->__bits[__i]; \
130 #define BIT_CLR_ATOMIC(_s, n, p) \
131 atomic_clear_long(&(p)->__bits[__bitset_word(_s, n)], \
132 __bitset_mask((_s), n))
134 #define BIT_SET_ATOMIC(_s, n, p) \
135 atomic_set_long(&(p)->__bits[__bitset_word(_s, n)], \
136 __bitset_mask((_s), n))
138 #define BIT_SET_ATOMIC_ACQ(_s, n, p) \
139 atomic_set_acq_long(&(p)->__bits[__bitset_word(_s, n)], \
140 __bitset_mask((_s), n))
142 /* Convenience functions catering special cases. */
143 #define BIT_AND_ATOMIC(_s, d, s) do { \
145 for (__i = 0; __i < __bitset_words((_s)); __i++) \
146 atomic_clear_long(&(d)->__bits[__i], \
147 ~(s)->__bits[__i]); \
150 #define BIT_OR_ATOMIC(_s, d, s) do { \
152 for (__i = 0; __i < __bitset_words((_s)); __i++) \
153 atomic_set_long(&(d)->__bits[__i], \
157 #define BIT_COPY_STORE_REL(_s, f, t) do { \
159 for (__i = 0; __i < __bitset_words((_s)); __i++) \
160 atomic_store_rel_long(&(t)->__bits[__i], \
164 #define BIT_FFS(_s, p) __extension__ ({ \
169 for (__i = 0; __i < __bitset_words((_s)); __i++) { \
170 if ((p)->__bits[__i] != 0) { \
171 __bit = ffsl((p)->__bits[__i]); \
172 __bit += __i * _BITSET_BITS; \
179 #define BIT_COUNT(_s, p) __extension__ ({ \
184 for (__i = 0; __i < __bitset_words((_s)); __i++) \
185 __count += __bitcountl((p)->__bits[__i]); \
189 #endif /* !_SYS_BITSET_H_ */