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.
51 static __inline__
unsigned int __RELAXED_FN_ATTRS
52 __tzcnt_u32(unsigned int __X
)
54 return __builtin_ia32_tzcnt_u32(__X
);
57 /// Counts the number of trailing zero bits in the operand.
59 /// \headerfile <x86intrin.h>
61 /// This intrinsic corresponds to the <c> TZCNT </c> instruction.
64 /// An unsigned 32-bit integer whose trailing zeros are to be counted.
65 /// \returns An 32-bit integer containing the number of trailing zero bits in
68 static __inline__
int __RELAXED_FN_ATTRS
69 _mm_tzcnt_32(unsigned int __X
)
71 return (int)__builtin_ia32_tzcnt_u32(__X
);
74 #define _tzcnt_u32(a) (__tzcnt_u32((a)))
78 /// Counts the number of trailing zero bits in the operand.
80 /// \headerfile <x86intrin.h>
82 /// This intrinsic corresponds to the <c> TZCNT </c> instruction.
85 /// An unsigned 64-bit integer whose trailing zeros are to be counted.
86 /// \returns An unsigned 64-bit integer containing the number of trailing zero
87 /// bits in the operand.
89 static __inline__
unsigned long long __RELAXED_FN_ATTRS
90 __tzcnt_u64(unsigned long long __X
)
92 return __builtin_ia32_tzcnt_u64(__X
);
95 /// Counts the number of trailing zero bits in the operand.
97 /// \headerfile <x86intrin.h>
99 /// This intrinsic corresponds to the <c> TZCNT </c> instruction.
102 /// An unsigned 64-bit integer whose trailing zeros are to be counted.
103 /// \returns An 64-bit integer containing the number of trailing zero bits in
106 static __inline__
long long __RELAXED_FN_ATTRS
107 _mm_tzcnt_64(unsigned long long __X
)
109 return (long long)__builtin_ia32_tzcnt_u64(__X
);
112 #define _tzcnt_u64(a) (__tzcnt_u64((a)))
114 #endif /* __x86_64__ */
116 #undef __RELAXED_FN_ATTRS
118 #if !(defined(_MSC_VER) || defined(__SCE__)) || __has_feature(modules) || \
121 /* Define the default attributes for the functions in this file. */
122 #define __DEFAULT_FN_ATTRS __attribute__((__always_inline__, __nodebug__, __target__("bmi")))
124 #define _andn_u32(a, b) (__andn_u32((a), (b)))
126 /* _bextr_u32 != __bextr_u32 */
127 #define _blsi_u32(a) (__blsi_u32((a)))
129 #define _blsmsk_u32(a) (__blsmsk_u32((a)))
131 #define _blsr_u32(a) (__blsr_u32((a)))
133 /// Performs a bitwise AND of the second operand with the one's
134 /// complement of the first operand.
136 /// \headerfile <x86intrin.h>
138 /// This intrinsic corresponds to the <c> ANDN </c> instruction.
141 /// An unsigned integer containing one of the operands.
143 /// An unsigned integer containing one of the operands.
144 /// \returns An unsigned integer containing the bitwise AND of the second
145 /// operand with the one's complement of the first operand.
146 static __inline__
unsigned int __DEFAULT_FN_ATTRS
147 __andn_u32(unsigned int __X
, unsigned int __Y
)
152 /* AMD-specified, double-leading-underscore version of BEXTR */
153 /// Extracts the specified bits from the first operand and returns them
154 /// in the least significant bits of the result.
156 /// \headerfile <x86intrin.h>
158 /// This intrinsic corresponds to the <c> BEXTR </c> instruction.
161 /// An unsigned integer whose bits are to be extracted.
163 /// An unsigned integer used to specify which bits are extracted. Bits [7:0]
164 /// specify the index of the least significant bit. Bits [15:8] specify the
165 /// number of bits to be extracted.
166 /// \returns An unsigned integer whose least significant bits contain the
169 static __inline__
unsigned int __DEFAULT_FN_ATTRS
170 __bextr_u32(unsigned int __X
, unsigned int __Y
)
172 return __builtin_ia32_bextr_u32(__X
, __Y
);
175 /* Intel-specified, single-leading-underscore version of BEXTR */
176 /// Extracts the specified bits from the first operand and returns them
177 /// in the least significant bits of the result.
179 /// \headerfile <x86intrin.h>
181 /// This intrinsic corresponds to the <c> BEXTR </c> instruction.
184 /// An unsigned integer whose bits are to be extracted.
186 /// An unsigned integer used to specify the index of the least significant
187 /// bit for the bits to be extracted. Bits [7:0] specify the index.
189 /// An unsigned integer used to specify the number of bits to be extracted.
190 /// Bits [7:0] specify the number of bits.
191 /// \returns An unsigned integer whose least significant bits contain the
194 static __inline__
unsigned int __DEFAULT_FN_ATTRS
195 _bextr_u32(unsigned int __X
, unsigned int __Y
, unsigned int __Z
)
197 return __builtin_ia32_bextr_u32 (__X
, ((__Y
& 0xff) | ((__Z
& 0xff) << 8)));
200 /* Intel-specified, single-leading-underscore version of BEXTR2 */
201 /// Extracts the specified bits from the first operand and returns them
202 /// in the least significant bits of the result.
204 /// \headerfile <x86intrin.h>
206 /// This intrinsic corresponds to the <c> BEXTR </c> instruction.
209 /// An unsigned integer whose bits are to be extracted.
211 /// An unsigned integer used to specify which bits are extracted. Bits [7:0]
212 /// specify the index of the least significant bit. Bits [15:8] specify the
213 /// number of bits to be extracted.
214 /// \returns An unsigned integer whose least significant bits contain the
217 static __inline__
unsigned int __DEFAULT_FN_ATTRS
218 _bextr2_u32(unsigned int __X
, unsigned int __Y
) {
219 return __builtin_ia32_bextr_u32(__X
, __Y
);
222 /// Clears all bits in the source except for the least significant bit
223 /// containing a value of 1 and returns the result.
225 /// \headerfile <x86intrin.h>
227 /// This intrinsic corresponds to the <c> BLSI </c> instruction.
230 /// An unsigned integer whose bits are to be cleared.
231 /// \returns An unsigned integer containing the result of clearing the bits from
232 /// the source operand.
233 static __inline__
unsigned int __DEFAULT_FN_ATTRS
234 __blsi_u32(unsigned int __X
)
239 /// Creates a mask whose bits are set to 1, using bit 0 up to and
240 /// including the least significant bit that is set to 1 in the source
241 /// operand and returns the result.
243 /// \headerfile <x86intrin.h>
245 /// This intrinsic corresponds to the <c> BLSMSK </c> instruction.
248 /// An unsigned integer used to create the mask.
249 /// \returns An unsigned integer containing the newly created mask.
250 static __inline__
unsigned int __DEFAULT_FN_ATTRS
251 __blsmsk_u32(unsigned int __X
)
253 return __X
^ (__X
- 1);
256 /// Clears the least significant bit that is set to 1 in the source
257 /// operand and returns the result.
259 /// \headerfile <x86intrin.h>
261 /// This intrinsic corresponds to the <c> BLSR </c> instruction.
264 /// An unsigned integer containing the operand to be cleared.
265 /// \returns An unsigned integer containing the result of clearing the source
267 static __inline__
unsigned int __DEFAULT_FN_ATTRS
268 __blsr_u32(unsigned int __X
)
270 return __X
& (__X
- 1);
275 #define _andn_u64(a, b) (__andn_u64((a), (b)))
277 /* _bextr_u64 != __bextr_u64 */
278 #define _blsi_u64(a) (__blsi_u64((a)))
280 #define _blsmsk_u64(a) (__blsmsk_u64((a)))
282 #define _blsr_u64(a) (__blsr_u64((a)))
284 /// Performs a bitwise AND of the second operand with the one's
285 /// complement of the first operand.
287 /// \headerfile <x86intrin.h>
289 /// This intrinsic corresponds to the <c> ANDN </c> instruction.
292 /// An unsigned 64-bit integer containing one of the operands.
294 /// An unsigned 64-bit integer containing one of the operands.
295 /// \returns An unsigned 64-bit integer containing the bitwise AND of the second
296 /// operand with the one's complement of the first operand.
297 static __inline__
unsigned long long __DEFAULT_FN_ATTRS
298 __andn_u64 (unsigned long long __X
, unsigned long long __Y
)
303 /* AMD-specified, double-leading-underscore version of BEXTR */
304 /// Extracts the specified bits from the first operand and returns them
305 /// in the least significant bits of the result.
307 /// \headerfile <x86intrin.h>
309 /// This intrinsic corresponds to the <c> BEXTR </c> instruction.
312 /// An unsigned 64-bit integer whose bits are to be extracted.
314 /// An unsigned 64-bit integer used to specify which bits are extracted. Bits
315 /// [7:0] specify the index of the least significant bit. Bits [15:8] specify
316 /// the number of bits to be extracted.
317 /// \returns An unsigned 64-bit integer whose least significant bits contain the
320 static __inline__
unsigned long long __DEFAULT_FN_ATTRS
321 __bextr_u64(unsigned long long __X
, unsigned long long __Y
)
323 return __builtin_ia32_bextr_u64(__X
, __Y
);
326 /* Intel-specified, single-leading-underscore version of BEXTR */
327 /// Extracts the specified bits from the first operand and returns them
328 /// in the least significant bits of the result.
330 /// \headerfile <x86intrin.h>
332 /// This intrinsic corresponds to the <c> BEXTR </c> instruction.
335 /// An unsigned 64-bit integer whose bits are to be extracted.
337 /// An unsigned integer used to specify the index of the least significant
338 /// bit for the bits to be extracted. Bits [7:0] specify the index.
340 /// An unsigned integer used to specify the number of bits to be extracted.
341 /// Bits [7:0] specify the number of bits.
342 /// \returns An unsigned 64-bit integer whose least significant bits contain the
345 static __inline__
unsigned long long __DEFAULT_FN_ATTRS
346 _bextr_u64(unsigned long long __X
, unsigned int __Y
, unsigned int __Z
)
348 return __builtin_ia32_bextr_u64 (__X
, ((__Y
& 0xff) | ((__Z
& 0xff) << 8)));
351 /* Intel-specified, single-leading-underscore version of BEXTR2 */
352 /// Extracts the specified bits from the first operand and returns them
353 /// in the least significant bits of the result.
355 /// \headerfile <x86intrin.h>
357 /// This intrinsic corresponds to the <c> BEXTR </c> instruction.
360 /// An unsigned 64-bit integer whose bits are to be extracted.
362 /// An unsigned 64-bit integer used to specify which bits are extracted. Bits
363 /// [7:0] specify the index of the least significant bit. Bits [15:8] specify
364 /// the number of bits to be extracted.
365 /// \returns An unsigned 64-bit integer whose least significant bits contain the
368 static __inline__
unsigned long long __DEFAULT_FN_ATTRS
369 _bextr2_u64(unsigned long long __X
, unsigned long long __Y
) {
370 return __builtin_ia32_bextr_u64(__X
, __Y
);
373 /// Clears all bits in the source except for the least significant bit
374 /// containing a value of 1 and returns the result.
376 /// \headerfile <x86intrin.h>
378 /// This intrinsic corresponds to the <c> BLSI </c> instruction.
381 /// An unsigned 64-bit integer whose bits are to be cleared.
382 /// \returns An unsigned 64-bit integer containing the result of clearing the
383 /// bits from the source operand.
384 static __inline__
unsigned long long __DEFAULT_FN_ATTRS
385 __blsi_u64(unsigned long long __X
)
390 /// Creates a mask whose bits are set to 1, using bit 0 up to and
391 /// including the least significant bit that is set to 1 in the source
392 /// operand and returns the result.
394 /// \headerfile <x86intrin.h>
396 /// This intrinsic corresponds to the <c> BLSMSK </c> instruction.
399 /// An unsigned 64-bit integer used to create the mask.
400 /// \returns An unsigned 64-bit integer containing the newly created mask.
401 static __inline__
unsigned long long __DEFAULT_FN_ATTRS
402 __blsmsk_u64(unsigned long long __X
)
404 return __X
^ (__X
- 1);
407 /// Clears the least significant bit that is set to 1 in the source
408 /// operand and returns the result.
410 /// \headerfile <x86intrin.h>
412 /// This intrinsic corresponds to the <c> BLSR </c> instruction.
415 /// An unsigned 64-bit integer containing the operand to be cleared.
416 /// \returns An unsigned 64-bit integer containing the result of clearing the
418 static __inline__
unsigned long long __DEFAULT_FN_ATTRS
419 __blsr_u64(unsigned long long __X
)
421 return __X
& (__X
- 1);
424 #endif /* __x86_64__ */
426 #undef __DEFAULT_FN_ATTRS
428 #endif /* !(defined(_MSC_VER) || defined(__SCE__)) || __has_feature(modules) \
429 || defined(__BMI__) */
431 #endif /* __BMIINTRIN_H */