xref: /freebsd/contrib/llvm-project/clang/lib/Headers/intrin.h (revision dd41de95a84d979615a2ef11df6850622bf6184e)
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 /* First include the standard intrinsics. */
19 #if defined(__i386__) || defined(__x86_64__)
20 #include <x86intrin.h>
21 #endif
22 
23 #if defined(__arm__)
24 #include <armintr.h>
25 #endif
26 
27 #if defined(__aarch64__)
28 #include <arm64intr.h>
29 #endif
30 
31 /* For the definition of jmp_buf. */
32 #if __STDC_HOSTED__
33 #include <setjmp.h>
34 #endif
35 
36 /* Define the default attributes for the functions in this file. */
37 #define __DEFAULT_FN_ATTRS __attribute__((__always_inline__, __nodebug__))
38 
39 #if __x86_64__
40 #define __LPTRINT_TYPE__ __int64
41 #else
42 #define __LPTRINT_TYPE__ long
43 #endif
44 
45 #ifdef __cplusplus
46 extern "C" {
47 #endif
48 
49 #if defined(__MMX__)
50 /* And the random ones that aren't in those files. */
51 __m64 _m_from_float(float);
52 float _m_to_float(__m64);
53 #endif
54 
55 /* Other assorted instruction intrinsics. */
56 void __addfsbyte(unsigned long, unsigned char);
57 void __addfsdword(unsigned long, unsigned long);
58 void __addfsword(unsigned long, unsigned short);
59 void __code_seg(const char *);
60 void __cpuid(int[4], int);
61 void __cpuidex(int[4], int, int);
62 __int64 __emul(int, int);
63 unsigned __int64 __emulu(unsigned int, unsigned int);
64 unsigned int __getcallerseflags(void);
65 void __halt(void);
66 unsigned char __inbyte(unsigned short);
67 void __inbytestring(unsigned short, unsigned char *, unsigned long);
68 void __incfsbyte(unsigned long);
69 void __incfsdword(unsigned long);
70 void __incfsword(unsigned long);
71 unsigned long __indword(unsigned short);
72 void __indwordstring(unsigned short, unsigned long *, unsigned long);
73 void __int2c(void);
74 void __invlpg(void *);
75 unsigned short __inword(unsigned short);
76 void __inwordstring(unsigned short, unsigned short *, unsigned long);
77 void __lidt(void *);
78 unsigned __int64 __ll_lshift(unsigned __int64, int);
79 __int64 __ll_rshift(__int64, int);
80 void __movsb(unsigned char *, unsigned char const *, size_t);
81 void __movsd(unsigned long *, unsigned long const *, size_t);
82 void __movsw(unsigned short *, unsigned short const *, size_t);
83 void __nop(void);
84 void __nvreg_restore_fence(void);
85 void __nvreg_save_fence(void);
86 void __outbyte(unsigned short, unsigned char);
87 void __outbytestring(unsigned short, unsigned char *, unsigned long);
88 void __outdword(unsigned short, unsigned long);
89 void __outdwordstring(unsigned short, unsigned long *, unsigned long);
90 void __outword(unsigned short, unsigned short);
91 void __outwordstring(unsigned short, unsigned short *, unsigned long);
92 unsigned long __readcr0(void);
93 unsigned long __readcr2(void);
94 unsigned __LPTRINT_TYPE__ __readcr3(void);
95 unsigned long __readcr4(void);
96 unsigned long __readcr8(void);
97 unsigned int __readdr(unsigned int);
98 #ifdef __i386__
99 unsigned char __readfsbyte(unsigned long);
100 unsigned __int64 __readfsqword(unsigned long);
101 unsigned short __readfsword(unsigned long);
102 #endif
103 unsigned __int64 __readmsr(unsigned long);
104 unsigned __int64 __readpmc(unsigned long);
105 unsigned long __segmentlimit(unsigned long);
106 void __sidt(void *);
107 void __stosb(unsigned char *, unsigned char, size_t);
108 void __stosd(unsigned long *, unsigned long, size_t);
109 void __stosw(unsigned short *, unsigned short, size_t);
110 void __svm_clgi(void);
111 void __svm_invlpga(void *, int);
112 void __svm_skinit(int);
113 void __svm_stgi(void);
114 void __svm_vmload(size_t);
115 void __svm_vmrun(size_t);
116 void __svm_vmsave(size_t);
117 void __ud2(void);
118 unsigned __int64 __ull_rshift(unsigned __int64, int);
119 void __vmx_off(void);
120 void __vmx_vmptrst(unsigned __int64 *);
121 void __wbinvd(void);
122 void __writecr0(unsigned int);
123 void __writecr3(unsigned __INTPTR_TYPE__);
124 void __writecr4(unsigned int);
125 void __writecr8(unsigned int);
126 void __writedr(unsigned int, unsigned int);
127 void __writefsbyte(unsigned long, unsigned char);
128 void __writefsdword(unsigned long, unsigned long);
129 void __writefsqword(unsigned long, unsigned __int64);
130 void __writefsword(unsigned long, unsigned short);
131 void __writemsr(unsigned long, unsigned __int64);
132 void *_AddressOfReturnAddress(void);
133 unsigned char _BitScanForward(unsigned long *_Index, unsigned long _Mask);
134 unsigned char _BitScanReverse(unsigned long *_Index, unsigned long _Mask);
135 unsigned char _bittest(long const *, long);
136 unsigned char _bittestandcomplement(long *, long);
137 unsigned char _bittestandreset(long *, long);
138 unsigned char _bittestandset(long *, long);
139 void __cdecl _disable(void);
140 void __cdecl _enable(void);
141 long _InterlockedAddLargeStatistic(__int64 volatile *_Addend, long _Value);
142 unsigned char _interlockedbittestandreset(long volatile *, long);
143 unsigned char _interlockedbittestandset(long volatile *, long);
144 void *_InterlockedCompareExchangePointer_HLEAcquire(void *volatile *, void *,
145                                                     void *);
146 void *_InterlockedCompareExchangePointer_HLERelease(void *volatile *, void *,
147                                                     void *);
148 long _InterlockedExchangeAdd_HLEAcquire(long volatile *, long);
149 long _InterlockedExchangeAdd_HLERelease(long volatile *, long);
150 __int64 _InterlockedExchangeAdd64_HLEAcquire(__int64 volatile *, __int64);
151 __int64 _InterlockedExchangeAdd64_HLERelease(__int64 volatile *, __int64);
152 void __attribute__((__deprecated__(
153     "use other intrinsics or C++11 atomics instead"))) _ReadBarrier(void);
154 void __attribute__((__deprecated__(
155     "use other intrinsics or C++11 atomics instead"))) _ReadWriteBarrier(void);
156 unsigned int _rorx_u32(unsigned int, const unsigned int);
157 int _sarx_i32(int, unsigned int);
158 #if __STDC_HOSTED__
159 int __cdecl _setjmp(jmp_buf);
160 #endif
161 unsigned int _shlx_u32(unsigned int, unsigned int);
162 unsigned int _shrx_u32(unsigned int, unsigned int);
163 void _Store_HLERelease(long volatile *, long);
164 void _Store64_HLERelease(__int64 volatile *, __int64);
165 void _StorePointer_HLERelease(void *volatile *, void *);
166 void __attribute__((__deprecated__(
167     "use other intrinsics or C++11 atomics instead"))) _WriteBarrier(void);
168 unsigned __int32 xbegin(void);
169 void _xend(void);
170 
171 /* These additional intrinsics are turned on in x64/amd64/x86_64 mode. */
172 #ifdef __x86_64__
173 void __addgsbyte(unsigned long, unsigned char);
174 void __addgsdword(unsigned long, unsigned long);
175 void __addgsqword(unsigned long, unsigned __int64);
176 void __addgsword(unsigned long, unsigned short);
177 void __faststorefence(void);
178 void __incgsbyte(unsigned long);
179 void __incgsdword(unsigned long);
180 void __incgsqword(unsigned long);
181 void __incgsword(unsigned long);
182 void __movsq(unsigned long long *, unsigned long long const *, size_t);
183 unsigned char __readgsbyte(unsigned long);
184 unsigned long __readgsdword(unsigned long);
185 unsigned __int64 __readgsqword(unsigned long);
186 unsigned short __readgsword(unsigned long);
187 unsigned __int64 __shiftleft128(unsigned __int64 _LowPart,
188                                 unsigned __int64 _HighPart,
189                                 unsigned char _Shift);
190 unsigned __int64 __shiftright128(unsigned __int64 _LowPart,
191                                  unsigned __int64 _HighPart,
192                                  unsigned char _Shift);
193 void __stosq(unsigned __int64 *, unsigned __int64, size_t);
194 unsigned char __vmx_on(unsigned __int64 *);
195 unsigned char __vmx_vmclear(unsigned __int64 *);
196 unsigned char __vmx_vmlaunch(void);
197 unsigned char __vmx_vmptrld(unsigned __int64 *);
198 unsigned char __vmx_vmread(size_t, size_t *);
199 unsigned char __vmx_vmresume(void);
200 unsigned char __vmx_vmwrite(size_t, size_t);
201 void __writegsbyte(unsigned long, unsigned char);
202 void __writegsdword(unsigned long, unsigned long);
203 void __writegsqword(unsigned long, unsigned __int64);
204 void __writegsword(unsigned long, unsigned short);
205 unsigned char _bittest64(__int64 const *, __int64);
206 unsigned char _bittestandcomplement64(__int64 *, __int64);
207 unsigned char _bittestandreset64(__int64 *, __int64);
208 unsigned char _bittestandset64(__int64 *, __int64);
209 long _InterlockedAnd_np(long volatile *_Value, long _Mask);
210 short _InterlockedAnd16_np(short volatile *_Value, short _Mask);
211 __int64 _InterlockedAnd64_np(__int64 volatile *_Value, __int64 _Mask);
212 char _InterlockedAnd8_np(char volatile *_Value, char _Mask);
213 unsigned char _interlockedbittestandreset64(__int64 volatile *, __int64);
214 unsigned char _interlockedbittestandset64(__int64 volatile *, __int64);
215 long _InterlockedCompareExchange_np(long volatile *_Destination, long _Exchange,
216                                     long _Comparand);
217 unsigned char _InterlockedCompareExchange128(__int64 volatile *_Destination,
218                                              __int64 _ExchangeHigh,
219                                              __int64 _ExchangeLow,
220                                              __int64 *_CompareandResult);
221 unsigned char _InterlockedCompareExchange128_np(__int64 volatile *_Destination,
222                                                 __int64 _ExchangeHigh,
223                                                 __int64 _ExchangeLow,
224                                                 __int64 *_ComparandResult);
225 short _InterlockedCompareExchange16_np(short volatile *_Destination,
226                                        short _Exchange, short _Comparand);
227 __int64 _InterlockedCompareExchange64_np(__int64 volatile *_Destination,
228                                          __int64 _Exchange, __int64 _Comparand);
229 void *_InterlockedCompareExchangePointer_np(void *volatile *_Destination,
230                                             void *_Exchange, void *_Comparand);
231 long _InterlockedOr_np(long volatile *_Value, long _Mask);
232 short _InterlockedOr16_np(short volatile *_Value, short _Mask);
233 __int64 _InterlockedOr64_np(__int64 volatile *_Value, __int64 _Mask);
234 char _InterlockedOr8_np(char volatile *_Value, char _Mask);
235 long _InterlockedXor_np(long volatile *_Value, long _Mask);
236 short _InterlockedXor16_np(short volatile *_Value, short _Mask);
237 __int64 _InterlockedXor64_np(__int64 volatile *_Value, __int64 _Mask);
238 char _InterlockedXor8_np(char volatile *_Value, char _Mask);
239 unsigned __int64 _rorx_u64(unsigned __int64, const unsigned int);
240 __int64 _sarx_i64(__int64, unsigned int);
241 unsigned __int64 _shlx_u64(unsigned __int64, unsigned int);
242 unsigned __int64 _shrx_u64(unsigned __int64, unsigned int);
243 __int64 __mulh(__int64, __int64);
244 unsigned __int64 __umulh(unsigned __int64, unsigned __int64);
245 __int64 _mul128(__int64, __int64, __int64*);
246 unsigned __int64 _umul128(unsigned __int64,
247                           unsigned __int64,
248                           unsigned __int64*);
249 
250 #endif /* __x86_64__ */
251 
252 #if defined(__x86_64__) || defined(__arm__) || defined(__aarch64__)
253 
254 unsigned char _BitScanForward64(unsigned long *_Index, unsigned __int64 _Mask);
255 unsigned char _BitScanReverse64(unsigned long *_Index, unsigned __int64 _Mask);
256 
257 #endif
258 
259 #if defined(__i386__) || defined(__x86_64__) || defined(__arm__) || defined(__aarch64__)
260 __int64 _InterlockedDecrement64(__int64 volatile *_Addend);
261 __int64 _InterlockedExchange64(__int64 volatile *_Target, __int64 _Value);
262 __int64 _InterlockedExchangeAdd64(__int64 volatile *_Addend, __int64 _Value);
263 __int64 _InterlockedExchangeSub64(__int64 volatile *_Subend, __int64 _Value);
264 __int64 _InterlockedIncrement64(__int64 volatile *_Addend);
265 __int64 _InterlockedOr64(__int64 volatile *_Value, __int64 _Mask);
266 __int64 _InterlockedXor64(__int64 volatile *_Value, __int64 _Mask);
267 __int64 _InterlockedAnd64(__int64 volatile *_Value, __int64 _Mask);
268 
269 #endif
270 
271 /*----------------------------------------------------------------------------*\
272 |* Interlocked Exchange Add
273 \*----------------------------------------------------------------------------*/
274 #if defined(__arm__) || defined(__aarch64__)
275 char _InterlockedExchangeAdd8_acq(char volatile *_Addend, char _Value);
276 char _InterlockedExchangeAdd8_nf(char volatile *_Addend, char _Value);
277 char _InterlockedExchangeAdd8_rel(char volatile *_Addend, char _Value);
278 short _InterlockedExchangeAdd16_acq(short volatile *_Addend, short _Value);
279 short _InterlockedExchangeAdd16_nf(short volatile *_Addend, short _Value);
280 short _InterlockedExchangeAdd16_rel(short volatile *_Addend, short _Value);
281 long _InterlockedExchangeAdd_acq(long volatile *_Addend, long _Value);
282 long _InterlockedExchangeAdd_nf(long volatile *_Addend, long _Value);
283 long _InterlockedExchangeAdd_rel(long volatile *_Addend, long _Value);
284 __int64 _InterlockedExchangeAdd64_acq(__int64 volatile *_Addend, __int64 _Value);
285 __int64 _InterlockedExchangeAdd64_nf(__int64 volatile *_Addend, __int64 _Value);
286 __int64 _InterlockedExchangeAdd64_rel(__int64 volatile *_Addend, __int64 _Value);
287 #endif
288 /*----------------------------------------------------------------------------*\
289 |* Interlocked Increment
290 \*----------------------------------------------------------------------------*/
291 #if defined(__arm__) || defined(__aarch64__)
292 short _InterlockedIncrement16_acq(short volatile *_Value);
293 short _InterlockedIncrement16_nf(short volatile *_Value);
294 short _InterlockedIncrement16_rel(short volatile *_Value);
295 long _InterlockedIncrement_acq(long volatile *_Value);
296 long _InterlockedIncrement_nf(long volatile *_Value);
297 long _InterlockedIncrement_rel(long volatile *_Value);
298 __int64 _InterlockedIncrement64_acq(__int64 volatile *_Value);
299 __int64 _InterlockedIncrement64_nf(__int64 volatile *_Value);
300 __int64 _InterlockedIncrement64_rel(__int64 volatile *_Value);
301 #endif
302 /*----------------------------------------------------------------------------*\
303 |* Interlocked Decrement
304 \*----------------------------------------------------------------------------*/
305 #if defined(__arm__) || defined(__aarch64__)
306 short _InterlockedDecrement16_acq(short volatile *_Value);
307 short _InterlockedDecrement16_nf(short volatile *_Value);
308 short _InterlockedDecrement16_rel(short volatile *_Value);
309 long _InterlockedDecrement_acq(long volatile *_Value);
310 long _InterlockedDecrement_nf(long volatile *_Value);
311 long _InterlockedDecrement_rel(long volatile *_Value);
312 __int64 _InterlockedDecrement64_acq(__int64 volatile *_Value);
313 __int64 _InterlockedDecrement64_nf(__int64 volatile *_Value);
314 __int64 _InterlockedDecrement64_rel(__int64 volatile *_Value);
315 #endif
316 /*----------------------------------------------------------------------------*\
317 |* Interlocked And
318 \*----------------------------------------------------------------------------*/
319 #if defined(__arm__) || defined(__aarch64__)
320 char _InterlockedAnd8_acq(char volatile *_Value, char _Mask);
321 char _InterlockedAnd8_nf(char volatile *_Value, char _Mask);
322 char _InterlockedAnd8_rel(char volatile *_Value, char _Mask);
323 short _InterlockedAnd16_acq(short volatile *_Value, short _Mask);
324 short _InterlockedAnd16_nf(short volatile *_Value, short _Mask);
325 short _InterlockedAnd16_rel(short volatile *_Value, short _Mask);
326 long _InterlockedAnd_acq(long volatile *_Value, long _Mask);
327 long _InterlockedAnd_nf(long volatile *_Value, long _Mask);
328 long _InterlockedAnd_rel(long volatile *_Value, long _Mask);
329 __int64 _InterlockedAnd64_acq(__int64 volatile *_Value, __int64 _Mask);
330 __int64 _InterlockedAnd64_nf(__int64 volatile *_Value, __int64 _Mask);
331 __int64 _InterlockedAnd64_rel(__int64 volatile *_Value, __int64 _Mask);
332 #endif
333 /*----------------------------------------------------------------------------*\
334 |* Bit Counting and Testing
335 \*----------------------------------------------------------------------------*/
336 #if defined(__arm__) || defined(__aarch64__)
337 unsigned char _interlockedbittestandset_acq(long volatile *_BitBase,
338                                             long _BitPos);
339 unsigned char _interlockedbittestandset_nf(long volatile *_BitBase,
340                                            long _BitPos);
341 unsigned char _interlockedbittestandset_rel(long volatile *_BitBase,
342                                             long _BitPos);
343 unsigned char _interlockedbittestandreset_acq(long volatile *_BitBase,
344                                               long _BitPos);
345 unsigned char _interlockedbittestandreset_nf(long volatile *_BitBase,
346                                              long _BitPos);
347 unsigned char _interlockedbittestandreset_rel(long volatile *_BitBase,
348                                               long _BitPos);
349 #endif
350 /*----------------------------------------------------------------------------*\
351 |* Interlocked Or
352 \*----------------------------------------------------------------------------*/
353 #if defined(__arm__) || defined(__aarch64__)
354 char _InterlockedOr8_acq(char volatile *_Value, char _Mask);
355 char _InterlockedOr8_nf(char volatile *_Value, char _Mask);
356 char _InterlockedOr8_rel(char volatile *_Value, char _Mask);
357 short _InterlockedOr16_acq(short volatile *_Value, short _Mask);
358 short _InterlockedOr16_nf(short volatile *_Value, short _Mask);
359 short _InterlockedOr16_rel(short volatile *_Value, short _Mask);
360 long _InterlockedOr_acq(long volatile *_Value, long _Mask);
361 long _InterlockedOr_nf(long volatile *_Value, long _Mask);
362 long _InterlockedOr_rel(long volatile *_Value, long _Mask);
363 __int64 _InterlockedOr64_acq(__int64 volatile *_Value, __int64 _Mask);
364 __int64 _InterlockedOr64_nf(__int64 volatile *_Value, __int64 _Mask);
365 __int64 _InterlockedOr64_rel(__int64 volatile *_Value, __int64 _Mask);
366 #endif
367 /*----------------------------------------------------------------------------*\
368 |* Interlocked Xor
369 \*----------------------------------------------------------------------------*/
370 #if defined(__arm__) || defined(__aarch64__)
371 char _InterlockedXor8_acq(char volatile *_Value, char _Mask);
372 char _InterlockedXor8_nf(char volatile *_Value, char _Mask);
373 char _InterlockedXor8_rel(char volatile *_Value, char _Mask);
374 short _InterlockedXor16_acq(short volatile *_Value, short _Mask);
375 short _InterlockedXor16_nf(short volatile *_Value, short _Mask);
376 short _InterlockedXor16_rel(short volatile *_Value, short _Mask);
377 long _InterlockedXor_acq(long volatile *_Value, long _Mask);
378 long _InterlockedXor_nf(long volatile *_Value, long _Mask);
379 long _InterlockedXor_rel(long volatile *_Value, long _Mask);
380 __int64 _InterlockedXor64_acq(__int64 volatile *_Value, __int64 _Mask);
381 __int64 _InterlockedXor64_nf(__int64 volatile *_Value, __int64 _Mask);
382 __int64 _InterlockedXor64_rel(__int64 volatile *_Value, __int64 _Mask);
383 #endif
384 /*----------------------------------------------------------------------------*\
385 |* Interlocked Exchange
386 \*----------------------------------------------------------------------------*/
387 #if defined(__arm__) || defined(__aarch64__)
388 char _InterlockedExchange8_acq(char volatile *_Target, char _Value);
389 char _InterlockedExchange8_nf(char volatile *_Target, char _Value);
390 char _InterlockedExchange8_rel(char volatile *_Target, char _Value);
391 short _InterlockedExchange16_acq(short volatile *_Target, short _Value);
392 short _InterlockedExchange16_nf(short volatile *_Target, short _Value);
393 short _InterlockedExchange16_rel(short volatile *_Target, short _Value);
394 long _InterlockedExchange_acq(long volatile *_Target, long _Value);
395 long _InterlockedExchange_nf(long volatile *_Target, long _Value);
396 long _InterlockedExchange_rel(long volatile *_Target, long _Value);
397 __int64 _InterlockedExchange64_acq(__int64 volatile *_Target, __int64 _Value);
398 __int64 _InterlockedExchange64_nf(__int64 volatile *_Target, __int64 _Value);
399 __int64 _InterlockedExchange64_rel(__int64 volatile *_Target, __int64 _Value);
400 #endif
401 /*----------------------------------------------------------------------------*\
402 |* Interlocked Compare Exchange
403 \*----------------------------------------------------------------------------*/
404 #if defined(__arm__) || defined(__aarch64__)
405 char _InterlockedCompareExchange8_acq(char volatile *_Destination,
406                              char _Exchange, char _Comparand);
407 char _InterlockedCompareExchange8_nf(char volatile *_Destination,
408                              char _Exchange, char _Comparand);
409 char _InterlockedCompareExchange8_rel(char volatile *_Destination,
410                              char _Exchange, char _Comparand);
411 short _InterlockedCompareExchange16_acq(short volatile *_Destination,
412                               short _Exchange, short _Comparand);
413 short _InterlockedCompareExchange16_nf(short volatile *_Destination,
414                               short _Exchange, short _Comparand);
415 short _InterlockedCompareExchange16_rel(short volatile *_Destination,
416                               short _Exchange, short _Comparand);
417 long _InterlockedCompareExchange_acq(long volatile *_Destination,
418                               long _Exchange, long _Comparand);
419 long _InterlockedCompareExchange_nf(long volatile *_Destination,
420                               long _Exchange, long _Comparand);
421 long _InterlockedCompareExchange_rel(long volatile *_Destination,
422                               long _Exchange, long _Comparand);
423 __int64 _InterlockedCompareExchange64_acq(__int64 volatile *_Destination,
424                               __int64 _Exchange, __int64 _Comparand);
425 __int64 _InterlockedCompareExchange64_nf(__int64 volatile *_Destination,
426                               __int64 _Exchange, __int64 _Comparand);
427 __int64 _InterlockedCompareExchange64_rel(__int64 volatile *_Destination,
428                               __int64 _Exchange, __int64 _Comparand);
429 #endif
430 
431 /*----------------------------------------------------------------------------*\
432 |* movs, stos
433 \*----------------------------------------------------------------------------*/
434 #if defined(__i386__) || defined(__x86_64__)
435 static __inline__ void __DEFAULT_FN_ATTRS __movsb(unsigned char *__dst,
436                                                   unsigned char const *__src,
437                                                   size_t __n) {
438   __asm__ __volatile__("rep movsb" : "+D"(__dst), "+S"(__src), "+c"(__n)
439                        : : "memory");
440 }
441 static __inline__ void __DEFAULT_FN_ATTRS __movsd(unsigned long *__dst,
442                                                   unsigned long const *__src,
443                                                   size_t __n) {
444   __asm__ __volatile__("rep movsl"
445                        : "+D"(__dst), "+S"(__src), "+c"(__n)
446                        :
447                        : "memory");
448 }
449 static __inline__ void __DEFAULT_FN_ATTRS __movsw(unsigned short *__dst,
450                                                   unsigned short const *__src,
451                                                   size_t __n) {
452   __asm__ __volatile__("rep movsw"
453                        : "+D"(__dst), "+S"(__src), "+c"(__n)
454                        :
455                        : "memory");
456 }
457 static __inline__ void __DEFAULT_FN_ATTRS __stosd(unsigned long *__dst,
458                                                   unsigned long __x,
459                                                   size_t __n) {
460   __asm__ __volatile__("rep stosl"
461                        : "+D"(__dst), "+c"(__n)
462                        : "a"(__x)
463                        : "memory");
464 }
465 static __inline__ void __DEFAULT_FN_ATTRS __stosw(unsigned short *__dst,
466                                                   unsigned short __x,
467                                                   size_t __n) {
468   __asm__ __volatile__("rep stosw"
469                        : "+D"(__dst), "+c"(__n)
470                        : "a"(__x)
471                        : "memory");
472 }
473 #endif
474 #ifdef __x86_64__
475 static __inline__ void __DEFAULT_FN_ATTRS __movsq(
476     unsigned long long *__dst, unsigned long long const *__src, size_t __n) {
477   __asm__ __volatile__("rep movsq"
478                        : "+D"(__dst), "+S"(__src), "+c"(__n)
479                        :
480                        : "memory");
481 }
482 static __inline__ void __DEFAULT_FN_ATTRS __stosq(unsigned __int64 *__dst,
483                                                   unsigned __int64 __x,
484                                                   size_t __n) {
485   __asm__ __volatile__("rep stosq" : "+D"(__dst), "+c"(__n) : "a"(__x)
486                        : "memory");
487 }
488 #endif
489 
490 /*----------------------------------------------------------------------------*\
491 |* Misc
492 \*----------------------------------------------------------------------------*/
493 #if defined(__i386__) || defined(__x86_64__)
494 static __inline__ void __DEFAULT_FN_ATTRS __cpuid(int __info[4], int __level) {
495   __asm__("cpuid"
496           : "=a"(__info[0]), "=b"(__info[1]), "=c"(__info[2]), "=d"(__info[3])
497           : "a"(__level), "c"(0));
498 }
499 static __inline__ void __DEFAULT_FN_ATTRS __cpuidex(int __info[4], int __level,
500                                                     int __ecx) {
501   __asm__("cpuid"
502           : "=a"(__info[0]), "=b"(__info[1]), "=c"(__info[2]), "=d"(__info[3])
503           : "a"(__level), "c"(__ecx));
504 }
505 static __inline__ void __DEFAULT_FN_ATTRS __halt(void) {
506   __asm__ volatile("hlt");
507 }
508 #endif
509 
510 #if defined(__i386__) || defined(__x86_64__) || defined(__aarch64__)
511 static __inline__ void __DEFAULT_FN_ATTRS __nop(void) {
512   __asm__ volatile("nop");
513 }
514 #endif
515 
516 /*----------------------------------------------------------------------------*\
517 |* MS AArch64 specific
518 \*----------------------------------------------------------------------------*/
519 #if defined(__aarch64__)
520 unsigned __int64 __getReg(int);
521 long _InterlockedAdd(long volatile *Addend, long Value);
522 __int64 _ReadStatusReg(int);
523 void _WriteStatusReg(int, __int64);
524 
525 unsigned short __cdecl _byteswap_ushort(unsigned short val);
526 unsigned long __cdecl _byteswap_ulong (unsigned long val);
527 unsigned __int64 __cdecl _byteswap_uint64(unsigned __int64 val);
528 #endif
529 
530 /*----------------------------------------------------------------------------*\
531 |* Privileged intrinsics
532 \*----------------------------------------------------------------------------*/
533 #if defined(__i386__) || defined(__x86_64__)
534 static __inline__ unsigned __int64 __DEFAULT_FN_ATTRS
535 __readmsr(unsigned long __register) {
536   // Loads the contents of a 64-bit model specific register (MSR) specified in
537   // the ECX register into registers EDX:EAX. The EDX register is loaded with
538   // the high-order 32 bits of the MSR and the EAX register is loaded with the
539   // low-order 32 bits. If less than 64 bits are implemented in the MSR being
540   // read, the values returned to EDX:EAX in unimplemented bit locations are
541   // undefined.
542   unsigned long __edx;
543   unsigned long __eax;
544   __asm__ ("rdmsr" : "=d"(__edx), "=a"(__eax) : "c"(__register));
545   return (((unsigned __int64)__edx) << 32) | (unsigned __int64)__eax;
546 }
547 #endif
548 
549 static __inline__ unsigned __LPTRINT_TYPE__ __DEFAULT_FN_ATTRS __readcr3(void) {
550   unsigned __LPTRINT_TYPE__ __cr3_val;
551   __asm__ __volatile__ ("mov %%cr3, %0" : "=r"(__cr3_val) : : "memory");
552   return __cr3_val;
553 }
554 
555 static __inline__ void __DEFAULT_FN_ATTRS
556 __writecr3(unsigned __INTPTR_TYPE__ __cr3_val) {
557   __asm__ ("mov %0, %%cr3" : : "r"(__cr3_val) : "memory");
558 }
559 
560 #ifdef __cplusplus
561 }
562 #endif
563 
564 #undef __LPTRINT_TYPE__
565 
566 #undef __DEFAULT_FN_ATTRS
567 
568 #endif /* __INTRIN_H */
569 #endif /* _MSC_VER */
570