xref: /illumos-gate/usr/src/uts/common/sys/bitmap.h (revision badf94ff)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
52b616c6cSwesolows  * Common Development and Distribution License (the "License").
62b616c6cSwesolows  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
212b616c6cSwesolows 
227c478bd9Sstevel@tonic-gate /*
239acbbeafSnn  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
247c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate  */
267c478bd9Sstevel@tonic-gate 
27bf16b11eSMatthew Ahrens /*
28bf16b11eSMatthew Ahrens  * Copyright (c) 2014 by Delphix. All rights reserved.
29811599a4SMatt Barden  * Copyright 2015 Nexenta Systems, Inc.  All rights reserved.
30f06dce2cSAndrew Stormont  * Copyright 2017 RackTop Systems.
31*badf94ffSPatrick Mooney  * Copyright 2022 Oxide Computer Company
32bf16b11eSMatthew Ahrens  */
33bf16b11eSMatthew Ahrens 
347c478bd9Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
35b4203d75SMarcel Telka /*	  All Rights Reserved	*/
367c478bd9Sstevel@tonic-gate 
377c478bd9Sstevel@tonic-gate 
387c478bd9Sstevel@tonic-gate #ifndef _SYS_BITMAP_H
397c478bd9Sstevel@tonic-gate #define	_SYS_BITMAP_H
407c478bd9Sstevel@tonic-gate 
417c478bd9Sstevel@tonic-gate #ifdef	__cplusplus
427c478bd9Sstevel@tonic-gate extern "C" {
437c478bd9Sstevel@tonic-gate #endif
447c478bd9Sstevel@tonic-gate 
457c478bd9Sstevel@tonic-gate #include <sys/feature_tests.h>
462b616c6cSwesolows #if defined(__GNUC__) && defined(_ASM_INLINES) && \
472b616c6cSwesolows 	(defined(__i386) || defined(__amd64))
487c478bd9Sstevel@tonic-gate #include <asm/bitmap.h>
497c478bd9Sstevel@tonic-gate #endif
507c478bd9Sstevel@tonic-gate 
517c478bd9Sstevel@tonic-gate /*
527c478bd9Sstevel@tonic-gate  * Operations on bitmaps of arbitrary size
537c478bd9Sstevel@tonic-gate  * A bitmap is a vector of 1 or more ulong_t's.
547c478bd9Sstevel@tonic-gate  * The user of the package is responsible for range checks and keeping
557c478bd9Sstevel@tonic-gate  * track of sizes.
567c478bd9Sstevel@tonic-gate  */
577c478bd9Sstevel@tonic-gate 
587c478bd9Sstevel@tonic-gate #ifdef _LP64
597c478bd9Sstevel@tonic-gate #define	BT_ULSHIFT	6 /* log base 2 of BT_NBIPUL, to extract word index */
607c478bd9Sstevel@tonic-gate #define	BT_ULSHIFT32	5 /* log base 2 of BT_NBIPUL, to extract word index */
617c478bd9Sstevel@tonic-gate #else
627c478bd9Sstevel@tonic-gate #define	BT_ULSHIFT	5 /* log base 2 of BT_NBIPUL, to extract word index */
637c478bd9Sstevel@tonic-gate #endif
647c478bd9Sstevel@tonic-gate 
657c478bd9Sstevel@tonic-gate #define	BT_NBIPUL	(1 << BT_ULSHIFT)	/* n bits per ulong_t */
667c478bd9Sstevel@tonic-gate #define	BT_ULMASK	(BT_NBIPUL - 1)		/* to extract bit index */
677c478bd9Sstevel@tonic-gate 
687c478bd9Sstevel@tonic-gate #ifdef _LP64
697c478bd9Sstevel@tonic-gate #define	BT_NBIPUL32	(1 << BT_ULSHIFT32)	/* n bits per ulong_t */
707c478bd9Sstevel@tonic-gate #define	BT_ULMASK32	(BT_NBIPUL32 - 1)	/* to extract bit index */
717c478bd9Sstevel@tonic-gate #define	BT_ULMAXMASK	0xffffffffffffffff	/* used by bt_getlowbit */
727c478bd9Sstevel@tonic-gate #else
737c478bd9Sstevel@tonic-gate #define	BT_ULMAXMASK	0xffffffff
747c478bd9Sstevel@tonic-gate #endif
757c478bd9Sstevel@tonic-gate 
767c478bd9Sstevel@tonic-gate /*
777c478bd9Sstevel@tonic-gate  * bitmap is a ulong_t *, bitindex an index_t
787c478bd9Sstevel@tonic-gate  *
797c478bd9Sstevel@tonic-gate  * The macros BT_WIM and BT_BIW internal; there is no need
807c478bd9Sstevel@tonic-gate  * for users of this package to use them.
817c478bd9Sstevel@tonic-gate  */
827c478bd9Sstevel@tonic-gate 
837c478bd9Sstevel@tonic-gate /*
847c478bd9Sstevel@tonic-gate  * word in map
857c478bd9Sstevel@tonic-gate  */
867c478bd9Sstevel@tonic-gate #define	BT_WIM(bitmap, bitindex) \
877c478bd9Sstevel@tonic-gate 	((bitmap)[(bitindex) >> BT_ULSHIFT])
887c478bd9Sstevel@tonic-gate /*
897c478bd9Sstevel@tonic-gate  * bit in word
907c478bd9Sstevel@tonic-gate  */
917c478bd9Sstevel@tonic-gate #define	BT_BIW(bitindex) \
927c478bd9Sstevel@tonic-gate 	(1UL << ((bitindex) & BT_ULMASK))
937c478bd9Sstevel@tonic-gate 
947c478bd9Sstevel@tonic-gate #ifdef _LP64
957c478bd9Sstevel@tonic-gate #define	BT_WIM32(bitmap, bitindex) \
967c478bd9Sstevel@tonic-gate 	((bitmap)[(bitindex) >> BT_ULSHIFT32])
977c478bd9Sstevel@tonic-gate 
987c478bd9Sstevel@tonic-gate #define	BT_BIW32(bitindex) \
997c478bd9Sstevel@tonic-gate 	(1UL << ((bitindex) & BT_ULMASK32))
1007c478bd9Sstevel@tonic-gate #endif
1017c478bd9Sstevel@tonic-gate 
1027c478bd9Sstevel@tonic-gate /*
1037c478bd9Sstevel@tonic-gate  * These are public macros
1047c478bd9Sstevel@tonic-gate  *
1057c478bd9Sstevel@tonic-gate  * BT_BITOUL == n bits to n ulong_t's
1067c478bd9Sstevel@tonic-gate  */
1077c478bd9Sstevel@tonic-gate #define	BT_BITOUL(nbits) \
1087c478bd9Sstevel@tonic-gate 	(((nbits) + BT_NBIPUL - 1l) / BT_NBIPUL)
1097c478bd9Sstevel@tonic-gate #define	BT_SIZEOFMAP(nbits) \
1107c478bd9Sstevel@tonic-gate 	(BT_BITOUL(nbits) * sizeof (ulong_t))
1117c478bd9Sstevel@tonic-gate #define	BT_TEST(bitmap, bitindex) \
1127c478bd9Sstevel@tonic-gate 	((BT_WIM((bitmap), (bitindex)) & BT_BIW(bitindex)) ? 1 : 0)
1137c478bd9Sstevel@tonic-gate #define	BT_SET(bitmap, bitindex) \
1147c478bd9Sstevel@tonic-gate 	{ BT_WIM((bitmap), (bitindex)) |= BT_BIW(bitindex); }
1157c478bd9Sstevel@tonic-gate #define	BT_CLEAR(bitmap, bitindex) \
1167c478bd9Sstevel@tonic-gate 	{ BT_WIM((bitmap), (bitindex)) &= ~BT_BIW(bitindex); }
1177c478bd9Sstevel@tonic-gate 
1187c478bd9Sstevel@tonic-gate #ifdef _LP64
1197c478bd9Sstevel@tonic-gate #define	BT_BITOUL32(nbits) \
1207c478bd9Sstevel@tonic-gate 	(((nbits) + BT_NBIPUL32 - 1l) / BT_NBIPUL32)
1217c478bd9Sstevel@tonic-gate #define	BT_SIZEOFMAP32(nbits) \
1227c478bd9Sstevel@tonic-gate 	(BT_BITOUL32(nbits) * sizeof (uint_t))
1237c478bd9Sstevel@tonic-gate #define	BT_TEST32(bitmap, bitindex) \
1247c478bd9Sstevel@tonic-gate 	((BT_WIM32((bitmap), (bitindex)) & BT_BIW32(bitindex)) ? 1 : 0)
1257c478bd9Sstevel@tonic-gate #define	BT_SET32(bitmap, bitindex) \
1267c478bd9Sstevel@tonic-gate 	{ BT_WIM32((bitmap), (bitindex)) |= BT_BIW32(bitindex); }
1277c478bd9Sstevel@tonic-gate #define	BT_CLEAR32(bitmap, bitindex) \
1287c478bd9Sstevel@tonic-gate 	{ BT_WIM32((bitmap), (bitindex)) &= ~BT_BIW32(bitindex); }
1297c478bd9Sstevel@tonic-gate #endif /* _LP64 */
1307c478bd9Sstevel@tonic-gate 
1317c478bd9Sstevel@tonic-gate 
1329acbbeafSnn /*
1339acbbeafSnn  * BIT_ONLYONESET is a private macro not designed for bitmaps of
1349acbbeafSnn  * arbitrary size.  u must be an unsigned integer/long.  It returns
1359acbbeafSnn  * true if one and only one bit is set in u.
1369acbbeafSnn  */
1379acbbeafSnn #define	BIT_ONLYONESET(u) \
1389acbbeafSnn 	((((u) == 0) ? 0 : ((u) & ((u) - 1)) == 0))
1399acbbeafSnn 
140f06dce2cSAndrew Stormont #if (defined(_KERNEL) || defined(_FAKE_KERNEL)) && !defined(_ASM)
1417c478bd9Sstevel@tonic-gate #include <sys/atomic.h>
1427c478bd9Sstevel@tonic-gate 
1437c478bd9Sstevel@tonic-gate /*
1447c478bd9Sstevel@tonic-gate  * return next available bit index from map with specified number of bits
1457c478bd9Sstevel@tonic-gate  */
146*badf94ffSPatrick Mooney extern index_t	bt_availbit(const ulong_t *, size_t);
1477c478bd9Sstevel@tonic-gate /*
1487c478bd9Sstevel@tonic-gate  * find the highest order bit that is on, and is within or below
1497c478bd9Sstevel@tonic-gate  * the word specified by wx
1507c478bd9Sstevel@tonic-gate  */
151*badf94ffSPatrick Mooney extern int	bt_gethighbit(const ulong_t *, int wx);
152*badf94ffSPatrick Mooney extern int	bt_range(const ulong_t *, size_t *, size_t *, size_t);
1537c478bd9Sstevel@tonic-gate /*
1547c478bd9Sstevel@tonic-gate  * Find highest and lowest one bit set.
1557c478bd9Sstevel@tonic-gate  *	Returns bit number + 1 of bit that is set, otherwise returns 0.
1567c478bd9Sstevel@tonic-gate  * Low order bit is 0, high order bit is 31.
1577c478bd9Sstevel@tonic-gate  */
1587c478bd9Sstevel@tonic-gate extern int	highbit(ulong_t);
159bf16b11eSMatthew Ahrens extern int	highbit64(uint64_t);
1607c478bd9Sstevel@tonic-gate extern int	lowbit(ulong_t);
161*badf94ffSPatrick Mooney extern int	bt_getlowbit(const ulong_t *, size_t, size_t);
162*badf94ffSPatrick Mooney extern void	bt_copy(const ulong_t *, ulong_t *, ulong_t);
1637c478bd9Sstevel@tonic-gate 
1647c478bd9Sstevel@tonic-gate /*
1657c478bd9Sstevel@tonic-gate  * find the parity
1667c478bd9Sstevel@tonic-gate  */
1677c478bd9Sstevel@tonic-gate extern int	odd_parity(ulong_t);
1687c478bd9Sstevel@tonic-gate 
1697c478bd9Sstevel@tonic-gate /*
1707c478bd9Sstevel@tonic-gate  * Atomically set/clear bits
1717c478bd9Sstevel@tonic-gate  * Atomic exclusive operations will set "result" to "-1"
1727c478bd9Sstevel@tonic-gate  * if the bit is already set/cleared. "result" will be set
1737c478bd9Sstevel@tonic-gate  * to 0 otherwise.
1747c478bd9Sstevel@tonic-gate  */
1757c478bd9Sstevel@tonic-gate #define	BT_ATOMIC_SET(bitmap, bitindex) \
17675d94465SJosef 'Jeff' Sipek 	{ atomic_or_ulong(&(BT_WIM(bitmap, bitindex)), BT_BIW(bitindex)); }
1777c478bd9Sstevel@tonic-gate #define	BT_ATOMIC_CLEAR(bitmap, bitindex) \
17875d94465SJosef 'Jeff' Sipek 	{ atomic_and_ulong(&(BT_WIM(bitmap, bitindex)), ~BT_BIW(bitindex)); }
1797c478bd9Sstevel@tonic-gate 
1807c478bd9Sstevel@tonic-gate #define	BT_ATOMIC_SET_EXCL(bitmap, bitindex, result) \
1817c478bd9Sstevel@tonic-gate 	{ result = atomic_set_long_excl(&(BT_WIM(bitmap, bitindex)),	\
1827c478bd9Sstevel@tonic-gate 	    (bitindex) % BT_NBIPUL); }
1837c478bd9Sstevel@tonic-gate #define	BT_ATOMIC_CLEAR_EXCL(bitmap, bitindex, result) \
1847c478bd9Sstevel@tonic-gate 	{ result = atomic_clear_long_excl(&(BT_WIM(bitmap, bitindex)),	\
1857c478bd9Sstevel@tonic-gate 	    (bitindex) % BT_NBIPUL); }
1867c478bd9Sstevel@tonic-gate 
1877c478bd9Sstevel@tonic-gate /*
1887c478bd9Sstevel@tonic-gate  * Extracts bits between index h (high, inclusive) and l (low, exclusive) from
1897c478bd9Sstevel@tonic-gate  * u, which must be an unsigned integer.
1907c478bd9Sstevel@tonic-gate  */
1917c478bd9Sstevel@tonic-gate #define	BITX(u, h, l)	(((u) >> (l)) & ((1LU << ((h) - (l) + 1LU)) - 1LU))
1927c478bd9Sstevel@tonic-gate 
193f06dce2cSAndrew Stormont #endif	/* (_KERNEL || _FAKE_KERNEL) && !_ASM */
1947c478bd9Sstevel@tonic-gate 
1957c478bd9Sstevel@tonic-gate #ifdef	__cplusplus
1967c478bd9Sstevel@tonic-gate }
1977c478bd9Sstevel@tonic-gate #endif
1987c478bd9Sstevel@tonic-gate 
1997c478bd9Sstevel@tonic-gate #endif	/* _SYS_BITMAP_H */
200