blob: 6a63b71605ccd3a80bec20bd3b4b2f09948dadac [file] [log] [blame]
Eric Christopherd1428bf2013-08-31 00:22:48 +00001/* ===-------- Intrin.h ---------------------------------------------------===
2 *
3 * Permission is hereby granted, free of charge, to any person obtaining a copy
4 * of this software and associated documentation files (the "Software"), to deal
5 * in the Software without restriction, including without limitation the rights
6 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7 * copies of the Software, and to permit persons to whom the Software is
8 * furnished to do so, subject to the following conditions:
9 *
10 * The above copyright notice and this permission notice shall be included in
11 * all copies or substantial portions of the Software.
12 *
13 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19 * THE SOFTWARE.
20 *
21 *===-----------------------------------------------------------------------===
22 */
23
24/* Only include this if we're compiling for the windows platform. */
25#ifndef _MSC_VER
26#include_next <Intrin.h>
27#else
28
29#ifndef __INTRIN_H
30#define __INTRIN_H
31
32/* First include the standard intrinsics. */
33#include <x86intrin.h>
34
Hans Wennborg1fd6dd32014-01-28 23:01:59 +000035/* For the definition of jmp_buf. */
36#include <setjmp.h>
37
Eric Christopher0db88a72013-09-18 22:24:01 +000038#ifdef __cplusplus
39extern "C" {
40#endif
41
Eric Christopherd1428bf2013-08-31 00:22:48 +000042/* And the random ones that aren't in those files. */
43__m64 _m_from_float(float);
44__m64 _m_from_int(int _l);
45void _m_prefetch(void *);
46float _m_to_float(__m64);
47int _m_to_int(__m64 _M);
48
49/* Other assorted instruction intrinsics. */
50void __addfsbyte(unsigned long, unsigned char);
51void __addfsdword(unsigned long, unsigned long);
52void __addfsword(unsigned long, unsigned short);
53void __code_seg(const char *);
Hans Wennborg854f7d32014-01-16 23:39:35 +000054static __inline__
Eric Christopherd1428bf2013-08-31 00:22:48 +000055void __cpuid(int[4], int);
Hans Wennborg12fb89e2014-01-31 19:44:55 +000056static __inline__
Eric Christopherd1428bf2013-08-31 00:22:48 +000057void __cpuidex(int[4], int, int);
Eric Christopher0db88a72013-09-18 22:24:01 +000058void __debugbreak(void);
Eric Christopherd1428bf2013-08-31 00:22:48 +000059__int64 __emul(int, int);
60unsigned __int64 __emulu(unsigned int, unsigned int);
Eric Christopher0db88a72013-09-18 22:24:01 +000061void __cdecl __fastfail(unsigned int);
Eric Christopherd1428bf2013-08-31 00:22:48 +000062unsigned int __getcallerseflags(void);
Hans Wennborg740a4d62014-01-28 22:55:01 +000063static __inline__
Eric Christopherd1428bf2013-08-31 00:22:48 +000064void __halt(void);
65unsigned char __inbyte(unsigned short);
66void __inbytestring(unsigned short, unsigned char *, unsigned long);
67void __incfsbyte(unsigned long);
68void __incfsdword(unsigned long);
69void __incfsword(unsigned long);
70unsigned long __indword(unsigned short);
71void __indwordstring(unsigned short, unsigned long *, unsigned long);
72void __int2c(void);
73void __invlpg(void *);
74unsigned short __inword(unsigned short);
75void __inwordstring(unsigned short, unsigned short *, unsigned long);
76void __lidt(void *);
77unsigned __int64 __ll_lshift(unsigned __int64, int);
78__int64 __ll_rshift(__int64, int);
79void __llwpcb(void *);
80unsigned char __lwpins32(unsigned int, unsigned int, unsigned int);
81void __lwpval32(unsigned int, unsigned int, unsigned int);
82unsigned int __lzcnt(unsigned int);
83unsigned short __lzcnt16(unsigned short);
Hans Wennborgd9be72e2014-03-12 22:00:32 +000084static __inline__
Eric Christopherd1428bf2013-08-31 00:22:48 +000085void __movsb(unsigned char *, unsigned char const *, size_t);
Hans Wennborgd9be72e2014-03-12 22:00:32 +000086static __inline__
Eric Christopherd1428bf2013-08-31 00:22:48 +000087void __movsd(unsigned long *, unsigned long const *, size_t);
Hans Wennborgd9be72e2014-03-12 22:00:32 +000088static __inline__
Eric Christopherd1428bf2013-08-31 00:22:48 +000089void __movsw(unsigned short *, unsigned short const *, size_t);
90void __nop(void);
91void __nvreg_restore_fence(void);
92void __nvreg_save_fence(void);
93void __outbyte(unsigned short, unsigned char);
94void __outbytestring(unsigned short, unsigned char *, unsigned long);
95void __outdword(unsigned short, unsigned long);
96void __outdwordstring(unsigned short, unsigned long *, unsigned long);
97void __outword(unsigned short, unsigned short);
98void __outwordstring(unsigned short, unsigned short *, unsigned long);
Warren Huntd6ffae92013-09-27 23:57:26 +000099static __inline__
Eric Christopherd1428bf2013-08-31 00:22:48 +0000100unsigned int __popcnt(unsigned int);
Warren Huntd6ffae92013-09-27 23:57:26 +0000101static __inline__
Eric Christopherd1428bf2013-08-31 00:22:48 +0000102unsigned short __popcnt16(unsigned short);
Hans Wennborg740a4d62014-01-28 22:55:01 +0000103static __inline__
Eric Christopherd1428bf2013-08-31 00:22:48 +0000104unsigned __int64 __rdtsc(void);
105unsigned __int64 __rdtscp(unsigned int *);
106unsigned long __readcr0(void);
107unsigned long __readcr2(void);
Reid Kleckner592dc612014-04-08 00:28:22 +0000108static __inline__
Eric Christopherd1428bf2013-08-31 00:22:48 +0000109unsigned long __readcr3(void);
Eric Christopher439137e2014-01-24 12:13:47 +0000110unsigned long __readcr4(void);
Eric Christopherd1428bf2013-08-31 00:22:48 +0000111unsigned long __readcr8(void);
112unsigned int __readdr(unsigned int);
Hans Wennborg74ca0c42014-01-24 00:52:39 +0000113#ifdef __i386__
114static __inline__
Eric Christopherd1428bf2013-08-31 00:22:48 +0000115unsigned char __readfsbyte(unsigned long);
Hans Wennborg74ca0c42014-01-24 00:52:39 +0000116static __inline__
Eric Christopherd1428bf2013-08-31 00:22:48 +0000117unsigned long __readfsdword(unsigned long);
Hans Wennborg74ca0c42014-01-24 00:52:39 +0000118static __inline__
Eric Christopherd1428bf2013-08-31 00:22:48 +0000119unsigned __int64 __readfsqword(unsigned long);
Hans Wennborg74ca0c42014-01-24 00:52:39 +0000120static __inline__
Eric Christopherd1428bf2013-08-31 00:22:48 +0000121unsigned short __readfsword(unsigned long);
Hans Wennborg74ca0c42014-01-24 00:52:39 +0000122#endif
Reid Kleckner592dc612014-04-08 00:28:22 +0000123static __inline__
Eric Christopherd1428bf2013-08-31 00:22:48 +0000124unsigned __int64 __readmsr(unsigned long);
125unsigned __int64 __readpmc(unsigned long);
126unsigned long __segmentlimit(unsigned long);
127void __sidt(void *);
128void *__slwpcb(void);
Hans Wennborgd9be72e2014-03-12 22:00:32 +0000129static __inline__
Eric Christopherd1428bf2013-08-31 00:22:48 +0000130void __stosb(unsigned char *, unsigned char, size_t);
Hans Wennborgd9be72e2014-03-12 22:00:32 +0000131static __inline__
Eric Christopherd1428bf2013-08-31 00:22:48 +0000132void __stosd(unsigned long *, unsigned long, size_t);
Hans Wennborgd9be72e2014-03-12 22:00:32 +0000133static __inline__
Eric Christopherd1428bf2013-08-31 00:22:48 +0000134void __stosw(unsigned short *, unsigned short, size_t);
135void __svm_clgi(void);
136void __svm_invlpga(void *, int);
137void __svm_skinit(int);
138void __svm_stgi(void);
139void __svm_vmload(size_t);
140void __svm_vmrun(size_t);
141void __svm_vmsave(size_t);
142void __ud2(void);
143unsigned __int64 __ull_rshift(unsigned __int64, int);
144void __vmx_off(void);
145void __vmx_vmptrst(unsigned __int64 *);
146void __wbinvd(void);
147void __writecr0(unsigned int);
Reid Kleckner592dc612014-04-08 00:28:22 +0000148static __inline__
Eric Christopherd1428bf2013-08-31 00:22:48 +0000149void __writecr3(unsigned int);
150void __writecr4(unsigned int);
151void __writecr8(unsigned int);
152void __writedr(unsigned int, unsigned int);
Eric Christopherd1428bf2013-08-31 00:22:48 +0000153void __writefsbyte(unsigned long, unsigned char);
154void __writefsdword(unsigned long, unsigned long);
155void __writefsqword(unsigned long, unsigned __int64);
156void __writefsword(unsigned long, unsigned short);
157void __writemsr(unsigned long, unsigned __int64);
Warren Huntd6ffae92013-09-27 23:57:26 +0000158static __inline__
Eric Christopherd1428bf2013-08-31 00:22:48 +0000159void *_AddressOfReturnAddress(void);
160unsigned int _andn_u32(unsigned int, unsigned int);
161unsigned int _bextr_u32(unsigned int, unsigned int, unsigned int);
Eric Christopherd1428bf2013-08-31 00:22:48 +0000162unsigned int _bextri_u32(unsigned int, unsigned int);
Warren Huntd6ffae92013-09-27 23:57:26 +0000163static __inline__
Eric Christopherd1428bf2013-08-31 00:22:48 +0000164unsigned char _BitScanForward(unsigned long *_Index, unsigned long _Mask);
Warren Huntd6ffae92013-09-27 23:57:26 +0000165static __inline__
Eric Christopherd1428bf2013-08-31 00:22:48 +0000166unsigned char _BitScanReverse(unsigned long *_Index, unsigned long _Mask);
Warren Huntd6ffae92013-09-27 23:57:26 +0000167static __inline__
Eric Christopherd1428bf2013-08-31 00:22:48 +0000168unsigned char _bittest(long const *, long);
Warren Huntd6ffae92013-09-27 23:57:26 +0000169static __inline__
Eric Christopherd1428bf2013-08-31 00:22:48 +0000170unsigned char _bittestandcomplement(long *, long);
Warren Huntd6ffae92013-09-27 23:57:26 +0000171static __inline__
Eric Christopherd1428bf2013-08-31 00:22:48 +0000172unsigned char _bittestandreset(long *, long);
Warren Huntd6ffae92013-09-27 23:57:26 +0000173static __inline__
Eric Christopherd1428bf2013-08-31 00:22:48 +0000174unsigned char _bittestandset(long *, long);
175unsigned int _blcfill_u32(unsigned int);
176unsigned int _blci_u32(unsigned int);
177unsigned int _blcic_u32(unsigned int);
178unsigned int _blcmsk_u32(unsigned int);
179unsigned int _blcs_u32(unsigned int);
180unsigned int _blsfill_u32(unsigned int);
181unsigned int _blsi_u32(unsigned int);
182unsigned int _blsic_u32(unsigned int);
183unsigned int _blsmsk_u32(unsigned int);
Eric Christopherd1428bf2013-08-31 00:22:48 +0000184unsigned int _blsr_u32(unsigned int);
185unsigned __int64 __cdecl _byteswap_uint64(unsigned __int64);
186unsigned long __cdecl _byteswap_ulong(unsigned long);
187unsigned short __cdecl _byteswap_ushort(unsigned short);
188unsigned _bzhi_u32(unsigned int, unsigned int);
189void __cdecl _disable(void);
190void __cdecl _enable(void);
191void __cdecl _fxrstor(void const *);
192void __cdecl _fxsave(void *);
193long _InterlockedAddLargeStatistic(__int64 volatile *_Addend, long _Value);
Warren Huntd6ffae92013-09-27 23:57:26 +0000194static __inline__
Eric Christopherd1428bf2013-08-31 00:22:48 +0000195long _InterlockedAnd(long volatile *_Value, long _Mask);
Warren Huntd6ffae92013-09-27 23:57:26 +0000196static __inline__
Eric Christopherd1428bf2013-08-31 00:22:48 +0000197short _InterlockedAnd16(short volatile *_Value, short _Mask);
Warren Huntd6ffae92013-09-27 23:57:26 +0000198static __inline__
Eric Christopherd1428bf2013-08-31 00:22:48 +0000199char _InterlockedAnd8(char volatile *_Value, char _Mask);
200unsigned char _interlockedbittestandreset(long volatile *, long);
Hans Wennborga3169332014-03-13 17:05:09 +0000201static __inline__
Eric Christopherd1428bf2013-08-31 00:22:48 +0000202unsigned char _interlockedbittestandset(long volatile *, long);
Warren Huntd6ffae92013-09-27 23:57:26 +0000203static __inline__
Eric Christopherd1428bf2013-08-31 00:22:48 +0000204long __cdecl _InterlockedCompareExchange(long volatile *_Destination,
205 long _Exchange, long _Comparand);
206long _InterlockedCompareExchange_HLEAcquire(long volatile *, long, long);
207long _InterlockedCompareExchange_HLERelease(long volatile *, long, long);
Warren Huntd6ffae92013-09-27 23:57:26 +0000208static __inline__
Eric Christopherd1428bf2013-08-31 00:22:48 +0000209short _InterlockedCompareExchange16(short volatile *_Destination,
210 short _Exchange, short _Comparand);
Warren Huntd6ffae92013-09-27 23:57:26 +0000211static __inline__
Eric Christopherd1428bf2013-08-31 00:22:48 +0000212__int64 _InterlockedCompareExchange64(__int64 volatile *_Destination,
213 __int64 _Exchange, __int64 _Comparand);
214__int64 _InterlockedcompareExchange64_HLEAcquire(__int64 volatile *, __int64,
215 __int64);
216__int64 _InterlockedCompareExchange64_HLERelease(__int64 volatile *, __int64,
217 __int64);
Warren Huntd6ffae92013-09-27 23:57:26 +0000218static __inline__
Eric Christopherd1428bf2013-08-31 00:22:48 +0000219char _InterlockedCompareExchange8(char volatile *_Destination, char _Exchange,
220 char _Comparand);
221void *_InterlockedCompareExchangePointer_HLEAcquire(void *volatile *, void *,
222 void *);
223void *_InterlockedCompareExchangePointer_HLERelease(void *volatile *, void *,
224 void *);
Warren Huntd6ffae92013-09-27 23:57:26 +0000225static __inline__
Eric Christopherd1428bf2013-08-31 00:22:48 +0000226long __cdecl _InterlockedDecrement(long volatile *_Addend);
Warren Huntd6ffae92013-09-27 23:57:26 +0000227static __inline__
Eric Christopherd1428bf2013-08-31 00:22:48 +0000228short _InterlockedDecrement16(short volatile *_Addend);
Warren Huntd6ffae92013-09-27 23:57:26 +0000229static __inline__
Eric Christopherd1428bf2013-08-31 00:22:48 +0000230long __cdecl _InterlockedExchange(long volatile *_Target, long _Value);
Warren Huntd6ffae92013-09-27 23:57:26 +0000231static __inline__
Eric Christopherd1428bf2013-08-31 00:22:48 +0000232short _InterlockedExchange16(short volatile *_Target, short _Value);
Warren Huntd6ffae92013-09-27 23:57:26 +0000233static __inline__
Eric Christopherd1428bf2013-08-31 00:22:48 +0000234char _InterlockedExchange8(char volatile *_Target, char _Value);
Warren Huntd6ffae92013-09-27 23:57:26 +0000235static __inline__
Eric Christopherd1428bf2013-08-31 00:22:48 +0000236long __cdecl _InterlockedExchangeAdd(long volatile *_Addend, long _Value);
237long _InterlockedExchangeAdd_HLEAcquire(long volatile *, long);
238long _InterlockedExchangeAdd_HLERelease(long volatile *, long);
Reid Kleckner924eb2a2014-01-27 18:48:02 +0000239static __inline__
Eric Christopher439137e2014-01-24 12:13:47 +0000240short _InterlockedExchangeAdd16(short volatile *_Addend, short _Value);
241__int64 _InterlockedExchangeAdd64_HLEAcquire(__int64 volatile *, __int64);
242__int64 _InterlockedExchangeAdd64_HLERelease(__int64 volatile *, __int64);
Warren Huntd6ffae92013-09-27 23:57:26 +0000243static __inline__
Eric Christopherd1428bf2013-08-31 00:22:48 +0000244char _InterlockedExchangeAdd8(char volatile *_Addend, char _Value);
Warren Huntd6ffae92013-09-27 23:57:26 +0000245static __inline__
Eric Christopherd1428bf2013-08-31 00:22:48 +0000246long __cdecl _InterlockedIncrement(long volatile *_Addend);
Warren Huntd6ffae92013-09-27 23:57:26 +0000247static __inline__
Eric Christopherd1428bf2013-08-31 00:22:48 +0000248short _InterlockedIncrement16(short volatile *_Addend);
Warren Huntd6ffae92013-09-27 23:57:26 +0000249static __inline__
Eric Christopherd1428bf2013-08-31 00:22:48 +0000250long _InterlockedOr(long volatile *_Value, long _Mask);
Warren Huntd6ffae92013-09-27 23:57:26 +0000251static __inline__
Eric Christopherd1428bf2013-08-31 00:22:48 +0000252short _InterlockedOr16(short volatile *_Value, short _Mask);
Warren Huntd6ffae92013-09-27 23:57:26 +0000253static __inline__
Eric Christopherd1428bf2013-08-31 00:22:48 +0000254char _InterlockedOr8(char volatile *_Value, char _Mask);
Warren Huntd6ffae92013-09-27 23:57:26 +0000255static __inline__
Eric Christopherd1428bf2013-08-31 00:22:48 +0000256long _InterlockedXor(long volatile *_Value, long _Mask);
Warren Huntd6ffae92013-09-27 23:57:26 +0000257static __inline__
Eric Christopherd1428bf2013-08-31 00:22:48 +0000258short _InterlockedXor16(short volatile *_Value, short _Mask);
Warren Huntd6ffae92013-09-27 23:57:26 +0000259static __inline__
Eric Christopherd1428bf2013-08-31 00:22:48 +0000260char _InterlockedXor8(char volatile *_Value, char _Mask);
261void __cdecl _invpcid(unsigned int, void *);
Warren Huntd6ffae92013-09-27 23:57:26 +0000262static __inline__
Eric Christopherd1428bf2013-08-31 00:22:48 +0000263unsigned long __cdecl _lrotl(unsigned long, int);
Warren Huntd6ffae92013-09-27 23:57:26 +0000264static __inline__
Eric Christopherd1428bf2013-08-31 00:22:48 +0000265unsigned long __cdecl _lrotr(unsigned long, int);
Warren Huntd6ffae92013-09-27 23:57:26 +0000266static __inline__
Eric Christopherd1428bf2013-08-31 00:22:48 +0000267unsigned int _lzcnt_u32(unsigned int);
Reid Kleckner00d33a52013-10-17 01:29:48 +0000268static __inline__
Eric Christopherd1428bf2013-08-31 00:22:48 +0000269void _ReadBarrier(void);
Reid Kleckner00d33a52013-10-17 01:29:48 +0000270static __inline__
Eric Christopherd1428bf2013-08-31 00:22:48 +0000271void _ReadWriteBarrier(void);
Warren Huntd6ffae92013-09-27 23:57:26 +0000272static __inline__
Eric Christopherd1428bf2013-08-31 00:22:48 +0000273void *_ReturnAddress(void);
274unsigned int _rorx_u32(unsigned int, const unsigned int);
275int __cdecl _rdrand16_step(unsigned short *);
276int __cdecl _rdrand32_step(unsigned int *);
Warren Huntd6ffae92013-09-27 23:57:26 +0000277static __inline__
Eric Christopherfb4b4332013-08-31 00:27:38 +0000278unsigned int __cdecl _rotl(unsigned int _Value, int _Shift);
Warren Huntd6ffae92013-09-27 23:57:26 +0000279static __inline__
Eric Christopherd1428bf2013-08-31 00:22:48 +0000280unsigned short _rotl16(unsigned short _Value, unsigned char _Shift);
Warren Huntd6ffae92013-09-27 23:57:26 +0000281static __inline__
Eric Christopherd1428bf2013-08-31 00:22:48 +0000282unsigned __int64 __cdecl _rotl64(unsigned __int64 _Value, int _Shift);
Warren Huntd6ffae92013-09-27 23:57:26 +0000283static __inline__
Eric Christopherd1428bf2013-08-31 00:22:48 +0000284unsigned char _rotl8(unsigned char _Value, unsigned char _Shift);
Warren Huntd6ffae92013-09-27 23:57:26 +0000285static __inline__
Eric Christopherd1428bf2013-08-31 00:22:48 +0000286unsigned int __cdecl _rotr(unsigned int _Value, int _Shift);
Warren Huntd6ffae92013-09-27 23:57:26 +0000287static __inline__
Eric Christopherd1428bf2013-08-31 00:22:48 +0000288unsigned short _rotr16(unsigned short _Value, unsigned char _Shift);
Warren Huntd6ffae92013-09-27 23:57:26 +0000289static __inline__
Eric Christopherd1428bf2013-08-31 00:22:48 +0000290unsigned __int64 __cdecl _rotr64(unsigned __int64 _Value, int _Shift);
Warren Huntd6ffae92013-09-27 23:57:26 +0000291static __inline__
Eric Christopherd1428bf2013-08-31 00:22:48 +0000292unsigned char _rotr8(unsigned char _Value, unsigned char _Shift);
293int _sarx_i32(int, unsigned int);
Hans Wennborg1fd6dd32014-01-28 23:01:59 +0000294int __cdecl _setjmp(jmp_buf);
Eric Christopherd1428bf2013-08-31 00:22:48 +0000295unsigned int _shlx_u32(unsigned int, unsigned int);
296unsigned int _shrx_u32(unsigned int, unsigned int);
297void _Store_HLERelease(long volatile *, long);
298void _Store64_HLERelease(__int64 volatile *, __int64);
299void _StorePointer_HLERelease(void *volatile *, void *);
300unsigned int _t1mskc_u32(unsigned int);
301unsigned int _tzcnt_u32(unsigned int);
Eric Christopherd1428bf2013-08-31 00:22:48 +0000302unsigned int _tzmsk_u32(unsigned int);
Reid Kleckner00d33a52013-10-17 01:29:48 +0000303static __inline__
Eric Christopherd1428bf2013-08-31 00:22:48 +0000304void _WriteBarrier(void);
305void _xabort(const unsigned int imm);
306unsigned __int32 xbegin(void);
307void _xend(void);
Hans Wennborg854f7d32014-01-16 23:39:35 +0000308static __inline__
Eric Christopherd1428bf2013-08-31 00:22:48 +0000309unsigned __int64 __cdecl _xgetbv(unsigned int);
310void __cdecl _xrstor(void const *, unsigned __int64);
311void __cdecl _xsave(void *, unsigned __int64);
312void __cdecl _xsaveopt(void *, unsigned __int64);
313void __cdecl _xsetbv(unsigned int, unsigned __int64);
314unsigned char _xtest(void);
315
316/* These additional intrinsics are turned on in x64/amd64/x86_64 mode. */
Warren Hunt3f987942013-09-30 21:08:05 +0000317#ifdef __x86_64__
Eric Christopherd1428bf2013-08-31 00:22:48 +0000318void __addgsbyte(unsigned long, unsigned char);
319void __addgsdword(unsigned long, unsigned long);
320void __addgsqword(unsigned long, unsigned __int64);
321void __addgsword(unsigned long, unsigned short);
Reid Klecknerf08d6582014-01-27 19:16:35 +0000322static __inline__
Eric Christopherd1428bf2013-08-31 00:22:48 +0000323void __faststorefence(void);
324void __incgsbyte(unsigned long);
325void __incgsdword(unsigned long);
326void __incgsqword(unsigned long);
327void __incgsword(unsigned long);
Eric Christopher439137e2014-01-24 12:13:47 +0000328unsigned char __lwpins64(unsigned __int64, unsigned int, unsigned int);
329void __lwpval64(unsigned __int64, unsigned int, unsigned int);
330unsigned __int64 __lzcnt64(unsigned __int64);
Hans Wennborgd9be72e2014-03-12 22:00:32 +0000331static __inline__
Eric Christopher439137e2014-01-24 12:13:47 +0000332void __movsq(unsigned long long *, unsigned long long const *, size_t);
333__int64 __mulh(__int64, __int64);
Reid Kleckner924eb2a2014-01-27 18:48:02 +0000334static __inline__
Eric Christopherd1428bf2013-08-31 00:22:48 +0000335unsigned __int64 __popcnt64(unsigned __int64);
Hans Wennborga4421e02014-03-12 21:09:05 +0000336static __inline__
Eric Christopher439137e2014-01-24 12:13:47 +0000337unsigned char __readgsbyte(unsigned long);
Hans Wennborga4421e02014-03-12 21:09:05 +0000338static __inline__
Eric Christopher439137e2014-01-24 12:13:47 +0000339unsigned long __readgsdword(unsigned long);
Hans Wennborga4421e02014-03-12 21:09:05 +0000340static __inline__
341unsigned __int64 __readgsqword(unsigned long);
Eric Christopher439137e2014-01-24 12:13:47 +0000342unsigned short __readgsword(unsigned long);
Eric Christopherd1428bf2013-08-31 00:22:48 +0000343unsigned __int64 __shiftleft128(unsigned __int64 _LowPart,
344 unsigned __int64 _HighPart,
345 unsigned char _Shift);
346unsigned __int64 __shiftright128(unsigned __int64 _LowPart,
347 unsigned __int64 _HighPart,
348 unsigned char _Shift);
Hans Wennborgd9be72e2014-03-12 22:00:32 +0000349static __inline__
Eric Christopherd1428bf2013-08-31 00:22:48 +0000350void __stosq(unsigned __int64 *, unsigned __int64, size_t);
Eric Christopher439137e2014-01-24 12:13:47 +0000351unsigned __int64 __umulh(unsigned __int64, unsigned __int64);
352unsigned char __vmx_on(unsigned __int64 *);
353unsigned char __vmx_vmclear(unsigned __int64 *);
354unsigned char __vmx_vmlaunch(void);
355unsigned char __vmx_vmptrld(unsigned __int64 *);
356unsigned char __vmx_vmread(size_t, size_t *);
357unsigned char __vmx_vmresume(void);
358unsigned char __vmx_vmwrite(size_t, size_t);
359void __writegsbyte(unsigned long, unsigned char);
360void __writegsdword(unsigned long, unsigned long);
361void __writegsqword(unsigned long, unsigned __int64);
362void __writegsword(unsigned long, unsigned short);
Eric Christopherd1428bf2013-08-31 00:22:48 +0000363unsigned __int64 _andn_u64(unsigned __int64, unsigned __int64);
364unsigned __int64 _bextr_u64(unsigned __int64, unsigned int, unsigned int);
365unsigned __int64 _bextri_u64(unsigned __int64, unsigned int);
Warren Huntd6ffae92013-09-27 23:57:26 +0000366static __inline__
Eric Christopherd1428bf2013-08-31 00:22:48 +0000367unsigned char _BitScanForward64(unsigned long *_Index, unsigned __int64 _Mask);
Warren Huntd6ffae92013-09-27 23:57:26 +0000368static __inline__
Eric Christopherd1428bf2013-08-31 00:22:48 +0000369unsigned char _BitScanReverse64(unsigned long *_Index, unsigned __int64 _Mask);
Warren Huntd6ffae92013-09-27 23:57:26 +0000370static __inline__
Eric Christopherd1428bf2013-08-31 00:22:48 +0000371unsigned char _bittest64(__int64 const *, __int64);
Warren Huntd6ffae92013-09-27 23:57:26 +0000372static __inline__
Eric Christopherd1428bf2013-08-31 00:22:48 +0000373unsigned char _bittestandcomplement64(__int64 *, __int64);
Warren Huntd6ffae92013-09-27 23:57:26 +0000374static __inline__
Eric Christopherd1428bf2013-08-31 00:22:48 +0000375unsigned char _bittestandreset64(__int64 *, __int64);
Warren Huntd6ffae92013-09-27 23:57:26 +0000376static __inline__
Eric Christopherd1428bf2013-08-31 00:22:48 +0000377unsigned char _bittestandset64(__int64 *, __int64);
378unsigned __int64 _blcfill_u64(unsigned __int64);
379unsigned __int64 _blci_u64(unsigned __int64);
380unsigned __int64 _blcic_u64(unsigned __int64);
381unsigned __int64 _blcmsk_u64(unsigned __int64);
382unsigned __int64 _blcs_u64(unsigned __int64);
383unsigned __int64 _blsfill_u64(unsigned __int64);
384unsigned __int64 _blsi_u64(unsigned __int64);
385unsigned __int64 _blsic_u64(unsigned __int64);
Eric Christopher439137e2014-01-24 12:13:47 +0000386unsigned __int64 _blsmsk_u64(unsigned __int64);
Eric Christopherd1428bf2013-08-31 00:22:48 +0000387unsigned __int64 _blsr_u64(unsigned __int64);
388unsigned __int64 __cdecl _byteswap_uint64(unsigned __int64);
389unsigned __int64 _bzhi_u64(unsigned __int64, unsigned int);
390void __cdecl _fxrstor64(void const *);
391void __cdecl _fxsave64(void *);
392long _InterlockedAnd_np(long volatile *_Value, long _Mask);
393short _InterlockedAnd16_np(short volatile *_Value, short _Mask);
394__int64 _InterlockedAnd64_np(__int64 volatile *_Value, __int64 _Mask);
395char _InterlockedAnd8_np(char volatile *_Value, char _Mask);
396unsigned char _interlockedbittestandreset64(__int64 volatile *, __int64);
Hans Wennborga3169332014-03-13 17:05:09 +0000397static __inline__
Eric Christopherd1428bf2013-08-31 00:22:48 +0000398unsigned char _interlockedbittestandset64(__int64 volatile *, __int64);
399long _InterlockedCompareExchange_np(long volatile *_Destination, long _Exchange,
400 long _Comparand);
401unsigned char _InterlockedCompareExchange128(__int64 volatile *_Destination,
402 __int64 _ExchangeHigh,
403 __int64 _ExchangeLow,
404 __int64 *_CompareandResult);
405unsigned char _InterlockedCompareExchange128_np(__int64 volatile *_Destination,
406 __int64 _ExchangeHigh,
407 __int64 _ExchangeLow,
408 __int64 *_ComparandResult);
409short _InterlockedCompareExchange16_np(short volatile *_Destination,
410 short _Exchange, short _Comparand);
Eric Christopher439137e2014-01-24 12:13:47 +0000411__int64 _InterlockedCompareExchange64_HLEAcquire(__int64 volatile *, __int64,
412 __int64);
413__int64 _InterlockedCompareExchange64_HLERelease(__int64 volatile *, __int64,
414 __int64);
Eric Christopherd1428bf2013-08-31 00:22:48 +0000415__int64 _InterlockedCompareExchange64_np(__int64 volatile *_Destination,
416 __int64 _Exchange, __int64 _Comparand);
Reid Klecknerf08d6582014-01-27 19:16:35 +0000417static __inline__
Eric Christopher58b40432014-01-25 01:38:30 +0000418void *_InterlockedCompareExchangePointer(void *volatile *_Destination,
419 void *_Exchange, void *_Comparand);
Eric Christopherd1428bf2013-08-31 00:22:48 +0000420void *_InterlockedCompareExchangePointer_np(void *volatile *_Destination,
421 void *_Exchange, void *_Comparand);
Reid Kleckner924eb2a2014-01-27 18:48:02 +0000422static __inline__
Eric Christopher439137e2014-01-24 12:13:47 +0000423__int64 _InterlockedDecrement64(__int64 volatile *_Addend);
Reid Kleckner924eb2a2014-01-27 18:48:02 +0000424static __inline__
Eric Christopher439137e2014-01-24 12:13:47 +0000425__int64 _InterlockedExchange64(__int64 volatile *_Target, __int64 _Value);
Reid Kleckner924eb2a2014-01-27 18:48:02 +0000426static __inline__
Eric Christopher439137e2014-01-24 12:13:47 +0000427__int64 _InterlockedExchangeAdd64(__int64 volatile *_Addend, __int64 _Value);
Reid Kleckner7dd8bc02014-03-26 16:09:48 +0000428static __inline__
Eric Christopher439137e2014-01-24 12:13:47 +0000429void *_InterlockedExchangePointer(void *volatile *_Target, void *_Value);
Reid Kleckner924eb2a2014-01-27 18:48:02 +0000430static __inline__
Eric Christopher439137e2014-01-24 12:13:47 +0000431__int64 _InterlockedIncrement64(__int64 volatile *_Addend);
Eric Christopherd1428bf2013-08-31 00:22:48 +0000432long _InterlockedOr_np(long volatile *_Value, long _Mask);
433short _InterlockedOr16_np(short volatile *_Value, short _Mask);
Reid Kleckner924eb2a2014-01-27 18:48:02 +0000434static __inline__
Eric Christopher439137e2014-01-24 12:13:47 +0000435__int64 _InterlockedOr64(__int64 volatile *_Value, __int64 _Mask);
Warren Hunt41a993f2013-09-28 00:15:41 +0000436__int64 _InterlockedOr64_np(__int64 volatile *_Value, __int64 _Mask);
Eric Christopherd1428bf2013-08-31 00:22:48 +0000437char _InterlockedOr8_np(char volatile *_Value, char _Mask);
438long _InterlockedXor_np(long volatile *_Value, long _Mask);
439short _InterlockedXor16_np(short volatile *_Value, short _Mask);
Reid Kleckner924eb2a2014-01-27 18:48:02 +0000440static __inline__
Eric Christopher439137e2014-01-24 12:13:47 +0000441__int64 _InterlockedXor64(__int64 volatile *_Value, __int64 _Mask);
Eric Christopherd1428bf2013-08-31 00:22:48 +0000442__int64 _InterlockedXor64_np(__int64 volatile *_Value, __int64 _Mask);
443char _InterlockedXor8_np(char volatile *_Value, char _Mask);
Reid Kleckner924eb2a2014-01-27 18:48:02 +0000444static __inline__
Eric Christopherd1428bf2013-08-31 00:22:48 +0000445unsigned __int64 _lzcnt_u64(unsigned __int64);
446__int64 _mul128(__int64 _Multiplier, __int64 _Multiplicand,
447 __int64 *_HighProduct);
448unsigned int __cdecl _readfsbase_u32(void);
449unsigned __int64 __cdecl _readfsbase_u64(void);
450unsigned int __cdecl _readgsbase_u32(void);
451unsigned __int64 __cdecl _readgsbase_u64(void);
452unsigned __int64 _rorx_u64(unsigned __int64, const unsigned int);
Eric Christopher439137e2014-01-24 12:13:47 +0000453__int64 _sarx_i64(__int64, unsigned int);
Reid Kleckner33630902014-01-27 19:32:42 +0000454/* FIXME: Need definition for jmp_buf.
455 int __cdecl _setjmpex(jmp_buf); */
Eric Christopher439137e2014-01-24 12:13:47 +0000456unsigned __int64 _shlx_u64(unsigned __int64, unsigned int);
457unsigned __int64 shrx_u64(unsigned __int64, unsigned int);
Eric Christopherd1428bf2013-08-31 00:22:48 +0000458unsigned __int64 _tzcnt_u64(unsigned __int64);
459unsigned __int64 _tzmsk_u64(unsigned __int64);
460unsigned __int64 _umul128(unsigned __int64 _Multiplier,
461 unsigned __int64 _Multiplicand,
462 unsigned __int64 *_HighProduct);
463void __cdecl _writefsbase_u32(unsigned int);
464void _cdecl _writefsbase_u64(unsigned __int64);
465void __cdecl _writegsbase_u32(unsigned int);
466void __cdecl _writegsbase_u64(unsigned __int64);
467void __cdecl _xrstor64(void const *, unsigned __int64);
468void __cdecl _xsave64(void *, unsigned __int64);
469void __cdecl _xsaveopt64(void *, unsigned __int64);
Eric Christopher0db88a72013-09-18 22:24:01 +0000470
Warren Hunt3f987942013-09-30 21:08:05 +0000471#endif /* __x86_64__ */
Reid Klecknerf0e23222013-09-19 00:19:53 +0000472
Warren Huntd6ffae92013-09-27 23:57:26 +0000473/*----------------------------------------------------------------------------*\
474|* Bit Twiddling
475\*----------------------------------------------------------------------------*/
476static __inline__ unsigned char __attribute__((__always_inline__, __nodebug__))
477_rotl8(unsigned char _Value, unsigned char _Shift) {
478 _Shift &= 0x7;
479 return _Shift ? (_Value << _Shift) | (_Value >> (8 - _Shift)) : _Value;
480}
481static __inline__ unsigned char __attribute__((__always_inline__, __nodebug__))
482_rotr8(unsigned char _Value, unsigned char _Shift) {
483 _Shift &= 0x7;
484 return _Shift ? (_Value >> _Shift) | (_Value << (8 - _Shift)) : _Value;
485}
486static __inline__ unsigned short __attribute__((__always_inline__, __nodebug__))
487_rotl16(unsigned short _Value, unsigned char _Shift) {
488 _Shift &= 0xf;
489 return _Shift ? (_Value << _Shift) | (_Value >> (16 - _Shift)) : _Value;
490}
491static __inline__ unsigned short __attribute__((__always_inline__, __nodebug__))
492_rotr16(unsigned short _Value, unsigned char _Shift) {
493 _Shift &= 0xf;
494 return _Shift ? (_Value >> _Shift) | (_Value << (16 - _Shift)) : _Value;
495}
496static __inline__ unsigned int __attribute__((__always_inline__, __nodebug__))
497_rotl(unsigned int _Value, int _Shift) {
498 _Shift &= 0x1f;
499 return _Shift ? (_Value << _Shift) | (_Value >> (32 - _Shift)) : _Value;
500}
501static __inline__ unsigned int __attribute__((__always_inline__, __nodebug__))
502_rotr(unsigned int _Value, int _Shift) {
503 _Shift &= 0x1f;
504 return _Shift ? (_Value >> _Shift) | (_Value << (32 - _Shift)) : _Value;
505}
506static __inline__ unsigned long __attribute__((__always_inline__, __nodebug__))
507_lrotl(unsigned long _Value, int _Shift) {
508 _Shift &= 0x1f;
509 return _Shift ? (_Value << _Shift) | (_Value >> (32 - _Shift)) : _Value;
510}
511static __inline__ unsigned long __attribute__((__always_inline__, __nodebug__))
512_lrotr(unsigned long _Value, int _Shift) {
513 _Shift &= 0x1f;
514 return _Shift ? (_Value >> _Shift) | (_Value << (32 - _Shift)) : _Value;
515}
516static
517__inline__ unsigned __int64 __attribute__((__always_inline__, __nodebug__))
518_rotl64(unsigned __int64 _Value, int _Shift) {
519 _Shift &= 0x3f;
520 return _Shift ? (_Value << _Shift) | (_Value >> (64 - _Shift)) : _Value;
521}
522static
523__inline__ unsigned __int64 __attribute__((__always_inline__, __nodebug__))
524_rotr64(unsigned __int64 _Value, int _Shift) {
525 _Shift &= 0x3f;
526 return _Shift ? (_Value >> _Shift) | (_Value << (64 - _Shift)) : _Value;
527}
528/*----------------------------------------------------------------------------*\
529|* Bit Counting and Testing
530\*----------------------------------------------------------------------------*/
531static __inline__ unsigned char __attribute__((__always_inline__, __nodebug__))
532_BitScanForward(unsigned long *_Index, unsigned long _Mask) {
533 if (!_Mask)
534 return 0;
535 *_Index = __builtin_ctzl(_Mask);
536 return 1;
537}
538static __inline__ unsigned char __attribute__((__always_inline__, __nodebug__))
539_BitScanReverse(unsigned long *_Index, unsigned long _Mask) {
540 if (!_Mask)
541 return 0;
542 *_Index = 31 - __builtin_clzl(_Mask);
543 return 1;
544}
545static __inline__ unsigned int __attribute__((__always_inline__, __nodebug__))
546_lzcnt_u32(unsigned int a) {
547 if (!a)
548 return 32;
549 return __builtin_clzl(a);
550}
551static __inline__ unsigned short __attribute__((__always_inline__, __nodebug__))
552__popcnt16(unsigned short value) {
553 return __builtin_popcount((int)value);
554}
555static __inline__ unsigned int __attribute__((__always_inline__, __nodebug__))
556__popcnt(unsigned int value) {
557 return __builtin_popcount(value);
558}
559static __inline__ unsigned char __attribute__((__always_inline__, __nodebug__))
560_bittest(long const *a, long b) {
561 return (*a >> b) & 1;
562}
563static __inline__ unsigned char __attribute__((__always_inline__, __nodebug__))
564_bittestandcomplement(long *a, long b) {
565 unsigned char x = (*a >> b) & 1;
566 *a = *a ^ (1 << b);
567 return x;
568}
569static __inline__ unsigned char __attribute__((__always_inline__, __nodebug__))
570_bittestandreset(long *a, long b) {
571 unsigned char x = (*a >> b) & 1;
572 *a = *a & ~(1 << b);
573 return x;
574}
575static __inline__ unsigned char __attribute__((__always_inline__, __nodebug__))
576_bittestandset(long *a, long b) {
577 unsigned char x = (*a >> b) & 1;
Warren Hunt2731e3e2013-10-01 17:12:40 +0000578 *a = *a | (1 << b);
Warren Huntd6ffae92013-09-27 23:57:26 +0000579 return x;
580}
Hans Wennborga3169332014-03-13 17:05:09 +0000581static __inline__ unsigned char __attribute__((__always_inline__, __nodebug__))
582_interlockedbittestandset(long volatile *__BitBase, long __BitPos) {
583 unsigned char __Res;
584 __asm__ ("xor %0, %0\n"
585 "lock bts %2, %1\n"
586 "setc %0\n"
587 : "=r" (__Res), "+m"(*__BitBase)
588 : "Ir"(__BitPos));
589 return __Res;
590}
Warren Hunt3f987942013-09-30 21:08:05 +0000591#ifdef __x86_64__
Warren Huntd6ffae92013-09-27 23:57:26 +0000592static __inline__ unsigned char __attribute__((__always_inline__, __nodebug__))
593_BitScanForward64(unsigned long *_Index, unsigned __int64 _Mask) {
594 if (!_Mask)
595 return 0;
596 *_Index = __builtin_ctzll(_Mask);
597 return 1;
598}
599static __inline__ unsigned char __attribute__((__always_inline__, __nodebug__))
600_BitScanReverse64(unsigned long *_Index, unsigned __int64 _Mask) {
601 if (!_Mask)
602 return 0;
603 *_Index = 63 - __builtin_clzll(_Mask);
604 return 1;
605}
606static
607__inline__ unsigned __int64 __attribute__((__always_inline__, __nodebug__))
608_lzcnt_u64(unsigned __int64 a) {
609 if (!a)
610 return 64;
611 return __builtin_clzll(a);
612}
613static __inline__
614unsigned __int64 __attribute__((__always_inline__, __nodebug__))
615 __popcnt64(unsigned __int64 value) {
616 return __builtin_popcountll(value);
617}
618static __inline__ unsigned char __attribute__((__always_inline__, __nodebug__))
619_bittest64(__int64 const *a, __int64 b) {
620 return (*a >> b) & 1;
621}
622static __inline__ unsigned char __attribute__((__always_inline__, __nodebug__))
623_bittestandcomplement64(__int64 *a, __int64 b) {
624 unsigned char x = (*a >> b) & 1;
625 *a = *a ^ (1ll << b);
626 return x;
627}
628static __inline__ unsigned char __attribute__((__always_inline__, __nodebug__))
629_bittestandreset64(__int64 *a, __int64 b) {
630 unsigned char x = (*a >> b) & 1;
631 *a = *a & ~(1ll << b);
632 return x;
633}
634static __inline__ unsigned char __attribute__((__always_inline__, __nodebug__))
635_bittestandset64(__int64 *a, __int64 b) {
636 unsigned char x = (*a >> b) & 1;
Warren Hunt2731e3e2013-10-01 17:12:40 +0000637 *a = *a | (1ll << b);
Warren Huntd6ffae92013-09-27 23:57:26 +0000638 return x;
639}
Hans Wennborga3169332014-03-13 17:05:09 +0000640static __inline__ unsigned char __attribute__((__always_inline__, __nodebug__))
641_interlockedbittestandset64(__int64 volatile *__BitBase, __int64 __BitPos) {
642 unsigned char __Res;
643 __asm__ ("xor %0, %0\n"
644 "lock bts %2, %1\n"
645 "setc %0\n"
646 : "=r" (__Res), "+m"(*__BitBase)
647 : "Ir"(__BitPos));
648 return __Res;
649}
Warren Huntd6ffae92013-09-27 23:57:26 +0000650#endif
651/*----------------------------------------------------------------------------*\
652|* Interlocked Exchange Add
653\*----------------------------------------------------------------------------*/
654static __inline__ char __attribute__((__always_inline__, __nodebug__))
655_InterlockedExchangeAdd8(char volatile *_Addend, char _Value) {
656 return __atomic_add_fetch(_Addend, _Value, 0) - _Value;
657}
658static __inline__ short __attribute__((__always_inline__, __nodebug__))
659_InterlockedExchangeAdd16(short volatile *_Addend, short _Value) {
660 return __atomic_add_fetch(_Addend, _Value, 0) - _Value;
661}
Warren Hunt3f987942013-09-30 21:08:05 +0000662#ifdef __x86_64__
Warren Huntd6ffae92013-09-27 23:57:26 +0000663static __inline__ __int64 __attribute__((__always_inline__, __nodebug__))
664_InterlockedExchangeAdd64(__int64 volatile *_Addend, __int64 _Value) {
665 return __atomic_add_fetch(_Addend, _Value, 0) - _Value;
666}
667#endif
668/*----------------------------------------------------------------------------*\
669|* Interlocked Exchange Sub
670\*----------------------------------------------------------------------------*/
671static __inline__ char __attribute__((__always_inline__, __nodebug__))
672_InterlockedExchangeSub8(char volatile *_Subend, char _Value) {
673 return __atomic_sub_fetch(_Subend, _Value, 0) + _Value;
674}
675static __inline__ short __attribute__((__always_inline__, __nodebug__))
676_InterlockedExchangeSub16(short volatile *_Subend, short _Value) {
677 return __atomic_sub_fetch(_Subend, _Value, 0) + _Value;
678}
679static __inline__ long __attribute__((__always_inline__, __nodebug__))
680_InterlockedExchangeSub(long volatile *_Subend, long _Value) {
681 return __atomic_sub_fetch(_Subend, _Value, 0) + _Value;
682}
Warren Hunt3f987942013-09-30 21:08:05 +0000683#ifdef __x86_64__
Warren Huntd6ffae92013-09-27 23:57:26 +0000684static __inline__ __int64 __attribute__((__always_inline__, __nodebug__))
685_InterlockedExchangeSub64(__int64 volatile *_Subend, __int64 _Value) {
686 return __atomic_sub_fetch(_Subend, _Value, 0) + _Value;
687}
688#endif
689/*----------------------------------------------------------------------------*\
690|* Interlocked Increment
691\*----------------------------------------------------------------------------*/
Hans Wennborg2ed88802014-01-23 19:15:39 +0000692static __inline__ short __attribute__((__always_inline__, __nodebug__))
693_InterlockedIncrement16(short volatile *_Value) {
Warren Huntd6ffae92013-09-27 23:57:26 +0000694 return __atomic_add_fetch(_Value, 1, 0);
695}
Warren Hunt3f987942013-09-30 21:08:05 +0000696#ifdef __x86_64__
Warren Huntd6ffae92013-09-27 23:57:26 +0000697static __inline__ __int64 __attribute__((__always_inline__, __nodebug__))
698_InterlockedIncrement64(__int64 volatile *_Value) {
699 return __atomic_add_fetch(_Value, 1, 0);
700}
701#endif
702/*----------------------------------------------------------------------------*\
703|* Interlocked Decrement
704\*----------------------------------------------------------------------------*/
Hans Wennborg2ed88802014-01-23 19:15:39 +0000705static __inline__ short __attribute__((__always_inline__, __nodebug__))
706_InterlockedDecrement16(short volatile *_Value) {
Warren Huntd6ffae92013-09-27 23:57:26 +0000707 return __atomic_sub_fetch(_Value, 1, 0);
708}
Warren Hunt3f987942013-09-30 21:08:05 +0000709#ifdef __x86_64__
Warren Huntd6ffae92013-09-27 23:57:26 +0000710static __inline__ __int64 __attribute__((__always_inline__, __nodebug__))
711_InterlockedDecrement64(__int64 volatile *_Value) {
712 return __atomic_sub_fetch(_Value, 1, 0);
713}
714#endif
715/*----------------------------------------------------------------------------*\
716|* Interlocked And
717\*----------------------------------------------------------------------------*/
718static __inline__ char __attribute__((__always_inline__, __nodebug__))
719_InterlockedAnd8(char volatile *_Value, char _Mask) {
720 return __atomic_and_fetch(_Value, _Mask, 0);
721}
722static __inline__ short __attribute__((__always_inline__, __nodebug__))
723_InterlockedAnd16(short volatile *_Value, short _Mask) {
724 return __atomic_and_fetch(_Value, _Mask, 0);
725}
726static __inline__ long __attribute__((__always_inline__, __nodebug__))
727_InterlockedAnd(long volatile *_Value, long _Mask) {
728 return __atomic_and_fetch(_Value, _Mask, 0);
729}
Warren Hunt3f987942013-09-30 21:08:05 +0000730#ifdef __x86_64__
Warren Huntd6ffae92013-09-27 23:57:26 +0000731static __inline__ __int64 __attribute__((__always_inline__, __nodebug__))
732_InterlockedAnd64(__int64 volatile *_Value, __int64 _Mask) {
733 return __atomic_and_fetch(_Value, _Mask, 0);
734}
735#endif
736/*----------------------------------------------------------------------------*\
737|* Interlocked Or
738\*----------------------------------------------------------------------------*/
739static __inline__ char __attribute__((__always_inline__, __nodebug__))
740_InterlockedOr8(char volatile *_Value, char _Mask) {
741 return __atomic_or_fetch(_Value, _Mask, 0);
742}
743static __inline__ short __attribute__((__always_inline__, __nodebug__))
744_InterlockedOr16(short volatile *_Value, short _Mask) {
745 return __atomic_or_fetch(_Value, _Mask, 0);
746}
747static __inline__ long __attribute__((__always_inline__, __nodebug__))
748_InterlockedOr(long volatile *_Value, long _Mask) {
749 return __atomic_or_fetch(_Value, _Mask, 0);
750}
Warren Hunt3f987942013-09-30 21:08:05 +0000751#ifdef __x86_64__
Warren Huntd6ffae92013-09-27 23:57:26 +0000752static __inline__ __int64 __attribute__((__always_inline__, __nodebug__))
753_InterlockedOr64(__int64 volatile *_Value, __int64 _Mask) {
754 return __atomic_or_fetch(_Value, _Mask, 0);
755}
756#endif
757/*----------------------------------------------------------------------------*\
758|* Interlocked Xor
759\*----------------------------------------------------------------------------*/
760static __inline__ char __attribute__((__always_inline__, __nodebug__))
761_InterlockedXor8(char volatile *_Value, char _Mask) {
762 return __atomic_xor_fetch(_Value, _Mask, 0);
763}
764static __inline__ short __attribute__((__always_inline__, __nodebug__))
765_InterlockedXor16(short volatile *_Value, short _Mask) {
766 return __atomic_xor_fetch(_Value, _Mask, 0);
767}
768static __inline__ long __attribute__((__always_inline__, __nodebug__))
769_InterlockedXor(long volatile *_Value, long _Mask) {
770 return __atomic_xor_fetch(_Value, _Mask, 0);
771}
Warren Hunt3f987942013-09-30 21:08:05 +0000772#ifdef __x86_64__
Warren Huntd6ffae92013-09-27 23:57:26 +0000773static __inline__ __int64 __attribute__((__always_inline__, __nodebug__))
774_InterlockedXor64(__int64 volatile *_Value, __int64 _Mask) {
775 return __atomic_xor_fetch(_Value, _Mask, 0);
776}
777#endif
778/*----------------------------------------------------------------------------*\
779|* Interlocked Exchange
780\*----------------------------------------------------------------------------*/
781static __inline__ char __attribute__((__always_inline__, __nodebug__))
782_InterlockedExchange8(char volatile *_Target, char _Value) {
783 __atomic_exchange(_Target, &_Value, &_Value, 0);
784 return _Value;
785}
786static __inline__ short __attribute__((__always_inline__, __nodebug__))
787_InterlockedExchange16(short volatile *_Target, short _Value) {
788 __atomic_exchange(_Target, &_Value, &_Value, 0);
789 return _Value;
790}
791static __inline__ long __attribute__((__always_inline__, __nodebug__))
792_InterlockedExchange(long volatile *_Target, long _Value) {
793 __atomic_exchange(_Target, &_Value, &_Value, 0);
794 return _Value;
795}
Warren Hunt3f987942013-09-30 21:08:05 +0000796#ifdef __x86_64__
Warren Huntd6ffae92013-09-27 23:57:26 +0000797static __inline__ __int64 __attribute__((__always_inline__, __nodebug__))
798_InterlockedExchange64(__int64 volatile *_Target, __int64 _Value) {
799 __atomic_exchange(_Target, &_Value, &_Value, 0);
800 return _Value;
801}
Reid Kleckner7dd8bc02014-03-26 16:09:48 +0000802static __inline__ void *__attribute__((__always_inline__, __nodebug__))
803_InterlockedExchangePointer(void *volatile *_Target, void *_Value) {
804 __atomic_exchange(_Target, &_Value, &_Value, 0);
805 return _Value;
806}
Warren Huntd6ffae92013-09-27 23:57:26 +0000807#endif
808/*----------------------------------------------------------------------------*\
809|* Interlocked Compare Exchange
810\*----------------------------------------------------------------------------*/
811static __inline__ char __attribute__((__always_inline__, __nodebug__))
812_InterlockedCompareExchange8(char volatile *_Destination,
813 char _Exchange, char _Comparand) {
814 __atomic_compare_exchange(_Destination, &_Comparand, &_Exchange, 0, 0, 0);
815 return _Comparand;
816}
817static __inline__ short __attribute__((__always_inline__, __nodebug__))
818_InterlockedCompareExchange16(short volatile *_Destination,
819 short _Exchange, short _Comparand) {
820 __atomic_compare_exchange(_Destination, &_Comparand, &_Exchange, 0, 0, 0);
821 return _Comparand;
822}
Warren Hunt3f987942013-09-30 21:08:05 +0000823#ifdef __x86_64__
Reid Klecknerf08d6582014-01-27 19:16:35 +0000824static __inline__ void *__attribute__((__always_inline__, __nodebug__))
825_InterlockedCompareExchangePointer(void *volatile *_Destination,
826 void *_Exchange, void *_Comparand) {
827 __atomic_compare_exchange(_Destination, &_Comparand, &_Exchange, 0, 0, 0);
828 return _Comparand;
829}
830#endif
831#ifdef __x86_64__
Warren Huntd6ffae92013-09-27 23:57:26 +0000832static __inline__ __int64 __attribute__((__always_inline__, __nodebug__))
833_InterlockedCompareExchange64(__int64 volatile *_Destination,
834 __int64 _Exchange, __int64 _Comparand) {
835 __atomic_compare_exchange(_Destination, &_Comparand, &_Exchange, 0, 0, 0);
836 return _Comparand;
837}
838#endif
839/*----------------------------------------------------------------------------*\
Reid Kleckner00d33a52013-10-17 01:29:48 +0000840|* Barriers
841\*----------------------------------------------------------------------------*/
842static __inline__ void __attribute__((__always_inline__, __nodebug__))
843__attribute__((deprecated("use other intrinsics or C++11 atomics instead")))
844_ReadWriteBarrier(void) {
845 __asm__ volatile ("" : : : "memory");
846}
847static __inline__ void __attribute__((__always_inline__, __nodebug__))
848__attribute__((deprecated("use other intrinsics or C++11 atomics instead")))
849_ReadBarrier(void) {
850 __asm__ volatile ("" : : : "memory");
851}
852static __inline__ void __attribute__((__always_inline__, __nodebug__))
853__attribute__((deprecated("use other intrinsics or C++11 atomics instead")))
854_WriteBarrier(void) {
855 __asm__ volatile ("" : : : "memory");
856}
Reid Klecknerf08d6582014-01-27 19:16:35 +0000857#ifdef __x86_64__
858static __inline__ void __attribute__((__always_inline__, __nodebug__))
859__faststorefence(void) {
860 __asm__ volatile("lock orq $0, (%%rsp)" : : : "memory");
861}
862#endif
Reid Kleckner00d33a52013-10-17 01:29:48 +0000863/*----------------------------------------------------------------------------*\
Hans Wennborga4421e02014-03-12 21:09:05 +0000864|* readfs, readgs
865|* (Pointers in address space #256 and #257 are relative to the GS and FS
866|* segment registers, respectively.)
Hans Wennborg74ca0c42014-01-24 00:52:39 +0000867\*----------------------------------------------------------------------------*/
Hans Wennborg74ca0c42014-01-24 00:52:39 +0000868#define __ptr_to_addr_space(__addr_space_nbr, __type, __offset) \
869 ((volatile __type __attribute__((__address_space__(__addr_space_nbr)))*) \
870 (__offset))
871
Hans Wennborga4421e02014-03-12 21:09:05 +0000872#ifdef __i386__
Hans Wennborg74ca0c42014-01-24 00:52:39 +0000873static __inline__ unsigned char __attribute__((__always_inline__, __nodebug__))
874__readfsbyte(unsigned long __offset) {
875 return *__ptr_to_addr_space(257, unsigned char, __offset);
876}
877static __inline__ unsigned long __attribute__((__always_inline__, __nodebug__))
878__readfsdword(unsigned long __offset) {
879 return *__ptr_to_addr_space(257, unsigned long, __offset);
880}
881static __inline__ unsigned __int64 __attribute__((__always_inline__, __nodebug__))
882__readfsqword(unsigned long __offset) {
883 return *__ptr_to_addr_space(257, unsigned __int64, __offset);
884}
885static __inline__ unsigned short __attribute__((__always_inline__, __nodebug__))
886__readfsword(unsigned long __offset) {
887 return *__ptr_to_addr_space(257, unsigned short, __offset);
888}
Hans Wennborg74ca0c42014-01-24 00:52:39 +0000889#endif
Hans Wennborga4421e02014-03-12 21:09:05 +0000890#ifdef __x86_64__
891static __inline__ unsigned char __attribute__((__always_inline__, __nodebug__))
892__readgsbyte(unsigned long __offset) {
893 return *__ptr_to_addr_space(256, unsigned char, __offset);
894}
895static __inline__ unsigned long __attribute__((__always_inline__, __nodebug__))
896__readgsdword(unsigned long __offset) {
897 return *__ptr_to_addr_space(256, unsigned long, __offset);
898}
899static __inline__ unsigned __int64 __attribute__((__always_inline__, __nodebug__))
900__readgsqword(unsigned long __offset) {
901 return *__ptr_to_addr_space(256, unsigned __int64, __offset);
902}
903static __inline__ unsigned short __attribute__((__always_inline__, __nodebug__))
904__readgsword(unsigned long __offset) {
905 return *__ptr_to_addr_space(256, unsigned short, __offset);
906}
907#endif
908#undef __ptr_to_addr_space
Hans Wennborg74ca0c42014-01-24 00:52:39 +0000909/*----------------------------------------------------------------------------*\
Hans Wennborgd9be72e2014-03-12 22:00:32 +0000910|* movs, stos
911\*----------------------------------------------------------------------------*/
912static __inline__ void __attribute__((__always_inline__, __nodebug__))
913__movsb(unsigned char *__dst, unsigned char const *__src, size_t __n) {
914 __asm__("rep movsb" : : "D"(__dst), "S"(__src), "c"(__n)
915 : "%edi", "%esi", "%ecx");
916}
917static __inline__ void __attribute__((__always_inline__, __nodebug__))
918__movsd(unsigned long *__dst, unsigned long const *__src, size_t __n) {
919 __asm__("rep movsl" : : "D"(__dst), "S"(__src), "c"(__n)
920 : "%edi", "%esi", "%ecx");
921}
922static __inline__ void __attribute__((__always_inline__, __nodebug__))
923__movsw(unsigned short *__dst, unsigned short const *__src, size_t __n) {
924 __asm__("rep movsh" : : "D"(__dst), "S"(__src), "c"(__n)
925 : "%edi", "%esi", "%ecx");
926}
927static __inline__ void __attribute__((__always_inline__, __nodebug__))
928__stosb(unsigned char *__dst, unsigned char __x, size_t __n) {
929 __asm__("rep stosb" : : "D"(__dst), "a"(__x), "c"(__n)
930 : "%edi", "%ecx");
931}
932static __inline__ void __attribute__((__always_inline__, __nodebug__))
933__stosd(unsigned long *__dst, unsigned long __x, size_t __n) {
934 __asm__("rep stosl" : : "D"(__dst), "a"(__x), "c"(__n)
935 : "%edi", "%ecx");
936}
937static __inline__ void __attribute__((__always_inline__, __nodebug__))
938__stosw(unsigned short *__dst, unsigned short __x, size_t __n) {
939 __asm__("rep stosh" : : "D"(__dst), "a"(__x), "c"(__n)
940 : "%edi", "%ecx");
941}
942#ifdef __x86_64__
943static __inline__ void __attribute__((__always_inline__, __nodebug__))
944__movsq(unsigned long long *__dst, unsigned long long const *__src, size_t __n) {
945 __asm__("rep movsq" : : "D"(__dst), "S"(__src), "c"(__n)
946 : "%edi", "%esi", "%ecx");
947}
948static __inline__ void __attribute__((__always_inline__, __nodebug__))
949__stosq(unsigned __int64 *__dst, unsigned __int64 __x, size_t __n) {
950 __asm__("rep stosq" : : "D"(__dst), "a"(__x), "c"(__n)
951 : "%edi", "%ecx");
952}
953#endif
954
955/*----------------------------------------------------------------------------*\
Warren Huntd6ffae92013-09-27 23:57:26 +0000956|* Misc
957\*----------------------------------------------------------------------------*/
958static __inline__ void * __attribute__((__always_inline__, __nodebug__))
959_AddressOfReturnAddress(void) {
960 return (void*)((char*)__builtin_frame_address(0) + sizeof(void*));
961}
962static __inline__ void * __attribute__((__always_inline__, __nodebug__))
963_ReturnAddress(void) {
964 return __builtin_return_address(0);
965}
Hans Wennborg854f7d32014-01-16 23:39:35 +0000966static __inline__ void __attribute__((__always_inline__, __nodebug__))
967__cpuid(int __info[4], int __level) {
Hans Wennborg854f7d32014-01-16 23:39:35 +0000968 __asm__ ("cpuid" : "=a"(__info[0]), "=b" (__info[1]), "=c"(__info[2]), "=d"(__info[3])
Hans Wennborg12fb89e2014-01-31 19:44:55 +0000969 : "a"(__level));
970}
971static __inline__ void __attribute__((__always_inline__, __nodebug__))
972__cpuidex(int __info[4], int __level, int __ecx) {
973 __asm__ ("cpuid" : "=a"(__info[0]), "=b" (__info[1]), "=c"(__info[2]), "=d"(__info[3])
974 : "a"(__level), "c"(__ecx));
Hans Wennborg854f7d32014-01-16 23:39:35 +0000975}
976static __inline__ unsigned __int64 __cdecl __attribute__((__always_inline__, __nodebug__))
977_xgetbv(unsigned int __xcr_no) {
978 unsigned int __eax, __edx;
979 __asm__ ("xgetbv" : "=a" (__eax), "=d" (__edx) : "c" (__xcr_no));
980 return ((unsigned __int64)__edx << 32) | __eax;
981}
Hans Wennborg740a4d62014-01-28 22:55:01 +0000982static __inline__ void __attribute__((__always_inline__, __nodebug__))
983__halt(void) {
984 __asm__ volatile ("hlt");
985}
Warren Huntd6ffae92013-09-27 23:57:26 +0000986
Reid Kleckner592dc612014-04-08 00:28:22 +0000987/*----------------------------------------------------------------------------*\
988|* Privileged intrinsics
989\*----------------------------------------------------------------------------*/
990static __inline__ unsigned __int64 __attribute__((__always_inline__, __nodebug__))
991__readmsr(unsigned long __register) {
992 // Loads the contents of a 64-bit model specific register (MSR) specified in
993 // the ECX register into registers EDX:EAX. The EDX register is loaded with
994 // the high-order 32 bits of the MSR and the EAX register is loaded with the
995 // low-order 32 bits. If less than 64 bits are implemented in the MSR being
996 // read, the values returned to EDX:EAX in unimplemented bit locations are
997 // undefined.
998 unsigned long __edx;
999 unsigned long __eax;
Reid Kleckner6df52542014-04-08 17:49:16 +00001000 __asm__ ("rdmsr" : "=d"(__edx), "=a"(__eax) : "c"(__register));
Reid Kleckner592dc612014-04-08 00:28:22 +00001001 return (((unsigned __int64)__edx) << 32) | (unsigned __int64)__eax;
1002}
1003
1004static __inline__ unsigned long __attribute__((always_inline, __nodebug__))
1005__readcr3(void) {
Reid Kleckner6df52542014-04-08 17:49:16 +00001006 unsigned long __cr3_val;
1007 __asm__ __volatile__ ("mov %%cr3, %0" : "=q"(__cr3_val) : : "memory");
1008 return __cr3_val;
Reid Kleckner592dc612014-04-08 00:28:22 +00001009}
1010
1011static __inline__ void __attribute__((always_inline, __nodebug__))
Reid Kleckner6df52542014-04-08 17:49:16 +00001012__writecr3(unsigned int __cr3_val) {
1013 __asm__ ("mov %0, %%cr3" : : "q"(__cr3_val) : "memory");
Reid Kleckner592dc612014-04-08 00:28:22 +00001014}
1015
Eric Christophercc872532013-09-18 22:40:18 +00001016#ifdef __cplusplus
Eric Christopher0db88a72013-09-18 22:24:01 +00001017}
Eric Christopherd1428bf2013-08-31 00:22:48 +00001018#endif
Reid Klecknerf0e23222013-09-19 00:19:53 +00001019
1020#endif /* __INTRIN_H */
1021#endif /* _MSC_VER */