blob: 7b4011f0a30c3e57edf85268dc75ce7beb0d8d06 [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
Jagan Teki79d4d5f2015-10-21 17:30:34 +05306#define BIT(nr) (1UL << (nr))
7#define BIT_MASK(nr) (1UL << ((nr) % BITS_PER_LONG))
8#define BIT_WORD(nr) ((nr) / BITS_PER_LONG)
Heiko Schocher69bab552015-09-07 13:43:52 +02009
wdenkb15cbc02000-08-21 15:05:47 +000010/*
Jagan Teki8aceb8f2015-10-21 16:46:51 +053011 * 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/*
wdenkb15cbc02000-08-21 15:05:47 +000022 * 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
27static 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
Simon Kagstrom2258cec2009-08-24 09:10:12 +020056/**
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 */
63static 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
wdenkb15cbc02000-08-21 15:05:47 +000093/*
94 * hweightN: returns the hamming weight (i.e. the number
95 * of bits set) of a N-bit word
96 */
97
98static inline unsigned int generic_hweight32(unsigned int w)
99{
wdenk57b2d802003-06-27 21:31:46 +0000100 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);
wdenkb15cbc02000-08-21 15:05:47 +0000105}
106
107static inline unsigned int generic_hweight16(unsigned int w)
108{
wdenk57b2d802003-06-27 21:31:46 +0000109 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);
wdenkb15cbc02000-08-21 15:05:47 +0000113}
114
115static inline unsigned int generic_hweight8(unsigned int w)
116{
wdenk57b2d802003-06-27 21:31:46 +0000117 unsigned int res = (w & 0x55) + ((w >> 1) & 0x55);
118 res = (res & 0x33) + ((res >> 2) & 0x33);
119 return (res & 0x0F) + ((res >> 4) & 0x0F);
wdenkb15cbc02000-08-21 15:05:47 +0000120}
121
122#include <asm/bitops.h>
123
Simon Kagstrom13edaf32009-08-24 09:09:50 +0200124/* linux/include/asm-generic/bitops/non-atomic.h */
125
Simon Kagstrom95a3c732009-09-17 15:15:52 +0200126#ifndef PLATFORM__SET_BIT
Simon Kagstrom13edaf32009-08-24 09:09:50 +0200127# define __set_bit generic_set_bit
128#endif
129
Simon Kagstrom95a3c732009-09-17 15:15:52 +0200130#ifndef PLATFORM__CLEAR_BIT
Simon Kagstrom13edaf32009-08-24 09:09:50 +0200131# define __clear_bit generic_clear_bit
132#endif
133
Simon Kagstrom95a3c732009-09-17 15:15:52 +0200134#ifndef PLATFORM_FFS
Simon Kagstrom2258cec2009-08-24 09:10:12 +0200135# define ffs generic_ffs
136#endif
137
Simon Kagstrom95a3c732009-09-17 15:15:52 +0200138#ifndef PLATFORM_FLS
Simon Kagstrom2258cec2009-08-24 09:10:12 +0200139# define fls generic_fls
140#endif
141
Simon Kagstrom13edaf32009-08-24 09:09:50 +0200142/**
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 */
151static 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
159static 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}
wdenkb15cbc02000-08-21 15:05:47 +0000166
167#endif