1 /*
2 * Copyright © 2008-2011 Kristian Høgsberg
3 * Copyright © 2011 Intel Corporation
4 * Copyright © 2013-2015 Red Hat, Inc.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 * and/or sell copies of the Software, and to permit persons to whom the
11 * Software is furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice (including the next
14 * paragraph) shall be included in all copies or substantial portions of the
15 * Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
22 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
23 * DEALINGS IN THE SOFTWARE.
24 */
25
26 #pragma once
27
28 #include "config.h"
29
30 #include <assert.h>
31 #include <stdbool.h>
32 #include <stddef.h>
33
34 #define bit(x_) (1UL << (x_))
35 #define NBITS(b) (b * 8)
36 #define LONG_BITS (sizeof(long) * 8)
37 #define NLONGS(x) (((x) + LONG_BITS - 1) / LONG_BITS)
38 #define NCHARS(x) ((size_t)(((x) + 7) / 8))
39
40
41 /* This bitfield helper implementation is taken from from libevdev-util.h,
42 * except that it has been modified to work with arrays of unsigned chars
43 */
44
45 static inline bool
bit_is_set(const unsigned char * array,int bit)46 bit_is_set(const unsigned char *array, int bit)
47 {
48 return !!(array[bit / 8] & (1 << (bit % 8)));
49 }
50
51 static inline void
set_bit(unsigned char * array,int bit)52 set_bit(unsigned char *array, int bit)
53 {
54 array[bit / 8] |= (1 << (bit % 8));
55 }
56
57 static inline void
clear_bit(unsigned char * array,int bit)58 clear_bit(unsigned char *array, int bit)
59 {
60 array[bit / 8] &= ~(1 << (bit % 8));
61 }
62
63 static inline bool
long_bit_is_set(const unsigned long * array,int bit)64 long_bit_is_set(const unsigned long *array, int bit)
65 {
66 return !!(array[bit / LONG_BITS] & (1ULL << (bit % LONG_BITS)));
67 }
68
69 static inline void
long_set_bit(unsigned long * array,int bit)70 long_set_bit(unsigned long *array, int bit)
71 {
72 array[bit / LONG_BITS] |= (1ULL << (bit % LONG_BITS));
73 }
74
75 static inline void
long_clear_bit(unsigned long * array,int bit)76 long_clear_bit(unsigned long *array, int bit)
77 {
78 array[bit / LONG_BITS] &= ~(1ULL << (bit % LONG_BITS));
79 }
80
81 static inline void
long_set_bit_state(unsigned long * array,int bit,int state)82 long_set_bit_state(unsigned long *array, int bit, int state)
83 {
84 if (state)
85 long_set_bit(array, bit);
86 else
87 long_clear_bit(array, bit);
88 }
89
90 static inline bool
long_any_bit_set(unsigned long * array,size_t size)91 long_any_bit_set(unsigned long *array, size_t size)
92 {
93 unsigned long i;
94
95 assert(size > 0);
96
97 for (i = 0; i < size; i++)
98 if (array[i] != 0)
99 return true;
100 return false;
101 }
102