1 #ifndef _LINUX_BITOPS_H
2 #define _LINUX_BITOPS_H
6 #define BIT(nr) (1UL << (nr))
9 * ffs: find first bit set. This is defined the same way as
10 * the libc and compiler builtin ffs routines, therefore
11 * differs in spirit from the above ffz (man ffs).
14 static inline int generic_ffs(int x)
44 * fls - find last (most-significant) bit set
45 * @x: the word to search
47 * This is defined the same way as ffs.
48 * Note fls(0) = 0, fls(1) = 1, fls(0x80000000) = 32.
50 static inline int generic_fls(int x)
56 if (!(x & 0xffff0000u)) {
60 if (!(x & 0xff000000u)) {
64 if (!(x & 0xf0000000u)) {
68 if (!(x & 0xc0000000u)) {
72 if (!(x & 0x80000000u)) {
81 * hweightN: returns the hamming weight (i.e. the number
82 * of bits set) of a N-bit word
85 static inline unsigned int generic_hweight32(unsigned int w)
87 unsigned int res = (w & 0x55555555) + ((w >> 1) & 0x55555555);
88 res = (res & 0x33333333) + ((res >> 2) & 0x33333333);
89 res = (res & 0x0F0F0F0F) + ((res >> 4) & 0x0F0F0F0F);
90 res = (res & 0x00FF00FF) + ((res >> 8) & 0x00FF00FF);
91 return (res & 0x0000FFFF) + ((res >> 16) & 0x0000FFFF);
94 static inline unsigned int generic_hweight16(unsigned int w)
96 unsigned int res = (w & 0x5555) + ((w >> 1) & 0x5555);
97 res = (res & 0x3333) + ((res >> 2) & 0x3333);
98 res = (res & 0x0F0F) + ((res >> 4) & 0x0F0F);
99 return (res & 0x00FF) + ((res >> 8) & 0x00FF);
102 static inline unsigned int generic_hweight8(unsigned int w)
104 unsigned int res = (w & 0x55) + ((w >> 1) & 0x55);
105 res = (res & 0x33) + ((res >> 2) & 0x33);
106 return (res & 0x0F) + ((res >> 4) & 0x0F);
109 #define BIT_MASK(nr) (1UL << ((nr) % BITS_PER_LONG))
110 #define BIT_WORD(nr) ((nr) / BITS_PER_LONG)
112 #include <asm/bitops.h>
114 /* linux/include/asm-generic/bitops/non-atomic.h */
116 #ifndef PLATFORM__SET_BIT
117 # define __set_bit generic_set_bit
120 #ifndef PLATFORM__CLEAR_BIT
121 # define __clear_bit generic_clear_bit
125 # define ffs generic_ffs
129 # define fls generic_fls
133 * __set_bit - Set a bit in memory
134 * @nr: the bit to set
135 * @addr: the address to start counting from
137 * Unlike set_bit(), this function is non-atomic and may be reordered.
138 * If it's called on the same region of memory simultaneously, the effect
139 * may be that only one operation succeeds.
141 static inline void generic_set_bit(int nr, volatile unsigned long *addr)
143 unsigned long mask = BIT_MASK(nr);
144 unsigned long *p = ((unsigned long *)addr) + BIT_WORD(nr);
149 static inline void generic_clear_bit(int nr, volatile unsigned long *addr)
151 unsigned long mask = BIT_MASK(nr);
152 unsigned long *p = ((unsigned long *)addr) + BIT_WORD(nr);