Eric Christopher | d1428bf | 2013-08-31 00:22:48 +0000 | [diff] [blame] | 1 | /* ===-------- 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. */ |
| 33 | #include <x86intrin.h> |
| 34 | |
Eric Christopher | 0db88a7 | 2013-09-18 22:24:01 +0000 | [diff] [blame] | 35 | #ifdef __cplusplus |
| 36 | extern "C" { |
| 37 | #endif |
| 38 | |
Eric Christopher | d1428bf | 2013-08-31 00:22:48 +0000 | [diff] [blame] | 39 | /* And the random ones that aren't in those files. */ |
| 40 | __m64 _m_from_float(float); |
| 41 | __m64 _m_from_int(int _l); |
| 42 | void _m_prefetch(void *); |
| 43 | float _m_to_float(__m64); |
| 44 | int _m_to_int(__m64 _M); |
| 45 | |
| 46 | /* Other assorted instruction intrinsics. */ |
| 47 | void __addfsbyte(unsigned long, unsigned char); |
| 48 | void __addfsdword(unsigned long, unsigned long); |
| 49 | void __addfsword(unsigned long, unsigned short); |
| 50 | void __code_seg(const char *); |
Hans Wennborg | 854f7d3 | 2014-01-16 23:39:35 +0000 | [diff] [blame^] | 51 | static __inline__ |
Eric Christopher | d1428bf | 2013-08-31 00:22:48 +0000 | [diff] [blame] | 52 | void __cpuid(int[4], int); |
| 53 | void __cpuidex(int[4], int, int); |
Eric Christopher | 0db88a7 | 2013-09-18 22:24:01 +0000 | [diff] [blame] | 54 | void __debugbreak(void); |
Eric Christopher | d1428bf | 2013-08-31 00:22:48 +0000 | [diff] [blame] | 55 | __int64 __emul(int, int); |
| 56 | unsigned __int64 __emulu(unsigned int, unsigned int); |
Eric Christopher | 0db88a7 | 2013-09-18 22:24:01 +0000 | [diff] [blame] | 57 | void __cdecl __fastfail(unsigned int); |
Eric Christopher | d1428bf | 2013-08-31 00:22:48 +0000 | [diff] [blame] | 58 | unsigned int __getcallerseflags(void); |
| 59 | void __halt(void); |
| 60 | unsigned char __inbyte(unsigned short); |
| 61 | void __inbytestring(unsigned short, unsigned char *, unsigned long); |
| 62 | void __incfsbyte(unsigned long); |
| 63 | void __incfsdword(unsigned long); |
| 64 | void __incfsword(unsigned long); |
| 65 | unsigned long __indword(unsigned short); |
| 66 | void __indwordstring(unsigned short, unsigned long *, unsigned long); |
| 67 | void __int2c(void); |
| 68 | void __invlpg(void *); |
| 69 | unsigned short __inword(unsigned short); |
| 70 | void __inwordstring(unsigned short, unsigned short *, unsigned long); |
| 71 | void __lidt(void *); |
| 72 | unsigned __int64 __ll_lshift(unsigned __int64, int); |
| 73 | __int64 __ll_rshift(__int64, int); |
| 74 | void __llwpcb(void *); |
| 75 | unsigned char __lwpins32(unsigned int, unsigned int, unsigned int); |
| 76 | void __lwpval32(unsigned int, unsigned int, unsigned int); |
| 77 | unsigned int __lzcnt(unsigned int); |
| 78 | unsigned short __lzcnt16(unsigned short); |
| 79 | void __movsb(unsigned char *, unsigned char const *, size_t); |
| 80 | void __movsd(unsigned long *, unsigned long const *, size_t); |
| 81 | void __movsw(unsigned short *, unsigned short const *, size_t); |
| 82 | void __nop(void); |
| 83 | void __nvreg_restore_fence(void); |
| 84 | void __nvreg_save_fence(void); |
| 85 | void __outbyte(unsigned short, unsigned char); |
| 86 | void __outbytestring(unsigned short, unsigned char *, unsigned long); |
| 87 | void __outdword(unsigned short, unsigned long); |
| 88 | void __outdwordstring(unsigned short, unsigned long *, unsigned long); |
| 89 | void __outword(unsigned short, unsigned short); |
| 90 | void __outwordstring(unsigned short, unsigned short *, unsigned long); |
Warren Hunt | d6ffae9 | 2013-09-27 23:57:26 +0000 | [diff] [blame] | 91 | static __inline__ |
Eric Christopher | d1428bf | 2013-08-31 00:22:48 +0000 | [diff] [blame] | 92 | unsigned int __popcnt(unsigned int); |
Warren Hunt | d6ffae9 | 2013-09-27 23:57:26 +0000 | [diff] [blame] | 93 | static __inline__ |
Eric Christopher | d1428bf | 2013-08-31 00:22:48 +0000 | [diff] [blame] | 94 | unsigned short __popcnt16(unsigned short); |
| 95 | unsigned __int64 __rdtsc(void); |
| 96 | unsigned __int64 __rdtscp(unsigned int *); |
| 97 | unsigned long __readcr0(void); |
| 98 | unsigned long __readcr2(void); |
| 99 | unsigned long __readcr3(void); |
| 100 | unsigned long __readcr5(void); |
| 101 | unsigned long __readcr8(void); |
| 102 | unsigned int __readdr(unsigned int); |
| 103 | unsigned int __readeflags(void); |
| 104 | unsigned char __readfsbyte(unsigned long); |
| 105 | unsigned long __readfsdword(unsigned long); |
| 106 | unsigned __int64 __readfsqword(unsigned long); |
| 107 | unsigned short __readfsword(unsigned long); |
| 108 | unsigned __int64 __readmsr(unsigned long); |
| 109 | unsigned __int64 __readpmc(unsigned long); |
| 110 | unsigned long __segmentlimit(unsigned long); |
| 111 | void __sidt(void *); |
| 112 | void *__slwpcb(void); |
| 113 | void __stosb(unsigned char *, unsigned char, size_t); |
| 114 | void __stosd(unsigned long *, unsigned long, size_t); |
| 115 | void __stosw(unsigned short *, unsigned short, size_t); |
| 116 | void __svm_clgi(void); |
| 117 | void __svm_invlpga(void *, int); |
| 118 | void __svm_skinit(int); |
| 119 | void __svm_stgi(void); |
| 120 | void __svm_vmload(size_t); |
| 121 | void __svm_vmrun(size_t); |
| 122 | void __svm_vmsave(size_t); |
| 123 | void __ud2(void); |
| 124 | unsigned __int64 __ull_rshift(unsigned __int64, int); |
| 125 | void __vmx_off(void); |
| 126 | void __vmx_vmptrst(unsigned __int64 *); |
| 127 | void __wbinvd(void); |
| 128 | void __writecr0(unsigned int); |
| 129 | void __writecr3(unsigned int); |
| 130 | void __writecr4(unsigned int); |
| 131 | void __writecr8(unsigned int); |
| 132 | void __writedr(unsigned int, unsigned int); |
| 133 | void __writeeflags(unsigned int); |
| 134 | void __writefsbyte(unsigned long, unsigned char); |
| 135 | void __writefsdword(unsigned long, unsigned long); |
| 136 | void __writefsqword(unsigned long, unsigned __int64); |
| 137 | void __writefsword(unsigned long, unsigned short); |
| 138 | void __writemsr(unsigned long, unsigned __int64); |
Warren Hunt | d6ffae9 | 2013-09-27 23:57:26 +0000 | [diff] [blame] | 139 | static __inline__ |
Eric Christopher | d1428bf | 2013-08-31 00:22:48 +0000 | [diff] [blame] | 140 | void *_AddressOfReturnAddress(void); |
| 141 | unsigned int _andn_u32(unsigned int, unsigned int); |
| 142 | unsigned int _bextr_u32(unsigned int, unsigned int, unsigned int); |
| 143 | unsigned int _bextr_u32(unsigned int, unsigned int, unsigned int); |
| 144 | unsigned int _bextri_u32(unsigned int, unsigned int); |
Warren Hunt | d6ffae9 | 2013-09-27 23:57:26 +0000 | [diff] [blame] | 145 | static __inline__ |
Eric Christopher | d1428bf | 2013-08-31 00:22:48 +0000 | [diff] [blame] | 146 | unsigned char _BitScanForward(unsigned long *_Index, unsigned long _Mask); |
Warren Hunt | d6ffae9 | 2013-09-27 23:57:26 +0000 | [diff] [blame] | 147 | static __inline__ |
Eric Christopher | d1428bf | 2013-08-31 00:22:48 +0000 | [diff] [blame] | 148 | unsigned char _BitScanReverse(unsigned long *_Index, unsigned long _Mask); |
Warren Hunt | d6ffae9 | 2013-09-27 23:57:26 +0000 | [diff] [blame] | 149 | static __inline__ |
Eric Christopher | d1428bf | 2013-08-31 00:22:48 +0000 | [diff] [blame] | 150 | unsigned char _bittest(long const *, long); |
Warren Hunt | d6ffae9 | 2013-09-27 23:57:26 +0000 | [diff] [blame] | 151 | static __inline__ |
Eric Christopher | d1428bf | 2013-08-31 00:22:48 +0000 | [diff] [blame] | 152 | unsigned char _bittestandcomplement(long *, long); |
Warren Hunt | d6ffae9 | 2013-09-27 23:57:26 +0000 | [diff] [blame] | 153 | static __inline__ |
Eric Christopher | d1428bf | 2013-08-31 00:22:48 +0000 | [diff] [blame] | 154 | unsigned char _bittestandreset(long *, long); |
Warren Hunt | d6ffae9 | 2013-09-27 23:57:26 +0000 | [diff] [blame] | 155 | static __inline__ |
Eric Christopher | d1428bf | 2013-08-31 00:22:48 +0000 | [diff] [blame] | 156 | unsigned char _bittestandset(long *, long); |
| 157 | unsigned int _blcfill_u32(unsigned int); |
| 158 | unsigned int _blci_u32(unsigned int); |
| 159 | unsigned int _blcic_u32(unsigned int); |
| 160 | unsigned int _blcmsk_u32(unsigned int); |
| 161 | unsigned int _blcs_u32(unsigned int); |
| 162 | unsigned int _blsfill_u32(unsigned int); |
| 163 | unsigned int _blsi_u32(unsigned int); |
| 164 | unsigned int _blsic_u32(unsigned int); |
| 165 | unsigned int _blsmsk_u32(unsigned int); |
| 166 | unsigned int _blsmsk_u32(unsigned int); |
| 167 | unsigned int _blsr_u32(unsigned int); |
| 168 | unsigned int _blsr_u32(unsigned int); |
| 169 | unsigned __int64 __cdecl _byteswap_uint64(unsigned __int64); |
| 170 | unsigned long __cdecl _byteswap_ulong(unsigned long); |
| 171 | unsigned short __cdecl _byteswap_ushort(unsigned short); |
| 172 | unsigned _bzhi_u32(unsigned int, unsigned int); |
| 173 | void __cdecl _disable(void); |
| 174 | void __cdecl _enable(void); |
| 175 | void __cdecl _fxrstor(void const *); |
| 176 | void __cdecl _fxsave(void *); |
| 177 | long _InterlockedAddLargeStatistic(__int64 volatile *_Addend, long _Value); |
Warren Hunt | d6ffae9 | 2013-09-27 23:57:26 +0000 | [diff] [blame] | 178 | static __inline__ |
Eric Christopher | d1428bf | 2013-08-31 00:22:48 +0000 | [diff] [blame] | 179 | long _InterlockedAnd(long volatile *_Value, long _Mask); |
Warren Hunt | d6ffae9 | 2013-09-27 23:57:26 +0000 | [diff] [blame] | 180 | static __inline__ |
Eric Christopher | d1428bf | 2013-08-31 00:22:48 +0000 | [diff] [blame] | 181 | short _InterlockedAnd16(short volatile *_Value, short _Mask); |
Warren Hunt | d6ffae9 | 2013-09-27 23:57:26 +0000 | [diff] [blame] | 182 | static __inline__ |
Eric Christopher | d1428bf | 2013-08-31 00:22:48 +0000 | [diff] [blame] | 183 | char _InterlockedAnd8(char volatile *_Value, char _Mask); |
| 184 | unsigned char _interlockedbittestandreset(long volatile *, long); |
| 185 | unsigned char _interlockedbittestandset(long volatile *, long); |
Warren Hunt | d6ffae9 | 2013-09-27 23:57:26 +0000 | [diff] [blame] | 186 | static __inline__ |
Eric Christopher | d1428bf | 2013-08-31 00:22:48 +0000 | [diff] [blame] | 187 | long __cdecl _InterlockedCompareExchange(long volatile *_Destination, |
| 188 | long _Exchange, long _Comparand); |
| 189 | long _InterlockedCompareExchange_HLEAcquire(long volatile *, long, long); |
| 190 | long _InterlockedCompareExchange_HLERelease(long volatile *, long, long); |
Warren Hunt | d6ffae9 | 2013-09-27 23:57:26 +0000 | [diff] [blame] | 191 | static __inline__ |
Eric Christopher | d1428bf | 2013-08-31 00:22:48 +0000 | [diff] [blame] | 192 | short _InterlockedCompareExchange16(short volatile *_Destination, |
| 193 | short _Exchange, short _Comparand); |
Warren Hunt | d6ffae9 | 2013-09-27 23:57:26 +0000 | [diff] [blame] | 194 | static __inline__ |
Eric Christopher | d1428bf | 2013-08-31 00:22:48 +0000 | [diff] [blame] | 195 | __int64 _InterlockedCompareExchange64(__int64 volatile *_Destination, |
| 196 | __int64 _Exchange, __int64 _Comparand); |
| 197 | __int64 _InterlockedcompareExchange64_HLEAcquire(__int64 volatile *, __int64, |
| 198 | __int64); |
| 199 | __int64 _InterlockedCompareExchange64_HLERelease(__int64 volatile *, __int64, |
| 200 | __int64); |
Warren Hunt | d6ffae9 | 2013-09-27 23:57:26 +0000 | [diff] [blame] | 201 | static __inline__ |
Eric Christopher | d1428bf | 2013-08-31 00:22:48 +0000 | [diff] [blame] | 202 | char _InterlockedCompareExchange8(char volatile *_Destination, char _Exchange, |
| 203 | char _Comparand); |
| 204 | void *_InterlockedCompareExchangePointer_HLEAcquire(void *volatile *, void *, |
| 205 | void *); |
| 206 | void *_InterlockedCompareExchangePointer_HLERelease(void *volatile *, void *, |
| 207 | void *); |
Warren Hunt | d6ffae9 | 2013-09-27 23:57:26 +0000 | [diff] [blame] | 208 | static __inline__ |
Eric Christopher | d1428bf | 2013-08-31 00:22:48 +0000 | [diff] [blame] | 209 | long __cdecl _InterlockedDecrement(long volatile *_Addend); |
Warren Hunt | d6ffae9 | 2013-09-27 23:57:26 +0000 | [diff] [blame] | 210 | static __inline__ |
Eric Christopher | d1428bf | 2013-08-31 00:22:48 +0000 | [diff] [blame] | 211 | short _InterlockedDecrement16(short volatile *_Addend); |
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 _InterlockedExchange(long volatile *_Target, long _Value); |
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 _InterlockedExchange16(short volatile *_Target, short _Value); |
Warren Hunt | d6ffae9 | 2013-09-27 23:57:26 +0000 | [diff] [blame] | 216 | static __inline__ |
Eric Christopher | d1428bf | 2013-08-31 00:22:48 +0000 | [diff] [blame] | 217 | char _InterlockedExchange8(char volatile *_Target, char _Value); |
Warren Hunt | d6ffae9 | 2013-09-27 23:57:26 +0000 | [diff] [blame] | 218 | static __inline__ |
Eric Christopher | d1428bf | 2013-08-31 00:22:48 +0000 | [diff] [blame] | 219 | long __cdecl _InterlockedExchangeAdd(long volatile *_Addend, long _Value); |
| 220 | long _InterlockedExchangeAdd_HLEAcquire(long volatile *, long); |
| 221 | long _InterlockedExchangeAdd_HLERelease(long volatile *, long); |
Warren Hunt | d6ffae9 | 2013-09-27 23:57:26 +0000 | [diff] [blame] | 222 | static __inline__ |
Eric Christopher | d1428bf | 2013-08-31 00:22:48 +0000 | [diff] [blame] | 223 | char _InterlockedExchangeAdd8(char volatile *_Addend, char _Value); |
Warren Hunt | d6ffae9 | 2013-09-27 23:57:26 +0000 | [diff] [blame] | 224 | static __inline__ |
Eric Christopher | d1428bf | 2013-08-31 00:22:48 +0000 | [diff] [blame] | 225 | long __cdecl _InterlockedIncrement(long volatile *_Addend); |
Warren Hunt | d6ffae9 | 2013-09-27 23:57:26 +0000 | [diff] [blame] | 226 | static __inline__ |
Eric Christopher | d1428bf | 2013-08-31 00:22:48 +0000 | [diff] [blame] | 227 | short _InterlockedIncrement16(short volatile *_Addend); |
Warren Hunt | d6ffae9 | 2013-09-27 23:57:26 +0000 | [diff] [blame] | 228 | static __inline__ |
Eric Christopher | d1428bf | 2013-08-31 00:22:48 +0000 | [diff] [blame] | 229 | long _InterlockedOr(long volatile *_Value, long _Mask); |
Warren Hunt | d6ffae9 | 2013-09-27 23:57:26 +0000 | [diff] [blame] | 230 | static __inline__ |
Eric Christopher | d1428bf | 2013-08-31 00:22:48 +0000 | [diff] [blame] | 231 | short _InterlockedOr16(short volatile *_Value, short _Mask); |
Warren Hunt | d6ffae9 | 2013-09-27 23:57:26 +0000 | [diff] [blame] | 232 | static __inline__ |
Eric Christopher | d1428bf | 2013-08-31 00:22:48 +0000 | [diff] [blame] | 233 | char _InterlockedOr8(char volatile *_Value, char _Mask); |
Warren Hunt | d6ffae9 | 2013-09-27 23:57:26 +0000 | [diff] [blame] | 234 | static __inline__ |
Eric Christopher | d1428bf | 2013-08-31 00:22:48 +0000 | [diff] [blame] | 235 | long _InterlockedXor(long volatile *_Value, long _Mask); |
Warren Hunt | d6ffae9 | 2013-09-27 23:57:26 +0000 | [diff] [blame] | 236 | static __inline__ |
Eric Christopher | d1428bf | 2013-08-31 00:22:48 +0000 | [diff] [blame] | 237 | short _InterlockedXor16(short volatile *_Value, short _Mask); |
Warren Hunt | d6ffae9 | 2013-09-27 23:57:26 +0000 | [diff] [blame] | 238 | static __inline__ |
Eric Christopher | d1428bf | 2013-08-31 00:22:48 +0000 | [diff] [blame] | 239 | char _InterlockedXor8(char volatile *_Value, char _Mask); |
| 240 | void __cdecl _invpcid(unsigned int, void *); |
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 | unsigned long __cdecl _lrotl(unsigned long, int); |
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 | unsigned long __cdecl _lrotr(unsigned long, int); |
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 | unsigned int _lzcnt_u32(unsigned int); |
Reid Kleckner | 00d33a5 | 2013-10-17 01:29:48 +0000 | [diff] [blame] | 247 | static __inline__ |
Eric Christopher | d1428bf | 2013-08-31 00:22:48 +0000 | [diff] [blame] | 248 | void _ReadBarrier(void); |
Reid Kleckner | 00d33a5 | 2013-10-17 01:29:48 +0000 | [diff] [blame] | 249 | static __inline__ |
Eric Christopher | d1428bf | 2013-08-31 00:22:48 +0000 | [diff] [blame] | 250 | void _ReadWriteBarrier(void); |
Warren Hunt | d6ffae9 | 2013-09-27 23:57:26 +0000 | [diff] [blame] | 251 | static __inline__ |
Eric Christopher | d1428bf | 2013-08-31 00:22:48 +0000 | [diff] [blame] | 252 | void *_ReturnAddress(void); |
| 253 | unsigned int _rorx_u32(unsigned int, const unsigned int); |
| 254 | int __cdecl _rdrand16_step(unsigned short *); |
| 255 | int __cdecl _rdrand32_step(unsigned int *); |
Warren Hunt | d6ffae9 | 2013-09-27 23:57:26 +0000 | [diff] [blame] | 256 | static __inline__ |
Eric Christopher | fb4b433 | 2013-08-31 00:27:38 +0000 | [diff] [blame] | 257 | unsigned int __cdecl _rotl(unsigned int _Value, int _Shift); |
Warren Hunt | d6ffae9 | 2013-09-27 23:57:26 +0000 | [diff] [blame] | 258 | static __inline__ |
Eric Christopher | d1428bf | 2013-08-31 00:22:48 +0000 | [diff] [blame] | 259 | unsigned short _rotl16(unsigned short _Value, unsigned char _Shift); |
Warren Hunt | d6ffae9 | 2013-09-27 23:57:26 +0000 | [diff] [blame] | 260 | static __inline__ |
Eric Christopher | d1428bf | 2013-08-31 00:22:48 +0000 | [diff] [blame] | 261 | unsigned __int64 __cdecl _rotl64(unsigned __int64 _Value, int _Shift); |
Warren Hunt | d6ffae9 | 2013-09-27 23:57:26 +0000 | [diff] [blame] | 262 | static __inline__ |
Eric Christopher | d1428bf | 2013-08-31 00:22:48 +0000 | [diff] [blame] | 263 | unsigned char _rotl8(unsigned char _Value, unsigned char _Shift); |
Warren Hunt | d6ffae9 | 2013-09-27 23:57:26 +0000 | [diff] [blame] | 264 | static __inline__ |
Eric Christopher | d1428bf | 2013-08-31 00:22:48 +0000 | [diff] [blame] | 265 | unsigned int __cdecl _rotr(unsigned int _Value, int _Shift); |
Warren Hunt | d6ffae9 | 2013-09-27 23:57:26 +0000 | [diff] [blame] | 266 | static __inline__ |
Eric Christopher | d1428bf | 2013-08-31 00:22:48 +0000 | [diff] [blame] | 267 | unsigned short _rotr16(unsigned short _Value, unsigned char _Shift); |
Warren Hunt | d6ffae9 | 2013-09-27 23:57:26 +0000 | [diff] [blame] | 268 | static __inline__ |
Eric Christopher | d1428bf | 2013-08-31 00:22:48 +0000 | [diff] [blame] | 269 | unsigned __int64 __cdecl _rotr64(unsigned __int64 _Value, int _Shift); |
Warren Hunt | d6ffae9 | 2013-09-27 23:57:26 +0000 | [diff] [blame] | 270 | static __inline__ |
Eric Christopher | d1428bf | 2013-08-31 00:22:48 +0000 | [diff] [blame] | 271 | unsigned char _rotr8(unsigned char _Value, unsigned char _Shift); |
| 272 | int _sarx_i32(int, unsigned int); |
| 273 | |
| 274 | /* FIXME: Need definition for jmp_buf. |
| 275 | int __cdecl _setjmp(jmp_buf); */ |
| 276 | |
| 277 | unsigned int _shlx_u32(unsigned int, unsigned int); |
| 278 | unsigned int _shrx_u32(unsigned int, unsigned int); |
| 279 | void _Store_HLERelease(long volatile *, long); |
| 280 | void _Store64_HLERelease(__int64 volatile *, __int64); |
| 281 | void _StorePointer_HLERelease(void *volatile *, void *); |
| 282 | unsigned int _t1mskc_u32(unsigned int); |
| 283 | unsigned int _tzcnt_u32(unsigned int); |
| 284 | unsigned int _tzcnt_u32(unsigned int); |
| 285 | unsigned int _tzmsk_u32(unsigned int); |
Reid Kleckner | 00d33a5 | 2013-10-17 01:29:48 +0000 | [diff] [blame] | 286 | static __inline__ |
Eric Christopher | d1428bf | 2013-08-31 00:22:48 +0000 | [diff] [blame] | 287 | void _WriteBarrier(void); |
| 288 | void _xabort(const unsigned int imm); |
| 289 | unsigned __int32 xbegin(void); |
| 290 | void _xend(void); |
Hans Wennborg | 854f7d3 | 2014-01-16 23:39:35 +0000 | [diff] [blame^] | 291 | static __inline__ |
Eric Christopher | d1428bf | 2013-08-31 00:22:48 +0000 | [diff] [blame] | 292 | unsigned __int64 __cdecl _xgetbv(unsigned int); |
| 293 | void __cdecl _xrstor(void const *, unsigned __int64); |
| 294 | void __cdecl _xsave(void *, unsigned __int64); |
| 295 | void __cdecl _xsaveopt(void *, unsigned __int64); |
| 296 | void __cdecl _xsetbv(unsigned int, unsigned __int64); |
| 297 | unsigned char _xtest(void); |
| 298 | |
| 299 | /* These additional intrinsics are turned on in x64/amd64/x86_64 mode. */ |
Warren Hunt | 3f98794 | 2013-09-30 21:08:05 +0000 | [diff] [blame] | 300 | #ifdef __x86_64__ |
Eric Christopher | d1428bf | 2013-08-31 00:22:48 +0000 | [diff] [blame] | 301 | void __addgsbyte(unsigned long, unsigned char); |
| 302 | void __addgsdword(unsigned long, unsigned long); |
| 303 | void __addgsqword(unsigned long, unsigned __int64); |
| 304 | void __addgsword(unsigned long, unsigned short); |
| 305 | void __faststorefence(void); |
| 306 | void __incgsbyte(unsigned long); |
| 307 | void __incgsdword(unsigned long); |
| 308 | void __incgsqword(unsigned long); |
| 309 | void __incgsword(unsigned long); |
| 310 | unsigned __int64 __popcnt64(unsigned __int64); |
| 311 | unsigned __int64 __shiftleft128(unsigned __int64 _LowPart, |
| 312 | unsigned __int64 _HighPart, |
| 313 | unsigned char _Shift); |
| 314 | unsigned __int64 __shiftright128(unsigned __int64 _LowPart, |
| 315 | unsigned __int64 _HighPart, |
| 316 | unsigned char _Shift); |
| 317 | void __stosq(unsigned __int64 *, unsigned __int64, size_t); |
| 318 | unsigned __int64 _andn_u64(unsigned __int64, unsigned __int64); |
| 319 | unsigned __int64 _bextr_u64(unsigned __int64, unsigned int, unsigned int); |
| 320 | unsigned __int64 _bextri_u64(unsigned __int64, unsigned int); |
Warren Hunt | d6ffae9 | 2013-09-27 23:57:26 +0000 | [diff] [blame] | 321 | static __inline__ |
Eric Christopher | d1428bf | 2013-08-31 00:22:48 +0000 | [diff] [blame] | 322 | unsigned char _BitScanForward64(unsigned long *_Index, unsigned __int64 _Mask); |
Warren Hunt | d6ffae9 | 2013-09-27 23:57:26 +0000 | [diff] [blame] | 323 | static __inline__ |
Eric Christopher | d1428bf | 2013-08-31 00:22:48 +0000 | [diff] [blame] | 324 | unsigned char _BitScanReverse64(unsigned long *_Index, unsigned __int64 _Mask); |
Warren Hunt | d6ffae9 | 2013-09-27 23:57:26 +0000 | [diff] [blame] | 325 | static __inline__ |
Eric Christopher | d1428bf | 2013-08-31 00:22:48 +0000 | [diff] [blame] | 326 | unsigned char _bittest64(__int64 const *, __int64); |
Warren Hunt | d6ffae9 | 2013-09-27 23:57:26 +0000 | [diff] [blame] | 327 | static __inline__ |
Eric Christopher | d1428bf | 2013-08-31 00:22:48 +0000 | [diff] [blame] | 328 | unsigned char _bittestandcomplement64(__int64 *, __int64); |
Warren Hunt | d6ffae9 | 2013-09-27 23:57:26 +0000 | [diff] [blame] | 329 | static __inline__ |
Eric Christopher | d1428bf | 2013-08-31 00:22:48 +0000 | [diff] [blame] | 330 | unsigned char _bittestandreset64(__int64 *, __int64); |
Warren Hunt | d6ffae9 | 2013-09-27 23:57:26 +0000 | [diff] [blame] | 331 | static __inline__ |
Eric Christopher | d1428bf | 2013-08-31 00:22:48 +0000 | [diff] [blame] | 332 | unsigned char _bittestandset64(__int64 *, __int64); |
| 333 | unsigned __int64 _blcfill_u64(unsigned __int64); |
| 334 | unsigned __int64 _blci_u64(unsigned __int64); |
| 335 | unsigned __int64 _blcic_u64(unsigned __int64); |
| 336 | unsigned __int64 _blcmsk_u64(unsigned __int64); |
| 337 | unsigned __int64 _blcs_u64(unsigned __int64); |
| 338 | unsigned __int64 _blsfill_u64(unsigned __int64); |
| 339 | unsigned __int64 _blsi_u64(unsigned __int64); |
| 340 | unsigned __int64 _blsic_u64(unsigned __int64); |
| 341 | unsigned __int64 _blmsk_u64(unsigned __int64); |
| 342 | unsigned __int64 _blsr_u64(unsigned __int64); |
| 343 | unsigned __int64 __cdecl _byteswap_uint64(unsigned __int64); |
| 344 | unsigned __int64 _bzhi_u64(unsigned __int64, unsigned int); |
| 345 | void __cdecl _fxrstor64(void const *); |
| 346 | void __cdecl _fxsave64(void *); |
| 347 | long _InterlockedAnd_np(long volatile *_Value, long _Mask); |
| 348 | short _InterlockedAnd16_np(short volatile *_Value, short _Mask); |
| 349 | __int64 _InterlockedAnd64_np(__int64 volatile *_Value, __int64 _Mask); |
| 350 | char _InterlockedAnd8_np(char volatile *_Value, char _Mask); |
| 351 | unsigned char _interlockedbittestandreset64(__int64 volatile *, __int64); |
| 352 | unsigned char _interlockedbittestandset64(__int64 volatile *, __int64); |
| 353 | long _InterlockedCompareExchange_np(long volatile *_Destination, long _Exchange, |
| 354 | long _Comparand); |
| 355 | unsigned char _InterlockedCompareExchange128(__int64 volatile *_Destination, |
| 356 | __int64 _ExchangeHigh, |
| 357 | __int64 _ExchangeLow, |
| 358 | __int64 *_CompareandResult); |
| 359 | unsigned char _InterlockedCompareExchange128_np(__int64 volatile *_Destination, |
| 360 | __int64 _ExchangeHigh, |
| 361 | __int64 _ExchangeLow, |
| 362 | __int64 *_ComparandResult); |
| 363 | short _InterlockedCompareExchange16_np(short volatile *_Destination, |
| 364 | short _Exchange, short _Comparand); |
| 365 | __int64 _InterlockedCompareExchange64_np(__int64 volatile *_Destination, |
| 366 | __int64 _Exchange, __int64 _Comparand); |
| 367 | void *_InterlockedCompareExchangePointer_np(void *volatile *_Destination, |
| 368 | void *_Exchange, void *_Comparand); |
| 369 | long _InterlockedOr_np(long volatile *_Value, long _Mask); |
| 370 | short _InterlockedOr16_np(short volatile *_Value, short _Mask); |
Warren Hunt | 41a993f | 2013-09-28 00:15:41 +0000 | [diff] [blame] | 371 | __int64 _InterlockedOr64_np(__int64 volatile *_Value, __int64 _Mask); |
Eric Christopher | d1428bf | 2013-08-31 00:22:48 +0000 | [diff] [blame] | 372 | char _InterlockedOr8_np(char volatile *_Value, char _Mask); |
| 373 | long _InterlockedXor_np(long volatile *_Value, long _Mask); |
| 374 | short _InterlockedXor16_np(short volatile *_Value, short _Mask); |
| 375 | __int64 _InterlockedXor64_np(__int64 volatile *_Value, __int64 _Mask); |
| 376 | char _InterlockedXor8_np(char volatile *_Value, char _Mask); |
| 377 | unsigned __int64 _lzcnt_u64(unsigned __int64); |
| 378 | __int64 _mul128(__int64 _Multiplier, __int64 _Multiplicand, |
| 379 | __int64 *_HighProduct); |
| 380 | unsigned int __cdecl _readfsbase_u32(void); |
| 381 | unsigned __int64 __cdecl _readfsbase_u64(void); |
| 382 | unsigned int __cdecl _readgsbase_u32(void); |
| 383 | unsigned __int64 __cdecl _readgsbase_u64(void); |
| 384 | unsigned __int64 _rorx_u64(unsigned __int64, const unsigned int); |
| 385 | unsigned __int64 _tzcnt_u64(unsigned __int64); |
| 386 | unsigned __int64 _tzmsk_u64(unsigned __int64); |
| 387 | unsigned __int64 _umul128(unsigned __int64 _Multiplier, |
| 388 | unsigned __int64 _Multiplicand, |
| 389 | unsigned __int64 *_HighProduct); |
| 390 | void __cdecl _writefsbase_u32(unsigned int); |
| 391 | void _cdecl _writefsbase_u64(unsigned __int64); |
| 392 | void __cdecl _writegsbase_u32(unsigned int); |
| 393 | void __cdecl _writegsbase_u64(unsigned __int64); |
| 394 | void __cdecl _xrstor64(void const *, unsigned __int64); |
| 395 | void __cdecl _xsave64(void *, unsigned __int64); |
| 396 | void __cdecl _xsaveopt64(void *, unsigned __int64); |
Eric Christopher | 0db88a7 | 2013-09-18 22:24:01 +0000 | [diff] [blame] | 397 | |
Warren Hunt | 3f98794 | 2013-09-30 21:08:05 +0000 | [diff] [blame] | 398 | #endif /* __x86_64__ */ |
Reid Kleckner | f0e2322 | 2013-09-19 00:19:53 +0000 | [diff] [blame] | 399 | |
Warren Hunt | d6ffae9 | 2013-09-27 23:57:26 +0000 | [diff] [blame] | 400 | /*----------------------------------------------------------------------------*\ |
| 401 | |* Bit Twiddling |
| 402 | \*----------------------------------------------------------------------------*/ |
| 403 | static __inline__ unsigned char __attribute__((__always_inline__, __nodebug__)) |
| 404 | _rotl8(unsigned char _Value, unsigned char _Shift) { |
| 405 | _Shift &= 0x7; |
| 406 | return _Shift ? (_Value << _Shift) | (_Value >> (8 - _Shift)) : _Value; |
| 407 | } |
| 408 | static __inline__ unsigned char __attribute__((__always_inline__, __nodebug__)) |
| 409 | _rotr8(unsigned char _Value, unsigned char _Shift) { |
| 410 | _Shift &= 0x7; |
| 411 | return _Shift ? (_Value >> _Shift) | (_Value << (8 - _Shift)) : _Value; |
| 412 | } |
| 413 | static __inline__ unsigned short __attribute__((__always_inline__, __nodebug__)) |
| 414 | _rotl16(unsigned short _Value, unsigned char _Shift) { |
| 415 | _Shift &= 0xf; |
| 416 | return _Shift ? (_Value << _Shift) | (_Value >> (16 - _Shift)) : _Value; |
| 417 | } |
| 418 | static __inline__ unsigned short __attribute__((__always_inline__, __nodebug__)) |
| 419 | _rotr16(unsigned short _Value, unsigned char _Shift) { |
| 420 | _Shift &= 0xf; |
| 421 | return _Shift ? (_Value >> _Shift) | (_Value << (16 - _Shift)) : _Value; |
| 422 | } |
| 423 | static __inline__ unsigned int __attribute__((__always_inline__, __nodebug__)) |
| 424 | _rotl(unsigned int _Value, int _Shift) { |
| 425 | _Shift &= 0x1f; |
| 426 | return _Shift ? (_Value << _Shift) | (_Value >> (32 - _Shift)) : _Value; |
| 427 | } |
| 428 | static __inline__ unsigned int __attribute__((__always_inline__, __nodebug__)) |
| 429 | _rotr(unsigned int _Value, int _Shift) { |
| 430 | _Shift &= 0x1f; |
| 431 | return _Shift ? (_Value >> _Shift) | (_Value << (32 - _Shift)) : _Value; |
| 432 | } |
| 433 | static __inline__ unsigned long __attribute__((__always_inline__, __nodebug__)) |
| 434 | _lrotl(unsigned long _Value, int _Shift) { |
| 435 | _Shift &= 0x1f; |
| 436 | return _Shift ? (_Value << _Shift) | (_Value >> (32 - _Shift)) : _Value; |
| 437 | } |
| 438 | static __inline__ unsigned long __attribute__((__always_inline__, __nodebug__)) |
| 439 | _lrotr(unsigned long _Value, int _Shift) { |
| 440 | _Shift &= 0x1f; |
| 441 | return _Shift ? (_Value >> _Shift) | (_Value << (32 - _Shift)) : _Value; |
| 442 | } |
| 443 | static |
| 444 | __inline__ unsigned __int64 __attribute__((__always_inline__, __nodebug__)) |
| 445 | _rotl64(unsigned __int64 _Value, int _Shift) { |
| 446 | _Shift &= 0x3f; |
| 447 | return _Shift ? (_Value << _Shift) | (_Value >> (64 - _Shift)) : _Value; |
| 448 | } |
| 449 | static |
| 450 | __inline__ unsigned __int64 __attribute__((__always_inline__, __nodebug__)) |
| 451 | _rotr64(unsigned __int64 _Value, int _Shift) { |
| 452 | _Shift &= 0x3f; |
| 453 | return _Shift ? (_Value >> _Shift) | (_Value << (64 - _Shift)) : _Value; |
| 454 | } |
| 455 | /*----------------------------------------------------------------------------*\ |
| 456 | |* Bit Counting and Testing |
| 457 | \*----------------------------------------------------------------------------*/ |
| 458 | static __inline__ unsigned char __attribute__((__always_inline__, __nodebug__)) |
| 459 | _BitScanForward(unsigned long *_Index, unsigned long _Mask) { |
| 460 | if (!_Mask) |
| 461 | return 0; |
| 462 | *_Index = __builtin_ctzl(_Mask); |
| 463 | return 1; |
| 464 | } |
| 465 | static __inline__ unsigned char __attribute__((__always_inline__, __nodebug__)) |
| 466 | _BitScanReverse(unsigned long *_Index, unsigned long _Mask) { |
| 467 | if (!_Mask) |
| 468 | return 0; |
| 469 | *_Index = 31 - __builtin_clzl(_Mask); |
| 470 | return 1; |
| 471 | } |
| 472 | static __inline__ unsigned int __attribute__((__always_inline__, __nodebug__)) |
| 473 | _lzcnt_u32(unsigned int a) { |
| 474 | if (!a) |
| 475 | return 32; |
| 476 | return __builtin_clzl(a); |
| 477 | } |
| 478 | static __inline__ unsigned short __attribute__((__always_inline__, __nodebug__)) |
| 479 | __popcnt16(unsigned short value) { |
| 480 | return __builtin_popcount((int)value); |
| 481 | } |
| 482 | static __inline__ unsigned int __attribute__((__always_inline__, __nodebug__)) |
| 483 | __popcnt(unsigned int value) { |
| 484 | return __builtin_popcount(value); |
| 485 | } |
| 486 | static __inline__ unsigned char __attribute__((__always_inline__, __nodebug__)) |
| 487 | _bittest(long const *a, long b) { |
| 488 | return (*a >> b) & 1; |
| 489 | } |
| 490 | static __inline__ unsigned char __attribute__((__always_inline__, __nodebug__)) |
| 491 | _bittestandcomplement(long *a, long b) { |
| 492 | unsigned char x = (*a >> b) & 1; |
| 493 | *a = *a ^ (1 << b); |
| 494 | return x; |
| 495 | } |
| 496 | static __inline__ unsigned char __attribute__((__always_inline__, __nodebug__)) |
| 497 | _bittestandreset(long *a, long b) { |
| 498 | unsigned char x = (*a >> b) & 1; |
| 499 | *a = *a & ~(1 << b); |
| 500 | return x; |
| 501 | } |
| 502 | static __inline__ unsigned char __attribute__((__always_inline__, __nodebug__)) |
| 503 | _bittestandset(long *a, long b) { |
| 504 | unsigned char x = (*a >> b) & 1; |
Warren Hunt | 2731e3e | 2013-10-01 17:12:40 +0000 | [diff] [blame] | 505 | *a = *a | (1 << b); |
Warren Hunt | d6ffae9 | 2013-09-27 23:57:26 +0000 | [diff] [blame] | 506 | return x; |
| 507 | } |
Warren Hunt | 3f98794 | 2013-09-30 21:08:05 +0000 | [diff] [blame] | 508 | #ifdef __x86_64__ |
Warren Hunt | d6ffae9 | 2013-09-27 23:57:26 +0000 | [diff] [blame] | 509 | static __inline__ unsigned char __attribute__((__always_inline__, __nodebug__)) |
| 510 | _BitScanForward64(unsigned long *_Index, unsigned __int64 _Mask) { |
| 511 | if (!_Mask) |
| 512 | return 0; |
| 513 | *_Index = __builtin_ctzll(_Mask); |
| 514 | return 1; |
| 515 | } |
| 516 | static __inline__ unsigned char __attribute__((__always_inline__, __nodebug__)) |
| 517 | _BitScanReverse64(unsigned long *_Index, unsigned __int64 _Mask) { |
| 518 | if (!_Mask) |
| 519 | return 0; |
| 520 | *_Index = 63 - __builtin_clzll(_Mask); |
| 521 | return 1; |
| 522 | } |
| 523 | static |
| 524 | __inline__ unsigned __int64 __attribute__((__always_inline__, __nodebug__)) |
| 525 | _lzcnt_u64(unsigned __int64 a) { |
| 526 | if (!a) |
| 527 | return 64; |
| 528 | return __builtin_clzll(a); |
| 529 | } |
| 530 | static __inline__ |
| 531 | unsigned __int64 __attribute__((__always_inline__, __nodebug__)) |
| 532 | __popcnt64(unsigned __int64 value) { |
| 533 | return __builtin_popcountll(value); |
| 534 | } |
| 535 | static __inline__ unsigned char __attribute__((__always_inline__, __nodebug__)) |
| 536 | _bittest64(__int64 const *a, __int64 b) { |
| 537 | return (*a >> b) & 1; |
| 538 | } |
| 539 | static __inline__ unsigned char __attribute__((__always_inline__, __nodebug__)) |
| 540 | _bittestandcomplement64(__int64 *a, __int64 b) { |
| 541 | unsigned char x = (*a >> b) & 1; |
| 542 | *a = *a ^ (1ll << b); |
| 543 | return x; |
| 544 | } |
| 545 | static __inline__ unsigned char __attribute__((__always_inline__, __nodebug__)) |
| 546 | _bittestandreset64(__int64 *a, __int64 b) { |
| 547 | unsigned char x = (*a >> b) & 1; |
| 548 | *a = *a & ~(1ll << b); |
| 549 | return x; |
| 550 | } |
| 551 | static __inline__ unsigned char __attribute__((__always_inline__, __nodebug__)) |
| 552 | _bittestandset64(__int64 *a, __int64 b) { |
| 553 | unsigned char x = (*a >> b) & 1; |
Warren Hunt | 2731e3e | 2013-10-01 17:12:40 +0000 | [diff] [blame] | 554 | *a = *a | (1ll << b); |
Warren Hunt | d6ffae9 | 2013-09-27 23:57:26 +0000 | [diff] [blame] | 555 | return x; |
| 556 | } |
| 557 | #endif |
| 558 | /*----------------------------------------------------------------------------*\ |
| 559 | |* Interlocked Exchange Add |
| 560 | \*----------------------------------------------------------------------------*/ |
| 561 | static __inline__ char __attribute__((__always_inline__, __nodebug__)) |
| 562 | _InterlockedExchangeAdd8(char volatile *_Addend, char _Value) { |
| 563 | return __atomic_add_fetch(_Addend, _Value, 0) - _Value; |
| 564 | } |
| 565 | static __inline__ short __attribute__((__always_inline__, __nodebug__)) |
| 566 | _InterlockedExchangeAdd16(short volatile *_Addend, short _Value) { |
| 567 | return __atomic_add_fetch(_Addend, _Value, 0) - _Value; |
| 568 | } |
| 569 | static __inline__ long __attribute__((__always_inline__, __nodebug__)) |
| 570 | _InterlockedExchangeAdd(long volatile *_Addend, long _Value) { |
| 571 | return __atomic_add_fetch(_Addend, _Value, 0) - _Value; |
| 572 | } |
Warren Hunt | 3f98794 | 2013-09-30 21:08:05 +0000 | [diff] [blame] | 573 | #ifdef __x86_64__ |
Warren Hunt | d6ffae9 | 2013-09-27 23:57:26 +0000 | [diff] [blame] | 574 | static __inline__ __int64 __attribute__((__always_inline__, __nodebug__)) |
| 575 | _InterlockedExchangeAdd64(__int64 volatile *_Addend, __int64 _Value) { |
| 576 | return __atomic_add_fetch(_Addend, _Value, 0) - _Value; |
| 577 | } |
| 578 | #endif |
| 579 | /*----------------------------------------------------------------------------*\ |
| 580 | |* Interlocked Exchange Sub |
| 581 | \*----------------------------------------------------------------------------*/ |
| 582 | static __inline__ char __attribute__((__always_inline__, __nodebug__)) |
| 583 | _InterlockedExchangeSub8(char volatile *_Subend, char _Value) { |
| 584 | return __atomic_sub_fetch(_Subend, _Value, 0) + _Value; |
| 585 | } |
| 586 | static __inline__ short __attribute__((__always_inline__, __nodebug__)) |
| 587 | _InterlockedExchangeSub16(short volatile *_Subend, short _Value) { |
| 588 | return __atomic_sub_fetch(_Subend, _Value, 0) + _Value; |
| 589 | } |
| 590 | static __inline__ long __attribute__((__always_inline__, __nodebug__)) |
| 591 | _InterlockedExchangeSub(long volatile *_Subend, long _Value) { |
| 592 | return __atomic_sub_fetch(_Subend, _Value, 0) + _Value; |
| 593 | } |
Warren Hunt | 3f98794 | 2013-09-30 21:08:05 +0000 | [diff] [blame] | 594 | #ifdef __x86_64__ |
Warren Hunt | d6ffae9 | 2013-09-27 23:57:26 +0000 | [diff] [blame] | 595 | static __inline__ __int64 __attribute__((__always_inline__, __nodebug__)) |
| 596 | _InterlockedExchangeSub64(__int64 volatile *_Subend, __int64 _Value) { |
| 597 | return __atomic_sub_fetch(_Subend, _Value, 0) + _Value; |
| 598 | } |
| 599 | #endif |
| 600 | /*----------------------------------------------------------------------------*\ |
| 601 | |* Interlocked Increment |
| 602 | \*----------------------------------------------------------------------------*/ |
| 603 | static __inline__ char __attribute__((__always_inline__, __nodebug__)) |
| 604 | _InterlockedIncrement16(char volatile *_Value) { |
| 605 | return __atomic_add_fetch(_Value, 1, 0); |
| 606 | } |
| 607 | static __inline__ long __attribute__((__always_inline__, __nodebug__)) |
| 608 | _InterlockedIncrement(long volatile *_Value) { |
| 609 | return __atomic_add_fetch(_Value, 1, 0); |
| 610 | } |
Warren Hunt | 3f98794 | 2013-09-30 21:08:05 +0000 | [diff] [blame] | 611 | #ifdef __x86_64__ |
Warren Hunt | d6ffae9 | 2013-09-27 23:57:26 +0000 | [diff] [blame] | 612 | static __inline__ __int64 __attribute__((__always_inline__, __nodebug__)) |
| 613 | _InterlockedIncrement64(__int64 volatile *_Value) { |
| 614 | return __atomic_add_fetch(_Value, 1, 0); |
| 615 | } |
| 616 | #endif |
| 617 | /*----------------------------------------------------------------------------*\ |
| 618 | |* Interlocked Decrement |
| 619 | \*----------------------------------------------------------------------------*/ |
| 620 | static __inline__ char __attribute__((__always_inline__, __nodebug__)) |
| 621 | _InterlockedDecrement16(char volatile *_Value) { |
| 622 | return __atomic_sub_fetch(_Value, 1, 0); |
| 623 | } |
| 624 | static __inline__ long __attribute__((__always_inline__, __nodebug__)) |
| 625 | _InterlockedDecrement(long volatile *_Value) { |
| 626 | return __atomic_sub_fetch(_Value, 1, 0); |
| 627 | } |
Warren Hunt | 3f98794 | 2013-09-30 21:08:05 +0000 | [diff] [blame] | 628 | #ifdef __x86_64__ |
Warren Hunt | d6ffae9 | 2013-09-27 23:57:26 +0000 | [diff] [blame] | 629 | static __inline__ __int64 __attribute__((__always_inline__, __nodebug__)) |
| 630 | _InterlockedDecrement64(__int64 volatile *_Value) { |
| 631 | return __atomic_sub_fetch(_Value, 1, 0); |
| 632 | } |
| 633 | #endif |
| 634 | /*----------------------------------------------------------------------------*\ |
| 635 | |* Interlocked And |
| 636 | \*----------------------------------------------------------------------------*/ |
| 637 | static __inline__ char __attribute__((__always_inline__, __nodebug__)) |
| 638 | _InterlockedAnd8(char volatile *_Value, char _Mask) { |
| 639 | return __atomic_and_fetch(_Value, _Mask, 0); |
| 640 | } |
| 641 | static __inline__ short __attribute__((__always_inline__, __nodebug__)) |
| 642 | _InterlockedAnd16(short volatile *_Value, short _Mask) { |
| 643 | return __atomic_and_fetch(_Value, _Mask, 0); |
| 644 | } |
| 645 | static __inline__ long __attribute__((__always_inline__, __nodebug__)) |
| 646 | _InterlockedAnd(long volatile *_Value, long _Mask) { |
| 647 | return __atomic_and_fetch(_Value, _Mask, 0); |
| 648 | } |
Warren Hunt | 3f98794 | 2013-09-30 21:08:05 +0000 | [diff] [blame] | 649 | #ifdef __x86_64__ |
Warren Hunt | d6ffae9 | 2013-09-27 23:57:26 +0000 | [diff] [blame] | 650 | static __inline__ __int64 __attribute__((__always_inline__, __nodebug__)) |
| 651 | _InterlockedAnd64(__int64 volatile *_Value, __int64 _Mask) { |
| 652 | return __atomic_and_fetch(_Value, _Mask, 0); |
| 653 | } |
| 654 | #endif |
| 655 | /*----------------------------------------------------------------------------*\ |
| 656 | |* Interlocked Or |
| 657 | \*----------------------------------------------------------------------------*/ |
| 658 | static __inline__ char __attribute__((__always_inline__, __nodebug__)) |
| 659 | _InterlockedOr8(char volatile *_Value, char _Mask) { |
| 660 | return __atomic_or_fetch(_Value, _Mask, 0); |
| 661 | } |
| 662 | static __inline__ short __attribute__((__always_inline__, __nodebug__)) |
| 663 | _InterlockedOr16(short volatile *_Value, short _Mask) { |
| 664 | return __atomic_or_fetch(_Value, _Mask, 0); |
| 665 | } |
| 666 | static __inline__ long __attribute__((__always_inline__, __nodebug__)) |
| 667 | _InterlockedOr(long volatile *_Value, long _Mask) { |
| 668 | return __atomic_or_fetch(_Value, _Mask, 0); |
| 669 | } |
Warren Hunt | 3f98794 | 2013-09-30 21:08:05 +0000 | [diff] [blame] | 670 | #ifdef __x86_64__ |
Warren Hunt | d6ffae9 | 2013-09-27 23:57:26 +0000 | [diff] [blame] | 671 | static __inline__ __int64 __attribute__((__always_inline__, __nodebug__)) |
| 672 | _InterlockedOr64(__int64 volatile *_Value, __int64 _Mask) { |
| 673 | return __atomic_or_fetch(_Value, _Mask, 0); |
| 674 | } |
| 675 | #endif |
| 676 | /*----------------------------------------------------------------------------*\ |
| 677 | |* Interlocked Xor |
| 678 | \*----------------------------------------------------------------------------*/ |
| 679 | static __inline__ char __attribute__((__always_inline__, __nodebug__)) |
| 680 | _InterlockedXor8(char volatile *_Value, char _Mask) { |
| 681 | return __atomic_xor_fetch(_Value, _Mask, 0); |
| 682 | } |
| 683 | static __inline__ short __attribute__((__always_inline__, __nodebug__)) |
| 684 | _InterlockedXor16(short volatile *_Value, short _Mask) { |
| 685 | return __atomic_xor_fetch(_Value, _Mask, 0); |
| 686 | } |
| 687 | static __inline__ long __attribute__((__always_inline__, __nodebug__)) |
| 688 | _InterlockedXor(long volatile *_Value, long _Mask) { |
| 689 | return __atomic_xor_fetch(_Value, _Mask, 0); |
| 690 | } |
Warren Hunt | 3f98794 | 2013-09-30 21:08:05 +0000 | [diff] [blame] | 691 | #ifdef __x86_64__ |
Warren Hunt | d6ffae9 | 2013-09-27 23:57:26 +0000 | [diff] [blame] | 692 | static __inline__ __int64 __attribute__((__always_inline__, __nodebug__)) |
| 693 | _InterlockedXor64(__int64 volatile *_Value, __int64 _Mask) { |
| 694 | return __atomic_xor_fetch(_Value, _Mask, 0); |
| 695 | } |
| 696 | #endif |
| 697 | /*----------------------------------------------------------------------------*\ |
| 698 | |* Interlocked Exchange |
| 699 | \*----------------------------------------------------------------------------*/ |
| 700 | static __inline__ char __attribute__((__always_inline__, __nodebug__)) |
| 701 | _InterlockedExchange8(char volatile *_Target, char _Value) { |
| 702 | __atomic_exchange(_Target, &_Value, &_Value, 0); |
| 703 | return _Value; |
| 704 | } |
| 705 | static __inline__ short __attribute__((__always_inline__, __nodebug__)) |
| 706 | _InterlockedExchange16(short volatile *_Target, short _Value) { |
| 707 | __atomic_exchange(_Target, &_Value, &_Value, 0); |
| 708 | return _Value; |
| 709 | } |
| 710 | static __inline__ long __attribute__((__always_inline__, __nodebug__)) |
| 711 | _InterlockedExchange(long volatile *_Target, long _Value) { |
| 712 | __atomic_exchange(_Target, &_Value, &_Value, 0); |
| 713 | return _Value; |
| 714 | } |
Warren Hunt | 3f98794 | 2013-09-30 21:08:05 +0000 | [diff] [blame] | 715 | #ifdef __x86_64__ |
Warren Hunt | d6ffae9 | 2013-09-27 23:57:26 +0000 | [diff] [blame] | 716 | static __inline__ __int64 __attribute__((__always_inline__, __nodebug__)) |
| 717 | _InterlockedExchange64(__int64 volatile *_Target, __int64 _Value) { |
| 718 | __atomic_exchange(_Target, &_Value, &_Value, 0); |
| 719 | return _Value; |
| 720 | } |
| 721 | #endif |
| 722 | /*----------------------------------------------------------------------------*\ |
| 723 | |* Interlocked Compare Exchange |
| 724 | \*----------------------------------------------------------------------------*/ |
| 725 | static __inline__ char __attribute__((__always_inline__, __nodebug__)) |
| 726 | _InterlockedCompareExchange8(char volatile *_Destination, |
| 727 | char _Exchange, char _Comparand) { |
| 728 | __atomic_compare_exchange(_Destination, &_Comparand, &_Exchange, 0, 0, 0); |
| 729 | return _Comparand; |
| 730 | } |
| 731 | static __inline__ short __attribute__((__always_inline__, __nodebug__)) |
| 732 | _InterlockedCompareExchange16(short volatile *_Destination, |
| 733 | short _Exchange, short _Comparand) { |
| 734 | __atomic_compare_exchange(_Destination, &_Comparand, &_Exchange, 0, 0, 0); |
| 735 | return _Comparand; |
| 736 | } |
| 737 | static __inline__ long __attribute__((__always_inline__, __nodebug__)) |
| 738 | _InterlockedCompareExchange(long volatile *_Destination, |
| 739 | long _Exchange, long _Comparand) { |
| 740 | __atomic_compare_exchange(_Destination, &_Comparand, &_Exchange, 0, 0, 0); |
| 741 | return _Comparand; |
| 742 | } |
Warren Hunt | 3f98794 | 2013-09-30 21:08:05 +0000 | [diff] [blame] | 743 | #ifdef __x86_64__ |
Warren Hunt | d6ffae9 | 2013-09-27 23:57:26 +0000 | [diff] [blame] | 744 | static __inline__ __int64 __attribute__((__always_inline__, __nodebug__)) |
| 745 | _InterlockedCompareExchange64(__int64 volatile *_Destination, |
| 746 | __int64 _Exchange, __int64 _Comparand) { |
| 747 | __atomic_compare_exchange(_Destination, &_Comparand, &_Exchange, 0, 0, 0); |
| 748 | return _Comparand; |
| 749 | } |
| 750 | #endif |
| 751 | /*----------------------------------------------------------------------------*\ |
Reid Kleckner | 00d33a5 | 2013-10-17 01:29:48 +0000 | [diff] [blame] | 752 | |* Barriers |
| 753 | \*----------------------------------------------------------------------------*/ |
| 754 | static __inline__ void __attribute__((__always_inline__, __nodebug__)) |
| 755 | __attribute__((deprecated("use other intrinsics or C++11 atomics instead"))) |
| 756 | _ReadWriteBarrier(void) { |
| 757 | __asm__ volatile ("" : : : "memory"); |
| 758 | } |
| 759 | static __inline__ void __attribute__((__always_inline__, __nodebug__)) |
| 760 | __attribute__((deprecated("use other intrinsics or C++11 atomics instead"))) |
| 761 | _ReadBarrier(void) { |
| 762 | __asm__ volatile ("" : : : "memory"); |
| 763 | } |
| 764 | static __inline__ void __attribute__((__always_inline__, __nodebug__)) |
| 765 | __attribute__((deprecated("use other intrinsics or C++11 atomics instead"))) |
| 766 | _WriteBarrier(void) { |
| 767 | __asm__ volatile ("" : : : "memory"); |
| 768 | } |
| 769 | /*----------------------------------------------------------------------------*\ |
Warren Hunt | d6ffae9 | 2013-09-27 23:57:26 +0000 | [diff] [blame] | 770 | |* Misc |
| 771 | \*----------------------------------------------------------------------------*/ |
| 772 | static __inline__ void * __attribute__((__always_inline__, __nodebug__)) |
| 773 | _AddressOfReturnAddress(void) { |
| 774 | return (void*)((char*)__builtin_frame_address(0) + sizeof(void*)); |
| 775 | } |
| 776 | static __inline__ void * __attribute__((__always_inline__, __nodebug__)) |
| 777 | _ReturnAddress(void) { |
| 778 | return __builtin_return_address(0); |
| 779 | } |
Hans Wennborg | 854f7d3 | 2014-01-16 23:39:35 +0000 | [diff] [blame^] | 780 | static __inline__ void __attribute__((__always_inline__, __nodebug__)) |
| 781 | __cpuid(int __info[4], int __level) { |
| 782 | #if __i386__ |
| 783 | __asm__ ("cpuid" |
| 784 | : "=a"(__info[0]), "=b" (__info[1]), "=c"(__info[2]), "=d"(__info[3]) |
| 785 | : "0"(__level)); |
| 786 | #else |
| 787 | __asm__ ("cpuid" : "=a"(__info[0]), "=b" (__info[1]), "=c"(__info[2]), "=d"(__info[3]) |
| 788 | : "0"(__level)); |
| 789 | #endif |
| 790 | } |
| 791 | static __inline__ unsigned __int64 __cdecl __attribute__((__always_inline__, __nodebug__)) |
| 792 | _xgetbv(unsigned int __xcr_no) { |
| 793 | unsigned int __eax, __edx; |
| 794 | __asm__ ("xgetbv" : "=a" (__eax), "=d" (__edx) : "c" (__xcr_no)); |
| 795 | return ((unsigned __int64)__edx << 32) | __eax; |
| 796 | } |
Warren Hunt | d6ffae9 | 2013-09-27 23:57:26 +0000 | [diff] [blame] | 797 | |
Eric Christopher | cc87253 | 2013-09-18 22:40:18 +0000 | [diff] [blame] | 798 | #ifdef __cplusplus |
Eric Christopher | 0db88a7 | 2013-09-18 22:24:01 +0000 | [diff] [blame] | 799 | } |
Eric Christopher | d1428bf | 2013-08-31 00:22:48 +0000 | [diff] [blame] | 800 | #endif |
Reid Kleckner | f0e2322 | 2013-09-19 00:19:53 +0000 | [diff] [blame] | 801 | |
| 802 | #endif /* __INTRIN_H */ |
| 803 | #endif /* _MSC_VER */ |