1 /*===---- bmiintrin.h - BMI intrinsics -------------------------------------===
3 * Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 * See https://llvm.org/LICENSE.txt for license information.
5 * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 *===-----------------------------------------------------------------------===
10 #if !defined __X86INTRIN_H && !defined __IMMINTRIN_H
11 #error "Never use <bmiintrin.h> directly; include <x86intrin.h> instead."
17 #define _tzcnt_u16(a) (__tzcnt_u16((a)))
19 #define _andn_u32(a, b) (__andn_u32((a), (b)))
21 /* _bextr_u32 != __bextr_u32 */
22 #define _blsi_u32(a) (__blsi_u32((a)))
24 #define _blsmsk_u32(a) (__blsmsk_u32((a)))
26 #define _blsr_u32(a) (__blsr_u32((a)))
28 #define _tzcnt_u32(a) (__tzcnt_u32((a)))
30 /* Define the default attributes for the functions in this file. */
31 #define __DEFAULT_FN_ATTRS __attribute__((__always_inline__, __nodebug__, __target__("bmi")))
33 /* Allow using the tzcnt intrinsics even for non-BMI targets. Since the TZCNT
34 instruction behaves as BSF on non-BMI targets, there is code that expects
35 to use it as a potentially faster version of BSF. */
36 #define __RELAXED_FN_ATTRS __attribute__((__always_inline__, __nodebug__))
38 /// Counts the number of trailing zero bits in the operand.
40 /// \headerfile <x86intrin.h>
42 /// This intrinsic corresponds to the <c> TZCNT </c> instruction.
45 /// An unsigned 16-bit integer whose trailing zeros are to be counted.
46 /// \returns An unsigned 16-bit integer containing the number of trailing zero
47 /// bits in the operand.
48 static __inline__ unsigned short __RELAXED_FN_ATTRS
49 __tzcnt_u16(unsigned short __X)
51 return __builtin_ia32_tzcnt_u16(__X);
54 /// Performs a bitwise AND of the second operand with the one's
55 /// complement of the first operand.
57 /// \headerfile <x86intrin.h>
59 /// This intrinsic corresponds to the <c> ANDN </c> instruction.
62 /// An unsigned integer containing one of the operands.
64 /// An unsigned integer containing one of the operands.
65 /// \returns An unsigned integer containing the bitwise AND of the second
66 /// operand with the one's complement of the first operand.
67 static __inline__ unsigned int __DEFAULT_FN_ATTRS
68 __andn_u32(unsigned int __X, unsigned int __Y)
73 /* AMD-specified, double-leading-underscore version of BEXTR */
74 /// Extracts the specified bits from the first operand and returns them
75 /// in the least significant bits of the result.
77 /// \headerfile <x86intrin.h>
79 /// This intrinsic corresponds to the <c> BEXTR </c> instruction.
82 /// An unsigned integer whose bits are to be extracted.
84 /// An unsigned integer used to specify which bits are extracted. Bits [7:0]
85 /// specify the index of the least significant bit. Bits [15:8] specify the
86 /// number of bits to be extracted.
87 /// \returns An unsigned integer whose least significant bits contain the
90 static __inline__ unsigned int __DEFAULT_FN_ATTRS
91 __bextr_u32(unsigned int __X, unsigned int __Y)
93 return __builtin_ia32_bextr_u32(__X, __Y);
96 /* Intel-specified, single-leading-underscore version of BEXTR */
97 /// Extracts the specified bits from the first operand and returns them
98 /// in the least significant bits of the result.
100 /// \headerfile <x86intrin.h>
102 /// This intrinsic corresponds to the <c> BEXTR </c> instruction.
105 /// An unsigned integer whose bits are to be extracted.
107 /// An unsigned integer used to specify the index of the least significant
108 /// bit for the bits to be extracted. Bits [7:0] specify the index.
110 /// An unsigned integer used to specify the number of bits to be extracted.
111 /// Bits [7:0] specify the number of bits.
112 /// \returns An unsigned integer whose least significant bits contain the
115 static __inline__ unsigned int __DEFAULT_FN_ATTRS
116 _bextr_u32(unsigned int __X, unsigned int __Y, unsigned int __Z)
118 return __builtin_ia32_bextr_u32 (__X, ((__Y & 0xff) | ((__Z & 0xff) << 8)));
121 /// Clears all bits in the source except for the least significant bit
122 /// containing a value of 1 and returns the result.
124 /// \headerfile <x86intrin.h>
126 /// This intrinsic corresponds to the <c> BLSI </c> instruction.
129 /// An unsigned integer whose bits are to be cleared.
130 /// \returns An unsigned integer containing the result of clearing the bits from
131 /// the source operand.
132 static __inline__ unsigned int __DEFAULT_FN_ATTRS
133 __blsi_u32(unsigned int __X)
138 /// Creates a mask whose bits are set to 1, using bit 0 up to and
139 /// including the least significant bit that is set to 1 in the source
140 /// operand and returns the result.
142 /// \headerfile <x86intrin.h>
144 /// This intrinsic corresponds to the <c> BLSMSK </c> instruction.
147 /// An unsigned integer used to create the mask.
148 /// \returns An unsigned integer containing the newly created mask.
149 static __inline__ unsigned int __DEFAULT_FN_ATTRS
150 __blsmsk_u32(unsigned int __X)
152 return __X ^ (__X - 1);
155 /// Clears the least significant bit that is set to 1 in the source
156 /// operand and returns the result.
158 /// \headerfile <x86intrin.h>
160 /// This intrinsic corresponds to the <c> BLSR </c> instruction.
163 /// An unsigned integer containing the operand to be cleared.
164 /// \returns An unsigned integer containing the result of clearing the source
166 static __inline__ unsigned int __DEFAULT_FN_ATTRS
167 __blsr_u32(unsigned int __X)
169 return __X & (__X - 1);
172 /// Counts the number of trailing zero bits in the operand.
174 /// \headerfile <x86intrin.h>
176 /// This intrinsic corresponds to the <c> TZCNT </c> instruction.
179 /// An unsigned 32-bit integer whose trailing zeros are to be counted.
180 /// \returns An unsigned 32-bit integer containing the number of trailing zero
181 /// bits in the operand.
182 static __inline__ unsigned int __RELAXED_FN_ATTRS
183 __tzcnt_u32(unsigned int __X)
185 return __builtin_ia32_tzcnt_u32(__X);
188 /// Counts the number of trailing zero bits in the operand.
190 /// \headerfile <x86intrin.h>
192 /// This intrinsic corresponds to the <c> TZCNT </c> instruction.
195 /// An unsigned 32-bit integer whose trailing zeros are to be counted.
196 /// \returns An 32-bit integer containing the number of trailing zero bits in
198 static __inline__ int __RELAXED_FN_ATTRS
199 _mm_tzcnt_32(unsigned int __X)
201 return __builtin_ia32_tzcnt_u32(__X);
206 #define _andn_u64(a, b) (__andn_u64((a), (b)))
208 /* _bextr_u64 != __bextr_u64 */
209 #define _blsi_u64(a) (__blsi_u64((a)))
211 #define _blsmsk_u64(a) (__blsmsk_u64((a)))
213 #define _blsr_u64(a) (__blsr_u64((a)))
215 #define _tzcnt_u64(a) (__tzcnt_u64((a)))
217 /// Performs a bitwise AND of the second operand with the one's
218 /// complement of the first operand.
220 /// \headerfile <x86intrin.h>
222 /// This intrinsic corresponds to the <c> ANDN </c> instruction.
225 /// An unsigned 64-bit integer containing one of the operands.
227 /// An unsigned 64-bit integer containing one of the operands.
228 /// \returns An unsigned 64-bit integer containing the bitwise AND of the second
229 /// operand with the one's complement of the first operand.
230 static __inline__ unsigned long long __DEFAULT_FN_ATTRS
231 __andn_u64 (unsigned long long __X, unsigned long long __Y)
236 /* AMD-specified, double-leading-underscore version of BEXTR */
237 /// Extracts the specified bits from the first operand and returns them
238 /// in the least significant bits of the result.
240 /// \headerfile <x86intrin.h>
242 /// This intrinsic corresponds to the <c> BEXTR </c> instruction.
245 /// An unsigned 64-bit integer whose bits are to be extracted.
247 /// An unsigned 64-bit integer used to specify which bits are extracted. Bits
248 /// [7:0] specify the index of the least significant bit. Bits [15:8] specify
249 /// the number of bits to be extracted.
250 /// \returns An unsigned 64-bit integer whose least significant bits contain the
253 static __inline__ unsigned long long __DEFAULT_FN_ATTRS
254 __bextr_u64(unsigned long long __X, unsigned long long __Y)
256 return __builtin_ia32_bextr_u64(__X, __Y);
259 /* Intel-specified, single-leading-underscore version of BEXTR */
260 /// Extracts the specified bits from the first operand and returns them
261 /// in the least significant bits of the result.
263 /// \headerfile <x86intrin.h>
265 /// This intrinsic corresponds to the <c> BEXTR </c> instruction.
268 /// An unsigned 64-bit integer whose bits are to be extracted.
270 /// An unsigned integer used to specify the index of the least significant
271 /// bit for the bits to be extracted. Bits [7:0] specify the index.
273 /// An unsigned integer used to specify the number of bits to be extracted.
274 /// Bits [7:0] specify the number of bits.
275 /// \returns An unsigned 64-bit integer whose least significant bits contain the
278 static __inline__ unsigned long long __DEFAULT_FN_ATTRS
279 _bextr_u64(unsigned long long __X, unsigned int __Y, unsigned int __Z)
281 return __builtin_ia32_bextr_u64 (__X, ((__Y & 0xff) | ((__Z & 0xff) << 8)));
284 /// Clears all bits in the source except for the least significant bit
285 /// containing a value of 1 and returns the result.
287 /// \headerfile <x86intrin.h>
289 /// This intrinsic corresponds to the <c> BLSI </c> instruction.
292 /// An unsigned 64-bit integer whose bits are to be cleared.
293 /// \returns An unsigned 64-bit integer containing the result of clearing the
294 /// bits from the source operand.
295 static __inline__ unsigned long long __DEFAULT_FN_ATTRS
296 __blsi_u64(unsigned long long __X)
301 /// Creates a mask whose bits are set to 1, using bit 0 up to and
302 /// including the least significant bit that is set to 1 in the source
303 /// operand and returns the result.
305 /// \headerfile <x86intrin.h>
307 /// This intrinsic corresponds to the <c> BLSMSK </c> instruction.
310 /// An unsigned 64-bit integer used to create the mask.
311 /// \returns An unsigned 64-bit integer containing the newly created mask.
312 static __inline__ unsigned long long __DEFAULT_FN_ATTRS
313 __blsmsk_u64(unsigned long long __X)
315 return __X ^ (__X - 1);
318 /// Clears the least significant bit that is set to 1 in the source
319 /// operand and returns the result.
321 /// \headerfile <x86intrin.h>
323 /// This intrinsic corresponds to the <c> BLSR </c> instruction.
326 /// An unsigned 64-bit integer containing the operand to be cleared.
327 /// \returns An unsigned 64-bit integer containing the result of clearing the
329 static __inline__ unsigned long long __DEFAULT_FN_ATTRS
330 __blsr_u64(unsigned long long __X)
332 return __X & (__X - 1);
335 /// Counts the number of trailing zero bits in the operand.
337 /// \headerfile <x86intrin.h>
339 /// This intrinsic corresponds to the <c> TZCNT </c> instruction.
342 /// An unsigned 64-bit integer whose trailing zeros are to be counted.
343 /// \returns An unsigned 64-bit integer containing the number of trailing zero
344 /// bits in the operand.
345 static __inline__ unsigned long long __RELAXED_FN_ATTRS
346 __tzcnt_u64(unsigned long long __X)
348 return __builtin_ia32_tzcnt_u64(__X);
351 /// Counts the number of trailing zero bits in the operand.
353 /// \headerfile <x86intrin.h>
355 /// This intrinsic corresponds to the <c> TZCNT </c> instruction.
358 /// An unsigned 64-bit integer whose trailing zeros are to be counted.
359 /// \returns An 64-bit integer containing the number of trailing zero bits in
361 static __inline__ long long __RELAXED_FN_ATTRS
362 _mm_tzcnt_64(unsigned long long __X)
364 return __builtin_ia32_tzcnt_u64(__X);
367 #endif /* __x86_64__ */
369 #undef __DEFAULT_FN_ATTRS
370 #undef __RELAXED_FN_ATTRS
372 #endif /* __BMIINTRIN_H */