fix broken mips a_fetch_add
[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 noreorder\n"
31                 "1:     ll %0, 0(%2)\n"
32                 "       bne %0, %3, 1f\n"
33                 "       addu %1, %4, $0\n"
34                 "       sc %1, 0(%2)\n"
35                 "       beq %1, $0, 1b\n"
36                 "       nop\n"
37                 "1:     \n"
38                 ".set pop\n"
39                 : "=&r"(t), "=&r"(dummy) : "r"(p), "r"(t), "r"(s) : "memory" );
40         return t;
41 }
42
43 static inline void *a_cas_p(volatile void *p, void *t, void *s)
44 {
45         return (void *)a_cas(p, (int)t, (int)s);
46 }
47
48 static inline long a_cas_l(volatile void *p, long t, long s)
49 {
50         return a_cas(p, t, s);
51 }
52
53
54 static inline int a_swap(volatile int *x, int v)
55 {
56         int old, dummy;
57         __asm__ __volatile__(
58                 ".set push\n"
59                 ".set noreorder\n"
60                 "1:     ll %0, 0(%2)\n"
61                 "       addu %1, %3, $0\n"
62                 "       sc %1, 0(%2)\n"
63                 "       beq %1, $0, 1b\n"
64                 "       nop\n"
65                 "1:     \n"
66                 ".set pop\n"
67                 : "=&r"(old), "=&r"(dummy) : "r"(x), "r"(v) : "memory" );
68         return old;
69 }
70
71 static inline int a_fetch_add(volatile int *x, int v)
72 {
73         int old, dummy;
74         __asm__ __volatile__(
75                 ".set push\n"
76                 ".set noreorder\n"
77                 "1:     ll %0, 0(%2)\n"
78                 "       addu %1, %0, %3\n"
79                 "       sc %1, 0(%2)\n"
80                 "       beq %1, $0, 1b\n"
81                 "       nop\n"
82                 "1:     \n"
83                 ".set pop\n"
84                 : "=&r"(old), "=&r"(dummy) : "r"(x), "r"(v) : "memory" );
85         return old;
86 }
87
88 static inline void a_inc(volatile int *x)
89 {
90         int dummy;
91         __asm__ __volatile__(
92                 ".set push\n"
93                 ".set noreorder\n"
94                 "1:     ll %0, 0(%1)\n"
95                 "       addu %0, %0, 1\n"
96                 "       sc %0, 0(%1)\n"
97                 "       beq %0, $0, 1b\n"
98                 "       nop\n"
99                 "1:     \n"
100                 ".set pop\n"
101                 : "=&r"(dummy) : "r"(x) : "memory" );
102 }
103
104 static inline void a_dec(volatile int *x)
105 {
106         int dummy;
107         __asm__ __volatile__(
108                 ".set push\n"
109                 ".set noreorder\n"
110                 "1:     ll %0, 0(%1)\n"
111                 "       subu %0, %0, 1\n"
112                 "       sc %0, 0(%1)\n"
113                 "       beq %0, $0, 1b\n"
114                 "       nop\n"
115                 "1:     \n"
116                 ".set pop\n"
117                 : "=&r"(dummy) : "r"(x) : "memory" );
118 }
119
120 static inline void a_store(volatile int *p, int x)
121 {
122         int dummy;
123         __asm__ __volatile__(
124                 ".set push\n"
125                 ".set noreorder\n"
126                 "1:     ll %0, 0(%1)\n"
127                 "       addu %0, %2, $0\n"
128                 "       sc %0, 0(%1)\n"
129                 "       beq %0, $0, 1b\n"
130                 "       nop\n"
131                 "1:     \n"
132                 ".set pop\n"
133                 : "=&r"(dummy) : "r"(p), "r"(x) : "memory" );
134 }
135
136 static inline void a_spin()
137 {
138 }
139
140 static inline void a_crash()
141 {
142         *(volatile char *)0=0;
143 }
144
145 static inline void a_and(volatile int *p, int v)
146 {
147         int dummy;
148         __asm__ __volatile__(
149                 ".set push\n"
150                 ".set noreorder\n"
151                 "1:     ll %0, 0(%1)\n"
152                 "       and %0, %0, %2\n"
153                 "       sc %0, 0(%1)\n"
154                 "       beq %0, $0, 1b\n"
155                 "       nop\n"
156                 "1:     \n"
157                 ".set pop\n"
158                 : "=&r"(dummy) : "r"(p), "r"(v) : "memory" );
159 }
160
161 static inline void a_or(volatile int *p, int v)
162 {
163         int dummy;
164         __asm__ __volatile__(
165                 ".set push\n"
166                 ".set noreorder\n"
167                 "1:     ll %0, 0(%1)\n"
168                 "       or %0, %0, %2\n"
169                 "       sc %0, 0(%1)\n"
170                 "       beq %0, $0, 1b\n"
171                 "       nop\n"
172                 "1:     \n"
173                 ".set pop\n"
174                 : "=&r"(dummy) : "r"(p), "r"(v) : "memory" );
175 }
176
177 static inline void a_and_64(volatile uint64_t *p, uint64_t v)
178 {
179         union { uint64_t v; uint32_t r[2]; } u = { v };
180         a_and((int *)p, u.r[0]);
181         a_and((int *)p+1, u.r[1]);
182 }
183
184 static inline void a_or_64(volatile uint64_t *p, uint64_t v)
185 {
186         union { uint64_t v; uint32_t r[2]; } u = { v };
187         a_or((int *)p, u.r[0]);
188         a_or((int *)p+1, u.r[1]);
189 }
190
191 #endif