1570af302Sopenharmony_ci#define a_cas a_cas
2570af302Sopenharmony_cistatic inline int a_cas(volatile int *p, int t, int s)
3570af302Sopenharmony_ci{
4570af302Sopenharmony_ci	__asm__ __volatile__ (
5570af302Sopenharmony_ci		"lock ; cmpxchg %3, %1"
6570af302Sopenharmony_ci		: "=a"(t), "=m"(*p) : "a"(t), "r"(s) : "memory" );
7570af302Sopenharmony_ci	return t;
8570af302Sopenharmony_ci}
9570af302Sopenharmony_ci
10570af302Sopenharmony_ci#define a_cas_p a_cas_p
11570af302Sopenharmony_cistatic inline void *a_cas_p(volatile void *p, void *t, void *s)
12570af302Sopenharmony_ci{
13570af302Sopenharmony_ci	__asm__( "lock ; cmpxchg %3, %1"
14570af302Sopenharmony_ci		: "=a"(t), "=m"(*(void *volatile *)p)
15570af302Sopenharmony_ci		: "a"(t), "r"(s) : "memory" );
16570af302Sopenharmony_ci	return t;
17570af302Sopenharmony_ci}
18570af302Sopenharmony_ci
19570af302Sopenharmony_ci#define a_swap a_swap
20570af302Sopenharmony_cistatic inline int a_swap(volatile int *p, int v)
21570af302Sopenharmony_ci{
22570af302Sopenharmony_ci	__asm__ __volatile__(
23570af302Sopenharmony_ci		"xchg %0, %1"
24570af302Sopenharmony_ci		: "=r"(v), "=m"(*p) : "0"(v) : "memory" );
25570af302Sopenharmony_ci	return v;
26570af302Sopenharmony_ci}
27570af302Sopenharmony_ci
28570af302Sopenharmony_ci#define a_fetch_add a_fetch_add
29570af302Sopenharmony_cistatic inline int a_fetch_add(volatile int *p, int v)
30570af302Sopenharmony_ci{
31570af302Sopenharmony_ci	__asm__ __volatile__(
32570af302Sopenharmony_ci		"lock ; xadd %0, %1"
33570af302Sopenharmony_ci		: "=r"(v), "=m"(*p) : "0"(v) : "memory" );
34570af302Sopenharmony_ci	return v;
35570af302Sopenharmony_ci}
36570af302Sopenharmony_ci
37570af302Sopenharmony_ci#define a_and a_and
38570af302Sopenharmony_cistatic inline void a_and(volatile int *p, int v)
39570af302Sopenharmony_ci{
40570af302Sopenharmony_ci	__asm__ __volatile__(
41570af302Sopenharmony_ci		"lock ; and %1, %0"
42570af302Sopenharmony_ci		: "=m"(*p) : "r"(v) : "memory" );
43570af302Sopenharmony_ci}
44570af302Sopenharmony_ci
45570af302Sopenharmony_ci#define a_or a_or
46570af302Sopenharmony_cistatic inline void a_or(volatile int *p, int v)
47570af302Sopenharmony_ci{
48570af302Sopenharmony_ci	__asm__ __volatile__(
49570af302Sopenharmony_ci		"lock ; or %1, %0"
50570af302Sopenharmony_ci		: "=m"(*p) : "r"(v) : "memory" );
51570af302Sopenharmony_ci}
52570af302Sopenharmony_ci
53570af302Sopenharmony_ci#define a_and_64 a_and_64
54570af302Sopenharmony_cistatic inline void a_and_64(volatile uint64_t *p, uint64_t v)
55570af302Sopenharmony_ci{
56570af302Sopenharmony_ci	__asm__ __volatile(
57570af302Sopenharmony_ci		"lock ; and %1, %0"
58570af302Sopenharmony_ci		 : "=m"(*p) : "r"(v) : "memory" );
59570af302Sopenharmony_ci}
60570af302Sopenharmony_ci
61570af302Sopenharmony_ci#define a_or_64 a_or_64
62570af302Sopenharmony_cistatic inline void a_or_64(volatile uint64_t *p, uint64_t v)
63570af302Sopenharmony_ci{
64570af302Sopenharmony_ci	__asm__ __volatile__(
65570af302Sopenharmony_ci		"lock ; or %1, %0"
66570af302Sopenharmony_ci		 : "=m"(*p) : "r"(v) : "memory" );
67570af302Sopenharmony_ci}
68570af302Sopenharmony_ci
69570af302Sopenharmony_ci#define a_inc a_inc
70570af302Sopenharmony_cistatic inline void a_inc(volatile int *p)
71570af302Sopenharmony_ci{
72570af302Sopenharmony_ci	__asm__ __volatile__(
73570af302Sopenharmony_ci		"lock ; incl %0"
74570af302Sopenharmony_ci		: "=m"(*p) : "m"(*p) : "memory" );
75570af302Sopenharmony_ci}
76570af302Sopenharmony_ci
77570af302Sopenharmony_ci#define a_dec a_dec
78570af302Sopenharmony_cistatic inline void a_dec(volatile int *p)
79570af302Sopenharmony_ci{
80570af302Sopenharmony_ci	__asm__ __volatile__(
81570af302Sopenharmony_ci		"lock ; decl %0"
82570af302Sopenharmony_ci		: "=m"(*p) : "m"(*p) : "memory" );
83570af302Sopenharmony_ci}
84570af302Sopenharmony_ci
85570af302Sopenharmony_ci#define a_store a_store
86570af302Sopenharmony_cistatic inline void a_store(volatile int *p, int x)
87570af302Sopenharmony_ci{
88570af302Sopenharmony_ci	__asm__ __volatile__(
89570af302Sopenharmony_ci		"mov %1, %0 ; lock ; orl $0,(%%rsp)"
90570af302Sopenharmony_ci		: "=m"(*p) : "r"(x) : "memory" );
91570af302Sopenharmony_ci}
92570af302Sopenharmony_ci
93570af302Sopenharmony_ci#define a_barrier a_barrier
94570af302Sopenharmony_cistatic inline void a_barrier()
95570af302Sopenharmony_ci{
96570af302Sopenharmony_ci	__asm__ __volatile__( "" : : : "memory" );
97570af302Sopenharmony_ci}
98570af302Sopenharmony_ci
99570af302Sopenharmony_ci#define a_spin a_spin
100570af302Sopenharmony_cistatic inline void a_spin()
101570af302Sopenharmony_ci{
102570af302Sopenharmony_ci	__asm__ __volatile__( "pause" : : : "memory" );
103570af302Sopenharmony_ci}
104570af302Sopenharmony_ci
105570af302Sopenharmony_ci#define a_crash a_crash
106570af302Sopenharmony_cistatic inline void a_crash()
107570af302Sopenharmony_ci{
108570af302Sopenharmony_ci	__asm__ __volatile__( "hlt" : : : "memory" );
109570af302Sopenharmony_ci}
110570af302Sopenharmony_ci
111570af302Sopenharmony_ci#define a_ctz_64 a_ctz_64
112570af302Sopenharmony_cistatic inline int a_ctz_64(uint64_t x)
113570af302Sopenharmony_ci{
114570af302Sopenharmony_ci	__asm__( "bsf %1,%0" : "=r"(x) : "r"(x) );
115570af302Sopenharmony_ci	return x;
116570af302Sopenharmony_ci}
117570af302Sopenharmony_ci
118570af302Sopenharmony_ci#define a_clz_64 a_clz_64
119570af302Sopenharmony_cistatic inline int a_clz_64(uint64_t x)
120570af302Sopenharmony_ci{
121570af302Sopenharmony_ci	__asm__( "bsr %1,%0 ; xor $63,%0" : "=r"(x) : "r"(x) );
122570af302Sopenharmony_ci	return x;
123570af302Sopenharmony_ci}
124