xref: /openbmc/u-boot/include/linux/bitops.h (revision fea7f3aa)
1 #ifndef _LINUX_BITOPS_H
2 #define _LINUX_BITOPS_H
3 
4 #include <asm/types.h>
5 
6 #define BIT(nr)			(1UL << (nr))
7 #define BIT_MASK(nr)		(1UL << ((nr) % BITS_PER_LONG))
8 #define BIT_WORD(nr)		((nr) / BITS_PER_LONG)
9 
10 /*
11  * Create a contiguous bitmask starting at bit position @l and ending at
12  * position @h. For example
13  * GENMASK_ULL(39, 21) gives us the 64bit vector 0x000000ffffe00000.
14  */
15 #define GENMASK(h, l) \
16 	(((~0UL) << (l)) & (~0UL >> (BITS_PER_LONG - 1 - (h))))
17 
18 #define GENMASK_ULL(h, l) \
19 	(((~0ULL) << (l)) & (~0ULL >> (BITS_PER_LONG_LONG - 1 - (h))))
20 
21 /*
22  * ffs: find first bit set. This is defined the same way as
23  * the libc and compiler builtin ffs routines, therefore
24  * differs in spirit from the above ffz (man ffs).
25  */
26 
27 static inline int generic_ffs(int x)
28 {
29 	int r = 1;
30 
31 	if (!x)
32 		return 0;
33 	if (!(x & 0xffff)) {
34 		x >>= 16;
35 		r += 16;
36 	}
37 	if (!(x & 0xff)) {
38 		x >>= 8;
39 		r += 8;
40 	}
41 	if (!(x & 0xf)) {
42 		x >>= 4;
43 		r += 4;
44 	}
45 	if (!(x & 3)) {
46 		x >>= 2;
47 		r += 2;
48 	}
49 	if (!(x & 1)) {
50 		x >>= 1;
51 		r += 1;
52 	}
53 	return r;
54 }
55 
56 /**
57  * fls - find last (most-significant) bit set
58  * @x: the word to search
59  *
60  * This is defined the same way as ffs.
61  * Note fls(0) = 0, fls(1) = 1, fls(0x80000000) = 32.
62  */
63 static inline int generic_fls(int x)
64 {
65 	int r = 32;
66 
67 	if (!x)
68 		return 0;
69 	if (!(x & 0xffff0000u)) {
70 		x <<= 16;
71 		r -= 16;
72 	}
73 	if (!(x & 0xff000000u)) {
74 		x <<= 8;
75 		r -= 8;
76 	}
77 	if (!(x & 0xf0000000u)) {
78 		x <<= 4;
79 		r -= 4;
80 	}
81 	if (!(x & 0xc0000000u)) {
82 		x <<= 2;
83 		r -= 2;
84 	}
85 	if (!(x & 0x80000000u)) {
86 		x <<= 1;
87 		r -= 1;
88 	}
89 	return r;
90 }
91 
92 
93 /*
94  * hweightN: returns the hamming weight (i.e. the number
95  * of bits set) of a N-bit word
96  */
97 
98 static inline unsigned int generic_hweight32(unsigned int w)
99 {
100 	unsigned int res = (w & 0x55555555) + ((w >> 1) & 0x55555555);
101 	res = (res & 0x33333333) + ((res >> 2) & 0x33333333);
102 	res = (res & 0x0F0F0F0F) + ((res >> 4) & 0x0F0F0F0F);
103 	res = (res & 0x00FF00FF) + ((res >> 8) & 0x00FF00FF);
104 	return (res & 0x0000FFFF) + ((res >> 16) & 0x0000FFFF);
105 }
106 
107 static inline unsigned int generic_hweight16(unsigned int w)
108 {
109 	unsigned int res = (w & 0x5555) + ((w >> 1) & 0x5555);
110 	res = (res & 0x3333) + ((res >> 2) & 0x3333);
111 	res = (res & 0x0F0F) + ((res >> 4) & 0x0F0F);
112 	return (res & 0x00FF) + ((res >> 8) & 0x00FF);
113 }
114 
115 static inline unsigned int generic_hweight8(unsigned int w)
116 {
117 	unsigned int res = (w & 0x55) + ((w >> 1) & 0x55);
118 	res = (res & 0x33) + ((res >> 2) & 0x33);
119 	return (res & 0x0F) + ((res >> 4) & 0x0F);
120 }
121 
122 #include <asm/bitops.h>
123 
124 /* linux/include/asm-generic/bitops/non-atomic.h */
125 
126 #ifndef PLATFORM__SET_BIT
127 # define __set_bit generic_set_bit
128 #endif
129 
130 #ifndef PLATFORM__CLEAR_BIT
131 # define __clear_bit generic_clear_bit
132 #endif
133 
134 #ifndef PLATFORM_FFS
135 # define ffs generic_ffs
136 #endif
137 
138 #ifndef PLATFORM_FLS
139 # define fls generic_fls
140 #endif
141 
142 /**
143  * __set_bit - Set a bit in memory
144  * @nr: the bit to set
145  * @addr: the address to start counting from
146  *
147  * Unlike set_bit(), this function is non-atomic and may be reordered.
148  * If it's called on the same region of memory simultaneously, the effect
149  * may be that only one operation succeeds.
150  */
151 static inline void generic_set_bit(int nr, volatile unsigned long *addr)
152 {
153 	unsigned long mask = BIT_MASK(nr);
154 	unsigned long *p = ((unsigned long *)addr) + BIT_WORD(nr);
155 
156 	*p  |= mask;
157 }
158 
159 static inline void generic_clear_bit(int nr, volatile unsigned long *addr)
160 {
161 	unsigned long mask = BIT_MASK(nr);
162 	unsigned long *p = ((unsigned long *)addr) + BIT_WORD(nr);
163 
164 	*p &= ~mask;
165 }
166 
167 #endif
168