xref: /minix3/external/bsd/llvm/dist/clang/lib/Headers/bmiintrin.h (revision 0a6a1f1d05b60e214de2f05a7310ddd1f0e590e7)
1f4a2713aSLionel Sambuc /*===---- bmiintrin.h - BMI intrinsics -------------------------------------===
2f4a2713aSLionel Sambuc  *
3f4a2713aSLionel Sambuc  * Permission is hereby granted, free of charge, to any person obtaining a copy
4f4a2713aSLionel Sambuc  * of this software and associated documentation files (the "Software"), to deal
5f4a2713aSLionel Sambuc  * in the Software without restriction, including without limitation the rights
6f4a2713aSLionel Sambuc  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7f4a2713aSLionel Sambuc  * copies of the Software, and to permit persons to whom the Software is
8f4a2713aSLionel Sambuc  * furnished to do so, subject to the following conditions:
9f4a2713aSLionel Sambuc  *
10f4a2713aSLionel Sambuc  * The above copyright notice and this permission notice shall be included in
11f4a2713aSLionel Sambuc  * all copies or substantial portions of the Software.
12f4a2713aSLionel Sambuc  *
13f4a2713aSLionel Sambuc  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14f4a2713aSLionel Sambuc  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15f4a2713aSLionel Sambuc  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16f4a2713aSLionel Sambuc  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17f4a2713aSLionel Sambuc  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18f4a2713aSLionel Sambuc  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19f4a2713aSLionel Sambuc  * THE SOFTWARE.
20f4a2713aSLionel Sambuc  *
21f4a2713aSLionel Sambuc  *===-----------------------------------------------------------------------===
22f4a2713aSLionel Sambuc  */
23f4a2713aSLionel Sambuc 
24f4a2713aSLionel Sambuc #if !defined __X86INTRIN_H && !defined __IMMINTRIN_H
25f4a2713aSLionel Sambuc #error "Never use <bmiintrin.h> directly; include <x86intrin.h> instead."
26f4a2713aSLionel Sambuc #endif
27f4a2713aSLionel Sambuc 
28f4a2713aSLionel Sambuc #ifndef __BMI__
29f4a2713aSLionel Sambuc # error "BMI instruction set not enabled"
30f4a2713aSLionel Sambuc #endif /* __BMI__ */
31f4a2713aSLionel Sambuc 
32f4a2713aSLionel Sambuc #ifndef __BMIINTRIN_H
33f4a2713aSLionel Sambuc #define __BMIINTRIN_H
34f4a2713aSLionel Sambuc 
35*0a6a1f1dSLionel Sambuc #define _tzcnt_u16(a)     (__tzcnt_u16((a)))
36*0a6a1f1dSLionel Sambuc #define _andn_u32(a, b)   (__andn_u32((a), (b)))
37*0a6a1f1dSLionel Sambuc /* _bextr_u32 != __bextr_u32 */
38*0a6a1f1dSLionel Sambuc #define _blsi_u32(a)      (__blsi_u32((a)))
39*0a6a1f1dSLionel Sambuc #define _blsmsk_u32(a)    (__blsmsk_u32((a)))
40*0a6a1f1dSLionel Sambuc #define _blsr_u32(a)      (__blsr_u32((a)))
41*0a6a1f1dSLionel Sambuc #define _tzcnt_u32(a)     (__tzcnt_u32((a)))
42*0a6a1f1dSLionel Sambuc 
43f4a2713aSLionel Sambuc static __inline__ unsigned short __attribute__((__always_inline__, __nodebug__))
__tzcnt_u16(unsigned short __X)44f4a2713aSLionel Sambuc __tzcnt_u16(unsigned short __X)
45f4a2713aSLionel Sambuc {
46*0a6a1f1dSLionel Sambuc   return __X ? __builtin_ctzs(__X) : 16;
47f4a2713aSLionel Sambuc }
48f4a2713aSLionel Sambuc 
49f4a2713aSLionel Sambuc static __inline__ unsigned int __attribute__((__always_inline__, __nodebug__))
__andn_u32(unsigned int __X,unsigned int __Y)50f4a2713aSLionel Sambuc __andn_u32(unsigned int __X, unsigned int __Y)
51f4a2713aSLionel Sambuc {
52f4a2713aSLionel Sambuc   return ~__X & __Y;
53f4a2713aSLionel Sambuc }
54f4a2713aSLionel Sambuc 
55*0a6a1f1dSLionel Sambuc /* AMD-specified, double-leading-underscore version of BEXTR */
56f4a2713aSLionel Sambuc static __inline__ unsigned int __attribute__((__always_inline__, __nodebug__))
__bextr_u32(unsigned int __X,unsigned int __Y)57f4a2713aSLionel Sambuc __bextr_u32(unsigned int __X, unsigned int __Y)
58f4a2713aSLionel Sambuc {
59f4a2713aSLionel Sambuc   return __builtin_ia32_bextr_u32(__X, __Y);
60f4a2713aSLionel Sambuc }
61f4a2713aSLionel Sambuc 
62*0a6a1f1dSLionel Sambuc /* Intel-specified, single-leading-underscore version of BEXTR */
63*0a6a1f1dSLionel Sambuc static __inline__ unsigned int __attribute__((__always_inline__, __nodebug__))
_bextr_u32(unsigned int __X,unsigned int __Y,unsigned int __Z)64*0a6a1f1dSLionel Sambuc _bextr_u32(unsigned int __X, unsigned int __Y, unsigned int __Z)
65*0a6a1f1dSLionel Sambuc {
66*0a6a1f1dSLionel Sambuc   return __builtin_ia32_bextr_u32 (__X, ((__Y & 0xff) | ((__Z & 0xff) << 8)));
67*0a6a1f1dSLionel Sambuc }
68*0a6a1f1dSLionel Sambuc 
69f4a2713aSLionel Sambuc static __inline__ unsigned int __attribute__((__always_inline__, __nodebug__))
__blsi_u32(unsigned int __X)70f4a2713aSLionel Sambuc __blsi_u32(unsigned int __X)
71f4a2713aSLionel Sambuc {
72f4a2713aSLionel Sambuc   return __X & -__X;
73f4a2713aSLionel Sambuc }
74f4a2713aSLionel Sambuc 
75f4a2713aSLionel Sambuc static __inline__ unsigned int __attribute__((__always_inline__, __nodebug__))
__blsmsk_u32(unsigned int __X)76f4a2713aSLionel Sambuc __blsmsk_u32(unsigned int __X)
77f4a2713aSLionel Sambuc {
78f4a2713aSLionel Sambuc   return __X ^ (__X - 1);
79f4a2713aSLionel Sambuc }
80f4a2713aSLionel Sambuc 
81f4a2713aSLionel Sambuc static __inline__ unsigned int __attribute__((__always_inline__, __nodebug__))
__blsr_u32(unsigned int __X)82f4a2713aSLionel Sambuc __blsr_u32(unsigned int __X)
83f4a2713aSLionel Sambuc {
84f4a2713aSLionel Sambuc   return __X & (__X - 1);
85f4a2713aSLionel Sambuc }
86f4a2713aSLionel Sambuc 
87f4a2713aSLionel Sambuc static __inline__ unsigned int __attribute__((__always_inline__, __nodebug__))
__tzcnt_u32(unsigned int __X)88f4a2713aSLionel Sambuc __tzcnt_u32(unsigned int __X)
89f4a2713aSLionel Sambuc {
90*0a6a1f1dSLionel Sambuc   return __X ? __builtin_ctz(__X) : 32;
91f4a2713aSLionel Sambuc }
92f4a2713aSLionel Sambuc 
93f4a2713aSLionel Sambuc #ifdef __x86_64__
94*0a6a1f1dSLionel Sambuc 
95*0a6a1f1dSLionel Sambuc #define _andn_u64(a, b)   (__andn_u64((a), (b)))
96*0a6a1f1dSLionel Sambuc /* _bextr_u64 != __bextr_u64 */
97*0a6a1f1dSLionel Sambuc #define _blsi_u64(a)      (__blsi_u64((a)))
98*0a6a1f1dSLionel Sambuc #define _blsmsk_u64(a)    (__blsmsk_u64((a)))
99*0a6a1f1dSLionel Sambuc #define _blsr_u64(a)      (__blsr_u64((a)))
100*0a6a1f1dSLionel Sambuc #define _tzcnt_u64(a)     (__tzcnt_u64((a)))
101*0a6a1f1dSLionel Sambuc 
102f4a2713aSLionel Sambuc static __inline__ unsigned long long __attribute__((__always_inline__, __nodebug__))
__andn_u64(unsigned long long __X,unsigned long long __Y)103f4a2713aSLionel Sambuc __andn_u64 (unsigned long long __X, unsigned long long __Y)
104f4a2713aSLionel Sambuc {
105f4a2713aSLionel Sambuc   return ~__X & __Y;
106f4a2713aSLionel Sambuc }
107f4a2713aSLionel Sambuc 
108*0a6a1f1dSLionel Sambuc /* AMD-specified, double-leading-underscore version of BEXTR */
109f4a2713aSLionel Sambuc static __inline__ unsigned long long __attribute__((__always_inline__, __nodebug__))
__bextr_u64(unsigned long long __X,unsigned long long __Y)110f4a2713aSLionel Sambuc __bextr_u64(unsigned long long __X, unsigned long long __Y)
111f4a2713aSLionel Sambuc {
112f4a2713aSLionel Sambuc   return __builtin_ia32_bextr_u64(__X, __Y);
113f4a2713aSLionel Sambuc }
114f4a2713aSLionel Sambuc 
115*0a6a1f1dSLionel Sambuc /* Intel-specified, single-leading-underscore version of BEXTR */
116*0a6a1f1dSLionel Sambuc static __inline__ unsigned long long __attribute__((__always_inline__, __nodebug__))
_bextr_u64(unsigned long long __X,unsigned int __Y,unsigned int __Z)117*0a6a1f1dSLionel Sambuc _bextr_u64(unsigned long long __X, unsigned int __Y, unsigned int __Z)
118*0a6a1f1dSLionel Sambuc {
119*0a6a1f1dSLionel Sambuc   return __builtin_ia32_bextr_u64 (__X, ((__Y & 0xff) | ((__Z & 0xff) << 8)));
120*0a6a1f1dSLionel Sambuc }
121*0a6a1f1dSLionel Sambuc 
122f4a2713aSLionel Sambuc static __inline__ unsigned long long __attribute__((__always_inline__, __nodebug__))
__blsi_u64(unsigned long long __X)123f4a2713aSLionel Sambuc __blsi_u64(unsigned long long __X)
124f4a2713aSLionel Sambuc {
125f4a2713aSLionel Sambuc   return __X & -__X;
126f4a2713aSLionel Sambuc }
127f4a2713aSLionel Sambuc 
128f4a2713aSLionel Sambuc static __inline__ unsigned long long __attribute__((__always_inline__, __nodebug__))
__blsmsk_u64(unsigned long long __X)129f4a2713aSLionel Sambuc __blsmsk_u64(unsigned long long __X)
130f4a2713aSLionel Sambuc {
131f4a2713aSLionel Sambuc   return __X ^ (__X - 1);
132f4a2713aSLionel Sambuc }
133f4a2713aSLionel Sambuc 
134f4a2713aSLionel Sambuc static __inline__ unsigned long long __attribute__((__always_inline__, __nodebug__))
__blsr_u64(unsigned long long __X)135f4a2713aSLionel Sambuc __blsr_u64(unsigned long long __X)
136f4a2713aSLionel Sambuc {
137f4a2713aSLionel Sambuc   return __X & (__X - 1);
138f4a2713aSLionel Sambuc }
139f4a2713aSLionel Sambuc 
140f4a2713aSLionel Sambuc static __inline__ unsigned long long __attribute__((__always_inline__, __nodebug__))
__tzcnt_u64(unsigned long long __X)141f4a2713aSLionel Sambuc __tzcnt_u64(unsigned long long __X)
142f4a2713aSLionel Sambuc {
143*0a6a1f1dSLionel Sambuc   return __X ? __builtin_ctzll(__X) : 64;
144f4a2713aSLionel Sambuc }
145*0a6a1f1dSLionel Sambuc 
146*0a6a1f1dSLionel Sambuc #endif /* __x86_64__ */
147f4a2713aSLionel Sambuc 
148f4a2713aSLionel Sambuc #endif /* __BMIINTRIN_H */
149