x86/speculation/mds: Fix documentation typo
[linux/fpc-iii.git] / arch / frv / include / asm / bitops.h
blob0df8e95e37151df000a8ca0610e28d04dfbb4cff
1 /* bitops.h: bit operations for the Fujitsu FR-V CPUs
3 * For an explanation of how atomic ops work in this arch, see:
4 * Documentation/frv/atomic-ops.txt
6 * Copyright (C) 2004 Red Hat, Inc. All Rights Reserved.
7 * Written by David Howells (dhowells@redhat.com)
9 * This program is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU General Public License
11 * as published by the Free Software Foundation; either version
12 * 2 of the License, or (at your option) any later version.
14 #ifndef _ASM_BITOPS_H
15 #define _ASM_BITOPS_H
17 #include <linux/compiler.h>
18 #include <asm/byteorder.h>
20 #ifdef __KERNEL__
22 #ifndef _LINUX_BITOPS_H
23 #error only <linux/bitops.h> can be included directly
24 #endif
26 #include <asm-generic/bitops/ffz.h>
28 #include <asm/atomic.h>
30 static inline int test_and_clear_bit(unsigned long nr, volatile void *addr)
32 unsigned int *ptr = (void *)addr;
33 unsigned int mask = 1UL << (nr & 31);
34 ptr += nr >> 5;
35 return (__atomic32_fetch_and(~mask, ptr) & mask) != 0;
38 static inline int test_and_set_bit(unsigned long nr, volatile void *addr)
40 unsigned int *ptr = (void *)addr;
41 unsigned int mask = 1UL << (nr & 31);
42 ptr += nr >> 5;
43 return (__atomic32_fetch_or(mask, ptr) & mask) != 0;
46 static inline int test_and_change_bit(unsigned long nr, volatile void *addr)
48 unsigned int *ptr = (void *)addr;
49 unsigned int mask = 1UL << (nr & 31);
50 ptr += nr >> 5;
51 return (__atomic32_fetch_xor(mask, ptr) & mask) != 0;
54 static inline void clear_bit(unsigned long nr, volatile void *addr)
56 test_and_clear_bit(nr, addr);
59 static inline void set_bit(unsigned long nr, volatile void *addr)
61 test_and_set_bit(nr, addr);
64 static inline void change_bit(unsigned long nr, volatile void *addr)
66 test_and_change_bit(nr, addr);
69 static inline void __clear_bit(unsigned long nr, volatile void *addr)
71 volatile unsigned long *a = addr;
72 int mask;
74 a += nr >> 5;
75 mask = 1 << (nr & 31);
76 *a &= ~mask;
79 static inline void __set_bit(unsigned long nr, volatile void *addr)
81 volatile unsigned long *a = addr;
82 int mask;
84 a += nr >> 5;
85 mask = 1 << (nr & 31);
86 *a |= mask;
89 static inline void __change_bit(unsigned long nr, volatile void *addr)
91 volatile unsigned long *a = addr;
92 int mask;
94 a += nr >> 5;
95 mask = 1 << (nr & 31);
96 *a ^= mask;
99 static inline int __test_and_clear_bit(unsigned long nr, volatile void *addr)
101 volatile unsigned long *a = addr;
102 int mask, retval;
104 a += nr >> 5;
105 mask = 1 << (nr & 31);
106 retval = (mask & *a) != 0;
107 *a &= ~mask;
108 return retval;
111 static inline int __test_and_set_bit(unsigned long nr, volatile void *addr)
113 volatile unsigned long *a = addr;
114 int mask, retval;
116 a += nr >> 5;
117 mask = 1 << (nr & 31);
118 retval = (mask & *a) != 0;
119 *a |= mask;
120 return retval;
123 static inline int __test_and_change_bit(unsigned long nr, volatile void *addr)
125 volatile unsigned long *a = addr;
126 int mask, retval;
128 a += nr >> 5;
129 mask = 1 << (nr & 31);
130 retval = (mask & *a) != 0;
131 *a ^= mask;
132 return retval;
136 * This routine doesn't need to be atomic.
138 static inline int
139 __constant_test_bit(unsigned long nr, const volatile void *addr)
141 return ((1UL << (nr & 31)) & (((const volatile unsigned int *) addr)[nr >> 5])) != 0;
144 static inline int __test_bit(unsigned long nr, const volatile void *addr)
146 int * a = (int *) addr;
147 int mask;
149 a += nr >> 5;
150 mask = 1 << (nr & 0x1f);
151 return ((mask & *a) != 0);
154 #define test_bit(nr,addr) \
155 (__builtin_constant_p(nr) ? \
156 __constant_test_bit((nr),(addr)) : \
157 __test_bit((nr),(addr)))
159 #include <asm-generic/bitops/find.h>
162 * fls - find last bit set
163 * @x: the word to search
165 * This is defined the same way as ffs:
166 * - return 32..1 to indicate bit 31..0 most significant bit set
167 * - return 0 to indicate no bits set
169 #define fls(x) \
170 ({ \
171 int bit; \
173 asm(" subcc %1,gr0,gr0,icc0 \n" \
174 " ckne icc0,cc4 \n" \
175 " cscan.p %1,gr0,%0 ,cc4,#1 \n" \
176 " csub %0,%0,%0 ,cc4,#0 \n" \
177 " csub %2,%0,%0 ,cc4,#1 \n" \
178 : "=&r"(bit) \
179 : "r"(x), "r"(32) \
180 : "icc0", "cc4" \
181 ); \
183 bit; \
187 * fls64 - find last bit set in a 64-bit value
188 * @n: the value to search
190 * This is defined the same way as ffs:
191 * - return 64..1 to indicate bit 63..0 most significant bit set
192 * - return 0 to indicate no bits set
194 static inline __attribute__((const))
195 int fls64(u64 n)
197 union {
198 u64 ll;
199 struct { u32 h, l; };
200 } _;
201 int bit, x, y;
203 _.ll = n;
205 asm(" subcc.p %3,gr0,gr0,icc0 \n"
206 " subcc %4,gr0,gr0,icc1 \n"
207 " ckne icc0,cc4 \n"
208 " ckne icc1,cc5 \n"
209 " norcr cc4,cc5,cc6 \n"
210 " csub.p %0,%0,%0 ,cc6,1 \n"
211 " orcr cc5,cc4,cc4 \n"
212 " andcr cc4,cc5,cc4 \n"
213 " cscan.p %3,gr0,%0 ,cc4,0 \n"
214 " setlos #64,%1 \n"
215 " cscan.p %4,gr0,%0 ,cc4,1 \n"
216 " setlos #32,%2 \n"
217 " csub.p %1,%0,%0 ,cc4,0 \n"
218 " csub %2,%0,%0 ,cc4,1 \n"
219 : "=&r"(bit), "=r"(x), "=r"(y)
220 : "0r"(_.h), "r"(_.l)
221 : "icc0", "icc1", "cc4", "cc5", "cc6"
223 return bit;
228 * ffs - find first bit set
229 * @x: the word to search
231 * - return 32..1 to indicate bit 31..0 most least significant bit set
232 * - return 0 to indicate no bits set
234 static inline __attribute__((const))
235 int ffs(int x)
237 /* Note: (x & -x) gives us a mask that is the least significant
238 * (rightmost) 1-bit of the value in x.
240 return fls(x & -x);
244 * __ffs - find first bit set
245 * @x: the word to search
247 * - return 31..0 to indicate bit 31..0 most least significant bit set
248 * - if no bits are set in x, the result is undefined
250 static inline __attribute__((const))
251 int __ffs(unsigned long x)
253 int bit;
254 asm("scan %1,gr0,%0" : "=r"(bit) : "r"(x & -x));
255 return 31 - bit;
259 * __fls - find last (most-significant) set bit in a long word
260 * @word: the word to search
262 * Undefined if no set bit exists, so code should check against 0 first.
264 static inline unsigned long __fls(unsigned long word)
266 unsigned long bit;
267 asm("scan %1,gr0,%0" : "=r"(bit) : "r"(word));
268 return bit;
272 * special slimline version of fls() for calculating ilog2_u32()
273 * - note: no protection against n == 0
275 #define ARCH_HAS_ILOG2_U32
276 static inline __attribute__((const))
277 int __ilog2_u32(u32 n)
279 int bit;
280 asm("scan %1,gr0,%0" : "=r"(bit) : "r"(n));
281 return 31 - bit;
285 * special slimline version of fls64() for calculating ilog2_u64()
286 * - note: no protection against n == 0
288 #define ARCH_HAS_ILOG2_U64
289 static inline __attribute__((const))
290 int __ilog2_u64(u64 n)
292 union {
293 u64 ll;
294 struct { u32 h, l; };
295 } _;
296 int bit, x, y;
298 _.ll = n;
300 asm(" subcc %3,gr0,gr0,icc0 \n"
301 " ckeq icc0,cc4 \n"
302 " cscan.p %3,gr0,%0 ,cc4,0 \n"
303 " setlos #63,%1 \n"
304 " cscan.p %4,gr0,%0 ,cc4,1 \n"
305 " setlos #31,%2 \n"
306 " csub.p %1,%0,%0 ,cc4,0 \n"
307 " csub %2,%0,%0 ,cc4,1 \n"
308 : "=&r"(bit), "=r"(x), "=r"(y)
309 : "0r"(_.h), "r"(_.l)
310 : "icc0", "cc4"
312 return bit;
315 #include <asm-generic/bitops/sched.h>
316 #include <asm-generic/bitops/hweight.h>
317 #include <asm-generic/bitops/lock.h>
319 #include <asm-generic/bitops/le.h>
321 #include <asm-generic/bitops/ext2-atomic-setbit.h>
323 #endif /* __KERNEL__ */
325 #endif /* _ASM_BITOPS_H */