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