add or1k (OpenRISC 1000) architecture port
[musl] / arch / or1k / 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         __asm__("1:     l.lwa %0, %1\n"
28                 "       l.sfeq %0, %2\n"
29                 "       l.bnf 1f\n"
30                 "        l.nop\n"
31                 "       l.swa %1, %3\n"
32                 "       l.bnf 1b\n"
33                 "        l.nop\n"
34                 "1:     \n"
35                 : "=&r"(t), "+m"(*p) : "r"(t), "r"(s) : "cc", "memory" );
36         return t;
37 }
38
39 static inline void *a_cas_p(volatile void *p, void *t, void *s)
40 {
41         return (void *)a_cas(p, (int)t, (int)s);
42 }
43
44 static inline long a_cas_l(volatile void *p, long t, long s)
45 {
46         return a_cas(p, t, s);
47 }
48
49 static inline int a_swap(volatile int *x, int v)
50 {
51         int old;
52         do old = *x;
53         while (a_cas(x, old, v) != old);
54         return old;
55 }
56
57 static inline int a_fetch_add(volatile int *x, int v)
58 {
59         int old;
60         do old = *x;
61         while (a_cas(x, old, old+v) != old);
62         return old;
63 }
64
65 static inline void a_inc(volatile int *x)
66 {
67         a_fetch_add(x, 1);
68 }
69
70 static inline void a_dec(volatile int *x)
71 {
72         a_fetch_add(x, -1);
73 }
74
75 static inline void a_store(volatile int *p, int x)
76 {
77         *p=x;
78 }
79
80 static inline void a_spin()
81 {
82 }
83
84 static inline void a_crash()
85 {
86         *(volatile char *)0=0;
87 }
88
89 static inline void a_and(volatile int *p, int v)
90 {
91         int old;
92         do old = *p;
93         while (a_cas(p, old, old&v) != old);
94 }
95
96 static inline void a_or(volatile int *p, int v)
97 {
98         int old;
99         do old = *p;
100         while (a_cas(p, old, old|v) != old);
101 }
102
103 static inline void a_or_l(volatile void *p, long v)
104 {
105         a_or(p, v);
106 }
107
108 static inline void a_and_64(volatile uint64_t *p, uint64_t v)
109 {
110         union { uint64_t v; uint32_t r[2]; } u = { v };
111         a_and((int *)p, u.r[0]);
112         a_and((int *)p+1, u.r[1]);
113 }
114
115 static inline void a_or_64(volatile uint64_t *p, uint64_t v)
116 {
117         union { uint64_t v; uint32_t r[2]; } u = { v };
118         a_or((int *)p, u.r[0]);
119         a_or((int *)p+1, u.r[1]);
120 }
121
122 #endif