Hans Wennborg | f8b91f8 | 2016-06-14 20:14:24 +0000 | [diff] [blame^] | 1 | /* ===-------- intrin.h ---------------------------------------------------=== |
Eric Christopher | d1428bf | 2013-08-31 00:22:48 +0000 | [diff] [blame] | 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 |
Hans Wennborg | f8b91f8 | 2016-06-14 20:14:24 +0000 | [diff] [blame^] | 26 | #include_next <intrin.h> |
Eric Christopher | d1428bf | 2013-08-31 00:22:48 +0000 | [diff] [blame] | 27 | #else |
| 28 | |
| 29 | #ifndef __INTRIN_H |
| 30 | #define __INTRIN_H |
| 31 | |
| 32 | /* First include the standard intrinsics. */ |
Saleem Abdulrasool | 702eefe | 2014-06-25 16:48:40 +0000 | [diff] [blame] | 33 | #if defined(__i386__) || defined(__x86_64__) |
Eric Christopher | d1428bf | 2013-08-31 00:22:48 +0000 | [diff] [blame] | 34 | #include <x86intrin.h> |
Saleem Abdulrasool | 702eefe | 2014-06-25 16:48:40 +0000 | [diff] [blame] | 35 | #endif |
Eric Christopher | d1428bf | 2013-08-31 00:22:48 +0000 | [diff] [blame] | 36 | |
Hans Wennborg | 1fd6dd3 | 2014-01-28 23:01:59 +0000 | [diff] [blame] | 37 | /* For the definition of jmp_buf. */ |
Nico Weber | a62cffa | 2014-07-08 18:34:46 +0000 | [diff] [blame] | 38 | #if __STDC_HOSTED__ |
Hans Wennborg | 1fd6dd3 | 2014-01-28 23:01:59 +0000 | [diff] [blame] | 39 | #include <setjmp.h> |
Nico Weber | a62cffa | 2014-07-08 18:34:46 +0000 | [diff] [blame] | 40 | #endif |
Hans Wennborg | 1fd6dd3 | 2014-01-28 23:01:59 +0000 | [diff] [blame] | 41 | |
Eric Christopher | 5a9bec1 | 2015-06-15 23:20:35 +0000 | [diff] [blame] | 42 | /* Define the default attributes for the functions in this file. */ |
Michael Kuperstein | e45af54 | 2015-06-30 13:36:19 +0000 | [diff] [blame] | 43 | #define __DEFAULT_FN_ATTRS __attribute__((__always_inline__, __nodebug__)) |
Eric Christopher | 5a9bec1 | 2015-06-15 23:20:35 +0000 | [diff] [blame] | 44 | |
Eric Christopher | 0db88a7 | 2013-09-18 22:24:01 +0000 | [diff] [blame] | 45 | #ifdef __cplusplus |
| 46 | extern "C" { |
| 47 | #endif |
| 48 | |
Saleem Abdulrasool | c4ebb12 | 2014-07-08 05:46:04 +0000 | [diff] [blame] | 49 | #if defined(__MMX__) |
Eric Christopher | d1428bf | 2013-08-31 00:22:48 +0000 | [diff] [blame] | 50 | /* And the random ones that aren't in those files. */ |
| 51 | __m64 _m_from_float(float); |
Eric Christopher | d1428bf | 2013-08-31 00:22:48 +0000 | [diff] [blame] | 52 | float _m_to_float(__m64); |
Saleem Abdulrasool | c4ebb12 | 2014-07-08 05:46:04 +0000 | [diff] [blame] | 53 | #endif |
Eric Christopher | d1428bf | 2013-08-31 00:22:48 +0000 | [diff] [blame] | 54 | |
| 55 | /* Other assorted instruction intrinsics. */ |
| 56 | void __addfsbyte(unsigned long, unsigned char); |
| 57 | void __addfsdword(unsigned long, unsigned long); |
| 58 | void __addfsword(unsigned long, unsigned short); |
| 59 | void __code_seg(const char *); |
Hans Wennborg | 854f7d3 | 2014-01-16 23:39:35 +0000 | [diff] [blame] | 60 | static __inline__ |
Eric Christopher | d1428bf | 2013-08-31 00:22:48 +0000 | [diff] [blame] | 61 | void __cpuid(int[4], int); |
Hans Wennborg | 12fb89e | 2014-01-31 19:44:55 +0000 | [diff] [blame] | 62 | static __inline__ |
Eric Christopher | d1428bf | 2013-08-31 00:22:48 +0000 | [diff] [blame] | 63 | void __cpuidex(int[4], int, int); |
Eric Christopher | 0db88a7 | 2013-09-18 22:24:01 +0000 | [diff] [blame] | 64 | void __debugbreak(void); |
Eric Christopher | d1428bf | 2013-08-31 00:22:48 +0000 | [diff] [blame] | 65 | __int64 __emul(int, int); |
| 66 | unsigned __int64 __emulu(unsigned int, unsigned int); |
Eric Christopher | 0db88a7 | 2013-09-18 22:24:01 +0000 | [diff] [blame] | 67 | void __cdecl __fastfail(unsigned int); |
Eric Christopher | d1428bf | 2013-08-31 00:22:48 +0000 | [diff] [blame] | 68 | unsigned int __getcallerseflags(void); |
Hans Wennborg | 740a4d6 | 2014-01-28 22:55:01 +0000 | [diff] [blame] | 69 | static __inline__ |
Eric Christopher | d1428bf | 2013-08-31 00:22:48 +0000 | [diff] [blame] | 70 | void __halt(void); |
| 71 | unsigned char __inbyte(unsigned short); |
| 72 | void __inbytestring(unsigned short, unsigned char *, unsigned long); |
| 73 | void __incfsbyte(unsigned long); |
| 74 | void __incfsdword(unsigned long); |
| 75 | void __incfsword(unsigned long); |
| 76 | unsigned long __indword(unsigned short); |
| 77 | void __indwordstring(unsigned short, unsigned long *, unsigned long); |
| 78 | void __int2c(void); |
| 79 | void __invlpg(void *); |
| 80 | unsigned short __inword(unsigned short); |
| 81 | void __inwordstring(unsigned short, unsigned short *, unsigned long); |
| 82 | void __lidt(void *); |
| 83 | unsigned __int64 __ll_lshift(unsigned __int64, int); |
| 84 | __int64 __ll_rshift(__int64, int); |
| 85 | void __llwpcb(void *); |
| 86 | unsigned char __lwpins32(unsigned int, unsigned int, unsigned int); |
| 87 | void __lwpval32(unsigned int, unsigned int, unsigned int); |
| 88 | unsigned int __lzcnt(unsigned int); |
| 89 | unsigned short __lzcnt16(unsigned short); |
Hans Wennborg | d9be72e | 2014-03-12 22:00:32 +0000 | [diff] [blame] | 90 | static __inline__ |
Eric Christopher | d1428bf | 2013-08-31 00:22:48 +0000 | [diff] [blame] | 91 | void __movsb(unsigned char *, unsigned char const *, size_t); |
Hans Wennborg | d9be72e | 2014-03-12 22:00:32 +0000 | [diff] [blame] | 92 | static __inline__ |
Eric Christopher | d1428bf | 2013-08-31 00:22:48 +0000 | [diff] [blame] | 93 | void __movsd(unsigned long *, unsigned long const *, size_t); |
Hans Wennborg | d9be72e | 2014-03-12 22:00:32 +0000 | [diff] [blame] | 94 | static __inline__ |
Eric Christopher | d1428bf | 2013-08-31 00:22:48 +0000 | [diff] [blame] | 95 | void __movsw(unsigned short *, unsigned short const *, size_t); |
| 96 | void __nop(void); |
| 97 | void __nvreg_restore_fence(void); |
| 98 | void __nvreg_save_fence(void); |
| 99 | void __outbyte(unsigned short, unsigned char); |
| 100 | void __outbytestring(unsigned short, unsigned char *, unsigned long); |
| 101 | void __outdword(unsigned short, unsigned long); |
| 102 | void __outdwordstring(unsigned short, unsigned long *, unsigned long); |
| 103 | void __outword(unsigned short, unsigned short); |
| 104 | void __outwordstring(unsigned short, unsigned short *, unsigned long); |
Warren Hunt | d6ffae9 | 2013-09-27 23:57:26 +0000 | [diff] [blame] | 105 | static __inline__ |
Eric Christopher | d1428bf | 2013-08-31 00:22:48 +0000 | [diff] [blame] | 106 | unsigned int __popcnt(unsigned int); |
Warren Hunt | d6ffae9 | 2013-09-27 23:57:26 +0000 | [diff] [blame] | 107 | static __inline__ |
Eric Christopher | d1428bf | 2013-08-31 00:22:48 +0000 | [diff] [blame] | 108 | unsigned short __popcnt16(unsigned short); |
Eric Christopher | d1428bf | 2013-08-31 00:22:48 +0000 | [diff] [blame] | 109 | unsigned long __readcr0(void); |
| 110 | unsigned long __readcr2(void); |
Reid Kleckner | 592dc61 | 2014-04-08 00:28:22 +0000 | [diff] [blame] | 111 | static __inline__ |
Eric Christopher | d1428bf | 2013-08-31 00:22:48 +0000 | [diff] [blame] | 112 | unsigned long __readcr3(void); |
Eric Christopher | 439137e | 2014-01-24 12:13:47 +0000 | [diff] [blame] | 113 | unsigned long __readcr4(void); |
Eric Christopher | d1428bf | 2013-08-31 00:22:48 +0000 | [diff] [blame] | 114 | unsigned long __readcr8(void); |
| 115 | unsigned int __readdr(unsigned int); |
Hans Wennborg | 74ca0c4 | 2014-01-24 00:52:39 +0000 | [diff] [blame] | 116 | #ifdef __i386__ |
| 117 | static __inline__ |
Eric Christopher | d1428bf | 2013-08-31 00:22:48 +0000 | [diff] [blame] | 118 | unsigned char __readfsbyte(unsigned long); |
Hans Wennborg | 74ca0c4 | 2014-01-24 00:52:39 +0000 | [diff] [blame] | 119 | static __inline__ |
Eric Christopher | d1428bf | 2013-08-31 00:22:48 +0000 | [diff] [blame] | 120 | unsigned long __readfsdword(unsigned long); |
Hans Wennborg | 74ca0c4 | 2014-01-24 00:52:39 +0000 | [diff] [blame] | 121 | static __inline__ |
Eric Christopher | d1428bf | 2013-08-31 00:22:48 +0000 | [diff] [blame] | 122 | unsigned __int64 __readfsqword(unsigned long); |
Hans Wennborg | 74ca0c4 | 2014-01-24 00:52:39 +0000 | [diff] [blame] | 123 | static __inline__ |
Eric Christopher | d1428bf | 2013-08-31 00:22:48 +0000 | [diff] [blame] | 124 | unsigned short __readfsword(unsigned long); |
Hans Wennborg | 74ca0c4 | 2014-01-24 00:52:39 +0000 | [diff] [blame] | 125 | #endif |
Reid Kleckner | 592dc61 | 2014-04-08 00:28:22 +0000 | [diff] [blame] | 126 | static __inline__ |
Eric Christopher | d1428bf | 2013-08-31 00:22:48 +0000 | [diff] [blame] | 127 | unsigned __int64 __readmsr(unsigned long); |
| 128 | unsigned __int64 __readpmc(unsigned long); |
| 129 | unsigned long __segmentlimit(unsigned long); |
| 130 | void __sidt(void *); |
| 131 | void *__slwpcb(void); |
Hans Wennborg | d9be72e | 2014-03-12 22:00:32 +0000 | [diff] [blame] | 132 | static __inline__ |
Eric Christopher | d1428bf | 2013-08-31 00:22:48 +0000 | [diff] [blame] | 133 | void __stosb(unsigned char *, unsigned char, size_t); |
Hans Wennborg | d9be72e | 2014-03-12 22:00:32 +0000 | [diff] [blame] | 134 | static __inline__ |
Eric Christopher | d1428bf | 2013-08-31 00:22:48 +0000 | [diff] [blame] | 135 | void __stosd(unsigned long *, unsigned long, size_t); |
Hans Wennborg | d9be72e | 2014-03-12 22:00:32 +0000 | [diff] [blame] | 136 | static __inline__ |
Eric Christopher | d1428bf | 2013-08-31 00:22:48 +0000 | [diff] [blame] | 137 | void __stosw(unsigned short *, unsigned short, size_t); |
| 138 | void __svm_clgi(void); |
| 139 | void __svm_invlpga(void *, int); |
| 140 | void __svm_skinit(int); |
| 141 | void __svm_stgi(void); |
| 142 | void __svm_vmload(size_t); |
| 143 | void __svm_vmrun(size_t); |
| 144 | void __svm_vmsave(size_t); |
| 145 | void __ud2(void); |
| 146 | unsigned __int64 __ull_rshift(unsigned __int64, int); |
| 147 | void __vmx_off(void); |
| 148 | void __vmx_vmptrst(unsigned __int64 *); |
| 149 | void __wbinvd(void); |
| 150 | void __writecr0(unsigned int); |
Reid Kleckner | 592dc61 | 2014-04-08 00:28:22 +0000 | [diff] [blame] | 151 | static __inline__ |
Eric Christopher | d1428bf | 2013-08-31 00:22:48 +0000 | [diff] [blame] | 152 | void __writecr3(unsigned int); |
| 153 | void __writecr4(unsigned int); |
| 154 | void __writecr8(unsigned int); |
| 155 | void __writedr(unsigned int, unsigned int); |
Eric Christopher | d1428bf | 2013-08-31 00:22:48 +0000 | [diff] [blame] | 156 | void __writefsbyte(unsigned long, unsigned char); |
| 157 | void __writefsdword(unsigned long, unsigned long); |
| 158 | void __writefsqword(unsigned long, unsigned __int64); |
| 159 | void __writefsword(unsigned long, unsigned short); |
| 160 | void __writemsr(unsigned long, unsigned __int64); |
Warren Hunt | d6ffae9 | 2013-09-27 23:57:26 +0000 | [diff] [blame] | 161 | static __inline__ |
Eric Christopher | d1428bf | 2013-08-31 00:22:48 +0000 | [diff] [blame] | 162 | void *_AddressOfReturnAddress(void); |
Warren Hunt | d6ffae9 | 2013-09-27 23:57:26 +0000 | [diff] [blame] | 163 | static __inline__ |
Eric Christopher | d1428bf | 2013-08-31 00:22:48 +0000 | [diff] [blame] | 164 | unsigned char _BitScanForward(unsigned long *_Index, unsigned long _Mask); |
Warren Hunt | d6ffae9 | 2013-09-27 23:57:26 +0000 | [diff] [blame] | 165 | static __inline__ |
Eric Christopher | d1428bf | 2013-08-31 00:22:48 +0000 | [diff] [blame] | 166 | unsigned char _BitScanReverse(unsigned long *_Index, unsigned long _Mask); |
Warren Hunt | d6ffae9 | 2013-09-27 23:57:26 +0000 | [diff] [blame] | 167 | static __inline__ |
Eric Christopher | d1428bf | 2013-08-31 00:22:48 +0000 | [diff] [blame] | 168 | unsigned char _bittest(long const *, long); |
Warren Hunt | d6ffae9 | 2013-09-27 23:57:26 +0000 | [diff] [blame] | 169 | static __inline__ |
Eric Christopher | d1428bf | 2013-08-31 00:22:48 +0000 | [diff] [blame] | 170 | unsigned char _bittestandcomplement(long *, long); |
Warren Hunt | d6ffae9 | 2013-09-27 23:57:26 +0000 | [diff] [blame] | 171 | static __inline__ |
Eric Christopher | d1428bf | 2013-08-31 00:22:48 +0000 | [diff] [blame] | 172 | unsigned char _bittestandreset(long *, long); |
Warren Hunt | d6ffae9 | 2013-09-27 23:57:26 +0000 | [diff] [blame] | 173 | static __inline__ |
Eric Christopher | d1428bf | 2013-08-31 00:22:48 +0000 | [diff] [blame] | 174 | unsigned char _bittestandset(long *, long); |
Eric Christopher | d1428bf | 2013-08-31 00:22:48 +0000 | [diff] [blame] | 175 | unsigned __int64 __cdecl _byteswap_uint64(unsigned __int64); |
| 176 | unsigned long __cdecl _byteswap_ulong(unsigned long); |
| 177 | unsigned short __cdecl _byteswap_ushort(unsigned short); |
Eric Christopher | d1428bf | 2013-08-31 00:22:48 +0000 | [diff] [blame] | 178 | void __cdecl _disable(void); |
| 179 | void __cdecl _enable(void); |
Eric Christopher | d1428bf | 2013-08-31 00:22:48 +0000 | [diff] [blame] | 180 | long _InterlockedAddLargeStatistic(__int64 volatile *_Addend, long _Value); |
Warren Hunt | d6ffae9 | 2013-09-27 23:57:26 +0000 | [diff] [blame] | 181 | static __inline__ |
Eric Christopher | d1428bf | 2013-08-31 00:22:48 +0000 | [diff] [blame] | 182 | long _InterlockedAnd(long volatile *_Value, long _Mask); |
Warren Hunt | d6ffae9 | 2013-09-27 23:57:26 +0000 | [diff] [blame] | 183 | static __inline__ |
Eric Christopher | d1428bf | 2013-08-31 00:22:48 +0000 | [diff] [blame] | 184 | short _InterlockedAnd16(short volatile *_Value, short _Mask); |
Warren Hunt | d6ffae9 | 2013-09-27 23:57:26 +0000 | [diff] [blame] | 185 | static __inline__ |
Eric Christopher | d1428bf | 2013-08-31 00:22:48 +0000 | [diff] [blame] | 186 | char _InterlockedAnd8(char volatile *_Value, char _Mask); |
| 187 | unsigned char _interlockedbittestandreset(long volatile *, long); |
Hans Wennborg | a316933 | 2014-03-13 17:05:09 +0000 | [diff] [blame] | 188 | static __inline__ |
Eric Christopher | d1428bf | 2013-08-31 00:22:48 +0000 | [diff] [blame] | 189 | unsigned char _interlockedbittestandset(long volatile *, long); |
Warren Hunt | d6ffae9 | 2013-09-27 23:57:26 +0000 | [diff] [blame] | 190 | static __inline__ |
Eric Christopher | d1428bf | 2013-08-31 00:22:48 +0000 | [diff] [blame] | 191 | long __cdecl _InterlockedCompareExchange(long volatile *_Destination, |
| 192 | long _Exchange, long _Comparand); |
| 193 | long _InterlockedCompareExchange_HLEAcquire(long volatile *, long, long); |
| 194 | long _InterlockedCompareExchange_HLERelease(long volatile *, long, long); |
Warren Hunt | d6ffae9 | 2013-09-27 23:57:26 +0000 | [diff] [blame] | 195 | static __inline__ |
Eric Christopher | d1428bf | 2013-08-31 00:22:48 +0000 | [diff] [blame] | 196 | short _InterlockedCompareExchange16(short volatile *_Destination, |
| 197 | short _Exchange, short _Comparand); |
Warren Hunt | d6ffae9 | 2013-09-27 23:57:26 +0000 | [diff] [blame] | 198 | static __inline__ |
Eric Christopher | d1428bf | 2013-08-31 00:22:48 +0000 | [diff] [blame] | 199 | __int64 _InterlockedCompareExchange64(__int64 volatile *_Destination, |
| 200 | __int64 _Exchange, __int64 _Comparand); |
| 201 | __int64 _InterlockedcompareExchange64_HLEAcquire(__int64 volatile *, __int64, |
| 202 | __int64); |
| 203 | __int64 _InterlockedCompareExchange64_HLERelease(__int64 volatile *, __int64, |
| 204 | __int64); |
Warren Hunt | d6ffae9 | 2013-09-27 23:57:26 +0000 | [diff] [blame] | 205 | static __inline__ |
Eric Christopher | d1428bf | 2013-08-31 00:22:48 +0000 | [diff] [blame] | 206 | char _InterlockedCompareExchange8(char volatile *_Destination, char _Exchange, |
| 207 | char _Comparand); |
| 208 | void *_InterlockedCompareExchangePointer_HLEAcquire(void *volatile *, void *, |
| 209 | void *); |
| 210 | void *_InterlockedCompareExchangePointer_HLERelease(void *volatile *, void *, |
| 211 | void *); |
Warren Hunt | d6ffae9 | 2013-09-27 23:57:26 +0000 | [diff] [blame] | 212 | static __inline__ |
Eric Christopher | d1428bf | 2013-08-31 00:22:48 +0000 | [diff] [blame] | 213 | long __cdecl _InterlockedDecrement(long volatile *_Addend); |
Warren Hunt | d6ffae9 | 2013-09-27 23:57:26 +0000 | [diff] [blame] | 214 | static __inline__ |
Eric Christopher | d1428bf | 2013-08-31 00:22:48 +0000 | [diff] [blame] | 215 | short _InterlockedDecrement16(short volatile *_Addend); |
Saleem Abdulrasool | 114efe0 | 2014-06-18 20:51:10 +0000 | [diff] [blame] | 216 | long _InterlockedExchange(long volatile *_Target, long _Value); |
Warren Hunt | d6ffae9 | 2013-09-27 23:57:26 +0000 | [diff] [blame] | 217 | static __inline__ |
Eric Christopher | d1428bf | 2013-08-31 00:22:48 +0000 | [diff] [blame] | 218 | short _InterlockedExchange16(short volatile *_Target, short _Value); |
Warren Hunt | d6ffae9 | 2013-09-27 23:57:26 +0000 | [diff] [blame] | 219 | static __inline__ |
Eric Christopher | d1428bf | 2013-08-31 00:22:48 +0000 | [diff] [blame] | 220 | char _InterlockedExchange8(char volatile *_Target, char _Value); |
Warren Hunt | d6ffae9 | 2013-09-27 23:57:26 +0000 | [diff] [blame] | 221 | static __inline__ |
Eric Christopher | d1428bf | 2013-08-31 00:22:48 +0000 | [diff] [blame] | 222 | long __cdecl _InterlockedExchangeAdd(long volatile *_Addend, long _Value); |
| 223 | long _InterlockedExchangeAdd_HLEAcquire(long volatile *, long); |
| 224 | long _InterlockedExchangeAdd_HLERelease(long volatile *, long); |
Reid Kleckner | 924eb2a | 2014-01-27 18:48:02 +0000 | [diff] [blame] | 225 | static __inline__ |
Eric Christopher | 439137e | 2014-01-24 12:13:47 +0000 | [diff] [blame] | 226 | short _InterlockedExchangeAdd16(short volatile *_Addend, short _Value); |
| 227 | __int64 _InterlockedExchangeAdd64_HLEAcquire(__int64 volatile *, __int64); |
| 228 | __int64 _InterlockedExchangeAdd64_HLERelease(__int64 volatile *, __int64); |
Warren Hunt | d6ffae9 | 2013-09-27 23:57:26 +0000 | [diff] [blame] | 229 | static __inline__ |
Eric Christopher | d1428bf | 2013-08-31 00:22:48 +0000 | [diff] [blame] | 230 | char _InterlockedExchangeAdd8(char volatile *_Addend, char _Value); |
Warren Hunt | d6ffae9 | 2013-09-27 23:57:26 +0000 | [diff] [blame] | 231 | static __inline__ |
Eric Christopher | d1428bf | 2013-08-31 00:22:48 +0000 | [diff] [blame] | 232 | long __cdecl _InterlockedIncrement(long volatile *_Addend); |
Warren Hunt | d6ffae9 | 2013-09-27 23:57:26 +0000 | [diff] [blame] | 233 | static __inline__ |
Eric Christopher | d1428bf | 2013-08-31 00:22:48 +0000 | [diff] [blame] | 234 | short _InterlockedIncrement16(short volatile *_Addend); |
Warren Hunt | d6ffae9 | 2013-09-27 23:57:26 +0000 | [diff] [blame] | 235 | static __inline__ |
Eric Christopher | d1428bf | 2013-08-31 00:22:48 +0000 | [diff] [blame] | 236 | long _InterlockedOr(long volatile *_Value, long _Mask); |
Warren Hunt | d6ffae9 | 2013-09-27 23:57:26 +0000 | [diff] [blame] | 237 | static __inline__ |
Eric Christopher | d1428bf | 2013-08-31 00:22:48 +0000 | [diff] [blame] | 238 | short _InterlockedOr16(short volatile *_Value, short _Mask); |
Warren Hunt | d6ffae9 | 2013-09-27 23:57:26 +0000 | [diff] [blame] | 239 | static __inline__ |
Eric Christopher | d1428bf | 2013-08-31 00:22:48 +0000 | [diff] [blame] | 240 | char _InterlockedOr8(char volatile *_Value, char _Mask); |
Warren Hunt | d6ffae9 | 2013-09-27 23:57:26 +0000 | [diff] [blame] | 241 | static __inline__ |
Eric Christopher | d1428bf | 2013-08-31 00:22:48 +0000 | [diff] [blame] | 242 | long _InterlockedXor(long volatile *_Value, long _Mask); |
Warren Hunt | d6ffae9 | 2013-09-27 23:57:26 +0000 | [diff] [blame] | 243 | static __inline__ |
Eric Christopher | d1428bf | 2013-08-31 00:22:48 +0000 | [diff] [blame] | 244 | short _InterlockedXor16(short volatile *_Value, short _Mask); |
Warren Hunt | d6ffae9 | 2013-09-27 23:57:26 +0000 | [diff] [blame] | 245 | static __inline__ |
Eric Christopher | d1428bf | 2013-08-31 00:22:48 +0000 | [diff] [blame] | 246 | char _InterlockedXor8(char volatile *_Value, char _Mask); |
| 247 | void __cdecl _invpcid(unsigned int, void *); |
Warren Hunt | d6ffae9 | 2013-09-27 23:57:26 +0000 | [diff] [blame] | 248 | static __inline__ |
Eric Christopher | d1428bf | 2013-08-31 00:22:48 +0000 | [diff] [blame] | 249 | unsigned long __cdecl _lrotl(unsigned long, int); |
Warren Hunt | d6ffae9 | 2013-09-27 23:57:26 +0000 | [diff] [blame] | 250 | static __inline__ |
Eric Christopher | d1428bf | 2013-08-31 00:22:48 +0000 | [diff] [blame] | 251 | unsigned long __cdecl _lrotr(unsigned long, int); |
Warren Hunt | d6ffae9 | 2013-09-27 23:57:26 +0000 | [diff] [blame] | 252 | static __inline__ |
Eric Christopher | d1428bf | 2013-08-31 00:22:48 +0000 | [diff] [blame] | 253 | void _ReadBarrier(void); |
Reid Kleckner | 00d33a5 | 2013-10-17 01:29:48 +0000 | [diff] [blame] | 254 | static __inline__ |
Eric Christopher | d1428bf | 2013-08-31 00:22:48 +0000 | [diff] [blame] | 255 | void _ReadWriteBarrier(void); |
Warren Hunt | d6ffae9 | 2013-09-27 23:57:26 +0000 | [diff] [blame] | 256 | static __inline__ |
Eric Christopher | d1428bf | 2013-08-31 00:22:48 +0000 | [diff] [blame] | 257 | void *_ReturnAddress(void); |
| 258 | unsigned int _rorx_u32(unsigned int, const unsigned int); |
Warren Hunt | d6ffae9 | 2013-09-27 23:57:26 +0000 | [diff] [blame] | 259 | static __inline__ |
Eric Christopher | fb4b433 | 2013-08-31 00:27:38 +0000 | [diff] [blame] | 260 | unsigned int __cdecl _rotl(unsigned int _Value, int _Shift); |
Warren Hunt | d6ffae9 | 2013-09-27 23:57:26 +0000 | [diff] [blame] | 261 | static __inline__ |
Eric Christopher | d1428bf | 2013-08-31 00:22:48 +0000 | [diff] [blame] | 262 | unsigned short _rotl16(unsigned short _Value, unsigned char _Shift); |
Warren Hunt | d6ffae9 | 2013-09-27 23:57:26 +0000 | [diff] [blame] | 263 | static __inline__ |
Eric Christopher | d1428bf | 2013-08-31 00:22:48 +0000 | [diff] [blame] | 264 | unsigned __int64 __cdecl _rotl64(unsigned __int64 _Value, int _Shift); |
Warren Hunt | d6ffae9 | 2013-09-27 23:57:26 +0000 | [diff] [blame] | 265 | static __inline__ |
Eric Christopher | d1428bf | 2013-08-31 00:22:48 +0000 | [diff] [blame] | 266 | unsigned char _rotl8(unsigned char _Value, unsigned char _Shift); |
Warren Hunt | d6ffae9 | 2013-09-27 23:57:26 +0000 | [diff] [blame] | 267 | static __inline__ |
Eric Christopher | d1428bf | 2013-08-31 00:22:48 +0000 | [diff] [blame] | 268 | unsigned int __cdecl _rotr(unsigned int _Value, int _Shift); |
Warren Hunt | d6ffae9 | 2013-09-27 23:57:26 +0000 | [diff] [blame] | 269 | static __inline__ |
Eric Christopher | d1428bf | 2013-08-31 00:22:48 +0000 | [diff] [blame] | 270 | unsigned short _rotr16(unsigned short _Value, unsigned char _Shift); |
Warren Hunt | d6ffae9 | 2013-09-27 23:57:26 +0000 | [diff] [blame] | 271 | static __inline__ |
Eric Christopher | d1428bf | 2013-08-31 00:22:48 +0000 | [diff] [blame] | 272 | unsigned __int64 __cdecl _rotr64(unsigned __int64 _Value, int _Shift); |
Warren Hunt | d6ffae9 | 2013-09-27 23:57:26 +0000 | [diff] [blame] | 273 | static __inline__ |
Eric Christopher | d1428bf | 2013-08-31 00:22:48 +0000 | [diff] [blame] | 274 | unsigned char _rotr8(unsigned char _Value, unsigned char _Shift); |
| 275 | int _sarx_i32(int, unsigned int); |
Nico Weber | a62cffa | 2014-07-08 18:34:46 +0000 | [diff] [blame] | 276 | #if __STDC_HOSTED__ |
Hans Wennborg | 1fd6dd3 | 2014-01-28 23:01:59 +0000 | [diff] [blame] | 277 | int __cdecl _setjmp(jmp_buf); |
Nico Weber | a62cffa | 2014-07-08 18:34:46 +0000 | [diff] [blame] | 278 | #endif |
Eric Christopher | d1428bf | 2013-08-31 00:22:48 +0000 | [diff] [blame] | 279 | unsigned int _shlx_u32(unsigned int, unsigned int); |
| 280 | unsigned int _shrx_u32(unsigned int, unsigned int); |
| 281 | void _Store_HLERelease(long volatile *, long); |
| 282 | void _Store64_HLERelease(__int64 volatile *, __int64); |
| 283 | void _StorePointer_HLERelease(void *volatile *, void *); |
Reid Kleckner | 00d33a5 | 2013-10-17 01:29:48 +0000 | [diff] [blame] | 284 | static __inline__ |
Eric Christopher | d1428bf | 2013-08-31 00:22:48 +0000 | [diff] [blame] | 285 | void _WriteBarrier(void); |
Eric Christopher | d1428bf | 2013-08-31 00:22:48 +0000 | [diff] [blame] | 286 | unsigned __int32 xbegin(void); |
| 287 | void _xend(void); |
Hans Wennborg | 854f7d3 | 2014-01-16 23:39:35 +0000 | [diff] [blame] | 288 | static __inline__ |
Hans Wennborg | 2e56d95 | 2015-01-27 23:34:35 +0000 | [diff] [blame] | 289 | #define _XCR_XFEATURE_ENABLED_MASK 0 |
Eric Christopher | d1428bf | 2013-08-31 00:22:48 +0000 | [diff] [blame] | 290 | unsigned __int64 __cdecl _xgetbv(unsigned int); |
Eric Christopher | d1428bf | 2013-08-31 00:22:48 +0000 | [diff] [blame] | 291 | void __cdecl _xsetbv(unsigned int, unsigned __int64); |
Eric Christopher | d1428bf | 2013-08-31 00:22:48 +0000 | [diff] [blame] | 292 | |
| 293 | /* These additional intrinsics are turned on in x64/amd64/x86_64 mode. */ |
Warren Hunt | 3f98794 | 2013-09-30 21:08:05 +0000 | [diff] [blame] | 294 | #ifdef __x86_64__ |
Eric Christopher | d1428bf | 2013-08-31 00:22:48 +0000 | [diff] [blame] | 295 | void __addgsbyte(unsigned long, unsigned char); |
| 296 | void __addgsdword(unsigned long, unsigned long); |
| 297 | void __addgsqword(unsigned long, unsigned __int64); |
| 298 | void __addgsword(unsigned long, unsigned short); |
Reid Kleckner | f08d658 | 2014-01-27 19:16:35 +0000 | [diff] [blame] | 299 | static __inline__ |
Eric Christopher | d1428bf | 2013-08-31 00:22:48 +0000 | [diff] [blame] | 300 | void __faststorefence(void); |
| 301 | void __incgsbyte(unsigned long); |
| 302 | void __incgsdword(unsigned long); |
| 303 | void __incgsqword(unsigned long); |
| 304 | void __incgsword(unsigned long); |
Eric Christopher | 439137e | 2014-01-24 12:13:47 +0000 | [diff] [blame] | 305 | unsigned char __lwpins64(unsigned __int64, unsigned int, unsigned int); |
| 306 | void __lwpval64(unsigned __int64, unsigned int, unsigned int); |
| 307 | unsigned __int64 __lzcnt64(unsigned __int64); |
Hans Wennborg | d9be72e | 2014-03-12 22:00:32 +0000 | [diff] [blame] | 308 | static __inline__ |
Eric Christopher | 439137e | 2014-01-24 12:13:47 +0000 | [diff] [blame] | 309 | void __movsq(unsigned long long *, unsigned long long const *, size_t); |
| 310 | __int64 __mulh(__int64, __int64); |
Reid Kleckner | 924eb2a | 2014-01-27 18:48:02 +0000 | [diff] [blame] | 311 | static __inline__ |
Eric Christopher | d1428bf | 2013-08-31 00:22:48 +0000 | [diff] [blame] | 312 | unsigned __int64 __popcnt64(unsigned __int64); |
Hans Wennborg | a4421e0 | 2014-03-12 21:09:05 +0000 | [diff] [blame] | 313 | static __inline__ |
Eric Christopher | 439137e | 2014-01-24 12:13:47 +0000 | [diff] [blame] | 314 | unsigned char __readgsbyte(unsigned long); |
Hans Wennborg | a4421e0 | 2014-03-12 21:09:05 +0000 | [diff] [blame] | 315 | static __inline__ |
Eric Christopher | 439137e | 2014-01-24 12:13:47 +0000 | [diff] [blame] | 316 | unsigned long __readgsdword(unsigned long); |
Hans Wennborg | a4421e0 | 2014-03-12 21:09:05 +0000 | [diff] [blame] | 317 | static __inline__ |
| 318 | unsigned __int64 __readgsqword(unsigned long); |
Eric Christopher | 439137e | 2014-01-24 12:13:47 +0000 | [diff] [blame] | 319 | unsigned short __readgsword(unsigned long); |
Eric Christopher | d1428bf | 2013-08-31 00:22:48 +0000 | [diff] [blame] | 320 | unsigned __int64 __shiftleft128(unsigned __int64 _LowPart, |
| 321 | unsigned __int64 _HighPart, |
| 322 | unsigned char _Shift); |
| 323 | unsigned __int64 __shiftright128(unsigned __int64 _LowPart, |
| 324 | unsigned __int64 _HighPart, |
| 325 | unsigned char _Shift); |
Hans Wennborg | d9be72e | 2014-03-12 22:00:32 +0000 | [diff] [blame] | 326 | static __inline__ |
Eric Christopher | d1428bf | 2013-08-31 00:22:48 +0000 | [diff] [blame] | 327 | void __stosq(unsigned __int64 *, unsigned __int64, size_t); |
Eric Christopher | 439137e | 2014-01-24 12:13:47 +0000 | [diff] [blame] | 328 | unsigned char __vmx_on(unsigned __int64 *); |
| 329 | unsigned char __vmx_vmclear(unsigned __int64 *); |
| 330 | unsigned char __vmx_vmlaunch(void); |
| 331 | unsigned char __vmx_vmptrld(unsigned __int64 *); |
| 332 | unsigned char __vmx_vmread(size_t, size_t *); |
| 333 | unsigned char __vmx_vmresume(void); |
| 334 | unsigned char __vmx_vmwrite(size_t, size_t); |
| 335 | void __writegsbyte(unsigned long, unsigned char); |
| 336 | void __writegsdword(unsigned long, unsigned long); |
| 337 | void __writegsqword(unsigned long, unsigned __int64); |
| 338 | void __writegsword(unsigned long, unsigned short); |
Warren Hunt | d6ffae9 | 2013-09-27 23:57:26 +0000 | [diff] [blame] | 339 | static __inline__ |
Eric Christopher | d1428bf | 2013-08-31 00:22:48 +0000 | [diff] [blame] | 340 | unsigned char _BitScanForward64(unsigned long *_Index, unsigned __int64 _Mask); |
Warren Hunt | d6ffae9 | 2013-09-27 23:57:26 +0000 | [diff] [blame] | 341 | static __inline__ |
Eric Christopher | d1428bf | 2013-08-31 00:22:48 +0000 | [diff] [blame] | 342 | unsigned char _BitScanReverse64(unsigned long *_Index, unsigned __int64 _Mask); |
Warren Hunt | d6ffae9 | 2013-09-27 23:57:26 +0000 | [diff] [blame] | 343 | static __inline__ |
Eric Christopher | d1428bf | 2013-08-31 00:22:48 +0000 | [diff] [blame] | 344 | unsigned char _bittest64(__int64 const *, __int64); |
Warren Hunt | d6ffae9 | 2013-09-27 23:57:26 +0000 | [diff] [blame] | 345 | static __inline__ |
Eric Christopher | d1428bf | 2013-08-31 00:22:48 +0000 | [diff] [blame] | 346 | unsigned char _bittestandcomplement64(__int64 *, __int64); |
Warren Hunt | d6ffae9 | 2013-09-27 23:57:26 +0000 | [diff] [blame] | 347 | static __inline__ |
Eric Christopher | d1428bf | 2013-08-31 00:22:48 +0000 | [diff] [blame] | 348 | unsigned char _bittestandreset64(__int64 *, __int64); |
Warren Hunt | d6ffae9 | 2013-09-27 23:57:26 +0000 | [diff] [blame] | 349 | static __inline__ |
Eric Christopher | d1428bf | 2013-08-31 00:22:48 +0000 | [diff] [blame] | 350 | unsigned char _bittestandset64(__int64 *, __int64); |
Eric Christopher | d1428bf | 2013-08-31 00:22:48 +0000 | [diff] [blame] | 351 | unsigned __int64 __cdecl _byteswap_uint64(unsigned __int64); |
Eric Christopher | d1428bf | 2013-08-31 00:22:48 +0000 | [diff] [blame] | 352 | long _InterlockedAnd_np(long volatile *_Value, long _Mask); |
| 353 | short _InterlockedAnd16_np(short volatile *_Value, short _Mask); |
| 354 | __int64 _InterlockedAnd64_np(__int64 volatile *_Value, __int64 _Mask); |
| 355 | char _InterlockedAnd8_np(char volatile *_Value, char _Mask); |
| 356 | unsigned char _interlockedbittestandreset64(__int64 volatile *, __int64); |
Hans Wennborg | a316933 | 2014-03-13 17:05:09 +0000 | [diff] [blame] | 357 | static __inline__ |
Eric Christopher | d1428bf | 2013-08-31 00:22:48 +0000 | [diff] [blame] | 358 | unsigned char _interlockedbittestandset64(__int64 volatile *, __int64); |
| 359 | long _InterlockedCompareExchange_np(long volatile *_Destination, long _Exchange, |
| 360 | long _Comparand); |
| 361 | unsigned char _InterlockedCompareExchange128(__int64 volatile *_Destination, |
| 362 | __int64 _ExchangeHigh, |
| 363 | __int64 _ExchangeLow, |
| 364 | __int64 *_CompareandResult); |
| 365 | unsigned char _InterlockedCompareExchange128_np(__int64 volatile *_Destination, |
| 366 | __int64 _ExchangeHigh, |
| 367 | __int64 _ExchangeLow, |
| 368 | __int64 *_ComparandResult); |
| 369 | short _InterlockedCompareExchange16_np(short volatile *_Destination, |
| 370 | short _Exchange, short _Comparand); |
Eric Christopher | 439137e | 2014-01-24 12:13:47 +0000 | [diff] [blame] | 371 | __int64 _InterlockedCompareExchange64_HLEAcquire(__int64 volatile *, __int64, |
| 372 | __int64); |
| 373 | __int64 _InterlockedCompareExchange64_HLERelease(__int64 volatile *, __int64, |
| 374 | __int64); |
Eric Christopher | d1428bf | 2013-08-31 00:22:48 +0000 | [diff] [blame] | 375 | __int64 _InterlockedCompareExchange64_np(__int64 volatile *_Destination, |
| 376 | __int64 _Exchange, __int64 _Comparand); |
Eric Christopher | 58b4043 | 2014-01-25 01:38:30 +0000 | [diff] [blame] | 377 | void *_InterlockedCompareExchangePointer(void *volatile *_Destination, |
| 378 | void *_Exchange, void *_Comparand); |
Eric Christopher | d1428bf | 2013-08-31 00:22:48 +0000 | [diff] [blame] | 379 | void *_InterlockedCompareExchangePointer_np(void *volatile *_Destination, |
| 380 | void *_Exchange, void *_Comparand); |
Reid Kleckner | 924eb2a | 2014-01-27 18:48:02 +0000 | [diff] [blame] | 381 | static __inline__ |
Eric Christopher | 439137e | 2014-01-24 12:13:47 +0000 | [diff] [blame] | 382 | __int64 _InterlockedDecrement64(__int64 volatile *_Addend); |
Reid Kleckner | 924eb2a | 2014-01-27 18:48:02 +0000 | [diff] [blame] | 383 | static __inline__ |
Eric Christopher | 439137e | 2014-01-24 12:13:47 +0000 | [diff] [blame] | 384 | __int64 _InterlockedExchange64(__int64 volatile *_Target, __int64 _Value); |
Reid Kleckner | 924eb2a | 2014-01-27 18:48:02 +0000 | [diff] [blame] | 385 | static __inline__ |
Eric Christopher | 439137e | 2014-01-24 12:13:47 +0000 | [diff] [blame] | 386 | __int64 _InterlockedExchangeAdd64(__int64 volatile *_Addend, __int64 _Value); |
| 387 | void *_InterlockedExchangePointer(void *volatile *_Target, void *_Value); |
Reid Kleckner | 924eb2a | 2014-01-27 18:48:02 +0000 | [diff] [blame] | 388 | static __inline__ |
Eric Christopher | 439137e | 2014-01-24 12:13:47 +0000 | [diff] [blame] | 389 | __int64 _InterlockedIncrement64(__int64 volatile *_Addend); |
Eric Christopher | d1428bf | 2013-08-31 00:22:48 +0000 | [diff] [blame] | 390 | long _InterlockedOr_np(long volatile *_Value, long _Mask); |
| 391 | short _InterlockedOr16_np(short volatile *_Value, short _Mask); |
Reid Kleckner | 924eb2a | 2014-01-27 18:48:02 +0000 | [diff] [blame] | 392 | static __inline__ |
Eric Christopher | 439137e | 2014-01-24 12:13:47 +0000 | [diff] [blame] | 393 | __int64 _InterlockedOr64(__int64 volatile *_Value, __int64 _Mask); |
Warren Hunt | 41a993f | 2013-09-28 00:15:41 +0000 | [diff] [blame] | 394 | __int64 _InterlockedOr64_np(__int64 volatile *_Value, __int64 _Mask); |
Eric Christopher | d1428bf | 2013-08-31 00:22:48 +0000 | [diff] [blame] | 395 | char _InterlockedOr8_np(char volatile *_Value, char _Mask); |
| 396 | long _InterlockedXor_np(long volatile *_Value, long _Mask); |
| 397 | short _InterlockedXor16_np(short volatile *_Value, short _Mask); |
Reid Kleckner | 924eb2a | 2014-01-27 18:48:02 +0000 | [diff] [blame] | 398 | static __inline__ |
Eric Christopher | 439137e | 2014-01-24 12:13:47 +0000 | [diff] [blame] | 399 | __int64 _InterlockedXor64(__int64 volatile *_Value, __int64 _Mask); |
Eric Christopher | d1428bf | 2013-08-31 00:22:48 +0000 | [diff] [blame] | 400 | __int64 _InterlockedXor64_np(__int64 volatile *_Value, __int64 _Mask); |
| 401 | char _InterlockedXor8_np(char volatile *_Value, char _Mask); |
Reid Kleckner | 924eb2a | 2014-01-27 18:48:02 +0000 | [diff] [blame] | 402 | static __inline__ |
Eric Christopher | d1428bf | 2013-08-31 00:22:48 +0000 | [diff] [blame] | 403 | __int64 _mul128(__int64 _Multiplier, __int64 _Multiplicand, |
| 404 | __int64 *_HighProduct); |
Eric Christopher | d1428bf | 2013-08-31 00:22:48 +0000 | [diff] [blame] | 405 | unsigned __int64 _rorx_u64(unsigned __int64, const unsigned int); |
Eric Christopher | 439137e | 2014-01-24 12:13:47 +0000 | [diff] [blame] | 406 | __int64 _sarx_i64(__int64, unsigned int); |
Nico Weber | a62cffa | 2014-07-08 18:34:46 +0000 | [diff] [blame] | 407 | #if __STDC_HOSTED__ |
| 408 | int __cdecl _setjmpex(jmp_buf); |
| 409 | #endif |
Eric Christopher | 439137e | 2014-01-24 12:13:47 +0000 | [diff] [blame] | 410 | unsigned __int64 _shlx_u64(unsigned __int64, unsigned int); |
David Majnemer | 5450763 | 2014-12-02 23:30:26 +0000 | [diff] [blame] | 411 | unsigned __int64 _shrx_u64(unsigned __int64, unsigned int); |
David Majnemer | 5f9afc5 | 2014-12-02 23:30:24 +0000 | [diff] [blame] | 412 | /* |
| 413 | * Multiply two 64-bit integers and obtain a 64-bit result. |
| 414 | * The low-half is returned directly and the high half is in an out parameter. |
| 415 | */ |
Michael Kuperstein | e45af54 | 2015-06-30 13:36:19 +0000 | [diff] [blame] | 416 | static __inline__ unsigned __int64 __DEFAULT_FN_ATTRS |
David Majnemer | 5f9afc5 | 2014-12-02 23:30:24 +0000 | [diff] [blame] | 417 | _umul128(unsigned __int64 _Multiplier, unsigned __int64 _Multiplicand, |
| 418 | unsigned __int64 *_HighProduct) { |
| 419 | unsigned __int128 _FullProduct = |
| 420 | (unsigned __int128)_Multiplier * (unsigned __int128)_Multiplicand; |
David Majnemer | 00973ce | 2014-12-02 23:44:40 +0000 | [diff] [blame] | 421 | *_HighProduct = _FullProduct >> 64; |
| 422 | return _FullProduct; |
David Majnemer | 5f9afc5 | 2014-12-02 23:30:24 +0000 | [diff] [blame] | 423 | } |
Michael Kuperstein | e45af54 | 2015-06-30 13:36:19 +0000 | [diff] [blame] | 424 | static __inline__ unsigned __int64 __DEFAULT_FN_ATTRS |
Reid Kleckner | baf7709 | 2014-12-03 23:36:14 +0000 | [diff] [blame] | 425 | __umulh(unsigned __int64 _Multiplier, unsigned __int64 _Multiplicand) { |
| 426 | unsigned __int128 _FullProduct = |
| 427 | (unsigned __int128)_Multiplier * (unsigned __int128)_Multiplicand; |
| 428 | return _FullProduct >> 64; |
| 429 | } |
Eric Christopher | 0db88a7 | 2013-09-18 22:24:01 +0000 | [diff] [blame] | 430 | |
Warren Hunt | 3f98794 | 2013-09-30 21:08:05 +0000 | [diff] [blame] | 431 | #endif /* __x86_64__ */ |
Reid Kleckner | f0e2322 | 2013-09-19 00:19:53 +0000 | [diff] [blame] | 432 | |
Warren Hunt | d6ffae9 | 2013-09-27 23:57:26 +0000 | [diff] [blame] | 433 | /*----------------------------------------------------------------------------*\ |
Hans Wennborg | 4ca00af | 2015-10-14 16:24:28 +0000 | [diff] [blame] | 434 | |* Multiplication |
| 435 | \*----------------------------------------------------------------------------*/ |
| 436 | static __inline__ __int64 __DEFAULT_FN_ATTRS |
| 437 | __emul(int __in1, int __in2) { |
| 438 | return (__int64)__in1 * (__int64)__in2; |
| 439 | } |
| 440 | static __inline__ unsigned __int64 __DEFAULT_FN_ATTRS |
| 441 | __emulu(unsigned int __in1, unsigned int __in2) { |
| 442 | return (unsigned __int64)__in1 * (unsigned __int64)__in2; |
| 443 | } |
| 444 | /*----------------------------------------------------------------------------*\ |
Warren Hunt | d6ffae9 | 2013-09-27 23:57:26 +0000 | [diff] [blame] | 445 | |* Bit Twiddling |
| 446 | \*----------------------------------------------------------------------------*/ |
Michael Kuperstein | e45af54 | 2015-06-30 13:36:19 +0000 | [diff] [blame] | 447 | static __inline__ unsigned char __DEFAULT_FN_ATTRS |
Warren Hunt | d6ffae9 | 2013-09-27 23:57:26 +0000 | [diff] [blame] | 448 | _rotl8(unsigned char _Value, unsigned char _Shift) { |
| 449 | _Shift &= 0x7; |
| 450 | return _Shift ? (_Value << _Shift) | (_Value >> (8 - _Shift)) : _Value; |
| 451 | } |
Michael Kuperstein | e45af54 | 2015-06-30 13:36:19 +0000 | [diff] [blame] | 452 | static __inline__ unsigned char __DEFAULT_FN_ATTRS |
Warren Hunt | d6ffae9 | 2013-09-27 23:57:26 +0000 | [diff] [blame] | 453 | _rotr8(unsigned char _Value, unsigned char _Shift) { |
| 454 | _Shift &= 0x7; |
| 455 | return _Shift ? (_Value >> _Shift) | (_Value << (8 - _Shift)) : _Value; |
| 456 | } |
Michael Kuperstein | e45af54 | 2015-06-30 13:36:19 +0000 | [diff] [blame] | 457 | static __inline__ unsigned short __DEFAULT_FN_ATTRS |
Warren Hunt | d6ffae9 | 2013-09-27 23:57:26 +0000 | [diff] [blame] | 458 | _rotl16(unsigned short _Value, unsigned char _Shift) { |
| 459 | _Shift &= 0xf; |
| 460 | return _Shift ? (_Value << _Shift) | (_Value >> (16 - _Shift)) : _Value; |
| 461 | } |
Michael Kuperstein | e45af54 | 2015-06-30 13:36:19 +0000 | [diff] [blame] | 462 | static __inline__ unsigned short __DEFAULT_FN_ATTRS |
Warren Hunt | d6ffae9 | 2013-09-27 23:57:26 +0000 | [diff] [blame] | 463 | _rotr16(unsigned short _Value, unsigned char _Shift) { |
| 464 | _Shift &= 0xf; |
| 465 | return _Shift ? (_Value >> _Shift) | (_Value << (16 - _Shift)) : _Value; |
| 466 | } |
Michael Kuperstein | b62c5bc | 2015-08-25 11:42:31 +0000 | [diff] [blame] | 467 | static __inline__ unsigned int __DEFAULT_FN_ATTRS |
| 468 | _rotl(unsigned int _Value, int _Shift) { |
| 469 | _Shift &= 0x1f; |
| 470 | return _Shift ? (_Value << _Shift) | (_Value >> (32 - _Shift)) : _Value; |
| 471 | } |
| 472 | static __inline__ unsigned int __DEFAULT_FN_ATTRS |
| 473 | _rotr(unsigned int _Value, int _Shift) { |
| 474 | _Shift &= 0x1f; |
| 475 | return _Shift ? (_Value >> _Shift) | (_Value << (32 - _Shift)) : _Value; |
| 476 | } |
| 477 | static __inline__ unsigned long __DEFAULT_FN_ATTRS |
| 478 | _lrotl(unsigned long _Value, int _Shift) { |
| 479 | _Shift &= 0x1f; |
| 480 | return _Shift ? (_Value << _Shift) | (_Value >> (32 - _Shift)) : _Value; |
| 481 | } |
| 482 | static __inline__ unsigned long __DEFAULT_FN_ATTRS |
| 483 | _lrotr(unsigned long _Value, int _Shift) { |
| 484 | _Shift &= 0x1f; |
| 485 | return _Shift ? (_Value >> _Shift) | (_Value << (32 - _Shift)) : _Value; |
| 486 | } |
Warren Hunt | d6ffae9 | 2013-09-27 23:57:26 +0000 | [diff] [blame] | 487 | static |
Michael Kuperstein | e45af54 | 2015-06-30 13:36:19 +0000 | [diff] [blame] | 488 | __inline__ unsigned __int64 __DEFAULT_FN_ATTRS |
Warren Hunt | d6ffae9 | 2013-09-27 23:57:26 +0000 | [diff] [blame] | 489 | _rotl64(unsigned __int64 _Value, int _Shift) { |
| 490 | _Shift &= 0x3f; |
| 491 | return _Shift ? (_Value << _Shift) | (_Value >> (64 - _Shift)) : _Value; |
| 492 | } |
| 493 | static |
Michael Kuperstein | e45af54 | 2015-06-30 13:36:19 +0000 | [diff] [blame] | 494 | __inline__ unsigned __int64 __DEFAULT_FN_ATTRS |
Warren Hunt | d6ffae9 | 2013-09-27 23:57:26 +0000 | [diff] [blame] | 495 | _rotr64(unsigned __int64 _Value, int _Shift) { |
| 496 | _Shift &= 0x3f; |
| 497 | return _Shift ? (_Value >> _Shift) | (_Value << (64 - _Shift)) : _Value; |
| 498 | } |
| 499 | /*----------------------------------------------------------------------------*\ |
| 500 | |* Bit Counting and Testing |
| 501 | \*----------------------------------------------------------------------------*/ |
Michael Kuperstein | e45af54 | 2015-06-30 13:36:19 +0000 | [diff] [blame] | 502 | static __inline__ unsigned char __DEFAULT_FN_ATTRS |
Warren Hunt | d6ffae9 | 2013-09-27 23:57:26 +0000 | [diff] [blame] | 503 | _BitScanForward(unsigned long *_Index, unsigned long _Mask) { |
| 504 | if (!_Mask) |
| 505 | return 0; |
| 506 | *_Index = __builtin_ctzl(_Mask); |
| 507 | return 1; |
| 508 | } |
Michael Kuperstein | e45af54 | 2015-06-30 13:36:19 +0000 | [diff] [blame] | 509 | static __inline__ unsigned char __DEFAULT_FN_ATTRS |
Warren Hunt | d6ffae9 | 2013-09-27 23:57:26 +0000 | [diff] [blame] | 510 | _BitScanReverse(unsigned long *_Index, unsigned long _Mask) { |
| 511 | if (!_Mask) |
| 512 | return 0; |
| 513 | *_Index = 31 - __builtin_clzl(_Mask); |
| 514 | return 1; |
| 515 | } |
Michael Kuperstein | e45af54 | 2015-06-30 13:36:19 +0000 | [diff] [blame] | 516 | static __inline__ unsigned short __DEFAULT_FN_ATTRS |
David Majnemer | 8dadce7 | 2015-07-13 02:53:23 +0000 | [diff] [blame] | 517 | __popcnt16(unsigned short _Value) { |
| 518 | return __builtin_popcount((int)_Value); |
Warren Hunt | d6ffae9 | 2013-09-27 23:57:26 +0000 | [diff] [blame] | 519 | } |
Michael Kuperstein | e45af54 | 2015-06-30 13:36:19 +0000 | [diff] [blame] | 520 | static __inline__ unsigned int __DEFAULT_FN_ATTRS |
David Majnemer | 8dadce7 | 2015-07-13 02:53:23 +0000 | [diff] [blame] | 521 | __popcnt(unsigned int _Value) { |
| 522 | return __builtin_popcount(_Value); |
Warren Hunt | d6ffae9 | 2013-09-27 23:57:26 +0000 | [diff] [blame] | 523 | } |
Michael Kuperstein | e45af54 | 2015-06-30 13:36:19 +0000 | [diff] [blame] | 524 | static __inline__ unsigned char __DEFAULT_FN_ATTRS |
David Majnemer | 56e4667 | 2015-07-13 23:38:56 +0000 | [diff] [blame] | 525 | _bittest(long const *_BitBase, long _BitPos) { |
| 526 | return (*_BitBase >> _BitPos) & 1; |
Warren Hunt | d6ffae9 | 2013-09-27 23:57:26 +0000 | [diff] [blame] | 527 | } |
Michael Kuperstein | e45af54 | 2015-06-30 13:36:19 +0000 | [diff] [blame] | 528 | static __inline__ unsigned char __DEFAULT_FN_ATTRS |
David Majnemer | 56e4667 | 2015-07-13 23:38:56 +0000 | [diff] [blame] | 529 | _bittestandcomplement(long *_BitBase, long _BitPos) { |
| 530 | unsigned char _Res = (*_BitBase >> _BitPos) & 1; |
| 531 | *_BitBase = *_BitBase ^ (1 << _BitPos); |
| 532 | return _Res; |
Warren Hunt | d6ffae9 | 2013-09-27 23:57:26 +0000 | [diff] [blame] | 533 | } |
Michael Kuperstein | e45af54 | 2015-06-30 13:36:19 +0000 | [diff] [blame] | 534 | static __inline__ unsigned char __DEFAULT_FN_ATTRS |
David Majnemer | 56e4667 | 2015-07-13 23:38:56 +0000 | [diff] [blame] | 535 | _bittestandreset(long *_BitBase, long _BitPos) { |
| 536 | unsigned char _Res = (*_BitBase >> _BitPos) & 1; |
| 537 | *_BitBase = *_BitBase & ~(1 << _BitPos); |
| 538 | return _Res; |
Warren Hunt | d6ffae9 | 2013-09-27 23:57:26 +0000 | [diff] [blame] | 539 | } |
Michael Kuperstein | e45af54 | 2015-06-30 13:36:19 +0000 | [diff] [blame] | 540 | static __inline__ unsigned char __DEFAULT_FN_ATTRS |
David Majnemer | 56e4667 | 2015-07-13 23:38:56 +0000 | [diff] [blame] | 541 | _bittestandset(long *_BitBase, long _BitPos) { |
| 542 | unsigned char _Res = (*_BitBase >> _BitPos) & 1; |
| 543 | *_BitBase = *_BitBase | (1 << _BitPos); |
| 544 | return _Res; |
Warren Hunt | d6ffae9 | 2013-09-27 23:57:26 +0000 | [diff] [blame] | 545 | } |
Michael Kuperstein | e45af54 | 2015-06-30 13:36:19 +0000 | [diff] [blame] | 546 | static __inline__ unsigned char __DEFAULT_FN_ATTRS |
David Majnemer | 56e4667 | 2015-07-13 23:38:56 +0000 | [diff] [blame] | 547 | _interlockedbittestandset(long volatile *_BitBase, long _BitPos) { |
David Majnemer | e0b863f | 2015-07-13 23:39:37 +0000 | [diff] [blame] | 548 | long _PrevVal = __atomic_fetch_or(_BitBase, 1l << _BitPos, __ATOMIC_SEQ_CST); |
| 549 | return (_PrevVal >> _BitPos) & 1; |
Hans Wennborg | a316933 | 2014-03-13 17:05:09 +0000 | [diff] [blame] | 550 | } |
Warren Hunt | 3f98794 | 2013-09-30 21:08:05 +0000 | [diff] [blame] | 551 | #ifdef __x86_64__ |
Michael Kuperstein | e45af54 | 2015-06-30 13:36:19 +0000 | [diff] [blame] | 552 | static __inline__ unsigned char __DEFAULT_FN_ATTRS |
Warren Hunt | d6ffae9 | 2013-09-27 23:57:26 +0000 | [diff] [blame] | 553 | _BitScanForward64(unsigned long *_Index, unsigned __int64 _Mask) { |
| 554 | if (!_Mask) |
| 555 | return 0; |
| 556 | *_Index = __builtin_ctzll(_Mask); |
| 557 | return 1; |
| 558 | } |
Michael Kuperstein | e45af54 | 2015-06-30 13:36:19 +0000 | [diff] [blame] | 559 | static __inline__ unsigned char __DEFAULT_FN_ATTRS |
Warren Hunt | d6ffae9 | 2013-09-27 23:57:26 +0000 | [diff] [blame] | 560 | _BitScanReverse64(unsigned long *_Index, unsigned __int64 _Mask) { |
| 561 | if (!_Mask) |
| 562 | return 0; |
| 563 | *_Index = 63 - __builtin_clzll(_Mask); |
| 564 | return 1; |
| 565 | } |
Warren Hunt | d6ffae9 | 2013-09-27 23:57:26 +0000 | [diff] [blame] | 566 | static __inline__ |
Michael Kuperstein | e45af54 | 2015-06-30 13:36:19 +0000 | [diff] [blame] | 567 | unsigned __int64 __DEFAULT_FN_ATTRS |
David Majnemer | 8dadce7 | 2015-07-13 02:53:23 +0000 | [diff] [blame] | 568 | __popcnt64(unsigned __int64 _Value) { |
| 569 | return __builtin_popcountll(_Value); |
Warren Hunt | d6ffae9 | 2013-09-27 23:57:26 +0000 | [diff] [blame] | 570 | } |
Michael Kuperstein | e45af54 | 2015-06-30 13:36:19 +0000 | [diff] [blame] | 571 | static __inline__ unsigned char __DEFAULT_FN_ATTRS |
David Majnemer | 56e4667 | 2015-07-13 23:38:56 +0000 | [diff] [blame] | 572 | _bittest64(__int64 const *_BitBase, __int64 _BitPos) { |
| 573 | return (*_BitBase >> _BitPos) & 1; |
Warren Hunt | d6ffae9 | 2013-09-27 23:57:26 +0000 | [diff] [blame] | 574 | } |
Michael Kuperstein | e45af54 | 2015-06-30 13:36:19 +0000 | [diff] [blame] | 575 | static __inline__ unsigned char __DEFAULT_FN_ATTRS |
David Majnemer | 56e4667 | 2015-07-13 23:38:56 +0000 | [diff] [blame] | 576 | _bittestandcomplement64(__int64 *_BitBase, __int64 _BitPos) { |
| 577 | unsigned char _Res = (*_BitBase >> _BitPos) & 1; |
| 578 | *_BitBase = *_BitBase ^ (1ll << _BitPos); |
| 579 | return _Res; |
Warren Hunt | d6ffae9 | 2013-09-27 23:57:26 +0000 | [diff] [blame] | 580 | } |
Michael Kuperstein | e45af54 | 2015-06-30 13:36:19 +0000 | [diff] [blame] | 581 | static __inline__ unsigned char __DEFAULT_FN_ATTRS |
David Majnemer | 56e4667 | 2015-07-13 23:38:56 +0000 | [diff] [blame] | 582 | _bittestandreset64(__int64 *_BitBase, __int64 _BitPos) { |
| 583 | unsigned char _Res = (*_BitBase >> _BitPos) & 1; |
| 584 | *_BitBase = *_BitBase & ~(1ll << _BitPos); |
| 585 | return _Res; |
Warren Hunt | d6ffae9 | 2013-09-27 23:57:26 +0000 | [diff] [blame] | 586 | } |
Michael Kuperstein | e45af54 | 2015-06-30 13:36:19 +0000 | [diff] [blame] | 587 | static __inline__ unsigned char __DEFAULT_FN_ATTRS |
David Majnemer | 56e4667 | 2015-07-13 23:38:56 +0000 | [diff] [blame] | 588 | _bittestandset64(__int64 *_BitBase, __int64 _BitPos) { |
| 589 | unsigned char _Res = (*_BitBase >> _BitPos) & 1; |
| 590 | *_BitBase = *_BitBase | (1ll << _BitPos); |
| 591 | return _Res; |
Warren Hunt | d6ffae9 | 2013-09-27 23:57:26 +0000 | [diff] [blame] | 592 | } |
Michael Kuperstein | e45af54 | 2015-06-30 13:36:19 +0000 | [diff] [blame] | 593 | static __inline__ unsigned char __DEFAULT_FN_ATTRS |
David Majnemer | 56e4667 | 2015-07-13 23:38:56 +0000 | [diff] [blame] | 594 | _interlockedbittestandset64(__int64 volatile *_BitBase, __int64 _BitPos) { |
David Majnemer | e0b863f | 2015-07-13 23:39:37 +0000 | [diff] [blame] | 595 | long long _PrevVal = |
| 596 | __atomic_fetch_or(_BitBase, 1ll << _BitPos, __ATOMIC_SEQ_CST); |
| 597 | return (_PrevVal >> _BitPos) & 1; |
Hans Wennborg | a316933 | 2014-03-13 17:05:09 +0000 | [diff] [blame] | 598 | } |
Warren Hunt | d6ffae9 | 2013-09-27 23:57:26 +0000 | [diff] [blame] | 599 | #endif |
| 600 | /*----------------------------------------------------------------------------*\ |
| 601 | |* Interlocked Exchange Add |
| 602 | \*----------------------------------------------------------------------------*/ |
Michael Kuperstein | e45af54 | 2015-06-30 13:36:19 +0000 | [diff] [blame] | 603 | static __inline__ char __DEFAULT_FN_ATTRS |
Warren Hunt | d6ffae9 | 2013-09-27 23:57:26 +0000 | [diff] [blame] | 604 | _InterlockedExchangeAdd8(char volatile *_Addend, char _Value) { |
David Majnemer | e0b863f | 2015-07-13 23:39:37 +0000 | [diff] [blame] | 605 | return __atomic_fetch_add(_Addend, _Value, __ATOMIC_SEQ_CST); |
Warren Hunt | d6ffae9 | 2013-09-27 23:57:26 +0000 | [diff] [blame] | 606 | } |
Michael Kuperstein | e45af54 | 2015-06-30 13:36:19 +0000 | [diff] [blame] | 607 | static __inline__ short __DEFAULT_FN_ATTRS |
Warren Hunt | d6ffae9 | 2013-09-27 23:57:26 +0000 | [diff] [blame] | 608 | _InterlockedExchangeAdd16(short volatile *_Addend, short _Value) { |
David Majnemer | e0b863f | 2015-07-13 23:39:37 +0000 | [diff] [blame] | 609 | return __atomic_fetch_add(_Addend, _Value, __ATOMIC_SEQ_CST); |
Warren Hunt | d6ffae9 | 2013-09-27 23:57:26 +0000 | [diff] [blame] | 610 | } |
Warren Hunt | 3f98794 | 2013-09-30 21:08:05 +0000 | [diff] [blame] | 611 | #ifdef __x86_64__ |
Michael Kuperstein | e45af54 | 2015-06-30 13:36:19 +0000 | [diff] [blame] | 612 | static __inline__ __int64 __DEFAULT_FN_ATTRS |
Warren Hunt | d6ffae9 | 2013-09-27 23:57:26 +0000 | [diff] [blame] | 613 | _InterlockedExchangeAdd64(__int64 volatile *_Addend, __int64 _Value) { |
David Majnemer | e0b863f | 2015-07-13 23:39:37 +0000 | [diff] [blame] | 614 | return __atomic_fetch_add(_Addend, _Value, __ATOMIC_SEQ_CST); |
Warren Hunt | d6ffae9 | 2013-09-27 23:57:26 +0000 | [diff] [blame] | 615 | } |
| 616 | #endif |
| 617 | /*----------------------------------------------------------------------------*\ |
| 618 | |* Interlocked Exchange Sub |
| 619 | \*----------------------------------------------------------------------------*/ |
Michael Kuperstein | e45af54 | 2015-06-30 13:36:19 +0000 | [diff] [blame] | 620 | static __inline__ char __DEFAULT_FN_ATTRS |
Warren Hunt | d6ffae9 | 2013-09-27 23:57:26 +0000 | [diff] [blame] | 621 | _InterlockedExchangeSub8(char volatile *_Subend, char _Value) { |
David Majnemer | e0b863f | 2015-07-13 23:39:37 +0000 | [diff] [blame] | 622 | return __atomic_fetch_sub(_Subend, _Value, __ATOMIC_SEQ_CST); |
Warren Hunt | d6ffae9 | 2013-09-27 23:57:26 +0000 | [diff] [blame] | 623 | } |
Michael Kuperstein | e45af54 | 2015-06-30 13:36:19 +0000 | [diff] [blame] | 624 | static __inline__ short __DEFAULT_FN_ATTRS |
Warren Hunt | d6ffae9 | 2013-09-27 23:57:26 +0000 | [diff] [blame] | 625 | _InterlockedExchangeSub16(short volatile *_Subend, short _Value) { |
David Majnemer | e0b863f | 2015-07-13 23:39:37 +0000 | [diff] [blame] | 626 | return __atomic_fetch_sub(_Subend, _Value, __ATOMIC_SEQ_CST); |
Warren Hunt | d6ffae9 | 2013-09-27 23:57:26 +0000 | [diff] [blame] | 627 | } |
Michael Kuperstein | e45af54 | 2015-06-30 13:36:19 +0000 | [diff] [blame] | 628 | static __inline__ long __DEFAULT_FN_ATTRS |
Warren Hunt | d6ffae9 | 2013-09-27 23:57:26 +0000 | [diff] [blame] | 629 | _InterlockedExchangeSub(long volatile *_Subend, long _Value) { |
David Majnemer | e0b863f | 2015-07-13 23:39:37 +0000 | [diff] [blame] | 630 | return __atomic_fetch_sub(_Subend, _Value, __ATOMIC_SEQ_CST); |
Warren Hunt | d6ffae9 | 2013-09-27 23:57:26 +0000 | [diff] [blame] | 631 | } |
Warren Hunt | 3f98794 | 2013-09-30 21:08:05 +0000 | [diff] [blame] | 632 | #ifdef __x86_64__ |
Michael Kuperstein | e45af54 | 2015-06-30 13:36:19 +0000 | [diff] [blame] | 633 | static __inline__ __int64 __DEFAULT_FN_ATTRS |
Warren Hunt | d6ffae9 | 2013-09-27 23:57:26 +0000 | [diff] [blame] | 634 | _InterlockedExchangeSub64(__int64 volatile *_Subend, __int64 _Value) { |
David Majnemer | e0b863f | 2015-07-13 23:39:37 +0000 | [diff] [blame] | 635 | return __atomic_fetch_sub(_Subend, _Value, __ATOMIC_SEQ_CST); |
Warren Hunt | d6ffae9 | 2013-09-27 23:57:26 +0000 | [diff] [blame] | 636 | } |
| 637 | #endif |
| 638 | /*----------------------------------------------------------------------------*\ |
| 639 | |* Interlocked Increment |
| 640 | \*----------------------------------------------------------------------------*/ |
Michael Kuperstein | e45af54 | 2015-06-30 13:36:19 +0000 | [diff] [blame] | 641 | static __inline__ short __DEFAULT_FN_ATTRS |
Hans Wennborg | 2ed8880 | 2014-01-23 19:15:39 +0000 | [diff] [blame] | 642 | _InterlockedIncrement16(short volatile *_Value) { |
David Majnemer | e0b863f | 2015-07-13 23:39:37 +0000 | [diff] [blame] | 643 | return __atomic_add_fetch(_Value, 1, __ATOMIC_SEQ_CST); |
Warren Hunt | d6ffae9 | 2013-09-27 23:57:26 +0000 | [diff] [blame] | 644 | } |
Warren Hunt | 3f98794 | 2013-09-30 21:08:05 +0000 | [diff] [blame] | 645 | #ifdef __x86_64__ |
Michael Kuperstein | e45af54 | 2015-06-30 13:36:19 +0000 | [diff] [blame] | 646 | static __inline__ __int64 __DEFAULT_FN_ATTRS |
Warren Hunt | d6ffae9 | 2013-09-27 23:57:26 +0000 | [diff] [blame] | 647 | _InterlockedIncrement64(__int64 volatile *_Value) { |
David Majnemer | e0b863f | 2015-07-13 23:39:37 +0000 | [diff] [blame] | 648 | return __atomic_add_fetch(_Value, 1, __ATOMIC_SEQ_CST); |
Warren Hunt | d6ffae9 | 2013-09-27 23:57:26 +0000 | [diff] [blame] | 649 | } |
| 650 | #endif |
| 651 | /*----------------------------------------------------------------------------*\ |
| 652 | |* Interlocked Decrement |
| 653 | \*----------------------------------------------------------------------------*/ |
Michael Kuperstein | e45af54 | 2015-06-30 13:36:19 +0000 | [diff] [blame] | 654 | static __inline__ short __DEFAULT_FN_ATTRS |
Hans Wennborg | 2ed8880 | 2014-01-23 19:15:39 +0000 | [diff] [blame] | 655 | _InterlockedDecrement16(short volatile *_Value) { |
David Majnemer | e0b863f | 2015-07-13 23:39:37 +0000 | [diff] [blame] | 656 | return __atomic_sub_fetch(_Value, 1, __ATOMIC_SEQ_CST); |
Warren Hunt | d6ffae9 | 2013-09-27 23:57:26 +0000 | [diff] [blame] | 657 | } |
Warren Hunt | 3f98794 | 2013-09-30 21:08:05 +0000 | [diff] [blame] | 658 | #ifdef __x86_64__ |
Michael Kuperstein | e45af54 | 2015-06-30 13:36:19 +0000 | [diff] [blame] | 659 | static __inline__ __int64 __DEFAULT_FN_ATTRS |
Warren Hunt | d6ffae9 | 2013-09-27 23:57:26 +0000 | [diff] [blame] | 660 | _InterlockedDecrement64(__int64 volatile *_Value) { |
David Majnemer | e0b863f | 2015-07-13 23:39:37 +0000 | [diff] [blame] | 661 | return __atomic_sub_fetch(_Value, 1, __ATOMIC_SEQ_CST); |
Warren Hunt | d6ffae9 | 2013-09-27 23:57:26 +0000 | [diff] [blame] | 662 | } |
| 663 | #endif |
| 664 | /*----------------------------------------------------------------------------*\ |
| 665 | |* Interlocked And |
| 666 | \*----------------------------------------------------------------------------*/ |
Michael Kuperstein | e45af54 | 2015-06-30 13:36:19 +0000 | [diff] [blame] | 667 | static __inline__ char __DEFAULT_FN_ATTRS |
Warren Hunt | d6ffae9 | 2013-09-27 23:57:26 +0000 | [diff] [blame] | 668 | _InterlockedAnd8(char volatile *_Value, char _Mask) { |
David Majnemer | e0b863f | 2015-07-13 23:39:37 +0000 | [diff] [blame] | 669 | return __atomic_and_fetch(_Value, _Mask, __ATOMIC_SEQ_CST); |
Warren Hunt | d6ffae9 | 2013-09-27 23:57:26 +0000 | [diff] [blame] | 670 | } |
Michael Kuperstein | e45af54 | 2015-06-30 13:36:19 +0000 | [diff] [blame] | 671 | static __inline__ short __DEFAULT_FN_ATTRS |
Warren Hunt | d6ffae9 | 2013-09-27 23:57:26 +0000 | [diff] [blame] | 672 | _InterlockedAnd16(short volatile *_Value, short _Mask) { |
David Majnemer | e0b863f | 2015-07-13 23:39:37 +0000 | [diff] [blame] | 673 | return __atomic_and_fetch(_Value, _Mask, __ATOMIC_SEQ_CST); |
Warren Hunt | d6ffae9 | 2013-09-27 23:57:26 +0000 | [diff] [blame] | 674 | } |
Michael Kuperstein | e45af54 | 2015-06-30 13:36:19 +0000 | [diff] [blame] | 675 | static __inline__ long __DEFAULT_FN_ATTRS |
Warren Hunt | d6ffae9 | 2013-09-27 23:57:26 +0000 | [diff] [blame] | 676 | _InterlockedAnd(long volatile *_Value, long _Mask) { |
David Majnemer | e0b863f | 2015-07-13 23:39:37 +0000 | [diff] [blame] | 677 | return __atomic_and_fetch(_Value, _Mask, __ATOMIC_SEQ_CST); |
Warren Hunt | d6ffae9 | 2013-09-27 23:57:26 +0000 | [diff] [blame] | 678 | } |
Warren Hunt | 3f98794 | 2013-09-30 21:08:05 +0000 | [diff] [blame] | 679 | #ifdef __x86_64__ |
Michael Kuperstein | e45af54 | 2015-06-30 13:36:19 +0000 | [diff] [blame] | 680 | static __inline__ __int64 __DEFAULT_FN_ATTRS |
Warren Hunt | d6ffae9 | 2013-09-27 23:57:26 +0000 | [diff] [blame] | 681 | _InterlockedAnd64(__int64 volatile *_Value, __int64 _Mask) { |
David Majnemer | e0b863f | 2015-07-13 23:39:37 +0000 | [diff] [blame] | 682 | return __atomic_and_fetch(_Value, _Mask, __ATOMIC_SEQ_CST); |
Warren Hunt | d6ffae9 | 2013-09-27 23:57:26 +0000 | [diff] [blame] | 683 | } |
| 684 | #endif |
| 685 | /*----------------------------------------------------------------------------*\ |
| 686 | |* Interlocked Or |
| 687 | \*----------------------------------------------------------------------------*/ |
Michael Kuperstein | e45af54 | 2015-06-30 13:36:19 +0000 | [diff] [blame] | 688 | static __inline__ char __DEFAULT_FN_ATTRS |
Warren Hunt | d6ffae9 | 2013-09-27 23:57:26 +0000 | [diff] [blame] | 689 | _InterlockedOr8(char volatile *_Value, char _Mask) { |
David Majnemer | e0b863f | 2015-07-13 23:39:37 +0000 | [diff] [blame] | 690 | return __atomic_or_fetch(_Value, _Mask, __ATOMIC_SEQ_CST); |
Warren Hunt | d6ffae9 | 2013-09-27 23:57:26 +0000 | [diff] [blame] | 691 | } |
Michael Kuperstein | e45af54 | 2015-06-30 13:36:19 +0000 | [diff] [blame] | 692 | static __inline__ short __DEFAULT_FN_ATTRS |
Warren Hunt | d6ffae9 | 2013-09-27 23:57:26 +0000 | [diff] [blame] | 693 | _InterlockedOr16(short volatile *_Value, short _Mask) { |
David Majnemer | e0b863f | 2015-07-13 23:39:37 +0000 | [diff] [blame] | 694 | return __atomic_or_fetch(_Value, _Mask, __ATOMIC_SEQ_CST); |
Warren Hunt | d6ffae9 | 2013-09-27 23:57:26 +0000 | [diff] [blame] | 695 | } |
Michael Kuperstein | e45af54 | 2015-06-30 13:36:19 +0000 | [diff] [blame] | 696 | static __inline__ long __DEFAULT_FN_ATTRS |
Warren Hunt | d6ffae9 | 2013-09-27 23:57:26 +0000 | [diff] [blame] | 697 | _InterlockedOr(long volatile *_Value, long _Mask) { |
David Majnemer | e0b863f | 2015-07-13 23:39:37 +0000 | [diff] [blame] | 698 | return __atomic_or_fetch(_Value, _Mask, __ATOMIC_SEQ_CST); |
Warren Hunt | d6ffae9 | 2013-09-27 23:57:26 +0000 | [diff] [blame] | 699 | } |
Warren Hunt | 3f98794 | 2013-09-30 21:08:05 +0000 | [diff] [blame] | 700 | #ifdef __x86_64__ |
Michael Kuperstein | e45af54 | 2015-06-30 13:36:19 +0000 | [diff] [blame] | 701 | static __inline__ __int64 __DEFAULT_FN_ATTRS |
Warren Hunt | d6ffae9 | 2013-09-27 23:57:26 +0000 | [diff] [blame] | 702 | _InterlockedOr64(__int64 volatile *_Value, __int64 _Mask) { |
David Majnemer | e0b863f | 2015-07-13 23:39:37 +0000 | [diff] [blame] | 703 | return __atomic_or_fetch(_Value, _Mask, __ATOMIC_SEQ_CST); |
Warren Hunt | d6ffae9 | 2013-09-27 23:57:26 +0000 | [diff] [blame] | 704 | } |
| 705 | #endif |
| 706 | /*----------------------------------------------------------------------------*\ |
| 707 | |* Interlocked Xor |
| 708 | \*----------------------------------------------------------------------------*/ |
Michael Kuperstein | e45af54 | 2015-06-30 13:36:19 +0000 | [diff] [blame] | 709 | static __inline__ char __DEFAULT_FN_ATTRS |
Warren Hunt | d6ffae9 | 2013-09-27 23:57:26 +0000 | [diff] [blame] | 710 | _InterlockedXor8(char volatile *_Value, char _Mask) { |
David Majnemer | e0b863f | 2015-07-13 23:39:37 +0000 | [diff] [blame] | 711 | return __atomic_xor_fetch(_Value, _Mask, __ATOMIC_SEQ_CST); |
Warren Hunt | d6ffae9 | 2013-09-27 23:57:26 +0000 | [diff] [blame] | 712 | } |
Michael Kuperstein | e45af54 | 2015-06-30 13:36:19 +0000 | [diff] [blame] | 713 | static __inline__ short __DEFAULT_FN_ATTRS |
Warren Hunt | d6ffae9 | 2013-09-27 23:57:26 +0000 | [diff] [blame] | 714 | _InterlockedXor16(short volatile *_Value, short _Mask) { |
David Majnemer | e0b863f | 2015-07-13 23:39:37 +0000 | [diff] [blame] | 715 | return __atomic_xor_fetch(_Value, _Mask, __ATOMIC_SEQ_CST); |
Warren Hunt | d6ffae9 | 2013-09-27 23:57:26 +0000 | [diff] [blame] | 716 | } |
Michael Kuperstein | e45af54 | 2015-06-30 13:36:19 +0000 | [diff] [blame] | 717 | static __inline__ long __DEFAULT_FN_ATTRS |
Warren Hunt | d6ffae9 | 2013-09-27 23:57:26 +0000 | [diff] [blame] | 718 | _InterlockedXor(long volatile *_Value, long _Mask) { |
David Majnemer | e0b863f | 2015-07-13 23:39:37 +0000 | [diff] [blame] | 719 | return __atomic_xor_fetch(_Value, _Mask, __ATOMIC_SEQ_CST); |
Warren Hunt | d6ffae9 | 2013-09-27 23:57:26 +0000 | [diff] [blame] | 720 | } |
Warren Hunt | 3f98794 | 2013-09-30 21:08:05 +0000 | [diff] [blame] | 721 | #ifdef __x86_64__ |
Michael Kuperstein | e45af54 | 2015-06-30 13:36:19 +0000 | [diff] [blame] | 722 | static __inline__ __int64 __DEFAULT_FN_ATTRS |
Warren Hunt | d6ffae9 | 2013-09-27 23:57:26 +0000 | [diff] [blame] | 723 | _InterlockedXor64(__int64 volatile *_Value, __int64 _Mask) { |
David Majnemer | e0b863f | 2015-07-13 23:39:37 +0000 | [diff] [blame] | 724 | return __atomic_xor_fetch(_Value, _Mask, __ATOMIC_SEQ_CST); |
Warren Hunt | d6ffae9 | 2013-09-27 23:57:26 +0000 | [diff] [blame] | 725 | } |
| 726 | #endif |
| 727 | /*----------------------------------------------------------------------------*\ |
| 728 | |* Interlocked Exchange |
| 729 | \*----------------------------------------------------------------------------*/ |
Michael Kuperstein | e45af54 | 2015-06-30 13:36:19 +0000 | [diff] [blame] | 730 | static __inline__ char __DEFAULT_FN_ATTRS |
Warren Hunt | d6ffae9 | 2013-09-27 23:57:26 +0000 | [diff] [blame] | 731 | _InterlockedExchange8(char volatile *_Target, char _Value) { |
David Majnemer | e0b863f | 2015-07-13 23:39:37 +0000 | [diff] [blame] | 732 | __atomic_exchange(_Target, &_Value, &_Value, __ATOMIC_SEQ_CST); |
Warren Hunt | d6ffae9 | 2013-09-27 23:57:26 +0000 | [diff] [blame] | 733 | return _Value; |
| 734 | } |
Michael Kuperstein | e45af54 | 2015-06-30 13:36:19 +0000 | [diff] [blame] | 735 | static __inline__ short __DEFAULT_FN_ATTRS |
Warren Hunt | d6ffae9 | 2013-09-27 23:57:26 +0000 | [diff] [blame] | 736 | _InterlockedExchange16(short volatile *_Target, short _Value) { |
David Majnemer | e0b863f | 2015-07-13 23:39:37 +0000 | [diff] [blame] | 737 | __atomic_exchange(_Target, &_Value, &_Value, __ATOMIC_SEQ_CST); |
Warren Hunt | d6ffae9 | 2013-09-27 23:57:26 +0000 | [diff] [blame] | 738 | return _Value; |
| 739 | } |
Warren Hunt | 3f98794 | 2013-09-30 21:08:05 +0000 | [diff] [blame] | 740 | #ifdef __x86_64__ |
Michael Kuperstein | e45af54 | 2015-06-30 13:36:19 +0000 | [diff] [blame] | 741 | static __inline__ __int64 __DEFAULT_FN_ATTRS |
Warren Hunt | d6ffae9 | 2013-09-27 23:57:26 +0000 | [diff] [blame] | 742 | _InterlockedExchange64(__int64 volatile *_Target, __int64 _Value) { |
David Majnemer | e0b863f | 2015-07-13 23:39:37 +0000 | [diff] [blame] | 743 | __atomic_exchange(_Target, &_Value, &_Value, __ATOMIC_SEQ_CST); |
Warren Hunt | d6ffae9 | 2013-09-27 23:57:26 +0000 | [diff] [blame] | 744 | return _Value; |
| 745 | } |
| 746 | #endif |
| 747 | /*----------------------------------------------------------------------------*\ |
| 748 | |* Interlocked Compare Exchange |
| 749 | \*----------------------------------------------------------------------------*/ |
Michael Kuperstein | e45af54 | 2015-06-30 13:36:19 +0000 | [diff] [blame] | 750 | static __inline__ char __DEFAULT_FN_ATTRS |
Warren Hunt | d6ffae9 | 2013-09-27 23:57:26 +0000 | [diff] [blame] | 751 | _InterlockedCompareExchange8(char volatile *_Destination, |
| 752 | char _Exchange, char _Comparand) { |
David Majnemer | e0b863f | 2015-07-13 23:39:37 +0000 | [diff] [blame] | 753 | __atomic_compare_exchange(_Destination, &_Comparand, &_Exchange, 0, |
| 754 | __ATOMIC_SEQ_CST, __ATOMIC_SEQ_CST); |
Warren Hunt | d6ffae9 | 2013-09-27 23:57:26 +0000 | [diff] [blame] | 755 | return _Comparand; |
| 756 | } |
Michael Kuperstein | e45af54 | 2015-06-30 13:36:19 +0000 | [diff] [blame] | 757 | static __inline__ short __DEFAULT_FN_ATTRS |
Warren Hunt | d6ffae9 | 2013-09-27 23:57:26 +0000 | [diff] [blame] | 758 | _InterlockedCompareExchange16(short volatile *_Destination, |
| 759 | short _Exchange, short _Comparand) { |
David Majnemer | e0b863f | 2015-07-13 23:39:37 +0000 | [diff] [blame] | 760 | __atomic_compare_exchange(_Destination, &_Comparand, &_Exchange, 0, |
| 761 | __ATOMIC_SEQ_CST, __ATOMIC_SEQ_CST); |
Warren Hunt | d6ffae9 | 2013-09-27 23:57:26 +0000 | [diff] [blame] | 762 | return _Comparand; |
| 763 | } |
Michael Kuperstein | e45af54 | 2015-06-30 13:36:19 +0000 | [diff] [blame] | 764 | static __inline__ __int64 __DEFAULT_FN_ATTRS |
Warren Hunt | d6ffae9 | 2013-09-27 23:57:26 +0000 | [diff] [blame] | 765 | _InterlockedCompareExchange64(__int64 volatile *_Destination, |
| 766 | __int64 _Exchange, __int64 _Comparand) { |
David Majnemer | e0b863f | 2015-07-13 23:39:37 +0000 | [diff] [blame] | 767 | __atomic_compare_exchange(_Destination, &_Comparand, &_Exchange, 0, |
| 768 | __ATOMIC_SEQ_CST, __ATOMIC_SEQ_CST); |
Warren Hunt | d6ffae9 | 2013-09-27 23:57:26 +0000 | [diff] [blame] | 769 | return _Comparand; |
| 770 | } |
Warren Hunt | d6ffae9 | 2013-09-27 23:57:26 +0000 | [diff] [blame] | 771 | /*----------------------------------------------------------------------------*\ |
Reid Kleckner | 00d33a5 | 2013-10-17 01:29:48 +0000 | [diff] [blame] | 772 | |* Barriers |
| 773 | \*----------------------------------------------------------------------------*/ |
Michael Kuperstein | e45af54 | 2015-06-30 13:36:19 +0000 | [diff] [blame] | 774 | static __inline__ void __DEFAULT_FN_ATTRS |
David Majnemer | 1cf22e6 | 2015-02-04 00:26:10 +0000 | [diff] [blame] | 775 | __attribute__((__deprecated__("use other intrinsics or C++11 atomics instead"))) |
Reid Kleckner | 00d33a5 | 2013-10-17 01:29:48 +0000 | [diff] [blame] | 776 | _ReadWriteBarrier(void) { |
David Majnemer | 6b8e297 | 2015-07-16 03:13:02 +0000 | [diff] [blame] | 777 | __atomic_signal_fence(__ATOMIC_SEQ_CST); |
Reid Kleckner | 00d33a5 | 2013-10-17 01:29:48 +0000 | [diff] [blame] | 778 | } |
Michael Kuperstein | e45af54 | 2015-06-30 13:36:19 +0000 | [diff] [blame] | 779 | static __inline__ void __DEFAULT_FN_ATTRS |
David Majnemer | 1cf22e6 | 2015-02-04 00:26:10 +0000 | [diff] [blame] | 780 | __attribute__((__deprecated__("use other intrinsics or C++11 atomics instead"))) |
Reid Kleckner | 00d33a5 | 2013-10-17 01:29:48 +0000 | [diff] [blame] | 781 | _ReadBarrier(void) { |
David Majnemer | 6b8e297 | 2015-07-16 03:13:02 +0000 | [diff] [blame] | 782 | __atomic_signal_fence(__ATOMIC_SEQ_CST); |
Reid Kleckner | 00d33a5 | 2013-10-17 01:29:48 +0000 | [diff] [blame] | 783 | } |
Michael Kuperstein | e45af54 | 2015-06-30 13:36:19 +0000 | [diff] [blame] | 784 | static __inline__ void __DEFAULT_FN_ATTRS |
David Majnemer | 1cf22e6 | 2015-02-04 00:26:10 +0000 | [diff] [blame] | 785 | __attribute__((__deprecated__("use other intrinsics or C++11 atomics instead"))) |
Reid Kleckner | 00d33a5 | 2013-10-17 01:29:48 +0000 | [diff] [blame] | 786 | _WriteBarrier(void) { |
David Majnemer | 6b8e297 | 2015-07-16 03:13:02 +0000 | [diff] [blame] | 787 | __atomic_signal_fence(__ATOMIC_SEQ_CST); |
Reid Kleckner | 00d33a5 | 2013-10-17 01:29:48 +0000 | [diff] [blame] | 788 | } |
Reid Kleckner | f08d658 | 2014-01-27 19:16:35 +0000 | [diff] [blame] | 789 | #ifdef __x86_64__ |
Michael Kuperstein | e45af54 | 2015-06-30 13:36:19 +0000 | [diff] [blame] | 790 | static __inline__ void __DEFAULT_FN_ATTRS |
Reid Kleckner | f08d658 | 2014-01-27 19:16:35 +0000 | [diff] [blame] | 791 | __faststorefence(void) { |
David Majnemer | 6b8e297 | 2015-07-16 03:13:02 +0000 | [diff] [blame] | 792 | __atomic_thread_fence(__ATOMIC_SEQ_CST); |
Reid Kleckner | f08d658 | 2014-01-27 19:16:35 +0000 | [diff] [blame] | 793 | } |
| 794 | #endif |
Reid Kleckner | 00d33a5 | 2013-10-17 01:29:48 +0000 | [diff] [blame] | 795 | /*----------------------------------------------------------------------------*\ |
Hans Wennborg | a4421e0 | 2014-03-12 21:09:05 +0000 | [diff] [blame] | 796 | |* readfs, readgs |
| 797 | |* (Pointers in address space #256 and #257 are relative to the GS and FS |
| 798 | |* segment registers, respectively.) |
Hans Wennborg | 74ca0c4 | 2014-01-24 00:52:39 +0000 | [diff] [blame] | 799 | \*----------------------------------------------------------------------------*/ |
Hans Wennborg | 74ca0c4 | 2014-01-24 00:52:39 +0000 | [diff] [blame] | 800 | #define __ptr_to_addr_space(__addr_space_nbr, __type, __offset) \ |
| 801 | ((volatile __type __attribute__((__address_space__(__addr_space_nbr)))*) \ |
| 802 | (__offset)) |
| 803 | |
Hans Wennborg | a4421e0 | 2014-03-12 21:09:05 +0000 | [diff] [blame] | 804 | #ifdef __i386__ |
Michael Kuperstein | e45af54 | 2015-06-30 13:36:19 +0000 | [diff] [blame] | 805 | static __inline__ unsigned char __DEFAULT_FN_ATTRS |
Hans Wennborg | 74ca0c4 | 2014-01-24 00:52:39 +0000 | [diff] [blame] | 806 | __readfsbyte(unsigned long __offset) { |
| 807 | return *__ptr_to_addr_space(257, unsigned char, __offset); |
| 808 | } |
Michael Kuperstein | e45af54 | 2015-06-30 13:36:19 +0000 | [diff] [blame] | 809 | static __inline__ unsigned short __DEFAULT_FN_ATTRS |
Hans Wennborg | 74ca0c4 | 2014-01-24 00:52:39 +0000 | [diff] [blame] | 810 | __readfsword(unsigned long __offset) { |
| 811 | return *__ptr_to_addr_space(257, unsigned short, __offset); |
| 812 | } |
David Majnemer | b2c5720 | 2016-05-27 02:06:14 +0000 | [diff] [blame] | 813 | static __inline__ unsigned __int64 __DEFAULT_FN_ATTRS |
| 814 | __readfsqword(unsigned long __offset) { |
| 815 | return *__ptr_to_addr_space(257, unsigned __int64, __offset); |
| 816 | } |
Hans Wennborg | 74ca0c4 | 2014-01-24 00:52:39 +0000 | [diff] [blame] | 817 | #endif |
Hans Wennborg | a4421e0 | 2014-03-12 21:09:05 +0000 | [diff] [blame] | 818 | #ifdef __x86_64__ |
Michael Kuperstein | e45af54 | 2015-06-30 13:36:19 +0000 | [diff] [blame] | 819 | static __inline__ unsigned char __DEFAULT_FN_ATTRS |
Hans Wennborg | a4421e0 | 2014-03-12 21:09:05 +0000 | [diff] [blame] | 820 | __readgsbyte(unsigned long __offset) { |
| 821 | return *__ptr_to_addr_space(256, unsigned char, __offset); |
| 822 | } |
David Majnemer | b2c5720 | 2016-05-27 02:06:14 +0000 | [diff] [blame] | 823 | static __inline__ unsigned short __DEFAULT_FN_ATTRS |
| 824 | __readgsword(unsigned long __offset) { |
| 825 | return *__ptr_to_addr_space(256, unsigned short, __offset); |
| 826 | } |
Michael Kuperstein | e45af54 | 2015-06-30 13:36:19 +0000 | [diff] [blame] | 827 | static __inline__ unsigned long __DEFAULT_FN_ATTRS |
Hans Wennborg | a4421e0 | 2014-03-12 21:09:05 +0000 | [diff] [blame] | 828 | __readgsdword(unsigned long __offset) { |
| 829 | return *__ptr_to_addr_space(256, unsigned long, __offset); |
| 830 | } |
Michael Kuperstein | e45af54 | 2015-06-30 13:36:19 +0000 | [diff] [blame] | 831 | static __inline__ unsigned __int64 __DEFAULT_FN_ATTRS |
Hans Wennborg | a4421e0 | 2014-03-12 21:09:05 +0000 | [diff] [blame] | 832 | __readgsqword(unsigned long __offset) { |
| 833 | return *__ptr_to_addr_space(256, unsigned __int64, __offset); |
| 834 | } |
Hans Wennborg | a4421e0 | 2014-03-12 21:09:05 +0000 | [diff] [blame] | 835 | #endif |
| 836 | #undef __ptr_to_addr_space |
Hans Wennborg | 74ca0c4 | 2014-01-24 00:52:39 +0000 | [diff] [blame] | 837 | /*----------------------------------------------------------------------------*\ |
Hans Wennborg | d9be72e | 2014-03-12 22:00:32 +0000 | [diff] [blame] | 838 | |* movs, stos |
| 839 | \*----------------------------------------------------------------------------*/ |
Saleem Abdulrasool | 702eefe | 2014-06-25 16:48:40 +0000 | [diff] [blame] | 840 | #if defined(__i386__) || defined(__x86_64__) |
Michael Kuperstein | e45af54 | 2015-06-30 13:36:19 +0000 | [diff] [blame] | 841 | static __inline__ void __DEFAULT_FN_ATTRS |
Hans Wennborg | d9be72e | 2014-03-12 22:00:32 +0000 | [diff] [blame] | 842 | __movsb(unsigned char *__dst, unsigned char const *__src, size_t __n) { |
| 843 | __asm__("rep movsb" : : "D"(__dst), "S"(__src), "c"(__n) |
| 844 | : "%edi", "%esi", "%ecx"); |
| 845 | } |
Michael Kuperstein | e45af54 | 2015-06-30 13:36:19 +0000 | [diff] [blame] | 846 | static __inline__ void __DEFAULT_FN_ATTRS |
Hans Wennborg | d9be72e | 2014-03-12 22:00:32 +0000 | [diff] [blame] | 847 | __movsd(unsigned long *__dst, unsigned long const *__src, size_t __n) { |
| 848 | __asm__("rep movsl" : : "D"(__dst), "S"(__src), "c"(__n) |
| 849 | : "%edi", "%esi", "%ecx"); |
| 850 | } |
Michael Kuperstein | e45af54 | 2015-06-30 13:36:19 +0000 | [diff] [blame] | 851 | static __inline__ void __DEFAULT_FN_ATTRS |
Hans Wennborg | d9be72e | 2014-03-12 22:00:32 +0000 | [diff] [blame] | 852 | __movsw(unsigned short *__dst, unsigned short const *__src, size_t __n) { |
Nico Weber | 1f22a34 | 2015-09-22 00:46:21 +0000 | [diff] [blame] | 853 | __asm__("rep movsw" : : "D"(__dst), "S"(__src), "c"(__n) |
Hans Wennborg | d9be72e | 2014-03-12 22:00:32 +0000 | [diff] [blame] | 854 | : "%edi", "%esi", "%ecx"); |
| 855 | } |
Michael Kuperstein | e45af54 | 2015-06-30 13:36:19 +0000 | [diff] [blame] | 856 | static __inline__ void __DEFAULT_FN_ATTRS |
Hans Wennborg | d9be72e | 2014-03-12 22:00:32 +0000 | [diff] [blame] | 857 | __stosb(unsigned char *__dst, unsigned char __x, size_t __n) { |
| 858 | __asm__("rep stosb" : : "D"(__dst), "a"(__x), "c"(__n) |
| 859 | : "%edi", "%ecx"); |
| 860 | } |
Michael Kuperstein | e45af54 | 2015-06-30 13:36:19 +0000 | [diff] [blame] | 861 | static __inline__ void __DEFAULT_FN_ATTRS |
Hans Wennborg | d9be72e | 2014-03-12 22:00:32 +0000 | [diff] [blame] | 862 | __stosd(unsigned long *__dst, unsigned long __x, size_t __n) { |
| 863 | __asm__("rep stosl" : : "D"(__dst), "a"(__x), "c"(__n) |
| 864 | : "%edi", "%ecx"); |
| 865 | } |
Michael Kuperstein | e45af54 | 2015-06-30 13:36:19 +0000 | [diff] [blame] | 866 | static __inline__ void __DEFAULT_FN_ATTRS |
Hans Wennborg | d9be72e | 2014-03-12 22:00:32 +0000 | [diff] [blame] | 867 | __stosw(unsigned short *__dst, unsigned short __x, size_t __n) { |
Nico Weber | 1f22a34 | 2015-09-22 00:46:21 +0000 | [diff] [blame] | 868 | __asm__("rep stosw" : : "D"(__dst), "a"(__x), "c"(__n) |
Hans Wennborg | d9be72e | 2014-03-12 22:00:32 +0000 | [diff] [blame] | 869 | : "%edi", "%ecx"); |
| 870 | } |
Saleem Abdulrasool | 702eefe | 2014-06-25 16:48:40 +0000 | [diff] [blame] | 871 | #endif |
Hans Wennborg | d9be72e | 2014-03-12 22:00:32 +0000 | [diff] [blame] | 872 | #ifdef __x86_64__ |
Michael Kuperstein | e45af54 | 2015-06-30 13:36:19 +0000 | [diff] [blame] | 873 | static __inline__ void __DEFAULT_FN_ATTRS |
Hans Wennborg | d9be72e | 2014-03-12 22:00:32 +0000 | [diff] [blame] | 874 | __movsq(unsigned long long *__dst, unsigned long long const *__src, size_t __n) { |
| 875 | __asm__("rep movsq" : : "D"(__dst), "S"(__src), "c"(__n) |
| 876 | : "%edi", "%esi", "%ecx"); |
| 877 | } |
Michael Kuperstein | e45af54 | 2015-06-30 13:36:19 +0000 | [diff] [blame] | 878 | static __inline__ void __DEFAULT_FN_ATTRS |
Hans Wennborg | d9be72e | 2014-03-12 22:00:32 +0000 | [diff] [blame] | 879 | __stosq(unsigned __int64 *__dst, unsigned __int64 __x, size_t __n) { |
| 880 | __asm__("rep stosq" : : "D"(__dst), "a"(__x), "c"(__n) |
| 881 | : "%edi", "%ecx"); |
| 882 | } |
| 883 | #endif |
| 884 | |
| 885 | /*----------------------------------------------------------------------------*\ |
Warren Hunt | d6ffae9 | 2013-09-27 23:57:26 +0000 | [diff] [blame] | 886 | |* Misc |
| 887 | \*----------------------------------------------------------------------------*/ |
Michael Kuperstein | e45af54 | 2015-06-30 13:36:19 +0000 | [diff] [blame] | 888 | static __inline__ void * __DEFAULT_FN_ATTRS |
Warren Hunt | d6ffae9 | 2013-09-27 23:57:26 +0000 | [diff] [blame] | 889 | _AddressOfReturnAddress(void) { |
| 890 | return (void*)((char*)__builtin_frame_address(0) + sizeof(void*)); |
| 891 | } |
Michael Kuperstein | e45af54 | 2015-06-30 13:36:19 +0000 | [diff] [blame] | 892 | static __inline__ void * __DEFAULT_FN_ATTRS |
Warren Hunt | d6ffae9 | 2013-09-27 23:57:26 +0000 | [diff] [blame] | 893 | _ReturnAddress(void) { |
| 894 | return __builtin_return_address(0); |
| 895 | } |
Saleem Abdulrasool | 702eefe | 2014-06-25 16:48:40 +0000 | [diff] [blame] | 896 | #if defined(__i386__) || defined(__x86_64__) |
Michael Kuperstein | e45af54 | 2015-06-30 13:36:19 +0000 | [diff] [blame] | 897 | static __inline__ void __DEFAULT_FN_ATTRS |
Hans Wennborg | 854f7d3 | 2014-01-16 23:39:35 +0000 | [diff] [blame] | 898 | __cpuid(int __info[4], int __level) { |
Hans Wennborg | 854f7d3 | 2014-01-16 23:39:35 +0000 | [diff] [blame] | 899 | __asm__ ("cpuid" : "=a"(__info[0]), "=b" (__info[1]), "=c"(__info[2]), "=d"(__info[3]) |
Hans Wennborg | 12fb89e | 2014-01-31 19:44:55 +0000 | [diff] [blame] | 900 | : "a"(__level)); |
| 901 | } |
Michael Kuperstein | e45af54 | 2015-06-30 13:36:19 +0000 | [diff] [blame] | 902 | static __inline__ void __DEFAULT_FN_ATTRS |
Hans Wennborg | 12fb89e | 2014-01-31 19:44:55 +0000 | [diff] [blame] | 903 | __cpuidex(int __info[4], int __level, int __ecx) { |
| 904 | __asm__ ("cpuid" : "=a"(__info[0]), "=b" (__info[1]), "=c"(__info[2]), "=d"(__info[3]) |
| 905 | : "a"(__level), "c"(__ecx)); |
Hans Wennborg | 854f7d3 | 2014-01-16 23:39:35 +0000 | [diff] [blame] | 906 | } |
Michael Kuperstein | e45af54 | 2015-06-30 13:36:19 +0000 | [diff] [blame] | 907 | static __inline__ unsigned __int64 __cdecl __DEFAULT_FN_ATTRS |
Hans Wennborg | 854f7d3 | 2014-01-16 23:39:35 +0000 | [diff] [blame] | 908 | _xgetbv(unsigned int __xcr_no) { |
| 909 | unsigned int __eax, __edx; |
| 910 | __asm__ ("xgetbv" : "=a" (__eax), "=d" (__edx) : "c" (__xcr_no)); |
| 911 | return ((unsigned __int64)__edx << 32) | __eax; |
| 912 | } |
Michael Kuperstein | e45af54 | 2015-06-30 13:36:19 +0000 | [diff] [blame] | 913 | static __inline__ void __DEFAULT_FN_ATTRS |
Hans Wennborg | 740a4d6 | 2014-01-28 22:55:01 +0000 | [diff] [blame] | 914 | __halt(void) { |
| 915 | __asm__ volatile ("hlt"); |
| 916 | } |
Saleem Abdulrasool | 702eefe | 2014-06-25 16:48:40 +0000 | [diff] [blame] | 917 | #endif |
Warren Hunt | d6ffae9 | 2013-09-27 23:57:26 +0000 | [diff] [blame] | 918 | |
Reid Kleckner | 592dc61 | 2014-04-08 00:28:22 +0000 | [diff] [blame] | 919 | /*----------------------------------------------------------------------------*\ |
| 920 | |* Privileged intrinsics |
| 921 | \*----------------------------------------------------------------------------*/ |
Saleem Abdulrasool | 702eefe | 2014-06-25 16:48:40 +0000 | [diff] [blame] | 922 | #if defined(__i386__) || defined(__x86_64__) |
Michael Kuperstein | e45af54 | 2015-06-30 13:36:19 +0000 | [diff] [blame] | 923 | static __inline__ unsigned __int64 __DEFAULT_FN_ATTRS |
Reid Kleckner | 592dc61 | 2014-04-08 00:28:22 +0000 | [diff] [blame] | 924 | __readmsr(unsigned long __register) { |
| 925 | // Loads the contents of a 64-bit model specific register (MSR) specified in |
| 926 | // the ECX register into registers EDX:EAX. The EDX register is loaded with |
| 927 | // the high-order 32 bits of the MSR and the EAX register is loaded with the |
| 928 | // low-order 32 bits. If less than 64 bits are implemented in the MSR being |
| 929 | // read, the values returned to EDX:EAX in unimplemented bit locations are |
| 930 | // undefined. |
| 931 | unsigned long __edx; |
| 932 | unsigned long __eax; |
Reid Kleckner | 6df5254 | 2014-04-08 17:49:16 +0000 | [diff] [blame] | 933 | __asm__ ("rdmsr" : "=d"(__edx), "=a"(__eax) : "c"(__register)); |
Reid Kleckner | 592dc61 | 2014-04-08 00:28:22 +0000 | [diff] [blame] | 934 | return (((unsigned __int64)__edx) << 32) | (unsigned __int64)__eax; |
| 935 | } |
| 936 | |
Michael Kuperstein | e45af54 | 2015-06-30 13:36:19 +0000 | [diff] [blame] | 937 | static __inline__ unsigned long __DEFAULT_FN_ATTRS |
Reid Kleckner | 592dc61 | 2014-04-08 00:28:22 +0000 | [diff] [blame] | 938 | __readcr3(void) { |
Reid Kleckner | 6df5254 | 2014-04-08 17:49:16 +0000 | [diff] [blame] | 939 | unsigned long __cr3_val; |
| 940 | __asm__ __volatile__ ("mov %%cr3, %0" : "=q"(__cr3_val) : : "memory"); |
| 941 | return __cr3_val; |
Reid Kleckner | 592dc61 | 2014-04-08 00:28:22 +0000 | [diff] [blame] | 942 | } |
| 943 | |
Michael Kuperstein | e45af54 | 2015-06-30 13:36:19 +0000 | [diff] [blame] | 944 | static __inline__ void __DEFAULT_FN_ATTRS |
Reid Kleckner | 6df5254 | 2014-04-08 17:49:16 +0000 | [diff] [blame] | 945 | __writecr3(unsigned int __cr3_val) { |
| 946 | __asm__ ("mov %0, %%cr3" : : "q"(__cr3_val) : "memory"); |
Reid Kleckner | 592dc61 | 2014-04-08 00:28:22 +0000 | [diff] [blame] | 947 | } |
Saleem Abdulrasool | 702eefe | 2014-06-25 16:48:40 +0000 | [diff] [blame] | 948 | #endif |
Reid Kleckner | 592dc61 | 2014-04-08 00:28:22 +0000 | [diff] [blame] | 949 | |
Eric Christopher | cc87253 | 2013-09-18 22:40:18 +0000 | [diff] [blame] | 950 | #ifdef __cplusplus |
Eric Christopher | 0db88a7 | 2013-09-18 22:24:01 +0000 | [diff] [blame] | 951 | } |
Eric Christopher | d1428bf | 2013-08-31 00:22:48 +0000 | [diff] [blame] | 952 | #endif |
Reid Kleckner | f0e2322 | 2013-09-19 00:19:53 +0000 | [diff] [blame] | 953 | |
Michael Kuperstein | e45af54 | 2015-06-30 13:36:19 +0000 | [diff] [blame] | 954 | #undef __DEFAULT_FN_ATTRS |
Eric Christopher | 5a9bec1 | 2015-06-15 23:20:35 +0000 | [diff] [blame] | 955 | |
Reid Kleckner | f0e2322 | 2013-09-19 00:19:53 +0000 | [diff] [blame] | 956 | #endif /* __INTRIN_H */ |
| 957 | #endif /* _MSC_VER */ |