blob: 2ed88301cf9b657a2d6b6a1747e1af137be3ced5 [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);
108unsigned long __readcr3(void);
Eric Christopher439137e2014-01-24 12:13:47 +0000109unsigned long __readcr4(void);
Eric Christopherd1428bf2013-08-31 00:22:48 +0000110unsigned long __readcr8(void);
111unsigned int __readdr(unsigned int);
Hans Wennborg74ca0c42014-01-24 00:52:39 +0000112#ifdef __i386__
113static __inline__
Eric Christopherd1428bf2013-08-31 00:22:48 +0000114unsigned char __readfsbyte(unsigned long);
Hans Wennborg74ca0c42014-01-24 00:52:39 +0000115static __inline__
Eric Christopherd1428bf2013-08-31 00:22:48 +0000116unsigned long __readfsdword(unsigned long);
Hans Wennborg74ca0c42014-01-24 00:52:39 +0000117static __inline__
Eric Christopherd1428bf2013-08-31 00:22:48 +0000118unsigned __int64 __readfsqword(unsigned long);
Hans Wennborg74ca0c42014-01-24 00:52:39 +0000119static __inline__
Eric Christopherd1428bf2013-08-31 00:22:48 +0000120unsigned short __readfsword(unsigned long);
Hans Wennborg74ca0c42014-01-24 00:52:39 +0000121#endif
Eric Christopherd1428bf2013-08-31 00:22:48 +0000122unsigned __int64 __readmsr(unsigned long);
123unsigned __int64 __readpmc(unsigned long);
124unsigned long __segmentlimit(unsigned long);
125void __sidt(void *);
126void *__slwpcb(void);
Hans Wennborgd9be72e2014-03-12 22:00:32 +0000127static __inline__
Eric Christopherd1428bf2013-08-31 00:22:48 +0000128void __stosb(unsigned char *, unsigned char, size_t);
Hans Wennborgd9be72e2014-03-12 22:00:32 +0000129static __inline__
Eric Christopherd1428bf2013-08-31 00:22:48 +0000130void __stosd(unsigned long *, unsigned long, size_t);
Hans Wennborgd9be72e2014-03-12 22:00:32 +0000131static __inline__
Eric Christopherd1428bf2013-08-31 00:22:48 +0000132void __stosw(unsigned short *, unsigned short, size_t);
133void __svm_clgi(void);
134void __svm_invlpga(void *, int);
135void __svm_skinit(int);
136void __svm_stgi(void);
137void __svm_vmload(size_t);
138void __svm_vmrun(size_t);
139void __svm_vmsave(size_t);
140void __ud2(void);
141unsigned __int64 __ull_rshift(unsigned __int64, int);
142void __vmx_off(void);
143void __vmx_vmptrst(unsigned __int64 *);
144void __wbinvd(void);
145void __writecr0(unsigned int);
146void __writecr3(unsigned int);
147void __writecr4(unsigned int);
148void __writecr8(unsigned int);
149void __writedr(unsigned int, unsigned int);
Eric Christopherd1428bf2013-08-31 00:22:48 +0000150void __writefsbyte(unsigned long, unsigned char);
151void __writefsdword(unsigned long, unsigned long);
152void __writefsqword(unsigned long, unsigned __int64);
153void __writefsword(unsigned long, unsigned short);
154void __writemsr(unsigned long, unsigned __int64);
Warren Huntd6ffae92013-09-27 23:57:26 +0000155static __inline__
Eric Christopherd1428bf2013-08-31 00:22:48 +0000156void *_AddressOfReturnAddress(void);
157unsigned int _andn_u32(unsigned int, unsigned int);
158unsigned int _bextr_u32(unsigned int, unsigned int, unsigned int);
Eric Christopherd1428bf2013-08-31 00:22:48 +0000159unsigned int _bextri_u32(unsigned int, unsigned int);
Warren Huntd6ffae92013-09-27 23:57:26 +0000160static __inline__
Eric Christopherd1428bf2013-08-31 00:22:48 +0000161unsigned char _BitScanForward(unsigned long *_Index, unsigned long _Mask);
Warren Huntd6ffae92013-09-27 23:57:26 +0000162static __inline__
Eric Christopherd1428bf2013-08-31 00:22:48 +0000163unsigned char _BitScanReverse(unsigned long *_Index, unsigned long _Mask);
Warren Huntd6ffae92013-09-27 23:57:26 +0000164static __inline__
Eric Christopherd1428bf2013-08-31 00:22:48 +0000165unsigned char _bittest(long const *, long);
Warren Huntd6ffae92013-09-27 23:57:26 +0000166static __inline__
Eric Christopherd1428bf2013-08-31 00:22:48 +0000167unsigned char _bittestandcomplement(long *, long);
Warren Huntd6ffae92013-09-27 23:57:26 +0000168static __inline__
Eric Christopherd1428bf2013-08-31 00:22:48 +0000169unsigned char _bittestandreset(long *, long);
Warren Huntd6ffae92013-09-27 23:57:26 +0000170static __inline__
Eric Christopherd1428bf2013-08-31 00:22:48 +0000171unsigned char _bittestandset(long *, long);
172unsigned int _blcfill_u32(unsigned int);
173unsigned int _blci_u32(unsigned int);
174unsigned int _blcic_u32(unsigned int);
175unsigned int _blcmsk_u32(unsigned int);
176unsigned int _blcs_u32(unsigned int);
177unsigned int _blsfill_u32(unsigned int);
178unsigned int _blsi_u32(unsigned int);
179unsigned int _blsic_u32(unsigned int);
180unsigned int _blsmsk_u32(unsigned int);
Eric Christopherd1428bf2013-08-31 00:22:48 +0000181unsigned int _blsr_u32(unsigned int);
182unsigned __int64 __cdecl _byteswap_uint64(unsigned __int64);
183unsigned long __cdecl _byteswap_ulong(unsigned long);
184unsigned short __cdecl _byteswap_ushort(unsigned short);
185unsigned _bzhi_u32(unsigned int, unsigned int);
186void __cdecl _disable(void);
187void __cdecl _enable(void);
188void __cdecl _fxrstor(void const *);
189void __cdecl _fxsave(void *);
190long _InterlockedAddLargeStatistic(__int64 volatile *_Addend, long _Value);
Warren Huntd6ffae92013-09-27 23:57:26 +0000191static __inline__
Eric Christopherd1428bf2013-08-31 00:22:48 +0000192long _InterlockedAnd(long volatile *_Value, long _Mask);
Warren Huntd6ffae92013-09-27 23:57:26 +0000193static __inline__
Eric Christopherd1428bf2013-08-31 00:22:48 +0000194short _InterlockedAnd16(short volatile *_Value, short _Mask);
Warren Huntd6ffae92013-09-27 23:57:26 +0000195static __inline__
Eric Christopherd1428bf2013-08-31 00:22:48 +0000196char _InterlockedAnd8(char volatile *_Value, char _Mask);
197unsigned char _interlockedbittestandreset(long volatile *, long);
Hans Wennborga3169332014-03-13 17:05:09 +0000198static __inline__
Eric Christopherd1428bf2013-08-31 00:22:48 +0000199unsigned char _interlockedbittestandset(long volatile *, long);
Warren Huntd6ffae92013-09-27 23:57:26 +0000200static __inline__
Eric Christopherd1428bf2013-08-31 00:22:48 +0000201long __cdecl _InterlockedCompareExchange(long volatile *_Destination,
202 long _Exchange, long _Comparand);
203long _InterlockedCompareExchange_HLEAcquire(long volatile *, long, long);
204long _InterlockedCompareExchange_HLERelease(long volatile *, long, long);
Warren Huntd6ffae92013-09-27 23:57:26 +0000205static __inline__
Eric Christopherd1428bf2013-08-31 00:22:48 +0000206short _InterlockedCompareExchange16(short volatile *_Destination,
207 short _Exchange, short _Comparand);
Warren Huntd6ffae92013-09-27 23:57:26 +0000208static __inline__
Eric Christopherd1428bf2013-08-31 00:22:48 +0000209__int64 _InterlockedCompareExchange64(__int64 volatile *_Destination,
210 __int64 _Exchange, __int64 _Comparand);
211__int64 _InterlockedcompareExchange64_HLEAcquire(__int64 volatile *, __int64,
212 __int64);
213__int64 _InterlockedCompareExchange64_HLERelease(__int64 volatile *, __int64,
214 __int64);
Warren Huntd6ffae92013-09-27 23:57:26 +0000215static __inline__
Eric Christopherd1428bf2013-08-31 00:22:48 +0000216char _InterlockedCompareExchange8(char volatile *_Destination, char _Exchange,
217 char _Comparand);
218void *_InterlockedCompareExchangePointer_HLEAcquire(void *volatile *, void *,
219 void *);
220void *_InterlockedCompareExchangePointer_HLERelease(void *volatile *, void *,
221 void *);
Warren Huntd6ffae92013-09-27 23:57:26 +0000222static __inline__
Eric Christopherd1428bf2013-08-31 00:22:48 +0000223long __cdecl _InterlockedDecrement(long volatile *_Addend);
Warren Huntd6ffae92013-09-27 23:57:26 +0000224static __inline__
Eric Christopherd1428bf2013-08-31 00:22:48 +0000225short _InterlockedDecrement16(short volatile *_Addend);
Warren Huntd6ffae92013-09-27 23:57:26 +0000226static __inline__
Eric Christopherd1428bf2013-08-31 00:22:48 +0000227long __cdecl _InterlockedExchange(long volatile *_Target, long _Value);
Warren Huntd6ffae92013-09-27 23:57:26 +0000228static __inline__
Eric Christopherd1428bf2013-08-31 00:22:48 +0000229short _InterlockedExchange16(short volatile *_Target, short _Value);
Warren Huntd6ffae92013-09-27 23:57:26 +0000230static __inline__
Eric Christopherd1428bf2013-08-31 00:22:48 +0000231char _InterlockedExchange8(char volatile *_Target, char _Value);
Warren Huntd6ffae92013-09-27 23:57:26 +0000232static __inline__
Eric Christopherd1428bf2013-08-31 00:22:48 +0000233long __cdecl _InterlockedExchangeAdd(long volatile *_Addend, long _Value);
234long _InterlockedExchangeAdd_HLEAcquire(long volatile *, long);
235long _InterlockedExchangeAdd_HLERelease(long volatile *, long);
Reid Kleckner924eb2a2014-01-27 18:48:02 +0000236static __inline__
Eric Christopher439137e2014-01-24 12:13:47 +0000237short _InterlockedExchangeAdd16(short volatile *_Addend, short _Value);
238__int64 _InterlockedExchangeAdd64_HLEAcquire(__int64 volatile *, __int64);
239__int64 _InterlockedExchangeAdd64_HLERelease(__int64 volatile *, __int64);
Warren Huntd6ffae92013-09-27 23:57:26 +0000240static __inline__
Eric Christopherd1428bf2013-08-31 00:22:48 +0000241char _InterlockedExchangeAdd8(char volatile *_Addend, char _Value);
Warren Huntd6ffae92013-09-27 23:57:26 +0000242static __inline__
Eric Christopherd1428bf2013-08-31 00:22:48 +0000243long __cdecl _InterlockedIncrement(long volatile *_Addend);
Warren Huntd6ffae92013-09-27 23:57:26 +0000244static __inline__
Eric Christopherd1428bf2013-08-31 00:22:48 +0000245short _InterlockedIncrement16(short volatile *_Addend);
Warren Huntd6ffae92013-09-27 23:57:26 +0000246static __inline__
Eric Christopherd1428bf2013-08-31 00:22:48 +0000247long _InterlockedOr(long volatile *_Value, long _Mask);
Warren Huntd6ffae92013-09-27 23:57:26 +0000248static __inline__
Eric Christopherd1428bf2013-08-31 00:22:48 +0000249short _InterlockedOr16(short volatile *_Value, short _Mask);
Warren Huntd6ffae92013-09-27 23:57:26 +0000250static __inline__
Eric Christopherd1428bf2013-08-31 00:22:48 +0000251char _InterlockedOr8(char volatile *_Value, char _Mask);
Warren Huntd6ffae92013-09-27 23:57:26 +0000252static __inline__
Eric Christopherd1428bf2013-08-31 00:22:48 +0000253long _InterlockedXor(long volatile *_Value, long _Mask);
Warren Huntd6ffae92013-09-27 23:57:26 +0000254static __inline__
Eric Christopherd1428bf2013-08-31 00:22:48 +0000255short _InterlockedXor16(short volatile *_Value, short _Mask);
Warren Huntd6ffae92013-09-27 23:57:26 +0000256static __inline__
Eric Christopherd1428bf2013-08-31 00:22:48 +0000257char _InterlockedXor8(char volatile *_Value, char _Mask);
258void __cdecl _invpcid(unsigned int, void *);
Warren Huntd6ffae92013-09-27 23:57:26 +0000259static __inline__
Eric Christopherd1428bf2013-08-31 00:22:48 +0000260unsigned long __cdecl _lrotl(unsigned long, int);
Warren Huntd6ffae92013-09-27 23:57:26 +0000261static __inline__
Eric Christopherd1428bf2013-08-31 00:22:48 +0000262unsigned long __cdecl _lrotr(unsigned long, int);
Warren Huntd6ffae92013-09-27 23:57:26 +0000263static __inline__
Eric Christopherd1428bf2013-08-31 00:22:48 +0000264unsigned int _lzcnt_u32(unsigned int);
Reid Kleckner00d33a52013-10-17 01:29:48 +0000265static __inline__
Eric Christopherd1428bf2013-08-31 00:22:48 +0000266void _ReadBarrier(void);
Reid Kleckner00d33a52013-10-17 01:29:48 +0000267static __inline__
Eric Christopherd1428bf2013-08-31 00:22:48 +0000268void _ReadWriteBarrier(void);
Warren Huntd6ffae92013-09-27 23:57:26 +0000269static __inline__
Eric Christopherd1428bf2013-08-31 00:22:48 +0000270void *_ReturnAddress(void);
271unsigned int _rorx_u32(unsigned int, const unsigned int);
272int __cdecl _rdrand16_step(unsigned short *);
273int __cdecl _rdrand32_step(unsigned int *);
Warren Huntd6ffae92013-09-27 23:57:26 +0000274static __inline__
Eric Christopherfb4b4332013-08-31 00:27:38 +0000275unsigned int __cdecl _rotl(unsigned int _Value, int _Shift);
Warren Huntd6ffae92013-09-27 23:57:26 +0000276static __inline__
Eric Christopherd1428bf2013-08-31 00:22:48 +0000277unsigned short _rotl16(unsigned short _Value, unsigned char _Shift);
Warren Huntd6ffae92013-09-27 23:57:26 +0000278static __inline__
Eric Christopherd1428bf2013-08-31 00:22:48 +0000279unsigned __int64 __cdecl _rotl64(unsigned __int64 _Value, int _Shift);
Warren Huntd6ffae92013-09-27 23:57:26 +0000280static __inline__
Eric Christopherd1428bf2013-08-31 00:22:48 +0000281unsigned char _rotl8(unsigned char _Value, unsigned char _Shift);
Warren Huntd6ffae92013-09-27 23:57:26 +0000282static __inline__
Eric Christopherd1428bf2013-08-31 00:22:48 +0000283unsigned int __cdecl _rotr(unsigned int _Value, int _Shift);
Warren Huntd6ffae92013-09-27 23:57:26 +0000284static __inline__
Eric Christopherd1428bf2013-08-31 00:22:48 +0000285unsigned short _rotr16(unsigned short _Value, unsigned char _Shift);
Warren Huntd6ffae92013-09-27 23:57:26 +0000286static __inline__
Eric Christopherd1428bf2013-08-31 00:22:48 +0000287unsigned __int64 __cdecl _rotr64(unsigned __int64 _Value, int _Shift);
Warren Huntd6ffae92013-09-27 23:57:26 +0000288static __inline__
Eric Christopherd1428bf2013-08-31 00:22:48 +0000289unsigned char _rotr8(unsigned char _Value, unsigned char _Shift);
290int _sarx_i32(int, unsigned int);
Hans Wennborg1fd6dd32014-01-28 23:01:59 +0000291int __cdecl _setjmp(jmp_buf);
Eric Christopherd1428bf2013-08-31 00:22:48 +0000292unsigned int _shlx_u32(unsigned int, unsigned int);
293unsigned int _shrx_u32(unsigned int, unsigned int);
294void _Store_HLERelease(long volatile *, long);
295void _Store64_HLERelease(__int64 volatile *, __int64);
296void _StorePointer_HLERelease(void *volatile *, void *);
297unsigned int _t1mskc_u32(unsigned int);
298unsigned int _tzcnt_u32(unsigned int);
Eric Christopherd1428bf2013-08-31 00:22:48 +0000299unsigned int _tzmsk_u32(unsigned int);
Reid Kleckner00d33a52013-10-17 01:29:48 +0000300static __inline__
Eric Christopherd1428bf2013-08-31 00:22:48 +0000301void _WriteBarrier(void);
302void _xabort(const unsigned int imm);
303unsigned __int32 xbegin(void);
304void _xend(void);
Hans Wennborg854f7d32014-01-16 23:39:35 +0000305static __inline__
Eric Christopherd1428bf2013-08-31 00:22:48 +0000306unsigned __int64 __cdecl _xgetbv(unsigned int);
307void __cdecl _xrstor(void const *, unsigned __int64);
308void __cdecl _xsave(void *, unsigned __int64);
309void __cdecl _xsaveopt(void *, unsigned __int64);
310void __cdecl _xsetbv(unsigned int, unsigned __int64);
311unsigned char _xtest(void);
312
313/* These additional intrinsics are turned on in x64/amd64/x86_64 mode. */
Warren Hunt3f987942013-09-30 21:08:05 +0000314#ifdef __x86_64__
Eric Christopherd1428bf2013-08-31 00:22:48 +0000315void __addgsbyte(unsigned long, unsigned char);
316void __addgsdword(unsigned long, unsigned long);
317void __addgsqword(unsigned long, unsigned __int64);
318void __addgsword(unsigned long, unsigned short);
Reid Klecknerf08d6582014-01-27 19:16:35 +0000319static __inline__
Eric Christopherd1428bf2013-08-31 00:22:48 +0000320void __faststorefence(void);
321void __incgsbyte(unsigned long);
322void __incgsdword(unsigned long);
323void __incgsqword(unsigned long);
324void __incgsword(unsigned long);
Eric Christopher439137e2014-01-24 12:13:47 +0000325unsigned char __lwpins64(unsigned __int64, unsigned int, unsigned int);
326void __lwpval64(unsigned __int64, unsigned int, unsigned int);
327unsigned __int64 __lzcnt64(unsigned __int64);
Hans Wennborgd9be72e2014-03-12 22:00:32 +0000328static __inline__
Eric Christopher439137e2014-01-24 12:13:47 +0000329void __movsq(unsigned long long *, unsigned long long const *, size_t);
330__int64 __mulh(__int64, __int64);
Reid Kleckner924eb2a2014-01-27 18:48:02 +0000331static __inline__
Eric Christopherd1428bf2013-08-31 00:22:48 +0000332unsigned __int64 __popcnt64(unsigned __int64);
Hans Wennborga4421e02014-03-12 21:09:05 +0000333static __inline__
Eric Christopher439137e2014-01-24 12:13:47 +0000334unsigned char __readgsbyte(unsigned long);
Hans Wennborga4421e02014-03-12 21:09:05 +0000335static __inline__
Eric Christopher439137e2014-01-24 12:13:47 +0000336unsigned long __readgsdword(unsigned long);
Hans Wennborga4421e02014-03-12 21:09:05 +0000337static __inline__
338unsigned __int64 __readgsqword(unsigned long);
Eric Christopher439137e2014-01-24 12:13:47 +0000339unsigned short __readgsword(unsigned long);
Eric Christopherd1428bf2013-08-31 00:22:48 +0000340unsigned __int64 __shiftleft128(unsigned __int64 _LowPart,
341 unsigned __int64 _HighPart,
342 unsigned char _Shift);
343unsigned __int64 __shiftright128(unsigned __int64 _LowPart,
344 unsigned __int64 _HighPart,
345 unsigned char _Shift);
Hans Wennborgd9be72e2014-03-12 22:00:32 +0000346static __inline__
Eric Christopherd1428bf2013-08-31 00:22:48 +0000347void __stosq(unsigned __int64 *, unsigned __int64, size_t);
Eric Christopher439137e2014-01-24 12:13:47 +0000348unsigned __int64 __umulh(unsigned __int64, unsigned __int64);
349unsigned char __vmx_on(unsigned __int64 *);
350unsigned char __vmx_vmclear(unsigned __int64 *);
351unsigned char __vmx_vmlaunch(void);
352unsigned char __vmx_vmptrld(unsigned __int64 *);
353unsigned char __vmx_vmread(size_t, size_t *);
354unsigned char __vmx_vmresume(void);
355unsigned char __vmx_vmwrite(size_t, size_t);
356void __writegsbyte(unsigned long, unsigned char);
357void __writegsdword(unsigned long, unsigned long);
358void __writegsqword(unsigned long, unsigned __int64);
359void __writegsword(unsigned long, unsigned short);
Eric Christopherd1428bf2013-08-31 00:22:48 +0000360unsigned __int64 _andn_u64(unsigned __int64, unsigned __int64);
361unsigned __int64 _bextr_u64(unsigned __int64, unsigned int, unsigned int);
362unsigned __int64 _bextri_u64(unsigned __int64, unsigned int);
Warren Huntd6ffae92013-09-27 23:57:26 +0000363static __inline__
Eric Christopherd1428bf2013-08-31 00:22:48 +0000364unsigned char _BitScanForward64(unsigned long *_Index, unsigned __int64 _Mask);
Warren Huntd6ffae92013-09-27 23:57:26 +0000365static __inline__
Eric Christopherd1428bf2013-08-31 00:22:48 +0000366unsigned char _BitScanReverse64(unsigned long *_Index, unsigned __int64 _Mask);
Warren Huntd6ffae92013-09-27 23:57:26 +0000367static __inline__
Eric Christopherd1428bf2013-08-31 00:22:48 +0000368unsigned char _bittest64(__int64 const *, __int64);
Warren Huntd6ffae92013-09-27 23:57:26 +0000369static __inline__
Eric Christopherd1428bf2013-08-31 00:22:48 +0000370unsigned char _bittestandcomplement64(__int64 *, __int64);
Warren Huntd6ffae92013-09-27 23:57:26 +0000371static __inline__
Eric Christopherd1428bf2013-08-31 00:22:48 +0000372unsigned char _bittestandreset64(__int64 *, __int64);
Warren Huntd6ffae92013-09-27 23:57:26 +0000373static __inline__
Eric Christopherd1428bf2013-08-31 00:22:48 +0000374unsigned char _bittestandset64(__int64 *, __int64);
375unsigned __int64 _blcfill_u64(unsigned __int64);
376unsigned __int64 _blci_u64(unsigned __int64);
377unsigned __int64 _blcic_u64(unsigned __int64);
378unsigned __int64 _blcmsk_u64(unsigned __int64);
379unsigned __int64 _blcs_u64(unsigned __int64);
380unsigned __int64 _blsfill_u64(unsigned __int64);
381unsigned __int64 _blsi_u64(unsigned __int64);
382unsigned __int64 _blsic_u64(unsigned __int64);
Eric Christopher439137e2014-01-24 12:13:47 +0000383unsigned __int64 _blsmsk_u64(unsigned __int64);
Eric Christopherd1428bf2013-08-31 00:22:48 +0000384unsigned __int64 _blsr_u64(unsigned __int64);
385unsigned __int64 __cdecl _byteswap_uint64(unsigned __int64);
386unsigned __int64 _bzhi_u64(unsigned __int64, unsigned int);
387void __cdecl _fxrstor64(void const *);
388void __cdecl _fxsave64(void *);
389long _InterlockedAnd_np(long volatile *_Value, long _Mask);
390short _InterlockedAnd16_np(short volatile *_Value, short _Mask);
391__int64 _InterlockedAnd64_np(__int64 volatile *_Value, __int64 _Mask);
392char _InterlockedAnd8_np(char volatile *_Value, char _Mask);
393unsigned char _interlockedbittestandreset64(__int64 volatile *, __int64);
Hans Wennborga3169332014-03-13 17:05:09 +0000394static __inline__
Eric Christopherd1428bf2013-08-31 00:22:48 +0000395unsigned char _interlockedbittestandset64(__int64 volatile *, __int64);
396long _InterlockedCompareExchange_np(long volatile *_Destination, long _Exchange,
397 long _Comparand);
398unsigned char _InterlockedCompareExchange128(__int64 volatile *_Destination,
399 __int64 _ExchangeHigh,
400 __int64 _ExchangeLow,
401 __int64 *_CompareandResult);
402unsigned char _InterlockedCompareExchange128_np(__int64 volatile *_Destination,
403 __int64 _ExchangeHigh,
404 __int64 _ExchangeLow,
405 __int64 *_ComparandResult);
406short _InterlockedCompareExchange16_np(short volatile *_Destination,
407 short _Exchange, short _Comparand);
Eric Christopher439137e2014-01-24 12:13:47 +0000408__int64 _InterlockedCompareExchange64_HLEAcquire(__int64 volatile *, __int64,
409 __int64);
410__int64 _InterlockedCompareExchange64_HLERelease(__int64 volatile *, __int64,
411 __int64);
Eric Christopherd1428bf2013-08-31 00:22:48 +0000412__int64 _InterlockedCompareExchange64_np(__int64 volatile *_Destination,
413 __int64 _Exchange, __int64 _Comparand);
Reid Klecknerf08d6582014-01-27 19:16:35 +0000414static __inline__
Eric Christopher58b40432014-01-25 01:38:30 +0000415void *_InterlockedCompareExchangePointer(void *volatile *_Destination,
416 void *_Exchange, void *_Comparand);
Eric Christopherd1428bf2013-08-31 00:22:48 +0000417void *_InterlockedCompareExchangePointer_np(void *volatile *_Destination,
418 void *_Exchange, void *_Comparand);
Reid Kleckner924eb2a2014-01-27 18:48:02 +0000419static __inline__
Eric Christopher439137e2014-01-24 12:13:47 +0000420__int64 _InterlockedDecrement64(__int64 volatile *_Addend);
Reid Kleckner924eb2a2014-01-27 18:48:02 +0000421static __inline__
Eric Christopher439137e2014-01-24 12:13:47 +0000422__int64 _InterlockedExchange64(__int64 volatile *_Target, __int64 _Value);
Reid Kleckner924eb2a2014-01-27 18:48:02 +0000423static __inline__
Eric Christopher439137e2014-01-24 12:13:47 +0000424__int64 _InterlockedExchangeAdd64(__int64 volatile *_Addend, __int64 _Value);
425void *_InterlockedExchangePointer(void *volatile *_Target, void *_Value);
Reid Kleckner924eb2a2014-01-27 18:48:02 +0000426static __inline__
Eric Christopher439137e2014-01-24 12:13:47 +0000427__int64 _InterlockedIncrement64(__int64 volatile *_Addend);
Eric Christopherd1428bf2013-08-31 00:22:48 +0000428long _InterlockedOr_np(long volatile *_Value, long _Mask);
429short _InterlockedOr16_np(short volatile *_Value, short _Mask);
Reid Kleckner924eb2a2014-01-27 18:48:02 +0000430static __inline__
Eric Christopher439137e2014-01-24 12:13:47 +0000431__int64 _InterlockedOr64(__int64 volatile *_Value, __int64 _Mask);
Warren Hunt41a993f2013-09-28 00:15:41 +0000432__int64 _InterlockedOr64_np(__int64 volatile *_Value, __int64 _Mask);
Eric Christopherd1428bf2013-08-31 00:22:48 +0000433char _InterlockedOr8_np(char volatile *_Value, char _Mask);
434long _InterlockedXor_np(long volatile *_Value, long _Mask);
435short _InterlockedXor16_np(short volatile *_Value, short _Mask);
Reid Kleckner924eb2a2014-01-27 18:48:02 +0000436static __inline__
Eric Christopher439137e2014-01-24 12:13:47 +0000437__int64 _InterlockedXor64(__int64 volatile *_Value, __int64 _Mask);
Eric Christopherd1428bf2013-08-31 00:22:48 +0000438__int64 _InterlockedXor64_np(__int64 volatile *_Value, __int64 _Mask);
439char _InterlockedXor8_np(char volatile *_Value, char _Mask);
Reid Kleckner924eb2a2014-01-27 18:48:02 +0000440static __inline__
Eric Christopherd1428bf2013-08-31 00:22:48 +0000441unsigned __int64 _lzcnt_u64(unsigned __int64);
442__int64 _mul128(__int64 _Multiplier, __int64 _Multiplicand,
443 __int64 *_HighProduct);
444unsigned int __cdecl _readfsbase_u32(void);
445unsigned __int64 __cdecl _readfsbase_u64(void);
446unsigned int __cdecl _readgsbase_u32(void);
447unsigned __int64 __cdecl _readgsbase_u64(void);
448unsigned __int64 _rorx_u64(unsigned __int64, const unsigned int);
Eric Christopher439137e2014-01-24 12:13:47 +0000449__int64 _sarx_i64(__int64, unsigned int);
Reid Kleckner33630902014-01-27 19:32:42 +0000450/* FIXME: Need definition for jmp_buf.
451 int __cdecl _setjmpex(jmp_buf); */
Eric Christopher439137e2014-01-24 12:13:47 +0000452unsigned __int64 _shlx_u64(unsigned __int64, unsigned int);
453unsigned __int64 shrx_u64(unsigned __int64, unsigned int);
Eric Christopherd1428bf2013-08-31 00:22:48 +0000454unsigned __int64 _tzcnt_u64(unsigned __int64);
455unsigned __int64 _tzmsk_u64(unsigned __int64);
456unsigned __int64 _umul128(unsigned __int64 _Multiplier,
457 unsigned __int64 _Multiplicand,
458 unsigned __int64 *_HighProduct);
459void __cdecl _writefsbase_u32(unsigned int);
460void _cdecl _writefsbase_u64(unsigned __int64);
461void __cdecl _writegsbase_u32(unsigned int);
462void __cdecl _writegsbase_u64(unsigned __int64);
463void __cdecl _xrstor64(void const *, unsigned __int64);
464void __cdecl _xsave64(void *, unsigned __int64);
465void __cdecl _xsaveopt64(void *, unsigned __int64);
Eric Christopher0db88a72013-09-18 22:24:01 +0000466
Warren Hunt3f987942013-09-30 21:08:05 +0000467#endif /* __x86_64__ */
Reid Klecknerf0e23222013-09-19 00:19:53 +0000468
Warren Huntd6ffae92013-09-27 23:57:26 +0000469/*----------------------------------------------------------------------------*\
470|* Bit Twiddling
471\*----------------------------------------------------------------------------*/
472static __inline__ unsigned char __attribute__((__always_inline__, __nodebug__))
473_rotl8(unsigned char _Value, unsigned char _Shift) {
474 _Shift &= 0x7;
475 return _Shift ? (_Value << _Shift) | (_Value >> (8 - _Shift)) : _Value;
476}
477static __inline__ unsigned char __attribute__((__always_inline__, __nodebug__))
478_rotr8(unsigned char _Value, unsigned char _Shift) {
479 _Shift &= 0x7;
480 return _Shift ? (_Value >> _Shift) | (_Value << (8 - _Shift)) : _Value;
481}
482static __inline__ unsigned short __attribute__((__always_inline__, __nodebug__))
483_rotl16(unsigned short _Value, unsigned char _Shift) {
484 _Shift &= 0xf;
485 return _Shift ? (_Value << _Shift) | (_Value >> (16 - _Shift)) : _Value;
486}
487static __inline__ unsigned short __attribute__((__always_inline__, __nodebug__))
488_rotr16(unsigned short _Value, unsigned char _Shift) {
489 _Shift &= 0xf;
490 return _Shift ? (_Value >> _Shift) | (_Value << (16 - _Shift)) : _Value;
491}
492static __inline__ unsigned int __attribute__((__always_inline__, __nodebug__))
493_rotl(unsigned int _Value, int _Shift) {
494 _Shift &= 0x1f;
495 return _Shift ? (_Value << _Shift) | (_Value >> (32 - _Shift)) : _Value;
496}
497static __inline__ unsigned int __attribute__((__always_inline__, __nodebug__))
498_rotr(unsigned int _Value, int _Shift) {
499 _Shift &= 0x1f;
500 return _Shift ? (_Value >> _Shift) | (_Value << (32 - _Shift)) : _Value;
501}
502static __inline__ unsigned long __attribute__((__always_inline__, __nodebug__))
503_lrotl(unsigned long _Value, int _Shift) {
504 _Shift &= 0x1f;
505 return _Shift ? (_Value << _Shift) | (_Value >> (32 - _Shift)) : _Value;
506}
507static __inline__ unsigned long __attribute__((__always_inline__, __nodebug__))
508_lrotr(unsigned long _Value, int _Shift) {
509 _Shift &= 0x1f;
510 return _Shift ? (_Value >> _Shift) | (_Value << (32 - _Shift)) : _Value;
511}
512static
513__inline__ unsigned __int64 __attribute__((__always_inline__, __nodebug__))
514_rotl64(unsigned __int64 _Value, int _Shift) {
515 _Shift &= 0x3f;
516 return _Shift ? (_Value << _Shift) | (_Value >> (64 - _Shift)) : _Value;
517}
518static
519__inline__ unsigned __int64 __attribute__((__always_inline__, __nodebug__))
520_rotr64(unsigned __int64 _Value, int _Shift) {
521 _Shift &= 0x3f;
522 return _Shift ? (_Value >> _Shift) | (_Value << (64 - _Shift)) : _Value;
523}
524/*----------------------------------------------------------------------------*\
525|* Bit Counting and Testing
526\*----------------------------------------------------------------------------*/
527static __inline__ unsigned char __attribute__((__always_inline__, __nodebug__))
528_BitScanForward(unsigned long *_Index, unsigned long _Mask) {
529 if (!_Mask)
530 return 0;
531 *_Index = __builtin_ctzl(_Mask);
532 return 1;
533}
534static __inline__ unsigned char __attribute__((__always_inline__, __nodebug__))
535_BitScanReverse(unsigned long *_Index, unsigned long _Mask) {
536 if (!_Mask)
537 return 0;
538 *_Index = 31 - __builtin_clzl(_Mask);
539 return 1;
540}
541static __inline__ unsigned int __attribute__((__always_inline__, __nodebug__))
542_lzcnt_u32(unsigned int a) {
543 if (!a)
544 return 32;
545 return __builtin_clzl(a);
546}
547static __inline__ unsigned short __attribute__((__always_inline__, __nodebug__))
548__popcnt16(unsigned short value) {
549 return __builtin_popcount((int)value);
550}
551static __inline__ unsigned int __attribute__((__always_inline__, __nodebug__))
552__popcnt(unsigned int value) {
553 return __builtin_popcount(value);
554}
555static __inline__ unsigned char __attribute__((__always_inline__, __nodebug__))
556_bittest(long const *a, long b) {
557 return (*a >> b) & 1;
558}
559static __inline__ unsigned char __attribute__((__always_inline__, __nodebug__))
560_bittestandcomplement(long *a, long b) {
561 unsigned char x = (*a >> b) & 1;
562 *a = *a ^ (1 << b);
563 return x;
564}
565static __inline__ unsigned char __attribute__((__always_inline__, __nodebug__))
566_bittestandreset(long *a, long b) {
567 unsigned char x = (*a >> b) & 1;
568 *a = *a & ~(1 << b);
569 return x;
570}
571static __inline__ unsigned char __attribute__((__always_inline__, __nodebug__))
572_bittestandset(long *a, long b) {
573 unsigned char x = (*a >> b) & 1;
Warren Hunt2731e3e2013-10-01 17:12:40 +0000574 *a = *a | (1 << b);
Warren Huntd6ffae92013-09-27 23:57:26 +0000575 return x;
576}
Hans Wennborga3169332014-03-13 17:05:09 +0000577static __inline__ unsigned char __attribute__((__always_inline__, __nodebug__))
578_interlockedbittestandset(long volatile *__BitBase, long __BitPos) {
579 unsigned char __Res;
580 __asm__ ("xor %0, %0\n"
581 "lock bts %2, %1\n"
582 "setc %0\n"
583 : "=r" (__Res), "+m"(*__BitBase)
584 : "Ir"(__BitPos));
585 return __Res;
586}
Warren Hunt3f987942013-09-30 21:08:05 +0000587#ifdef __x86_64__
Warren Huntd6ffae92013-09-27 23:57:26 +0000588static __inline__ unsigned char __attribute__((__always_inline__, __nodebug__))
589_BitScanForward64(unsigned long *_Index, unsigned __int64 _Mask) {
590 if (!_Mask)
591 return 0;
592 *_Index = __builtin_ctzll(_Mask);
593 return 1;
594}
595static __inline__ unsigned char __attribute__((__always_inline__, __nodebug__))
596_BitScanReverse64(unsigned long *_Index, unsigned __int64 _Mask) {
597 if (!_Mask)
598 return 0;
599 *_Index = 63 - __builtin_clzll(_Mask);
600 return 1;
601}
602static
603__inline__ unsigned __int64 __attribute__((__always_inline__, __nodebug__))
604_lzcnt_u64(unsigned __int64 a) {
605 if (!a)
606 return 64;
607 return __builtin_clzll(a);
608}
609static __inline__
610unsigned __int64 __attribute__((__always_inline__, __nodebug__))
611 __popcnt64(unsigned __int64 value) {
612 return __builtin_popcountll(value);
613}
614static __inline__ unsigned char __attribute__((__always_inline__, __nodebug__))
615_bittest64(__int64 const *a, __int64 b) {
616 return (*a >> b) & 1;
617}
618static __inline__ unsigned char __attribute__((__always_inline__, __nodebug__))
619_bittestandcomplement64(__int64 *a, __int64 b) {
620 unsigned char x = (*a >> b) & 1;
621 *a = *a ^ (1ll << b);
622 return x;
623}
624static __inline__ unsigned char __attribute__((__always_inline__, __nodebug__))
625_bittestandreset64(__int64 *a, __int64 b) {
626 unsigned char x = (*a >> b) & 1;
627 *a = *a & ~(1ll << b);
628 return x;
629}
630static __inline__ unsigned char __attribute__((__always_inline__, __nodebug__))
631_bittestandset64(__int64 *a, __int64 b) {
632 unsigned char x = (*a >> b) & 1;
Warren Hunt2731e3e2013-10-01 17:12:40 +0000633 *a = *a | (1ll << b);
Warren Huntd6ffae92013-09-27 23:57:26 +0000634 return x;
635}
Hans Wennborga3169332014-03-13 17:05:09 +0000636static __inline__ unsigned char __attribute__((__always_inline__, __nodebug__))
637_interlockedbittestandset64(__int64 volatile *__BitBase, __int64 __BitPos) {
638 unsigned char __Res;
639 __asm__ ("xor %0, %0\n"
640 "lock bts %2, %1\n"
641 "setc %0\n"
642 : "=r" (__Res), "+m"(*__BitBase)
643 : "Ir"(__BitPos));
644 return __Res;
645}
Warren Huntd6ffae92013-09-27 23:57:26 +0000646#endif
647/*----------------------------------------------------------------------------*\
648|* Interlocked Exchange Add
649\*----------------------------------------------------------------------------*/
650static __inline__ char __attribute__((__always_inline__, __nodebug__))
651_InterlockedExchangeAdd8(char volatile *_Addend, char _Value) {
652 return __atomic_add_fetch(_Addend, _Value, 0) - _Value;
653}
654static __inline__ short __attribute__((__always_inline__, __nodebug__))
655_InterlockedExchangeAdd16(short volatile *_Addend, short _Value) {
656 return __atomic_add_fetch(_Addend, _Value, 0) - _Value;
657}
Warren Hunt3f987942013-09-30 21:08:05 +0000658#ifdef __x86_64__
Warren Huntd6ffae92013-09-27 23:57:26 +0000659static __inline__ __int64 __attribute__((__always_inline__, __nodebug__))
660_InterlockedExchangeAdd64(__int64 volatile *_Addend, __int64 _Value) {
661 return __atomic_add_fetch(_Addend, _Value, 0) - _Value;
662}
663#endif
664/*----------------------------------------------------------------------------*\
665|* Interlocked Exchange Sub
666\*----------------------------------------------------------------------------*/
667static __inline__ char __attribute__((__always_inline__, __nodebug__))
668_InterlockedExchangeSub8(char volatile *_Subend, char _Value) {
669 return __atomic_sub_fetch(_Subend, _Value, 0) + _Value;
670}
671static __inline__ short __attribute__((__always_inline__, __nodebug__))
672_InterlockedExchangeSub16(short volatile *_Subend, short _Value) {
673 return __atomic_sub_fetch(_Subend, _Value, 0) + _Value;
674}
675static __inline__ long __attribute__((__always_inline__, __nodebug__))
676_InterlockedExchangeSub(long volatile *_Subend, long _Value) {
677 return __atomic_sub_fetch(_Subend, _Value, 0) + _Value;
678}
Warren Hunt3f987942013-09-30 21:08:05 +0000679#ifdef __x86_64__
Warren Huntd6ffae92013-09-27 23:57:26 +0000680static __inline__ __int64 __attribute__((__always_inline__, __nodebug__))
681_InterlockedExchangeSub64(__int64 volatile *_Subend, __int64 _Value) {
682 return __atomic_sub_fetch(_Subend, _Value, 0) + _Value;
683}
684#endif
685/*----------------------------------------------------------------------------*\
686|* Interlocked Increment
687\*----------------------------------------------------------------------------*/
Hans Wennborg2ed88802014-01-23 19:15:39 +0000688static __inline__ short __attribute__((__always_inline__, __nodebug__))
689_InterlockedIncrement16(short volatile *_Value) {
Warren Huntd6ffae92013-09-27 23:57:26 +0000690 return __atomic_add_fetch(_Value, 1, 0);
691}
Warren Hunt3f987942013-09-30 21:08:05 +0000692#ifdef __x86_64__
Warren Huntd6ffae92013-09-27 23:57:26 +0000693static __inline__ __int64 __attribute__((__always_inline__, __nodebug__))
694_InterlockedIncrement64(__int64 volatile *_Value) {
695 return __atomic_add_fetch(_Value, 1, 0);
696}
697#endif
698/*----------------------------------------------------------------------------*\
699|* Interlocked Decrement
700\*----------------------------------------------------------------------------*/
Hans Wennborg2ed88802014-01-23 19:15:39 +0000701static __inline__ short __attribute__((__always_inline__, __nodebug__))
702_InterlockedDecrement16(short volatile *_Value) {
Warren Huntd6ffae92013-09-27 23:57:26 +0000703 return __atomic_sub_fetch(_Value, 1, 0);
704}
Warren Hunt3f987942013-09-30 21:08:05 +0000705#ifdef __x86_64__
Warren Huntd6ffae92013-09-27 23:57:26 +0000706static __inline__ __int64 __attribute__((__always_inline__, __nodebug__))
707_InterlockedDecrement64(__int64 volatile *_Value) {
708 return __atomic_sub_fetch(_Value, 1, 0);
709}
710#endif
711/*----------------------------------------------------------------------------*\
712|* Interlocked And
713\*----------------------------------------------------------------------------*/
714static __inline__ char __attribute__((__always_inline__, __nodebug__))
715_InterlockedAnd8(char volatile *_Value, char _Mask) {
716 return __atomic_and_fetch(_Value, _Mask, 0);
717}
718static __inline__ short __attribute__((__always_inline__, __nodebug__))
719_InterlockedAnd16(short volatile *_Value, short _Mask) {
720 return __atomic_and_fetch(_Value, _Mask, 0);
721}
722static __inline__ long __attribute__((__always_inline__, __nodebug__))
723_InterlockedAnd(long volatile *_Value, long _Mask) {
724 return __atomic_and_fetch(_Value, _Mask, 0);
725}
Warren Hunt3f987942013-09-30 21:08:05 +0000726#ifdef __x86_64__
Warren Huntd6ffae92013-09-27 23:57:26 +0000727static __inline__ __int64 __attribute__((__always_inline__, __nodebug__))
728_InterlockedAnd64(__int64 volatile *_Value, __int64 _Mask) {
729 return __atomic_and_fetch(_Value, _Mask, 0);
730}
731#endif
732/*----------------------------------------------------------------------------*\
733|* Interlocked Or
734\*----------------------------------------------------------------------------*/
735static __inline__ char __attribute__((__always_inline__, __nodebug__))
736_InterlockedOr8(char volatile *_Value, char _Mask) {
737 return __atomic_or_fetch(_Value, _Mask, 0);
738}
739static __inline__ short __attribute__((__always_inline__, __nodebug__))
740_InterlockedOr16(short volatile *_Value, short _Mask) {
741 return __atomic_or_fetch(_Value, _Mask, 0);
742}
743static __inline__ long __attribute__((__always_inline__, __nodebug__))
744_InterlockedOr(long volatile *_Value, long _Mask) {
745 return __atomic_or_fetch(_Value, _Mask, 0);
746}
Warren Hunt3f987942013-09-30 21:08:05 +0000747#ifdef __x86_64__
Warren Huntd6ffae92013-09-27 23:57:26 +0000748static __inline__ __int64 __attribute__((__always_inline__, __nodebug__))
749_InterlockedOr64(__int64 volatile *_Value, __int64 _Mask) {
750 return __atomic_or_fetch(_Value, _Mask, 0);
751}
752#endif
753/*----------------------------------------------------------------------------*\
754|* Interlocked Xor
755\*----------------------------------------------------------------------------*/
756static __inline__ char __attribute__((__always_inline__, __nodebug__))
757_InterlockedXor8(char volatile *_Value, char _Mask) {
758 return __atomic_xor_fetch(_Value, _Mask, 0);
759}
760static __inline__ short __attribute__((__always_inline__, __nodebug__))
761_InterlockedXor16(short volatile *_Value, short _Mask) {
762 return __atomic_xor_fetch(_Value, _Mask, 0);
763}
764static __inline__ long __attribute__((__always_inline__, __nodebug__))
765_InterlockedXor(long volatile *_Value, long _Mask) {
766 return __atomic_xor_fetch(_Value, _Mask, 0);
767}
Warren Hunt3f987942013-09-30 21:08:05 +0000768#ifdef __x86_64__
Warren Huntd6ffae92013-09-27 23:57:26 +0000769static __inline__ __int64 __attribute__((__always_inline__, __nodebug__))
770_InterlockedXor64(__int64 volatile *_Value, __int64 _Mask) {
771 return __atomic_xor_fetch(_Value, _Mask, 0);
772}
773#endif
774/*----------------------------------------------------------------------------*\
775|* Interlocked Exchange
776\*----------------------------------------------------------------------------*/
777static __inline__ char __attribute__((__always_inline__, __nodebug__))
778_InterlockedExchange8(char volatile *_Target, char _Value) {
779 __atomic_exchange(_Target, &_Value, &_Value, 0);
780 return _Value;
781}
782static __inline__ short __attribute__((__always_inline__, __nodebug__))
783_InterlockedExchange16(short volatile *_Target, short _Value) {
784 __atomic_exchange(_Target, &_Value, &_Value, 0);
785 return _Value;
786}
787static __inline__ long __attribute__((__always_inline__, __nodebug__))
788_InterlockedExchange(long volatile *_Target, long _Value) {
789 __atomic_exchange(_Target, &_Value, &_Value, 0);
790 return _Value;
791}
Warren Hunt3f987942013-09-30 21:08:05 +0000792#ifdef __x86_64__
Warren Huntd6ffae92013-09-27 23:57:26 +0000793static __inline__ __int64 __attribute__((__always_inline__, __nodebug__))
794_InterlockedExchange64(__int64 volatile *_Target, __int64 _Value) {
795 __atomic_exchange(_Target, &_Value, &_Value, 0);
796 return _Value;
797}
798#endif
799/*----------------------------------------------------------------------------*\
800|* Interlocked Compare Exchange
801\*----------------------------------------------------------------------------*/
802static __inline__ char __attribute__((__always_inline__, __nodebug__))
803_InterlockedCompareExchange8(char volatile *_Destination,
804 char _Exchange, char _Comparand) {
805 __atomic_compare_exchange(_Destination, &_Comparand, &_Exchange, 0, 0, 0);
806 return _Comparand;
807}
808static __inline__ short __attribute__((__always_inline__, __nodebug__))
809_InterlockedCompareExchange16(short volatile *_Destination,
810 short _Exchange, short _Comparand) {
811 __atomic_compare_exchange(_Destination, &_Comparand, &_Exchange, 0, 0, 0);
812 return _Comparand;
813}
Warren Hunt3f987942013-09-30 21:08:05 +0000814#ifdef __x86_64__
Reid Klecknerf08d6582014-01-27 19:16:35 +0000815static __inline__ void *__attribute__((__always_inline__, __nodebug__))
816_InterlockedCompareExchangePointer(void *volatile *_Destination,
817 void *_Exchange, void *_Comparand) {
818 __atomic_compare_exchange(_Destination, &_Comparand, &_Exchange, 0, 0, 0);
819 return _Comparand;
820}
821#endif
822#ifdef __x86_64__
Warren Huntd6ffae92013-09-27 23:57:26 +0000823static __inline__ __int64 __attribute__((__always_inline__, __nodebug__))
824_InterlockedCompareExchange64(__int64 volatile *_Destination,
825 __int64 _Exchange, __int64 _Comparand) {
826 __atomic_compare_exchange(_Destination, &_Comparand, &_Exchange, 0, 0, 0);
827 return _Comparand;
828}
829#endif
830/*----------------------------------------------------------------------------*\
Reid Kleckner00d33a52013-10-17 01:29:48 +0000831|* Barriers
832\*----------------------------------------------------------------------------*/
833static __inline__ void __attribute__((__always_inline__, __nodebug__))
834__attribute__((deprecated("use other intrinsics or C++11 atomics instead")))
835_ReadWriteBarrier(void) {
836 __asm__ volatile ("" : : : "memory");
837}
838static __inline__ void __attribute__((__always_inline__, __nodebug__))
839__attribute__((deprecated("use other intrinsics or C++11 atomics instead")))
840_ReadBarrier(void) {
841 __asm__ volatile ("" : : : "memory");
842}
843static __inline__ void __attribute__((__always_inline__, __nodebug__))
844__attribute__((deprecated("use other intrinsics or C++11 atomics instead")))
845_WriteBarrier(void) {
846 __asm__ volatile ("" : : : "memory");
847}
Reid Klecknerf08d6582014-01-27 19:16:35 +0000848#ifdef __x86_64__
849static __inline__ void __attribute__((__always_inline__, __nodebug__))
850__faststorefence(void) {
851 __asm__ volatile("lock orq $0, (%%rsp)" : : : "memory");
852}
853#endif
Reid Kleckner00d33a52013-10-17 01:29:48 +0000854/*----------------------------------------------------------------------------*\
Hans Wennborga4421e02014-03-12 21:09:05 +0000855|* readfs, readgs
856|* (Pointers in address space #256 and #257 are relative to the GS and FS
857|* segment registers, respectively.)
Hans Wennborg74ca0c42014-01-24 00:52:39 +0000858\*----------------------------------------------------------------------------*/
Hans Wennborg74ca0c42014-01-24 00:52:39 +0000859#define __ptr_to_addr_space(__addr_space_nbr, __type, __offset) \
860 ((volatile __type __attribute__((__address_space__(__addr_space_nbr)))*) \
861 (__offset))
862
Hans Wennborga4421e02014-03-12 21:09:05 +0000863#ifdef __i386__
Hans Wennborg74ca0c42014-01-24 00:52:39 +0000864static __inline__ unsigned char __attribute__((__always_inline__, __nodebug__))
865__readfsbyte(unsigned long __offset) {
866 return *__ptr_to_addr_space(257, unsigned char, __offset);
867}
868static __inline__ unsigned long __attribute__((__always_inline__, __nodebug__))
869__readfsdword(unsigned long __offset) {
870 return *__ptr_to_addr_space(257, unsigned long, __offset);
871}
872static __inline__ unsigned __int64 __attribute__((__always_inline__, __nodebug__))
873__readfsqword(unsigned long __offset) {
874 return *__ptr_to_addr_space(257, unsigned __int64, __offset);
875}
876static __inline__ unsigned short __attribute__((__always_inline__, __nodebug__))
877__readfsword(unsigned long __offset) {
878 return *__ptr_to_addr_space(257, unsigned short, __offset);
879}
Hans Wennborg74ca0c42014-01-24 00:52:39 +0000880#endif
Hans Wennborga4421e02014-03-12 21:09:05 +0000881#ifdef __x86_64__
882static __inline__ unsigned char __attribute__((__always_inline__, __nodebug__))
883__readgsbyte(unsigned long __offset) {
884 return *__ptr_to_addr_space(256, unsigned char, __offset);
885}
886static __inline__ unsigned long __attribute__((__always_inline__, __nodebug__))
887__readgsdword(unsigned long __offset) {
888 return *__ptr_to_addr_space(256, unsigned long, __offset);
889}
890static __inline__ unsigned __int64 __attribute__((__always_inline__, __nodebug__))
891__readgsqword(unsigned long __offset) {
892 return *__ptr_to_addr_space(256, unsigned __int64, __offset);
893}
894static __inline__ unsigned short __attribute__((__always_inline__, __nodebug__))
895__readgsword(unsigned long __offset) {
896 return *__ptr_to_addr_space(256, unsigned short, __offset);
897}
898#endif
899#undef __ptr_to_addr_space
Hans Wennborg74ca0c42014-01-24 00:52:39 +0000900/*----------------------------------------------------------------------------*\
Hans Wennborgd9be72e2014-03-12 22:00:32 +0000901|* movs, stos
902\*----------------------------------------------------------------------------*/
903static __inline__ void __attribute__((__always_inline__, __nodebug__))
904__movsb(unsigned char *__dst, unsigned char const *__src, size_t __n) {
905 __asm__("rep movsb" : : "D"(__dst), "S"(__src), "c"(__n)
906 : "%edi", "%esi", "%ecx");
907}
908static __inline__ void __attribute__((__always_inline__, __nodebug__))
909__movsd(unsigned long *__dst, unsigned long const *__src, size_t __n) {
910 __asm__("rep movsl" : : "D"(__dst), "S"(__src), "c"(__n)
911 : "%edi", "%esi", "%ecx");
912}
913static __inline__ void __attribute__((__always_inline__, __nodebug__))
914__movsw(unsigned short *__dst, unsigned short const *__src, size_t __n) {
915 __asm__("rep movsh" : : "D"(__dst), "S"(__src), "c"(__n)
916 : "%edi", "%esi", "%ecx");
917}
918static __inline__ void __attribute__((__always_inline__, __nodebug__))
919__stosb(unsigned char *__dst, unsigned char __x, size_t __n) {
920 __asm__("rep stosb" : : "D"(__dst), "a"(__x), "c"(__n)
921 : "%edi", "%ecx");
922}
923static __inline__ void __attribute__((__always_inline__, __nodebug__))
924__stosd(unsigned long *__dst, unsigned long __x, size_t __n) {
925 __asm__("rep stosl" : : "D"(__dst), "a"(__x), "c"(__n)
926 : "%edi", "%ecx");
927}
928static __inline__ void __attribute__((__always_inline__, __nodebug__))
929__stosw(unsigned short *__dst, unsigned short __x, size_t __n) {
930 __asm__("rep stosh" : : "D"(__dst), "a"(__x), "c"(__n)
931 : "%edi", "%ecx");
932}
933#ifdef __x86_64__
934static __inline__ void __attribute__((__always_inline__, __nodebug__))
935__movsq(unsigned long long *__dst, unsigned long long const *__src, size_t __n) {
936 __asm__("rep movsq" : : "D"(__dst), "S"(__src), "c"(__n)
937 : "%edi", "%esi", "%ecx");
938}
939static __inline__ void __attribute__((__always_inline__, __nodebug__))
940__stosq(unsigned __int64 *__dst, unsigned __int64 __x, size_t __n) {
941 __asm__("rep stosq" : : "D"(__dst), "a"(__x), "c"(__n)
942 : "%edi", "%ecx");
943}
944#endif
945
946/*----------------------------------------------------------------------------*\
Warren Huntd6ffae92013-09-27 23:57:26 +0000947|* Misc
948\*----------------------------------------------------------------------------*/
949static __inline__ void * __attribute__((__always_inline__, __nodebug__))
950_AddressOfReturnAddress(void) {
951 return (void*)((char*)__builtin_frame_address(0) + sizeof(void*));
952}
953static __inline__ void * __attribute__((__always_inline__, __nodebug__))
954_ReturnAddress(void) {
955 return __builtin_return_address(0);
956}
Hans Wennborg854f7d32014-01-16 23:39:35 +0000957static __inline__ void __attribute__((__always_inline__, __nodebug__))
958__cpuid(int __info[4], int __level) {
Hans Wennborg854f7d32014-01-16 23:39:35 +0000959 __asm__ ("cpuid" : "=a"(__info[0]), "=b" (__info[1]), "=c"(__info[2]), "=d"(__info[3])
Hans Wennborg12fb89e2014-01-31 19:44:55 +0000960 : "a"(__level));
961}
962static __inline__ void __attribute__((__always_inline__, __nodebug__))
963__cpuidex(int __info[4], int __level, int __ecx) {
964 __asm__ ("cpuid" : "=a"(__info[0]), "=b" (__info[1]), "=c"(__info[2]), "=d"(__info[3])
965 : "a"(__level), "c"(__ecx));
Hans Wennborg854f7d32014-01-16 23:39:35 +0000966}
967static __inline__ unsigned __int64 __cdecl __attribute__((__always_inline__, __nodebug__))
968_xgetbv(unsigned int __xcr_no) {
969 unsigned int __eax, __edx;
970 __asm__ ("xgetbv" : "=a" (__eax), "=d" (__edx) : "c" (__xcr_no));
971 return ((unsigned __int64)__edx << 32) | __eax;
972}
Hans Wennborg740a4d62014-01-28 22:55:01 +0000973static __inline__ unsigned __int64 __attribute__((__always_inline__, __nodebug__))
974__rdtsc(void) {
975 unsigned int __eax, __edx;
976 __asm__ ("rdtsc" : "=a" (__eax), "=d" (__edx));
977 return ((unsigned __int64)__edx << 32) | __eax;
978}
979static __inline__ void __attribute__((__always_inline__, __nodebug__))
980__halt(void) {
981 __asm__ volatile ("hlt");
982}
Warren Huntd6ffae92013-09-27 23:57:26 +0000983
Eric Christophercc872532013-09-18 22:40:18 +0000984#ifdef __cplusplus
Eric Christopher0db88a72013-09-18 22:24:01 +0000985}
Eric Christopherd1428bf2013-08-31 00:22:48 +0000986#endif
Reid Klecknerf0e23222013-09-19 00:19:53 +0000987
988#endif /* __INTRIN_H */
989#endif /* _MSC_VER */