1 /* ===-------- intrin.h ---------------------------------------------------===
3 * Permission is hereby granted, free of charge, to any person obtaining a copy
4 * of this software and associated documentation files (the "Software"), to deal
5 * in the Software without restriction, including without limitation the rights
6 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7 * copies of the Software, and to permit persons to whom the Software is
8 * furnished to do so, subject to the following conditions:
10 * The above copyright notice and this permission notice shall be included in
11 * all copies or substantial portions of the Software.
13 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21 *===-----------------------------------------------------------------------===
24 /* Only include this if we're compiling for the windows platform. */
26 #include_next <intrin.h>
32 /* First include the standard intrinsics. */
33 #if defined(__i386__) || defined(__x86_64__)
34 #include <x86intrin.h>
41 /* For the definition of jmp_buf. */
46 /* Define the default attributes for the functions in this file. */
47 #define __DEFAULT_FN_ATTRS __attribute__((__always_inline__, __nodebug__))
54 /* And the random ones that aren't in those files. */
55 __m64 _m_from_float(float);
56 float _m_to_float(__m64);
59 /* Other assorted instruction intrinsics. */
60 void __addfsbyte(unsigned long, unsigned char);
61 void __addfsdword(unsigned long, unsigned long);
62 void __addfsword(unsigned long, unsigned short);
63 void __code_seg(const char *);
65 void __cpuid(int[4], int);
67 void __cpuidex(int[4], int, int);
69 __int64 __emul(int, int);
71 unsigned __int64 __emulu(unsigned int, unsigned int);
72 unsigned int __getcallerseflags(void);
75 unsigned char __inbyte(unsigned short);
76 void __inbytestring(unsigned short, unsigned char *, unsigned long);
77 void __incfsbyte(unsigned long);
78 void __incfsdword(unsigned long);
79 void __incfsword(unsigned long);
80 unsigned long __indword(unsigned short);
81 void __indwordstring(unsigned short, unsigned long *, unsigned long);
82 void __invlpg(void *);
83 unsigned short __inword(unsigned short);
84 void __inwordstring(unsigned short, unsigned short *, unsigned long);
86 unsigned __int64 __ll_lshift(unsigned __int64, int);
87 __int64 __ll_rshift(__int64, int);
88 void __llwpcb(void *);
89 unsigned char __lwpins32(unsigned int, unsigned int, unsigned int);
90 void __lwpval32(unsigned int, unsigned int, unsigned int);
91 unsigned int __lzcnt(unsigned int);
92 unsigned short __lzcnt16(unsigned short);
94 void __movsb(unsigned char *, unsigned char const *, size_t);
96 void __movsd(unsigned long *, unsigned long const *, size_t);
98 void __movsw(unsigned short *, unsigned short const *, size_t);
101 void __nvreg_restore_fence(void);
102 void __nvreg_save_fence(void);
103 void __outbyte(unsigned short, unsigned char);
104 void __outbytestring(unsigned short, unsigned char *, unsigned long);
105 void __outdword(unsigned short, unsigned long);
106 void __outdwordstring(unsigned short, unsigned long *, unsigned long);
107 void __outword(unsigned short, unsigned short);
108 void __outwordstring(unsigned short, unsigned short *, unsigned long);
109 unsigned long __readcr0(void);
110 unsigned long __readcr2(void);
112 unsigned long __readcr3(void);
113 unsigned long __readcr4(void);
114 unsigned long __readcr8(void);
115 unsigned int __readdr(unsigned int);
118 unsigned char __readfsbyte(unsigned long);
120 unsigned __int64 __readfsqword(unsigned long);
122 unsigned short __readfsword(unsigned long);
125 unsigned __int64 __readmsr(unsigned long);
126 unsigned __int64 __readpmc(unsigned long);
127 unsigned long __segmentlimit(unsigned long);
129 void *__slwpcb(void);
131 void __stosb(unsigned char *, unsigned char, size_t);
133 void __stosd(unsigned long *, unsigned long, size_t);
135 void __stosw(unsigned short *, unsigned short, size_t);
136 void __svm_clgi(void);
137 void __svm_invlpga(void *, int);
138 void __svm_skinit(int);
139 void __svm_stgi(void);
140 void __svm_vmload(size_t);
141 void __svm_vmrun(size_t);
142 void __svm_vmsave(size_t);
143 unsigned __int64 __ull_rshift(unsigned __int64, int);
144 void __vmx_off(void);
145 void __vmx_vmptrst(unsigned __int64 *);
147 void __writecr0(unsigned int);
149 void __writecr3(unsigned int);
150 void __writecr4(unsigned int);
151 void __writecr8(unsigned int);
152 void __writedr(unsigned int, unsigned int);
153 void __writefsbyte(unsigned long, unsigned char);
154 void __writefsdword(unsigned long, unsigned long);
155 void __writefsqword(unsigned long, unsigned __int64);
156 void __writefsword(unsigned long, unsigned short);
157 void __writemsr(unsigned long, unsigned __int64);
159 void *_AddressOfReturnAddress(void);
161 unsigned char _BitScanForward(unsigned long *_Index, unsigned long _Mask);
163 unsigned char _BitScanReverse(unsigned long *_Index, unsigned long _Mask);
165 unsigned char _bittest(long const *, long);
167 unsigned char _bittestandcomplement(long *, long);
169 unsigned char _bittestandreset(long *, long);
171 unsigned char _bittestandset(long *, long);
172 void __cdecl _disable(void);
173 void __cdecl _enable(void);
174 long _InterlockedAddLargeStatistic(__int64 volatile *_Addend, long _Value);
175 unsigned char _interlockedbittestandreset(long volatile *, long);
176 unsigned char _interlockedbittestandset(long volatile *, long);
177 long _InterlockedCompareExchange_HLEAcquire(long volatile *, long, long);
178 long _InterlockedCompareExchange_HLERelease(long volatile *, long, long);
179 __int64 _InterlockedcompareExchange64_HLEAcquire(__int64 volatile *, __int64,
181 __int64 _InterlockedCompareExchange64_HLERelease(__int64 volatile *, __int64,
183 void *_InterlockedCompareExchangePointer_HLEAcquire(void *volatile *, void *,
185 void *_InterlockedCompareExchangePointer_HLERelease(void *volatile *, void *,
187 long _InterlockedExchangeAdd_HLEAcquire(long volatile *, long);
188 long _InterlockedExchangeAdd_HLERelease(long volatile *, long);
189 __int64 _InterlockedExchangeAdd64_HLEAcquire(__int64 volatile *, __int64);
190 __int64 _InterlockedExchangeAdd64_HLERelease(__int64 volatile *, __int64);
191 void __cdecl _invpcid(unsigned int, void *);
192 static __inline__ void
193 __attribute__((__deprecated__("use other intrinsics or C++11 atomics instead")))
195 static __inline__ void
196 __attribute__((__deprecated__("use other intrinsics or C++11 atomics instead")))
197 _ReadWriteBarrier(void);
198 unsigned int _rorx_u32(unsigned int, const unsigned int);
199 int _sarx_i32(int, unsigned int);
201 int __cdecl _setjmp(jmp_buf);
203 unsigned int _shlx_u32(unsigned int, unsigned int);
204 unsigned int _shrx_u32(unsigned int, unsigned int);
205 void _Store_HLERelease(long volatile *, long);
206 void _Store64_HLERelease(__int64 volatile *, __int64);
207 void _StorePointer_HLERelease(void *volatile *, void *);
208 static __inline__ void
209 __attribute__((__deprecated__("use other intrinsics or C++11 atomics instead")))
211 unsigned __int32 xbegin(void);
214 #define _XCR_XFEATURE_ENABLED_MASK 0
215 unsigned __int64 __cdecl _xgetbv(unsigned int);
216 void __cdecl _xsetbv(unsigned int, unsigned __int64);
218 /* These additional intrinsics are turned on in x64/amd64/x86_64 mode. */
220 void __addgsbyte(unsigned long, unsigned char);
221 void __addgsdword(unsigned long, unsigned long);
222 void __addgsqword(unsigned long, unsigned __int64);
223 void __addgsword(unsigned long, unsigned short);
225 void __faststorefence(void);
226 void __incgsbyte(unsigned long);
227 void __incgsdword(unsigned long);
228 void __incgsqword(unsigned long);
229 void __incgsword(unsigned long);
230 unsigned char __lwpins64(unsigned __int64, unsigned int, unsigned int);
231 void __lwpval64(unsigned __int64, unsigned int, unsigned int);
232 unsigned __int64 __lzcnt64(unsigned __int64);
234 void __movsq(unsigned long long *, unsigned long long const *, size_t);
236 unsigned char __readgsbyte(unsigned long);
238 unsigned long __readgsdword(unsigned long);
240 unsigned __int64 __readgsqword(unsigned long);
241 unsigned short __readgsword(unsigned long);
242 unsigned __int64 __shiftleft128(unsigned __int64 _LowPart,
243 unsigned __int64 _HighPart,
244 unsigned char _Shift);
245 unsigned __int64 __shiftright128(unsigned __int64 _LowPart,
246 unsigned __int64 _HighPart,
247 unsigned char _Shift);
249 void __stosq(unsigned __int64 *, unsigned __int64, size_t);
250 unsigned char __vmx_on(unsigned __int64 *);
251 unsigned char __vmx_vmclear(unsigned __int64 *);
252 unsigned char __vmx_vmlaunch(void);
253 unsigned char __vmx_vmptrld(unsigned __int64 *);
254 unsigned char __vmx_vmread(size_t, size_t *);
255 unsigned char __vmx_vmresume(void);
256 unsigned char __vmx_vmwrite(size_t, size_t);
257 void __writegsbyte(unsigned long, unsigned char);
258 void __writegsdword(unsigned long, unsigned long);
259 void __writegsqword(unsigned long, unsigned __int64);
260 void __writegsword(unsigned long, unsigned short);
262 unsigned char _BitScanForward64(unsigned long *_Index, unsigned __int64 _Mask);
264 unsigned char _BitScanReverse64(unsigned long *_Index, unsigned __int64 _Mask);
266 unsigned char _bittest64(__int64 const *, __int64);
268 unsigned char _bittestandcomplement64(__int64 *, __int64);
270 unsigned char _bittestandreset64(__int64 *, __int64);
272 unsigned char _bittestandset64(__int64 *, __int64);
273 long _InterlockedAnd_np(long volatile *_Value, long _Mask);
274 short _InterlockedAnd16_np(short volatile *_Value, short _Mask);
275 __int64 _InterlockedAnd64_np(__int64 volatile *_Value, __int64 _Mask);
276 char _InterlockedAnd8_np(char volatile *_Value, char _Mask);
277 unsigned char _interlockedbittestandreset64(__int64 volatile *, __int64);
279 unsigned char _interlockedbittestandset64(__int64 volatile *, __int64);
280 long _InterlockedCompareExchange_np(long volatile *_Destination, long _Exchange,
282 unsigned char _InterlockedCompareExchange128(__int64 volatile *_Destination,
283 __int64 _ExchangeHigh,
284 __int64 _ExchangeLow,
285 __int64 *_CompareandResult);
286 unsigned char _InterlockedCompareExchange128_np(__int64 volatile *_Destination,
287 __int64 _ExchangeHigh,
288 __int64 _ExchangeLow,
289 __int64 *_ComparandResult);
290 short _InterlockedCompareExchange16_np(short volatile *_Destination,
291 short _Exchange, short _Comparand);
292 __int64 _InterlockedCompareExchange64_HLEAcquire(__int64 volatile *, __int64,
294 __int64 _InterlockedCompareExchange64_HLERelease(__int64 volatile *, __int64,
296 __int64 _InterlockedCompareExchange64_np(__int64 volatile *_Destination,
297 __int64 _Exchange, __int64 _Comparand);
298 void *_InterlockedCompareExchangePointer_np(void *volatile *_Destination,
299 void *_Exchange, void *_Comparand);
300 long _InterlockedOr_np(long volatile *_Value, long _Mask);
301 short _InterlockedOr16_np(short volatile *_Value, short _Mask);
302 __int64 _InterlockedOr64_np(__int64 volatile *_Value, __int64 _Mask);
303 char _InterlockedOr8_np(char volatile *_Value, char _Mask);
304 long _InterlockedXor_np(long volatile *_Value, long _Mask);
305 short _InterlockedXor16_np(short volatile *_Value, short _Mask);
306 __int64 _InterlockedXor64_np(__int64 volatile *_Value, __int64 _Mask);
307 char _InterlockedXor8_np(char volatile *_Value, char _Mask);
308 unsigned __int64 _rorx_u64(unsigned __int64, const unsigned int);
309 __int64 _sarx_i64(__int64, unsigned int);
310 unsigned __int64 _shlx_u64(unsigned __int64, unsigned int);
311 unsigned __int64 _shrx_u64(unsigned __int64, unsigned int);
313 __int64 __mulh(__int64, __int64);
315 unsigned __int64 __umulh(unsigned __int64, unsigned __int64);
317 __int64 _mul128(__int64, __int64, __int64*);
319 unsigned __int64 _umul128(unsigned __int64,
323 #endif /* __x86_64__ */
325 #if defined(__x86_64__) || defined(__arm__)
328 __int64 _InterlockedDecrement64(__int64 volatile *_Addend);
330 __int64 _InterlockedExchange64(__int64 volatile *_Target, __int64 _Value);
332 __int64 _InterlockedExchangeAdd64(__int64 volatile *_Addend, __int64 _Value);
334 __int64 _InterlockedExchangeSub64(__int64 volatile *_Subend, __int64 _Value);
336 __int64 _InterlockedIncrement64(__int64 volatile *_Addend);
338 __int64 _InterlockedOr64(__int64 volatile *_Value, __int64 _Mask);
340 __int64 _InterlockedXor64(__int64 volatile *_Value, __int64 _Mask);
342 __int64 _InterlockedAnd64(__int64 volatile *_Value, __int64 _Mask);
346 /*----------------------------------------------------------------------------*\
347 |* Bit Counting and Testing
348 \*----------------------------------------------------------------------------*/
349 static __inline__ unsigned char __DEFAULT_FN_ATTRS
350 _bittest(long const *_BitBase, long _BitPos) {
351 return (*_BitBase >> _BitPos) & 1;
353 static __inline__ unsigned char __DEFAULT_FN_ATTRS
354 _bittestandcomplement(long *_BitBase, long _BitPos) {
355 unsigned char _Res = (*_BitBase >> _BitPos) & 1;
356 *_BitBase = *_BitBase ^ (1 << _BitPos);
359 static __inline__ unsigned char __DEFAULT_FN_ATTRS
360 _bittestandreset(long *_BitBase, long _BitPos) {
361 unsigned char _Res = (*_BitBase >> _BitPos) & 1;
362 *_BitBase = *_BitBase & ~(1 << _BitPos);
365 static __inline__ unsigned char __DEFAULT_FN_ATTRS
366 _bittestandset(long *_BitBase, long _BitPos) {
367 unsigned char _Res = (*_BitBase >> _BitPos) & 1;
368 *_BitBase = *_BitBase | (1 << _BitPos);
371 #if defined(__arm__) || defined(__aarch64__)
372 static __inline__ unsigned char __DEFAULT_FN_ATTRS
373 _interlockedbittestandset_acq(long volatile *_BitBase, long _BitPos) {
374 long _PrevVal = __atomic_fetch_or(_BitBase, 1l << _BitPos, __ATOMIC_ACQUIRE);
375 return (_PrevVal >> _BitPos) & 1;
377 static __inline__ unsigned char __DEFAULT_FN_ATTRS
378 _interlockedbittestandset_nf(long volatile *_BitBase, long _BitPos) {
379 long _PrevVal = __atomic_fetch_or(_BitBase, 1l << _BitPos, __ATOMIC_RELAXED);
380 return (_PrevVal >> _BitPos) & 1;
382 static __inline__ unsigned char __DEFAULT_FN_ATTRS
383 _interlockedbittestandset_rel(long volatile *_BitBase, long _BitPos) {
384 long _PrevVal = __atomic_fetch_or(_BitBase, 1l << _BitPos, __ATOMIC_RELEASE);
385 return (_PrevVal >> _BitPos) & 1;
389 static __inline__ unsigned char __DEFAULT_FN_ATTRS
390 _bittest64(__int64 const *_BitBase, __int64 _BitPos) {
391 return (*_BitBase >> _BitPos) & 1;
393 static __inline__ unsigned char __DEFAULT_FN_ATTRS
394 _bittestandcomplement64(__int64 *_BitBase, __int64 _BitPos) {
395 unsigned char _Res = (*_BitBase >> _BitPos) & 1;
396 *_BitBase = *_BitBase ^ (1ll << _BitPos);
399 static __inline__ unsigned char __DEFAULT_FN_ATTRS
400 _bittestandreset64(__int64 *_BitBase, __int64 _BitPos) {
401 unsigned char _Res = (*_BitBase >> _BitPos) & 1;
402 *_BitBase = *_BitBase & ~(1ll << _BitPos);
405 static __inline__ unsigned char __DEFAULT_FN_ATTRS
406 _bittestandset64(__int64 *_BitBase, __int64 _BitPos) {
407 unsigned char _Res = (*_BitBase >> _BitPos) & 1;
408 *_BitBase = *_BitBase | (1ll << _BitPos);
411 static __inline__ unsigned char __DEFAULT_FN_ATTRS
412 _interlockedbittestandset64(__int64 volatile *_BitBase, __int64 _BitPos) {
414 __atomic_fetch_or(_BitBase, 1ll << _BitPos, __ATOMIC_SEQ_CST);
415 return (_PrevVal >> _BitPos) & 1;
418 /*----------------------------------------------------------------------------*\
419 |* Interlocked Exchange Add
420 \*----------------------------------------------------------------------------*/
421 #if defined(__arm__) || defined(__aarch64__)
422 static __inline__ char __DEFAULT_FN_ATTRS
423 _InterlockedExchangeAdd8_acq(char volatile *_Addend, char _Value) {
424 return __atomic_fetch_add(_Addend, _Value, __ATOMIC_ACQUIRE);
426 static __inline__ char __DEFAULT_FN_ATTRS
427 _InterlockedExchangeAdd8_nf(char volatile *_Addend, char _Value) {
428 return __atomic_fetch_add(_Addend, _Value, __ATOMIC_RELAXED);
430 static __inline__ char __DEFAULT_FN_ATTRS
431 _InterlockedExchangeAdd8_rel(char volatile *_Addend, char _Value) {
432 return __atomic_fetch_add(_Addend, _Value, __ATOMIC_RELAXED);
434 static __inline__ short __DEFAULT_FN_ATTRS
435 _InterlockedExchangeAdd16_acq(short volatile *_Addend, short _Value) {
436 return __atomic_fetch_add(_Addend, _Value, __ATOMIC_ACQUIRE);
438 static __inline__ short __DEFAULT_FN_ATTRS
439 _InterlockedExchangeAdd16_nf(short volatile *_Addend, short _Value) {
440 return __atomic_fetch_add(_Addend, _Value, __ATOMIC_RELAXED);
442 static __inline__ short __DEFAULT_FN_ATTRS
443 _InterlockedExchangeAdd16_rel(short volatile *_Addend, short _Value) {
444 return __atomic_fetch_add(_Addend, _Value, __ATOMIC_RELEASE);
446 static __inline__ long __DEFAULT_FN_ATTRS
447 _InterlockedExchangeAdd_acq(long volatile *_Addend, long _Value) {
448 return __atomic_fetch_add(_Addend, _Value, __ATOMIC_ACQUIRE);
450 static __inline__ long __DEFAULT_FN_ATTRS
451 _InterlockedExchangeAdd_nf(long volatile *_Addend, long _Value) {
452 return __atomic_fetch_add(_Addend, _Value, __ATOMIC_RELAXED);
454 static __inline__ long __DEFAULT_FN_ATTRS
455 _InterlockedExchangeAdd_rel(long volatile *_Addend, long _Value) {
456 return __atomic_fetch_add(_Addend, _Value, __ATOMIC_RELEASE);
458 static __inline__ __int64 __DEFAULT_FN_ATTRS
459 _InterlockedExchangeAdd64_acq(__int64 volatile *_Addend, __int64 _Value) {
460 return __atomic_fetch_add(_Addend, _Value, __ATOMIC_ACQUIRE);
462 static __inline__ __int64 __DEFAULT_FN_ATTRS
463 _InterlockedExchangeAdd64_nf(__int64 volatile *_Addend, __int64 _Value) {
464 return __atomic_fetch_add(_Addend, _Value, __ATOMIC_RELAXED);
466 static __inline__ __int64 __DEFAULT_FN_ATTRS
467 _InterlockedExchangeAdd64_rel(__int64 volatile *_Addend, __int64 _Value) {
468 return __atomic_fetch_add(_Addend, _Value, __ATOMIC_RELEASE);
471 /*----------------------------------------------------------------------------*\
472 |* Interlocked Increment
473 \*----------------------------------------------------------------------------*/
474 #if defined(__arm__) || defined(__aarch64__)
475 static __inline__ short __DEFAULT_FN_ATTRS
476 _InterlockedIncrement16_acq(short volatile *_Value) {
477 return __atomic_add_fetch(_Value, 1, __ATOMIC_ACQUIRE);
479 static __inline__ short __DEFAULT_FN_ATTRS
480 _InterlockedIncrement16_nf(short volatile *_Value) {
481 return __atomic_add_fetch(_Value, 1, __ATOMIC_RELAXED);
483 static __inline__ short __DEFAULT_FN_ATTRS
484 _InterlockedIncrement16_rel(short volatile *_Value) {
485 return __atomic_add_fetch(_Value, 1, __ATOMIC_RELEASE);
487 static __inline__ long __DEFAULT_FN_ATTRS
488 _InterlockedIncrement_acq(long volatile *_Value) {
489 return __atomic_add_fetch(_Value, 1, __ATOMIC_ACQUIRE);
491 static __inline__ long __DEFAULT_FN_ATTRS
492 _InterlockedIncrement_nf(long volatile *_Value) {
493 return __atomic_add_fetch(_Value, 1, __ATOMIC_RELAXED);
495 static __inline__ long __DEFAULT_FN_ATTRS
496 _InterlockedIncrement_rel(long volatile *_Value) {
497 return __atomic_add_fetch(_Value, 1, __ATOMIC_RELEASE);
499 static __inline__ __int64 __DEFAULT_FN_ATTRS
500 _InterlockedIncrement64_acq(__int64 volatile *_Value) {
501 return __atomic_add_fetch(_Value, 1, __ATOMIC_ACQUIRE);
503 static __inline__ __int64 __DEFAULT_FN_ATTRS
504 _InterlockedIncrement64_nf(__int64 volatile *_Value) {
505 return __atomic_add_fetch(_Value, 1, __ATOMIC_RELAXED);
507 static __inline__ __int64 __DEFAULT_FN_ATTRS
508 _InterlockedIncrement64_rel(__int64 volatile *_Value) {
509 return __atomic_add_fetch(_Value, 1, __ATOMIC_RELEASE);
512 /*----------------------------------------------------------------------------*\
513 |* Interlocked Decrement
514 \*----------------------------------------------------------------------------*/
515 #if defined(__arm__) || defined(__aarch64__)
516 static __inline__ short __DEFAULT_FN_ATTRS
517 _InterlockedDecrement16_acq(short volatile *_Value) {
518 return __atomic_sub_fetch(_Value, 1, __ATOMIC_ACQUIRE);
520 static __inline__ short __DEFAULT_FN_ATTRS
521 _InterlockedDecrement16_nf(short volatile *_Value) {
522 return __atomic_sub_fetch(_Value, 1, __ATOMIC_RELAXED);
524 static __inline__ short __DEFAULT_FN_ATTRS
525 _InterlockedDecrement16_rel(short volatile *_Value) {
526 return __atomic_sub_fetch(_Value, 1, __ATOMIC_RELEASE);
528 static __inline__ long __DEFAULT_FN_ATTRS
529 _InterlockedDecrement_acq(long volatile *_Value) {
530 return __atomic_sub_fetch(_Value, 1, __ATOMIC_ACQUIRE);
532 static __inline__ long __DEFAULT_FN_ATTRS
533 _InterlockedDecrement_nf(long volatile *_Value) {
534 return __atomic_sub_fetch(_Value, 1, __ATOMIC_RELAXED);
536 static __inline__ long __DEFAULT_FN_ATTRS
537 _InterlockedDecrement_rel(long volatile *_Value) {
538 return __atomic_sub_fetch(_Value, 1, __ATOMIC_RELEASE);
540 static __inline__ __int64 __DEFAULT_FN_ATTRS
541 _InterlockedDecrement64_acq(__int64 volatile *_Value) {
542 return __atomic_sub_fetch(_Value, 1, __ATOMIC_ACQUIRE);
544 static __inline__ __int64 __DEFAULT_FN_ATTRS
545 _InterlockedDecrement64_nf(__int64 volatile *_Value) {
546 return __atomic_sub_fetch(_Value, 1, __ATOMIC_RELAXED);
548 static __inline__ __int64 __DEFAULT_FN_ATTRS
549 _InterlockedDecrement64_rel(__int64 volatile *_Value) {
550 return __atomic_sub_fetch(_Value, 1, __ATOMIC_RELEASE);
553 /*----------------------------------------------------------------------------*\
555 \*----------------------------------------------------------------------------*/
556 #if defined(__arm__) || defined(__aarch64__)
557 static __inline__ char __DEFAULT_FN_ATTRS
558 _InterlockedAnd8_acq(char volatile *_Value, char _Mask) {
559 return __atomic_fetch_and(_Value, _Mask, __ATOMIC_ACQUIRE);
561 static __inline__ char __DEFAULT_FN_ATTRS
562 _InterlockedAnd8_nf(char volatile *_Value, char _Mask) {
563 return __atomic_fetch_and(_Value, _Mask, __ATOMIC_RELAXED);
565 static __inline__ char __DEFAULT_FN_ATTRS
566 _InterlockedAnd8_rel(char volatile *_Value, char _Mask) {
567 return __atomic_fetch_and(_Value, _Mask, __ATOMIC_RELEASE);
569 static __inline__ short __DEFAULT_FN_ATTRS
570 _InterlockedAnd16_acq(short volatile *_Value, short _Mask) {
571 return __atomic_fetch_and(_Value, _Mask, __ATOMIC_ACQUIRE);
573 static __inline__ short __DEFAULT_FN_ATTRS
574 _InterlockedAnd16_nf(short volatile *_Value, short _Mask) {
575 return __atomic_fetch_and(_Value, _Mask, __ATOMIC_RELAXED);
577 static __inline__ short __DEFAULT_FN_ATTRS
578 _InterlockedAnd16_rel(short volatile *_Value, short _Mask) {
579 return __atomic_fetch_and(_Value, _Mask, __ATOMIC_RELEASE);
581 static __inline__ long __DEFAULT_FN_ATTRS
582 _InterlockedAnd_acq(long volatile *_Value, long _Mask) {
583 return __atomic_fetch_and(_Value, _Mask, __ATOMIC_ACQUIRE);
585 static __inline__ long __DEFAULT_FN_ATTRS
586 _InterlockedAnd_nf(long volatile *_Value, long _Mask) {
587 return __atomic_fetch_and(_Value, _Mask, __ATOMIC_RELAXED);
589 static __inline__ long __DEFAULT_FN_ATTRS
590 _InterlockedAnd_rel(long volatile *_Value, long _Mask) {
591 return __atomic_fetch_and(_Value, _Mask, __ATOMIC_RELEASE);
593 static __inline__ __int64 __DEFAULT_FN_ATTRS
594 _InterlockedAnd64_acq(__int64 volatile *_Value, __int64 _Mask) {
595 return __atomic_fetch_and(_Value, _Mask, __ATOMIC_ACQUIRE);
597 static __inline__ __int64 __DEFAULT_FN_ATTRS
598 _InterlockedAnd64_nf(__int64 volatile *_Value, __int64 _Mask) {
599 return __atomic_fetch_and(_Value, _Mask, __ATOMIC_RELAXED);
601 static __inline__ __int64 __DEFAULT_FN_ATTRS
602 _InterlockedAnd64_rel(__int64 volatile *_Value, __int64 _Mask) {
603 return __atomic_fetch_and(_Value, _Mask, __ATOMIC_RELEASE);
606 /*----------------------------------------------------------------------------*\
608 \*----------------------------------------------------------------------------*/
609 #if defined(__arm__) || defined(__aarch64__)
610 static __inline__ char __DEFAULT_FN_ATTRS
611 _InterlockedOr8_acq(char volatile *_Value, char _Mask) {
612 return __atomic_fetch_or(_Value, _Mask, __ATOMIC_ACQUIRE);
614 static __inline__ char __DEFAULT_FN_ATTRS
615 _InterlockedOr8_nf(char volatile *_Value, char _Mask) {
616 return __atomic_fetch_or(_Value, _Mask, __ATOMIC_RELAXED);
618 static __inline__ char __DEFAULT_FN_ATTRS
619 _InterlockedOr8_rel(char volatile *_Value, char _Mask) {
620 return __atomic_fetch_or(_Value, _Mask, __ATOMIC_RELEASE);
622 static __inline__ short __DEFAULT_FN_ATTRS
623 _InterlockedOr16_acq(short volatile *_Value, short _Mask) {
624 return __atomic_fetch_or(_Value, _Mask, __ATOMIC_ACQUIRE);
626 static __inline__ short __DEFAULT_FN_ATTRS
627 _InterlockedOr16_nf(short volatile *_Value, short _Mask) {
628 return __atomic_fetch_or(_Value, _Mask, __ATOMIC_RELAXED);
630 static __inline__ short __DEFAULT_FN_ATTRS
631 _InterlockedOr16_rel(short volatile *_Value, short _Mask) {
632 return __atomic_fetch_or(_Value, _Mask, __ATOMIC_RELEASE);
634 static __inline__ long __DEFAULT_FN_ATTRS
635 _InterlockedOr_acq(long volatile *_Value, long _Mask) {
636 return __atomic_fetch_or(_Value, _Mask, __ATOMIC_ACQUIRE);
638 static __inline__ long __DEFAULT_FN_ATTRS
639 _InterlockedOr_nf(long volatile *_Value, long _Mask) {
640 return __atomic_fetch_or(_Value, _Mask, __ATOMIC_RELAXED);
642 static __inline__ long __DEFAULT_FN_ATTRS
643 _InterlockedOr_rel(long volatile *_Value, long _Mask) {
644 return __atomic_fetch_or(_Value, _Mask, __ATOMIC_RELEASE);
646 static __inline__ __int64 __DEFAULT_FN_ATTRS
647 _InterlockedOr64_acq(__int64 volatile *_Value, __int64 _Mask) {
648 return __atomic_fetch_or(_Value, _Mask, __ATOMIC_ACQUIRE);
650 static __inline__ __int64 __DEFAULT_FN_ATTRS
651 _InterlockedOr64_nf(__int64 volatile *_Value, __int64 _Mask) {
652 return __atomic_fetch_or(_Value, _Mask, __ATOMIC_RELAXED);
654 static __inline__ __int64 __DEFAULT_FN_ATTRS
655 _InterlockedOr64_rel(__int64 volatile *_Value, __int64 _Mask) {
656 return __atomic_fetch_or(_Value, _Mask, __ATOMIC_RELEASE);
659 /*----------------------------------------------------------------------------*\
661 \*----------------------------------------------------------------------------*/
662 #if defined(__arm__) || defined(__aarch64__)
663 static __inline__ char __DEFAULT_FN_ATTRS
664 _InterlockedXor8_acq(char volatile *_Value, char _Mask) {
665 return __atomic_fetch_xor(_Value, _Mask, __ATOMIC_ACQUIRE);
667 static __inline__ char __DEFAULT_FN_ATTRS
668 _InterlockedXor8_nf(char volatile *_Value, char _Mask) {
669 return __atomic_fetch_xor(_Value, _Mask, __ATOMIC_RELAXED);
671 static __inline__ char __DEFAULT_FN_ATTRS
672 _InterlockedXor8_rel(char volatile *_Value, char _Mask) {
673 return __atomic_fetch_xor(_Value, _Mask, __ATOMIC_RELEASE);
675 static __inline__ short __DEFAULT_FN_ATTRS
676 _InterlockedXor16_acq(short volatile *_Value, short _Mask) {
677 return __atomic_fetch_xor(_Value, _Mask, __ATOMIC_ACQUIRE);
679 static __inline__ short __DEFAULT_FN_ATTRS
680 _InterlockedXor16_nf(short volatile *_Value, short _Mask) {
681 return __atomic_fetch_xor(_Value, _Mask, __ATOMIC_RELAXED);
683 static __inline__ short __DEFAULT_FN_ATTRS
684 _InterlockedXor16_rel(short volatile *_Value, short _Mask) {
685 return __atomic_fetch_xor(_Value, _Mask, __ATOMIC_RELEASE);
687 static __inline__ long __DEFAULT_FN_ATTRS
688 _InterlockedXor_acq(long volatile *_Value, long _Mask) {
689 return __atomic_fetch_xor(_Value, _Mask, __ATOMIC_ACQUIRE);
691 static __inline__ long __DEFAULT_FN_ATTRS
692 _InterlockedXor_nf(long volatile *_Value, long _Mask) {
693 return __atomic_fetch_xor(_Value, _Mask, __ATOMIC_RELAXED);
695 static __inline__ long __DEFAULT_FN_ATTRS
696 _InterlockedXor_rel(long volatile *_Value, long _Mask) {
697 return __atomic_fetch_xor(_Value, _Mask, __ATOMIC_RELEASE);
699 static __inline__ __int64 __DEFAULT_FN_ATTRS
700 _InterlockedXor64_acq(__int64 volatile *_Value, __int64 _Mask) {
701 return __atomic_fetch_xor(_Value, _Mask, __ATOMIC_ACQUIRE);
703 static __inline__ __int64 __DEFAULT_FN_ATTRS
704 _InterlockedXor64_nf(__int64 volatile *_Value, __int64 _Mask) {
705 return __atomic_fetch_xor(_Value, _Mask, __ATOMIC_RELAXED);
707 static __inline__ __int64 __DEFAULT_FN_ATTRS
708 _InterlockedXor64_rel(__int64 volatile *_Value, __int64 _Mask) {
709 return __atomic_fetch_xor(_Value, _Mask, __ATOMIC_RELEASE);
712 /*----------------------------------------------------------------------------*\
713 |* Interlocked Exchange
714 \*----------------------------------------------------------------------------*/
715 #if defined(__arm__) || defined(__aarch64__)
716 static __inline__ char __DEFAULT_FN_ATTRS
717 _InterlockedExchange8_acq(char volatile *_Target, char _Value) {
718 __atomic_exchange(_Target, &_Value, &_Value, __ATOMIC_ACQUIRE);
721 static __inline__ char __DEFAULT_FN_ATTRS
722 _InterlockedExchange8_nf(char volatile *_Target, char _Value) {
723 __atomic_exchange(_Target, &_Value, &_Value, __ATOMIC_RELAXED);
726 static __inline__ char __DEFAULT_FN_ATTRS
727 _InterlockedExchange8_rel(char volatile *_Target, char _Value) {
728 __atomic_exchange(_Target, &_Value, &_Value, __ATOMIC_RELEASE);
731 static __inline__ short __DEFAULT_FN_ATTRS
732 _InterlockedExchange16_acq(short volatile *_Target, short _Value) {
733 __atomic_exchange(_Target, &_Value, &_Value, __ATOMIC_ACQUIRE);
736 static __inline__ short __DEFAULT_FN_ATTRS
737 _InterlockedExchange16_nf(short volatile *_Target, short _Value) {
738 __atomic_exchange(_Target, &_Value, &_Value, __ATOMIC_RELAXED);
741 static __inline__ short __DEFAULT_FN_ATTRS
742 _InterlockedExchange16_rel(short volatile *_Target, short _Value) {
743 __atomic_exchange(_Target, &_Value, &_Value, __ATOMIC_RELEASE);
746 static __inline__ long __DEFAULT_FN_ATTRS
747 _InterlockedExchange_acq(long volatile *_Target, long _Value) {
748 __atomic_exchange(_Target, &_Value, &_Value, __ATOMIC_ACQUIRE);
751 static __inline__ long __DEFAULT_FN_ATTRS
752 _InterlockedExchange_nf(long volatile *_Target, long _Value) {
753 __atomic_exchange(_Target, &_Value, &_Value, __ATOMIC_RELAXED);
756 static __inline__ long __DEFAULT_FN_ATTRS
757 _InterlockedExchange_rel(long volatile *_Target, long _Value) {
758 __atomic_exchange(_Target, &_Value, &_Value, __ATOMIC_RELEASE);
761 static __inline__ __int64 __DEFAULT_FN_ATTRS
762 _InterlockedExchange64_acq(__int64 volatile *_Target, __int64 _Value) {
763 __atomic_exchange(_Target, &_Value, &_Value, __ATOMIC_ACQUIRE);
766 static __inline__ __int64 __DEFAULT_FN_ATTRS
767 _InterlockedExchange64_nf(__int64 volatile *_Target, __int64 _Value) {
768 __atomic_exchange(_Target, &_Value, &_Value, __ATOMIC_RELAXED);
771 static __inline__ __int64 __DEFAULT_FN_ATTRS
772 _InterlockedExchange64_rel(__int64 volatile *_Target, __int64 _Value) {
773 __atomic_exchange(_Target, &_Value, &_Value, __ATOMIC_RELEASE);
777 /*----------------------------------------------------------------------------*\
778 |* Interlocked Compare Exchange
779 \*----------------------------------------------------------------------------*/
780 #if defined(__arm__) || defined(__aarch64__)
781 static __inline__ char __DEFAULT_FN_ATTRS
782 _InterlockedCompareExchange8_acq(char volatile *_Destination,
783 char _Exchange, char _Comparand) {
784 __atomic_compare_exchange(_Destination, &_Comparand, &_Exchange, 0,
785 __ATOMIC_SEQ_CST, __ATOMIC_ACQUIRE);
788 static __inline__ char __DEFAULT_FN_ATTRS
789 _InterlockedCompareExchange8_nf(char volatile *_Destination,
790 char _Exchange, char _Comparand) {
791 __atomic_compare_exchange(_Destination, &_Comparand, &_Exchange, 0,
792 __ATOMIC_SEQ_CST, __ATOMIC_RELAXED);
795 static __inline__ char __DEFAULT_FN_ATTRS
796 _InterlockedCompareExchange8_rel(char volatile *_Destination,
797 char _Exchange, char _Comparand) {
798 __atomic_compare_exchange(_Destination, &_Comparand, &_Exchange, 0,
799 __ATOMIC_SEQ_CST, __ATOMIC_RELEASE);
802 static __inline__ short __DEFAULT_FN_ATTRS
803 _InterlockedCompareExchange16_acq(short volatile *_Destination,
804 short _Exchange, short _Comparand) {
805 __atomic_compare_exchange(_Destination, &_Comparand, &_Exchange, 0,
806 __ATOMIC_SEQ_CST, __ATOMIC_ACQUIRE);
809 static __inline__ short __DEFAULT_FN_ATTRS
810 _InterlockedCompareExchange16_nf(short volatile *_Destination,
811 short _Exchange, short _Comparand) {
812 __atomic_compare_exchange(_Destination, &_Comparand, &_Exchange, 0,
813 __ATOMIC_SEQ_CST, __ATOMIC_RELAXED);
816 static __inline__ short __DEFAULT_FN_ATTRS
817 _InterlockedCompareExchange16_rel(short volatile *_Destination,
818 short _Exchange, short _Comparand) {
819 __atomic_compare_exchange(_Destination, &_Comparand, &_Exchange, 0,
820 __ATOMIC_SEQ_CST, __ATOMIC_RELEASE);
823 static __inline__ long __DEFAULT_FN_ATTRS
824 _InterlockedCompareExchange_acq(long volatile *_Destination,
825 long _Exchange, long _Comparand) {
826 __atomic_compare_exchange(_Destination, &_Comparand, &_Exchange, 0,
827 __ATOMIC_SEQ_CST, __ATOMIC_ACQUIRE);
830 static __inline__ long __DEFAULT_FN_ATTRS
831 _InterlockedCompareExchange_nf(long volatile *_Destination,
832 long _Exchange, long _Comparand) {
833 __atomic_compare_exchange(_Destination, &_Comparand, &_Exchange, 0,
834 __ATOMIC_SEQ_CST, __ATOMIC_RELAXED);
837 static __inline__ short __DEFAULT_FN_ATTRS
838 _InterlockedCompareExchange_rel(long volatile *_Destination,
839 long _Exchange, long _Comparand) {
840 __atomic_compare_exchange(_Destination, &_Comparand, &_Exchange, 0,
841 __ATOMIC_SEQ_CST, __ATOMIC_RELEASE);
844 static __inline__ __int64 __DEFAULT_FN_ATTRS
845 _InterlockedCompareExchange64_acq(__int64 volatile *_Destination,
846 __int64 _Exchange, __int64 _Comparand) {
847 __atomic_compare_exchange(_Destination, &_Comparand, &_Exchange, 0,
848 __ATOMIC_SEQ_CST, __ATOMIC_ACQUIRE);
851 static __inline__ __int64 __DEFAULT_FN_ATTRS
852 _InterlockedCompareExchange64_nf(__int64 volatile *_Destination,
853 __int64 _Exchange, __int64 _Comparand) {
854 __atomic_compare_exchange(_Destination, &_Comparand, &_Exchange, 0,
855 __ATOMIC_SEQ_CST, __ATOMIC_RELAXED);
858 static __inline__ __int64 __DEFAULT_FN_ATTRS
859 _InterlockedCompareExchange64_rel(__int64 volatile *_Destination,
860 __int64 _Exchange, __int64 _Comparand) {
861 __atomic_compare_exchange(_Destination, &_Comparand, &_Exchange, 0,
862 __ATOMIC_SEQ_CST, __ATOMIC_RELEASE);
867 /*----------------------------------------------------------------------------*\
869 \*----------------------------------------------------------------------------*/
870 #if defined(__i386__) || defined(__x86_64__)
871 static __inline__ void __DEFAULT_FN_ATTRS
872 __movsb(unsigned char *__dst, unsigned char const *__src, size_t __n) {
873 __asm__("rep movsb" : : "D"(__dst), "S"(__src), "c"(__n));
875 static __inline__ void __DEFAULT_FN_ATTRS
876 __movsd(unsigned long *__dst, unsigned long const *__src, size_t __n) {
877 __asm__("rep movsl" : : "D"(__dst), "S"(__src), "c"(__n));
879 static __inline__ void __DEFAULT_FN_ATTRS
880 __movsw(unsigned short *__dst, unsigned short const *__src, size_t __n) {
881 __asm__("rep movsw" : : "D"(__dst), "S"(__src), "c"(__n));
883 static __inline__ void __DEFAULT_FN_ATTRS
884 __stosd(unsigned long *__dst, unsigned long __x, size_t __n) {
885 __asm__("rep stosl" : : "D"(__dst), "a"(__x), "c"(__n));
887 static __inline__ void __DEFAULT_FN_ATTRS
888 __stosw(unsigned short *__dst, unsigned short __x, size_t __n) {
889 __asm__("rep stosw" : : "D"(__dst), "a"(__x), "c"(__n));
893 static __inline__ void __DEFAULT_FN_ATTRS
894 __movsq(unsigned long long *__dst, unsigned long long const *__src, size_t __n) {
895 __asm__("rep movsq" : : "D"(__dst), "S"(__src), "c"(__n));
897 static __inline__ void __DEFAULT_FN_ATTRS
898 __stosq(unsigned __int64 *__dst, unsigned __int64 __x, size_t __n) {
899 __asm__("rep stosq" : : "D"(__dst), "a"(__x), "c"(__n));
903 /*----------------------------------------------------------------------------*\
905 \*----------------------------------------------------------------------------*/
906 #if defined(__i386__) || defined(__x86_64__)
907 static __inline__ void __DEFAULT_FN_ATTRS
908 __cpuid(int __info[4], int __level) {
909 __asm__ ("cpuid" : "=a"(__info[0]), "=b" (__info[1]), "=c"(__info[2]), "=d"(__info[3])
912 static __inline__ void __DEFAULT_FN_ATTRS
913 __cpuidex(int __info[4], int __level, int __ecx) {
914 __asm__ ("cpuid" : "=a"(__info[0]), "=b" (__info[1]), "=c"(__info[2]), "=d"(__info[3])
915 : "a"(__level), "c"(__ecx));
917 static __inline__ unsigned __int64 __cdecl __DEFAULT_FN_ATTRS
918 _xgetbv(unsigned int __xcr_no) {
919 unsigned int __eax, __edx;
920 __asm__ ("xgetbv" : "=a" (__eax), "=d" (__edx) : "c" (__xcr_no));
921 return ((unsigned __int64)__edx << 32) | __eax;
923 static __inline__ void __DEFAULT_FN_ATTRS
925 __asm__ volatile ("hlt");
927 static __inline__ void __DEFAULT_FN_ATTRS
929 __asm__ volatile ("nop");
933 /*----------------------------------------------------------------------------*\
934 |* Privileged intrinsics
935 \*----------------------------------------------------------------------------*/
936 #if defined(__i386__) || defined(__x86_64__)
937 static __inline__ unsigned __int64 __DEFAULT_FN_ATTRS
938 __readmsr(unsigned long __register) {
939 // Loads the contents of a 64-bit model specific register (MSR) specified in
940 // the ECX register into registers EDX:EAX. The EDX register is loaded with
941 // the high-order 32 bits of the MSR and the EAX register is loaded with the
942 // low-order 32 bits. If less than 64 bits are implemented in the MSR being
943 // read, the values returned to EDX:EAX in unimplemented bit locations are
947 __asm__ ("rdmsr" : "=d"(__edx), "=a"(__eax) : "c"(__register));
948 return (((unsigned __int64)__edx) << 32) | (unsigned __int64)__eax;
951 static __inline__ unsigned long __DEFAULT_FN_ATTRS
953 unsigned long __cr3_val;
954 __asm__ __volatile__ ("mov %%cr3, %0" : "=q"(__cr3_val) : : "memory");
958 static __inline__ void __DEFAULT_FN_ATTRS
959 __writecr3(unsigned int __cr3_val) {
960 __asm__ ("mov %0, %%cr3" : : "q"(__cr3_val) : "memory");
968 #undef __DEFAULT_FN_ATTRS
970 #endif /* __INTRIN_H */
971 #endif /* _MSC_VER */