xref: /haiku/src/system/libroot/posix/musl/arch/arm64/atomic_arch.h (revision f504f61099b010fbfa94b1cc63d2e9072c7f7185)
1*f504f610SAugustin Cavalier #define a_ll a_ll
a_ll(volatile int * p)2*f504f610SAugustin Cavalier static inline int a_ll(volatile int *p)
3*f504f610SAugustin Cavalier {
4*f504f610SAugustin Cavalier 	int v;
5*f504f610SAugustin Cavalier 	__asm__ __volatile__ ("ldaxr %w0,%1" : "=r"(v) : "Q"(*p));
6*f504f610SAugustin Cavalier 	return v;
7*f504f610SAugustin Cavalier }
8*f504f610SAugustin Cavalier 
9*f504f610SAugustin Cavalier #define a_sc a_sc
a_sc(volatile int * p,int v)10*f504f610SAugustin Cavalier static inline int a_sc(volatile int *p, int v)
11*f504f610SAugustin Cavalier {
12*f504f610SAugustin Cavalier 	int r;
13*f504f610SAugustin Cavalier 	__asm__ __volatile__ ("stlxr %w0,%w2,%1" : "=&r"(r), "=Q"(*p) : "r"(v) : "memory");
14*f504f610SAugustin Cavalier 	return !r;
15*f504f610SAugustin Cavalier }
16*f504f610SAugustin Cavalier 
17*f504f610SAugustin Cavalier #define a_barrier a_barrier
a_barrier()18*f504f610SAugustin Cavalier static inline void a_barrier()
19*f504f610SAugustin Cavalier {
20*f504f610SAugustin Cavalier 	__asm__ __volatile__ ("dmb ish" : : : "memory");
21*f504f610SAugustin Cavalier }
22*f504f610SAugustin Cavalier 
23*f504f610SAugustin Cavalier #define a_cas a_cas
a_cas(volatile int * p,int t,int s)24*f504f610SAugustin Cavalier static inline int a_cas(volatile int *p, int t, int s)
25*f504f610SAugustin Cavalier {
26*f504f610SAugustin Cavalier 	int old;
27*f504f610SAugustin Cavalier 	do {
28*f504f610SAugustin Cavalier 		old = a_ll(p);
29*f504f610SAugustin Cavalier 		if (old != t) {
30*f504f610SAugustin Cavalier 			a_barrier();
31*f504f610SAugustin Cavalier 			break;
32*f504f610SAugustin Cavalier 		}
33*f504f610SAugustin Cavalier 	} while (!a_sc(p, s));
34*f504f610SAugustin Cavalier 	return old;
35*f504f610SAugustin Cavalier }
36*f504f610SAugustin Cavalier 
37*f504f610SAugustin Cavalier #define a_ll_p a_ll_p
a_ll_p(volatile void * p)38*f504f610SAugustin Cavalier static inline void *a_ll_p(volatile void *p)
39*f504f610SAugustin Cavalier {
40*f504f610SAugustin Cavalier 	void *v;
41*f504f610SAugustin Cavalier 	__asm__ __volatile__ ("ldaxr %0, %1" : "=r"(v) : "Q"(*(void *volatile *)p));
42*f504f610SAugustin Cavalier 	return v;
43*f504f610SAugustin Cavalier }
44*f504f610SAugustin Cavalier 
45*f504f610SAugustin Cavalier #define a_sc_p a_sc_p
a_sc_p(volatile int * p,void * v)46*f504f610SAugustin Cavalier static inline int a_sc_p(volatile int *p, void *v)
47*f504f610SAugustin Cavalier {
48*f504f610SAugustin Cavalier 	int r;
49*f504f610SAugustin Cavalier 	__asm__ __volatile__ ("stlxr %w0,%2,%1" : "=&r"(r), "=Q"(*(void *volatile *)p) : "r"(v) : "memory");
50*f504f610SAugustin Cavalier 	return !r;
51*f504f610SAugustin Cavalier }
52*f504f610SAugustin Cavalier 
53*f504f610SAugustin Cavalier #define a_cas_p a_cas_p
a_cas_p(volatile void * p,void * t,void * s)54*f504f610SAugustin Cavalier static inline void *a_cas_p(volatile void *p, void *t, void *s)
55*f504f610SAugustin Cavalier {
56*f504f610SAugustin Cavalier 	void *old;
57*f504f610SAugustin Cavalier 	do {
58*f504f610SAugustin Cavalier 		old = a_ll_p(p);
59*f504f610SAugustin Cavalier 		if (old != t) {
60*f504f610SAugustin Cavalier 			a_barrier();
61*f504f610SAugustin Cavalier 			break;
62*f504f610SAugustin Cavalier 		}
63*f504f610SAugustin Cavalier 	} while (!a_sc_p(p, s));
64*f504f610SAugustin Cavalier 	return old;
65*f504f610SAugustin Cavalier }
66*f504f610SAugustin Cavalier 
67*f504f610SAugustin Cavalier #define a_ctz_64 a_ctz_64
a_ctz_64(uint64_t x)68*f504f610SAugustin Cavalier static inline int a_ctz_64(uint64_t x)
69*f504f610SAugustin Cavalier {
70*f504f610SAugustin Cavalier 	__asm__(
71*f504f610SAugustin Cavalier 		"	rbit %0, %1\n"
72*f504f610SAugustin Cavalier 		"	clz %0, %0\n"
73*f504f610SAugustin Cavalier 		: "=r"(x) : "r"(x));
74*f504f610SAugustin Cavalier 	return x;
75*f504f610SAugustin Cavalier }
76*f504f610SAugustin Cavalier 
77*f504f610SAugustin Cavalier #define a_clz_64 a_clz_64
a_clz_64(uint64_t x)78*f504f610SAugustin Cavalier static inline int a_clz_64(uint64_t x)
79*f504f610SAugustin Cavalier {
80*f504f610SAugustin Cavalier 	__asm__("clz %0, %1" : "=r"(x) : "r"(x));
81*f504f610SAugustin Cavalier 	return x;
82*f504f610SAugustin Cavalier }
83