bitops.h revision 1.2 1 1.1 riastrad /* $NetBSD: bitops.h,v 1.2 2018/08/27 06:17:17 riastradh Exp $ */
2 1.1 riastrad
3 1.1 riastrad /*-
4 1.1 riastrad * Copyright (c) 2013 The NetBSD Foundation, Inc.
5 1.1 riastrad * All rights reserved.
6 1.1 riastrad *
7 1.1 riastrad * This code is derived from software contributed to The NetBSD Foundation
8 1.1 riastrad * by Taylor R. Campbell.
9 1.1 riastrad *
10 1.1 riastrad * Redistribution and use in source and binary forms, with or without
11 1.1 riastrad * modification, are permitted provided that the following conditions
12 1.1 riastrad * are met:
13 1.1 riastrad * 1. Redistributions of source code must retain the above copyright
14 1.1 riastrad * notice, this list of conditions and the following disclaimer.
15 1.1 riastrad * 2. Redistributions in binary form must reproduce the above copyright
16 1.1 riastrad * notice, this list of conditions and the following disclaimer in the
17 1.1 riastrad * documentation and/or other materials provided with the distribution.
18 1.1 riastrad *
19 1.1 riastrad * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 1.1 riastrad * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 1.1 riastrad * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 1.1 riastrad * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 1.1 riastrad * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 1.1 riastrad * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 1.1 riastrad * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 1.1 riastrad * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 1.1 riastrad * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 1.1 riastrad * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 1.1 riastrad * POSSIBILITY OF SUCH DAMAGE.
30 1.1 riastrad */
31 1.1 riastrad
32 1.1 riastrad #ifndef _LINUX_BITOPS_H_
33 1.1 riastrad #define _LINUX_BITOPS_H_
34 1.1 riastrad
35 1.1 riastrad #include <sys/cdefs.h>
36 1.1 riastrad #include <sys/types.h>
37 1.1 riastrad #include <sys/param.h>
38 1.1 riastrad #include <sys/atomic.h>
39 1.1 riastrad #include <sys/bitops.h>
40 1.1 riastrad
41 1.1 riastrad #include <machine/limits.h>
42 1.1 riastrad
43 1.1 riastrad #include <lib/libkern/libkern.h>
44 1.1 riastrad
45 1.1 riastrad /*
46 1.1 riastrad * Linux __ffs/__ffs64 is zero-based; zero input is undefined. Our
47 1.1 riastrad * ffs/ffs64 is one-based; zero input yields zero.
48 1.1 riastrad */
49 1.1 riastrad static inline unsigned long
50 1.1 riastrad __ffs(unsigned long x)
51 1.1 riastrad {
52 1.1 riastrad
53 1.1 riastrad KASSERT(x != 0);
54 1.1 riastrad return ffs64(x) - 1;
55 1.1 riastrad }
56 1.1 riastrad
57 1.1 riastrad static inline unsigned long
58 1.1 riastrad __ffs64(uint64_t x)
59 1.1 riastrad {
60 1.1 riastrad
61 1.1 riastrad KASSERT(x != 0);
62 1.1 riastrad return ffs64(x) - 1;
63 1.1 riastrad }
64 1.1 riastrad
65 1.1 riastrad static inline unsigned int
66 1.1 riastrad hweight16(uint16_t n)
67 1.1 riastrad {
68 1.1 riastrad return popcount32(n);
69 1.1 riastrad }
70 1.1 riastrad
71 1.1 riastrad static inline unsigned int
72 1.1 riastrad hweight32(uint32_t n)
73 1.1 riastrad {
74 1.1 riastrad return popcount32(n);
75 1.1 riastrad }
76 1.1 riastrad
77 1.1 riastrad /*
78 1.1 riastrad * XXX Don't define BITS_PER_LONG as sizeof(unsigned long)*CHAR_BIT
79 1.1 riastrad * because that won't work in preprocessor conditionals, where it often
80 1.1 riastrad * turns up.
81 1.1 riastrad */
82 1.1 riastrad
83 1.1 riastrad #define BITS_TO_LONGS(n) \
84 1.1 riastrad roundup2((n), (sizeof(unsigned long) * CHAR_BIT))
85 1.1 riastrad
86 1.1 riastrad #define BIT(n) ((uintmax_t)1 << (n))
87 1.2 riastrad #define GENMASK(h,l) __BITS(h,l)
88 1.1 riastrad
89 1.1 riastrad static inline int
90 1.1 riastrad test_bit(unsigned int n, const volatile unsigned long *p)
91 1.1 riastrad {
92 1.1 riastrad const unsigned units = (sizeof(unsigned long) * CHAR_BIT);
93 1.1 riastrad
94 1.1 riastrad return ((p[n / units] & (1UL << (n % units))) != 0);
95 1.1 riastrad }
96 1.1 riastrad
97 1.1 riastrad static inline void
98 1.1 riastrad __set_bit(unsigned int n, volatile unsigned long *p)
99 1.1 riastrad {
100 1.1 riastrad const unsigned units = (sizeof(unsigned long) * CHAR_BIT);
101 1.1 riastrad
102 1.1 riastrad p[n / units] |= (1UL << (n % units));
103 1.1 riastrad }
104 1.1 riastrad
105 1.1 riastrad static inline void
106 1.1 riastrad __clear_bit(unsigned int n, volatile unsigned long *p)
107 1.1 riastrad {
108 1.1 riastrad const unsigned units = (sizeof(unsigned long) * CHAR_BIT);
109 1.1 riastrad
110 1.1 riastrad p[n / units] &= ~(1UL << (n % units));
111 1.1 riastrad }
112 1.1 riastrad
113 1.1 riastrad static inline void
114 1.1 riastrad __change_bit(unsigned int n, volatile unsigned long *p)
115 1.1 riastrad {
116 1.1 riastrad const unsigned units = (sizeof(unsigned long) * CHAR_BIT);
117 1.1 riastrad
118 1.1 riastrad p[n / units] ^= (1UL << (n % units));
119 1.1 riastrad }
120 1.1 riastrad
121 1.1 riastrad static inline unsigned long
122 1.1 riastrad __test_and_set_bit(unsigned int bit, volatile unsigned long *ptr)
123 1.1 riastrad {
124 1.1 riastrad const unsigned int units = (sizeof(*ptr) * CHAR_BIT);
125 1.1 riastrad volatile unsigned long *const p = &ptr[bit / units];
126 1.1 riastrad const unsigned long mask = (1UL << (bit % units));
127 1.1 riastrad unsigned long v;
128 1.1 riastrad
129 1.1 riastrad v = *p;
130 1.1 riastrad *p |= mask;
131 1.1 riastrad
132 1.1 riastrad return ((v & mask) != 0);
133 1.1 riastrad }
134 1.1 riastrad
135 1.1 riastrad static inline unsigned long
136 1.1 riastrad __test_and_clear_bit(unsigned int bit, volatile unsigned long *ptr)
137 1.1 riastrad {
138 1.1 riastrad const unsigned int units = (sizeof(*ptr) * CHAR_BIT);
139 1.1 riastrad volatile unsigned long *const p = &ptr[bit / units];
140 1.1 riastrad const unsigned long mask = (1UL << (bit % units));
141 1.1 riastrad unsigned long v;
142 1.1 riastrad
143 1.1 riastrad v = *p;
144 1.1 riastrad *p &= ~mask;
145 1.1 riastrad
146 1.1 riastrad return ((v & mask) != 0);
147 1.1 riastrad }
148 1.1 riastrad
149 1.1 riastrad static inline unsigned long
150 1.1 riastrad __test_and_change_bit(unsigned int bit, volatile unsigned long *ptr)
151 1.1 riastrad {
152 1.1 riastrad const unsigned int units = (sizeof(*ptr) * CHAR_BIT);
153 1.1 riastrad volatile unsigned long *const p = &ptr[bit / units];
154 1.1 riastrad const unsigned long mask = (1UL << (bit % units));
155 1.1 riastrad unsigned long v;
156 1.1 riastrad
157 1.1 riastrad v = *p;
158 1.1 riastrad *p ^= mask;
159 1.1 riastrad
160 1.1 riastrad return ((v & mask) != 0);
161 1.1 riastrad }
162 1.1 riastrad
163 1.1 riastrad static inline unsigned long
164 1.1 riastrad find_first_zero_bit(const unsigned long *ptr, unsigned long nbits)
165 1.1 riastrad {
166 1.1 riastrad const size_t bpl = (CHAR_BIT * sizeof(*ptr));
167 1.1 riastrad const unsigned long *p;
168 1.1 riastrad unsigned long result = 0;
169 1.1 riastrad
170 1.1 riastrad for (p = ptr; bpl < nbits; nbits -= bpl, p++, result += bpl) {
171 1.1 riastrad if (~*p)
172 1.1 riastrad break;
173 1.1 riastrad }
174 1.1 riastrad
175 1.1 riastrad CTASSERT(sizeof(unsigned long) <= sizeof(uint64_t));
176 1.1 riastrad result += ffs64(~(uint64_t)*p | (~0UL << MIN(nbits, bpl)));
177 1.1 riastrad return result;
178 1.1 riastrad }
179 1.1 riastrad
180 1.1 riastrad static inline unsigned
181 1.1 riastrad hweight8(unsigned w)
182 1.1 riastrad {
183 1.1 riastrad
184 1.1 riastrad return popcount(w & 0xff);
185 1.1 riastrad }
186 1.1 riastrad
187 1.1 riastrad #endif /* _LINUX_BITOPS_H_ */
188