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 /* Allow using the tzcnt intrinsics even for non-BMI targets. Since the TZCNT
18 instruction behaves as BSF on non-BMI targets, there is code that expects
19 to use it as a potentially faster version of BSF. */
20 #define __RELAXED_FN_ATTRS __attribute__((__always_inline__, __nodebug__))
22 #define _tzcnt_u16(a) (__tzcnt_u16((a)))
24 /// Counts the number of trailing zero bits in the operand.
26 /// \headerfile <x86intrin.h>
28 /// This intrinsic corresponds to the <c> TZCNT </c> instruction.
31 /// An unsigned 16-bit integer whose trailing zeros are to be counted.
32 /// \returns An unsigned 16-bit integer containing the number of trailing zero
33 /// bits in the operand.
34 static __inline__ unsigned short __RELAXED_FN_ATTRS
35 __tzcnt_u16(unsigned short __X)
37 return __builtin_ia32_tzcnt_u16(__X);
40 /// Counts the number of trailing zero bits in the operand.
42 /// \headerfile <x86intrin.h>
44 /// This intrinsic corresponds to the <c> TZCNT </c> instruction.
47 /// An unsigned 32-bit integer whose trailing zeros are to be counted.
48 /// \returns An unsigned 32-bit integer containing the number of trailing zero
49 /// bits in the operand.
50 static __inline__ unsigned int __RELAXED_FN_ATTRS
51 __tzcnt_u32(unsigned int __X)
53 return __builtin_ia32_tzcnt_u32(__X);
56 /// Counts the number of trailing zero bits in the operand.
58 /// \headerfile <x86intrin.h>
60 /// This intrinsic corresponds to the <c> TZCNT </c> instruction.
63 /// An unsigned 32-bit integer whose trailing zeros are to be counted.
64 /// \returns An 32-bit integer containing the number of trailing zero bits in
66 static __inline__ int __RELAXED_FN_ATTRS
67 _mm_tzcnt_32(unsigned int __X)
69 return __builtin_ia32_tzcnt_u32(__X);
72 #define _tzcnt_u32(a) (__tzcnt_u32((a)))
76 /// Counts the number of trailing zero bits in the operand.
78 /// \headerfile <x86intrin.h>
80 /// This intrinsic corresponds to the <c> TZCNT </c> instruction.
83 /// An unsigned 64-bit integer whose trailing zeros are to be counted.
84 /// \returns An unsigned 64-bit integer containing the number of trailing zero
85 /// bits in the operand.
86 static __inline__ unsigned long long __RELAXED_FN_ATTRS
87 __tzcnt_u64(unsigned long long __X)
89 return __builtin_ia32_tzcnt_u64(__X);
92 /// Counts the number of trailing zero bits in the operand.
94 /// \headerfile <x86intrin.h>
96 /// This intrinsic corresponds to the <c> TZCNT </c> instruction.
99 /// An unsigned 64-bit integer whose trailing zeros are to be counted.
100 /// \returns An 64-bit integer containing the number of trailing zero bits in
102 static __inline__ long long __RELAXED_FN_ATTRS
103 _mm_tzcnt_64(unsigned long long __X)
105 return __builtin_ia32_tzcnt_u64(__X);
108 #define _tzcnt_u64(a) (__tzcnt_u64((a)))
110 #endif /* __x86_64__ */
112 #undef __RELAXED_FN_ATTRS
114 #if !(defined(_MSC_VER) || defined(__SCE__)) || __has_feature(modules) || \
117 /* Define the default attributes for the functions in this file. */
118 #define __DEFAULT_FN_ATTRS __attribute__((__always_inline__, __nodebug__, __target__("bmi")))
120 #define _andn_u32(a, b) (__andn_u32((a), (b)))
122 /* _bextr_u32 != __bextr_u32 */
123 #define _blsi_u32(a) (__blsi_u32((a)))
125 #define _blsmsk_u32(a) (__blsmsk_u32((a)))
127 #define _blsr_u32(a) (__blsr_u32((a)))
129 /// Performs a bitwise AND of the second operand with the one's
130 /// complement of the first operand.
132 /// \headerfile <x86intrin.h>
134 /// This intrinsic corresponds to the <c> ANDN </c> instruction.
137 /// An unsigned integer containing one of the operands.
139 /// An unsigned integer containing one of the operands.
140 /// \returns An unsigned integer containing the bitwise AND of the second
141 /// operand with the one's complement of the first operand.
142 static __inline__ unsigned int __DEFAULT_FN_ATTRS
143 __andn_u32(unsigned int __X, unsigned int __Y)
148 /* AMD-specified, double-leading-underscore version of BEXTR */
149 /// Extracts the specified bits from the first operand and returns them
150 /// in the least significant bits of the result.
152 /// \headerfile <x86intrin.h>
154 /// This intrinsic corresponds to the <c> BEXTR </c> instruction.
157 /// An unsigned integer whose bits are to be extracted.
159 /// An unsigned integer used to specify which bits are extracted. Bits [7:0]
160 /// specify the index of the least significant bit. Bits [15:8] specify the
161 /// number of bits to be extracted.
162 /// \returns An unsigned integer whose least significant bits contain the
165 static __inline__ unsigned int __DEFAULT_FN_ATTRS
166 __bextr_u32(unsigned int __X, unsigned int __Y)
168 return __builtin_ia32_bextr_u32(__X, __Y);
171 /* Intel-specified, single-leading-underscore version of BEXTR */
172 /// Extracts the specified bits from the first operand and returns them
173 /// in the least significant bits of the result.
175 /// \headerfile <x86intrin.h>
177 /// This intrinsic corresponds to the <c> BEXTR </c> instruction.
180 /// An unsigned integer whose bits are to be extracted.
182 /// An unsigned integer used to specify the index of the least significant
183 /// bit for the bits to be extracted. Bits [7:0] specify the index.
185 /// An unsigned integer used to specify the number of bits to be extracted.
186 /// Bits [7:0] specify the number of bits.
187 /// \returns An unsigned integer whose least significant bits contain the
190 static __inline__ unsigned int __DEFAULT_FN_ATTRS
191 _bextr_u32(unsigned int __X, unsigned int __Y, unsigned int __Z)
193 return __builtin_ia32_bextr_u32 (__X, ((__Y & 0xff) | ((__Z & 0xff) << 8)));
196 /* Intel-specified, single-leading-underscore version of BEXTR2 */
197 /// Extracts the specified bits from the first operand and returns them
198 /// in the least significant bits of the result.
200 /// \headerfile <x86intrin.h>
202 /// This intrinsic corresponds to the <c> BEXTR </c> instruction.
205 /// An unsigned integer whose bits are to be extracted.
207 /// An unsigned integer used to specify which bits are extracted. Bits [7:0]
208 /// specify the index of the least significant bit. Bits [15:8] specify the
209 /// number of bits to be extracted.
210 /// \returns An unsigned integer whose least significant bits contain the
213 static __inline__ unsigned int __DEFAULT_FN_ATTRS
214 _bextr2_u32(unsigned int __X, unsigned int __Y) {
215 return __builtin_ia32_bextr_u32(__X, __Y);
218 /// Clears all bits in the source except for the least significant bit
219 /// containing a value of 1 and returns the result.
221 /// \headerfile <x86intrin.h>
223 /// This intrinsic corresponds to the <c> BLSI </c> instruction.
226 /// An unsigned integer whose bits are to be cleared.
227 /// \returns An unsigned integer containing the result of clearing the bits from
228 /// the source operand.
229 static __inline__ unsigned int __DEFAULT_FN_ATTRS
230 __blsi_u32(unsigned int __X)
235 /// Creates a mask whose bits are set to 1, using bit 0 up to and
236 /// including the least significant bit that is set to 1 in the source
237 /// operand and returns the result.
239 /// \headerfile <x86intrin.h>
241 /// This intrinsic corresponds to the <c> BLSMSK </c> instruction.
244 /// An unsigned integer used to create the mask.
245 /// \returns An unsigned integer containing the newly created mask.
246 static __inline__ unsigned int __DEFAULT_FN_ATTRS
247 __blsmsk_u32(unsigned int __X)
249 return __X ^ (__X - 1);
252 /// Clears the least significant bit that is set to 1 in the source
253 /// operand and returns the result.
255 /// \headerfile <x86intrin.h>
257 /// This intrinsic corresponds to the <c> BLSR </c> instruction.
260 /// An unsigned integer containing the operand to be cleared.
261 /// \returns An unsigned integer containing the result of clearing the source
263 static __inline__ unsigned int __DEFAULT_FN_ATTRS
264 __blsr_u32(unsigned int __X)
266 return __X & (__X - 1);
271 #define _andn_u64(a, b) (__andn_u64((a), (b)))
273 /* _bextr_u64 != __bextr_u64 */
274 #define _blsi_u64(a) (__blsi_u64((a)))
276 #define _blsmsk_u64(a) (__blsmsk_u64((a)))
278 #define _blsr_u64(a) (__blsr_u64((a)))
280 /// Performs a bitwise AND of the second operand with the one's
281 /// complement of the first operand.
283 /// \headerfile <x86intrin.h>
285 /// This intrinsic corresponds to the <c> ANDN </c> instruction.
288 /// An unsigned 64-bit integer containing one of the operands.
290 /// An unsigned 64-bit integer containing one of the operands.
291 /// \returns An unsigned 64-bit integer containing the bitwise AND of the second
292 /// operand with the one's complement of the first operand.
293 static __inline__ unsigned long long __DEFAULT_FN_ATTRS
294 __andn_u64 (unsigned long long __X, unsigned long long __Y)
299 /* AMD-specified, double-leading-underscore version of BEXTR */
300 /// Extracts the specified bits from the first operand and returns them
301 /// in the least significant bits of the result.
303 /// \headerfile <x86intrin.h>
305 /// This intrinsic corresponds to the <c> BEXTR </c> instruction.
308 /// An unsigned 64-bit integer whose bits are to be extracted.
310 /// An unsigned 64-bit integer used to specify which bits are extracted. Bits
311 /// [7:0] specify the index of the least significant bit. Bits [15:8] specify
312 /// the number of bits to be extracted.
313 /// \returns An unsigned 64-bit integer whose least significant bits contain the
316 static __inline__ unsigned long long __DEFAULT_FN_ATTRS
317 __bextr_u64(unsigned long long __X, unsigned long long __Y)
319 return __builtin_ia32_bextr_u64(__X, __Y);
322 /* Intel-specified, single-leading-underscore version of BEXTR */
323 /// Extracts the specified bits from the first operand and returns them
324 /// in the least significant bits of the result.
326 /// \headerfile <x86intrin.h>
328 /// This intrinsic corresponds to the <c> BEXTR </c> instruction.
331 /// An unsigned 64-bit integer whose bits are to be extracted.
333 /// An unsigned integer used to specify the index of the least significant
334 /// bit for the bits to be extracted. Bits [7:0] specify the index.
336 /// An unsigned integer used to specify the number of bits to be extracted.
337 /// Bits [7:0] specify the number of bits.
338 /// \returns An unsigned 64-bit integer whose least significant bits contain the
341 static __inline__ unsigned long long __DEFAULT_FN_ATTRS
342 _bextr_u64(unsigned long long __X, unsigned int __Y, unsigned int __Z)
344 return __builtin_ia32_bextr_u64 (__X, ((__Y & 0xff) | ((__Z & 0xff) << 8)));
347 /* Intel-specified, single-leading-underscore version of BEXTR2 */
348 /// Extracts the specified bits from the first operand and returns them
349 /// in the least significant bits of the result.
351 /// \headerfile <x86intrin.h>
353 /// This intrinsic corresponds to the <c> BEXTR </c> instruction.
356 /// An unsigned 64-bit integer whose bits are to be extracted.
358 /// An unsigned 64-bit integer used to specify which bits are extracted. Bits
359 /// [7:0] specify the index of the least significant bit. Bits [15:8] specify
360 /// the number of bits to be extracted.
361 /// \returns An unsigned 64-bit integer whose least significant bits contain the
364 static __inline__ unsigned long long __DEFAULT_FN_ATTRS
365 _bextr2_u64(unsigned long long __X, unsigned long long __Y) {
366 return __builtin_ia32_bextr_u64(__X, __Y);
369 /// Clears all bits in the source except for the least significant bit
370 /// containing a value of 1 and returns the result.
372 /// \headerfile <x86intrin.h>
374 /// This intrinsic corresponds to the <c> BLSI </c> instruction.
377 /// An unsigned 64-bit integer whose bits are to be cleared.
378 /// \returns An unsigned 64-bit integer containing the result of clearing the
379 /// bits from the source operand.
380 static __inline__ unsigned long long __DEFAULT_FN_ATTRS
381 __blsi_u64(unsigned long long __X)
386 /// Creates a mask whose bits are set to 1, using bit 0 up to and
387 /// including the least significant bit that is set to 1 in the source
388 /// operand and returns the result.
390 /// \headerfile <x86intrin.h>
392 /// This intrinsic corresponds to the <c> BLSMSK </c> instruction.
395 /// An unsigned 64-bit integer used to create the mask.
396 /// \returns An unsigned 64-bit integer containing the newly created mask.
397 static __inline__ unsigned long long __DEFAULT_FN_ATTRS
398 __blsmsk_u64(unsigned long long __X)
400 return __X ^ (__X - 1);
403 /// Clears the least significant bit that is set to 1 in the source
404 /// operand and returns the result.
406 /// \headerfile <x86intrin.h>
408 /// This intrinsic corresponds to the <c> BLSR </c> instruction.
411 /// An unsigned 64-bit integer containing the operand to be cleared.
412 /// \returns An unsigned 64-bit integer containing the result of clearing the
414 static __inline__ unsigned long long __DEFAULT_FN_ATTRS
415 __blsr_u64(unsigned long long __X)
417 return __X & (__X - 1);
420 #endif /* __x86_64__ */
422 #undef __DEFAULT_FN_ATTRS
424 #endif /* !(defined(_MSC_VER) || defined(__SCE__)) || __has_feature(modules) \
425 || defined(__BMI__) */
427 #endif /* __BMIINTRIN_H */