1 #ifndef _LINUX_BITOPS_H
2 #define _LINUX_BITOPS_H
6 * ffs: find first bit set. This is defined the same way as
7 * the libc and compiler builtin ffs routines, therefore
8 * differs in spirit from the above ffz (man ffs).
11 static inline int generic_ffs(int x
)
41 * fls: find last bit set.
44 static __inline__
int generic_fls(int x
)
50 if (!(x
& 0xffff0000u
)) {
54 if (!(x
& 0xff000000u
)) {
58 if (!(x
& 0xf0000000u
)) {
62 if (!(x
& 0xc0000000u
)) {
66 if (!(x
& 0x80000000u
)) {
74 * Include this here because some architectures need generic_ffs/fls in
77 #include <asm/bitops.h>
79 static __inline__
int get_bitmask_order(unsigned int count
)
84 return order
; /* We could be slightly more clever with -1 here... */
87 static __inline__
int get_count_order(unsigned int count
)
91 order
= fls(count
) - 1;
92 if (count
& (count
- 1))
98 * hweightN: returns the hamming weight (i.e. the number
99 * of bits set) of a N-bit word
102 static inline unsigned int generic_hweight32(unsigned int w
)
104 unsigned int res
= (w
& 0x55555555) + ((w
>> 1) & 0x55555555);
105 res
= (res
& 0x33333333) + ((res
>> 2) & 0x33333333);
106 res
= (res
& 0x0F0F0F0F) + ((res
>> 4) & 0x0F0F0F0F);
107 res
= (res
& 0x00FF00FF) + ((res
>> 8) & 0x00FF00FF);
108 return (res
& 0x0000FFFF) + ((res
>> 16) & 0x0000FFFF);
111 static inline unsigned int generic_hweight16(unsigned int w
)
113 unsigned int res
= (w
& 0x5555) + ((w
>> 1) & 0x5555);
114 res
= (res
& 0x3333) + ((res
>> 2) & 0x3333);
115 res
= (res
& 0x0F0F) + ((res
>> 4) & 0x0F0F);
116 return (res
& 0x00FF) + ((res
>> 8) & 0x00FF);
119 static inline unsigned int generic_hweight8(unsigned int w
)
121 unsigned int res
= (w
& 0x55) + ((w
>> 1) & 0x55);
122 res
= (res
& 0x33) + ((res
>> 2) & 0x33);
123 return (res
& 0x0F) + ((res
>> 4) & 0x0F);
126 static inline unsigned long generic_hweight64(__u64 w
)
128 #if BITS_PER_LONG < 64
129 return generic_hweight32((unsigned int)(w
>> 32)) +
130 generic_hweight32((unsigned int)w
);
133 res
= (w
& 0x5555555555555555ul
) + ((w
>> 1) & 0x5555555555555555ul
);
134 res
= (res
& 0x3333333333333333ul
) + ((res
>> 2) & 0x3333333333333333ul
);
135 res
= (res
& 0x0F0F0F0F0F0F0F0Ful
) + ((res
>> 4) & 0x0F0F0F0F0F0F0F0Ful
);
136 res
= (res
& 0x00FF00FF00FF00FFul
) + ((res
>> 8) & 0x00FF00FF00FF00FFul
);
137 res
= (res
& 0x0000FFFF0000FFFFul
) + ((res
>> 16) & 0x0000FFFF0000FFFFul
);
138 return (res
& 0x00000000FFFFFFFFul
) + ((res
>> 32) & 0x00000000FFFFFFFFul
);
142 static inline unsigned long hweight_long(unsigned long w
)
144 return sizeof(w
) == 4 ? generic_hweight32(w
) : generic_hweight64(w
);
148 * rol32 - rotate a 32-bit value left
150 * @word: value to rotate
151 * @shift: bits to roll
153 static inline __u32
rol32(__u32 word
, unsigned int shift
)
155 return (word
<< shift
) | (word
>> (32 - shift
));
159 * ror32 - rotate a 32-bit value right
161 * @word: value to rotate
162 * @shift: bits to roll
164 static inline __u32
ror32(__u32 word
, unsigned int shift
)
166 return (word
>> shift
) | (word
<< (32 - shift
));