summaryrefslogtreecommitdiff
path: root/sys/dev/pci/drm/include/linux/bitmap.h
blob: 5c8572f4442f426d8f7c58d029eb55453c1ec2fa (plain)
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
/*	$OpenBSD: bitmap.h,v 1.7 2024/03/20 02:51:06 jsg Exp $	*/
/*
 * Copyright (c) 2013, 2014, 2015 Mark Kettenis
 *
 * Permission to use, copy, modify, and distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 *
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 */

#ifndef _LINUX_BITMAP_H
#define _LINUX_BITMAP_H

#include <linux/align.h>
#include <linux/bitops.h>
#include <linux/string.h>

#define bitmap_empty(p, n)	(find_first_bit(p, n) == n)

static inline void
bitmap_set(void *p, int b, u_int n)
{
	u_int end = b + n;

	for (; b < end; b++)
		__set_bit(b, p);
}

static inline void
bitmap_clear(void *p, int b, u_int n)
{
	u_int end = b + n;

	for (; b < end; b++)
		__clear_bit(b, p);
}

static inline void
bitmap_zero(void *p, u_int n)
{
	u_int *ptr = p;
	u_int b;

	for (b = 0; b < n; b += 32)
		ptr[b >> 5] = 0;
}

static inline void
bitmap_fill(void *p, u_int n)
{
	u_int *ptr = p;
	u_int b;

	for (b = 0; b < n; b += 32)
		ptr[b >> 5] = 0xffffffff;
}

static inline void
bitmap_or(void *d, void *s1, void *s2, u_int n)
{
	u_int *dst = d;
	u_int *src1 = s1;
	u_int *src2 = s2;
	u_int b;

	for (b = 0; b < n; b += 32)
		dst[b >> 5] = src1[b >> 5] | src2[b >> 5];
}

static inline void
bitmap_andnot(void *d, void *s1, void *s2, u_int n)
{
	u_int *dst = d;
	u_int *src1 = s1;
	u_int *src2 = s2;
	u_int b;

	for (b = 0; b < n; b += 32)
		dst[b >> 5] = src1[b >> 5] & ~src2[b >> 5];
}

static inline void
bitmap_complement(void *d, void *s, u_int n)
{
	u_int *dst = d;
	u_int *src = s;
	u_int b;

	for (b = 0; b < n; b += 32)
		dst[b >> 5] = ~src[b >> 5];
}

static inline bool
bitmap_intersects(const void *s1, const void *s2, u_int n)
{
	const u_int *b1 = s1;
	const u_int *b2 = s2;
	u_int b;

	for (b = 0; b < n; b += 32)
		if (b1[b >> 5] & b2[b >> 5])
			return true;
	if ((n % 32) != 0)
		if ((b1[n >> 5] & b2[b >> 5]) & (0xffffffff >> (32 - (n % 32))))
			return true;

	return false;
}

static inline void
bitmap_copy(void *d, void *s, u_int n)
{
	u_int *dst = d;
	u_int *src = s;
	u_int b;

	for (b = 0; b < n; b += 32)
		dst[b >> 5] = src[b >> 5];
}

static inline void
bitmap_to_arr32(void *d, const unsigned long *src, u_int n)
{
	u_int *dst = d;
	u_int b;

#ifdef __LP64__
	for (b = 0; b < n; b += 32) {
		dst[b >> 5] = src[b >> 6] & 0xffffffff;
		b += 32;
		if (b < n)
			dst[b >> 5] = src[b >> 6] >> 32;
	}
#else
	bitmap_copy(d, src, n);
#endif
	if ((n % 32) != 0)
		dst[n >> 5] &= (0xffffffff >> (32 - (n % 32)));
}

static inline void
bitmap_from_arr32(unsigned long *dst, const void *s, u_int n)
{
	const u_int *src = s;
	u_int b;

#ifdef __LP64__
	for (b = 0; b < n; b += 32) {
		dst[b >> 6] = src[b >> 5];
		b += 32;
		if (b < n)
			dst[b >> 6] |= ((unsigned long)src[b >> 5]) << 32;
	}
	if ((n % 64) != 0)
		dst[n >> 6] &= (0xffffffffffffffffUL >> (64 - (n % 64)));
#else
	bitmap_copy(dst, s, n);
	if ((n % 32) != 0)
		dst[n >> 5] &=  (0xffffffff >> (32 - (n % 32)));
#endif
}

static inline int
bitmap_weight(const void *p, u_int n)
{
	const u_int *ptr = p;
	u_int b;
	int sum = 0;

	for (b = 0; b < n; b += 32)
		sum += hweight32(ptr[b >> 5]);
	return sum;
}

static inline int
bitmap_find_free_region(void *p, u_int n, int o)
{
	int b;

	KASSERT(o == 0);
	b = find_first_zero_bit(p, n);
	if (b == n)
		return -ENOMEM;
	__set_bit(b, p);
	return b;
}

static inline void
bitmap_release_region(void *p, u_int b, int o)
{
	KASSERT(o == 0);
	__clear_bit(b, p);
}

void *bitmap_zalloc(u_int, gfp_t);
void bitmap_free(void *);

#endif