1#include <stdint.h>
2
3#define a_cas a_cas
4static inline int a_cas(volatile int *p, int t, int s)
5{
6 __asm__ __volatile__ (
7 "lock ; cmpxchg %3, %1"
8 : "=a"(t), "=m"(*p) : "a"(t), "r"(s) : "memory" );
9 return t;
10}
11
12#define a_cas_p a_cas_p
13static inline void *a_cas_p(volatile void *p, void *t, void *s)
14{
15 __asm__( "lock ; cmpxchg %3, %1"
16 : "=a"(t), "=m"(*(void *volatile *)p)
17 : "a"(t), "r"(s) : "memory" );
18 return t;
19}
20
21#define a_swap a_swap
22static inline int a_swap(volatile int *p, int v)
23{
24 __asm__ __volatile__(
25 "xchg %0, %1"
26 : "=r"(v), "=m"(*p) : "0"(v) : "memory" );
27 return v;
28}
29
30#define a_fetch_add a_fetch_add
31static inline int a_fetch_add(volatile int *p, int v)
32{
33 __asm__ __volatile__(
34 "lock ; xadd %0, %1"
35 : "=r"(v), "=m"(*p) : "0"(v) : "memory" );
36 return v;
37}
38
39#define a_and a_and
40static inline void a_and(volatile int *p, int v)
41{
42 __asm__ __volatile__(
43 "lock ; and %1, %0"
44 : "=m"(*p) : "r"(v) : "memory" );
45}
46
47#define a_or a_or
48static inline void a_or(volatile int *p, int v)
49{
50 __asm__ __volatile__(
51 "lock ; or %1, %0"
52 : "=m"(*p) : "r"(v) : "memory" );
53}
54
55#define a_and_64 a_and_64
56static inline void a_and_64(volatile uint64_t *p, uint64_t v)
57{
58 __asm__ __volatile(
59 "lock ; and %1, %0"
60 : "=m"(*p) : "r"(v) : "memory" );
61}
62
63#define a_or_64 a_or_64
64static inline void a_or_64(volatile uint64_t *p, uint64_t v)
65{
66 __asm__ __volatile__(
67 "lock ; or %1, %0"
68 : "=m"(*p) : "r"(v) : "memory" );
69}
70
71#define a_inc a_inc
72static inline void a_inc(volatile int *p)
73{
74 __asm__ __volatile__(
75 "lock ; incl %0"
76 : "=m"(*p) : "m"(*p) : "memory" );
77}
78
79#define a_dec a_dec
80static inline void a_dec(volatile int *p)
81{
82 __asm__ __volatile__(
83 "lock ; decl %0"
84 : "=m"(*p) : "m"(*p) : "memory" );
85}
86
87#define a_store a_store
88static inline void a_store(volatile int *p, int x)
89{
90 __asm__ __volatile__(
91 "mov %1, %0 ; lock ; orl $0,(%%rsp)"
92 : "=m"(*p) : "r"(x) : "memory" );
93}
94
95#define a_barrier a_barrier
96static inline void a_barrier()
97{
98 __asm__ __volatile__( "" : : : "memory" );
99}
100
101#define a_spin a_spin
102static inline void a_spin()
103{
104 __asm__ __volatile__( "pause" : : : "memory" );
105}
106
107#define a_crash a_crash
108static inline void a_crash()
109{
110 __asm__ __volatile__( "hlt" : : : "memory" );
111}
112
113#define a_ctz_64 a_ctz_64
114static inline int a_ctz_64(uint64_t x)
115{
116 __asm__( "bsf %1,%0" : "=r"(x) : "r"(x) );
117 return x;
118}
119
120#define a_clz_64 a_clz_64
121static inline int a_clz_64(uint64_t x)
122{
123 __asm__( "bsr %1,%0 ; xor $63,%0" : "=r"(x) : "r"(x) );
124 return x;
125}
126