blob: a1728f8c070519776ed379a018bf56ff7d619d08 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * This file is subject to the terms and conditions of the GNU General Public
3 * License. See the file "COPYING" in the main directory of this archive
4 * for more details.
5 *
6 * Copyright (c) 1994 - 1997, 1999, 2000 Ralf Baechle (ralf@gnu.org)
7 * Copyright (c) 1999, 2000 Silicon Graphics, Inc.
8 */
9#ifndef _ASM_BITOPS_H
10#define _ASM_BITOPS_H
11
12#include <linux/config.h>
13#include <linux/compiler.h>
14#include <linux/types.h>
Ralf Baechleec917c22005-10-07 16:58:15 +010015#include <asm/bug.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070016#include <asm/byteorder.h> /* sigh ... */
17#include <asm/cpu-features.h>
18
19#if (_MIPS_SZLONG == 32)
20#define SZLONG_LOG 5
21#define SZLONG_MASK 31UL
Maciej W. Rozyckiaac8aa72005-06-14 17:35:03 +000022#define __LL "ll "
23#define __SC "sc "
Ralf Baechle42a3b4f2005-09-03 15:56:17 -070024#define cpu_to_lelongp(x) cpu_to_le32p((__u32 *) (x))
Linus Torvalds1da177e2005-04-16 15:20:36 -070025#elif (_MIPS_SZLONG == 64)
26#define SZLONG_LOG 6
27#define SZLONG_MASK 63UL
Maciej W. Rozyckiaac8aa72005-06-14 17:35:03 +000028#define __LL "lld "
29#define __SC "scd "
Ralf Baechle42a3b4f2005-09-03 15:56:17 -070030#define cpu_to_lelongp(x) cpu_to_le64p((__u64 *) (x))
Linus Torvalds1da177e2005-04-16 15:20:36 -070031#endif
32
33#ifdef __KERNEL__
34
35#include <asm/interrupt.h>
36#include <asm/sgidefs.h>
37#include <asm/war.h>
38
39/*
40 * clear_bit() doesn't provide any barrier for the compiler.
41 */
42#define smp_mb__before_clear_bit() smp_mb()
43#define smp_mb__after_clear_bit() smp_mb()
44
45/*
46 * Only disable interrupt for kernel mode stuff to keep usermode stuff
47 * that dares to use kernel include files alive.
48 */
49
50#define __bi_flags unsigned long flags
51#define __bi_local_irq_save(x) local_irq_save(x)
52#define __bi_local_irq_restore(x) local_irq_restore(x)
53#else
54#define __bi_flags
55#define __bi_local_irq_save(x)
56#define __bi_local_irq_restore(x)
57#endif /* __KERNEL__ */
58
59/*
60 * set_bit - Atomically set a bit in memory
61 * @nr: the bit to set
62 * @addr: the address to start counting from
63 *
64 * This function is atomic and may not be reordered. See __set_bit()
65 * if you do not require the atomic guarantees.
66 * Note that @nr may be almost arbitrarily large; this function is not
67 * restricted to acting on a single-word quantity.
68 */
69static inline void set_bit(unsigned long nr, volatile unsigned long *addr)
70{
71 unsigned long *m = ((unsigned long *) addr) + (nr >> SZLONG_LOG);
72 unsigned long temp;
73
74 if (cpu_has_llsc && R10000_LLSC_WAR) {
75 __asm__ __volatile__(
Maciej W. Rozyckic4559f62005-06-23 15:57:15 +000076 " .set mips3 \n"
Linus Torvalds1da177e2005-04-16 15:20:36 -070077 "1: " __LL "%0, %1 # set_bit \n"
78 " or %0, %2 \n"
Maciej W. Rozyckiaac8aa72005-06-14 17:35:03 +000079 " " __SC "%0, %1 \n"
Linus Torvalds1da177e2005-04-16 15:20:36 -070080 " beqzl %0, 1b \n"
Maciej W. Rozyckiaac8aa72005-06-14 17:35:03 +000081 " .set mips0 \n"
Linus Torvalds1da177e2005-04-16 15:20:36 -070082 : "=&r" (temp), "=m" (*m)
83 : "ir" (1UL << (nr & SZLONG_MASK)), "m" (*m));
84 } else if (cpu_has_llsc) {
85 __asm__ __volatile__(
Maciej W. Rozyckic4559f62005-06-23 15:57:15 +000086 " .set mips3 \n"
Linus Torvalds1da177e2005-04-16 15:20:36 -070087 "1: " __LL "%0, %1 # set_bit \n"
88 " or %0, %2 \n"
Maciej W. Rozyckiaac8aa72005-06-14 17:35:03 +000089 " " __SC "%0, %1 \n"
Linus Torvalds1da177e2005-04-16 15:20:36 -070090 " beqz %0, 1b \n"
Maciej W. Rozyckiaac8aa72005-06-14 17:35:03 +000091 " .set mips0 \n"
Linus Torvalds1da177e2005-04-16 15:20:36 -070092 : "=&r" (temp), "=m" (*m)
93 : "ir" (1UL << (nr & SZLONG_MASK)), "m" (*m));
94 } else {
95 volatile unsigned long *a = addr;
96 unsigned long mask;
97 __bi_flags;
98
99 a += nr >> SZLONG_LOG;
100 mask = 1UL << (nr & SZLONG_MASK);
101 __bi_local_irq_save(flags);
102 *a |= mask;
103 __bi_local_irq_restore(flags);
104 }
105}
106
107/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108 * clear_bit - Clears a bit in memory
109 * @nr: Bit to clear
110 * @addr: Address to start counting from
111 *
112 * clear_bit() is atomic and may not be reordered. However, it does
113 * not contain a memory barrier, so if it is used for locking purposes,
114 * you should call smp_mb__before_clear_bit() and/or smp_mb__after_clear_bit()
115 * in order to ensure changes are visible on other processors.
116 */
117static inline void clear_bit(unsigned long nr, volatile unsigned long *addr)
118{
119 unsigned long *m = ((unsigned long *) addr) + (nr >> SZLONG_LOG);
120 unsigned long temp;
121
122 if (cpu_has_llsc && R10000_LLSC_WAR) {
123 __asm__ __volatile__(
Maciej W. Rozyckic4559f62005-06-23 15:57:15 +0000124 " .set mips3 \n"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125 "1: " __LL "%0, %1 # clear_bit \n"
126 " and %0, %2 \n"
127 " " __SC "%0, %1 \n"
128 " beqzl %0, 1b \n"
Maciej W. Rozyckiaac8aa72005-06-14 17:35:03 +0000129 " .set mips0 \n"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130 : "=&r" (temp), "=m" (*m)
131 : "ir" (~(1UL << (nr & SZLONG_MASK))), "m" (*m));
132 } else if (cpu_has_llsc) {
133 __asm__ __volatile__(
Maciej W. Rozyckic4559f62005-06-23 15:57:15 +0000134 " .set mips3 \n"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135 "1: " __LL "%0, %1 # clear_bit \n"
136 " and %0, %2 \n"
137 " " __SC "%0, %1 \n"
138 " beqz %0, 1b \n"
Maciej W. Rozyckiaac8aa72005-06-14 17:35:03 +0000139 " .set mips0 \n"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140 : "=&r" (temp), "=m" (*m)
141 : "ir" (~(1UL << (nr & SZLONG_MASK))), "m" (*m));
142 } else {
143 volatile unsigned long *a = addr;
144 unsigned long mask;
145 __bi_flags;
146
147 a += nr >> SZLONG_LOG;
148 mask = 1UL << (nr & SZLONG_MASK);
149 __bi_local_irq_save(flags);
150 *a &= ~mask;
151 __bi_local_irq_restore(flags);
152 }
153}
154
155/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156 * change_bit - Toggle a bit in memory
157 * @nr: Bit to change
158 * @addr: Address to start counting from
159 *
160 * change_bit() is atomic and may not be reordered.
161 * Note that @nr may be almost arbitrarily large; this function is not
162 * restricted to acting on a single-word quantity.
163 */
164static inline void change_bit(unsigned long nr, volatile unsigned long *addr)
165{
166 if (cpu_has_llsc && R10000_LLSC_WAR) {
167 unsigned long *m = ((unsigned long *) addr) + (nr >> SZLONG_LOG);
168 unsigned long temp;
169
170 __asm__ __volatile__(
Maciej W. Rozyckic4559f62005-06-23 15:57:15 +0000171 " .set mips3 \n"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 "1: " __LL "%0, %1 # change_bit \n"
173 " xor %0, %2 \n"
Maciej W. Rozyckiaac8aa72005-06-14 17:35:03 +0000174 " " __SC "%0, %1 \n"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175 " beqzl %0, 1b \n"
Maciej W. Rozyckiaac8aa72005-06-14 17:35:03 +0000176 " .set mips0 \n"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177 : "=&r" (temp), "=m" (*m)
178 : "ir" (1UL << (nr & SZLONG_MASK)), "m" (*m));
179 } else if (cpu_has_llsc) {
180 unsigned long *m = ((unsigned long *) addr) + (nr >> SZLONG_LOG);
181 unsigned long temp;
182
183 __asm__ __volatile__(
Maciej W. Rozyckic4559f62005-06-23 15:57:15 +0000184 " .set mips3 \n"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185 "1: " __LL "%0, %1 # change_bit \n"
186 " xor %0, %2 \n"
Maciej W. Rozyckiaac8aa72005-06-14 17:35:03 +0000187 " " __SC "%0, %1 \n"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188 " beqz %0, 1b \n"
Maciej W. Rozyckiaac8aa72005-06-14 17:35:03 +0000189 " .set mips0 \n"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190 : "=&r" (temp), "=m" (*m)
191 : "ir" (1UL << (nr & SZLONG_MASK)), "m" (*m));
192 } else {
193 volatile unsigned long *a = addr;
194 unsigned long mask;
195 __bi_flags;
196
197 a += nr >> SZLONG_LOG;
198 mask = 1UL << (nr & SZLONG_MASK);
199 __bi_local_irq_save(flags);
200 *a ^= mask;
201 __bi_local_irq_restore(flags);
202 }
203}
204
205/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206 * test_and_set_bit - Set a bit and return its old value
207 * @nr: Bit to set
208 * @addr: Address to count from
209 *
210 * This operation is atomic and cannot be reordered.
211 * It also implies a memory barrier.
212 */
213static inline int test_and_set_bit(unsigned long nr,
214 volatile unsigned long *addr)
215{
216 if (cpu_has_llsc && R10000_LLSC_WAR) {
217 unsigned long *m = ((unsigned long *) addr) + (nr >> SZLONG_LOG);
218 unsigned long temp, res;
219
220 __asm__ __volatile__(
Maciej W. Rozyckic4559f62005-06-23 15:57:15 +0000221 " .set mips3 \n"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222 "1: " __LL "%0, %1 # test_and_set_bit \n"
223 " or %2, %0, %3 \n"
224 " " __SC "%2, %1 \n"
225 " beqzl %2, 1b \n"
226 " and %2, %0, %3 \n"
227#ifdef CONFIG_SMP
Maciej W. Rozyckiaac8aa72005-06-14 17:35:03 +0000228 " sync \n"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229#endif
Maciej W. Rozyckiaac8aa72005-06-14 17:35:03 +0000230 " .set mips0 \n"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231 : "=&r" (temp), "=m" (*m), "=&r" (res)
232 : "r" (1UL << (nr & SZLONG_MASK)), "m" (*m)
233 : "memory");
234
235 return res != 0;
236 } else if (cpu_has_llsc) {
237 unsigned long *m = ((unsigned long *) addr) + (nr >> SZLONG_LOG);
238 unsigned long temp, res;
239
240 __asm__ __volatile__(
Maciej W. Rozyckiaac8aa72005-06-14 17:35:03 +0000241 " .set push \n"
242 " .set noreorder \n"
Maciej W. Rozyckic4559f62005-06-23 15:57:15 +0000243 " .set mips3 \n"
Maciej W. Rozyckiaac8aa72005-06-14 17:35:03 +0000244 "1: " __LL "%0, %1 # test_and_set_bit \n"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245 " or %2, %0, %3 \n"
246 " " __SC "%2, %1 \n"
247 " beqz %2, 1b \n"
248 " and %2, %0, %3 \n"
249#ifdef CONFIG_SMP
Maciej W. Rozyckiaac8aa72005-06-14 17:35:03 +0000250 " sync \n"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251#endif
Maciej W. Rozyckiaac8aa72005-06-14 17:35:03 +0000252 " .set pop \n"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253 : "=&r" (temp), "=m" (*m), "=&r" (res)
254 : "r" (1UL << (nr & SZLONG_MASK)), "m" (*m)
255 : "memory");
256
257 return res != 0;
258 } else {
259 volatile unsigned long *a = addr;
260 unsigned long mask;
261 int retval;
262 __bi_flags;
263
264 a += nr >> SZLONG_LOG;
265 mask = 1UL << (nr & SZLONG_MASK);
266 __bi_local_irq_save(flags);
267 retval = (mask & *a) != 0;
268 *a |= mask;
269 __bi_local_irq_restore(flags);
270
271 return retval;
272 }
273}
274
275/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276 * test_and_clear_bit - Clear a bit and return its old value
277 * @nr: Bit to clear
278 * @addr: Address to count from
279 *
280 * This operation is atomic and cannot be reordered.
281 * It also implies a memory barrier.
282 */
283static inline int test_and_clear_bit(unsigned long nr,
284 volatile unsigned long *addr)
285{
286 if (cpu_has_llsc && R10000_LLSC_WAR) {
287 unsigned long *m = ((unsigned long *) addr) + (nr >> SZLONG_LOG);
288 unsigned long temp, res;
289
290 __asm__ __volatile__(
Maciej W. Rozyckic4559f62005-06-23 15:57:15 +0000291 " .set mips3 \n"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292 "1: " __LL "%0, %1 # test_and_clear_bit \n"
293 " or %2, %0, %3 \n"
294 " xor %2, %3 \n"
Maciej W. Rozyckiaac8aa72005-06-14 17:35:03 +0000295 " " __SC "%2, %1 \n"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296 " beqzl %2, 1b \n"
297 " and %2, %0, %3 \n"
298#ifdef CONFIG_SMP
299 " sync \n"
300#endif
Maciej W. Rozyckiaac8aa72005-06-14 17:35:03 +0000301 " .set mips0 \n"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302 : "=&r" (temp), "=m" (*m), "=&r" (res)
303 : "r" (1UL << (nr & SZLONG_MASK)), "m" (*m)
304 : "memory");
305
306 return res != 0;
307 } else if (cpu_has_llsc) {
308 unsigned long *m = ((unsigned long *) addr) + (nr >> SZLONG_LOG);
309 unsigned long temp, res;
310
311 __asm__ __volatile__(
Maciej W. Rozyckiaac8aa72005-06-14 17:35:03 +0000312 " .set push \n"
313 " .set noreorder \n"
Maciej W. Rozyckic4559f62005-06-23 15:57:15 +0000314 " .set mips3 \n"
Maciej W. Rozyckiaac8aa72005-06-14 17:35:03 +0000315 "1: " __LL "%0, %1 # test_and_clear_bit \n"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316 " or %2, %0, %3 \n"
317 " xor %2, %3 \n"
Maciej W. Rozyckiaac8aa72005-06-14 17:35:03 +0000318 " " __SC "%2, %1 \n"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319 " beqz %2, 1b \n"
320 " and %2, %0, %3 \n"
321#ifdef CONFIG_SMP
322 " sync \n"
323#endif
Maciej W. Rozyckiaac8aa72005-06-14 17:35:03 +0000324 " .set pop \n"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325 : "=&r" (temp), "=m" (*m), "=&r" (res)
326 : "r" (1UL << (nr & SZLONG_MASK)), "m" (*m)
327 : "memory");
328
329 return res != 0;
330 } else {
331 volatile unsigned long *a = addr;
332 unsigned long mask;
333 int retval;
334 __bi_flags;
335
336 a += nr >> SZLONG_LOG;
337 mask = 1UL << (nr & SZLONG_MASK);
338 __bi_local_irq_save(flags);
339 retval = (mask & *a) != 0;
340 *a &= ~mask;
341 __bi_local_irq_restore(flags);
342
343 return retval;
344 }
345}
346
347/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348 * test_and_change_bit - Change a bit and return its old value
349 * @nr: Bit to change
350 * @addr: Address to count from
351 *
352 * This operation is atomic and cannot be reordered.
353 * It also implies a memory barrier.
354 */
355static inline int test_and_change_bit(unsigned long nr,
356 volatile unsigned long *addr)
357{
358 if (cpu_has_llsc && R10000_LLSC_WAR) {
359 unsigned long *m = ((unsigned long *) addr) + (nr >> SZLONG_LOG);
360 unsigned long temp, res;
361
362 __asm__ __volatile__(
Maciej W. Rozyckic4559f62005-06-23 15:57:15 +0000363 " .set mips3 \n"
Maciej W. Rozyckiaac8aa72005-06-14 17:35:03 +0000364 "1: " __LL "%0, %1 # test_and_change_bit \n"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365 " xor %2, %0, %3 \n"
Maciej W. Rozyckiaac8aa72005-06-14 17:35:03 +0000366 " " __SC "%2, %1 \n"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367 " beqzl %2, 1b \n"
368 " and %2, %0, %3 \n"
369#ifdef CONFIG_SMP
370 " sync \n"
371#endif
Maciej W. Rozyckiaac8aa72005-06-14 17:35:03 +0000372 " .set mips0 \n"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373 : "=&r" (temp), "=m" (*m), "=&r" (res)
374 : "r" (1UL << (nr & SZLONG_MASK)), "m" (*m)
375 : "memory");
376
377 return res != 0;
378 } else if (cpu_has_llsc) {
379 unsigned long *m = ((unsigned long *) addr) + (nr >> SZLONG_LOG);
380 unsigned long temp, res;
381
382 __asm__ __volatile__(
Maciej W. Rozyckiaac8aa72005-06-14 17:35:03 +0000383 " .set push \n"
384 " .set noreorder \n"
Maciej W. Rozyckic4559f62005-06-23 15:57:15 +0000385 " .set mips3 \n"
Maciej W. Rozyckiaac8aa72005-06-14 17:35:03 +0000386 "1: " __LL "%0, %1 # test_and_change_bit \n"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387 " xor %2, %0, %3 \n"
Maciej W. Rozyckiaac8aa72005-06-14 17:35:03 +0000388 " " __SC "\t%2, %1 \n"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389 " beqz %2, 1b \n"
390 " and %2, %0, %3 \n"
391#ifdef CONFIG_SMP
392 " sync \n"
393#endif
Maciej W. Rozyckiaac8aa72005-06-14 17:35:03 +0000394 " .set pop \n"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 : "=&r" (temp), "=m" (*m), "=&r" (res)
396 : "r" (1UL << (nr & SZLONG_MASK)), "m" (*m)
397 : "memory");
398
399 return res != 0;
400 } else {
401 volatile unsigned long *a = addr;
402 unsigned long mask, retval;
403 __bi_flags;
404
405 a += nr >> SZLONG_LOG;
406 mask = 1UL << (nr & SZLONG_MASK);
407 __bi_local_irq_save(flags);
408 retval = (mask & *a) != 0;
409 *a ^= mask;
410 __bi_local_irq_restore(flags);
411
412 return retval;
413 }
414}
415
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416#undef __bi_flags
417#undef __bi_local_irq_save
418#undef __bi_local_irq_restore
419
Akinobu Mita3c9ee7e2006-03-26 01:39:30 -0800420#include <asm-generic/bitops/non-atomic.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421
Ralf Baechle65903262005-07-12 12:50:30 +0000422/*
423 * Return the bit position (0..63) of the most significant 1 bit in a word
424 * Returns -1 if no 1 bit exists
425 */
Ralf Baechleec917c22005-10-07 16:58:15 +0100426static inline int __ilog2(unsigned long x)
Ralf Baechle65903262005-07-12 12:50:30 +0000427{
428 int lz;
429
Ralf Baechleec917c22005-10-07 16:58:15 +0100430 if (sizeof(x) == 4) {
431 __asm__ (
432 " .set push \n"
433 " .set mips32 \n"
434 " clz %0, %1 \n"
435 " .set pop \n"
436 : "=r" (lz)
437 : "r" (x));
438
439 return 31 - lz;
440 }
441
442 BUG_ON(sizeof(x) != 8);
443
Ralf Baechle65903262005-07-12 12:50:30 +0000444 __asm__ (
445 " .set push \n"
446 " .set mips64 \n"
447 " dclz %0, %1 \n"
448 " .set pop \n"
449 : "=r" (lz)
450 : "r" (x));
451
452 return 63 - lz;
453}
Ralf Baechle65903262005-07-12 12:50:30 +0000454
Akinobu Mita3c9ee7e2006-03-26 01:39:30 -0800455#if defined(CONFIG_CPU_MIPS32) || defined(CONFIG_CPU_MIPS64)
456
Ralf Baechle65903262005-07-12 12:50:30 +0000457/*
458 * __ffs - find first bit in word.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459 * @word: The word to search
460 *
Ralf Baechle65903262005-07-12 12:50:30 +0000461 * Returns 0..SZLONG-1
462 * Undefined if no bit exists, so code should check against 0 first.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463 */
Ralf Baechle65903262005-07-12 12:50:30 +0000464static inline unsigned long __ffs(unsigned long word)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465{
Ralf Baechle65903262005-07-12 12:50:30 +0000466 return __ilog2(word & -word);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467}
468
469/*
Ralf Baechle65903262005-07-12 12:50:30 +0000470 * ffs - find first bit set.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471 * @word: The word to search
472 *
Ralf Baechle65903262005-07-12 12:50:30 +0000473 * Returns 1..SZLONG
474 * Returns 0 if no bit exists
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475 */
Ralf Baechle65903262005-07-12 12:50:30 +0000476
477static inline unsigned long ffs(unsigned long word)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478{
Ralf Baechle65903262005-07-12 12:50:30 +0000479 if (!word)
480 return 0;
481
482 return __ffs(word) + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483}
484
485/*
Ralf Baechle65903262005-07-12 12:50:30 +0000486 * ffz - find first zero in word.
487 * @word: The word to search
488 *
489 * Undefined if no zero exists, so code should check against ~0UL first.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490 */
Ralf Baechle65903262005-07-12 12:50:30 +0000491static inline unsigned long ffz(unsigned long word)
492{
493 return __ffs (~word);
494}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495
Ralf Baechle65903262005-07-12 12:50:30 +0000496/*
Ralf Baechle65903262005-07-12 12:50:30 +0000497 * fls - find last bit set.
498 * @word: The word to search
499 *
500 * Returns 1..SZLONG
501 * Returns 0 if no bit exists
502 */
503static inline unsigned long fls(unsigned long word)
504{
Ralf Baechle2caf1902006-01-30 17:14:41 +0000505#ifdef CONFIG_CPU_MIPS32
506 __asm__ ("clz %0, %1" : "=r" (word) : "r" (word));
507
508 return 32 - word;
Ralf Baechle2caf1902006-01-30 17:14:41 +0000509#endif
Ralf Baechle2caf1902006-01-30 17:14:41 +0000510
Ralf Baechle2caf1902006-01-30 17:14:41 +0000511#ifdef CONFIG_CPU_MIPS64
Ralf Baechle2caf1902006-01-30 17:14:41 +0000512 __asm__ ("dclz %0, %1" : "=r" (word) : "r" (word));
513
514 return 64 - word;
Ralf Baechle2caf1902006-01-30 17:14:41 +0000515#endif
Ralf Baechle65903262005-07-12 12:50:30 +0000516}
Ralf Baechle2caf1902006-01-30 17:14:41 +0000517
Akinobu Mita3c9ee7e2006-03-26 01:39:30 -0800518#else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519
Akinobu Mita3c9ee7e2006-03-26 01:39:30 -0800520#include <asm-generic/bitops/__ffs.h>
521#include <asm-generic/bitops/ffs.h>
522#include <asm-generic/bitops/ffz.h>
523#include <asm-generic/bitops/fls.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524
Akinobu Mita3c9ee7e2006-03-26 01:39:30 -0800525#endif /*defined(CONFIG_CPU_MIPS32) || defined(CONFIG_CPU_MIPS64) */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526
Akinobu Mita3c9ee7e2006-03-26 01:39:30 -0800527#include <asm-generic/bitops/fls64.h>
528#include <asm-generic/bitops/find.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529
530#ifdef __KERNEL__
531
Akinobu Mita3c9ee7e2006-03-26 01:39:30 -0800532#include <asm-generic/bitops/sched.h>
533#include <asm-generic/bitops/hweight.h>
534#include <asm-generic/bitops/ext2-non-atomic.h>
535#include <asm-generic/bitops/ext2-atomic.h>
536#include <asm-generic/bitops/minix.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537
538#endif /* __KERNEL__ */
539
540#endif /* _ASM_BITOPS_H */