clean up unused and inconsistent atomics in arch dirs
[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 int a_swap(volatile int *x, int v)
52 {
53         int old, dummy;
54         __asm__ __volatile__(
55                 ".set push\n"
56                 ".set mips2\n"
57                 ".set noreorder\n"
58                 "       sync\n"
59                 "1:     ll %0, %2\n"
60                 "       addu %1, %3, $0\n"
61                 "       sc %1, %2\n"
62                 "       beq %1, $0, 1b\n"
63                 "       nop\n"
64                 "       sync\n"
65                 ".set pop\n"
66                 : "=&r"(old), "=&r"(dummy), "+m"(*x) : "r"(v) : "memory" );
67         return old;
68 }
69
70 static inline int a_fetch_add(volatile int *x, int v)
71 {
72         int old, dummy;
73         __asm__ __volatile__(
74                 ".set push\n"
75                 ".set mips2\n"
76                 ".set noreorder\n"
77                 "       sync\n"
78                 "1:     ll %0, %2\n"
79                 "       addu %1, %0, %3\n"
80                 "       sc %1, %2\n"
81                 "       beq %1, $0, 1b\n"
82                 "       nop\n"
83                 "       sync\n"
84                 ".set pop\n"
85                 : "=&r"(old), "=&r"(dummy), "+m"(*x) : "r"(v) : "memory" );
86         return old;
87 }
88
89 static inline void a_inc(volatile int *x)
90 {
91         int dummy;
92         __asm__ __volatile__(
93                 ".set push\n"
94                 ".set mips2\n"
95                 ".set noreorder\n"
96                 "       sync\n"
97                 "1:     ll %0, %1\n"
98                 "       addu %0, %0, 1\n"
99                 "       sc %0, %1\n"
100                 "       beq %0, $0, 1b\n"
101                 "       nop\n"
102                 "       sync\n"
103                 ".set pop\n"
104                 : "=&r"(dummy), "+m"(*x) : : "memory" );
105 }
106
107 static inline void a_dec(volatile int *x)
108 {
109         int dummy;
110         __asm__ __volatile__(
111                 ".set push\n"
112                 ".set mips2\n"
113                 ".set noreorder\n"
114                 "       sync\n"
115                 "1:     ll %0, %1\n"
116                 "       subu %0, %0, 1\n"
117                 "       sc %0, %1\n"
118                 "       beq %0, $0, 1b\n"
119                 "       nop\n"
120                 "       sync\n"
121                 ".set pop\n"
122                 : "=&r"(dummy), "+m"(*x) : : "memory" );
123 }
124
125 static inline void a_store(volatile int *p, int x)
126 {
127         __asm__ __volatile__(
128                 ".set push\n"
129                 ".set mips2\n"
130                 ".set noreorder\n"
131                 "       sync\n"
132                 "       sw %1, %0\n"
133                 "       sync\n"
134                 ".set pop\n"
135                 : "+m"(*p) : "r"(x) : "memory" );
136 }
137
138 static inline void a_spin()
139 {
140 }
141
142 static inline void a_crash()
143 {
144         *(volatile char *)0=0;
145 }
146
147 static inline void a_and(volatile int *p, int v)
148 {
149         int dummy;
150         __asm__ __volatile__(
151                 ".set push\n"
152                 ".set mips2\n"
153                 ".set noreorder\n"
154                 "       sync\n"
155                 "1:     ll %0, %1\n"
156                 "       and %0, %0, %2\n"
157                 "       sc %0, %1\n"
158                 "       beq %0, $0, 1b\n"
159                 "       nop\n"
160                 "       sync\n"
161                 ".set pop\n"
162                 : "=&r"(dummy), "+m"(*p) : "r"(v) : "memory" );
163 }
164
165 static inline void a_or(volatile int *p, int v)
166 {
167         int dummy;
168         __asm__ __volatile__(
169                 ".set push\n"
170                 ".set mips2\n"
171                 ".set noreorder\n"
172                 "       sync\n"
173                 "1:     ll %0, %1\n"
174                 "       or %0, %0, %2\n"
175                 "       sc %0, %1\n"
176                 "       beq %0, $0, 1b\n"
177                 "       nop\n"
178                 "       sync\n"
179                 ".set pop\n"
180                 : "=&r"(dummy), "+m"(*p) : "r"(v) : "memory" );
181 }
182
183 static inline void a_or_l(volatile void *p, long v)
184 {
185         a_or(p, v);
186 }
187
188 static inline void a_and_64(volatile uint64_t *p, uint64_t v)
189 {
190         union { uint64_t v; uint32_t r[2]; } u = { v };
191         a_and((int *)p, u.r[0]);
192         a_and((int *)p+1, u.r[1]);
193 }
194
195 static inline void a_or_64(volatile uint64_t *p, uint64_t v)
196 {
197         union { uint64_t v; uint32_t r[2]; } u = { v };
198         a_or((int *)p, u.r[0]);
199         a_or((int *)p+1, u.r[1]);
200 }
201
202 #endif