]> git.sur5r.net Git - u-boot/blob - include/linux/bitops.h
Move __set/clear_bit from ubifs.h to bitops.h
[u-boot] / include / linux / bitops.h
1 #ifndef _LINUX_BITOPS_H
2 #define _LINUX_BITOPS_H
3
4 #include <asm/types.h>
5
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
12 static 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
46 static inline unsigned int generic_hweight32(unsigned int w)
47 {
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);
53 }
54
55 static inline unsigned int generic_hweight16(unsigned int w)
56 {
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);
61 }
62
63 static inline unsigned int generic_hweight8(unsigned int w)
64 {
65         unsigned int res = (w & 0x55) + ((w >> 1) & 0x55);
66         res = (res & 0x33) + ((res >> 2) & 0x33);
67         return (res & 0x0F) + ((res >> 4) & 0x0F);
68 }
69
70 #define BIT_MASK(nr)            (1UL << ((nr) % BITS_PER_LONG))
71 #define BIT_WORD(nr)            ((nr) / BITS_PER_LONG)
72
73 #include <asm/bitops.h>
74
75 /* 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  */
94 static 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
102 static 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 }
109
110 #endif