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