Clang Project

clang_source_code/lib/Headers/ia32intrin.h
1/* ===-------- ia32intrin.h ---------------------------------------------------===
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#ifndef __X86INTRIN_H
25#error "Never use <ia32intrin.h> directly; include <x86intrin.h> instead."
26#endif
27
28#ifndef __IA32INTRIN_H
29#define __IA32INTRIN_H
30
31/** Find the first set bit starting from the lsb. Result is undefined if
32 *  input is 0.
33 *
34 *  \headerfile <x86intrin.h>
35 *
36 *  This intrinsic corresponds to the <c> BSF </c> instruction or the
37 *  <c> TZCNT </c> instruction.
38 *
39 *  \param __A
40 *     A 32-bit integer operand.
41 *  \returns A 32-bit integer containing the bit number.
42 */
43static __inline__ int __attribute__((__always_inline__, __nodebug__))
44__bsfd(int __A) {
45  return __builtin_ctz(__A);
46}
47
48/** Find the first set bit starting from the msb. Result is undefined if
49 *  input is 0.
50 *
51 *  \headerfile <x86intrin.h>
52 *
53 *  This intrinsic corresponds to the <c> BSR </c> instruction or the
54 *  <c> LZCNT </c> instruction and an <c> XOR </c>.
55 *
56 *  \param __A
57 *     A 32-bit integer operand.
58 *  \returns A 32-bit integer containing the bit number.
59 */
60static __inline__ int __attribute__((__always_inline__, __nodebug__))
61__bsrd(int __A) {
62  return 31 - __builtin_clz(__A);
63}
64
65/** Swaps the bytes in the input. Converting little endian to big endian or
66 *  vice versa.
67 *
68 *  \headerfile <x86intrin.h>
69 *
70 *  This intrinsic corresponds to the <c> BSWAP </c> instruction.
71 *
72 *  \param __A
73 *     A 32-bit integer operand.
74 *  \returns A 32-bit integer containing the swapped bytes.
75 */
76static __inline__ int __attribute__((__always_inline__, __nodebug__))
77__bswapd(int __A) {
78  return __builtin_bswap32(__A);
79}
80
81static __inline__ int __attribute__((__always_inline__, __nodebug__))
82_bswap(int __A) {
83  return __builtin_bswap32(__A);
84}
85
86#define _bit_scan_forward(A) __bsfd((A))
87#define _bit_scan_reverse(A) __bsrd((A))
88
89#ifdef __x86_64__
90/** Find the first set bit starting from the lsb. Result is undefined if
91 *  input is 0.
92 *
93 *  \headerfile <x86intrin.h>
94 *
95 *  This intrinsic corresponds to the <c> BSF </c> instruction or the
96 *  <c> TZCNT </c> instruction.
97 *
98 *  \param __A
99 *     A 64-bit integer operand.
100 *  \returns A 32-bit integer containing the bit number.
101 */
102static __inline__ int __attribute__((__always_inline__, __nodebug__))
103__bsfq(long long __A) {
104  return __builtin_ctzll(__A);
105}
106
107/** Find the first set bit starting from the msb. Result is undefined if
108 *  input is 0.
109 *
110 *  \headerfile <x86intrin.h>
111 *
112 *  This intrinsic corresponds to the <c> BSR </c> instruction or the
113 *  <c> LZCNT </c> instruction and an <c> XOR </c>.
114 *
115 *  \param __A
116 *     A 64-bit integer operand.
117 *  \returns A 32-bit integer containing the bit number.
118 */
119static __inline__ int __attribute__((__always_inline__, __nodebug__))
120__bsrq(long long __A) {
121  return 63 - __builtin_clzll(__A);
122}
123
124/** Swaps the bytes in the input. Converting little endian to big endian or
125 *  vice versa.
126 *
127 *  \headerfile <x86intrin.h>
128 *
129 *  This intrinsic corresponds to the <c> BSWAP </c> instruction.
130 *
131 *  \param __A
132 *     A 64-bit integer operand.
133 *  \returns A 64-bit integer containing the swapped bytes.
134 */
135static __inline__ long long __attribute__((__always_inline__, __nodebug__))
136__bswapq(long long __A) {
137  return __builtin_bswap64(__A);
138}
139
140#define _bswap64(A) __bswapq((A))
141#endif
142
143/** Counts the number of bits in the source operand having a value of 1.
144 *
145 *  \headerfile <x86intrin.h>
146 *
147 *  This intrinsic corresponds to the <c> POPCNT </c> instruction or a
148 *  a sequence of arithmetic and logic ops to calculate it.
149 *
150 *  \param __A
151 *     An unsigned 32-bit integer operand.
152 *  \returns A 32-bit integer containing the number of bits with value 1 in the
153 *     source operand.
154 */
155static __inline__ int __attribute__((__always_inline__, __nodebug__))
156__popcntd(unsigned int __A)
157{
158  return __builtin_popcount(__A);
159}
160
161#define _popcnt32(A) __popcntd((A))
162
163#ifdef __x86_64__
164/** Counts the number of bits in the source operand having a value of 1.
165 *
166 *  \headerfile <x86intrin.h>
167 *
168 *  This intrinsic corresponds to the <c> POPCNT </c> instruction or a
169 *  a sequence of arithmetic and logic ops to calculate it.
170 *
171 *  \param __A
172 *     An unsigned 64-bit integer operand.
173 *  \returns A 64-bit integer containing the number of bits with value 1 in the
174 *     source operand.
175 */
176static __inline__ long long __attribute__((__always_inline__, __nodebug__))
177__popcntq(unsigned long long __A)
178{
179  return __builtin_popcountll(__A);
180}
181
182#define _popcnt64(A) __popcntq((A))
183#endif /* __x86_64__ */
184
185#ifdef __x86_64__
186static __inline__ unsigned long long __attribute__((__always_inline__, __nodebug__))
187__readeflags(void)
188{
189  return __builtin_ia32_readeflags_u64();
190}
191
192static __inline__ void __attribute__((__always_inline__, __nodebug__))
193__writeeflags(unsigned long long __f)
194{
195  __builtin_ia32_writeeflags_u64(__f);
196}
197
198#else /* !__x86_64__ */
199static __inline__ unsigned int __attribute__((__always_inline__, __nodebug__))
200__readeflags(void)
201{
202  return __builtin_ia32_readeflags_u32();
203}
204
205static __inline__ void __attribute__((__always_inline__, __nodebug__))
206__writeeflags(unsigned int __f)
207{
208  __builtin_ia32_writeeflags_u32(__f);
209}
210#endif /* !__x86_64__ */
211
212/** Adds the unsigned integer operand to the CRC-32C checksum of the
213 *     unsigned char operand.
214 *
215 *  \headerfile <x86intrin.h>
216 *
217 *  This intrinsic corresponds to the <c> CRC32B </c> instruction.
218 *
219 *  \param __C
220 *     An unsigned integer operand to add to the CRC-32C checksum of operand
221 *     \a  __D.
222 *  \param __D
223 *     An unsigned 8-bit integer operand used to compute the CRC-32C checksum.
224 *  \returns The result of adding operand \a __C to the CRC-32C checksum of
225 *     operand \a __D.
226 */
227static __inline__ unsigned int __attribute__((__always_inline__, __nodebug__, __target__("sse4.2")))
228__crc32b(unsigned int __Cunsigned char __D)
229{
230  return __builtin_ia32_crc32qi(__C__D);
231}
232
233/** Adds the unsigned integer operand to the CRC-32C checksum of the
234 *     unsigned short operand.
235 *
236 *  \headerfile <x86intrin.h>
237 *
238 *  This intrinsic corresponds to the <c> CRC32W </c> instruction.
239 *
240 *  \param __C
241 *     An unsigned integer operand to add to the CRC-32C checksum of operand
242 *     \a  __D.
243 *  \param __D
244 *     An unsigned 16-bit integer operand used to compute the CRC-32C checksum.
245 *  \returns The result of adding operand \a __C to the CRC-32C checksum of
246 *     operand \a __D.
247 */
248static __inline__ unsigned int __attribute__((__always_inline__, __nodebug__, __target__("sse4.2")))
249__crc32w(unsigned int __Cunsigned short __D)
250{
251  return __builtin_ia32_crc32hi(__C__D);
252}
253
254/** Adds the unsigned integer operand to the CRC-32C checksum of the
255 *     second unsigned integer operand.
256 *
257 *  \headerfile <x86intrin.h>
258 *
259 *  This intrinsic corresponds to the <c> CRC32D </c> instruction.
260 *
261 *  \param __C
262 *     An unsigned integer operand to add to the CRC-32C checksum of operand
263 *     \a  __D.
264 *  \param __D
265 *     An unsigned 32-bit integer operand used to compute the CRC-32C checksum.
266 *  \returns The result of adding operand \a __C to the CRC-32C checksum of
267 *     operand \a __D.
268 */
269static __inline__ unsigned int __attribute__((__always_inline__, __nodebug__, __target__("sse4.2")))
270__crc32d(unsigned int __Cunsigned int __D)
271{
272  return __builtin_ia32_crc32si(__C__D);
273}
274
275#ifdef __x86_64__
276/** Adds the unsigned integer operand to the CRC-32C checksum of the
277 *     unsigned 64-bit integer operand.
278 *
279 *  \headerfile <x86intrin.h>
280 *
281 *  This intrinsic corresponds to the <c> CRC32Q </c> instruction.
282 *
283 *  \param __C
284 *     An unsigned integer operand to add to the CRC-32C checksum of operand
285 *     \a  __D.
286 *  \param __D
287 *     An unsigned 64-bit integer operand used to compute the CRC-32C checksum.
288 *  \returns The result of adding operand \a __C to the CRC-32C checksum of
289 *     operand \a __D.
290 */
291static __inline__ unsigned long long __attribute__((__always_inline__, __nodebug__, __target__("sse4.2")))
292__crc32q(unsigned long long __Cunsigned long long __D)
293{
294  return __builtin_ia32_crc32di(__C__D);
295}
296#endif /* __x86_64__ */
297
298static __inline__ unsigned long long __attribute__((__always_inline__, __nodebug__))
299__rdpmc(int __A) {
300  return __builtin_ia32_rdpmc(__A);
301}
302
303/* __rdtscp */
304static __inline__ unsigned long long __attribute__((__always_inline__, __nodebug__))
305__rdtscp(unsigned int *__A) {
306  return __builtin_ia32_rdtscp(__A);
307}
308
309#define _rdtsc() __rdtsc()
310
311#define _rdpmc(A) __rdpmc(A)
312
313static __inline__ void __attribute__((__always_inline__, __nodebug__))
314_wbinvd(void) {
315  __builtin_ia32_wbinvd();
316}
317
318static __inline__ unsigned char __attribute__((__always_inline__, __nodebug__))
319__rolb(unsigned char __Xint __C) {
320  return __builtin_rotateleft8(__X__C);
321}
322
323static __inline__ unsigned char __attribute__((__always_inline__, __nodebug__))
324__rorb(unsigned char __Xint __C) {
325  return __builtin_rotateright8(__X__C);
326}
327
328static __inline__ unsigned short __attribute__((__always_inline__, __nodebug__))
329__rolw(unsigned short __Xint __C) {
330  return __builtin_rotateleft16(__X__C);
331}
332
333static __inline__ unsigned short __attribute__((__always_inline__, __nodebug__))
334__rorw(unsigned short __Xint __C) {
335  return __builtin_rotateright16(__X__C);
336}
337
338static __inline__ unsigned int __attribute__((__always_inline__, __nodebug__))
339__rold(unsigned int __Xint __C) {
340  return __builtin_rotateleft32(__X__C);
341}
342
343static __inline__ unsigned int __attribute__((__always_inline__, __nodebug__))
344__rord(unsigned int __Xint __C) {
345  return __builtin_rotateright32(__X__C);
346}
347
348#ifdef __x86_64__
349static __inline__ unsigned long long __attribute__((__always_inline__, __nodebug__))
350__rolq(unsigned long long __Xint __C) {
351  return __builtin_rotateleft64(__X__C);
352}
353
354static __inline__ unsigned long long __attribute__((__always_inline__, __nodebug__))
355__rorq(unsigned long long __Xint __C) {
356  return __builtin_rotateright64(__X__C);
357}
358#endif /* __x86_64__ */
359
360#ifndef _MSC_VER
361/* These are already provided as builtins for MSVC. */
362/* Select the correct function based on the size of long. */
363#ifdef __LP64__
364#define _lrotl(a,b) __rolq((a), (b))
365#define _lrotr(a,b) __rorq((a), (b))
366#else
367#define _lrotl(a,b) __rold((a), (b))
368#define _lrotr(a,b) __rord((a), (b))
369#endif
370#define _rotl(a,b) __rold((a), (b))
371#define _rotr(a,b) __rord((a), (b))
372#endif // _MSC_VER
373
374/* These are not builtins so need to be provided in all modes. */
375#define _rotwl(a,b) __rolw((a), (b))
376#define _rotwr(a,b) __rorw((a), (b))
377
378#endif /* __IA32INTRIN_H */
379