blob: cad3a3a0bd8d8d71c542e10c50eb04bff98bf773 [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. */
Saleem Abdulrasool702eefe2014-06-25 16:48:40 +000033#if defined(__i386__) || defined(__x86_64__)
Eric Christopherd1428bf2013-08-31 00:22:48 +000034#include <x86intrin.h>
Saleem Abdulrasool702eefe2014-06-25 16:48:40 +000035#endif
Eric Christopherd1428bf2013-08-31 00:22:48 +000036
Hans Wennborg1fd6dd32014-01-28 23:01:59 +000037/* For the definition of jmp_buf. */
38#include <setjmp.h>
39
Eric Christopher0db88a72013-09-18 22:24:01 +000040#ifdef __cplusplus
41extern "C" {
42#endif
43
Eric Christopherd1428bf2013-08-31 00:22:48 +000044/* And the random ones that aren't in those files. */
45__m64 _m_from_float(float);
46__m64 _m_from_int(int _l);
47void _m_prefetch(void *);
48float _m_to_float(__m64);
49int _m_to_int(__m64 _M);
50
51/* Other assorted instruction intrinsics. */
52void __addfsbyte(unsigned long, unsigned char);
53void __addfsdword(unsigned long, unsigned long);
54void __addfsword(unsigned long, unsigned short);
55void __code_seg(const char *);
Hans Wennborg854f7d32014-01-16 23:39:35 +000056static __inline__
Eric Christopherd1428bf2013-08-31 00:22:48 +000057void __cpuid(int[4], int);
Hans Wennborg12fb89e2014-01-31 19:44:55 +000058static __inline__
Eric Christopherd1428bf2013-08-31 00:22:48 +000059void __cpuidex(int[4], int, int);
Eric Christopher0db88a72013-09-18 22:24:01 +000060void __debugbreak(void);
Eric Christopherd1428bf2013-08-31 00:22:48 +000061__int64 __emul(int, int);
62unsigned __int64 __emulu(unsigned int, unsigned int);
Eric Christopher0db88a72013-09-18 22:24:01 +000063void __cdecl __fastfail(unsigned int);
Eric Christopherd1428bf2013-08-31 00:22:48 +000064unsigned int __getcallerseflags(void);
Hans Wennborg740a4d62014-01-28 22:55:01 +000065static __inline__
Eric Christopherd1428bf2013-08-31 00:22:48 +000066void __halt(void);
67unsigned char __inbyte(unsigned short);
68void __inbytestring(unsigned short, unsigned char *, unsigned long);
69void __incfsbyte(unsigned long);
70void __incfsdword(unsigned long);
71void __incfsword(unsigned long);
72unsigned long __indword(unsigned short);
73void __indwordstring(unsigned short, unsigned long *, unsigned long);
74void __int2c(void);
75void __invlpg(void *);
76unsigned short __inword(unsigned short);
77void __inwordstring(unsigned short, unsigned short *, unsigned long);
78void __lidt(void *);
79unsigned __int64 __ll_lshift(unsigned __int64, int);
80__int64 __ll_rshift(__int64, int);
81void __llwpcb(void *);
82unsigned char __lwpins32(unsigned int, unsigned int, unsigned int);
83void __lwpval32(unsigned int, unsigned int, unsigned int);
84unsigned int __lzcnt(unsigned int);
85unsigned short __lzcnt16(unsigned short);
Hans Wennborgd9be72e2014-03-12 22:00:32 +000086static __inline__
Eric Christopherd1428bf2013-08-31 00:22:48 +000087void __movsb(unsigned char *, unsigned char const *, size_t);
Hans Wennborgd9be72e2014-03-12 22:00:32 +000088static __inline__
Eric Christopherd1428bf2013-08-31 00:22:48 +000089void __movsd(unsigned long *, unsigned long const *, size_t);
Hans Wennborgd9be72e2014-03-12 22:00:32 +000090static __inline__
Eric Christopherd1428bf2013-08-31 00:22:48 +000091void __movsw(unsigned short *, unsigned short const *, size_t);
92void __nop(void);
93void __nvreg_restore_fence(void);
94void __nvreg_save_fence(void);
95void __outbyte(unsigned short, unsigned char);
96void __outbytestring(unsigned short, unsigned char *, unsigned long);
97void __outdword(unsigned short, unsigned long);
98void __outdwordstring(unsigned short, unsigned long *, unsigned long);
99void __outword(unsigned short, unsigned short);
100void __outwordstring(unsigned short, unsigned short *, unsigned long);
Warren Huntd6ffae92013-09-27 23:57:26 +0000101static __inline__
Eric Christopherd1428bf2013-08-31 00:22:48 +0000102unsigned int __popcnt(unsigned int);
Warren Huntd6ffae92013-09-27 23:57:26 +0000103static __inline__
Eric Christopherd1428bf2013-08-31 00:22:48 +0000104unsigned short __popcnt16(unsigned short);
Eric Christopherd1428bf2013-08-31 00:22:48 +0000105unsigned long __readcr0(void);
106unsigned long __readcr2(void);
Reid Kleckner592dc612014-04-08 00:28:22 +0000107static __inline__
Eric Christopherd1428bf2013-08-31 00:22:48 +0000108unsigned 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
Reid Kleckner592dc612014-04-08 00:28:22 +0000122static __inline__
Eric Christopherd1428bf2013-08-31 00:22:48 +0000123unsigned __int64 __readmsr(unsigned long);
124unsigned __int64 __readpmc(unsigned long);
125unsigned long __segmentlimit(unsigned long);
126void __sidt(void *);
127void *__slwpcb(void);
Hans Wennborgd9be72e2014-03-12 22:00:32 +0000128static __inline__
Eric Christopherd1428bf2013-08-31 00:22:48 +0000129void __stosb(unsigned char *, unsigned char, size_t);
Hans Wennborgd9be72e2014-03-12 22:00:32 +0000130static __inline__
Eric Christopherd1428bf2013-08-31 00:22:48 +0000131void __stosd(unsigned long *, unsigned long, size_t);
Hans Wennborgd9be72e2014-03-12 22:00:32 +0000132static __inline__
Eric Christopherd1428bf2013-08-31 00:22:48 +0000133void __stosw(unsigned short *, unsigned short, size_t);
134void __svm_clgi(void);
135void __svm_invlpga(void *, int);
136void __svm_skinit(int);
137void __svm_stgi(void);
138void __svm_vmload(size_t);
139void __svm_vmrun(size_t);
140void __svm_vmsave(size_t);
141void __ud2(void);
142unsigned __int64 __ull_rshift(unsigned __int64, int);
143void __vmx_off(void);
144void __vmx_vmptrst(unsigned __int64 *);
145void __wbinvd(void);
146void __writecr0(unsigned int);
Reid Kleckner592dc612014-04-08 00:28:22 +0000147static __inline__
Eric Christopherd1428bf2013-08-31 00:22:48 +0000148void __writecr3(unsigned int);
149void __writecr4(unsigned int);
150void __writecr8(unsigned int);
151void __writedr(unsigned int, unsigned int);
Eric Christopherd1428bf2013-08-31 00:22:48 +0000152void __writefsbyte(unsigned long, unsigned char);
153void __writefsdword(unsigned long, unsigned long);
154void __writefsqword(unsigned long, unsigned __int64);
155void __writefsword(unsigned long, unsigned short);
156void __writemsr(unsigned long, unsigned __int64);
Warren Huntd6ffae92013-09-27 23:57:26 +0000157static __inline__
Eric Christopherd1428bf2013-08-31 00:22:48 +0000158void *_AddressOfReturnAddress(void);
159unsigned int _andn_u32(unsigned int, unsigned int);
160unsigned int _bextr_u32(unsigned int, unsigned int, unsigned int);
Eric Christopherd1428bf2013-08-31 00:22:48 +0000161unsigned int _bextri_u32(unsigned int, unsigned int);
Warren Huntd6ffae92013-09-27 23:57:26 +0000162static __inline__
Eric Christopherd1428bf2013-08-31 00:22:48 +0000163unsigned char _BitScanForward(unsigned long *_Index, unsigned long _Mask);
Warren Huntd6ffae92013-09-27 23:57:26 +0000164static __inline__
Eric Christopherd1428bf2013-08-31 00:22:48 +0000165unsigned char _BitScanReverse(unsigned long *_Index, unsigned long _Mask);
Warren Huntd6ffae92013-09-27 23:57:26 +0000166static __inline__
Eric Christopherd1428bf2013-08-31 00:22:48 +0000167unsigned char _bittest(long const *, long);
Warren Huntd6ffae92013-09-27 23:57:26 +0000168static __inline__
Eric Christopherd1428bf2013-08-31 00:22:48 +0000169unsigned char _bittestandcomplement(long *, long);
Warren Huntd6ffae92013-09-27 23:57:26 +0000170static __inline__
Eric Christopherd1428bf2013-08-31 00:22:48 +0000171unsigned char _bittestandreset(long *, long);
Warren Huntd6ffae92013-09-27 23:57:26 +0000172static __inline__
Eric Christopherd1428bf2013-08-31 00:22:48 +0000173unsigned char _bittestandset(long *, long);
174unsigned int _blcfill_u32(unsigned int);
175unsigned int _blci_u32(unsigned int);
176unsigned int _blcic_u32(unsigned int);
177unsigned int _blcmsk_u32(unsigned int);
178unsigned int _blcs_u32(unsigned int);
179unsigned int _blsfill_u32(unsigned int);
180unsigned int _blsi_u32(unsigned int);
181unsigned int _blsic_u32(unsigned int);
182unsigned int _blsmsk_u32(unsigned int);
Eric Christopherd1428bf2013-08-31 00:22:48 +0000183unsigned int _blsr_u32(unsigned int);
184unsigned __int64 __cdecl _byteswap_uint64(unsigned __int64);
185unsigned long __cdecl _byteswap_ulong(unsigned long);
186unsigned short __cdecl _byteswap_ushort(unsigned short);
187unsigned _bzhi_u32(unsigned int, unsigned int);
188void __cdecl _disable(void);
189void __cdecl _enable(void);
190void __cdecl _fxrstor(void const *);
191void __cdecl _fxsave(void *);
192long _InterlockedAddLargeStatistic(__int64 volatile *_Addend, long _Value);
Warren Huntd6ffae92013-09-27 23:57:26 +0000193static __inline__
Eric Christopherd1428bf2013-08-31 00:22:48 +0000194long _InterlockedAnd(long volatile *_Value, long _Mask);
Warren Huntd6ffae92013-09-27 23:57:26 +0000195static __inline__
Eric Christopherd1428bf2013-08-31 00:22:48 +0000196short _InterlockedAnd16(short volatile *_Value, short _Mask);
Warren Huntd6ffae92013-09-27 23:57:26 +0000197static __inline__
Eric Christopherd1428bf2013-08-31 00:22:48 +0000198char _InterlockedAnd8(char volatile *_Value, char _Mask);
199unsigned char _interlockedbittestandreset(long volatile *, long);
Hans Wennborga3169332014-03-13 17:05:09 +0000200static __inline__
Eric Christopherd1428bf2013-08-31 00:22:48 +0000201unsigned char _interlockedbittestandset(long volatile *, long);
Warren Huntd6ffae92013-09-27 23:57:26 +0000202static __inline__
Eric Christopherd1428bf2013-08-31 00:22:48 +0000203long __cdecl _InterlockedCompareExchange(long volatile *_Destination,
204 long _Exchange, long _Comparand);
205long _InterlockedCompareExchange_HLEAcquire(long volatile *, long, long);
206long _InterlockedCompareExchange_HLERelease(long volatile *, long, long);
Warren Huntd6ffae92013-09-27 23:57:26 +0000207static __inline__
Eric Christopherd1428bf2013-08-31 00:22:48 +0000208short _InterlockedCompareExchange16(short volatile *_Destination,
209 short _Exchange, short _Comparand);
Warren Huntd6ffae92013-09-27 23:57:26 +0000210static __inline__
Eric Christopherd1428bf2013-08-31 00:22:48 +0000211__int64 _InterlockedCompareExchange64(__int64 volatile *_Destination,
212 __int64 _Exchange, __int64 _Comparand);
213__int64 _InterlockedcompareExchange64_HLEAcquire(__int64 volatile *, __int64,
214 __int64);
215__int64 _InterlockedCompareExchange64_HLERelease(__int64 volatile *, __int64,
216 __int64);
Warren Huntd6ffae92013-09-27 23:57:26 +0000217static __inline__
Eric Christopherd1428bf2013-08-31 00:22:48 +0000218char _InterlockedCompareExchange8(char volatile *_Destination, char _Exchange,
219 char _Comparand);
220void *_InterlockedCompareExchangePointer_HLEAcquire(void *volatile *, void *,
221 void *);
222void *_InterlockedCompareExchangePointer_HLERelease(void *volatile *, void *,
223 void *);
Warren Huntd6ffae92013-09-27 23:57:26 +0000224static __inline__
Eric Christopherd1428bf2013-08-31 00:22:48 +0000225long __cdecl _InterlockedDecrement(long volatile *_Addend);
Warren Huntd6ffae92013-09-27 23:57:26 +0000226static __inline__
Eric Christopherd1428bf2013-08-31 00:22:48 +0000227short _InterlockedDecrement16(short volatile *_Addend);
Saleem Abdulrasool114efe02014-06-18 20:51:10 +0000228long _InterlockedExchange(long volatile *_Target, long _Value);
Warren Huntd6ffae92013-09-27 23:57:26 +0000229static __inline__
Eric Christopherd1428bf2013-08-31 00:22:48 +0000230short _InterlockedExchange16(short volatile *_Target, short _Value);
Warren Huntd6ffae92013-09-27 23:57:26 +0000231static __inline__
Eric Christopherd1428bf2013-08-31 00:22:48 +0000232char _InterlockedExchange8(char volatile *_Target, char _Value);
Warren Huntd6ffae92013-09-27 23:57:26 +0000233static __inline__
Eric Christopherd1428bf2013-08-31 00:22:48 +0000234long __cdecl _InterlockedExchangeAdd(long volatile *_Addend, long _Value);
235long _InterlockedExchangeAdd_HLEAcquire(long volatile *, long);
236long _InterlockedExchangeAdd_HLERelease(long volatile *, long);
Reid Kleckner924eb2a2014-01-27 18:48:02 +0000237static __inline__
Eric Christopher439137e2014-01-24 12:13:47 +0000238short _InterlockedExchangeAdd16(short volatile *_Addend, short _Value);
239__int64 _InterlockedExchangeAdd64_HLEAcquire(__int64 volatile *, __int64);
240__int64 _InterlockedExchangeAdd64_HLERelease(__int64 volatile *, __int64);
Warren Huntd6ffae92013-09-27 23:57:26 +0000241static __inline__
Eric Christopherd1428bf2013-08-31 00:22:48 +0000242char _InterlockedExchangeAdd8(char volatile *_Addend, char _Value);
Warren Huntd6ffae92013-09-27 23:57:26 +0000243static __inline__
Eric Christopherd1428bf2013-08-31 00:22:48 +0000244long __cdecl _InterlockedIncrement(long volatile *_Addend);
Warren Huntd6ffae92013-09-27 23:57:26 +0000245static __inline__
Eric Christopherd1428bf2013-08-31 00:22:48 +0000246short _InterlockedIncrement16(short volatile *_Addend);
Warren Huntd6ffae92013-09-27 23:57:26 +0000247static __inline__
Eric Christopherd1428bf2013-08-31 00:22:48 +0000248long _InterlockedOr(long volatile *_Value, long _Mask);
Warren Huntd6ffae92013-09-27 23:57:26 +0000249static __inline__
Eric Christopherd1428bf2013-08-31 00:22:48 +0000250short _InterlockedOr16(short volatile *_Value, short _Mask);
Warren Huntd6ffae92013-09-27 23:57:26 +0000251static __inline__
Eric Christopherd1428bf2013-08-31 00:22:48 +0000252char _InterlockedOr8(char volatile *_Value, char _Mask);
Warren Huntd6ffae92013-09-27 23:57:26 +0000253static __inline__
Eric Christopherd1428bf2013-08-31 00:22:48 +0000254long _InterlockedXor(long volatile *_Value, long _Mask);
Warren Huntd6ffae92013-09-27 23:57:26 +0000255static __inline__
Eric Christopherd1428bf2013-08-31 00:22:48 +0000256short _InterlockedXor16(short volatile *_Value, short _Mask);
Warren Huntd6ffae92013-09-27 23:57:26 +0000257static __inline__
Eric Christopherd1428bf2013-08-31 00:22:48 +0000258char _InterlockedXor8(char volatile *_Value, char _Mask);
259void __cdecl _invpcid(unsigned int, void *);
Warren Huntd6ffae92013-09-27 23:57:26 +0000260static __inline__
Eric Christopherd1428bf2013-08-31 00:22:48 +0000261unsigned long __cdecl _lrotl(unsigned long, int);
Warren Huntd6ffae92013-09-27 23:57:26 +0000262static __inline__
Eric Christopherd1428bf2013-08-31 00:22:48 +0000263unsigned long __cdecl _lrotr(unsigned long, int);
Warren Huntd6ffae92013-09-27 23:57:26 +0000264static __inline__
Eric Christopherd1428bf2013-08-31 00:22:48 +0000265unsigned int _lzcnt_u32(unsigned int);
Reid Kleckner00d33a52013-10-17 01:29:48 +0000266static __inline__
Eric Christopherd1428bf2013-08-31 00:22:48 +0000267void _ReadBarrier(void);
Reid Kleckner00d33a52013-10-17 01:29:48 +0000268static __inline__
Eric Christopherd1428bf2013-08-31 00:22:48 +0000269void _ReadWriteBarrier(void);
Warren Huntd6ffae92013-09-27 23:57:26 +0000270static __inline__
Eric Christopherd1428bf2013-08-31 00:22:48 +0000271void *_ReturnAddress(void);
272unsigned int _rorx_u32(unsigned int, const unsigned int);
273int __cdecl _rdrand16_step(unsigned short *);
274int __cdecl _rdrand32_step(unsigned int *);
Warren Huntd6ffae92013-09-27 23:57:26 +0000275static __inline__
Eric Christopherfb4b4332013-08-31 00:27:38 +0000276unsigned int __cdecl _rotl(unsigned int _Value, int _Shift);
Warren Huntd6ffae92013-09-27 23:57:26 +0000277static __inline__
Eric Christopherd1428bf2013-08-31 00:22:48 +0000278unsigned short _rotl16(unsigned short _Value, unsigned char _Shift);
Warren Huntd6ffae92013-09-27 23:57:26 +0000279static __inline__
Eric Christopherd1428bf2013-08-31 00:22:48 +0000280unsigned __int64 __cdecl _rotl64(unsigned __int64 _Value, int _Shift);
Warren Huntd6ffae92013-09-27 23:57:26 +0000281static __inline__
Eric Christopherd1428bf2013-08-31 00:22:48 +0000282unsigned char _rotl8(unsigned char _Value, unsigned char _Shift);
Warren Huntd6ffae92013-09-27 23:57:26 +0000283static __inline__
Eric Christopherd1428bf2013-08-31 00:22:48 +0000284unsigned int __cdecl _rotr(unsigned int _Value, int _Shift);
Warren Huntd6ffae92013-09-27 23:57:26 +0000285static __inline__
Eric Christopherd1428bf2013-08-31 00:22:48 +0000286unsigned short _rotr16(unsigned short _Value, unsigned char _Shift);
Warren Huntd6ffae92013-09-27 23:57:26 +0000287static __inline__
Eric Christopherd1428bf2013-08-31 00:22:48 +0000288unsigned __int64 __cdecl _rotr64(unsigned __int64 _Value, int _Shift);
Warren Huntd6ffae92013-09-27 23:57:26 +0000289static __inline__
Eric Christopherd1428bf2013-08-31 00:22:48 +0000290unsigned char _rotr8(unsigned char _Value, unsigned char _Shift);
291int _sarx_i32(int, unsigned int);
Hans Wennborg1fd6dd32014-01-28 23:01:59 +0000292int __cdecl _setjmp(jmp_buf);
Eric Christopherd1428bf2013-08-31 00:22:48 +0000293unsigned int _shlx_u32(unsigned int, unsigned int);
294unsigned int _shrx_u32(unsigned int, unsigned int);
295void _Store_HLERelease(long volatile *, long);
296void _Store64_HLERelease(__int64 volatile *, __int64);
297void _StorePointer_HLERelease(void *volatile *, void *);
298unsigned int _t1mskc_u32(unsigned int);
299unsigned int _tzcnt_u32(unsigned int);
Eric Christopherd1428bf2013-08-31 00:22:48 +0000300unsigned int _tzmsk_u32(unsigned int);
Reid Kleckner00d33a52013-10-17 01:29:48 +0000301static __inline__
Eric Christopherd1428bf2013-08-31 00:22:48 +0000302void _WriteBarrier(void);
303void _xabort(const unsigned int imm);
304unsigned __int32 xbegin(void);
305void _xend(void);
Hans Wennborg854f7d32014-01-16 23:39:35 +0000306static __inline__
Eric Christopherd1428bf2013-08-31 00:22:48 +0000307unsigned __int64 __cdecl _xgetbv(unsigned int);
308void __cdecl _xrstor(void const *, unsigned __int64);
309void __cdecl _xsave(void *, unsigned __int64);
310void __cdecl _xsaveopt(void *, unsigned __int64);
311void __cdecl _xsetbv(unsigned int, unsigned __int64);
312unsigned char _xtest(void);
313
314/* These additional intrinsics are turned on in x64/amd64/x86_64 mode. */
Warren Hunt3f987942013-09-30 21:08:05 +0000315#ifdef __x86_64__
Eric Christopherd1428bf2013-08-31 00:22:48 +0000316void __addgsbyte(unsigned long, unsigned char);
317void __addgsdword(unsigned long, unsigned long);
318void __addgsqword(unsigned long, unsigned __int64);
319void __addgsword(unsigned long, unsigned short);
Reid Klecknerf08d6582014-01-27 19:16:35 +0000320static __inline__
Eric Christopherd1428bf2013-08-31 00:22:48 +0000321void __faststorefence(void);
322void __incgsbyte(unsigned long);
323void __incgsdword(unsigned long);
324void __incgsqword(unsigned long);
325void __incgsword(unsigned long);
Eric Christopher439137e2014-01-24 12:13:47 +0000326unsigned char __lwpins64(unsigned __int64, unsigned int, unsigned int);
327void __lwpval64(unsigned __int64, unsigned int, unsigned int);
328unsigned __int64 __lzcnt64(unsigned __int64);
Hans Wennborgd9be72e2014-03-12 22:00:32 +0000329static __inline__
Eric Christopher439137e2014-01-24 12:13:47 +0000330void __movsq(unsigned long long *, unsigned long long const *, size_t);
331__int64 __mulh(__int64, __int64);
Reid Kleckner924eb2a2014-01-27 18:48:02 +0000332static __inline__
Eric Christopherd1428bf2013-08-31 00:22:48 +0000333unsigned __int64 __popcnt64(unsigned __int64);
Hans Wennborga4421e02014-03-12 21:09:05 +0000334static __inline__
Eric Christopher439137e2014-01-24 12:13:47 +0000335unsigned char __readgsbyte(unsigned long);
Hans Wennborga4421e02014-03-12 21:09:05 +0000336static __inline__
Eric Christopher439137e2014-01-24 12:13:47 +0000337unsigned long __readgsdword(unsigned long);
Hans Wennborga4421e02014-03-12 21:09:05 +0000338static __inline__
339unsigned __int64 __readgsqword(unsigned long);
Eric Christopher439137e2014-01-24 12:13:47 +0000340unsigned short __readgsword(unsigned long);
Eric Christopherd1428bf2013-08-31 00:22:48 +0000341unsigned __int64 __shiftleft128(unsigned __int64 _LowPart,
342 unsigned __int64 _HighPart,
343 unsigned char _Shift);
344unsigned __int64 __shiftright128(unsigned __int64 _LowPart,
345 unsigned __int64 _HighPart,
346 unsigned char _Shift);
Hans Wennborgd9be72e2014-03-12 22:00:32 +0000347static __inline__
Eric Christopherd1428bf2013-08-31 00:22:48 +0000348void __stosq(unsigned __int64 *, unsigned __int64, size_t);
Eric Christopher439137e2014-01-24 12:13:47 +0000349unsigned __int64 __umulh(unsigned __int64, unsigned __int64);
350unsigned char __vmx_on(unsigned __int64 *);
351unsigned char __vmx_vmclear(unsigned __int64 *);
352unsigned char __vmx_vmlaunch(void);
353unsigned char __vmx_vmptrld(unsigned __int64 *);
354unsigned char __vmx_vmread(size_t, size_t *);
355unsigned char __vmx_vmresume(void);
356unsigned char __vmx_vmwrite(size_t, size_t);
357void __writegsbyte(unsigned long, unsigned char);
358void __writegsdword(unsigned long, unsigned long);
359void __writegsqword(unsigned long, unsigned __int64);
360void __writegsword(unsigned long, unsigned short);
Eric Christopherd1428bf2013-08-31 00:22:48 +0000361unsigned __int64 _andn_u64(unsigned __int64, unsigned __int64);
362unsigned __int64 _bextr_u64(unsigned __int64, unsigned int, unsigned int);
363unsigned __int64 _bextri_u64(unsigned __int64, unsigned int);
Warren Huntd6ffae92013-09-27 23:57:26 +0000364static __inline__
Eric Christopherd1428bf2013-08-31 00:22:48 +0000365unsigned char _BitScanForward64(unsigned long *_Index, unsigned __int64 _Mask);
Warren Huntd6ffae92013-09-27 23:57:26 +0000366static __inline__
Eric Christopherd1428bf2013-08-31 00:22:48 +0000367unsigned char _BitScanReverse64(unsigned long *_Index, unsigned __int64 _Mask);
Warren Huntd6ffae92013-09-27 23:57:26 +0000368static __inline__
Eric Christopherd1428bf2013-08-31 00:22:48 +0000369unsigned char _bittest64(__int64 const *, __int64);
Warren Huntd6ffae92013-09-27 23:57:26 +0000370static __inline__
Eric Christopherd1428bf2013-08-31 00:22:48 +0000371unsigned char _bittestandcomplement64(__int64 *, __int64);
Warren Huntd6ffae92013-09-27 23:57:26 +0000372static __inline__
Eric Christopherd1428bf2013-08-31 00:22:48 +0000373unsigned char _bittestandreset64(__int64 *, __int64);
Warren Huntd6ffae92013-09-27 23:57:26 +0000374static __inline__
Eric Christopherd1428bf2013-08-31 00:22:48 +0000375unsigned char _bittestandset64(__int64 *, __int64);
376unsigned __int64 _blcfill_u64(unsigned __int64);
377unsigned __int64 _blci_u64(unsigned __int64);
378unsigned __int64 _blcic_u64(unsigned __int64);
379unsigned __int64 _blcmsk_u64(unsigned __int64);
380unsigned __int64 _blcs_u64(unsigned __int64);
381unsigned __int64 _blsfill_u64(unsigned __int64);
382unsigned __int64 _blsi_u64(unsigned __int64);
383unsigned __int64 _blsic_u64(unsigned __int64);
Eric Christopher439137e2014-01-24 12:13:47 +0000384unsigned __int64 _blsmsk_u64(unsigned __int64);
Eric Christopherd1428bf2013-08-31 00:22:48 +0000385unsigned __int64 _blsr_u64(unsigned __int64);
386unsigned __int64 __cdecl _byteswap_uint64(unsigned __int64);
387unsigned __int64 _bzhi_u64(unsigned __int64, unsigned int);
388void __cdecl _fxrstor64(void const *);
389void __cdecl _fxsave64(void *);
390long _InterlockedAnd_np(long volatile *_Value, long _Mask);
391short _InterlockedAnd16_np(short volatile *_Value, short _Mask);
392__int64 _InterlockedAnd64_np(__int64 volatile *_Value, __int64 _Mask);
393char _InterlockedAnd8_np(char volatile *_Value, char _Mask);
394unsigned char _interlockedbittestandreset64(__int64 volatile *, __int64);
Hans Wennborga3169332014-03-13 17:05:09 +0000395static __inline__
Eric Christopherd1428bf2013-08-31 00:22:48 +0000396unsigned char _interlockedbittestandset64(__int64 volatile *, __int64);
397long _InterlockedCompareExchange_np(long volatile *_Destination, long _Exchange,
398 long _Comparand);
399unsigned char _InterlockedCompareExchange128(__int64 volatile *_Destination,
400 __int64 _ExchangeHigh,
401 __int64 _ExchangeLow,
402 __int64 *_CompareandResult);
403unsigned char _InterlockedCompareExchange128_np(__int64 volatile *_Destination,
404 __int64 _ExchangeHigh,
405 __int64 _ExchangeLow,
406 __int64 *_ComparandResult);
407short _InterlockedCompareExchange16_np(short volatile *_Destination,
408 short _Exchange, short _Comparand);
Eric Christopher439137e2014-01-24 12:13:47 +0000409__int64 _InterlockedCompareExchange64_HLEAcquire(__int64 volatile *, __int64,
410 __int64);
411__int64 _InterlockedCompareExchange64_HLERelease(__int64 volatile *, __int64,
412 __int64);
Eric Christopherd1428bf2013-08-31 00:22:48 +0000413__int64 _InterlockedCompareExchange64_np(__int64 volatile *_Destination,
414 __int64 _Exchange, __int64 _Comparand);
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}
Saleem Abdulrasool702eefe2014-06-25 16:48:40 +0000577#if defined(__i386__) || defined(__x86_64__)
Hans Wennborga3169332014-03-13 17:05:09 +0000578static __inline__ unsigned char __attribute__((__always_inline__, __nodebug__))
579_interlockedbittestandset(long volatile *__BitBase, long __BitPos) {
580 unsigned char __Res;
581 __asm__ ("xor %0, %0\n"
582 "lock bts %2, %1\n"
583 "setc %0\n"
584 : "=r" (__Res), "+m"(*__BitBase)
585 : "Ir"(__BitPos));
586 return __Res;
587}
Saleem Abdulrasool702eefe2014-06-25 16:48:40 +0000588#endif
Warren Hunt3f987942013-09-30 21:08:05 +0000589#ifdef __x86_64__
Warren Huntd6ffae92013-09-27 23:57:26 +0000590static __inline__ unsigned char __attribute__((__always_inline__, __nodebug__))
591_BitScanForward64(unsigned long *_Index, unsigned __int64 _Mask) {
592 if (!_Mask)
593 return 0;
594 *_Index = __builtin_ctzll(_Mask);
595 return 1;
596}
597static __inline__ unsigned char __attribute__((__always_inline__, __nodebug__))
598_BitScanReverse64(unsigned long *_Index, unsigned __int64 _Mask) {
599 if (!_Mask)
600 return 0;
601 *_Index = 63 - __builtin_clzll(_Mask);
602 return 1;
603}
604static
605__inline__ unsigned __int64 __attribute__((__always_inline__, __nodebug__))
606_lzcnt_u64(unsigned __int64 a) {
607 if (!a)
608 return 64;
609 return __builtin_clzll(a);
610}
611static __inline__
612unsigned __int64 __attribute__((__always_inline__, __nodebug__))
613 __popcnt64(unsigned __int64 value) {
614 return __builtin_popcountll(value);
615}
616static __inline__ unsigned char __attribute__((__always_inline__, __nodebug__))
617_bittest64(__int64 const *a, __int64 b) {
618 return (*a >> b) & 1;
619}
620static __inline__ unsigned char __attribute__((__always_inline__, __nodebug__))
621_bittestandcomplement64(__int64 *a, __int64 b) {
622 unsigned char x = (*a >> b) & 1;
623 *a = *a ^ (1ll << b);
624 return x;
625}
626static __inline__ unsigned char __attribute__((__always_inline__, __nodebug__))
627_bittestandreset64(__int64 *a, __int64 b) {
628 unsigned char x = (*a >> b) & 1;
629 *a = *a & ~(1ll << b);
630 return x;
631}
632static __inline__ unsigned char __attribute__((__always_inline__, __nodebug__))
633_bittestandset64(__int64 *a, __int64 b) {
634 unsigned char x = (*a >> b) & 1;
Warren Hunt2731e3e2013-10-01 17:12:40 +0000635 *a = *a | (1ll << b);
Warren Huntd6ffae92013-09-27 23:57:26 +0000636 return x;
637}
Hans Wennborga3169332014-03-13 17:05:09 +0000638static __inline__ unsigned char __attribute__((__always_inline__, __nodebug__))
639_interlockedbittestandset64(__int64 volatile *__BitBase, __int64 __BitPos) {
640 unsigned char __Res;
641 __asm__ ("xor %0, %0\n"
642 "lock bts %2, %1\n"
643 "setc %0\n"
644 : "=r" (__Res), "+m"(*__BitBase)
645 : "Ir"(__BitPos));
646 return __Res;
647}
Warren Huntd6ffae92013-09-27 23:57:26 +0000648#endif
649/*----------------------------------------------------------------------------*\
650|* Interlocked Exchange Add
651\*----------------------------------------------------------------------------*/
652static __inline__ char __attribute__((__always_inline__, __nodebug__))
653_InterlockedExchangeAdd8(char volatile *_Addend, char _Value) {
654 return __atomic_add_fetch(_Addend, _Value, 0) - _Value;
655}
656static __inline__ short __attribute__((__always_inline__, __nodebug__))
657_InterlockedExchangeAdd16(short volatile *_Addend, short _Value) {
658 return __atomic_add_fetch(_Addend, _Value, 0) - _Value;
659}
Warren Hunt3f987942013-09-30 21:08:05 +0000660#ifdef __x86_64__
Warren Huntd6ffae92013-09-27 23:57:26 +0000661static __inline__ __int64 __attribute__((__always_inline__, __nodebug__))
662_InterlockedExchangeAdd64(__int64 volatile *_Addend, __int64 _Value) {
663 return __atomic_add_fetch(_Addend, _Value, 0) - _Value;
664}
665#endif
666/*----------------------------------------------------------------------------*\
667|* Interlocked Exchange Sub
668\*----------------------------------------------------------------------------*/
669static __inline__ char __attribute__((__always_inline__, __nodebug__))
670_InterlockedExchangeSub8(char volatile *_Subend, char _Value) {
671 return __atomic_sub_fetch(_Subend, _Value, 0) + _Value;
672}
673static __inline__ short __attribute__((__always_inline__, __nodebug__))
674_InterlockedExchangeSub16(short volatile *_Subend, short _Value) {
675 return __atomic_sub_fetch(_Subend, _Value, 0) + _Value;
676}
677static __inline__ long __attribute__((__always_inline__, __nodebug__))
678_InterlockedExchangeSub(long volatile *_Subend, long _Value) {
679 return __atomic_sub_fetch(_Subend, _Value, 0) + _Value;
680}
Warren Hunt3f987942013-09-30 21:08:05 +0000681#ifdef __x86_64__
Warren Huntd6ffae92013-09-27 23:57:26 +0000682static __inline__ __int64 __attribute__((__always_inline__, __nodebug__))
683_InterlockedExchangeSub64(__int64 volatile *_Subend, __int64 _Value) {
684 return __atomic_sub_fetch(_Subend, _Value, 0) + _Value;
685}
686#endif
687/*----------------------------------------------------------------------------*\
688|* Interlocked Increment
689\*----------------------------------------------------------------------------*/
Hans Wennborg2ed88802014-01-23 19:15:39 +0000690static __inline__ short __attribute__((__always_inline__, __nodebug__))
691_InterlockedIncrement16(short volatile *_Value) {
Warren Huntd6ffae92013-09-27 23:57:26 +0000692 return __atomic_add_fetch(_Value, 1, 0);
693}
Warren Hunt3f987942013-09-30 21:08:05 +0000694#ifdef __x86_64__
Warren Huntd6ffae92013-09-27 23:57:26 +0000695static __inline__ __int64 __attribute__((__always_inline__, __nodebug__))
696_InterlockedIncrement64(__int64 volatile *_Value) {
697 return __atomic_add_fetch(_Value, 1, 0);
698}
699#endif
700/*----------------------------------------------------------------------------*\
701|* Interlocked Decrement
702\*----------------------------------------------------------------------------*/
Hans Wennborg2ed88802014-01-23 19:15:39 +0000703static __inline__ short __attribute__((__always_inline__, __nodebug__))
704_InterlockedDecrement16(short volatile *_Value) {
Warren Huntd6ffae92013-09-27 23:57:26 +0000705 return __atomic_sub_fetch(_Value, 1, 0);
706}
Warren Hunt3f987942013-09-30 21:08:05 +0000707#ifdef __x86_64__
Warren Huntd6ffae92013-09-27 23:57:26 +0000708static __inline__ __int64 __attribute__((__always_inline__, __nodebug__))
709_InterlockedDecrement64(__int64 volatile *_Value) {
710 return __atomic_sub_fetch(_Value, 1, 0);
711}
712#endif
713/*----------------------------------------------------------------------------*\
714|* Interlocked And
715\*----------------------------------------------------------------------------*/
716static __inline__ char __attribute__((__always_inline__, __nodebug__))
717_InterlockedAnd8(char volatile *_Value, char _Mask) {
718 return __atomic_and_fetch(_Value, _Mask, 0);
719}
720static __inline__ short __attribute__((__always_inline__, __nodebug__))
721_InterlockedAnd16(short volatile *_Value, short _Mask) {
722 return __atomic_and_fetch(_Value, _Mask, 0);
723}
724static __inline__ long __attribute__((__always_inline__, __nodebug__))
725_InterlockedAnd(long volatile *_Value, long _Mask) {
726 return __atomic_and_fetch(_Value, _Mask, 0);
727}
Warren Hunt3f987942013-09-30 21:08:05 +0000728#ifdef __x86_64__
Warren Huntd6ffae92013-09-27 23:57:26 +0000729static __inline__ __int64 __attribute__((__always_inline__, __nodebug__))
730_InterlockedAnd64(__int64 volatile *_Value, __int64 _Mask) {
731 return __atomic_and_fetch(_Value, _Mask, 0);
732}
733#endif
734/*----------------------------------------------------------------------------*\
735|* Interlocked Or
736\*----------------------------------------------------------------------------*/
737static __inline__ char __attribute__((__always_inline__, __nodebug__))
738_InterlockedOr8(char volatile *_Value, char _Mask) {
739 return __atomic_or_fetch(_Value, _Mask, 0);
740}
741static __inline__ short __attribute__((__always_inline__, __nodebug__))
742_InterlockedOr16(short volatile *_Value, short _Mask) {
743 return __atomic_or_fetch(_Value, _Mask, 0);
744}
745static __inline__ long __attribute__((__always_inline__, __nodebug__))
746_InterlockedOr(long volatile *_Value, long _Mask) {
747 return __atomic_or_fetch(_Value, _Mask, 0);
748}
Warren Hunt3f987942013-09-30 21:08:05 +0000749#ifdef __x86_64__
Warren Huntd6ffae92013-09-27 23:57:26 +0000750static __inline__ __int64 __attribute__((__always_inline__, __nodebug__))
751_InterlockedOr64(__int64 volatile *_Value, __int64 _Mask) {
752 return __atomic_or_fetch(_Value, _Mask, 0);
753}
754#endif
755/*----------------------------------------------------------------------------*\
756|* Interlocked Xor
757\*----------------------------------------------------------------------------*/
758static __inline__ char __attribute__((__always_inline__, __nodebug__))
759_InterlockedXor8(char volatile *_Value, char _Mask) {
760 return __atomic_xor_fetch(_Value, _Mask, 0);
761}
762static __inline__ short __attribute__((__always_inline__, __nodebug__))
763_InterlockedXor16(short volatile *_Value, short _Mask) {
764 return __atomic_xor_fetch(_Value, _Mask, 0);
765}
766static __inline__ long __attribute__((__always_inline__, __nodebug__))
767_InterlockedXor(long volatile *_Value, long _Mask) {
768 return __atomic_xor_fetch(_Value, _Mask, 0);
769}
Warren Hunt3f987942013-09-30 21:08:05 +0000770#ifdef __x86_64__
Warren Huntd6ffae92013-09-27 23:57:26 +0000771static __inline__ __int64 __attribute__((__always_inline__, __nodebug__))
772_InterlockedXor64(__int64 volatile *_Value, __int64 _Mask) {
773 return __atomic_xor_fetch(_Value, _Mask, 0);
774}
775#endif
776/*----------------------------------------------------------------------------*\
777|* Interlocked Exchange
778\*----------------------------------------------------------------------------*/
779static __inline__ char __attribute__((__always_inline__, __nodebug__))
780_InterlockedExchange8(char volatile *_Target, char _Value) {
781 __atomic_exchange(_Target, &_Value, &_Value, 0);
782 return _Value;
783}
784static __inline__ short __attribute__((__always_inline__, __nodebug__))
785_InterlockedExchange16(short volatile *_Target, short _Value) {
786 __atomic_exchange(_Target, &_Value, &_Value, 0);
787 return _Value;
788}
Warren Hunt3f987942013-09-30 21:08:05 +0000789#ifdef __x86_64__
Warren Huntd6ffae92013-09-27 23:57:26 +0000790static __inline__ __int64 __attribute__((__always_inline__, __nodebug__))
791_InterlockedExchange64(__int64 volatile *_Target, __int64 _Value) {
792 __atomic_exchange(_Target, &_Value, &_Value, 0);
793 return _Value;
794}
795#endif
796/*----------------------------------------------------------------------------*\
797|* Interlocked Compare Exchange
798\*----------------------------------------------------------------------------*/
799static __inline__ char __attribute__((__always_inline__, __nodebug__))
800_InterlockedCompareExchange8(char volatile *_Destination,
801 char _Exchange, char _Comparand) {
802 __atomic_compare_exchange(_Destination, &_Comparand, &_Exchange, 0, 0, 0);
803 return _Comparand;
804}
805static __inline__ short __attribute__((__always_inline__, __nodebug__))
806_InterlockedCompareExchange16(short volatile *_Destination,
807 short _Exchange, short _Comparand) {
808 __atomic_compare_exchange(_Destination, &_Comparand, &_Exchange, 0, 0, 0);
809 return _Comparand;
810}
Warren Huntd6ffae92013-09-27 23:57:26 +0000811static __inline__ __int64 __attribute__((__always_inline__, __nodebug__))
812_InterlockedCompareExchange64(__int64 volatile *_Destination,
813 __int64 _Exchange, __int64 _Comparand) {
814 __atomic_compare_exchange(_Destination, &_Comparand, &_Exchange, 0, 0, 0);
815 return _Comparand;
816}
Warren Huntd6ffae92013-09-27 23:57:26 +0000817/*----------------------------------------------------------------------------*\
Reid Kleckner00d33a52013-10-17 01:29:48 +0000818|* Barriers
819\*----------------------------------------------------------------------------*/
Saleem Abdulrasool702eefe2014-06-25 16:48:40 +0000820#if defined(__i386__) || defined(__x86_64__)
Reid Kleckner00d33a52013-10-17 01:29:48 +0000821static __inline__ void __attribute__((__always_inline__, __nodebug__))
822__attribute__((deprecated("use other intrinsics or C++11 atomics instead")))
823_ReadWriteBarrier(void) {
824 __asm__ volatile ("" : : : "memory");
825}
826static __inline__ void __attribute__((__always_inline__, __nodebug__))
827__attribute__((deprecated("use other intrinsics or C++11 atomics instead")))
828_ReadBarrier(void) {
829 __asm__ volatile ("" : : : "memory");
830}
831static __inline__ void __attribute__((__always_inline__, __nodebug__))
832__attribute__((deprecated("use other intrinsics or C++11 atomics instead")))
833_WriteBarrier(void) {
834 __asm__ volatile ("" : : : "memory");
835}
Saleem Abdulrasool702eefe2014-06-25 16:48:40 +0000836#endif
Reid Klecknerf08d6582014-01-27 19:16:35 +0000837#ifdef __x86_64__
838static __inline__ void __attribute__((__always_inline__, __nodebug__))
839__faststorefence(void) {
840 __asm__ volatile("lock orq $0, (%%rsp)" : : : "memory");
841}
842#endif
Reid Kleckner00d33a52013-10-17 01:29:48 +0000843/*----------------------------------------------------------------------------*\
Hans Wennborga4421e02014-03-12 21:09:05 +0000844|* readfs, readgs
845|* (Pointers in address space #256 and #257 are relative to the GS and FS
846|* segment registers, respectively.)
Hans Wennborg74ca0c42014-01-24 00:52:39 +0000847\*----------------------------------------------------------------------------*/
Hans Wennborg74ca0c42014-01-24 00:52:39 +0000848#define __ptr_to_addr_space(__addr_space_nbr, __type, __offset) \
849 ((volatile __type __attribute__((__address_space__(__addr_space_nbr)))*) \
850 (__offset))
851
Hans Wennborga4421e02014-03-12 21:09:05 +0000852#ifdef __i386__
Hans Wennborg74ca0c42014-01-24 00:52:39 +0000853static __inline__ unsigned char __attribute__((__always_inline__, __nodebug__))
854__readfsbyte(unsigned long __offset) {
855 return *__ptr_to_addr_space(257, unsigned char, __offset);
856}
857static __inline__ unsigned long __attribute__((__always_inline__, __nodebug__))
858__readfsdword(unsigned long __offset) {
859 return *__ptr_to_addr_space(257, unsigned long, __offset);
860}
861static __inline__ unsigned __int64 __attribute__((__always_inline__, __nodebug__))
862__readfsqword(unsigned long __offset) {
863 return *__ptr_to_addr_space(257, unsigned __int64, __offset);
864}
865static __inline__ unsigned short __attribute__((__always_inline__, __nodebug__))
866__readfsword(unsigned long __offset) {
867 return *__ptr_to_addr_space(257, unsigned short, __offset);
868}
Hans Wennborg74ca0c42014-01-24 00:52:39 +0000869#endif
Hans Wennborga4421e02014-03-12 21:09:05 +0000870#ifdef __x86_64__
871static __inline__ unsigned char __attribute__((__always_inline__, __nodebug__))
872__readgsbyte(unsigned long __offset) {
873 return *__ptr_to_addr_space(256, unsigned char, __offset);
874}
875static __inline__ unsigned long __attribute__((__always_inline__, __nodebug__))
876__readgsdword(unsigned long __offset) {
877 return *__ptr_to_addr_space(256, unsigned long, __offset);
878}
879static __inline__ unsigned __int64 __attribute__((__always_inline__, __nodebug__))
880__readgsqword(unsigned long __offset) {
881 return *__ptr_to_addr_space(256, unsigned __int64, __offset);
882}
883static __inline__ unsigned short __attribute__((__always_inline__, __nodebug__))
884__readgsword(unsigned long __offset) {
885 return *__ptr_to_addr_space(256, unsigned short, __offset);
886}
887#endif
888#undef __ptr_to_addr_space
Hans Wennborg74ca0c42014-01-24 00:52:39 +0000889/*----------------------------------------------------------------------------*\
Hans Wennborgd9be72e2014-03-12 22:00:32 +0000890|* movs, stos
891\*----------------------------------------------------------------------------*/
Saleem Abdulrasool702eefe2014-06-25 16:48:40 +0000892#if defined(__i386__) || defined(__x86_64__)
Hans Wennborgd9be72e2014-03-12 22:00:32 +0000893static __inline__ void __attribute__((__always_inline__, __nodebug__))
894__movsb(unsigned char *__dst, unsigned char const *__src, size_t __n) {
895 __asm__("rep movsb" : : "D"(__dst), "S"(__src), "c"(__n)
896 : "%edi", "%esi", "%ecx");
897}
898static __inline__ void __attribute__((__always_inline__, __nodebug__))
899__movsd(unsigned long *__dst, unsigned long const *__src, size_t __n) {
900 __asm__("rep movsl" : : "D"(__dst), "S"(__src), "c"(__n)
901 : "%edi", "%esi", "%ecx");
902}
903static __inline__ void __attribute__((__always_inline__, __nodebug__))
904__movsw(unsigned short *__dst, unsigned short const *__src, size_t __n) {
905 __asm__("rep movsh" : : "D"(__dst), "S"(__src), "c"(__n)
906 : "%edi", "%esi", "%ecx");
907}
908static __inline__ void __attribute__((__always_inline__, __nodebug__))
909__stosb(unsigned char *__dst, unsigned char __x, size_t __n) {
910 __asm__("rep stosb" : : "D"(__dst), "a"(__x), "c"(__n)
911 : "%edi", "%ecx");
912}
913static __inline__ void __attribute__((__always_inline__, __nodebug__))
914__stosd(unsigned long *__dst, unsigned long __x, size_t __n) {
915 __asm__("rep stosl" : : "D"(__dst), "a"(__x), "c"(__n)
916 : "%edi", "%ecx");
917}
918static __inline__ void __attribute__((__always_inline__, __nodebug__))
919__stosw(unsigned short *__dst, unsigned short __x, size_t __n) {
920 __asm__("rep stosh" : : "D"(__dst), "a"(__x), "c"(__n)
921 : "%edi", "%ecx");
922}
Saleem Abdulrasool702eefe2014-06-25 16:48:40 +0000923#endif
Hans Wennborgd9be72e2014-03-12 22:00:32 +0000924#ifdef __x86_64__
925static __inline__ void __attribute__((__always_inline__, __nodebug__))
926__movsq(unsigned long long *__dst, unsigned long long const *__src, size_t __n) {
927 __asm__("rep movsq" : : "D"(__dst), "S"(__src), "c"(__n)
928 : "%edi", "%esi", "%ecx");
929}
930static __inline__ void __attribute__((__always_inline__, __nodebug__))
931__stosq(unsigned __int64 *__dst, unsigned __int64 __x, size_t __n) {
932 __asm__("rep stosq" : : "D"(__dst), "a"(__x), "c"(__n)
933 : "%edi", "%ecx");
934}
935#endif
936
937/*----------------------------------------------------------------------------*\
Warren Huntd6ffae92013-09-27 23:57:26 +0000938|* Misc
939\*----------------------------------------------------------------------------*/
940static __inline__ void * __attribute__((__always_inline__, __nodebug__))
941_AddressOfReturnAddress(void) {
942 return (void*)((char*)__builtin_frame_address(0) + sizeof(void*));
943}
944static __inline__ void * __attribute__((__always_inline__, __nodebug__))
945_ReturnAddress(void) {
946 return __builtin_return_address(0);
947}
Saleem Abdulrasool702eefe2014-06-25 16:48:40 +0000948#if defined(__i386__) || defined(__x86_64__)
Hans Wennborg854f7d32014-01-16 23:39:35 +0000949static __inline__ void __attribute__((__always_inline__, __nodebug__))
950__cpuid(int __info[4], int __level) {
Hans Wennborg854f7d32014-01-16 23:39:35 +0000951 __asm__ ("cpuid" : "=a"(__info[0]), "=b" (__info[1]), "=c"(__info[2]), "=d"(__info[3])
Hans Wennborg12fb89e2014-01-31 19:44:55 +0000952 : "a"(__level));
953}
954static __inline__ void __attribute__((__always_inline__, __nodebug__))
955__cpuidex(int __info[4], int __level, int __ecx) {
956 __asm__ ("cpuid" : "=a"(__info[0]), "=b" (__info[1]), "=c"(__info[2]), "=d"(__info[3])
957 : "a"(__level), "c"(__ecx));
Hans Wennborg854f7d32014-01-16 23:39:35 +0000958}
959static __inline__ unsigned __int64 __cdecl __attribute__((__always_inline__, __nodebug__))
960_xgetbv(unsigned int __xcr_no) {
961 unsigned int __eax, __edx;
962 __asm__ ("xgetbv" : "=a" (__eax), "=d" (__edx) : "c" (__xcr_no));
963 return ((unsigned __int64)__edx << 32) | __eax;
964}
Hans Wennborg740a4d62014-01-28 22:55:01 +0000965static __inline__ void __attribute__((__always_inline__, __nodebug__))
966__halt(void) {
967 __asm__ volatile ("hlt");
968}
Saleem Abdulrasool702eefe2014-06-25 16:48:40 +0000969#endif
Warren Huntd6ffae92013-09-27 23:57:26 +0000970
Reid Kleckner592dc612014-04-08 00:28:22 +0000971/*----------------------------------------------------------------------------*\
972|* Privileged intrinsics
973\*----------------------------------------------------------------------------*/
Saleem Abdulrasool702eefe2014-06-25 16:48:40 +0000974#if defined(__i386__) || defined(__x86_64__)
Reid Kleckner592dc612014-04-08 00:28:22 +0000975static __inline__ unsigned __int64 __attribute__((__always_inline__, __nodebug__))
976__readmsr(unsigned long __register) {
977 // Loads the contents of a 64-bit model specific register (MSR) specified in
978 // the ECX register into registers EDX:EAX. The EDX register is loaded with
979 // the high-order 32 bits of the MSR and the EAX register is loaded with the
980 // low-order 32 bits. If less than 64 bits are implemented in the MSR being
981 // read, the values returned to EDX:EAX in unimplemented bit locations are
982 // undefined.
983 unsigned long __edx;
984 unsigned long __eax;
Reid Kleckner6df52542014-04-08 17:49:16 +0000985 __asm__ ("rdmsr" : "=d"(__edx), "=a"(__eax) : "c"(__register));
Reid Kleckner592dc612014-04-08 00:28:22 +0000986 return (((unsigned __int64)__edx) << 32) | (unsigned __int64)__eax;
987}
988
989static __inline__ unsigned long __attribute__((always_inline, __nodebug__))
990__readcr3(void) {
Reid Kleckner6df52542014-04-08 17:49:16 +0000991 unsigned long __cr3_val;
992 __asm__ __volatile__ ("mov %%cr3, %0" : "=q"(__cr3_val) : : "memory");
993 return __cr3_val;
Reid Kleckner592dc612014-04-08 00:28:22 +0000994}
995
996static __inline__ void __attribute__((always_inline, __nodebug__))
Reid Kleckner6df52542014-04-08 17:49:16 +0000997__writecr3(unsigned int __cr3_val) {
998 __asm__ ("mov %0, %%cr3" : : "q"(__cr3_val) : "memory");
Reid Kleckner592dc612014-04-08 00:28:22 +0000999}
Saleem Abdulrasool702eefe2014-06-25 16:48:40 +00001000#endif
Reid Kleckner592dc612014-04-08 00:28:22 +00001001
Eric Christophercc872532013-09-18 22:40:18 +00001002#ifdef __cplusplus
Eric Christopher0db88a72013-09-18 22:24:01 +00001003}
Eric Christopherd1428bf2013-08-31 00:22:48 +00001004#endif
Reid Klecknerf0e23222013-09-19 00:19:53 +00001005
1006#endif /* __INTRIN_H */
1007#endif /* _MSC_VER */