1*67e74705SXin Li /*===---- bmiintrin.h - BMI intrinsics -------------------------------------===
2*67e74705SXin Li *
3*67e74705SXin Li * Permission is hereby granted, free of charge, to any person obtaining a copy
4*67e74705SXin Li * of this software and associated documentation files (the "Software"), to deal
5*67e74705SXin Li * in the Software without restriction, including without limitation the rights
6*67e74705SXin Li * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7*67e74705SXin Li * copies of the Software, and to permit persons to whom the Software is
8*67e74705SXin Li * furnished to do so, subject to the following conditions:
9*67e74705SXin Li *
10*67e74705SXin Li * The above copyright notice and this permission notice shall be included in
11*67e74705SXin Li * all copies or substantial portions of the Software.
12*67e74705SXin Li *
13*67e74705SXin Li * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14*67e74705SXin Li * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15*67e74705SXin Li * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16*67e74705SXin Li * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17*67e74705SXin Li * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18*67e74705SXin Li * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19*67e74705SXin Li * THE SOFTWARE.
20*67e74705SXin Li *
21*67e74705SXin Li *===-----------------------------------------------------------------------===
22*67e74705SXin Li */
23*67e74705SXin Li
24*67e74705SXin Li #if !defined __X86INTRIN_H && !defined __IMMINTRIN_H
25*67e74705SXin Li #error "Never use <bmiintrin.h> directly; include <x86intrin.h> instead."
26*67e74705SXin Li #endif
27*67e74705SXin Li
28*67e74705SXin Li #ifndef __BMIINTRIN_H
29*67e74705SXin Li #define __BMIINTRIN_H
30*67e74705SXin Li
31*67e74705SXin Li /// \brief Counts the number of trailing zero bits in the operand.
32*67e74705SXin Li ///
33*67e74705SXin Li /// \headerfile <x86intrin.h>
34*67e74705SXin Li ///
35*67e74705SXin Li /// \code
36*67e74705SXin Li /// unsigned short _tzcnt_u16(unsigned short a);
37*67e74705SXin Li /// \endcode
38*67e74705SXin Li ///
39*67e74705SXin Li /// This intrinsic corresponds to the \c TZCNT instruction.
40*67e74705SXin Li ///
41*67e74705SXin Li /// \param a
42*67e74705SXin Li /// An unsigned 16-bit integer whose trailing zeros are to be counted.
43*67e74705SXin Li /// \returns An unsigned 16-bit integer containing the number of trailing zero
44*67e74705SXin Li /// bits in the operand.
45*67e74705SXin Li #define _tzcnt_u16(a) (__tzcnt_u16((a)))
46*67e74705SXin Li
47*67e74705SXin Li /// \brief Performs a bitwise AND of the second operand with the one's
48*67e74705SXin Li /// complement of the first operand.
49*67e74705SXin Li ///
50*67e74705SXin Li /// \headerfile <x86intrin.h>
51*67e74705SXin Li ///
52*67e74705SXin Li /// \code
53*67e74705SXin Li /// unsigned int _andn_u32(unsigned int a, unsigned int b);
54*67e74705SXin Li /// \endcode
55*67e74705SXin Li ///
56*67e74705SXin Li /// This intrinsic corresponds to the \c ANDN instruction.
57*67e74705SXin Li ///
58*67e74705SXin Li /// \param a
59*67e74705SXin Li /// An unsigned integer containing one of the operands.
60*67e74705SXin Li /// \param b
61*67e74705SXin Li /// An unsigned integer containing one of the operands.
62*67e74705SXin Li /// \returns An unsigned integer containing the bitwise AND of the second
63*67e74705SXin Li /// operand with the one's complement of the first operand.
64*67e74705SXin Li #define _andn_u32(a, b) (__andn_u32((a), (b)))
65*67e74705SXin Li
66*67e74705SXin Li /* _bextr_u32 != __bextr_u32 */
67*67e74705SXin Li /// \brief Clears all bits in the source except for the least significant bit
68*67e74705SXin Li /// containing a value of 1 and returns the result.
69*67e74705SXin Li ///
70*67e74705SXin Li /// \headerfile <x86intrin.h>
71*67e74705SXin Li ///
72*67e74705SXin Li /// \code
73*67e74705SXin Li /// unsigned int _blsi_u32(unsigned int a);
74*67e74705SXin Li /// \endcode
75*67e74705SXin Li ///
76*67e74705SXin Li /// This intrinsic corresponds to the \c BLSI instruction.
77*67e74705SXin Li ///
78*67e74705SXin Li /// \param a
79*67e74705SXin Li /// An unsigned integer whose bits are to be cleared.
80*67e74705SXin Li /// \returns An unsigned integer containing the result of clearing the bits from
81*67e74705SXin Li /// the source operand.
82*67e74705SXin Li #define _blsi_u32(a) (__blsi_u32((a)))
83*67e74705SXin Li
84*67e74705SXin Li /// \brief Creates a mask whose bits are set to 1, using bit 0 up to and
85*67e74705SXin Li /// including the least siginificant bit that is set to 1 in the source
86*67e74705SXin Li /// operand and returns the result.
87*67e74705SXin Li ///
88*67e74705SXin Li /// \headerfile <x86intrin.h>
89*67e74705SXin Li ///
90*67e74705SXin Li /// \code
91*67e74705SXin Li /// unsigned int _blsmsk_u32(unsigned int a);
92*67e74705SXin Li /// \endcode
93*67e74705SXin Li ///
94*67e74705SXin Li /// This intrinsic corresponds to the \c BLSMSK instruction.
95*67e74705SXin Li ///
96*67e74705SXin Li /// \param a
97*67e74705SXin Li /// An unsigned integer used to create the mask.
98*67e74705SXin Li /// \returns An unsigned integer containing the newly created mask.
99*67e74705SXin Li #define _blsmsk_u32(a) (__blsmsk_u32((a)))
100*67e74705SXin Li
101*67e74705SXin Li /// \brief Clears the least siginificant bit that is set to 1 in the source
102*67e74705SXin Li /// operand and returns the result.
103*67e74705SXin Li ///
104*67e74705SXin Li /// \headerfile <x86intrin.h>
105*67e74705SXin Li ///
106*67e74705SXin Li /// \code
107*67e74705SXin Li /// unsigned int _blsr_u32(unsigned int a);
108*67e74705SXin Li /// \endcode
109*67e74705SXin Li ///
110*67e74705SXin Li /// This intrinsic corresponds to the \c BLSR instruction.
111*67e74705SXin Li ///
112*67e74705SXin Li /// \param a
113*67e74705SXin Li /// An unsigned integer containing the operand to be cleared.
114*67e74705SXin Li /// \returns An unsigned integer containing the result of clearing the source
115*67e74705SXin Li /// operand.
116*67e74705SXin Li #define _blsr_u32(a) (__blsr_u32((a)))
117*67e74705SXin Li
118*67e74705SXin Li /// \brief Counts the number of trailing zero bits in the operand.
119*67e74705SXin Li ///
120*67e74705SXin Li /// \headerfile <x86intrin.h>
121*67e74705SXin Li ///
122*67e74705SXin Li /// \code
123*67e74705SXin Li /// unsigned int _tzcnt_u32(unsigned int a);
124*67e74705SXin Li /// \endcode
125*67e74705SXin Li ///
126*67e74705SXin Li /// This intrinsic corresponds to the \c TZCNT instruction.
127*67e74705SXin Li ///
128*67e74705SXin Li /// \param a
129*67e74705SXin Li /// An unsigned 32-bit integer whose trailing zeros are to be counted.
130*67e74705SXin Li /// \returns An unsigned 32-bit integer containing the number of trailing zero
131*67e74705SXin Li /// bits in the operand.
132*67e74705SXin Li #define _tzcnt_u32(a) (__tzcnt_u32((a)))
133*67e74705SXin Li
134*67e74705SXin Li /* Define the default attributes for the functions in this file. */
135*67e74705SXin Li #define __DEFAULT_FN_ATTRS __attribute__((__always_inline__, __nodebug__, __target__("bmi")))
136*67e74705SXin Li
137*67e74705SXin Li /* Allow using the tzcnt intrinsics even for non-BMI targets. Since the TZCNT
138*67e74705SXin Li instruction behaves as BSF on non-BMI targets, there is code that expects
139*67e74705SXin Li to use it as a potentially faster version of BSF. */
140*67e74705SXin Li #define __RELAXED_FN_ATTRS __attribute__((__always_inline__, __nodebug__))
141*67e74705SXin Li
142*67e74705SXin Li /// \brief Counts the number of trailing zero bits in the operand.
143*67e74705SXin Li ///
144*67e74705SXin Li /// \headerfile <x86intrin.h>
145*67e74705SXin Li ///
146*67e74705SXin Li /// This intrinsic corresponds to the \c TZCNT instruction.
147*67e74705SXin Li ///
148*67e74705SXin Li /// \param __X
149*67e74705SXin Li /// An unsigned 16-bit integer whose trailing zeros are to be counted.
150*67e74705SXin Li /// \returns An unsigned 16-bit integer containing the number of trailing zero
151*67e74705SXin Li /// bits in the operand.
152*67e74705SXin Li static __inline__ unsigned short __RELAXED_FN_ATTRS
__tzcnt_u16(unsigned short __X)153*67e74705SXin Li __tzcnt_u16(unsigned short __X)
154*67e74705SXin Li {
155*67e74705SXin Li return __X ? __builtin_ctzs(__X) : 16;
156*67e74705SXin Li }
157*67e74705SXin Li
158*67e74705SXin Li /// \brief Performs a bitwise AND of the second operand with the one's
159*67e74705SXin Li /// complement of the first operand.
160*67e74705SXin Li ///
161*67e74705SXin Li /// \headerfile <x86intrin.h>
162*67e74705SXin Li ///
163*67e74705SXin Li /// This intrinsic corresponds to the \c ANDN instruction.
164*67e74705SXin Li ///
165*67e74705SXin Li /// \param __X
166*67e74705SXin Li /// An unsigned integer containing one of the operands.
167*67e74705SXin Li /// \param __Y
168*67e74705SXin Li /// An unsigned integer containing one of the operands.
169*67e74705SXin Li /// \returns An unsigned integer containing the bitwise AND of the second
170*67e74705SXin Li /// operand with the one's complement of the first operand.
171*67e74705SXin Li static __inline__ unsigned int __DEFAULT_FN_ATTRS
__andn_u32(unsigned int __X,unsigned int __Y)172*67e74705SXin Li __andn_u32(unsigned int __X, unsigned int __Y)
173*67e74705SXin Li {
174*67e74705SXin Li return ~__X & __Y;
175*67e74705SXin Li }
176*67e74705SXin Li
177*67e74705SXin Li /* AMD-specified, double-leading-underscore version of BEXTR */
178*67e74705SXin Li /// \brief Extracts the specified bits from the first operand and returns them
179*67e74705SXin Li /// in the least significant bits of the result.
180*67e74705SXin Li ///
181*67e74705SXin Li /// \headerfile <x86intrin.h>
182*67e74705SXin Li ///
183*67e74705SXin Li /// This intrinsic corresponds to the \c BEXTR instruction.
184*67e74705SXin Li ///
185*67e74705SXin Li /// \param __X
186*67e74705SXin Li /// An unsigned integer whose bits are to be extracted.
187*67e74705SXin Li /// \param __Y
188*67e74705SXin Li /// An unsigned integer used to specify which bits are extracted. Bits [7:0]
189*67e74705SXin Li /// specify the index of the least significant bit. Bits [15:8] specify the
190*67e74705SXin Li /// number of bits to be extracted.
191*67e74705SXin Li /// \returns An unsigned integer whose least significant bits contain the
192*67e74705SXin Li /// extracted bits.
193*67e74705SXin Li static __inline__ unsigned int __DEFAULT_FN_ATTRS
__bextr_u32(unsigned int __X,unsigned int __Y)194*67e74705SXin Li __bextr_u32(unsigned int __X, unsigned int __Y)
195*67e74705SXin Li {
196*67e74705SXin Li return __builtin_ia32_bextr_u32(__X, __Y);
197*67e74705SXin Li }
198*67e74705SXin Li
199*67e74705SXin Li /* Intel-specified, single-leading-underscore version of BEXTR */
200*67e74705SXin Li /// \brief Extracts the specified bits from the first operand and returns them
201*67e74705SXin Li /// in the least significant bits of the result.
202*67e74705SXin Li ///
203*67e74705SXin Li /// \headerfile <x86intrin.h>
204*67e74705SXin Li ///
205*67e74705SXin Li /// This intrinsic corresponds to the \c BEXTR instruction.
206*67e74705SXin Li ///
207*67e74705SXin Li /// \param __X
208*67e74705SXin Li /// An unsigned integer whose bits are to be extracted.
209*67e74705SXin Li /// \param __Y
210*67e74705SXin Li /// An unsigned integer used to specify the index of the least significant
211*67e74705SXin Li /// bit for the bits to be extracted. Bits [7:0] specify the index.
212*67e74705SXin Li /// \param __Z
213*67e74705SXin Li /// An unsigned integer used to specify the number of bits to be extracted.
214*67e74705SXin Li /// Bits [7:0] specify the number of bits.
215*67e74705SXin Li /// \returns An unsigned integer whose least significant bits contain the
216*67e74705SXin Li /// extracted bits.
217*67e74705SXin Li static __inline__ unsigned int __DEFAULT_FN_ATTRS
_bextr_u32(unsigned int __X,unsigned int __Y,unsigned int __Z)218*67e74705SXin Li _bextr_u32(unsigned int __X, unsigned int __Y, unsigned int __Z)
219*67e74705SXin Li {
220*67e74705SXin Li return __builtin_ia32_bextr_u32 (__X, ((__Y & 0xff) | ((__Z & 0xff) << 8)));
221*67e74705SXin Li }
222*67e74705SXin Li
223*67e74705SXin Li /// \brief Clears all bits in the source except for the least significant bit
224*67e74705SXin Li /// containing a value of 1 and returns the result.
225*67e74705SXin Li ///
226*67e74705SXin Li /// \headerfile <x86intrin.h>
227*67e74705SXin Li ///
228*67e74705SXin Li /// This intrinsic corresponds to the \c BLSI instruction.
229*67e74705SXin Li ///
230*67e74705SXin Li /// \param __X
231*67e74705SXin Li /// An unsigned integer whose bits are to be cleared.
232*67e74705SXin Li /// \returns An unsigned integer containing the result of clearing the bits from
233*67e74705SXin Li /// the source operand.
234*67e74705SXin Li static __inline__ unsigned int __DEFAULT_FN_ATTRS
__blsi_u32(unsigned int __X)235*67e74705SXin Li __blsi_u32(unsigned int __X)
236*67e74705SXin Li {
237*67e74705SXin Li return __X & -__X;
238*67e74705SXin Li }
239*67e74705SXin Li
240*67e74705SXin Li /// \brief Creates a mask whose bits are set to 1, using bit 0 up to and
241*67e74705SXin Li /// including the least siginificant bit that is set to 1 in the source
242*67e74705SXin Li /// operand and returns the result.
243*67e74705SXin Li ///
244*67e74705SXin Li /// \headerfile <x86intrin.h>
245*67e74705SXin Li ///
246*67e74705SXin Li /// This intrinsic corresponds to the \c BLSMSK instruction.
247*67e74705SXin Li ///
248*67e74705SXin Li /// \param __X
249*67e74705SXin Li /// An unsigned integer used to create the mask.
250*67e74705SXin Li /// \returns An unsigned integer containing the newly created mask.
251*67e74705SXin Li static __inline__ unsigned int __DEFAULT_FN_ATTRS
__blsmsk_u32(unsigned int __X)252*67e74705SXin Li __blsmsk_u32(unsigned int __X)
253*67e74705SXin Li {
254*67e74705SXin Li return __X ^ (__X - 1);
255*67e74705SXin Li }
256*67e74705SXin Li
257*67e74705SXin Li /// \brief Clears the least siginificant bit that is set to 1 in the source
258*67e74705SXin Li /// operand and returns the result.
259*67e74705SXin Li ///
260*67e74705SXin Li /// \headerfile <x86intrin.h>
261*67e74705SXin Li ///
262*67e74705SXin Li /// This intrinsic corresponds to the \c BLSR instruction.
263*67e74705SXin Li ///
264*67e74705SXin Li /// \param __X
265*67e74705SXin Li /// An unsigned integer containing the operand to be cleared.
266*67e74705SXin Li /// \returns An unsigned integer containing the result of clearing the source
267*67e74705SXin Li /// operand.
268*67e74705SXin Li static __inline__ unsigned int __DEFAULT_FN_ATTRS
__blsr_u32(unsigned int __X)269*67e74705SXin Li __blsr_u32(unsigned int __X)
270*67e74705SXin Li {
271*67e74705SXin Li return __X & (__X - 1);
272*67e74705SXin Li }
273*67e74705SXin Li
274*67e74705SXin Li /// \brief Counts the number of trailing zero bits in the operand.
275*67e74705SXin Li ///
276*67e74705SXin Li /// \headerfile <x86intrin.h>
277*67e74705SXin Li ///
278*67e74705SXin Li /// This intrinsic corresponds to the \c TZCNT instruction.
279*67e74705SXin Li ///
280*67e74705SXin Li /// \param __X
281*67e74705SXin Li /// An unsigned 32-bit integer whose trailing zeros are to be counted.
282*67e74705SXin Li /// \returns An unsigned 32-bit integer containing the number of trailing zero
283*67e74705SXin Li /// bits in the operand.
284*67e74705SXin Li static __inline__ unsigned int __RELAXED_FN_ATTRS
__tzcnt_u32(unsigned int __X)285*67e74705SXin Li __tzcnt_u32(unsigned int __X)
286*67e74705SXin Li {
287*67e74705SXin Li return __X ? __builtin_ctz(__X) : 32;
288*67e74705SXin Li }
289*67e74705SXin Li
290*67e74705SXin Li /// \brief Counts the number of trailing zero bits in the operand.
291*67e74705SXin Li ///
292*67e74705SXin Li /// \headerfile <x86intrin.h>
293*67e74705SXin Li ///
294*67e74705SXin Li /// This intrinsic corresponds to the \c TZCNT instruction.
295*67e74705SXin Li ///
296*67e74705SXin Li /// \param __X
297*67e74705SXin Li /// An unsigned 32-bit integer whose trailing zeros are to be counted.
298*67e74705SXin Li /// \returns An 32-bit integer containing the number of trailing zero
299*67e74705SXin Li /// bits in the operand.
300*67e74705SXin Li static __inline__ int __RELAXED_FN_ATTRS
_mm_tzcnt_32(unsigned int __X)301*67e74705SXin Li _mm_tzcnt_32(unsigned int __X)
302*67e74705SXin Li {
303*67e74705SXin Li return __X ? __builtin_ctz(__X) : 32;
304*67e74705SXin Li }
305*67e74705SXin Li
306*67e74705SXin Li #ifdef __x86_64__
307*67e74705SXin Li
308*67e74705SXin Li /// \brief Performs a bitwise AND of the second operand with the one's
309*67e74705SXin Li /// complement of the first operand.
310*67e74705SXin Li ///
311*67e74705SXin Li /// \headerfile <x86intrin.h>
312*67e74705SXin Li ///
313*67e74705SXin Li /// \code
314*67e74705SXin Li /// unsigned long long _andn_u64 (unsigned long long a, unsigned long long b);
315*67e74705SXin Li /// \endcode
316*67e74705SXin Li ///
317*67e74705SXin Li /// This intrinsic corresponds to the \c ANDN instruction.
318*67e74705SXin Li ///
319*67e74705SXin Li /// \param a
320*67e74705SXin Li /// An unsigned 64-bit integer containing one of the operands.
321*67e74705SXin Li /// \param b
322*67e74705SXin Li /// An unsigned 64-bit integer containing one of the operands.
323*67e74705SXin Li /// \returns An unsigned 64-bit integer containing the bitwise AND of the second
324*67e74705SXin Li /// operand with the one's complement of the first operand.
325*67e74705SXin Li #define _andn_u64(a, b) (__andn_u64((a), (b)))
326*67e74705SXin Li
327*67e74705SXin Li /* _bextr_u64 != __bextr_u64 */
328*67e74705SXin Li /// \brief Clears all bits in the source except for the least significant bit
329*67e74705SXin Li /// containing a value of 1 and returns the result.
330*67e74705SXin Li ///
331*67e74705SXin Li /// \headerfile <x86intrin.h>
332*67e74705SXin Li ///
333*67e74705SXin Li /// \code
334*67e74705SXin Li /// unsigned long long _blsi_u64(unsigned long long a);
335*67e74705SXin Li /// \endcode
336*67e74705SXin Li ///
337*67e74705SXin Li /// This intrinsic corresponds to the \c BLSI instruction.
338*67e74705SXin Li ///
339*67e74705SXin Li /// \param a
340*67e74705SXin Li /// An unsigned 64-bit integer whose bits are to be cleared.
341*67e74705SXin Li /// \returns An unsigned 64-bit integer containing the result of clearing the
342*67e74705SXin Li /// bits from the source operand.
343*67e74705SXin Li #define _blsi_u64(a) (__blsi_u64((a)))
344*67e74705SXin Li
345*67e74705SXin Li /// \brief Creates a mask whose bits are set to 1, using bit 0 up to and
346*67e74705SXin Li /// including the least siginificant bit that is set to 1 in the source
347*67e74705SXin Li /// operand and returns the result.
348*67e74705SXin Li ///
349*67e74705SXin Li /// \headerfile <x86intrin.h>
350*67e74705SXin Li ///
351*67e74705SXin Li /// \code
352*67e74705SXin Li /// unsigned long long _blsmsk_u64(unsigned long long a);
353*67e74705SXin Li /// \endcode
354*67e74705SXin Li ///
355*67e74705SXin Li /// This intrinsic corresponds to the \c BLSMSK instruction.
356*67e74705SXin Li ///
357*67e74705SXin Li /// \param a
358*67e74705SXin Li /// An unsigned 64-bit integer used to create the mask.
359*67e74705SXin Li /// \returns A unsigned 64-bit integer containing the newly created mask.
360*67e74705SXin Li #define _blsmsk_u64(a) (__blsmsk_u64((a)))
361*67e74705SXin Li
362*67e74705SXin Li /// \brief Clears the least siginificant bit that is set to 1 in the source
363*67e74705SXin Li /// operand and returns the result.
364*67e74705SXin Li ///
365*67e74705SXin Li /// \headerfile <x86intrin.h>
366*67e74705SXin Li ///
367*67e74705SXin Li /// \code
368*67e74705SXin Li /// unsigned long long _blsr_u64(unsigned long long a);
369*67e74705SXin Li /// \endcode
370*67e74705SXin Li ///
371*67e74705SXin Li /// This intrinsic corresponds to the \c BLSR instruction.
372*67e74705SXin Li ///
373*67e74705SXin Li /// \param a
374*67e74705SXin Li /// An unsigned 64-bit integer containing the operand to be cleared.
375*67e74705SXin Li /// \returns An unsigned 64-bit integer containing the result of clearing the
376*67e74705SXin Li /// source operand.
377*67e74705SXin Li #define _blsr_u64(a) (__blsr_u64((a)))
378*67e74705SXin Li
379*67e74705SXin Li /// \brief Counts the number of trailing zero bits in the operand.
380*67e74705SXin Li ///
381*67e74705SXin Li /// \headerfile <x86intrin.h>
382*67e74705SXin Li ///
383*67e74705SXin Li /// \code
384*67e74705SXin Li /// unsigned long long _tzcnt_u64(unsigned long long a);
385*67e74705SXin Li /// \endcode
386*67e74705SXin Li ///
387*67e74705SXin Li /// This intrinsic corresponds to the \c TZCNT instruction.
388*67e74705SXin Li ///
389*67e74705SXin Li /// \param a
390*67e74705SXin Li /// An unsigned 64-bit integer whose trailing zeros are to be counted.
391*67e74705SXin Li /// \returns An unsigned 64-bit integer containing the number of trailing zero
392*67e74705SXin Li /// bits in the operand.
393*67e74705SXin Li #define _tzcnt_u64(a) (__tzcnt_u64((a)))
394*67e74705SXin Li
395*67e74705SXin Li /// \brief Performs a bitwise AND of the second operand with the one's
396*67e74705SXin Li /// complement of the first operand.
397*67e74705SXin Li ///
398*67e74705SXin Li /// \headerfile <x86intrin.h>
399*67e74705SXin Li ///
400*67e74705SXin Li /// This intrinsic corresponds to the \c ANDN instruction.
401*67e74705SXin Li ///
402*67e74705SXin Li /// \param __X
403*67e74705SXin Li /// An unsigned 64-bit integer containing one of the operands.
404*67e74705SXin Li /// \param __Y
405*67e74705SXin Li /// An unsigned 64-bit integer containing one of the operands.
406*67e74705SXin Li /// \returns An unsigned 64-bit integer containing the bitwise AND of the second
407*67e74705SXin Li /// operand with the one's complement of the first operand.
408*67e74705SXin Li static __inline__ unsigned long long __DEFAULT_FN_ATTRS
__andn_u64(unsigned long long __X,unsigned long long __Y)409*67e74705SXin Li __andn_u64 (unsigned long long __X, unsigned long long __Y)
410*67e74705SXin Li {
411*67e74705SXin Li return ~__X & __Y;
412*67e74705SXin Li }
413*67e74705SXin Li
414*67e74705SXin Li /* AMD-specified, double-leading-underscore version of BEXTR */
415*67e74705SXin Li /// \brief Extracts the specified bits from the first operand and returns them
416*67e74705SXin Li /// in the least significant bits of the result.
417*67e74705SXin Li ///
418*67e74705SXin Li /// \headerfile <x86intrin.h>
419*67e74705SXin Li ///
420*67e74705SXin Li /// This intrinsic corresponds to the \c BEXTR instruction.
421*67e74705SXin Li ///
422*67e74705SXin Li /// \param __X
423*67e74705SXin Li /// An unsigned 64-bit integer whose bits are to be extracted.
424*67e74705SXin Li /// \param __Y
425*67e74705SXin Li /// An unsigned 64-bit integer used to specify which bits are extracted. Bits
426*67e74705SXin Li /// [7:0] specify the index of the least significant bit. Bits [15:8] specify
427*67e74705SXin Li /// the number of bits to be extracted.
428*67e74705SXin Li /// \returns An unsigned 64-bit integer whose least significant bits contain the
429*67e74705SXin Li /// extracted bits.
430*67e74705SXin Li static __inline__ unsigned long long __DEFAULT_FN_ATTRS
__bextr_u64(unsigned long long __X,unsigned long long __Y)431*67e74705SXin Li __bextr_u64(unsigned long long __X, unsigned long long __Y)
432*67e74705SXin Li {
433*67e74705SXin Li return __builtin_ia32_bextr_u64(__X, __Y);
434*67e74705SXin Li }
435*67e74705SXin Li
436*67e74705SXin Li /* Intel-specified, single-leading-underscore version of BEXTR */
437*67e74705SXin Li /// \brief Extracts the specified bits from the first operand and returns them
438*67e74705SXin Li /// in the least significant bits of the result.
439*67e74705SXin Li ///
440*67e74705SXin Li /// \headerfile <x86intrin.h>
441*67e74705SXin Li ///
442*67e74705SXin Li /// This intrinsic corresponds to the \c BEXTR instruction.
443*67e74705SXin Li ///
444*67e74705SXin Li /// \param __X
445*67e74705SXin Li /// An unsigned 64-bit integer whose bits are to be extracted.
446*67e74705SXin Li /// \param __Y
447*67e74705SXin Li /// An unsigned integer used to specify the index of the least significant
448*67e74705SXin Li /// bit for the bits to be extracted. Bits [7:0] specify the index.
449*67e74705SXin Li /// \param __Z
450*67e74705SXin Li /// An unsigned integer used to specify the number of bits to be extracted.
451*67e74705SXin Li /// Bits [7:0] specify the number of bits.
452*67e74705SXin Li /// \returns An unsigned 64-bit integer whose least significant bits contain the
453*67e74705SXin Li /// extracted bits.
454*67e74705SXin Li static __inline__ unsigned long long __DEFAULT_FN_ATTRS
_bextr_u64(unsigned long long __X,unsigned int __Y,unsigned int __Z)455*67e74705SXin Li _bextr_u64(unsigned long long __X, unsigned int __Y, unsigned int __Z)
456*67e74705SXin Li {
457*67e74705SXin Li return __builtin_ia32_bextr_u64 (__X, ((__Y & 0xff) | ((__Z & 0xff) << 8)));
458*67e74705SXin Li }
459*67e74705SXin Li
460*67e74705SXin Li /// \brief Clears all bits in the source except for the least significant bit
461*67e74705SXin Li /// containing a value of 1 and returns the result.
462*67e74705SXin Li ///
463*67e74705SXin Li /// \headerfile <x86intrin.h>
464*67e74705SXin Li ///
465*67e74705SXin Li /// This intrinsic corresponds to the \c BLSI instruction.
466*67e74705SXin Li ///
467*67e74705SXin Li /// \param __X
468*67e74705SXin Li /// An unsigned 64-bit integer whose bits are to be cleared.
469*67e74705SXin Li /// \returns An unsigned 64-bit integer containing the result of clearing the
470*67e74705SXin Li /// bits from the source operand.
471*67e74705SXin Li static __inline__ unsigned long long __DEFAULT_FN_ATTRS
__blsi_u64(unsigned long long __X)472*67e74705SXin Li __blsi_u64(unsigned long long __X)
473*67e74705SXin Li {
474*67e74705SXin Li return __X & -__X;
475*67e74705SXin Li }
476*67e74705SXin Li
477*67e74705SXin Li /// \brief Creates a mask whose bits are set to 1, using bit 0 up to and
478*67e74705SXin Li /// including the least siginificant bit that is set to 1 in the source
479*67e74705SXin Li /// operand and returns the result.
480*67e74705SXin Li ///
481*67e74705SXin Li /// \headerfile <x86intrin.h>
482*67e74705SXin Li ///
483*67e74705SXin Li /// This intrinsic corresponds to the \c BLSMSK instruction.
484*67e74705SXin Li ///
485*67e74705SXin Li /// \param __X
486*67e74705SXin Li /// An unsigned 64-bit integer used to create the mask.
487*67e74705SXin Li /// \returns A unsigned 64-bit integer containing the newly created mask.
488*67e74705SXin Li static __inline__ unsigned long long __DEFAULT_FN_ATTRS
__blsmsk_u64(unsigned long long __X)489*67e74705SXin Li __blsmsk_u64(unsigned long long __X)
490*67e74705SXin Li {
491*67e74705SXin Li return __X ^ (__X - 1);
492*67e74705SXin Li }
493*67e74705SXin Li
494*67e74705SXin Li /// \brief Clears the least siginificant bit that is set to 1 in the source
495*67e74705SXin Li /// operand and returns the result.
496*67e74705SXin Li ///
497*67e74705SXin Li /// \headerfile <x86intrin.h>
498*67e74705SXin Li ///
499*67e74705SXin Li /// This intrinsic corresponds to the \c BLSR instruction.
500*67e74705SXin Li ///
501*67e74705SXin Li /// \param __X
502*67e74705SXin Li /// An unsigned 64-bit integer containing the operand to be cleared.
503*67e74705SXin Li /// \returns An unsigned 64-bit integer containing the result of clearing the
504*67e74705SXin Li /// source operand.
505*67e74705SXin Li static __inline__ unsigned long long __DEFAULT_FN_ATTRS
__blsr_u64(unsigned long long __X)506*67e74705SXin Li __blsr_u64(unsigned long long __X)
507*67e74705SXin Li {
508*67e74705SXin Li return __X & (__X - 1);
509*67e74705SXin Li }
510*67e74705SXin Li
511*67e74705SXin Li /// \brief Counts the number of trailing zero bits in the operand.
512*67e74705SXin Li ///
513*67e74705SXin Li /// \headerfile <x86intrin.h>
514*67e74705SXin Li ///
515*67e74705SXin Li /// This intrinsic corresponds to the \c TZCNT instruction.
516*67e74705SXin Li ///
517*67e74705SXin Li /// \param __X
518*67e74705SXin Li /// An unsigned 64-bit integer whose trailing zeros are to be counted.
519*67e74705SXin Li /// \returns An unsigned 64-bit integer containing the number of trailing zero
520*67e74705SXin Li /// bits in the operand.
521*67e74705SXin Li static __inline__ unsigned long long __RELAXED_FN_ATTRS
__tzcnt_u64(unsigned long long __X)522*67e74705SXin Li __tzcnt_u64(unsigned long long __X)
523*67e74705SXin Li {
524*67e74705SXin Li return __X ? __builtin_ctzll(__X) : 64;
525*67e74705SXin Li }
526*67e74705SXin Li
527*67e74705SXin Li /// \brief Counts the number of trailing zero bits in the operand.
528*67e74705SXin Li ///
529*67e74705SXin Li /// \headerfile <x86intrin.h>
530*67e74705SXin Li ///
531*67e74705SXin Li /// This intrinsic corresponds to the \c TZCNT instruction.
532*67e74705SXin Li ///
533*67e74705SXin Li /// \param __X
534*67e74705SXin Li /// An unsigned 64-bit integer whose trailing zeros are to be counted.
535*67e74705SXin Li /// \returns An 64-bit integer containing the number of trailing zero
536*67e74705SXin Li /// bits in the operand.
537*67e74705SXin Li static __inline__ long long __RELAXED_FN_ATTRS
_mm_tzcnt_64(unsigned long long __X)538*67e74705SXin Li _mm_tzcnt_64(unsigned long long __X)
539*67e74705SXin Li {
540*67e74705SXin Li return __X ? __builtin_ctzll(__X) : 64;
541*67e74705SXin Li }
542*67e74705SXin Li
543*67e74705SXin Li #endif /* __x86_64__ */
544*67e74705SXin Li
545*67e74705SXin Li #undef __DEFAULT_FN_ATTRS
546*67e74705SXin Li #undef __RELAXED_FN_ATTRS
547*67e74705SXin Li
548*67e74705SXin Li #endif /* __BMIINTRIN_H */
549