File: mask_inline.h

package info (click to toggle)
alsa-lib 1.1.3-5
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 8,656 kB
  • sloc: ansic: 84,132; sh: 11,192; makefile: 711
file content (299 lines) | stat: -rw-r--r-- 6,911 bytes parent folder | download | duplicates (7)
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
/*
 *  Mask inlines
 *  Copyright (c) 2000 by Abramo Bagnara <abramo@alsa-project.org>
 *
 *
 *   This library is free software; you can redistribute it and/or modify
 *   it under the terms of the GNU Lesser General Public License as
 *   published by the Free Software Foundation; either version 2.1 of
 *   the License, or (at your option) any later version.
 *
 *   This program is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU Lesser General Public License for more details.
 *
 *   You should have received a copy of the GNU Lesser General Public
 *   License along with this library; if not, write to the Free Software
 *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
 *
 */
  
#include <sys/types.h>

#define MASK_INLINE static inline

#define MASK_MAX SND_MASK_MAX
#define MASK_SIZE (MASK_MAX / 32)

#define MASK_OFS(i)	((i) >> 5)
#define MASK_BIT(i)	(1U << ((i) & 31))

MASK_INLINE unsigned int ld2(u_int32_t v)
{
        unsigned r = 0;

        if (v >= 0x10000) {
                v >>= 16;
                r += 16;
        }
        if (v >= 0x100) {
                v >>= 8;
                r += 8;
        }
        if (v >= 0x10) {
                v >>= 4;
                r += 4;
        }
        if (v >= 4) {
                v >>= 2;
                r += 2;
        }
        if (v >= 2)
                r++;
        return r;
}

MASK_INLINE unsigned int hweight32(u_int32_t v)
{
        v = (v & 0x55555555) + ((v >> 1) & 0x55555555);
        v = (v & 0x33333333) + ((v >> 2) & 0x33333333);
        v = (v & 0x0F0F0F0F) + ((v >> 4) & 0x0F0F0F0F);
        v = (v & 0x00FF00FF) + ((v >> 8) & 0x00FF00FF);
        return (v & 0x0000FFFF) + ((v >> 16) & 0x0000FFFF);
}

MASK_INLINE size_t snd_mask_sizeof(void)
{
	return sizeof(snd_mask_t);
}

MASK_INLINE void snd_mask_none(snd_mask_t *mask)
{
	memset(mask, 0, sizeof(*mask));
}

MASK_INLINE void snd_mask_any(snd_mask_t *mask)
{
	memset(mask, 0xff, MASK_SIZE * sizeof(u_int32_t));
}

MASK_INLINE int snd_mask_empty(const snd_mask_t *mask)
{
	int i;
	for (i = 0; i < MASK_SIZE; i++)
		if (mask->bits[i])
			return 0;
	return 1;
}

MASK_INLINE int snd_mask_full(const snd_mask_t *mask)
{
	int i;
	for (i = 0; i < MASK_SIZE; i++)
		if (mask->bits[i] != 0xffffffff)
			return 0;
	return 1;
}

MASK_INLINE unsigned int snd_mask_count(const snd_mask_t *mask)
{
	int i, w = 0;
	for (i = 0; i < MASK_SIZE; i++)
		w += hweight32(mask->bits[i]);
	return w;
}

MASK_INLINE unsigned int snd_mask_min(const snd_mask_t *mask)
{
	int i;
	assert(!snd_mask_empty(mask));
	for (i = 0; i < MASK_SIZE; i++) {
		if (mask->bits[i])
			return ffs(mask->bits[i]) - 1 + (i << 5);
	}
	return 0;
}

MASK_INLINE unsigned int snd_mask_max(const snd_mask_t *mask)
{
	int i;
	assert(!snd_mask_empty(mask));
	for (i = MASK_SIZE - 1; i >= 0; i--) {
		if (mask->bits[i])
			return ld2(mask->bits[i]) + (i << 5);
	}
	return 0;
}

MASK_INLINE void snd_mask_set(snd_mask_t *mask, unsigned int val)
{
	assert(val <= SND_MASK_MAX);
	mask->bits[MASK_OFS(val)] |= MASK_BIT(val);
}

MASK_INLINE void snd_mask_reset(snd_mask_t *mask, unsigned int val)
{
	assert(val <= SND_MASK_MAX);
	mask->bits[MASK_OFS(val)] &= ~MASK_BIT(val);
}

MASK_INLINE void snd_mask_set_range(snd_mask_t *mask, unsigned int from, unsigned int to)
{
	unsigned int i;
	assert(to <= SND_MASK_MAX && from <= to);
	for (i = from; i <= to; i++)
		mask->bits[MASK_OFS(i)] |= MASK_BIT(i);
}

MASK_INLINE void snd_mask_reset_range(snd_mask_t *mask, unsigned int from, unsigned int to)
{
	unsigned int i;
	assert(to <= SND_MASK_MAX && from <= to);
	for (i = from; i <= to; i++)
		mask->bits[MASK_OFS(i)] &= ~MASK_BIT(i);
}

MASK_INLINE void snd_mask_leave(snd_mask_t *mask, unsigned int val)
{
	unsigned int v;
	assert(val <= SND_MASK_MAX);
	v = mask->bits[MASK_OFS(val)] & MASK_BIT(val);
	snd_mask_none(mask);
	mask->bits[MASK_OFS(val)] = v;
}

MASK_INLINE void snd_mask_intersect(snd_mask_t *mask, const snd_mask_t *v)
{
	int i;
	for (i = 0; i < MASK_SIZE; i++)
		mask->bits[i] &= v->bits[i];
}

MASK_INLINE void snd_mask_union(snd_mask_t *mask, const snd_mask_t *v)
{
	int i;
	for (i = 0; i < MASK_SIZE; i++)
		mask->bits[i] |= v->bits[i];
}

MASK_INLINE int snd_mask_eq(const snd_mask_t *mask, const snd_mask_t *v)
{
	return ! memcmp(mask, v, MASK_SIZE * 4);
}

MASK_INLINE void snd_mask_copy(snd_mask_t *mask, const snd_mask_t *v)
{
	*mask = *v;
}

MASK_INLINE int snd_mask_test(const snd_mask_t *mask, unsigned int val)
{
	assert(val <= SND_MASK_MAX);
	return mask->bits[MASK_OFS(val)] & MASK_BIT(val);
}

MASK_INLINE int snd_mask_single(const snd_mask_t *mask)
{
	int i, c = 0;
	assert(!snd_mask_empty(mask));
	for (i = 0; i < MASK_SIZE; i++) {
		if (! mask->bits[i])
			continue;
		if (mask->bits[i] & (mask->bits[i] - 1))
			return 0;
		if (c)
			return 0;
		c++;
	}
	return 1;
}

MASK_INLINE int snd_mask_refine(snd_mask_t *mask, const snd_mask_t *v)
{
	snd_mask_t old;
	if (snd_mask_empty(mask))
		return -ENOENT;
	snd_mask_copy(&old, mask);
	snd_mask_intersect(mask, v);
	if (snd_mask_empty(mask))
		return -EINVAL;
	return !snd_mask_eq(mask, &old);
}

MASK_INLINE int snd_mask_refine_first(snd_mask_t *mask)
{
	if (snd_mask_empty(mask))
		return -ENOENT;
	if (snd_mask_single(mask))
		return 0;
	snd_mask_leave(mask, snd_mask_min(mask));
	return 1;
}

MASK_INLINE int snd_mask_refine_last(snd_mask_t *mask)
{
	if (snd_mask_empty(mask))
		return -ENOENT;
	if (snd_mask_single(mask))
		return 0;
	snd_mask_leave(mask, snd_mask_max(mask));
	return 1;
}

MASK_INLINE int snd_mask_refine_min(snd_mask_t *mask, unsigned int val)
{
	if (snd_mask_empty(mask))
		return -ENOENT;
	if (snd_mask_min(mask) >= val)
		return 0;
	snd_mask_reset_range(mask, 0, val - 1);
	if (snd_mask_empty(mask))
		return -EINVAL;
	return 1;
}

MASK_INLINE int snd_mask_refine_max(snd_mask_t *mask, unsigned int val)
{
	if (snd_mask_empty(mask))
		return -ENOENT;
	if (snd_mask_max(mask) <= val)
		return 0;
	snd_mask_reset_range(mask, val + 1, SND_MASK_MAX);
	if (snd_mask_empty(mask))
		return -EINVAL;
	return 1;
}

MASK_INLINE int snd_mask_refine_set(snd_mask_t *mask, unsigned int val)
{
	int changed;
	if (snd_mask_empty(mask))
		return -ENOENT;
	changed = !snd_mask_single(mask);
	snd_mask_leave(mask, val);
	if (snd_mask_empty(mask))
		return -EINVAL;
	return changed;
}

MASK_INLINE int snd_mask_value(const snd_mask_t *mask)
{
	assert(!snd_mask_empty(mask));
	return snd_mask_min(mask);
}

MASK_INLINE int snd_mask_always_eq(const snd_mask_t *m1, const snd_mask_t *m2)
{
	return snd_mask_single(m1) && snd_mask_single(m2) &&
		snd_mask_value(m1) == snd_mask_value(m2);
}

MASK_INLINE int snd_mask_never_eq(const snd_mask_t *m1, const snd_mask_t *m2)
{
	int i;
	for (i = 0; i < MASK_SIZE; i++)
		if (m1->bits[i] & m2->bits[i])
			return 0;
	return 1;
}