blob: 1b2e4915a08f26397edd8f69a249e30673b01dbf [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>
Fabio Estevam5e39d082015-11-05 12:43:38 -02005#include <linux/compiler.h>
wdenkb15cbc02000-08-21 15:05:47 +00006
Jagan Teki79d4d5f2015-10-21 17:30:34 +05307#define BIT(nr) (1UL << (nr))
8#define BIT_MASK(nr) (1UL << ((nr) % BITS_PER_LONG))
9#define BIT_WORD(nr) ((nr) / BITS_PER_LONG)
Heiko Schocher69bab552015-09-07 13:43:52 +020010
wdenkb15cbc02000-08-21 15:05:47 +000011/*
Jagan Teki8aceb8f2015-10-21 16:46:51 +053012 * Create a contiguous bitmask starting at bit position @l and ending at
13 * position @h. For example
14 * GENMASK_ULL(39, 21) gives us the 64bit vector 0x000000ffffe00000.
15 */
16#define GENMASK(h, l) \
17 (((~0UL) << (l)) & (~0UL >> (BITS_PER_LONG - 1 - (h))))
18
19#define GENMASK_ULL(h, l) \
20 (((~0ULL) << (l)) & (~0ULL >> (BITS_PER_LONG_LONG - 1 - (h))))
21
22/*
wdenkb15cbc02000-08-21 15:05:47 +000023 * ffs: find first bit set. This is defined the same way as
24 * the libc and compiler builtin ffs routines, therefore
25 * differs in spirit from the above ffz (man ffs).
26 */
27
28static inline int generic_ffs(int x)
29{
30 int r = 1;
31
32 if (!x)
33 return 0;
34 if (!(x & 0xffff)) {
35 x >>= 16;
36 r += 16;
37 }
38 if (!(x & 0xff)) {
39 x >>= 8;
40 r += 8;
41 }
42 if (!(x & 0xf)) {
43 x >>= 4;
44 r += 4;
45 }
46 if (!(x & 3)) {
47 x >>= 2;
48 r += 2;
49 }
50 if (!(x & 1)) {
51 x >>= 1;
52 r += 1;
53 }
54 return r;
55}
56
Simon Kagstrom2258cec2009-08-24 09:10:12 +020057/**
58 * fls - find last (most-significant) bit set
59 * @x: the word to search
60 *
61 * This is defined the same way as ffs.
62 * Note fls(0) = 0, fls(1) = 1, fls(0x80000000) = 32.
63 */
64static inline int generic_fls(int x)
65{
66 int r = 32;
67
68 if (!x)
69 return 0;
70 if (!(x & 0xffff0000u)) {
71 x <<= 16;
72 r -= 16;
73 }
74 if (!(x & 0xff000000u)) {
75 x <<= 8;
76 r -= 8;
77 }
78 if (!(x & 0xf0000000u)) {
79 x <<= 4;
80 r -= 4;
81 }
82 if (!(x & 0xc0000000u)) {
83 x <<= 2;
84 r -= 2;
85 }
86 if (!(x & 0x80000000u)) {
87 x <<= 1;
88 r -= 1;
89 }
90 return r;
91}
92
93
wdenkb15cbc02000-08-21 15:05:47 +000094/*
95 * hweightN: returns the hamming weight (i.e. the number
96 * of bits set) of a N-bit word
97 */
98
99static inline unsigned int generic_hweight32(unsigned int w)
100{
wdenk57b2d802003-06-27 21:31:46 +0000101 unsigned int res = (w & 0x55555555) + ((w >> 1) & 0x55555555);
102 res = (res & 0x33333333) + ((res >> 2) & 0x33333333);
103 res = (res & 0x0F0F0F0F) + ((res >> 4) & 0x0F0F0F0F);
104 res = (res & 0x00FF00FF) + ((res >> 8) & 0x00FF00FF);
105 return (res & 0x0000FFFF) + ((res >> 16) & 0x0000FFFF);
wdenkb15cbc02000-08-21 15:05:47 +0000106}
107
108static inline unsigned int generic_hweight16(unsigned int w)
109{
wdenk57b2d802003-06-27 21:31:46 +0000110 unsigned int res = (w & 0x5555) + ((w >> 1) & 0x5555);
111 res = (res & 0x3333) + ((res >> 2) & 0x3333);
112 res = (res & 0x0F0F) + ((res >> 4) & 0x0F0F);
113 return (res & 0x00FF) + ((res >> 8) & 0x00FF);
wdenkb15cbc02000-08-21 15:05:47 +0000114}
115
116static inline unsigned int generic_hweight8(unsigned int w)
117{
wdenk57b2d802003-06-27 21:31:46 +0000118 unsigned int res = (w & 0x55) + ((w >> 1) & 0x55);
119 res = (res & 0x33) + ((res >> 2) & 0x33);
120 return (res & 0x0F) + ((res >> 4) & 0x0F);
wdenkb15cbc02000-08-21 15:05:47 +0000121}
122
123#include <asm/bitops.h>
124
Simon Kagstrom13edaf32009-08-24 09:09:50 +0200125/* linux/include/asm-generic/bitops/non-atomic.h */
126
Simon Kagstrom95a3c732009-09-17 15:15:52 +0200127#ifndef PLATFORM__SET_BIT
Simon Kagstrom13edaf32009-08-24 09:09:50 +0200128# define __set_bit generic_set_bit
129#endif
130
Simon Kagstrom95a3c732009-09-17 15:15:52 +0200131#ifndef PLATFORM__CLEAR_BIT
Simon Kagstrom13edaf32009-08-24 09:09:50 +0200132# define __clear_bit generic_clear_bit
133#endif
134
Simon Kagstrom95a3c732009-09-17 15:15:52 +0200135#ifndef PLATFORM_FFS
Simon Kagstrom2258cec2009-08-24 09:10:12 +0200136# define ffs generic_ffs
137#endif
138
Simon Kagstrom95a3c732009-09-17 15:15:52 +0200139#ifndef PLATFORM_FLS
Simon Kagstrom2258cec2009-08-24 09:10:12 +0200140# define fls generic_fls
141#endif
142
Fabio Estevam5e39d082015-11-05 12:43:38 -0200143static inline unsigned fls_long(unsigned long l)
144{
145 if (sizeof(l) == 4)
146 return fls(l);
147 return fls64(l);
148}
149
150/**
151 * __ffs64 - find first set bit in a 64 bit word
152 * @word: The 64 bit word
153 *
154 * On 64 bit arches this is a synomyn for __ffs
155 * The result is not defined if no bits are set, so check that @word
156 * is non-zero before calling this.
157 */
158static inline unsigned long __ffs64(u64 word)
159{
160#if BITS_PER_LONG == 32
161 if (((u32)word) == 0UL)
162 return __ffs((u32)(word >> 32)) + 32;
163#elif BITS_PER_LONG != 64
164#error BITS_PER_LONG not 32 or 64
165#endif
166 return __ffs((unsigned long)word);
167}
168
Simon Kagstrom13edaf32009-08-24 09:09:50 +0200169/**
170 * __set_bit - Set a bit in memory
171 * @nr: the bit to set
172 * @addr: the address to start counting from
173 *
174 * Unlike set_bit(), this function is non-atomic and may be reordered.
175 * If it's called on the same region of memory simultaneously, the effect
176 * may be that only one operation succeeds.
177 */
178static inline void generic_set_bit(int nr, volatile unsigned long *addr)
179{
180 unsigned long mask = BIT_MASK(nr);
181 unsigned long *p = ((unsigned long *)addr) + BIT_WORD(nr);
182
183 *p |= mask;
184}
185
186static inline void generic_clear_bit(int nr, volatile unsigned long *addr)
187{
188 unsigned long mask = BIT_MASK(nr);
189 unsigned long *p = ((unsigned long *)addr) + BIT_WORD(nr);
190
191 *p &= ~mask;
192}
wdenkb15cbc02000-08-21 15:05:47 +0000193
194#endif