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