xref: /f-stack/freebsd/sys/_bitset.h (revision 22ce4aff)
1a9643ea8Slogwang /*-
2*22ce4affSfengbojiang  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3*22ce4affSfengbojiang  *
4a9643ea8Slogwang  * Copyright (c) 2008, Jeffrey Roberson <[email protected]>
5a9643ea8Slogwang  * All rights reserved.
6a9643ea8Slogwang  *
7a9643ea8Slogwang  * Copyright (c) 2008 Nokia Corporation
8a9643ea8Slogwang  * All rights reserved.
9a9643ea8Slogwang  *
10a9643ea8Slogwang  * Redistribution and use in source and binary forms, with or without
11a9643ea8Slogwang  * modification, are permitted provided that the following conditions
12a9643ea8Slogwang  * are met:
13a9643ea8Slogwang  * 1. Redistributions of source code must retain the above copyright
14a9643ea8Slogwang  *    notice unmodified, this list of conditions, and the following
15a9643ea8Slogwang  *    disclaimer.
16a9643ea8Slogwang  * 2. Redistributions in binary form must reproduce the above copyright
17a9643ea8Slogwang  *    notice, this list of conditions and the following disclaimer in the
18a9643ea8Slogwang  *    documentation and/or other materials provided with the distribution.
19a9643ea8Slogwang  *
20a9643ea8Slogwang  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
21a9643ea8Slogwang  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
22a9643ea8Slogwang  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
23a9643ea8Slogwang  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
24a9643ea8Slogwang  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
25a9643ea8Slogwang  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26a9643ea8Slogwang  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27a9643ea8Slogwang  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28a9643ea8Slogwang  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
29a9643ea8Slogwang  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30a9643ea8Slogwang  *
31a9643ea8Slogwang  * $FreeBSD$
32a9643ea8Slogwang  */
33a9643ea8Slogwang 
34a9643ea8Slogwang #ifndef _SYS__BITSET_H_
35a9643ea8Slogwang #define	_SYS__BITSET_H_
36a9643ea8Slogwang 
37a9643ea8Slogwang /*
38a9643ea8Slogwang  * Macros addressing word and bit within it, tuned to make compiler
39a9643ea8Slogwang  * optimize cases when SETSIZE fits into single machine word.
40a9643ea8Slogwang  */
41a9643ea8Slogwang #define	_BITSET_BITS		(sizeof(long) * 8)
42a9643ea8Slogwang 
43a9643ea8Slogwang #define	__howmany(x, y)	(((x) + ((y) - 1)) / (y))
44a9643ea8Slogwang 
45a9643ea8Slogwang #define	__bitset_words(_s)	(__howmany(_s, _BITSET_BITS))
46a9643ea8Slogwang 
47a9643ea8Slogwang #define	BITSET_DEFINE(t, _s)						\
48a9643ea8Slogwang struct t {								\
49a9643ea8Slogwang         long    __bits[__bitset_words((_s))];				\
50a9643ea8Slogwang }
51a9643ea8Slogwang 
52a9643ea8Slogwang /*
53a9643ea8Slogwang  * Helper to declare a bitset without it's size being a constant.
54a9643ea8Slogwang  *
55a9643ea8Slogwang  * Sadly we cannot declare a bitset struct with '__bits[]', because it's
56a9643ea8Slogwang  * the only member of the struct and the compiler complains.
57a9643ea8Slogwang  */
58a9643ea8Slogwang #define BITSET_DEFINE_VAR(t)	BITSET_DEFINE(t, 1)
59a9643ea8Slogwang 
60*22ce4affSfengbojiang /*
61*22ce4affSfengbojiang  * Define a default type that can be used while manually specifying size
62*22ce4affSfengbojiang  * to every call.
63*22ce4affSfengbojiang  */
64*22ce4affSfengbojiang BITSET_DEFINE(bitset, 1);
65*22ce4affSfengbojiang 
66a9643ea8Slogwang #endif /* !_SYS__BITSET_H_ */
67