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 /// \brief Counts the number of trailing zero bits in the operand.
33 /// \headerfile <x86intrin.h>
36 /// unsigned short _tzcnt_u16(unsigned short a);
39 /// This intrinsic corresponds to the <c> TZCNT </c> instruction.
42 /// An unsigned 16-bit integer whose trailing zeros are to be counted.
43 /// \returns An unsigned 16-bit integer containing the number of trailing zero
44 /// bits in the operand.
45 #define _tzcnt_u16(a) (__tzcnt_u16((a)))
47 /// \brief Performs a bitwise AND of the second operand with the one's
48 /// complement of the first operand.
50 /// \headerfile <x86intrin.h>
53 /// unsigned int _andn_u32(unsigned int a, unsigned int b);
56 /// This intrinsic corresponds to the <c> ANDN </c> instruction.
59 /// An unsigned integer containing one of the operands.
61 /// An unsigned integer containing one of the operands.
62 /// \returns An unsigned integer containing the bitwise AND of the second
63 /// operand with the one's complement of the first operand.
64 #define _andn_u32(a, b) (__andn_u32((a), (b)))
66 /* _bextr_u32 != __bextr_u32 */
67 /// \brief Clears all bits in the source except for the least significant bit
68 /// containing a value of 1 and returns the result.
70 /// \headerfile <x86intrin.h>
73 /// unsigned int _blsi_u32(unsigned int a);
76 /// This intrinsic corresponds to the <c> BLSI </c> instruction.
79 /// An unsigned integer whose bits are to be cleared.
80 /// \returns An unsigned integer containing the result of clearing the bits from
81 /// the source operand.
82 #define _blsi_u32(a) (__blsi_u32((a)))
84 /// \brief Creates a mask whose bits are set to 1, using bit 0 up to and
85 /// including the least siginificant bit that is set to 1 in the source
86 /// operand and returns the result.
88 /// \headerfile <x86intrin.h>
91 /// unsigned int _blsmsk_u32(unsigned int a);
94 /// This intrinsic corresponds to the <c> BLSMSK </c> instruction.
97 /// An unsigned integer used to create the mask.
98 /// \returns An unsigned integer containing the newly created mask.
99 #define _blsmsk_u32(a) (__blsmsk_u32((a)))
101 /// \brief Clears the least siginificant bit that is set to 1 in the source
102 /// operand and returns the result.
104 /// \headerfile <x86intrin.h>
107 /// unsigned int _blsr_u32(unsigned int a);
110 /// This intrinsic corresponds to the <c> BLSR </c> instruction.
113 /// An unsigned integer containing the operand to be cleared.
114 /// \returns An unsigned integer containing the result of clearing the source
116 #define _blsr_u32(a) (__blsr_u32((a)))
118 /// \brief Counts the number of trailing zero bits in the operand.
120 /// \headerfile <x86intrin.h>
123 /// unsigned int _tzcnt_u32(unsigned int a);
126 /// This intrinsic corresponds to the <c> TZCNT </c> instruction.
129 /// An unsigned 32-bit integer whose trailing zeros are to be counted.
130 /// \returns An unsigned 32-bit integer containing the number of trailing zero
131 /// bits in the operand.
132 #define _tzcnt_u32(a) (__tzcnt_u32((a)))
134 /* Define the default attributes for the functions in this file. */
135 #define __DEFAULT_FN_ATTRS __attribute__((__always_inline__, __nodebug__, __target__("bmi")))
137 /* Allow using the tzcnt intrinsics even for non-BMI targets. Since the TZCNT
138 instruction behaves as BSF on non-BMI targets, there is code that expects
139 to use it as a potentially faster version of BSF. */
140 #define __RELAXED_FN_ATTRS __attribute__((__always_inline__, __nodebug__))
142 /// \brief Counts the number of trailing zero bits in the operand.
144 /// \headerfile <x86intrin.h>
146 /// This intrinsic corresponds to the <c> TZCNT </c> instruction.
149 /// An unsigned 16-bit integer whose trailing zeros are to be counted.
150 /// \returns An unsigned 16-bit integer containing the number of trailing zero
151 /// bits in the operand.
152 static __inline__ unsigned short __RELAXED_FN_ATTRS
153 __tzcnt_u16(unsigned short __X)
155 return __X ? __builtin_ctzs(__X) : 16;
158 /// \brief Performs a bitwise AND of the second operand with the one's
159 /// complement of the first operand.
161 /// \headerfile <x86intrin.h>
163 /// This intrinsic corresponds to the <c> ANDN </c> instruction.
166 /// An unsigned integer containing one of the operands.
168 /// An unsigned integer containing one of the operands.
169 /// \returns An unsigned integer containing the bitwise AND of the second
170 /// operand with the one's complement of the first operand.
171 static __inline__ unsigned int __DEFAULT_FN_ATTRS
172 __andn_u32(unsigned int __X, unsigned int __Y)
177 /* AMD-specified, double-leading-underscore version of BEXTR */
178 /// \brief Extracts the specified bits from the first operand and returns them
179 /// in the least significant bits of the result.
181 /// \headerfile <x86intrin.h>
183 /// This intrinsic corresponds to the <c> BEXTR </c> instruction.
186 /// An unsigned integer whose bits are to be extracted.
188 /// An unsigned integer used to specify which bits are extracted. Bits [7:0]
189 /// specify the index of the least significant bit. Bits [15:8] specify the
190 /// number of bits to be extracted.
191 /// \returns An unsigned integer whose least significant bits contain the
193 static __inline__ unsigned int __DEFAULT_FN_ATTRS
194 __bextr_u32(unsigned int __X, unsigned int __Y)
196 return __builtin_ia32_bextr_u32(__X, __Y);
199 /* Intel-specified, single-leading-underscore version of BEXTR */
200 /// \brief Extracts the specified bits from the first operand and returns them
201 /// in the least significant bits of the result.
203 /// \headerfile <x86intrin.h>
205 /// This intrinsic corresponds to the <c> BEXTR </c> instruction.
208 /// An unsigned integer whose bits are to be extracted.
210 /// An unsigned integer used to specify the index of the least significant
211 /// bit for the bits to be extracted. Bits [7:0] specify the index.
213 /// An unsigned integer used to specify the number of bits to be extracted.
214 /// Bits [7:0] specify the number of bits.
215 /// \returns An unsigned integer whose least significant bits contain the
217 static __inline__ unsigned int __DEFAULT_FN_ATTRS
218 _bextr_u32(unsigned int __X, unsigned int __Y, unsigned int __Z)
220 return __builtin_ia32_bextr_u32 (__X, ((__Y & 0xff) | ((__Z & 0xff) << 8)));
223 /// \brief Clears all bits in the source except for the least significant bit
224 /// containing a value of 1 and returns the result.
226 /// \headerfile <x86intrin.h>
228 /// This intrinsic corresponds to the <c> BLSI </c> instruction.
231 /// An unsigned integer whose bits are to be cleared.
232 /// \returns An unsigned integer containing the result of clearing the bits from
233 /// the source operand.
234 static __inline__ unsigned int __DEFAULT_FN_ATTRS
235 __blsi_u32(unsigned int __X)
240 /// \brief Creates a mask whose bits are set to 1, using bit 0 up to and
241 /// including the least siginificant bit that is set to 1 in the source
242 /// operand and returns the result.
244 /// \headerfile <x86intrin.h>
246 /// This intrinsic corresponds to the <c> BLSMSK </c> instruction.
249 /// An unsigned integer used to create the mask.
250 /// \returns An unsigned integer containing the newly created mask.
251 static __inline__ unsigned int __DEFAULT_FN_ATTRS
252 __blsmsk_u32(unsigned int __X)
254 return __X ^ (__X - 1);
257 /// \brief Clears the least siginificant bit that is set to 1 in the source
258 /// operand and returns the result.
260 /// \headerfile <x86intrin.h>
262 /// This intrinsic corresponds to the <c> BLSR </c> instruction.
265 /// An unsigned integer containing the operand to be cleared.
266 /// \returns An unsigned integer containing the result of clearing the source
268 static __inline__ unsigned int __DEFAULT_FN_ATTRS
269 __blsr_u32(unsigned int __X)
271 return __X & (__X - 1);
274 /// \brief Counts the number of trailing zero bits in the operand.
276 /// \headerfile <x86intrin.h>
278 /// This intrinsic corresponds to the <c> TZCNT </c> instruction.
281 /// An unsigned 32-bit integer whose trailing zeros are to be counted.
282 /// \returns An unsigned 32-bit integer containing the number of trailing zero
283 /// bits in the operand.
284 static __inline__ unsigned int __RELAXED_FN_ATTRS
285 __tzcnt_u32(unsigned int __X)
287 return __X ? __builtin_ctz(__X) : 32;
290 /// \brief Counts the number of trailing zero bits in the operand.
292 /// \headerfile <x86intrin.h>
294 /// This intrinsic corresponds to the <c> TZCNT </c> instruction.
297 /// An unsigned 32-bit integer whose trailing zeros are to be counted.
298 /// \returns An 32-bit integer containing the number of trailing zero bits in
300 static __inline__ int __RELAXED_FN_ATTRS
301 _mm_tzcnt_32(unsigned int __X)
303 return __X ? __builtin_ctz(__X) : 32;
308 /// \brief Performs a bitwise AND of the second operand with the one's
309 /// complement of the first operand.
311 /// \headerfile <x86intrin.h>
314 /// unsigned long long _andn_u64 (unsigned long long a, unsigned long long b);
317 /// This intrinsic corresponds to the <c> ANDN </c> instruction.
320 /// An unsigned 64-bit integer containing one of the operands.
322 /// An unsigned 64-bit integer containing one of the operands.
323 /// \returns An unsigned 64-bit integer containing the bitwise AND of the second
324 /// operand with the one's complement of the first operand.
325 #define _andn_u64(a, b) (__andn_u64((a), (b)))
327 /* _bextr_u64 != __bextr_u64 */
328 /// \brief Clears all bits in the source except for the least significant bit
329 /// containing a value of 1 and returns the result.
331 /// \headerfile <x86intrin.h>
334 /// unsigned long long _blsi_u64(unsigned long long a);
337 /// This intrinsic corresponds to the <c> BLSI </c> instruction.
340 /// An unsigned 64-bit integer whose bits are to be cleared.
341 /// \returns An unsigned 64-bit integer containing the result of clearing the
342 /// bits from the source operand.
343 #define _blsi_u64(a) (__blsi_u64((a)))
345 /// \brief Creates a mask whose bits are set to 1, using bit 0 up to and
346 /// including the least siginificant bit that is set to 1 in the source
347 /// operand and returns the result.
349 /// \headerfile <x86intrin.h>
352 /// unsigned long long _blsmsk_u64(unsigned long long a);
355 /// This intrinsic corresponds to the <c> BLSMSK </c> instruction.
358 /// An unsigned 64-bit integer used to create the mask.
359 /// \returns A unsigned 64-bit integer containing the newly created mask.
360 #define _blsmsk_u64(a) (__blsmsk_u64((a)))
362 /// \brief Clears the least siginificant bit that is set to 1 in the source
363 /// operand and returns the result.
365 /// \headerfile <x86intrin.h>
368 /// unsigned long long _blsr_u64(unsigned long long a);
371 /// This intrinsic corresponds to the <c> BLSR </c> instruction.
374 /// An unsigned 64-bit integer containing the operand to be cleared.
375 /// \returns An unsigned 64-bit integer containing the result of clearing the
377 #define _blsr_u64(a) (__blsr_u64((a)))
379 /// \brief Counts the number of trailing zero bits in the operand.
381 /// \headerfile <x86intrin.h>
384 /// unsigned long long _tzcnt_u64(unsigned long long a);
387 /// This intrinsic corresponds to the <c> TZCNT </c> instruction.
390 /// An unsigned 64-bit integer whose trailing zeros are to be counted.
391 /// \returns An unsigned 64-bit integer containing the number of trailing zero
392 /// bits in the operand.
393 #define _tzcnt_u64(a) (__tzcnt_u64((a)))
395 /// \brief Performs a bitwise AND of the second operand with the one's
396 /// complement of the first operand.
398 /// \headerfile <x86intrin.h>
400 /// This intrinsic corresponds to the <c> ANDN </c> instruction.
403 /// An unsigned 64-bit integer containing one of the operands.
405 /// An unsigned 64-bit integer containing one of the operands.
406 /// \returns An unsigned 64-bit integer containing the bitwise AND of the second
407 /// operand with the one's complement of the first operand.
408 static __inline__ unsigned long long __DEFAULT_FN_ATTRS
409 __andn_u64 (unsigned long long __X, unsigned long long __Y)
414 /* AMD-specified, double-leading-underscore version of BEXTR */
415 /// \brief Extracts the specified bits from the first operand and returns them
416 /// in the least significant bits of the result.
418 /// \headerfile <x86intrin.h>
420 /// This intrinsic corresponds to the <c> BEXTR </c> instruction.
423 /// An unsigned 64-bit integer whose bits are to be extracted.
425 /// An unsigned 64-bit integer used to specify which bits are extracted. Bits
426 /// [7:0] specify the index of the least significant bit. Bits [15:8] specify
427 /// the number of bits to be extracted.
428 /// \returns An unsigned 64-bit integer whose least significant bits contain the
430 static __inline__ unsigned long long __DEFAULT_FN_ATTRS
431 __bextr_u64(unsigned long long __X, unsigned long long __Y)
433 return __builtin_ia32_bextr_u64(__X, __Y);
436 /* Intel-specified, single-leading-underscore version of BEXTR */
437 /// \brief Extracts the specified bits from the first operand and returns them
438 /// in the least significant bits of the result.
440 /// \headerfile <x86intrin.h>
442 /// This intrinsic corresponds to the <c> BEXTR </c> instruction.
445 /// An unsigned 64-bit integer whose bits are to be extracted.
447 /// An unsigned integer used to specify the index of the least significant
448 /// bit for the bits to be extracted. Bits [7:0] specify the index.
450 /// An unsigned integer used to specify the number of bits to be extracted.
451 /// Bits [7:0] specify the number of bits.
452 /// \returns An unsigned 64-bit integer whose least significant bits contain the
454 static __inline__ unsigned long long __DEFAULT_FN_ATTRS
455 _bextr_u64(unsigned long long __X, unsigned int __Y, unsigned int __Z)
457 return __builtin_ia32_bextr_u64 (__X, ((__Y & 0xff) | ((__Z & 0xff) << 8)));
460 /// \brief Clears all bits in the source except for the least significant bit
461 /// containing a value of 1 and returns the result.
463 /// \headerfile <x86intrin.h>
465 /// This intrinsic corresponds to the <c> BLSI </c> instruction.
468 /// An unsigned 64-bit integer whose bits are to be cleared.
469 /// \returns An unsigned 64-bit integer containing the result of clearing the
470 /// bits from the source operand.
471 static __inline__ unsigned long long __DEFAULT_FN_ATTRS
472 __blsi_u64(unsigned long long __X)
477 /// \brief Creates a mask whose bits are set to 1, using bit 0 up to and
478 /// including the least siginificant bit that is set to 1 in the source
479 /// operand and returns the result.
481 /// \headerfile <x86intrin.h>
483 /// This intrinsic corresponds to the <c> BLSMSK </c> instruction.
486 /// An unsigned 64-bit integer used to create the mask.
487 /// \returns A unsigned 64-bit integer containing the newly created mask.
488 static __inline__ unsigned long long __DEFAULT_FN_ATTRS
489 __blsmsk_u64(unsigned long long __X)
491 return __X ^ (__X - 1);
494 /// \brief Clears the least siginificant bit that is set to 1 in the source
495 /// operand and returns the result.
497 /// \headerfile <x86intrin.h>
499 /// This intrinsic corresponds to the <c> BLSR </c> instruction.
502 /// An unsigned 64-bit integer containing the operand to be cleared.
503 /// \returns An unsigned 64-bit integer containing the result of clearing the
505 static __inline__ unsigned long long __DEFAULT_FN_ATTRS
506 __blsr_u64(unsigned long long __X)
508 return __X & (__X - 1);
511 /// \brief Counts the number of trailing zero bits in the operand.
513 /// \headerfile <x86intrin.h>
515 /// This intrinsic corresponds to the <c> TZCNT </c> instruction.
518 /// An unsigned 64-bit integer whose trailing zeros are to be counted.
519 /// \returns An unsigned 64-bit integer containing the number of trailing zero
520 /// bits in the operand.
521 static __inline__ unsigned long long __RELAXED_FN_ATTRS
522 __tzcnt_u64(unsigned long long __X)
524 return __X ? __builtin_ctzll(__X) : 64;
527 /// \brief Counts the number of trailing zero bits in the operand.
529 /// \headerfile <x86intrin.h>
531 /// This intrinsic corresponds to the <c> TZCNT </c> instruction.
534 /// An unsigned 64-bit integer whose trailing zeros are to be counted.
535 /// \returns An 64-bit integer containing the number of trailing zero bits in
537 static __inline__ long long __RELAXED_FN_ATTRS
538 _mm_tzcnt_64(unsigned long long __X)
540 return __X ? __builtin_ctzll(__X) : 64;
543 #endif /* __x86_64__ */
545 #undef __DEFAULT_FN_ATTRS
546 #undef __RELAXED_FN_ATTRS
548 #endif /* __BMIINTRIN_H */