1 /*===---- immintrin.h - Intel intrinsics -----------------------------------===
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:
10 * The above copyright notice and this permission notice shall be included in
11 * all copies or substantial portions of the Software.
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
21 *===-----------------------------------------------------------------------===
27 #if !defined(_MSC_VER) || __has_feature(modules) || defined(__MMX__)
31 #if !defined(_MSC_VER) || __has_feature(modules) || defined(__SSE__)
32 #include <xmmintrin.h>
35 #if !defined(_MSC_VER) || __has_feature(modules) || defined(__SSE2__)
36 #include <emmintrin.h>
39 #if !defined(_MSC_VER) || __has_feature(modules) || defined(__SSE3__)
40 #include <pmmintrin.h>
43 #if !defined(_MSC_VER) || __has_feature(modules) || defined(__SSSE3__)
44 #include <tmmintrin.h>
47 #if !defined(_MSC_VER) || __has_feature(modules) || \
48 (defined(__SSE4_2__) || defined(__SSE4_1__))
49 #include <smmintrin.h>
52 #if !defined(_MSC_VER) || __has_feature(modules) || \
53 (defined(__AES__) || defined(__PCLMUL__))
54 #include <wmmintrin.h>
57 #if !defined(_MSC_VER) || __has_feature(modules) || defined(__CLFLUSHOPT__)
58 #include <clflushoptintrin.h>
61 #if !defined(_MSC_VER) || __has_feature(modules) || defined(__CLWB__)
62 #include <clwbintrin.h>
65 #if !defined(_MSC_VER) || __has_feature(modules) || defined(__AVX__)
66 #include <avxintrin.h>
69 #if !defined(_MSC_VER) || __has_feature(modules) || defined(__AVX2__)
70 #include <avx2intrin.h>
73 #if !defined(_MSC_VER) || __has_feature(modules) || defined(__F16C__)
74 #include <f16cintrin.h>
77 #if !defined(_MSC_VER) || __has_feature(modules) || defined(__VPCLMULQDQ__)
78 #include <vpclmulqdqintrin.h>
81 #if !defined(_MSC_VER) || __has_feature(modules) || defined(__BMI__)
82 #include <bmiintrin.h>
85 #if !defined(_MSC_VER) || __has_feature(modules) || defined(__BMI2__)
86 #include <bmi2intrin.h>
89 #if !defined(_MSC_VER) || __has_feature(modules) || defined(__LZCNT__)
90 #include <lzcntintrin.h>
93 #if !defined(_MSC_VER) || __has_feature(modules) || defined(__POPCNT__)
94 #include <popcntintrin.h>
97 #if !defined(_MSC_VER) || __has_feature(modules) || defined(__FMA__)
98 #include <fmaintrin.h>
101 #if !defined(_MSC_VER) || __has_feature(modules) || defined(__AVX512F__)
102 #include <avx512fintrin.h>
105 #if !defined(_MSC_VER) || __has_feature(modules) || defined(__AVX512VL__)
106 #include <avx512vlintrin.h>
109 #if !defined(_MSC_VER) || __has_feature(modules) || defined(__AVX512BW__)
110 #include <avx512bwintrin.h>
113 #if !defined(_MSC_VER) || __has_feature(modules) || defined(__AVX512BITALG__)
114 #include <avx512bitalgintrin.h>
117 #if !defined(_MSC_VER) || __has_feature(modules) || defined(__AVX512CD__)
118 #include <avx512cdintrin.h>
121 #if !defined(_MSC_VER) || __has_feature(modules) || defined(__AVX512VPOPCNTDQ__)
122 #include <avx512vpopcntdqintrin.h>
125 #if !defined(_MSC_VER) || __has_feature(modules) || \
126 (defined(__AVX512VL__) && defined(__AVX512VPOPCNTDQ__))
127 #include <avx512vpopcntdqvlintrin.h>
130 #if !defined(_MSC_VER) || __has_feature(modules) || defined(__AVX512VNNI__)
131 #include <avx512vnniintrin.h>
134 #if !defined(_MSC_VER) || __has_feature(modules) || \
135 (defined(__AVX512VL__) && defined(__AVX512VNNI__))
136 #include <avx512vlvnniintrin.h>
139 #if !defined(_MSC_VER) || __has_feature(modules) || defined(__AVX512DQ__)
140 #include <avx512dqintrin.h>
143 #if !defined(_MSC_VER) || __has_feature(modules) || \
144 (defined(__AVX512VL__) && defined(__AVX512BITALG__))
145 #include <avx512vlbitalgintrin.h>
148 #if !defined(_MSC_VER) || __has_feature(modules) || \
149 (defined(__AVX512VL__) && defined(__AVX512BW__))
150 #include <avx512vlbwintrin.h>
153 #if !defined(_MSC_VER) || __has_feature(modules) || \
154 (defined(__AVX512VL__) && defined(__AVX512CD__))
155 #include <avx512vlcdintrin.h>
158 #if !defined(_MSC_VER) || __has_feature(modules) || \
159 (defined(__AVX512VL__) && defined(__AVX512DQ__))
160 #include <avx512vldqintrin.h>
163 #if !defined(_MSC_VER) || __has_feature(modules) || defined(__AVX512ER__)
164 #include <avx512erintrin.h>
167 #if !defined(_MSC_VER) || __has_feature(modules) || defined(__AVX512IFMA__)
168 #include <avx512ifmaintrin.h>
171 #if !defined(_MSC_VER) || __has_feature(modules) || \
172 (defined(__AVX512IFMA__) && defined(__AVX512VL__))
173 #include <avx512ifmavlintrin.h>
176 #if !defined(_MSC_VER) || __has_feature(modules) || defined(__AVX512VBMI__)
177 #include <avx512vbmiintrin.h>
180 #if !defined(_MSC_VER) || __has_feature(modules) || \
181 (defined(__AVX512VBMI__) && defined(__AVX512VL__))
182 #include <avx512vbmivlintrin.h>
185 #if !defined(_MSC_VER) || __has_feature(modules) || defined(__AVX512VBMI2__)
186 #include <avx512vbmi2intrin.h>
189 #if !defined(_MSC_VER) || __has_feature(modules) || \
190 (defined(__AVX512VBMI2__) && defined(__AVX512VL__))
191 #include <avx512vlvbmi2intrin.h>
194 #if !defined(_MSC_VER) || __has_feature(modules) || defined(__AVX512PF__)
195 #include <avx512pfintrin.h>
198 #if !defined(_MSC_VER) || __has_feature(modules) || defined(__PKU__)
199 #include <pkuintrin.h>
202 #if !defined(_MSC_VER) || __has_feature(modules) || defined(__VAES__)
203 #include <vaesintrin.h>
206 #if !defined(_MSC_VER) || __has_feature(modules) || defined(__GFNI__)
207 #include <gfniintrin.h>
210 #if !defined(_MSC_VER) || __has_feature(modules) || defined(__RDPID__)
211 /// Returns the value of the IA32_TSC_AUX MSR (0xc0000103).
213 /// \headerfile <immintrin.h>
215 /// This intrinsic corresponds to the <c> RDPID </c> instruction.
216 static __inline__ unsigned int __attribute__((__always_inline__, __nodebug__, __target__("rdpid")))
218 return __builtin_ia32_rdpid();
222 #if !defined(_MSC_VER) || __has_feature(modules) || defined(__RDRND__)
223 static __inline__ int __attribute__((__always_inline__, __nodebug__, __target__("rdrnd")))
224 _rdrand16_step(unsigned short *__p)
226 return __builtin_ia32_rdrand16_step(__p);
229 static __inline__ int __attribute__((__always_inline__, __nodebug__, __target__("rdrnd")))
230 _rdrand32_step(unsigned int *__p)
232 return __builtin_ia32_rdrand32_step(__p);
236 static __inline__ int __attribute__((__always_inline__, __nodebug__, __target__("rdrnd")))
237 _rdrand64_step(unsigned long long *__p)
239 return __builtin_ia32_rdrand64_step(__p);
242 #endif /* __RDRND__ */
244 /* __bit_scan_forward */
245 static __inline__ int __attribute__((__always_inline__, __nodebug__))
246 _bit_scan_forward(int __A) {
247 return __builtin_ctz(__A);
250 /* __bit_scan_reverse */
251 static __inline__ int __attribute__((__always_inline__, __nodebug__))
252 _bit_scan_reverse(int __A) {
253 return 31 - __builtin_clz(__A);
256 #if !defined(_MSC_VER) || __has_feature(modules) || defined(__FSGSBASE__)
258 static __inline__ unsigned int __attribute__((__always_inline__, __nodebug__, __target__("fsgsbase")))
259 _readfsbase_u32(void)
261 return __builtin_ia32_rdfsbase32();
264 static __inline__ unsigned long long __attribute__((__always_inline__, __nodebug__, __target__("fsgsbase")))
265 _readfsbase_u64(void)
267 return __builtin_ia32_rdfsbase64();
270 static __inline__ unsigned int __attribute__((__always_inline__, __nodebug__, __target__("fsgsbase")))
271 _readgsbase_u32(void)
273 return __builtin_ia32_rdgsbase32();
276 static __inline__ unsigned long long __attribute__((__always_inline__, __nodebug__, __target__("fsgsbase")))
277 _readgsbase_u64(void)
279 return __builtin_ia32_rdgsbase64();
282 static __inline__ void __attribute__((__always_inline__, __nodebug__, __target__("fsgsbase")))
283 _writefsbase_u32(unsigned int __V)
285 __builtin_ia32_wrfsbase32(__V);
288 static __inline__ void __attribute__((__always_inline__, __nodebug__, __target__("fsgsbase")))
289 _writefsbase_u64(unsigned long long __V)
291 __builtin_ia32_wrfsbase64(__V);
294 static __inline__ void __attribute__((__always_inline__, __nodebug__, __target__("fsgsbase")))
295 _writegsbase_u32(unsigned int __V)
297 __builtin_ia32_wrgsbase32(__V);
300 static __inline__ void __attribute__((__always_inline__, __nodebug__, __target__("fsgsbase")))
301 _writegsbase_u64(unsigned long long __V)
303 __builtin_ia32_wrgsbase64(__V);
307 #endif /* __FSGSBASE__ */
309 #if !defined(_MSC_VER) || __has_feature(modules) || defined(__MOVBE__)
311 /* The structs used below are to force the load/store to be unaligned. This
312 * is accomplished with the __packed__ attribute. The __may_alias__ prevents
313 * tbaa metadata from being generated based on the struct and the type of the
314 * field inside of it.
317 static __inline__ short __attribute__((__always_inline__, __nodebug__, __target__("movbe")))
318 _loadbe_i16(void const * __P) {
321 } __attribute__((__packed__, __may_alias__));
322 return __builtin_bswap16(((struct __loadu_i16*)__P)->__v);
325 static __inline__ void __attribute__((__always_inline__, __nodebug__, __target__("movbe")))
326 _storebe_i16(void * __P, short __D) {
327 struct __storeu_i16 {
329 } __attribute__((__packed__, __may_alias__));
330 ((struct __storeu_i16*)__P)->__v = __builtin_bswap16(__D);
333 static __inline__ int __attribute__((__always_inline__, __nodebug__, __target__("movbe")))
334 _loadbe_i32(void const * __P) {
337 } __attribute__((__packed__, __may_alias__));
338 return __builtin_bswap32(((struct __loadu_i32*)__P)->__v);
341 static __inline__ void __attribute__((__always_inline__, __nodebug__, __target__("movbe")))
342 _storebe_i32(void * __P, int __D) {
343 struct __storeu_i32 {
345 } __attribute__((__packed__, __may_alias__));
346 ((struct __storeu_i32*)__P)->__v = __builtin_bswap32(__D);
350 static __inline__ long long __attribute__((__always_inline__, __nodebug__, __target__("movbe")))
351 _loadbe_i64(void const * __P) {
354 } __attribute__((__packed__, __may_alias__));
355 return __builtin_bswap64(((struct __loadu_i64*)__P)->__v);
358 static __inline__ void __attribute__((__always_inline__, __nodebug__, __target__("movbe")))
359 _storebe_i64(void * __P, long long __D) {
360 struct __storeu_i64 {
362 } __attribute__((__packed__, __may_alias__));
363 ((struct __storeu_i64*)__P)->__v = __builtin_bswap64(__D);
368 #if !defined(_MSC_VER) || __has_feature(modules) || defined(__RTM__)
369 #include <rtmintrin.h>
370 #include <xtestintrin.h>
373 #if !defined(_MSC_VER) || __has_feature(modules) || defined(__SHA__)
374 #include <shaintrin.h>
377 #if !defined(_MSC_VER) || __has_feature(modules) || defined(__FXSR__)
378 #include <fxsrintrin.h>
381 #if !defined(_MSC_VER) || __has_feature(modules) || defined(__XSAVE__)
382 #include <xsaveintrin.h>
385 #if !defined(_MSC_VER) || __has_feature(modules) || defined(__XSAVEOPT__)
386 #include <xsaveoptintrin.h>
389 #if !defined(_MSC_VER) || __has_feature(modules) || defined(__XSAVEC__)
390 #include <xsavecintrin.h>
393 #if !defined(_MSC_VER) || __has_feature(modules) || defined(__XSAVES__)
394 #include <xsavesintrin.h>
397 #if !defined(_MSC_VER) || __has_feature(modules) || defined(__SHSTK__)
398 #include <cetintrin.h>
401 /* Some intrinsics inside adxintrin.h are available only on processors with ADX,
402 * whereas others are also available at all times. */
403 #include <adxintrin.h>
405 #if !defined(_MSC_VER) || __has_feature(modules) || defined(__RDSEED__)
406 #include <rdseedintrin.h>
409 #if !defined(_MSC_VER) || __has_feature(modules) || defined(__WBNOINVD__)
410 #include <wbnoinvdintrin.h>
413 #if !defined(_MSC_VER) || __has_feature(modules) || defined(__CLDEMOTE__)
414 #include <cldemoteintrin.h>
417 #if !defined(_MSC_VER) || __has_feature(modules) || defined(__WAITPKG__)
418 #include <waitpkgintrin.h>
421 #if !defined(_MSC_VER) || __has_feature(modules) || \
422 defined(__MOVDIRI__) || defined(__MOVDIR64B__)
423 #include <movdirintrin.h>
426 #if !defined(_MSC_VER) || __has_feature(modules) || defined(__PCONFIG__)
427 #include <pconfigintrin.h>
430 #if !defined(_MSC_VER) || __has_feature(modules) || defined(__SGX__)
431 #include <sgxintrin.h>
434 #if !defined(_MSC_VER) || __has_feature(modules) || defined(__PTWRITE__)
435 #include <ptwriteintrin.h>
438 #if !defined(_MSC_VER) || __has_feature(modules) || defined(__INVPCID__)
439 #include <invpcidintrin.h>
443 /* Define the default attributes for these intrinsics */
444 #define __DEFAULT_FN_ATTRS __attribute__((__always_inline__, __nodebug__))
448 /*----------------------------------------------------------------------------*\
449 |* Interlocked Exchange HLE
450 \*----------------------------------------------------------------------------*/
451 #if defined(__i386__) || defined(__x86_64__)
452 static __inline__ long __DEFAULT_FN_ATTRS
453 _InterlockedExchange_HLEAcquire(long volatile *_Target, long _Value) {
454 __asm__ __volatile__(".byte 0xf2 ; lock ; xchg %0, %1"
455 : "+r" (_Value), "+m" (*_Target) :: "memory");
458 static __inline__ long __DEFAULT_FN_ATTRS
459 _InterlockedExchange_HLERelease(long volatile *_Target, long _Value) {
460 __asm__ __volatile__(".byte 0xf3 ; lock ; xchg %0, %1"
461 : "+r" (_Value), "+m" (*_Target) :: "memory");
465 #if defined(__x86_64__)
466 static __inline__ __int64 __DEFAULT_FN_ATTRS
467 _InterlockedExchange64_HLEAcquire(__int64 volatile *_Target, __int64 _Value) {
468 __asm__ __volatile__(".byte 0xf2 ; lock ; xchg %0, %1"
469 : "+r" (_Value), "+m" (*_Target) :: "memory");
472 static __inline__ __int64 __DEFAULT_FN_ATTRS
473 _InterlockedExchange64_HLERelease(__int64 volatile *_Target, __int64 _Value) {
474 __asm__ __volatile__(".byte 0xf3 ; lock ; xchg %0, %1"
475 : "+r" (_Value), "+m" (*_Target) :: "memory");
479 /*----------------------------------------------------------------------------*\
480 |* Interlocked Compare Exchange HLE
481 \*----------------------------------------------------------------------------*/
482 #if defined(__i386__) || defined(__x86_64__)
483 static __inline__ long __DEFAULT_FN_ATTRS
484 _InterlockedCompareExchange_HLEAcquire(long volatile *_Destination,
485 long _Exchange, long _Comparand) {
486 __asm__ __volatile__(".byte 0xf2 ; lock ; cmpxchg %2, %1"
487 : "+a" (_Comparand), "+m" (*_Destination)
488 : "r" (_Exchange) : "memory");
491 static __inline__ long __DEFAULT_FN_ATTRS
492 _InterlockedCompareExchange_HLERelease(long volatile *_Destination,
493 long _Exchange, long _Comparand) {
494 __asm__ __volatile__(".byte 0xf3 ; lock ; cmpxchg %2, %1"
495 : "+a" (_Comparand), "+m" (*_Destination)
496 : "r" (_Exchange) : "memory");
500 #if defined(__x86_64__)
501 static __inline__ __int64 __DEFAULT_FN_ATTRS
502 _InterlockedCompareExchange64_HLEAcquire(__int64 volatile *_Destination,
503 __int64 _Exchange, __int64 _Comparand) {
504 __asm__ __volatile__(".byte 0xf2 ; lock ; cmpxchg %2, %1"
505 : "+a" (_Comparand), "+m" (*_Destination)
506 : "r" (_Exchange) : "memory");
509 static __inline__ __int64 __DEFAULT_FN_ATTRS
510 _InterlockedCompareExchange64_HLERelease(__int64 volatile *_Destination,
511 __int64 _Exchange, __int64 _Comparand) {
512 __asm__ __volatile__(".byte 0xf3 ; lock ; cmpxchg %2, %1"
513 : "+a" (_Comparand), "+m" (*_Destination)
514 : "r" (_Exchange) : "memory");
522 #undef __DEFAULT_FN_ATTRS
524 #endif /* _MSC_VER */
526 #endif /* __IMMINTRIN_H */