Loading...
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
/*
 * Copyright (c) 2015-2016 Apple Inc. All rights reserved.
 *
 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
 *
 * This file contains Original Code and/or Modifications of Original Code
 * as defined in and that are subject to the Apple Public Source License
 * Version 2.0 (the 'License'). You may not use this file except in
 * compliance with the License. The rights granted to you under the License
 * may not be used to create, or enable the creation or redistribution of,
 * unlawful or unlicensed copies of an Apple operating system, or to
 * circumvent, violate, or enable the circumvention or violation of, any
 * terms of an Apple operating system software license agreement.
 *
 * Please obtain a copy of the License at
 * http://www.opensource.apple.com/apsl/ and read it before using this file.
 *
 * The Original Code and all software distributed under the License are
 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
 * Please see the License for the specific language governing rights and
 * limitations under the License.
 *
 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
 *
 * Bit manipulation functions
 */

#ifndef __BITS_H__
#define __BITS_H__

#ifdef KERNEL
#include <kern/assert.h>
#include <kern/kalloc.h>
#else
#include <assert.h>
#include <stdlib.h>
#define kalloc_data(x, y) malloc(x)
#define kfree_data(x, y) free(x)
#endif
#include <stdbool.h>
#include <stdint.h>
#include <stdatomic.h>
#include <string.h>

#ifndef __DARWIN_UINT
typedef unsigned int                    uint;
#define __DARWIN_UINT
#endif

#define BIT(b)                          (1ULL << (b))

#define bits_mask(width)                ((width) >= 64 ? -1ULL : (BIT(width) - 1))
#define bits_extract(x, shift, width)   ((((uint64_t)(x)) >> (shift)) & bits_mask(width))
#define bits(x, hi, lo)                 bits_extract((x), (lo), (hi) - (lo) + 1)

#define bit_assign(x, b, e)             ((x) = (((x) & ~BIT(b))) | ((((uint64_t) (!!(e)))) << (b)))
#define bit_set(x, b)                   ((x) |= BIT(b))
#define bit_clear(x, b)                 ((x) &= ~BIT(b))
#define bit_test(x, b)                  ((bool)((x) & BIT(b)))

/*
 * FIXME(rdar://154775164): Deprecate `mask` and `extract` in kern/bits.h
 *
 * These macros have been deprecated.
 */
#define mask(width) \
	_Pragma("message \"mask has been deprecated. Please use bits_mask instead.\"") \
	bits_mask(width)
#define extract(x, shift, width) \
	_Pragma("message \"extract has been deprecated. Please use bits_extract instead.\"") \
	bits_extract(x, shift, width)

inline static uint64_t
bit_ror64(uint64_t bitmap, uint n)
{
	return __builtin_rotateright64(bitmap, n);
}

inline static uint64_t
bit_rol64(uint64_t bitmap, uint n)
{
	return __builtin_rotateleft64(bitmap, n);
}

/* Non-atomically clear the bit and returns whether the bit value was changed */
#define bit_clear_if_set(bitmap, bit) \
({ \
	int _n = (bit); \
	__auto_type _map = &(bitmap); \
	bool _bit_is_set = bit_test(*_map, _n); \
	bit_clear(*_map, _n); \
	_bit_is_set; \
})

/* Non-atomically set the bit and returns whether the bit value was changed */
#define bit_set_if_clear(bitmap, bit) \
({ \
	int _n = (bit); \
	__auto_type _map = &(bitmap); \
	bool _bit_is_set = bit_test(*_map, _n); \
	bit_set(*_map, _n); \
	!_bit_is_set; \
})

/*
 * Note on bit indexing: bit indices are offsets from the least significant bit.
 * So the bit at index `i` would be found by `1 & (bitmap >> i)`.
 */

/* Returns the most significant '1' bit, or -1 if all zeros */
inline static int
bit_first(uint64_t bitmap)
{
	return 63 - __builtin_clzg(bitmap, 64);
}


inline static int
__bit_next(uint64_t bitmap, int previous_bit)
{
	uint64_t mask = previous_bit ? bits_mask(previous_bit) : ~0ULL;

	return bit_first(bitmap & mask);
}

/* Returns the most significant '1' bit that is less significant than previous_bit,
 * or -1 if no such bit exists.
 */
inline static int
bit_next(uint64_t bitmap, int previous_bit)
{
	if (previous_bit == 0) {
		return -1;
	} else {
		return __bit_next(bitmap, previous_bit);
	}
}

/* Returns the least significant '1' bit, or -1 if all zeros */
inline static int
lsb_first(uint64_t bitmap)
{
	return __builtin_ctzg(bitmap, -1);
}

/* Returns the least significant '1' bit that is more significant than previous_bit,
 * or -1 if no such bit exists.
 * previous_bit may be -1, in which case this is equivalent to lsb_first()
 */
inline static int
lsb_next(uint64_t bitmap, int previous_bit)
{
	uint64_t mask = bits_mask(previous_bit + 1);

	return lsb_first(bitmap & ~mask);
}

inline static int
bit_count(uint64_t x)
{
	return __builtin_popcountll(x);
}

/* Return the highest power of 2 that is <= n, or -1 if n == 0 */
inline static int
bit_floor(uint64_t n)
{
	return bit_first(n);
}

/* Return the lowest power of 2 that is >= n, or -1 if n == 0 */
inline static int
bit_ceiling(uint64_t n)
{
	if (n == 0) {
		return -1;
	}
	return bit_first(n - 1) + 1;
}

/* If n is a power of 2, bit_log2(n) == bit_floor(n) == bit_ceiling(n) */
#define bit_log2(n)             bit_floor((uint64_t)(n))

typedef uint64_t                bitmap_t;


inline static bool
atomic_bit_set(_Atomic bitmap_t *__single map, int n, int mem_order)
{
	bitmap_t prev;
	prev = __c11_atomic_fetch_or(map, BIT(n), mem_order);
	return bit_test(prev, n);
}

inline static bool
atomic_bit_clear(_Atomic bitmap_t *__single map, int n, int mem_order)
{
	bitmap_t prev;
	prev = __c11_atomic_fetch_and(map, ~BIT(n), mem_order);
	return bit_test(prev, n);
}

inline static bool
atomic_bit_test(_Atomic bitmap_t *__single map, int n, int mem_order)
{
	bitmap_t prev;
	prev = __c11_atomic_load(map, mem_order);
	return bit_test(prev, n);
}

inline static int
atomic_bit_count(_Atomic bitmap_t *__single map, int mem_order)
{
	bitmap_t prev;
	prev = __c11_atomic_load(map, mem_order);
	return bit_count(prev);
}

#define BITMAP_LEN(n)   (((uint)(n) + 63) >> 6)         /* Round to 64bit bitmap_t */
#define BITMAP_SIZE(n)  (size_t)(BITMAP_LEN(n) << 3)            /* Round to 64bit bitmap_t, then convert to bytes */
#define bitmap_bit(n)   bits(n, 5, 0)
#define bitmap_index(n) bits(n, 63, 6)

inline static bitmap_t * __header_indexable
bitmap_zero(bitmap_t *__header_indexable map, uint nbits)
{
	memset((void *)map, 0, BITMAP_SIZE(nbits));
	return map;
}

inline static bitmap_t *__header_indexable
bitmap_full(bitmap_t *__header_indexable map, uint nbits)
{
	uint i;

	for (i = 0; i < bitmap_index(nbits - 1); i++) {
		map[i] = ~((uint64_t)0);
	}

	uint nbits_filled = i * 64;

	if (nbits > nbits_filled) {
		map[i] = bits_mask(nbits - nbits_filled);
	}

	return map;
}

inline static bool
bitmap_is_empty(bitmap_t *__header_indexable map, uint nbits)
{
	for (uint i = 0; i < BITMAP_LEN(nbits); i++) {
		if (map[i]) {
			return false;
		}
	}

	return true;
}

inline static bool
bitmap_is_full(bitmap_t *__header_indexable map, uint nbits)
{
	uint i;

	for (i = 0; i < bitmap_index(nbits - 1); i++) {
		if (map[i] != ~((uint64_t)0)) {
			return false;
		}
	}

	uint nbits_filled = i * 64;

	if (nbits > nbits_filled) {
		return map[i] == bits_mask(nbits - nbits_filled);
	}

	return true;
}

inline static bitmap_t *__header_indexable
bitmap_alloc(uint nbits)
{
	assert(nbits > 0);
	return (bitmap_t *)kalloc_data(BITMAP_SIZE(nbits), Z_WAITOK_ZERO);
}

inline static void
bitmap_free(bitmap_t *map, uint nbits)
{
	assert(nbits > 0);
	kfree_data(map, BITMAP_SIZE(nbits));
}

inline static void
bitmap_set(bitmap_t *__header_indexable map, uint n)
{
	bit_set(map[bitmap_index(n)], bitmap_bit(n));
}

inline static void
bitmap_clear(bitmap_t *__header_indexable map, uint n)
{
	bit_clear(map[bitmap_index(n)], bitmap_bit(n));
}

inline static bool
atomic_bitmap_set(_Atomic bitmap_t *__header_indexable map, uint n, int mem_order)
{
	return atomic_bit_set(&map[bitmap_index(n)], bitmap_bit(n), mem_order);
}

inline static bool
atomic_bitmap_clear(_Atomic bitmap_t *__header_indexable map, uint n, int mem_order)
{
	return atomic_bit_clear(&map[bitmap_index(n)], bitmap_bit(n), mem_order);
}

inline static bool
bitmap_test(const bitmap_t *__header_indexable map, uint n)
{
	return bit_test(map[bitmap_index(n)], bitmap_bit(n));
}

inline static int
bitmap_first(bitmap_t *__header_indexable map, uint nbits)
{
	for (int i = (int)bitmap_index(nbits - 1); i >= 0; i--) {
		if (map[i] == 0) {
			continue;
		}
		return (i << 6) + bit_first(map[i]);
	}

	return -1;
}

inline static void
bitmap_not(
	bitmap_t       *__header_indexable out,
	const bitmap_t *__header_indexable in,
	uint                               nbits)
{
	uint i;

	for (i = 0; i < bitmap_index(nbits - 1); i++) {
		out[i] = ~in[i];
	}

	uint nbits_complete = i * 64;

	if (nbits > nbits_complete) {
		out[i] = ~in[i] & bits_mask(nbits - nbits_complete);
	}
}

inline static void
bitmap_and(
	bitmap_t       *__header_indexable out,
	const bitmap_t *__header_indexable in1,
	const bitmap_t *__header_indexable in2,
	uint                               nbits)
{
	for (uint i = 0; i <= bitmap_index(nbits - 1); i++) {
		out[i] = in1[i] & in2[i];
	}
}

inline static void
bitmap_and_not(
	bitmap_t       *__header_indexable out,
	const bitmap_t *__header_indexable in1,
	const bitmap_t *__header_indexable in2,
	uint                               nbits)
{
	uint i;

	for (i = 0; i <= bitmap_index(nbits - 1); i++) {
		out[i] = in1[i] & ~in2[i];
	}
}

inline static void
bitmap_or(
	bitmap_t       *__header_indexable out,
	const bitmap_t *__header_indexable in1,
	const bitmap_t *__header_indexable in2,
	uint                        nbits)
{
	for (uint i = 0; i <= bitmap_index(nbits - 1); i++) {
		out[i] = in1[i] | in2[i];
	}
}

inline static bool
bitmap_equal(
	const bitmap_t *__header_indexable in1,
	const bitmap_t *__header_indexable in2,
	uint                               nbits)
{
	for (uint i = 0; i <= bitmap_index(nbits - 1); i++) {
		if (in1[i] != in2[i]) {
			return false;
		}
	}

	return true;
}

inline static int
bitmap_and_not_mask_first(
	bitmap_t       *__header_indexable map,
	const bitmap_t *__header_indexable mask,
	uint                               nbits)
{
	for (int i = (int)bitmap_index(nbits - 1); i >= 0; i--) {
		if ((map[i] & ~mask[i]) == 0) {
			continue;
		}
		return (i << 6) + bit_first(map[i] & ~mask[i]);
	}

	return -1;
}

inline static int
bitmap_lsb_first(const bitmap_t *__header_indexable map, uint nbits)
{
	for (uint i = 0; i <= bitmap_index(nbits - 1); i++) {
		if (map[i] == 0) {
			continue;
		}
		return (int)((i << 6) + (uint32_t)lsb_first(map[i]));
	}

	return -1;
}

inline static int
bitmap_next(const bitmap_t *__header_indexable map, uint prev)
{
	if (prev == 0) {
		return -1;
	}

	int64_t i = bitmap_index(prev - 1);
	int res = __bit_next(map[i], bits(prev, 5, 0));
	if (res >= 0) {
		return (int)(res + (i << 6));
	}

	for (i = i - 1; i >= 0; i--) {
		if (map[i] == 0) {
			continue;
		}
		return (int)((i << 6) + bit_first(map[i]));
	}

	return -1;
}

inline static int
bitmap_lsb_next(const bitmap_t *__header_indexable map, uint nbits, uint prev)
{
	if ((prev + 1) >= nbits) {
		return -1;
	}

	uint64_t i = bitmap_index(prev + 1);
	uint b = bits((prev + 1), 5, 0) - 1;
	int32_t res = lsb_next((uint64_t)map[i], (int)b);
	if (res >= 0) {
		return (int)((uint64_t)res + (i << 6));
	}

	for (i = i + 1; i <= bitmap_index(nbits - 1); i++) {
		if (map[i] == 0) {
			continue;
		}
		return (int)((i << 6) + (uint64_t)lsb_first(map[i]));
	}

	return -1;
}

inline static uint
bitmap_count(const bitmap_t *__header_indexable map, uint nbits)
{
	uint res = 0;
	for (uint i = 0; i <= bitmap_index(nbits - 1); i++) {
		res += (uint)bit_count(map[i]);
	}
	return res;
}


#endif