9dcd1555af68dd434a1bea5976837c26c57cb993
[musl] / arch / mips / atomic.h
1 #ifndef _INTERNAL_ATOMIC_H
2 #define _INTERNAL_ATOMIC_H
3
4 #include <stdint.h>
5
6 static inline int a_ctz_l(unsigned long x)
7 {
8         static const char debruijn32[32] = {
9                 0, 1, 23, 2, 29, 24, 19, 3, 30, 27, 25, 11, 20, 8, 4, 13,
10                 31, 22, 28, 18, 26, 10, 7, 12, 21, 17, 9, 6, 16, 5, 15, 14
11         };
12         return debruijn32[(x&-x)*0x076be629 >> 27];
13 }
14
15 static inline int a_ctz_64(uint64_t x)
16 {
17         uint32_t y = x;
18         if (!y) {
19                 y = x>>32;
20                 return 32 + a_ctz_l(y);
21         }
22         return a_ctz_l(y);
23 }
24
25 static inline int a_cas(volatile int *p, int t, int s)
26 {
27         int dummy;
28         __asm__ __volatile__(
29                 ".set push\n"
30                 ".set mips2\n"
31                 ".set noreorder\n"
32                 "       sync\n"
33                 "1:     ll %0, %2\n"
34                 "       bne %0, %3, 1f\n"
35                 "       addu %1, %4, $0\n"
36                 "       sc %1, %2\n"
37                 "       beq %1, $0, 1b\n"
38                 "       nop\n"
39                 "       sync\n"
40                 "1:     \n"
41                 ".set pop\n"
42                 : "=&r"(t), "=&r"(dummy), "+m"(*p) : "r"(t), "r"(s) : "memory" );
43         return t;
44 }
45
46 static inline void *a_cas_p(volatile void *p, void *t, void *s)
47 {
48         return (void *)a_cas(p, (int)t, (int)s);
49 }
50
51 static inline long a_cas_l(volatile void *p, long t, long s)
52 {
53         return a_cas(p, t, s);
54 }
55
56
57 static inline int a_swap(volatile int *x, int v)
58 {
59         int old, dummy;
60         __asm__ __volatile__(
61                 ".set push\n"
62                 ".set mips2\n"
63                 ".set noreorder\n"
64                 "       sync\n"
65                 "1:     ll %0, %2\n"
66                 "       addu %1, %3, $0\n"
67                 "       sc %1, %2\n"
68                 "       beq %1, $0, 1b\n"
69                 "       nop\n"
70                 "       sync\n"
71                 ".set pop\n"
72                 : "=&r"(old), "=&r"(dummy), "+m"(*x) : "r"(v) : "memory" );
73         return old;
74 }
75
76 static inline int a_fetch_add(volatile int *x, int v)
77 {
78         int old, dummy;
79         __asm__ __volatile__(
80                 ".set push\n"
81                 ".set mips2\n"
82                 ".set noreorder\n"
83                 "       sync\n"
84                 "1:     ll %0, %2\n"
85                 "       addu %1, %0, %3\n"
86                 "       sc %1, %2\n"
87                 "       beq %1, $0, 1b\n"
88                 "       nop\n"
89                 "       sync\n"
90                 ".set pop\n"
91                 : "=&r"(old), "=&r"(dummy), "+m"(*x) : "r"(v) : "memory" );
92         return old;
93 }
94
95 static inline void a_inc(volatile int *x)
96 {
97         int dummy;
98         __asm__ __volatile__(
99                 ".set push\n"
100                 ".set mips2\n"
101                 ".set noreorder\n"
102                 "       sync\n"
103                 "1:     ll %0, %1\n"
104                 "       addu %0, %0, 1\n"
105                 "       sc %0, %1\n"
106                 "       beq %0, $0, 1b\n"
107                 "       nop\n"
108                 "       sync\n"
109                 ".set pop\n"
110                 : "=&r"(dummy), "+m"(*x) : : "memory" );
111 }
112
113 static inline void a_dec(volatile int *x)
114 {
115         int dummy;
116         __asm__ __volatile__(
117                 ".set push\n"
118                 ".set mips2\n"
119                 ".set noreorder\n"
120                 "       sync\n"
121                 "1:     ll %0, %1\n"
122                 "       subu %0, %0, 1\n"
123                 "       sc %0, %1\n"
124                 "       beq %0, $0, 1b\n"
125                 "       nop\n"
126                 "       sync\n"
127                 ".set pop\n"
128                 : "=&r"(dummy), "+m"(*x) : : "memory" );
129 }
130
131 static inline void a_store(volatile int *p, int x)
132 {
133         __asm__ __volatile__(
134                 ".set push\n"
135                 ".set mips2\n"
136                 ".set noreorder\n"
137                 "       sync\n"
138                 "       sw %1, %0\n"
139                 "       sync\n"
140                 ".set pop\n"
141                 : "+m"(*p) : "r"(x) : "memory" );
142 }
143
144 static inline void a_spin()
145 {
146 }
147
148 static inline void a_crash()
149 {
150         *(volatile char *)0=0;
151 }
152
153 static inline void a_and(volatile int *p, int v)
154 {
155         int dummy;
156         __asm__ __volatile__(
157                 ".set push\n"
158                 ".set mips2\n"
159                 ".set noreorder\n"
160                 "       sync\n"
161                 "1:     ll %0, %1\n"
162                 "       and %0, %0, %2\n"
163                 "       sc %0, %1\n"
164                 "       beq %0, $0, 1b\n"
165                 "       nop\n"
166                 "       sync\n"
167                 ".set pop\n"
168                 : "=&r"(dummy), "+m"(*p) : "r"(v) : "memory" );
169 }
170
171 static inline void a_or(volatile int *p, int v)
172 {
173         int dummy;
174         __asm__ __volatile__(
175                 ".set push\n"
176                 ".set mips2\n"
177                 ".set noreorder\n"
178                 "       sync\n"
179                 "1:     ll %0, %1\n"
180                 "       or %0, %0, %2\n"
181                 "       sc %0, %1\n"
182                 "       beq %0, $0, 1b\n"
183                 "       nop\n"
184                 "       sync\n"
185                 ".set pop\n"
186                 : "=&r"(dummy), "+m"(*p) : "r"(v) : "memory" );
187 }
188
189 static inline void a_or_l(volatile void *p, long v)
190 {
191         a_or(p, v);
192 }
193
194 static inline void a_and_64(volatile uint64_t *p, uint64_t v)
195 {
196         union { uint64_t v; uint32_t r[2]; } u = { v };
197         a_and((int *)p, u.r[0]);
198         a_and((int *)p+1, u.r[1]);
199 }
200
201 static inline void a_or_64(volatile uint64_t *p, uint64_t v)
202 {
203         union { uint64_t v; uint32_t r[2]; } u = { v };
204         a_or((int *)p, u.r[0]);
205         a_or((int *)p+1, u.r[1]);
206 }
207
208 #endif