1 /* ===-------- ia32intrin.h ---------------------------------------------------===
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 <ia32intrin.h> directly; include <x86intrin.h> instead."
14 #ifndef __IA32INTRIN_H
15 #define __IA32INTRIN_H
17 /* Define the default attributes for the functions in this file. */
18 #define __DEFAULT_FN_ATTRS __attribute__((__always_inline__, __nodebug__))
19 #define __DEFAULT_FN_ATTRS_CRC32 __attribute__((__always_inline__, __nodebug__, __target__("crc32")))
21 #if defined(__cplusplus) && (__cplusplus >= 201103L)
22 #define __DEFAULT_FN_ATTRS_CAST __attribute__((__always_inline__)) constexpr
23 #define __DEFAULT_FN_ATTRS_CONSTEXPR __DEFAULT_FN_ATTRS constexpr
25 #define __DEFAULT_FN_ATTRS_CAST __attribute__((__always_inline__))
26 #define __DEFAULT_FN_ATTRS_CONSTEXPR __DEFAULT_FN_ATTRS
29 /** Find the first set bit starting from the lsb. Result is undefined if
32 * \headerfile <x86intrin.h>
34 * This intrinsic corresponds to the <c> BSF </c> instruction or the
35 * <c> TZCNT </c> instruction.
38 * A 32-bit integer operand.
39 * \returns A 32-bit integer containing the bit number.
41 static __inline__
int __DEFAULT_FN_ATTRS_CONSTEXPR
43 return __builtin_ctz((unsigned int)__A
);
46 /** Find the first set bit starting from the msb. Result is undefined if
49 * \headerfile <x86intrin.h>
51 * This intrinsic corresponds to the <c> BSR </c> instruction or the
52 * <c> LZCNT </c> instruction and an <c> XOR </c>.
55 * A 32-bit integer operand.
56 * \returns A 32-bit integer containing the bit number.
58 static __inline__
int __DEFAULT_FN_ATTRS_CONSTEXPR
60 return 31 - __builtin_clz((unsigned int)__A
);
63 /** Swaps the bytes in the input. Converting little endian to big endian or
66 * \headerfile <x86intrin.h>
68 * This intrinsic corresponds to the <c> BSWAP </c> instruction.
71 * A 32-bit integer operand.
72 * \returns A 32-bit integer containing the swapped bytes.
74 static __inline__
int __DEFAULT_FN_ATTRS_CONSTEXPR
76 return (int)__builtin_bswap32((unsigned int)__A
);
79 static __inline__
int __DEFAULT_FN_ATTRS_CONSTEXPR
81 return (int)__builtin_bswap32((unsigned int)__A
);
84 #define _bit_scan_forward(A) __bsfd((A))
85 #define _bit_scan_reverse(A) __bsrd((A))
88 /** Find the first set bit starting from the lsb. Result is undefined if
91 * \headerfile <x86intrin.h>
93 * This intrinsic corresponds to the <c> BSF </c> instruction or the
94 * <c> TZCNT </c> instruction.
97 * A 64-bit integer operand.
98 * \returns A 32-bit integer containing the bit number.
100 static __inline__
int __DEFAULT_FN_ATTRS_CONSTEXPR
101 __bsfq(long long __A
) {
102 return (long long)__builtin_ctzll((unsigned long long)__A
);
105 /** Find the first set bit starting from the msb. Result is undefined if
108 * \headerfile <x86intrin.h>
110 * This intrinsic corresponds to the <c> BSR </c> instruction or the
111 * <c> LZCNT </c> instruction and an <c> XOR </c>.
114 * A 64-bit integer operand.
115 * \returns A 32-bit integer containing the bit number.
117 static __inline__
int __DEFAULT_FN_ATTRS_CONSTEXPR
118 __bsrq(long long __A
) {
119 return 63 - __builtin_clzll((unsigned long long)__A
);
122 /** Swaps the bytes in the input. Converting little endian to big endian or
125 * \headerfile <x86intrin.h>
127 * This intrinsic corresponds to the <c> BSWAP </c> instruction.
130 * A 64-bit integer operand.
131 * \returns A 64-bit integer containing the swapped bytes.
133 static __inline__
long long __DEFAULT_FN_ATTRS_CONSTEXPR
134 __bswapq(long long __A
) {
135 return (long long)__builtin_bswap64((unsigned long long)__A
);
138 #define _bswap64(A) __bswapq((A))
141 /** Counts the number of bits in the source operand having a value of 1.
143 * \headerfile <x86intrin.h>
145 * This intrinsic corresponds to the <c> POPCNT </c> instruction or a
146 * a sequence of arithmetic and logic ops to calculate it.
149 * An unsigned 32-bit integer operand.
150 * \returns A 32-bit integer containing the number of bits with value 1 in the
153 static __inline__
int __DEFAULT_FN_ATTRS_CONSTEXPR
154 __popcntd(unsigned int __A
)
156 return __builtin_popcount(__A
);
159 #define _popcnt32(A) __popcntd((A))
162 /** Counts the number of bits in the source operand having a value of 1.
164 * \headerfile <x86intrin.h>
166 * This intrinsic corresponds to the <c> POPCNT </c> instruction or a
167 * a sequence of arithmetic and logic ops to calculate it.
170 * An unsigned 64-bit integer operand.
171 * \returns A 64-bit integer containing the number of bits with value 1 in the
174 static __inline__
long long __DEFAULT_FN_ATTRS_CONSTEXPR
175 __popcntq(unsigned long long __A
)
177 return __builtin_popcountll(__A
);
180 #define _popcnt64(A) __popcntq((A))
181 #endif /* __x86_64__ */
184 static __inline__
unsigned long long __DEFAULT_FN_ATTRS
187 return __builtin_ia32_readeflags_u64();
190 static __inline__
void __DEFAULT_FN_ATTRS
191 __writeeflags(unsigned long long __f
)
193 __builtin_ia32_writeeflags_u64(__f
);
196 #else /* !__x86_64__ */
197 static __inline__
unsigned int __DEFAULT_FN_ATTRS
200 return __builtin_ia32_readeflags_u32();
203 static __inline__
void __DEFAULT_FN_ATTRS
204 __writeeflags(unsigned int __f
)
206 __builtin_ia32_writeeflags_u32(__f
);
208 #endif /* !__x86_64__ */
210 /** Cast a 32-bit float value to a 32-bit unsigned integer value
212 * \headerfile <x86intrin.h>
213 * This intrinsic corresponds to the <c> VMOVD / MOVD </c> instruction in x86_64,
214 * and corresponds to the <c> VMOVL / MOVL </c> instruction in ia32.
217 * A 32-bit float value.
218 * \returns a 32-bit unsigned integer containing the converted value.
220 static __inline__
unsigned int __DEFAULT_FN_ATTRS_CAST
221 _castf32_u32(float __A
) {
222 return __builtin_bit_cast(unsigned int, __A
);
225 /** Cast a 64-bit float value to a 64-bit unsigned integer value
227 * \headerfile <x86intrin.h>
228 * This intrinsic corresponds to the <c> VMOVQ / MOVQ </c> instruction in x86_64,
229 * and corresponds to the <c> VMOVL / MOVL </c> instruction in ia32.
232 * A 64-bit float value.
233 * \returns a 64-bit unsigned integer containing the converted value.
235 static __inline__
unsigned long long __DEFAULT_FN_ATTRS_CAST
236 _castf64_u64(double __A
) {
237 return __builtin_bit_cast(unsigned long long, __A
);
240 /** Cast a 32-bit unsigned integer value to a 32-bit float value
242 * \headerfile <x86intrin.h>
243 * This intrinsic corresponds to the <c> VMOVQ / MOVQ </c> instruction in x86_64,
244 * and corresponds to the <c> FLDS </c> instruction in ia32.
247 * A 32-bit unsigned integer value.
248 * \returns a 32-bit float value containing the converted value.
250 static __inline__
float __DEFAULT_FN_ATTRS_CAST
251 _castu32_f32(unsigned int __A
) {
252 return __builtin_bit_cast(float, __A
);
255 /** Cast a 64-bit unsigned integer value to a 64-bit float value
257 * \headerfile <x86intrin.h>
258 * This intrinsic corresponds to the <c> VMOVQ / MOVQ </c> instruction in x86_64,
259 * and corresponds to the <c> FLDL </c> instruction in ia32.
262 * A 64-bit unsigned integer value.
263 * \returns a 64-bit float value containing the converted value.
265 static __inline__
double __DEFAULT_FN_ATTRS_CAST
266 _castu64_f64(unsigned long long __A
) {
267 return __builtin_bit_cast(double, __A
);
270 /** Adds the unsigned integer operand to the CRC-32C checksum of the
271 * unsigned char operand.
273 * \headerfile <x86intrin.h>
275 * This intrinsic corresponds to the <c> CRC32B </c> instruction.
278 * An unsigned integer operand to add to the CRC-32C checksum of operand
281 * An unsigned 8-bit integer operand used to compute the CRC-32C checksum.
282 * \returns The result of adding operand \a __C to the CRC-32C checksum of
285 static __inline__
unsigned int __DEFAULT_FN_ATTRS_CRC32
286 __crc32b(unsigned int __C
, unsigned char __D
)
288 return __builtin_ia32_crc32qi(__C
, __D
);
291 /** Adds the unsigned integer operand to the CRC-32C checksum of the
292 * unsigned short operand.
294 * \headerfile <x86intrin.h>
296 * This intrinsic corresponds to the <c> CRC32W </c> instruction.
299 * An unsigned integer operand to add to the CRC-32C checksum of operand
302 * An unsigned 16-bit integer operand used to compute the CRC-32C checksum.
303 * \returns The result of adding operand \a __C to the CRC-32C checksum of
306 static __inline__
unsigned int __DEFAULT_FN_ATTRS_CRC32
307 __crc32w(unsigned int __C
, unsigned short __D
)
309 return __builtin_ia32_crc32hi(__C
, __D
);
312 /** Adds the unsigned integer operand to the CRC-32C checksum of the
313 * second unsigned integer operand.
315 * \headerfile <x86intrin.h>
317 * This intrinsic corresponds to the <c> CRC32D </c> instruction.
320 * An unsigned integer operand to add to the CRC-32C checksum of operand
323 * An unsigned 32-bit integer operand used to compute the CRC-32C checksum.
324 * \returns The result of adding operand \a __C to the CRC-32C checksum of
327 static __inline__
unsigned int __DEFAULT_FN_ATTRS_CRC32
328 __crc32d(unsigned int __C
, unsigned int __D
)
330 return __builtin_ia32_crc32si(__C
, __D
);
334 /** Adds the unsigned integer operand to the CRC-32C checksum of the
335 * unsigned 64-bit integer operand.
337 * \headerfile <x86intrin.h>
339 * This intrinsic corresponds to the <c> CRC32Q </c> instruction.
342 * An unsigned integer operand to add to the CRC-32C checksum of operand
345 * An unsigned 64-bit integer operand used to compute the CRC-32C checksum.
346 * \returns The result of adding operand \a __C to the CRC-32C checksum of
349 static __inline__
unsigned long long __DEFAULT_FN_ATTRS_CRC32
350 __crc32q(unsigned long long __C
, unsigned long long __D
)
352 return __builtin_ia32_crc32di(__C
, __D
);
354 #endif /* __x86_64__ */
356 static __inline__
unsigned long long __DEFAULT_FN_ATTRS
358 return __builtin_ia32_rdpmc(__A
);
362 static __inline__
unsigned long long __DEFAULT_FN_ATTRS
363 __rdtscp(unsigned int *__A
) {
364 return __builtin_ia32_rdtscp(__A
);
367 #define _rdtsc() __rdtsc()
369 #define _rdpmc(A) __rdpmc(A)
371 static __inline__
void __DEFAULT_FN_ATTRS
373 __builtin_ia32_wbinvd();
376 static __inline__
unsigned char __DEFAULT_FN_ATTRS_CONSTEXPR
377 __rolb(unsigned char __X
, int __C
) {
378 return __builtin_rotateleft8(__X
, __C
);
381 static __inline__
unsigned char __DEFAULT_FN_ATTRS_CONSTEXPR
382 __rorb(unsigned char __X
, int __C
) {
383 return __builtin_rotateright8(__X
, __C
);
386 static __inline__
unsigned short __DEFAULT_FN_ATTRS_CONSTEXPR
387 __rolw(unsigned short __X
, int __C
) {
388 return __builtin_rotateleft16(__X
, __C
);
391 static __inline__
unsigned short __DEFAULT_FN_ATTRS_CONSTEXPR
392 __rorw(unsigned short __X
, int __C
) {
393 return __builtin_rotateright16(__X
, __C
);
396 static __inline__
unsigned int __DEFAULT_FN_ATTRS_CONSTEXPR
397 __rold(unsigned int __X
, int __C
) {
398 return __builtin_rotateleft32(__X
, (unsigned int)__C
);
401 static __inline__
unsigned int __DEFAULT_FN_ATTRS_CONSTEXPR
402 __rord(unsigned int __X
, int __C
) {
403 return __builtin_rotateright32(__X
, (unsigned int)__C
);
407 static __inline__
unsigned long long __DEFAULT_FN_ATTRS_CONSTEXPR
408 __rolq(unsigned long long __X
, int __C
) {
409 return __builtin_rotateleft64(__X
, (unsigned long long)__C
);
412 static __inline__
unsigned long long __DEFAULT_FN_ATTRS_CONSTEXPR
413 __rorq(unsigned long long __X
, int __C
) {
414 return __builtin_rotateright64(__X
, (unsigned long long)__C
);
416 #endif /* __x86_64__ */
419 /* These are already provided as builtins for MSVC. */
420 /* Select the correct function based on the size of long. */
422 #define _lrotl(a,b) __rolq((a), (b))
423 #define _lrotr(a,b) __rorq((a), (b))
425 #define _lrotl(a,b) __rold((a), (b))
426 #define _lrotr(a,b) __rord((a), (b))
428 #define _rotl(a,b) __rold((a), (b))
429 #define _rotr(a,b) __rord((a), (b))
432 /* These are not builtins so need to be provided in all modes. */
433 #define _rotwl(a,b) __rolw((a), (b))
434 #define _rotwr(a,b) __rorw((a), (b))
436 #undef __DEFAULT_FN_ATTRS
437 #undef __DEFAULT_FN_ATTRS_CAST
438 #undef __DEFAULT_FN_ATTRS_CRC32
439 #undef __DEFAULT_FN_ATTRS_CONSTEXPR
441 #endif /* __IA32INTRIN_H */