1 /* ===-------- Intrin.h ---------------------------------------------------===
3 * Permission is hereby granted, free of charge, to any person obtaining a copy
4 * of this software and associated documentation files (the "Software"), to deal
5 * in the Software without restriction, including without limitation the rights
6 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7 * copies of the Software, and to permit persons to whom the Software is
8 * furnished to do so, subject to the following conditions:
10 * The above copyright notice and this permission notice shall be included in
11 * all copies or substantial portions of the Software.
13 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21 *===-----------------------------------------------------------------------===
24 /* Only include this if we're compiling for the windows platform. */
26 #include_next <Intrin.h>
32 /* First include the standard intrinsics. */
33 #if defined(__i386__) || defined(__x86_64__)
34 #include <x86intrin.h>
37 /* For the definition of jmp_buf. */
47 /* And the random ones that aren't in those files. */
48 __m64 _m_from_float(float);
49 __m64 _m_from_int(int _l);
50 void _m_prefetch(void *);
51 float _m_to_float(__m64);
52 int _m_to_int(__m64 _M);
55 /* Other assorted instruction intrinsics. */
56 void __addfsbyte(unsigned long, unsigned char);
57 void __addfsdword(unsigned long, unsigned long);
58 void __addfsword(unsigned long, unsigned short);
59 void __code_seg(const char *);
61 void __cpuid(int[4], int);
63 void __cpuidex(int[4], int, int);
64 void __debugbreak(void);
65 __int64 __emul(int, int);
66 unsigned __int64 __emulu(unsigned int, unsigned int);
67 void __cdecl __fastfail(unsigned int);
68 unsigned int __getcallerseflags(void);
71 unsigned char __inbyte(unsigned short);
72 void __inbytestring(unsigned short, unsigned char *, unsigned long);
73 void __incfsbyte(unsigned long);
74 void __incfsdword(unsigned long);
75 void __incfsword(unsigned long);
76 unsigned long __indword(unsigned short);
77 void __indwordstring(unsigned short, unsigned long *, unsigned long);
79 void __invlpg(void *);
80 unsigned short __inword(unsigned short);
81 void __inwordstring(unsigned short, unsigned short *, unsigned long);
83 unsigned __int64 __ll_lshift(unsigned __int64, int);
84 __int64 __ll_rshift(__int64, int);
85 void __llwpcb(void *);
86 unsigned char __lwpins32(unsigned int, unsigned int, unsigned int);
87 void __lwpval32(unsigned int, unsigned int, unsigned int);
88 unsigned int __lzcnt(unsigned int);
89 unsigned short __lzcnt16(unsigned short);
91 void __movsb(unsigned char *, unsigned char const *, size_t);
93 void __movsd(unsigned long *, unsigned long const *, size_t);
95 void __movsw(unsigned short *, unsigned short const *, size_t);
97 void __nvreg_restore_fence(void);
98 void __nvreg_save_fence(void);
99 void __outbyte(unsigned short, unsigned char);
100 void __outbytestring(unsigned short, unsigned char *, unsigned long);
101 void __outdword(unsigned short, unsigned long);
102 void __outdwordstring(unsigned short, unsigned long *, unsigned long);
103 void __outword(unsigned short, unsigned short);
104 void __outwordstring(unsigned short, unsigned short *, unsigned long);
106 unsigned int __popcnt(unsigned int);
108 unsigned short __popcnt16(unsigned short);
109 unsigned long __readcr0(void);
110 unsigned long __readcr2(void);
112 unsigned long __readcr3(void);
113 unsigned long __readcr4(void);
114 unsigned long __readcr8(void);
115 unsigned int __readdr(unsigned int);
118 unsigned char __readfsbyte(unsigned long);
120 unsigned long __readfsdword(unsigned long);
122 unsigned __int64 __readfsqword(unsigned long);
124 unsigned short __readfsword(unsigned long);
127 unsigned __int64 __readmsr(unsigned long);
128 unsigned __int64 __readpmc(unsigned long);
129 unsigned long __segmentlimit(unsigned long);
131 void *__slwpcb(void);
133 void __stosb(unsigned char *, unsigned char, size_t);
135 void __stosd(unsigned long *, unsigned long, size_t);
137 void __stosw(unsigned short *, unsigned short, size_t);
138 void __svm_clgi(void);
139 void __svm_invlpga(void *, int);
140 void __svm_skinit(int);
141 void __svm_stgi(void);
142 void __svm_vmload(size_t);
143 void __svm_vmrun(size_t);
144 void __svm_vmsave(size_t);
146 unsigned __int64 __ull_rshift(unsigned __int64, int);
147 void __vmx_off(void);
148 void __vmx_vmptrst(unsigned __int64 *);
150 void __writecr0(unsigned int);
152 void __writecr3(unsigned int);
153 void __writecr4(unsigned int);
154 void __writecr8(unsigned int);
155 void __writedr(unsigned int, unsigned int);
156 void __writefsbyte(unsigned long, unsigned char);
157 void __writefsdword(unsigned long, unsigned long);
158 void __writefsqword(unsigned long, unsigned __int64);
159 void __writefsword(unsigned long, unsigned short);
160 void __writemsr(unsigned long, unsigned __int64);
162 void *_AddressOfReturnAddress(void);
164 unsigned char _BitScanForward(unsigned long *_Index, unsigned long _Mask);
166 unsigned char _BitScanReverse(unsigned long *_Index, unsigned long _Mask);
168 unsigned char _bittest(long const *, long);
170 unsigned char _bittestandcomplement(long *, long);
172 unsigned char _bittestandreset(long *, long);
174 unsigned char _bittestandset(long *, long);
175 unsigned __int64 __cdecl _byteswap_uint64(unsigned __int64);
176 unsigned long __cdecl _byteswap_ulong(unsigned long);
177 unsigned short __cdecl _byteswap_ushort(unsigned short);
178 void __cdecl _disable(void);
179 void __cdecl _enable(void);
180 void __cdecl _fxrstor(void const *);
181 void __cdecl _fxsave(void *);
182 long _InterlockedAddLargeStatistic(__int64 volatile *_Addend, long _Value);
184 long _InterlockedAnd(long volatile *_Value, long _Mask);
186 short _InterlockedAnd16(short volatile *_Value, short _Mask);
188 char _InterlockedAnd8(char volatile *_Value, char _Mask);
189 unsigned char _interlockedbittestandreset(long volatile *, long);
191 unsigned char _interlockedbittestandset(long volatile *, long);
193 long __cdecl _InterlockedCompareExchange(long volatile *_Destination,
194 long _Exchange, long _Comparand);
195 long _InterlockedCompareExchange_HLEAcquire(long volatile *, long, long);
196 long _InterlockedCompareExchange_HLERelease(long volatile *, long, long);
198 short _InterlockedCompareExchange16(short volatile *_Destination,
199 short _Exchange, short _Comparand);
201 __int64 _InterlockedCompareExchange64(__int64 volatile *_Destination,
202 __int64 _Exchange, __int64 _Comparand);
203 __int64 _InterlockedcompareExchange64_HLEAcquire(__int64 volatile *, __int64,
205 __int64 _InterlockedCompareExchange64_HLERelease(__int64 volatile *, __int64,
208 char _InterlockedCompareExchange8(char volatile *_Destination, char _Exchange,
210 void *_InterlockedCompareExchangePointer_HLEAcquire(void *volatile *, void *,
212 void *_InterlockedCompareExchangePointer_HLERelease(void *volatile *, void *,
215 long __cdecl _InterlockedDecrement(long volatile *_Addend);
217 short _InterlockedDecrement16(short volatile *_Addend);
218 long _InterlockedExchange(long volatile *_Target, long _Value);
220 short _InterlockedExchange16(short volatile *_Target, short _Value);
222 char _InterlockedExchange8(char volatile *_Target, char _Value);
224 long __cdecl _InterlockedExchangeAdd(long volatile *_Addend, long _Value);
225 long _InterlockedExchangeAdd_HLEAcquire(long volatile *, long);
226 long _InterlockedExchangeAdd_HLERelease(long volatile *, long);
228 short _InterlockedExchangeAdd16(short volatile *_Addend, short _Value);
229 __int64 _InterlockedExchangeAdd64_HLEAcquire(__int64 volatile *, __int64);
230 __int64 _InterlockedExchangeAdd64_HLERelease(__int64 volatile *, __int64);
232 char _InterlockedExchangeAdd8(char volatile *_Addend, char _Value);
234 long __cdecl _InterlockedIncrement(long volatile *_Addend);
236 short _InterlockedIncrement16(short volatile *_Addend);
238 long _InterlockedOr(long volatile *_Value, long _Mask);
240 short _InterlockedOr16(short volatile *_Value, short _Mask);
242 char _InterlockedOr8(char volatile *_Value, char _Mask);
244 long _InterlockedXor(long volatile *_Value, long _Mask);
246 short _InterlockedXor16(short volatile *_Value, short _Mask);
248 char _InterlockedXor8(char volatile *_Value, char _Mask);
249 void __cdecl _invpcid(unsigned int, void *);
251 unsigned long __cdecl _lrotl(unsigned long, int);
253 unsigned long __cdecl _lrotr(unsigned long, int);
256 void _ReadBarrier(void);
258 void _ReadWriteBarrier(void);
260 void *_ReturnAddress(void);
261 unsigned int _rorx_u32(unsigned int, const unsigned int);
263 unsigned int __cdecl _rotl(unsigned int _Value, int _Shift);
265 unsigned short _rotl16(unsigned short _Value, unsigned char _Shift);
267 unsigned __int64 __cdecl _rotl64(unsigned __int64 _Value, int _Shift);
269 unsigned char _rotl8(unsigned char _Value, unsigned char _Shift);
271 unsigned int __cdecl _rotr(unsigned int _Value, int _Shift);
273 unsigned short _rotr16(unsigned short _Value, unsigned char _Shift);
275 unsigned __int64 __cdecl _rotr64(unsigned __int64 _Value, int _Shift);
277 unsigned char _rotr8(unsigned char _Value, unsigned char _Shift);
278 int _sarx_i32(int, unsigned int);
280 int __cdecl _setjmp(jmp_buf);
282 unsigned int _shlx_u32(unsigned int, unsigned int);
283 unsigned int _shrx_u32(unsigned int, unsigned int);
284 void _Store_HLERelease(long volatile *, long);
285 void _Store64_HLERelease(__int64 volatile *, __int64);
286 void _StorePointer_HLERelease(void *volatile *, void *);
288 void _WriteBarrier(void);
289 unsigned __int32 xbegin(void);
292 unsigned __int64 __cdecl _xgetbv(unsigned int);
293 void __cdecl _xrstor(void const *, unsigned __int64);
294 void __cdecl _xsave(void *, unsigned __int64);
295 void __cdecl _xsaveopt(void *, unsigned __int64);
296 void __cdecl _xsetbv(unsigned int, unsigned __int64);
298 /* These additional intrinsics are turned on in x64/amd64/x86_64 mode. */
300 void __addgsbyte(unsigned long, unsigned char);
301 void __addgsdword(unsigned long, unsigned long);
302 void __addgsqword(unsigned long, unsigned __int64);
303 void __addgsword(unsigned long, unsigned short);
305 void __faststorefence(void);
306 void __incgsbyte(unsigned long);
307 void __incgsdword(unsigned long);
308 void __incgsqword(unsigned long);
309 void __incgsword(unsigned long);
310 unsigned char __lwpins64(unsigned __int64, unsigned int, unsigned int);
311 void __lwpval64(unsigned __int64, unsigned int, unsigned int);
312 unsigned __int64 __lzcnt64(unsigned __int64);
314 void __movsq(unsigned long long *, unsigned long long const *, size_t);
315 __int64 __mulh(__int64, __int64);
317 unsigned __int64 __popcnt64(unsigned __int64);
319 unsigned char __readgsbyte(unsigned long);
321 unsigned long __readgsdword(unsigned long);
323 unsigned __int64 __readgsqword(unsigned long);
324 unsigned short __readgsword(unsigned long);
325 unsigned __int64 __shiftleft128(unsigned __int64 _LowPart,
326 unsigned __int64 _HighPart,
327 unsigned char _Shift);
328 unsigned __int64 __shiftright128(unsigned __int64 _LowPart,
329 unsigned __int64 _HighPart,
330 unsigned char _Shift);
332 void __stosq(unsigned __int64 *, unsigned __int64, size_t);
333 unsigned char __vmx_on(unsigned __int64 *);
334 unsigned char __vmx_vmclear(unsigned __int64 *);
335 unsigned char __vmx_vmlaunch(void);
336 unsigned char __vmx_vmptrld(unsigned __int64 *);
337 unsigned char __vmx_vmread(size_t, size_t *);
338 unsigned char __vmx_vmresume(void);
339 unsigned char __vmx_vmwrite(size_t, size_t);
340 void __writegsbyte(unsigned long, unsigned char);
341 void __writegsdword(unsigned long, unsigned long);
342 void __writegsqword(unsigned long, unsigned __int64);
343 void __writegsword(unsigned long, unsigned short);
345 unsigned char _BitScanForward64(unsigned long *_Index, unsigned __int64 _Mask);
347 unsigned char _BitScanReverse64(unsigned long *_Index, unsigned __int64 _Mask);
349 unsigned char _bittest64(__int64 const *, __int64);
351 unsigned char _bittestandcomplement64(__int64 *, __int64);
353 unsigned char _bittestandreset64(__int64 *, __int64);
355 unsigned char _bittestandset64(__int64 *, __int64);
356 unsigned __int64 __cdecl _byteswap_uint64(unsigned __int64);
357 void __cdecl _fxrstor64(void const *);
358 void __cdecl _fxsave64(void *);
359 long _InterlockedAnd_np(long volatile *_Value, long _Mask);
360 short _InterlockedAnd16_np(short volatile *_Value, short _Mask);
361 __int64 _InterlockedAnd64_np(__int64 volatile *_Value, __int64 _Mask);
362 char _InterlockedAnd8_np(char volatile *_Value, char _Mask);
363 unsigned char _interlockedbittestandreset64(__int64 volatile *, __int64);
365 unsigned char _interlockedbittestandset64(__int64 volatile *, __int64);
366 long _InterlockedCompareExchange_np(long volatile *_Destination, long _Exchange,
368 unsigned char _InterlockedCompareExchange128(__int64 volatile *_Destination,
369 __int64 _ExchangeHigh,
370 __int64 _ExchangeLow,
371 __int64 *_CompareandResult);
372 unsigned char _InterlockedCompareExchange128_np(__int64 volatile *_Destination,
373 __int64 _ExchangeHigh,
374 __int64 _ExchangeLow,
375 __int64 *_ComparandResult);
376 short _InterlockedCompareExchange16_np(short volatile *_Destination,
377 short _Exchange, short _Comparand);
378 __int64 _InterlockedCompareExchange64_HLEAcquire(__int64 volatile *, __int64,
380 __int64 _InterlockedCompareExchange64_HLERelease(__int64 volatile *, __int64,
382 __int64 _InterlockedCompareExchange64_np(__int64 volatile *_Destination,
383 __int64 _Exchange, __int64 _Comparand);
384 void *_InterlockedCompareExchangePointer(void *volatile *_Destination,
385 void *_Exchange, void *_Comparand);
386 void *_InterlockedCompareExchangePointer_np(void *volatile *_Destination,
387 void *_Exchange, void *_Comparand);
389 __int64 _InterlockedDecrement64(__int64 volatile *_Addend);
391 __int64 _InterlockedExchange64(__int64 volatile *_Target, __int64 _Value);
393 __int64 _InterlockedExchangeAdd64(__int64 volatile *_Addend, __int64 _Value);
394 void *_InterlockedExchangePointer(void *volatile *_Target, void *_Value);
396 __int64 _InterlockedIncrement64(__int64 volatile *_Addend);
397 long _InterlockedOr_np(long volatile *_Value, long _Mask);
398 short _InterlockedOr16_np(short volatile *_Value, short _Mask);
400 __int64 _InterlockedOr64(__int64 volatile *_Value, __int64 _Mask);
401 __int64 _InterlockedOr64_np(__int64 volatile *_Value, __int64 _Mask);
402 char _InterlockedOr8_np(char volatile *_Value, char _Mask);
403 long _InterlockedXor_np(long volatile *_Value, long _Mask);
404 short _InterlockedXor16_np(short volatile *_Value, short _Mask);
406 __int64 _InterlockedXor64(__int64 volatile *_Value, __int64 _Mask);
407 __int64 _InterlockedXor64_np(__int64 volatile *_Value, __int64 _Mask);
408 char _InterlockedXor8_np(char volatile *_Value, char _Mask);
410 __int64 _mul128(__int64 _Multiplier, __int64 _Multiplicand,
411 __int64 *_HighProduct);
412 unsigned __int64 _rorx_u64(unsigned __int64, const unsigned int);
413 __int64 _sarx_i64(__int64, unsigned int);
415 int __cdecl _setjmpex(jmp_buf);
417 unsigned __int64 _shlx_u64(unsigned __int64, unsigned int);
418 unsigned __int64 _shrx_u64(unsigned __int64, unsigned int);
420 * Multiply two 64-bit integers and obtain a 64-bit result.
421 * The low-half is returned directly and the high half is in an out parameter.
423 static __inline__ unsigned __int64 __attribute__((__always_inline__, __nodebug__))
424 _umul128(unsigned __int64 _Multiplier, unsigned __int64 _Multiplicand,
425 unsigned __int64 *_HighProduct) {
426 unsigned __int128 _FullProduct =
427 (unsigned __int128)_Multiplier * (unsigned __int128)_Multiplicand;
428 *_HighProduct = _FullProduct >> 64;
431 static __inline__ unsigned __int64 __attribute__((__always_inline__, __nodebug__))
432 __umulh(unsigned __int64 _Multiplier, unsigned __int64 _Multiplicand) {
433 unsigned __int128 _FullProduct =
434 (unsigned __int128)_Multiplier * (unsigned __int128)_Multiplicand;
435 return _FullProduct >> 64;
437 void __cdecl _xrstor64(void const *, unsigned __int64);
438 void __cdecl _xsave64(void *, unsigned __int64);
439 void __cdecl _xsaveopt64(void *, unsigned __int64);
441 #endif /* __x86_64__ */
443 /*----------------------------------------------------------------------------*\
445 \*----------------------------------------------------------------------------*/
446 static __inline__ unsigned char __attribute__((__always_inline__, __nodebug__))
447 _rotl8(unsigned char _Value, unsigned char _Shift) {
449 return _Shift ? (_Value << _Shift) | (_Value >> (8 - _Shift)) : _Value;
451 static __inline__ unsigned char __attribute__((__always_inline__, __nodebug__))
452 _rotr8(unsigned char _Value, unsigned char _Shift) {
454 return _Shift ? (_Value >> _Shift) | (_Value << (8 - _Shift)) : _Value;
456 static __inline__ unsigned short __attribute__((__always_inline__, __nodebug__))
457 _rotl16(unsigned short _Value, unsigned char _Shift) {
459 return _Shift ? (_Value << _Shift) | (_Value >> (16 - _Shift)) : _Value;
461 static __inline__ unsigned short __attribute__((__always_inline__, __nodebug__))
462 _rotr16(unsigned short _Value, unsigned char _Shift) {
464 return _Shift ? (_Value >> _Shift) | (_Value << (16 - _Shift)) : _Value;
466 static __inline__ unsigned int __attribute__((__always_inline__, __nodebug__))
467 _rotl(unsigned int _Value, int _Shift) {
469 return _Shift ? (_Value << _Shift) | (_Value >> (32 - _Shift)) : _Value;
471 static __inline__ unsigned int __attribute__((__always_inline__, __nodebug__))
472 _rotr(unsigned int _Value, int _Shift) {
474 return _Shift ? (_Value >> _Shift) | (_Value << (32 - _Shift)) : _Value;
476 static __inline__ unsigned long __attribute__((__always_inline__, __nodebug__))
477 _lrotl(unsigned long _Value, int _Shift) {
479 return _Shift ? (_Value << _Shift) | (_Value >> (32 - _Shift)) : _Value;
481 static __inline__ unsigned long __attribute__((__always_inline__, __nodebug__))
482 _lrotr(unsigned long _Value, int _Shift) {
484 return _Shift ? (_Value >> _Shift) | (_Value << (32 - _Shift)) : _Value;
487 __inline__ unsigned __int64 __attribute__((__always_inline__, __nodebug__))
488 _rotl64(unsigned __int64 _Value, int _Shift) {
490 return _Shift ? (_Value << _Shift) | (_Value >> (64 - _Shift)) : _Value;
493 __inline__ unsigned __int64 __attribute__((__always_inline__, __nodebug__))
494 _rotr64(unsigned __int64 _Value, int _Shift) {
496 return _Shift ? (_Value >> _Shift) | (_Value << (64 - _Shift)) : _Value;
498 /*----------------------------------------------------------------------------*\
499 |* Bit Counting and Testing
500 \*----------------------------------------------------------------------------*/
501 static __inline__ unsigned char __attribute__((__always_inline__, __nodebug__))
502 _BitScanForward(unsigned long *_Index, unsigned long _Mask) {
505 *_Index = __builtin_ctzl(_Mask);
508 static __inline__ unsigned char __attribute__((__always_inline__, __nodebug__))
509 _BitScanReverse(unsigned long *_Index, unsigned long _Mask) {
512 *_Index = 31 - __builtin_clzl(_Mask);
515 static __inline__ unsigned short __attribute__((__always_inline__, __nodebug__))
516 __popcnt16(unsigned short value) {
517 return __builtin_popcount((int)value);
519 static __inline__ unsigned int __attribute__((__always_inline__, __nodebug__))
520 __popcnt(unsigned int value) {
521 return __builtin_popcount(value);
523 static __inline__ unsigned char __attribute__((__always_inline__, __nodebug__))
524 _bittest(long const *a, long b) {
525 return (*a >> b) & 1;
527 static __inline__ unsigned char __attribute__((__always_inline__, __nodebug__))
528 _bittestandcomplement(long *a, long b) {
529 unsigned char x = (*a >> b) & 1;
533 static __inline__ unsigned char __attribute__((__always_inline__, __nodebug__))
534 _bittestandreset(long *a, long b) {
535 unsigned char x = (*a >> b) & 1;
539 static __inline__ unsigned char __attribute__((__always_inline__, __nodebug__))
540 _bittestandset(long *a, long b) {
541 unsigned char x = (*a >> b) & 1;
545 #if defined(__i386__) || defined(__x86_64__)
546 static __inline__ unsigned char __attribute__((__always_inline__, __nodebug__))
547 _interlockedbittestandset(long volatile *__BitBase, long __BitPos) {
549 __asm__ ("xor %0, %0\n"
552 : "=r" (__Res), "+m"(*__BitBase)
558 static __inline__ unsigned char __attribute__((__always_inline__, __nodebug__))
559 _BitScanForward64(unsigned long *_Index, unsigned __int64 _Mask) {
562 *_Index = __builtin_ctzll(_Mask);
565 static __inline__ unsigned char __attribute__((__always_inline__, __nodebug__))
566 _BitScanReverse64(unsigned long *_Index, unsigned __int64 _Mask) {
569 *_Index = 63 - __builtin_clzll(_Mask);
573 unsigned __int64 __attribute__((__always_inline__, __nodebug__))
574 __popcnt64(unsigned __int64 value) {
575 return __builtin_popcountll(value);
577 static __inline__ unsigned char __attribute__((__always_inline__, __nodebug__))
578 _bittest64(__int64 const *a, __int64 b) {
579 return (*a >> b) & 1;
581 static __inline__ unsigned char __attribute__((__always_inline__, __nodebug__))
582 _bittestandcomplement64(__int64 *a, __int64 b) {
583 unsigned char x = (*a >> b) & 1;
584 *a = *a ^ (1ll << b);
587 static __inline__ unsigned char __attribute__((__always_inline__, __nodebug__))
588 _bittestandreset64(__int64 *a, __int64 b) {
589 unsigned char x = (*a >> b) & 1;
590 *a = *a & ~(1ll << b);
593 static __inline__ unsigned char __attribute__((__always_inline__, __nodebug__))
594 _bittestandset64(__int64 *a, __int64 b) {
595 unsigned char x = (*a >> b) & 1;
596 *a = *a | (1ll << b);
599 static __inline__ unsigned char __attribute__((__always_inline__, __nodebug__))
600 _interlockedbittestandset64(__int64 volatile *__BitBase, __int64 __BitPos) {
602 __asm__ ("xor %0, %0\n"
605 : "=r" (__Res), "+m"(*__BitBase)
610 /*----------------------------------------------------------------------------*\
611 |* Interlocked Exchange Add
612 \*----------------------------------------------------------------------------*/
613 static __inline__ char __attribute__((__always_inline__, __nodebug__))
614 _InterlockedExchangeAdd8(char volatile *_Addend, char _Value) {
615 return __atomic_add_fetch(_Addend, _Value, 0) - _Value;
617 static __inline__ short __attribute__((__always_inline__, __nodebug__))
618 _InterlockedExchangeAdd16(short volatile *_Addend, short _Value) {
619 return __atomic_add_fetch(_Addend, _Value, 0) - _Value;
622 static __inline__ __int64 __attribute__((__always_inline__, __nodebug__))
623 _InterlockedExchangeAdd64(__int64 volatile *_Addend, __int64 _Value) {
624 return __atomic_add_fetch(_Addend, _Value, 0) - _Value;
627 /*----------------------------------------------------------------------------*\
628 |* Interlocked Exchange Sub
629 \*----------------------------------------------------------------------------*/
630 static __inline__ char __attribute__((__always_inline__, __nodebug__))
631 _InterlockedExchangeSub8(char volatile *_Subend, char _Value) {
632 return __atomic_sub_fetch(_Subend, _Value, 0) + _Value;
634 static __inline__ short __attribute__((__always_inline__, __nodebug__))
635 _InterlockedExchangeSub16(short volatile *_Subend, short _Value) {
636 return __atomic_sub_fetch(_Subend, _Value, 0) + _Value;
638 static __inline__ long __attribute__((__always_inline__, __nodebug__))
639 _InterlockedExchangeSub(long volatile *_Subend, long _Value) {
640 return __atomic_sub_fetch(_Subend, _Value, 0) + _Value;
643 static __inline__ __int64 __attribute__((__always_inline__, __nodebug__))
644 _InterlockedExchangeSub64(__int64 volatile *_Subend, __int64 _Value) {
645 return __atomic_sub_fetch(_Subend, _Value, 0) + _Value;
648 /*----------------------------------------------------------------------------*\
649 |* Interlocked Increment
650 \*----------------------------------------------------------------------------*/
651 static __inline__ short __attribute__((__always_inline__, __nodebug__))
652 _InterlockedIncrement16(short volatile *_Value) {
653 return __atomic_add_fetch(_Value, 1, 0);
656 static __inline__ __int64 __attribute__((__always_inline__, __nodebug__))
657 _InterlockedIncrement64(__int64 volatile *_Value) {
658 return __atomic_add_fetch(_Value, 1, 0);
661 /*----------------------------------------------------------------------------*\
662 |* Interlocked Decrement
663 \*----------------------------------------------------------------------------*/
664 static __inline__ short __attribute__((__always_inline__, __nodebug__))
665 _InterlockedDecrement16(short volatile *_Value) {
666 return __atomic_sub_fetch(_Value, 1, 0);
669 static __inline__ __int64 __attribute__((__always_inline__, __nodebug__))
670 _InterlockedDecrement64(__int64 volatile *_Value) {
671 return __atomic_sub_fetch(_Value, 1, 0);
674 /*----------------------------------------------------------------------------*\
676 \*----------------------------------------------------------------------------*/
677 static __inline__ char __attribute__((__always_inline__, __nodebug__))
678 _InterlockedAnd8(char volatile *_Value, char _Mask) {
679 return __atomic_and_fetch(_Value, _Mask, 0);
681 static __inline__ short __attribute__((__always_inline__, __nodebug__))
682 _InterlockedAnd16(short volatile *_Value, short _Mask) {
683 return __atomic_and_fetch(_Value, _Mask, 0);
685 static __inline__ long __attribute__((__always_inline__, __nodebug__))
686 _InterlockedAnd(long volatile *_Value, long _Mask) {
687 return __atomic_and_fetch(_Value, _Mask, 0);
690 static __inline__ __int64 __attribute__((__always_inline__, __nodebug__))
691 _InterlockedAnd64(__int64 volatile *_Value, __int64 _Mask) {
692 return __atomic_and_fetch(_Value, _Mask, 0);
695 /*----------------------------------------------------------------------------*\
697 \*----------------------------------------------------------------------------*/
698 static __inline__ char __attribute__((__always_inline__, __nodebug__))
699 _InterlockedOr8(char volatile *_Value, char _Mask) {
700 return __atomic_or_fetch(_Value, _Mask, 0);
702 static __inline__ short __attribute__((__always_inline__, __nodebug__))
703 _InterlockedOr16(short volatile *_Value, short _Mask) {
704 return __atomic_or_fetch(_Value, _Mask, 0);
706 static __inline__ long __attribute__((__always_inline__, __nodebug__))
707 _InterlockedOr(long volatile *_Value, long _Mask) {
708 return __atomic_or_fetch(_Value, _Mask, 0);
711 static __inline__ __int64 __attribute__((__always_inline__, __nodebug__))
712 _InterlockedOr64(__int64 volatile *_Value, __int64 _Mask) {
713 return __atomic_or_fetch(_Value, _Mask, 0);
716 /*----------------------------------------------------------------------------*\
718 \*----------------------------------------------------------------------------*/
719 static __inline__ char __attribute__((__always_inline__, __nodebug__))
720 _InterlockedXor8(char volatile *_Value, char _Mask) {
721 return __atomic_xor_fetch(_Value, _Mask, 0);
723 static __inline__ short __attribute__((__always_inline__, __nodebug__))
724 _InterlockedXor16(short volatile *_Value, short _Mask) {
725 return __atomic_xor_fetch(_Value, _Mask, 0);
727 static __inline__ long __attribute__((__always_inline__, __nodebug__))
728 _InterlockedXor(long volatile *_Value, long _Mask) {
729 return __atomic_xor_fetch(_Value, _Mask, 0);
732 static __inline__ __int64 __attribute__((__always_inline__, __nodebug__))
733 _InterlockedXor64(__int64 volatile *_Value, __int64 _Mask) {
734 return __atomic_xor_fetch(_Value, _Mask, 0);
737 /*----------------------------------------------------------------------------*\
738 |* Interlocked Exchange
739 \*----------------------------------------------------------------------------*/
740 static __inline__ char __attribute__((__always_inline__, __nodebug__))
741 _InterlockedExchange8(char volatile *_Target, char _Value) {
742 __atomic_exchange(_Target, &_Value, &_Value, 0);
745 static __inline__ short __attribute__((__always_inline__, __nodebug__))
746 _InterlockedExchange16(short volatile *_Target, short _Value) {
747 __atomic_exchange(_Target, &_Value, &_Value, 0);
751 static __inline__ __int64 __attribute__((__always_inline__, __nodebug__))
752 _InterlockedExchange64(__int64 volatile *_Target, __int64 _Value) {
753 __atomic_exchange(_Target, &_Value, &_Value, 0);
757 /*----------------------------------------------------------------------------*\
758 |* Interlocked Compare Exchange
759 \*----------------------------------------------------------------------------*/
760 static __inline__ char __attribute__((__always_inline__, __nodebug__))
761 _InterlockedCompareExchange8(char volatile *_Destination,
762 char _Exchange, char _Comparand) {
763 __atomic_compare_exchange(_Destination, &_Comparand, &_Exchange, 0, 0, 0);
766 static __inline__ short __attribute__((__always_inline__, __nodebug__))
767 _InterlockedCompareExchange16(short volatile *_Destination,
768 short _Exchange, short _Comparand) {
769 __atomic_compare_exchange(_Destination, &_Comparand, &_Exchange, 0, 0, 0);
772 static __inline__ __int64 __attribute__((__always_inline__, __nodebug__))
773 _InterlockedCompareExchange64(__int64 volatile *_Destination,
774 __int64 _Exchange, __int64 _Comparand) {
775 __atomic_compare_exchange(_Destination, &_Comparand, &_Exchange, 0, 0, 0);
778 /*----------------------------------------------------------------------------*\
780 \*----------------------------------------------------------------------------*/
781 #if defined(__i386__) || defined(__x86_64__)
782 static __inline__ void __attribute__((__always_inline__, __nodebug__))
783 __attribute__((deprecated("use other intrinsics or C++11 atomics instead")))
784 _ReadWriteBarrier(void) {
785 __asm__ volatile ("" : : : "memory");
787 static __inline__ void __attribute__((__always_inline__, __nodebug__))
788 __attribute__((deprecated("use other intrinsics or C++11 atomics instead")))
790 __asm__ volatile ("" : : : "memory");
792 static __inline__ void __attribute__((__always_inline__, __nodebug__))
793 __attribute__((deprecated("use other intrinsics or C++11 atomics instead")))
794 _WriteBarrier(void) {
795 __asm__ volatile ("" : : : "memory");
799 static __inline__ void __attribute__((__always_inline__, __nodebug__))
800 __faststorefence(void) {
801 __asm__ volatile("lock orq $0, (%%rsp)" : : : "memory");
804 /*----------------------------------------------------------------------------*\
806 |* (Pointers in address space #256 and #257 are relative to the GS and FS
807 |* segment registers, respectively.)
808 \*----------------------------------------------------------------------------*/
809 #define __ptr_to_addr_space(__addr_space_nbr, __type, __offset) \
810 ((volatile __type __attribute__((__address_space__(__addr_space_nbr)))*) \
814 static __inline__ unsigned char __attribute__((__always_inline__, __nodebug__))
815 __readfsbyte(unsigned long __offset) {
816 return *__ptr_to_addr_space(257, unsigned char, __offset);
818 static __inline__ unsigned __int64 __attribute__((__always_inline__, __nodebug__))
819 __readfsqword(unsigned long __offset) {
820 return *__ptr_to_addr_space(257, unsigned __int64, __offset);
822 static __inline__ unsigned short __attribute__((__always_inline__, __nodebug__))
823 __readfsword(unsigned long __offset) {
824 return *__ptr_to_addr_space(257, unsigned short, __offset);
828 static __inline__ unsigned char __attribute__((__always_inline__, __nodebug__))
829 __readgsbyte(unsigned long __offset) {
830 return *__ptr_to_addr_space(256, unsigned char, __offset);
832 static __inline__ unsigned long __attribute__((__always_inline__, __nodebug__))
833 __readgsdword(unsigned long __offset) {
834 return *__ptr_to_addr_space(256, unsigned long, __offset);
836 static __inline__ unsigned __int64 __attribute__((__always_inline__, __nodebug__))
837 __readgsqword(unsigned long __offset) {
838 return *__ptr_to_addr_space(256, unsigned __int64, __offset);
840 static __inline__ unsigned short __attribute__((__always_inline__, __nodebug__))
841 __readgsword(unsigned long __offset) {
842 return *__ptr_to_addr_space(256, unsigned short, __offset);
845 #undef __ptr_to_addr_space
846 /*----------------------------------------------------------------------------*\
848 \*----------------------------------------------------------------------------*/
849 #if defined(__i386__) || defined(__x86_64__)
850 static __inline__ void __attribute__((__always_inline__, __nodebug__))
851 __movsb(unsigned char *__dst, unsigned char const *__src, size_t __n) {
852 __asm__("rep movsb" : : "D"(__dst), "S"(__src), "c"(__n)
853 : "%edi", "%esi", "%ecx");
855 static __inline__ void __attribute__((__always_inline__, __nodebug__))
856 __movsd(unsigned long *__dst, unsigned long const *__src, size_t __n) {
857 __asm__("rep movsl" : : "D"(__dst), "S"(__src), "c"(__n)
858 : "%edi", "%esi", "%ecx");
860 static __inline__ void __attribute__((__always_inline__, __nodebug__))
861 __movsw(unsigned short *__dst, unsigned short const *__src, size_t __n) {
862 __asm__("rep movsh" : : "D"(__dst), "S"(__src), "c"(__n)
863 : "%edi", "%esi", "%ecx");
865 static __inline__ void __attribute__((__always_inline__, __nodebug__))
866 __stosb(unsigned char *__dst, unsigned char __x, size_t __n) {
867 __asm__("rep stosb" : : "D"(__dst), "a"(__x), "c"(__n)
870 static __inline__ void __attribute__((__always_inline__, __nodebug__))
871 __stosd(unsigned long *__dst, unsigned long __x, size_t __n) {
872 __asm__("rep stosl" : : "D"(__dst), "a"(__x), "c"(__n)
875 static __inline__ void __attribute__((__always_inline__, __nodebug__))
876 __stosw(unsigned short *__dst, unsigned short __x, size_t __n) {
877 __asm__("rep stosh" : : "D"(__dst), "a"(__x), "c"(__n)
882 static __inline__ void __attribute__((__always_inline__, __nodebug__))
883 __movsq(unsigned long long *__dst, unsigned long long const *__src, size_t __n) {
884 __asm__("rep movsq" : : "D"(__dst), "S"(__src), "c"(__n)
885 : "%edi", "%esi", "%ecx");
887 static __inline__ void __attribute__((__always_inline__, __nodebug__))
888 __stosq(unsigned __int64 *__dst, unsigned __int64 __x, size_t __n) {
889 __asm__("rep stosq" : : "D"(__dst), "a"(__x), "c"(__n)
894 /*----------------------------------------------------------------------------*\
896 \*----------------------------------------------------------------------------*/
897 static __inline__ void * __attribute__((__always_inline__, __nodebug__))
898 _AddressOfReturnAddress(void) {
899 return (void*)((char*)__builtin_frame_address(0) + sizeof(void*));
901 static __inline__ void * __attribute__((__always_inline__, __nodebug__))
902 _ReturnAddress(void) {
903 return __builtin_return_address(0);
905 #if defined(__i386__) || defined(__x86_64__)
906 static __inline__ void __attribute__((__always_inline__, __nodebug__))
907 __cpuid(int __info[4], int __level) {
908 __asm__ ("cpuid" : "=a"(__info[0]), "=b" (__info[1]), "=c"(__info[2]), "=d"(__info[3])
911 static __inline__ void __attribute__((__always_inline__, __nodebug__))
912 __cpuidex(int __info[4], int __level, int __ecx) {
913 __asm__ ("cpuid" : "=a"(__info[0]), "=b" (__info[1]), "=c"(__info[2]), "=d"(__info[3])
914 : "a"(__level), "c"(__ecx));
916 static __inline__ unsigned __int64 __cdecl __attribute__((__always_inline__, __nodebug__))
917 _xgetbv(unsigned int __xcr_no) {
918 unsigned int __eax, __edx;
919 __asm__ ("xgetbv" : "=a" (__eax), "=d" (__edx) : "c" (__xcr_no));
920 return ((unsigned __int64)__edx << 32) | __eax;
922 static __inline__ void __attribute__((__always_inline__, __nodebug__))
924 __asm__ volatile ("hlt");
928 /*----------------------------------------------------------------------------*\
929 |* Privileged intrinsics
930 \*----------------------------------------------------------------------------*/
931 #if defined(__i386__) || defined(__x86_64__)
932 static __inline__ unsigned __int64 __attribute__((__always_inline__, __nodebug__))
933 __readmsr(unsigned long __register) {
934 // Loads the contents of a 64-bit model specific register (MSR) specified in
935 // the ECX register into registers EDX:EAX. The EDX register is loaded with
936 // the high-order 32 bits of the MSR and the EAX register is loaded with the
937 // low-order 32 bits. If less than 64 bits are implemented in the MSR being
938 // read, the values returned to EDX:EAX in unimplemented bit locations are
942 __asm__ ("rdmsr" : "=d"(__edx), "=a"(__eax) : "c"(__register));
943 return (((unsigned __int64)__edx) << 32) | (unsigned __int64)__eax;
946 static __inline__ unsigned long __attribute__((always_inline, __nodebug__))
948 unsigned long __cr3_val;
949 __asm__ __volatile__ ("mov %%cr3, %0" : "=q"(__cr3_val) : : "memory");
953 static __inline__ void __attribute__((always_inline, __nodebug__))
954 __writecr3(unsigned int __cr3_val) {
955 __asm__ ("mov %0, %%cr3" : : "q"(__cr3_val) : "memory");
963 #endif /* __INTRIN_H */
964 #endif /* _MSC_VER */