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 /// \brief 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 /// \brief 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 /// \brief 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
103 static __inline__ unsigned int __DEFAULT_FN_ATTRS
104 __bextr_u32(unsigned int __X, unsigned int __Y)
106 return __builtin_ia32_bextr_u32(__X, __Y);
109 /* Intel-specified, single-leading-underscore version of BEXTR */
110 /// \brief Extracts the specified bits from the first operand and returns them
111 /// in the least significant bits of the result.
113 /// \headerfile <x86intrin.h>
115 /// This intrinsic corresponds to the <c> BEXTR </c> instruction.
118 /// An unsigned integer whose bits are to be extracted.
120 /// An unsigned integer used to specify the index of the least significant
121 /// bit for the bits to be extracted. Bits [7:0] specify the index.
123 /// An unsigned integer used to specify the number of bits to be extracted.
124 /// Bits [7:0] specify the number of bits.
125 /// \returns An unsigned integer whose least significant bits contain the
127 static __inline__ unsigned int __DEFAULT_FN_ATTRS
128 _bextr_u32(unsigned int __X, unsigned int __Y, unsigned int __Z)
130 return __builtin_ia32_bextr_u32 (__X, ((__Y & 0xff) | ((__Z & 0xff) << 8)));
133 /// \brief Clears all bits in the source except for the least significant bit
134 /// containing a value of 1 and returns the result.
136 /// \headerfile <x86intrin.h>
138 /// This intrinsic corresponds to the <c> BLSI </c> instruction.
141 /// An unsigned integer whose bits are to be cleared.
142 /// \returns An unsigned integer containing the result of clearing the bits from
143 /// the source operand.
144 static __inline__ unsigned int __DEFAULT_FN_ATTRS
145 __blsi_u32(unsigned int __X)
150 /// \brief Creates a mask whose bits are set to 1, using bit 0 up to and
151 /// including the least siginificant bit that is set to 1 in the source
152 /// operand and returns the result.
154 /// \headerfile <x86intrin.h>
156 /// This intrinsic corresponds to the <c> BLSMSK </c> instruction.
159 /// An unsigned integer used to create the mask.
160 /// \returns An unsigned integer containing the newly created mask.
161 static __inline__ unsigned int __DEFAULT_FN_ATTRS
162 __blsmsk_u32(unsigned int __X)
164 return __X ^ (__X - 1);
167 /// \brief Clears the least siginificant bit that is set to 1 in the source
168 /// operand and returns the result.
170 /// \headerfile <x86intrin.h>
172 /// This intrinsic corresponds to the <c> BLSR </c> instruction.
175 /// An unsigned integer containing the operand to be cleared.
176 /// \returns An unsigned integer containing the result of clearing the source
178 static __inline__ unsigned int __DEFAULT_FN_ATTRS
179 __blsr_u32(unsigned int __X)
181 return __X & (__X - 1);
184 /// \brief Counts the number of trailing zero bits in the operand.
186 /// \headerfile <x86intrin.h>
188 /// This intrinsic corresponds to the <c> TZCNT </c> instruction.
191 /// An unsigned 32-bit integer whose trailing zeros are to be counted.
192 /// \returns An unsigned 32-bit integer containing the number of trailing zero
193 /// bits in the operand.
194 static __inline__ unsigned int __RELAXED_FN_ATTRS
195 __tzcnt_u32(unsigned int __X)
197 return __X ? __builtin_ctz(__X) : 32;
200 /// \brief Counts the number of trailing zero bits in the operand.
202 /// \headerfile <x86intrin.h>
204 /// This intrinsic corresponds to the <c> TZCNT </c> instruction.
207 /// An unsigned 32-bit integer whose trailing zeros are to be counted.
208 /// \returns An 32-bit integer containing the number of trailing zero bits in
210 static __inline__ int __RELAXED_FN_ATTRS
211 _mm_tzcnt_32(unsigned int __X)
213 return __X ? __builtin_ctz(__X) : 32;
218 #define _andn_u64(a, b) (__andn_u64((a), (b)))
220 /* _bextr_u64 != __bextr_u64 */
221 #define _blsi_u64(a) (__blsi_u64((a)))
223 #define _blsmsk_u64(a) (__blsmsk_u64((a)))
225 #define _blsr_u64(a) (__blsr_u64((a)))
227 #define _tzcnt_u64(a) (__tzcnt_u64((a)))
229 /// \brief Performs a bitwise AND of the second operand with the one's
230 /// complement of the first operand.
232 /// \headerfile <x86intrin.h>
234 /// This intrinsic corresponds to the <c> ANDN </c> instruction.
237 /// An unsigned 64-bit integer containing one of the operands.
239 /// An unsigned 64-bit integer containing one of the operands.
240 /// \returns An unsigned 64-bit integer containing the bitwise AND of the second
241 /// operand with the one's complement of the first operand.
242 static __inline__ unsigned long long __DEFAULT_FN_ATTRS
243 __andn_u64 (unsigned long long __X, unsigned long long __Y)
248 /* AMD-specified, double-leading-underscore version of BEXTR */
249 /// \brief Extracts the specified bits from the first operand and returns them
250 /// in the least significant bits of the result.
252 /// \headerfile <x86intrin.h>
254 /// This intrinsic corresponds to the <c> BEXTR </c> instruction.
257 /// An unsigned 64-bit integer whose bits are to be extracted.
259 /// An unsigned 64-bit integer used to specify which bits are extracted. Bits
260 /// [7:0] specify the index of the least significant bit. Bits [15:8] specify
261 /// the number of bits to be extracted.
262 /// \returns An unsigned 64-bit integer whose least significant bits contain the
264 static __inline__ unsigned long long __DEFAULT_FN_ATTRS
265 __bextr_u64(unsigned long long __X, unsigned long long __Y)
267 return __builtin_ia32_bextr_u64(__X, __Y);
270 /* Intel-specified, single-leading-underscore version of BEXTR */
271 /// \brief Extracts the specified bits from the first operand and returns them
272 /// in the least significant bits of the result.
274 /// \headerfile <x86intrin.h>
276 /// This intrinsic corresponds to the <c> BEXTR </c> instruction.
279 /// An unsigned 64-bit integer whose bits are to be extracted.
281 /// An unsigned integer used to specify the index of the least significant
282 /// bit for the bits to be extracted. Bits [7:0] specify the index.
284 /// An unsigned integer used to specify the number of bits to be extracted.
285 /// Bits [7:0] specify the number of bits.
286 /// \returns An unsigned 64-bit integer whose least significant bits contain the
288 static __inline__ unsigned long long __DEFAULT_FN_ATTRS
289 _bextr_u64(unsigned long long __X, unsigned int __Y, unsigned int __Z)
291 return __builtin_ia32_bextr_u64 (__X, ((__Y & 0xff) | ((__Z & 0xff) << 8)));
294 /// \brief Clears all bits in the source except for the least significant bit
295 /// containing a value of 1 and returns the result.
297 /// \headerfile <x86intrin.h>
299 /// This intrinsic corresponds to the <c> BLSI </c> instruction.
302 /// An unsigned 64-bit integer whose bits are to be cleared.
303 /// \returns An unsigned 64-bit integer containing the result of clearing the
304 /// bits from the source operand.
305 static __inline__ unsigned long long __DEFAULT_FN_ATTRS
306 __blsi_u64(unsigned long long __X)
311 /// \brief Creates a mask whose bits are set to 1, using bit 0 up to and
312 /// including the least siginificant bit that is set to 1 in the source
313 /// operand and returns the result.
315 /// \headerfile <x86intrin.h>
317 /// This intrinsic corresponds to the <c> BLSMSK </c> instruction.
320 /// An unsigned 64-bit integer used to create the mask.
321 /// \returns A unsigned 64-bit integer containing the newly created mask.
322 static __inline__ unsigned long long __DEFAULT_FN_ATTRS
323 __blsmsk_u64(unsigned long long __X)
325 return __X ^ (__X - 1);
328 /// \brief Clears the least siginificant bit that is set to 1 in the source
329 /// operand and returns the result.
331 /// \headerfile <x86intrin.h>
333 /// This intrinsic corresponds to the <c> BLSR </c> instruction.
336 /// An unsigned 64-bit integer containing the operand to be cleared.
337 /// \returns An unsigned 64-bit integer containing the result of clearing the
339 static __inline__ unsigned long long __DEFAULT_FN_ATTRS
340 __blsr_u64(unsigned long long __X)
342 return __X & (__X - 1);
345 /// \brief Counts the number of trailing zero bits in the operand.
347 /// \headerfile <x86intrin.h>
349 /// This intrinsic corresponds to the <c> TZCNT </c> instruction.
352 /// An unsigned 64-bit integer whose trailing zeros are to be counted.
353 /// \returns An unsigned 64-bit integer containing the number of trailing zero
354 /// bits in the operand.
355 static __inline__ unsigned long long __RELAXED_FN_ATTRS
356 __tzcnt_u64(unsigned long long __X)
358 return __X ? __builtin_ctzll(__X) : 64;
361 /// \brief Counts the number of trailing zero bits in the operand.
363 /// \headerfile <x86intrin.h>
365 /// This intrinsic corresponds to the <c> TZCNT </c> instruction.
368 /// An unsigned 64-bit integer whose trailing zeros are to be counted.
369 /// \returns An 64-bit integer containing the number of trailing zero bits in
371 static __inline__ long long __RELAXED_FN_ATTRS
372 _mm_tzcnt_64(unsigned long long __X)
374 return __X ? __builtin_ctzll(__X) : 64;
377 #endif /* __x86_64__ */
379 #undef __DEFAULT_FN_ATTRS
380 #undef __RELAXED_FN_ATTRS
382 #endif /* __BMIINTRIN_H */