blob: 7d41ae62ccf7ead43fe4a468d6b58624410b7836 [file] [log] [blame]
wdenkb15cbc02000-08-21 15:05:47 +00001#ifndef _LINUX_BITOPS_H
2#define _LINUX_BITOPS_H
3
4
5/*
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).
9 */
10
11static inline int generic_ffs(int x)
12{
13 int r = 1;
14
15 if (!x)
16 return 0;
17 if (!(x & 0xffff)) {
18 x >>= 16;
19 r += 16;
20 }
21 if (!(x & 0xff)) {
22 x >>= 8;
23 r += 8;
24 }
25 if (!(x & 0xf)) {
26 x >>= 4;
27 r += 4;
28 }
29 if (!(x & 3)) {
30 x >>= 2;
31 r += 2;
32 }
33 if (!(x & 1)) {
34 x >>= 1;
35 r += 1;
36 }
37 return r;
38}
39
40/*
41 * hweightN: returns the hamming weight (i.e. the number
42 * of bits set) of a N-bit word
43 */
44
45static inline unsigned int generic_hweight32(unsigned int w)
46{
wdenk8bde7f72003-06-27 21:31:46 +000047 unsigned int res = (w & 0x55555555) + ((w >> 1) & 0x55555555);
48 res = (res & 0x33333333) + ((res >> 2) & 0x33333333);
49 res = (res & 0x0F0F0F0F) + ((res >> 4) & 0x0F0F0F0F);
50 res = (res & 0x00FF00FF) + ((res >> 8) & 0x00FF00FF);
51 return (res & 0x0000FFFF) + ((res >> 16) & 0x0000FFFF);
wdenkb15cbc02000-08-21 15:05:47 +000052}
53
54static inline unsigned int generic_hweight16(unsigned int w)
55{
wdenk8bde7f72003-06-27 21:31:46 +000056 unsigned int res = (w & 0x5555) + ((w >> 1) & 0x5555);
57 res = (res & 0x3333) + ((res >> 2) & 0x3333);
58 res = (res & 0x0F0F) + ((res >> 4) & 0x0F0F);
59 return (res & 0x00FF) + ((res >> 8) & 0x00FF);
wdenkb15cbc02000-08-21 15:05:47 +000060}
61
62static inline unsigned int generic_hweight8(unsigned int w)
63{
wdenk8bde7f72003-06-27 21:31:46 +000064 unsigned int res = (w & 0x55) + ((w >> 1) & 0x55);
65 res = (res & 0x33) + ((res >> 2) & 0x33);
66 return (res & 0x0F) + ((res >> 4) & 0x0F);
wdenkb15cbc02000-08-21 15:05:47 +000067}
68
69#include <asm/bitops.h>
70
71
72#endif