blob: baba6330a1cedee7b2d731669d31b735fd9866be [file] [log] [blame]
Eric Christopherd1428bf2013-08-31 00:22:48 +00001/* ===-------- Intrin.h ---------------------------------------------------===
2 *
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:
9 *
10 * The above copyright notice and this permission notice shall be included in
11 * all copies or substantial portions of the Software.
12 *
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
19 * THE SOFTWARE.
20 *
21 *===-----------------------------------------------------------------------===
22 */
23
24/* Only include this if we're compiling for the windows platform. */
25#ifndef _MSC_VER
26#include_next <Intrin.h>
27#else
28
29#ifndef __INTRIN_H
30#define __INTRIN_H
31
32/* First include the standard intrinsics. */
33#include <x86intrin.h>
34
Hans Wennborg1fd6dd32014-01-28 23:01:59 +000035/* For the definition of jmp_buf. */
36#include <setjmp.h>
37
Eric Christopher0db88a72013-09-18 22:24:01 +000038#ifdef __cplusplus
39extern "C" {
40#endif
41
Eric Christopherd1428bf2013-08-31 00:22:48 +000042/* And the random ones that aren't in those files. */
43__m64 _m_from_float(float);
44__m64 _m_from_int(int _l);
45void _m_prefetch(void *);
46float _m_to_float(__m64);
47int _m_to_int(__m64 _M);
48
49/* Other assorted instruction intrinsics. */
50void __addfsbyte(unsigned long, unsigned char);
51void __addfsdword(unsigned long, unsigned long);
52void __addfsword(unsigned long, unsigned short);
53void __code_seg(const char *);
Hans Wennborg854f7d32014-01-16 23:39:35 +000054static __inline__
Eric Christopherd1428bf2013-08-31 00:22:48 +000055void __cpuid(int[4], int);
Hans Wennborg12fb89e2014-01-31 19:44:55 +000056static __inline__
Eric Christopherd1428bf2013-08-31 00:22:48 +000057void __cpuidex(int[4], int, int);
Eric Christopher0db88a72013-09-18 22:24:01 +000058void __debugbreak(void);
Eric Christopherd1428bf2013-08-31 00:22:48 +000059__int64 __emul(int, int);
60unsigned __int64 __emulu(unsigned int, unsigned int);
Eric Christopher0db88a72013-09-18 22:24:01 +000061void __cdecl __fastfail(unsigned int);
Eric Christopherd1428bf2013-08-31 00:22:48 +000062unsigned int __getcallerseflags(void);
Hans Wennborg740a4d62014-01-28 22:55:01 +000063static __inline__
Eric Christopherd1428bf2013-08-31 00:22:48 +000064void __halt(void);
65unsigned char __inbyte(unsigned short);
66void __inbytestring(unsigned short, unsigned char *, unsigned long);
67void __incfsbyte(unsigned long);
68void __incfsdword(unsigned long);
69void __incfsword(unsigned long);
70unsigned long __indword(unsigned short);
71void __indwordstring(unsigned short, unsigned long *, unsigned long);
72void __int2c(void);
73void __invlpg(void *);
74unsigned short __inword(unsigned short);
75void __inwordstring(unsigned short, unsigned short *, unsigned long);
76void __lidt(void *);
77unsigned __int64 __ll_lshift(unsigned __int64, int);
78__int64 __ll_rshift(__int64, int);
79void __llwpcb(void *);
80unsigned char __lwpins32(unsigned int, unsigned int, unsigned int);
81void __lwpval32(unsigned int, unsigned int, unsigned int);
82unsigned int __lzcnt(unsigned int);
83unsigned short __lzcnt16(unsigned short);
84void __movsb(unsigned char *, unsigned char const *, size_t);
85void __movsd(unsigned long *, unsigned long const *, size_t);
86void __movsw(unsigned short *, unsigned short const *, size_t);
87void __nop(void);
88void __nvreg_restore_fence(void);
89void __nvreg_save_fence(void);
90void __outbyte(unsigned short, unsigned char);
91void __outbytestring(unsigned short, unsigned char *, unsigned long);
92void __outdword(unsigned short, unsigned long);
93void __outdwordstring(unsigned short, unsigned long *, unsigned long);
94void __outword(unsigned short, unsigned short);
95void __outwordstring(unsigned short, unsigned short *, unsigned long);
Warren Huntd6ffae92013-09-27 23:57:26 +000096static __inline__
Eric Christopherd1428bf2013-08-31 00:22:48 +000097unsigned int __popcnt(unsigned int);
Warren Huntd6ffae92013-09-27 23:57:26 +000098static __inline__
Eric Christopherd1428bf2013-08-31 00:22:48 +000099unsigned short __popcnt16(unsigned short);
Hans Wennborg740a4d62014-01-28 22:55:01 +0000100static __inline__
Eric Christopherd1428bf2013-08-31 00:22:48 +0000101unsigned __int64 __rdtsc(void);
102unsigned __int64 __rdtscp(unsigned int *);
103unsigned long __readcr0(void);
104unsigned long __readcr2(void);
105unsigned long __readcr3(void);
Eric Christopher439137e2014-01-24 12:13:47 +0000106unsigned long __readcr4(void);
Eric Christopherd1428bf2013-08-31 00:22:48 +0000107unsigned long __readcr8(void);
108unsigned int __readdr(unsigned int);
Hans Wennborg74ca0c42014-01-24 00:52:39 +0000109#ifdef __i386__
110static __inline__
Eric Christopherd1428bf2013-08-31 00:22:48 +0000111unsigned char __readfsbyte(unsigned long);
Hans Wennborg74ca0c42014-01-24 00:52:39 +0000112static __inline__
Eric Christopherd1428bf2013-08-31 00:22:48 +0000113unsigned long __readfsdword(unsigned long);
Hans Wennborg74ca0c42014-01-24 00:52:39 +0000114static __inline__
Eric Christopherd1428bf2013-08-31 00:22:48 +0000115unsigned __int64 __readfsqword(unsigned long);
Hans Wennborg74ca0c42014-01-24 00:52:39 +0000116static __inline__
Eric Christopherd1428bf2013-08-31 00:22:48 +0000117unsigned short __readfsword(unsigned long);
Hans Wennborg74ca0c42014-01-24 00:52:39 +0000118#endif
Eric Christopherd1428bf2013-08-31 00:22:48 +0000119unsigned __int64 __readmsr(unsigned long);
120unsigned __int64 __readpmc(unsigned long);
121unsigned long __segmentlimit(unsigned long);
122void __sidt(void *);
123void *__slwpcb(void);
124void __stosb(unsigned char *, unsigned char, size_t);
125void __stosd(unsigned long *, unsigned long, size_t);
126void __stosw(unsigned short *, unsigned short, size_t);
127void __svm_clgi(void);
128void __svm_invlpga(void *, int);
129void __svm_skinit(int);
130void __svm_stgi(void);
131void __svm_vmload(size_t);
132void __svm_vmrun(size_t);
133void __svm_vmsave(size_t);
134void __ud2(void);
135unsigned __int64 __ull_rshift(unsigned __int64, int);
136void __vmx_off(void);
137void __vmx_vmptrst(unsigned __int64 *);
138void __wbinvd(void);
139void __writecr0(unsigned int);
140void __writecr3(unsigned int);
141void __writecr4(unsigned int);
142void __writecr8(unsigned int);
143void __writedr(unsigned int, unsigned int);
Eric Christopherd1428bf2013-08-31 00:22:48 +0000144void __writefsbyte(unsigned long, unsigned char);
145void __writefsdword(unsigned long, unsigned long);
146void __writefsqword(unsigned long, unsigned __int64);
147void __writefsword(unsigned long, unsigned short);
148void __writemsr(unsigned long, unsigned __int64);
Warren Huntd6ffae92013-09-27 23:57:26 +0000149static __inline__
Eric Christopherd1428bf2013-08-31 00:22:48 +0000150void *_AddressOfReturnAddress(void);
151unsigned int _andn_u32(unsigned int, unsigned int);
152unsigned int _bextr_u32(unsigned int, unsigned int, unsigned int);
Eric Christopherd1428bf2013-08-31 00:22:48 +0000153unsigned int _bextri_u32(unsigned int, unsigned int);
Warren Huntd6ffae92013-09-27 23:57:26 +0000154static __inline__
Eric Christopherd1428bf2013-08-31 00:22:48 +0000155unsigned char _BitScanForward(unsigned long *_Index, unsigned long _Mask);
Warren Huntd6ffae92013-09-27 23:57:26 +0000156static __inline__
Eric Christopherd1428bf2013-08-31 00:22:48 +0000157unsigned char _BitScanReverse(unsigned long *_Index, unsigned long _Mask);
Warren Huntd6ffae92013-09-27 23:57:26 +0000158static __inline__
Eric Christopherd1428bf2013-08-31 00:22:48 +0000159unsigned char _bittest(long const *, long);
Warren Huntd6ffae92013-09-27 23:57:26 +0000160static __inline__
Eric Christopherd1428bf2013-08-31 00:22:48 +0000161unsigned char _bittestandcomplement(long *, long);
Warren Huntd6ffae92013-09-27 23:57:26 +0000162static __inline__
Eric Christopherd1428bf2013-08-31 00:22:48 +0000163unsigned char _bittestandreset(long *, long);
Warren Huntd6ffae92013-09-27 23:57:26 +0000164static __inline__
Eric Christopherd1428bf2013-08-31 00:22:48 +0000165unsigned char _bittestandset(long *, long);
166unsigned int _blcfill_u32(unsigned int);
167unsigned int _blci_u32(unsigned int);
168unsigned int _blcic_u32(unsigned int);
169unsigned int _blcmsk_u32(unsigned int);
170unsigned int _blcs_u32(unsigned int);
171unsigned int _blsfill_u32(unsigned int);
172unsigned int _blsi_u32(unsigned int);
173unsigned int _blsic_u32(unsigned int);
174unsigned int _blsmsk_u32(unsigned int);
Eric Christopherd1428bf2013-08-31 00:22:48 +0000175unsigned int _blsr_u32(unsigned int);
176unsigned __int64 __cdecl _byteswap_uint64(unsigned __int64);
177unsigned long __cdecl _byteswap_ulong(unsigned long);
178unsigned short __cdecl _byteswap_ushort(unsigned short);
179unsigned _bzhi_u32(unsigned int, unsigned int);
180void __cdecl _disable(void);
181void __cdecl _enable(void);
182void __cdecl _fxrstor(void const *);
183void __cdecl _fxsave(void *);
184long _InterlockedAddLargeStatistic(__int64 volatile *_Addend, long _Value);
Warren Huntd6ffae92013-09-27 23:57:26 +0000185static __inline__
Eric Christopherd1428bf2013-08-31 00:22:48 +0000186long _InterlockedAnd(long volatile *_Value, long _Mask);
Warren Huntd6ffae92013-09-27 23:57:26 +0000187static __inline__
Eric Christopherd1428bf2013-08-31 00:22:48 +0000188short _InterlockedAnd16(short volatile *_Value, short _Mask);
Warren Huntd6ffae92013-09-27 23:57:26 +0000189static __inline__
Eric Christopherd1428bf2013-08-31 00:22:48 +0000190char _InterlockedAnd8(char volatile *_Value, char _Mask);
191unsigned char _interlockedbittestandreset(long volatile *, long);
192unsigned char _interlockedbittestandset(long volatile *, long);
Warren Huntd6ffae92013-09-27 23:57:26 +0000193static __inline__
Eric Christopherd1428bf2013-08-31 00:22:48 +0000194long __cdecl _InterlockedCompareExchange(long volatile *_Destination,
195 long _Exchange, long _Comparand);
196long _InterlockedCompareExchange_HLEAcquire(long volatile *, long, long);
197long _InterlockedCompareExchange_HLERelease(long volatile *, long, long);
Warren Huntd6ffae92013-09-27 23:57:26 +0000198static __inline__
Eric Christopherd1428bf2013-08-31 00:22:48 +0000199short _InterlockedCompareExchange16(short volatile *_Destination,
200 short _Exchange, short _Comparand);
Warren Huntd6ffae92013-09-27 23:57:26 +0000201static __inline__
Eric Christopherd1428bf2013-08-31 00:22:48 +0000202__int64 _InterlockedCompareExchange64(__int64 volatile *_Destination,
203 __int64 _Exchange, __int64 _Comparand);
204__int64 _InterlockedcompareExchange64_HLEAcquire(__int64 volatile *, __int64,
205 __int64);
206__int64 _InterlockedCompareExchange64_HLERelease(__int64 volatile *, __int64,
207 __int64);
Warren Huntd6ffae92013-09-27 23:57:26 +0000208static __inline__
Eric Christopherd1428bf2013-08-31 00:22:48 +0000209char _InterlockedCompareExchange8(char volatile *_Destination, char _Exchange,
210 char _Comparand);
211void *_InterlockedCompareExchangePointer_HLEAcquire(void *volatile *, void *,
212 void *);
213void *_InterlockedCompareExchangePointer_HLERelease(void *volatile *, void *,
214 void *);
Warren Huntd6ffae92013-09-27 23:57:26 +0000215static __inline__
Eric Christopherd1428bf2013-08-31 00:22:48 +0000216long __cdecl _InterlockedDecrement(long volatile *_Addend);
Warren Huntd6ffae92013-09-27 23:57:26 +0000217static __inline__
Eric Christopherd1428bf2013-08-31 00:22:48 +0000218short _InterlockedDecrement16(short volatile *_Addend);
Warren Huntd6ffae92013-09-27 23:57:26 +0000219static __inline__
Eric Christopherd1428bf2013-08-31 00:22:48 +0000220long __cdecl _InterlockedExchange(long volatile *_Target, long _Value);
Warren Huntd6ffae92013-09-27 23:57:26 +0000221static __inline__
Eric Christopherd1428bf2013-08-31 00:22:48 +0000222short _InterlockedExchange16(short volatile *_Target, short _Value);
Warren Huntd6ffae92013-09-27 23:57:26 +0000223static __inline__
Eric Christopherd1428bf2013-08-31 00:22:48 +0000224char _InterlockedExchange8(char volatile *_Target, char _Value);
Warren Huntd6ffae92013-09-27 23:57:26 +0000225static __inline__
Eric Christopherd1428bf2013-08-31 00:22:48 +0000226long __cdecl _InterlockedExchangeAdd(long volatile *_Addend, long _Value);
227long _InterlockedExchangeAdd_HLEAcquire(long volatile *, long);
228long _InterlockedExchangeAdd_HLERelease(long volatile *, long);
Reid Kleckner924eb2a2014-01-27 18:48:02 +0000229static __inline__
Eric Christopher439137e2014-01-24 12:13:47 +0000230short _InterlockedExchangeAdd16(short volatile *_Addend, short _Value);
231__int64 _InterlockedExchangeAdd64_HLEAcquire(__int64 volatile *, __int64);
232__int64 _InterlockedExchangeAdd64_HLERelease(__int64 volatile *, __int64);
Warren Huntd6ffae92013-09-27 23:57:26 +0000233static __inline__
Eric Christopherd1428bf2013-08-31 00:22:48 +0000234char _InterlockedExchangeAdd8(char volatile *_Addend, char _Value);
Warren Huntd6ffae92013-09-27 23:57:26 +0000235static __inline__
Eric Christopherd1428bf2013-08-31 00:22:48 +0000236long __cdecl _InterlockedIncrement(long volatile *_Addend);
Warren Huntd6ffae92013-09-27 23:57:26 +0000237static __inline__
Eric Christopherd1428bf2013-08-31 00:22:48 +0000238short _InterlockedIncrement16(short volatile *_Addend);
Warren Huntd6ffae92013-09-27 23:57:26 +0000239static __inline__
Eric Christopherd1428bf2013-08-31 00:22:48 +0000240long _InterlockedOr(long volatile *_Value, long _Mask);
Warren Huntd6ffae92013-09-27 23:57:26 +0000241static __inline__
Eric Christopherd1428bf2013-08-31 00:22:48 +0000242short _InterlockedOr16(short volatile *_Value, short _Mask);
Warren Huntd6ffae92013-09-27 23:57:26 +0000243static __inline__
Eric Christopherd1428bf2013-08-31 00:22:48 +0000244char _InterlockedOr8(char volatile *_Value, char _Mask);
Warren Huntd6ffae92013-09-27 23:57:26 +0000245static __inline__
Eric Christopherd1428bf2013-08-31 00:22:48 +0000246long _InterlockedXor(long volatile *_Value, long _Mask);
Warren Huntd6ffae92013-09-27 23:57:26 +0000247static __inline__
Eric Christopherd1428bf2013-08-31 00:22:48 +0000248short _InterlockedXor16(short volatile *_Value, short _Mask);
Warren Huntd6ffae92013-09-27 23:57:26 +0000249static __inline__
Eric Christopherd1428bf2013-08-31 00:22:48 +0000250char _InterlockedXor8(char volatile *_Value, char _Mask);
251void __cdecl _invpcid(unsigned int, void *);
Warren Huntd6ffae92013-09-27 23:57:26 +0000252static __inline__
Eric Christopherd1428bf2013-08-31 00:22:48 +0000253unsigned long __cdecl _lrotl(unsigned long, int);
Warren Huntd6ffae92013-09-27 23:57:26 +0000254static __inline__
Eric Christopherd1428bf2013-08-31 00:22:48 +0000255unsigned long __cdecl _lrotr(unsigned long, int);
Warren Huntd6ffae92013-09-27 23:57:26 +0000256static __inline__
Eric Christopherd1428bf2013-08-31 00:22:48 +0000257unsigned int _lzcnt_u32(unsigned int);
Reid Kleckner00d33a52013-10-17 01:29:48 +0000258static __inline__
Eric Christopherd1428bf2013-08-31 00:22:48 +0000259void _ReadBarrier(void);
Reid Kleckner00d33a52013-10-17 01:29:48 +0000260static __inline__
Eric Christopherd1428bf2013-08-31 00:22:48 +0000261void _ReadWriteBarrier(void);
Warren Huntd6ffae92013-09-27 23:57:26 +0000262static __inline__
Eric Christopherd1428bf2013-08-31 00:22:48 +0000263void *_ReturnAddress(void);
264unsigned int _rorx_u32(unsigned int, const unsigned int);
265int __cdecl _rdrand16_step(unsigned short *);
266int __cdecl _rdrand32_step(unsigned int *);
Warren Huntd6ffae92013-09-27 23:57:26 +0000267static __inline__
Eric Christopherfb4b4332013-08-31 00:27:38 +0000268unsigned int __cdecl _rotl(unsigned int _Value, int _Shift);
Warren Huntd6ffae92013-09-27 23:57:26 +0000269static __inline__
Eric Christopherd1428bf2013-08-31 00:22:48 +0000270unsigned short _rotl16(unsigned short _Value, unsigned char _Shift);
Warren Huntd6ffae92013-09-27 23:57:26 +0000271static __inline__
Eric Christopherd1428bf2013-08-31 00:22:48 +0000272unsigned __int64 __cdecl _rotl64(unsigned __int64 _Value, int _Shift);
Warren Huntd6ffae92013-09-27 23:57:26 +0000273static __inline__
Eric Christopherd1428bf2013-08-31 00:22:48 +0000274unsigned char _rotl8(unsigned char _Value, unsigned char _Shift);
Warren Huntd6ffae92013-09-27 23:57:26 +0000275static __inline__
Eric Christopherd1428bf2013-08-31 00:22:48 +0000276unsigned int __cdecl _rotr(unsigned int _Value, int _Shift);
Warren Huntd6ffae92013-09-27 23:57:26 +0000277static __inline__
Eric Christopherd1428bf2013-08-31 00:22:48 +0000278unsigned short _rotr16(unsigned short _Value, unsigned char _Shift);
Warren Huntd6ffae92013-09-27 23:57:26 +0000279static __inline__
Eric Christopherd1428bf2013-08-31 00:22:48 +0000280unsigned __int64 __cdecl _rotr64(unsigned __int64 _Value, int _Shift);
Warren Huntd6ffae92013-09-27 23:57:26 +0000281static __inline__
Eric Christopherd1428bf2013-08-31 00:22:48 +0000282unsigned char _rotr8(unsigned char _Value, unsigned char _Shift);
283int _sarx_i32(int, unsigned int);
Hans Wennborg1fd6dd32014-01-28 23:01:59 +0000284int __cdecl _setjmp(jmp_buf);
Eric Christopherd1428bf2013-08-31 00:22:48 +0000285unsigned int _shlx_u32(unsigned int, unsigned int);
286unsigned int _shrx_u32(unsigned int, unsigned int);
287void _Store_HLERelease(long volatile *, long);
288void _Store64_HLERelease(__int64 volatile *, __int64);
289void _StorePointer_HLERelease(void *volatile *, void *);
290unsigned int _t1mskc_u32(unsigned int);
291unsigned int _tzcnt_u32(unsigned int);
Eric Christopherd1428bf2013-08-31 00:22:48 +0000292unsigned int _tzmsk_u32(unsigned int);
Reid Kleckner00d33a52013-10-17 01:29:48 +0000293static __inline__
Eric Christopherd1428bf2013-08-31 00:22:48 +0000294void _WriteBarrier(void);
295void _xabort(const unsigned int imm);
296unsigned __int32 xbegin(void);
297void _xend(void);
Hans Wennborg854f7d32014-01-16 23:39:35 +0000298static __inline__
Eric Christopherd1428bf2013-08-31 00:22:48 +0000299unsigned __int64 __cdecl _xgetbv(unsigned int);
300void __cdecl _xrstor(void const *, unsigned __int64);
301void __cdecl _xsave(void *, unsigned __int64);
302void __cdecl _xsaveopt(void *, unsigned __int64);
303void __cdecl _xsetbv(unsigned int, unsigned __int64);
304unsigned char _xtest(void);
305
306/* These additional intrinsics are turned on in x64/amd64/x86_64 mode. */
Warren Hunt3f987942013-09-30 21:08:05 +0000307#ifdef __x86_64__
Eric Christopherd1428bf2013-08-31 00:22:48 +0000308void __addgsbyte(unsigned long, unsigned char);
309void __addgsdword(unsigned long, unsigned long);
310void __addgsqword(unsigned long, unsigned __int64);
311void __addgsword(unsigned long, unsigned short);
Reid Klecknerf08d6582014-01-27 19:16:35 +0000312static __inline__
Eric Christopherd1428bf2013-08-31 00:22:48 +0000313void __faststorefence(void);
314void __incgsbyte(unsigned long);
315void __incgsdword(unsigned long);
316void __incgsqword(unsigned long);
317void __incgsword(unsigned long);
Eric Christopher439137e2014-01-24 12:13:47 +0000318unsigned char __lwpins64(unsigned __int64, unsigned int, unsigned int);
319void __lwpval64(unsigned __int64, unsigned int, unsigned int);
320unsigned __int64 __lzcnt64(unsigned __int64);
321void __movsq(unsigned long long *, unsigned long long const *, size_t);
322__int64 __mulh(__int64, __int64);
Reid Kleckner924eb2a2014-01-27 18:48:02 +0000323static __inline__
Eric Christopherd1428bf2013-08-31 00:22:48 +0000324unsigned __int64 __popcnt64(unsigned __int64);
Hans Wennborga4421e02014-03-12 21:09:05 +0000325static __inline__
Eric Christopher439137e2014-01-24 12:13:47 +0000326unsigned char __readgsbyte(unsigned long);
Hans Wennborga4421e02014-03-12 21:09:05 +0000327static __inline__
Eric Christopher439137e2014-01-24 12:13:47 +0000328unsigned long __readgsdword(unsigned long);
Hans Wennborga4421e02014-03-12 21:09:05 +0000329static __inline__
330unsigned __int64 __readgsqword(unsigned long);
Eric Christopher439137e2014-01-24 12:13:47 +0000331unsigned short __readgsword(unsigned long);
Eric Christopherd1428bf2013-08-31 00:22:48 +0000332unsigned __int64 __shiftleft128(unsigned __int64 _LowPart,
333 unsigned __int64 _HighPart,
334 unsigned char _Shift);
335unsigned __int64 __shiftright128(unsigned __int64 _LowPart,
336 unsigned __int64 _HighPart,
337 unsigned char _Shift);
338void __stosq(unsigned __int64 *, unsigned __int64, size_t);
Eric Christopher439137e2014-01-24 12:13:47 +0000339unsigned __int64 __umulh(unsigned __int64, unsigned __int64);
340unsigned char __vmx_on(unsigned __int64 *);
341unsigned char __vmx_vmclear(unsigned __int64 *);
342unsigned char __vmx_vmlaunch(void);
343unsigned char __vmx_vmptrld(unsigned __int64 *);
344unsigned char __vmx_vmread(size_t, size_t *);
345unsigned char __vmx_vmresume(void);
346unsigned char __vmx_vmwrite(size_t, size_t);
347void __writegsbyte(unsigned long, unsigned char);
348void __writegsdword(unsigned long, unsigned long);
349void __writegsqword(unsigned long, unsigned __int64);
350void __writegsword(unsigned long, unsigned short);
Eric Christopherd1428bf2013-08-31 00:22:48 +0000351unsigned __int64 _andn_u64(unsigned __int64, unsigned __int64);
352unsigned __int64 _bextr_u64(unsigned __int64, unsigned int, unsigned int);
353unsigned __int64 _bextri_u64(unsigned __int64, unsigned int);
Warren Huntd6ffae92013-09-27 23:57:26 +0000354static __inline__
Eric Christopherd1428bf2013-08-31 00:22:48 +0000355unsigned char _BitScanForward64(unsigned long *_Index, unsigned __int64 _Mask);
Warren Huntd6ffae92013-09-27 23:57:26 +0000356static __inline__
Eric Christopherd1428bf2013-08-31 00:22:48 +0000357unsigned char _BitScanReverse64(unsigned long *_Index, unsigned __int64 _Mask);
Warren Huntd6ffae92013-09-27 23:57:26 +0000358static __inline__
Eric Christopherd1428bf2013-08-31 00:22:48 +0000359unsigned char _bittest64(__int64 const *, __int64);
Warren Huntd6ffae92013-09-27 23:57:26 +0000360static __inline__
Eric Christopherd1428bf2013-08-31 00:22:48 +0000361unsigned char _bittestandcomplement64(__int64 *, __int64);
Warren Huntd6ffae92013-09-27 23:57:26 +0000362static __inline__
Eric Christopherd1428bf2013-08-31 00:22:48 +0000363unsigned char _bittestandreset64(__int64 *, __int64);
Warren Huntd6ffae92013-09-27 23:57:26 +0000364static __inline__
Eric Christopherd1428bf2013-08-31 00:22:48 +0000365unsigned char _bittestandset64(__int64 *, __int64);
366unsigned __int64 _blcfill_u64(unsigned __int64);
367unsigned __int64 _blci_u64(unsigned __int64);
368unsigned __int64 _blcic_u64(unsigned __int64);
369unsigned __int64 _blcmsk_u64(unsigned __int64);
370unsigned __int64 _blcs_u64(unsigned __int64);
371unsigned __int64 _blsfill_u64(unsigned __int64);
372unsigned __int64 _blsi_u64(unsigned __int64);
373unsigned __int64 _blsic_u64(unsigned __int64);
Eric Christopher439137e2014-01-24 12:13:47 +0000374unsigned __int64 _blsmsk_u64(unsigned __int64);
Eric Christopherd1428bf2013-08-31 00:22:48 +0000375unsigned __int64 _blsr_u64(unsigned __int64);
376unsigned __int64 __cdecl _byteswap_uint64(unsigned __int64);
377unsigned __int64 _bzhi_u64(unsigned __int64, unsigned int);
378void __cdecl _fxrstor64(void const *);
379void __cdecl _fxsave64(void *);
380long _InterlockedAnd_np(long volatile *_Value, long _Mask);
381short _InterlockedAnd16_np(short volatile *_Value, short _Mask);
382__int64 _InterlockedAnd64_np(__int64 volatile *_Value, __int64 _Mask);
383char _InterlockedAnd8_np(char volatile *_Value, char _Mask);
384unsigned char _interlockedbittestandreset64(__int64 volatile *, __int64);
385unsigned char _interlockedbittestandset64(__int64 volatile *, __int64);
386long _InterlockedCompareExchange_np(long volatile *_Destination, long _Exchange,
387 long _Comparand);
388unsigned char _InterlockedCompareExchange128(__int64 volatile *_Destination,
389 __int64 _ExchangeHigh,
390 __int64 _ExchangeLow,
391 __int64 *_CompareandResult);
392unsigned char _InterlockedCompareExchange128_np(__int64 volatile *_Destination,
393 __int64 _ExchangeHigh,
394 __int64 _ExchangeLow,
395 __int64 *_ComparandResult);
396short _InterlockedCompareExchange16_np(short volatile *_Destination,
397 short _Exchange, short _Comparand);
Eric Christopher439137e2014-01-24 12:13:47 +0000398__int64 _InterlockedCompareExchange64_HLEAcquire(__int64 volatile *, __int64,
399 __int64);
400__int64 _InterlockedCompareExchange64_HLERelease(__int64 volatile *, __int64,
401 __int64);
Eric Christopherd1428bf2013-08-31 00:22:48 +0000402__int64 _InterlockedCompareExchange64_np(__int64 volatile *_Destination,
403 __int64 _Exchange, __int64 _Comparand);
Reid Klecknerf08d6582014-01-27 19:16:35 +0000404static __inline__
Eric Christopher58b40432014-01-25 01:38:30 +0000405void *_InterlockedCompareExchangePointer(void *volatile *_Destination,
406 void *_Exchange, void *_Comparand);
Eric Christopherd1428bf2013-08-31 00:22:48 +0000407void *_InterlockedCompareExchangePointer_np(void *volatile *_Destination,
408 void *_Exchange, void *_Comparand);
Reid Kleckner924eb2a2014-01-27 18:48:02 +0000409static __inline__
Eric Christopher439137e2014-01-24 12:13:47 +0000410__int64 _InterlockedDecrement64(__int64 volatile *_Addend);
Reid Kleckner924eb2a2014-01-27 18:48:02 +0000411static __inline__
Eric Christopher439137e2014-01-24 12:13:47 +0000412__int64 _InterlockedExchange64(__int64 volatile *_Target, __int64 _Value);
Reid Kleckner924eb2a2014-01-27 18:48:02 +0000413static __inline__
Eric Christopher439137e2014-01-24 12:13:47 +0000414__int64 _InterlockedExchangeAdd64(__int64 volatile *_Addend, __int64 _Value);
415void *_InterlockedExchangePointer(void *volatile *_Target, void *_Value);
Reid Kleckner924eb2a2014-01-27 18:48:02 +0000416static __inline__
Eric Christopher439137e2014-01-24 12:13:47 +0000417__int64 _InterlockedIncrement64(__int64 volatile *_Addend);
Eric Christopherd1428bf2013-08-31 00:22:48 +0000418long _InterlockedOr_np(long volatile *_Value, long _Mask);
419short _InterlockedOr16_np(short volatile *_Value, short _Mask);
Reid Kleckner924eb2a2014-01-27 18:48:02 +0000420static __inline__
Eric Christopher439137e2014-01-24 12:13:47 +0000421__int64 _InterlockedOr64(__int64 volatile *_Value, __int64 _Mask);
Warren Hunt41a993f2013-09-28 00:15:41 +0000422__int64 _InterlockedOr64_np(__int64 volatile *_Value, __int64 _Mask);
Eric Christopherd1428bf2013-08-31 00:22:48 +0000423char _InterlockedOr8_np(char volatile *_Value, char _Mask);
424long _InterlockedXor_np(long volatile *_Value, long _Mask);
425short _InterlockedXor16_np(short volatile *_Value, short _Mask);
Reid Kleckner924eb2a2014-01-27 18:48:02 +0000426static __inline__
Eric Christopher439137e2014-01-24 12:13:47 +0000427__int64 _InterlockedXor64(__int64 volatile *_Value, __int64 _Mask);
Eric Christopherd1428bf2013-08-31 00:22:48 +0000428__int64 _InterlockedXor64_np(__int64 volatile *_Value, __int64 _Mask);
429char _InterlockedXor8_np(char volatile *_Value, char _Mask);
Reid Kleckner924eb2a2014-01-27 18:48:02 +0000430static __inline__
Eric Christopherd1428bf2013-08-31 00:22:48 +0000431unsigned __int64 _lzcnt_u64(unsigned __int64);
432__int64 _mul128(__int64 _Multiplier, __int64 _Multiplicand,
433 __int64 *_HighProduct);
434unsigned int __cdecl _readfsbase_u32(void);
435unsigned __int64 __cdecl _readfsbase_u64(void);
436unsigned int __cdecl _readgsbase_u32(void);
437unsigned __int64 __cdecl _readgsbase_u64(void);
438unsigned __int64 _rorx_u64(unsigned __int64, const unsigned int);
Eric Christopher439137e2014-01-24 12:13:47 +0000439__int64 _sarx_i64(__int64, unsigned int);
Reid Kleckner33630902014-01-27 19:32:42 +0000440/* FIXME: Need definition for jmp_buf.
441 int __cdecl _setjmpex(jmp_buf); */
Eric Christopher439137e2014-01-24 12:13:47 +0000442unsigned __int64 _shlx_u64(unsigned __int64, unsigned int);
443unsigned __int64 shrx_u64(unsigned __int64, unsigned int);
Eric Christopherd1428bf2013-08-31 00:22:48 +0000444unsigned __int64 _tzcnt_u64(unsigned __int64);
445unsigned __int64 _tzmsk_u64(unsigned __int64);
446unsigned __int64 _umul128(unsigned __int64 _Multiplier,
447 unsigned __int64 _Multiplicand,
448 unsigned __int64 *_HighProduct);
449void __cdecl _writefsbase_u32(unsigned int);
450void _cdecl _writefsbase_u64(unsigned __int64);
451void __cdecl _writegsbase_u32(unsigned int);
452void __cdecl _writegsbase_u64(unsigned __int64);
453void __cdecl _xrstor64(void const *, unsigned __int64);
454void __cdecl _xsave64(void *, unsigned __int64);
455void __cdecl _xsaveopt64(void *, unsigned __int64);
Eric Christopher0db88a72013-09-18 22:24:01 +0000456
Warren Hunt3f987942013-09-30 21:08:05 +0000457#endif /* __x86_64__ */
Reid Klecknerf0e23222013-09-19 00:19:53 +0000458
Warren Huntd6ffae92013-09-27 23:57:26 +0000459/*----------------------------------------------------------------------------*\
460|* Bit Twiddling
461\*----------------------------------------------------------------------------*/
462static __inline__ unsigned char __attribute__((__always_inline__, __nodebug__))
463_rotl8(unsigned char _Value, unsigned char _Shift) {
464 _Shift &= 0x7;
465 return _Shift ? (_Value << _Shift) | (_Value >> (8 - _Shift)) : _Value;
466}
467static __inline__ unsigned char __attribute__((__always_inline__, __nodebug__))
468_rotr8(unsigned char _Value, unsigned char _Shift) {
469 _Shift &= 0x7;
470 return _Shift ? (_Value >> _Shift) | (_Value << (8 - _Shift)) : _Value;
471}
472static __inline__ unsigned short __attribute__((__always_inline__, __nodebug__))
473_rotl16(unsigned short _Value, unsigned char _Shift) {
474 _Shift &= 0xf;
475 return _Shift ? (_Value << _Shift) | (_Value >> (16 - _Shift)) : _Value;
476}
477static __inline__ unsigned short __attribute__((__always_inline__, __nodebug__))
478_rotr16(unsigned short _Value, unsigned char _Shift) {
479 _Shift &= 0xf;
480 return _Shift ? (_Value >> _Shift) | (_Value << (16 - _Shift)) : _Value;
481}
482static __inline__ unsigned int __attribute__((__always_inline__, __nodebug__))
483_rotl(unsigned int _Value, int _Shift) {
484 _Shift &= 0x1f;
485 return _Shift ? (_Value << _Shift) | (_Value >> (32 - _Shift)) : _Value;
486}
487static __inline__ unsigned int __attribute__((__always_inline__, __nodebug__))
488_rotr(unsigned int _Value, int _Shift) {
489 _Shift &= 0x1f;
490 return _Shift ? (_Value >> _Shift) | (_Value << (32 - _Shift)) : _Value;
491}
492static __inline__ unsigned long __attribute__((__always_inline__, __nodebug__))
493_lrotl(unsigned long _Value, int _Shift) {
494 _Shift &= 0x1f;
495 return _Shift ? (_Value << _Shift) | (_Value >> (32 - _Shift)) : _Value;
496}
497static __inline__ unsigned long __attribute__((__always_inline__, __nodebug__))
498_lrotr(unsigned long _Value, int _Shift) {
499 _Shift &= 0x1f;
500 return _Shift ? (_Value >> _Shift) | (_Value << (32 - _Shift)) : _Value;
501}
502static
503__inline__ unsigned __int64 __attribute__((__always_inline__, __nodebug__))
504_rotl64(unsigned __int64 _Value, int _Shift) {
505 _Shift &= 0x3f;
506 return _Shift ? (_Value << _Shift) | (_Value >> (64 - _Shift)) : _Value;
507}
508static
509__inline__ unsigned __int64 __attribute__((__always_inline__, __nodebug__))
510_rotr64(unsigned __int64 _Value, int _Shift) {
511 _Shift &= 0x3f;
512 return _Shift ? (_Value >> _Shift) | (_Value << (64 - _Shift)) : _Value;
513}
514/*----------------------------------------------------------------------------*\
515|* Bit Counting and Testing
516\*----------------------------------------------------------------------------*/
517static __inline__ unsigned char __attribute__((__always_inline__, __nodebug__))
518_BitScanForward(unsigned long *_Index, unsigned long _Mask) {
519 if (!_Mask)
520 return 0;
521 *_Index = __builtin_ctzl(_Mask);
522 return 1;
523}
524static __inline__ unsigned char __attribute__((__always_inline__, __nodebug__))
525_BitScanReverse(unsigned long *_Index, unsigned long _Mask) {
526 if (!_Mask)
527 return 0;
528 *_Index = 31 - __builtin_clzl(_Mask);
529 return 1;
530}
531static __inline__ unsigned int __attribute__((__always_inline__, __nodebug__))
532_lzcnt_u32(unsigned int a) {
533 if (!a)
534 return 32;
535 return __builtin_clzl(a);
536}
537static __inline__ unsigned short __attribute__((__always_inline__, __nodebug__))
538__popcnt16(unsigned short value) {
539 return __builtin_popcount((int)value);
540}
541static __inline__ unsigned int __attribute__((__always_inline__, __nodebug__))
542__popcnt(unsigned int value) {
543 return __builtin_popcount(value);
544}
545static __inline__ unsigned char __attribute__((__always_inline__, __nodebug__))
546_bittest(long const *a, long b) {
547 return (*a >> b) & 1;
548}
549static __inline__ unsigned char __attribute__((__always_inline__, __nodebug__))
550_bittestandcomplement(long *a, long b) {
551 unsigned char x = (*a >> b) & 1;
552 *a = *a ^ (1 << b);
553 return x;
554}
555static __inline__ unsigned char __attribute__((__always_inline__, __nodebug__))
556_bittestandreset(long *a, long b) {
557 unsigned char x = (*a >> b) & 1;
558 *a = *a & ~(1 << b);
559 return x;
560}
561static __inline__ unsigned char __attribute__((__always_inline__, __nodebug__))
562_bittestandset(long *a, long b) {
563 unsigned char x = (*a >> b) & 1;
Warren Hunt2731e3e2013-10-01 17:12:40 +0000564 *a = *a | (1 << b);
Warren Huntd6ffae92013-09-27 23:57:26 +0000565 return x;
566}
Warren Hunt3f987942013-09-30 21:08:05 +0000567#ifdef __x86_64__
Warren Huntd6ffae92013-09-27 23:57:26 +0000568static __inline__ unsigned char __attribute__((__always_inline__, __nodebug__))
569_BitScanForward64(unsigned long *_Index, unsigned __int64 _Mask) {
570 if (!_Mask)
571 return 0;
572 *_Index = __builtin_ctzll(_Mask);
573 return 1;
574}
575static __inline__ unsigned char __attribute__((__always_inline__, __nodebug__))
576_BitScanReverse64(unsigned long *_Index, unsigned __int64 _Mask) {
577 if (!_Mask)
578 return 0;
579 *_Index = 63 - __builtin_clzll(_Mask);
580 return 1;
581}
582static
583__inline__ unsigned __int64 __attribute__((__always_inline__, __nodebug__))
584_lzcnt_u64(unsigned __int64 a) {
585 if (!a)
586 return 64;
587 return __builtin_clzll(a);
588}
589static __inline__
590unsigned __int64 __attribute__((__always_inline__, __nodebug__))
591 __popcnt64(unsigned __int64 value) {
592 return __builtin_popcountll(value);
593}
594static __inline__ unsigned char __attribute__((__always_inline__, __nodebug__))
595_bittest64(__int64 const *a, __int64 b) {
596 return (*a >> b) & 1;
597}
598static __inline__ unsigned char __attribute__((__always_inline__, __nodebug__))
599_bittestandcomplement64(__int64 *a, __int64 b) {
600 unsigned char x = (*a >> b) & 1;
601 *a = *a ^ (1ll << b);
602 return x;
603}
604static __inline__ unsigned char __attribute__((__always_inline__, __nodebug__))
605_bittestandreset64(__int64 *a, __int64 b) {
606 unsigned char x = (*a >> b) & 1;
607 *a = *a & ~(1ll << b);
608 return x;
609}
610static __inline__ unsigned char __attribute__((__always_inline__, __nodebug__))
611_bittestandset64(__int64 *a, __int64 b) {
612 unsigned char x = (*a >> b) & 1;
Warren Hunt2731e3e2013-10-01 17:12:40 +0000613 *a = *a | (1ll << b);
Warren Huntd6ffae92013-09-27 23:57:26 +0000614 return x;
615}
616#endif
617/*----------------------------------------------------------------------------*\
618|* Interlocked Exchange Add
619\*----------------------------------------------------------------------------*/
620static __inline__ char __attribute__((__always_inline__, __nodebug__))
621_InterlockedExchangeAdd8(char volatile *_Addend, char _Value) {
622 return __atomic_add_fetch(_Addend, _Value, 0) - _Value;
623}
624static __inline__ short __attribute__((__always_inline__, __nodebug__))
625_InterlockedExchangeAdd16(short volatile *_Addend, short _Value) {
626 return __atomic_add_fetch(_Addend, _Value, 0) - _Value;
627}
Warren Hunt3f987942013-09-30 21:08:05 +0000628#ifdef __x86_64__
Warren Huntd6ffae92013-09-27 23:57:26 +0000629static __inline__ __int64 __attribute__((__always_inline__, __nodebug__))
630_InterlockedExchangeAdd64(__int64 volatile *_Addend, __int64 _Value) {
631 return __atomic_add_fetch(_Addend, _Value, 0) - _Value;
632}
633#endif
634/*----------------------------------------------------------------------------*\
635|* Interlocked Exchange Sub
636\*----------------------------------------------------------------------------*/
637static __inline__ char __attribute__((__always_inline__, __nodebug__))
638_InterlockedExchangeSub8(char volatile *_Subend, char _Value) {
639 return __atomic_sub_fetch(_Subend, _Value, 0) + _Value;
640}
641static __inline__ short __attribute__((__always_inline__, __nodebug__))
642_InterlockedExchangeSub16(short volatile *_Subend, short _Value) {
643 return __atomic_sub_fetch(_Subend, _Value, 0) + _Value;
644}
645static __inline__ long __attribute__((__always_inline__, __nodebug__))
646_InterlockedExchangeSub(long volatile *_Subend, long _Value) {
647 return __atomic_sub_fetch(_Subend, _Value, 0) + _Value;
648}
Warren Hunt3f987942013-09-30 21:08:05 +0000649#ifdef __x86_64__
Warren Huntd6ffae92013-09-27 23:57:26 +0000650static __inline__ __int64 __attribute__((__always_inline__, __nodebug__))
651_InterlockedExchangeSub64(__int64 volatile *_Subend, __int64 _Value) {
652 return __atomic_sub_fetch(_Subend, _Value, 0) + _Value;
653}
654#endif
655/*----------------------------------------------------------------------------*\
656|* Interlocked Increment
657\*----------------------------------------------------------------------------*/
Hans Wennborg2ed88802014-01-23 19:15:39 +0000658static __inline__ short __attribute__((__always_inline__, __nodebug__))
659_InterlockedIncrement16(short volatile *_Value) {
Warren Huntd6ffae92013-09-27 23:57:26 +0000660 return __atomic_add_fetch(_Value, 1, 0);
661}
Warren Hunt3f987942013-09-30 21:08:05 +0000662#ifdef __x86_64__
Warren Huntd6ffae92013-09-27 23:57:26 +0000663static __inline__ __int64 __attribute__((__always_inline__, __nodebug__))
664_InterlockedIncrement64(__int64 volatile *_Value) {
665 return __atomic_add_fetch(_Value, 1, 0);
666}
667#endif
668/*----------------------------------------------------------------------------*\
669|* Interlocked Decrement
670\*----------------------------------------------------------------------------*/
Hans Wennborg2ed88802014-01-23 19:15:39 +0000671static __inline__ short __attribute__((__always_inline__, __nodebug__))
672_InterlockedDecrement16(short volatile *_Value) {
Warren Huntd6ffae92013-09-27 23:57:26 +0000673 return __atomic_sub_fetch(_Value, 1, 0);
674}
Warren Hunt3f987942013-09-30 21:08:05 +0000675#ifdef __x86_64__
Warren Huntd6ffae92013-09-27 23:57:26 +0000676static __inline__ __int64 __attribute__((__always_inline__, __nodebug__))
677_InterlockedDecrement64(__int64 volatile *_Value) {
678 return __atomic_sub_fetch(_Value, 1, 0);
679}
680#endif
681/*----------------------------------------------------------------------------*\
682|* Interlocked And
683\*----------------------------------------------------------------------------*/
684static __inline__ char __attribute__((__always_inline__, __nodebug__))
685_InterlockedAnd8(char volatile *_Value, char _Mask) {
686 return __atomic_and_fetch(_Value, _Mask, 0);
687}
688static __inline__ short __attribute__((__always_inline__, __nodebug__))
689_InterlockedAnd16(short volatile *_Value, short _Mask) {
690 return __atomic_and_fetch(_Value, _Mask, 0);
691}
692static __inline__ long __attribute__((__always_inline__, __nodebug__))
693_InterlockedAnd(long volatile *_Value, long _Mask) {
694 return __atomic_and_fetch(_Value, _Mask, 0);
695}
Warren Hunt3f987942013-09-30 21:08:05 +0000696#ifdef __x86_64__
Warren Huntd6ffae92013-09-27 23:57:26 +0000697static __inline__ __int64 __attribute__((__always_inline__, __nodebug__))
698_InterlockedAnd64(__int64 volatile *_Value, __int64 _Mask) {
699 return __atomic_and_fetch(_Value, _Mask, 0);
700}
701#endif
702/*----------------------------------------------------------------------------*\
703|* Interlocked Or
704\*----------------------------------------------------------------------------*/
705static __inline__ char __attribute__((__always_inline__, __nodebug__))
706_InterlockedOr8(char volatile *_Value, char _Mask) {
707 return __atomic_or_fetch(_Value, _Mask, 0);
708}
709static __inline__ short __attribute__((__always_inline__, __nodebug__))
710_InterlockedOr16(short volatile *_Value, short _Mask) {
711 return __atomic_or_fetch(_Value, _Mask, 0);
712}
713static __inline__ long __attribute__((__always_inline__, __nodebug__))
714_InterlockedOr(long volatile *_Value, long _Mask) {
715 return __atomic_or_fetch(_Value, _Mask, 0);
716}
Warren Hunt3f987942013-09-30 21:08:05 +0000717#ifdef __x86_64__
Warren Huntd6ffae92013-09-27 23:57:26 +0000718static __inline__ __int64 __attribute__((__always_inline__, __nodebug__))
719_InterlockedOr64(__int64 volatile *_Value, __int64 _Mask) {
720 return __atomic_or_fetch(_Value, _Mask, 0);
721}
722#endif
723/*----------------------------------------------------------------------------*\
724|* Interlocked Xor
725\*----------------------------------------------------------------------------*/
726static __inline__ char __attribute__((__always_inline__, __nodebug__))
727_InterlockedXor8(char volatile *_Value, char _Mask) {
728 return __atomic_xor_fetch(_Value, _Mask, 0);
729}
730static __inline__ short __attribute__((__always_inline__, __nodebug__))
731_InterlockedXor16(short volatile *_Value, short _Mask) {
732 return __atomic_xor_fetch(_Value, _Mask, 0);
733}
734static __inline__ long __attribute__((__always_inline__, __nodebug__))
735_InterlockedXor(long volatile *_Value, long _Mask) {
736 return __atomic_xor_fetch(_Value, _Mask, 0);
737}
Warren Hunt3f987942013-09-30 21:08:05 +0000738#ifdef __x86_64__
Warren Huntd6ffae92013-09-27 23:57:26 +0000739static __inline__ __int64 __attribute__((__always_inline__, __nodebug__))
740_InterlockedXor64(__int64 volatile *_Value, __int64 _Mask) {
741 return __atomic_xor_fetch(_Value, _Mask, 0);
742}
743#endif
744/*----------------------------------------------------------------------------*\
745|* Interlocked Exchange
746\*----------------------------------------------------------------------------*/
747static __inline__ char __attribute__((__always_inline__, __nodebug__))
748_InterlockedExchange8(char volatile *_Target, char _Value) {
749 __atomic_exchange(_Target, &_Value, &_Value, 0);
750 return _Value;
751}
752static __inline__ short __attribute__((__always_inline__, __nodebug__))
753_InterlockedExchange16(short volatile *_Target, short _Value) {
754 __atomic_exchange(_Target, &_Value, &_Value, 0);
755 return _Value;
756}
757static __inline__ long __attribute__((__always_inline__, __nodebug__))
758_InterlockedExchange(long volatile *_Target, long _Value) {
759 __atomic_exchange(_Target, &_Value, &_Value, 0);
760 return _Value;
761}
Warren Hunt3f987942013-09-30 21:08:05 +0000762#ifdef __x86_64__
Warren Huntd6ffae92013-09-27 23:57:26 +0000763static __inline__ __int64 __attribute__((__always_inline__, __nodebug__))
764_InterlockedExchange64(__int64 volatile *_Target, __int64 _Value) {
765 __atomic_exchange(_Target, &_Value, &_Value, 0);
766 return _Value;
767}
768#endif
769/*----------------------------------------------------------------------------*\
770|* Interlocked Compare Exchange
771\*----------------------------------------------------------------------------*/
772static __inline__ char __attribute__((__always_inline__, __nodebug__))
773_InterlockedCompareExchange8(char volatile *_Destination,
774 char _Exchange, char _Comparand) {
775 __atomic_compare_exchange(_Destination, &_Comparand, &_Exchange, 0, 0, 0);
776 return _Comparand;
777}
778static __inline__ short __attribute__((__always_inline__, __nodebug__))
779_InterlockedCompareExchange16(short volatile *_Destination,
780 short _Exchange, short _Comparand) {
781 __atomic_compare_exchange(_Destination, &_Comparand, &_Exchange, 0, 0, 0);
782 return _Comparand;
783}
Warren Hunt3f987942013-09-30 21:08:05 +0000784#ifdef __x86_64__
Reid Klecknerf08d6582014-01-27 19:16:35 +0000785static __inline__ void *__attribute__((__always_inline__, __nodebug__))
786_InterlockedCompareExchangePointer(void *volatile *_Destination,
787 void *_Exchange, void *_Comparand) {
788 __atomic_compare_exchange(_Destination, &_Comparand, &_Exchange, 0, 0, 0);
789 return _Comparand;
790}
791#endif
792#ifdef __x86_64__
Warren Huntd6ffae92013-09-27 23:57:26 +0000793static __inline__ __int64 __attribute__((__always_inline__, __nodebug__))
794_InterlockedCompareExchange64(__int64 volatile *_Destination,
795 __int64 _Exchange, __int64 _Comparand) {
796 __atomic_compare_exchange(_Destination, &_Comparand, &_Exchange, 0, 0, 0);
797 return _Comparand;
798}
799#endif
800/*----------------------------------------------------------------------------*\
Reid Kleckner00d33a52013-10-17 01:29:48 +0000801|* Barriers
802\*----------------------------------------------------------------------------*/
803static __inline__ void __attribute__((__always_inline__, __nodebug__))
804__attribute__((deprecated("use other intrinsics or C++11 atomics instead")))
805_ReadWriteBarrier(void) {
806 __asm__ volatile ("" : : : "memory");
807}
808static __inline__ void __attribute__((__always_inline__, __nodebug__))
809__attribute__((deprecated("use other intrinsics or C++11 atomics instead")))
810_ReadBarrier(void) {
811 __asm__ volatile ("" : : : "memory");
812}
813static __inline__ void __attribute__((__always_inline__, __nodebug__))
814__attribute__((deprecated("use other intrinsics or C++11 atomics instead")))
815_WriteBarrier(void) {
816 __asm__ volatile ("" : : : "memory");
817}
Reid Klecknerf08d6582014-01-27 19:16:35 +0000818#ifdef __x86_64__
819static __inline__ void __attribute__((__always_inline__, __nodebug__))
820__faststorefence(void) {
821 __asm__ volatile("lock orq $0, (%%rsp)" : : : "memory");
822}
823#endif
Reid Kleckner00d33a52013-10-17 01:29:48 +0000824/*----------------------------------------------------------------------------*\
Hans Wennborga4421e02014-03-12 21:09:05 +0000825|* readfs, readgs
826|* (Pointers in address space #256 and #257 are relative to the GS and FS
827|* segment registers, respectively.)
Hans Wennborg74ca0c42014-01-24 00:52:39 +0000828\*----------------------------------------------------------------------------*/
Hans Wennborg74ca0c42014-01-24 00:52:39 +0000829#define __ptr_to_addr_space(__addr_space_nbr, __type, __offset) \
830 ((volatile __type __attribute__((__address_space__(__addr_space_nbr)))*) \
831 (__offset))
832
Hans Wennborga4421e02014-03-12 21:09:05 +0000833#ifdef __i386__
Hans Wennborg74ca0c42014-01-24 00:52:39 +0000834static __inline__ unsigned char __attribute__((__always_inline__, __nodebug__))
835__readfsbyte(unsigned long __offset) {
836 return *__ptr_to_addr_space(257, unsigned char, __offset);
837}
838static __inline__ unsigned long __attribute__((__always_inline__, __nodebug__))
839__readfsdword(unsigned long __offset) {
840 return *__ptr_to_addr_space(257, unsigned long, __offset);
841}
842static __inline__ unsigned __int64 __attribute__((__always_inline__, __nodebug__))
843__readfsqword(unsigned long __offset) {
844 return *__ptr_to_addr_space(257, unsigned __int64, __offset);
845}
846static __inline__ unsigned short __attribute__((__always_inline__, __nodebug__))
847__readfsword(unsigned long __offset) {
848 return *__ptr_to_addr_space(257, unsigned short, __offset);
849}
Hans Wennborg74ca0c42014-01-24 00:52:39 +0000850#endif
Hans Wennborga4421e02014-03-12 21:09:05 +0000851#ifdef __x86_64__
852static __inline__ unsigned char __attribute__((__always_inline__, __nodebug__))
853__readgsbyte(unsigned long __offset) {
854 return *__ptr_to_addr_space(256, unsigned char, __offset);
855}
856static __inline__ unsigned long __attribute__((__always_inline__, __nodebug__))
857__readgsdword(unsigned long __offset) {
858 return *__ptr_to_addr_space(256, unsigned long, __offset);
859}
860static __inline__ unsigned __int64 __attribute__((__always_inline__, __nodebug__))
861__readgsqword(unsigned long __offset) {
862 return *__ptr_to_addr_space(256, unsigned __int64, __offset);
863}
864static __inline__ unsigned short __attribute__((__always_inline__, __nodebug__))
865__readgsword(unsigned long __offset) {
866 return *__ptr_to_addr_space(256, unsigned short, __offset);
867}
868#endif
869#undef __ptr_to_addr_space
Hans Wennborg74ca0c42014-01-24 00:52:39 +0000870/*----------------------------------------------------------------------------*\
Warren Huntd6ffae92013-09-27 23:57:26 +0000871|* Misc
872\*----------------------------------------------------------------------------*/
873static __inline__ void * __attribute__((__always_inline__, __nodebug__))
874_AddressOfReturnAddress(void) {
875 return (void*)((char*)__builtin_frame_address(0) + sizeof(void*));
876}
877static __inline__ void * __attribute__((__always_inline__, __nodebug__))
878_ReturnAddress(void) {
879 return __builtin_return_address(0);
880}
Hans Wennborg854f7d32014-01-16 23:39:35 +0000881static __inline__ void __attribute__((__always_inline__, __nodebug__))
882__cpuid(int __info[4], int __level) {
Hans Wennborg854f7d32014-01-16 23:39:35 +0000883 __asm__ ("cpuid" : "=a"(__info[0]), "=b" (__info[1]), "=c"(__info[2]), "=d"(__info[3])
Hans Wennborg12fb89e2014-01-31 19:44:55 +0000884 : "a"(__level));
885}
886static __inline__ void __attribute__((__always_inline__, __nodebug__))
887__cpuidex(int __info[4], int __level, int __ecx) {
888 __asm__ ("cpuid" : "=a"(__info[0]), "=b" (__info[1]), "=c"(__info[2]), "=d"(__info[3])
889 : "a"(__level), "c"(__ecx));
Hans Wennborg854f7d32014-01-16 23:39:35 +0000890}
891static __inline__ unsigned __int64 __cdecl __attribute__((__always_inline__, __nodebug__))
892_xgetbv(unsigned int __xcr_no) {
893 unsigned int __eax, __edx;
894 __asm__ ("xgetbv" : "=a" (__eax), "=d" (__edx) : "c" (__xcr_no));
895 return ((unsigned __int64)__edx << 32) | __eax;
896}
Hans Wennborg740a4d62014-01-28 22:55:01 +0000897static __inline__ unsigned __int64 __attribute__((__always_inline__, __nodebug__))
898__rdtsc(void) {
899 unsigned int __eax, __edx;
900 __asm__ ("rdtsc" : "=a" (__eax), "=d" (__edx));
901 return ((unsigned __int64)__edx << 32) | __eax;
902}
903static __inline__ void __attribute__((__always_inline__, __nodebug__))
904__halt(void) {
905 __asm__ volatile ("hlt");
906}
Warren Huntd6ffae92013-09-27 23:57:26 +0000907
Eric Christophercc872532013-09-18 22:40:18 +0000908#ifdef __cplusplus
Eric Christopher0db88a72013-09-18 22:24:01 +0000909}
Eric Christopherd1428bf2013-08-31 00:22:48 +0000910#endif
Reid Klecknerf0e23222013-09-19 00:19:53 +0000911
912#endif /* __INTRIN_H */
913#endif /* _MSC_VER */