blob: 95167bd45acb877b4de070967a316097d4baae97 [file] [log] [blame]
Nobuhiro Iwamatsub02bad12007-09-23 02:12:30 +09001#ifndef __ASM_SH_BITOPS_H
2#define __ASM_SH_BITOPS_H
Nobuhiro Iwamatsu0b135cf2007-05-13 20:58:00 +09003
Nobuhiro Iwamatsub02bad12007-09-23 02:12:30 +09004#ifdef __KERNEL__
Nobuhiro Iwamatsub02bad12007-09-23 02:12:30 +09005#include <asm/irqflags.h>
6/* For __swab32 */
7#include <asm/byteorder.h>
Nobuhiro Iwamatsu0b135cf2007-05-13 20:58:00 +09008
Nobuhiro Iwamatsub02bad12007-09-23 02:12:30 +09009static inline void set_bit(int nr, volatile void * addr)
10{
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
22/*
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()
27static inline void clear_bit(int nr, volatile void * addr)
28{
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
40static inline void change_bit(int nr, volatile void * addr)
41{
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
53static inline int test_and_set_bit(int nr, volatile void * addr)
54{
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
69static inline int test_and_clear_bit(int nr, volatile void * addr)
70{
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
85static inline int test_and_change_bit(int nr, volatile void * addr)
86{
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
Nobuhiro Iwamatsub02bad12007-09-23 02:12:30 +0900101static inline unsigned long ffz(unsigned long word)
102{
103 unsigned long result;
104
105 __asm__("1:\n\t"
106 "shlr %1\n\t"
107 "bt/s 1b\n\t"
108 " add #1, %0"
109 : "=r" (result), "=r" (word)
110 : "0" (~0L), "1" (word)
111 : "t");
112 return result;
113}
114
115/**
116 * ffs - find first bit in word.
117 * @word: The word to search
118 *
119 * Undefined if no bit exists, so code should check against 0 first.
120 */
Wolfgang Denk61fb15c52007-12-27 01:52:50 +0100121static inline int ffs (int x)
Nobuhiro Iwamatsub02bad12007-09-23 02:12:30 +0900122{
Wolfgang Denk61fb15c52007-12-27 01:52:50 +0100123 int r = 1;
Nobuhiro Iwamatsub02bad12007-09-23 02:12:30 +0900124
Wolfgang Denk61fb15c52007-12-27 01:52:50 +0100125 if (!x)
126 return 0;
127 if (!(x & 0xffff)) {
128 x >>= 16;
129 r += 16;
130 }
131 if (!(x & 0xff)) {
132 x >>= 8;
133 r += 8;
134 }
135 if (!(x & 0xf)) {
136 x >>= 4;
137 r += 4;
138 }
139 if (!(x & 3)) {
140 x >>= 2;
141 r += 2;
142 }
143 if (!(x & 1)) {
144 x >>= 1;
145 r += 1;
146 }
147 return r;
Nobuhiro Iwamatsub02bad12007-09-23 02:12:30 +0900148}
Simon Kagstrom52d61222009-08-24 09:10:12 +0200149#define ffs
150
Nobuhiro Iwamatsub02bad12007-09-23 02:12:30 +0900151#endif /* __KERNEL__ */
152
153#endif /* __ASM_SH_BITOPS_H */