1570af302Sopenharmony_ci#define a_ll a_ll
2570af302Sopenharmony_cistatic inline int a_ll(volatile int *p)
3570af302Sopenharmony_ci{
4570af302Sopenharmony_ci	int v;
5570af302Sopenharmony_ci	__asm__ __volatile__ ("lwarx %0, 0, %2" : "=r"(v) : "m"(*p), "r"(p));
6570af302Sopenharmony_ci	return v;
7570af302Sopenharmony_ci}
8570af302Sopenharmony_ci
9570af302Sopenharmony_ci#define a_sc a_sc
10570af302Sopenharmony_cistatic inline int a_sc(volatile int *p, int v)
11570af302Sopenharmony_ci{
12570af302Sopenharmony_ci	int r;
13570af302Sopenharmony_ci	__asm__ __volatile__ (
14570af302Sopenharmony_ci		"stwcx. %2, 0, %3 ; mfcr %0"
15570af302Sopenharmony_ci		: "=r"(r), "=m"(*p) : "r"(v), "r"(p) : "memory", "cc");
16570af302Sopenharmony_ci	return r & 0x20000000; /* "bit 2" of "cr0" (backwards bit order) */
17570af302Sopenharmony_ci}
18570af302Sopenharmony_ci
19570af302Sopenharmony_ci#define a_ll_p a_ll_p
20570af302Sopenharmony_cistatic inline void *a_ll_p(volatile void *p)
21570af302Sopenharmony_ci{
22570af302Sopenharmony_ci	void *v;
23570af302Sopenharmony_ci	__asm__ __volatile__ ("ldarx %0, 0, %2" : "=r"(v) : "m"(*(void *volatile *)p), "r"(p));
24570af302Sopenharmony_ci	return v;
25570af302Sopenharmony_ci}
26570af302Sopenharmony_ci
27570af302Sopenharmony_ci#define a_sc_p a_sc_p
28570af302Sopenharmony_cistatic inline int a_sc_p(volatile void *p, void *v)
29570af302Sopenharmony_ci{
30570af302Sopenharmony_ci	int r;
31570af302Sopenharmony_ci	__asm__ __volatile__ (
32570af302Sopenharmony_ci		"stdcx. %2, 0, %3 ; mfcr %0"
33570af302Sopenharmony_ci		: "=r"(r), "=m"(*(void *volatile *)p) : "r"(v), "r"(p) : "memory", "cc");
34570af302Sopenharmony_ci	return r & 0x20000000; /* "bit 2" of "cr0" (backwards bit order) */
35570af302Sopenharmony_ci}
36570af302Sopenharmony_ci
37570af302Sopenharmony_ci#define a_barrier a_barrier
38570af302Sopenharmony_cistatic inline void a_barrier()
39570af302Sopenharmony_ci{
40570af302Sopenharmony_ci	__asm__ __volatile__ ("sync" : : : "memory");
41570af302Sopenharmony_ci}
42570af302Sopenharmony_ci
43570af302Sopenharmony_ci#define a_pre_llsc a_barrier
44570af302Sopenharmony_ci
45570af302Sopenharmony_ci#define a_post_llsc a_post_llsc
46570af302Sopenharmony_cistatic inline void a_post_llsc()
47570af302Sopenharmony_ci{
48570af302Sopenharmony_ci	__asm__ __volatile__ ("isync" : : : "memory");
49570af302Sopenharmony_ci}
50570af302Sopenharmony_ci
51570af302Sopenharmony_ci#define a_crash a_crash
52570af302Sopenharmony_cistatic inline void a_crash()
53570af302Sopenharmony_ci{
54570af302Sopenharmony_ci	__asm__ __volatile__ (".long 0");
55570af302Sopenharmony_ci}
56570af302Sopenharmony_ci
57570af302Sopenharmony_ci#define a_clz_64 a_clz_64
58570af302Sopenharmony_cistatic inline int a_clz_64(uint64_t x)
59570af302Sopenharmony_ci{
60570af302Sopenharmony_ci	__asm__ ("cntlzd %0, %1" : "=r"(x) : "r"(x));
61570af302Sopenharmony_ci	return x;
62570af302Sopenharmony_ci}
63