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_inc a_inc
45570af302Sopenharmony_cistatic inline void a_inc(volatile int *p)
46570af302Sopenharmony_ci{
47570af302Sopenharmony_ci	__asm__ __volatile__(
48570af302Sopenharmony_ci		"lock ; incl %0"
49570af302Sopenharmony_ci		: "=m"(*p) : "m"(*p) : "memory" );
50570af302Sopenharmony_ci}
51570af302Sopenharmony_ci
52570af302Sopenharmony_ci#define a_dec a_dec
53570af302Sopenharmony_cistatic inline void a_dec(volatile int *p)
54570af302Sopenharmony_ci{
55570af302Sopenharmony_ci	__asm__ __volatile__(
56570af302Sopenharmony_ci		"lock ; decl %0"
57570af302Sopenharmony_ci		: "=m"(*p) : "m"(*p) : "memory" );
58570af302Sopenharmony_ci}
59570af302Sopenharmony_ci
60570af302Sopenharmony_ci#define a_store a_store
61570af302Sopenharmony_cistatic inline void a_store(volatile int *p, int x)
62570af302Sopenharmony_ci{
63570af302Sopenharmony_ci	__asm__ __volatile__(
64570af302Sopenharmony_ci		"mov %1, %0 ; lock ; orl $0,(%%esp)"
65570af302Sopenharmony_ci		: "=m"(*p) : "r"(x) : "memory" );
66570af302Sopenharmony_ci}
67570af302Sopenharmony_ci
68570af302Sopenharmony_ci#define a_barrier a_barrier
69570af302Sopenharmony_cistatic inline void a_barrier()
70570af302Sopenharmony_ci{
71570af302Sopenharmony_ci	__asm__ __volatile__( "" : : : "memory" );
72570af302Sopenharmony_ci}
73570af302Sopenharmony_ci
74570af302Sopenharmony_ci#define a_spin a_spin
75570af302Sopenharmony_cistatic inline void a_spin()
76570af302Sopenharmony_ci{
77570af302Sopenharmony_ci	__asm__ __volatile__( "pause" : : : "memory" );
78570af302Sopenharmony_ci}
79570af302Sopenharmony_ci
80570af302Sopenharmony_ci#define a_crash a_crash
81570af302Sopenharmony_cistatic inline void a_crash()
82570af302Sopenharmony_ci{
83570af302Sopenharmony_ci	__asm__ __volatile__( "hlt" : : : "memory" );
84570af302Sopenharmony_ci}
85570af302Sopenharmony_ci
86570af302Sopenharmony_ci#define a_ctz_64 a_ctz_64
87570af302Sopenharmony_cistatic inline int a_ctz_64(uint64_t x)
88570af302Sopenharmony_ci{
89570af302Sopenharmony_ci	int r;
90570af302Sopenharmony_ci	__asm__( "bsf %1,%0 ; jnz 1f ; bsf %2,%0 ; add $32,%0\n1:"
91570af302Sopenharmony_ci		: "=&r"(r) : "r"((unsigned)x), "r"((unsigned)(x>>32)) );
92570af302Sopenharmony_ci	return r;
93570af302Sopenharmony_ci}
94570af302Sopenharmony_ci
95570af302Sopenharmony_ci#define a_ctz_32 a_ctz_32
96570af302Sopenharmony_cistatic inline int a_ctz_32(uint32_t x)
97570af302Sopenharmony_ci{
98570af302Sopenharmony_ci	int r;
99570af302Sopenharmony_ci	__asm__( "bsf %1,%0" : "=r"(r) : "r"(x) );
100570af302Sopenharmony_ci	return r;
101570af302Sopenharmony_ci}
102570af302Sopenharmony_ci
103570af302Sopenharmony_ci#define a_clz_32 a_clz_32
104570af302Sopenharmony_cistatic inline int a_clz_32(uint32_t x)
105570af302Sopenharmony_ci{
106570af302Sopenharmony_ci	__asm__( "bsr %1,%0 ; xor $31,%0" : "=r"(x) : "r"(x) );
107570af302Sopenharmony_ci	return x;
108570af302Sopenharmony_ci}
109