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
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
|
#pragma once
#ifdef _MSC_VER
#include <intrin.h>
#include "stdint.h"
#ifdef _M_X64
inline uint32_t XCHG_32(uint32_t volatile & Dest, uint32_t Exchange)
{
return (uint32_t)_InterlockedExchange((long volatile*)&(Dest), (long)(Exchange));
}
inline int32_t XCHG_32(int32_t volatile & Dest, int32_t Exchange)
{
return (int32_t)_InterlockedExchange((long volatile*)&(Dest), (long)(Exchange));
}
inline uint64_t XCHG_64(uint64_t volatile & Dest, uint64_t Exchange)
{
return (uint64_t)_InterlockedExchange64((__int64 volatile*)&Dest, (__int64)Exchange);
}
inline int64_t XCHG_64(int64_t volatile & Dest, int64_t Exchange)
{
return (int64_t)_InterlockedExchange64((__int64 volatile*)&Dest, (__int64)Exchange);
}
template <typename T>
inline T * XCHG_Ptr(T * volatile & Dest, T * Exchange)
{
return (T*)_InterlockedExchangePointer((void*volatile*)&Dest, (void*)Exchange);
}
inline uint32_t CMPXCHG_32(uint32_t volatile & Dest, uint32_t Exchange, uint32_t Comperand)
{
return (uint64_t)_InterlockedCompareExchange((long volatile*)&(Dest), (long)(Exchange), (long)Comperand);
}
inline int32_t CMPXCHG_32(int32_t volatile & Dest, int32_t Exchange, int32_t Comperand)
{
return (int32_t)_InterlockedCompareExchange((long volatile*)&(Dest), (long)(Exchange), (long)Comperand);
}
inline uint64_t CMPXCHG_64(uint64_t volatile & Dest, uint64_t Exchange, uint64_t Comperand)
{
return (uint64_t)_InterlockedCompareExchange64((__int64 volatile*)&Dest, (__int64)Exchange, (__int64)Comperand);
}
inline int64_t CMPXCHG_64(int64_t volatile & Dest, int64_t Exchange, int64_t Comperand)
{
return (int64_t)_InterlockedCompareExchange64((__int64 volatile*)&Dest, (__int64)Exchange, (__int64)Comperand);
}
template <typename T>
inline T * CMPXCHG_Ptr(T * volatile & Dest, T * Exchange, T * Comperand)
{
return (T*)_InterlockedCompareExchangePointer((void*volatile*)&Dest, (void*)Exchange, (void*)Comperand);
}
inline uint32_t XADD_32(uint32_t volatile & Dest, int32_t Addend)
{
return (uint32_t)_InterlockedExchangeAdd((long volatile*)&Dest, (long)Addend);
}
inline int32_t XADD_32(int32_t volatile & Dest, int32_t Addend)
{
return (int32_t)_InterlockedExchangeAdd((long volatile*)&Dest, (long)Addend);
}
inline uint64_t XADD_64(uint64_t volatile & Dest, int64_t Addend)
{
return (uint64_t)_InterlockedExchangeAdd64((__int64 volatile*)&Dest, (__int64)Addend);
}
inline int64_t XADD_64(int64_t volatile & Dest, int64_t Addend)
{
return (int64_t)_InterlockedExchangeAdd64((__int64 volatile*)&Dest, (__int64)Addend);
}
inline uint32_t DEC_32(uint32_t volatile & Dest)
{
return (uint32_t)_InterlockedDecrement((long volatile*)&Dest);
}
inline int32_t DEC_32(int32_t volatile & Dest)
{
return (int32_t)_InterlockedDecrement((long volatile*)&Dest);
}
inline uint64_t DEC_64(uint64_t volatile & Dest)
{
return (uint64_t)_InterlockedDecrement64((__int64 volatile*)&Dest);
}
inline int64_t DEC_64(int64_t volatile & Dest)
{
return (int64_t)_InterlockedDecrement64((__int64 volatile*)&Dest);
}
inline uint32_t INC_32(uint32_t volatile & Dest)
{
return (uint32_t)_InterlockedIncrement((long volatile*)&Dest);
}
inline int32_t INC_32(int32_t volatile & Dest)
{
return (int32_t)_InterlockedIncrement((long volatile*)&Dest);
}
inline uint64_t INC_64(uint64_t volatile & Dest)
{
return (uint64_t)_InterlockedIncrement64((__int64 volatile*)&Dest);
}
inline int64_t INC_64(int64_t volatile & Dest)
{
return (int64_t)_InterlockedIncrement64((__int64 volatile*)&Dest);
}
inline bool BTS_32(uint32_t volatile & Dest, uint8_t Offset)
{
return !!_interlockedbittestandset((long volatile*)&Dest, Offset);
}
inline bool BTS_32(int32_t volatile & Dest, uint8_t Offset)
{
return !!_interlockedbittestandset((long volatile*)&Dest, Offset);
}
inline bool BTS_64(uint64_t volatile & Dest, uint8_t Offset)
{
return !!_interlockedbittestandset64((__int64 volatile*)&Dest, Offset);
}
inline bool BTS_64(int64_t volatile & Dest, uint8_t Offset)
{
return !!_interlockedbittestandset64((__int64 volatile*)&Dest, Offset);
}
inline bool BTR_32(uint32_t volatile & Dest, uint8_t Offset)
{
return !!_interlockedbittestandreset((long volatile*)&Dest, Offset);
}
inline bool BTR_32(int32_t volatile & Dest, uint8_t Offset)
{
return !!_interlockedbittestandreset((long volatile*)&Dest, Offset);
}
inline bool BTR_64(uint64_t volatile & Dest, uint8_t Offset)
{
return !!_interlockedbittestandreset64((__int64 volatile*)&Dest, Offset);
}
inline bool BTR_64(int64_t volatile & Dest, uint8_t Offset)
{
return !!_interlockedbittestandreset64((__int64 volatile*)&Dest, Offset);
}
inline uint32_t OR_32(uint32_t volatile & Value, uint32_t Operator)
{
return (uint32_t)_InterlockedOr((long volatile*)&Value, (long)Operator);
}
inline int32_t OR_32(int32_t volatile & Value, int32_t Operator)
{
return (int32_t)_InterlockedOr((long volatile*)&Value, (long)Operator);
}
inline uint64_t OR_64(uint64_t volatile & Value, uint64_t Operator)
{
return (uint64_t)_InterlockedOr64((__int64 volatile*)&Value, (__int64)Operator);
}
inline int64_t OR_64(int64_t volatile & Value, int64_t Operator)
{
return (int64_t)_InterlockedOr64((__int64 volatile*)&Value, (__int64)Operator);
}
inline uint32_t AND_32(uint32_t volatile & Value, uint32_t Operator)
{
return (uint32_t)_InterlockedAnd((long volatile*)&Value, (long)Operator);
}
inline int32_t AND_32(int32_t volatile & Value, int32_t Operator)
{
return (int32_t)_InterlockedAnd((long volatile*)&Value, (long)Operator);
}
inline uint64_t AND_64(uint64_t volatile & Value, uint64_t Operator)
{
return (uint64_t)_InterlockedAnd64((__int64 volatile*)&Value, (__int64)Operator);
}
inline int64_t AND_64(int64_t volatile & Value, int64_t Operator)
{
return (int64_t)_InterlockedAnd64((__int64 volatile*)&Value, (__int64)Operator);
}
inline uint32_t XOR_32(uint32_t volatile & Value, uint32_t Operator)
{
return (uint32_t)_InterlockedXor((long volatile*)&Value, (long)Operator);
}
inline int32_t XOR_32(int32_t volatile & Value, int32_t Operator)
{
return (int32_t)_InterlockedXor((long volatile*)&Value, (long)Operator);
}
inline uint64_t XOR_64(uint64_t volatile & Value, uint64_t Operator)
{
return (uint64_t)_InterlockedXor64((__int64 volatile*)&Value, (__int64)Operator);
}
inline int64_t XOR_64(int64_t volatile & Value, int64_t Operator)
{
return (int64_t)_InterlockedXor64((__int64 volatile*)&Value, (__int64)Operator);
}
inline uint32_t BSWAP_32(uint32_t Value)
{
return _byteswap_ulong(Value);
}
inline uint32_t ROL_32(uint32_t Value, uint8_t Shift)
{
return _rotl(Value, Shift);
}
inline uint32_t ROR_32(uint32_t Value, uint8_t Shift)
{
return _rotr(Value, Shift);
}
#elif defined(_M_IX86)
inline uint32_t XCHG_32(uint32_t volatile & Dest, uint32_t Exchange)
{
return (uint32_t)_InterlockedExchange((long volatile*)&(Dest), (long)(Exchange));
}
inline int32_t XCHG_32(int32_t volatile & Dest, int32_t Exchange)
{
return (int32_t)_InterlockedExchange((long volatile*)&(Dest), (long)(Exchange));
}
/*
inline uint64_t XCHG_64(uint64_t volatile & Dest, uint64_t Exchange)
{
return (uint64_t)_InterlockedExchange64((__int64 volatile*)&Dest, (__int64)Exchange);
}
inline int64_t XCHG_64(int64_t volatile & Dest, int64_t Exchange)
{
return (int64_t)_InterlockedExchange64((__int64 volatile*)&Dest, (__int64)Exchange);
}
*/
template <typename T>
inline T * XCHG_Ptr(T * volatile & Dest, T * Exchange)
{
return (T*)_InterlockedExchange((long volatile*)&Dest, (long)Exchange);
}
inline uint32_t CMPXCHG_32(uint32_t volatile & Dest, uint32_t Exchange, uint32_t Comperand)
{
return (uint64_t)_InterlockedCompareExchange((long volatile*)&(Dest), (long)(Exchange), (long)Comperand);
}
inline int32_t CMPXCHG_32(int32_t volatile & Dest, int32_t Exchange, int32_t Comperand)
{
return (int32_t)_InterlockedCompareExchange((long volatile*)&(Dest), (long)(Exchange), (long)Comperand);
}
inline uint64_t CMPXCHG_64(uint64_t volatile & Dest, uint64_t Exchange, uint64_t Comperand)
{
return (uint64_t)_InterlockedCompareExchange64((__int64 volatile*)&Dest, (__int64)Exchange, (__int64)Comperand);
}
inline int64_t CMPXCHG_64(int64_t volatile & Dest, int64_t Exchange, int64_t Comperand)
{
return (int64_t)_InterlockedCompareExchange64((__int64 volatile*)&Dest, (__int64)Exchange, (__int64)Comperand);
}
template <typename T>
inline T * CMPXCHG_Ptr(T * volatile & Dest, T * Exchange, T * Comperand)
{
return (T*)_InterlockedCompareExchange((long volatile *)&Dest, (long)Exchange, (long)Comperand);
}
inline uint32_t XADD_32(uint32_t volatile & Dest, int32_t Addend)
{
return (uint32_t)_InterlockedExchangeAdd((long volatile*)&Dest, (long)Addend);
}
inline int32_t XADD_32(int32_t volatile & Dest, int32_t Addend)
{
return (int32_t)_InterlockedExchangeAdd((long volatile*)&Dest, (long)Addend);
}
inline uint32_t DEC_32(uint32_t volatile & Dest)
{
return (uint32_t)_InterlockedDecrement((long volatile*)&Dest);
}
inline int32_t DEC_32(int32_t volatile & Dest)
{
return (int32_t)_InterlockedDecrement((long volatile*)&Dest);
}
inline uint32_t INC_32(uint32_t volatile & Dest)
{
return (uint32_t)_InterlockedIncrement((long volatile*)&Dest);
}
inline int32_t INC_32(int32_t volatile & Dest)
{
return (int32_t)_InterlockedIncrement((long volatile*)&Dest);
}
inline bool BTS_32(uint32_t volatile & Dest, uint8_t Offset)
{
return !!_interlockedbittestandset((long volatile*)&Dest, Offset);
}
inline bool BTS_32(int32_t volatile & Dest, uint8_t Offset)
{
return !!_interlockedbittestandset((long volatile*)&Dest, Offset);
}
inline bool BTS_64(uint64_t volatile & Dest, uint8_t Offset)
{
if (Offset > 31)
return !!_interlockedbittestandset((long volatile*)&Dest, Offset);
else
return !!_interlockedbittestandset(((long volatile*)&Dest) + 1, Offset - 32);
}
inline bool BTS_64(int64_t volatile & Dest, uint8_t Offset)
{
if (Offset > 31)
return !!_interlockedbittestandset((long volatile*)&Dest, Offset);
else
return !!_interlockedbittestandset(((long volatile*)&Dest) + 1, Offset - 32);
}
inline bool BTR_32(uint32_t volatile & Dest, uint8_t Offset)
{
return !!_interlockedbittestandreset((long volatile*)&Dest, Offset);
}
inline bool BTR_32(int32_t volatile & Dest, uint8_t Offset)
{
return !!_interlockedbittestandreset((long volatile*)&Dest, Offset);
}
inline bool BTR_64(uint64_t volatile & Dest, uint8_t Offset)
{
if (Offset > 31)
return !!_interlockedbittestandreset((long volatile*)&Dest, Offset);
else
return !!_interlockedbittestandreset(((long volatile*)&Dest) + 1, Offset - 32);
}
inline bool BTR_64(int64_t volatile & Dest, uint8_t Offset)
{
if (Offset > 31)
return !!_interlockedbittestandreset((long volatile*)&Dest, Offset);
else
return !!_interlockedbittestandreset(((long volatile*)&Dest) + 1, Offset - 32);
}
inline uint32_t OR_32(uint32_t volatile & Value, uint32_t Operator)
{
return (uint32_t)_InterlockedOr((long volatile*)&Value, (long)Operator);
}
inline int32_t OR_32(int32_t volatile & Value, int32_t Operator)
{
return (int32_t)_InterlockedOr((long volatile*)&Value, (long)Operator);
}
inline uint32_t AND_32(uint32_t volatile & Value, uint32_t Operator)
{
return (uint32_t)_InterlockedAnd((long volatile*)&Value, (long)Operator);
}
inline int32_t AND_32(int32_t volatile & Value, int32_t Operator)
{
return (int32_t)_InterlockedAnd((long volatile*)&Value, (long)Operator);
}
inline uint32_t XOR_32(uint32_t volatile & Value, uint32_t Operator)
{
return (uint32_t)_InterlockedXor((long volatile*)&Value, (long)Operator);
}
inline int32_t XOR_32(int32_t volatile & Value, int32_t Operator)
{
return (int32_t)_InterlockedXor((long volatile*)&Value, (long)Operator);
}
inline uint32_t BSWAP_32(uint32_t Value)
{
return _byteswap_ulong(Value);
}
inline uint32_t ROL_32(uint32_t Value, uint8_t Shift)
{
return _rotl(Value, Shift);
}
inline uint32_t ROR_32(uint32_t Value, uint8_t Shift)
{
return _rotr(Value, Shift);
}
#else
#error unsupported architecture
#endif
#else
#error unsupported compiler
#endif
|