1e5ae5b5826d42d2e26812699c4ced96a43c02eb7Andrew Hsieh/*===---- bmi2intrin.h - BMI2 intrinsics -----------------------------------=== 2e5ae5b5826d42d2e26812699c4ced96a43c02eb7Andrew Hsieh * 3e5ae5b5826d42d2e26812699c4ced96a43c02eb7Andrew Hsieh * Permission is hereby granted, free of charge, to any person obtaining a copy 4e5ae5b5826d42d2e26812699c4ced96a43c02eb7Andrew Hsieh * of this software and associated documentation files (the "Software"), to deal 5e5ae5b5826d42d2e26812699c4ced96a43c02eb7Andrew Hsieh * in the Software without restriction, including without limitation the rights 6e5ae5b5826d42d2e26812699c4ced96a43c02eb7Andrew Hsieh * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 7e5ae5b5826d42d2e26812699c4ced96a43c02eb7Andrew Hsieh * copies of the Software, and to permit persons to whom the Software is 8e5ae5b5826d42d2e26812699c4ced96a43c02eb7Andrew Hsieh * furnished to do so, subject to the following conditions: 9e5ae5b5826d42d2e26812699c4ced96a43c02eb7Andrew Hsieh * 10e5ae5b5826d42d2e26812699c4ced96a43c02eb7Andrew Hsieh * The above copyright notice and this permission notice shall be included in 11e5ae5b5826d42d2e26812699c4ced96a43c02eb7Andrew Hsieh * all copies or substantial portions of the Software. 12e5ae5b5826d42d2e26812699c4ced96a43c02eb7Andrew Hsieh * 13e5ae5b5826d42d2e26812699c4ced96a43c02eb7Andrew Hsieh * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 14e5ae5b5826d42d2e26812699c4ced96a43c02eb7Andrew Hsieh * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 15e5ae5b5826d42d2e26812699c4ced96a43c02eb7Andrew Hsieh * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 16e5ae5b5826d42d2e26812699c4ced96a43c02eb7Andrew Hsieh * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 17e5ae5b5826d42d2e26812699c4ced96a43c02eb7Andrew Hsieh * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 18e5ae5b5826d42d2e26812699c4ced96a43c02eb7Andrew Hsieh * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 19e5ae5b5826d42d2e26812699c4ced96a43c02eb7Andrew Hsieh * THE SOFTWARE. 20e5ae5b5826d42d2e26812699c4ced96a43c02eb7Andrew Hsieh * 21e5ae5b5826d42d2e26812699c4ced96a43c02eb7Andrew Hsieh *===-----------------------------------------------------------------------=== 22e5ae5b5826d42d2e26812699c4ced96a43c02eb7Andrew Hsieh */ 23e5ae5b5826d42d2e26812699c4ced96a43c02eb7Andrew Hsieh 24e5ae5b5826d42d2e26812699c4ced96a43c02eb7Andrew Hsieh#if !defined __X86INTRIN_H && !defined __IMMINTRIN_H 25e5ae5b5826d42d2e26812699c4ced96a43c02eb7Andrew Hsieh#error "Never use <bmi2intrin.h> directly; include <x86intrin.h> instead." 26e5ae5b5826d42d2e26812699c4ced96a43c02eb7Andrew Hsieh#endif 27e5ae5b5826d42d2e26812699c4ced96a43c02eb7Andrew Hsieh 28e5ae5b5826d42d2e26812699c4ced96a43c02eb7Andrew Hsieh#ifndef __BMI2__ 29e5ae5b5826d42d2e26812699c4ced96a43c02eb7Andrew Hsieh# error "BMI2 instruction set not enabled" 30e5ae5b5826d42d2e26812699c4ced96a43c02eb7Andrew Hsieh#endif /* __BMI2__ */ 31e5ae5b5826d42d2e26812699c4ced96a43c02eb7Andrew Hsieh 32e5ae5b5826d42d2e26812699c4ced96a43c02eb7Andrew Hsieh#ifndef __BMI2INTRIN_H 33e5ae5b5826d42d2e26812699c4ced96a43c02eb7Andrew Hsieh#define __BMI2INTRIN_H 34e5ae5b5826d42d2e26812699c4ced96a43c02eb7Andrew Hsieh 35e5ae5b5826d42d2e26812699c4ced96a43c02eb7Andrew Hsiehstatic __inline__ unsigned int __attribute__((__always_inline__, __nodebug__)) 36e5ae5b5826d42d2e26812699c4ced96a43c02eb7Andrew Hsieh_bzhi_u32(unsigned int __X, unsigned int __Y) 37e5ae5b5826d42d2e26812699c4ced96a43c02eb7Andrew Hsieh{ 38e5ae5b5826d42d2e26812699c4ced96a43c02eb7Andrew Hsieh return __builtin_ia32_bzhi_si(__X, __Y); 39e5ae5b5826d42d2e26812699c4ced96a43c02eb7Andrew Hsieh} 40e5ae5b5826d42d2e26812699c4ced96a43c02eb7Andrew Hsieh 41e5ae5b5826d42d2e26812699c4ced96a43c02eb7Andrew Hsiehstatic __inline__ unsigned int __attribute__((__always_inline__, __nodebug__)) 42e5ae5b5826d42d2e26812699c4ced96a43c02eb7Andrew Hsieh_pdep_u32(unsigned int __X, unsigned int __Y) 43e5ae5b5826d42d2e26812699c4ced96a43c02eb7Andrew Hsieh{ 44e5ae5b5826d42d2e26812699c4ced96a43c02eb7Andrew Hsieh return __builtin_ia32_pdep_si(__X, __Y); 45e5ae5b5826d42d2e26812699c4ced96a43c02eb7Andrew Hsieh} 46e5ae5b5826d42d2e26812699c4ced96a43c02eb7Andrew Hsieh 47e5ae5b5826d42d2e26812699c4ced96a43c02eb7Andrew Hsiehstatic __inline__ unsigned int __attribute__((__always_inline__, __nodebug__)) 48e5ae5b5826d42d2e26812699c4ced96a43c02eb7Andrew Hsieh_pext_u32(unsigned int __X, unsigned int __Y) 49e5ae5b5826d42d2e26812699c4ced96a43c02eb7Andrew Hsieh{ 50e5ae5b5826d42d2e26812699c4ced96a43c02eb7Andrew Hsieh return __builtin_ia32_pext_si(__X, __Y); 51e5ae5b5826d42d2e26812699c4ced96a43c02eb7Andrew Hsieh} 52e5ae5b5826d42d2e26812699c4ced96a43c02eb7Andrew Hsieh 53e5ae5b5826d42d2e26812699c4ced96a43c02eb7Andrew Hsieh#ifdef __x86_64__ 54e5ae5b5826d42d2e26812699c4ced96a43c02eb7Andrew Hsieh 55e5ae5b5826d42d2e26812699c4ced96a43c02eb7Andrew Hsiehstatic __inline__ unsigned long long __attribute__((__always_inline__, __nodebug__)) 56e5ae5b5826d42d2e26812699c4ced96a43c02eb7Andrew Hsieh_bzhi_u64(unsigned long long __X, unsigned long long __Y) 57e5ae5b5826d42d2e26812699c4ced96a43c02eb7Andrew Hsieh{ 58e5ae5b5826d42d2e26812699c4ced96a43c02eb7Andrew Hsieh return __builtin_ia32_bzhi_di(__X, __Y); 59e5ae5b5826d42d2e26812699c4ced96a43c02eb7Andrew Hsieh} 60e5ae5b5826d42d2e26812699c4ced96a43c02eb7Andrew Hsieh 61e5ae5b5826d42d2e26812699c4ced96a43c02eb7Andrew Hsiehstatic __inline__ unsigned long long __attribute__((__always_inline__, __nodebug__)) 62e5ae5b5826d42d2e26812699c4ced96a43c02eb7Andrew Hsieh_pdep_u64(unsigned long long __X, unsigned long long __Y) 63e5ae5b5826d42d2e26812699c4ced96a43c02eb7Andrew Hsieh{ 64e5ae5b5826d42d2e26812699c4ced96a43c02eb7Andrew Hsieh return __builtin_ia32_pdep_di(__X, __Y); 65e5ae5b5826d42d2e26812699c4ced96a43c02eb7Andrew Hsieh} 66e5ae5b5826d42d2e26812699c4ced96a43c02eb7Andrew Hsieh 67e5ae5b5826d42d2e26812699c4ced96a43c02eb7Andrew Hsiehstatic __inline__ unsigned long long __attribute__((__always_inline__, __nodebug__)) 68e5ae5b5826d42d2e26812699c4ced96a43c02eb7Andrew Hsieh_pext_u64(unsigned long long __X, unsigned long long __Y) 69e5ae5b5826d42d2e26812699c4ced96a43c02eb7Andrew Hsieh{ 70e5ae5b5826d42d2e26812699c4ced96a43c02eb7Andrew Hsieh return __builtin_ia32_pext_di(__X, __Y); 71e5ae5b5826d42d2e26812699c4ced96a43c02eb7Andrew Hsieh} 72e5ae5b5826d42d2e26812699c4ced96a43c02eb7Andrew Hsieh 73e5ae5b5826d42d2e26812699c4ced96a43c02eb7Andrew Hsiehstatic __inline__ unsigned long long __attribute__((__always_inline__, __nodebug__)) 74e5ae5b5826d42d2e26812699c4ced96a43c02eb7Andrew Hsieh_mulx_u64 (unsigned long long __X, unsigned long long __Y, 75e5ae5b5826d42d2e26812699c4ced96a43c02eb7Andrew Hsieh unsigned long long *__P) 76e5ae5b5826d42d2e26812699c4ced96a43c02eb7Andrew Hsieh{ 77e5ae5b5826d42d2e26812699c4ced96a43c02eb7Andrew Hsieh unsigned __int128 __res = (unsigned __int128) __X * __Y; 78e5ae5b5826d42d2e26812699c4ced96a43c02eb7Andrew Hsieh *__P = (unsigned long long) (__res >> 64); 79e5ae5b5826d42d2e26812699c4ced96a43c02eb7Andrew Hsieh return (unsigned long long) __res; 80e5ae5b5826d42d2e26812699c4ced96a43c02eb7Andrew Hsieh} 81e5ae5b5826d42d2e26812699c4ced96a43c02eb7Andrew Hsieh 82e5ae5b5826d42d2e26812699c4ced96a43c02eb7Andrew Hsieh#else /* !__x86_64__ */ 83e5ae5b5826d42d2e26812699c4ced96a43c02eb7Andrew Hsieh 84e5ae5b5826d42d2e26812699c4ced96a43c02eb7Andrew Hsiehstatic __inline__ unsigned int __attribute__((__always_inline__, __nodebug__)) 85e5ae5b5826d42d2e26812699c4ced96a43c02eb7Andrew Hsieh_mulx_u32 (unsigned int __X, unsigned int __Y, unsigned int *__P) 86e5ae5b5826d42d2e26812699c4ced96a43c02eb7Andrew Hsieh{ 87e5ae5b5826d42d2e26812699c4ced96a43c02eb7Andrew Hsieh unsigned long long __res = (unsigned long long) __X * __Y; 88e5ae5b5826d42d2e26812699c4ced96a43c02eb7Andrew Hsieh *__P = (unsigned int) (__res >> 32); 89e5ae5b5826d42d2e26812699c4ced96a43c02eb7Andrew Hsieh return (unsigned int) __res; 90e5ae5b5826d42d2e26812699c4ced96a43c02eb7Andrew Hsieh} 91e5ae5b5826d42d2e26812699c4ced96a43c02eb7Andrew Hsieh 92e5ae5b5826d42d2e26812699c4ced96a43c02eb7Andrew Hsieh#endif /* !__x86_64__ */ 93e5ae5b5826d42d2e26812699c4ced96a43c02eb7Andrew Hsieh 94e5ae5b5826d42d2e26812699c4ced96a43c02eb7Andrew Hsieh#endif /* __BMI2INTRIN_H */ 95