-
Notifications
You must be signed in to change notification settings - Fork 2
/
arch_dep.h
328 lines (277 loc) · 6.03 KB
/
arch_dep.h
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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
#ifndef _ARCH_DEP_H_
#define _ARCH_DEP_H_
#ifdef __i386__
#define LOCK_PREFIX "lock; "
static __inline__ unsigned long long rdtsc(void)
{
unsigned long long int x;
__asm__ volatile (".byte 0x0f, 0x31" : "=A" (x));
return x;
}
#define mb() __asm__ __volatile__("mfence")
#define rmb() __asm__ __volatile__("lfence")
#define wmb() __asm__ __volatile__ ("": : :"memory")
static inline int _ffs(int x) {
int r;
__asm__("bsfl %1,%0\n\t"
"jnz 1f\n\t"
"movl $-1,%0\n"
"1:" : "=r" (r) : "g" (x));
return r;
}
static inline int _fls(int x) {
int r;
__asm__("bsrl %1,%0\n\t"
"jnz 1f\n\t"
"movl $-1,%0\n"
"1:" : "=r" (r) : "g" (x));
return r;
}
static inline int atomic_add_return(int i, int *val)
{
int __i;
/* Modern 486+ processor */
__i = i;
__asm__ __volatile__(
"lock xaddl %0, %1"
:"+r" (i), "+m" (*val)
: : "memory");
return i + __i;
}
#define atomic_inc_return(val) atomic_add_return(1, val)
/* the CAS code is copied from the linux kernel sources */
/**
* atomic_inc - increment atomic variable
* @v: pointer of type atomic_t
*
* Atomically increments @v by 1.
*/
static __inline__ void atomic_inc(int *v)
{
__asm__ __volatile__(
LOCK_PREFIX "incl %0"
:"+m" (*v));
}
/**
* atomic_dec - decrement atomic variable
* @v: pointer of type atomic_t
*
* Atomically decrements @v by 1.
*/
static __inline__ void atomic_dec(int *v)
{
__asm__ __volatile__(
LOCK_PREFIX "decl %0"
:"+m" (*v));
}
struct __xchg_dummy { unsigned long a[100]; };
#define __xg(x) ((struct __xchg_dummy *)(x))
static inline long long __cmpxchg64(uint64_t *ptr, uint64_t old, uint64_t new)
{
unsigned long long prev;
__asm__ __volatile__(LOCK_PREFIX "cmpxchg8b %3"
: "=A"(prev)
: "b"((unsigned long)new),
"c"((unsigned long)(new >> 32)),
"m"(*__xg(ptr)),
"0"(old)
: "memory");
return prev;
}
#define cmpxchg64(ptr,o,n)\
((__typeof__(*(ptr)))__cmpxchg64((ptr),(unsigned long long)(o),\
(unsigned long long)(n)))
#define ARCH_HAS_CAS64 1
#define USE_RDTSC 1
static inline unsigned long __cmpxchg(volatile void *ptr, unsigned long old,
unsigned long new, int size)
{
unsigned long prev;
switch (size) {
case 1:
__asm__ __volatile__(LOCK_PREFIX "cmpxchgb %b1,%2"
: "=a"(prev)
: "q"(new), "m"(*__xg(ptr)), "0"(old)
: "memory");
return prev;
case 2:
__asm__ __volatile__(LOCK_PREFIX "cmpxchgw %w1,%2"
: "=a"(prev)
: "r"(new), "m"(*__xg(ptr)), "0"(old)
: "memory");
return prev;
case 4:
__asm__ __volatile__(LOCK_PREFIX "cmpxchgl %1,%2"
: "=a"(prev)
: "r"(new), "m"(*__xg(ptr)), "0"(old)
: "memory");
return prev;
}
return old;
}
#define cmpxchg(ptr,o,n)\
((__typeof__(*(ptr)))__cmpxchg((ptr),(unsigned long)(o),\
(unsigned long)(n),sizeof(*(ptr))))
#elif defined(__x86_64__)
static __inline__ unsigned long long rdtsc(void)
{
unsigned hi, lo;
__asm__ __volatile__ ("rdtsc" : "=a"(lo), "=d"(hi));
return ( (unsigned long long)lo)|( ((unsigned long long)hi)<<32 );
}
#elif __arm__
/*
ARM's dependent code has been provided by
Adam Scislowicz <proteuskor@gmail.com>
Thank you very much Adam.
*/
#define _fls(x) ({ \
int __r; \
asm("clz\t%0, %1" : "=r"(__r) : "r"(x) : "cc"); \
31-__r; \
})
#define _ffs(x) ({ unsigned long __t = (x); _fls(__t & -__t); })
/* FIXME: all this atomic stuff for armv5
* the problem is that armv5 does not support atomic operations natively. arg ...
*/
static inline int atomic_add_return(int i, int *v)
{
unsigned long tmp;
int result;
#if 0
__asm__ __volatile__("@ atomic_add_return\n"
"1: ldrex %0, [%2]\n"
" add %0, %0, %3\n"
" strex %1, %0, [%2]\n"
" teq %1, #0\n"
" bne 1b"
: "=&r" (result), "=&r" (tmp)
: "r" (v), "Ir" (i)
: "cc");
#else
tmp = *v;
result = ++tmp;
*v = tmp;
#endif
return result;
}
#define atomic_inc_return(val) atomic_add_return(1, val)
static inline int atomic_dec_return(int i, int *v)
{
unsigned long tmp;
int result;
#if 0
__asm__ __volatile__("@ atomic_add_return\n"
"1: ldrex %0, [%2]\n"
" add %0, %0, %3\n"
" strex %1, %0, [%2]\n"
" teq %1, #0\n"
" bne 1b"
: "=&r" (result), "=&r" (tmp)
: "r" (v), "Ir" (i)
: "cc");
#else
tmp = *v;
result = tmp--;
*v = tmp;
#endif
return result;
}
#define atomic_dec_return(val) atomic_add_return(1, val)
static inline void atomic_dec(int *v)
{
*v = *v - 1;
}
static inline void atomic_inc(int *v)
{
*v = *v + 1;
}
static inline int atomic_cmpxchg(long *ptr, int old, int new)
{
unsigned long oldval, res;
do {
__asm__ __volatile__("@ atomic_cmpxchg\n"
"ldrex %1, [%2]\n"
"mov %0, #0\n"
"teq %1, %3\n"
"strexeq %0, %4, [%2]\n"
: "=&r" (res), "=&r" (oldval)
: "r" (ptr), "Ir" (old), "r" (new)
: "cc");
} while (res);
return oldval;
}
#define cmpxchg(ptr,o,n)\
((__typeof__(*(ptr)))atomic_cmpxchg((ptr),(unsigned long)(o),\
(unsigned long)(n)))
#undef ARCH_HAS_CAS64
#else
#warning Unsupported CPU architecture, using unoptimized bitmap handling
static inline int _ffs (int x) {
int r = 0;
if (!x)
return -1;
if (!(x & 0xffff)) {
x >>= 16;
r += 16;
}
if (!(x & 0xff)) {
x >>= 8;
r += 8;
}
if (!(x & 0xf)) {
x >>= 4;
r += 4;
}
if (!(x & 0x3)) {
x >>= 2;
r += 2;
}
if (!(x & 0x1)) {
x >>= 1;
r += 1;
}
return r;
}
static inline int _fls (int x) {
int r = 31;
if (!x)
return -1;
if (!(x & 0xffff0000)) {
x <<= 16;
r -= 16;
}
if (!(x & 0xff000000)) {
x <<= 8;
r -= 8;
}
if (!(x & 0xf0000000)) {
x <<= 4;
r -= 4;
}
if (!(x & 0xc0000000)) {
x <<= 2;
r -= 2;
}
if (!(x & 0x80000000)) {
x <<= 1;
r -= 1;
}
return r;
}
#endif
#if USE_RDTSC
#define get_utime rdtsc
#else
#include <sys/time.h>
#include <time.h>
static inline uint64_t get_utime()
{
struct timeval tv;
uint64_t usecs;
gettimeofday(&tv, NULL);
usecs = tv.tv_usec + tv.tv_sec*1000000;
return usecs;
}
#endif
#endif /* !_ARCH_DEP_H_ */