xref: /illumos-gate/usr/src/uts/common/sys/bitmap.h (revision badf94ff3599fab15963f6c532929e9bc411757a)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 
22 /*
23  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 /*
28  * Copyright (c) 2014 by Delphix. All rights reserved.
29  * Copyright 2015 Nexenta Systems, Inc.  All rights reserved.
30  * Copyright 2017 RackTop Systems.
31  * Copyright 2022 Oxide Computer Company
32  */
33 
34 /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
35 /*	  All Rights Reserved	*/
36 
37 
38 #ifndef _SYS_BITMAP_H
39 #define	_SYS_BITMAP_H
40 
41 #ifdef	__cplusplus
42 extern "C" {
43 #endif
44 
45 #include <sys/feature_tests.h>
46 #if defined(__GNUC__) && defined(_ASM_INLINES) && \
47 	(defined(__i386) || defined(__amd64))
48 #include <asm/bitmap.h>
49 #endif
50 
51 /*
52  * Operations on bitmaps of arbitrary size
53  * A bitmap is a vector of 1 or more ulong_t's.
54  * The user of the package is responsible for range checks and keeping
55  * track of sizes.
56  */
57 
58 #ifdef _LP64
59 #define	BT_ULSHIFT	6 /* log base 2 of BT_NBIPUL, to extract word index */
60 #define	BT_ULSHIFT32	5 /* log base 2 of BT_NBIPUL, to extract word index */
61 #else
62 #define	BT_ULSHIFT	5 /* log base 2 of BT_NBIPUL, to extract word index */
63 #endif
64 
65 #define	BT_NBIPUL	(1 << BT_ULSHIFT)	/* n bits per ulong_t */
66 #define	BT_ULMASK	(BT_NBIPUL - 1)		/* to extract bit index */
67 
68 #ifdef _LP64
69 #define	BT_NBIPUL32	(1 << BT_ULSHIFT32)	/* n bits per ulong_t */
70 #define	BT_ULMASK32	(BT_NBIPUL32 - 1)	/* to extract bit index */
71 #define	BT_ULMAXMASK	0xffffffffffffffff	/* used by bt_getlowbit */
72 #else
73 #define	BT_ULMAXMASK	0xffffffff
74 #endif
75 
76 /*
77  * bitmap is a ulong_t *, bitindex an index_t
78  *
79  * The macros BT_WIM and BT_BIW internal; there is no need
80  * for users of this package to use them.
81  */
82 
83 /*
84  * word in map
85  */
86 #define	BT_WIM(bitmap, bitindex) \
87 	((bitmap)[(bitindex) >> BT_ULSHIFT])
88 /*
89  * bit in word
90  */
91 #define	BT_BIW(bitindex) \
92 	(1UL << ((bitindex) & BT_ULMASK))
93 
94 #ifdef _LP64
95 #define	BT_WIM32(bitmap, bitindex) \
96 	((bitmap)[(bitindex) >> BT_ULSHIFT32])
97 
98 #define	BT_BIW32(bitindex) \
99 	(1UL << ((bitindex) & BT_ULMASK32))
100 #endif
101 
102 /*
103  * These are public macros
104  *
105  * BT_BITOUL == n bits to n ulong_t's
106  */
107 #define	BT_BITOUL(nbits) \
108 	(((nbits) + BT_NBIPUL - 1l) / BT_NBIPUL)
109 #define	BT_SIZEOFMAP(nbits) \
110 	(BT_BITOUL(nbits) * sizeof (ulong_t))
111 #define	BT_TEST(bitmap, bitindex) \
112 	((BT_WIM((bitmap), (bitindex)) & BT_BIW(bitindex)) ? 1 : 0)
113 #define	BT_SET(bitmap, bitindex) \
114 	{ BT_WIM((bitmap), (bitindex)) |= BT_BIW(bitindex); }
115 #define	BT_CLEAR(bitmap, bitindex) \
116 	{ BT_WIM((bitmap), (bitindex)) &= ~BT_BIW(bitindex); }
117 
118 #ifdef _LP64
119 #define	BT_BITOUL32(nbits) \
120 	(((nbits) + BT_NBIPUL32 - 1l) / BT_NBIPUL32)
121 #define	BT_SIZEOFMAP32(nbits) \
122 	(BT_BITOUL32(nbits) * sizeof (uint_t))
123 #define	BT_TEST32(bitmap, bitindex) \
124 	((BT_WIM32((bitmap), (bitindex)) & BT_BIW32(bitindex)) ? 1 : 0)
125 #define	BT_SET32(bitmap, bitindex) \
126 	{ BT_WIM32((bitmap), (bitindex)) |= BT_BIW32(bitindex); }
127 #define	BT_CLEAR32(bitmap, bitindex) \
128 	{ BT_WIM32((bitmap), (bitindex)) &= ~BT_BIW32(bitindex); }
129 #endif /* _LP64 */
130 
131 
132 /*
133  * BIT_ONLYONESET is a private macro not designed for bitmaps of
134  * arbitrary size.  u must be an unsigned integer/long.  It returns
135  * true if one and only one bit is set in u.
136  */
137 #define	BIT_ONLYONESET(u) \
138 	((((u) == 0) ? 0 : ((u) & ((u) - 1)) == 0))
139 
140 #if (defined(_KERNEL) || defined(_FAKE_KERNEL)) && !defined(_ASM)
141 #include <sys/atomic.h>
142 
143 /*
144  * return next available bit index from map with specified number of bits
145  */
146 extern index_t	bt_availbit(const ulong_t *, size_t);
147 /*
148  * find the highest order bit that is on, and is within or below
149  * the word specified by wx
150  */
151 extern int	bt_gethighbit(const ulong_t *, int wx);
152 extern int	bt_range(const ulong_t *, size_t *, size_t *, size_t);
153 /*
154  * Find highest and lowest one bit set.
155  *	Returns bit number + 1 of bit that is set, otherwise returns 0.
156  * Low order bit is 0, high order bit is 31.
157  */
158 extern int	highbit(ulong_t);
159 extern int	highbit64(uint64_t);
160 extern int	lowbit(ulong_t);
161 extern int	bt_getlowbit(const ulong_t *, size_t, size_t);
162 extern void	bt_copy(const ulong_t *, ulong_t *, ulong_t);
163 
164 /*
165  * find the parity
166  */
167 extern int	odd_parity(ulong_t);
168 
169 /*
170  * Atomically set/clear bits
171  * Atomic exclusive operations will set "result" to "-1"
172  * if the bit is already set/cleared. "result" will be set
173  * to 0 otherwise.
174  */
175 #define	BT_ATOMIC_SET(bitmap, bitindex) \
176 	{ atomic_or_ulong(&(BT_WIM(bitmap, bitindex)), BT_BIW(bitindex)); }
177 #define	BT_ATOMIC_CLEAR(bitmap, bitindex) \
178 	{ atomic_and_ulong(&(BT_WIM(bitmap, bitindex)), ~BT_BIW(bitindex)); }
179 
180 #define	BT_ATOMIC_SET_EXCL(bitmap, bitindex, result) \
181 	{ result = atomic_set_long_excl(&(BT_WIM(bitmap, bitindex)),	\
182 	    (bitindex) % BT_NBIPUL); }
183 #define	BT_ATOMIC_CLEAR_EXCL(bitmap, bitindex, result) \
184 	{ result = atomic_clear_long_excl(&(BT_WIM(bitmap, bitindex)),	\
185 	    (bitindex) % BT_NBIPUL); }
186 
187 /*
188  * Extracts bits between index h (high, inclusive) and l (low, exclusive) from
189  * u, which must be an unsigned integer.
190  */
191 #define	BITX(u, h, l)	(((u) >> (l)) & ((1LU << ((h) - (l) + 1LU)) - 1LU))
192 
193 #endif	/* (_KERNEL || _FAKE_KERNEL) && !_ASM */
194 
195 #ifdef	__cplusplus
196 }
197 #endif
198 
199 #endif	/* _SYS_BITMAP_H */
200