1 #ifndef _LINUX_BITOPS_H
2 #define _LINUX_BITOPS_H
7 * ffs: find first bit set. This is defined the same way as
8 * the libc and compiler builtin ffs routines, therefore
9 * differs in spirit from the above ffz (man ffs).
12 static inline int generic_ffs(int x)
42 * hweightN: returns the hamming weight (i.e. the number
43 * of bits set) of a N-bit word
46 static inline unsigned int generic_hweight32(unsigned int w)
48 unsigned int res = (w & 0x55555555) + ((w >> 1) & 0x55555555);
49 res = (res & 0x33333333) + ((res >> 2) & 0x33333333);
50 res = (res & 0x0F0F0F0F) + ((res >> 4) & 0x0F0F0F0F);
51 res = (res & 0x00FF00FF) + ((res >> 8) & 0x00FF00FF);
52 return (res & 0x0000FFFF) + ((res >> 16) & 0x0000FFFF);
55 static inline unsigned int generic_hweight16(unsigned int w)
57 unsigned int res = (w & 0x5555) + ((w >> 1) & 0x5555);
58 res = (res & 0x3333) + ((res >> 2) & 0x3333);
59 res = (res & 0x0F0F) + ((res >> 4) & 0x0F0F);
60 return (res & 0x00FF) + ((res >> 8) & 0x00FF);
63 static inline unsigned int generic_hweight8(unsigned int w)
65 unsigned int res = (w & 0x55) + ((w >> 1) & 0x55);
66 res = (res & 0x33) + ((res >> 2) & 0x33);
67 return (res & 0x0F) + ((res >> 4) & 0x0F);
70 #define BIT_MASK(nr) (1UL << ((nr) % BITS_PER_LONG))
71 #define BIT_WORD(nr) ((nr) / BITS_PER_LONG)
73 #include <asm/bitops.h>
75 /* linux/include/asm-generic/bitops/non-atomic.h */
78 # define __set_bit generic_set_bit
82 # define __clear_bit generic_clear_bit
86 * __set_bit - Set a bit in memory
88 * @addr: the address to start counting from
90 * Unlike set_bit(), this function is non-atomic and may be reordered.
91 * If it's called on the same region of memory simultaneously, the effect
92 * may be that only one operation succeeds.
94 static inline void generic_set_bit(int nr, volatile unsigned long *addr)
96 unsigned long mask = BIT_MASK(nr);
97 unsigned long *p = ((unsigned long *)addr) + BIT_WORD(nr);
102 static inline void generic_clear_bit(int nr, volatile unsigned long *addr)
104 unsigned long mask = BIT_MASK(nr);
105 unsigned long *p = ((unsigned long *)addr) + BIT_WORD(nr);