blob: 1c16792cee1dbaf239948ac8971dd2ad7290865e [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001#ifndef __ASM_SH_BITOPS_H
2#define __ASM_SH_BITOPS_H
3
4#ifdef __KERNEL__
5#include <asm/system.h>
6/* For __swab32 */
7#include <asm/byteorder.h>
8
Paul Mundte4c2cfe2006-09-27 12:31:01 +09009static inline void set_bit(int nr, volatile void * addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -070010{
11 int mask;
12 volatile unsigned int *a = addr;
13 unsigned long flags;
14
15 a += nr >> 5;
16 mask = 1 << (nr & 0x1f);
17 local_irq_save(flags);
18 *a |= mask;
19 local_irq_restore(flags);
20}
21
Linus Torvalds1da177e2005-04-16 15:20:36 -070022/*
23 * clear_bit() doesn't provide any barrier for the compiler.
24 */
25#define smp_mb__before_clear_bit() barrier()
26#define smp_mb__after_clear_bit() barrier()
Paul Mundte4c2cfe2006-09-27 12:31:01 +090027static inline void clear_bit(int nr, volatile void * addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -070028{
29 int mask;
30 volatile unsigned int *a = addr;
31 unsigned long flags;
32
33 a += nr >> 5;
34 mask = 1 << (nr & 0x1f);
35 local_irq_save(flags);
36 *a &= ~mask;
37 local_irq_restore(flags);
38}
39
Paul Mundte4c2cfe2006-09-27 12:31:01 +090040static inline void change_bit(int nr, volatile void * addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -070041{
42 int mask;
43 volatile unsigned int *a = addr;
44 unsigned long flags;
45
46 a += nr >> 5;
47 mask = 1 << (nr & 0x1f);
48 local_irq_save(flags);
49 *a ^= mask;
50 local_irq_restore(flags);
51}
52
Paul Mundte4c2cfe2006-09-27 12:31:01 +090053static inline int test_and_set_bit(int nr, volatile void * addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -070054{
55 int mask, retval;
56 volatile unsigned int *a = addr;
57 unsigned long flags;
58
59 a += nr >> 5;
60 mask = 1 << (nr & 0x1f);
61 local_irq_save(flags);
62 retval = (mask & *a) != 0;
63 *a |= mask;
64 local_irq_restore(flags);
65
66 return retval;
67}
68
Paul Mundte4c2cfe2006-09-27 12:31:01 +090069static inline int test_and_clear_bit(int nr, volatile void * addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -070070{
71 int mask, retval;
72 volatile unsigned int *a = addr;
73 unsigned long flags;
74
75 a += nr >> 5;
76 mask = 1 << (nr & 0x1f);
77 local_irq_save(flags);
78 retval = (mask & *a) != 0;
79 *a &= ~mask;
80 local_irq_restore(flags);
81
82 return retval;
83}
84
Paul Mundte4c2cfe2006-09-27 12:31:01 +090085static inline int test_and_change_bit(int nr, volatile void * addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -070086{
87 int mask, retval;
88 volatile unsigned int *a = addr;
89 unsigned long flags;
90
91 a += nr >> 5;
92 mask = 1 << (nr & 0x1f);
93 local_irq_save(flags);
94 retval = (mask & *a) != 0;
95 *a ^= mask;
96 local_irq_restore(flags);
97
98 return retval;
99}
100
Akinobu Mitae2268c72006-03-26 01:39:35 -0800101#include <asm-generic/bitops/non-atomic.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102
Paul Mundte4c2cfe2006-09-27 12:31:01 +0900103static inline unsigned long ffz(unsigned long word)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104{
105 unsigned long result;
106
107 __asm__("1:\n\t"
108 "shlr %1\n\t"
109 "bt/s 1b\n\t"
110 " add #1, %0"
111 : "=r" (result), "=r" (word)
112 : "0" (~0L), "1" (word)
113 : "t");
114 return result;
115}
116
117/**
118 * __ffs - find first bit in word.
119 * @word: The word to search
120 *
121 * Undefined if no bit exists, so code should check against 0 first.
122 */
Paul Mundte4c2cfe2006-09-27 12:31:01 +0900123static inline unsigned long __ffs(unsigned long word)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124{
125 unsigned long result;
126
127 __asm__("1:\n\t"
128 "shlr %1\n\t"
129 "bf/s 1b\n\t"
130 " add #1, %0"
131 : "=r" (result), "=r" (word)
132 : "0" (~0L), "1" (word)
133 : "t");
134 return result;
135}
136
Akinobu Mitae2268c72006-03-26 01:39:35 -0800137#include <asm-generic/bitops/find.h>
138#include <asm-generic/bitops/ffs.h>
139#include <asm-generic/bitops/hweight.h>
140#include <asm-generic/bitops/sched.h>
141#include <asm-generic/bitops/ext2-non-atomic.h>
142#include <asm-generic/bitops/ext2-atomic.h>
143#include <asm-generic/bitops/minix.h>
144#include <asm-generic/bitops/fls.h>
145#include <asm-generic/bitops/fls64.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146
147#endif /* __KERNEL__ */
148
149#endif /* __ASM_SH_BITOPS_H */