fix microblaze atomic store
[musl] / arch / microblaze / 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         register int old, tmp;
28         __asm__ __volatile__ (
29                 "       addi %0, r0, 0\n"
30                 "1:     lwx %0, %2, r0\n"
31                 "       rsubk %1, %0, %3\n"
32                 "       bnei %1, 1f\n"
33                 "       swx %4, %2, r0\n"
34                 "       addic %1, r0, 0\n"
35                 "       bnei %1, 1b\n"
36                 "1:     "
37                 : "=&r"(old), "=&r"(tmp)
38                 : "r"(p), "r"(t), "r"(s)
39                 : "cc", "memory" );
40         return old;
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 static inline int a_swap(volatile int *x, int v)
54 {
55         register int old, tmp;
56         __asm__ __volatile__ (
57                 "       addi %0, r0, 0\n"
58                 "1:     lwx %0, %2, r0\n"
59                 "       swx %3, %2, r0\n"
60                 "       addic %1, r0, 0\n"
61                 "       bnei %1, 1b\n"
62                 "1:     "
63                 : "=&r"(old), "=&r"(tmp)
64                 : "r"(x), "r"(v)
65                 : "cc", "memory" );
66         return old;
67 }
68
69 static inline int a_fetch_add(volatile int *x, int v)
70 {
71         register int new, tmp;
72         __asm__ __volatile__ (
73                 "       addi %0, r0, 0\n"
74                 "1:     lwx %0, %2, r0\n"
75                 "       addk %0, %0, %3\n"
76                 "       swx %0, %2, r0\n"
77                 "       addic %1, r0, 0\n"
78                 "       bnei %1, 1b\n"
79                 "1:     "
80                 : "=&r"(new), "=&r"(tmp)
81                 : "r"(x), "r"(v)
82                 : "cc", "memory" );
83         return new-v;
84 }
85
86 static inline void a_inc(volatile int *x)
87 {
88         a_fetch_add(x, 1);
89 }
90
91 static inline void a_dec(volatile int *x)
92 {
93         a_fetch_add(x, -1);
94 }
95
96 static inline void a_store(volatile int *p, int x)
97 {
98         __asm__ __volatile__ (
99                 "swi %1, %0"
100                 : "=m"(*p) : "r"(x) : "memory" );
101 }
102
103 static inline void a_spin()
104 {
105 }
106
107 static inline void a_crash()
108 {
109         *(volatile char *)0=0;
110 }
111
112 static inline void a_and(volatile int *p, int v)
113 {
114         int old;
115         do old = *p;
116         while (a_cas(p, old, old&v) != old);
117 }
118
119 static inline void a_or(volatile int *p, int v)
120 {
121         int old;
122         do old = *p;
123         while (a_cas(p, old, old|v) != old);
124 }
125
126 static inline void a_or_l(volatile void *p, long v)
127 {
128         a_or(p, v);
129 }
130
131 static inline void a_and_64(volatile uint64_t *p, uint64_t v)
132 {
133         union { uint64_t v; uint32_t r[2]; } u = { v };
134         a_and((int *)p, u.r[0]);
135         a_and((int *)p+1, u.r[1]);
136 }
137
138 static inline void a_or_64(volatile uint64_t *p, uint64_t v)
139 {
140         union { uint64_t v; uint32_t r[2]; } u = { v };
141         a_or((int *)p, u.r[0]);
142         a_or((int *)p+1, u.r[1]);
143 }
144
145 #endif