1 /*===---- tbmintrin.h - TBM intrinsics -------------------------------------===
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 #ifndef __TBM__
25 #error "TBM instruction set is not enabled"
26 #endif
27 
28 #ifndef __X86INTRIN_H
29 #error "Never use <tbmintrin.h> directly; include <x86intrin.h> instead."
30 #endif
31 
32 #ifndef __TBMINTRIN_H
33 #define __TBMINTRIN_H
34 
35 #define __bextri_u32(a, b) (__builtin_ia32_bextri_u32((a), (b)))
36 
37 static __inline__ unsigned int __attribute__((__always_inline__, __nodebug__))
__blcfill_u32(unsigned int a)38 __blcfill_u32(unsigned int a)
39 {
40   return a & (a + 1);
41 }
42 
43 static __inline__ unsigned int __attribute__((__always_inline__, __nodebug__))
__blci_u32(unsigned int a)44 __blci_u32(unsigned int a)
45 {
46   return a | ~(a + 1);
47 }
48 
49 static __inline__ unsigned int __attribute__((__always_inline__, __nodebug__))
__blcic_u32(unsigned int a)50 __blcic_u32(unsigned int a)
51 {
52   return ~a & (a + 1);
53 }
54 
55 static __inline__ unsigned int __attribute__((__always_inline__, __nodebug__))
__blcmsk_u32(unsigned int a)56 __blcmsk_u32(unsigned int a)
57 {
58   return a ^ (a + 1);
59 }
60 
61 static __inline__ unsigned int __attribute__((__always_inline__, __nodebug__))
__blcs_u32(unsigned int a)62 __blcs_u32(unsigned int a)
63 {
64   return a | (a + 1);
65 }
66 
67 static __inline__ unsigned int __attribute__((__always_inline__, __nodebug__))
__blsfill_u32(unsigned int a)68 __blsfill_u32(unsigned int a)
69 {
70   return a | (a - 1);
71 }
72 
73 static __inline__ unsigned int __attribute__((__always_inline__, __nodebug__))
__blsic_u32(unsigned int a)74 __blsic_u32(unsigned int a)
75 {
76   return ~a | (a - 1);
77 }
78 
79 static __inline__ unsigned int __attribute__((__always_inline__, __nodebug__))
__t1mskc_u32(unsigned int a)80 __t1mskc_u32(unsigned int a)
81 {
82   return ~a | (a + 1);
83 }
84 
85 static __inline__ unsigned int __attribute__((__always_inline__, __nodebug__))
__tzmsk_u32(unsigned int a)86 __tzmsk_u32(unsigned int a)
87 {
88   return ~a & (a - 1);
89 }
90 
91 #ifdef __x86_64__
92 #define __bextri_u64(a, b) (__builtin_ia32_bextri_u64((a), (int)(b)))
93 
94 static __inline__ unsigned long long __attribute__((__always_inline__,
95                                                     __nodebug__))
__blcfill_u64(unsigned long long a)96 __blcfill_u64(unsigned long long a)
97 {
98   return a & (a + 1);
99 }
100 
101 static __inline__ unsigned long long __attribute__((__always_inline__,
102                                                     __nodebug__))
__blci_u64(unsigned long long a)103 __blci_u64(unsigned long long a)
104 {
105   return a | ~(a + 1);
106 }
107 
108 static __inline__ unsigned long long __attribute__((__always_inline__,
109                                                     __nodebug__))
__blcic_u64(unsigned long long a)110 __blcic_u64(unsigned long long a)
111 {
112   return ~a & (a + 1);
113 }
114 
115 static __inline__ unsigned long long __attribute__((__always_inline__,
116                                                     __nodebug__))
__blcmsk_u64(unsigned long long a)117 __blcmsk_u64(unsigned long long a)
118 {
119   return a ^ (a + 1);
120 }
121 
122 static __inline__ unsigned long long __attribute__((__always_inline__,
123                                                     __nodebug__))
__blcs_u64(unsigned long long a)124 __blcs_u64(unsigned long long a)
125 {
126   return a | (a + 1);
127 }
128 
129 static __inline__ unsigned long long __attribute__((__always_inline__,
130                                                     __nodebug__))
__blsfill_u64(unsigned long long a)131 __blsfill_u64(unsigned long long a)
132 {
133   return a | (a - 1);
134 }
135 
136 static __inline__ unsigned long long __attribute__((__always_inline__,
137                                                     __nodebug__))
__blsic_u64(unsigned long long a)138 __blsic_u64(unsigned long long a)
139 {
140   return ~a | (a - 1);
141 }
142 
143 static __inline__ unsigned long long __attribute__((__always_inline__,
144                                                     __nodebug__))
__t1mskc_u64(unsigned long long a)145 __t1mskc_u64(unsigned long long a)
146 {
147   return ~a | (a + 1);
148 }
149 
150 static __inline__ unsigned long long __attribute__((__always_inline__,
151                                                     __nodebug__))
__tzmsk_u64(unsigned long long a)152 __tzmsk_u64(unsigned long long a)
153 {
154   return ~a & (a - 1);
155 }
156 #endif
157 
158 #endif /* __TBMINTRIN_H */
159