Debian Codegraph @

clang/lib/Headers/intrin.h

raw
1 /* ===-------- intrin.h ---------------------------------------------------===
2 *
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
6 *
7 *===-----------------------------------------------------------------------===
8 */
9
10 /* Only include this if we're compiling for the windows platform. */
11 #ifndef _MSC_VER
12 #include_next <intrin.h>
13 #else
14
15 #ifndef __INTRIN_H
16 #define __INTRIN_H
17
18 #include <intrin0.h>
19
20 /* First include the standard intrinsics. */
21 #if defined(__i386__) || (defined(__x86_64__) && !defined(__arm64ec__))
22 #include <x86intrin.h>
23 #endif
24
25 #if defined(__arm__)
26 #include <armintr.h>
27 #endif
28
29 #if defined(__aarch64__) || defined(__arm64ec__)
30 #include <arm64intr.h>
31 #endif
32
33 #if defined(__ARM_ACLE)
34 #include <arm_acle.h>
35 #endif
36
37 /* For the definition of jmp_buf. */
38 #if __STDC_HOSTED__
39 #include <setjmp.h>
40 #endif
41
42 /* Define the default attributes for the functions in this file. */
43 #define __DEFAULT_FN_ATTRS __attribute__((__always_inline__, __nodebug__))
44
45 #if __x86_64__
46 #define __LPTRINT_TYPE__ __int64
47 #else
48 #define __LPTRINT_TYPE__ long
49 #endif
50
51 #ifdef __cplusplus
52 extern "C" {
53 #endif
54
55 #if defined(__MMX__)
56 /* And the random ones that aren't in those files. */
57 __m64 _m_from_float(float);
58 float _m_to_float(__m64);
59 #endif
60
61 /* Other assorted instruction intrinsics. */
62 void __addfsbyte(unsigned long, unsigned char);
63 void __addfsdword(unsigned long, unsigned long);
64 void __addfsword(unsigned long, unsigned short);
65 void __code_seg(const char *);
66 void __cpuid(int[4], int);
67 void __cpuidex(int[4], int, int);
68 __int64 __emul(int, int);
69 unsigned __int64 __emulu(unsigned int, unsigned int);
70 unsigned int __getcallerseflags(void);
71 void __halt(void);
72 unsigned char __inbyte(unsigned short);
73 void __inbytestring(unsigned short, unsigned char *, unsigned long);
74 void __incfsbyte(unsigned long);
75 void __incfsdword(unsigned long);
76 void __incfsword(unsigned long);
77 unsigned long __indword(unsigned short);
78 void __indwordstring(unsigned short, unsigned long *, unsigned long);
79 void __int2c(void);
80 void __invlpg(void *);
81 unsigned short __inword(unsigned short);
82 void __inwordstring(unsigned short, unsigned short *, unsigned long);
83 void __lidt(void *);
84 unsigned __int64 __ll_lshift(unsigned __int64, int);
85 __int64 __ll_rshift(__int64, int);
86 void __movsb(unsigned char *, unsigned char const *, size_t);
87 void __movsd(unsigned long *, unsigned long const *, size_t);
88 void __movsw(unsigned short *, unsigned short const *, size_t);
89 void __nop(void);
90 void __nvreg_restore_fence(void);
91 void __nvreg_save_fence(void);
92 void __outbyte(unsigned short, unsigned char);
93 void __outbytestring(unsigned short, unsigned char *, unsigned long);
94 void __outdword(unsigned short, unsigned long);
95 void __outdwordstring(unsigned short, unsigned long *, unsigned long);
96 void __outword(unsigned short, unsigned short);
97 void __outwordstring(unsigned short, unsigned short *, unsigned long);
98 unsigned long __readcr0(void);
99 unsigned long __readcr2(void);
100 unsigned __LPTRINT_TYPE__ __readcr3(void);
101 unsigned __LPTRINT_TYPE__ __readcr4(void);
102 unsigned __int64 __readcr8(void);
103 unsigned int __readdr(unsigned int);
104 #ifdef __i386__
105 unsigned char __readfsbyte(unsigned long);
106 unsigned short __readfsword(unsigned long);
107 unsigned long __readfsdword(unsigned long);
108 unsigned __int64 __readfsqword(unsigned long);
109 #endif
110 unsigned __int64 __readmsr(unsigned long);
111 unsigned __int64 __readpmc(unsigned long);
112 unsigned long __segmentlimit(unsigned long);
113 void __sidt(void *);
114 void __stosb(unsigned char *, unsigned char, size_t);
115 void __stosd(unsigned long *, unsigned long, size_t);
116 void __stosw(unsigned short *, unsigned short, size_t);
117 void __svm_clgi(void);
118 void __svm_invlpga(void *, int);
119 void __svm_skinit(int);
120 void __svm_stgi(void);
121 void __svm_vmload(size_t);
122 void __svm_vmrun(size_t);
123 void __svm_vmsave(size_t);
124 void __ud2(void);
125 unsigned __int64 __ull_rshift(unsigned __int64, int);
126 void __vmx_off(void);
127 void __vmx_vmptrst(unsigned __int64 *);
128 void __wbinvd(void);
129 void __writecr0(unsigned int);
130 void __writecr3(unsigned __INTPTR_TYPE__);
131 void __writecr4(unsigned __INTPTR_TYPE__);
132 void __writecr8(unsigned __int64);
133 void __writedr(unsigned int, unsigned int);
134 void __writefsbyte(unsigned long, unsigned char);
135 void __writefsdword(unsigned long, unsigned long);
136 void __writefsqword(unsigned long, unsigned __int64);
137 void __writefsword(unsigned long, unsigned short);
138 void __writemsr(unsigned long, unsigned __int64);
139 void *_AddressOfReturnAddress(void);
140 unsigned char _bittest(long const *, long);
141 unsigned char _bittestandcomplement(long *, long);
142 unsigned char _bittestandreset(long *, long);
143 unsigned char _bittestandset(long *, long);
144 void __cdecl _disable(void);
145 void __cdecl _enable(void);
146 long _InterlockedAddLargeStatistic(__int64 volatile *_Addend, long _Value);
147 unsigned char _interlockedbittestandreset(long volatile *, long);
148 unsigned char _interlockedbittestandset(long volatile *, long);
149 void *_InterlockedCompareExchangePointer_HLEAcquire(void *volatile *, void *,
150 void *);
151 void *_InterlockedCompareExchangePointer_HLERelease(void *volatile *, void *,
152 void *);
153 long _InterlockedExchangeAdd_HLEAcquire(long volatile *, long);
154 long _InterlockedExchangeAdd_HLERelease(long volatile *, long);
155 __int64 _InterlockedExchangeAdd64_HLEAcquire(__int64 volatile *, __int64);
156 __int64 _InterlockedExchangeAdd64_HLERelease(__int64 volatile *, __int64);
157 void _ReadBarrier(void);
158 unsigned int _rorx_u32(unsigned int, const unsigned int);
159 int _sarx_i32(int, unsigned int);
160 #if __STDC_HOSTED__
161 int __cdecl _setjmp(jmp_buf);
162 #endif
163 unsigned int _shlx_u32(unsigned int, unsigned int);
164 unsigned int _shrx_u32(unsigned int, unsigned int);
165 void _Store_HLERelease(long volatile *, long);
166 void _Store64_HLERelease(__int64 volatile *, __int64);
167 void _StorePointer_HLERelease(void *volatile *, void *);
168 void _WriteBarrier(void);
169
170 /* These additional intrinsics are turned on in x64/amd64/x86_64 mode. */
171 #if defined(__x86_64__) && !defined(__arm64ec__)
172 void __addgsbyte(unsigned long, unsigned char);
173 void __addgsdword(unsigned long, unsigned long);
174 void __addgsqword(unsigned long, unsigned __int64);
175 void __addgsword(unsigned long, unsigned short);
176 void __faststorefence(void);
177 void __incgsbyte(unsigned long);
178 void __incgsdword(unsigned long);
179 void __incgsqword(unsigned long);
180 void __incgsword(unsigned long);
181 void __movsq(unsigned long long *, unsigned long long const *, size_t);
182 unsigned char __readgsbyte(unsigned long);
183 unsigned long __readgsdword(unsigned long);
184 unsigned __int64 __readgsqword(unsigned long);
185 unsigned short __readgsword(unsigned long);
186 void __stosq(unsigned __int64 *, unsigned __int64, size_t);
187 unsigned char __vmx_on(unsigned __int64 *);
188 unsigned char __vmx_vmclear(unsigned __int64 *);
189 unsigned char __vmx_vmlaunch(void);
190 unsigned char __vmx_vmptrld(unsigned __int64 *);
191 unsigned char __vmx_vmread(size_t, size_t *);
192 unsigned char __vmx_vmresume(void);
193 unsigned char __vmx_vmwrite(size_t, size_t);
194 void __writegsbyte(unsigned long, unsigned char);
195 void __writegsdword(unsigned long, unsigned long);
196 void __writegsqword(unsigned long, unsigned __int64);
197 void __writegsword(unsigned long, unsigned short);
198 unsigned char _bittest64(__int64 const *, __int64);
199 unsigned char _bittestandcomplement64(__int64 *, __int64);
200 unsigned char _bittestandreset64(__int64 *, __int64);
201 unsigned char _bittestandset64(__int64 *, __int64);
202 long _InterlockedAnd_np(long volatile *_Value, long _Mask);
203 short _InterlockedAnd16_np(short volatile *_Value, short _Mask);
204 __int64 _InterlockedAnd64_np(__int64 volatile *_Value, __int64 _Mask);
205 char _InterlockedAnd8_np(char volatile *_Value, char _Mask);
206 unsigned char _interlockedbittestandreset64(__int64 volatile *, __int64);
207 unsigned char _interlockedbittestandset64(__int64 volatile *, __int64);
208 long _InterlockedCompareExchange_np(long volatile *_Destination, long _Exchange,
209 long _Comparand);
210 unsigned char _InterlockedCompareExchange128_np(__int64 volatile *_Destination,
211 __int64 _ExchangeHigh,
212 __int64 _ExchangeLow,
213 __int64 *_ComparandResult);
214 short _InterlockedCompareExchange16_np(short volatile *_Destination,
215 short _Exchange, short _Comparand);
216 __int64 _InterlockedCompareExchange64_np(__int64 volatile *_Destination,
217 __int64 _Exchange, __int64 _Comparand);
218 void *_InterlockedCompareExchangePointer_np(void *volatile *_Destination,
219 void *_Exchange, void *_Comparand);
220 long _InterlockedOr_np(long volatile *_Value, long _Mask);
221 short _InterlockedOr16_np(short volatile *_Value, short _Mask);
222 __int64 _InterlockedOr64_np(__int64 volatile *_Value, __int64 _Mask);
223 char _InterlockedOr8_np(char volatile *_Value, char _Mask);
224 long _InterlockedXor_np(long volatile *_Value, long _Mask);
225 short _InterlockedXor16_np(short volatile *_Value, short _Mask);
226 __int64 _InterlockedXor64_np(__int64 volatile *_Value, __int64 _Mask);
227 char _InterlockedXor8_np(char volatile *_Value, char _Mask);
228 unsigned __int64 _rorx_u64(unsigned __int64, const unsigned int);
229 __int64 _sarx_i64(__int64, unsigned int);
230 unsigned __int64 _shlx_u64(unsigned __int64, unsigned int);
231 unsigned __int64 _shrx_u64(unsigned __int64, unsigned int);
232 __int64 __mulh(__int64, __int64);
233 unsigned __int64 __umulh(unsigned __int64, unsigned __int64);
234 __int64 _mul128(__int64, __int64, __int64 *);
235
236 #endif /* __x86_64__ */
237
238 /*----------------------------------------------------------------------------*\
239 |* movs, stos
240 \*----------------------------------------------------------------------------*/
241
242 #if defined(__i386__) || (defined(__x86_64__) && !defined(__arm64ec__))
243 static __inline__ void __DEFAULT_FN_ATTRS __movsb(unsigned char *__dst,
244 unsigned char const *__src,
245 size_t __n) {
246 #if defined(__x86_64__)
247 __asm__ __volatile__("rep movsb"
248 : "+D"(__dst), "+S"(__src), "+c"(__n)
249 :
250 : "memory");
251 #else
252 __asm__ __volatile__("xchg {%%esi, %1|%1, esi}\n"
253 "rep movsb\n"
254 "xchg {%%esi, %1|%1, esi}"
255 : "+D"(__dst), "+r"(__src), "+c"(__n)
256 :
257 : "memory");
258 #endif
259 }
260 static __inline__ void __DEFAULT_FN_ATTRS __movsd(unsigned long *__dst,
261 unsigned long const *__src,
262 size_t __n) {
263 #if defined(__x86_64__)
264 __asm__ __volatile__("rep movs{l|d}"
265 : "+D"(__dst), "+S"(__src), "+c"(__n)
266 :
267 : "memory");
268 #else
269 __asm__ __volatile__("xchg {%%esi, %1|%1, esi}\n"
270 "rep movs{l|d}\n"
271 "xchg {%%esi, %1|%1, esi}"
272 : "+D"(__dst), "+r"(__src), "+c"(__n)
273 :
274 : "memory");
275 #endif
276 }
277 static __inline__ void __DEFAULT_FN_ATTRS __movsw(unsigned short *__dst,
278 unsigned short const *__src,
279 size_t __n) {
280 #if defined(__x86_64__)
281 __asm__ __volatile__("rep movsw"
282 : "+D"(__dst), "+S"(__src), "+c"(__n)
283 :
284 : "memory");
285 #else
286 __asm__ __volatile__("xchg {%%esi, %1|%1, esi}\n"
287 "rep movsw\n"
288 "xchg {%%esi, %1|%1, esi}"
289 : "+D"(__dst), "+r"(__src), "+c"(__n)
290 :
291 : "memory");
292 #endif
293 }
294 static __inline__ void __DEFAULT_FN_ATTRS __stosd(unsigned long *__dst,
295 unsigned long __x,
296 size_t __n) {
297 __asm__ __volatile__("rep stos{l|d}"
298 : "+D"(__dst), "+c"(__n)
299 : "a"(__x)
300 : "memory");
301 }
302 static __inline__ void __DEFAULT_FN_ATTRS __stosw(unsigned short *__dst,
303 unsigned short __x,
304 size_t __n) {
305 __asm__ __volatile__("rep stosw"
306 : "+D"(__dst), "+c"(__n)
307 : "a"(__x)
308 : "memory");
309 }
310 #endif
311 #if defined(__x86_64__) && !defined(__arm64ec__)
312 static __inline__ void __DEFAULT_FN_ATTRS __movsq(
313 unsigned long long *__dst, unsigned long long const *__src, size_t __n) {
314 __asm__ __volatile__("rep movsq"
315 : "+D"(__dst), "+S"(__src), "+c"(__n)
316 :
317 : "memory");
318 }
319 static __inline__ void __DEFAULT_FN_ATTRS __stosq(unsigned __int64 *__dst,
320 unsigned __int64 __x,
321 size_t __n) {
322 __asm__ __volatile__("rep stosq" : "+D"(__dst), "+c"(__n) : "a"(__x)
323 : "memory");
324 }
325 #endif
326
327 /*----------------------------------------------------------------------------*\
328 |* Misc
329 \*----------------------------------------------------------------------------*/
330 #if defined(__i386__) || (defined(__x86_64__) && !defined(__arm64ec__))
331 static __inline__ void __DEFAULT_FN_ATTRS __halt(void) {
332 __asm__ volatile("hlt");
333 }
334
335 static __inline__ unsigned char __inbyte(unsigned short port) {
336 unsigned char ret;
337 __asm__ __volatile__("inb %w1, %b0" : "=a"(ret) : "Nd"(port));
338 return ret;
339 }
340
341 static __inline__ unsigned short __inword(unsigned short port) {
342 unsigned short ret;
343 __asm__ __volatile__("inw %w1, %w0" : "=a"(ret) : "Nd"(port));
344 return ret;
345 }
346
347 static __inline__ unsigned long __indword(unsigned short port) {
348 unsigned long ret;
349 __asm__ __volatile__("inl %w1, %k0" : "=a"(ret) : "Nd"(port));
350 return ret;
351 }
352
353 static __inline__ void __outbyte(unsigned short port, unsigned char data) {
354 __asm__ __volatile__("outb %b0, %w1" : : "a"(data), "Nd"(port));
355 }
356
357 static __inline__ void __outword(unsigned short port, unsigned short data) {
358 __asm__ __volatile__("outw %w0, %w1" : : "a"(data), "Nd"(port));
359 }
360
361 static __inline__ void __outdword(unsigned short port, unsigned long data) {
362 __asm__ __volatile__("outl %k0, %w1" : : "a"(data), "Nd"(port));
363 }
364 #endif
365
366 #if defined(__i386__) || defined(__x86_64__) || defined(__aarch64__)
367 static __inline__ void __DEFAULT_FN_ATTRS __nop(void) {
368 __asm__ volatile("nop");
369 }
370 #endif
371
372 /*----------------------------------------------------------------------------*\
373 |* MS AArch64 specific
374 \*----------------------------------------------------------------------------*/
375 #if defined(__aarch64__) || defined(__arm64ec__)
376 unsigned __int64 __getReg(int);
377 unsigned char _interlockedbittestandreset_acq(long volatile *, long);
378 unsigned char _interlockedbittestandreset_nf(long volatile *, long);
379 unsigned char _interlockedbittestandreset_rel(long volatile *, long);
380 unsigned char _interlockedbittestandreset64_acq(__int64 volatile *, __int64);
381 unsigned char _interlockedbittestandreset64_nf(__int64 volatile *, __int64);
382 unsigned char _interlockedbittestandreset64_rel(__int64 volatile *, __int64);
383 unsigned char _interlockedbittestandset_acq(long volatile *, long);
384 unsigned char _interlockedbittestandset_nf(long volatile *, long);
385 unsigned char _interlockedbittestandset_rel(long volatile *, long);
386 unsigned char _interlockedbittestandset64_acq(__int64 volatile *, __int64);
387 unsigned char _interlockedbittestandset64_nf(__int64 volatile *, __int64);
388 unsigned char _interlockedbittestandset64_rel(__int64 volatile *, __int64);
389 long _InterlockedAdd(long volatile *, long);
390 long _InterlockedAdd_acq(long volatile *, long);
391 long _InterlockedAdd_nf(long volatile *, long);
392 long _InterlockedAdd_rel(long volatile *, long);
393 __int64 _InterlockedAdd64(__int64 volatile *, __int64);
394 __int64 _InterlockedAdd64_acq(__int64 volatile *, __int64);
395 __int64 _InterlockedAdd64_nf(__int64 volatile *, __int64);
396 __int64 _InterlockedAdd64_rel(__int64 volatile *, __int64);
397 __int64 _ReadStatusReg(int);
398 void _WriteStatusReg(int, __int64);
399 unsigned int __sys(int, __int64);
400
401 unsigned short __cdecl _byteswap_ushort(unsigned short val);
402 unsigned long __cdecl _byteswap_ulong (unsigned long val);
403 unsigned __int64 __cdecl _byteswap_uint64(unsigned __int64 val);
404
405 __int64 __mulh(__int64 __a, __int64 __b);
406 unsigned __int64 __umulh(unsigned __int64 __a, unsigned __int64 __b);
407
408 void __break(int);
409
410 void __writex18byte(unsigned long offset, unsigned char data);
411 void __writex18word(unsigned long offset, unsigned short data);
412 void __writex18dword(unsigned long offset, unsigned long data);
413 void __writex18qword(unsigned long offset, unsigned __int64 data);
414
415 unsigned char __readx18byte(unsigned long offset);
416 unsigned short __readx18word(unsigned long offset);
417 unsigned long __readx18dword(unsigned long offset);
418 unsigned __int64 __readx18qword(unsigned long offset);
419
420 void __addx18byte(unsigned long offset, unsigned char data);
421 void __addx18word(unsigned long offset, unsigned short data);
422 void __addx18dword(unsigned long offset, unsigned long data);
423 void __addx18qword(unsigned long offset, unsigned __int64 data);
424
425 void __incx18byte(unsigned long offset);
426 void __incx18word(unsigned long offset);
427 void __incx18dword(unsigned long offset);
428 void __incx18qword(unsigned long offset);
429
430 double _CopyDoubleFromInt64(__int64);
431 float _CopyFloatFromInt32(__int32);
432 __int32 _CopyInt32FromFloat(float);
433 __int64 _CopyInt64FromDouble(double);
434
435 unsigned int _CountLeadingOnes(unsigned long);
436 unsigned int _CountLeadingOnes64(unsigned __int64);
437 unsigned int _CountLeadingSigns(long);
438 unsigned int _CountLeadingSigns64(__int64);
439 unsigned int _CountOneBits(unsigned long);
440 unsigned int _CountOneBits64(unsigned __int64);
441
442 unsigned int __hlt(unsigned int, ...);
443
444 void __cdecl __prefetch(const void *);
445
446 #endif
447
448 /*----------------------------------------------------------------------------*\
449 |* Privileged intrinsics
450 \*----------------------------------------------------------------------------*/
451 #if defined(__i386__) || (defined(__x86_64__) && !defined(__arm64ec__))
452 static __inline__ unsigned __int64 __DEFAULT_FN_ATTRS
453 __readmsr(unsigned long __register) {
454 // Loads the contents of a 64-bit model specific register (MSR) specified in
455 // the ECX register into registers EDX:EAX. The EDX register is loaded with
456 // the high-order 32 bits of the MSR and the EAX register is loaded with the
457 // low-order 32 bits. If less than 64 bits are implemented in the MSR being
458 // read, the values returned to EDX:EAX in unimplemented bit locations are
459 // undefined.
460 unsigned long __edx;
461 unsigned long __eax;
462 __asm__ ("rdmsr" : "=d"(__edx), "=a"(__eax) : "c"(__register));
463 return (((unsigned __int64)__edx) << 32) | (unsigned __int64)__eax;
464 }
465
466 static __inline__ unsigned __LPTRINT_TYPE__ __DEFAULT_FN_ATTRS __readcr3(void) {
467 unsigned __LPTRINT_TYPE__ __cr3_val;
468 __asm__ __volatile__(
469 "mov {%%cr3, %0|%0, cr3}"
470 : "=r"(__cr3_val)
471 :
472 : "memory");
473 return __cr3_val;
474 }
475
476 static __inline__ void __DEFAULT_FN_ATTRS
477 __writecr3(unsigned __INTPTR_TYPE__ __cr3_val) {
478 __asm__ ("mov {%0, %%cr3|cr3, %0}" : : "r"(__cr3_val) : "memory");
479 }
480 #endif
481
482 #ifdef __cplusplus
483 }
484 #endif
485
486 #undef __LPTRINT_TYPE__
487
488 #undef __DEFAULT_FN_ATTRS
489
490 #endif /* __INTRIN_H */
491 #endif /* _MSC_VER */