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 /* This bitfield helper implementation is taken from from libevdev-util.h,
41 * except that it has been modified to work with arrays of unsigned chars
42 */
43
44 static inline bool
bit_is_set(const unsigned char * array,int bit)45 bit_is_set(const unsigned char *array, int bit)
46 {
47 return !!(array[bit / 8] & (1 << (bit % 8)));
48 }
49
50 static inline void
set_bit(unsigned char * array,int bit)51 set_bit(unsigned char *array, int bit)
52 {
53 array[bit / 8] |= (1 << (bit % 8));
54 }
55
56 static inline void
clear_bit(unsigned char * array,int bit)57 clear_bit(unsigned char *array, int bit)
58 {
59 array[bit / 8] &= ~(1 << (bit % 8));
60 }
61
62 static inline bool
long_bit_is_set(const unsigned long * array,int bit)63 long_bit_is_set(const unsigned long *array, int bit)
64 {
65 return !!(array[bit / LONG_BITS] & (1ULL << (bit % LONG_BITS)));
66 }
67
68 static inline void
long_set_bit(unsigned long * array,int bit)69 long_set_bit(unsigned long *array, int bit)
70 {
71 array[bit / LONG_BITS] |= (1ULL << (bit % LONG_BITS));
72 }
73
74 static inline void
long_clear_bit(unsigned long * array,int bit)75 long_clear_bit(unsigned long *array, int bit)
76 {
77 array[bit / LONG_BITS] &= ~(1ULL << (bit % LONG_BITS));
78 }
79
80 static inline void
long_set_bit_state(unsigned long * array,int bit,int state)81 long_set_bit_state(unsigned long *array, int bit, int state)
82 {
83 if (state)
84 long_set_bit(array, bit);
85 else
86 long_clear_bit(array, bit);
87 }
88
89 static inline bool
long_any_bit_set(unsigned long * array,size_t size)90 long_any_bit_set(unsigned long *array, size_t size)
91 {
92 unsigned long i;
93
94 assert(size > 0);
95
96 for (i = 0; i < size; i++)
97 if (array[i] != 0)
98 return true;
99 return false;
100 }
101