blob: 650ee04e016dea662667ddb5dd61b037d2d5f7a3 [file] [log] [blame]
Craig Topperf2855ad2011-12-25 06:25:37 +00001/*===---- bmiintrin.h - BMI intrinsics -------------------------------------===
2 *
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:
9 *
10 * The above copyright notice and this permission notice shall be included in
11 * all copies or substantial portions of the Software.
12 *
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
19 * THE SOFTWARE.
20 *
21 *===-----------------------------------------------------------------------===
22 */
23
24#if !defined __X86INTRIN_H && !defined __IMMINTRIN_H
25#error "Never use <bmiintrin.h> directly; include <x86intrin.h> instead."
26#endif
27
Craig Topperf2855ad2011-12-25 06:25:37 +000028#ifndef __BMIINTRIN_H
29#define __BMIINTRIN_H
30
Ekaterina Romanovac8976d52016-03-08 01:36:59 +000031/// \brief Counts the number of trailing zero bits in the operand.
32///
33/// \headerfile <x86intrin.h>
34///
35/// \code
36/// unsigned short _tzcnt_u16(unsigned short a);
37/// \endcode
38///
Ekaterina Romanova0c1c3bb2016-12-09 18:35:50 +000039/// This intrinsic corresponds to the <c> TZCNT </c> instruction.
Ekaterina Romanovac8976d52016-03-08 01:36:59 +000040///
41/// \param a
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.
Sanjay Patel1585fb92014-05-28 20:26:57 +000045#define _tzcnt_u16(a) (__tzcnt_u16((a)))
Ekaterina Romanovac8976d52016-03-08 01:36:59 +000046
47/// \brief Performs a bitwise AND of the second operand with the one's
48/// complement of the first operand.
49///
50/// \headerfile <x86intrin.h>
51///
52/// \code
53/// unsigned int _andn_u32(unsigned int a, unsigned int b);
54/// \endcode
55///
Ekaterina Romanova0c1c3bb2016-12-09 18:35:50 +000056/// This intrinsic corresponds to the <c> ANDN </c> instruction.
Ekaterina Romanovac8976d52016-03-08 01:36:59 +000057///
58/// \param a
59/// An unsigned integer containing one of the operands.
60/// \param b
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.
Sanjay Patel1585fb92014-05-28 20:26:57 +000064#define _andn_u32(a, b) (__andn_u32((a), (b)))
Ekaterina Romanovac8976d52016-03-08 01:36:59 +000065
Sanjay Patel1585fb92014-05-28 20:26:57 +000066/* _bextr_u32 != __bextr_u32 */
Ekaterina Romanovac8976d52016-03-08 01:36:59 +000067/// \brief Clears all bits in the source except for the least significant bit
68/// containing a value of 1 and returns the result.
69///
70/// \headerfile <x86intrin.h>
71///
72/// \code
73/// unsigned int _blsi_u32(unsigned int a);
74/// \endcode
75///
Ekaterina Romanova0c1c3bb2016-12-09 18:35:50 +000076/// This intrinsic corresponds to the <c> BLSI </c> instruction.
Ekaterina Romanovac8976d52016-03-08 01:36:59 +000077///
Ekaterina Romanova1168fdc2016-05-16 22:54:45 +000078/// \param a
Ekaterina Romanovac8976d52016-03-08 01:36:59 +000079/// 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.
Sanjay Patel1585fb92014-05-28 20:26:57 +000082#define _blsi_u32(a) (__blsi_u32((a)))
Ekaterina Romanovac8976d52016-03-08 01:36:59 +000083
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.
87///
88/// \headerfile <x86intrin.h>
89///
90/// \code
91/// unsigned int _blsmsk_u32(unsigned int a);
92/// \endcode
93///
Ekaterina Romanova0c1c3bb2016-12-09 18:35:50 +000094/// This intrinsic corresponds to the <c> BLSMSK </c> instruction.
Ekaterina Romanovac8976d52016-03-08 01:36:59 +000095///
96/// \param a
97/// An unsigned integer used to create the mask.
98/// \returns An unsigned integer containing the newly created mask.
Sanjay Patel1585fb92014-05-28 20:26:57 +000099#define _blsmsk_u32(a) (__blsmsk_u32((a)))
Ekaterina Romanovac8976d52016-03-08 01:36:59 +0000100
101/// \brief Clears the least siginificant bit that is set to 1 in the source
102/// operand and returns the result.
103///
104/// \headerfile <x86intrin.h>
105///
106/// \code
107/// unsigned int _blsr_u32(unsigned int a);
108/// \endcode
109///
Ekaterina Romanova0c1c3bb2016-12-09 18:35:50 +0000110/// This intrinsic corresponds to the <c> BLSR </c> instruction.
Ekaterina Romanovac8976d52016-03-08 01:36:59 +0000111///
112/// \param a
113/// An unsigned integer containing the operand to be cleared.
114/// \returns An unsigned integer containing the result of clearing the source
115/// operand.
Sanjay Patel1585fb92014-05-28 20:26:57 +0000116#define _blsr_u32(a) (__blsr_u32((a)))
Ekaterina Romanovac8976d52016-03-08 01:36:59 +0000117
118/// \brief Counts the number of trailing zero bits in the operand.
119///
120/// \headerfile <x86intrin.h>
121///
122/// \code
123/// unsigned int _tzcnt_u32(unsigned int a);
124/// \endcode
125///
Ekaterina Romanova0c1c3bb2016-12-09 18:35:50 +0000126/// This intrinsic corresponds to the <c> TZCNT </c> instruction.
Ekaterina Romanovac8976d52016-03-08 01:36:59 +0000127///
128/// \param a
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.
Sanjay Patel1585fb92014-05-28 20:26:57 +0000132#define _tzcnt_u32(a) (__tzcnt_u32((a)))
133
Eric Christopher4d1851682015-06-17 07:09:20 +0000134/* Define the default attributes for the functions in this file. */
Michael Kupersteine45af542015-06-30 13:36:19 +0000135#define __DEFAULT_FN_ATTRS __attribute__((__always_inline__, __nodebug__, __target__("bmi")))
Eric Christopher4d1851682015-06-17 07:09:20 +0000136
Hans Wennborg1acf9552015-11-17 18:46:48 +0000137/* 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__))
141
Ekaterina Romanovac8976d52016-03-08 01:36:59 +0000142/// \brief Counts the number of trailing zero bits in the operand.
143///
144/// \headerfile <x86intrin.h>
145///
Ekaterina Romanova0c1c3bb2016-12-09 18:35:50 +0000146/// This intrinsic corresponds to the <c> TZCNT </c> instruction.
Ekaterina Romanovac8976d52016-03-08 01:36:59 +0000147///
148/// \param __X
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.
Hans Wennborg1acf9552015-11-17 18:46:48 +0000152static __inline__ unsigned short __RELAXED_FN_ATTRS
Craig Topper6490bdc2012-07-02 06:52:51 +0000153__tzcnt_u16(unsigned short __X)
Craig Topperf2855ad2011-12-25 06:25:37 +0000154{
Craig Toppera52e0d72014-11-01 22:50:54 +0000155 return __X ? __builtin_ctzs(__X) : 16;
Craig Topperf2855ad2011-12-25 06:25:37 +0000156}
157
Ekaterina Romanovac8976d52016-03-08 01:36:59 +0000158/// \brief Performs a bitwise AND of the second operand with the one's
159/// complement of the first operand.
160///
161/// \headerfile <x86intrin.h>
162///
Ekaterina Romanova0c1c3bb2016-12-09 18:35:50 +0000163/// This intrinsic corresponds to the <c> ANDN </c> instruction.
Ekaterina Romanovac8976d52016-03-08 01:36:59 +0000164///
165/// \param __X
166/// An unsigned integer containing one of the operands.
167/// \param __Y
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.
Michael Kupersteine45af542015-06-30 13:36:19 +0000171static __inline__ unsigned int __DEFAULT_FN_ATTRS
Craig Toppera06d4a12011-12-25 07:27:12 +0000172__andn_u32(unsigned int __X, unsigned int __Y)
173{
174 return ~__X & __Y;
175}
176
Sanjay Patel1585fb92014-05-28 20:26:57 +0000177/* AMD-specified, double-leading-underscore version of BEXTR */
Ekaterina Romanovac8976d52016-03-08 01:36:59 +0000178/// \brief Extracts the specified bits from the first operand and returns them
179/// in the least significant bits of the result.
180///
181/// \headerfile <x86intrin.h>
182///
Ekaterina Romanova0c1c3bb2016-12-09 18:35:50 +0000183/// This intrinsic corresponds to the <c> BEXTR </c> instruction.
Ekaterina Romanovac8976d52016-03-08 01:36:59 +0000184///
185/// \param __X
186/// An unsigned integer whose bits are to be extracted.
187/// \param __Y
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
192/// extracted bits.
Michael Kupersteine45af542015-06-30 13:36:19 +0000193static __inline__ unsigned int __DEFAULT_FN_ATTRS
Craig Toppera06d4a12011-12-25 07:27:12 +0000194__bextr_u32(unsigned int __X, unsigned int __Y)
195{
196 return __builtin_ia32_bextr_u32(__X, __Y);
197}
198
Sanjay Patel1585fb92014-05-28 20:26:57 +0000199/* Intel-specified, single-leading-underscore version of BEXTR */
Ekaterina Romanovac8976d52016-03-08 01:36:59 +0000200/// \brief Extracts the specified bits from the first operand and returns them
201/// in the least significant bits of the result.
202///
203/// \headerfile <x86intrin.h>
204///
Ekaterina Romanova0c1c3bb2016-12-09 18:35:50 +0000205/// This intrinsic corresponds to the <c> BEXTR </c> instruction.
Ekaterina Romanovac8976d52016-03-08 01:36:59 +0000206///
207/// \param __X
208/// An unsigned integer whose bits are to be extracted.
209/// \param __Y
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.
212/// \param __Z
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
216/// extracted bits.
Michael Kupersteine45af542015-06-30 13:36:19 +0000217static __inline__ unsigned int __DEFAULT_FN_ATTRS
Sanjay Patel1585fb92014-05-28 20:26:57 +0000218_bextr_u32(unsigned int __X, unsigned int __Y, unsigned int __Z)
219{
220 return __builtin_ia32_bextr_u32 (__X, ((__Y & 0xff) | ((__Z & 0xff) << 8)));
221}
222
Ekaterina Romanovac8976d52016-03-08 01:36:59 +0000223/// \brief Clears all bits in the source except for the least significant bit
224/// containing a value of 1 and returns the result.
225///
226/// \headerfile <x86intrin.h>
227///
Ekaterina Romanova0c1c3bb2016-12-09 18:35:50 +0000228/// This intrinsic corresponds to the <c> BLSI </c> instruction.
Ekaterina Romanovac8976d52016-03-08 01:36:59 +0000229///
230/// \param __X
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.
Michael Kupersteine45af542015-06-30 13:36:19 +0000234static __inline__ unsigned int __DEFAULT_FN_ATTRS
Craig Toppera06d4a12011-12-25 07:27:12 +0000235__blsi_u32(unsigned int __X)
236{
237 return __X & -__X;
238}
239
Ekaterina Romanovac8976d52016-03-08 01:36:59 +0000240/// \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.
243///
244/// \headerfile <x86intrin.h>
245///
Ekaterina Romanova0c1c3bb2016-12-09 18:35:50 +0000246/// This intrinsic corresponds to the <c> BLSMSK </c> instruction.
Ekaterina Romanovac8976d52016-03-08 01:36:59 +0000247///
248/// \param __X
249/// An unsigned integer used to create the mask.
250/// \returns An unsigned integer containing the newly created mask.
Michael Kupersteine45af542015-06-30 13:36:19 +0000251static __inline__ unsigned int __DEFAULT_FN_ATTRS
Craig Toppera06d4a12011-12-25 07:27:12 +0000252__blsmsk_u32(unsigned int __X)
253{
254 return __X ^ (__X - 1);
255}
256
Ekaterina Romanovac8976d52016-03-08 01:36:59 +0000257/// \brief Clears the least siginificant bit that is set to 1 in the source
258/// operand and returns the result.
259///
260/// \headerfile <x86intrin.h>
261///
Ekaterina Romanova0c1c3bb2016-12-09 18:35:50 +0000262/// This intrinsic corresponds to the <c> BLSR </c> instruction.
Ekaterina Romanovac8976d52016-03-08 01:36:59 +0000263///
264/// \param __X
265/// An unsigned integer containing the operand to be cleared.
266/// \returns An unsigned integer containing the result of clearing the source
267/// operand.
Michael Kupersteine45af542015-06-30 13:36:19 +0000268static __inline__ unsigned int __DEFAULT_FN_ATTRS
Craig Toppera06d4a12011-12-25 07:27:12 +0000269__blsr_u32(unsigned int __X)
270{
271 return __X & (__X - 1);
272}
273
Ekaterina Romanovac8976d52016-03-08 01:36:59 +0000274/// \brief Counts the number of trailing zero bits in the operand.
275///
276/// \headerfile <x86intrin.h>
277///
Ekaterina Romanova0c1c3bb2016-12-09 18:35:50 +0000278/// This intrinsic corresponds to the <c> TZCNT </c> instruction.
Ekaterina Romanovac8976d52016-03-08 01:36:59 +0000279///
280/// \param __X
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.
Hans Wennborg1acf9552015-11-17 18:46:48 +0000284static __inline__ unsigned int __RELAXED_FN_ATTRS
Craig Topper6490bdc2012-07-02 06:52:51 +0000285__tzcnt_u32(unsigned int __X)
Craig Topperf2855ad2011-12-25 06:25:37 +0000286{
Craig Toppera52e0d72014-11-01 22:50:54 +0000287 return __X ? __builtin_ctz(__X) : 32;
Craig Topperf2855ad2011-12-25 06:25:37 +0000288}
289
Michael Zuckerman716859a2016-06-22 12:32:43 +0000290/// \brief Counts the number of trailing zero bits in the operand.
291///
292/// \headerfile <x86intrin.h>
293///
Ekaterina Romanova0c1c3bb2016-12-09 18:35:50 +0000294/// This intrinsic corresponds to the <c> TZCNT </c> instruction.
Michael Zuckerman716859a2016-06-22 12:32:43 +0000295///
296/// \param __X
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
299/// bits in the operand.
300static __inline__ int __RELAXED_FN_ATTRS
301_mm_tzcnt_32(unsigned int __X)
302{
303 return __X ? __builtin_ctz(__X) : 32;
304}
305
Craig Topperf2855ad2011-12-25 06:25:37 +0000306#ifdef __x86_64__
Sanjay Patel1585fb92014-05-28 20:26:57 +0000307
Ekaterina Romanovac8976d52016-03-08 01:36:59 +0000308/// \brief Performs a bitwise AND of the second operand with the one's
309/// complement of the first operand.
310///
311/// \headerfile <x86intrin.h>
312///
313/// \code
314/// unsigned long long _andn_u64 (unsigned long long a, unsigned long long b);
315/// \endcode
316///
Ekaterina Romanova0c1c3bb2016-12-09 18:35:50 +0000317/// This intrinsic corresponds to the <c> ANDN </c> instruction.
Ekaterina Romanovac8976d52016-03-08 01:36:59 +0000318///
319/// \param a
320/// An unsigned 64-bit integer containing one of the operands.
321/// \param b
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.
Sanjay Patel1585fb92014-05-28 20:26:57 +0000325#define _andn_u64(a, b) (__andn_u64((a), (b)))
Ekaterina Romanovac8976d52016-03-08 01:36:59 +0000326
Sanjay Patel1585fb92014-05-28 20:26:57 +0000327/* _bextr_u64 != __bextr_u64 */
Ekaterina Romanovac8976d52016-03-08 01:36:59 +0000328/// \brief Clears all bits in the source except for the least significant bit
329/// containing a value of 1 and returns the result.
330///
331/// \headerfile <x86intrin.h>
332///
333/// \code
334/// unsigned long long _blsi_u64(unsigned long long a);
335/// \endcode
336///
Ekaterina Romanova0c1c3bb2016-12-09 18:35:50 +0000337/// This intrinsic corresponds to the <c> BLSI </c> instruction.
Ekaterina Romanovac8976d52016-03-08 01:36:59 +0000338///
339/// \param a
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.
Sanjay Patel1585fb92014-05-28 20:26:57 +0000343#define _blsi_u64(a) (__blsi_u64((a)))
Ekaterina Romanovac8976d52016-03-08 01:36:59 +0000344
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.
348///
349/// \headerfile <x86intrin.h>
350///
351/// \code
352/// unsigned long long _blsmsk_u64(unsigned long long a);
353/// \endcode
354///
Ekaterina Romanova0c1c3bb2016-12-09 18:35:50 +0000355/// This intrinsic corresponds to the <c> BLSMSK </c> instruction.
Ekaterina Romanovac8976d52016-03-08 01:36:59 +0000356///
357/// \param a
358/// An unsigned 64-bit integer used to create the mask.
359/// \returns A unsigned 64-bit integer containing the newly created mask.
Sanjay Patel1585fb92014-05-28 20:26:57 +0000360#define _blsmsk_u64(a) (__blsmsk_u64((a)))
Ekaterina Romanovac8976d52016-03-08 01:36:59 +0000361
362/// \brief Clears the least siginificant bit that is set to 1 in the source
363/// operand and returns the result.
364///
365/// \headerfile <x86intrin.h>
366///
367/// \code
368/// unsigned long long _blsr_u64(unsigned long long a);
369/// \endcode
370///
Ekaterina Romanova0c1c3bb2016-12-09 18:35:50 +0000371/// This intrinsic corresponds to the <c> BLSR </c> instruction.
Ekaterina Romanovac8976d52016-03-08 01:36:59 +0000372///
373/// \param a
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
376/// source operand.
Sanjay Patel1585fb92014-05-28 20:26:57 +0000377#define _blsr_u64(a) (__blsr_u64((a)))
Ekaterina Romanovac8976d52016-03-08 01:36:59 +0000378
379/// \brief Counts the number of trailing zero bits in the operand.
380///
381/// \headerfile <x86intrin.h>
382///
383/// \code
384/// unsigned long long _tzcnt_u64(unsigned long long a);
385/// \endcode
386///
Ekaterina Romanova0c1c3bb2016-12-09 18:35:50 +0000387/// This intrinsic corresponds to the <c> TZCNT </c> instruction.
Ekaterina Romanovac8976d52016-03-08 01:36:59 +0000388///
389/// \param a
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.
Sanjay Patel1585fb92014-05-28 20:26:57 +0000393#define _tzcnt_u64(a) (__tzcnt_u64((a)))
394
Ekaterina Romanovac8976d52016-03-08 01:36:59 +0000395/// \brief Performs a bitwise AND of the second operand with the one's
396/// complement of the first operand.
397///
398/// \headerfile <x86intrin.h>
399///
Ekaterina Romanova0c1c3bb2016-12-09 18:35:50 +0000400/// This intrinsic corresponds to the <c> ANDN </c> instruction.
Ekaterina Romanovac8976d52016-03-08 01:36:59 +0000401///
402/// \param __X
403/// An unsigned 64-bit integer containing one of the operands.
404/// \param __Y
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.
Michael Kupersteine45af542015-06-30 13:36:19 +0000408static __inline__ unsigned long long __DEFAULT_FN_ATTRS
Craig Toppera06d4a12011-12-25 07:27:12 +0000409__andn_u64 (unsigned long long __X, unsigned long long __Y)
410{
411 return ~__X & __Y;
412}
413
Sanjay Patel1585fb92014-05-28 20:26:57 +0000414/* AMD-specified, double-leading-underscore version of BEXTR */
Ekaterina Romanovac8976d52016-03-08 01:36:59 +0000415/// \brief Extracts the specified bits from the first operand and returns them
416/// in the least significant bits of the result.
417///
418/// \headerfile <x86intrin.h>
419///
Ekaterina Romanova0c1c3bb2016-12-09 18:35:50 +0000420/// This intrinsic corresponds to the <c> BEXTR </c> instruction.
Ekaterina Romanovac8976d52016-03-08 01:36:59 +0000421///
422/// \param __X
423/// An unsigned 64-bit integer whose bits are to be extracted.
424/// \param __Y
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
429/// extracted bits.
Michael Kupersteine45af542015-06-30 13:36:19 +0000430static __inline__ unsigned long long __DEFAULT_FN_ATTRS
Craig Toppera06d4a12011-12-25 07:27:12 +0000431__bextr_u64(unsigned long long __X, unsigned long long __Y)
432{
433 return __builtin_ia32_bextr_u64(__X, __Y);
434}
435
Sanjay Patel1585fb92014-05-28 20:26:57 +0000436/* Intel-specified, single-leading-underscore version of BEXTR */
Ekaterina Romanovac8976d52016-03-08 01:36:59 +0000437/// \brief Extracts the specified bits from the first operand and returns them
438/// in the least significant bits of the result.
439///
440/// \headerfile <x86intrin.h>
441///
Ekaterina Romanova0c1c3bb2016-12-09 18:35:50 +0000442/// This intrinsic corresponds to the <c> BEXTR </c> instruction.
Ekaterina Romanovac8976d52016-03-08 01:36:59 +0000443///
444/// \param __X
445/// An unsigned 64-bit integer whose bits are to be extracted.
446/// \param __Y
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.
449/// \param __Z
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
453/// extracted bits.
Michael Kupersteine45af542015-06-30 13:36:19 +0000454static __inline__ unsigned long long __DEFAULT_FN_ATTRS
Sanjay Patel1585fb92014-05-28 20:26:57 +0000455_bextr_u64(unsigned long long __X, unsigned int __Y, unsigned int __Z)
456{
457 return __builtin_ia32_bextr_u64 (__X, ((__Y & 0xff) | ((__Z & 0xff) << 8)));
458}
459
Ekaterina Romanovac8976d52016-03-08 01:36:59 +0000460/// \brief Clears all bits in the source except for the least significant bit
461/// containing a value of 1 and returns the result.
462///
463/// \headerfile <x86intrin.h>
464///
Ekaterina Romanova0c1c3bb2016-12-09 18:35:50 +0000465/// This intrinsic corresponds to the <c> BLSI </c> instruction.
Ekaterina Romanovac8976d52016-03-08 01:36:59 +0000466///
467/// \param __X
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.
Michael Kupersteine45af542015-06-30 13:36:19 +0000471static __inline__ unsigned long long __DEFAULT_FN_ATTRS
Craig Toppera06d4a12011-12-25 07:27:12 +0000472__blsi_u64(unsigned long long __X)
473{
474 return __X & -__X;
475}
476
Ekaterina Romanovac8976d52016-03-08 01:36:59 +0000477/// \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.
480///
481/// \headerfile <x86intrin.h>
482///
Ekaterina Romanova0c1c3bb2016-12-09 18:35:50 +0000483/// This intrinsic corresponds to the <c> BLSMSK </c> instruction.
Ekaterina Romanovac8976d52016-03-08 01:36:59 +0000484///
485/// \param __X
486/// An unsigned 64-bit integer used to create the mask.
487/// \returns A unsigned 64-bit integer containing the newly created mask.
Michael Kupersteine45af542015-06-30 13:36:19 +0000488static __inline__ unsigned long long __DEFAULT_FN_ATTRS
Craig Toppera06d4a12011-12-25 07:27:12 +0000489__blsmsk_u64(unsigned long long __X)
490{
491 return __X ^ (__X - 1);
492}
493
Ekaterina Romanovac8976d52016-03-08 01:36:59 +0000494/// \brief Clears the least siginificant bit that is set to 1 in the source
495/// operand and returns the result.
496///
497/// \headerfile <x86intrin.h>
498///
Ekaterina Romanova0c1c3bb2016-12-09 18:35:50 +0000499/// This intrinsic corresponds to the <c> BLSR </c> instruction.
Ekaterina Romanovac8976d52016-03-08 01:36:59 +0000500///
501/// \param __X
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
504/// source operand.
Michael Kupersteine45af542015-06-30 13:36:19 +0000505static __inline__ unsigned long long __DEFAULT_FN_ATTRS
Craig Toppera06d4a12011-12-25 07:27:12 +0000506__blsr_u64(unsigned long long __X)
507{
508 return __X & (__X - 1);
509}
510
Ekaterina Romanovac8976d52016-03-08 01:36:59 +0000511/// \brief Counts the number of trailing zero bits in the operand.
512///
513/// \headerfile <x86intrin.h>
514///
Ekaterina Romanova0c1c3bb2016-12-09 18:35:50 +0000515/// This intrinsic corresponds to the <c> TZCNT </c> instruction.
Ekaterina Romanovac8976d52016-03-08 01:36:59 +0000516///
517/// \param __X
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.
Hans Wennborg1acf9552015-11-17 18:46:48 +0000521static __inline__ unsigned long long __RELAXED_FN_ATTRS
Craig Topper6490bdc2012-07-02 06:52:51 +0000522__tzcnt_u64(unsigned long long __X)
Craig Topperf2855ad2011-12-25 06:25:37 +0000523{
Craig Toppera52e0d72014-11-01 22:50:54 +0000524 return __X ? __builtin_ctzll(__X) : 64;
Craig Topperf2855ad2011-12-25 06:25:37 +0000525}
Sanjay Patel1585fb92014-05-28 20:26:57 +0000526
Michael Zuckerman716859a2016-06-22 12:32:43 +0000527/// \brief Counts the number of trailing zero bits in the operand.
528///
529/// \headerfile <x86intrin.h>
530///
Ekaterina Romanova0c1c3bb2016-12-09 18:35:50 +0000531/// This intrinsic corresponds to the <c> TZCNT </c> instruction.
Michael Zuckerman716859a2016-06-22 12:32:43 +0000532///
533/// \param __X
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
536/// bits in the operand.
537static __inline__ long long __RELAXED_FN_ATTRS
538_mm_tzcnt_64(unsigned long long __X)
539{
540 return __X ? __builtin_ctzll(__X) : 64;
541}
542
Sanjay Patel1585fb92014-05-28 20:26:57 +0000543#endif /* __x86_64__ */
Craig Topperf2855ad2011-12-25 06:25:37 +0000544
Michael Kupersteine45af542015-06-30 13:36:19 +0000545#undef __DEFAULT_FN_ATTRS
Hans Wennborg1acf9552015-11-17 18:46:48 +0000546#undef __RELAXED_FN_ATTRS
Eric Christopher4d1851682015-06-17 07:09:20 +0000547
Craig Topperf2855ad2011-12-25 06:25:37 +0000548#endif /* __BMIINTRIN_H */