Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | #ifndef _LINUX_BITOPS_H |
| 2 | #define _LINUX_BITOPS_H |
| 3 | #include <asm/types.h> |
| 4 | |
| 5 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6 | * Include this here because some architectures need generic_ffs/fls in |
| 7 | * scope |
| 8 | */ |
| 9 | #include <asm/bitops.h> |
| 10 | |
| 11 | static __inline__ int get_bitmask_order(unsigned int count) |
| 12 | { |
| 13 | int order; |
| 14 | |
| 15 | order = fls(count); |
| 16 | return order; /* We could be slightly more clever with -1 here... */ |
| 17 | } |
| 18 | |
Siddha, Suresh B | 94605ef | 2005-11-05 17:25:54 +0100 | [diff] [blame] | 19 | static __inline__ int get_count_order(unsigned int count) |
| 20 | { |
| 21 | int order; |
| 22 | |
| 23 | order = fls(count) - 1; |
| 24 | if (count & (count - 1)) |
| 25 | order++; |
| 26 | return order; |
| 27 | } |
| 28 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 29 | static inline unsigned long hweight_long(unsigned long w) |
| 30 | { |
Akinobu Mita | e9bebd6 | 2006-03-26 01:39:55 -0800 | [diff] [blame] | 31 | return sizeof(w) == 4 ? hweight32(w) : hweight64(w); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 32 | } |
| 33 | |
Robert P. J. Day | 45f8bde | 2007-01-26 00:57:09 -0800 | [diff] [blame] | 34 | /** |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 35 | * rol32 - rotate a 32-bit value left |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 36 | * @word: value to rotate |
| 37 | * @shift: bits to roll |
| 38 | */ |
| 39 | static inline __u32 rol32(__u32 word, unsigned int shift) |
| 40 | { |
| 41 | return (word << shift) | (word >> (32 - shift)); |
| 42 | } |
| 43 | |
Robert P. J. Day | 45f8bde | 2007-01-26 00:57:09 -0800 | [diff] [blame] | 44 | /** |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 45 | * ror32 - rotate a 32-bit value right |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 46 | * @word: value to rotate |
| 47 | * @shift: bits to roll |
| 48 | */ |
| 49 | static inline __u32 ror32(__u32 word, unsigned int shift) |
| 50 | { |
| 51 | return (word >> shift) | (word << (32 - shift)); |
| 52 | } |
| 53 | |
Andrew Morton | 962749a | 2006-03-25 03:08:01 -0800 | [diff] [blame] | 54 | static inline unsigned fls_long(unsigned long l) |
| 55 | { |
| 56 | if (sizeof(l) == 4) |
| 57 | return fls(l); |
| 58 | return fls64(l); |
| 59 | } |
| 60 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 61 | #endif |