blob: c908ca46a9b3559a8f1b49ec042df1b887c26bd4 [file] [log] [blame]
Eric Christopherd1428bf2013-08-31 00:22:48 +00001/* ===-------- Intrin.h ---------------------------------------------------===
2 *
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:
9 *
10 * The above copyright notice and this permission notice shall be included in
11 * all copies or substantial portions of the Software.
12 *
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
19 * THE SOFTWARE.
20 *
21 *===-----------------------------------------------------------------------===
22 */
23
24/* Only include this if we're compiling for the windows platform. */
25#ifndef _MSC_VER
26#include_next <Intrin.h>
27#else
28
29#ifndef __INTRIN_H
30#define __INTRIN_H
31
32/* First include the standard intrinsics. */
Saleem Abdulrasool702eefe2014-06-25 16:48:40 +000033#if defined(__i386__) || defined(__x86_64__)
Eric Christopherd1428bf2013-08-31 00:22:48 +000034#include <x86intrin.h>
Saleem Abdulrasool702eefe2014-06-25 16:48:40 +000035#endif
Eric Christopherd1428bf2013-08-31 00:22:48 +000036
Hans Wennborg1fd6dd32014-01-28 23:01:59 +000037/* For the definition of jmp_buf. */
Nico Webera62cffa2014-07-08 18:34:46 +000038#if __STDC_HOSTED__
Hans Wennborg1fd6dd32014-01-28 23:01:59 +000039#include <setjmp.h>
Nico Webera62cffa2014-07-08 18:34:46 +000040#endif
Hans Wennborg1fd6dd32014-01-28 23:01:59 +000041
Eric Christopher5a9bec12015-06-15 23:20:35 +000042/* Define the default attributes for the functions in this file. */
Michael Kupersteine45af542015-06-30 13:36:19 +000043#define __DEFAULT_FN_ATTRS __attribute__((__always_inline__, __nodebug__))
Eric Christopher5a9bec12015-06-15 23:20:35 +000044
Eric Christopher0db88a72013-09-18 22:24:01 +000045#ifdef __cplusplus
46extern "C" {
47#endif
48
Saleem Abdulrasoolc4ebb122014-07-08 05:46:04 +000049#if defined(__MMX__)
Eric Christopherd1428bf2013-08-31 00:22:48 +000050/* And the random ones that aren't in those files. */
51__m64 _m_from_float(float);
52__m64 _m_from_int(int _l);
53void _m_prefetch(void *);
54float _m_to_float(__m64);
55int _m_to_int(__m64 _M);
Saleem Abdulrasoolc4ebb122014-07-08 05:46:04 +000056#endif
Eric Christopherd1428bf2013-08-31 00:22:48 +000057
58/* Other assorted instruction intrinsics. */
59void __addfsbyte(unsigned long, unsigned char);
60void __addfsdword(unsigned long, unsigned long);
61void __addfsword(unsigned long, unsigned short);
62void __code_seg(const char *);
Hans Wennborg854f7d32014-01-16 23:39:35 +000063static __inline__
Eric Christopherd1428bf2013-08-31 00:22:48 +000064void __cpuid(int[4], int);
Hans Wennborg12fb89e2014-01-31 19:44:55 +000065static __inline__
Eric Christopherd1428bf2013-08-31 00:22:48 +000066void __cpuidex(int[4], int, int);
Eric Christopher0db88a72013-09-18 22:24:01 +000067void __debugbreak(void);
Eric Christopherd1428bf2013-08-31 00:22:48 +000068__int64 __emul(int, int);
69unsigned __int64 __emulu(unsigned int, unsigned int);
Eric Christopher0db88a72013-09-18 22:24:01 +000070void __cdecl __fastfail(unsigned int);
Eric Christopherd1428bf2013-08-31 00:22:48 +000071unsigned int __getcallerseflags(void);
Hans Wennborg740a4d62014-01-28 22:55:01 +000072static __inline__
Eric Christopherd1428bf2013-08-31 00:22:48 +000073void __halt(void);
74unsigned char __inbyte(unsigned short);
75void __inbytestring(unsigned short, unsigned char *, unsigned long);
76void __incfsbyte(unsigned long);
77void __incfsdword(unsigned long);
78void __incfsword(unsigned long);
79unsigned long __indword(unsigned short);
80void __indwordstring(unsigned short, unsigned long *, unsigned long);
81void __int2c(void);
82void __invlpg(void *);
83unsigned short __inword(unsigned short);
84void __inwordstring(unsigned short, unsigned short *, unsigned long);
85void __lidt(void *);
86unsigned __int64 __ll_lshift(unsigned __int64, int);
87__int64 __ll_rshift(__int64, int);
88void __llwpcb(void *);
89unsigned char __lwpins32(unsigned int, unsigned int, unsigned int);
90void __lwpval32(unsigned int, unsigned int, unsigned int);
91unsigned int __lzcnt(unsigned int);
92unsigned short __lzcnt16(unsigned short);
Hans Wennborgd9be72e2014-03-12 22:00:32 +000093static __inline__
Eric Christopherd1428bf2013-08-31 00:22:48 +000094void __movsb(unsigned char *, unsigned char const *, size_t);
Hans Wennborgd9be72e2014-03-12 22:00:32 +000095static __inline__
Eric Christopherd1428bf2013-08-31 00:22:48 +000096void __movsd(unsigned long *, unsigned long const *, size_t);
Hans Wennborgd9be72e2014-03-12 22:00:32 +000097static __inline__
Eric Christopherd1428bf2013-08-31 00:22:48 +000098void __movsw(unsigned short *, unsigned short const *, size_t);
99void __nop(void);
100void __nvreg_restore_fence(void);
101void __nvreg_save_fence(void);
102void __outbyte(unsigned short, unsigned char);
103void __outbytestring(unsigned short, unsigned char *, unsigned long);
104void __outdword(unsigned short, unsigned long);
105void __outdwordstring(unsigned short, unsigned long *, unsigned long);
106void __outword(unsigned short, unsigned short);
107void __outwordstring(unsigned short, unsigned short *, unsigned long);
Warren Huntd6ffae92013-09-27 23:57:26 +0000108static __inline__
Eric Christopherd1428bf2013-08-31 00:22:48 +0000109unsigned int __popcnt(unsigned int);
Warren Huntd6ffae92013-09-27 23:57:26 +0000110static __inline__
Eric Christopherd1428bf2013-08-31 00:22:48 +0000111unsigned short __popcnt16(unsigned short);
Eric Christopherd1428bf2013-08-31 00:22:48 +0000112unsigned long __readcr0(void);
113unsigned long __readcr2(void);
Reid Kleckner592dc612014-04-08 00:28:22 +0000114static __inline__
Eric Christopherd1428bf2013-08-31 00:22:48 +0000115unsigned long __readcr3(void);
Eric Christopher439137e2014-01-24 12:13:47 +0000116unsigned long __readcr4(void);
Eric Christopherd1428bf2013-08-31 00:22:48 +0000117unsigned long __readcr8(void);
118unsigned int __readdr(unsigned int);
Hans Wennborg74ca0c42014-01-24 00:52:39 +0000119#ifdef __i386__
120static __inline__
Eric Christopherd1428bf2013-08-31 00:22:48 +0000121unsigned char __readfsbyte(unsigned long);
Hans Wennborg74ca0c42014-01-24 00:52:39 +0000122static __inline__
Eric Christopherd1428bf2013-08-31 00:22:48 +0000123unsigned long __readfsdword(unsigned long);
Hans Wennborg74ca0c42014-01-24 00:52:39 +0000124static __inline__
Eric Christopherd1428bf2013-08-31 00:22:48 +0000125unsigned __int64 __readfsqword(unsigned long);
Hans Wennborg74ca0c42014-01-24 00:52:39 +0000126static __inline__
Eric Christopherd1428bf2013-08-31 00:22:48 +0000127unsigned short __readfsword(unsigned long);
Hans Wennborg74ca0c42014-01-24 00:52:39 +0000128#endif
Reid Kleckner592dc612014-04-08 00:28:22 +0000129static __inline__
Eric Christopherd1428bf2013-08-31 00:22:48 +0000130unsigned __int64 __readmsr(unsigned long);
131unsigned __int64 __readpmc(unsigned long);
132unsigned long __segmentlimit(unsigned long);
133void __sidt(void *);
134void *__slwpcb(void);
Hans Wennborgd9be72e2014-03-12 22:00:32 +0000135static __inline__
Eric Christopherd1428bf2013-08-31 00:22:48 +0000136void __stosb(unsigned char *, unsigned char, size_t);
Hans Wennborgd9be72e2014-03-12 22:00:32 +0000137static __inline__
Eric Christopherd1428bf2013-08-31 00:22:48 +0000138void __stosd(unsigned long *, unsigned long, size_t);
Hans Wennborgd9be72e2014-03-12 22:00:32 +0000139static __inline__
Eric Christopherd1428bf2013-08-31 00:22:48 +0000140void __stosw(unsigned short *, unsigned short, size_t);
141void __svm_clgi(void);
142void __svm_invlpga(void *, int);
143void __svm_skinit(int);
144void __svm_stgi(void);
145void __svm_vmload(size_t);
146void __svm_vmrun(size_t);
147void __svm_vmsave(size_t);
148void __ud2(void);
149unsigned __int64 __ull_rshift(unsigned __int64, int);
150void __vmx_off(void);
151void __vmx_vmptrst(unsigned __int64 *);
152void __wbinvd(void);
153void __writecr0(unsigned int);
Reid Kleckner592dc612014-04-08 00:28:22 +0000154static __inline__
Eric Christopherd1428bf2013-08-31 00:22:48 +0000155void __writecr3(unsigned int);
156void __writecr4(unsigned int);
157void __writecr8(unsigned int);
158void __writedr(unsigned int, unsigned int);
Eric Christopherd1428bf2013-08-31 00:22:48 +0000159void __writefsbyte(unsigned long, unsigned char);
160void __writefsdword(unsigned long, unsigned long);
161void __writefsqword(unsigned long, unsigned __int64);
162void __writefsword(unsigned long, unsigned short);
163void __writemsr(unsigned long, unsigned __int64);
Warren Huntd6ffae92013-09-27 23:57:26 +0000164static __inline__
Eric Christopherd1428bf2013-08-31 00:22:48 +0000165void *_AddressOfReturnAddress(void);
Warren Huntd6ffae92013-09-27 23:57:26 +0000166static __inline__
Eric Christopherd1428bf2013-08-31 00:22:48 +0000167unsigned char _BitScanForward(unsigned long *_Index, unsigned long _Mask);
Warren Huntd6ffae92013-09-27 23:57:26 +0000168static __inline__
Eric Christopherd1428bf2013-08-31 00:22:48 +0000169unsigned char _BitScanReverse(unsigned long *_Index, unsigned long _Mask);
Warren Huntd6ffae92013-09-27 23:57:26 +0000170static __inline__
Eric Christopherd1428bf2013-08-31 00:22:48 +0000171unsigned char _bittest(long const *, long);
Warren Huntd6ffae92013-09-27 23:57:26 +0000172static __inline__
Eric Christopherd1428bf2013-08-31 00:22:48 +0000173unsigned char _bittestandcomplement(long *, long);
Warren Huntd6ffae92013-09-27 23:57:26 +0000174static __inline__
Eric Christopherd1428bf2013-08-31 00:22:48 +0000175unsigned char _bittestandreset(long *, long);
Warren Huntd6ffae92013-09-27 23:57:26 +0000176static __inline__
Eric Christopherd1428bf2013-08-31 00:22:48 +0000177unsigned char _bittestandset(long *, long);
Eric Christopherd1428bf2013-08-31 00:22:48 +0000178unsigned __int64 __cdecl _byteswap_uint64(unsigned __int64);
179unsigned long __cdecl _byteswap_ulong(unsigned long);
180unsigned short __cdecl _byteswap_ushort(unsigned short);
Eric Christopherd1428bf2013-08-31 00:22:48 +0000181void __cdecl _disable(void);
182void __cdecl _enable(void);
Eric Christopherd1428bf2013-08-31 00:22:48 +0000183long _InterlockedAddLargeStatistic(__int64 volatile *_Addend, long _Value);
Warren Huntd6ffae92013-09-27 23:57:26 +0000184static __inline__
Eric Christopherd1428bf2013-08-31 00:22:48 +0000185long _InterlockedAnd(long volatile *_Value, long _Mask);
Warren Huntd6ffae92013-09-27 23:57:26 +0000186static __inline__
Eric Christopherd1428bf2013-08-31 00:22:48 +0000187short _InterlockedAnd16(short volatile *_Value, short _Mask);
Warren Huntd6ffae92013-09-27 23:57:26 +0000188static __inline__
Eric Christopherd1428bf2013-08-31 00:22:48 +0000189char _InterlockedAnd8(char volatile *_Value, char _Mask);
190unsigned char _interlockedbittestandreset(long volatile *, long);
Hans Wennborga3169332014-03-13 17:05:09 +0000191static __inline__
Eric Christopherd1428bf2013-08-31 00:22:48 +0000192unsigned char _interlockedbittestandset(long volatile *, long);
Warren Huntd6ffae92013-09-27 23:57:26 +0000193static __inline__
Eric Christopherd1428bf2013-08-31 00:22:48 +0000194long __cdecl _InterlockedCompareExchange(long volatile *_Destination,
195 long _Exchange, long _Comparand);
196long _InterlockedCompareExchange_HLEAcquire(long volatile *, long, long);
197long _InterlockedCompareExchange_HLERelease(long volatile *, long, long);
Warren Huntd6ffae92013-09-27 23:57:26 +0000198static __inline__
Eric Christopherd1428bf2013-08-31 00:22:48 +0000199short _InterlockedCompareExchange16(short volatile *_Destination,
200 short _Exchange, short _Comparand);
Warren Huntd6ffae92013-09-27 23:57:26 +0000201static __inline__
Eric Christopherd1428bf2013-08-31 00:22:48 +0000202__int64 _InterlockedCompareExchange64(__int64 volatile *_Destination,
203 __int64 _Exchange, __int64 _Comparand);
204__int64 _InterlockedcompareExchange64_HLEAcquire(__int64 volatile *, __int64,
205 __int64);
206__int64 _InterlockedCompareExchange64_HLERelease(__int64 volatile *, __int64,
207 __int64);
Warren Huntd6ffae92013-09-27 23:57:26 +0000208static __inline__
Eric Christopherd1428bf2013-08-31 00:22:48 +0000209char _InterlockedCompareExchange8(char volatile *_Destination, char _Exchange,
210 char _Comparand);
211void *_InterlockedCompareExchangePointer_HLEAcquire(void *volatile *, void *,
212 void *);
213void *_InterlockedCompareExchangePointer_HLERelease(void *volatile *, void *,
214 void *);
Warren Huntd6ffae92013-09-27 23:57:26 +0000215static __inline__
Eric Christopherd1428bf2013-08-31 00:22:48 +0000216long __cdecl _InterlockedDecrement(long volatile *_Addend);
Warren Huntd6ffae92013-09-27 23:57:26 +0000217static __inline__
Eric Christopherd1428bf2013-08-31 00:22:48 +0000218short _InterlockedDecrement16(short volatile *_Addend);
Saleem Abdulrasool114efe02014-06-18 20:51:10 +0000219long _InterlockedExchange(long volatile *_Target, long _Value);
Warren Huntd6ffae92013-09-27 23:57:26 +0000220static __inline__
Eric Christopherd1428bf2013-08-31 00:22:48 +0000221short _InterlockedExchange16(short volatile *_Target, short _Value);
Warren Huntd6ffae92013-09-27 23:57:26 +0000222static __inline__
Eric Christopherd1428bf2013-08-31 00:22:48 +0000223char _InterlockedExchange8(char volatile *_Target, char _Value);
Warren Huntd6ffae92013-09-27 23:57:26 +0000224static __inline__
Eric Christopherd1428bf2013-08-31 00:22:48 +0000225long __cdecl _InterlockedExchangeAdd(long volatile *_Addend, long _Value);
226long _InterlockedExchangeAdd_HLEAcquire(long volatile *, long);
227long _InterlockedExchangeAdd_HLERelease(long volatile *, long);
Reid Kleckner924eb2a2014-01-27 18:48:02 +0000228static __inline__
Eric Christopher439137e2014-01-24 12:13:47 +0000229short _InterlockedExchangeAdd16(short volatile *_Addend, short _Value);
230__int64 _InterlockedExchangeAdd64_HLEAcquire(__int64 volatile *, __int64);
231__int64 _InterlockedExchangeAdd64_HLERelease(__int64 volatile *, __int64);
Warren Huntd6ffae92013-09-27 23:57:26 +0000232static __inline__
Eric Christopherd1428bf2013-08-31 00:22:48 +0000233char _InterlockedExchangeAdd8(char volatile *_Addend, char _Value);
Warren Huntd6ffae92013-09-27 23:57:26 +0000234static __inline__
Eric Christopherd1428bf2013-08-31 00:22:48 +0000235long __cdecl _InterlockedIncrement(long volatile *_Addend);
Warren Huntd6ffae92013-09-27 23:57:26 +0000236static __inline__
Eric Christopherd1428bf2013-08-31 00:22:48 +0000237short _InterlockedIncrement16(short volatile *_Addend);
Warren Huntd6ffae92013-09-27 23:57:26 +0000238static __inline__
Eric Christopherd1428bf2013-08-31 00:22:48 +0000239long _InterlockedOr(long volatile *_Value, long _Mask);
Warren Huntd6ffae92013-09-27 23:57:26 +0000240static __inline__
Eric Christopherd1428bf2013-08-31 00:22:48 +0000241short _InterlockedOr16(short volatile *_Value, short _Mask);
Warren Huntd6ffae92013-09-27 23:57:26 +0000242static __inline__
Eric Christopherd1428bf2013-08-31 00:22:48 +0000243char _InterlockedOr8(char volatile *_Value, char _Mask);
Warren Huntd6ffae92013-09-27 23:57:26 +0000244static __inline__
Eric Christopherd1428bf2013-08-31 00:22:48 +0000245long _InterlockedXor(long volatile *_Value, long _Mask);
Warren Huntd6ffae92013-09-27 23:57:26 +0000246static __inline__
Eric Christopherd1428bf2013-08-31 00:22:48 +0000247short _InterlockedXor16(short volatile *_Value, short _Mask);
Warren Huntd6ffae92013-09-27 23:57:26 +0000248static __inline__
Eric Christopherd1428bf2013-08-31 00:22:48 +0000249char _InterlockedXor8(char volatile *_Value, char _Mask);
250void __cdecl _invpcid(unsigned int, void *);
Warren Huntd6ffae92013-09-27 23:57:26 +0000251static __inline__
Eric Christopherd1428bf2013-08-31 00:22:48 +0000252unsigned long __cdecl _lrotl(unsigned long, int);
Warren Huntd6ffae92013-09-27 23:57:26 +0000253static __inline__
Eric Christopherd1428bf2013-08-31 00:22:48 +0000254unsigned long __cdecl _lrotr(unsigned long, int);
Warren Huntd6ffae92013-09-27 23:57:26 +0000255static __inline__
Reid Kleckner00d33a52013-10-17 01:29:48 +0000256static __inline__
Eric Christopherd1428bf2013-08-31 00:22:48 +0000257void _ReadBarrier(void);
Reid Kleckner00d33a52013-10-17 01:29:48 +0000258static __inline__
Eric Christopherd1428bf2013-08-31 00:22:48 +0000259void _ReadWriteBarrier(void);
Warren Huntd6ffae92013-09-27 23:57:26 +0000260static __inline__
Eric Christopherd1428bf2013-08-31 00:22:48 +0000261void *_ReturnAddress(void);
262unsigned int _rorx_u32(unsigned int, const unsigned int);
Warren Huntd6ffae92013-09-27 23:57:26 +0000263static __inline__
Eric Christopherfb4b4332013-08-31 00:27:38 +0000264unsigned int __cdecl _rotl(unsigned int _Value, int _Shift);
Warren Huntd6ffae92013-09-27 23:57:26 +0000265static __inline__
Eric Christopherd1428bf2013-08-31 00:22:48 +0000266unsigned short _rotl16(unsigned short _Value, unsigned char _Shift);
Warren Huntd6ffae92013-09-27 23:57:26 +0000267static __inline__
Eric Christopherd1428bf2013-08-31 00:22:48 +0000268unsigned __int64 __cdecl _rotl64(unsigned __int64 _Value, int _Shift);
Warren Huntd6ffae92013-09-27 23:57:26 +0000269static __inline__
Eric Christopherd1428bf2013-08-31 00:22:48 +0000270unsigned char _rotl8(unsigned char _Value, unsigned char _Shift);
Warren Huntd6ffae92013-09-27 23:57:26 +0000271static __inline__
Eric Christopherd1428bf2013-08-31 00:22:48 +0000272unsigned int __cdecl _rotr(unsigned int _Value, int _Shift);
Warren Huntd6ffae92013-09-27 23:57:26 +0000273static __inline__
Eric Christopherd1428bf2013-08-31 00:22:48 +0000274unsigned short _rotr16(unsigned short _Value, unsigned char _Shift);
Warren Huntd6ffae92013-09-27 23:57:26 +0000275static __inline__
Eric Christopherd1428bf2013-08-31 00:22:48 +0000276unsigned __int64 __cdecl _rotr64(unsigned __int64 _Value, int _Shift);
Warren Huntd6ffae92013-09-27 23:57:26 +0000277static __inline__
Eric Christopherd1428bf2013-08-31 00:22:48 +0000278unsigned char _rotr8(unsigned char _Value, unsigned char _Shift);
279int _sarx_i32(int, unsigned int);
Nico Webera62cffa2014-07-08 18:34:46 +0000280#if __STDC_HOSTED__
Hans Wennborg1fd6dd32014-01-28 23:01:59 +0000281int __cdecl _setjmp(jmp_buf);
Nico Webera62cffa2014-07-08 18:34:46 +0000282#endif
Eric Christopherd1428bf2013-08-31 00:22:48 +0000283unsigned int _shlx_u32(unsigned int, unsigned int);
284unsigned int _shrx_u32(unsigned int, unsigned int);
285void _Store_HLERelease(long volatile *, long);
286void _Store64_HLERelease(__int64 volatile *, __int64);
287void _StorePointer_HLERelease(void *volatile *, void *);
Reid Kleckner00d33a52013-10-17 01:29:48 +0000288static __inline__
Eric Christopherd1428bf2013-08-31 00:22:48 +0000289void _WriteBarrier(void);
Eric Christopherd1428bf2013-08-31 00:22:48 +0000290unsigned __int32 xbegin(void);
291void _xend(void);
Hans Wennborg854f7d32014-01-16 23:39:35 +0000292static __inline__
Hans Wennborg2e56d952015-01-27 23:34:35 +0000293#define _XCR_XFEATURE_ENABLED_MASK 0
Eric Christopherd1428bf2013-08-31 00:22:48 +0000294unsigned __int64 __cdecl _xgetbv(unsigned int);
295void __cdecl _xrstor(void const *, unsigned __int64);
296void __cdecl _xsave(void *, unsigned __int64);
297void __cdecl _xsaveopt(void *, unsigned __int64);
298void __cdecl _xsetbv(unsigned int, unsigned __int64);
Eric Christopherd1428bf2013-08-31 00:22:48 +0000299
300/* These additional intrinsics are turned on in x64/amd64/x86_64 mode. */
Warren Hunt3f987942013-09-30 21:08:05 +0000301#ifdef __x86_64__
Eric Christopherd1428bf2013-08-31 00:22:48 +0000302void __addgsbyte(unsigned long, unsigned char);
303void __addgsdword(unsigned long, unsigned long);
304void __addgsqword(unsigned long, unsigned __int64);
305void __addgsword(unsigned long, unsigned short);
Reid Klecknerf08d6582014-01-27 19:16:35 +0000306static __inline__
Eric Christopherd1428bf2013-08-31 00:22:48 +0000307void __faststorefence(void);
308void __incgsbyte(unsigned long);
309void __incgsdword(unsigned long);
310void __incgsqword(unsigned long);
311void __incgsword(unsigned long);
Eric Christopher439137e2014-01-24 12:13:47 +0000312unsigned char __lwpins64(unsigned __int64, unsigned int, unsigned int);
313void __lwpval64(unsigned __int64, unsigned int, unsigned int);
314unsigned __int64 __lzcnt64(unsigned __int64);
Hans Wennborgd9be72e2014-03-12 22:00:32 +0000315static __inline__
Eric Christopher439137e2014-01-24 12:13:47 +0000316void __movsq(unsigned long long *, unsigned long long const *, size_t);
317__int64 __mulh(__int64, __int64);
Reid Kleckner924eb2a2014-01-27 18:48:02 +0000318static __inline__
Eric Christopherd1428bf2013-08-31 00:22:48 +0000319unsigned __int64 __popcnt64(unsigned __int64);
Hans Wennborga4421e02014-03-12 21:09:05 +0000320static __inline__
Eric Christopher439137e2014-01-24 12:13:47 +0000321unsigned char __readgsbyte(unsigned long);
Hans Wennborga4421e02014-03-12 21:09:05 +0000322static __inline__
Eric Christopher439137e2014-01-24 12:13:47 +0000323unsigned long __readgsdword(unsigned long);
Hans Wennborga4421e02014-03-12 21:09:05 +0000324static __inline__
325unsigned __int64 __readgsqword(unsigned long);
Eric Christopher439137e2014-01-24 12:13:47 +0000326unsigned short __readgsword(unsigned long);
Eric Christopherd1428bf2013-08-31 00:22:48 +0000327unsigned __int64 __shiftleft128(unsigned __int64 _LowPart,
328 unsigned __int64 _HighPart,
329 unsigned char _Shift);
330unsigned __int64 __shiftright128(unsigned __int64 _LowPart,
331 unsigned __int64 _HighPart,
332 unsigned char _Shift);
Hans Wennborgd9be72e2014-03-12 22:00:32 +0000333static __inline__
Eric Christopherd1428bf2013-08-31 00:22:48 +0000334void __stosq(unsigned __int64 *, unsigned __int64, size_t);
Eric Christopher439137e2014-01-24 12:13:47 +0000335unsigned char __vmx_on(unsigned __int64 *);
336unsigned char __vmx_vmclear(unsigned __int64 *);
337unsigned char __vmx_vmlaunch(void);
338unsigned char __vmx_vmptrld(unsigned __int64 *);
339unsigned char __vmx_vmread(size_t, size_t *);
340unsigned char __vmx_vmresume(void);
341unsigned char __vmx_vmwrite(size_t, size_t);
342void __writegsbyte(unsigned long, unsigned char);
343void __writegsdword(unsigned long, unsigned long);
344void __writegsqword(unsigned long, unsigned __int64);
345void __writegsword(unsigned long, unsigned short);
Warren Huntd6ffae92013-09-27 23:57:26 +0000346static __inline__
Eric Christopherd1428bf2013-08-31 00:22:48 +0000347unsigned char _BitScanForward64(unsigned long *_Index, unsigned __int64 _Mask);
Warren Huntd6ffae92013-09-27 23:57:26 +0000348static __inline__
Eric Christopherd1428bf2013-08-31 00:22:48 +0000349unsigned char _BitScanReverse64(unsigned long *_Index, unsigned __int64 _Mask);
Warren Huntd6ffae92013-09-27 23:57:26 +0000350static __inline__
Eric Christopherd1428bf2013-08-31 00:22:48 +0000351unsigned char _bittest64(__int64 const *, __int64);
Warren Huntd6ffae92013-09-27 23:57:26 +0000352static __inline__
Eric Christopherd1428bf2013-08-31 00:22:48 +0000353unsigned char _bittestandcomplement64(__int64 *, __int64);
Warren Huntd6ffae92013-09-27 23:57:26 +0000354static __inline__
Eric Christopherd1428bf2013-08-31 00:22:48 +0000355unsigned char _bittestandreset64(__int64 *, __int64);
Warren Huntd6ffae92013-09-27 23:57:26 +0000356static __inline__
Eric Christopherd1428bf2013-08-31 00:22:48 +0000357unsigned char _bittestandset64(__int64 *, __int64);
Eric Christopherd1428bf2013-08-31 00:22:48 +0000358unsigned __int64 __cdecl _byteswap_uint64(unsigned __int64);
Eric Christopherd1428bf2013-08-31 00:22:48 +0000359long _InterlockedAnd_np(long volatile *_Value, long _Mask);
360short _InterlockedAnd16_np(short volatile *_Value, short _Mask);
361__int64 _InterlockedAnd64_np(__int64 volatile *_Value, __int64 _Mask);
362char _InterlockedAnd8_np(char volatile *_Value, char _Mask);
363unsigned char _interlockedbittestandreset64(__int64 volatile *, __int64);
Hans Wennborga3169332014-03-13 17:05:09 +0000364static __inline__
Eric Christopherd1428bf2013-08-31 00:22:48 +0000365unsigned char _interlockedbittestandset64(__int64 volatile *, __int64);
366long _InterlockedCompareExchange_np(long volatile *_Destination, long _Exchange,
367 long _Comparand);
368unsigned char _InterlockedCompareExchange128(__int64 volatile *_Destination,
369 __int64 _ExchangeHigh,
370 __int64 _ExchangeLow,
371 __int64 *_CompareandResult);
372unsigned char _InterlockedCompareExchange128_np(__int64 volatile *_Destination,
373 __int64 _ExchangeHigh,
374 __int64 _ExchangeLow,
375 __int64 *_ComparandResult);
376short _InterlockedCompareExchange16_np(short volatile *_Destination,
377 short _Exchange, short _Comparand);
Eric Christopher439137e2014-01-24 12:13:47 +0000378__int64 _InterlockedCompareExchange64_HLEAcquire(__int64 volatile *, __int64,
379 __int64);
380__int64 _InterlockedCompareExchange64_HLERelease(__int64 volatile *, __int64,
381 __int64);
Eric Christopherd1428bf2013-08-31 00:22:48 +0000382__int64 _InterlockedCompareExchange64_np(__int64 volatile *_Destination,
383 __int64 _Exchange, __int64 _Comparand);
Eric Christopher58b40432014-01-25 01:38:30 +0000384void *_InterlockedCompareExchangePointer(void *volatile *_Destination,
385 void *_Exchange, void *_Comparand);
Eric Christopherd1428bf2013-08-31 00:22:48 +0000386void *_InterlockedCompareExchangePointer_np(void *volatile *_Destination,
387 void *_Exchange, void *_Comparand);
Reid Kleckner924eb2a2014-01-27 18:48:02 +0000388static __inline__
Eric Christopher439137e2014-01-24 12:13:47 +0000389__int64 _InterlockedDecrement64(__int64 volatile *_Addend);
Reid Kleckner924eb2a2014-01-27 18:48:02 +0000390static __inline__
Eric Christopher439137e2014-01-24 12:13:47 +0000391__int64 _InterlockedExchange64(__int64 volatile *_Target, __int64 _Value);
Reid Kleckner924eb2a2014-01-27 18:48:02 +0000392static __inline__
Eric Christopher439137e2014-01-24 12:13:47 +0000393__int64 _InterlockedExchangeAdd64(__int64 volatile *_Addend, __int64 _Value);
394void *_InterlockedExchangePointer(void *volatile *_Target, void *_Value);
Reid Kleckner924eb2a2014-01-27 18:48:02 +0000395static __inline__
Eric Christopher439137e2014-01-24 12:13:47 +0000396__int64 _InterlockedIncrement64(__int64 volatile *_Addend);
Eric Christopherd1428bf2013-08-31 00:22:48 +0000397long _InterlockedOr_np(long volatile *_Value, long _Mask);
398short _InterlockedOr16_np(short volatile *_Value, short _Mask);
Reid Kleckner924eb2a2014-01-27 18:48:02 +0000399static __inline__
Eric Christopher439137e2014-01-24 12:13:47 +0000400__int64 _InterlockedOr64(__int64 volatile *_Value, __int64 _Mask);
Warren Hunt41a993f2013-09-28 00:15:41 +0000401__int64 _InterlockedOr64_np(__int64 volatile *_Value, __int64 _Mask);
Eric Christopherd1428bf2013-08-31 00:22:48 +0000402char _InterlockedOr8_np(char volatile *_Value, char _Mask);
403long _InterlockedXor_np(long volatile *_Value, long _Mask);
404short _InterlockedXor16_np(short volatile *_Value, short _Mask);
Reid Kleckner924eb2a2014-01-27 18:48:02 +0000405static __inline__
Eric Christopher439137e2014-01-24 12:13:47 +0000406__int64 _InterlockedXor64(__int64 volatile *_Value, __int64 _Mask);
Eric Christopherd1428bf2013-08-31 00:22:48 +0000407__int64 _InterlockedXor64_np(__int64 volatile *_Value, __int64 _Mask);
408char _InterlockedXor8_np(char volatile *_Value, char _Mask);
Reid Kleckner924eb2a2014-01-27 18:48:02 +0000409static __inline__
Eric Christopherd1428bf2013-08-31 00:22:48 +0000410__int64 _mul128(__int64 _Multiplier, __int64 _Multiplicand,
411 __int64 *_HighProduct);
Eric Christopherd1428bf2013-08-31 00:22:48 +0000412unsigned __int64 _rorx_u64(unsigned __int64, const unsigned int);
Eric Christopher439137e2014-01-24 12:13:47 +0000413__int64 _sarx_i64(__int64, unsigned int);
Nico Webera62cffa2014-07-08 18:34:46 +0000414#if __STDC_HOSTED__
415int __cdecl _setjmpex(jmp_buf);
416#endif
Eric Christopher439137e2014-01-24 12:13:47 +0000417unsigned __int64 _shlx_u64(unsigned __int64, unsigned int);
David Majnemer54507632014-12-02 23:30:26 +0000418unsigned __int64 _shrx_u64(unsigned __int64, unsigned int);
David Majnemer5f9afc52014-12-02 23:30:24 +0000419/*
420 * Multiply two 64-bit integers and obtain a 64-bit result.
421 * The low-half is returned directly and the high half is in an out parameter.
422 */
Michael Kupersteine45af542015-06-30 13:36:19 +0000423static __inline__ unsigned __int64 __DEFAULT_FN_ATTRS
David Majnemer5f9afc52014-12-02 23:30:24 +0000424_umul128(unsigned __int64 _Multiplier, unsigned __int64 _Multiplicand,
425 unsigned __int64 *_HighProduct) {
426 unsigned __int128 _FullProduct =
427 (unsigned __int128)_Multiplier * (unsigned __int128)_Multiplicand;
David Majnemer00973ce2014-12-02 23:44:40 +0000428 *_HighProduct = _FullProduct >> 64;
429 return _FullProduct;
David Majnemer5f9afc52014-12-02 23:30:24 +0000430}
Michael Kupersteine45af542015-06-30 13:36:19 +0000431static __inline__ unsigned __int64 __DEFAULT_FN_ATTRS
Reid Klecknerbaf77092014-12-03 23:36:14 +0000432__umulh(unsigned __int64 _Multiplier, unsigned __int64 _Multiplicand) {
433 unsigned __int128 _FullProduct =
434 (unsigned __int128)_Multiplier * (unsigned __int128)_Multiplicand;
435 return _FullProduct >> 64;
436}
Eric Christopherd1428bf2013-08-31 00:22:48 +0000437void __cdecl _xrstor64(void const *, unsigned __int64);
438void __cdecl _xsave64(void *, unsigned __int64);
439void __cdecl _xsaveopt64(void *, unsigned __int64);
Eric Christopher0db88a72013-09-18 22:24:01 +0000440
Warren Hunt3f987942013-09-30 21:08:05 +0000441#endif /* __x86_64__ */
Reid Klecknerf0e23222013-09-19 00:19:53 +0000442
Warren Huntd6ffae92013-09-27 23:57:26 +0000443/*----------------------------------------------------------------------------*\
444|* Bit Twiddling
445\*----------------------------------------------------------------------------*/
Michael Kupersteine45af542015-06-30 13:36:19 +0000446static __inline__ unsigned char __DEFAULT_FN_ATTRS
Warren Huntd6ffae92013-09-27 23:57:26 +0000447_rotl8(unsigned char _Value, unsigned char _Shift) {
448 _Shift &= 0x7;
449 return _Shift ? (_Value << _Shift) | (_Value >> (8 - _Shift)) : _Value;
450}
Michael Kupersteine45af542015-06-30 13:36:19 +0000451static __inline__ unsigned char __DEFAULT_FN_ATTRS
Warren Huntd6ffae92013-09-27 23:57:26 +0000452_rotr8(unsigned char _Value, unsigned char _Shift) {
453 _Shift &= 0x7;
454 return _Shift ? (_Value >> _Shift) | (_Value << (8 - _Shift)) : _Value;
455}
Michael Kupersteine45af542015-06-30 13:36:19 +0000456static __inline__ unsigned short __DEFAULT_FN_ATTRS
Warren Huntd6ffae92013-09-27 23:57:26 +0000457_rotl16(unsigned short _Value, unsigned char _Shift) {
458 _Shift &= 0xf;
459 return _Shift ? (_Value << _Shift) | (_Value >> (16 - _Shift)) : _Value;
460}
Michael Kupersteine45af542015-06-30 13:36:19 +0000461static __inline__ unsigned short __DEFAULT_FN_ATTRS
Warren Huntd6ffae92013-09-27 23:57:26 +0000462_rotr16(unsigned short _Value, unsigned char _Shift) {
463 _Shift &= 0xf;
464 return _Shift ? (_Value >> _Shift) | (_Value << (16 - _Shift)) : _Value;
465}
Michael Kupersteine45af542015-06-30 13:36:19 +0000466static __inline__ unsigned int __DEFAULT_FN_ATTRS
Warren Huntd6ffae92013-09-27 23:57:26 +0000467_rotl(unsigned int _Value, int _Shift) {
468 _Shift &= 0x1f;
469 return _Shift ? (_Value << _Shift) | (_Value >> (32 - _Shift)) : _Value;
470}
Michael Kupersteine45af542015-06-30 13:36:19 +0000471static __inline__ unsigned int __DEFAULT_FN_ATTRS
Warren Huntd6ffae92013-09-27 23:57:26 +0000472_rotr(unsigned int _Value, int _Shift) {
473 _Shift &= 0x1f;
474 return _Shift ? (_Value >> _Shift) | (_Value << (32 - _Shift)) : _Value;
475}
Michael Kupersteine45af542015-06-30 13:36:19 +0000476static __inline__ unsigned long __DEFAULT_FN_ATTRS
Warren Huntd6ffae92013-09-27 23:57:26 +0000477_lrotl(unsigned long _Value, int _Shift) {
478 _Shift &= 0x1f;
479 return _Shift ? (_Value << _Shift) | (_Value >> (32 - _Shift)) : _Value;
480}
Michael Kupersteine45af542015-06-30 13:36:19 +0000481static __inline__ unsigned long __DEFAULT_FN_ATTRS
Warren Huntd6ffae92013-09-27 23:57:26 +0000482_lrotr(unsigned long _Value, int _Shift) {
483 _Shift &= 0x1f;
484 return _Shift ? (_Value >> _Shift) | (_Value << (32 - _Shift)) : _Value;
485}
486static
Michael Kupersteine45af542015-06-30 13:36:19 +0000487__inline__ unsigned __int64 __DEFAULT_FN_ATTRS
Warren Huntd6ffae92013-09-27 23:57:26 +0000488_rotl64(unsigned __int64 _Value, int _Shift) {
489 _Shift &= 0x3f;
490 return _Shift ? (_Value << _Shift) | (_Value >> (64 - _Shift)) : _Value;
491}
492static
Michael Kupersteine45af542015-06-30 13:36:19 +0000493__inline__ unsigned __int64 __DEFAULT_FN_ATTRS
Warren Huntd6ffae92013-09-27 23:57:26 +0000494_rotr64(unsigned __int64 _Value, int _Shift) {
495 _Shift &= 0x3f;
496 return _Shift ? (_Value >> _Shift) | (_Value << (64 - _Shift)) : _Value;
497}
498/*----------------------------------------------------------------------------*\
499|* Bit Counting and Testing
500\*----------------------------------------------------------------------------*/
Michael Kupersteine45af542015-06-30 13:36:19 +0000501static __inline__ unsigned char __DEFAULT_FN_ATTRS
Warren Huntd6ffae92013-09-27 23:57:26 +0000502_BitScanForward(unsigned long *_Index, unsigned long _Mask) {
503 if (!_Mask)
504 return 0;
505 *_Index = __builtin_ctzl(_Mask);
506 return 1;
507}
Michael Kupersteine45af542015-06-30 13:36:19 +0000508static __inline__ unsigned char __DEFAULT_FN_ATTRS
Warren Huntd6ffae92013-09-27 23:57:26 +0000509_BitScanReverse(unsigned long *_Index, unsigned long _Mask) {
510 if (!_Mask)
511 return 0;
512 *_Index = 31 - __builtin_clzl(_Mask);
513 return 1;
514}
Michael Kupersteine45af542015-06-30 13:36:19 +0000515static __inline__ unsigned short __DEFAULT_FN_ATTRS
David Majnemer8dadce72015-07-13 02:53:23 +0000516__popcnt16(unsigned short _Value) {
517 return __builtin_popcount((int)_Value);
Warren Huntd6ffae92013-09-27 23:57:26 +0000518}
Michael Kupersteine45af542015-06-30 13:36:19 +0000519static __inline__ unsigned int __DEFAULT_FN_ATTRS
David Majnemer8dadce72015-07-13 02:53:23 +0000520__popcnt(unsigned int _Value) {
521 return __builtin_popcount(_Value);
Warren Huntd6ffae92013-09-27 23:57:26 +0000522}
Michael Kupersteine45af542015-06-30 13:36:19 +0000523static __inline__ unsigned char __DEFAULT_FN_ATTRS
David Majnemer8dadce72015-07-13 02:53:23 +0000524_bittest(long const *__BitBase, long __BitPos) {
525 return (*__BitBase >> __BitPos) & 1;
Warren Huntd6ffae92013-09-27 23:57:26 +0000526}
Michael Kupersteine45af542015-06-30 13:36:19 +0000527static __inline__ unsigned char __DEFAULT_FN_ATTRS
David Majnemer8dadce72015-07-13 02:53:23 +0000528_bittestandcomplement(long *__BitBase, long __BitPos) {
529 unsigned char __Res = (*__BitBase >> __BitPos) & 1;
530 *__BitBase = *__BitBase ^ (1 << __BitPos);
531 return __Res;
Warren Huntd6ffae92013-09-27 23:57:26 +0000532}
Michael Kupersteine45af542015-06-30 13:36:19 +0000533static __inline__ unsigned char __DEFAULT_FN_ATTRS
David Majnemer8dadce72015-07-13 02:53:23 +0000534_bittestandreset(long *__BitBase, long __BitPos) {
535 unsigned char __Res = (*__BitBase >> __BitPos) & 1;
536 *__BitBase = *__BitBase & ~(1 << __BitPos);
537 return __Res;
Warren Huntd6ffae92013-09-27 23:57:26 +0000538}
Michael Kupersteine45af542015-06-30 13:36:19 +0000539static __inline__ unsigned char __DEFAULT_FN_ATTRS
David Majnemer8dadce72015-07-13 02:53:23 +0000540_bittestandset(long *__BitBase, long __BitPos) {
541 unsigned char __Res = (*__BitBase >> __BitPos) & 1;
542 *__BitBase = *__BitBase | (1 << __BitPos);
543 return __Res;
Warren Huntd6ffae92013-09-27 23:57:26 +0000544}
Saleem Abdulrasool702eefe2014-06-25 16:48:40 +0000545#if defined(__i386__) || defined(__x86_64__)
Michael Kupersteine45af542015-06-30 13:36:19 +0000546static __inline__ unsigned char __DEFAULT_FN_ATTRS
Hans Wennborga3169332014-03-13 17:05:09 +0000547_interlockedbittestandset(long volatile *__BitBase, long __BitPos) {
David Majnemer3c8ea5f2015-07-13 02:53:19 +0000548 return (__atomic_fetch_or(__BitBase, 1l << __BitPos, 5) >> __BitPos) & 1;
Hans Wennborga3169332014-03-13 17:05:09 +0000549}
Saleem Abdulrasool702eefe2014-06-25 16:48:40 +0000550#endif
Warren Hunt3f987942013-09-30 21:08:05 +0000551#ifdef __x86_64__
Michael Kupersteine45af542015-06-30 13:36:19 +0000552static __inline__ unsigned char __DEFAULT_FN_ATTRS
Warren Huntd6ffae92013-09-27 23:57:26 +0000553_BitScanForward64(unsigned long *_Index, unsigned __int64 _Mask) {
554 if (!_Mask)
555 return 0;
556 *_Index = __builtin_ctzll(_Mask);
557 return 1;
558}
Michael Kupersteine45af542015-06-30 13:36:19 +0000559static __inline__ unsigned char __DEFAULT_FN_ATTRS
Warren Huntd6ffae92013-09-27 23:57:26 +0000560_BitScanReverse64(unsigned long *_Index, unsigned __int64 _Mask) {
561 if (!_Mask)
562 return 0;
563 *_Index = 63 - __builtin_clzll(_Mask);
564 return 1;
565}
Warren Huntd6ffae92013-09-27 23:57:26 +0000566static __inline__
Michael Kupersteine45af542015-06-30 13:36:19 +0000567unsigned __int64 __DEFAULT_FN_ATTRS
David Majnemer8dadce72015-07-13 02:53:23 +0000568__popcnt64(unsigned __int64 _Value) {
569 return __builtin_popcountll(_Value);
Warren Huntd6ffae92013-09-27 23:57:26 +0000570}
Michael Kupersteine45af542015-06-30 13:36:19 +0000571static __inline__ unsigned char __DEFAULT_FN_ATTRS
David Majnemer8dadce72015-07-13 02:53:23 +0000572_bittest64(__int64 const *__BitBase, __int64 __BitPos) {
573 return (*__BitBase >> __BitPos) & 1;
Warren Huntd6ffae92013-09-27 23:57:26 +0000574}
Michael Kupersteine45af542015-06-30 13:36:19 +0000575static __inline__ unsigned char __DEFAULT_FN_ATTRS
David Majnemer8dadce72015-07-13 02:53:23 +0000576_bittestandcomplement64(__int64 *__BitBase, __int64 __BitPos) {
577 unsigned char __Res = (*__BitBase >> __BitPos) & 1;
578 *__BitBase = *__BitBase ^ (1ll << __BitPos);
579 return __Res;
Warren Huntd6ffae92013-09-27 23:57:26 +0000580}
Michael Kupersteine45af542015-06-30 13:36:19 +0000581static __inline__ unsigned char __DEFAULT_FN_ATTRS
David Majnemer8dadce72015-07-13 02:53:23 +0000582_bittestandreset64(__int64 *__BitBase, __int64 __BitPos) {
583 unsigned char __Res = (*__BitBase >> __BitPos) & 1;
584 *__BitBase = *__BitBase & ~(1ll << __BitPos);
585 return __Res;
Warren Huntd6ffae92013-09-27 23:57:26 +0000586}
Michael Kupersteine45af542015-06-30 13:36:19 +0000587static __inline__ unsigned char __DEFAULT_FN_ATTRS
David Majnemer8dadce72015-07-13 02:53:23 +0000588_bittestandset64(__int64 *__BitBase, __int64 __BitPos) {
589 unsigned char __Res = (*__BitBase >> __BitPos) & 1;
590 *__BitBase = *__BitBase | (1ll << __BitPos);
591 return __Res;
Warren Huntd6ffae92013-09-27 23:57:26 +0000592}
Michael Kupersteine45af542015-06-30 13:36:19 +0000593static __inline__ unsigned char __DEFAULT_FN_ATTRS
Hans Wennborga3169332014-03-13 17:05:09 +0000594_interlockedbittestandset64(__int64 volatile *__BitBase, __int64 __BitPos) {
David Majnemer3c8ea5f2015-07-13 02:53:19 +0000595 return (__atomic_fetch_or(__BitBase, 1ll << __BitPos, 5) >> __BitPos) & 1;
Hans Wennborga3169332014-03-13 17:05:09 +0000596}
Warren Huntd6ffae92013-09-27 23:57:26 +0000597#endif
598/*----------------------------------------------------------------------------*\
599|* Interlocked Exchange Add
600\*----------------------------------------------------------------------------*/
Michael Kupersteine45af542015-06-30 13:36:19 +0000601static __inline__ char __DEFAULT_FN_ATTRS
Warren Huntd6ffae92013-09-27 23:57:26 +0000602_InterlockedExchangeAdd8(char volatile *_Addend, char _Value) {
David Majnemer3c8ea5f2015-07-13 02:53:19 +0000603 return __atomic_fetch_add(_Addend, _Value, 5);
Warren Huntd6ffae92013-09-27 23:57:26 +0000604}
Michael Kupersteine45af542015-06-30 13:36:19 +0000605static __inline__ short __DEFAULT_FN_ATTRS
Warren Huntd6ffae92013-09-27 23:57:26 +0000606_InterlockedExchangeAdd16(short volatile *_Addend, short _Value) {
David Majnemer3c8ea5f2015-07-13 02:53:19 +0000607 return __atomic_fetch_add(_Addend, _Value, 5);
Warren Huntd6ffae92013-09-27 23:57:26 +0000608}
Warren Hunt3f987942013-09-30 21:08:05 +0000609#ifdef __x86_64__
Michael Kupersteine45af542015-06-30 13:36:19 +0000610static __inline__ __int64 __DEFAULT_FN_ATTRS
Warren Huntd6ffae92013-09-27 23:57:26 +0000611_InterlockedExchangeAdd64(__int64 volatile *_Addend, __int64 _Value) {
David Majnemer3c8ea5f2015-07-13 02:53:19 +0000612 return __atomic_fetch_add(_Addend, _Value, 5);
Warren Huntd6ffae92013-09-27 23:57:26 +0000613}
614#endif
615/*----------------------------------------------------------------------------*\
616|* Interlocked Exchange Sub
617\*----------------------------------------------------------------------------*/
Michael Kupersteine45af542015-06-30 13:36:19 +0000618static __inline__ char __DEFAULT_FN_ATTRS
Warren Huntd6ffae92013-09-27 23:57:26 +0000619_InterlockedExchangeSub8(char volatile *_Subend, char _Value) {
David Majnemer3c8ea5f2015-07-13 02:53:19 +0000620 return __atomic_fetch_sub(_Subend, _Value, 5);
Warren Huntd6ffae92013-09-27 23:57:26 +0000621}
Michael Kupersteine45af542015-06-30 13:36:19 +0000622static __inline__ short __DEFAULT_FN_ATTRS
Warren Huntd6ffae92013-09-27 23:57:26 +0000623_InterlockedExchangeSub16(short volatile *_Subend, short _Value) {
David Majnemer3c8ea5f2015-07-13 02:53:19 +0000624 return __atomic_fetch_sub(_Subend, _Value, 5);
Warren Huntd6ffae92013-09-27 23:57:26 +0000625}
Michael Kupersteine45af542015-06-30 13:36:19 +0000626static __inline__ long __DEFAULT_FN_ATTRS
Warren Huntd6ffae92013-09-27 23:57:26 +0000627_InterlockedExchangeSub(long volatile *_Subend, long _Value) {
David Majnemer3c8ea5f2015-07-13 02:53:19 +0000628 return __atomic_fetch_sub(_Subend, _Value, 5);
Warren Huntd6ffae92013-09-27 23:57:26 +0000629}
Warren Hunt3f987942013-09-30 21:08:05 +0000630#ifdef __x86_64__
Michael Kupersteine45af542015-06-30 13:36:19 +0000631static __inline__ __int64 __DEFAULT_FN_ATTRS
Warren Huntd6ffae92013-09-27 23:57:26 +0000632_InterlockedExchangeSub64(__int64 volatile *_Subend, __int64 _Value) {
David Majnemer3c8ea5f2015-07-13 02:53:19 +0000633 return __atomic_fetch_sub(_Subend, _Value, 5);
Warren Huntd6ffae92013-09-27 23:57:26 +0000634}
635#endif
636/*----------------------------------------------------------------------------*\
637|* Interlocked Increment
638\*----------------------------------------------------------------------------*/
Michael Kupersteine45af542015-06-30 13:36:19 +0000639static __inline__ short __DEFAULT_FN_ATTRS
Hans Wennborg2ed88802014-01-23 19:15:39 +0000640_InterlockedIncrement16(short volatile *_Value) {
David Majnemer3c8ea5f2015-07-13 02:53:19 +0000641 return __atomic_add_fetch(_Value, 1, 5);
Warren Huntd6ffae92013-09-27 23:57:26 +0000642}
Warren Hunt3f987942013-09-30 21:08:05 +0000643#ifdef __x86_64__
Michael Kupersteine45af542015-06-30 13:36:19 +0000644static __inline__ __int64 __DEFAULT_FN_ATTRS
Warren Huntd6ffae92013-09-27 23:57:26 +0000645_InterlockedIncrement64(__int64 volatile *_Value) {
David Majnemer3c8ea5f2015-07-13 02:53:19 +0000646 return __atomic_add_fetch(_Value, 1, 5);
Warren Huntd6ffae92013-09-27 23:57:26 +0000647}
648#endif
649/*----------------------------------------------------------------------------*\
650|* Interlocked Decrement
651\*----------------------------------------------------------------------------*/
Michael Kupersteine45af542015-06-30 13:36:19 +0000652static __inline__ short __DEFAULT_FN_ATTRS
Hans Wennborg2ed88802014-01-23 19:15:39 +0000653_InterlockedDecrement16(short volatile *_Value) {
David Majnemer3c8ea5f2015-07-13 02:53:19 +0000654 return __atomic_sub_fetch(_Value, 1, 5);
Warren Huntd6ffae92013-09-27 23:57:26 +0000655}
Warren Hunt3f987942013-09-30 21:08:05 +0000656#ifdef __x86_64__
Michael Kupersteine45af542015-06-30 13:36:19 +0000657static __inline__ __int64 __DEFAULT_FN_ATTRS
Warren Huntd6ffae92013-09-27 23:57:26 +0000658_InterlockedDecrement64(__int64 volatile *_Value) {
David Majnemer3c8ea5f2015-07-13 02:53:19 +0000659 return __atomic_sub_fetch(_Value, 1, 5);
Warren Huntd6ffae92013-09-27 23:57:26 +0000660}
661#endif
662/*----------------------------------------------------------------------------*\
663|* Interlocked And
664\*----------------------------------------------------------------------------*/
Michael Kupersteine45af542015-06-30 13:36:19 +0000665static __inline__ char __DEFAULT_FN_ATTRS
Warren Huntd6ffae92013-09-27 23:57:26 +0000666_InterlockedAnd8(char volatile *_Value, char _Mask) {
David Majnemer3c8ea5f2015-07-13 02:53:19 +0000667 return __atomic_and_fetch(_Value, _Mask, 5);
Warren Huntd6ffae92013-09-27 23:57:26 +0000668}
Michael Kupersteine45af542015-06-30 13:36:19 +0000669static __inline__ short __DEFAULT_FN_ATTRS
Warren Huntd6ffae92013-09-27 23:57:26 +0000670_InterlockedAnd16(short volatile *_Value, short _Mask) {
David Majnemer3c8ea5f2015-07-13 02:53:19 +0000671 return __atomic_and_fetch(_Value, _Mask, 5);
Warren Huntd6ffae92013-09-27 23:57:26 +0000672}
Michael Kupersteine45af542015-06-30 13:36:19 +0000673static __inline__ long __DEFAULT_FN_ATTRS
Warren Huntd6ffae92013-09-27 23:57:26 +0000674_InterlockedAnd(long volatile *_Value, long _Mask) {
David Majnemer3c8ea5f2015-07-13 02:53:19 +0000675 return __atomic_and_fetch(_Value, _Mask, 5);
Warren Huntd6ffae92013-09-27 23:57:26 +0000676}
Warren Hunt3f987942013-09-30 21:08:05 +0000677#ifdef __x86_64__
Michael Kupersteine45af542015-06-30 13:36:19 +0000678static __inline__ __int64 __DEFAULT_FN_ATTRS
Warren Huntd6ffae92013-09-27 23:57:26 +0000679_InterlockedAnd64(__int64 volatile *_Value, __int64 _Mask) {
David Majnemer3c8ea5f2015-07-13 02:53:19 +0000680 return __atomic_and_fetch(_Value, _Mask, 5);
Warren Huntd6ffae92013-09-27 23:57:26 +0000681}
682#endif
683/*----------------------------------------------------------------------------*\
684|* Interlocked Or
685\*----------------------------------------------------------------------------*/
Michael Kupersteine45af542015-06-30 13:36:19 +0000686static __inline__ char __DEFAULT_FN_ATTRS
Warren Huntd6ffae92013-09-27 23:57:26 +0000687_InterlockedOr8(char volatile *_Value, char _Mask) {
David Majnemer3c8ea5f2015-07-13 02:53:19 +0000688 return __atomic_or_fetch(_Value, _Mask, 5);
Warren Huntd6ffae92013-09-27 23:57:26 +0000689}
Michael Kupersteine45af542015-06-30 13:36:19 +0000690static __inline__ short __DEFAULT_FN_ATTRS
Warren Huntd6ffae92013-09-27 23:57:26 +0000691_InterlockedOr16(short volatile *_Value, short _Mask) {
David Majnemer3c8ea5f2015-07-13 02:53:19 +0000692 return __atomic_or_fetch(_Value, _Mask, 5);
Warren Huntd6ffae92013-09-27 23:57:26 +0000693}
Michael Kupersteine45af542015-06-30 13:36:19 +0000694static __inline__ long __DEFAULT_FN_ATTRS
Warren Huntd6ffae92013-09-27 23:57:26 +0000695_InterlockedOr(long volatile *_Value, long _Mask) {
David Majnemer3c8ea5f2015-07-13 02:53:19 +0000696 return __atomic_or_fetch(_Value, _Mask, 5);
Warren Huntd6ffae92013-09-27 23:57:26 +0000697}
Warren Hunt3f987942013-09-30 21:08:05 +0000698#ifdef __x86_64__
Michael Kupersteine45af542015-06-30 13:36:19 +0000699static __inline__ __int64 __DEFAULT_FN_ATTRS
Warren Huntd6ffae92013-09-27 23:57:26 +0000700_InterlockedOr64(__int64 volatile *_Value, __int64 _Mask) {
David Majnemer3c8ea5f2015-07-13 02:53:19 +0000701 return __atomic_or_fetch(_Value, _Mask, 5);
Warren Huntd6ffae92013-09-27 23:57:26 +0000702}
703#endif
704/*----------------------------------------------------------------------------*\
705|* Interlocked Xor
706\*----------------------------------------------------------------------------*/
Michael Kupersteine45af542015-06-30 13:36:19 +0000707static __inline__ char __DEFAULT_FN_ATTRS
Warren Huntd6ffae92013-09-27 23:57:26 +0000708_InterlockedXor8(char volatile *_Value, char _Mask) {
David Majnemer3c8ea5f2015-07-13 02:53:19 +0000709 return __atomic_xor_fetch(_Value, _Mask, 5);
Warren Huntd6ffae92013-09-27 23:57:26 +0000710}
Michael Kupersteine45af542015-06-30 13:36:19 +0000711static __inline__ short __DEFAULT_FN_ATTRS
Warren Huntd6ffae92013-09-27 23:57:26 +0000712_InterlockedXor16(short volatile *_Value, short _Mask) {
David Majnemer3c8ea5f2015-07-13 02:53:19 +0000713 return __atomic_xor_fetch(_Value, _Mask, 5);
Warren Huntd6ffae92013-09-27 23:57:26 +0000714}
Michael Kupersteine45af542015-06-30 13:36:19 +0000715static __inline__ long __DEFAULT_FN_ATTRS
Warren Huntd6ffae92013-09-27 23:57:26 +0000716_InterlockedXor(long volatile *_Value, long _Mask) {
David Majnemer3c8ea5f2015-07-13 02:53:19 +0000717 return __atomic_xor_fetch(_Value, _Mask, 5);
Warren Huntd6ffae92013-09-27 23:57:26 +0000718}
Warren Hunt3f987942013-09-30 21:08:05 +0000719#ifdef __x86_64__
Michael Kupersteine45af542015-06-30 13:36:19 +0000720static __inline__ __int64 __DEFAULT_FN_ATTRS
Warren Huntd6ffae92013-09-27 23:57:26 +0000721_InterlockedXor64(__int64 volatile *_Value, __int64 _Mask) {
David Majnemer3c8ea5f2015-07-13 02:53:19 +0000722 return __atomic_xor_fetch(_Value, _Mask, 5);
Warren Huntd6ffae92013-09-27 23:57:26 +0000723}
724#endif
725/*----------------------------------------------------------------------------*\
726|* Interlocked Exchange
727\*----------------------------------------------------------------------------*/
Michael Kupersteine45af542015-06-30 13:36:19 +0000728static __inline__ char __DEFAULT_FN_ATTRS
Warren Huntd6ffae92013-09-27 23:57:26 +0000729_InterlockedExchange8(char volatile *_Target, char _Value) {
David Majnemer3c8ea5f2015-07-13 02:53:19 +0000730 __atomic_exchange(_Target, &_Value, &_Value, 5);
Warren Huntd6ffae92013-09-27 23:57:26 +0000731 return _Value;
732}
Michael Kupersteine45af542015-06-30 13:36:19 +0000733static __inline__ short __DEFAULT_FN_ATTRS
Warren Huntd6ffae92013-09-27 23:57:26 +0000734_InterlockedExchange16(short volatile *_Target, short _Value) {
David Majnemer3c8ea5f2015-07-13 02:53:19 +0000735 __atomic_exchange(_Target, &_Value, &_Value, 5);
Warren Huntd6ffae92013-09-27 23:57:26 +0000736 return _Value;
737}
Warren Hunt3f987942013-09-30 21:08:05 +0000738#ifdef __x86_64__
Michael Kupersteine45af542015-06-30 13:36:19 +0000739static __inline__ __int64 __DEFAULT_FN_ATTRS
Warren Huntd6ffae92013-09-27 23:57:26 +0000740_InterlockedExchange64(__int64 volatile *_Target, __int64 _Value) {
David Majnemer3c8ea5f2015-07-13 02:53:19 +0000741 __atomic_exchange(_Target, &_Value, &_Value, 5);
Warren Huntd6ffae92013-09-27 23:57:26 +0000742 return _Value;
743}
744#endif
745/*----------------------------------------------------------------------------*\
746|* Interlocked Compare Exchange
747\*----------------------------------------------------------------------------*/
Michael Kupersteine45af542015-06-30 13:36:19 +0000748static __inline__ char __DEFAULT_FN_ATTRS
Warren Huntd6ffae92013-09-27 23:57:26 +0000749_InterlockedCompareExchange8(char volatile *_Destination,
750 char _Exchange, char _Comparand) {
David Majnemer3c8ea5f2015-07-13 02:53:19 +0000751 __atomic_compare_exchange(_Destination, &_Comparand, &_Exchange, 0, 5, 5);
Warren Huntd6ffae92013-09-27 23:57:26 +0000752 return _Comparand;
753}
Michael Kupersteine45af542015-06-30 13:36:19 +0000754static __inline__ short __DEFAULT_FN_ATTRS
Warren Huntd6ffae92013-09-27 23:57:26 +0000755_InterlockedCompareExchange16(short volatile *_Destination,
756 short _Exchange, short _Comparand) {
David Majnemer3c8ea5f2015-07-13 02:53:19 +0000757 __atomic_compare_exchange(_Destination, &_Comparand, &_Exchange, 0, 5, 5);
Warren Huntd6ffae92013-09-27 23:57:26 +0000758 return _Comparand;
759}
Michael Kupersteine45af542015-06-30 13:36:19 +0000760static __inline__ __int64 __DEFAULT_FN_ATTRS
Warren Huntd6ffae92013-09-27 23:57:26 +0000761_InterlockedCompareExchange64(__int64 volatile *_Destination,
762 __int64 _Exchange, __int64 _Comparand) {
David Majnemer3c8ea5f2015-07-13 02:53:19 +0000763 __atomic_compare_exchange(_Destination, &_Comparand, &_Exchange, 0, 5, 5);
Warren Huntd6ffae92013-09-27 23:57:26 +0000764 return _Comparand;
765}
Warren Huntd6ffae92013-09-27 23:57:26 +0000766/*----------------------------------------------------------------------------*\
Reid Kleckner00d33a52013-10-17 01:29:48 +0000767|* Barriers
768\*----------------------------------------------------------------------------*/
Saleem Abdulrasool702eefe2014-06-25 16:48:40 +0000769#if defined(__i386__) || defined(__x86_64__)
Michael Kupersteine45af542015-06-30 13:36:19 +0000770static __inline__ void __DEFAULT_FN_ATTRS
David Majnemer1cf22e62015-02-04 00:26:10 +0000771__attribute__((__deprecated__("use other intrinsics or C++11 atomics instead")))
Reid Kleckner00d33a52013-10-17 01:29:48 +0000772_ReadWriteBarrier(void) {
773 __asm__ volatile ("" : : : "memory");
774}
Michael Kupersteine45af542015-06-30 13:36:19 +0000775static __inline__ void __DEFAULT_FN_ATTRS
David Majnemer1cf22e62015-02-04 00:26:10 +0000776__attribute__((__deprecated__("use other intrinsics or C++11 atomics instead")))
Reid Kleckner00d33a52013-10-17 01:29:48 +0000777_ReadBarrier(void) {
778 __asm__ volatile ("" : : : "memory");
779}
Michael Kupersteine45af542015-06-30 13:36:19 +0000780static __inline__ void __DEFAULT_FN_ATTRS
David Majnemer1cf22e62015-02-04 00:26:10 +0000781__attribute__((__deprecated__("use other intrinsics or C++11 atomics instead")))
Reid Kleckner00d33a52013-10-17 01:29:48 +0000782_WriteBarrier(void) {
783 __asm__ volatile ("" : : : "memory");
784}
Saleem Abdulrasool702eefe2014-06-25 16:48:40 +0000785#endif
Reid Klecknerf08d6582014-01-27 19:16:35 +0000786#ifdef __x86_64__
Michael Kupersteine45af542015-06-30 13:36:19 +0000787static __inline__ void __DEFAULT_FN_ATTRS
Reid Klecknerf08d6582014-01-27 19:16:35 +0000788__faststorefence(void) {
789 __asm__ volatile("lock orq $0, (%%rsp)" : : : "memory");
790}
791#endif
Reid Kleckner00d33a52013-10-17 01:29:48 +0000792/*----------------------------------------------------------------------------*\
Hans Wennborga4421e02014-03-12 21:09:05 +0000793|* readfs, readgs
794|* (Pointers in address space #256 and #257 are relative to the GS and FS
795|* segment registers, respectively.)
Hans Wennborg74ca0c42014-01-24 00:52:39 +0000796\*----------------------------------------------------------------------------*/
Hans Wennborg74ca0c42014-01-24 00:52:39 +0000797#define __ptr_to_addr_space(__addr_space_nbr, __type, __offset) \
798 ((volatile __type __attribute__((__address_space__(__addr_space_nbr)))*) \
799 (__offset))
800
Hans Wennborga4421e02014-03-12 21:09:05 +0000801#ifdef __i386__
Michael Kupersteine45af542015-06-30 13:36:19 +0000802static __inline__ unsigned char __DEFAULT_FN_ATTRS
Hans Wennborg74ca0c42014-01-24 00:52:39 +0000803__readfsbyte(unsigned long __offset) {
804 return *__ptr_to_addr_space(257, unsigned char, __offset);
805}
Michael Kupersteine45af542015-06-30 13:36:19 +0000806static __inline__ unsigned __int64 __DEFAULT_FN_ATTRS
Hans Wennborg74ca0c42014-01-24 00:52:39 +0000807__readfsqword(unsigned long __offset) {
808 return *__ptr_to_addr_space(257, unsigned __int64, __offset);
809}
Michael Kupersteine45af542015-06-30 13:36:19 +0000810static __inline__ unsigned short __DEFAULT_FN_ATTRS
Hans Wennborg74ca0c42014-01-24 00:52:39 +0000811__readfsword(unsigned long __offset) {
812 return *__ptr_to_addr_space(257, unsigned short, __offset);
813}
Hans Wennborg74ca0c42014-01-24 00:52:39 +0000814#endif
Hans Wennborga4421e02014-03-12 21:09:05 +0000815#ifdef __x86_64__
Michael Kupersteine45af542015-06-30 13:36:19 +0000816static __inline__ unsigned char __DEFAULT_FN_ATTRS
Hans Wennborga4421e02014-03-12 21:09:05 +0000817__readgsbyte(unsigned long __offset) {
818 return *__ptr_to_addr_space(256, unsigned char, __offset);
819}
Michael Kupersteine45af542015-06-30 13:36:19 +0000820static __inline__ unsigned long __DEFAULT_FN_ATTRS
Hans Wennborga4421e02014-03-12 21:09:05 +0000821__readgsdword(unsigned long __offset) {
822 return *__ptr_to_addr_space(256, unsigned long, __offset);
823}
Michael Kupersteine45af542015-06-30 13:36:19 +0000824static __inline__ unsigned __int64 __DEFAULT_FN_ATTRS
Hans Wennborga4421e02014-03-12 21:09:05 +0000825__readgsqword(unsigned long __offset) {
826 return *__ptr_to_addr_space(256, unsigned __int64, __offset);
827}
Michael Kupersteine45af542015-06-30 13:36:19 +0000828static __inline__ unsigned short __DEFAULT_FN_ATTRS
Hans Wennborga4421e02014-03-12 21:09:05 +0000829__readgsword(unsigned long __offset) {
830 return *__ptr_to_addr_space(256, unsigned short, __offset);
831}
832#endif
833#undef __ptr_to_addr_space
Hans Wennborg74ca0c42014-01-24 00:52:39 +0000834/*----------------------------------------------------------------------------*\
Hans Wennborgd9be72e2014-03-12 22:00:32 +0000835|* movs, stos
836\*----------------------------------------------------------------------------*/
Saleem Abdulrasool702eefe2014-06-25 16:48:40 +0000837#if defined(__i386__) || defined(__x86_64__)
Michael Kupersteine45af542015-06-30 13:36:19 +0000838static __inline__ void __DEFAULT_FN_ATTRS
Hans Wennborgd9be72e2014-03-12 22:00:32 +0000839__movsb(unsigned char *__dst, unsigned char const *__src, size_t __n) {
840 __asm__("rep movsb" : : "D"(__dst), "S"(__src), "c"(__n)
841 : "%edi", "%esi", "%ecx");
842}
Michael Kupersteine45af542015-06-30 13:36:19 +0000843static __inline__ void __DEFAULT_FN_ATTRS
Hans Wennborgd9be72e2014-03-12 22:00:32 +0000844__movsd(unsigned long *__dst, unsigned long const *__src, size_t __n) {
845 __asm__("rep movsl" : : "D"(__dst), "S"(__src), "c"(__n)
846 : "%edi", "%esi", "%ecx");
847}
Michael Kupersteine45af542015-06-30 13:36:19 +0000848static __inline__ void __DEFAULT_FN_ATTRS
Hans Wennborgd9be72e2014-03-12 22:00:32 +0000849__movsw(unsigned short *__dst, unsigned short const *__src, size_t __n) {
850 __asm__("rep movsh" : : "D"(__dst), "S"(__src), "c"(__n)
851 : "%edi", "%esi", "%ecx");
852}
Michael Kupersteine45af542015-06-30 13:36:19 +0000853static __inline__ void __DEFAULT_FN_ATTRS
Hans Wennborgd9be72e2014-03-12 22:00:32 +0000854__stosb(unsigned char *__dst, unsigned char __x, size_t __n) {
855 __asm__("rep stosb" : : "D"(__dst), "a"(__x), "c"(__n)
856 : "%edi", "%ecx");
857}
Michael Kupersteine45af542015-06-30 13:36:19 +0000858static __inline__ void __DEFAULT_FN_ATTRS
Hans Wennborgd9be72e2014-03-12 22:00:32 +0000859__stosd(unsigned long *__dst, unsigned long __x, size_t __n) {
860 __asm__("rep stosl" : : "D"(__dst), "a"(__x), "c"(__n)
861 : "%edi", "%ecx");
862}
Michael Kupersteine45af542015-06-30 13:36:19 +0000863static __inline__ void __DEFAULT_FN_ATTRS
Hans Wennborgd9be72e2014-03-12 22:00:32 +0000864__stosw(unsigned short *__dst, unsigned short __x, size_t __n) {
865 __asm__("rep stosh" : : "D"(__dst), "a"(__x), "c"(__n)
866 : "%edi", "%ecx");
867}
Saleem Abdulrasool702eefe2014-06-25 16:48:40 +0000868#endif
Hans Wennborgd9be72e2014-03-12 22:00:32 +0000869#ifdef __x86_64__
Michael Kupersteine45af542015-06-30 13:36:19 +0000870static __inline__ void __DEFAULT_FN_ATTRS
Hans Wennborgd9be72e2014-03-12 22:00:32 +0000871__movsq(unsigned long long *__dst, unsigned long long const *__src, size_t __n) {
872 __asm__("rep movsq" : : "D"(__dst), "S"(__src), "c"(__n)
873 : "%edi", "%esi", "%ecx");
874}
Michael Kupersteine45af542015-06-30 13:36:19 +0000875static __inline__ void __DEFAULT_FN_ATTRS
Hans Wennborgd9be72e2014-03-12 22:00:32 +0000876__stosq(unsigned __int64 *__dst, unsigned __int64 __x, size_t __n) {
877 __asm__("rep stosq" : : "D"(__dst), "a"(__x), "c"(__n)
878 : "%edi", "%ecx");
879}
880#endif
881
882/*----------------------------------------------------------------------------*\
Warren Huntd6ffae92013-09-27 23:57:26 +0000883|* Misc
884\*----------------------------------------------------------------------------*/
Michael Kupersteine45af542015-06-30 13:36:19 +0000885static __inline__ void * __DEFAULT_FN_ATTRS
Warren Huntd6ffae92013-09-27 23:57:26 +0000886_AddressOfReturnAddress(void) {
887 return (void*)((char*)__builtin_frame_address(0) + sizeof(void*));
888}
Michael Kupersteine45af542015-06-30 13:36:19 +0000889static __inline__ void * __DEFAULT_FN_ATTRS
Warren Huntd6ffae92013-09-27 23:57:26 +0000890_ReturnAddress(void) {
891 return __builtin_return_address(0);
892}
Saleem Abdulrasool702eefe2014-06-25 16:48:40 +0000893#if defined(__i386__) || defined(__x86_64__)
Michael Kupersteine45af542015-06-30 13:36:19 +0000894static __inline__ void __DEFAULT_FN_ATTRS
Hans Wennborg854f7d32014-01-16 23:39:35 +0000895__cpuid(int __info[4], int __level) {
Hans Wennborg854f7d32014-01-16 23:39:35 +0000896 __asm__ ("cpuid" : "=a"(__info[0]), "=b" (__info[1]), "=c"(__info[2]), "=d"(__info[3])
Hans Wennborg12fb89e2014-01-31 19:44:55 +0000897 : "a"(__level));
898}
Michael Kupersteine45af542015-06-30 13:36:19 +0000899static __inline__ void __DEFAULT_FN_ATTRS
Hans Wennborg12fb89e2014-01-31 19:44:55 +0000900__cpuidex(int __info[4], int __level, int __ecx) {
901 __asm__ ("cpuid" : "=a"(__info[0]), "=b" (__info[1]), "=c"(__info[2]), "=d"(__info[3])
902 : "a"(__level), "c"(__ecx));
Hans Wennborg854f7d32014-01-16 23:39:35 +0000903}
Michael Kupersteine45af542015-06-30 13:36:19 +0000904static __inline__ unsigned __int64 __cdecl __DEFAULT_FN_ATTRS
Hans Wennborg854f7d32014-01-16 23:39:35 +0000905_xgetbv(unsigned int __xcr_no) {
906 unsigned int __eax, __edx;
907 __asm__ ("xgetbv" : "=a" (__eax), "=d" (__edx) : "c" (__xcr_no));
908 return ((unsigned __int64)__edx << 32) | __eax;
909}
Michael Kupersteine45af542015-06-30 13:36:19 +0000910static __inline__ void __DEFAULT_FN_ATTRS
Hans Wennborg740a4d62014-01-28 22:55:01 +0000911__halt(void) {
912 __asm__ volatile ("hlt");
913}
Saleem Abdulrasool702eefe2014-06-25 16:48:40 +0000914#endif
Warren Huntd6ffae92013-09-27 23:57:26 +0000915
Reid Kleckner592dc612014-04-08 00:28:22 +0000916/*----------------------------------------------------------------------------*\
917|* Privileged intrinsics
918\*----------------------------------------------------------------------------*/
Saleem Abdulrasool702eefe2014-06-25 16:48:40 +0000919#if defined(__i386__) || defined(__x86_64__)
Michael Kupersteine45af542015-06-30 13:36:19 +0000920static __inline__ unsigned __int64 __DEFAULT_FN_ATTRS
Reid Kleckner592dc612014-04-08 00:28:22 +0000921__readmsr(unsigned long __register) {
922 // Loads the contents of a 64-bit model specific register (MSR) specified in
923 // the ECX register into registers EDX:EAX. The EDX register is loaded with
924 // the high-order 32 bits of the MSR and the EAX register is loaded with the
925 // low-order 32 bits. If less than 64 bits are implemented in the MSR being
926 // read, the values returned to EDX:EAX in unimplemented bit locations are
927 // undefined.
928 unsigned long __edx;
929 unsigned long __eax;
Reid Kleckner6df52542014-04-08 17:49:16 +0000930 __asm__ ("rdmsr" : "=d"(__edx), "=a"(__eax) : "c"(__register));
Reid Kleckner592dc612014-04-08 00:28:22 +0000931 return (((unsigned __int64)__edx) << 32) | (unsigned __int64)__eax;
932}
933
Michael Kupersteine45af542015-06-30 13:36:19 +0000934static __inline__ unsigned long __DEFAULT_FN_ATTRS
Reid Kleckner592dc612014-04-08 00:28:22 +0000935__readcr3(void) {
Reid Kleckner6df52542014-04-08 17:49:16 +0000936 unsigned long __cr3_val;
937 __asm__ __volatile__ ("mov %%cr3, %0" : "=q"(__cr3_val) : : "memory");
938 return __cr3_val;
Reid Kleckner592dc612014-04-08 00:28:22 +0000939}
940
Michael Kupersteine45af542015-06-30 13:36:19 +0000941static __inline__ void __DEFAULT_FN_ATTRS
Reid Kleckner6df52542014-04-08 17:49:16 +0000942__writecr3(unsigned int __cr3_val) {
943 __asm__ ("mov %0, %%cr3" : : "q"(__cr3_val) : "memory");
Reid Kleckner592dc612014-04-08 00:28:22 +0000944}
Saleem Abdulrasool702eefe2014-06-25 16:48:40 +0000945#endif
Reid Kleckner592dc612014-04-08 00:28:22 +0000946
Eric Christophercc872532013-09-18 22:40:18 +0000947#ifdef __cplusplus
Eric Christopher0db88a72013-09-18 22:24:01 +0000948}
Eric Christopherd1428bf2013-08-31 00:22:48 +0000949#endif
Reid Klecknerf0e23222013-09-19 00:19:53 +0000950
Michael Kupersteine45af542015-06-30 13:36:19 +0000951#undef __DEFAULT_FN_ATTRS
Eric Christopher5a9bec12015-06-15 23:20:35 +0000952
Reid Klecknerf0e23222013-09-19 00:19:53 +0000953#endif /* __INTRIN_H */
954#endif /* _MSC_VER */