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