xref: /openbsd/sys/dev/pci/drm/include/linux/bitmap.h (revision 09467b48)
1 /*	$OpenBSD: bitmap.h,v 1.3 2020/06/08 04:48:14 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_andnot(void *d, void *s1, void *s2, u_int n)
68 {
69 	u_int *dst = d;
70 	u_int *src1 = s1;
71 	u_int *src2 = s2;
72 	u_int b;
73 
74 	for (b = 0; b < n; b += 32)
75 		dst[b >> 5] = src1[b >> 5] & ~src2[b >> 5];
76 }
77 
78 static inline void
79 bitmap_complement(void *d, void *s, u_int n)
80 {
81 	u_int *dst = d;
82 	u_int *src = s;
83 	u_int b;
84 
85 	for (b = 0; b < n; b += 32)
86 		dst[b >> 5] = ~src[b >> 5];
87 }
88 
89 static inline void
90 bitmap_copy(void *d, void *s, u_int n)
91 {
92 	u_int *dst = d;
93 	u_int *src = s;
94 	u_int b;
95 
96 	for (b = 0; b < n; b += 32)
97 		dst[b >> 5] = src[b >> 5];
98 }
99 
100 static inline int
101 bitmap_weight(void *p, u_int n)
102 {
103 	u_int *ptr = p;
104 	u_int b;
105 	int sum = 0;
106 
107 	for (b = 0; b < n; b += 32)
108 		sum += hweight32(ptr[b >> 5]);
109 	return sum;
110 }
111 
112 void *bitmap_zalloc(u_int, gfp_t);
113 void bitmap_free(void *);
114 
115 #endif
116