xref: /openbsd-src/sys/dev/pci/drm/include/linux/bitmap.h (revision 99fd087599a8791921855f21bd7e36130f39aadc)
1 /*	$OpenBSD: bitmap.h,v 1.2 2019/07/11 04:26:37 jsg Exp $	*/
2 /*
3  * Copyright (c) 2013, 2014, 2015 Mark Kettenis
4  *
5  * Permission to use, copy, modify, and distribute this software for any
6  * purpose with or without fee is hereby granted, provided that the above
7  * copyright notice and this permission notice appear in all copies.
8  *
9  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16  */
17 
18 #ifndef _LINUX_BITMAP_H
19 #define _LINUX_BITMAP_H
20 
21 #include <linux/bitops.h>
22 #include <linux/string.h>
23 
24 #define bitmap_empty(p, n)	(find_first_bit(p, n) == n)
25 
26 static inline void
27 bitmap_set(void *p, int b, u_int n)
28 {
29 	u_int end = b + n;
30 
31 	for (; b < end; b++)
32 		__set_bit(b, p);
33 }
34 
35 static inline void
36 bitmap_clear(void *p, int b, u_int n)
37 {
38 	u_int end = b + n;
39 
40 	for (; b < end; b++)
41 		__clear_bit(b, p);
42 }
43 
44 static inline void
45 bitmap_zero(void *p, u_int n)
46 {
47 	u_int *ptr = p;
48 	u_int b;
49 
50 	for (b = 0; b < n; b += 32)
51 		ptr[b >> 5] = 0;
52 }
53 
54 static inline void
55 bitmap_or(void *d, void *s1, void *s2, u_int n)
56 {
57 	u_int *dst = d;
58 	u_int *src1 = s1;
59 	u_int *src2 = s2;
60 	u_int b;
61 
62 	for (b = 0; b < n; b += 32)
63 		dst[b >> 5] = src1[b >> 5] | src2[b >> 5];
64 }
65 
66 static inline void
67 bitmap_complement(void *d, void *s, u_int n)
68 {
69 	u_int *dst = d;
70 	u_int *src = s;
71 	u_int b;
72 
73 	for (b = 0; b < n; b += 32)
74 		dst[b >> 5] = ~src[b >> 5];
75 }
76 
77 static inline int
78 bitmap_weight(void *p, u_int n)
79 {
80 	u_int *ptr = p;
81 	u_int b;
82 	int sum = 0;
83 
84 	for (b = 0; b < n; b += 32)
85 		sum += hweight32(ptr[b >> 5]);
86 	return sum;
87 }
88 
89 #endif
90