bitops.h 15.4 KB
Newer Older
L
Linus Torvalds 已提交
1 2 3 4 5
/*
 * This file is subject to the terms and conditions of the GNU General Public
 * License.  See the file "COPYING" in the main directory of this archive
 * for more details.
 *
6
 * Copyright (c) 1994 - 1997, 99, 2000, 06, 07  Ralf Baechle (ralf@linux-mips.org)
L
Linus Torvalds 已提交
7 8 9 10 11
 * Copyright (c) 1999, 2000  Silicon Graphics, Inc.
 */
#ifndef _ASM_BITOPS_H
#define _ASM_BITOPS_H

J
Jiri Slaby 已提交
12 13 14 15
#ifndef _LINUX_BITOPS_H
#error only <linux/bitops.h> can be included directly
#endif

L
Linus Torvalds 已提交
16 17
#include <linux/compiler.h>
#include <linux/types.h>
18
#include <asm/barrier.h>
L
Linus Torvalds 已提交
19
#include <asm/byteorder.h>		/* sigh ... */
20
#include <asm/compiler.h>
L
Linus Torvalds 已提交
21
#include <asm/cpu-features.h>
22
#include <asm/llsc.h>
23 24
#include <asm/sgidefs.h>
#include <asm/war.h>
L
Linus Torvalds 已提交
25

26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
/*
 * These are the "slower" versions of the functions and are in bitops.c.
 * These functions call raw_local_irq_{save,restore}().
 */
void __mips_set_bit(unsigned long nr, volatile unsigned long *addr);
void __mips_clear_bit(unsigned long nr, volatile unsigned long *addr);
void __mips_change_bit(unsigned long nr, volatile unsigned long *addr);
int __mips_test_and_set_bit(unsigned long nr,
			    volatile unsigned long *addr);
int __mips_test_and_set_bit_lock(unsigned long nr,
				 volatile unsigned long *addr);
int __mips_test_and_clear_bit(unsigned long nr,
			      volatile unsigned long *addr);
int __mips_test_and_change_bit(unsigned long nr,
			       volatile unsigned long *addr);


L
Linus Torvalds 已提交
43 44 45 46 47 48 49 50 51 52 53 54
/*
 * set_bit - Atomically set a bit in memory
 * @nr: the bit to set
 * @addr: the address to start counting from
 *
 * This function is atomic and may not be reordered.  See __set_bit()
 * if you do not require the atomic guarantees.
 * Note that @nr may be almost arbitrarily large; this function is not
 * restricted to acting on a single-word quantity.
 */
static inline void set_bit(unsigned long nr, volatile unsigned long *addr)
{
55
	unsigned long *m = ((unsigned long *)addr) + (nr >> SZLONG_LOG);
56
	int bit = nr & SZLONG_MASK;
L
Linus Torvalds 已提交
57 58
	unsigned long temp;

59 60 61 62 63 64
	if (!kernel_uses_llsc) {
		__mips_set_bit(nr, addr);
		return;
	}

	if (R10000_LLSC_WAR) {
L
Linus Torvalds 已提交
65
		__asm__ __volatile__(
66
		"	.set	push					\n"
67
		"	.set	arch=r4000				\n"
L
Linus Torvalds 已提交
68 69
		"1:	" __LL "%0, %1			# set_bit	\n"
		"	or	%0, %2					\n"
70
		"	" __SC	"%0, %1					\n"
L
Linus Torvalds 已提交
71
		"	beqzl	%0, 1b					\n"
72
		"	.set	pop					\n"
73
		: "=&r" (temp), "=" GCC_OFF_SMALL_ASM() (*m)
74 75
		: "ir" (1UL << bit), GCC_OFF_SMALL_ASM() (*m)
		: __LLSC_CLOBBER);
76 77 78
		return;
	}

79
#if defined(CONFIG_CPU_MIPSR2) || defined(CONFIG_CPU_MIPSR6)
80
	if (__builtin_constant_p(bit) && (bit >= 16)) {
81
		loongson_llsc_mb();
82 83 84 85 86
		do {
			__asm__ __volatile__(
			"	" __LL "%0, %1		# set_bit	\n"
			"	" __INS "%0, %3, %2, 1			\n"
			"	" __SC "%0, %1				\n"
87
			: "=&r" (temp), "+" GCC_OFF_SMALL_ASM() (*m)
88 89
			: "ir" (bit), "r" (~0)
			: __LLSC_CLOBBER);
90
		} while (unlikely(!temp));
91 92
		return;
	}
93
#endif /* CONFIG_CPU_MIPSR2 || CONFIG_CPU_MIPSR6 */
94 95 96 97 98 99 100 101 102 103 104 105 106 107

	loongson_llsc_mb();
	do {
		__asm__ __volatile__(
		"	.set	push				\n"
		"	.set	"MIPS_ISA_ARCH_LEVEL"		\n"
		"	" __LL "%0, %1		# set_bit	\n"
		"	or	%0, %2				\n"
		"	" __SC	"%0, %1				\n"
		"	.set	pop				\n"
		: "=&r" (temp), "+" GCC_OFF_SMALL_ASM() (*m)
		: "ir" (1UL << bit)
		: __LLSC_CLOBBER);
	} while (unlikely(!temp));
L
Linus Torvalds 已提交
108 109 110 111 112 113 114 115 116
}

/*
 * clear_bit - Clears a bit in memory
 * @nr: Bit to clear
 * @addr: Address to start counting from
 *
 * clear_bit() is atomic and may not be reordered.  However, it does
 * not contain a memory barrier, so if it is used for locking purposes,
P
Peter Zijlstra 已提交
117
 * you should call smp_mb__before_atomic() and/or smp_mb__after_atomic()
L
Linus Torvalds 已提交
118 119 120 121
 * in order to ensure changes are visible on other processors.
 */
static inline void clear_bit(unsigned long nr, volatile unsigned long *addr)
{
122
	unsigned long *m = ((unsigned long *)addr) + (nr >> SZLONG_LOG);
123
	int bit = nr & SZLONG_MASK;
L
Linus Torvalds 已提交
124 125
	unsigned long temp;

126 127 128 129 130 131
	if (!kernel_uses_llsc) {
		__mips_clear_bit(nr, addr);
		return;
	}

	if (R10000_LLSC_WAR) {
L
Linus Torvalds 已提交
132
		__asm__ __volatile__(
133
		"	.set	push					\n"
134
		"	.set	arch=r4000				\n"
L
Linus Torvalds 已提交
135 136 137 138
		"1:	" __LL "%0, %1			# clear_bit	\n"
		"	and	%0, %2					\n"
		"	" __SC "%0, %1					\n"
		"	beqzl	%0, 1b					\n"
139
		"	.set	pop					\n"
140
		: "=&r" (temp), "+" GCC_OFF_SMALL_ASM() (*m)
141 142
		: "ir" (~(1UL << bit))
		: __LLSC_CLOBBER);
143 144 145
		return;
	}

146
#if defined(CONFIG_CPU_MIPSR2) || defined(CONFIG_CPU_MIPSR6)
147
	if (__builtin_constant_p(bit)) {
148
		loongson_llsc_mb();
149 150 151 152 153
		do {
			__asm__ __volatile__(
			"	" __LL "%0, %1		# clear_bit	\n"
			"	" __INS "%0, $0, %2, 1			\n"
			"	" __SC "%0, %1				\n"
154
			: "=&r" (temp), "+" GCC_OFF_SMALL_ASM() (*m)
155 156
			: "ir" (bit)
			: __LLSC_CLOBBER);
157
		} while (unlikely(!temp));
158 159
		return;
	}
160
#endif /* CONFIG_CPU_MIPSR2 || CONFIG_CPU_MIPSR6 */
161 162 163 164 165 166 167 168 169 170 171 172 173 174

	loongson_llsc_mb();
	do {
		__asm__ __volatile__(
		"	.set	push				\n"
		"	.set	"MIPS_ISA_ARCH_LEVEL"		\n"
		"	" __LL "%0, %1		# clear_bit	\n"
		"	and	%0, %2				\n"
		"	" __SC "%0, %1				\n"
		"	.set	pop				\n"
		: "=&r" (temp), "+" GCC_OFF_SMALL_ASM() (*m)
		: "ir" (~(1UL << bit))
		: __LLSC_CLOBBER);
	} while (unlikely(!temp));
L
Linus Torvalds 已提交
175 176
}

N
Nick Piggin 已提交
177 178 179 180 181 182 183 184 185 186
/*
 * clear_bit_unlock - Clears a bit in memory
 * @nr: Bit to clear
 * @addr: Address to start counting from
 *
 * clear_bit() is atomic and implies release semantics before the memory
 * operation. It can be used for an unlock.
 */
static inline void clear_bit_unlock(unsigned long nr, volatile unsigned long *addr)
{
P
Peter Zijlstra 已提交
187
	smp_mb__before_atomic();
N
Nick Piggin 已提交
188 189 190
	clear_bit(nr, addr);
}

L
Linus Torvalds 已提交
191 192 193 194 195 196 197 198 199 200 201
/*
 * change_bit - Toggle a bit in memory
 * @nr: Bit to change
 * @addr: Address to start counting from
 *
 * change_bit() is atomic and may not be reordered.
 * Note that @nr may be almost arbitrarily large; this function is not
 * restricted to acting on a single-word quantity.
 */
static inline void change_bit(unsigned long nr, volatile unsigned long *addr)
{
202
	unsigned long *m = ((unsigned long *)addr) + (nr >> SZLONG_LOG);
203
	int bit = nr & SZLONG_MASK;
204
	unsigned long temp;
205

206 207 208 209
	if (!kernel_uses_llsc) {
		__mips_change_bit(nr, addr);
		return;
	}
L
Linus Torvalds 已提交
210

211
	if (R10000_LLSC_WAR) {
L
Linus Torvalds 已提交
212
		__asm__ __volatile__(
213
		"	.set	push				\n"
214
		"	.set	arch=r4000			\n"
L
Linus Torvalds 已提交
215 216
		"1:	" __LL "%0, %1		# change_bit	\n"
		"	xor	%0, %2				\n"
217
		"	" __SC	"%0, %1				\n"
L
Linus Torvalds 已提交
218
		"	beqzl	%0, 1b				\n"
219
		"	.set	pop				\n"
220
		: "=&r" (temp), "+" GCC_OFF_SMALL_ASM() (*m)
221 222
		: "ir" (1UL << bit)
		: __LLSC_CLOBBER);
223 224
		return;
	}
L
Linus Torvalds 已提交
225

226 227 228 229 230 231 232 233 234 235 236 237 238
	loongson_llsc_mb();
	do {
		__asm__ __volatile__(
		"	.set	push				\n"
		"	.set	"MIPS_ISA_ARCH_LEVEL"		\n"
		"	" __LL "%0, %1		# change_bit	\n"
		"	xor	%0, %2				\n"
		"	" __SC	"%0, %1				\n"
		"	.set	pop				\n"
		: "=&r" (temp), "+" GCC_OFF_SMALL_ASM() (*m)
		: "ir" (1UL << bit)
		: __LLSC_CLOBBER);
	} while (unlikely(!temp));
L
Linus Torvalds 已提交
239 240 241 242 243 244 245 246 247 248 249 250 251
}

/*
 * test_and_set_bit - Set a bit and return its old value
 * @nr: Bit to set
 * @addr: Address to count from
 *
 * This operation is atomic and cannot be reordered.
 * It also implies a memory barrier.
 */
static inline int test_and_set_bit(unsigned long nr,
	volatile unsigned long *addr)
{
252
	unsigned long *m = ((unsigned long *)addr) + (nr >> SZLONG_LOG);
253
	int bit = nr & SZLONG_MASK;
254
	unsigned long res, temp;
255

256
	smp_mb__before_llsc();
N
Nick Piggin 已提交
257

258 259 260
	if (!kernel_uses_llsc) {
		res = __mips_test_and_set_bit(nr, addr);
	} else if (R10000_LLSC_WAR) {
L
Linus Torvalds 已提交
261
		__asm__ __volatile__(
262
		"	.set	push					\n"
263
		"	.set	arch=r4000				\n"
L
Linus Torvalds 已提交
264 265 266 267 268
		"1:	" __LL "%0, %1		# test_and_set_bit	\n"
		"	or	%2, %0, %3				\n"
		"	" __SC	"%2, %1					\n"
		"	beqzl	%2, 1b					\n"
		"	and	%2, %0, %3				\n"
269
		"	.set	pop					\n"
270
		: "=&r" (temp), "+" GCC_OFF_SMALL_ASM() (*m), "=&r" (res)
271
		: "r" (1UL << bit)
272
		: __LLSC_CLOBBER);
273
	} else {
274
		loongson_llsc_mb();
275 276
		do {
			__asm__ __volatile__(
277
			"	.set	push				\n"
278
			"	.set	"MIPS_ISA_ARCH_LEVEL"		\n"
279 280 281
			"	" __LL "%0, %1	# test_and_set_bit	\n"
			"	or	%2, %0, %3			\n"
			"	" __SC	"%2, %1				\n"
282
			"	.set	pop				\n"
283
			: "=&r" (temp), "+" GCC_OFF_SMALL_ASM() (*m), "=&r" (res)
284
			: "r" (1UL << bit)
285
			: __LLSC_CLOBBER);
286 287 288
		} while (unlikely(!res));

		res = temp & (1UL << bit);
289
	}
290

291
	smp_llsc_mb();
292 293

	return res != 0;
L
Linus Torvalds 已提交
294 295
}

N
Nick Piggin 已提交
296 297 298 299 300 301 302 303 304 305 306
/*
 * test_and_set_bit_lock - Set a bit and return its old value
 * @nr: Bit to set
 * @addr: Address to count from
 *
 * This operation is atomic and implies acquire ordering semantics
 * after the memory operation.
 */
static inline int test_and_set_bit_lock(unsigned long nr,
	volatile unsigned long *addr)
{
307
	unsigned long *m = ((unsigned long *)addr) + (nr >> SZLONG_LOG);
308
	int bit = nr & SZLONG_MASK;
309
	unsigned long res, temp;
N
Nick Piggin 已提交
310

311 312 313
	if (!kernel_uses_llsc) {
		res = __mips_test_and_set_bit_lock(nr, addr);
	} else if (R10000_LLSC_WAR) {
N
Nick Piggin 已提交
314
		__asm__ __volatile__(
315
		"	.set	push					\n"
316
		"	.set	arch=r4000				\n"
N
Nick Piggin 已提交
317 318 319 320 321
		"1:	" __LL "%0, %1		# test_and_set_bit	\n"
		"	or	%2, %0, %3				\n"
		"	" __SC	"%2, %1					\n"
		"	beqzl	%2, 1b					\n"
		"	and	%2, %0, %3				\n"
322
		"	.set	pop					\n"
323 324
		: "=&r" (temp), "+m" (*m), "=&r" (res)
		: "r" (1UL << bit)
325
		: __LLSC_CLOBBER);
326
	} else {
327 328
		do {
			__asm__ __volatile__(
329
			"	.set	push				\n"
330
			"	.set	"MIPS_ISA_ARCH_LEVEL"		\n"
331 332 333
			"	" __LL "%0, %1	# test_and_set_bit	\n"
			"	or	%2, %0, %3			\n"
			"	" __SC	"%2, %1				\n"
334
			"	.set	pop				\n"
335
			: "=&r" (temp), "+" GCC_OFF_SMALL_ASM() (*m), "=&r" (res)
336
			: "r" (1UL << bit)
337
			: __LLSC_CLOBBER);
338 339 340
		} while (unlikely(!res));

		res = temp & (1UL << bit);
341
	}
N
Nick Piggin 已提交
342 343 344 345 346

	smp_llsc_mb();

	return res != 0;
}
L
Linus Torvalds 已提交
347 348 349 350 351 352 353 354 355 356 357
/*
 * test_and_clear_bit - Clear a bit and return its old value
 * @nr: Bit to clear
 * @addr: Address to count from
 *
 * This operation is atomic and cannot be reordered.
 * It also implies a memory barrier.
 */
static inline int test_and_clear_bit(unsigned long nr,
	volatile unsigned long *addr)
{
358
	unsigned long *m = ((unsigned long *)addr) + (nr >> SZLONG_LOG);
359
	int bit = nr & SZLONG_MASK;
360
	unsigned long res, temp;
361

362
	smp_mb__before_llsc();
N
Nick Piggin 已提交
363

364 365 366
	if (!kernel_uses_llsc) {
		res = __mips_test_and_clear_bit(nr, addr);
	} else if (R10000_LLSC_WAR) {
L
Linus Torvalds 已提交
367
		__asm__ __volatile__(
368
		"	.set	push					\n"
369
		"	.set	arch=r4000				\n"
L
Linus Torvalds 已提交
370 371 372
		"1:	" __LL	"%0, %1		# test_and_clear_bit	\n"
		"	or	%2, %0, %3				\n"
		"	xor	%2, %3					\n"
R
Ralf Baechle 已提交
373
		"	" __SC	"%2, %1					\n"
L
Linus Torvalds 已提交
374 375
		"	beqzl	%2, 1b					\n"
		"	and	%2, %0, %3				\n"
376
		"	.set	pop					\n"
377
		: "=&r" (temp), "+" GCC_OFF_SMALL_ASM() (*m), "=&r" (res)
378
		: "r" (1UL << bit)
379
		: __LLSC_CLOBBER);
380
#if defined(CONFIG_CPU_MIPSR2) || defined(CONFIG_CPU_MIPSR6)
381
	} else if (__builtin_constant_p(nr)) {
382
		loongson_llsc_mb();
383 384
		do {
			__asm__ __volatile__(
R
Ralf Baechle 已提交
385
			"	" __LL	"%0, %1 # test_and_clear_bit	\n"
386
			"	" __EXT "%2, %0, %3, 1			\n"
R
Ralf Baechle 已提交
387 388
			"	" __INS "%0, $0, %3, 1			\n"
			"	" __SC	"%0, %1				\n"
389
			: "=&r" (temp), "+" GCC_OFF_SMALL_ASM() (*m), "=&r" (res)
390
			: "ir" (bit)
391
			: __LLSC_CLOBBER);
392
		} while (unlikely(!temp));
393
#endif
394
	} else {
395
		loongson_llsc_mb();
396 397
		do {
			__asm__ __volatile__(
398
			"	.set	push				\n"
399
			"	.set	"MIPS_ISA_ARCH_LEVEL"		\n"
R
Ralf Baechle 已提交
400
			"	" __LL	"%0, %1 # test_and_clear_bit	\n"
401 402
			"	or	%2, %0, %3			\n"
			"	xor	%2, %3				\n"
R
Ralf Baechle 已提交
403
			"	" __SC	"%2, %1				\n"
404
			"	.set	pop				\n"
405
			: "=&r" (temp), "+" GCC_OFF_SMALL_ASM() (*m), "=&r" (res)
406
			: "r" (1UL << bit)
407
			: __LLSC_CLOBBER);
408 409 410
		} while (unlikely(!res));

		res = temp & (1UL << bit);
411
	}
412

413
	smp_llsc_mb();
414 415

	return res != 0;
L
Linus Torvalds 已提交
416 417 418 419 420 421 422 423 424 425 426 427 428
}

/*
 * test_and_change_bit - Change a bit and return its old value
 * @nr: Bit to change
 * @addr: Address to count from
 *
 * This operation is atomic and cannot be reordered.
 * It also implies a memory barrier.
 */
static inline int test_and_change_bit(unsigned long nr,
	volatile unsigned long *addr)
{
429
	unsigned long *m = ((unsigned long *)addr) + (nr >> SZLONG_LOG);
430
	int bit = nr & SZLONG_MASK;
431
	unsigned long res, temp;
432

433
	smp_mb__before_llsc();
N
Nick Piggin 已提交
434

435 436 437
	if (!kernel_uses_llsc) {
		res = __mips_test_and_change_bit(nr, addr);
	} else if (R10000_LLSC_WAR) {
L
Linus Torvalds 已提交
438
		__asm__ __volatile__(
439
		"	.set	push					\n"
440
		"	.set	arch=r4000				\n"
441
		"1:	" __LL	"%0, %1		# test_and_change_bit	\n"
L
Linus Torvalds 已提交
442
		"	xor	%2, %0, %3				\n"
443
		"	" __SC	"%2, %1					\n"
L
Linus Torvalds 已提交
444 445
		"	beqzl	%2, 1b					\n"
		"	and	%2, %0, %3				\n"
446
		"	.set	pop					\n"
447
		: "=&r" (temp), "+" GCC_OFF_SMALL_ASM() (*m), "=&r" (res)
448
		: "r" (1UL << bit)
449
		: __LLSC_CLOBBER);
450
	} else {
451
		loongson_llsc_mb();
452 453
		do {
			__asm__ __volatile__(
454
			"	.set	push				\n"
455
			"	.set	"MIPS_ISA_ARCH_LEVEL"		\n"
R
Ralf Baechle 已提交
456
			"	" __LL	"%0, %1 # test_and_change_bit	\n"
457 458
			"	xor	%2, %0, %3			\n"
			"	" __SC	"\t%2, %1			\n"
459
			"	.set	pop				\n"
460
			: "=&r" (temp), "+" GCC_OFF_SMALL_ASM() (*m), "=&r" (res)
461
			: "r" (1UL << bit)
462
			: __LLSC_CLOBBER);
463 464 465
		} while (unlikely(!res));

		res = temp & (1UL << bit);
466
	}
467

468
	smp_llsc_mb();
469 470

	return res != 0;
L
Linus Torvalds 已提交
471 472
}

473
#include <asm-generic/bitops/non-atomic.h>
L
Linus Torvalds 已提交
474

N
Nick Piggin 已提交
475 476 477 478 479 480 481 482 483 484 485
/*
 * __clear_bit_unlock - Clears a bit in memory
 * @nr: Bit to clear
 * @addr: Address to start counting from
 *
 * __clear_bit() is non-atomic and implies release semantics before the memory
 * operation. It can be used for an unlock if no other CPUs can concurrently
 * modify other bits in the word.
 */
static inline void __clear_bit_unlock(unsigned long nr, volatile unsigned long *addr)
{
486
	smp_mb__before_llsc();
N
Nick Piggin 已提交
487
	__clear_bit(nr, addr);
488
	nudge_writes();
N
Nick Piggin 已提交
489 490
}

L
Linus Torvalds 已提交
491
/*
492
 * Return the bit position (0..63) of the most significant 1 bit in a word
493 494
 * Returns -1 if no 1 bit exists
 */
495
static __always_inline unsigned long __fls(unsigned long word)
496
{
497
	int num;
498

499
	if (BITS_PER_LONG == 32 && !__builtin_constant_p(word) &&
500
	    __builtin_constant_p(cpu_has_clo_clz) && cpu_has_clo_clz) {
501
		__asm__(
502
		"	.set	push					\n"
503
		"	.set	"MIPS_ISA_LEVEL"			\n"
504 505
		"	clz	%0, %1					\n"
		"	.set	pop					\n"
506 507
		: "=r" (num)
		: "r" (word));
508

509
		return 31 - num;
510 511
	}

512
	if (BITS_PER_LONG == 64 && !__builtin_constant_p(word) &&
513 514 515
	    __builtin_constant_p(cpu_has_mips64) && cpu_has_mips64) {
		__asm__(
		"	.set	push					\n"
516
		"	.set	"MIPS_ISA_LEVEL"			\n"
517 518 519 520
		"	dclz	%0, %1					\n"
		"	.set	pop					\n"
		: "=r" (num)
		: "r" (word));
521

522 523 524 525
		return 63 - num;
	}

	num = BITS_PER_LONG - 1;
526

527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551
#if BITS_PER_LONG == 64
	if (!(word & (~0ul << 32))) {
		num -= 32;
		word <<= 32;
	}
#endif
	if (!(word & (~0ul << (BITS_PER_LONG-16)))) {
		num -= 16;
		word <<= 16;
	}
	if (!(word & (~0ul << (BITS_PER_LONG-8)))) {
		num -= 8;
		word <<= 8;
	}
	if (!(word & (~0ul << (BITS_PER_LONG-4)))) {
		num -= 4;
		word <<= 4;
	}
	if (!(word & (~0ul << (BITS_PER_LONG-2)))) {
		num -= 2;
		word <<= 2;
	}
	if (!(word & (~0ul << (BITS_PER_LONG-1))))
		num -= 1;
	return num;
552 553 554 555
}

/*
 * __ffs - find first bit in word.
L
Linus Torvalds 已提交
556 557
 * @word: The word to search
 *
558 559
 * Returns 0..SZLONG-1
 * Undefined if no bit exists, so code should check against 0 first.
L
Linus Torvalds 已提交
560
 */
561
static __always_inline unsigned long __ffs(unsigned long word)
L
Linus Torvalds 已提交
562
{
R
Ralf Baechle 已提交
563
	return __fls(word & -word);
L
Linus Torvalds 已提交
564 565 566
}

/*
567
 * fls - find last bit set.
L
Linus Torvalds 已提交
568 569
 * @word: The word to search
 *
570 571
 * This is defined the same way as ffs.
 * Note fls(0) = 0, fls(1) = 1, fls(0x80000000) = 32.
L
Linus Torvalds 已提交
572
 */
573
static inline int fls(unsigned int x)
L
Linus Torvalds 已提交
574
{
575
	int r;
576

577 578
	if (!__builtin_constant_p(x) &&
	    __builtin_constant_p(cpu_has_clo_clz) && cpu_has_clo_clz) {
579 580
		__asm__(
		"	.set	push					\n"
581
		"	.set	"MIPS_ISA_LEVEL"			\n"
582 583 584 585
		"	clz	%0, %1					\n"
		"	.set	pop					\n"
		: "=r" (x)
		: "r" (x));
L
Linus Torvalds 已提交
586

587 588
		return 32 - x;
	}
589

590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613
	r = 32;
	if (!x)
		return 0;
	if (!(x & 0xffff0000u)) {
		x <<= 16;
		r -= 16;
	}
	if (!(x & 0xff000000u)) {
		x <<= 8;
		r -= 8;
	}
	if (!(x & 0xf0000000u)) {
		x <<= 4;
		r -= 4;
	}
	if (!(x & 0xc0000000u)) {
		x <<= 2;
		r -= 2;
	}
	if (!(x & 0x80000000u)) {
		x <<= 1;
		r -= 1;
	}
	return r;
614
}
615

616
#include <asm-generic/bitops/fls64.h>
617 618

/*
619
 * ffs - find first bit set.
620 621
 * @word: The word to search
 *
622 623 624
 * This is defined the same way as
 * the libc and compiler builtin ffs routines, therefore
 * differs in spirit from the above ffz (man ffs).
625
 */
626
static inline int ffs(int word)
627
{
628 629
	if (!word)
		return 0;
630

631
	return fls(word & -word);
632 633
}

634
#include <asm-generic/bitops/ffz.h>
635
#include <asm-generic/bitops/find.h>
L
Linus Torvalds 已提交
636 637 638

#ifdef __KERNEL__

639
#include <asm-generic/bitops/sched.h>
640 641 642 643

#include <asm/arch_hweight.h>
#include <asm-generic/bitops/const_hweight.h>

644
#include <asm-generic/bitops/le.h>
645
#include <asm-generic/bitops/ext2-atomic.h>
L
Linus Torvalds 已提交
646 647 648 649

#endif /* __KERNEL__ */

#endif /* _ASM_BITOPS_H */