blob: 6fd1eeb37398a2f0af28ce7546d5590e38459176 [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);
56void __cpuidex(int[4], int, int);
Eric Christopher0db88a72013-09-18 22:24:01 +000057void __debugbreak(void);
Eric Christopherd1428bf2013-08-31 00:22:48 +000058__int64 __emul(int, int);
59unsigned __int64 __emulu(unsigned int, unsigned int);
Eric Christopher0db88a72013-09-18 22:24:01 +000060void __cdecl __fastfail(unsigned int);
Eric Christopherd1428bf2013-08-31 00:22:48 +000061unsigned int __getcallerseflags(void);
Hans Wennborg740a4d62014-01-28 22:55:01 +000062static __inline__
Eric Christopherd1428bf2013-08-31 00:22:48 +000063void __halt(void);
64unsigned char __inbyte(unsigned short);
65void __inbytestring(unsigned short, unsigned char *, unsigned long);
66void __incfsbyte(unsigned long);
67void __incfsdword(unsigned long);
68void __incfsword(unsigned long);
69unsigned long __indword(unsigned short);
70void __indwordstring(unsigned short, unsigned long *, unsigned long);
71void __int2c(void);
72void __invlpg(void *);
73unsigned short __inword(unsigned short);
74void __inwordstring(unsigned short, unsigned short *, unsigned long);
75void __lidt(void *);
76unsigned __int64 __ll_lshift(unsigned __int64, int);
77__int64 __ll_rshift(__int64, int);
78void __llwpcb(void *);
79unsigned char __lwpins32(unsigned int, unsigned int, unsigned int);
80void __lwpval32(unsigned int, unsigned int, unsigned int);
81unsigned int __lzcnt(unsigned int);
82unsigned short __lzcnt16(unsigned short);
83void __movsb(unsigned char *, unsigned char const *, size_t);
84void __movsd(unsigned long *, unsigned long const *, size_t);
85void __movsw(unsigned short *, unsigned short const *, size_t);
86void __nop(void);
87void __nvreg_restore_fence(void);
88void __nvreg_save_fence(void);
89void __outbyte(unsigned short, unsigned char);
90void __outbytestring(unsigned short, unsigned char *, unsigned long);
91void __outdword(unsigned short, unsigned long);
92void __outdwordstring(unsigned short, unsigned long *, unsigned long);
93void __outword(unsigned short, unsigned short);
94void __outwordstring(unsigned short, unsigned short *, unsigned long);
Warren Huntd6ffae92013-09-27 23:57:26 +000095static __inline__
Eric Christopherd1428bf2013-08-31 00:22:48 +000096unsigned int __popcnt(unsigned int);
Warren Huntd6ffae92013-09-27 23:57:26 +000097static __inline__
Eric Christopherd1428bf2013-08-31 00:22:48 +000098unsigned short __popcnt16(unsigned short);
Hans Wennborg740a4d62014-01-28 22:55:01 +000099static __inline__
Eric Christopherd1428bf2013-08-31 00:22:48 +0000100unsigned __int64 __rdtsc(void);
101unsigned __int64 __rdtscp(unsigned int *);
102unsigned long __readcr0(void);
103unsigned long __readcr2(void);
104unsigned long __readcr3(void);
Eric Christopher439137e2014-01-24 12:13:47 +0000105unsigned long __readcr4(void);
Eric Christopherd1428bf2013-08-31 00:22:48 +0000106unsigned long __readcr8(void);
107unsigned int __readdr(unsigned int);
108unsigned int __readeflags(void);
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);
144void __writeeflags(unsigned int);
145void __writefsbyte(unsigned long, unsigned char);
146void __writefsdword(unsigned long, unsigned long);
147void __writefsqword(unsigned long, unsigned __int64);
148void __writefsword(unsigned long, unsigned short);
149void __writemsr(unsigned long, unsigned __int64);
Warren Huntd6ffae92013-09-27 23:57:26 +0000150static __inline__
Eric Christopherd1428bf2013-08-31 00:22:48 +0000151void *_AddressOfReturnAddress(void);
152unsigned int _andn_u32(unsigned int, unsigned int);
153unsigned int _bextr_u32(unsigned int, unsigned int, unsigned int);
Eric Christopherd1428bf2013-08-31 00:22:48 +0000154unsigned int _bextri_u32(unsigned int, unsigned int);
Warren Huntd6ffae92013-09-27 23:57:26 +0000155static __inline__
Eric Christopherd1428bf2013-08-31 00:22:48 +0000156unsigned char _BitScanForward(unsigned long *_Index, unsigned long _Mask);
Warren Huntd6ffae92013-09-27 23:57:26 +0000157static __inline__
Eric Christopherd1428bf2013-08-31 00:22:48 +0000158unsigned char _BitScanReverse(unsigned long *_Index, unsigned long _Mask);
Warren Huntd6ffae92013-09-27 23:57:26 +0000159static __inline__
Eric Christopherd1428bf2013-08-31 00:22:48 +0000160unsigned char _bittest(long const *, long);
Warren Huntd6ffae92013-09-27 23:57:26 +0000161static __inline__
Eric Christopherd1428bf2013-08-31 00:22:48 +0000162unsigned char _bittestandcomplement(long *, long);
Warren Huntd6ffae92013-09-27 23:57:26 +0000163static __inline__
Eric Christopherd1428bf2013-08-31 00:22:48 +0000164unsigned char _bittestandreset(long *, long);
Warren Huntd6ffae92013-09-27 23:57:26 +0000165static __inline__
Eric Christopherd1428bf2013-08-31 00:22:48 +0000166unsigned char _bittestandset(long *, long);
167unsigned int _blcfill_u32(unsigned int);
168unsigned int _blci_u32(unsigned int);
169unsigned int _blcic_u32(unsigned int);
170unsigned int _blcmsk_u32(unsigned int);
171unsigned int _blcs_u32(unsigned int);
172unsigned int _blsfill_u32(unsigned int);
173unsigned int _blsi_u32(unsigned int);
174unsigned int _blsic_u32(unsigned int);
175unsigned int _blsmsk_u32(unsigned int);
Eric Christopherd1428bf2013-08-31 00:22:48 +0000176unsigned int _blsr_u32(unsigned int);
177unsigned __int64 __cdecl _byteswap_uint64(unsigned __int64);
178unsigned long __cdecl _byteswap_ulong(unsigned long);
179unsigned short __cdecl _byteswap_ushort(unsigned short);
180unsigned _bzhi_u32(unsigned int, unsigned int);
181void __cdecl _disable(void);
182void __cdecl _enable(void);
183void __cdecl _fxrstor(void const *);
184void __cdecl _fxsave(void *);
185long _InterlockedAddLargeStatistic(__int64 volatile *_Addend, long _Value);
Warren Huntd6ffae92013-09-27 23:57:26 +0000186static __inline__
Eric Christopherd1428bf2013-08-31 00:22:48 +0000187long _InterlockedAnd(long volatile *_Value, long _Mask);
Warren Huntd6ffae92013-09-27 23:57:26 +0000188static __inline__
Eric Christopherd1428bf2013-08-31 00:22:48 +0000189short _InterlockedAnd16(short volatile *_Value, short _Mask);
Warren Huntd6ffae92013-09-27 23:57:26 +0000190static __inline__
Eric Christopherd1428bf2013-08-31 00:22:48 +0000191char _InterlockedAnd8(char volatile *_Value, char _Mask);
192unsigned char _interlockedbittestandreset(long volatile *, long);
193unsigned char _interlockedbittestandset(long volatile *, long);
Warren Huntd6ffae92013-09-27 23:57:26 +0000194static __inline__
Eric Christopherd1428bf2013-08-31 00:22:48 +0000195long __cdecl _InterlockedCompareExchange(long volatile *_Destination,
196 long _Exchange, long _Comparand);
197long _InterlockedCompareExchange_HLEAcquire(long volatile *, long, long);
198long _InterlockedCompareExchange_HLERelease(long volatile *, long, long);
Warren Huntd6ffae92013-09-27 23:57:26 +0000199static __inline__
Eric Christopherd1428bf2013-08-31 00:22:48 +0000200short _InterlockedCompareExchange16(short volatile *_Destination,
201 short _Exchange, short _Comparand);
Warren Huntd6ffae92013-09-27 23:57:26 +0000202static __inline__
Eric Christopherd1428bf2013-08-31 00:22:48 +0000203__int64 _InterlockedCompareExchange64(__int64 volatile *_Destination,
204 __int64 _Exchange, __int64 _Comparand);
205__int64 _InterlockedcompareExchange64_HLEAcquire(__int64 volatile *, __int64,
206 __int64);
207__int64 _InterlockedCompareExchange64_HLERelease(__int64 volatile *, __int64,
208 __int64);
Warren Huntd6ffae92013-09-27 23:57:26 +0000209static __inline__
Eric Christopherd1428bf2013-08-31 00:22:48 +0000210char _InterlockedCompareExchange8(char volatile *_Destination, char _Exchange,
211 char _Comparand);
212void *_InterlockedCompareExchangePointer_HLEAcquire(void *volatile *, void *,
213 void *);
214void *_InterlockedCompareExchangePointer_HLERelease(void *volatile *, void *,
215 void *);
Warren Huntd6ffae92013-09-27 23:57:26 +0000216static __inline__
Eric Christopherd1428bf2013-08-31 00:22:48 +0000217long __cdecl _InterlockedDecrement(long volatile *_Addend);
Warren Huntd6ffae92013-09-27 23:57:26 +0000218static __inline__
Eric Christopherd1428bf2013-08-31 00:22:48 +0000219short _InterlockedDecrement16(short volatile *_Addend);
Warren Huntd6ffae92013-09-27 23:57:26 +0000220static __inline__
Eric Christopherd1428bf2013-08-31 00:22:48 +0000221long __cdecl _InterlockedExchange(long volatile *_Target, long _Value);
Warren Huntd6ffae92013-09-27 23:57:26 +0000222static __inline__
Eric Christopherd1428bf2013-08-31 00:22:48 +0000223short _InterlockedExchange16(short volatile *_Target, short _Value);
Warren Huntd6ffae92013-09-27 23:57:26 +0000224static __inline__
Eric Christopherd1428bf2013-08-31 00:22:48 +0000225char _InterlockedExchange8(char volatile *_Target, char _Value);
Warren Huntd6ffae92013-09-27 23:57:26 +0000226static __inline__
Eric Christopherd1428bf2013-08-31 00:22:48 +0000227long __cdecl _InterlockedExchangeAdd(long volatile *_Addend, long _Value);
228long _InterlockedExchangeAdd_HLEAcquire(long volatile *, long);
229long _InterlockedExchangeAdd_HLERelease(long volatile *, long);
Reid Kleckner924eb2a2014-01-27 18:48:02 +0000230static __inline__
Eric Christopher439137e2014-01-24 12:13:47 +0000231short _InterlockedExchangeAdd16(short volatile *_Addend, short _Value);
232__int64 _InterlockedExchangeAdd64_HLEAcquire(__int64 volatile *, __int64);
233__int64 _InterlockedExchangeAdd64_HLERelease(__int64 volatile *, __int64);
Warren Huntd6ffae92013-09-27 23:57:26 +0000234static __inline__
Eric Christopherd1428bf2013-08-31 00:22:48 +0000235char _InterlockedExchangeAdd8(char volatile *_Addend, char _Value);
Warren Huntd6ffae92013-09-27 23:57:26 +0000236static __inline__
Eric Christopherd1428bf2013-08-31 00:22:48 +0000237long __cdecl _InterlockedIncrement(long volatile *_Addend);
Warren Huntd6ffae92013-09-27 23:57:26 +0000238static __inline__
Eric Christopherd1428bf2013-08-31 00:22:48 +0000239short _InterlockedIncrement16(short volatile *_Addend);
Warren Huntd6ffae92013-09-27 23:57:26 +0000240static __inline__
Eric Christopherd1428bf2013-08-31 00:22:48 +0000241long _InterlockedOr(long volatile *_Value, long _Mask);
Warren Huntd6ffae92013-09-27 23:57:26 +0000242static __inline__
Eric Christopherd1428bf2013-08-31 00:22:48 +0000243short _InterlockedOr16(short volatile *_Value, short _Mask);
Warren Huntd6ffae92013-09-27 23:57:26 +0000244static __inline__
Eric Christopherd1428bf2013-08-31 00:22:48 +0000245char _InterlockedOr8(char volatile *_Value, char _Mask);
Warren Huntd6ffae92013-09-27 23:57:26 +0000246static __inline__
Eric Christopherd1428bf2013-08-31 00:22:48 +0000247long _InterlockedXor(long volatile *_Value, long _Mask);
Warren Huntd6ffae92013-09-27 23:57:26 +0000248static __inline__
Eric Christopherd1428bf2013-08-31 00:22:48 +0000249short _InterlockedXor16(short volatile *_Value, short _Mask);
Warren Huntd6ffae92013-09-27 23:57:26 +0000250static __inline__
Eric Christopherd1428bf2013-08-31 00:22:48 +0000251char _InterlockedXor8(char volatile *_Value, char _Mask);
252void __cdecl _invpcid(unsigned int, void *);
Warren Huntd6ffae92013-09-27 23:57:26 +0000253static __inline__
Eric Christopherd1428bf2013-08-31 00:22:48 +0000254unsigned long __cdecl _lrotl(unsigned long, int);
Warren Huntd6ffae92013-09-27 23:57:26 +0000255static __inline__
Eric Christopherd1428bf2013-08-31 00:22:48 +0000256unsigned long __cdecl _lrotr(unsigned long, int);
Warren Huntd6ffae92013-09-27 23:57:26 +0000257static __inline__
Eric Christopherd1428bf2013-08-31 00:22:48 +0000258unsigned int _lzcnt_u32(unsigned int);
Reid Kleckner00d33a52013-10-17 01:29:48 +0000259static __inline__
Eric Christopherd1428bf2013-08-31 00:22:48 +0000260void _ReadBarrier(void);
Reid Kleckner00d33a52013-10-17 01:29:48 +0000261static __inline__
Eric Christopherd1428bf2013-08-31 00:22:48 +0000262void _ReadWriteBarrier(void);
Warren Huntd6ffae92013-09-27 23:57:26 +0000263static __inline__
Eric Christopherd1428bf2013-08-31 00:22:48 +0000264void *_ReturnAddress(void);
265unsigned int _rorx_u32(unsigned int, const unsigned int);
266int __cdecl _rdrand16_step(unsigned short *);
267int __cdecl _rdrand32_step(unsigned int *);
Warren Huntd6ffae92013-09-27 23:57:26 +0000268static __inline__
Eric Christopherfb4b4332013-08-31 00:27:38 +0000269unsigned int __cdecl _rotl(unsigned int _Value, int _Shift);
Warren Huntd6ffae92013-09-27 23:57:26 +0000270static __inline__
Eric Christopherd1428bf2013-08-31 00:22:48 +0000271unsigned short _rotl16(unsigned short _Value, unsigned char _Shift);
Warren Huntd6ffae92013-09-27 23:57:26 +0000272static __inline__
Eric Christopherd1428bf2013-08-31 00:22:48 +0000273unsigned __int64 __cdecl _rotl64(unsigned __int64 _Value, int _Shift);
Warren Huntd6ffae92013-09-27 23:57:26 +0000274static __inline__
Eric Christopherd1428bf2013-08-31 00:22:48 +0000275unsigned char _rotl8(unsigned char _Value, unsigned char _Shift);
Warren Huntd6ffae92013-09-27 23:57:26 +0000276static __inline__
Eric Christopherd1428bf2013-08-31 00:22:48 +0000277unsigned int __cdecl _rotr(unsigned int _Value, int _Shift);
Warren Huntd6ffae92013-09-27 23:57:26 +0000278static __inline__
Eric Christopherd1428bf2013-08-31 00:22:48 +0000279unsigned short _rotr16(unsigned short _Value, unsigned char _Shift);
Warren Huntd6ffae92013-09-27 23:57:26 +0000280static __inline__
Eric Christopherd1428bf2013-08-31 00:22:48 +0000281unsigned __int64 __cdecl _rotr64(unsigned __int64 _Value, int _Shift);
Warren Huntd6ffae92013-09-27 23:57:26 +0000282static __inline__
Eric Christopherd1428bf2013-08-31 00:22:48 +0000283unsigned char _rotr8(unsigned char _Value, unsigned char _Shift);
284int _sarx_i32(int, unsigned int);
Hans Wennborg1fd6dd32014-01-28 23:01:59 +0000285int __cdecl _setjmp(jmp_buf);
Eric Christopherd1428bf2013-08-31 00:22:48 +0000286unsigned int _shlx_u32(unsigned int, unsigned int);
287unsigned int _shrx_u32(unsigned int, unsigned int);
288void _Store_HLERelease(long volatile *, long);
289void _Store64_HLERelease(__int64 volatile *, __int64);
290void _StorePointer_HLERelease(void *volatile *, void *);
291unsigned int _t1mskc_u32(unsigned int);
292unsigned int _tzcnt_u32(unsigned int);
Eric Christopherd1428bf2013-08-31 00:22:48 +0000293unsigned int _tzmsk_u32(unsigned int);
Reid Kleckner00d33a52013-10-17 01:29:48 +0000294static __inline__
Eric Christopherd1428bf2013-08-31 00:22:48 +0000295void _WriteBarrier(void);
296void _xabort(const unsigned int imm);
297unsigned __int32 xbegin(void);
298void _xend(void);
Hans Wennborg854f7d32014-01-16 23:39:35 +0000299static __inline__
Eric Christopherd1428bf2013-08-31 00:22:48 +0000300unsigned __int64 __cdecl _xgetbv(unsigned int);
301void __cdecl _xrstor(void const *, unsigned __int64);
302void __cdecl _xsave(void *, unsigned __int64);
303void __cdecl _xsaveopt(void *, unsigned __int64);
304void __cdecl _xsetbv(unsigned int, unsigned __int64);
305unsigned char _xtest(void);
306
307/* These additional intrinsics are turned on in x64/amd64/x86_64 mode. */
Warren Hunt3f987942013-09-30 21:08:05 +0000308#ifdef __x86_64__
Eric Christopherd1428bf2013-08-31 00:22:48 +0000309void __addgsbyte(unsigned long, unsigned char);
310void __addgsdword(unsigned long, unsigned long);
311void __addgsqword(unsigned long, unsigned __int64);
312void __addgsword(unsigned long, unsigned short);
Reid Klecknerf08d6582014-01-27 19:16:35 +0000313static __inline__
Eric Christopherd1428bf2013-08-31 00:22:48 +0000314void __faststorefence(void);
315void __incgsbyte(unsigned long);
316void __incgsdword(unsigned long);
317void __incgsqword(unsigned long);
318void __incgsword(unsigned long);
Eric Christopher439137e2014-01-24 12:13:47 +0000319unsigned char __lwpins64(unsigned __int64, unsigned int, unsigned int);
320void __lwpval64(unsigned __int64, unsigned int, unsigned int);
321unsigned __int64 __lzcnt64(unsigned __int64);
322void __movsq(unsigned long long *, unsigned long long const *, size_t);
323__int64 __mulh(__int64, __int64);
Reid Kleckner924eb2a2014-01-27 18:48:02 +0000324static __inline__
Eric Christopherd1428bf2013-08-31 00:22:48 +0000325unsigned __int64 __popcnt64(unsigned __int64);
Eric Christopher439137e2014-01-24 12:13:47 +0000326unsigned char __readgsbyte(unsigned long);
327unsigned long __readgsdword(unsigned long);
328unsigned short __readgsword(unsigned long);
Eric Christopherd1428bf2013-08-31 00:22:48 +0000329unsigned __int64 __shiftleft128(unsigned __int64 _LowPart,
330 unsigned __int64 _HighPart,
331 unsigned char _Shift);
332unsigned __int64 __shiftright128(unsigned __int64 _LowPart,
333 unsigned __int64 _HighPart,
334 unsigned char _Shift);
335void __stosq(unsigned __int64 *, unsigned __int64, size_t);
Eric Christopher439137e2014-01-24 12:13:47 +0000336unsigned __int64 __umulh(unsigned __int64, unsigned __int64);
337unsigned char __vmx_on(unsigned __int64 *);
338unsigned char __vmx_vmclear(unsigned __int64 *);
339unsigned char __vmx_vmlaunch(void);
340unsigned char __vmx_vmptrld(unsigned __int64 *);
341unsigned char __vmx_vmread(size_t, size_t *);
342unsigned char __vmx_vmresume(void);
343unsigned char __vmx_vmwrite(size_t, size_t);
344void __writegsbyte(unsigned long, unsigned char);
345void __writegsdword(unsigned long, unsigned long);
346void __writegsqword(unsigned long, unsigned __int64);
347void __writegsword(unsigned long, unsigned short);
Eric Christopherd1428bf2013-08-31 00:22:48 +0000348unsigned __int64 _andn_u64(unsigned __int64, unsigned __int64);
349unsigned __int64 _bextr_u64(unsigned __int64, unsigned int, unsigned int);
350unsigned __int64 _bextri_u64(unsigned __int64, unsigned int);
Warren Huntd6ffae92013-09-27 23:57:26 +0000351static __inline__
Eric Christopherd1428bf2013-08-31 00:22:48 +0000352unsigned char _BitScanForward64(unsigned long *_Index, unsigned __int64 _Mask);
Warren Huntd6ffae92013-09-27 23:57:26 +0000353static __inline__
Eric Christopherd1428bf2013-08-31 00:22:48 +0000354unsigned char _BitScanReverse64(unsigned long *_Index, unsigned __int64 _Mask);
Warren Huntd6ffae92013-09-27 23:57:26 +0000355static __inline__
Eric Christopherd1428bf2013-08-31 00:22:48 +0000356unsigned char _bittest64(__int64 const *, __int64);
Warren Huntd6ffae92013-09-27 23:57:26 +0000357static __inline__
Eric Christopherd1428bf2013-08-31 00:22:48 +0000358unsigned char _bittestandcomplement64(__int64 *, __int64);
Warren Huntd6ffae92013-09-27 23:57:26 +0000359static __inline__
Eric Christopherd1428bf2013-08-31 00:22:48 +0000360unsigned char _bittestandreset64(__int64 *, __int64);
Warren Huntd6ffae92013-09-27 23:57:26 +0000361static __inline__
Eric Christopherd1428bf2013-08-31 00:22:48 +0000362unsigned char _bittestandset64(__int64 *, __int64);
363unsigned __int64 _blcfill_u64(unsigned __int64);
364unsigned __int64 _blci_u64(unsigned __int64);
365unsigned __int64 _blcic_u64(unsigned __int64);
366unsigned __int64 _blcmsk_u64(unsigned __int64);
367unsigned __int64 _blcs_u64(unsigned __int64);
368unsigned __int64 _blsfill_u64(unsigned __int64);
369unsigned __int64 _blsi_u64(unsigned __int64);
370unsigned __int64 _blsic_u64(unsigned __int64);
Eric Christopher439137e2014-01-24 12:13:47 +0000371unsigned __int64 _blsmsk_u64(unsigned __int64);
Eric Christopherd1428bf2013-08-31 00:22:48 +0000372unsigned __int64 _blsr_u64(unsigned __int64);
373unsigned __int64 __cdecl _byteswap_uint64(unsigned __int64);
374unsigned __int64 _bzhi_u64(unsigned __int64, unsigned int);
375void __cdecl _fxrstor64(void const *);
376void __cdecl _fxsave64(void *);
377long _InterlockedAnd_np(long volatile *_Value, long _Mask);
378short _InterlockedAnd16_np(short volatile *_Value, short _Mask);
379__int64 _InterlockedAnd64_np(__int64 volatile *_Value, __int64 _Mask);
380char _InterlockedAnd8_np(char volatile *_Value, char _Mask);
381unsigned char _interlockedbittestandreset64(__int64 volatile *, __int64);
382unsigned char _interlockedbittestandset64(__int64 volatile *, __int64);
383long _InterlockedCompareExchange_np(long volatile *_Destination, long _Exchange,
384 long _Comparand);
385unsigned char _InterlockedCompareExchange128(__int64 volatile *_Destination,
386 __int64 _ExchangeHigh,
387 __int64 _ExchangeLow,
388 __int64 *_CompareandResult);
389unsigned char _InterlockedCompareExchange128_np(__int64 volatile *_Destination,
390 __int64 _ExchangeHigh,
391 __int64 _ExchangeLow,
392 __int64 *_ComparandResult);
393short _InterlockedCompareExchange16_np(short volatile *_Destination,
394 short _Exchange, short _Comparand);
Eric Christopher439137e2014-01-24 12:13:47 +0000395__int64 _InterlockedCompareExchange64_HLEAcquire(__int64 volatile *, __int64,
396 __int64);
397__int64 _InterlockedCompareExchange64_HLERelease(__int64 volatile *, __int64,
398 __int64);
Eric Christopherd1428bf2013-08-31 00:22:48 +0000399__int64 _InterlockedCompareExchange64_np(__int64 volatile *_Destination,
400 __int64 _Exchange, __int64 _Comparand);
Reid Klecknerf08d6582014-01-27 19:16:35 +0000401static __inline__
Eric Christopher58b40432014-01-25 01:38:30 +0000402void *_InterlockedCompareExchangePointer(void *volatile *_Destination,
403 void *_Exchange, void *_Comparand);
Eric Christopherd1428bf2013-08-31 00:22:48 +0000404void *_InterlockedCompareExchangePointer_np(void *volatile *_Destination,
405 void *_Exchange, void *_Comparand);
Reid Kleckner924eb2a2014-01-27 18:48:02 +0000406static __inline__
Eric Christopher439137e2014-01-24 12:13:47 +0000407__int64 _InterlockedDecrement64(__int64 volatile *_Addend);
Reid Kleckner924eb2a2014-01-27 18:48:02 +0000408static __inline__
Eric Christopher439137e2014-01-24 12:13:47 +0000409__int64 _InterlockedExchange64(__int64 volatile *_Target, __int64 _Value);
Reid Kleckner924eb2a2014-01-27 18:48:02 +0000410static __inline__
Eric Christopher439137e2014-01-24 12:13:47 +0000411__int64 _InterlockedExchangeAdd64(__int64 volatile *_Addend, __int64 _Value);
412void *_InterlockedExchangePointer(void *volatile *_Target, void *_Value);
Reid Kleckner924eb2a2014-01-27 18:48:02 +0000413static __inline__
Eric Christopher439137e2014-01-24 12:13:47 +0000414__int64 _InterlockedIncrement64(__int64 volatile *_Addend);
Eric Christopherd1428bf2013-08-31 00:22:48 +0000415long _InterlockedOr_np(long volatile *_Value, long _Mask);
416short _InterlockedOr16_np(short volatile *_Value, short _Mask);
Reid Kleckner924eb2a2014-01-27 18:48:02 +0000417static __inline__
Eric Christopher439137e2014-01-24 12:13:47 +0000418__int64 _InterlockedOr64(__int64 volatile *_Value, __int64 _Mask);
Warren Hunt41a993f2013-09-28 00:15:41 +0000419__int64 _InterlockedOr64_np(__int64 volatile *_Value, __int64 _Mask);
Eric Christopherd1428bf2013-08-31 00:22:48 +0000420char _InterlockedOr8_np(char volatile *_Value, char _Mask);
421long _InterlockedXor_np(long volatile *_Value, long _Mask);
422short _InterlockedXor16_np(short volatile *_Value, short _Mask);
Reid Kleckner924eb2a2014-01-27 18:48:02 +0000423static __inline__
Eric Christopher439137e2014-01-24 12:13:47 +0000424__int64 _InterlockedXor64(__int64 volatile *_Value, __int64 _Mask);
Eric Christopherd1428bf2013-08-31 00:22:48 +0000425__int64 _InterlockedXor64_np(__int64 volatile *_Value, __int64 _Mask);
426char _InterlockedXor8_np(char volatile *_Value, char _Mask);
Reid Kleckner924eb2a2014-01-27 18:48:02 +0000427static __inline__
Eric Christopherd1428bf2013-08-31 00:22:48 +0000428unsigned __int64 _lzcnt_u64(unsigned __int64);
429__int64 _mul128(__int64 _Multiplier, __int64 _Multiplicand,
430 __int64 *_HighProduct);
431unsigned int __cdecl _readfsbase_u32(void);
432unsigned __int64 __cdecl _readfsbase_u64(void);
433unsigned int __cdecl _readgsbase_u32(void);
434unsigned __int64 __cdecl _readgsbase_u64(void);
435unsigned __int64 _rorx_u64(unsigned __int64, const unsigned int);
Eric Christopher439137e2014-01-24 12:13:47 +0000436__int64 _sarx_i64(__int64, unsigned int);
Reid Kleckner33630902014-01-27 19:32:42 +0000437/* FIXME: Need definition for jmp_buf.
438 int __cdecl _setjmpex(jmp_buf); */
Eric Christopher439137e2014-01-24 12:13:47 +0000439unsigned __int64 _shlx_u64(unsigned __int64, unsigned int);
440unsigned __int64 shrx_u64(unsigned __int64, unsigned int);
Eric Christopherd1428bf2013-08-31 00:22:48 +0000441unsigned __int64 _tzcnt_u64(unsigned __int64);
442unsigned __int64 _tzmsk_u64(unsigned __int64);
443unsigned __int64 _umul128(unsigned __int64 _Multiplier,
444 unsigned __int64 _Multiplicand,
445 unsigned __int64 *_HighProduct);
446void __cdecl _writefsbase_u32(unsigned int);
447void _cdecl _writefsbase_u64(unsigned __int64);
448void __cdecl _writegsbase_u32(unsigned int);
449void __cdecl _writegsbase_u64(unsigned __int64);
450void __cdecl _xrstor64(void const *, unsigned __int64);
451void __cdecl _xsave64(void *, unsigned __int64);
452void __cdecl _xsaveopt64(void *, unsigned __int64);
Eric Christopher0db88a72013-09-18 22:24:01 +0000453
Warren Hunt3f987942013-09-30 21:08:05 +0000454#endif /* __x86_64__ */
Reid Klecknerf0e23222013-09-19 00:19:53 +0000455
Warren Huntd6ffae92013-09-27 23:57:26 +0000456/*----------------------------------------------------------------------------*\
457|* Bit Twiddling
458\*----------------------------------------------------------------------------*/
459static __inline__ unsigned char __attribute__((__always_inline__, __nodebug__))
460_rotl8(unsigned char _Value, unsigned char _Shift) {
461 _Shift &= 0x7;
462 return _Shift ? (_Value << _Shift) | (_Value >> (8 - _Shift)) : _Value;
463}
464static __inline__ unsigned char __attribute__((__always_inline__, __nodebug__))
465_rotr8(unsigned char _Value, unsigned char _Shift) {
466 _Shift &= 0x7;
467 return _Shift ? (_Value >> _Shift) | (_Value << (8 - _Shift)) : _Value;
468}
469static __inline__ unsigned short __attribute__((__always_inline__, __nodebug__))
470_rotl16(unsigned short _Value, unsigned char _Shift) {
471 _Shift &= 0xf;
472 return _Shift ? (_Value << _Shift) | (_Value >> (16 - _Shift)) : _Value;
473}
474static __inline__ unsigned short __attribute__((__always_inline__, __nodebug__))
475_rotr16(unsigned short _Value, unsigned char _Shift) {
476 _Shift &= 0xf;
477 return _Shift ? (_Value >> _Shift) | (_Value << (16 - _Shift)) : _Value;
478}
479static __inline__ unsigned int __attribute__((__always_inline__, __nodebug__))
480_rotl(unsigned int _Value, int _Shift) {
481 _Shift &= 0x1f;
482 return _Shift ? (_Value << _Shift) | (_Value >> (32 - _Shift)) : _Value;
483}
484static __inline__ unsigned int __attribute__((__always_inline__, __nodebug__))
485_rotr(unsigned int _Value, int _Shift) {
486 _Shift &= 0x1f;
487 return _Shift ? (_Value >> _Shift) | (_Value << (32 - _Shift)) : _Value;
488}
489static __inline__ unsigned long __attribute__((__always_inline__, __nodebug__))
490_lrotl(unsigned long _Value, int _Shift) {
491 _Shift &= 0x1f;
492 return _Shift ? (_Value << _Shift) | (_Value >> (32 - _Shift)) : _Value;
493}
494static __inline__ unsigned long __attribute__((__always_inline__, __nodebug__))
495_lrotr(unsigned long _Value, int _Shift) {
496 _Shift &= 0x1f;
497 return _Shift ? (_Value >> _Shift) | (_Value << (32 - _Shift)) : _Value;
498}
499static
500__inline__ unsigned __int64 __attribute__((__always_inline__, __nodebug__))
501_rotl64(unsigned __int64 _Value, int _Shift) {
502 _Shift &= 0x3f;
503 return _Shift ? (_Value << _Shift) | (_Value >> (64 - _Shift)) : _Value;
504}
505static
506__inline__ unsigned __int64 __attribute__((__always_inline__, __nodebug__))
507_rotr64(unsigned __int64 _Value, int _Shift) {
508 _Shift &= 0x3f;
509 return _Shift ? (_Value >> _Shift) | (_Value << (64 - _Shift)) : _Value;
510}
511/*----------------------------------------------------------------------------*\
512|* Bit Counting and Testing
513\*----------------------------------------------------------------------------*/
514static __inline__ unsigned char __attribute__((__always_inline__, __nodebug__))
515_BitScanForward(unsigned long *_Index, unsigned long _Mask) {
516 if (!_Mask)
517 return 0;
518 *_Index = __builtin_ctzl(_Mask);
519 return 1;
520}
521static __inline__ unsigned char __attribute__((__always_inline__, __nodebug__))
522_BitScanReverse(unsigned long *_Index, unsigned long _Mask) {
523 if (!_Mask)
524 return 0;
525 *_Index = 31 - __builtin_clzl(_Mask);
526 return 1;
527}
528static __inline__ unsigned int __attribute__((__always_inline__, __nodebug__))
529_lzcnt_u32(unsigned int a) {
530 if (!a)
531 return 32;
532 return __builtin_clzl(a);
533}
534static __inline__ unsigned short __attribute__((__always_inline__, __nodebug__))
535__popcnt16(unsigned short value) {
536 return __builtin_popcount((int)value);
537}
538static __inline__ unsigned int __attribute__((__always_inline__, __nodebug__))
539__popcnt(unsigned int value) {
540 return __builtin_popcount(value);
541}
542static __inline__ unsigned char __attribute__((__always_inline__, __nodebug__))
543_bittest(long const *a, long b) {
544 return (*a >> b) & 1;
545}
546static __inline__ unsigned char __attribute__((__always_inline__, __nodebug__))
547_bittestandcomplement(long *a, long b) {
548 unsigned char x = (*a >> b) & 1;
549 *a = *a ^ (1 << b);
550 return x;
551}
552static __inline__ unsigned char __attribute__((__always_inline__, __nodebug__))
553_bittestandreset(long *a, long b) {
554 unsigned char x = (*a >> b) & 1;
555 *a = *a & ~(1 << b);
556 return x;
557}
558static __inline__ unsigned char __attribute__((__always_inline__, __nodebug__))
559_bittestandset(long *a, long b) {
560 unsigned char x = (*a >> b) & 1;
Warren Hunt2731e3e2013-10-01 17:12:40 +0000561 *a = *a | (1 << b);
Warren Huntd6ffae92013-09-27 23:57:26 +0000562 return x;
563}
Warren Hunt3f987942013-09-30 21:08:05 +0000564#ifdef __x86_64__
Warren Huntd6ffae92013-09-27 23:57:26 +0000565static __inline__ unsigned char __attribute__((__always_inline__, __nodebug__))
566_BitScanForward64(unsigned long *_Index, unsigned __int64 _Mask) {
567 if (!_Mask)
568 return 0;
569 *_Index = __builtin_ctzll(_Mask);
570 return 1;
571}
572static __inline__ unsigned char __attribute__((__always_inline__, __nodebug__))
573_BitScanReverse64(unsigned long *_Index, unsigned __int64 _Mask) {
574 if (!_Mask)
575 return 0;
576 *_Index = 63 - __builtin_clzll(_Mask);
577 return 1;
578}
579static
580__inline__ unsigned __int64 __attribute__((__always_inline__, __nodebug__))
581_lzcnt_u64(unsigned __int64 a) {
582 if (!a)
583 return 64;
584 return __builtin_clzll(a);
585}
586static __inline__
587unsigned __int64 __attribute__((__always_inline__, __nodebug__))
588 __popcnt64(unsigned __int64 value) {
589 return __builtin_popcountll(value);
590}
591static __inline__ unsigned char __attribute__((__always_inline__, __nodebug__))
592_bittest64(__int64 const *a, __int64 b) {
593 return (*a >> b) & 1;
594}
595static __inline__ unsigned char __attribute__((__always_inline__, __nodebug__))
596_bittestandcomplement64(__int64 *a, __int64 b) {
597 unsigned char x = (*a >> b) & 1;
598 *a = *a ^ (1ll << b);
599 return x;
600}
601static __inline__ unsigned char __attribute__((__always_inline__, __nodebug__))
602_bittestandreset64(__int64 *a, __int64 b) {
603 unsigned char x = (*a >> b) & 1;
604 *a = *a & ~(1ll << b);
605 return x;
606}
607static __inline__ unsigned char __attribute__((__always_inline__, __nodebug__))
608_bittestandset64(__int64 *a, __int64 b) {
609 unsigned char x = (*a >> b) & 1;
Warren Hunt2731e3e2013-10-01 17:12:40 +0000610 *a = *a | (1ll << b);
Warren Huntd6ffae92013-09-27 23:57:26 +0000611 return x;
612}
613#endif
614/*----------------------------------------------------------------------------*\
615|* Interlocked Exchange Add
616\*----------------------------------------------------------------------------*/
617static __inline__ char __attribute__((__always_inline__, __nodebug__))
618_InterlockedExchangeAdd8(char volatile *_Addend, char _Value) {
619 return __atomic_add_fetch(_Addend, _Value, 0) - _Value;
620}
621static __inline__ short __attribute__((__always_inline__, __nodebug__))
622_InterlockedExchangeAdd16(short volatile *_Addend, short _Value) {
623 return __atomic_add_fetch(_Addend, _Value, 0) - _Value;
624}
625static __inline__ long __attribute__((__always_inline__, __nodebug__))
626_InterlockedExchangeAdd(long volatile *_Addend, long _Value) {
627 return __atomic_add_fetch(_Addend, _Value, 0) - _Value;
628}
Warren Hunt3f987942013-09-30 21:08:05 +0000629#ifdef __x86_64__
Warren Huntd6ffae92013-09-27 23:57:26 +0000630static __inline__ __int64 __attribute__((__always_inline__, __nodebug__))
631_InterlockedExchangeAdd64(__int64 volatile *_Addend, __int64 _Value) {
632 return __atomic_add_fetch(_Addend, _Value, 0) - _Value;
633}
634#endif
635/*----------------------------------------------------------------------------*\
636|* Interlocked Exchange Sub
637\*----------------------------------------------------------------------------*/
638static __inline__ char __attribute__((__always_inline__, __nodebug__))
639_InterlockedExchangeSub8(char volatile *_Subend, char _Value) {
640 return __atomic_sub_fetch(_Subend, _Value, 0) + _Value;
641}
642static __inline__ short __attribute__((__always_inline__, __nodebug__))
643_InterlockedExchangeSub16(short volatile *_Subend, short _Value) {
644 return __atomic_sub_fetch(_Subend, _Value, 0) + _Value;
645}
646static __inline__ long __attribute__((__always_inline__, __nodebug__))
647_InterlockedExchangeSub(long volatile *_Subend, long _Value) {
648 return __atomic_sub_fetch(_Subend, _Value, 0) + _Value;
649}
Warren Hunt3f987942013-09-30 21:08:05 +0000650#ifdef __x86_64__
Warren Huntd6ffae92013-09-27 23:57:26 +0000651static __inline__ __int64 __attribute__((__always_inline__, __nodebug__))
652_InterlockedExchangeSub64(__int64 volatile *_Subend, __int64 _Value) {
653 return __atomic_sub_fetch(_Subend, _Value, 0) + _Value;
654}
655#endif
656/*----------------------------------------------------------------------------*\
657|* Interlocked Increment
658\*----------------------------------------------------------------------------*/
Hans Wennborg2ed88802014-01-23 19:15:39 +0000659static __inline__ short __attribute__((__always_inline__, __nodebug__))
660_InterlockedIncrement16(short volatile *_Value) {
Warren Huntd6ffae92013-09-27 23:57:26 +0000661 return __atomic_add_fetch(_Value, 1, 0);
662}
663static __inline__ long __attribute__((__always_inline__, __nodebug__))
664_InterlockedIncrement(long volatile *_Value) {
665 return __atomic_add_fetch(_Value, 1, 0);
666}
Warren Hunt3f987942013-09-30 21:08:05 +0000667#ifdef __x86_64__
Warren Huntd6ffae92013-09-27 23:57:26 +0000668static __inline__ __int64 __attribute__((__always_inline__, __nodebug__))
669_InterlockedIncrement64(__int64 volatile *_Value) {
670 return __atomic_add_fetch(_Value, 1, 0);
671}
672#endif
673/*----------------------------------------------------------------------------*\
674|* Interlocked Decrement
675\*----------------------------------------------------------------------------*/
Hans Wennborg2ed88802014-01-23 19:15:39 +0000676static __inline__ short __attribute__((__always_inline__, __nodebug__))
677_InterlockedDecrement16(short volatile *_Value) {
Warren Huntd6ffae92013-09-27 23:57:26 +0000678 return __atomic_sub_fetch(_Value, 1, 0);
679}
680static __inline__ long __attribute__((__always_inline__, __nodebug__))
681_InterlockedDecrement(long volatile *_Value) {
682 return __atomic_sub_fetch(_Value, 1, 0);
683}
Warren Hunt3f987942013-09-30 21:08:05 +0000684#ifdef __x86_64__
Warren Huntd6ffae92013-09-27 23:57:26 +0000685static __inline__ __int64 __attribute__((__always_inline__, __nodebug__))
686_InterlockedDecrement64(__int64 volatile *_Value) {
687 return __atomic_sub_fetch(_Value, 1, 0);
688}
689#endif
690/*----------------------------------------------------------------------------*\
691|* Interlocked And
692\*----------------------------------------------------------------------------*/
693static __inline__ char __attribute__((__always_inline__, __nodebug__))
694_InterlockedAnd8(char volatile *_Value, char _Mask) {
695 return __atomic_and_fetch(_Value, _Mask, 0);
696}
697static __inline__ short __attribute__((__always_inline__, __nodebug__))
698_InterlockedAnd16(short volatile *_Value, short _Mask) {
699 return __atomic_and_fetch(_Value, _Mask, 0);
700}
701static __inline__ long __attribute__((__always_inline__, __nodebug__))
702_InterlockedAnd(long volatile *_Value, long _Mask) {
703 return __atomic_and_fetch(_Value, _Mask, 0);
704}
Warren Hunt3f987942013-09-30 21:08:05 +0000705#ifdef __x86_64__
Warren Huntd6ffae92013-09-27 23:57:26 +0000706static __inline__ __int64 __attribute__((__always_inline__, __nodebug__))
707_InterlockedAnd64(__int64 volatile *_Value, __int64 _Mask) {
708 return __atomic_and_fetch(_Value, _Mask, 0);
709}
710#endif
711/*----------------------------------------------------------------------------*\
712|* Interlocked Or
713\*----------------------------------------------------------------------------*/
714static __inline__ char __attribute__((__always_inline__, __nodebug__))
715_InterlockedOr8(char volatile *_Value, char _Mask) {
716 return __atomic_or_fetch(_Value, _Mask, 0);
717}
718static __inline__ short __attribute__((__always_inline__, __nodebug__))
719_InterlockedOr16(short volatile *_Value, short _Mask) {
720 return __atomic_or_fetch(_Value, _Mask, 0);
721}
722static __inline__ long __attribute__((__always_inline__, __nodebug__))
723_InterlockedOr(long volatile *_Value, long _Mask) {
724 return __atomic_or_fetch(_Value, _Mask, 0);
725}
Warren Hunt3f987942013-09-30 21:08:05 +0000726#ifdef __x86_64__
Warren Huntd6ffae92013-09-27 23:57:26 +0000727static __inline__ __int64 __attribute__((__always_inline__, __nodebug__))
728_InterlockedOr64(__int64 volatile *_Value, __int64 _Mask) {
729 return __atomic_or_fetch(_Value, _Mask, 0);
730}
731#endif
732/*----------------------------------------------------------------------------*\
733|* Interlocked Xor
734\*----------------------------------------------------------------------------*/
735static __inline__ char __attribute__((__always_inline__, __nodebug__))
736_InterlockedXor8(char volatile *_Value, char _Mask) {
737 return __atomic_xor_fetch(_Value, _Mask, 0);
738}
739static __inline__ short __attribute__((__always_inline__, __nodebug__))
740_InterlockedXor16(short volatile *_Value, short _Mask) {
741 return __atomic_xor_fetch(_Value, _Mask, 0);
742}
743static __inline__ long __attribute__((__always_inline__, __nodebug__))
744_InterlockedXor(long volatile *_Value, long _Mask) {
745 return __atomic_xor_fetch(_Value, _Mask, 0);
746}
Warren Hunt3f987942013-09-30 21:08:05 +0000747#ifdef __x86_64__
Warren Huntd6ffae92013-09-27 23:57:26 +0000748static __inline__ __int64 __attribute__((__always_inline__, __nodebug__))
749_InterlockedXor64(__int64 volatile *_Value, __int64 _Mask) {
750 return __atomic_xor_fetch(_Value, _Mask, 0);
751}
752#endif
753/*----------------------------------------------------------------------------*\
754|* Interlocked Exchange
755\*----------------------------------------------------------------------------*/
756static __inline__ char __attribute__((__always_inline__, __nodebug__))
757_InterlockedExchange8(char volatile *_Target, char _Value) {
758 __atomic_exchange(_Target, &_Value, &_Value, 0);
759 return _Value;
760}
761static __inline__ short __attribute__((__always_inline__, __nodebug__))
762_InterlockedExchange16(short volatile *_Target, short _Value) {
763 __atomic_exchange(_Target, &_Value, &_Value, 0);
764 return _Value;
765}
766static __inline__ long __attribute__((__always_inline__, __nodebug__))
767_InterlockedExchange(long volatile *_Target, long _Value) {
768 __atomic_exchange(_Target, &_Value, &_Value, 0);
769 return _Value;
770}
Warren Hunt3f987942013-09-30 21:08:05 +0000771#ifdef __x86_64__
Warren Huntd6ffae92013-09-27 23:57:26 +0000772static __inline__ __int64 __attribute__((__always_inline__, __nodebug__))
773_InterlockedExchange64(__int64 volatile *_Target, __int64 _Value) {
774 __atomic_exchange(_Target, &_Value, &_Value, 0);
775 return _Value;
776}
777#endif
778/*----------------------------------------------------------------------------*\
779|* Interlocked Compare Exchange
780\*----------------------------------------------------------------------------*/
781static __inline__ char __attribute__((__always_inline__, __nodebug__))
782_InterlockedCompareExchange8(char volatile *_Destination,
783 char _Exchange, char _Comparand) {
784 __atomic_compare_exchange(_Destination, &_Comparand, &_Exchange, 0, 0, 0);
785 return _Comparand;
786}
787static __inline__ short __attribute__((__always_inline__, __nodebug__))
788_InterlockedCompareExchange16(short volatile *_Destination,
789 short _Exchange, short _Comparand) {
790 __atomic_compare_exchange(_Destination, &_Comparand, &_Exchange, 0, 0, 0);
791 return _Comparand;
792}
793static __inline__ long __attribute__((__always_inline__, __nodebug__))
794_InterlockedCompareExchange(long volatile *_Destination,
795 long _Exchange, long _Comparand) {
796 __atomic_compare_exchange(_Destination, &_Comparand, &_Exchange, 0, 0, 0);
797 return _Comparand;
798}
Warren Hunt3f987942013-09-30 21:08:05 +0000799#ifdef __x86_64__
Reid Klecknerf08d6582014-01-27 19:16:35 +0000800static __inline__ void *__attribute__((__always_inline__, __nodebug__))
801_InterlockedCompareExchangePointer(void *volatile *_Destination,
802 void *_Exchange, void *_Comparand) {
803 __atomic_compare_exchange(_Destination, &_Comparand, &_Exchange, 0, 0, 0);
804 return _Comparand;
805}
806#endif
807#ifdef __x86_64__
Warren Huntd6ffae92013-09-27 23:57:26 +0000808static __inline__ __int64 __attribute__((__always_inline__, __nodebug__))
809_InterlockedCompareExchange64(__int64 volatile *_Destination,
810 __int64 _Exchange, __int64 _Comparand) {
811 __atomic_compare_exchange(_Destination, &_Comparand, &_Exchange, 0, 0, 0);
812 return _Comparand;
813}
814#endif
815/*----------------------------------------------------------------------------*\
Reid Kleckner00d33a52013-10-17 01:29:48 +0000816|* Barriers
817\*----------------------------------------------------------------------------*/
818static __inline__ void __attribute__((__always_inline__, __nodebug__))
819__attribute__((deprecated("use other intrinsics or C++11 atomics instead")))
820_ReadWriteBarrier(void) {
821 __asm__ volatile ("" : : : "memory");
822}
823static __inline__ void __attribute__((__always_inline__, __nodebug__))
824__attribute__((deprecated("use other intrinsics or C++11 atomics instead")))
825_ReadBarrier(void) {
826 __asm__ volatile ("" : : : "memory");
827}
828static __inline__ void __attribute__((__always_inline__, __nodebug__))
829__attribute__((deprecated("use other intrinsics or C++11 atomics instead")))
830_WriteBarrier(void) {
831 __asm__ volatile ("" : : : "memory");
832}
Reid Klecknerf08d6582014-01-27 19:16:35 +0000833#ifdef __x86_64__
834static __inline__ void __attribute__((__always_inline__, __nodebug__))
835__faststorefence(void) {
836 __asm__ volatile("lock orq $0, (%%rsp)" : : : "memory");
837}
838#endif
Reid Kleckner00d33a52013-10-17 01:29:48 +0000839/*----------------------------------------------------------------------------*\
Hans Wennborg74ca0c42014-01-24 00:52:39 +0000840|* readfs
841|* (Pointers in address space #257 are relative to the FS segment register.)
842\*----------------------------------------------------------------------------*/
843#ifdef __i386__
844#define __ptr_to_addr_space(__addr_space_nbr, __type, __offset) \
845 ((volatile __type __attribute__((__address_space__(__addr_space_nbr)))*) \
846 (__offset))
847
848static __inline__ unsigned char __attribute__((__always_inline__, __nodebug__))
849__readfsbyte(unsigned long __offset) {
850 return *__ptr_to_addr_space(257, unsigned char, __offset);
851}
852static __inline__ unsigned long __attribute__((__always_inline__, __nodebug__))
853__readfsdword(unsigned long __offset) {
854 return *__ptr_to_addr_space(257, unsigned long, __offset);
855}
856static __inline__ unsigned __int64 __attribute__((__always_inline__, __nodebug__))
857__readfsqword(unsigned long __offset) {
858 return *__ptr_to_addr_space(257, unsigned __int64, __offset);
859}
860static __inline__ unsigned short __attribute__((__always_inline__, __nodebug__))
861__readfsword(unsigned long __offset) {
862 return *__ptr_to_addr_space(257, unsigned short, __offset);
863}
864#undef __ptr_to_addr_space
865#endif
866/*----------------------------------------------------------------------------*\
Warren Huntd6ffae92013-09-27 23:57:26 +0000867|* Misc
868\*----------------------------------------------------------------------------*/
869static __inline__ void * __attribute__((__always_inline__, __nodebug__))
870_AddressOfReturnAddress(void) {
871 return (void*)((char*)__builtin_frame_address(0) + sizeof(void*));
872}
873static __inline__ void * __attribute__((__always_inline__, __nodebug__))
874_ReturnAddress(void) {
875 return __builtin_return_address(0);
876}
Hans Wennborg854f7d32014-01-16 23:39:35 +0000877static __inline__ void __attribute__((__always_inline__, __nodebug__))
878__cpuid(int __info[4], int __level) {
879#if __i386__
880 __asm__ ("cpuid"
881 : "=a"(__info[0]), "=b" (__info[1]), "=c"(__info[2]), "=d"(__info[3])
882 : "0"(__level));
883#else
884 __asm__ ("cpuid" : "=a"(__info[0]), "=b" (__info[1]), "=c"(__info[2]), "=d"(__info[3])
885 : "0"(__level));
886#endif
887}
888static __inline__ unsigned __int64 __cdecl __attribute__((__always_inline__, __nodebug__))
889_xgetbv(unsigned int __xcr_no) {
890 unsigned int __eax, __edx;
891 __asm__ ("xgetbv" : "=a" (__eax), "=d" (__edx) : "c" (__xcr_no));
892 return ((unsigned __int64)__edx << 32) | __eax;
893}
Hans Wennborg740a4d62014-01-28 22:55:01 +0000894static __inline__ unsigned __int64 __attribute__((__always_inline__, __nodebug__))
895__rdtsc(void) {
896 unsigned int __eax, __edx;
897 __asm__ ("rdtsc" : "=a" (__eax), "=d" (__edx));
898 return ((unsigned __int64)__edx << 32) | __eax;
899}
900static __inline__ void __attribute__((__always_inline__, __nodebug__))
901__halt(void) {
902 __asm__ volatile ("hlt");
903}
Warren Huntd6ffae92013-09-27 23:57:26 +0000904
Eric Christophercc872532013-09-18 22:40:18 +0000905#ifdef __cplusplus
Eric Christopher0db88a72013-09-18 22:24:01 +0000906}
Eric Christopherd1428bf2013-08-31 00:22:48 +0000907#endif
Reid Klecknerf0e23222013-09-19 00:19:53 +0000908
909#endif /* __INTRIN_H */
910#endif /* _MSC_VER */