blob: 6005adfcd4ebe7b71d40fd8b1c9b570466467443 [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
Eric Christopher0db88a72013-09-18 22:24:01 +000035#ifdef __cplusplus
36extern "C" {
37#endif
38
Eric Christopherd1428bf2013-08-31 00:22:48 +000039/* And the random ones that aren't in those files. */
40__m64 _m_from_float(float);
41__m64 _m_from_int(int _l);
42void _m_prefetch(void *);
43float _m_to_float(__m64);
44int _m_to_int(__m64 _M);
45
46/* Other assorted instruction intrinsics. */
47void __addfsbyte(unsigned long, unsigned char);
48void __addfsdword(unsigned long, unsigned long);
49void __addfsword(unsigned long, unsigned short);
50void __code_seg(const char *);
Hans Wennborg854f7d32014-01-16 23:39:35 +000051static __inline__
Eric Christopherd1428bf2013-08-31 00:22:48 +000052void __cpuid(int[4], int);
53void __cpuidex(int[4], int, int);
Eric Christopher0db88a72013-09-18 22:24:01 +000054void __debugbreak(void);
Eric Christopherd1428bf2013-08-31 00:22:48 +000055__int64 __emul(int, int);
56unsigned __int64 __emulu(unsigned int, unsigned int);
Eric Christopher0db88a72013-09-18 22:24:01 +000057void __cdecl __fastfail(unsigned int);
Eric Christopherd1428bf2013-08-31 00:22:48 +000058unsigned int __getcallerseflags(void);
59void __halt(void);
60unsigned char __inbyte(unsigned short);
61void __inbytestring(unsigned short, unsigned char *, unsigned long);
62void __incfsbyte(unsigned long);
63void __incfsdword(unsigned long);
64void __incfsword(unsigned long);
65unsigned long __indword(unsigned short);
66void __indwordstring(unsigned short, unsigned long *, unsigned long);
67void __int2c(void);
68void __invlpg(void *);
69unsigned short __inword(unsigned short);
70void __inwordstring(unsigned short, unsigned short *, unsigned long);
71void __lidt(void *);
72unsigned __int64 __ll_lshift(unsigned __int64, int);
73__int64 __ll_rshift(__int64, int);
74void __llwpcb(void *);
75unsigned char __lwpins32(unsigned int, unsigned int, unsigned int);
76void __lwpval32(unsigned int, unsigned int, unsigned int);
77unsigned int __lzcnt(unsigned int);
78unsigned short __lzcnt16(unsigned short);
79void __movsb(unsigned char *, unsigned char const *, size_t);
80void __movsd(unsigned long *, unsigned long const *, size_t);
81void __movsw(unsigned short *, unsigned short const *, size_t);
82void __nop(void);
83void __nvreg_restore_fence(void);
84void __nvreg_save_fence(void);
85void __outbyte(unsigned short, unsigned char);
86void __outbytestring(unsigned short, unsigned char *, unsigned long);
87void __outdword(unsigned short, unsigned long);
88void __outdwordstring(unsigned short, unsigned long *, unsigned long);
89void __outword(unsigned short, unsigned short);
90void __outwordstring(unsigned short, unsigned short *, unsigned long);
Warren Huntd6ffae92013-09-27 23:57:26 +000091static __inline__
Eric Christopherd1428bf2013-08-31 00:22:48 +000092unsigned int __popcnt(unsigned int);
Warren Huntd6ffae92013-09-27 23:57:26 +000093static __inline__
Eric Christopherd1428bf2013-08-31 00:22:48 +000094unsigned short __popcnt16(unsigned short);
95unsigned __int64 __rdtsc(void);
96unsigned __int64 __rdtscp(unsigned int *);
97unsigned long __readcr0(void);
98unsigned long __readcr2(void);
99unsigned long __readcr3(void);
Eric Christopher439137e2014-01-24 12:13:47 +0000100unsigned long __readcr4(void);
Eric Christopherd1428bf2013-08-31 00:22:48 +0000101unsigned long __readcr8(void);
102unsigned int __readdr(unsigned int);
103unsigned int __readeflags(void);
Hans Wennborg74ca0c42014-01-24 00:52:39 +0000104#ifdef __i386__
105static __inline__
Eric Christopherd1428bf2013-08-31 00:22:48 +0000106unsigned char __readfsbyte(unsigned long);
Hans Wennborg74ca0c42014-01-24 00:52:39 +0000107static __inline__
Eric Christopherd1428bf2013-08-31 00:22:48 +0000108unsigned long __readfsdword(unsigned long);
Hans Wennborg74ca0c42014-01-24 00:52:39 +0000109static __inline__
Eric Christopherd1428bf2013-08-31 00:22:48 +0000110unsigned __int64 __readfsqword(unsigned long);
Hans Wennborg74ca0c42014-01-24 00:52:39 +0000111static __inline__
Eric Christopherd1428bf2013-08-31 00:22:48 +0000112unsigned short __readfsword(unsigned long);
Hans Wennborg74ca0c42014-01-24 00:52:39 +0000113#endif
Eric Christopherd1428bf2013-08-31 00:22:48 +0000114unsigned __int64 __readmsr(unsigned long);
115unsigned __int64 __readpmc(unsigned long);
116unsigned long __segmentlimit(unsigned long);
117void __sidt(void *);
118void *__slwpcb(void);
119void __stosb(unsigned char *, unsigned char, size_t);
120void __stosd(unsigned long *, unsigned long, size_t);
121void __stosw(unsigned short *, unsigned short, size_t);
122void __svm_clgi(void);
123void __svm_invlpga(void *, int);
124void __svm_skinit(int);
125void __svm_stgi(void);
126void __svm_vmload(size_t);
127void __svm_vmrun(size_t);
128void __svm_vmsave(size_t);
129void __ud2(void);
130unsigned __int64 __ull_rshift(unsigned __int64, int);
131void __vmx_off(void);
132void __vmx_vmptrst(unsigned __int64 *);
133void __wbinvd(void);
134void __writecr0(unsigned int);
135void __writecr3(unsigned int);
136void __writecr4(unsigned int);
137void __writecr8(unsigned int);
138void __writedr(unsigned int, unsigned int);
139void __writeeflags(unsigned int);
140void __writefsbyte(unsigned long, unsigned char);
141void __writefsdword(unsigned long, unsigned long);
142void __writefsqword(unsigned long, unsigned __int64);
143void __writefsword(unsigned long, unsigned short);
144void __writemsr(unsigned long, unsigned __int64);
Warren Huntd6ffae92013-09-27 23:57:26 +0000145static __inline__
Eric Christopherd1428bf2013-08-31 00:22:48 +0000146void *_AddressOfReturnAddress(void);
147unsigned int _andn_u32(unsigned int, unsigned int);
148unsigned int _bextr_u32(unsigned int, unsigned int, unsigned int);
Eric Christopherd1428bf2013-08-31 00:22:48 +0000149unsigned int _bextri_u32(unsigned int, unsigned int);
Warren Huntd6ffae92013-09-27 23:57:26 +0000150static __inline__
Eric Christopherd1428bf2013-08-31 00:22:48 +0000151unsigned char _BitScanForward(unsigned long *_Index, unsigned long _Mask);
Warren Huntd6ffae92013-09-27 23:57:26 +0000152static __inline__
Eric Christopherd1428bf2013-08-31 00:22:48 +0000153unsigned char _BitScanReverse(unsigned long *_Index, unsigned long _Mask);
Warren Huntd6ffae92013-09-27 23:57:26 +0000154static __inline__
Eric Christopherd1428bf2013-08-31 00:22:48 +0000155unsigned char _bittest(long const *, long);
Warren Huntd6ffae92013-09-27 23:57:26 +0000156static __inline__
Eric Christopherd1428bf2013-08-31 00:22:48 +0000157unsigned char _bittestandcomplement(long *, long);
Warren Huntd6ffae92013-09-27 23:57:26 +0000158static __inline__
Eric Christopherd1428bf2013-08-31 00:22:48 +0000159unsigned char _bittestandreset(long *, long);
Warren Huntd6ffae92013-09-27 23:57:26 +0000160static __inline__
Eric Christopherd1428bf2013-08-31 00:22:48 +0000161unsigned char _bittestandset(long *, long);
162unsigned int _blcfill_u32(unsigned int);
163unsigned int _blci_u32(unsigned int);
164unsigned int _blcic_u32(unsigned int);
165unsigned int _blcmsk_u32(unsigned int);
166unsigned int _blcs_u32(unsigned int);
167unsigned int _blsfill_u32(unsigned int);
168unsigned int _blsi_u32(unsigned int);
169unsigned int _blsic_u32(unsigned int);
170unsigned int _blsmsk_u32(unsigned int);
Eric Christopherd1428bf2013-08-31 00:22:48 +0000171unsigned int _blsr_u32(unsigned int);
172unsigned __int64 __cdecl _byteswap_uint64(unsigned __int64);
173unsigned long __cdecl _byteswap_ulong(unsigned long);
174unsigned short __cdecl _byteswap_ushort(unsigned short);
175unsigned _bzhi_u32(unsigned int, unsigned int);
176void __cdecl _disable(void);
177void __cdecl _enable(void);
178void __cdecl _fxrstor(void const *);
179void __cdecl _fxsave(void *);
180long _InterlockedAddLargeStatistic(__int64 volatile *_Addend, long _Value);
Warren Huntd6ffae92013-09-27 23:57:26 +0000181static __inline__
Eric Christopherd1428bf2013-08-31 00:22:48 +0000182long _InterlockedAnd(long volatile *_Value, long _Mask);
Warren Huntd6ffae92013-09-27 23:57:26 +0000183static __inline__
Eric Christopherd1428bf2013-08-31 00:22:48 +0000184short _InterlockedAnd16(short volatile *_Value, short _Mask);
Warren Huntd6ffae92013-09-27 23:57:26 +0000185static __inline__
Eric Christopherd1428bf2013-08-31 00:22:48 +0000186char _InterlockedAnd8(char volatile *_Value, char _Mask);
187unsigned char _interlockedbittestandreset(long volatile *, long);
188unsigned char _interlockedbittestandset(long volatile *, long);
Warren Huntd6ffae92013-09-27 23:57:26 +0000189static __inline__
Eric Christopherd1428bf2013-08-31 00:22:48 +0000190long __cdecl _InterlockedCompareExchange(long volatile *_Destination,
191 long _Exchange, long _Comparand);
192long _InterlockedCompareExchange_HLEAcquire(long volatile *, long, long);
193long _InterlockedCompareExchange_HLERelease(long volatile *, long, long);
Warren Huntd6ffae92013-09-27 23:57:26 +0000194static __inline__
Eric Christopherd1428bf2013-08-31 00:22:48 +0000195short _InterlockedCompareExchange16(short volatile *_Destination,
196 short _Exchange, short _Comparand);
Warren Huntd6ffae92013-09-27 23:57:26 +0000197static __inline__
Eric Christopherd1428bf2013-08-31 00:22:48 +0000198__int64 _InterlockedCompareExchange64(__int64 volatile *_Destination,
199 __int64 _Exchange, __int64 _Comparand);
200__int64 _InterlockedcompareExchange64_HLEAcquire(__int64 volatile *, __int64,
201 __int64);
202__int64 _InterlockedCompareExchange64_HLERelease(__int64 volatile *, __int64,
203 __int64);
Warren Huntd6ffae92013-09-27 23:57:26 +0000204static __inline__
Eric Christopherd1428bf2013-08-31 00:22:48 +0000205char _InterlockedCompareExchange8(char volatile *_Destination, char _Exchange,
206 char _Comparand);
207void *_InterlockedCompareExchangePointer_HLEAcquire(void *volatile *, void *,
208 void *);
209void *_InterlockedCompareExchangePointer_HLERelease(void *volatile *, void *,
210 void *);
Warren Huntd6ffae92013-09-27 23:57:26 +0000211static __inline__
Eric Christopherd1428bf2013-08-31 00:22:48 +0000212long __cdecl _InterlockedDecrement(long volatile *_Addend);
Warren Huntd6ffae92013-09-27 23:57:26 +0000213static __inline__
Eric Christopherd1428bf2013-08-31 00:22:48 +0000214short _InterlockedDecrement16(short volatile *_Addend);
Warren Huntd6ffae92013-09-27 23:57:26 +0000215static __inline__
Eric Christopherd1428bf2013-08-31 00:22:48 +0000216long __cdecl _InterlockedExchange(long volatile *_Target, long _Value);
Warren Huntd6ffae92013-09-27 23:57:26 +0000217static __inline__
Eric Christopherd1428bf2013-08-31 00:22:48 +0000218short _InterlockedExchange16(short volatile *_Target, short _Value);
Warren Huntd6ffae92013-09-27 23:57:26 +0000219static __inline__
Eric Christopherd1428bf2013-08-31 00:22:48 +0000220char _InterlockedExchange8(char volatile *_Target, char _Value);
Warren Huntd6ffae92013-09-27 23:57:26 +0000221static __inline__
Eric Christopherd1428bf2013-08-31 00:22:48 +0000222long __cdecl _InterlockedExchangeAdd(long volatile *_Addend, long _Value);
223long _InterlockedExchangeAdd_HLEAcquire(long volatile *, long);
224long _InterlockedExchangeAdd_HLERelease(long volatile *, long);
Reid Kleckner924eb2a2014-01-27 18:48:02 +0000225static __inline__
Eric Christopher439137e2014-01-24 12:13:47 +0000226short _InterlockedExchangeAdd16(short volatile *_Addend, short _Value);
227__int64 _InterlockedExchangeAdd64_HLEAcquire(__int64 volatile *, __int64);
228__int64 _InterlockedExchangeAdd64_HLERelease(__int64 volatile *, __int64);
Warren Huntd6ffae92013-09-27 23:57:26 +0000229static __inline__
Eric Christopherd1428bf2013-08-31 00:22:48 +0000230char _InterlockedExchangeAdd8(char volatile *_Addend, char _Value);
Warren Huntd6ffae92013-09-27 23:57:26 +0000231static __inline__
Eric Christopherd1428bf2013-08-31 00:22:48 +0000232long __cdecl _InterlockedIncrement(long volatile *_Addend);
Warren Huntd6ffae92013-09-27 23:57:26 +0000233static __inline__
Eric Christopherd1428bf2013-08-31 00:22:48 +0000234short _InterlockedIncrement16(short volatile *_Addend);
Warren Huntd6ffae92013-09-27 23:57:26 +0000235static __inline__
Eric Christopherd1428bf2013-08-31 00:22:48 +0000236long _InterlockedOr(long volatile *_Value, long _Mask);
Warren Huntd6ffae92013-09-27 23:57:26 +0000237static __inline__
Eric Christopherd1428bf2013-08-31 00:22:48 +0000238short _InterlockedOr16(short volatile *_Value, short _Mask);
Warren Huntd6ffae92013-09-27 23:57:26 +0000239static __inline__
Eric Christopherd1428bf2013-08-31 00:22:48 +0000240char _InterlockedOr8(char volatile *_Value, char _Mask);
Warren Huntd6ffae92013-09-27 23:57:26 +0000241static __inline__
Eric Christopherd1428bf2013-08-31 00:22:48 +0000242long _InterlockedXor(long volatile *_Value, long _Mask);
Warren Huntd6ffae92013-09-27 23:57:26 +0000243static __inline__
Eric Christopherd1428bf2013-08-31 00:22:48 +0000244short _InterlockedXor16(short volatile *_Value, short _Mask);
Warren Huntd6ffae92013-09-27 23:57:26 +0000245static __inline__
Eric Christopherd1428bf2013-08-31 00:22:48 +0000246char _InterlockedXor8(char volatile *_Value, char _Mask);
247void __cdecl _invpcid(unsigned int, void *);
Warren Huntd6ffae92013-09-27 23:57:26 +0000248static __inline__
Eric Christopherd1428bf2013-08-31 00:22:48 +0000249unsigned long __cdecl _lrotl(unsigned long, int);
Warren Huntd6ffae92013-09-27 23:57:26 +0000250static __inline__
Eric Christopherd1428bf2013-08-31 00:22:48 +0000251unsigned long __cdecl _lrotr(unsigned long, int);
Warren Huntd6ffae92013-09-27 23:57:26 +0000252static __inline__
Eric Christopherd1428bf2013-08-31 00:22:48 +0000253unsigned int _lzcnt_u32(unsigned int);
Reid Kleckner00d33a52013-10-17 01:29:48 +0000254static __inline__
Eric Christopherd1428bf2013-08-31 00:22:48 +0000255void _ReadBarrier(void);
Reid Kleckner00d33a52013-10-17 01:29:48 +0000256static __inline__
Eric Christopherd1428bf2013-08-31 00:22:48 +0000257void _ReadWriteBarrier(void);
Warren Huntd6ffae92013-09-27 23:57:26 +0000258static __inline__
Eric Christopherd1428bf2013-08-31 00:22:48 +0000259void *_ReturnAddress(void);
260unsigned int _rorx_u32(unsigned int, const unsigned int);
261int __cdecl _rdrand16_step(unsigned short *);
262int __cdecl _rdrand32_step(unsigned int *);
Warren Huntd6ffae92013-09-27 23:57:26 +0000263static __inline__
Eric Christopherfb4b4332013-08-31 00:27:38 +0000264unsigned int __cdecl _rotl(unsigned int _Value, int _Shift);
Warren Huntd6ffae92013-09-27 23:57:26 +0000265static __inline__
Eric Christopherd1428bf2013-08-31 00:22:48 +0000266unsigned short _rotl16(unsigned short _Value, unsigned char _Shift);
Warren Huntd6ffae92013-09-27 23:57:26 +0000267static __inline__
Eric Christopherd1428bf2013-08-31 00:22:48 +0000268unsigned __int64 __cdecl _rotl64(unsigned __int64 _Value, int _Shift);
Warren Huntd6ffae92013-09-27 23:57:26 +0000269static __inline__
Eric Christopherd1428bf2013-08-31 00:22:48 +0000270unsigned char _rotl8(unsigned char _Value, unsigned char _Shift);
Warren Huntd6ffae92013-09-27 23:57:26 +0000271static __inline__
Eric Christopherd1428bf2013-08-31 00:22:48 +0000272unsigned int __cdecl _rotr(unsigned int _Value, int _Shift);
Warren Huntd6ffae92013-09-27 23:57:26 +0000273static __inline__
Eric Christopherd1428bf2013-08-31 00:22:48 +0000274unsigned short _rotr16(unsigned short _Value, unsigned char _Shift);
Warren Huntd6ffae92013-09-27 23:57:26 +0000275static __inline__
Eric Christopherd1428bf2013-08-31 00:22:48 +0000276unsigned __int64 __cdecl _rotr64(unsigned __int64 _Value, int _Shift);
Warren Huntd6ffae92013-09-27 23:57:26 +0000277static __inline__
Eric Christopherd1428bf2013-08-31 00:22:48 +0000278unsigned char _rotr8(unsigned char _Value, unsigned char _Shift);
279int _sarx_i32(int, unsigned int);
Reid Kleckner33630902014-01-27 19:32:42 +0000280
281/* FIXME: Need definition for jmp_buf.
282 int __cdecl _setjmp(jmp_buf); */
283
Eric Christopherd1428bf2013-08-31 00:22:48 +0000284unsigned int _shlx_u32(unsigned int, unsigned int);
285unsigned int _shrx_u32(unsigned int, unsigned int);
286void _Store_HLERelease(long volatile *, long);
287void _Store64_HLERelease(__int64 volatile *, __int64);
288void _StorePointer_HLERelease(void *volatile *, void *);
289unsigned int _t1mskc_u32(unsigned int);
290unsigned int _tzcnt_u32(unsigned int);
Eric Christopherd1428bf2013-08-31 00:22:48 +0000291unsigned int _tzmsk_u32(unsigned int);
Reid Kleckner00d33a52013-10-17 01:29:48 +0000292static __inline__
Eric Christopherd1428bf2013-08-31 00:22:48 +0000293void _WriteBarrier(void);
294void _xabort(const unsigned int imm);
295unsigned __int32 xbegin(void);
296void _xend(void);
Hans Wennborg854f7d32014-01-16 23:39:35 +0000297static __inline__
Eric Christopherd1428bf2013-08-31 00:22:48 +0000298unsigned __int64 __cdecl _xgetbv(unsigned int);
299void __cdecl _xrstor(void const *, unsigned __int64);
300void __cdecl _xsave(void *, unsigned __int64);
301void __cdecl _xsaveopt(void *, unsigned __int64);
302void __cdecl _xsetbv(unsigned int, unsigned __int64);
303unsigned char _xtest(void);
304
305/* These additional intrinsics are turned on in x64/amd64/x86_64 mode. */
Warren Hunt3f987942013-09-30 21:08:05 +0000306#ifdef __x86_64__
Eric Christopherd1428bf2013-08-31 00:22:48 +0000307void __addgsbyte(unsigned long, unsigned char);
308void __addgsdword(unsigned long, unsigned long);
309void __addgsqword(unsigned long, unsigned __int64);
310void __addgsword(unsigned long, unsigned short);
Reid Klecknerf08d6582014-01-27 19:16:35 +0000311static __inline__
Eric Christopherd1428bf2013-08-31 00:22:48 +0000312void __faststorefence(void);
313void __incgsbyte(unsigned long);
314void __incgsdword(unsigned long);
315void __incgsqword(unsigned long);
316void __incgsword(unsigned long);
Eric Christopher439137e2014-01-24 12:13:47 +0000317unsigned char __lwpins64(unsigned __int64, unsigned int, unsigned int);
318void __lwpval64(unsigned __int64, unsigned int, unsigned int);
319unsigned __int64 __lzcnt64(unsigned __int64);
320void __movsq(unsigned long long *, unsigned long long const *, size_t);
321__int64 __mulh(__int64, __int64);
Reid Kleckner924eb2a2014-01-27 18:48:02 +0000322static __inline__
Eric Christopherd1428bf2013-08-31 00:22:48 +0000323unsigned __int64 __popcnt64(unsigned __int64);
Eric Christopher439137e2014-01-24 12:13:47 +0000324unsigned char __readgsbyte(unsigned long);
325unsigned long __readgsdword(unsigned long);
326unsigned short __readgsword(unsigned long);
Eric Christopherd1428bf2013-08-31 00:22:48 +0000327unsigned __int64 __shiftleft128(unsigned __int64 _LowPart,
328 unsigned __int64 _HighPart,
329 unsigned char _Shift);
330unsigned __int64 __shiftright128(unsigned __int64 _LowPart,
331 unsigned __int64 _HighPart,
332 unsigned char _Shift);
333void __stosq(unsigned __int64 *, unsigned __int64, size_t);
Eric Christopher439137e2014-01-24 12:13:47 +0000334unsigned __int64 __umulh(unsigned __int64, unsigned __int64);
335unsigned char __vmx_on(unsigned __int64 *);
336unsigned char __vmx_vmclear(unsigned __int64 *);
337unsigned char __vmx_vmlaunch(void);
338unsigned char __vmx_vmptrld(unsigned __int64 *);
339unsigned char __vmx_vmread(size_t, size_t *);
340unsigned char __vmx_vmresume(void);
341unsigned char __vmx_vmwrite(size_t, size_t);
342void __writegsbyte(unsigned long, unsigned char);
343void __writegsdword(unsigned long, unsigned long);
344void __writegsqword(unsigned long, unsigned __int64);
345void __writegsword(unsigned long, unsigned short);
Eric Christopherd1428bf2013-08-31 00:22:48 +0000346unsigned __int64 _andn_u64(unsigned __int64, unsigned __int64);
347unsigned __int64 _bextr_u64(unsigned __int64, unsigned int, unsigned int);
348unsigned __int64 _bextri_u64(unsigned __int64, unsigned int);
Warren Huntd6ffae92013-09-27 23:57:26 +0000349static __inline__
Eric Christopherd1428bf2013-08-31 00:22:48 +0000350unsigned char _BitScanForward64(unsigned long *_Index, unsigned __int64 _Mask);
Warren Huntd6ffae92013-09-27 23:57:26 +0000351static __inline__
Eric Christopherd1428bf2013-08-31 00:22:48 +0000352unsigned char _BitScanReverse64(unsigned long *_Index, unsigned __int64 _Mask);
Warren Huntd6ffae92013-09-27 23:57:26 +0000353static __inline__
Eric Christopherd1428bf2013-08-31 00:22:48 +0000354unsigned char _bittest64(__int64 const *, __int64);
Warren Huntd6ffae92013-09-27 23:57:26 +0000355static __inline__
Eric Christopherd1428bf2013-08-31 00:22:48 +0000356unsigned char _bittestandcomplement64(__int64 *, __int64);
Warren Huntd6ffae92013-09-27 23:57:26 +0000357static __inline__
Eric Christopherd1428bf2013-08-31 00:22:48 +0000358unsigned char _bittestandreset64(__int64 *, __int64);
Warren Huntd6ffae92013-09-27 23:57:26 +0000359static __inline__
Eric Christopherd1428bf2013-08-31 00:22:48 +0000360unsigned char _bittestandset64(__int64 *, __int64);
361unsigned __int64 _blcfill_u64(unsigned __int64);
362unsigned __int64 _blci_u64(unsigned __int64);
363unsigned __int64 _blcic_u64(unsigned __int64);
364unsigned __int64 _blcmsk_u64(unsigned __int64);
365unsigned __int64 _blcs_u64(unsigned __int64);
366unsigned __int64 _blsfill_u64(unsigned __int64);
367unsigned __int64 _blsi_u64(unsigned __int64);
368unsigned __int64 _blsic_u64(unsigned __int64);
Eric Christopher439137e2014-01-24 12:13:47 +0000369unsigned __int64 _blsmsk_u64(unsigned __int64);
Eric Christopherd1428bf2013-08-31 00:22:48 +0000370unsigned __int64 _blsr_u64(unsigned __int64);
371unsigned __int64 __cdecl _byteswap_uint64(unsigned __int64);
372unsigned __int64 _bzhi_u64(unsigned __int64, unsigned int);
373void __cdecl _fxrstor64(void const *);
374void __cdecl _fxsave64(void *);
375long _InterlockedAnd_np(long volatile *_Value, long _Mask);
376short _InterlockedAnd16_np(short volatile *_Value, short _Mask);
377__int64 _InterlockedAnd64_np(__int64 volatile *_Value, __int64 _Mask);
378char _InterlockedAnd8_np(char volatile *_Value, char _Mask);
379unsigned char _interlockedbittestandreset64(__int64 volatile *, __int64);
380unsigned char _interlockedbittestandset64(__int64 volatile *, __int64);
381long _InterlockedCompareExchange_np(long volatile *_Destination, long _Exchange,
382 long _Comparand);
383unsigned char _InterlockedCompareExchange128(__int64 volatile *_Destination,
384 __int64 _ExchangeHigh,
385 __int64 _ExchangeLow,
386 __int64 *_CompareandResult);
387unsigned char _InterlockedCompareExchange128_np(__int64 volatile *_Destination,
388 __int64 _ExchangeHigh,
389 __int64 _ExchangeLow,
390 __int64 *_ComparandResult);
391short _InterlockedCompareExchange16_np(short volatile *_Destination,
392 short _Exchange, short _Comparand);
Eric Christopher439137e2014-01-24 12:13:47 +0000393__int64 _InterlockedCompareExchange64_HLEAcquire(__int64 volatile *, __int64,
394 __int64);
395__int64 _InterlockedCompareExchange64_HLERelease(__int64 volatile *, __int64,
396 __int64);
Eric Christopherd1428bf2013-08-31 00:22:48 +0000397__int64 _InterlockedCompareExchange64_np(__int64 volatile *_Destination,
398 __int64 _Exchange, __int64 _Comparand);
Reid Klecknerf08d6582014-01-27 19:16:35 +0000399static __inline__
Eric Christopher58b40432014-01-25 01:38:30 +0000400void *_InterlockedCompareExchangePointer(void *volatile *_Destination,
401 void *_Exchange, void *_Comparand);
Eric Christopherd1428bf2013-08-31 00:22:48 +0000402void *_InterlockedCompareExchangePointer_np(void *volatile *_Destination,
403 void *_Exchange, void *_Comparand);
Reid Kleckner924eb2a2014-01-27 18:48:02 +0000404static __inline__
Eric Christopher439137e2014-01-24 12:13:47 +0000405__int64 _InterlockedDecrement64(__int64 volatile *_Addend);
Reid Kleckner924eb2a2014-01-27 18:48:02 +0000406static __inline__
Eric Christopher439137e2014-01-24 12:13:47 +0000407__int64 _InterlockedExchange64(__int64 volatile *_Target, __int64 _Value);
Reid Kleckner924eb2a2014-01-27 18:48:02 +0000408static __inline__
Eric Christopher439137e2014-01-24 12:13:47 +0000409__int64 _InterlockedExchangeAdd64(__int64 volatile *_Addend, __int64 _Value);
410void *_InterlockedExchangePointer(void *volatile *_Target, void *_Value);
Reid Kleckner924eb2a2014-01-27 18:48:02 +0000411static __inline__
Eric Christopher439137e2014-01-24 12:13:47 +0000412__int64 _InterlockedIncrement64(__int64 volatile *_Addend);
Eric Christopherd1428bf2013-08-31 00:22:48 +0000413long _InterlockedOr_np(long volatile *_Value, long _Mask);
414short _InterlockedOr16_np(short volatile *_Value, short _Mask);
Reid Kleckner924eb2a2014-01-27 18:48:02 +0000415static __inline__
Eric Christopher439137e2014-01-24 12:13:47 +0000416__int64 _InterlockedOr64(__int64 volatile *_Value, __int64 _Mask);
Warren Hunt41a993f2013-09-28 00:15:41 +0000417__int64 _InterlockedOr64_np(__int64 volatile *_Value, __int64 _Mask);
Eric Christopherd1428bf2013-08-31 00:22:48 +0000418char _InterlockedOr8_np(char volatile *_Value, char _Mask);
419long _InterlockedXor_np(long volatile *_Value, long _Mask);
420short _InterlockedXor16_np(short volatile *_Value, short _Mask);
Reid Kleckner924eb2a2014-01-27 18:48:02 +0000421static __inline__
Eric Christopher439137e2014-01-24 12:13:47 +0000422__int64 _InterlockedXor64(__int64 volatile *_Value, __int64 _Mask);
Eric Christopherd1428bf2013-08-31 00:22:48 +0000423__int64 _InterlockedXor64_np(__int64 volatile *_Value, __int64 _Mask);
424char _InterlockedXor8_np(char volatile *_Value, char _Mask);
Reid Kleckner924eb2a2014-01-27 18:48:02 +0000425static __inline__
Eric Christopherd1428bf2013-08-31 00:22:48 +0000426unsigned __int64 _lzcnt_u64(unsigned __int64);
427__int64 _mul128(__int64 _Multiplier, __int64 _Multiplicand,
428 __int64 *_HighProduct);
429unsigned int __cdecl _readfsbase_u32(void);
430unsigned __int64 __cdecl _readfsbase_u64(void);
431unsigned int __cdecl _readgsbase_u32(void);
432unsigned __int64 __cdecl _readgsbase_u64(void);
433unsigned __int64 _rorx_u64(unsigned __int64, const unsigned int);
Eric Christopher439137e2014-01-24 12:13:47 +0000434__int64 _sarx_i64(__int64, unsigned int);
Reid Kleckner33630902014-01-27 19:32:42 +0000435/* FIXME: Need definition for jmp_buf.
436 int __cdecl _setjmpex(jmp_buf); */
Eric Christopher439137e2014-01-24 12:13:47 +0000437unsigned __int64 _shlx_u64(unsigned __int64, unsigned int);
438unsigned __int64 shrx_u64(unsigned __int64, unsigned int);
Eric Christopherd1428bf2013-08-31 00:22:48 +0000439unsigned __int64 _tzcnt_u64(unsigned __int64);
440unsigned __int64 _tzmsk_u64(unsigned __int64);
441unsigned __int64 _umul128(unsigned __int64 _Multiplier,
442 unsigned __int64 _Multiplicand,
443 unsigned __int64 *_HighProduct);
444void __cdecl _writefsbase_u32(unsigned int);
445void _cdecl _writefsbase_u64(unsigned __int64);
446void __cdecl _writegsbase_u32(unsigned int);
447void __cdecl _writegsbase_u64(unsigned __int64);
448void __cdecl _xrstor64(void const *, unsigned __int64);
449void __cdecl _xsave64(void *, unsigned __int64);
450void __cdecl _xsaveopt64(void *, unsigned __int64);
Eric Christopher0db88a72013-09-18 22:24:01 +0000451
Warren Hunt3f987942013-09-30 21:08:05 +0000452#endif /* __x86_64__ */
Reid Klecknerf0e23222013-09-19 00:19:53 +0000453
Warren Huntd6ffae92013-09-27 23:57:26 +0000454/*----------------------------------------------------------------------------*\
455|* Bit Twiddling
456\*----------------------------------------------------------------------------*/
457static __inline__ unsigned char __attribute__((__always_inline__, __nodebug__))
458_rotl8(unsigned char _Value, unsigned char _Shift) {
459 _Shift &= 0x7;
460 return _Shift ? (_Value << _Shift) | (_Value >> (8 - _Shift)) : _Value;
461}
462static __inline__ unsigned char __attribute__((__always_inline__, __nodebug__))
463_rotr8(unsigned char _Value, unsigned char _Shift) {
464 _Shift &= 0x7;
465 return _Shift ? (_Value >> _Shift) | (_Value << (8 - _Shift)) : _Value;
466}
467static __inline__ unsigned short __attribute__((__always_inline__, __nodebug__))
468_rotl16(unsigned short _Value, unsigned char _Shift) {
469 _Shift &= 0xf;
470 return _Shift ? (_Value << _Shift) | (_Value >> (16 - _Shift)) : _Value;
471}
472static __inline__ unsigned short __attribute__((__always_inline__, __nodebug__))
473_rotr16(unsigned short _Value, unsigned char _Shift) {
474 _Shift &= 0xf;
475 return _Shift ? (_Value >> _Shift) | (_Value << (16 - _Shift)) : _Value;
476}
477static __inline__ unsigned int __attribute__((__always_inline__, __nodebug__))
478_rotl(unsigned int _Value, int _Shift) {
479 _Shift &= 0x1f;
480 return _Shift ? (_Value << _Shift) | (_Value >> (32 - _Shift)) : _Value;
481}
482static __inline__ unsigned int __attribute__((__always_inline__, __nodebug__))
483_rotr(unsigned int _Value, int _Shift) {
484 _Shift &= 0x1f;
485 return _Shift ? (_Value >> _Shift) | (_Value << (32 - _Shift)) : _Value;
486}
487static __inline__ unsigned long __attribute__((__always_inline__, __nodebug__))
488_lrotl(unsigned long _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_lrotr(unsigned long _Value, int _Shift) {
494 _Shift &= 0x1f;
495 return _Shift ? (_Value >> _Shift) | (_Value << (32 - _Shift)) : _Value;
496}
497static
498__inline__ unsigned __int64 __attribute__((__always_inline__, __nodebug__))
499_rotl64(unsigned __int64 _Value, int _Shift) {
500 _Shift &= 0x3f;
501 return _Shift ? (_Value << _Shift) | (_Value >> (64 - _Shift)) : _Value;
502}
503static
504__inline__ unsigned __int64 __attribute__((__always_inline__, __nodebug__))
505_rotr64(unsigned __int64 _Value, int _Shift) {
506 _Shift &= 0x3f;
507 return _Shift ? (_Value >> _Shift) | (_Value << (64 - _Shift)) : _Value;
508}
509/*----------------------------------------------------------------------------*\
510|* Bit Counting and Testing
511\*----------------------------------------------------------------------------*/
512static __inline__ unsigned char __attribute__((__always_inline__, __nodebug__))
513_BitScanForward(unsigned long *_Index, unsigned long _Mask) {
514 if (!_Mask)
515 return 0;
516 *_Index = __builtin_ctzl(_Mask);
517 return 1;
518}
519static __inline__ unsigned char __attribute__((__always_inline__, __nodebug__))
520_BitScanReverse(unsigned long *_Index, unsigned long _Mask) {
521 if (!_Mask)
522 return 0;
523 *_Index = 31 - __builtin_clzl(_Mask);
524 return 1;
525}
526static __inline__ unsigned int __attribute__((__always_inline__, __nodebug__))
527_lzcnt_u32(unsigned int a) {
528 if (!a)
529 return 32;
530 return __builtin_clzl(a);
531}
532static __inline__ unsigned short __attribute__((__always_inline__, __nodebug__))
533__popcnt16(unsigned short value) {
534 return __builtin_popcount((int)value);
535}
536static __inline__ unsigned int __attribute__((__always_inline__, __nodebug__))
537__popcnt(unsigned int value) {
538 return __builtin_popcount(value);
539}
540static __inline__ unsigned char __attribute__((__always_inline__, __nodebug__))
541_bittest(long const *a, long b) {
542 return (*a >> b) & 1;
543}
544static __inline__ unsigned char __attribute__((__always_inline__, __nodebug__))
545_bittestandcomplement(long *a, long b) {
546 unsigned char x = (*a >> b) & 1;
547 *a = *a ^ (1 << b);
548 return x;
549}
550static __inline__ unsigned char __attribute__((__always_inline__, __nodebug__))
551_bittestandreset(long *a, long b) {
552 unsigned char x = (*a >> b) & 1;
553 *a = *a & ~(1 << b);
554 return x;
555}
556static __inline__ unsigned char __attribute__((__always_inline__, __nodebug__))
557_bittestandset(long *a, long b) {
558 unsigned char x = (*a >> b) & 1;
Warren Hunt2731e3e2013-10-01 17:12:40 +0000559 *a = *a | (1 << b);
Warren Huntd6ffae92013-09-27 23:57:26 +0000560 return x;
561}
Warren Hunt3f987942013-09-30 21:08:05 +0000562#ifdef __x86_64__
Warren Huntd6ffae92013-09-27 23:57:26 +0000563static __inline__ unsigned char __attribute__((__always_inline__, __nodebug__))
564_BitScanForward64(unsigned long *_Index, unsigned __int64 _Mask) {
565 if (!_Mask)
566 return 0;
567 *_Index = __builtin_ctzll(_Mask);
568 return 1;
569}
570static __inline__ unsigned char __attribute__((__always_inline__, __nodebug__))
571_BitScanReverse64(unsigned long *_Index, unsigned __int64 _Mask) {
572 if (!_Mask)
573 return 0;
574 *_Index = 63 - __builtin_clzll(_Mask);
575 return 1;
576}
577static
578__inline__ unsigned __int64 __attribute__((__always_inline__, __nodebug__))
579_lzcnt_u64(unsigned __int64 a) {
580 if (!a)
581 return 64;
582 return __builtin_clzll(a);
583}
584static __inline__
585unsigned __int64 __attribute__((__always_inline__, __nodebug__))
586 __popcnt64(unsigned __int64 value) {
587 return __builtin_popcountll(value);
588}
589static __inline__ unsigned char __attribute__((__always_inline__, __nodebug__))
590_bittest64(__int64 const *a, __int64 b) {
591 return (*a >> b) & 1;
592}
593static __inline__ unsigned char __attribute__((__always_inline__, __nodebug__))
594_bittestandcomplement64(__int64 *a, __int64 b) {
595 unsigned char x = (*a >> b) & 1;
596 *a = *a ^ (1ll << b);
597 return x;
598}
599static __inline__ unsigned char __attribute__((__always_inline__, __nodebug__))
600_bittestandreset64(__int64 *a, __int64 b) {
601 unsigned char x = (*a >> b) & 1;
602 *a = *a & ~(1ll << b);
603 return x;
604}
605static __inline__ unsigned char __attribute__((__always_inline__, __nodebug__))
606_bittestandset64(__int64 *a, __int64 b) {
607 unsigned char x = (*a >> b) & 1;
Warren Hunt2731e3e2013-10-01 17:12:40 +0000608 *a = *a | (1ll << b);
Warren Huntd6ffae92013-09-27 23:57:26 +0000609 return x;
610}
611#endif
612/*----------------------------------------------------------------------------*\
613|* Interlocked Exchange Add
614\*----------------------------------------------------------------------------*/
615static __inline__ char __attribute__((__always_inline__, __nodebug__))
616_InterlockedExchangeAdd8(char volatile *_Addend, char _Value) {
617 return __atomic_add_fetch(_Addend, _Value, 0) - _Value;
618}
619static __inline__ short __attribute__((__always_inline__, __nodebug__))
620_InterlockedExchangeAdd16(short volatile *_Addend, short _Value) {
621 return __atomic_add_fetch(_Addend, _Value, 0) - _Value;
622}
623static __inline__ long __attribute__((__always_inline__, __nodebug__))
624_InterlockedExchangeAdd(long volatile *_Addend, long _Value) {
625 return __atomic_add_fetch(_Addend, _Value, 0) - _Value;
626}
Warren Hunt3f987942013-09-30 21:08:05 +0000627#ifdef __x86_64__
Warren Huntd6ffae92013-09-27 23:57:26 +0000628static __inline__ __int64 __attribute__((__always_inline__, __nodebug__))
629_InterlockedExchangeAdd64(__int64 volatile *_Addend, __int64 _Value) {
630 return __atomic_add_fetch(_Addend, _Value, 0) - _Value;
631}
632#endif
633/*----------------------------------------------------------------------------*\
634|* Interlocked Exchange Sub
635\*----------------------------------------------------------------------------*/
636static __inline__ char __attribute__((__always_inline__, __nodebug__))
637_InterlockedExchangeSub8(char volatile *_Subend, char _Value) {
638 return __atomic_sub_fetch(_Subend, _Value, 0) + _Value;
639}
640static __inline__ short __attribute__((__always_inline__, __nodebug__))
641_InterlockedExchangeSub16(short volatile *_Subend, short _Value) {
642 return __atomic_sub_fetch(_Subend, _Value, 0) + _Value;
643}
644static __inline__ long __attribute__((__always_inline__, __nodebug__))
645_InterlockedExchangeSub(long volatile *_Subend, long _Value) {
646 return __atomic_sub_fetch(_Subend, _Value, 0) + _Value;
647}
Warren Hunt3f987942013-09-30 21:08:05 +0000648#ifdef __x86_64__
Warren Huntd6ffae92013-09-27 23:57:26 +0000649static __inline__ __int64 __attribute__((__always_inline__, __nodebug__))
650_InterlockedExchangeSub64(__int64 volatile *_Subend, __int64 _Value) {
651 return __atomic_sub_fetch(_Subend, _Value, 0) + _Value;
652}
653#endif
654/*----------------------------------------------------------------------------*\
655|* Interlocked Increment
656\*----------------------------------------------------------------------------*/
Hans Wennborg2ed88802014-01-23 19:15:39 +0000657static __inline__ short __attribute__((__always_inline__, __nodebug__))
658_InterlockedIncrement16(short volatile *_Value) {
Warren Huntd6ffae92013-09-27 23:57:26 +0000659 return __atomic_add_fetch(_Value, 1, 0);
660}
661static __inline__ long __attribute__((__always_inline__, __nodebug__))
662_InterlockedIncrement(long volatile *_Value) {
663 return __atomic_add_fetch(_Value, 1, 0);
664}
Warren Hunt3f987942013-09-30 21:08:05 +0000665#ifdef __x86_64__
Warren Huntd6ffae92013-09-27 23:57:26 +0000666static __inline__ __int64 __attribute__((__always_inline__, __nodebug__))
667_InterlockedIncrement64(__int64 volatile *_Value) {
668 return __atomic_add_fetch(_Value, 1, 0);
669}
670#endif
671/*----------------------------------------------------------------------------*\
672|* Interlocked Decrement
673\*----------------------------------------------------------------------------*/
Hans Wennborg2ed88802014-01-23 19:15:39 +0000674static __inline__ short __attribute__((__always_inline__, __nodebug__))
675_InterlockedDecrement16(short volatile *_Value) {
Warren Huntd6ffae92013-09-27 23:57:26 +0000676 return __atomic_sub_fetch(_Value, 1, 0);
677}
678static __inline__ long __attribute__((__always_inline__, __nodebug__))
679_InterlockedDecrement(long volatile *_Value) {
680 return __atomic_sub_fetch(_Value, 1, 0);
681}
Warren Hunt3f987942013-09-30 21:08:05 +0000682#ifdef __x86_64__
Warren Huntd6ffae92013-09-27 23:57:26 +0000683static __inline__ __int64 __attribute__((__always_inline__, __nodebug__))
684_InterlockedDecrement64(__int64 volatile *_Value) {
685 return __atomic_sub_fetch(_Value, 1, 0);
686}
687#endif
688/*----------------------------------------------------------------------------*\
689|* Interlocked And
690\*----------------------------------------------------------------------------*/
691static __inline__ char __attribute__((__always_inline__, __nodebug__))
692_InterlockedAnd8(char volatile *_Value, char _Mask) {
693 return __atomic_and_fetch(_Value, _Mask, 0);
694}
695static __inline__ short __attribute__((__always_inline__, __nodebug__))
696_InterlockedAnd16(short volatile *_Value, short _Mask) {
697 return __atomic_and_fetch(_Value, _Mask, 0);
698}
699static __inline__ long __attribute__((__always_inline__, __nodebug__))
700_InterlockedAnd(long volatile *_Value, long _Mask) {
701 return __atomic_and_fetch(_Value, _Mask, 0);
702}
Warren Hunt3f987942013-09-30 21:08:05 +0000703#ifdef __x86_64__
Warren Huntd6ffae92013-09-27 23:57:26 +0000704static __inline__ __int64 __attribute__((__always_inline__, __nodebug__))
705_InterlockedAnd64(__int64 volatile *_Value, __int64 _Mask) {
706 return __atomic_and_fetch(_Value, _Mask, 0);
707}
708#endif
709/*----------------------------------------------------------------------------*\
710|* Interlocked Or
711\*----------------------------------------------------------------------------*/
712static __inline__ char __attribute__((__always_inline__, __nodebug__))
713_InterlockedOr8(char volatile *_Value, char _Mask) {
714 return __atomic_or_fetch(_Value, _Mask, 0);
715}
716static __inline__ short __attribute__((__always_inline__, __nodebug__))
717_InterlockedOr16(short volatile *_Value, short _Mask) {
718 return __atomic_or_fetch(_Value, _Mask, 0);
719}
720static __inline__ long __attribute__((__always_inline__, __nodebug__))
721_InterlockedOr(long volatile *_Value, long _Mask) {
722 return __atomic_or_fetch(_Value, _Mask, 0);
723}
Warren Hunt3f987942013-09-30 21:08:05 +0000724#ifdef __x86_64__
Warren Huntd6ffae92013-09-27 23:57:26 +0000725static __inline__ __int64 __attribute__((__always_inline__, __nodebug__))
726_InterlockedOr64(__int64 volatile *_Value, __int64 _Mask) {
727 return __atomic_or_fetch(_Value, _Mask, 0);
728}
729#endif
730/*----------------------------------------------------------------------------*\
731|* Interlocked Xor
732\*----------------------------------------------------------------------------*/
733static __inline__ char __attribute__((__always_inline__, __nodebug__))
734_InterlockedXor8(char volatile *_Value, char _Mask) {
735 return __atomic_xor_fetch(_Value, _Mask, 0);
736}
737static __inline__ short __attribute__((__always_inline__, __nodebug__))
738_InterlockedXor16(short volatile *_Value, short _Mask) {
739 return __atomic_xor_fetch(_Value, _Mask, 0);
740}
741static __inline__ long __attribute__((__always_inline__, __nodebug__))
742_InterlockedXor(long volatile *_Value, long _Mask) {
743 return __atomic_xor_fetch(_Value, _Mask, 0);
744}
Warren Hunt3f987942013-09-30 21:08:05 +0000745#ifdef __x86_64__
Warren Huntd6ffae92013-09-27 23:57:26 +0000746static __inline__ __int64 __attribute__((__always_inline__, __nodebug__))
747_InterlockedXor64(__int64 volatile *_Value, __int64 _Mask) {
748 return __atomic_xor_fetch(_Value, _Mask, 0);
749}
750#endif
751/*----------------------------------------------------------------------------*\
752|* Interlocked Exchange
753\*----------------------------------------------------------------------------*/
754static __inline__ char __attribute__((__always_inline__, __nodebug__))
755_InterlockedExchange8(char volatile *_Target, char _Value) {
756 __atomic_exchange(_Target, &_Value, &_Value, 0);
757 return _Value;
758}
759static __inline__ short __attribute__((__always_inline__, __nodebug__))
760_InterlockedExchange16(short volatile *_Target, short _Value) {
761 __atomic_exchange(_Target, &_Value, &_Value, 0);
762 return _Value;
763}
764static __inline__ long __attribute__((__always_inline__, __nodebug__))
765_InterlockedExchange(long volatile *_Target, long _Value) {
766 __atomic_exchange(_Target, &_Value, &_Value, 0);
767 return _Value;
768}
Warren Hunt3f987942013-09-30 21:08:05 +0000769#ifdef __x86_64__
Warren Huntd6ffae92013-09-27 23:57:26 +0000770static __inline__ __int64 __attribute__((__always_inline__, __nodebug__))
771_InterlockedExchange64(__int64 volatile *_Target, __int64 _Value) {
772 __atomic_exchange(_Target, &_Value, &_Value, 0);
773 return _Value;
774}
775#endif
776/*----------------------------------------------------------------------------*\
777|* Interlocked Compare Exchange
778\*----------------------------------------------------------------------------*/
779static __inline__ char __attribute__((__always_inline__, __nodebug__))
780_InterlockedCompareExchange8(char volatile *_Destination,
781 char _Exchange, char _Comparand) {
782 __atomic_compare_exchange(_Destination, &_Comparand, &_Exchange, 0, 0, 0);
783 return _Comparand;
784}
785static __inline__ short __attribute__((__always_inline__, __nodebug__))
786_InterlockedCompareExchange16(short volatile *_Destination,
787 short _Exchange, short _Comparand) {
788 __atomic_compare_exchange(_Destination, &_Comparand, &_Exchange, 0, 0, 0);
789 return _Comparand;
790}
791static __inline__ long __attribute__((__always_inline__, __nodebug__))
792_InterlockedCompareExchange(long volatile *_Destination,
793 long _Exchange, long _Comparand) {
794 __atomic_compare_exchange(_Destination, &_Comparand, &_Exchange, 0, 0, 0);
795 return _Comparand;
796}
Warren Hunt3f987942013-09-30 21:08:05 +0000797#ifdef __x86_64__
Reid Klecknerf08d6582014-01-27 19:16:35 +0000798static __inline__ void *__attribute__((__always_inline__, __nodebug__))
799_InterlockedCompareExchangePointer(void *volatile *_Destination,
800 void *_Exchange, void *_Comparand) {
801 __atomic_compare_exchange(_Destination, &_Comparand, &_Exchange, 0, 0, 0);
802 return _Comparand;
803}
804#endif
805#ifdef __x86_64__
Warren Huntd6ffae92013-09-27 23:57:26 +0000806static __inline__ __int64 __attribute__((__always_inline__, __nodebug__))
807_InterlockedCompareExchange64(__int64 volatile *_Destination,
808 __int64 _Exchange, __int64 _Comparand) {
809 __atomic_compare_exchange(_Destination, &_Comparand, &_Exchange, 0, 0, 0);
810 return _Comparand;
811}
812#endif
813/*----------------------------------------------------------------------------*\
Reid Kleckner00d33a52013-10-17 01:29:48 +0000814|* Barriers
815\*----------------------------------------------------------------------------*/
816static __inline__ void __attribute__((__always_inline__, __nodebug__))
817__attribute__((deprecated("use other intrinsics or C++11 atomics instead")))
818_ReadWriteBarrier(void) {
819 __asm__ volatile ("" : : : "memory");
820}
821static __inline__ void __attribute__((__always_inline__, __nodebug__))
822__attribute__((deprecated("use other intrinsics or C++11 atomics instead")))
823_ReadBarrier(void) {
824 __asm__ volatile ("" : : : "memory");
825}
826static __inline__ void __attribute__((__always_inline__, __nodebug__))
827__attribute__((deprecated("use other intrinsics or C++11 atomics instead")))
828_WriteBarrier(void) {
829 __asm__ volatile ("" : : : "memory");
830}
Reid Klecknerf08d6582014-01-27 19:16:35 +0000831#ifdef __x86_64__
832static __inline__ void __attribute__((__always_inline__, __nodebug__))
833__faststorefence(void) {
834 __asm__ volatile("lock orq $0, (%%rsp)" : : : "memory");
835}
836#endif
Reid Kleckner00d33a52013-10-17 01:29:48 +0000837/*----------------------------------------------------------------------------*\
Hans Wennborg74ca0c42014-01-24 00:52:39 +0000838|* readfs
839|* (Pointers in address space #257 are relative to the FS segment register.)
840\*----------------------------------------------------------------------------*/
841#ifdef __i386__
842#define __ptr_to_addr_space(__addr_space_nbr, __type, __offset) \
843 ((volatile __type __attribute__((__address_space__(__addr_space_nbr)))*) \
844 (__offset))
845
846static __inline__ unsigned char __attribute__((__always_inline__, __nodebug__))
847__readfsbyte(unsigned long __offset) {
848 return *__ptr_to_addr_space(257, unsigned char, __offset);
849}
850static __inline__ unsigned long __attribute__((__always_inline__, __nodebug__))
851__readfsdword(unsigned long __offset) {
852 return *__ptr_to_addr_space(257, unsigned long, __offset);
853}
854static __inline__ unsigned __int64 __attribute__((__always_inline__, __nodebug__))
855__readfsqword(unsigned long __offset) {
856 return *__ptr_to_addr_space(257, unsigned __int64, __offset);
857}
858static __inline__ unsigned short __attribute__((__always_inline__, __nodebug__))
859__readfsword(unsigned long __offset) {
860 return *__ptr_to_addr_space(257, unsigned short, __offset);
861}
862#undef __ptr_to_addr_space
863#endif
864/*----------------------------------------------------------------------------*\
Warren Huntd6ffae92013-09-27 23:57:26 +0000865|* Misc
866\*----------------------------------------------------------------------------*/
867static __inline__ void * __attribute__((__always_inline__, __nodebug__))
868_AddressOfReturnAddress(void) {
869 return (void*)((char*)__builtin_frame_address(0) + sizeof(void*));
870}
871static __inline__ void * __attribute__((__always_inline__, __nodebug__))
872_ReturnAddress(void) {
873 return __builtin_return_address(0);
874}
Hans Wennborg854f7d32014-01-16 23:39:35 +0000875static __inline__ void __attribute__((__always_inline__, __nodebug__))
876__cpuid(int __info[4], int __level) {
877#if __i386__
878 __asm__ ("cpuid"
879 : "=a"(__info[0]), "=b" (__info[1]), "=c"(__info[2]), "=d"(__info[3])
880 : "0"(__level));
881#else
882 __asm__ ("cpuid" : "=a"(__info[0]), "=b" (__info[1]), "=c"(__info[2]), "=d"(__info[3])
883 : "0"(__level));
884#endif
885}
886static __inline__ unsigned __int64 __cdecl __attribute__((__always_inline__, __nodebug__))
887_xgetbv(unsigned int __xcr_no) {
888 unsigned int __eax, __edx;
889 __asm__ ("xgetbv" : "=a" (__eax), "=d" (__edx) : "c" (__xcr_no));
890 return ((unsigned __int64)__edx << 32) | __eax;
891}
Warren Huntd6ffae92013-09-27 23:57:26 +0000892
Eric Christophercc872532013-09-18 22:40:18 +0000893#ifdef __cplusplus
Eric Christopher0db88a72013-09-18 22:24:01 +0000894}
Eric Christopherd1428bf2013-08-31 00:22:48 +0000895#endif
Reid Klecknerf0e23222013-09-19 00:19:53 +0000896
897#endif /* __INTRIN_H */
898#endif /* _MSC_VER */