Arnaldo Carvalho de Melo | 88bcea4 | 2014-12-16 10:53:27 -0300 | [diff] [blame] | 1 | #ifndef _TOOLS_LINUX_BITOPS_H_ |
| 2 | #define _TOOLS_LINUX_BITOPS_H_ |
Frederic Weisbecker | 5a116dd | 2009-10-17 17:12:33 +0200 | [diff] [blame] | 3 | |
Arnaldo Carvalho de Melo | 25cd480 | 2015-01-13 10:19:12 -0300 | [diff] [blame] | 4 | #include <asm/types.h> |
Arnaldo Carvalho de Melo | fb72014 | 2010-04-30 19:31:12 -0300 | [diff] [blame] | 5 | #include <linux/kernel.h> |
Arnaldo Carvalho de Melo | a860a60 | 2011-01-22 19:07:36 -0200 | [diff] [blame] | 6 | #include <linux/compiler.h> |
Frederic Weisbecker | 5a116dd | 2009-10-17 17:12:33 +0200 | [diff] [blame] | 7 | |
Irina Tirdea | 3f34f6c | 2012-09-11 01:15:00 +0300 | [diff] [blame] | 8 | #ifndef __WORDSIZE |
| 9 | #define __WORDSIZE (__SIZEOF_LONG__ * 8) |
| 10 | #endif |
| 11 | |
Arnaldo Carvalho de Melo | fb72014 | 2010-04-30 19:31:12 -0300 | [diff] [blame] | 12 | #define BITS_PER_LONG __WORDSIZE |
Arnaldo Carvalho de Melo | 93c49b3 | 2014-12-15 17:08:00 -0300 | [diff] [blame] | 13 | |
| 14 | #define BIT_MASK(nr) (1UL << ((nr) % BITS_PER_LONG)) |
| 15 | #define BIT_WORD(nr) ((nr) / BITS_PER_LONG) |
| 16 | #define BITS_PER_BYTE 8 |
| 17 | #define BITS_TO_LONGS(nr) DIV_ROUND_UP(nr, BITS_PER_BYTE * sizeof(long)) |
| 18 | #define BITS_TO_U64(nr) DIV_ROUND_UP(nr, BITS_PER_BYTE * sizeof(u64)) |
| 19 | #define BITS_TO_U32(nr) DIV_ROUND_UP(nr, BITS_PER_BYTE * sizeof(u32)) |
| 20 | #define BITS_TO_BYTES(nr) DIV_ROUND_UP(nr, BITS_PER_BYTE) |
Paul Mackerras | ee3d250 | 2009-11-24 15:19:43 +1100 | [diff] [blame] | 21 | |
Arnaldo Carvalho de Melo | 25cd480 | 2015-01-13 10:19:12 -0300 | [diff] [blame] | 22 | extern unsigned int __sw_hweight8(unsigned int w); |
| 23 | extern unsigned int __sw_hweight16(unsigned int w); |
| 24 | extern unsigned int __sw_hweight32(unsigned int w); |
| 25 | extern unsigned long __sw_hweight64(__u64 w); |
| 26 | |
Arnaldo Carvalho de Melo | 88bcea4 | 2014-12-16 10:53:27 -0300 | [diff] [blame] | 27 | /* |
| 28 | * Include this here because some architectures need generic_ffs/fls in |
| 29 | * scope |
| 30 | * |
| 31 | * XXX: this needs to be asm/bitops.h, when we get to per arch optimizations |
| 32 | */ |
| 33 | #include <asm-generic/bitops.h> |
| 34 | |
Robert Richter | b1e5a9b | 2011-12-07 10:02:57 +0100 | [diff] [blame] | 35 | #define for_each_set_bit(bit, addr, size) \ |
| 36 | for ((bit) = find_first_bit((addr), (size)); \ |
| 37 | (bit) < (size); \ |
| 38 | (bit) = find_next_bit((addr), (size), (bit) + 1)) |
| 39 | |
| 40 | /* same as for_each_set_bit() but use bit as value to start with */ |
Akinobu Mita | 307b1cd | 2012-03-23 15:02:03 -0700 | [diff] [blame] | 41 | #define for_each_set_bit_from(bit, addr, size) \ |
Robert Richter | b1e5a9b | 2011-12-07 10:02:57 +0100 | [diff] [blame] | 42 | for ((bit) = find_next_bit((addr), (size), (bit)); \ |
| 43 | (bit) < (size); \ |
| 44 | (bit) = find_next_bit((addr), (size), (bit) + 1)) |
| 45 | |
Arnaldo Carvalho de Melo | fb72014 | 2010-04-30 19:31:12 -0300 | [diff] [blame] | 46 | static inline unsigned long hweight_long(unsigned long w) |
| 47 | { |
| 48 | return sizeof(w) == 4 ? hweight32(w) : hweight64(w); |
| 49 | } |
Frederic Weisbecker | 5a116dd | 2009-10-17 17:12:33 +0200 | [diff] [blame] | 50 | |
Arnaldo Carvalho de Melo | afcd4f6 | 2014-12-16 11:26:35 -0300 | [diff] [blame] | 51 | static inline unsigned fls_long(unsigned long l) |
| 52 | { |
| 53 | if (sizeof(l) == 4) |
| 54 | return fls(l); |
| 55 | return fls64(l); |
| 56 | } |
| 57 | |
Frederic Weisbecker | 5a116dd | 2009-10-17 17:12:33 +0200 | [diff] [blame] | 58 | #endif |