1/* SPDX-License-Identifier: GPL-2.0-only */
2/*
3 *  Port on Texas Instruments TMS320C6x architecture
4 *
5 *  Copyright (C) 2004, 2009, 2010 Texas Instruments Incorporated
6 *  Author: Aurelien Jacquiot (aurelien.jacquiot@jaluna.com)
7 */
8#ifndef _ASM_C6X_BITOPS_H
9#define _ASM_C6X_BITOPS_H
10
11#ifdef __KERNEL__
12
13#include <linux/bitops.h>
14#include <asm/byteorder.h>
15#include <asm/barrier.h>
16
17/*
18 * We are lucky, DSP is perfect for bitops: do it in 3 cycles
19 */
20
21/**
22 * __ffs - find first bit in word.
23 * @word: The word to search
24 *
25 * Undefined if no bit exists, so code should check against 0 first.
26 * Note __ffs(0) = undef, __ffs(1) = 0, __ffs(0x80000000) = 31.
27 *
28 */
29static inline unsigned long __ffs(unsigned long x)
30{
31	asm (" bitr  .M1  %0,%0\n"
32	     " nop\n"
33	     " lmbd  .L1  1,%0,%0\n"
34	     : "+a"(x));
35
36	return x;
37}
38
39/*
40 * ffz - find first zero in word.
41 * @word: The word to search
42 *
43 * Undefined if no zero exists, so code should check against ~0UL first.
44 */
45#define ffz(x) __ffs(~(x))
46
47/**
48 * fls - find last (most-significant) bit set
49 * @x: the word to search
50 *
51 * This is defined the same way as ffs.
52 * Note fls(0) = 0, fls(1) = 1, fls(0x80000000) = 32.
53 */
54static inline int fls(unsigned int x)
55{
56	if (!x)
57		return 0;
58
59	asm (" lmbd  .L1  1,%0,%0\n" : "+a"(x));
60
61	return 32 - x;
62}
63
64/**
65 * ffs - find first bit set
66 * @x: the word to search
67 *
68 * This is defined the same way as
69 * the libc and compiler builtin ffs routines, therefore
70 * differs in spirit from the above ffz (man ffs).
71 * Note ffs(0) = 0, ffs(1) = 1, ffs(0x80000000) = 32.
72 */
73static inline int ffs(int x)
74{
75	if (!x)
76		return 0;
77
78	return __ffs(x) + 1;
79}
80
81#include <asm-generic/bitops/__fls.h>
82#include <asm-generic/bitops/fls64.h>
83#include <asm-generic/bitops/find.h>
84
85#include <asm-generic/bitops/sched.h>
86#include <asm-generic/bitops/hweight.h>
87#include <asm-generic/bitops/lock.h>
88
89#include <asm-generic/bitops/atomic.h>
90#include <asm-generic/bitops/non-atomic.h>
91#include <asm-generic/bitops/le.h>
92#include <asm-generic/bitops/ext2-atomic.h>
93
94#endif /* __KERNEL__ */
95#endif /* _ASM_C6X_BITOPS_H */
96