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