1 /*===---- bmi2intrin.h - BMI2 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 *===-----------------------------------------------------------------------===
11 #error "Never use <bmi2intrin.h> directly; include <immintrin.h> instead."
14 #ifndef __BMI2INTRIN_H
15 #define __BMI2INTRIN_H
17 /* Define the default attributes for the functions in this file. */
18 #if defined(__cplusplus) && (__cplusplus >= 201103L)
19 #define __DEFAULT_FN_ATTRS \
20 __attribute__((__always_inline__, __nodebug__, __target__("bmi2"))) constexpr
22 #define __DEFAULT_FN_ATTRS \
23 __attribute__((__always_inline__, __nodebug__, __target__("bmi2")))
26 /// Copies the unsigned 32-bit integer \a __X and zeroes the upper bits
27 /// starting at bit number \a __Y.
37 /// \headerfile <immintrin.h>
39 /// This intrinsic corresponds to the \c BZHI instruction.
42 /// The 32-bit source value to copy.
44 /// The lower 8 bits specify the bit number of the lowest bit to zero.
45 /// \returns The partially zeroed 32-bit value.
46 static __inline__
unsigned int __DEFAULT_FN_ATTRS
47 _bzhi_u32(unsigned int __X
, unsigned int __Y
) {
48 return __builtin_ia32_bzhi_si(__X
, __Y
);
51 /// Deposit (scatter) low-order bits from the unsigned 32-bit integer \a __X
52 /// into the 32-bit result, according to the mask in the unsigned 32-bit
53 /// integer \a __Y. All other bits of the result are zero.
60 /// result[m] := __X[i]
66 /// \headerfile <immintrin.h>
68 /// This intrinsic corresponds to the \c PDEP instruction.
71 /// The 32-bit source value to copy.
73 /// The 32-bit mask specifying where to deposit source bits.
74 /// \returns The 32-bit result.
75 static __inline__
unsigned int __DEFAULT_FN_ATTRS
76 _pdep_u32(unsigned int __X
, unsigned int __Y
) {
77 return __builtin_ia32_pdep_si(__X
, __Y
);
80 /// Extract (gather) bits from the unsigned 32-bit integer \a __X into the
81 /// low-order bits of the 32-bit result, according to the mask in the
82 /// unsigned 32-bit integer \a __Y. All other bits of the result are zero.
89 /// result[i] := __X[m]
95 /// \headerfile <immintrin.h>
97 /// This intrinsic corresponds to the \c PEXT instruction.
100 /// The 32-bit source value to copy.
102 /// The 32-bit mask specifying which source bits to extract.
103 /// \returns The 32-bit result.
104 static __inline__
unsigned int __DEFAULT_FN_ATTRS
105 _pext_u32(unsigned int __X
, unsigned int __Y
) {
106 return __builtin_ia32_pext_si(__X
, __Y
);
109 /// Multiplies the unsigned 32-bit integers \a __X and \a __Y to form a
110 /// 64-bit product. Stores the upper 32 bits of the product in the
111 /// memory at \a __P and returns the lower 32 bits.
113 /// \code{.operation}
114 /// Store32(__P, (__X * __Y)[63:32])
115 /// result := (__X * __Y)[31:0]
118 /// \headerfile <immintrin.h>
120 /// This intrinsic corresponds to the \c MULX instruction.
123 /// An unsigned 32-bit multiplicand.
125 /// An unsigned 32-bit multiplicand.
127 /// A pointer to memory for storing the upper half of the product.
128 /// \returns The lower half of the product.
129 static __inline__
unsigned int __DEFAULT_FN_ATTRS
130 _mulx_u32(unsigned int __X
, unsigned int __Y
, unsigned int *__P
) {
131 unsigned long long __res
= (unsigned long long) __X
* __Y
;
132 *__P
= (unsigned int)(__res
>> 32);
133 return (unsigned int)__res
;
138 /// Copies the unsigned 64-bit integer \a __X and zeroes the upper bits
139 /// starting at bit number \a __Y.
141 /// \code{.operation}
145 /// result[63:i] := 0
149 /// \headerfile <immintrin.h>
151 /// This intrinsic corresponds to the \c BZHI instruction.
154 /// The 64-bit source value to copy.
156 /// The lower 8 bits specify the bit number of the lowest bit to zero.
157 /// \returns The partially zeroed 64-bit value.
158 static __inline__
unsigned long long __DEFAULT_FN_ATTRS
159 _bzhi_u64(unsigned long long __X
, unsigned long long __Y
) {
160 return __builtin_ia32_bzhi_di(__X
, __Y
);
163 /// Deposit (scatter) low-order bits from the unsigned 64-bit integer \a __X
164 /// into the 64-bit result, according to the mask in the unsigned 64-bit
165 /// integer \a __Y. All other bits of the result are zero.
167 /// \code{.operation}
172 /// result[m] := __X[i]
178 /// \headerfile <immintrin.h>
180 /// This intrinsic corresponds to the \c PDEP instruction.
183 /// The 64-bit source value to copy.
185 /// The 64-bit mask specifying where to deposit source bits.
186 /// \returns The 64-bit result.
187 static __inline__
unsigned long long __DEFAULT_FN_ATTRS
188 _pdep_u64(unsigned long long __X
, unsigned long long __Y
) {
189 return __builtin_ia32_pdep_di(__X
, __Y
);
192 /// Extract (gather) bits from the unsigned 64-bit integer \a __X into the
193 /// low-order bits of the 64-bit result, according to the mask in the
194 /// unsigned 64-bit integer \a __Y. All other bits of the result are zero.
196 /// \code{.operation}
201 /// result[i] := __X[m]
207 /// \headerfile <immintrin.h>
209 /// This intrinsic corresponds to the \c PEXT instruction.
212 /// The 64-bit source value to copy.
214 /// The 64-bit mask specifying which source bits to extract.
215 /// \returns The 64-bit result.
216 static __inline__
unsigned long long __DEFAULT_FN_ATTRS
217 _pext_u64(unsigned long long __X
, unsigned long long __Y
) {
218 return __builtin_ia32_pext_di(__X
, __Y
);
221 /// Multiplies the unsigned 64-bit integers \a __X and \a __Y to form a
222 /// 128-bit product. Stores the upper 64 bits of the product to the
223 /// memory addressed by \a __P and returns the lower 64 bits.
225 /// \code{.operation}
226 /// Store64(__P, (__X * __Y)[127:64])
227 /// result := (__X * __Y)[63:0]
230 /// \headerfile <immintrin.h>
232 /// This intrinsic corresponds to the \c MULX instruction.
235 /// An unsigned 64-bit multiplicand.
237 /// An unsigned 64-bit multiplicand.
239 /// A pointer to memory for storing the upper half of the product.
240 /// \returns The lower half of the product.
241 static __inline__
unsigned long long __DEFAULT_FN_ATTRS
242 _mulx_u64 (unsigned long long __X
, unsigned long long __Y
,
243 unsigned long long *__P
) {
244 unsigned __int128 __res
= (unsigned __int128
) __X
* __Y
;
245 *__P
= (unsigned long long) (__res
>> 64);
246 return (unsigned long long) __res
;
249 #endif /* __x86_64__ */
251 #undef __DEFAULT_FN_ATTRS
253 #endif /* __BMI2INTRIN_H */