1 /* SPDX-License-Identifier: GPL-2.0-only */
3 * Copyright (C) 2004, 2007-2010, 2011-2012 Synopsys, Inc. (www.synopsys.com)
9 #ifndef _LINUX_BITOPS_H
10 #error only <linux/bitops.h> can be included directly
15 #include <linux/types.h>
16 #include <linux/compiler.h>
18 #ifdef CONFIG_ISA_ARCOMPACT
21 * Count the number of zeros, starting from MSB
22 * Helper for fls( ) friends
23 * This is a pure count, so (1-32) or (0-31) doesn't apply
24 * It could be 0 to 32, based on num of 0's in there
25 * clz(0x8000_0000) = 0, clz(0xFFFF_FFFF)=0, clz(0) = 32, clz(1) = 31
27 static inline __attribute__ ((const)) int clz(unsigned int x
)
42 static inline int constant_fls(unsigned int x
)
48 if (!(x
& 0xffff0000u
)) {
52 if (!(x
& 0xff000000u
)) {
56 if (!(x
& 0xf0000000u
)) {
60 if (!(x
& 0xc0000000u
)) {
64 if (!(x
& 0x80000000u
))
70 * fls = Find Last Set in word
72 * fls(1) = 1, fls(0x80000000) = 32, fls(0) = 0
74 static inline __attribute__ ((const)) int fls(unsigned int x
)
76 if (__builtin_constant_p(x
))
77 return constant_fls(x
);
83 * __fls: Similar to fls, but zero based (0-31)
85 static inline __attribute__ ((const)) unsigned long __fls(unsigned long x
)
94 * ffs = Find First Set in word (LSB to MSB)
95 * @result: [1-32], 0 if all 0's
97 #define ffs(x) ({ unsigned long __t = (x); fls(__t & -__t); })
100 * __ffs: Similar to ffs, but zero based (0-31)
102 static inline __attribute__ ((const)) unsigned long __ffs(unsigned long word
)
107 return ffs(word
) - 1;
110 #else /* CONFIG_ISA_ARCV2 */
113 * fls = Find Last Set in word
115 * fls(1) = 1, fls(0x80000000) = 32, fls(0) = 0
117 static inline __attribute__ ((const)) int fls(unsigned int x
)
122 " fls.f %0, %1 \n" /* 0:31; 0(Z) if src 0 */
123 " add.nz %0, %0, 1 \n" /* 0:31 -> 1:32 */
124 : "=r"(n
) /* Early clobber not needed */
132 * __fls: Similar to fls, but zero based (0-31). Also 0 if no bit set
134 static inline __attribute__ ((const)) unsigned long __fls(unsigned long x
)
136 /* FLS insn has exactly same semantics as the API */
137 return __builtin_arc_fls(x
);
141 * ffs = Find First Set in word (LSB to MSB)
142 * @result: [1-32], 0 if all 0's
144 static inline __attribute__ ((const)) int ffs(unsigned int x
)
149 " ffs.f %0, %1 \n" /* 0:31; 31(Z) if src 0 */
150 " add.nz %0, %0, 1 \n" /* 0:31 -> 1:32 */
151 " mov.z %0, 0 \n" /* 31(Z)-> 0 */
152 : "=r"(n
) /* Early clobber not needed */
160 * __ffs: Similar to ffs, but zero based (0-31)
162 static inline __attribute__ ((const)) unsigned long __ffs(unsigned long x
)
167 " ffs.f %0, %1 \n" /* 0:31; 31(Z) if src 0 */
168 " mov.z %0, 0 \n" /* 31(Z)-> 0 */
177 #endif /* CONFIG_ISA_ARCOMPACT */
180 * ffz = Find First Zero in word.
181 * @return:[0-31], 32 if all 1's
183 #define ffz(x) __ffs(~(x))
185 #include <asm-generic/bitops/hweight.h>
186 #include <asm-generic/bitops/fls64.h>
187 #include <asm-generic/bitops/sched.h>
188 #include <asm-generic/bitops/lock.h>
189 #include <asm-generic/bitops/atomic.h>
190 #include <asm-generic/bitops/non-atomic.h>
192 #include <asm-generic/bitops/le.h>
193 #include <asm-generic/bitops/ext2-atomic-setbit.h>
195 #endif /* !__ASSEMBLY__ */