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