blob: e9bb827f429b4eb4b2487b68a061dd31331f6931 [file] [log] [blame]
wdenkb15cbc02000-08-21 15:05:47 +00001#ifndef _LINUX_BITOPS_H
2#define _LINUX_BITOPS_H
3
Simon Kagstrom02f99902009-08-24 09:09:50 +02004#include <asm/types.h>
wdenkb15cbc02000-08-21 15:05:47 +00005
Jagan Teki67345282015-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 Schocher92a31882015-09-07 13:43:52 +02009
wdenkb15cbc02000-08-21 15:05:47 +000010/*
11 * ffs: find first bit set. This is defined the same way as
12 * the libc and compiler builtin ffs routines, therefore
13 * differs in spirit from the above ffz (man ffs).
14 */
15
16static inline int generic_ffs(int x)
17{
18 int r = 1;
19
20 if (!x)
21 return 0;
22 if (!(x & 0xffff)) {
23 x >>= 16;
24 r += 16;
25 }
26 if (!(x & 0xff)) {
27 x >>= 8;
28 r += 8;
29 }
30 if (!(x & 0xf)) {
31 x >>= 4;
32 r += 4;
33 }
34 if (!(x & 3)) {
35 x >>= 2;
36 r += 2;
37 }
38 if (!(x & 1)) {
39 x >>= 1;
40 r += 1;
41 }
42 return r;
43}
44
Simon Kagstrom52d61222009-08-24 09:10:12 +020045/**
46 * fls - find last (most-significant) bit set
47 * @x: the word to search
48 *
49 * This is defined the same way as ffs.
50 * Note fls(0) = 0, fls(1) = 1, fls(0x80000000) = 32.
51 */
52static inline int generic_fls(int x)
53{
54 int r = 32;
55
56 if (!x)
57 return 0;
58 if (!(x & 0xffff0000u)) {
59 x <<= 16;
60 r -= 16;
61 }
62 if (!(x & 0xff000000u)) {
63 x <<= 8;
64 r -= 8;
65 }
66 if (!(x & 0xf0000000u)) {
67 x <<= 4;
68 r -= 4;
69 }
70 if (!(x & 0xc0000000u)) {
71 x <<= 2;
72 r -= 2;
73 }
74 if (!(x & 0x80000000u)) {
75 x <<= 1;
76 r -= 1;
77 }
78 return r;
79}
80
81
wdenkb15cbc02000-08-21 15:05:47 +000082/*
83 * hweightN: returns the hamming weight (i.e. the number
84 * of bits set) of a N-bit word
85 */
86
87static inline unsigned int generic_hweight32(unsigned int w)
88{
wdenk8bde7f72003-06-27 21:31:46 +000089 unsigned int res = (w & 0x55555555) + ((w >> 1) & 0x55555555);
90 res = (res & 0x33333333) + ((res >> 2) & 0x33333333);
91 res = (res & 0x0F0F0F0F) + ((res >> 4) & 0x0F0F0F0F);
92 res = (res & 0x00FF00FF) + ((res >> 8) & 0x00FF00FF);
93 return (res & 0x0000FFFF) + ((res >> 16) & 0x0000FFFF);
wdenkb15cbc02000-08-21 15:05:47 +000094}
95
96static inline unsigned int generic_hweight16(unsigned int w)
97{
wdenk8bde7f72003-06-27 21:31:46 +000098 unsigned int res = (w & 0x5555) + ((w >> 1) & 0x5555);
99 res = (res & 0x3333) + ((res >> 2) & 0x3333);
100 res = (res & 0x0F0F) + ((res >> 4) & 0x0F0F);
101 return (res & 0x00FF) + ((res >> 8) & 0x00FF);
wdenkb15cbc02000-08-21 15:05:47 +0000102}
103
104static inline unsigned int generic_hweight8(unsigned int w)
105{
wdenk8bde7f72003-06-27 21:31:46 +0000106 unsigned int res = (w & 0x55) + ((w >> 1) & 0x55);
107 res = (res & 0x33) + ((res >> 2) & 0x33);
108 return (res & 0x0F) + ((res >> 4) & 0x0F);
wdenkb15cbc02000-08-21 15:05:47 +0000109}
110
111#include <asm/bitops.h>
112
Simon Kagstrom02f99902009-08-24 09:09:50 +0200113/* linux/include/asm-generic/bitops/non-atomic.h */
114
Simon Kagstrom0413cfe2009-09-17 15:15:52 +0200115#ifndef PLATFORM__SET_BIT
Simon Kagstrom02f99902009-08-24 09:09:50 +0200116# define __set_bit generic_set_bit
117#endif
118
Simon Kagstrom0413cfe2009-09-17 15:15:52 +0200119#ifndef PLATFORM__CLEAR_BIT
Simon Kagstrom02f99902009-08-24 09:09:50 +0200120# define __clear_bit generic_clear_bit
121#endif
122
Simon Kagstrom0413cfe2009-09-17 15:15:52 +0200123#ifndef PLATFORM_FFS
Simon Kagstrom52d61222009-08-24 09:10:12 +0200124# define ffs generic_ffs
125#endif
126
Simon Kagstrom0413cfe2009-09-17 15:15:52 +0200127#ifndef PLATFORM_FLS
Simon Kagstrom52d61222009-08-24 09:10:12 +0200128# define fls generic_fls
129#endif
130
Simon Kagstrom02f99902009-08-24 09:09:50 +0200131/**
132 * __set_bit - Set a bit in memory
133 * @nr: the bit to set
134 * @addr: the address to start counting from
135 *
136 * Unlike set_bit(), this function is non-atomic and may be reordered.
137 * If it's called on the same region of memory simultaneously, the effect
138 * may be that only one operation succeeds.
139 */
140static inline void generic_set_bit(int nr, volatile unsigned long *addr)
141{
142 unsigned long mask = BIT_MASK(nr);
143 unsigned long *p = ((unsigned long *)addr) + BIT_WORD(nr);
144
145 *p |= mask;
146}
147
148static inline void generic_clear_bit(int nr, volatile unsigned long *addr)
149{
150 unsigned long mask = BIT_MASK(nr);
151 unsigned long *p = ((unsigned long *)addr) + BIT_WORD(nr);
152
153 *p &= ~mask;
154}
wdenkb15cbc02000-08-21 15:05:47 +0000155
156#endif