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.
17 #include <linux/compiler.h>
18 #include <asm/byteorder.h>
22 #ifndef _LINUX_BITOPS_H
23 #error only <linux/bitops.h> can be included directly
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);
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);
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);
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
;
75 mask
= 1 << (nr
& 31);
79 static inline void __set_bit(unsigned long nr
, volatile void *addr
)
81 volatile unsigned long *a
= addr
;
85 mask
= 1 << (nr
& 31);
89 static inline void __change_bit(unsigned long nr
, volatile void *addr
)
91 volatile unsigned long *a
= addr
;
95 mask
= 1 << (nr
& 31);
99 static inline int __test_and_clear_bit(unsigned long nr
, volatile void *addr
)
101 volatile unsigned long *a
= addr
;
105 mask
= 1 << (nr
& 31);
106 retval
= (mask
& *a
) != 0;
111 static inline int __test_and_set_bit(unsigned long nr
, volatile void *addr
)
113 volatile unsigned long *a
= addr
;
117 mask
= 1 << (nr
& 31);
118 retval
= (mask
& *a
) != 0;
123 static inline int __test_and_change_bit(unsigned long nr
, volatile void *addr
)
125 volatile unsigned long *a
= addr
;
129 mask
= 1 << (nr
& 31);
130 retval
= (mask
& *a
) != 0;
136 * This routine doesn't need to be atomic.
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
;
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
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" \
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))
199 struct { u32 h
, l
; };
205 asm(" subcc.p %3,gr0,gr0,icc0 \n"
206 " subcc %4,gr0,gr0,icc1 \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"
215 " cscan.p %4,gr0,%0 ,cc4,1 \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"
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))
237 /* Note: (x & -x) gives us a mask that is the least significant
238 * (rightmost) 1-bit of the value in 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
)
254 asm("scan %1,gr0,%0" : "=r"(bit
) : "r"(x
& -x
));
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
)
267 asm("scan %1,gr0,%0" : "=r"(bit
) : "r"(word
));
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
)
280 asm("scan %1,gr0,%0" : "=r"(bit
) : "r"(n
));
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
)
294 struct { u32 h
, l
; };
300 asm(" subcc %3,gr0,gr0,icc0 \n"
302 " cscan.p %3,gr0,%0 ,cc4,0 \n"
304 " cscan.p %4,gr0,%0 ,cc4,1 \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
)
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 */