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 /** Find the first set bit starting from the lsb. Result is undefined if
20 * \headerfile <x86intrin.h>
22 * This intrinsic corresponds to the <c> BSF </c> instruction or the
23 * <c> TZCNT </c> instruction.
26 * A 32-bit integer operand.
27 * \returns A 32-bit integer containing the bit number.
29 static __inline__ int __attribute__((__always_inline__, __nodebug__))
31 return __builtin_ctz(__A);
34 /** Find the first set bit starting from the msb. Result is undefined if
37 * \headerfile <x86intrin.h>
39 * This intrinsic corresponds to the <c> BSR </c> instruction or the
40 * <c> LZCNT </c> instruction and an <c> XOR </c>.
43 * A 32-bit integer operand.
44 * \returns A 32-bit integer containing the bit number.
46 static __inline__ int __attribute__((__always_inline__, __nodebug__))
48 return 31 - __builtin_clz(__A);
51 /** Swaps the bytes in the input. Converting little endian to big endian or
54 * \headerfile <x86intrin.h>
56 * This intrinsic corresponds to the <c> BSWAP </c> instruction.
59 * A 32-bit integer operand.
60 * \returns A 32-bit integer containing the swapped bytes.
62 static __inline__ int __attribute__((__always_inline__, __nodebug__))
64 return __builtin_bswap32(__A);
67 static __inline__ int __attribute__((__always_inline__, __nodebug__))
69 return __builtin_bswap32(__A);
72 #define _bit_scan_forward(A) __bsfd((A))
73 #define _bit_scan_reverse(A) __bsrd((A))
76 /** Find the first set bit starting from the lsb. Result is undefined if
79 * \headerfile <x86intrin.h>
81 * This intrinsic corresponds to the <c> BSF </c> instruction or the
82 * <c> TZCNT </c> instruction.
85 * A 64-bit integer operand.
86 * \returns A 32-bit integer containing the bit number.
88 static __inline__ int __attribute__((__always_inline__, __nodebug__))
89 __bsfq(long long __A) {
90 return __builtin_ctzll(__A);
93 /** Find the first set bit starting from the msb. Result is undefined if
96 * \headerfile <x86intrin.h>
98 * This intrinsic corresponds to the <c> BSR </c> instruction or the
99 * <c> LZCNT </c> instruction and an <c> XOR </c>.
102 * A 64-bit integer operand.
103 * \returns A 32-bit integer containing the bit number.
105 static __inline__ int __attribute__((__always_inline__, __nodebug__))
106 __bsrq(long long __A) {
107 return 63 - __builtin_clzll(__A);
110 /** Swaps the bytes in the input. Converting little endian to big endian or
113 * \headerfile <x86intrin.h>
115 * This intrinsic corresponds to the <c> BSWAP </c> instruction.
118 * A 64-bit integer operand.
119 * \returns A 64-bit integer containing the swapped bytes.
121 static __inline__ long long __attribute__((__always_inline__, __nodebug__))
122 __bswapq(long long __A) {
123 return __builtin_bswap64(__A);
126 #define _bswap64(A) __bswapq((A))
129 /** Counts the number of bits in the source operand having a value of 1.
131 * \headerfile <x86intrin.h>
133 * This intrinsic corresponds to the <c> POPCNT </c> instruction or a
134 * a sequence of arithmetic and logic ops to calculate it.
137 * An unsigned 32-bit integer operand.
138 * \returns A 32-bit integer containing the number of bits with value 1 in the
141 static __inline__ int __attribute__((__always_inline__, __nodebug__))
142 __popcntd(unsigned int __A)
144 return __builtin_popcount(__A);
147 #define _popcnt32(A) __popcntd((A))
150 /** Counts the number of bits in the source operand having a value of 1.
152 * \headerfile <x86intrin.h>
154 * This intrinsic corresponds to the <c> POPCNT </c> instruction or a
155 * a sequence of arithmetic and logic ops to calculate it.
158 * An unsigned 64-bit integer operand.
159 * \returns A 64-bit integer containing the number of bits with value 1 in the
162 static __inline__ long long __attribute__((__always_inline__, __nodebug__))
163 __popcntq(unsigned long long __A)
165 return __builtin_popcountll(__A);
168 #define _popcnt64(A) __popcntq((A))
169 #endif /* __x86_64__ */
172 static __inline__ unsigned long long __attribute__((__always_inline__, __nodebug__))
175 return __builtin_ia32_readeflags_u64();
178 static __inline__ void __attribute__((__always_inline__, __nodebug__))
179 __writeeflags(unsigned long long __f)
181 __builtin_ia32_writeeflags_u64(__f);
184 #else /* !__x86_64__ */
185 static __inline__ unsigned int __attribute__((__always_inline__, __nodebug__))
188 return __builtin_ia32_readeflags_u32();
191 static __inline__ void __attribute__((__always_inline__, __nodebug__))
192 __writeeflags(unsigned int __f)
194 __builtin_ia32_writeeflags_u32(__f);
196 #endif /* !__x86_64__ */
198 /** Adds the unsigned integer operand to the CRC-32C checksum of the
199 * unsigned char operand.
201 * \headerfile <x86intrin.h>
203 * This intrinsic corresponds to the <c> CRC32B </c> instruction.
206 * An unsigned integer operand to add to the CRC-32C checksum of operand
209 * An unsigned 8-bit integer operand used to compute the CRC-32C checksum.
210 * \returns The result of adding operand \a __C to the CRC-32C checksum of
213 static __inline__ unsigned int __attribute__((__always_inline__, __nodebug__, __target__("sse4.2")))
214 __crc32b(unsigned int __C, unsigned char __D)
216 return __builtin_ia32_crc32qi(__C, __D);
219 /** Adds the unsigned integer operand to the CRC-32C checksum of the
220 * unsigned short operand.
222 * \headerfile <x86intrin.h>
224 * This intrinsic corresponds to the <c> CRC32W </c> instruction.
227 * An unsigned integer operand to add to the CRC-32C checksum of operand
230 * An unsigned 16-bit integer operand used to compute the CRC-32C checksum.
231 * \returns The result of adding operand \a __C to the CRC-32C checksum of
234 static __inline__ unsigned int __attribute__((__always_inline__, __nodebug__, __target__("sse4.2")))
235 __crc32w(unsigned int __C, unsigned short __D)
237 return __builtin_ia32_crc32hi(__C, __D);
240 /** Adds the unsigned integer operand to the CRC-32C checksum of the
241 * second unsigned integer operand.
243 * \headerfile <x86intrin.h>
245 * This intrinsic corresponds to the <c> CRC32D </c> instruction.
248 * An unsigned integer operand to add to the CRC-32C checksum of operand
251 * An unsigned 32-bit integer operand used to compute the CRC-32C checksum.
252 * \returns The result of adding operand \a __C to the CRC-32C checksum of
255 static __inline__ unsigned int __attribute__((__always_inline__, __nodebug__, __target__("sse4.2")))
256 __crc32d(unsigned int __C, unsigned int __D)
258 return __builtin_ia32_crc32si(__C, __D);
262 /** Adds the unsigned integer operand to the CRC-32C checksum of the
263 * unsigned 64-bit integer operand.
265 * \headerfile <x86intrin.h>
267 * This intrinsic corresponds to the <c> CRC32Q </c> instruction.
270 * An unsigned integer operand to add to the CRC-32C checksum of operand
273 * An unsigned 64-bit integer operand used to compute the CRC-32C checksum.
274 * \returns The result of adding operand \a __C to the CRC-32C checksum of
277 static __inline__ unsigned long long __attribute__((__always_inline__, __nodebug__, __target__("sse4.2")))
278 __crc32q(unsigned long long __C, unsigned long long __D)
280 return __builtin_ia32_crc32di(__C, __D);
282 #endif /* __x86_64__ */
284 static __inline__ unsigned long long __attribute__((__always_inline__, __nodebug__))
286 return __builtin_ia32_rdpmc(__A);
290 static __inline__ unsigned long long __attribute__((__always_inline__, __nodebug__))
291 __rdtscp(unsigned int *__A) {
292 return __builtin_ia32_rdtscp(__A);
295 #define _rdtsc() __rdtsc()
297 #define _rdpmc(A) __rdpmc(A)
299 static __inline__ void __attribute__((__always_inline__, __nodebug__))
301 __builtin_ia32_wbinvd();
304 static __inline__ unsigned char __attribute__((__always_inline__, __nodebug__))
305 __rolb(unsigned char __X, int __C) {
306 return __builtin_rotateleft8(__X, __C);
309 static __inline__ unsigned char __attribute__((__always_inline__, __nodebug__))
310 __rorb(unsigned char __X, int __C) {
311 return __builtin_rotateright8(__X, __C);
314 static __inline__ unsigned short __attribute__((__always_inline__, __nodebug__))
315 __rolw(unsigned short __X, int __C) {
316 return __builtin_rotateleft16(__X, __C);
319 static __inline__ unsigned short __attribute__((__always_inline__, __nodebug__))
320 __rorw(unsigned short __X, int __C) {
321 return __builtin_rotateright16(__X, __C);
324 static __inline__ unsigned int __attribute__((__always_inline__, __nodebug__))
325 __rold(unsigned int __X, int __C) {
326 return __builtin_rotateleft32(__X, __C);
329 static __inline__ unsigned int __attribute__((__always_inline__, __nodebug__))
330 __rord(unsigned int __X, int __C) {
331 return __builtin_rotateright32(__X, __C);
335 static __inline__ unsigned long long __attribute__((__always_inline__, __nodebug__))
336 __rolq(unsigned long long __X, int __C) {
337 return __builtin_rotateleft64(__X, __C);
340 static __inline__ unsigned long long __attribute__((__always_inline__, __nodebug__))
341 __rorq(unsigned long long __X, int __C) {
342 return __builtin_rotateright64(__X, __C);
344 #endif /* __x86_64__ */
347 /* These are already provided as builtins for MSVC. */
348 /* Select the correct function based on the size of long. */
350 #define _lrotl(a,b) __rolq((a), (b))
351 #define _lrotr(a,b) __rorq((a), (b))
353 #define _lrotl(a,b) __rold((a), (b))
354 #define _lrotr(a,b) __rord((a), (b))
356 #define _rotl(a,b) __rold((a), (b))
357 #define _rotr(a,b) __rord((a), (b))
360 /* These are not builtins so need to be provided in all modes. */
361 #define _rotwl(a,b) __rolw((a), (b))
362 #define _rotwr(a,b) __rorw((a), (b))
364 #endif /* __IA32INTRIN_H */