1250395Sattilio/*-
2250395Sattilio * Copyright (c) 2008, Jeffrey Roberson <jeff@freebsd.org>
3250395Sattilio * All rights reserved.
4250395Sattilio *
5250395Sattilio * Copyright (c) 2008 Nokia Corporation
6250395Sattilio * All rights reserved.
7250395Sattilio *
8250395Sattilio * Redistribution and use in source and binary forms, with or without
9250395Sattilio * modification, are permitted provided that the following conditions
10250395Sattilio * are met:
11250395Sattilio * 1. Redistributions of source code must retain the above copyright
12250395Sattilio *    notice unmodified, this list of conditions, and the following
13250395Sattilio *    disclaimer.
14250395Sattilio * 2. Redistributions in binary form must reproduce the above copyright
15250395Sattilio *    notice, this list of conditions and the following disclaimer in the
16250395Sattilio *    documentation and/or other materials provided with the distribution.
17250395Sattilio *
18250395Sattilio * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19250395Sattilio * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20250395Sattilio * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21250395Sattilio * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22250395Sattilio * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23250395Sattilio * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24250395Sattilio * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25250395Sattilio * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26250395Sattilio * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27250395Sattilio * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28250395Sattilio *
29250395Sattilio * $FreeBSD$
30250395Sattilio */
31250395Sattilio
32250395Sattilio#ifndef _SYS__BITSET_H_
33250395Sattilio#define	_SYS__BITSET_H_
34250395Sattilio
35250395Sattilio/*
36250395Sattilio * Macros addressing word and bit within it, tuned to make compiler
37250395Sattilio * optimize cases when SETSIZE fits into single machine word.
38250395Sattilio */
39299122Sjhb#define	_BITSET_BITS		(sizeof(long) * 8)
40250395Sattilio
41299122Sjhb#define	__howmany(x, y)	(((x) + ((y) - 1)) / (y))
42250395Sattilio
43299122Sjhb#define	__bitset_words(_s)	(__howmany(_s, _BITSET_BITS))
44250395Sattilio
45250395Sattilio#define	BITSET_DEFINE(t, _s)						\
46250395Sattiliostruct t {								\
47250395Sattilio        long    __bits[__bitset_words((_s))];				\
48289867Smarkj}
49250395Sattilio
50299184Sroyger/*
51299184Sroyger * Helper to declare a bitset without it's size being a constant.
52299184Sroyger *
53299184Sroyger * Sadly we cannot declare a bitset struct with '__bits[]', because it's
54299184Sroyger * the only member of the struct and the compiler complains.
55299184Sroyger */
56299184Sroyger#define BITSET_DEFINE_VAR(t)	BITSET_DEFINE(t, 1)
57299184Sroyger
58250395Sattilio#endif /* !_SYS__BITSET_H_ */
59