xref: /xnu-8019.80.24/osfmk/kern/bits.h (revision a325d9c4a84054e40bbe985afedcb50ab80993ea)
1 /*
2  * Copyright (c) 2015-2016 Apple Inc. All rights reserved.
3  *
4  * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5  *
6  * This file contains Original Code and/or Modifications of Original Code
7  * as defined in and that are subject to the Apple Public Source License
8  * Version 2.0 (the 'License'). You may not use this file except in
9  * compliance with the License. The rights granted to you under the License
10  * may not be used to create, or enable the creation or redistribution of,
11  * unlawful or unlicensed copies of an Apple operating system, or to
12  * circumvent, violate, or enable the circumvention or violation of, any
13  * terms of an Apple operating system software license agreement.
14  *
15  * Please obtain a copy of the License at
16  * http://www.opensource.apple.com/apsl/ and read it before using this file.
17  *
18  * The Original Code and all software distributed under the License are
19  * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20  * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21  * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22  * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23  * Please see the License for the specific language governing rights and
24  * limitations under the License.
25  *
26  * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27  *
28  * Bit manipulation functions
29  */
30 
31 #ifndef __BITS_H__
32 #define __BITS_H__
33 
34 #ifdef KERNEL
35 #include <kern/assert.h>
36 #include <kern/kalloc.h>
37 #else
38 #include <assert.h>
39 #include <stdlib.h>
40 #define kalloc_data(x) malloc(x)
41 #define kfree_data(x, y) free(x)
42 #endif
43 #include <stdbool.h>
44 #include <stdint.h>
45 #include <stdatomic.h>
46 
47 typedef unsigned int                    uint;
48 
49 #define BIT(b)                          (1ULL << (b))
50 
51 #define mask(width)                     (width >= 64 ? -1ULL : (BIT(width) - 1))
52 #define extract(x, shift, width)        ((((uint64_t)(x)) >> (shift)) & mask(width))
53 #define bits(x, hi, lo)                 extract((x), (lo), (hi) - (lo) + 1)
54 
55 #define bit_set(x, b)                   ((x) |= BIT(b))
56 #define bit_clear(x, b)                 ((x) &= ~BIT(b))
57 #define bit_test(x, b)                  ((bool)((x) & BIT(b)))
58 
59 inline static uint64_t
bit_ror64(uint64_t bitmap,uint n)60 bit_ror64(uint64_t bitmap, uint n)
61 {
62 #if defined(__arm64__)
63 	uint64_t result;
64 	uint64_t _n = (uint64_t)n;
65 	asm volatile ("ror %0, %1, %2" : "=r" (result) : "r" (bitmap), "r" (_n));
66 	return result;
67 #else
68 	n = n & 63;
69 	return (bitmap >> n) | (bitmap << (64 - n));
70 #endif
71 }
72 
73 inline static uint64_t
bit_rol64(uint64_t bitmap,uint n)74 bit_rol64(uint64_t bitmap, uint n)
75 {
76 #if defined(__arm64__)
77 	return bit_ror64(bitmap, 64U - n);
78 #else
79 	n = n & 63;
80 	return (bitmap << n) | (bitmap >> (64 - n));
81 #endif
82 }
83 
84 /* Non-atomically clear the bit and returns whether the bit value was changed */
85 #define bit_clear_if_set(bitmap, bit) \
86 ({ \
87 	int _n = (bit); \
88 	__auto_type _map = &(bitmap); \
89 	bool _bit_is_set = bit_test(*_map, _n); \
90 	bit_clear(*_map, _n); \
91 	_bit_is_set; \
92 })
93 
94 /* Non-atomically set the bit and returns whether the bit value was changed */
95 #define bit_set_if_clear(bitmap, bit) \
96 ({ \
97 	int _n = (bit); \
98 	__auto_type _map = &(bitmap); \
99 	bool _bit_is_set = bit_test(*_map, _n); \
100 	bit_set(*_map, _n); \
101 	!_bit_is_set; \
102 })
103 
104 /* Returns the most significant '1' bit, or -1 if all zeros */
105 inline static int
bit_first(uint64_t bitmap)106 bit_first(uint64_t bitmap)
107 {
108 #if defined(__arm64__)
109 	int64_t result;
110 	asm volatile ("clz %0, %1" : "=r" (result) : "r" (bitmap));
111 	return 63 - (int)result;
112 #else
113 	return (bitmap == 0) ? -1 : 63 - __builtin_clzll(bitmap);
114 #endif
115 }
116 
117 
118 inline static int
__bit_next(uint64_t bitmap,int previous_bit)119 __bit_next(uint64_t bitmap, int previous_bit)
120 {
121 	uint64_t mask = previous_bit ? mask(previous_bit) : ~0ULL;
122 
123 	return bit_first(bitmap & mask);
124 }
125 
126 /* Returns the most significant '1' bit that is less significant than previous_bit,
127  * or -1 if no such bit exists.
128  */
129 inline static int
bit_next(uint64_t bitmap,int previous_bit)130 bit_next(uint64_t bitmap, int previous_bit)
131 {
132 	if (previous_bit == 0) {
133 		return -1;
134 	} else {
135 		return __bit_next(bitmap, previous_bit);
136 	}
137 }
138 
139 /* Returns the least significant '1' bit, or -1 if all zeros */
140 inline static int
lsb_first(uint64_t bitmap)141 lsb_first(uint64_t bitmap)
142 {
143 	return __builtin_ffsll((long long)bitmap) - 1;
144 }
145 
146 /* Returns the least significant '1' bit that is more significant than previous_bit,
147  * or -1 if no such bit exists.
148  * previous_bit may be -1, in which case this is equivalent to lsb_first()
149  */
150 inline static int
lsb_next(uint64_t bitmap,int previous_bit)151 lsb_next(uint64_t bitmap, int previous_bit)
152 {
153 	uint64_t mask = mask(previous_bit + 1);
154 
155 	return lsb_first(bitmap & ~mask);
156 }
157 
158 inline static int
bit_count(uint64_t x)159 bit_count(uint64_t x)
160 {
161 	return __builtin_popcountll(x);
162 }
163 
164 /* Return the highest power of 2 that is <= n, or -1 if n == 0 */
165 inline static int
bit_floor(uint64_t n)166 bit_floor(uint64_t n)
167 {
168 	return bit_first(n);
169 }
170 
171 /* Return the lowest power of 2 that is >= n, or -1 if n == 0 */
172 inline static int
bit_ceiling(uint64_t n)173 bit_ceiling(uint64_t n)
174 {
175 	if (n == 0) {
176 		return -1;
177 	}
178 	return bit_first(n - 1) + 1;
179 }
180 
181 /* If n is a power of 2, bit_log2(n) == bit_floor(n) == bit_ceiling(n) */
182 #define bit_log2(n)             bit_floor((uint64_t)(n))
183 
184 typedef uint64_t                bitmap_t;
185 
186 
187 inline static bool
atomic_bit_set(_Atomic bitmap_t * __single map,int n,int mem_order)188 atomic_bit_set(_Atomic bitmap_t *__single map, int n, int mem_order)
189 {
190 	bitmap_t prev;
191 	prev = __c11_atomic_fetch_or(map, BIT(n), mem_order);
192 	return bit_test(prev, n);
193 }
194 
195 inline static bool
atomic_bit_clear(_Atomic bitmap_t * __single map,int n,int mem_order)196 atomic_bit_clear(_Atomic bitmap_t *__single map, int n, int mem_order)
197 {
198 	bitmap_t prev;
199 	prev = __c11_atomic_fetch_and(map, ~BIT(n), mem_order);
200 	return bit_test(prev, n);
201 }
202 
203 
204 #define BITMAP_LEN(n)   (((uint)(n) + 63) >> 6)         /* Round to 64bit bitmap_t */
205 #define BITMAP_SIZE(n)  (size_t)(BITMAP_LEN(n) << 3)            /* Round to 64bit bitmap_t, then convert to bytes */
206 #define bitmap_bit(n)   bits(n, 5, 0)
207 #define bitmap_index(n) bits(n, 63, 6)
208 
209 inline static bitmap_t * __indexable
bitmap_zero(bitmap_t * __indexable map,uint nbits)210 bitmap_zero(bitmap_t *__indexable map, uint nbits)
211 {
212 	memset((void *)map, 0, BITMAP_SIZE(nbits));
213 	return map;
214 }
215 
216 inline static bitmap_t *__indexable
bitmap_full(bitmap_t * __indexable map,uint nbits)217 bitmap_full(bitmap_t *__indexable map, uint nbits)
218 {
219 	uint i;
220 
221 	for (i = 0; i < bitmap_index(nbits - 1); i++) {
222 		map[i] = ~((uint64_t)0);
223 	}
224 
225 	uint nbits_filled = i * 64;
226 
227 	if (nbits > nbits_filled) {
228 		map[i] = mask(nbits - nbits_filled);
229 	}
230 
231 	return map;
232 }
233 
234 inline static bool
bitmap_is_full(bitmap_t * __indexable map,uint nbits)235 bitmap_is_full(bitmap_t *__indexable map, uint nbits)
236 {
237 	uint i;
238 
239 	for (i = 0; i < bitmap_index(nbits - 1); i++) {
240 		if (map[i] != ~((uint64_t)0)) {
241 			return false;
242 		}
243 	}
244 
245 	uint nbits_filled = i * 64;
246 
247 	if (nbits > nbits_filled) {
248 		return map[i] == mask(nbits - nbits_filled);
249 	}
250 
251 	return true;
252 }
253 
254 inline static bitmap_t *__indexable
bitmap_alloc(uint nbits)255 bitmap_alloc(uint nbits)
256 {
257 	bitmap_t *__unsafe_indexable map;
258 	assert(nbits > 0);
259 	map = (bitmap_t *)kalloc_data(BITMAP_SIZE(nbits), Z_WAITOK_ZERO);
260 #if XNU_BOUND_CHECKS /* helps stripping */
261 	return __unsafe_forge_bidi_indexable(map, BITMAP_SIZE(nbits));
262 #else
263 	return map;
264 #endif
265 }
266 
267 inline static void
bitmap_free(bitmap_t * map,uint nbits)268 bitmap_free(bitmap_t *map, uint nbits)
269 {
270 	assert(nbits > 0);
271 	kfree_data(map, BITMAP_SIZE(nbits));
272 }
273 
274 inline static void
bitmap_set(bitmap_t * __indexable map,uint n)275 bitmap_set(bitmap_t *__indexable map, uint n)
276 {
277 	bit_set(map[bitmap_index(n)], bitmap_bit(n));
278 }
279 
280 inline static void
bitmap_clear(bitmap_t * __indexable map,uint n)281 bitmap_clear(bitmap_t *__indexable map, uint n)
282 {
283 	bit_clear(map[bitmap_index(n)], bitmap_bit(n));
284 }
285 
286 inline static bool
atomic_bitmap_set(_Atomic bitmap_t * __indexable map,uint n,int mem_order)287 atomic_bitmap_set(_Atomic bitmap_t *__indexable map, uint n, int mem_order)
288 {
289 	return atomic_bit_set(&map[bitmap_index(n)], bitmap_bit(n), mem_order);
290 }
291 
292 inline static bool
atomic_bitmap_clear(_Atomic bitmap_t * __indexable map,uint n,int mem_order)293 atomic_bitmap_clear(_Atomic bitmap_t *__indexable map, uint n, int mem_order)
294 {
295 	return atomic_bit_clear(&map[bitmap_index(n)], bitmap_bit(n), mem_order);
296 }
297 
298 inline static bool
bitmap_test(const bitmap_t * __indexable map,uint n)299 bitmap_test(const bitmap_t *__indexable map, uint n)
300 {
301 	return bit_test(map[bitmap_index(n)], bitmap_bit(n));
302 }
303 
304 inline static int
bitmap_first(bitmap_t * __indexable map,uint nbits)305 bitmap_first(bitmap_t *__indexable map, uint nbits)
306 {
307 	for (int i = (int)bitmap_index(nbits - 1); i >= 0; i--) {
308 		if (map[i] == 0) {
309 			continue;
310 		}
311 		return (i << 6) + bit_first(map[i]);
312 	}
313 
314 	return -1;
315 }
316 
317 inline static void
bitmap_not(bitmap_t * __indexable out,const bitmap_t * __indexable in,uint nbits)318 bitmap_not(bitmap_t *__indexable out, const bitmap_t *__indexable in, uint nbits)
319 {
320 	uint i;
321 
322 	for (i = 0; i < bitmap_index(nbits - 1); i++) {
323 		out[i] = ~in[i];
324 	}
325 
326 	uint nbits_complete = i * 64;
327 
328 	if (nbits > nbits_complete) {
329 		out[i] = ~in[i] & mask(nbits - nbits_complete);
330 	}
331 }
332 
333 inline static void
bitmap_and(bitmap_t * __indexable out,const bitmap_t * __indexable in1,const bitmap_t * __indexable in2,uint nbits)334 bitmap_and(
335 	bitmap_t       *__indexable out,
336 	const bitmap_t *__indexable in1,
337 	const bitmap_t *__indexable in2,
338 	uint                        nbits)
339 {
340 	for (uint i = 0; i <= bitmap_index(nbits - 1); i++) {
341 		out[i] = in1[i] & in2[i];
342 	}
343 }
344 
345 inline static void
bitmap_and_not(bitmap_t * __indexable out,const bitmap_t * __indexable in1,const bitmap_t * __indexable in2,uint nbits)346 bitmap_and_not(
347 	bitmap_t       *__indexable out,
348 	const bitmap_t *__indexable in1,
349 	const bitmap_t *__indexable in2,
350 	uint                        nbits)
351 {
352 	uint i;
353 
354 	for (i = 0; i <= bitmap_index(nbits - 1); i++) {
355 		out[i] = in1[i] & ~in2[i];
356 	}
357 }
358 
359 inline static void
bitmap_or(bitmap_t * __indexable out,const bitmap_t * __indexable in1,const bitmap_t * __indexable in2,uint nbits)360 bitmap_or(
361 	bitmap_t       *__indexable out,
362 	const bitmap_t *__indexable in1,
363 	const bitmap_t *__indexable in2,
364 	uint                        nbits)
365 {
366 	for (uint i = 0; i <= bitmap_index(nbits - 1); i++) {
367 		out[i] = in1[i] | in2[i];
368 	}
369 }
370 
371 inline static bool
bitmap_equal(const bitmap_t * __indexable in1,const bitmap_t * __indexable in2,uint nbits)372 bitmap_equal(const bitmap_t *__indexable in1, const bitmap_t *__indexable in2, uint nbits)
373 {
374 	for (uint i = 0; i <= bitmap_index(nbits - 1); i++) {
375 		if (in1[i] != in2[i]) {
376 			return false;
377 		}
378 	}
379 
380 	return true;
381 }
382 
383 inline static int
bitmap_and_not_mask_first(bitmap_t * __indexable map,const bitmap_t * __indexable mask,uint nbits)384 bitmap_and_not_mask_first(bitmap_t *__indexable map, const bitmap_t *__indexable mask, uint nbits)
385 {
386 	for (int i = (int)bitmap_index(nbits - 1); i >= 0; i--) {
387 		if ((map[i] & ~mask[i]) == 0) {
388 			continue;
389 		}
390 		return (i << 6) + bit_first(map[i] & ~mask[i]);
391 	}
392 
393 	return -1;
394 }
395 
396 inline static int
bitmap_lsb_first(const bitmap_t * __indexable map,uint nbits)397 bitmap_lsb_first(const bitmap_t *__indexable map, uint nbits)
398 {
399 	for (uint i = 0; i <= bitmap_index(nbits - 1); i++) {
400 		if (map[i] == 0) {
401 			continue;
402 		}
403 		return (int)((i << 6) + (uint32_t)lsb_first(map[i]));
404 	}
405 
406 	return -1;
407 }
408 
409 inline static int
bitmap_next(const bitmap_t * __indexable map,uint prev)410 bitmap_next(const bitmap_t *__indexable map, uint prev)
411 {
412 	if (prev == 0) {
413 		return -1;
414 	}
415 
416 	int64_t i = bitmap_index(prev - 1);
417 	int res = __bit_next(map[i], bits(prev, 5, 0));
418 	if (res >= 0) {
419 		return (int)(res + (i << 6));
420 	}
421 
422 	for (i = i - 1; i >= 0; i--) {
423 		if (map[i] == 0) {
424 			continue;
425 		}
426 		return (int)((i << 6) + bit_first(map[i]));
427 	}
428 
429 	return -1;
430 }
431 
432 inline static int
bitmap_lsb_next(const bitmap_t * __indexable map,uint nbits,uint prev)433 bitmap_lsb_next(const bitmap_t *__indexable map, uint nbits, uint prev)
434 {
435 	if ((prev + 1) >= nbits) {
436 		return -1;
437 	}
438 
439 	uint64_t i = bitmap_index(prev + 1);
440 	uint b = bits((prev + 1), 5, 0) - 1;
441 	int32_t res = lsb_next((uint64_t)map[i], (int)b);
442 	if (res >= 0) {
443 		return (int)((uint64_t)res + (i << 6));
444 	}
445 
446 	for (i = i + 1; i <= bitmap_index(nbits - 1); i++) {
447 		if (map[i] == 0) {
448 			continue;
449 		}
450 		return (int)((i << 6) + (uint64_t)lsb_first(map[i]));
451 	}
452 
453 	return -1;
454 }
455 
456 #endif
457