Repository navigation
Expand file tree
/
Copy pathatomic-impl.h
More file actions
173 lines (155 loc) · 10.9 KB
/
Copy pathatomic-impl.h
File metadata and controls
173 lines (155 loc) · 10.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
/*
* DON'T INCLUDE THIS FILE DIRECTLY
*/
#define atomic_store_small_template(scope, type, cscope, size) \
static inline void atomic_store_##cscope##_##size(volatile uint##size##_t *address, \
uint##size##_t value) \
{ \
asm volatile( \
"s" #type #scope " %[val], %[addr]" \
: [addr] "=A" (*address) \
: [val] "r" (value) \
); \
}
#define atomic_load_small_template(cscope, size) \
static inline uint##size##_t atomic_load_##cscope##_##size(volatile const uint##size##_t *address) \
{ \
uint32_t result, offset, *address32; \
offset = (uintptr_t)address & 3ull; \
address32 = (uint32_t *)((uintptr_t)address & ~3ull); \
result = atomic_load_##cscope##_32(address32); \
return (result >> (offset * 8)) & ((1ul << (sizeof(uint##size##_t) * 8)) - 1); \
}
#define atomic_load_template(scope, type, cscope, size) \
static inline uint##size##_t atomic_load_##cscope##_##size(volatile const uint##size##_t *address) \
{ \
uint##size##_t result; \
asm volatile( \
"amoor" #scope "." #type " %[res], x0, %[addr]" \
: [res] "=r" (result) \
: [addr] "A" (*address) \
); \
return result; \
}
#define atomic_store_template(scope, type, cscope, size) \
static inline void atomic_store_##cscope##_##size(volatile uint##size##_t *address, \
uint##size##_t value) \
{ \
asm volatile( \
"amoswap" #scope "." #type " x0, %[val], %[addr]" \
: [addr] "=A" (*address) \
: [val] "r" (value) \
); \
}
#define atomic_load_signed_template(cscope, size) \
static inline int##size##_t atomic_load_signed_##cscope##_##size(volatile const int##size##_t *address) \
{ \
return (int##size##_t)atomic_load_##cscope##_##size((volatile const uint##size##_t*)address); \
}
#define atomic_store_signed_template(cscope, size) \
static inline void atomic_store_signed_##cscope##_##size(volatile int##size##_t *address, \
int##size##_t value) \
{ \
atomic_store_##cscope##_##size((volatile uint##size##_t*)address, \
(uint##size##_t)value); \
}
#define atomic_op_template(name, op, scope, type, cscope, size) \
static inline uint##size##_t atomic_##name##_##cscope##_##size(volatile uint##size##_t *address, \
uint##size##_t value) \
{ \
uint##size##_t result; \
asm volatile( \
"amo" #op "" #scope "." #type " %[res], %[val], %[addr]" \
: [res] "=r" (result), \
[addr] "+A" (*address) \
: [val] "r" (value) \
); \
return result; \
}
#define atomic_signed_op_template(name, op, scope, type, cscope, size) \
static inline int##size##_t atomic_##name##_signed_##cscope##_##size(volatile int##size##_t *address, \
int##size##_t value) \
{ \
int##size##_t result; \
asm volatile( \
"amo" #op "" #scope "." #type " %[res], %[val], %[addr]" \
: [res] "=r" (result), \
[addr] "+A" (*address) \
: [val] "r" (value) \
); \
return result; \
}
#define atomic_compare_and_exchange_template(scope, type, cscope, size) \
static inline uint##size##_t atomic_compare_and_exchange_##cscope##_##size(volatile uint##size##_t *address, \
uint##size##_t expected, \
uint##size##_t desired) \
{ \
uint##size##_t previous; \
register uint##size##_t x31 asm("x31") = expected; \
asm volatile( \
"amocmpswap" #scope "." #type " %[prev], %[desired], %[addr]" \
: [prev] "=r" (previous), \
[addr] "+A" (*address) \
: [desired] "r" (desired), \
"r" (x31) \
); \
return previous; \
}
#define atomic_define_variants(func) \
atomic_##func##_template(l, w, local, 32) \
atomic_##func##_template(l, d, local, 64) \
atomic_##func##_template(g, w, global, 32) \
atomic_##func##_template(g, d, global, 64)
#define atomic_define_signed_variants(func) \
atomic_##func##_signed_template(local, 8) \
atomic_##func##_signed_template(local, 16) \
atomic_##func##_signed_template(local, 32) \
atomic_##func##_signed_template(local, 64) \
atomic_##func##_signed_template(global, 8) \
atomic_##func##_signed_template(global, 16) \
atomic_##func##_signed_template(global, 32) \
atomic_##func##_signed_template(global, 64)
#define atomic_define_op_variants(name, op) \
atomic_op_template(name, op, l, w, local, 32) \
atomic_op_template(name, op, l, d, local, 64) \
atomic_op_template(name, op, g, w, global, 32) \
atomic_op_template(name, op, g, d, global, 64)
#define atomic_define_signed_op_variants(name, op) \
atomic_signed_op_template(name, op, l, w, local, 32) \
atomic_signed_op_template(name, op, l, d, local, 64) \
atomic_signed_op_template(name, op, g, w, global, 32) \
atomic_signed_op_template(name, op, g, d, global, 64)
atomic_load_small_template(local, 8)
atomic_load_small_template(local, 16)
atomic_load_small_template(global, 8)
atomic_load_small_template(global, 16)
atomic_store_small_template(l, b, local, 8)
atomic_store_small_template(l, h, local, 16)
atomic_store_small_template(g, b, global, 8)
atomic_store_small_template(g, h, global, 16)
atomic_define_variants(load)
atomic_define_variants(store)
atomic_define_signed_variants(load)
atomic_define_signed_variants(store)
atomic_define_op_variants(exchange, swap)
atomic_define_op_variants(add, add)
atomic_define_op_variants(and, and)
atomic_define_op_variants(or, or)
atomic_define_signed_op_variants(add, add)
atomic_compare_and_exchange_template(l, w, local, 32)
atomic_compare_and_exchange_template(l, d, local, 64)
atomic_compare_and_exchange_template(g, w, global, 32)
atomic_compare_and_exchange_template(g, d, global, 64)
#undef atomic_store_small_template
#undef atomic_load_small_template
#undef atomic_load_template
#undef atomic_store_template
#undef atomic_load_signed_template
#undef atomic_store_signed_template
#undef atomic_op_template
#undef atomic_signed_op_template
#undef atomic_define_variants
#undef atomic_define_signed_variants
#undef atomic_define_op_variants
#undef atomic_define_signed_op_variants
#undef atomic_compare_and_exchange_template