blob: 387a81813b9903c480fa2bed141284058d863347 [file] [log] [blame]
wdenkb15cbc02000-08-21 15:05:47 +00001#ifndef _LINUX_BITOPS_H
2#define _LINUX_BITOPS_H
3
Simon Kagstrom13edaf32009-08-24 09:09:50 +02004#include <asm/types.h>
wdenkb15cbc02000-08-21 15:05:47 +00005
6/*
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).
10 */
11
12static inline int generic_ffs(int x)
13{
14 int r = 1;
15
16 if (!x)
17 return 0;
18 if (!(x & 0xffff)) {
19 x >>= 16;
20 r += 16;
21 }
22 if (!(x & 0xff)) {
23 x >>= 8;
24 r += 8;
25 }
26 if (!(x & 0xf)) {
27 x >>= 4;
28 r += 4;
29 }
30 if (!(x & 3)) {
31 x >>= 2;
32 r += 2;
33 }
34 if (!(x & 1)) {
35 x >>= 1;
36 r += 1;
37 }
38 return r;
39}
40
41/*
42 * hweightN: returns the hamming weight (i.e. the number
43 * of bits set) of a N-bit word
44 */
45
46static inline unsigned int generic_hweight32(unsigned int w)
47{
wdenk57b2d802003-06-27 21:31:46 +000048 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);
wdenkb15cbc02000-08-21 15:05:47 +000053}
54
55static inline unsigned int generic_hweight16(unsigned int w)
56{
wdenk57b2d802003-06-27 21:31:46 +000057 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);
wdenkb15cbc02000-08-21 15:05:47 +000061}
62
63static inline unsigned int generic_hweight8(unsigned int w)
64{
wdenk57b2d802003-06-27 21:31:46 +000065 unsigned int res = (w & 0x55) + ((w >> 1) & 0x55);
66 res = (res & 0x33) + ((res >> 2) & 0x33);
67 return (res & 0x0F) + ((res >> 4) & 0x0F);
wdenkb15cbc02000-08-21 15:05:47 +000068}
69
Simon Kagstrom13edaf32009-08-24 09:09:50 +020070#define BIT_MASK(nr) (1UL << ((nr) % BITS_PER_LONG))
71#define BIT_WORD(nr) ((nr) / BITS_PER_LONG)
72
wdenkb15cbc02000-08-21 15:05:47 +000073#include <asm/bitops.h>
74
Simon Kagstrom13edaf32009-08-24 09:09:50 +020075/* linux/include/asm-generic/bitops/non-atomic.h */
76
77#ifndef __set_bit
78# define __set_bit generic_set_bit
79#endif
80
81#ifndef __clear_bit
82# define __clear_bit generic_clear_bit
83#endif
84
85/**
86 * __set_bit - Set a bit in memory
87 * @nr: the bit to set
88 * @addr: the address to start counting from
89 *
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.
93 */
94static inline void generic_set_bit(int nr, volatile unsigned long *addr)
95{
96 unsigned long mask = BIT_MASK(nr);
97 unsigned long *p = ((unsigned long *)addr) + BIT_WORD(nr);
98
99 *p |= mask;
100}
101
102static inline void generic_clear_bit(int nr, volatile unsigned long *addr)
103{
104 unsigned long mask = BIT_MASK(nr);
105 unsigned long *p = ((unsigned long *)addr) + BIT_WORD(nr);
106
107 *p &= ~mask;
108}
wdenkb15cbc02000-08-21 15:05:47 +0000109
110#endif