xref: /freebsd/contrib/llvm-project/clang/lib/Headers/intrin.h (revision ec0ea6efa1ad229d75c394c1a9b9cac33af2b1d3)
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_np(__int64 volatile *_Destination,
218                                                 __int64 _ExchangeHigh,
219                                                 __int64 _ExchangeLow,
220                                                 __int64 *_ComparandResult);
221 short _InterlockedCompareExchange16_np(short volatile *_Destination,
222                                        short _Exchange, short _Comparand);
223 __int64 _InterlockedCompareExchange64_np(__int64 volatile *_Destination,
224                                          __int64 _Exchange, __int64 _Comparand);
225 void *_InterlockedCompareExchangePointer_np(void *volatile *_Destination,
226                                             void *_Exchange, void *_Comparand);
227 long _InterlockedOr_np(long volatile *_Value, long _Mask);
228 short _InterlockedOr16_np(short volatile *_Value, short _Mask);
229 __int64 _InterlockedOr64_np(__int64 volatile *_Value, __int64 _Mask);
230 char _InterlockedOr8_np(char volatile *_Value, char _Mask);
231 long _InterlockedXor_np(long volatile *_Value, long _Mask);
232 short _InterlockedXor16_np(short volatile *_Value, short _Mask);
233 __int64 _InterlockedXor64_np(__int64 volatile *_Value, __int64 _Mask);
234 char _InterlockedXor8_np(char volatile *_Value, char _Mask);
235 unsigned __int64 _rorx_u64(unsigned __int64, const unsigned int);
236 __int64 _sarx_i64(__int64, unsigned int);
237 unsigned __int64 _shlx_u64(unsigned __int64, unsigned int);
238 unsigned __int64 _shrx_u64(unsigned __int64, unsigned int);
239 __int64 __mulh(__int64, __int64);
240 unsigned __int64 __umulh(unsigned __int64, unsigned __int64);
241 __int64 _mul128(__int64, __int64, __int64*);
242 unsigned __int64 _umul128(unsigned __int64,
243                           unsigned __int64,
244                           unsigned __int64*);
245 
246 #endif /* __x86_64__ */
247 
248 #if defined(__x86_64__) || defined(__arm__) || defined(__aarch64__)
249 
250 unsigned char _BitScanForward64(unsigned long *_Index, unsigned __int64 _Mask);
251 unsigned char _BitScanReverse64(unsigned long *_Index, unsigned __int64 _Mask);
252 
253 #endif
254 
255 #if defined(__i386__) || defined(__x86_64__) || defined(__arm__) || defined(__aarch64__)
256 __int64 _InterlockedDecrement64(__int64 volatile *_Addend);
257 __int64 _InterlockedExchange64(__int64 volatile *_Target, __int64 _Value);
258 __int64 _InterlockedExchangeAdd64(__int64 volatile *_Addend, __int64 _Value);
259 __int64 _InterlockedExchangeSub64(__int64 volatile *_Subend, __int64 _Value);
260 __int64 _InterlockedIncrement64(__int64 volatile *_Addend);
261 __int64 _InterlockedOr64(__int64 volatile *_Value, __int64 _Mask);
262 __int64 _InterlockedXor64(__int64 volatile *_Value, __int64 _Mask);
263 __int64 _InterlockedAnd64(__int64 volatile *_Value, __int64 _Mask);
264 
265 #endif
266 
267 /*----------------------------------------------------------------------------*\
268 |* Interlocked Exchange Add
269 \*----------------------------------------------------------------------------*/
270 #if defined(__arm__) || defined(__aarch64__)
271 char _InterlockedExchangeAdd8_acq(char volatile *_Addend, char _Value);
272 char _InterlockedExchangeAdd8_nf(char volatile *_Addend, char _Value);
273 char _InterlockedExchangeAdd8_rel(char volatile *_Addend, char _Value);
274 short _InterlockedExchangeAdd16_acq(short volatile *_Addend, short _Value);
275 short _InterlockedExchangeAdd16_nf(short volatile *_Addend, short _Value);
276 short _InterlockedExchangeAdd16_rel(short volatile *_Addend, short _Value);
277 long _InterlockedExchangeAdd_acq(long volatile *_Addend, long _Value);
278 long _InterlockedExchangeAdd_nf(long volatile *_Addend, long _Value);
279 long _InterlockedExchangeAdd_rel(long volatile *_Addend, long _Value);
280 __int64 _InterlockedExchangeAdd64_acq(__int64 volatile *_Addend, __int64 _Value);
281 __int64 _InterlockedExchangeAdd64_nf(__int64 volatile *_Addend, __int64 _Value);
282 __int64 _InterlockedExchangeAdd64_rel(__int64 volatile *_Addend, __int64 _Value);
283 #endif
284 /*----------------------------------------------------------------------------*\
285 |* Interlocked Increment
286 \*----------------------------------------------------------------------------*/
287 #if defined(__arm__) || defined(__aarch64__)
288 short _InterlockedIncrement16_acq(short volatile *_Value);
289 short _InterlockedIncrement16_nf(short volatile *_Value);
290 short _InterlockedIncrement16_rel(short volatile *_Value);
291 long _InterlockedIncrement_acq(long volatile *_Value);
292 long _InterlockedIncrement_nf(long volatile *_Value);
293 long _InterlockedIncrement_rel(long volatile *_Value);
294 __int64 _InterlockedIncrement64_acq(__int64 volatile *_Value);
295 __int64 _InterlockedIncrement64_nf(__int64 volatile *_Value);
296 __int64 _InterlockedIncrement64_rel(__int64 volatile *_Value);
297 #endif
298 /*----------------------------------------------------------------------------*\
299 |* Interlocked Decrement
300 \*----------------------------------------------------------------------------*/
301 #if defined(__arm__) || defined(__aarch64__)
302 short _InterlockedDecrement16_acq(short volatile *_Value);
303 short _InterlockedDecrement16_nf(short volatile *_Value);
304 short _InterlockedDecrement16_rel(short volatile *_Value);
305 long _InterlockedDecrement_acq(long volatile *_Value);
306 long _InterlockedDecrement_nf(long volatile *_Value);
307 long _InterlockedDecrement_rel(long volatile *_Value);
308 __int64 _InterlockedDecrement64_acq(__int64 volatile *_Value);
309 __int64 _InterlockedDecrement64_nf(__int64 volatile *_Value);
310 __int64 _InterlockedDecrement64_rel(__int64 volatile *_Value);
311 #endif
312 /*----------------------------------------------------------------------------*\
313 |* Interlocked And
314 \*----------------------------------------------------------------------------*/
315 #if defined(__arm__) || defined(__aarch64__)
316 char _InterlockedAnd8_acq(char volatile *_Value, char _Mask);
317 char _InterlockedAnd8_nf(char volatile *_Value, char _Mask);
318 char _InterlockedAnd8_rel(char volatile *_Value, char _Mask);
319 short _InterlockedAnd16_acq(short volatile *_Value, short _Mask);
320 short _InterlockedAnd16_nf(short volatile *_Value, short _Mask);
321 short _InterlockedAnd16_rel(short volatile *_Value, short _Mask);
322 long _InterlockedAnd_acq(long volatile *_Value, long _Mask);
323 long _InterlockedAnd_nf(long volatile *_Value, long _Mask);
324 long _InterlockedAnd_rel(long volatile *_Value, long _Mask);
325 __int64 _InterlockedAnd64_acq(__int64 volatile *_Value, __int64 _Mask);
326 __int64 _InterlockedAnd64_nf(__int64 volatile *_Value, __int64 _Mask);
327 __int64 _InterlockedAnd64_rel(__int64 volatile *_Value, __int64 _Mask);
328 #endif
329 /*----------------------------------------------------------------------------*\
330 |* Bit Counting and Testing
331 \*----------------------------------------------------------------------------*/
332 #if defined(__arm__) || defined(__aarch64__)
333 unsigned char _interlockedbittestandset_acq(long volatile *_BitBase,
334                                             long _BitPos);
335 unsigned char _interlockedbittestandset_nf(long volatile *_BitBase,
336                                            long _BitPos);
337 unsigned char _interlockedbittestandset_rel(long volatile *_BitBase,
338                                             long _BitPos);
339 unsigned char _interlockedbittestandreset_acq(long volatile *_BitBase,
340                                               long _BitPos);
341 unsigned char _interlockedbittestandreset_nf(long volatile *_BitBase,
342                                              long _BitPos);
343 unsigned char _interlockedbittestandreset_rel(long volatile *_BitBase,
344                                               long _BitPos);
345 #endif
346 /*----------------------------------------------------------------------------*\
347 |* Interlocked Or
348 \*----------------------------------------------------------------------------*/
349 #if defined(__arm__) || defined(__aarch64__)
350 char _InterlockedOr8_acq(char volatile *_Value, char _Mask);
351 char _InterlockedOr8_nf(char volatile *_Value, char _Mask);
352 char _InterlockedOr8_rel(char volatile *_Value, char _Mask);
353 short _InterlockedOr16_acq(short volatile *_Value, short _Mask);
354 short _InterlockedOr16_nf(short volatile *_Value, short _Mask);
355 short _InterlockedOr16_rel(short volatile *_Value, short _Mask);
356 long _InterlockedOr_acq(long volatile *_Value, long _Mask);
357 long _InterlockedOr_nf(long volatile *_Value, long _Mask);
358 long _InterlockedOr_rel(long volatile *_Value, long _Mask);
359 __int64 _InterlockedOr64_acq(__int64 volatile *_Value, __int64 _Mask);
360 __int64 _InterlockedOr64_nf(__int64 volatile *_Value, __int64 _Mask);
361 __int64 _InterlockedOr64_rel(__int64 volatile *_Value, __int64 _Mask);
362 #endif
363 /*----------------------------------------------------------------------------*\
364 |* Interlocked Xor
365 \*----------------------------------------------------------------------------*/
366 #if defined(__arm__) || defined(__aarch64__)
367 char _InterlockedXor8_acq(char volatile *_Value, char _Mask);
368 char _InterlockedXor8_nf(char volatile *_Value, char _Mask);
369 char _InterlockedXor8_rel(char volatile *_Value, char _Mask);
370 short _InterlockedXor16_acq(short volatile *_Value, short _Mask);
371 short _InterlockedXor16_nf(short volatile *_Value, short _Mask);
372 short _InterlockedXor16_rel(short volatile *_Value, short _Mask);
373 long _InterlockedXor_acq(long volatile *_Value, long _Mask);
374 long _InterlockedXor_nf(long volatile *_Value, long _Mask);
375 long _InterlockedXor_rel(long volatile *_Value, long _Mask);
376 __int64 _InterlockedXor64_acq(__int64 volatile *_Value, __int64 _Mask);
377 __int64 _InterlockedXor64_nf(__int64 volatile *_Value, __int64 _Mask);
378 __int64 _InterlockedXor64_rel(__int64 volatile *_Value, __int64 _Mask);
379 #endif
380 /*----------------------------------------------------------------------------*\
381 |* Interlocked Exchange
382 \*----------------------------------------------------------------------------*/
383 #if defined(__arm__) || defined(__aarch64__)
384 char _InterlockedExchange8_acq(char volatile *_Target, char _Value);
385 char _InterlockedExchange8_nf(char volatile *_Target, char _Value);
386 char _InterlockedExchange8_rel(char volatile *_Target, char _Value);
387 short _InterlockedExchange16_acq(short volatile *_Target, short _Value);
388 short _InterlockedExchange16_nf(short volatile *_Target, short _Value);
389 short _InterlockedExchange16_rel(short volatile *_Target, short _Value);
390 long _InterlockedExchange_acq(long volatile *_Target, long _Value);
391 long _InterlockedExchange_nf(long volatile *_Target, long _Value);
392 long _InterlockedExchange_rel(long volatile *_Target, long _Value);
393 __int64 _InterlockedExchange64_acq(__int64 volatile *_Target, __int64 _Value);
394 __int64 _InterlockedExchange64_nf(__int64 volatile *_Target, __int64 _Value);
395 __int64 _InterlockedExchange64_rel(__int64 volatile *_Target, __int64 _Value);
396 #endif
397 /*----------------------------------------------------------------------------*\
398 |* Interlocked Compare Exchange
399 \*----------------------------------------------------------------------------*/
400 #if defined(__arm__) || defined(__aarch64__)
401 char _InterlockedCompareExchange8_acq(char volatile *_Destination,
402                              char _Exchange, char _Comparand);
403 char _InterlockedCompareExchange8_nf(char volatile *_Destination,
404                              char _Exchange, char _Comparand);
405 char _InterlockedCompareExchange8_rel(char volatile *_Destination,
406                              char _Exchange, char _Comparand);
407 short _InterlockedCompareExchange16_acq(short volatile *_Destination,
408                               short _Exchange, short _Comparand);
409 short _InterlockedCompareExchange16_nf(short volatile *_Destination,
410                               short _Exchange, short _Comparand);
411 short _InterlockedCompareExchange16_rel(short volatile *_Destination,
412                               short _Exchange, short _Comparand);
413 long _InterlockedCompareExchange_acq(long volatile *_Destination,
414                               long _Exchange, long _Comparand);
415 long _InterlockedCompareExchange_nf(long volatile *_Destination,
416                               long _Exchange, long _Comparand);
417 long _InterlockedCompareExchange_rel(long volatile *_Destination,
418                               long _Exchange, long _Comparand);
419 __int64 _InterlockedCompareExchange64_acq(__int64 volatile *_Destination,
420                               __int64 _Exchange, __int64 _Comparand);
421 __int64 _InterlockedCompareExchange64_nf(__int64 volatile *_Destination,
422                               __int64 _Exchange, __int64 _Comparand);
423 __int64 _InterlockedCompareExchange64_rel(__int64 volatile *_Destination,
424                               __int64 _Exchange, __int64 _Comparand);
425 #endif
426 #if defined(__x86_64__) || defined(__aarch64__)
427 unsigned char _InterlockedCompareExchange128(__int64 volatile *_Destination,
428                                              __int64 _ExchangeHigh,
429                                              __int64 _ExchangeLow,
430                                              __int64 *_ComparandResult);
431 #endif
432 #if defined(__aarch64__)
433 unsigned char _InterlockedCompareExchange128_acq(__int64 volatile *_Destination,
434                                                  __int64 _ExchangeHigh,
435                                                  __int64 _ExchangeLow,
436                                                  __int64 *_ComparandResult);
437 unsigned char _InterlockedCompareExchange128_nf(__int64 volatile *_Destination,
438                                                 __int64 _ExchangeHigh,
439                                                 __int64 _ExchangeLow,
440                                                 __int64 *_ComparandResult);
441 unsigned char _InterlockedCompareExchange128_rel(__int64 volatile *_Destination,
442                                                  __int64 _ExchangeHigh,
443                                                  __int64 _ExchangeLow,
444                                                  __int64 *_ComparandResult);
445 #endif
446 
447 /*----------------------------------------------------------------------------*\
448 |* movs, stos
449 \*----------------------------------------------------------------------------*/
450 #if defined(__i386__) || defined(__x86_64__)
451 static __inline__ void __DEFAULT_FN_ATTRS __movsb(unsigned char *__dst,
452                                                   unsigned char const *__src,
453                                                   size_t __n) {
454 #if defined(__x86_64__)
455   __asm__ __volatile__("rep movsb"
456                        : "+D"(__dst), "+S"(__src), "+c"(__n)
457                        :
458                        : "memory");
459 #else
460   __asm__ __volatile__("xchg %%esi, %1\nrep movsb\nxchg %%esi, %1"
461                        : "+D"(__dst), "+r"(__src), "+c"(__n)
462                        :
463                        : "memory");
464 #endif
465 }
466 static __inline__ void __DEFAULT_FN_ATTRS __movsd(unsigned long *__dst,
467                                                   unsigned long const *__src,
468                                                   size_t __n) {
469 #if defined(__x86_64__)
470   __asm__ __volatile__("rep movsl"
471                        : "+D"(__dst), "+S"(__src), "+c"(__n)
472                        :
473                        : "memory");
474 #else
475   __asm__ __volatile__("xchg %%esi, %1\nrep movsl\nxchg %%esi, %1"
476                        : "+D"(__dst), "+r"(__src), "+c"(__n)
477                        :
478                        : "memory");
479 #endif
480 }
481 static __inline__ void __DEFAULT_FN_ATTRS __movsw(unsigned short *__dst,
482                                                   unsigned short const *__src,
483                                                   size_t __n) {
484 #if defined(__x86_64__)
485   __asm__ __volatile__("rep movsw"
486                        : "+D"(__dst), "+S"(__src), "+c"(__n)
487                        :
488                        : "memory");
489 #else
490   __asm__ __volatile__("xchg %%esi, %1\nrep movsw\nxchg %%esi, %1"
491                        : "+D"(__dst), "+r"(__src), "+c"(__n)
492                        :
493                        : "memory");
494 #endif
495 }
496 static __inline__ void __DEFAULT_FN_ATTRS __stosd(unsigned long *__dst,
497                                                   unsigned long __x,
498                                                   size_t __n) {
499   __asm__ __volatile__("rep stosl"
500                        : "+D"(__dst), "+c"(__n)
501                        : "a"(__x)
502                        : "memory");
503 }
504 static __inline__ void __DEFAULT_FN_ATTRS __stosw(unsigned short *__dst,
505                                                   unsigned short __x,
506                                                   size_t __n) {
507   __asm__ __volatile__("rep stosw"
508                        : "+D"(__dst), "+c"(__n)
509                        : "a"(__x)
510                        : "memory");
511 }
512 #endif
513 #ifdef __x86_64__
514 static __inline__ void __DEFAULT_FN_ATTRS __movsq(
515     unsigned long long *__dst, unsigned long long const *__src, size_t __n) {
516   __asm__ __volatile__("rep movsq"
517                        : "+D"(__dst), "+S"(__src), "+c"(__n)
518                        :
519                        : "memory");
520 }
521 static __inline__ void __DEFAULT_FN_ATTRS __stosq(unsigned __int64 *__dst,
522                                                   unsigned __int64 __x,
523                                                   size_t __n) {
524   __asm__ __volatile__("rep stosq" : "+D"(__dst), "+c"(__n) : "a"(__x)
525                        : "memory");
526 }
527 #endif
528 
529 /*----------------------------------------------------------------------------*\
530 |* Misc
531 \*----------------------------------------------------------------------------*/
532 #if defined(__i386__) || defined(__x86_64__)
533 #if defined(__i386__)
534 #define __cpuid_count(__leaf, __count, __eax, __ebx, __ecx, __edx)             \
535   __asm("cpuid"                                                                \
536         : "=a"(__eax), "=b"(__ebx), "=c"(__ecx), "=d"(__edx)                   \
537         : "0"(__leaf), "2"(__count))
538 #else
539 /* x86-64 uses %rbx as the base register, so preserve it. */
540 #define __cpuid_count(__leaf, __count, __eax, __ebx, __ecx, __edx)             \
541   __asm("xchgq %%rbx,%q1\n"                                                    \
542         "cpuid\n"                                                              \
543         "xchgq %%rbx,%q1"                                                      \
544         : "=a"(__eax), "=r"(__ebx), "=c"(__ecx), "=d"(__edx)                   \
545         : "0"(__leaf), "2"(__count))
546 #endif
547 static __inline__ void __DEFAULT_FN_ATTRS __cpuid(int __info[4], int __level) {
548   __cpuid_count(__level, 0, __info[0], __info[1], __info[2], __info[3]);
549 }
550 static __inline__ void __DEFAULT_FN_ATTRS __cpuidex(int __info[4], int __level,
551                                                     int __ecx) {
552   __cpuid_count(__level, __ecx, __info[0], __info[1], __info[2], __info[3]);
553 }
554 static __inline__ void __DEFAULT_FN_ATTRS __halt(void) {
555   __asm__ volatile("hlt");
556 }
557 #endif
558 
559 #if defined(__i386__) || defined(__x86_64__) || defined(__aarch64__)
560 static __inline__ void __DEFAULT_FN_ATTRS __nop(void) {
561   __asm__ volatile("nop");
562 }
563 #endif
564 
565 /*----------------------------------------------------------------------------*\
566 |* MS AArch64 specific
567 \*----------------------------------------------------------------------------*/
568 #if defined(__aarch64__)
569 unsigned __int64 __getReg(int);
570 long _InterlockedAdd(long volatile *Addend, long Value);
571 __int64 _ReadStatusReg(int);
572 void _WriteStatusReg(int, __int64);
573 
574 unsigned short __cdecl _byteswap_ushort(unsigned short val);
575 unsigned long __cdecl _byteswap_ulong (unsigned long val);
576 unsigned __int64 __cdecl _byteswap_uint64(unsigned __int64 val);
577 
578 __int64 __mulh(__int64 __a, __int64 __b);
579 unsigned __int64 __umulh(unsigned __int64 __a, unsigned __int64 __b);
580 #endif
581 
582 /*----------------------------------------------------------------------------*\
583 |* Privileged intrinsics
584 \*----------------------------------------------------------------------------*/
585 #if defined(__i386__) || defined(__x86_64__)
586 static __inline__ unsigned __int64 __DEFAULT_FN_ATTRS
587 __readmsr(unsigned long __register) {
588   // Loads the contents of a 64-bit model specific register (MSR) specified in
589   // the ECX register into registers EDX:EAX. The EDX register is loaded with
590   // the high-order 32 bits of the MSR and the EAX register is loaded with the
591   // low-order 32 bits. If less than 64 bits are implemented in the MSR being
592   // read, the values returned to EDX:EAX in unimplemented bit locations are
593   // undefined.
594   unsigned long __edx;
595   unsigned long __eax;
596   __asm__ ("rdmsr" : "=d"(__edx), "=a"(__eax) : "c"(__register));
597   return (((unsigned __int64)__edx) << 32) | (unsigned __int64)__eax;
598 }
599 #endif
600 
601 static __inline__ unsigned __LPTRINT_TYPE__ __DEFAULT_FN_ATTRS __readcr3(void) {
602   unsigned __LPTRINT_TYPE__ __cr3_val;
603   __asm__ __volatile__ ("mov %%cr3, %0" : "=r"(__cr3_val) : : "memory");
604   return __cr3_val;
605 }
606 
607 static __inline__ void __DEFAULT_FN_ATTRS
608 __writecr3(unsigned __INTPTR_TYPE__ __cr3_val) {
609   __asm__ ("mov %0, %%cr3" : : "r"(__cr3_val) : "memory");
610 }
611 
612 #ifdef __cplusplus
613 }
614 #endif
615 
616 #undef __LPTRINT_TYPE__
617 
618 #undef __DEFAULT_FN_ATTRS
619 
620 #endif /* __INTRIN_H */
621 #endif /* _MSC_VER */
622