1 #ifndef _BITOPS_H_ 2 #define _BITOPS_H_ 3 4 /* 5 * Adapted from 6 * include/linux/bitops.h 7 * 8 * Copyright (C) 2017, Red Hat Inc, Andrew Jones <drjones@redhat.com> 9 * 10 * This work is licensed under the terms of the GNU GPL, version 2. 11 */ 12 13 #define BITS_PER_LONG_LONG 64 14 #define BIT(nr) (1UL << (nr)) 15 #define BIT_ULL(nr) (1ULL << (nr)) 16 #define BIT_MASK(nr) (1UL << ((nr) % BITS_PER_LONG)) 17 #define BIT_WORD(nr) ((nr) / BITS_PER_LONG) 18 #define BIT_ULL_MASK(nr) (1ULL << ((nr) % BITS_PER_LONG_LONG)) 19 #define BIT_ULL_WORD(nr) ((nr) / BITS_PER_LONG_LONG) 20 #define BITS_PER_BYTE 8 21 #define BITS_TO_LONGS(nr) DIV_ROUND_UP(nr, BITS_PER_BYTE * sizeof(long)) 22 23 #include <asm/bitops.h> 24 25 /* 26 * Create a contiguous bitmask starting at bit position @l and ending at 27 * position @h. For example 28 * GENMASK_ULL(39, 21) gives us the 64bit vector 0x000000ffffe00000. 29 */ 30 #define GENMASK(h, l) \ 31 (((~0UL) << (l)) & (~0UL >> (BITS_PER_LONG - 1 - (h)))) 32 33 #define GENMASK_ULL(h, l) \ 34 (((~0ULL) << (l)) & (~0ULL >> (BITS_PER_LONG_LONG - 1 - (h)))) 35 36 #ifndef HAVE_BUILTIN_FLS 37 static inline unsigned long fls(unsigned long word) 38 { 39 int num = BITS_PER_LONG - 1; 40 41 #if BITS_PER_LONG == 64 42 if (!(word & (~0ul << 32))) { 43 num -= 32; 44 word <<= 32; 45 } 46 #endif 47 if (!(word & (~0ul << (BITS_PER_LONG-16)))) { 48 num -= 16; 49 word <<= 16; 50 } 51 if (!(word & (~0ul << (BITS_PER_LONG-8)))) { 52 num -= 8; 53 word <<= 8; 54 } 55 if (!(word & (~0ul << (BITS_PER_LONG-4)))) { 56 num -= 4; 57 word <<= 4; 58 } 59 if (!(word & (~0ul << (BITS_PER_LONG-2)))) { 60 num -= 2; 61 word <<= 2; 62 } 63 if (!(word & (~0ul << (BITS_PER_LONG-1)))) 64 num -= 1; 65 return num; 66 } 67 #else 68 static inline unsigned long fls(unsigned long word) 69 { 70 return BITS_PER_LONG - __builtin_clzl(word) - 1; 71 } 72 #endif 73 74 #endif 75