blob: b0fe2ce439505461818fd62f594180588d0295f6 [file] [log] [blame]
Eric Christopherd1428bf2013-08-31 00:22:48 +00001/* ===-------- Intrin.h ---------------------------------------------------===
2 *
3 * Permission is hereby granted, free of charge, to any person obtaining a copy
4 * of this software and associated documentation files (the "Software"), to deal
5 * in the Software without restriction, including without limitation the rights
6 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7 * copies of the Software, and to permit persons to whom the Software is
8 * furnished to do so, subject to the following conditions:
9 *
10 * The above copyright notice and this permission notice shall be included in
11 * all copies or substantial portions of the Software.
12 *
13 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19 * THE SOFTWARE.
20 *
21 *===-----------------------------------------------------------------------===
22 */
23
24/* Only include this if we're compiling for the windows platform. */
25#ifndef _MSC_VER
26#include_next <Intrin.h>
27#else
28
29#ifndef __INTRIN_H
30#define __INTRIN_H
31
32/* First include the standard intrinsics. */
33#include <x86intrin.h>
34
Eric Christopher0db88a72013-09-18 22:24:01 +000035#ifdef __cplusplus
36extern "C" {
37#endif
38
Eric Christopherd1428bf2013-08-31 00:22:48 +000039/* And the random ones that aren't in those files. */
40__m64 _m_from_float(float);
41__m64 _m_from_int(int _l);
42void _m_prefetch(void *);
43float _m_to_float(__m64);
44int _m_to_int(__m64 _M);
45
46/* Other assorted instruction intrinsics. */
47void __addfsbyte(unsigned long, unsigned char);
48void __addfsdword(unsigned long, unsigned long);
49void __addfsword(unsigned long, unsigned short);
50void __code_seg(const char *);
Hans Wennborg854f7d32014-01-16 23:39:35 +000051static __inline__
Eric Christopherd1428bf2013-08-31 00:22:48 +000052void __cpuid(int[4], int);
53void __cpuidex(int[4], int, int);
Eric Christopher0db88a72013-09-18 22:24:01 +000054void __debugbreak(void);
Eric Christopherd1428bf2013-08-31 00:22:48 +000055__int64 __emul(int, int);
56unsigned __int64 __emulu(unsigned int, unsigned int);
Eric Christopher0db88a72013-09-18 22:24:01 +000057void __cdecl __fastfail(unsigned int);
Eric Christopherd1428bf2013-08-31 00:22:48 +000058unsigned int __getcallerseflags(void);
59void __halt(void);
60unsigned char __inbyte(unsigned short);
61void __inbytestring(unsigned short, unsigned char *, unsigned long);
62void __incfsbyte(unsigned long);
63void __incfsdword(unsigned long);
64void __incfsword(unsigned long);
65unsigned long __indword(unsigned short);
66void __indwordstring(unsigned short, unsigned long *, unsigned long);
67void __int2c(void);
68void __invlpg(void *);
69unsigned short __inword(unsigned short);
70void __inwordstring(unsigned short, unsigned short *, unsigned long);
71void __lidt(void *);
72unsigned __int64 __ll_lshift(unsigned __int64, int);
73__int64 __ll_rshift(__int64, int);
74void __llwpcb(void *);
75unsigned char __lwpins32(unsigned int, unsigned int, unsigned int);
76void __lwpval32(unsigned int, unsigned int, unsigned int);
77unsigned int __lzcnt(unsigned int);
78unsigned short __lzcnt16(unsigned short);
79void __movsb(unsigned char *, unsigned char const *, size_t);
80void __movsd(unsigned long *, unsigned long const *, size_t);
81void __movsw(unsigned short *, unsigned short const *, size_t);
82void __nop(void);
83void __nvreg_restore_fence(void);
84void __nvreg_save_fence(void);
85void __outbyte(unsigned short, unsigned char);
86void __outbytestring(unsigned short, unsigned char *, unsigned long);
87void __outdword(unsigned short, unsigned long);
88void __outdwordstring(unsigned short, unsigned long *, unsigned long);
89void __outword(unsigned short, unsigned short);
90void __outwordstring(unsigned short, unsigned short *, unsigned long);
Warren Huntd6ffae92013-09-27 23:57:26 +000091static __inline__
Eric Christopherd1428bf2013-08-31 00:22:48 +000092unsigned int __popcnt(unsigned int);
Warren Huntd6ffae92013-09-27 23:57:26 +000093static __inline__
Eric Christopherd1428bf2013-08-31 00:22:48 +000094unsigned short __popcnt16(unsigned short);
95unsigned __int64 __rdtsc(void);
96unsigned __int64 __rdtscp(unsigned int *);
97unsigned long __readcr0(void);
98unsigned long __readcr2(void);
99unsigned long __readcr3(void);
100unsigned long __readcr5(void);
101unsigned long __readcr8(void);
102unsigned int __readdr(unsigned int);
103unsigned int __readeflags(void);
104unsigned char __readfsbyte(unsigned long);
105unsigned long __readfsdword(unsigned long);
106unsigned __int64 __readfsqword(unsigned long);
107unsigned short __readfsword(unsigned long);
108unsigned __int64 __readmsr(unsigned long);
109unsigned __int64 __readpmc(unsigned long);
110unsigned long __segmentlimit(unsigned long);
111void __sidt(void *);
112void *__slwpcb(void);
113void __stosb(unsigned char *, unsigned char, size_t);
114void __stosd(unsigned long *, unsigned long, size_t);
115void __stosw(unsigned short *, unsigned short, size_t);
116void __svm_clgi(void);
117void __svm_invlpga(void *, int);
118void __svm_skinit(int);
119void __svm_stgi(void);
120void __svm_vmload(size_t);
121void __svm_vmrun(size_t);
122void __svm_vmsave(size_t);
123void __ud2(void);
124unsigned __int64 __ull_rshift(unsigned __int64, int);
125void __vmx_off(void);
126void __vmx_vmptrst(unsigned __int64 *);
127void __wbinvd(void);
128void __writecr0(unsigned int);
129void __writecr3(unsigned int);
130void __writecr4(unsigned int);
131void __writecr8(unsigned int);
132void __writedr(unsigned int, unsigned int);
133void __writeeflags(unsigned int);
134void __writefsbyte(unsigned long, unsigned char);
135void __writefsdword(unsigned long, unsigned long);
136void __writefsqword(unsigned long, unsigned __int64);
137void __writefsword(unsigned long, unsigned short);
138void __writemsr(unsigned long, unsigned __int64);
Warren Huntd6ffae92013-09-27 23:57:26 +0000139static __inline__
Eric Christopherd1428bf2013-08-31 00:22:48 +0000140void *_AddressOfReturnAddress(void);
141unsigned int _andn_u32(unsigned int, unsigned int);
142unsigned int _bextr_u32(unsigned int, unsigned int, unsigned int);
143unsigned int _bextr_u32(unsigned int, unsigned int, unsigned int);
144unsigned int _bextri_u32(unsigned int, unsigned int);
Warren Huntd6ffae92013-09-27 23:57:26 +0000145static __inline__
Eric Christopherd1428bf2013-08-31 00:22:48 +0000146unsigned char _BitScanForward(unsigned long *_Index, unsigned long _Mask);
Warren Huntd6ffae92013-09-27 23:57:26 +0000147static __inline__
Eric Christopherd1428bf2013-08-31 00:22:48 +0000148unsigned char _BitScanReverse(unsigned long *_Index, unsigned long _Mask);
Warren Huntd6ffae92013-09-27 23:57:26 +0000149static __inline__
Eric Christopherd1428bf2013-08-31 00:22:48 +0000150unsigned char _bittest(long const *, long);
Warren Huntd6ffae92013-09-27 23:57:26 +0000151static __inline__
Eric Christopherd1428bf2013-08-31 00:22:48 +0000152unsigned char _bittestandcomplement(long *, long);
Warren Huntd6ffae92013-09-27 23:57:26 +0000153static __inline__
Eric Christopherd1428bf2013-08-31 00:22:48 +0000154unsigned char _bittestandreset(long *, long);
Warren Huntd6ffae92013-09-27 23:57:26 +0000155static __inline__
Eric Christopherd1428bf2013-08-31 00:22:48 +0000156unsigned char _bittestandset(long *, long);
157unsigned int _blcfill_u32(unsigned int);
158unsigned int _blci_u32(unsigned int);
159unsigned int _blcic_u32(unsigned int);
160unsigned int _blcmsk_u32(unsigned int);
161unsigned int _blcs_u32(unsigned int);
162unsigned int _blsfill_u32(unsigned int);
163unsigned int _blsi_u32(unsigned int);
164unsigned int _blsic_u32(unsigned int);
165unsigned int _blsmsk_u32(unsigned int);
166unsigned int _blsmsk_u32(unsigned int);
167unsigned int _blsr_u32(unsigned int);
168unsigned int _blsr_u32(unsigned int);
169unsigned __int64 __cdecl _byteswap_uint64(unsigned __int64);
170unsigned long __cdecl _byteswap_ulong(unsigned long);
171unsigned short __cdecl _byteswap_ushort(unsigned short);
172unsigned _bzhi_u32(unsigned int, unsigned int);
173void __cdecl _disable(void);
174void __cdecl _enable(void);
175void __cdecl _fxrstor(void const *);
176void __cdecl _fxsave(void *);
177long _InterlockedAddLargeStatistic(__int64 volatile *_Addend, long _Value);
Warren Huntd6ffae92013-09-27 23:57:26 +0000178static __inline__
Eric Christopherd1428bf2013-08-31 00:22:48 +0000179long _InterlockedAnd(long volatile *_Value, long _Mask);
Warren Huntd6ffae92013-09-27 23:57:26 +0000180static __inline__
Eric Christopherd1428bf2013-08-31 00:22:48 +0000181short _InterlockedAnd16(short volatile *_Value, short _Mask);
Warren Huntd6ffae92013-09-27 23:57:26 +0000182static __inline__
Eric Christopherd1428bf2013-08-31 00:22:48 +0000183char _InterlockedAnd8(char volatile *_Value, char _Mask);
184unsigned char _interlockedbittestandreset(long volatile *, long);
185unsigned char _interlockedbittestandset(long volatile *, long);
Warren Huntd6ffae92013-09-27 23:57:26 +0000186static __inline__
Eric Christopherd1428bf2013-08-31 00:22:48 +0000187long __cdecl _InterlockedCompareExchange(long volatile *_Destination,
188 long _Exchange, long _Comparand);
189long _InterlockedCompareExchange_HLEAcquire(long volatile *, long, long);
190long _InterlockedCompareExchange_HLERelease(long volatile *, long, long);
Warren Huntd6ffae92013-09-27 23:57:26 +0000191static __inline__
Eric Christopherd1428bf2013-08-31 00:22:48 +0000192short _InterlockedCompareExchange16(short volatile *_Destination,
193 short _Exchange, short _Comparand);
Warren Huntd6ffae92013-09-27 23:57:26 +0000194static __inline__
Eric Christopherd1428bf2013-08-31 00:22:48 +0000195__int64 _InterlockedCompareExchange64(__int64 volatile *_Destination,
196 __int64 _Exchange, __int64 _Comparand);
197__int64 _InterlockedcompareExchange64_HLEAcquire(__int64 volatile *, __int64,
198 __int64);
199__int64 _InterlockedCompareExchange64_HLERelease(__int64 volatile *, __int64,
200 __int64);
Warren Huntd6ffae92013-09-27 23:57:26 +0000201static __inline__
Eric Christopherd1428bf2013-08-31 00:22:48 +0000202char _InterlockedCompareExchange8(char volatile *_Destination, char _Exchange,
203 char _Comparand);
204void *_InterlockedCompareExchangePointer_HLEAcquire(void *volatile *, void *,
205 void *);
206void *_InterlockedCompareExchangePointer_HLERelease(void *volatile *, void *,
207 void *);
Warren Huntd6ffae92013-09-27 23:57:26 +0000208static __inline__
Eric Christopherd1428bf2013-08-31 00:22:48 +0000209long __cdecl _InterlockedDecrement(long volatile *_Addend);
Warren Huntd6ffae92013-09-27 23:57:26 +0000210static __inline__
Eric Christopherd1428bf2013-08-31 00:22:48 +0000211short _InterlockedDecrement16(short volatile *_Addend);
Warren Huntd6ffae92013-09-27 23:57:26 +0000212static __inline__
Eric Christopherd1428bf2013-08-31 00:22:48 +0000213long __cdecl _InterlockedExchange(long volatile *_Target, long _Value);
Warren Huntd6ffae92013-09-27 23:57:26 +0000214static __inline__
Eric Christopherd1428bf2013-08-31 00:22:48 +0000215short _InterlockedExchange16(short volatile *_Target, short _Value);
Warren Huntd6ffae92013-09-27 23:57:26 +0000216static __inline__
Eric Christopherd1428bf2013-08-31 00:22:48 +0000217char _InterlockedExchange8(char volatile *_Target, char _Value);
Warren Huntd6ffae92013-09-27 23:57:26 +0000218static __inline__
Eric Christopherd1428bf2013-08-31 00:22:48 +0000219long __cdecl _InterlockedExchangeAdd(long volatile *_Addend, long _Value);
220long _InterlockedExchangeAdd_HLEAcquire(long volatile *, long);
221long _InterlockedExchangeAdd_HLERelease(long volatile *, long);
Warren Huntd6ffae92013-09-27 23:57:26 +0000222static __inline__
Eric Christopherd1428bf2013-08-31 00:22:48 +0000223char _InterlockedExchangeAdd8(char volatile *_Addend, char _Value);
Warren Huntd6ffae92013-09-27 23:57:26 +0000224static __inline__
Eric Christopherd1428bf2013-08-31 00:22:48 +0000225long __cdecl _InterlockedIncrement(long volatile *_Addend);
Warren Huntd6ffae92013-09-27 23:57:26 +0000226static __inline__
Eric Christopherd1428bf2013-08-31 00:22:48 +0000227short _InterlockedIncrement16(short volatile *_Addend);
Warren Huntd6ffae92013-09-27 23:57:26 +0000228static __inline__
Eric Christopherd1428bf2013-08-31 00:22:48 +0000229long _InterlockedOr(long volatile *_Value, long _Mask);
Warren Huntd6ffae92013-09-27 23:57:26 +0000230static __inline__
Eric Christopherd1428bf2013-08-31 00:22:48 +0000231short _InterlockedOr16(short volatile *_Value, short _Mask);
Warren Huntd6ffae92013-09-27 23:57:26 +0000232static __inline__
Eric Christopherd1428bf2013-08-31 00:22:48 +0000233char _InterlockedOr8(char volatile *_Value, char _Mask);
Warren Huntd6ffae92013-09-27 23:57:26 +0000234static __inline__
Eric Christopherd1428bf2013-08-31 00:22:48 +0000235long _InterlockedXor(long volatile *_Value, long _Mask);
Warren Huntd6ffae92013-09-27 23:57:26 +0000236static __inline__
Eric Christopherd1428bf2013-08-31 00:22:48 +0000237short _InterlockedXor16(short volatile *_Value, short _Mask);
Warren Huntd6ffae92013-09-27 23:57:26 +0000238static __inline__
Eric Christopherd1428bf2013-08-31 00:22:48 +0000239char _InterlockedXor8(char volatile *_Value, char _Mask);
240void __cdecl _invpcid(unsigned int, void *);
Warren Huntd6ffae92013-09-27 23:57:26 +0000241static __inline__
Eric Christopherd1428bf2013-08-31 00:22:48 +0000242unsigned long __cdecl _lrotl(unsigned long, int);
Warren Huntd6ffae92013-09-27 23:57:26 +0000243static __inline__
Eric Christopherd1428bf2013-08-31 00:22:48 +0000244unsigned long __cdecl _lrotr(unsigned long, int);
Warren Huntd6ffae92013-09-27 23:57:26 +0000245static __inline__
Eric Christopherd1428bf2013-08-31 00:22:48 +0000246unsigned int _lzcnt_u32(unsigned int);
Reid Kleckner00d33a52013-10-17 01:29:48 +0000247static __inline__
Eric Christopherd1428bf2013-08-31 00:22:48 +0000248void _ReadBarrier(void);
Reid Kleckner00d33a52013-10-17 01:29:48 +0000249static __inline__
Eric Christopherd1428bf2013-08-31 00:22:48 +0000250void _ReadWriteBarrier(void);
Warren Huntd6ffae92013-09-27 23:57:26 +0000251static __inline__
Eric Christopherd1428bf2013-08-31 00:22:48 +0000252void *_ReturnAddress(void);
253unsigned int _rorx_u32(unsigned int, const unsigned int);
254int __cdecl _rdrand16_step(unsigned short *);
255int __cdecl _rdrand32_step(unsigned int *);
Warren Huntd6ffae92013-09-27 23:57:26 +0000256static __inline__
Eric Christopherfb4b4332013-08-31 00:27:38 +0000257unsigned int __cdecl _rotl(unsigned int _Value, int _Shift);
Warren Huntd6ffae92013-09-27 23:57:26 +0000258static __inline__
Eric Christopherd1428bf2013-08-31 00:22:48 +0000259unsigned short _rotl16(unsigned short _Value, unsigned char _Shift);
Warren Huntd6ffae92013-09-27 23:57:26 +0000260static __inline__
Eric Christopherd1428bf2013-08-31 00:22:48 +0000261unsigned __int64 __cdecl _rotl64(unsigned __int64 _Value, int _Shift);
Warren Huntd6ffae92013-09-27 23:57:26 +0000262static __inline__
Eric Christopherd1428bf2013-08-31 00:22:48 +0000263unsigned char _rotl8(unsigned char _Value, unsigned char _Shift);
Warren Huntd6ffae92013-09-27 23:57:26 +0000264static __inline__
Eric Christopherd1428bf2013-08-31 00:22:48 +0000265unsigned int __cdecl _rotr(unsigned int _Value, int _Shift);
Warren Huntd6ffae92013-09-27 23:57:26 +0000266static __inline__
Eric Christopherd1428bf2013-08-31 00:22:48 +0000267unsigned short _rotr16(unsigned short _Value, unsigned char _Shift);
Warren Huntd6ffae92013-09-27 23:57:26 +0000268static __inline__
Eric Christopherd1428bf2013-08-31 00:22:48 +0000269unsigned __int64 __cdecl _rotr64(unsigned __int64 _Value, int _Shift);
Warren Huntd6ffae92013-09-27 23:57:26 +0000270static __inline__
Eric Christopherd1428bf2013-08-31 00:22:48 +0000271unsigned char _rotr8(unsigned char _Value, unsigned char _Shift);
272int _sarx_i32(int, unsigned int);
273
274/* FIXME: Need definition for jmp_buf.
275 int __cdecl _setjmp(jmp_buf); */
276
277unsigned int _shlx_u32(unsigned int, unsigned int);
278unsigned int _shrx_u32(unsigned int, unsigned int);
279void _Store_HLERelease(long volatile *, long);
280void _Store64_HLERelease(__int64 volatile *, __int64);
281void _StorePointer_HLERelease(void *volatile *, void *);
282unsigned int _t1mskc_u32(unsigned int);
283unsigned int _tzcnt_u32(unsigned int);
284unsigned int _tzcnt_u32(unsigned int);
285unsigned int _tzmsk_u32(unsigned int);
Reid Kleckner00d33a52013-10-17 01:29:48 +0000286static __inline__
Eric Christopherd1428bf2013-08-31 00:22:48 +0000287void _WriteBarrier(void);
288void _xabort(const unsigned int imm);
289unsigned __int32 xbegin(void);
290void _xend(void);
Hans Wennborg854f7d32014-01-16 23:39:35 +0000291static __inline__
Eric Christopherd1428bf2013-08-31 00:22:48 +0000292unsigned __int64 __cdecl _xgetbv(unsigned int);
293void __cdecl _xrstor(void const *, unsigned __int64);
294void __cdecl _xsave(void *, unsigned __int64);
295void __cdecl _xsaveopt(void *, unsigned __int64);
296void __cdecl _xsetbv(unsigned int, unsigned __int64);
297unsigned char _xtest(void);
298
299/* These additional intrinsics are turned on in x64/amd64/x86_64 mode. */
Warren Hunt3f987942013-09-30 21:08:05 +0000300#ifdef __x86_64__
Eric Christopherd1428bf2013-08-31 00:22:48 +0000301void __addgsbyte(unsigned long, unsigned char);
302void __addgsdword(unsigned long, unsigned long);
303void __addgsqword(unsigned long, unsigned __int64);
304void __addgsword(unsigned long, unsigned short);
305void __faststorefence(void);
306void __incgsbyte(unsigned long);
307void __incgsdword(unsigned long);
308void __incgsqword(unsigned long);
309void __incgsword(unsigned long);
310unsigned __int64 __popcnt64(unsigned __int64);
311unsigned __int64 __shiftleft128(unsigned __int64 _LowPart,
312 unsigned __int64 _HighPart,
313 unsigned char _Shift);
314unsigned __int64 __shiftright128(unsigned __int64 _LowPart,
315 unsigned __int64 _HighPart,
316 unsigned char _Shift);
317void __stosq(unsigned __int64 *, unsigned __int64, size_t);
318unsigned __int64 _andn_u64(unsigned __int64, unsigned __int64);
319unsigned __int64 _bextr_u64(unsigned __int64, unsigned int, unsigned int);
320unsigned __int64 _bextri_u64(unsigned __int64, unsigned int);
Warren Huntd6ffae92013-09-27 23:57:26 +0000321static __inline__
Eric Christopherd1428bf2013-08-31 00:22:48 +0000322unsigned char _BitScanForward64(unsigned long *_Index, unsigned __int64 _Mask);
Warren Huntd6ffae92013-09-27 23:57:26 +0000323static __inline__
Eric Christopherd1428bf2013-08-31 00:22:48 +0000324unsigned char _BitScanReverse64(unsigned long *_Index, unsigned __int64 _Mask);
Warren Huntd6ffae92013-09-27 23:57:26 +0000325static __inline__
Eric Christopherd1428bf2013-08-31 00:22:48 +0000326unsigned char _bittest64(__int64 const *, __int64);
Warren Huntd6ffae92013-09-27 23:57:26 +0000327static __inline__
Eric Christopherd1428bf2013-08-31 00:22:48 +0000328unsigned char _bittestandcomplement64(__int64 *, __int64);
Warren Huntd6ffae92013-09-27 23:57:26 +0000329static __inline__
Eric Christopherd1428bf2013-08-31 00:22:48 +0000330unsigned char _bittestandreset64(__int64 *, __int64);
Warren Huntd6ffae92013-09-27 23:57:26 +0000331static __inline__
Eric Christopherd1428bf2013-08-31 00:22:48 +0000332unsigned char _bittestandset64(__int64 *, __int64);
333unsigned __int64 _blcfill_u64(unsigned __int64);
334unsigned __int64 _blci_u64(unsigned __int64);
335unsigned __int64 _blcic_u64(unsigned __int64);
336unsigned __int64 _blcmsk_u64(unsigned __int64);
337unsigned __int64 _blcs_u64(unsigned __int64);
338unsigned __int64 _blsfill_u64(unsigned __int64);
339unsigned __int64 _blsi_u64(unsigned __int64);
340unsigned __int64 _blsic_u64(unsigned __int64);
341unsigned __int64 _blmsk_u64(unsigned __int64);
342unsigned __int64 _blsr_u64(unsigned __int64);
343unsigned __int64 __cdecl _byteswap_uint64(unsigned __int64);
344unsigned __int64 _bzhi_u64(unsigned __int64, unsigned int);
345void __cdecl _fxrstor64(void const *);
346void __cdecl _fxsave64(void *);
347long _InterlockedAnd_np(long volatile *_Value, long _Mask);
348short _InterlockedAnd16_np(short volatile *_Value, short _Mask);
349__int64 _InterlockedAnd64_np(__int64 volatile *_Value, __int64 _Mask);
350char _InterlockedAnd8_np(char volatile *_Value, char _Mask);
351unsigned char _interlockedbittestandreset64(__int64 volatile *, __int64);
352unsigned char _interlockedbittestandset64(__int64 volatile *, __int64);
353long _InterlockedCompareExchange_np(long volatile *_Destination, long _Exchange,
354 long _Comparand);
355unsigned char _InterlockedCompareExchange128(__int64 volatile *_Destination,
356 __int64 _ExchangeHigh,
357 __int64 _ExchangeLow,
358 __int64 *_CompareandResult);
359unsigned char _InterlockedCompareExchange128_np(__int64 volatile *_Destination,
360 __int64 _ExchangeHigh,
361 __int64 _ExchangeLow,
362 __int64 *_ComparandResult);
363short _InterlockedCompareExchange16_np(short volatile *_Destination,
364 short _Exchange, short _Comparand);
365__int64 _InterlockedCompareExchange64_np(__int64 volatile *_Destination,
366 __int64 _Exchange, __int64 _Comparand);
367void *_InterlockedCompareExchangePointer_np(void *volatile *_Destination,
368 void *_Exchange, void *_Comparand);
369long _InterlockedOr_np(long volatile *_Value, long _Mask);
370short _InterlockedOr16_np(short volatile *_Value, short _Mask);
Warren Hunt41a993f2013-09-28 00:15:41 +0000371__int64 _InterlockedOr64_np(__int64 volatile *_Value, __int64 _Mask);
Eric Christopherd1428bf2013-08-31 00:22:48 +0000372char _InterlockedOr8_np(char volatile *_Value, char _Mask);
373long _InterlockedXor_np(long volatile *_Value, long _Mask);
374short _InterlockedXor16_np(short volatile *_Value, short _Mask);
375__int64 _InterlockedXor64_np(__int64 volatile *_Value, __int64 _Mask);
376char _InterlockedXor8_np(char volatile *_Value, char _Mask);
377unsigned __int64 _lzcnt_u64(unsigned __int64);
378__int64 _mul128(__int64 _Multiplier, __int64 _Multiplicand,
379 __int64 *_HighProduct);
380unsigned int __cdecl _readfsbase_u32(void);
381unsigned __int64 __cdecl _readfsbase_u64(void);
382unsigned int __cdecl _readgsbase_u32(void);
383unsigned __int64 __cdecl _readgsbase_u64(void);
384unsigned __int64 _rorx_u64(unsigned __int64, const unsigned int);
385unsigned __int64 _tzcnt_u64(unsigned __int64);
386unsigned __int64 _tzmsk_u64(unsigned __int64);
387unsigned __int64 _umul128(unsigned __int64 _Multiplier,
388 unsigned __int64 _Multiplicand,
389 unsigned __int64 *_HighProduct);
390void __cdecl _writefsbase_u32(unsigned int);
391void _cdecl _writefsbase_u64(unsigned __int64);
392void __cdecl _writegsbase_u32(unsigned int);
393void __cdecl _writegsbase_u64(unsigned __int64);
394void __cdecl _xrstor64(void const *, unsigned __int64);
395void __cdecl _xsave64(void *, unsigned __int64);
396void __cdecl _xsaveopt64(void *, unsigned __int64);
Eric Christopher0db88a72013-09-18 22:24:01 +0000397
Warren Hunt3f987942013-09-30 21:08:05 +0000398#endif /* __x86_64__ */
Reid Klecknerf0e23222013-09-19 00:19:53 +0000399
Warren Huntd6ffae92013-09-27 23:57:26 +0000400/*----------------------------------------------------------------------------*\
401|* Bit Twiddling
402\*----------------------------------------------------------------------------*/
403static __inline__ unsigned char __attribute__((__always_inline__, __nodebug__))
404_rotl8(unsigned char _Value, unsigned char _Shift) {
405 _Shift &= 0x7;
406 return _Shift ? (_Value << _Shift) | (_Value >> (8 - _Shift)) : _Value;
407}
408static __inline__ unsigned char __attribute__((__always_inline__, __nodebug__))
409_rotr8(unsigned char _Value, unsigned char _Shift) {
410 _Shift &= 0x7;
411 return _Shift ? (_Value >> _Shift) | (_Value << (8 - _Shift)) : _Value;
412}
413static __inline__ unsigned short __attribute__((__always_inline__, __nodebug__))
414_rotl16(unsigned short _Value, unsigned char _Shift) {
415 _Shift &= 0xf;
416 return _Shift ? (_Value << _Shift) | (_Value >> (16 - _Shift)) : _Value;
417}
418static __inline__ unsigned short __attribute__((__always_inline__, __nodebug__))
419_rotr16(unsigned short _Value, unsigned char _Shift) {
420 _Shift &= 0xf;
421 return _Shift ? (_Value >> _Shift) | (_Value << (16 - _Shift)) : _Value;
422}
423static __inline__ unsigned int __attribute__((__always_inline__, __nodebug__))
424_rotl(unsigned int _Value, int _Shift) {
425 _Shift &= 0x1f;
426 return _Shift ? (_Value << _Shift) | (_Value >> (32 - _Shift)) : _Value;
427}
428static __inline__ unsigned int __attribute__((__always_inline__, __nodebug__))
429_rotr(unsigned int _Value, int _Shift) {
430 _Shift &= 0x1f;
431 return _Shift ? (_Value >> _Shift) | (_Value << (32 - _Shift)) : _Value;
432}
433static __inline__ unsigned long __attribute__((__always_inline__, __nodebug__))
434_lrotl(unsigned long _Value, int _Shift) {
435 _Shift &= 0x1f;
436 return _Shift ? (_Value << _Shift) | (_Value >> (32 - _Shift)) : _Value;
437}
438static __inline__ unsigned long __attribute__((__always_inline__, __nodebug__))
439_lrotr(unsigned long _Value, int _Shift) {
440 _Shift &= 0x1f;
441 return _Shift ? (_Value >> _Shift) | (_Value << (32 - _Shift)) : _Value;
442}
443static
444__inline__ unsigned __int64 __attribute__((__always_inline__, __nodebug__))
445_rotl64(unsigned __int64 _Value, int _Shift) {
446 _Shift &= 0x3f;
447 return _Shift ? (_Value << _Shift) | (_Value >> (64 - _Shift)) : _Value;
448}
449static
450__inline__ unsigned __int64 __attribute__((__always_inline__, __nodebug__))
451_rotr64(unsigned __int64 _Value, int _Shift) {
452 _Shift &= 0x3f;
453 return _Shift ? (_Value >> _Shift) | (_Value << (64 - _Shift)) : _Value;
454}
455/*----------------------------------------------------------------------------*\
456|* Bit Counting and Testing
457\*----------------------------------------------------------------------------*/
458static __inline__ unsigned char __attribute__((__always_inline__, __nodebug__))
459_BitScanForward(unsigned long *_Index, unsigned long _Mask) {
460 if (!_Mask)
461 return 0;
462 *_Index = __builtin_ctzl(_Mask);
463 return 1;
464}
465static __inline__ unsigned char __attribute__((__always_inline__, __nodebug__))
466_BitScanReverse(unsigned long *_Index, unsigned long _Mask) {
467 if (!_Mask)
468 return 0;
469 *_Index = 31 - __builtin_clzl(_Mask);
470 return 1;
471}
472static __inline__ unsigned int __attribute__((__always_inline__, __nodebug__))
473_lzcnt_u32(unsigned int a) {
474 if (!a)
475 return 32;
476 return __builtin_clzl(a);
477}
478static __inline__ unsigned short __attribute__((__always_inline__, __nodebug__))
479__popcnt16(unsigned short value) {
480 return __builtin_popcount((int)value);
481}
482static __inline__ unsigned int __attribute__((__always_inline__, __nodebug__))
483__popcnt(unsigned int value) {
484 return __builtin_popcount(value);
485}
486static __inline__ unsigned char __attribute__((__always_inline__, __nodebug__))
487_bittest(long const *a, long b) {
488 return (*a >> b) & 1;
489}
490static __inline__ unsigned char __attribute__((__always_inline__, __nodebug__))
491_bittestandcomplement(long *a, long b) {
492 unsigned char x = (*a >> b) & 1;
493 *a = *a ^ (1 << b);
494 return x;
495}
496static __inline__ unsigned char __attribute__((__always_inline__, __nodebug__))
497_bittestandreset(long *a, long b) {
498 unsigned char x = (*a >> b) & 1;
499 *a = *a & ~(1 << b);
500 return x;
501}
502static __inline__ unsigned char __attribute__((__always_inline__, __nodebug__))
503_bittestandset(long *a, long b) {
504 unsigned char x = (*a >> b) & 1;
Warren Hunt2731e3e2013-10-01 17:12:40 +0000505 *a = *a | (1 << b);
Warren Huntd6ffae92013-09-27 23:57:26 +0000506 return x;
507}
Warren Hunt3f987942013-09-30 21:08:05 +0000508#ifdef __x86_64__
Warren Huntd6ffae92013-09-27 23:57:26 +0000509static __inline__ unsigned char __attribute__((__always_inline__, __nodebug__))
510_BitScanForward64(unsigned long *_Index, unsigned __int64 _Mask) {
511 if (!_Mask)
512 return 0;
513 *_Index = __builtin_ctzll(_Mask);
514 return 1;
515}
516static __inline__ unsigned char __attribute__((__always_inline__, __nodebug__))
517_BitScanReverse64(unsigned long *_Index, unsigned __int64 _Mask) {
518 if (!_Mask)
519 return 0;
520 *_Index = 63 - __builtin_clzll(_Mask);
521 return 1;
522}
523static
524__inline__ unsigned __int64 __attribute__((__always_inline__, __nodebug__))
525_lzcnt_u64(unsigned __int64 a) {
526 if (!a)
527 return 64;
528 return __builtin_clzll(a);
529}
530static __inline__
531unsigned __int64 __attribute__((__always_inline__, __nodebug__))
532 __popcnt64(unsigned __int64 value) {
533 return __builtin_popcountll(value);
534}
535static __inline__ unsigned char __attribute__((__always_inline__, __nodebug__))
536_bittest64(__int64 const *a, __int64 b) {
537 return (*a >> b) & 1;
538}
539static __inline__ unsigned char __attribute__((__always_inline__, __nodebug__))
540_bittestandcomplement64(__int64 *a, __int64 b) {
541 unsigned char x = (*a >> b) & 1;
542 *a = *a ^ (1ll << b);
543 return x;
544}
545static __inline__ unsigned char __attribute__((__always_inline__, __nodebug__))
546_bittestandreset64(__int64 *a, __int64 b) {
547 unsigned char x = (*a >> b) & 1;
548 *a = *a & ~(1ll << b);
549 return x;
550}
551static __inline__ unsigned char __attribute__((__always_inline__, __nodebug__))
552_bittestandset64(__int64 *a, __int64 b) {
553 unsigned char x = (*a >> b) & 1;
Warren Hunt2731e3e2013-10-01 17:12:40 +0000554 *a = *a | (1ll << b);
Warren Huntd6ffae92013-09-27 23:57:26 +0000555 return x;
556}
557#endif
558/*----------------------------------------------------------------------------*\
559|* Interlocked Exchange Add
560\*----------------------------------------------------------------------------*/
561static __inline__ char __attribute__((__always_inline__, __nodebug__))
562_InterlockedExchangeAdd8(char volatile *_Addend, char _Value) {
563 return __atomic_add_fetch(_Addend, _Value, 0) - _Value;
564}
565static __inline__ short __attribute__((__always_inline__, __nodebug__))
566_InterlockedExchangeAdd16(short volatile *_Addend, short _Value) {
567 return __atomic_add_fetch(_Addend, _Value, 0) - _Value;
568}
569static __inline__ long __attribute__((__always_inline__, __nodebug__))
570_InterlockedExchangeAdd(long volatile *_Addend, long _Value) {
571 return __atomic_add_fetch(_Addend, _Value, 0) - _Value;
572}
Warren Hunt3f987942013-09-30 21:08:05 +0000573#ifdef __x86_64__
Warren Huntd6ffae92013-09-27 23:57:26 +0000574static __inline__ __int64 __attribute__((__always_inline__, __nodebug__))
575_InterlockedExchangeAdd64(__int64 volatile *_Addend, __int64 _Value) {
576 return __atomic_add_fetch(_Addend, _Value, 0) - _Value;
577}
578#endif
579/*----------------------------------------------------------------------------*\
580|* Interlocked Exchange Sub
581\*----------------------------------------------------------------------------*/
582static __inline__ char __attribute__((__always_inline__, __nodebug__))
583_InterlockedExchangeSub8(char volatile *_Subend, char _Value) {
584 return __atomic_sub_fetch(_Subend, _Value, 0) + _Value;
585}
586static __inline__ short __attribute__((__always_inline__, __nodebug__))
587_InterlockedExchangeSub16(short volatile *_Subend, short _Value) {
588 return __atomic_sub_fetch(_Subend, _Value, 0) + _Value;
589}
590static __inline__ long __attribute__((__always_inline__, __nodebug__))
591_InterlockedExchangeSub(long volatile *_Subend, long _Value) {
592 return __atomic_sub_fetch(_Subend, _Value, 0) + _Value;
593}
Warren Hunt3f987942013-09-30 21:08:05 +0000594#ifdef __x86_64__
Warren Huntd6ffae92013-09-27 23:57:26 +0000595static __inline__ __int64 __attribute__((__always_inline__, __nodebug__))
596_InterlockedExchangeSub64(__int64 volatile *_Subend, __int64 _Value) {
597 return __atomic_sub_fetch(_Subend, _Value, 0) + _Value;
598}
599#endif
600/*----------------------------------------------------------------------------*\
601|* Interlocked Increment
602\*----------------------------------------------------------------------------*/
603static __inline__ char __attribute__((__always_inline__, __nodebug__))
604_InterlockedIncrement16(char volatile *_Value) {
605 return __atomic_add_fetch(_Value, 1, 0);
606}
607static __inline__ long __attribute__((__always_inline__, __nodebug__))
608_InterlockedIncrement(long volatile *_Value) {
609 return __atomic_add_fetch(_Value, 1, 0);
610}
Warren Hunt3f987942013-09-30 21:08:05 +0000611#ifdef __x86_64__
Warren Huntd6ffae92013-09-27 23:57:26 +0000612static __inline__ __int64 __attribute__((__always_inline__, __nodebug__))
613_InterlockedIncrement64(__int64 volatile *_Value) {
614 return __atomic_add_fetch(_Value, 1, 0);
615}
616#endif
617/*----------------------------------------------------------------------------*\
618|* Interlocked Decrement
619\*----------------------------------------------------------------------------*/
620static __inline__ char __attribute__((__always_inline__, __nodebug__))
621_InterlockedDecrement16(char volatile *_Value) {
622 return __atomic_sub_fetch(_Value, 1, 0);
623}
624static __inline__ long __attribute__((__always_inline__, __nodebug__))
625_InterlockedDecrement(long volatile *_Value) {
626 return __atomic_sub_fetch(_Value, 1, 0);
627}
Warren Hunt3f987942013-09-30 21:08:05 +0000628#ifdef __x86_64__
Warren Huntd6ffae92013-09-27 23:57:26 +0000629static __inline__ __int64 __attribute__((__always_inline__, __nodebug__))
630_InterlockedDecrement64(__int64 volatile *_Value) {
631 return __atomic_sub_fetch(_Value, 1, 0);
632}
633#endif
634/*----------------------------------------------------------------------------*\
635|* Interlocked And
636\*----------------------------------------------------------------------------*/
637static __inline__ char __attribute__((__always_inline__, __nodebug__))
638_InterlockedAnd8(char volatile *_Value, char _Mask) {
639 return __atomic_and_fetch(_Value, _Mask, 0);
640}
641static __inline__ short __attribute__((__always_inline__, __nodebug__))
642_InterlockedAnd16(short volatile *_Value, short _Mask) {
643 return __atomic_and_fetch(_Value, _Mask, 0);
644}
645static __inline__ long __attribute__((__always_inline__, __nodebug__))
646_InterlockedAnd(long volatile *_Value, long _Mask) {
647 return __atomic_and_fetch(_Value, _Mask, 0);
648}
Warren Hunt3f987942013-09-30 21:08:05 +0000649#ifdef __x86_64__
Warren Huntd6ffae92013-09-27 23:57:26 +0000650static __inline__ __int64 __attribute__((__always_inline__, __nodebug__))
651_InterlockedAnd64(__int64 volatile *_Value, __int64 _Mask) {
652 return __atomic_and_fetch(_Value, _Mask, 0);
653}
654#endif
655/*----------------------------------------------------------------------------*\
656|* Interlocked Or
657\*----------------------------------------------------------------------------*/
658static __inline__ char __attribute__((__always_inline__, __nodebug__))
659_InterlockedOr8(char volatile *_Value, char _Mask) {
660 return __atomic_or_fetch(_Value, _Mask, 0);
661}
662static __inline__ short __attribute__((__always_inline__, __nodebug__))
663_InterlockedOr16(short volatile *_Value, short _Mask) {
664 return __atomic_or_fetch(_Value, _Mask, 0);
665}
666static __inline__ long __attribute__((__always_inline__, __nodebug__))
667_InterlockedOr(long volatile *_Value, long _Mask) {
668 return __atomic_or_fetch(_Value, _Mask, 0);
669}
Warren Hunt3f987942013-09-30 21:08:05 +0000670#ifdef __x86_64__
Warren Huntd6ffae92013-09-27 23:57:26 +0000671static __inline__ __int64 __attribute__((__always_inline__, __nodebug__))
672_InterlockedOr64(__int64 volatile *_Value, __int64 _Mask) {
673 return __atomic_or_fetch(_Value, _Mask, 0);
674}
675#endif
676/*----------------------------------------------------------------------------*\
677|* Interlocked Xor
678\*----------------------------------------------------------------------------*/
679static __inline__ char __attribute__((__always_inline__, __nodebug__))
680_InterlockedXor8(char volatile *_Value, char _Mask) {
681 return __atomic_xor_fetch(_Value, _Mask, 0);
682}
683static __inline__ short __attribute__((__always_inline__, __nodebug__))
684_InterlockedXor16(short volatile *_Value, short _Mask) {
685 return __atomic_xor_fetch(_Value, _Mask, 0);
686}
687static __inline__ long __attribute__((__always_inline__, __nodebug__))
688_InterlockedXor(long volatile *_Value, long _Mask) {
689 return __atomic_xor_fetch(_Value, _Mask, 0);
690}
Warren Hunt3f987942013-09-30 21:08:05 +0000691#ifdef __x86_64__
Warren Huntd6ffae92013-09-27 23:57:26 +0000692static __inline__ __int64 __attribute__((__always_inline__, __nodebug__))
693_InterlockedXor64(__int64 volatile *_Value, __int64 _Mask) {
694 return __atomic_xor_fetch(_Value, _Mask, 0);
695}
696#endif
697/*----------------------------------------------------------------------------*\
698|* Interlocked Exchange
699\*----------------------------------------------------------------------------*/
700static __inline__ char __attribute__((__always_inline__, __nodebug__))
701_InterlockedExchange8(char volatile *_Target, char _Value) {
702 __atomic_exchange(_Target, &_Value, &_Value, 0);
703 return _Value;
704}
705static __inline__ short __attribute__((__always_inline__, __nodebug__))
706_InterlockedExchange16(short volatile *_Target, short _Value) {
707 __atomic_exchange(_Target, &_Value, &_Value, 0);
708 return _Value;
709}
710static __inline__ long __attribute__((__always_inline__, __nodebug__))
711_InterlockedExchange(long volatile *_Target, long _Value) {
712 __atomic_exchange(_Target, &_Value, &_Value, 0);
713 return _Value;
714}
Warren Hunt3f987942013-09-30 21:08:05 +0000715#ifdef __x86_64__
Warren Huntd6ffae92013-09-27 23:57:26 +0000716static __inline__ __int64 __attribute__((__always_inline__, __nodebug__))
717_InterlockedExchange64(__int64 volatile *_Target, __int64 _Value) {
718 __atomic_exchange(_Target, &_Value, &_Value, 0);
719 return _Value;
720}
721#endif
722/*----------------------------------------------------------------------------*\
723|* Interlocked Compare Exchange
724\*----------------------------------------------------------------------------*/
725static __inline__ char __attribute__((__always_inline__, __nodebug__))
726_InterlockedCompareExchange8(char volatile *_Destination,
727 char _Exchange, char _Comparand) {
728 __atomic_compare_exchange(_Destination, &_Comparand, &_Exchange, 0, 0, 0);
729 return _Comparand;
730}
731static __inline__ short __attribute__((__always_inline__, __nodebug__))
732_InterlockedCompareExchange16(short volatile *_Destination,
733 short _Exchange, short _Comparand) {
734 __atomic_compare_exchange(_Destination, &_Comparand, &_Exchange, 0, 0, 0);
735 return _Comparand;
736}
737static __inline__ long __attribute__((__always_inline__, __nodebug__))
738_InterlockedCompareExchange(long volatile *_Destination,
739 long _Exchange, long _Comparand) {
740 __atomic_compare_exchange(_Destination, &_Comparand, &_Exchange, 0, 0, 0);
741 return _Comparand;
742}
Warren Hunt3f987942013-09-30 21:08:05 +0000743#ifdef __x86_64__
Warren Huntd6ffae92013-09-27 23:57:26 +0000744static __inline__ __int64 __attribute__((__always_inline__, __nodebug__))
745_InterlockedCompareExchange64(__int64 volatile *_Destination,
746 __int64 _Exchange, __int64 _Comparand) {
747 __atomic_compare_exchange(_Destination, &_Comparand, &_Exchange, 0, 0, 0);
748 return _Comparand;
749}
750#endif
751/*----------------------------------------------------------------------------*\
Reid Kleckner00d33a52013-10-17 01:29:48 +0000752|* Barriers
753\*----------------------------------------------------------------------------*/
754static __inline__ void __attribute__((__always_inline__, __nodebug__))
755__attribute__((deprecated("use other intrinsics or C++11 atomics instead")))
756_ReadWriteBarrier(void) {
757 __asm__ volatile ("" : : : "memory");
758}
759static __inline__ void __attribute__((__always_inline__, __nodebug__))
760__attribute__((deprecated("use other intrinsics or C++11 atomics instead")))
761_ReadBarrier(void) {
762 __asm__ volatile ("" : : : "memory");
763}
764static __inline__ void __attribute__((__always_inline__, __nodebug__))
765__attribute__((deprecated("use other intrinsics or C++11 atomics instead")))
766_WriteBarrier(void) {
767 __asm__ volatile ("" : : : "memory");
768}
769/*----------------------------------------------------------------------------*\
Warren Huntd6ffae92013-09-27 23:57:26 +0000770|* Misc
771\*----------------------------------------------------------------------------*/
772static __inline__ void * __attribute__((__always_inline__, __nodebug__))
773_AddressOfReturnAddress(void) {
774 return (void*)((char*)__builtin_frame_address(0) + sizeof(void*));
775}
776static __inline__ void * __attribute__((__always_inline__, __nodebug__))
777_ReturnAddress(void) {
778 return __builtin_return_address(0);
779}
Hans Wennborg854f7d32014-01-16 23:39:35 +0000780static __inline__ void __attribute__((__always_inline__, __nodebug__))
781__cpuid(int __info[4], int __level) {
782#if __i386__
783 __asm__ ("cpuid"
784 : "=a"(__info[0]), "=b" (__info[1]), "=c"(__info[2]), "=d"(__info[3])
785 : "0"(__level));
786#else
787 __asm__ ("cpuid" : "=a"(__info[0]), "=b" (__info[1]), "=c"(__info[2]), "=d"(__info[3])
788 : "0"(__level));
789#endif
790}
791static __inline__ unsigned __int64 __cdecl __attribute__((__always_inline__, __nodebug__))
792_xgetbv(unsigned int __xcr_no) {
793 unsigned int __eax, __edx;
794 __asm__ ("xgetbv" : "=a" (__eax), "=d" (__edx) : "c" (__xcr_no));
795 return ((unsigned __int64)__edx << 32) | __eax;
796}
Warren Huntd6ffae92013-09-27 23:57:26 +0000797
Eric Christophercc872532013-09-18 22:40:18 +0000798#ifdef __cplusplus
Eric Christopher0db88a72013-09-18 22:24:01 +0000799}
Eric Christopherd1428bf2013-08-31 00:22:48 +0000800#endif
Reid Klecknerf0e23222013-09-19 00:19:53 +0000801
802#endif /* __INTRIN_H */
803#endif /* _MSC_VER */