xref: /minix3/external/bsd/llvm/dist/clang/lib/Headers/Intrin.h (revision 0a6a1f1d05b60e214de2f05a7310ddd1f0e590e7)
1f4a2713aSLionel Sambuc /* ===-------- Intrin.h ---------------------------------------------------===
2f4a2713aSLionel Sambuc  *
3f4a2713aSLionel Sambuc  * Permission is hereby granted, free of charge, to any person obtaining a copy
4f4a2713aSLionel Sambuc  * of this software and associated documentation files (the "Software"), to deal
5f4a2713aSLionel Sambuc  * in the Software without restriction, including without limitation the rights
6f4a2713aSLionel Sambuc  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7f4a2713aSLionel Sambuc  * copies of the Software, and to permit persons to whom the Software is
8f4a2713aSLionel Sambuc  * furnished to do so, subject to the following conditions:
9f4a2713aSLionel Sambuc  *
10f4a2713aSLionel Sambuc  * The above copyright notice and this permission notice shall be included in
11f4a2713aSLionel Sambuc  * all copies or substantial portions of the Software.
12f4a2713aSLionel Sambuc  *
13f4a2713aSLionel Sambuc  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14f4a2713aSLionel Sambuc  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15f4a2713aSLionel Sambuc  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16f4a2713aSLionel Sambuc  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17f4a2713aSLionel Sambuc  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18f4a2713aSLionel Sambuc  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19f4a2713aSLionel Sambuc  * THE SOFTWARE.
20f4a2713aSLionel Sambuc  *
21f4a2713aSLionel Sambuc  *===-----------------------------------------------------------------------===
22f4a2713aSLionel Sambuc  */
23f4a2713aSLionel Sambuc 
24f4a2713aSLionel Sambuc /* Only include this if we're compiling for the windows platform. */
25f4a2713aSLionel Sambuc #ifndef _MSC_VER
26f4a2713aSLionel Sambuc #include_next <Intrin.h>
27f4a2713aSLionel Sambuc #else
28f4a2713aSLionel Sambuc 
29f4a2713aSLionel Sambuc #ifndef __INTRIN_H
30f4a2713aSLionel Sambuc #define __INTRIN_H
31f4a2713aSLionel Sambuc 
32f4a2713aSLionel Sambuc /* First include the standard intrinsics. */
33*0a6a1f1dSLionel Sambuc #if defined(__i386__) || defined(__x86_64__)
34f4a2713aSLionel Sambuc #include <x86intrin.h>
35*0a6a1f1dSLionel Sambuc #endif
36*0a6a1f1dSLionel Sambuc 
37*0a6a1f1dSLionel Sambuc /* For the definition of jmp_buf. */
38*0a6a1f1dSLionel Sambuc #if __STDC_HOSTED__
39*0a6a1f1dSLionel Sambuc #include <setjmp.h>
40*0a6a1f1dSLionel Sambuc #endif
41f4a2713aSLionel Sambuc 
42f4a2713aSLionel Sambuc #ifdef __cplusplus
43f4a2713aSLionel Sambuc extern "C" {
44f4a2713aSLionel Sambuc #endif
45f4a2713aSLionel Sambuc 
46*0a6a1f1dSLionel Sambuc #if defined(__MMX__)
47f4a2713aSLionel Sambuc /* And the random ones that aren't in those files. */
48f4a2713aSLionel Sambuc __m64 _m_from_float(float);
49f4a2713aSLionel Sambuc __m64 _m_from_int(int _l);
50f4a2713aSLionel Sambuc void _m_prefetch(void *);
51f4a2713aSLionel Sambuc float _m_to_float(__m64);
52f4a2713aSLionel Sambuc int _m_to_int(__m64 _M);
53*0a6a1f1dSLionel Sambuc #endif
54f4a2713aSLionel Sambuc 
55f4a2713aSLionel Sambuc /* Other assorted instruction intrinsics. */
56f4a2713aSLionel Sambuc void __addfsbyte(unsigned long, unsigned char);
57f4a2713aSLionel Sambuc void __addfsdword(unsigned long, unsigned long);
58f4a2713aSLionel Sambuc void __addfsword(unsigned long, unsigned short);
59f4a2713aSLionel Sambuc void __code_seg(const char *);
60*0a6a1f1dSLionel Sambuc static __inline__
61f4a2713aSLionel Sambuc void __cpuid(int[4], int);
62*0a6a1f1dSLionel Sambuc static __inline__
63f4a2713aSLionel Sambuc void __cpuidex(int[4], int, int);
64f4a2713aSLionel Sambuc void __debugbreak(void);
65f4a2713aSLionel Sambuc __int64 __emul(int, int);
66f4a2713aSLionel Sambuc unsigned __int64 __emulu(unsigned int, unsigned int);
67f4a2713aSLionel Sambuc void __cdecl __fastfail(unsigned int);
68f4a2713aSLionel Sambuc unsigned int __getcallerseflags(void);
69*0a6a1f1dSLionel Sambuc static __inline__
70f4a2713aSLionel Sambuc void __halt(void);
71f4a2713aSLionel Sambuc unsigned char __inbyte(unsigned short);
72f4a2713aSLionel Sambuc void __inbytestring(unsigned short, unsigned char *, unsigned long);
73f4a2713aSLionel Sambuc void __incfsbyte(unsigned long);
74f4a2713aSLionel Sambuc void __incfsdword(unsigned long);
75f4a2713aSLionel Sambuc void __incfsword(unsigned long);
76f4a2713aSLionel Sambuc unsigned long __indword(unsigned short);
77f4a2713aSLionel Sambuc void __indwordstring(unsigned short, unsigned long *, unsigned long);
78f4a2713aSLionel Sambuc void __int2c(void);
79f4a2713aSLionel Sambuc void __invlpg(void *);
80f4a2713aSLionel Sambuc unsigned short __inword(unsigned short);
81f4a2713aSLionel Sambuc void __inwordstring(unsigned short, unsigned short *, unsigned long);
82f4a2713aSLionel Sambuc void __lidt(void *);
83f4a2713aSLionel Sambuc unsigned __int64 __ll_lshift(unsigned __int64, int);
84f4a2713aSLionel Sambuc __int64 __ll_rshift(__int64, int);
85f4a2713aSLionel Sambuc void __llwpcb(void *);
86f4a2713aSLionel Sambuc unsigned char __lwpins32(unsigned int, unsigned int, unsigned int);
87f4a2713aSLionel Sambuc void __lwpval32(unsigned int, unsigned int, unsigned int);
88f4a2713aSLionel Sambuc unsigned int __lzcnt(unsigned int);
89f4a2713aSLionel Sambuc unsigned short __lzcnt16(unsigned short);
90*0a6a1f1dSLionel Sambuc static __inline__
91f4a2713aSLionel Sambuc void __movsb(unsigned char *, unsigned char const *, size_t);
92*0a6a1f1dSLionel Sambuc static __inline__
93f4a2713aSLionel Sambuc void __movsd(unsigned long *, unsigned long const *, size_t);
94*0a6a1f1dSLionel Sambuc static __inline__
95f4a2713aSLionel Sambuc void __movsw(unsigned short *, unsigned short const *, size_t);
96f4a2713aSLionel Sambuc void __nop(void);
97f4a2713aSLionel Sambuc void __nvreg_restore_fence(void);
98f4a2713aSLionel Sambuc void __nvreg_save_fence(void);
99f4a2713aSLionel Sambuc void __outbyte(unsigned short, unsigned char);
100f4a2713aSLionel Sambuc void __outbytestring(unsigned short, unsigned char *, unsigned long);
101f4a2713aSLionel Sambuc void __outdword(unsigned short, unsigned long);
102f4a2713aSLionel Sambuc void __outdwordstring(unsigned short, unsigned long *, unsigned long);
103f4a2713aSLionel Sambuc void __outword(unsigned short, unsigned short);
104f4a2713aSLionel Sambuc void __outwordstring(unsigned short, unsigned short *, unsigned long);
105f4a2713aSLionel Sambuc static __inline__
106f4a2713aSLionel Sambuc unsigned int __popcnt(unsigned int);
107f4a2713aSLionel Sambuc static __inline__
108f4a2713aSLionel Sambuc unsigned short __popcnt16(unsigned short);
109f4a2713aSLionel Sambuc unsigned long __readcr0(void);
110f4a2713aSLionel Sambuc unsigned long __readcr2(void);
111*0a6a1f1dSLionel Sambuc static __inline__
112f4a2713aSLionel Sambuc unsigned long __readcr3(void);
113*0a6a1f1dSLionel Sambuc unsigned long __readcr4(void);
114f4a2713aSLionel Sambuc unsigned long __readcr8(void);
115f4a2713aSLionel Sambuc unsigned int __readdr(unsigned int);
116*0a6a1f1dSLionel Sambuc #ifdef __i386__
117*0a6a1f1dSLionel Sambuc static __inline__
118f4a2713aSLionel Sambuc unsigned char __readfsbyte(unsigned long);
119*0a6a1f1dSLionel Sambuc static __inline__
120f4a2713aSLionel Sambuc unsigned long __readfsdword(unsigned long);
121*0a6a1f1dSLionel Sambuc static __inline__
122f4a2713aSLionel Sambuc unsigned __int64 __readfsqword(unsigned long);
123*0a6a1f1dSLionel Sambuc static __inline__
124f4a2713aSLionel Sambuc unsigned short __readfsword(unsigned long);
125*0a6a1f1dSLionel Sambuc #endif
126*0a6a1f1dSLionel Sambuc static __inline__
127f4a2713aSLionel Sambuc unsigned __int64 __readmsr(unsigned long);
128f4a2713aSLionel Sambuc unsigned __int64 __readpmc(unsigned long);
129f4a2713aSLionel Sambuc unsigned long __segmentlimit(unsigned long);
130f4a2713aSLionel Sambuc void __sidt(void *);
131f4a2713aSLionel Sambuc void *__slwpcb(void);
132*0a6a1f1dSLionel Sambuc static __inline__
133f4a2713aSLionel Sambuc void __stosb(unsigned char *, unsigned char, size_t);
134*0a6a1f1dSLionel Sambuc static __inline__
135f4a2713aSLionel Sambuc void __stosd(unsigned long *, unsigned long, size_t);
136*0a6a1f1dSLionel Sambuc static __inline__
137f4a2713aSLionel Sambuc void __stosw(unsigned short *, unsigned short, size_t);
138f4a2713aSLionel Sambuc void __svm_clgi(void);
139f4a2713aSLionel Sambuc void __svm_invlpga(void *, int);
140f4a2713aSLionel Sambuc void __svm_skinit(int);
141f4a2713aSLionel Sambuc void __svm_stgi(void);
142f4a2713aSLionel Sambuc void __svm_vmload(size_t);
143f4a2713aSLionel Sambuc void __svm_vmrun(size_t);
144f4a2713aSLionel Sambuc void __svm_vmsave(size_t);
145f4a2713aSLionel Sambuc void __ud2(void);
146f4a2713aSLionel Sambuc unsigned __int64 __ull_rshift(unsigned __int64, int);
147f4a2713aSLionel Sambuc void __vmx_off(void);
148f4a2713aSLionel Sambuc void __vmx_vmptrst(unsigned __int64 *);
149f4a2713aSLionel Sambuc void __wbinvd(void);
150f4a2713aSLionel Sambuc void __writecr0(unsigned int);
151*0a6a1f1dSLionel Sambuc static __inline__
152f4a2713aSLionel Sambuc void __writecr3(unsigned int);
153f4a2713aSLionel Sambuc void __writecr4(unsigned int);
154f4a2713aSLionel Sambuc void __writecr8(unsigned int);
155f4a2713aSLionel Sambuc void __writedr(unsigned int, unsigned int);
156f4a2713aSLionel Sambuc void __writefsbyte(unsigned long, unsigned char);
157f4a2713aSLionel Sambuc void __writefsdword(unsigned long, unsigned long);
158f4a2713aSLionel Sambuc void __writefsqword(unsigned long, unsigned __int64);
159f4a2713aSLionel Sambuc void __writefsword(unsigned long, unsigned short);
160f4a2713aSLionel Sambuc void __writemsr(unsigned long, unsigned __int64);
161f4a2713aSLionel Sambuc static __inline__
162f4a2713aSLionel Sambuc void *_AddressOfReturnAddress(void);
163f4a2713aSLionel Sambuc static __inline__
164f4a2713aSLionel Sambuc unsigned char _BitScanForward(unsigned long *_Index, unsigned long _Mask);
165f4a2713aSLionel Sambuc static __inline__
166f4a2713aSLionel Sambuc unsigned char _BitScanReverse(unsigned long *_Index, unsigned long _Mask);
167f4a2713aSLionel Sambuc static __inline__
168f4a2713aSLionel Sambuc unsigned char _bittest(long const *, long);
169f4a2713aSLionel Sambuc static __inline__
170f4a2713aSLionel Sambuc unsigned char _bittestandcomplement(long *, long);
171f4a2713aSLionel Sambuc static __inline__
172f4a2713aSLionel Sambuc unsigned char _bittestandreset(long *, long);
173f4a2713aSLionel Sambuc static __inline__
174f4a2713aSLionel Sambuc unsigned char _bittestandset(long *, long);
175f4a2713aSLionel Sambuc unsigned __int64 __cdecl _byteswap_uint64(unsigned __int64);
176f4a2713aSLionel Sambuc unsigned long __cdecl _byteswap_ulong(unsigned long);
177f4a2713aSLionel Sambuc unsigned short __cdecl _byteswap_ushort(unsigned short);
178f4a2713aSLionel Sambuc void __cdecl _disable(void);
179f4a2713aSLionel Sambuc void __cdecl _enable(void);
180f4a2713aSLionel Sambuc void __cdecl _fxrstor(void const *);
181f4a2713aSLionel Sambuc void __cdecl _fxsave(void *);
182f4a2713aSLionel Sambuc long _InterlockedAddLargeStatistic(__int64 volatile *_Addend, long _Value);
183f4a2713aSLionel Sambuc static __inline__
184f4a2713aSLionel Sambuc long _InterlockedAnd(long volatile *_Value, long _Mask);
185f4a2713aSLionel Sambuc static __inline__
186f4a2713aSLionel Sambuc short _InterlockedAnd16(short volatile *_Value, short _Mask);
187f4a2713aSLionel Sambuc static __inline__
188f4a2713aSLionel Sambuc char _InterlockedAnd8(char volatile *_Value, char _Mask);
189f4a2713aSLionel Sambuc unsigned char _interlockedbittestandreset(long volatile *, long);
190*0a6a1f1dSLionel Sambuc static __inline__
191f4a2713aSLionel Sambuc unsigned char _interlockedbittestandset(long volatile *, long);
192f4a2713aSLionel Sambuc static __inline__
193f4a2713aSLionel Sambuc long __cdecl _InterlockedCompareExchange(long volatile *_Destination,
194f4a2713aSLionel Sambuc                                          long _Exchange, long _Comparand);
195f4a2713aSLionel Sambuc long _InterlockedCompareExchange_HLEAcquire(long volatile *, long, long);
196f4a2713aSLionel Sambuc long _InterlockedCompareExchange_HLERelease(long volatile *, long, long);
197f4a2713aSLionel Sambuc static __inline__
198f4a2713aSLionel Sambuc short _InterlockedCompareExchange16(short volatile *_Destination,
199f4a2713aSLionel Sambuc                                     short _Exchange, short _Comparand);
200f4a2713aSLionel Sambuc static __inline__
201f4a2713aSLionel Sambuc __int64 _InterlockedCompareExchange64(__int64 volatile *_Destination,
202f4a2713aSLionel Sambuc                                       __int64 _Exchange, __int64 _Comparand);
203f4a2713aSLionel Sambuc __int64 _InterlockedcompareExchange64_HLEAcquire(__int64 volatile *, __int64,
204f4a2713aSLionel Sambuc                                                  __int64);
205f4a2713aSLionel Sambuc __int64 _InterlockedCompareExchange64_HLERelease(__int64 volatile *, __int64,
206f4a2713aSLionel Sambuc                                                  __int64);
207f4a2713aSLionel Sambuc static __inline__
208f4a2713aSLionel Sambuc char _InterlockedCompareExchange8(char volatile *_Destination, char _Exchange,
209f4a2713aSLionel Sambuc                                   char _Comparand);
210f4a2713aSLionel Sambuc void *_InterlockedCompareExchangePointer_HLEAcquire(void *volatile *, void *,
211f4a2713aSLionel Sambuc                                                     void *);
212f4a2713aSLionel Sambuc void *_InterlockedCompareExchangePointer_HLERelease(void *volatile *, void *,
213f4a2713aSLionel Sambuc                                                     void *);
214f4a2713aSLionel Sambuc static __inline__
215f4a2713aSLionel Sambuc long __cdecl _InterlockedDecrement(long volatile *_Addend);
216f4a2713aSLionel Sambuc static __inline__
217f4a2713aSLionel Sambuc short _InterlockedDecrement16(short volatile *_Addend);
218*0a6a1f1dSLionel Sambuc long _InterlockedExchange(long volatile *_Target, long _Value);
219f4a2713aSLionel Sambuc static __inline__
220f4a2713aSLionel Sambuc short _InterlockedExchange16(short volatile *_Target, short _Value);
221f4a2713aSLionel Sambuc static __inline__
222f4a2713aSLionel Sambuc char _InterlockedExchange8(char volatile *_Target, char _Value);
223f4a2713aSLionel Sambuc static __inline__
224f4a2713aSLionel Sambuc long __cdecl _InterlockedExchangeAdd(long volatile *_Addend, long _Value);
225f4a2713aSLionel Sambuc long _InterlockedExchangeAdd_HLEAcquire(long volatile *, long);
226f4a2713aSLionel Sambuc long _InterlockedExchangeAdd_HLERelease(long volatile *, long);
227f4a2713aSLionel Sambuc static __inline__
228*0a6a1f1dSLionel Sambuc short _InterlockedExchangeAdd16(short volatile *_Addend, short _Value);
229*0a6a1f1dSLionel Sambuc __int64 _InterlockedExchangeAdd64_HLEAcquire(__int64 volatile *, __int64);
230*0a6a1f1dSLionel Sambuc __int64 _InterlockedExchangeAdd64_HLERelease(__int64 volatile *, __int64);
231*0a6a1f1dSLionel Sambuc static __inline__
232f4a2713aSLionel Sambuc char _InterlockedExchangeAdd8(char volatile *_Addend, char _Value);
233f4a2713aSLionel Sambuc static __inline__
234f4a2713aSLionel Sambuc long __cdecl _InterlockedIncrement(long volatile *_Addend);
235f4a2713aSLionel Sambuc static __inline__
236f4a2713aSLionel Sambuc short _InterlockedIncrement16(short volatile *_Addend);
237f4a2713aSLionel Sambuc static __inline__
238f4a2713aSLionel Sambuc long _InterlockedOr(long volatile *_Value, long _Mask);
239f4a2713aSLionel Sambuc static __inline__
240f4a2713aSLionel Sambuc short _InterlockedOr16(short volatile *_Value, short _Mask);
241f4a2713aSLionel Sambuc static __inline__
242f4a2713aSLionel Sambuc char _InterlockedOr8(char volatile *_Value, char _Mask);
243f4a2713aSLionel Sambuc static __inline__
244f4a2713aSLionel Sambuc long _InterlockedXor(long volatile *_Value, long _Mask);
245f4a2713aSLionel Sambuc static __inline__
246f4a2713aSLionel Sambuc short _InterlockedXor16(short volatile *_Value, short _Mask);
247f4a2713aSLionel Sambuc static __inline__
248f4a2713aSLionel Sambuc char _InterlockedXor8(char volatile *_Value, char _Mask);
249f4a2713aSLionel Sambuc void __cdecl _invpcid(unsigned int, void *);
250f4a2713aSLionel Sambuc static __inline__
251f4a2713aSLionel Sambuc unsigned long __cdecl _lrotl(unsigned long, int);
252f4a2713aSLionel Sambuc static __inline__
253f4a2713aSLionel Sambuc unsigned long __cdecl _lrotr(unsigned long, int);
254f4a2713aSLionel Sambuc static __inline__
255f4a2713aSLionel Sambuc static __inline__
256f4a2713aSLionel Sambuc void _ReadBarrier(void);
257f4a2713aSLionel Sambuc static __inline__
258f4a2713aSLionel Sambuc void _ReadWriteBarrier(void);
259f4a2713aSLionel Sambuc static __inline__
260f4a2713aSLionel Sambuc void *_ReturnAddress(void);
261f4a2713aSLionel Sambuc unsigned int _rorx_u32(unsigned int, const unsigned int);
262f4a2713aSLionel Sambuc static __inline__
263f4a2713aSLionel Sambuc unsigned int __cdecl _rotl(unsigned int _Value, int _Shift);
264f4a2713aSLionel Sambuc static __inline__
265f4a2713aSLionel Sambuc unsigned short _rotl16(unsigned short _Value, unsigned char _Shift);
266f4a2713aSLionel Sambuc static __inline__
267f4a2713aSLionel Sambuc unsigned __int64 __cdecl _rotl64(unsigned __int64 _Value, int _Shift);
268f4a2713aSLionel Sambuc static __inline__
269f4a2713aSLionel Sambuc unsigned char _rotl8(unsigned char _Value, unsigned char _Shift);
270f4a2713aSLionel Sambuc static __inline__
271f4a2713aSLionel Sambuc unsigned int __cdecl _rotr(unsigned int _Value, int _Shift);
272f4a2713aSLionel Sambuc static __inline__
273f4a2713aSLionel Sambuc unsigned short _rotr16(unsigned short _Value, unsigned char _Shift);
274f4a2713aSLionel Sambuc static __inline__
275f4a2713aSLionel Sambuc unsigned __int64 __cdecl _rotr64(unsigned __int64 _Value, int _Shift);
276f4a2713aSLionel Sambuc static __inline__
277f4a2713aSLionel Sambuc unsigned char _rotr8(unsigned char _Value, unsigned char _Shift);
278f4a2713aSLionel Sambuc int _sarx_i32(int, unsigned int);
279*0a6a1f1dSLionel Sambuc #if __STDC_HOSTED__
280*0a6a1f1dSLionel Sambuc int __cdecl _setjmp(jmp_buf);
281*0a6a1f1dSLionel Sambuc #endif
282f4a2713aSLionel Sambuc unsigned int _shlx_u32(unsigned int, unsigned int);
283f4a2713aSLionel Sambuc unsigned int _shrx_u32(unsigned int, unsigned int);
284f4a2713aSLionel Sambuc void _Store_HLERelease(long volatile *, long);
285f4a2713aSLionel Sambuc void _Store64_HLERelease(__int64 volatile *, __int64);
286f4a2713aSLionel Sambuc void _StorePointer_HLERelease(void *volatile *, void *);
287f4a2713aSLionel Sambuc static __inline__
288f4a2713aSLionel Sambuc void _WriteBarrier(void);
289f4a2713aSLionel Sambuc unsigned __int32 xbegin(void);
290f4a2713aSLionel Sambuc void _xend(void);
291*0a6a1f1dSLionel Sambuc static __inline__
292f4a2713aSLionel Sambuc unsigned __int64 __cdecl _xgetbv(unsigned int);
293f4a2713aSLionel Sambuc void __cdecl _xrstor(void const *, unsigned __int64);
294f4a2713aSLionel Sambuc void __cdecl _xsave(void *, unsigned __int64);
295f4a2713aSLionel Sambuc void __cdecl _xsaveopt(void *, unsigned __int64);
296f4a2713aSLionel Sambuc void __cdecl _xsetbv(unsigned int, unsigned __int64);
297f4a2713aSLionel Sambuc 
298f4a2713aSLionel Sambuc /* These additional intrinsics are turned on in x64/amd64/x86_64 mode. */
299f4a2713aSLionel Sambuc #ifdef __x86_64__
300f4a2713aSLionel Sambuc void __addgsbyte(unsigned long, unsigned char);
301f4a2713aSLionel Sambuc void __addgsdword(unsigned long, unsigned long);
302f4a2713aSLionel Sambuc void __addgsqword(unsigned long, unsigned __int64);
303f4a2713aSLionel Sambuc void __addgsword(unsigned long, unsigned short);
304*0a6a1f1dSLionel Sambuc static __inline__
305f4a2713aSLionel Sambuc void __faststorefence(void);
306f4a2713aSLionel Sambuc void __incgsbyte(unsigned long);
307f4a2713aSLionel Sambuc void __incgsdword(unsigned long);
308f4a2713aSLionel Sambuc void __incgsqword(unsigned long);
309f4a2713aSLionel Sambuc void __incgsword(unsigned long);
310*0a6a1f1dSLionel Sambuc unsigned char __lwpins64(unsigned __int64, unsigned int, unsigned int);
311*0a6a1f1dSLionel Sambuc void __lwpval64(unsigned __int64, unsigned int, unsigned int);
312*0a6a1f1dSLionel Sambuc unsigned __int64 __lzcnt64(unsigned __int64);
313*0a6a1f1dSLionel Sambuc static __inline__
314*0a6a1f1dSLionel Sambuc void __movsq(unsigned long long *, unsigned long long const *, size_t);
315*0a6a1f1dSLionel Sambuc __int64 __mulh(__int64, __int64);
316*0a6a1f1dSLionel Sambuc static __inline__
317f4a2713aSLionel Sambuc unsigned __int64 __popcnt64(unsigned __int64);
318*0a6a1f1dSLionel Sambuc static __inline__
319*0a6a1f1dSLionel Sambuc unsigned char __readgsbyte(unsigned long);
320*0a6a1f1dSLionel Sambuc static __inline__
321*0a6a1f1dSLionel Sambuc unsigned long __readgsdword(unsigned long);
322*0a6a1f1dSLionel Sambuc static __inline__
323*0a6a1f1dSLionel Sambuc unsigned __int64 __readgsqword(unsigned long);
324*0a6a1f1dSLionel Sambuc unsigned short __readgsword(unsigned long);
325f4a2713aSLionel Sambuc unsigned __int64 __shiftleft128(unsigned __int64 _LowPart,
326f4a2713aSLionel Sambuc                                 unsigned __int64 _HighPart,
327f4a2713aSLionel Sambuc                                 unsigned char _Shift);
328f4a2713aSLionel Sambuc unsigned __int64 __shiftright128(unsigned __int64 _LowPart,
329f4a2713aSLionel Sambuc                                  unsigned __int64 _HighPart,
330f4a2713aSLionel Sambuc                                  unsigned char _Shift);
331*0a6a1f1dSLionel Sambuc static __inline__
332f4a2713aSLionel Sambuc void __stosq(unsigned __int64 *, unsigned __int64, size_t);
333*0a6a1f1dSLionel Sambuc unsigned char __vmx_on(unsigned __int64 *);
334*0a6a1f1dSLionel Sambuc unsigned char __vmx_vmclear(unsigned __int64 *);
335*0a6a1f1dSLionel Sambuc unsigned char __vmx_vmlaunch(void);
336*0a6a1f1dSLionel Sambuc unsigned char __vmx_vmptrld(unsigned __int64 *);
337*0a6a1f1dSLionel Sambuc unsigned char __vmx_vmread(size_t, size_t *);
338*0a6a1f1dSLionel Sambuc unsigned char __vmx_vmresume(void);
339*0a6a1f1dSLionel Sambuc unsigned char __vmx_vmwrite(size_t, size_t);
340*0a6a1f1dSLionel Sambuc void __writegsbyte(unsigned long, unsigned char);
341*0a6a1f1dSLionel Sambuc void __writegsdword(unsigned long, unsigned long);
342*0a6a1f1dSLionel Sambuc void __writegsqword(unsigned long, unsigned __int64);
343*0a6a1f1dSLionel Sambuc void __writegsword(unsigned long, unsigned short);
344f4a2713aSLionel Sambuc static __inline__
345f4a2713aSLionel Sambuc unsigned char _BitScanForward64(unsigned long *_Index, unsigned __int64 _Mask);
346f4a2713aSLionel Sambuc static __inline__
347f4a2713aSLionel Sambuc unsigned char _BitScanReverse64(unsigned long *_Index, unsigned __int64 _Mask);
348f4a2713aSLionel Sambuc static __inline__
349f4a2713aSLionel Sambuc unsigned char _bittest64(__int64 const *, __int64);
350f4a2713aSLionel Sambuc static __inline__
351f4a2713aSLionel Sambuc unsigned char _bittestandcomplement64(__int64 *, __int64);
352f4a2713aSLionel Sambuc static __inline__
353f4a2713aSLionel Sambuc unsigned char _bittestandreset64(__int64 *, __int64);
354f4a2713aSLionel Sambuc static __inline__
355f4a2713aSLionel Sambuc unsigned char _bittestandset64(__int64 *, __int64);
356f4a2713aSLionel Sambuc unsigned __int64 __cdecl _byteswap_uint64(unsigned __int64);
357f4a2713aSLionel Sambuc void __cdecl _fxrstor64(void const *);
358f4a2713aSLionel Sambuc void __cdecl _fxsave64(void *);
359f4a2713aSLionel Sambuc long _InterlockedAnd_np(long volatile *_Value, long _Mask);
360f4a2713aSLionel Sambuc short _InterlockedAnd16_np(short volatile *_Value, short _Mask);
361f4a2713aSLionel Sambuc __int64 _InterlockedAnd64_np(__int64 volatile *_Value, __int64 _Mask);
362f4a2713aSLionel Sambuc char _InterlockedAnd8_np(char volatile *_Value, char _Mask);
363f4a2713aSLionel Sambuc unsigned char _interlockedbittestandreset64(__int64 volatile *, __int64);
364*0a6a1f1dSLionel Sambuc static __inline__
365f4a2713aSLionel Sambuc unsigned char _interlockedbittestandset64(__int64 volatile *, __int64);
366f4a2713aSLionel Sambuc long _InterlockedCompareExchange_np(long volatile *_Destination, long _Exchange,
367f4a2713aSLionel Sambuc                                     long _Comparand);
368f4a2713aSLionel Sambuc unsigned char _InterlockedCompareExchange128(__int64 volatile *_Destination,
369f4a2713aSLionel Sambuc                                              __int64 _ExchangeHigh,
370f4a2713aSLionel Sambuc                                              __int64 _ExchangeLow,
371f4a2713aSLionel Sambuc                                              __int64 *_CompareandResult);
372f4a2713aSLionel Sambuc unsigned char _InterlockedCompareExchange128_np(__int64 volatile *_Destination,
373f4a2713aSLionel Sambuc                                                 __int64 _ExchangeHigh,
374f4a2713aSLionel Sambuc                                                 __int64 _ExchangeLow,
375f4a2713aSLionel Sambuc                                                 __int64 *_ComparandResult);
376f4a2713aSLionel Sambuc short _InterlockedCompareExchange16_np(short volatile *_Destination,
377f4a2713aSLionel Sambuc                                        short _Exchange, short _Comparand);
378*0a6a1f1dSLionel Sambuc __int64 _InterlockedCompareExchange64_HLEAcquire(__int64 volatile *, __int64,
379*0a6a1f1dSLionel Sambuc                                                  __int64);
380*0a6a1f1dSLionel Sambuc __int64 _InterlockedCompareExchange64_HLERelease(__int64 volatile *, __int64,
381*0a6a1f1dSLionel Sambuc                                                  __int64);
382f4a2713aSLionel Sambuc __int64 _InterlockedCompareExchange64_np(__int64 volatile *_Destination,
383f4a2713aSLionel Sambuc                                          __int64 _Exchange, __int64 _Comparand);
384*0a6a1f1dSLionel Sambuc void *_InterlockedCompareExchangePointer(void *volatile *_Destination,
385*0a6a1f1dSLionel Sambuc                                          void *_Exchange, void *_Comparand);
386f4a2713aSLionel Sambuc void *_InterlockedCompareExchangePointer_np(void *volatile *_Destination,
387f4a2713aSLionel Sambuc                                             void *_Exchange, void *_Comparand);
388*0a6a1f1dSLionel Sambuc static __inline__
389*0a6a1f1dSLionel Sambuc __int64 _InterlockedDecrement64(__int64 volatile *_Addend);
390*0a6a1f1dSLionel Sambuc static __inline__
391*0a6a1f1dSLionel Sambuc __int64 _InterlockedExchange64(__int64 volatile *_Target, __int64 _Value);
392*0a6a1f1dSLionel Sambuc static __inline__
393*0a6a1f1dSLionel Sambuc __int64 _InterlockedExchangeAdd64(__int64 volatile *_Addend, __int64 _Value);
394*0a6a1f1dSLionel Sambuc void *_InterlockedExchangePointer(void *volatile *_Target, void *_Value);
395*0a6a1f1dSLionel Sambuc static __inline__
396*0a6a1f1dSLionel Sambuc __int64 _InterlockedIncrement64(__int64 volatile *_Addend);
397f4a2713aSLionel Sambuc long _InterlockedOr_np(long volatile *_Value, long _Mask);
398f4a2713aSLionel Sambuc short _InterlockedOr16_np(short volatile *_Value, short _Mask);
399*0a6a1f1dSLionel Sambuc static __inline__
400*0a6a1f1dSLionel Sambuc __int64 _InterlockedOr64(__int64 volatile *_Value, __int64 _Mask);
401f4a2713aSLionel Sambuc __int64 _InterlockedOr64_np(__int64 volatile *_Value, __int64 _Mask);
402f4a2713aSLionel Sambuc char _InterlockedOr8_np(char volatile *_Value, char _Mask);
403f4a2713aSLionel Sambuc long _InterlockedXor_np(long volatile *_Value, long _Mask);
404f4a2713aSLionel Sambuc short _InterlockedXor16_np(short volatile *_Value, short _Mask);
405*0a6a1f1dSLionel Sambuc static __inline__
406*0a6a1f1dSLionel Sambuc __int64 _InterlockedXor64(__int64 volatile *_Value, __int64 _Mask);
407f4a2713aSLionel Sambuc __int64 _InterlockedXor64_np(__int64 volatile *_Value, __int64 _Mask);
408f4a2713aSLionel Sambuc char _InterlockedXor8_np(char volatile *_Value, char _Mask);
409*0a6a1f1dSLionel Sambuc static __inline__
410f4a2713aSLionel Sambuc __int64 _mul128(__int64 _Multiplier, __int64 _Multiplicand,
411f4a2713aSLionel Sambuc                 __int64 *_HighProduct);
412f4a2713aSLionel Sambuc unsigned __int64 _rorx_u64(unsigned __int64, const unsigned int);
413*0a6a1f1dSLionel Sambuc __int64 _sarx_i64(__int64, unsigned int);
414*0a6a1f1dSLionel Sambuc #if __STDC_HOSTED__
415*0a6a1f1dSLionel Sambuc int __cdecl _setjmpex(jmp_buf);
416*0a6a1f1dSLionel Sambuc #endif
417*0a6a1f1dSLionel Sambuc unsigned __int64 _shlx_u64(unsigned __int64, unsigned int);
418*0a6a1f1dSLionel Sambuc unsigned __int64 _shrx_u64(unsigned __int64, unsigned int);
419*0a6a1f1dSLionel Sambuc /*
420*0a6a1f1dSLionel Sambuc  * Multiply two 64-bit integers and obtain a 64-bit result.
421*0a6a1f1dSLionel Sambuc  * The low-half is returned directly and the high half is in an out parameter.
422*0a6a1f1dSLionel Sambuc  */
423*0a6a1f1dSLionel Sambuc static __inline__ unsigned __int64 __attribute__((__always_inline__, __nodebug__))
_umul128(unsigned __int64 _Multiplier,unsigned __int64 _Multiplicand,unsigned __int64 * _HighProduct)424*0a6a1f1dSLionel Sambuc _umul128(unsigned __int64 _Multiplier, unsigned __int64 _Multiplicand,
425*0a6a1f1dSLionel Sambuc          unsigned __int64 *_HighProduct) {
426*0a6a1f1dSLionel Sambuc   unsigned __int128 _FullProduct =
427*0a6a1f1dSLionel Sambuc       (unsigned __int128)_Multiplier * (unsigned __int128)_Multiplicand;
428*0a6a1f1dSLionel Sambuc   *_HighProduct = _FullProduct >> 64;
429*0a6a1f1dSLionel Sambuc   return _FullProduct;
430*0a6a1f1dSLionel Sambuc }
431*0a6a1f1dSLionel Sambuc static __inline__ unsigned __int64 __attribute__((__always_inline__, __nodebug__))
__umulh(unsigned __int64 _Multiplier,unsigned __int64 _Multiplicand)432*0a6a1f1dSLionel Sambuc __umulh(unsigned __int64 _Multiplier, unsigned __int64 _Multiplicand) {
433*0a6a1f1dSLionel Sambuc   unsigned __int128 _FullProduct =
434*0a6a1f1dSLionel Sambuc       (unsigned __int128)_Multiplier * (unsigned __int128)_Multiplicand;
435*0a6a1f1dSLionel Sambuc   return _FullProduct >> 64;
436*0a6a1f1dSLionel Sambuc }
437f4a2713aSLionel Sambuc void __cdecl _xrstor64(void const *, unsigned __int64);
438f4a2713aSLionel Sambuc void __cdecl _xsave64(void *, unsigned __int64);
439f4a2713aSLionel Sambuc void __cdecl _xsaveopt64(void *, unsigned __int64);
440f4a2713aSLionel Sambuc 
441f4a2713aSLionel Sambuc #endif /* __x86_64__ */
442f4a2713aSLionel Sambuc 
443f4a2713aSLionel Sambuc /*----------------------------------------------------------------------------*\
444f4a2713aSLionel Sambuc |* Bit Twiddling
445f4a2713aSLionel Sambuc \*----------------------------------------------------------------------------*/
446f4a2713aSLionel Sambuc static __inline__ unsigned char __attribute__((__always_inline__, __nodebug__))
_rotl8(unsigned char _Value,unsigned char _Shift)447f4a2713aSLionel Sambuc _rotl8(unsigned char _Value, unsigned char _Shift) {
448f4a2713aSLionel Sambuc   _Shift &= 0x7;
449f4a2713aSLionel Sambuc   return _Shift ? (_Value << _Shift) | (_Value >> (8 - _Shift)) : _Value;
450f4a2713aSLionel Sambuc }
451f4a2713aSLionel Sambuc static __inline__ unsigned char __attribute__((__always_inline__, __nodebug__))
_rotr8(unsigned char _Value,unsigned char _Shift)452f4a2713aSLionel Sambuc _rotr8(unsigned char _Value, unsigned char _Shift) {
453f4a2713aSLionel Sambuc   _Shift &= 0x7;
454f4a2713aSLionel Sambuc   return _Shift ? (_Value >> _Shift) | (_Value << (8 - _Shift)) : _Value;
455f4a2713aSLionel Sambuc }
456f4a2713aSLionel Sambuc static __inline__ unsigned short __attribute__((__always_inline__, __nodebug__))
_rotl16(unsigned short _Value,unsigned char _Shift)457f4a2713aSLionel Sambuc _rotl16(unsigned short _Value, unsigned char _Shift) {
458f4a2713aSLionel Sambuc   _Shift &= 0xf;
459f4a2713aSLionel Sambuc   return _Shift ? (_Value << _Shift) | (_Value >> (16 - _Shift)) : _Value;
460f4a2713aSLionel Sambuc }
461f4a2713aSLionel Sambuc static __inline__ unsigned short __attribute__((__always_inline__, __nodebug__))
_rotr16(unsigned short _Value,unsigned char _Shift)462f4a2713aSLionel Sambuc _rotr16(unsigned short _Value, unsigned char _Shift) {
463f4a2713aSLionel Sambuc   _Shift &= 0xf;
464f4a2713aSLionel Sambuc   return _Shift ? (_Value >> _Shift) | (_Value << (16 - _Shift)) : _Value;
465f4a2713aSLionel Sambuc }
466f4a2713aSLionel Sambuc static __inline__ unsigned int __attribute__((__always_inline__, __nodebug__))
_rotl(unsigned int _Value,int _Shift)467f4a2713aSLionel Sambuc _rotl(unsigned int _Value, int _Shift) {
468f4a2713aSLionel Sambuc   _Shift &= 0x1f;
469f4a2713aSLionel Sambuc   return _Shift ? (_Value << _Shift) | (_Value >> (32 - _Shift)) : _Value;
470f4a2713aSLionel Sambuc }
471f4a2713aSLionel Sambuc static __inline__ unsigned int __attribute__((__always_inline__, __nodebug__))
_rotr(unsigned int _Value,int _Shift)472f4a2713aSLionel Sambuc _rotr(unsigned int _Value, int _Shift) {
473f4a2713aSLionel Sambuc   _Shift &= 0x1f;
474f4a2713aSLionel Sambuc   return _Shift ? (_Value >> _Shift) | (_Value << (32 - _Shift)) : _Value;
475f4a2713aSLionel Sambuc }
476f4a2713aSLionel Sambuc static __inline__ unsigned long __attribute__((__always_inline__, __nodebug__))
_lrotl(unsigned long _Value,int _Shift)477f4a2713aSLionel Sambuc _lrotl(unsigned long _Value, int _Shift) {
478f4a2713aSLionel Sambuc   _Shift &= 0x1f;
479f4a2713aSLionel Sambuc   return _Shift ? (_Value << _Shift) | (_Value >> (32 - _Shift)) : _Value;
480f4a2713aSLionel Sambuc }
481f4a2713aSLionel Sambuc static __inline__ unsigned long __attribute__((__always_inline__, __nodebug__))
_lrotr(unsigned long _Value,int _Shift)482f4a2713aSLionel Sambuc _lrotr(unsigned long _Value, int _Shift) {
483f4a2713aSLionel Sambuc   _Shift &= 0x1f;
484f4a2713aSLionel Sambuc   return _Shift ? (_Value >> _Shift) | (_Value << (32 - _Shift)) : _Value;
485f4a2713aSLionel Sambuc }
486f4a2713aSLionel Sambuc static
487f4a2713aSLionel Sambuc __inline__ unsigned __int64 __attribute__((__always_inline__, __nodebug__))
_rotl64(unsigned __int64 _Value,int _Shift)488f4a2713aSLionel Sambuc _rotl64(unsigned __int64 _Value, int _Shift) {
489f4a2713aSLionel Sambuc   _Shift &= 0x3f;
490f4a2713aSLionel Sambuc   return _Shift ? (_Value << _Shift) | (_Value >> (64 - _Shift)) : _Value;
491f4a2713aSLionel Sambuc }
492f4a2713aSLionel Sambuc static
493f4a2713aSLionel Sambuc __inline__ unsigned __int64 __attribute__((__always_inline__, __nodebug__))
_rotr64(unsigned __int64 _Value,int _Shift)494f4a2713aSLionel Sambuc _rotr64(unsigned __int64 _Value, int _Shift) {
495f4a2713aSLionel Sambuc   _Shift &= 0x3f;
496f4a2713aSLionel Sambuc   return _Shift ? (_Value >> _Shift) | (_Value << (64 - _Shift)) : _Value;
497f4a2713aSLionel Sambuc }
498f4a2713aSLionel Sambuc /*----------------------------------------------------------------------------*\
499f4a2713aSLionel Sambuc |* Bit Counting and Testing
500f4a2713aSLionel Sambuc \*----------------------------------------------------------------------------*/
501f4a2713aSLionel Sambuc static __inline__ unsigned char __attribute__((__always_inline__, __nodebug__))
_BitScanForward(unsigned long * _Index,unsigned long _Mask)502f4a2713aSLionel Sambuc _BitScanForward(unsigned long *_Index, unsigned long _Mask) {
503f4a2713aSLionel Sambuc   if (!_Mask)
504f4a2713aSLionel Sambuc     return 0;
505f4a2713aSLionel Sambuc   *_Index = __builtin_ctzl(_Mask);
506f4a2713aSLionel Sambuc   return 1;
507f4a2713aSLionel Sambuc }
508f4a2713aSLionel Sambuc static __inline__ unsigned char __attribute__((__always_inline__, __nodebug__))
_BitScanReverse(unsigned long * _Index,unsigned long _Mask)509f4a2713aSLionel Sambuc _BitScanReverse(unsigned long *_Index, unsigned long _Mask) {
510f4a2713aSLionel Sambuc   if (!_Mask)
511f4a2713aSLionel Sambuc     return 0;
512f4a2713aSLionel Sambuc   *_Index = 31 - __builtin_clzl(_Mask);
513f4a2713aSLionel Sambuc   return 1;
514f4a2713aSLionel Sambuc }
515f4a2713aSLionel Sambuc static __inline__ unsigned short __attribute__((__always_inline__, __nodebug__))
__popcnt16(unsigned short value)516f4a2713aSLionel Sambuc __popcnt16(unsigned short value) {
517f4a2713aSLionel Sambuc   return __builtin_popcount((int)value);
518f4a2713aSLionel Sambuc }
519f4a2713aSLionel Sambuc static __inline__ unsigned int __attribute__((__always_inline__, __nodebug__))
__popcnt(unsigned int value)520f4a2713aSLionel Sambuc __popcnt(unsigned int value) {
521f4a2713aSLionel Sambuc   return __builtin_popcount(value);
522f4a2713aSLionel Sambuc }
523f4a2713aSLionel Sambuc static __inline__ unsigned char __attribute__((__always_inline__, __nodebug__))
_bittest(long const * a,long b)524f4a2713aSLionel Sambuc _bittest(long const *a, long b) {
525f4a2713aSLionel Sambuc   return (*a >> b) & 1;
526f4a2713aSLionel Sambuc }
527f4a2713aSLionel Sambuc static __inline__ unsigned char __attribute__((__always_inline__, __nodebug__))
_bittestandcomplement(long * a,long b)528f4a2713aSLionel Sambuc _bittestandcomplement(long *a, long b) {
529f4a2713aSLionel Sambuc   unsigned char x = (*a >> b) & 1;
530f4a2713aSLionel Sambuc   *a = *a ^ (1 << b);
531f4a2713aSLionel Sambuc   return x;
532f4a2713aSLionel Sambuc }
533f4a2713aSLionel Sambuc static __inline__ unsigned char __attribute__((__always_inline__, __nodebug__))
_bittestandreset(long * a,long b)534f4a2713aSLionel Sambuc _bittestandreset(long *a, long b) {
535f4a2713aSLionel Sambuc   unsigned char x = (*a >> b) & 1;
536f4a2713aSLionel Sambuc   *a = *a & ~(1 << b);
537f4a2713aSLionel Sambuc   return x;
538f4a2713aSLionel Sambuc }
539f4a2713aSLionel Sambuc static __inline__ unsigned char __attribute__((__always_inline__, __nodebug__))
_bittestandset(long * a,long b)540f4a2713aSLionel Sambuc _bittestandset(long *a, long b) {
541f4a2713aSLionel Sambuc   unsigned char x = (*a >> b) & 1;
542f4a2713aSLionel Sambuc   *a = *a | (1 << b);
543f4a2713aSLionel Sambuc   return x;
544f4a2713aSLionel Sambuc }
545*0a6a1f1dSLionel Sambuc #if defined(__i386__) || defined(__x86_64__)
546*0a6a1f1dSLionel Sambuc static __inline__ unsigned char __attribute__((__always_inline__, __nodebug__))
_interlockedbittestandset(long volatile * __BitBase,long __BitPos)547*0a6a1f1dSLionel Sambuc _interlockedbittestandset(long volatile *__BitBase, long __BitPos) {
548*0a6a1f1dSLionel Sambuc   unsigned char __Res;
549*0a6a1f1dSLionel Sambuc   __asm__ ("xor %0, %0\n"
550*0a6a1f1dSLionel Sambuc            "lock bts %2, %1\n"
551*0a6a1f1dSLionel Sambuc            "setc %0\n"
552*0a6a1f1dSLionel Sambuc            : "=r" (__Res), "+m"(*__BitBase)
553*0a6a1f1dSLionel Sambuc            : "Ir"(__BitPos));
554*0a6a1f1dSLionel Sambuc   return __Res;
555*0a6a1f1dSLionel Sambuc }
556*0a6a1f1dSLionel Sambuc #endif
557f4a2713aSLionel Sambuc #ifdef __x86_64__
558f4a2713aSLionel Sambuc static __inline__ unsigned char __attribute__((__always_inline__, __nodebug__))
_BitScanForward64(unsigned long * _Index,unsigned __int64 _Mask)559f4a2713aSLionel Sambuc _BitScanForward64(unsigned long *_Index, unsigned __int64 _Mask) {
560f4a2713aSLionel Sambuc   if (!_Mask)
561f4a2713aSLionel Sambuc     return 0;
562f4a2713aSLionel Sambuc   *_Index = __builtin_ctzll(_Mask);
563f4a2713aSLionel Sambuc   return 1;
564f4a2713aSLionel Sambuc }
565f4a2713aSLionel Sambuc static __inline__ unsigned char __attribute__((__always_inline__, __nodebug__))
_BitScanReverse64(unsigned long * _Index,unsigned __int64 _Mask)566f4a2713aSLionel Sambuc _BitScanReverse64(unsigned long *_Index, unsigned __int64 _Mask) {
567f4a2713aSLionel Sambuc   if (!_Mask)
568f4a2713aSLionel Sambuc     return 0;
569f4a2713aSLionel Sambuc   *_Index = 63 - __builtin_clzll(_Mask);
570f4a2713aSLionel Sambuc   return 1;
571f4a2713aSLionel Sambuc }
572f4a2713aSLionel Sambuc static __inline__
573f4a2713aSLionel Sambuc unsigned __int64 __attribute__((__always_inline__, __nodebug__))
__popcnt64(unsigned __int64 value)574f4a2713aSLionel Sambuc  __popcnt64(unsigned __int64 value) {
575f4a2713aSLionel Sambuc   return __builtin_popcountll(value);
576f4a2713aSLionel Sambuc }
577f4a2713aSLionel Sambuc static __inline__ unsigned char __attribute__((__always_inline__, __nodebug__))
_bittest64(__int64 const * a,__int64 b)578f4a2713aSLionel Sambuc _bittest64(__int64 const *a, __int64 b) {
579f4a2713aSLionel Sambuc   return (*a >> b) & 1;
580f4a2713aSLionel Sambuc }
581f4a2713aSLionel Sambuc static __inline__ unsigned char __attribute__((__always_inline__, __nodebug__))
_bittestandcomplement64(__int64 * a,__int64 b)582f4a2713aSLionel Sambuc _bittestandcomplement64(__int64 *a, __int64 b) {
583f4a2713aSLionel Sambuc   unsigned char x = (*a >> b) & 1;
584f4a2713aSLionel Sambuc   *a = *a ^ (1ll << b);
585f4a2713aSLionel Sambuc   return x;
586f4a2713aSLionel Sambuc }
587f4a2713aSLionel Sambuc static __inline__ unsigned char __attribute__((__always_inline__, __nodebug__))
_bittestandreset64(__int64 * a,__int64 b)588f4a2713aSLionel Sambuc _bittestandreset64(__int64 *a, __int64 b) {
589f4a2713aSLionel Sambuc   unsigned char x = (*a >> b) & 1;
590f4a2713aSLionel Sambuc   *a = *a & ~(1ll << b);
591f4a2713aSLionel Sambuc   return x;
592f4a2713aSLionel Sambuc }
593f4a2713aSLionel Sambuc static __inline__ unsigned char __attribute__((__always_inline__, __nodebug__))
_bittestandset64(__int64 * a,__int64 b)594f4a2713aSLionel Sambuc _bittestandset64(__int64 *a, __int64 b) {
595f4a2713aSLionel Sambuc   unsigned char x = (*a >> b) & 1;
596f4a2713aSLionel Sambuc   *a = *a | (1ll << b);
597f4a2713aSLionel Sambuc   return x;
598f4a2713aSLionel Sambuc }
599*0a6a1f1dSLionel Sambuc static __inline__ unsigned char __attribute__((__always_inline__, __nodebug__))
_interlockedbittestandset64(__int64 volatile * __BitBase,__int64 __BitPos)600*0a6a1f1dSLionel Sambuc _interlockedbittestandset64(__int64 volatile *__BitBase, __int64 __BitPos) {
601*0a6a1f1dSLionel Sambuc   unsigned char __Res;
602*0a6a1f1dSLionel Sambuc   __asm__ ("xor %0, %0\n"
603*0a6a1f1dSLionel Sambuc            "lock bts %2, %1\n"
604*0a6a1f1dSLionel Sambuc            "setc %0\n"
605*0a6a1f1dSLionel Sambuc            : "=r" (__Res), "+m"(*__BitBase)
606*0a6a1f1dSLionel Sambuc            : "Ir"(__BitPos));
607*0a6a1f1dSLionel Sambuc   return __Res;
608*0a6a1f1dSLionel Sambuc }
609f4a2713aSLionel Sambuc #endif
610f4a2713aSLionel Sambuc /*----------------------------------------------------------------------------*\
611f4a2713aSLionel Sambuc |* Interlocked Exchange Add
612f4a2713aSLionel Sambuc \*----------------------------------------------------------------------------*/
613f4a2713aSLionel Sambuc static __inline__ char __attribute__((__always_inline__, __nodebug__))
_InterlockedExchangeAdd8(char volatile * _Addend,char _Value)614f4a2713aSLionel Sambuc _InterlockedExchangeAdd8(char volatile *_Addend, char _Value) {
615f4a2713aSLionel Sambuc   return __atomic_add_fetch(_Addend, _Value, 0) - _Value;
616f4a2713aSLionel Sambuc }
617f4a2713aSLionel Sambuc static __inline__ short __attribute__((__always_inline__, __nodebug__))
_InterlockedExchangeAdd16(short volatile * _Addend,short _Value)618f4a2713aSLionel Sambuc _InterlockedExchangeAdd16(short volatile *_Addend, short _Value) {
619f4a2713aSLionel Sambuc   return __atomic_add_fetch(_Addend, _Value, 0) - _Value;
620f4a2713aSLionel Sambuc }
621f4a2713aSLionel Sambuc #ifdef __x86_64__
622f4a2713aSLionel Sambuc static __inline__ __int64 __attribute__((__always_inline__, __nodebug__))
_InterlockedExchangeAdd64(__int64 volatile * _Addend,__int64 _Value)623f4a2713aSLionel Sambuc _InterlockedExchangeAdd64(__int64 volatile *_Addend, __int64 _Value) {
624f4a2713aSLionel Sambuc   return __atomic_add_fetch(_Addend, _Value, 0) - _Value;
625f4a2713aSLionel Sambuc }
626f4a2713aSLionel Sambuc #endif
627f4a2713aSLionel Sambuc /*----------------------------------------------------------------------------*\
628f4a2713aSLionel Sambuc |* Interlocked Exchange Sub
629f4a2713aSLionel Sambuc \*----------------------------------------------------------------------------*/
630f4a2713aSLionel Sambuc static __inline__ char __attribute__((__always_inline__, __nodebug__))
_InterlockedExchangeSub8(char volatile * _Subend,char _Value)631f4a2713aSLionel Sambuc _InterlockedExchangeSub8(char volatile *_Subend, char _Value) {
632f4a2713aSLionel Sambuc   return __atomic_sub_fetch(_Subend, _Value, 0) + _Value;
633f4a2713aSLionel Sambuc }
634f4a2713aSLionel Sambuc static __inline__ short __attribute__((__always_inline__, __nodebug__))
_InterlockedExchangeSub16(short volatile * _Subend,short _Value)635f4a2713aSLionel Sambuc _InterlockedExchangeSub16(short volatile *_Subend, short _Value) {
636f4a2713aSLionel Sambuc   return __atomic_sub_fetch(_Subend, _Value, 0) + _Value;
637f4a2713aSLionel Sambuc }
638f4a2713aSLionel Sambuc static __inline__ long __attribute__((__always_inline__, __nodebug__))
_InterlockedExchangeSub(long volatile * _Subend,long _Value)639f4a2713aSLionel Sambuc _InterlockedExchangeSub(long volatile *_Subend, long _Value) {
640f4a2713aSLionel Sambuc   return __atomic_sub_fetch(_Subend, _Value, 0) + _Value;
641f4a2713aSLionel Sambuc }
642f4a2713aSLionel Sambuc #ifdef __x86_64__
643f4a2713aSLionel Sambuc static __inline__ __int64 __attribute__((__always_inline__, __nodebug__))
_InterlockedExchangeSub64(__int64 volatile * _Subend,__int64 _Value)644f4a2713aSLionel Sambuc _InterlockedExchangeSub64(__int64 volatile *_Subend, __int64 _Value) {
645f4a2713aSLionel Sambuc   return __atomic_sub_fetch(_Subend, _Value, 0) + _Value;
646f4a2713aSLionel Sambuc }
647f4a2713aSLionel Sambuc #endif
648f4a2713aSLionel Sambuc /*----------------------------------------------------------------------------*\
649f4a2713aSLionel Sambuc |* Interlocked Increment
650f4a2713aSLionel Sambuc \*----------------------------------------------------------------------------*/
651*0a6a1f1dSLionel Sambuc static __inline__ short __attribute__((__always_inline__, __nodebug__))
_InterlockedIncrement16(short volatile * _Value)652*0a6a1f1dSLionel Sambuc _InterlockedIncrement16(short volatile *_Value) {
653f4a2713aSLionel Sambuc   return __atomic_add_fetch(_Value, 1, 0);
654f4a2713aSLionel Sambuc }
655f4a2713aSLionel Sambuc #ifdef __x86_64__
656f4a2713aSLionel Sambuc static __inline__ __int64 __attribute__((__always_inline__, __nodebug__))
_InterlockedIncrement64(__int64 volatile * _Value)657f4a2713aSLionel Sambuc _InterlockedIncrement64(__int64 volatile *_Value) {
658f4a2713aSLionel Sambuc   return __atomic_add_fetch(_Value, 1, 0);
659f4a2713aSLionel Sambuc }
660f4a2713aSLionel Sambuc #endif
661f4a2713aSLionel Sambuc /*----------------------------------------------------------------------------*\
662f4a2713aSLionel Sambuc |* Interlocked Decrement
663f4a2713aSLionel Sambuc \*----------------------------------------------------------------------------*/
664*0a6a1f1dSLionel Sambuc static __inline__ short __attribute__((__always_inline__, __nodebug__))
_InterlockedDecrement16(short volatile * _Value)665*0a6a1f1dSLionel Sambuc _InterlockedDecrement16(short volatile *_Value) {
666f4a2713aSLionel Sambuc   return __atomic_sub_fetch(_Value, 1, 0);
667f4a2713aSLionel Sambuc }
668f4a2713aSLionel Sambuc #ifdef __x86_64__
669f4a2713aSLionel Sambuc static __inline__ __int64 __attribute__((__always_inline__, __nodebug__))
_InterlockedDecrement64(__int64 volatile * _Value)670f4a2713aSLionel Sambuc _InterlockedDecrement64(__int64 volatile *_Value) {
671f4a2713aSLionel Sambuc   return __atomic_sub_fetch(_Value, 1, 0);
672f4a2713aSLionel Sambuc }
673f4a2713aSLionel Sambuc #endif
674f4a2713aSLionel Sambuc /*----------------------------------------------------------------------------*\
675f4a2713aSLionel Sambuc |* Interlocked And
676f4a2713aSLionel Sambuc \*----------------------------------------------------------------------------*/
677f4a2713aSLionel Sambuc static __inline__ char __attribute__((__always_inline__, __nodebug__))
_InterlockedAnd8(char volatile * _Value,char _Mask)678f4a2713aSLionel Sambuc _InterlockedAnd8(char volatile *_Value, char _Mask) {
679f4a2713aSLionel Sambuc   return __atomic_and_fetch(_Value, _Mask, 0);
680f4a2713aSLionel Sambuc }
681f4a2713aSLionel Sambuc static __inline__ short __attribute__((__always_inline__, __nodebug__))
_InterlockedAnd16(short volatile * _Value,short _Mask)682f4a2713aSLionel Sambuc _InterlockedAnd16(short volatile *_Value, short _Mask) {
683f4a2713aSLionel Sambuc   return __atomic_and_fetch(_Value, _Mask, 0);
684f4a2713aSLionel Sambuc }
685f4a2713aSLionel Sambuc static __inline__ long __attribute__((__always_inline__, __nodebug__))
_InterlockedAnd(long volatile * _Value,long _Mask)686f4a2713aSLionel Sambuc _InterlockedAnd(long volatile *_Value, long _Mask) {
687f4a2713aSLionel Sambuc   return __atomic_and_fetch(_Value, _Mask, 0);
688f4a2713aSLionel Sambuc }
689f4a2713aSLionel Sambuc #ifdef __x86_64__
690f4a2713aSLionel Sambuc static __inline__ __int64 __attribute__((__always_inline__, __nodebug__))
_InterlockedAnd64(__int64 volatile * _Value,__int64 _Mask)691f4a2713aSLionel Sambuc _InterlockedAnd64(__int64 volatile *_Value, __int64 _Mask) {
692f4a2713aSLionel Sambuc   return __atomic_and_fetch(_Value, _Mask, 0);
693f4a2713aSLionel Sambuc }
694f4a2713aSLionel Sambuc #endif
695f4a2713aSLionel Sambuc /*----------------------------------------------------------------------------*\
696f4a2713aSLionel Sambuc |* Interlocked Or
697f4a2713aSLionel Sambuc \*----------------------------------------------------------------------------*/
698f4a2713aSLionel Sambuc static __inline__ char __attribute__((__always_inline__, __nodebug__))
_InterlockedOr8(char volatile * _Value,char _Mask)699f4a2713aSLionel Sambuc _InterlockedOr8(char volatile *_Value, char _Mask) {
700f4a2713aSLionel Sambuc   return __atomic_or_fetch(_Value, _Mask, 0);
701f4a2713aSLionel Sambuc }
702f4a2713aSLionel Sambuc static __inline__ short __attribute__((__always_inline__, __nodebug__))
_InterlockedOr16(short volatile * _Value,short _Mask)703f4a2713aSLionel Sambuc _InterlockedOr16(short volatile *_Value, short _Mask) {
704f4a2713aSLionel Sambuc   return __atomic_or_fetch(_Value, _Mask, 0);
705f4a2713aSLionel Sambuc }
706f4a2713aSLionel Sambuc static __inline__ long __attribute__((__always_inline__, __nodebug__))
_InterlockedOr(long volatile * _Value,long _Mask)707f4a2713aSLionel Sambuc _InterlockedOr(long volatile *_Value, long _Mask) {
708f4a2713aSLionel Sambuc   return __atomic_or_fetch(_Value, _Mask, 0);
709f4a2713aSLionel Sambuc }
710f4a2713aSLionel Sambuc #ifdef __x86_64__
711f4a2713aSLionel Sambuc static __inline__ __int64 __attribute__((__always_inline__, __nodebug__))
_InterlockedOr64(__int64 volatile * _Value,__int64 _Mask)712f4a2713aSLionel Sambuc _InterlockedOr64(__int64 volatile *_Value, __int64 _Mask) {
713f4a2713aSLionel Sambuc   return __atomic_or_fetch(_Value, _Mask, 0);
714f4a2713aSLionel Sambuc }
715f4a2713aSLionel Sambuc #endif
716f4a2713aSLionel Sambuc /*----------------------------------------------------------------------------*\
717f4a2713aSLionel Sambuc |* Interlocked Xor
718f4a2713aSLionel Sambuc \*----------------------------------------------------------------------------*/
719f4a2713aSLionel Sambuc static __inline__ char __attribute__((__always_inline__, __nodebug__))
_InterlockedXor8(char volatile * _Value,char _Mask)720f4a2713aSLionel Sambuc _InterlockedXor8(char volatile *_Value, char _Mask) {
721f4a2713aSLionel Sambuc   return __atomic_xor_fetch(_Value, _Mask, 0);
722f4a2713aSLionel Sambuc }
723f4a2713aSLionel Sambuc static __inline__ short __attribute__((__always_inline__, __nodebug__))
_InterlockedXor16(short volatile * _Value,short _Mask)724f4a2713aSLionel Sambuc _InterlockedXor16(short volatile *_Value, short _Mask) {
725f4a2713aSLionel Sambuc   return __atomic_xor_fetch(_Value, _Mask, 0);
726f4a2713aSLionel Sambuc }
727f4a2713aSLionel Sambuc static __inline__ long __attribute__((__always_inline__, __nodebug__))
_InterlockedXor(long volatile * _Value,long _Mask)728f4a2713aSLionel Sambuc _InterlockedXor(long volatile *_Value, long _Mask) {
729f4a2713aSLionel Sambuc   return __atomic_xor_fetch(_Value, _Mask, 0);
730f4a2713aSLionel Sambuc }
731f4a2713aSLionel Sambuc #ifdef __x86_64__
732f4a2713aSLionel Sambuc static __inline__ __int64 __attribute__((__always_inline__, __nodebug__))
_InterlockedXor64(__int64 volatile * _Value,__int64 _Mask)733f4a2713aSLionel Sambuc _InterlockedXor64(__int64 volatile *_Value, __int64 _Mask) {
734f4a2713aSLionel Sambuc   return __atomic_xor_fetch(_Value, _Mask, 0);
735f4a2713aSLionel Sambuc }
736f4a2713aSLionel Sambuc #endif
737f4a2713aSLionel Sambuc /*----------------------------------------------------------------------------*\
738f4a2713aSLionel Sambuc |* Interlocked Exchange
739f4a2713aSLionel Sambuc \*----------------------------------------------------------------------------*/
740f4a2713aSLionel Sambuc static __inline__ char __attribute__((__always_inline__, __nodebug__))
_InterlockedExchange8(char volatile * _Target,char _Value)741f4a2713aSLionel Sambuc _InterlockedExchange8(char volatile *_Target, char _Value) {
742f4a2713aSLionel Sambuc   __atomic_exchange(_Target, &_Value, &_Value, 0);
743f4a2713aSLionel Sambuc   return _Value;
744f4a2713aSLionel Sambuc }
745f4a2713aSLionel Sambuc static __inline__ short __attribute__((__always_inline__, __nodebug__))
_InterlockedExchange16(short volatile * _Target,short _Value)746f4a2713aSLionel Sambuc _InterlockedExchange16(short volatile *_Target, short _Value) {
747f4a2713aSLionel Sambuc   __atomic_exchange(_Target, &_Value, &_Value, 0);
748f4a2713aSLionel Sambuc   return _Value;
749f4a2713aSLionel Sambuc }
750f4a2713aSLionel Sambuc #ifdef __x86_64__
751f4a2713aSLionel Sambuc static __inline__ __int64 __attribute__((__always_inline__, __nodebug__))
_InterlockedExchange64(__int64 volatile * _Target,__int64 _Value)752f4a2713aSLionel Sambuc _InterlockedExchange64(__int64 volatile *_Target, __int64 _Value) {
753f4a2713aSLionel Sambuc   __atomic_exchange(_Target, &_Value, &_Value, 0);
754f4a2713aSLionel Sambuc   return _Value;
755f4a2713aSLionel Sambuc }
756f4a2713aSLionel Sambuc #endif
757f4a2713aSLionel Sambuc /*----------------------------------------------------------------------------*\
758f4a2713aSLionel Sambuc |* Interlocked Compare Exchange
759f4a2713aSLionel Sambuc \*----------------------------------------------------------------------------*/
760f4a2713aSLionel Sambuc static __inline__ char __attribute__((__always_inline__, __nodebug__))
_InterlockedCompareExchange8(char volatile * _Destination,char _Exchange,char _Comparand)761f4a2713aSLionel Sambuc _InterlockedCompareExchange8(char volatile *_Destination,
762f4a2713aSLionel Sambuc                              char _Exchange, char _Comparand) {
763f4a2713aSLionel Sambuc   __atomic_compare_exchange(_Destination, &_Comparand, &_Exchange, 0, 0, 0);
764f4a2713aSLionel Sambuc   return _Comparand;
765f4a2713aSLionel Sambuc }
766f4a2713aSLionel Sambuc static __inline__ short __attribute__((__always_inline__, __nodebug__))
_InterlockedCompareExchange16(short volatile * _Destination,short _Exchange,short _Comparand)767f4a2713aSLionel Sambuc _InterlockedCompareExchange16(short volatile *_Destination,
768f4a2713aSLionel Sambuc                               short _Exchange, short _Comparand) {
769f4a2713aSLionel Sambuc   __atomic_compare_exchange(_Destination, &_Comparand, &_Exchange, 0, 0, 0);
770f4a2713aSLionel Sambuc   return _Comparand;
771f4a2713aSLionel Sambuc }
772f4a2713aSLionel Sambuc static __inline__ __int64 __attribute__((__always_inline__, __nodebug__))
_InterlockedCompareExchange64(__int64 volatile * _Destination,__int64 _Exchange,__int64 _Comparand)773f4a2713aSLionel Sambuc _InterlockedCompareExchange64(__int64 volatile *_Destination,
774f4a2713aSLionel Sambuc                               __int64 _Exchange, __int64 _Comparand) {
775f4a2713aSLionel Sambuc   __atomic_compare_exchange(_Destination, &_Comparand, &_Exchange, 0, 0, 0);
776f4a2713aSLionel Sambuc   return _Comparand;
777f4a2713aSLionel Sambuc }
778f4a2713aSLionel Sambuc /*----------------------------------------------------------------------------*\
779f4a2713aSLionel Sambuc |* Barriers
780f4a2713aSLionel Sambuc \*----------------------------------------------------------------------------*/
781*0a6a1f1dSLionel Sambuc #if defined(__i386__) || defined(__x86_64__)
782f4a2713aSLionel Sambuc static __inline__ void __attribute__((__always_inline__, __nodebug__))
783f4a2713aSLionel Sambuc __attribute__((deprecated("use other intrinsics or C++11 atomics instead")))
_ReadWriteBarrier(void)784f4a2713aSLionel Sambuc _ReadWriteBarrier(void) {
785f4a2713aSLionel Sambuc   __asm__ volatile ("" : : : "memory");
786f4a2713aSLionel Sambuc }
787f4a2713aSLionel Sambuc static __inline__ void __attribute__((__always_inline__, __nodebug__))
788f4a2713aSLionel Sambuc __attribute__((deprecated("use other intrinsics or C++11 atomics instead")))
_ReadBarrier(void)789f4a2713aSLionel Sambuc _ReadBarrier(void) {
790f4a2713aSLionel Sambuc   __asm__ volatile ("" : : : "memory");
791f4a2713aSLionel Sambuc }
792f4a2713aSLionel Sambuc static __inline__ void __attribute__((__always_inline__, __nodebug__))
793f4a2713aSLionel Sambuc __attribute__((deprecated("use other intrinsics or C++11 atomics instead")))
_WriteBarrier(void)794f4a2713aSLionel Sambuc _WriteBarrier(void) {
795f4a2713aSLionel Sambuc   __asm__ volatile ("" : : : "memory");
796f4a2713aSLionel Sambuc }
797*0a6a1f1dSLionel Sambuc #endif
798*0a6a1f1dSLionel Sambuc #ifdef __x86_64__
799*0a6a1f1dSLionel Sambuc static __inline__ void __attribute__((__always_inline__, __nodebug__))
__faststorefence(void)800*0a6a1f1dSLionel Sambuc __faststorefence(void) {
801*0a6a1f1dSLionel Sambuc   __asm__ volatile("lock orq $0, (%%rsp)" : : : "memory");
802*0a6a1f1dSLionel Sambuc }
803*0a6a1f1dSLionel Sambuc #endif
804*0a6a1f1dSLionel Sambuc /*----------------------------------------------------------------------------*\
805*0a6a1f1dSLionel Sambuc |* readfs, readgs
806*0a6a1f1dSLionel Sambuc |* (Pointers in address space #256 and #257 are relative to the GS and FS
807*0a6a1f1dSLionel Sambuc |* segment registers, respectively.)
808*0a6a1f1dSLionel Sambuc \*----------------------------------------------------------------------------*/
809*0a6a1f1dSLionel Sambuc #define __ptr_to_addr_space(__addr_space_nbr, __type, __offset)              \
810*0a6a1f1dSLionel Sambuc     ((volatile __type __attribute__((__address_space__(__addr_space_nbr)))*) \
811*0a6a1f1dSLionel Sambuc     (__offset))
812*0a6a1f1dSLionel Sambuc 
813*0a6a1f1dSLionel Sambuc #ifdef __i386__
814*0a6a1f1dSLionel Sambuc static __inline__ unsigned char __attribute__((__always_inline__, __nodebug__))
__readfsbyte(unsigned long __offset)815*0a6a1f1dSLionel Sambuc __readfsbyte(unsigned long __offset) {
816*0a6a1f1dSLionel Sambuc   return *__ptr_to_addr_space(257, unsigned char, __offset);
817*0a6a1f1dSLionel Sambuc }
818*0a6a1f1dSLionel Sambuc static __inline__ unsigned __int64 __attribute__((__always_inline__, __nodebug__))
__readfsqword(unsigned long __offset)819*0a6a1f1dSLionel Sambuc __readfsqword(unsigned long __offset) {
820*0a6a1f1dSLionel Sambuc   return *__ptr_to_addr_space(257, unsigned __int64, __offset);
821*0a6a1f1dSLionel Sambuc }
822*0a6a1f1dSLionel Sambuc static __inline__ unsigned short __attribute__((__always_inline__, __nodebug__))
__readfsword(unsigned long __offset)823*0a6a1f1dSLionel Sambuc __readfsword(unsigned long __offset) {
824*0a6a1f1dSLionel Sambuc   return *__ptr_to_addr_space(257, unsigned short, __offset);
825*0a6a1f1dSLionel Sambuc }
826*0a6a1f1dSLionel Sambuc #endif
827*0a6a1f1dSLionel Sambuc #ifdef __x86_64__
828*0a6a1f1dSLionel Sambuc static __inline__ unsigned char __attribute__((__always_inline__, __nodebug__))
__readgsbyte(unsigned long __offset)829*0a6a1f1dSLionel Sambuc __readgsbyte(unsigned long __offset) {
830*0a6a1f1dSLionel Sambuc   return *__ptr_to_addr_space(256, unsigned char, __offset);
831*0a6a1f1dSLionel Sambuc }
832*0a6a1f1dSLionel Sambuc static __inline__ unsigned long __attribute__((__always_inline__, __nodebug__))
__readgsdword(unsigned long __offset)833*0a6a1f1dSLionel Sambuc __readgsdword(unsigned long __offset) {
834*0a6a1f1dSLionel Sambuc   return *__ptr_to_addr_space(256, unsigned long, __offset);
835*0a6a1f1dSLionel Sambuc }
836*0a6a1f1dSLionel Sambuc static __inline__ unsigned __int64 __attribute__((__always_inline__, __nodebug__))
__readgsqword(unsigned long __offset)837*0a6a1f1dSLionel Sambuc __readgsqword(unsigned long __offset) {
838*0a6a1f1dSLionel Sambuc   return *__ptr_to_addr_space(256, unsigned __int64, __offset);
839*0a6a1f1dSLionel Sambuc }
840*0a6a1f1dSLionel Sambuc static __inline__ unsigned short __attribute__((__always_inline__, __nodebug__))
__readgsword(unsigned long __offset)841*0a6a1f1dSLionel Sambuc __readgsword(unsigned long __offset) {
842*0a6a1f1dSLionel Sambuc   return *__ptr_to_addr_space(256, unsigned short, __offset);
843*0a6a1f1dSLionel Sambuc }
844*0a6a1f1dSLionel Sambuc #endif
845*0a6a1f1dSLionel Sambuc #undef __ptr_to_addr_space
846*0a6a1f1dSLionel Sambuc /*----------------------------------------------------------------------------*\
847*0a6a1f1dSLionel Sambuc |* movs, stos
848*0a6a1f1dSLionel Sambuc \*----------------------------------------------------------------------------*/
849*0a6a1f1dSLionel Sambuc #if defined(__i386__) || defined(__x86_64__)
850*0a6a1f1dSLionel Sambuc static __inline__ void __attribute__((__always_inline__, __nodebug__))
__movsb(unsigned char * __dst,unsigned char const * __src,size_t __n)851*0a6a1f1dSLionel Sambuc __movsb(unsigned char *__dst, unsigned char const *__src, size_t __n) {
852*0a6a1f1dSLionel Sambuc   __asm__("rep movsb" : : "D"(__dst), "S"(__src), "c"(__n)
853*0a6a1f1dSLionel Sambuc                         : "%edi", "%esi", "%ecx");
854*0a6a1f1dSLionel Sambuc }
855*0a6a1f1dSLionel Sambuc static __inline__ void __attribute__((__always_inline__, __nodebug__))
__movsd(unsigned long * __dst,unsigned long const * __src,size_t __n)856*0a6a1f1dSLionel Sambuc __movsd(unsigned long *__dst, unsigned long const *__src, size_t __n) {
857*0a6a1f1dSLionel Sambuc   __asm__("rep movsl" : : "D"(__dst), "S"(__src), "c"(__n)
858*0a6a1f1dSLionel Sambuc                         : "%edi", "%esi", "%ecx");
859*0a6a1f1dSLionel Sambuc }
860*0a6a1f1dSLionel Sambuc static __inline__ void __attribute__((__always_inline__, __nodebug__))
__movsw(unsigned short * __dst,unsigned short const * __src,size_t __n)861*0a6a1f1dSLionel Sambuc __movsw(unsigned short *__dst, unsigned short const *__src, size_t __n) {
862*0a6a1f1dSLionel Sambuc   __asm__("rep movsh" : : "D"(__dst), "S"(__src), "c"(__n)
863*0a6a1f1dSLionel Sambuc                         : "%edi", "%esi", "%ecx");
864*0a6a1f1dSLionel Sambuc }
865*0a6a1f1dSLionel Sambuc static __inline__ void __attribute__((__always_inline__, __nodebug__))
__stosb(unsigned char * __dst,unsigned char __x,size_t __n)866*0a6a1f1dSLionel Sambuc __stosb(unsigned char *__dst, unsigned char __x, size_t __n) {
867*0a6a1f1dSLionel Sambuc   __asm__("rep stosb" : : "D"(__dst), "a"(__x), "c"(__n)
868*0a6a1f1dSLionel Sambuc                         : "%edi", "%ecx");
869*0a6a1f1dSLionel Sambuc }
870*0a6a1f1dSLionel Sambuc static __inline__ void __attribute__((__always_inline__, __nodebug__))
__stosd(unsigned long * __dst,unsigned long __x,size_t __n)871*0a6a1f1dSLionel Sambuc __stosd(unsigned long *__dst, unsigned long __x, size_t __n) {
872*0a6a1f1dSLionel Sambuc   __asm__("rep stosl" : : "D"(__dst), "a"(__x), "c"(__n)
873*0a6a1f1dSLionel Sambuc                         : "%edi", "%ecx");
874*0a6a1f1dSLionel Sambuc }
875*0a6a1f1dSLionel Sambuc static __inline__ void __attribute__((__always_inline__, __nodebug__))
__stosw(unsigned short * __dst,unsigned short __x,size_t __n)876*0a6a1f1dSLionel Sambuc __stosw(unsigned short *__dst, unsigned short __x, size_t __n) {
877*0a6a1f1dSLionel Sambuc   __asm__("rep stosh" : : "D"(__dst), "a"(__x), "c"(__n)
878*0a6a1f1dSLionel Sambuc                         : "%edi", "%ecx");
879*0a6a1f1dSLionel Sambuc }
880*0a6a1f1dSLionel Sambuc #endif
881*0a6a1f1dSLionel Sambuc #ifdef __x86_64__
882*0a6a1f1dSLionel Sambuc static __inline__ void __attribute__((__always_inline__, __nodebug__))
__movsq(unsigned long long * __dst,unsigned long long const * __src,size_t __n)883*0a6a1f1dSLionel Sambuc __movsq(unsigned long long *__dst, unsigned long long const *__src, size_t __n) {
884*0a6a1f1dSLionel Sambuc   __asm__("rep movsq" : : "D"(__dst), "S"(__src), "c"(__n)
885*0a6a1f1dSLionel Sambuc                         : "%edi", "%esi", "%ecx");
886*0a6a1f1dSLionel Sambuc }
887*0a6a1f1dSLionel Sambuc static __inline__ void __attribute__((__always_inline__, __nodebug__))
__stosq(unsigned __int64 * __dst,unsigned __int64 __x,size_t __n)888*0a6a1f1dSLionel Sambuc __stosq(unsigned __int64 *__dst, unsigned __int64 __x, size_t __n) {
889*0a6a1f1dSLionel Sambuc   __asm__("rep stosq" : : "D"(__dst), "a"(__x), "c"(__n)
890*0a6a1f1dSLionel Sambuc                         : "%edi", "%ecx");
891*0a6a1f1dSLionel Sambuc }
892*0a6a1f1dSLionel Sambuc #endif
893*0a6a1f1dSLionel Sambuc 
894f4a2713aSLionel Sambuc /*----------------------------------------------------------------------------*\
895f4a2713aSLionel Sambuc |* Misc
896f4a2713aSLionel Sambuc \*----------------------------------------------------------------------------*/
897f4a2713aSLionel Sambuc static __inline__ void * __attribute__((__always_inline__, __nodebug__))
_AddressOfReturnAddress(void)898f4a2713aSLionel Sambuc _AddressOfReturnAddress(void) {
899f4a2713aSLionel Sambuc   return (void*)((char*)__builtin_frame_address(0) + sizeof(void*));
900f4a2713aSLionel Sambuc }
901f4a2713aSLionel Sambuc static __inline__ void * __attribute__((__always_inline__, __nodebug__))
_ReturnAddress(void)902f4a2713aSLionel Sambuc _ReturnAddress(void) {
903f4a2713aSLionel Sambuc   return __builtin_return_address(0);
904f4a2713aSLionel Sambuc }
905*0a6a1f1dSLionel Sambuc #if defined(__i386__) || defined(__x86_64__)
906*0a6a1f1dSLionel Sambuc static __inline__ void __attribute__((__always_inline__, __nodebug__))
__cpuid(int __info[4],int __level)907*0a6a1f1dSLionel Sambuc __cpuid(int __info[4], int __level) {
908*0a6a1f1dSLionel Sambuc   __asm__ ("cpuid" : "=a"(__info[0]), "=b" (__info[1]), "=c"(__info[2]), "=d"(__info[3])
909*0a6a1f1dSLionel Sambuc                    : "a"(__level));
910*0a6a1f1dSLionel Sambuc }
911*0a6a1f1dSLionel Sambuc static __inline__ void __attribute__((__always_inline__, __nodebug__))
__cpuidex(int __info[4],int __level,int __ecx)912*0a6a1f1dSLionel Sambuc __cpuidex(int __info[4], int __level, int __ecx) {
913*0a6a1f1dSLionel Sambuc   __asm__ ("cpuid" : "=a"(__info[0]), "=b" (__info[1]), "=c"(__info[2]), "=d"(__info[3])
914*0a6a1f1dSLionel Sambuc                    : "a"(__level), "c"(__ecx));
915*0a6a1f1dSLionel Sambuc }
916*0a6a1f1dSLionel Sambuc static __inline__ unsigned __int64 __cdecl __attribute__((__always_inline__, __nodebug__))
_xgetbv(unsigned int __xcr_no)917*0a6a1f1dSLionel Sambuc _xgetbv(unsigned int __xcr_no) {
918*0a6a1f1dSLionel Sambuc   unsigned int __eax, __edx;
919*0a6a1f1dSLionel Sambuc   __asm__ ("xgetbv" : "=a" (__eax), "=d" (__edx) : "c" (__xcr_no));
920*0a6a1f1dSLionel Sambuc   return ((unsigned __int64)__edx << 32) | __eax;
921*0a6a1f1dSLionel Sambuc }
922*0a6a1f1dSLionel Sambuc static __inline__ void __attribute__((__always_inline__, __nodebug__))
__halt(void)923*0a6a1f1dSLionel Sambuc __halt(void) {
924*0a6a1f1dSLionel Sambuc   __asm__ volatile ("hlt");
925*0a6a1f1dSLionel Sambuc }
926*0a6a1f1dSLionel Sambuc #endif
927*0a6a1f1dSLionel Sambuc 
928*0a6a1f1dSLionel Sambuc /*----------------------------------------------------------------------------*\
929*0a6a1f1dSLionel Sambuc |* Privileged intrinsics
930*0a6a1f1dSLionel Sambuc \*----------------------------------------------------------------------------*/
931*0a6a1f1dSLionel Sambuc #if defined(__i386__) || defined(__x86_64__)
932*0a6a1f1dSLionel Sambuc static __inline__ unsigned __int64 __attribute__((__always_inline__, __nodebug__))
__readmsr(unsigned long __register)933*0a6a1f1dSLionel Sambuc __readmsr(unsigned long __register) {
934*0a6a1f1dSLionel Sambuc   // Loads the contents of a 64-bit model specific register (MSR) specified in
935*0a6a1f1dSLionel Sambuc   // the ECX register into registers EDX:EAX. The EDX register is loaded with
936*0a6a1f1dSLionel Sambuc   // the high-order 32 bits of the MSR and the EAX register is loaded with the
937*0a6a1f1dSLionel Sambuc   // low-order 32 bits. If less than 64 bits are implemented in the MSR being
938*0a6a1f1dSLionel Sambuc   // read, the values returned to EDX:EAX in unimplemented bit locations are
939*0a6a1f1dSLionel Sambuc   // undefined.
940*0a6a1f1dSLionel Sambuc   unsigned long __edx;
941*0a6a1f1dSLionel Sambuc   unsigned long __eax;
942*0a6a1f1dSLionel Sambuc   __asm__ ("rdmsr" : "=d"(__edx), "=a"(__eax) : "c"(__register));
943*0a6a1f1dSLionel Sambuc   return (((unsigned __int64)__edx) << 32) | (unsigned __int64)__eax;
944*0a6a1f1dSLionel Sambuc }
945*0a6a1f1dSLionel Sambuc 
946*0a6a1f1dSLionel Sambuc static __inline__ unsigned long __attribute__((always_inline, __nodebug__))
__readcr3(void)947*0a6a1f1dSLionel Sambuc __readcr3(void) {
948*0a6a1f1dSLionel Sambuc   unsigned long __cr3_val;
949*0a6a1f1dSLionel Sambuc   __asm__ __volatile__ ("mov %%cr3, %0" : "=q"(__cr3_val) : : "memory");
950*0a6a1f1dSLionel Sambuc   return __cr3_val;
951*0a6a1f1dSLionel Sambuc }
952*0a6a1f1dSLionel Sambuc 
953*0a6a1f1dSLionel Sambuc static __inline__ void __attribute__((always_inline, __nodebug__))
__writecr3(unsigned int __cr3_val)954*0a6a1f1dSLionel Sambuc __writecr3(unsigned int __cr3_val) {
955*0a6a1f1dSLionel Sambuc   __asm__ ("mov %0, %%cr3" : : "q"(__cr3_val) : "memory");
956*0a6a1f1dSLionel Sambuc }
957*0a6a1f1dSLionel Sambuc #endif
958f4a2713aSLionel Sambuc 
959f4a2713aSLionel Sambuc #ifdef __cplusplus
960f4a2713aSLionel Sambuc }
961f4a2713aSLionel Sambuc #endif
962f4a2713aSLionel Sambuc 
963f4a2713aSLionel Sambuc #endif /* __INTRIN_H */
964f4a2713aSLionel Sambuc #endif /* _MSC_VER */
965