bitmap.h revision 1.2.36.1 1 1.2.36.1 christos /* $NetBSD: bitmap.h,v 1.2.36.1 2019/06/10 22:08:31 christos Exp $ */
2 1.2 riastrad
3 1.2 riastrad /*-
4 1.2.36.1 christos * Copyright (c) 2018 The NetBSD Foundation, Inc.
5 1.2 riastrad * All rights reserved.
6 1.2 riastrad *
7 1.2 riastrad * This code is derived from software contributed to The NetBSD Foundation
8 1.2 riastrad * by Taylor R. Campbell.
9 1.2 riastrad *
10 1.2 riastrad * Redistribution and use in source and binary forms, with or without
11 1.2 riastrad * modification, are permitted provided that the following conditions
12 1.2 riastrad * are met:
13 1.2 riastrad * 1. Redistributions of source code must retain the above copyright
14 1.2 riastrad * notice, this list of conditions and the following disclaimer.
15 1.2 riastrad * 2. Redistributions in binary form must reproduce the above copyright
16 1.2 riastrad * notice, this list of conditions and the following disclaimer in the
17 1.2 riastrad * documentation and/or other materials provided with the distribution.
18 1.2 riastrad *
19 1.2 riastrad * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 1.2 riastrad * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 1.2 riastrad * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 1.2 riastrad * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 1.2 riastrad * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 1.2 riastrad * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 1.2 riastrad * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 1.2 riastrad * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 1.2 riastrad * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 1.2 riastrad * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 1.2 riastrad * POSSIBILITY OF SUCH DAMAGE.
30 1.2 riastrad */
31 1.2 riastrad
32 1.2 riastrad #ifndef _LINUX_BITMAP_H_
33 1.2 riastrad #define _LINUX_BITMAP_H_
34 1.2 riastrad
35 1.2.36.1 christos #include <sys/param.h>
36 1.2.36.1 christos #include <sys/types.h>
37 1.2.36.1 christos #include <sys/systm.h>
38 1.2.36.1 christos
39 1.2.36.1 christos /*
40 1.2.36.1 christos * bitmap_zero(bitmap, nbits)
41 1.2.36.1 christos *
42 1.2.36.1 christos * Zero a bitmap that was allocated to have nbits bits. Yes, this
43 1.2.36.1 christos * zeros bits past nbits.
44 1.2.36.1 christos */
45 1.2.36.1 christos static inline void
46 1.2.36.1 christos bitmap_zero(unsigned long *bitmap, size_t nbits)
47 1.2.36.1 christos {
48 1.2.36.1 christos const size_t bpl = NBBY * sizeof(*bitmap);
49 1.2.36.1 christos size_t n = howmany(nbits, bpl);
50 1.2.36.1 christos
51 1.2.36.1 christos memset(bitmap, 0, n * sizeof(*bitmap));
52 1.2.36.1 christos }
53 1.2.36.1 christos
54 1.2.36.1 christos /*
55 1.2.36.1 christos * bitmap_empty(bitmap, nbits)
56 1.2.36.1 christos *
57 1.2.36.1 christos * Return true if all bits at 0, 1, 2, ..., nbits-2, nbits-1 are
58 1.2.36.1 christos * 0, or false if any of them is 1.
59 1.2.36.1 christos */
60 1.2.36.1 christos static inline bool
61 1.2.36.1 christos bitmap_empty(const unsigned long *bitmap, size_t nbits)
62 1.2.36.1 christos {
63 1.2.36.1 christos const size_t bpl = NBBY * sizeof(*bitmap);
64 1.2.36.1 christos
65 1.2.36.1 christos for (; nbits >= bpl; nbits -= bpl) {
66 1.2.36.1 christos if (*bitmap++)
67 1.2.36.1 christos return false;
68 1.2.36.1 christos }
69 1.2.36.1 christos
70 1.2.36.1 christos if (nbits) {
71 1.2.36.1 christos if (*bitmap & ~(~0UL << nbits))
72 1.2.36.1 christos return false;
73 1.2.36.1 christos }
74 1.2.36.1 christos
75 1.2.36.1 christos return true;
76 1.2.36.1 christos }
77 1.2.36.1 christos
78 1.2.36.1 christos /*
79 1.2.36.1 christos * bitmap_weight(bitmap, nbits)
80 1.2.36.1 christos *
81 1.2.36.1 christos * Compute the number of 1 bits at 0, 1, 2, ..., nbits-2, nbits-1.
82 1.2.36.1 christos */
83 1.2.36.1 christos static inline int
84 1.2.36.1 christos bitmap_weight(const unsigned long *bitmap, size_t nbits)
85 1.2.36.1 christos {
86 1.2.36.1 christos const size_t bpl = NBBY * sizeof(*bitmap);
87 1.2.36.1 christos int weight = 0;
88 1.2.36.1 christos
89 1.2.36.1 christos for (; nbits >= bpl; nbits -= bpl)
90 1.2.36.1 christos weight += popcountl(*bitmap++);
91 1.2.36.1 christos if (nbits)
92 1.2.36.1 christos weight += popcountl(*bitmap & ~(~0UL << nbits));
93 1.2.36.1 christos
94 1.2.36.1 christos return weight;
95 1.2.36.1 christos }
96 1.2.36.1 christos
97 1.2.36.1 christos /*
98 1.2.36.1 christos * bitmap_set(bitmap, startbit, nbits)
99 1.2.36.1 christos *
100 1.2.36.1 christos * Set bits at startbit, startbit+1, ..., startbit+nbits-2,
101 1.2.36.1 christos * startbit+nbits-1 to 1.
102 1.2.36.1 christos */
103 1.2.36.1 christos static inline void
104 1.2.36.1 christos bitmap_set(unsigned long *bitmap, size_t startbit, size_t nbits)
105 1.2.36.1 christos {
106 1.2.36.1 christos const size_t bpl = NBBY * sizeof(*bitmap);
107 1.2.36.1 christos unsigned long *p = bitmap + startbit/bpl;
108 1.2.36.1 christos unsigned initial = startbit%bpl;
109 1.2.36.1 christos
110 1.2.36.1 christos /* Handle an initial odd word if any. */
111 1.2.36.1 christos if (initial) {
112 1.2.36.1 christos /* Does the whole thing fit in a single word? */
113 1.2.36.1 christos if (nbits <= bpl - initial) {
114 1.2.36.1 christos /* Yes: just set nbits starting at initial. */
115 1.2.36.1 christos *p |= ~(~0ULL << nbits) << initial;
116 1.2.36.1 christos return;
117 1.2.36.1 christos }
118 1.2.36.1 christos /* Nope: set all bits above initial, and advance. */
119 1.2.36.1 christos *p++ |= ~0ULL << initial;
120 1.2.36.1 christos nbits -= bpl - initial;
121 1.2.36.1 christos }
122 1.2.36.1 christos
123 1.2.36.1 christos /* Set the middle part to all bits 1. */
124 1.2.36.1 christos for (; nbits >= bpl; nbits -= bpl)
125 1.2.36.1 christos *p++ = ~0UL;
126 1.2.36.1 christos
127 1.2.36.1 christos /* Handle a final odd word if any by setting its low nbits. */
128 1.2.36.1 christos if (nbits)
129 1.2.36.1 christos *p |= ~(~0ULL << nbits);
130 1.2.36.1 christos }
131 1.2.36.1 christos
132 1.2.36.1 christos /*
133 1.2.36.1 christos * bitmap_clear(bitmap, startbit, nbits)
134 1.2.36.1 christos *
135 1.2.36.1 christos * Clear bits at startbit, startbit+1, ..., startbit+nbits-2,
136 1.2.36.1 christos * startbit+nbits-1, replacing them by 0.
137 1.2.36.1 christos */
138 1.2.36.1 christos static inline void
139 1.2.36.1 christos bitmap_clear(unsigned long *bitmap, size_t startbit, size_t nbits)
140 1.2.36.1 christos {
141 1.2.36.1 christos const size_t bpl = NBBY * sizeof(*bitmap);
142 1.2.36.1 christos unsigned long *p = bitmap + startbit/bpl;
143 1.2.36.1 christos unsigned initial = startbit%bpl;
144 1.2.36.1 christos
145 1.2.36.1 christos /* Handle an initial odd word if any. */
146 1.2.36.1 christos if (initial) {
147 1.2.36.1 christos /* Does the whole thing fit in a single word? */
148 1.2.36.1 christos if (nbits <= bpl - initial) {
149 1.2.36.1 christos /* Yes: just clear nbits starting at initial. */
150 1.2.36.1 christos *p &= ~(~(~0ULL << nbits) << initial);
151 1.2.36.1 christos return;
152 1.2.36.1 christos }
153 1.2.36.1 christos /* Nope: clear all bits above initial, and advance. */
154 1.2.36.1 christos *p++ &= ~(~0ULL << initial);
155 1.2.36.1 christos nbits -= bpl - initial;
156 1.2.36.1 christos }
157 1.2.36.1 christos
158 1.2.36.1 christos /* Zero the middle part. */
159 1.2.36.1 christos for (; nbits >= bpl; nbits -= bpl)
160 1.2.36.1 christos *p++ = 0UL;
161 1.2.36.1 christos
162 1.2.36.1 christos /* Handle a final odd word if any by clearing its low nbits. */
163 1.2.36.1 christos if (nbits)
164 1.2.36.1 christos *p &= ~0ULL << nbits;
165 1.2.36.1 christos }
166 1.2.36.1 christos
167 1.2.36.1 christos /*
168 1.2.36.1 christos * bitmap_and(dst, src1, src2, nbits)
169 1.2.36.1 christos *
170 1.2.36.1 christos * Set dst to be the bitwise AND of src1 and src2, all bitmaps
171 1.2.36.1 christos * allocated to have nbits bits. Yes, this modifies bits past
172 1.2.36.1 christos * nbits. Any pair of {dst, src1, src2} may be aliases.
173 1.2.36.1 christos */
174 1.2.36.1 christos static inline void
175 1.2.36.1 christos bitmap_and(unsigned long *dst, const unsigned long *src1,
176 1.2.36.1 christos const unsigned long *src2, size_t nbits)
177 1.2.36.1 christos {
178 1.2.36.1 christos const size_t bpl = NBBY * sizeof(unsigned long);
179 1.2.36.1 christos size_t n = howmany(nbits, bpl);
180 1.2.36.1 christos
181 1.2.36.1 christos while (n --> 0)
182 1.2.36.1 christos *dst++ = *src1++ & *src2++;
183 1.2.36.1 christos }
184 1.2.36.1 christos
185 1.2.36.1 christos /*
186 1.2.36.1 christos * bitmap_or(dst, src1, src2, nbits)
187 1.2.36.1 christos *
188 1.2.36.1 christos * Set dst to be the bitwise inclusive-OR of src1 and src2, all
189 1.2.36.1 christos * bitmaps allocated to have nbits bits. Yes, this modifies bits
190 1.2.36.1 christos * past nbits. Any pair of {dst, src1, src2} may be aliases.
191 1.2.36.1 christos */
192 1.2.36.1 christos static inline void
193 1.2.36.1 christos bitmap_or(unsigned long *dst, const unsigned long *src1,
194 1.2.36.1 christos const unsigned long *src2, size_t nbits)
195 1.2.36.1 christos {
196 1.2.36.1 christos const size_t bpl = NBBY * sizeof(unsigned long);
197 1.2.36.1 christos size_t n = howmany(nbits, bpl);
198 1.2.36.1 christos
199 1.2.36.1 christos while (n --> 0)
200 1.2.36.1 christos *dst++ = *src1++ | *src2++;
201 1.2.36.1 christos }
202 1.2.36.1 christos
203 1.2 riastrad #endif /* _LINUX_BITMAP_H_ */
204