1 /*===---- bmiintrin.h - BMI 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 *===-----------------------------------------------------------------------===
24 #if !defined __X86INTRIN_H && !defined __IMMINTRIN_H
25 #error "Never use <bmiintrin.h> directly; include <x86intrin.h> instead."
31 #define _tzcnt_u16(a) (__tzcnt_u16((a)))
33 #define _andn_u32(a, b) (__andn_u32((a), (b)))
35 /* _bextr_u32 != __bextr_u32 */
36 #define _blsi_u32(a) (__blsi_u32((a)))
38 #define _blsmsk_u32(a) (__blsmsk_u32((a)))
40 #define _blsr_u32(a) (__blsr_u32((a)))
42 #define _tzcnt_u32(a) (__tzcnt_u32((a)))
44 /* Define the default attributes for the functions in this file. */
45 #define __DEFAULT_FN_ATTRS __attribute__((__always_inline__, __nodebug__, __target__("bmi")))
47 /* Allow using the tzcnt intrinsics even for non-BMI targets. Since the TZCNT
48 instruction behaves as BSF on non-BMI targets, there is code that expects
49 to use it as a potentially faster version of BSF. */
50 #define __RELAXED_FN_ATTRS __attribute__((__always_inline__, __nodebug__))
52 /// Counts the number of trailing zero bits in the operand.
54 /// \headerfile <x86intrin.h>
56 /// This intrinsic corresponds to the <c> TZCNT </c> instruction.
59 /// An unsigned 16-bit integer whose trailing zeros are to be counted.
60 /// \returns An unsigned 16-bit integer containing the number of trailing zero
61 /// bits in the operand.
62 static __inline__ unsigned short __RELAXED_FN_ATTRS
63 __tzcnt_u16(unsigned short __X)
65 return __X ? __builtin_ctzs(__X) : 16;
68 /// Performs a bitwise AND of the second operand with the one's
69 /// complement of the first operand.
71 /// \headerfile <x86intrin.h>
73 /// This intrinsic corresponds to the <c> ANDN </c> instruction.
76 /// An unsigned integer containing one of the operands.
78 /// An unsigned integer containing one of the operands.
79 /// \returns An unsigned integer containing the bitwise AND of the second
80 /// operand with the one's complement of the first operand.
81 static __inline__ unsigned int __DEFAULT_FN_ATTRS
82 __andn_u32(unsigned int __X, unsigned int __Y)
87 /* AMD-specified, double-leading-underscore version of BEXTR */
88 /// Extracts the specified bits from the first operand and returns them
89 /// in the least significant bits of the result.
91 /// \headerfile <x86intrin.h>
93 /// This intrinsic corresponds to the <c> BEXTR </c> instruction.
96 /// An unsigned integer whose bits are to be extracted.
98 /// An unsigned integer used to specify which bits are extracted. Bits [7:0]
99 /// specify the index of the least significant bit. Bits [15:8] specify the
100 /// number of bits to be extracted.
101 /// \returns An unsigned integer whose least significant bits contain the
104 static __inline__ unsigned int __DEFAULT_FN_ATTRS
105 __bextr_u32(unsigned int __X, unsigned int __Y)
107 return __builtin_ia32_bextr_u32(__X, __Y);
110 /* Intel-specified, single-leading-underscore version of BEXTR */
111 /// Extracts the specified bits from the first operand and returns them
112 /// in the least significant bits of the result.
114 /// \headerfile <x86intrin.h>
116 /// This intrinsic corresponds to the <c> BEXTR </c> instruction.
119 /// An unsigned integer whose bits are to be extracted.
121 /// An unsigned integer used to specify the index of the least significant
122 /// bit for the bits to be extracted. Bits [7:0] specify the index.
124 /// An unsigned integer used to specify the number of bits to be extracted.
125 /// Bits [7:0] specify the number of bits.
126 /// \returns An unsigned integer whose least significant bits contain the
129 static __inline__ unsigned int __DEFAULT_FN_ATTRS
130 _bextr_u32(unsigned int __X, unsigned int __Y, unsigned int __Z)
132 return __builtin_ia32_bextr_u32 (__X, ((__Y & 0xff) | ((__Z & 0xff) << 8)));
135 /// Clears all bits in the source except for the least significant bit
136 /// containing a value of 1 and returns the result.
138 /// \headerfile <x86intrin.h>
140 /// This intrinsic corresponds to the <c> BLSI </c> instruction.
143 /// An unsigned integer whose bits are to be cleared.
144 /// \returns An unsigned integer containing the result of clearing the bits from
145 /// the source operand.
146 static __inline__ unsigned int __DEFAULT_FN_ATTRS
147 __blsi_u32(unsigned int __X)
152 /// Creates a mask whose bits are set to 1, using bit 0 up to and
153 /// including the least significant bit that is set to 1 in the source
154 /// operand and returns the result.
156 /// \headerfile <x86intrin.h>
158 /// This intrinsic corresponds to the <c> BLSMSK </c> instruction.
161 /// An unsigned integer used to create the mask.
162 /// \returns An unsigned integer containing the newly created mask.
163 static __inline__ unsigned int __DEFAULT_FN_ATTRS
164 __blsmsk_u32(unsigned int __X)
166 return __X ^ (__X - 1);
169 /// Clears the least significant bit that is set to 1 in the source
170 /// operand and returns the result.
172 /// \headerfile <x86intrin.h>
174 /// This intrinsic corresponds to the <c> BLSR </c> instruction.
177 /// An unsigned integer containing the operand to be cleared.
178 /// \returns An unsigned integer containing the result of clearing the source
180 static __inline__ unsigned int __DEFAULT_FN_ATTRS
181 __blsr_u32(unsigned int __X)
183 return __X & (__X - 1);
186 /// Counts the number of trailing zero bits in the operand.
188 /// \headerfile <x86intrin.h>
190 /// This intrinsic corresponds to the <c> TZCNT </c> instruction.
193 /// An unsigned 32-bit integer whose trailing zeros are to be counted.
194 /// \returns An unsigned 32-bit integer containing the number of trailing zero
195 /// bits in the operand.
196 static __inline__ unsigned int __RELAXED_FN_ATTRS
197 __tzcnt_u32(unsigned int __X)
199 return __X ? __builtin_ctz(__X) : 32;
202 /// Counts the number of trailing zero bits in the operand.
204 /// \headerfile <x86intrin.h>
206 /// This intrinsic corresponds to the <c> TZCNT </c> instruction.
209 /// An unsigned 32-bit integer whose trailing zeros are to be counted.
210 /// \returns An 32-bit integer containing the number of trailing zero bits in
212 static __inline__ int __RELAXED_FN_ATTRS
213 _mm_tzcnt_32(unsigned int __X)
215 return __X ? __builtin_ctz(__X) : 32;
220 #define _andn_u64(a, b) (__andn_u64((a), (b)))
222 /* _bextr_u64 != __bextr_u64 */
223 #define _blsi_u64(a) (__blsi_u64((a)))
225 #define _blsmsk_u64(a) (__blsmsk_u64((a)))
227 #define _blsr_u64(a) (__blsr_u64((a)))
229 #define _tzcnt_u64(a) (__tzcnt_u64((a)))
231 /// Performs a bitwise AND of the second operand with the one's
232 /// complement of the first operand.
234 /// \headerfile <x86intrin.h>
236 /// This intrinsic corresponds to the <c> ANDN </c> instruction.
239 /// An unsigned 64-bit integer containing one of the operands.
241 /// An unsigned 64-bit integer containing one of the operands.
242 /// \returns An unsigned 64-bit integer containing the bitwise AND of the second
243 /// operand with the one's complement of the first operand.
244 static __inline__ unsigned long long __DEFAULT_FN_ATTRS
245 __andn_u64 (unsigned long long __X, unsigned long long __Y)
250 /* AMD-specified, double-leading-underscore version of BEXTR */
251 /// Extracts the specified bits from the first operand and returns them
252 /// in the least significant bits of the result.
254 /// \headerfile <x86intrin.h>
256 /// This intrinsic corresponds to the <c> BEXTR </c> instruction.
259 /// An unsigned 64-bit integer whose bits are to be extracted.
261 /// An unsigned 64-bit integer used to specify which bits are extracted. Bits
262 /// [7:0] specify the index of the least significant bit. Bits [15:8] specify
263 /// the number of bits to be extracted.
264 /// \returns An unsigned 64-bit integer whose least significant bits contain the
267 static __inline__ unsigned long long __DEFAULT_FN_ATTRS
268 __bextr_u64(unsigned long long __X, unsigned long long __Y)
270 return __builtin_ia32_bextr_u64(__X, __Y);
273 /* Intel-specified, single-leading-underscore version of BEXTR */
274 /// Extracts the specified bits from the first operand and returns them
275 /// in the least significant bits of the result.
277 /// \headerfile <x86intrin.h>
279 /// This intrinsic corresponds to the <c> BEXTR </c> instruction.
282 /// An unsigned 64-bit integer whose bits are to be extracted.
284 /// An unsigned integer used to specify the index of the least significant
285 /// bit for the bits to be extracted. Bits [7:0] specify the index.
287 /// An unsigned integer used to specify the number of bits to be extracted.
288 /// Bits [7:0] specify the number of bits.
289 /// \returns An unsigned 64-bit integer whose least significant bits contain the
292 static __inline__ unsigned long long __DEFAULT_FN_ATTRS
293 _bextr_u64(unsigned long long __X, unsigned int __Y, unsigned int __Z)
295 return __builtin_ia32_bextr_u64 (__X, ((__Y & 0xff) | ((__Z & 0xff) << 8)));
298 /// Clears all bits in the source except for the least significant bit
299 /// containing a value of 1 and returns the result.
301 /// \headerfile <x86intrin.h>
303 /// This intrinsic corresponds to the <c> BLSI </c> instruction.
306 /// An unsigned 64-bit integer whose bits are to be cleared.
307 /// \returns An unsigned 64-bit integer containing the result of clearing the
308 /// bits from the source operand.
309 static __inline__ unsigned long long __DEFAULT_FN_ATTRS
310 __blsi_u64(unsigned long long __X)
315 /// Creates a mask whose bits are set to 1, using bit 0 up to and
316 /// including the least significant bit that is set to 1 in the source
317 /// operand and returns the result.
319 /// \headerfile <x86intrin.h>
321 /// This intrinsic corresponds to the <c> BLSMSK </c> instruction.
324 /// An unsigned 64-bit integer used to create the mask.
325 /// \returns An unsigned 64-bit integer containing the newly created mask.
326 static __inline__ unsigned long long __DEFAULT_FN_ATTRS
327 __blsmsk_u64(unsigned long long __X)
329 return __X ^ (__X - 1);
332 /// Clears the least significant bit that is set to 1 in the source
333 /// operand and returns the result.
335 /// \headerfile <x86intrin.h>
337 /// This intrinsic corresponds to the <c> BLSR </c> instruction.
340 /// An unsigned 64-bit integer containing the operand to be cleared.
341 /// \returns An unsigned 64-bit integer containing the result of clearing the
343 static __inline__ unsigned long long __DEFAULT_FN_ATTRS
344 __blsr_u64(unsigned long long __X)
346 return __X & (__X - 1);
349 /// Counts the number of trailing zero bits in the operand.
351 /// \headerfile <x86intrin.h>
353 /// This intrinsic corresponds to the <c> TZCNT </c> instruction.
356 /// An unsigned 64-bit integer whose trailing zeros are to be counted.
357 /// \returns An unsigned 64-bit integer containing the number of trailing zero
358 /// bits in the operand.
359 static __inline__ unsigned long long __RELAXED_FN_ATTRS
360 __tzcnt_u64(unsigned long long __X)
362 return __X ? __builtin_ctzll(__X) : 64;
365 /// Counts the number of trailing zero bits in the operand.
367 /// \headerfile <x86intrin.h>
369 /// This intrinsic corresponds to the <c> TZCNT </c> instruction.
372 /// An unsigned 64-bit integer whose trailing zeros are to be counted.
373 /// \returns An 64-bit integer containing the number of trailing zero bits in
375 static __inline__ long long __RELAXED_FN_ATTRS
376 _mm_tzcnt_64(unsigned long long __X)
378 return __X ? __builtin_ctzll(__X) : 64;
381 #endif /* __x86_64__ */
383 #undef __DEFAULT_FN_ATTRS
384 #undef __RELAXED_FN_ATTRS
386 #endif /* __BMIINTRIN_H */