15c75208a5b88c835bce0a1671015c7e22c72f35fCraig Topper/*===---- bmiintrin.h - BMI intrinsics -------------------------------------=== 25c75208a5b88c835bce0a1671015c7e22c72f35fCraig Topper * 35c75208a5b88c835bce0a1671015c7e22c72f35fCraig Topper * Permission is hereby granted, free of charge, to any person obtaining a copy 45c75208a5b88c835bce0a1671015c7e22c72f35fCraig Topper * of this software and associated documentation files (the "Software"), to deal 55c75208a5b88c835bce0a1671015c7e22c72f35fCraig Topper * in the Software without restriction, including without limitation the rights 65c75208a5b88c835bce0a1671015c7e22c72f35fCraig Topper * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 75c75208a5b88c835bce0a1671015c7e22c72f35fCraig Topper * copies of the Software, and to permit persons to whom the Software is 85c75208a5b88c835bce0a1671015c7e22c72f35fCraig Topper * furnished to do so, subject to the following conditions: 95c75208a5b88c835bce0a1671015c7e22c72f35fCraig Topper * 105c75208a5b88c835bce0a1671015c7e22c72f35fCraig Topper * The above copyright notice and this permission notice shall be included in 115c75208a5b88c835bce0a1671015c7e22c72f35fCraig Topper * all copies or substantial portions of the Software. 125c75208a5b88c835bce0a1671015c7e22c72f35fCraig Topper * 135c75208a5b88c835bce0a1671015c7e22c72f35fCraig Topper * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 145c75208a5b88c835bce0a1671015c7e22c72f35fCraig Topper * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 155c75208a5b88c835bce0a1671015c7e22c72f35fCraig Topper * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 165c75208a5b88c835bce0a1671015c7e22c72f35fCraig Topper * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 175c75208a5b88c835bce0a1671015c7e22c72f35fCraig Topper * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 185c75208a5b88c835bce0a1671015c7e22c72f35fCraig Topper * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 195c75208a5b88c835bce0a1671015c7e22c72f35fCraig Topper * THE SOFTWARE. 205c75208a5b88c835bce0a1671015c7e22c72f35fCraig Topper * 215c75208a5b88c835bce0a1671015c7e22c72f35fCraig Topper *===-----------------------------------------------------------------------=== 225c75208a5b88c835bce0a1671015c7e22c72f35fCraig Topper */ 235c75208a5b88c835bce0a1671015c7e22c72f35fCraig Topper 245c75208a5b88c835bce0a1671015c7e22c72f35fCraig Topper#if !defined __X86INTRIN_H && !defined __IMMINTRIN_H 255c75208a5b88c835bce0a1671015c7e22c72f35fCraig Topper#error "Never use <bmiintrin.h> directly; include <x86intrin.h> instead." 265c75208a5b88c835bce0a1671015c7e22c72f35fCraig Topper#endif 275c75208a5b88c835bce0a1671015c7e22c72f35fCraig Topper 285c75208a5b88c835bce0a1671015c7e22c72f35fCraig Topper#ifndef __BMI__ 295c75208a5b88c835bce0a1671015c7e22c72f35fCraig Topper# error "BMI instruction set not enabled" 305c75208a5b88c835bce0a1671015c7e22c72f35fCraig Topper#endif /* __BMI__ */ 315c75208a5b88c835bce0a1671015c7e22c72f35fCraig Topper 325c75208a5b88c835bce0a1671015c7e22c72f35fCraig Topper#ifndef __BMIINTRIN_H 335c75208a5b88c835bce0a1671015c7e22c72f35fCraig Topper#define __BMIINTRIN_H 345c75208a5b88c835bce0a1671015c7e22c72f35fCraig Topper 355c75208a5b88c835bce0a1671015c7e22c72f35fCraig Topperstatic __inline__ unsigned short __attribute__((__always_inline__, __nodebug__)) 36435d2682893f5ebdaa652b567073ff223f5ada4fCraig Topper__tzcnt_u16(unsigned short __X) 375c75208a5b88c835bce0a1671015c7e22c72f35fCraig Topper{ 385c75208a5b88c835bce0a1671015c7e22c72f35fCraig Topper return __builtin_ctzs(__X); 395c75208a5b88c835bce0a1671015c7e22c72f35fCraig Topper} 405c75208a5b88c835bce0a1671015c7e22c72f35fCraig Topper 415c75208a5b88c835bce0a1671015c7e22c72f35fCraig Topperstatic __inline__ unsigned int __attribute__((__always_inline__, __nodebug__)) 420b269c1f3641511fb397488f8b82850416892fc9Craig Topper__andn_u32(unsigned int __X, unsigned int __Y) 430b269c1f3641511fb397488f8b82850416892fc9Craig Topper{ 440b269c1f3641511fb397488f8b82850416892fc9Craig Topper return ~__X & __Y; 450b269c1f3641511fb397488f8b82850416892fc9Craig Topper} 460b269c1f3641511fb397488f8b82850416892fc9Craig Topper 470b269c1f3641511fb397488f8b82850416892fc9Craig Topperstatic __inline__ unsigned int __attribute__((__always_inline__, __nodebug__)) 480b269c1f3641511fb397488f8b82850416892fc9Craig Topper__bextr_u32(unsigned int __X, unsigned int __Y) 490b269c1f3641511fb397488f8b82850416892fc9Craig Topper{ 500b269c1f3641511fb397488f8b82850416892fc9Craig Topper return __builtin_ia32_bextr_u32(__X, __Y); 510b269c1f3641511fb397488f8b82850416892fc9Craig Topper} 520b269c1f3641511fb397488f8b82850416892fc9Craig Topper 530b269c1f3641511fb397488f8b82850416892fc9Craig Topperstatic __inline__ unsigned int __attribute__((__always_inline__, __nodebug__)) 540b269c1f3641511fb397488f8b82850416892fc9Craig Topper__blsi_u32(unsigned int __X) 550b269c1f3641511fb397488f8b82850416892fc9Craig Topper{ 560b269c1f3641511fb397488f8b82850416892fc9Craig Topper return __X & -__X; 570b269c1f3641511fb397488f8b82850416892fc9Craig Topper} 580b269c1f3641511fb397488f8b82850416892fc9Craig Topper 590b269c1f3641511fb397488f8b82850416892fc9Craig Topperstatic __inline__ unsigned int __attribute__((__always_inline__, __nodebug__)) 600b269c1f3641511fb397488f8b82850416892fc9Craig Topper__blsmsk_u32(unsigned int __X) 610b269c1f3641511fb397488f8b82850416892fc9Craig Topper{ 620b269c1f3641511fb397488f8b82850416892fc9Craig Topper return __X ^ (__X - 1); 630b269c1f3641511fb397488f8b82850416892fc9Craig Topper} 640b269c1f3641511fb397488f8b82850416892fc9Craig Topper 650b269c1f3641511fb397488f8b82850416892fc9Craig Topperstatic __inline__ unsigned int __attribute__((__always_inline__, __nodebug__)) 660b269c1f3641511fb397488f8b82850416892fc9Craig Topper__blsr_u32(unsigned int __X) 670b269c1f3641511fb397488f8b82850416892fc9Craig Topper{ 680b269c1f3641511fb397488f8b82850416892fc9Craig Topper return __X & (__X - 1); 690b269c1f3641511fb397488f8b82850416892fc9Craig Topper} 700b269c1f3641511fb397488f8b82850416892fc9Craig Topper 710b269c1f3641511fb397488f8b82850416892fc9Craig Topperstatic __inline__ unsigned int __attribute__((__always_inline__, __nodebug__)) 72435d2682893f5ebdaa652b567073ff223f5ada4fCraig Topper__tzcnt_u32(unsigned int __X) 735c75208a5b88c835bce0a1671015c7e22c72f35fCraig Topper{ 745c75208a5b88c835bce0a1671015c7e22c72f35fCraig Topper return __builtin_ctz(__X); 755c75208a5b88c835bce0a1671015c7e22c72f35fCraig Topper} 765c75208a5b88c835bce0a1671015c7e22c72f35fCraig Topper 775c75208a5b88c835bce0a1671015c7e22c72f35fCraig Topper#ifdef __x86_64__ 785c75208a5b88c835bce0a1671015c7e22c72f35fCraig Topperstatic __inline__ unsigned long long __attribute__((__always_inline__, __nodebug__)) 790b269c1f3641511fb397488f8b82850416892fc9Craig Topper__andn_u64 (unsigned long long __X, unsigned long long __Y) 800b269c1f3641511fb397488f8b82850416892fc9Craig Topper{ 810b269c1f3641511fb397488f8b82850416892fc9Craig Topper return ~__X & __Y; 820b269c1f3641511fb397488f8b82850416892fc9Craig Topper} 830b269c1f3641511fb397488f8b82850416892fc9Craig Topper 840b269c1f3641511fb397488f8b82850416892fc9Craig Topperstatic __inline__ unsigned long long __attribute__((__always_inline__, __nodebug__)) 850b269c1f3641511fb397488f8b82850416892fc9Craig Topper__bextr_u64(unsigned long long __X, unsigned long long __Y) 860b269c1f3641511fb397488f8b82850416892fc9Craig Topper{ 870b269c1f3641511fb397488f8b82850416892fc9Craig Topper return __builtin_ia32_bextr_u64(__X, __Y); 880b269c1f3641511fb397488f8b82850416892fc9Craig Topper} 890b269c1f3641511fb397488f8b82850416892fc9Craig Topper 900b269c1f3641511fb397488f8b82850416892fc9Craig Topperstatic __inline__ unsigned long long __attribute__((__always_inline__, __nodebug__)) 910b269c1f3641511fb397488f8b82850416892fc9Craig Topper__blsi_u64(unsigned long long __X) 920b269c1f3641511fb397488f8b82850416892fc9Craig Topper{ 930b269c1f3641511fb397488f8b82850416892fc9Craig Topper return __X & -__X; 940b269c1f3641511fb397488f8b82850416892fc9Craig Topper} 950b269c1f3641511fb397488f8b82850416892fc9Craig Topper 960b269c1f3641511fb397488f8b82850416892fc9Craig Topperstatic __inline__ unsigned long long __attribute__((__always_inline__, __nodebug__)) 970b269c1f3641511fb397488f8b82850416892fc9Craig Topper__blsmsk_u64(unsigned long long __X) 980b269c1f3641511fb397488f8b82850416892fc9Craig Topper{ 990b269c1f3641511fb397488f8b82850416892fc9Craig Topper return __X ^ (__X - 1); 1000b269c1f3641511fb397488f8b82850416892fc9Craig Topper} 1010b269c1f3641511fb397488f8b82850416892fc9Craig Topper 1020b269c1f3641511fb397488f8b82850416892fc9Craig Topperstatic __inline__ unsigned long long __attribute__((__always_inline__, __nodebug__)) 1030b269c1f3641511fb397488f8b82850416892fc9Craig Topper__blsr_u64(unsigned long long __X) 1040b269c1f3641511fb397488f8b82850416892fc9Craig Topper{ 1050b269c1f3641511fb397488f8b82850416892fc9Craig Topper return __X & (__X - 1); 1060b269c1f3641511fb397488f8b82850416892fc9Craig Topper} 1070b269c1f3641511fb397488f8b82850416892fc9Craig Topper 1080b269c1f3641511fb397488f8b82850416892fc9Craig Topperstatic __inline__ unsigned long long __attribute__((__always_inline__, __nodebug__)) 109435d2682893f5ebdaa652b567073ff223f5ada4fCraig Topper__tzcnt_u64(unsigned long long __X) 1105c75208a5b88c835bce0a1671015c7e22c72f35fCraig Topper{ 1115c75208a5b88c835bce0a1671015c7e22c72f35fCraig Topper return __builtin_ctzll(__X); 1125c75208a5b88c835bce0a1671015c7e22c72f35fCraig Topper} 1135c75208a5b88c835bce0a1671015c7e22c72f35fCraig Topper#endif 1145c75208a5b88c835bce0a1671015c7e22c72f35fCraig Topper 1155c75208a5b88c835bce0a1671015c7e22c72f35fCraig Topper#endif /* __BMIINTRIN_H */ 116