clang API Documentation
00001 /* ===-------- Intrin.h ---------------------------------------------------=== 00002 * 00003 * Permission is hereby granted, free of charge, to any person obtaining a copy 00004 * of this software and associated documentation files (the "Software"), to deal 00005 * in the Software without restriction, including without limitation the rights 00006 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 00007 * copies of the Software, and to permit persons to whom the Software is 00008 * furnished to do so, subject to the following conditions: 00009 * 00010 * The above copyright notice and this permission notice shall be included in 00011 * all copies or substantial portions of the Software. 00012 * 00013 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 00014 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 00015 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 00016 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 00017 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 00018 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 00019 * THE SOFTWARE. 00020 * 00021 *===-----------------------------------------------------------------------=== 00022 */ 00023 00024 /* Only include this if we're compiling for the windows platform. */ 00025 #ifndef _MSC_VER 00026 #include_next <Intrin.h> 00027 #else 00028 00029 #ifndef __INTRIN_H 00030 #define __INTRIN_H 00031 00032 /* First include the standard intrinsics. */ 00033 #if defined(__i386__) || defined(__x86_64__) 00034 #include <x86intrin.h> 00035 #endif 00036 00037 /* For the definition of jmp_buf. */ 00038 #if __STDC_HOSTED__ 00039 #include <setjmp.h> 00040 #endif 00041 00042 #ifdef __cplusplus 00043 extern "C" { 00044 #endif 00045 00046 #if defined(__MMX__) 00047 /* And the random ones that aren't in those files. */ 00048 __m64 _m_from_float(float); 00049 __m64 _m_from_int(int _l); 00050 void _m_prefetch(void *); 00051 float _m_to_float(__m64); 00052 int _m_to_int(__m64 _M); 00053 #endif 00054 00055 /* Other assorted instruction intrinsics. */ 00056 void __addfsbyte(unsigned long, unsigned char); 00057 void __addfsdword(unsigned long, unsigned long); 00058 void __addfsword(unsigned long, unsigned short); 00059 void __code_seg(const char *); 00060 static __inline__ 00061 void __cpuid(int[4], int); 00062 static __inline__ 00063 void __cpuidex(int[4], int, int); 00064 void __debugbreak(void); 00065 __int64 __emul(int, int); 00066 unsigned __int64 __emulu(unsigned int, unsigned int); 00067 void __cdecl __fastfail(unsigned int); 00068 unsigned int __getcallerseflags(void); 00069 static __inline__ 00070 void __halt(void); 00071 unsigned char __inbyte(unsigned short); 00072 void __inbytestring(unsigned short, unsigned char *, unsigned long); 00073 void __incfsbyte(unsigned long); 00074 void __incfsdword(unsigned long); 00075 void __incfsword(unsigned long); 00076 unsigned long __indword(unsigned short); 00077 void __indwordstring(unsigned short, unsigned long *, unsigned long); 00078 void __int2c(void); 00079 void __invlpg(void *); 00080 unsigned short __inword(unsigned short); 00081 void __inwordstring(unsigned short, unsigned short *, unsigned long); 00082 void __lidt(void *); 00083 unsigned __int64 __ll_lshift(unsigned __int64, int); 00084 __int64 __ll_rshift(__int64, int); 00085 void __llwpcb(void *); 00086 unsigned char __lwpins32(unsigned int, unsigned int, unsigned int); 00087 void __lwpval32(unsigned int, unsigned int, unsigned int); 00088 unsigned int __lzcnt(unsigned int); 00089 unsigned short __lzcnt16(unsigned short); 00090 static __inline__ 00091 void __movsb(unsigned char *, unsigned char const *, size_t); 00092 static __inline__ 00093 void __movsd(unsigned long *, unsigned long const *, size_t); 00094 static __inline__ 00095 void __movsw(unsigned short *, unsigned short const *, size_t); 00096 void __nop(void); 00097 void __nvreg_restore_fence(void); 00098 void __nvreg_save_fence(void); 00099 void __outbyte(unsigned short, unsigned char); 00100 void __outbytestring(unsigned short, unsigned char *, unsigned long); 00101 void __outdword(unsigned short, unsigned long); 00102 void __outdwordstring(unsigned short, unsigned long *, unsigned long); 00103 void __outword(unsigned short, unsigned short); 00104 void __outwordstring(unsigned short, unsigned short *, unsigned long); 00105 static __inline__ 00106 unsigned int __popcnt(unsigned int); 00107 static __inline__ 00108 unsigned short __popcnt16(unsigned short); 00109 unsigned long __readcr0(void); 00110 unsigned long __readcr2(void); 00111 static __inline__ 00112 unsigned long __readcr3(void); 00113 unsigned long __readcr4(void); 00114 unsigned long __readcr8(void); 00115 unsigned int __readdr(unsigned int); 00116 #ifdef __i386__ 00117 static __inline__ 00118 unsigned char __readfsbyte(unsigned long); 00119 static __inline__ 00120 unsigned long __readfsdword(unsigned long); 00121 static __inline__ 00122 unsigned __int64 __readfsqword(unsigned long); 00123 static __inline__ 00124 unsigned short __readfsword(unsigned long); 00125 #endif 00126 static __inline__ 00127 unsigned __int64 __readmsr(unsigned long); 00128 unsigned __int64 __readpmc(unsigned long); 00129 unsigned long __segmentlimit(unsigned long); 00130 void __sidt(void *); 00131 void *__slwpcb(void); 00132 static __inline__ 00133 void __stosb(unsigned char *, unsigned char, size_t); 00134 static __inline__ 00135 void __stosd(unsigned long *, unsigned long, size_t); 00136 static __inline__ 00137 void __stosw(unsigned short *, unsigned short, size_t); 00138 void __svm_clgi(void); 00139 void __svm_invlpga(void *, int); 00140 void __svm_skinit(int); 00141 void __svm_stgi(void); 00142 void __svm_vmload(size_t); 00143 void __svm_vmrun(size_t); 00144 void __svm_vmsave(size_t); 00145 void __ud2(void); 00146 unsigned __int64 __ull_rshift(unsigned __int64, int); 00147 void __vmx_off(void); 00148 void __vmx_vmptrst(unsigned __int64 *); 00149 void __wbinvd(void); 00150 void __writecr0(unsigned int); 00151 static __inline__ 00152 void __writecr3(unsigned int); 00153 void __writecr4(unsigned int); 00154 void __writecr8(unsigned int); 00155 void __writedr(unsigned int, unsigned int); 00156 void __writefsbyte(unsigned long, unsigned char); 00157 void __writefsdword(unsigned long, unsigned long); 00158 void __writefsqword(unsigned long, unsigned __int64); 00159 void __writefsword(unsigned long, unsigned short); 00160 void __writemsr(unsigned long, unsigned __int64); 00161 static __inline__ 00162 void *_AddressOfReturnAddress(void); 00163 static __inline__ 00164 unsigned char _BitScanForward(unsigned long *_Index, unsigned long _Mask); 00165 static __inline__ 00166 unsigned char _BitScanReverse(unsigned long *_Index, unsigned long _Mask); 00167 static __inline__ 00168 unsigned char _bittest(long const *, long); 00169 static __inline__ 00170 unsigned char _bittestandcomplement(long *, long); 00171 static __inline__ 00172 unsigned char _bittestandreset(long *, long); 00173 static __inline__ 00174 unsigned char _bittestandset(long *, long); 00175 unsigned __int64 __cdecl _byteswap_uint64(unsigned __int64); 00176 unsigned long __cdecl _byteswap_ulong(unsigned long); 00177 unsigned short __cdecl _byteswap_ushort(unsigned short); 00178 void __cdecl _disable(void); 00179 void __cdecl _enable(void); 00180 void __cdecl _fxrstor(void const *); 00181 void __cdecl _fxsave(void *); 00182 long _InterlockedAddLargeStatistic(__int64 volatile *_Addend, long _Value); 00183 static __inline__ 00184 long _InterlockedAnd(long volatile *_Value, long _Mask); 00185 static __inline__ 00186 short _InterlockedAnd16(short volatile *_Value, short _Mask); 00187 static __inline__ 00188 char _InterlockedAnd8(char volatile *_Value, char _Mask); 00189 unsigned char _interlockedbittestandreset(long volatile *, long); 00190 static __inline__ 00191 unsigned char _interlockedbittestandset(long volatile *, long); 00192 static __inline__ 00193 long __cdecl _InterlockedCompareExchange(long volatile *_Destination, 00194 long _Exchange, long _Comparand); 00195 long _InterlockedCompareExchange_HLEAcquire(long volatile *, long, long); 00196 long _InterlockedCompareExchange_HLERelease(long volatile *, long, long); 00197 static __inline__ 00198 short _InterlockedCompareExchange16(short volatile *_Destination, 00199 short _Exchange, short _Comparand); 00200 static __inline__ 00201 __int64 _InterlockedCompareExchange64(__int64 volatile *_Destination, 00202 __int64 _Exchange, __int64 _Comparand); 00203 __int64 _InterlockedcompareExchange64_HLEAcquire(__int64 volatile *, __int64, 00204 __int64); 00205 __int64 _InterlockedCompareExchange64_HLERelease(__int64 volatile *, __int64, 00206 __int64); 00207 static __inline__ 00208 char _InterlockedCompareExchange8(char volatile *_Destination, char _Exchange, 00209 char _Comparand); 00210 void *_InterlockedCompareExchangePointer_HLEAcquire(void *volatile *, void *, 00211 void *); 00212 void *_InterlockedCompareExchangePointer_HLERelease(void *volatile *, void *, 00213 void *); 00214 static __inline__ 00215 long __cdecl _InterlockedDecrement(long volatile *_Addend); 00216 static __inline__ 00217 short _InterlockedDecrement16(short volatile *_Addend); 00218 long _InterlockedExchange(long volatile *_Target, long _Value); 00219 static __inline__ 00220 short _InterlockedExchange16(short volatile *_Target, short _Value); 00221 static __inline__ 00222 char _InterlockedExchange8(char volatile *_Target, char _Value); 00223 static __inline__ 00224 long __cdecl _InterlockedExchangeAdd(long volatile *_Addend, long _Value); 00225 long _InterlockedExchangeAdd_HLEAcquire(long volatile *, long); 00226 long _InterlockedExchangeAdd_HLERelease(long volatile *, long); 00227 static __inline__ 00228 short _InterlockedExchangeAdd16(short volatile *_Addend, short _Value); 00229 __int64 _InterlockedExchangeAdd64_HLEAcquire(__int64 volatile *, __int64); 00230 __int64 _InterlockedExchangeAdd64_HLERelease(__int64 volatile *, __int64); 00231 static __inline__ 00232 char _InterlockedExchangeAdd8(char volatile *_Addend, char _Value); 00233 static __inline__ 00234 long __cdecl _InterlockedIncrement(long volatile *_Addend); 00235 static __inline__ 00236 short _InterlockedIncrement16(short volatile *_Addend); 00237 static __inline__ 00238 long _InterlockedOr(long volatile *_Value, long _Mask); 00239 static __inline__ 00240 short _InterlockedOr16(short volatile *_Value, short _Mask); 00241 static __inline__ 00242 char _InterlockedOr8(char volatile *_Value, char _Mask); 00243 static __inline__ 00244 long _InterlockedXor(long volatile *_Value, long _Mask); 00245 static __inline__ 00246 short _InterlockedXor16(short volatile *_Value, short _Mask); 00247 static __inline__ 00248 char _InterlockedXor8(char volatile *_Value, char _Mask); 00249 void __cdecl _invpcid(unsigned int, void *); 00250 static __inline__ 00251 unsigned long __cdecl _lrotl(unsigned long, int); 00252 static __inline__ 00253 unsigned long __cdecl _lrotr(unsigned long, int); 00254 static __inline__ 00255 static __inline__ 00256 void _ReadBarrier(void); 00257 static __inline__ 00258 void _ReadWriteBarrier(void); 00259 static __inline__ 00260 void *_ReturnAddress(void); 00261 unsigned int _rorx_u32(unsigned int, const unsigned int); 00262 static __inline__ 00263 unsigned int __cdecl _rotl(unsigned int _Value, int _Shift); 00264 static __inline__ 00265 unsigned short _rotl16(unsigned short _Value, unsigned char _Shift); 00266 static __inline__ 00267 unsigned __int64 __cdecl _rotl64(unsigned __int64 _Value, int _Shift); 00268 static __inline__ 00269 unsigned char _rotl8(unsigned char _Value, unsigned char _Shift); 00270 static __inline__ 00271 unsigned int __cdecl _rotr(unsigned int _Value, int _Shift); 00272 static __inline__ 00273 unsigned short _rotr16(unsigned short _Value, unsigned char _Shift); 00274 static __inline__ 00275 unsigned __int64 __cdecl _rotr64(unsigned __int64 _Value, int _Shift); 00276 static __inline__ 00277 unsigned char _rotr8(unsigned char _Value, unsigned char _Shift); 00278 int _sarx_i32(int, unsigned int); 00279 #if __STDC_HOSTED__ 00280 int __cdecl _setjmp(jmp_buf); 00281 #endif 00282 unsigned int _shlx_u32(unsigned int, unsigned int); 00283 unsigned int _shrx_u32(unsigned int, unsigned int); 00284 void _Store_HLERelease(long volatile *, long); 00285 void _Store64_HLERelease(__int64 volatile *, __int64); 00286 void _StorePointer_HLERelease(void *volatile *, void *); 00287 static __inline__ 00288 void _WriteBarrier(void); 00289 unsigned __int32 xbegin(void); 00290 void _xend(void); 00291 static __inline__ 00292 unsigned __int64 __cdecl _xgetbv(unsigned int); 00293 void __cdecl _xrstor(void const *, unsigned __int64); 00294 void __cdecl _xsave(void *, unsigned __int64); 00295 void __cdecl _xsaveopt(void *, unsigned __int64); 00296 void __cdecl _xsetbv(unsigned int, unsigned __int64); 00297 00298 /* These additional intrinsics are turned on in x64/amd64/x86_64 mode. */ 00299 #ifdef __x86_64__ 00300 void __addgsbyte(unsigned long, unsigned char); 00301 void __addgsdword(unsigned long, unsigned long); 00302 void __addgsqword(unsigned long, unsigned __int64); 00303 void __addgsword(unsigned long, unsigned short); 00304 static __inline__ 00305 void __faststorefence(void); 00306 void __incgsbyte(unsigned long); 00307 void __incgsdword(unsigned long); 00308 void __incgsqword(unsigned long); 00309 void __incgsword(unsigned long); 00310 unsigned char __lwpins64(unsigned __int64, unsigned int, unsigned int); 00311 void __lwpval64(unsigned __int64, unsigned int, unsigned int); 00312 unsigned __int64 __lzcnt64(unsigned __int64); 00313 static __inline__ 00314 void __movsq(unsigned long long *, unsigned long long const *, size_t); 00315 __int64 __mulh(__int64, __int64); 00316 static __inline__ 00317 unsigned __int64 __popcnt64(unsigned __int64); 00318 static __inline__ 00319 unsigned char __readgsbyte(unsigned long); 00320 static __inline__ 00321 unsigned long __readgsdword(unsigned long); 00322 static __inline__ 00323 unsigned __int64 __readgsqword(unsigned long); 00324 unsigned short __readgsword(unsigned long); 00325 unsigned __int64 __shiftleft128(unsigned __int64 _LowPart, 00326 unsigned __int64 _HighPart, 00327 unsigned char _Shift); 00328 unsigned __int64 __shiftright128(unsigned __int64 _LowPart, 00329 unsigned __int64 _HighPart, 00330 unsigned char _Shift); 00331 static __inline__ 00332 void __stosq(unsigned __int64 *, unsigned __int64, size_t); 00333 unsigned __int64 __umulh(unsigned __int64, unsigned __int64); 00334 unsigned char __vmx_on(unsigned __int64 *); 00335 unsigned char __vmx_vmclear(unsigned __int64 *); 00336 unsigned char __vmx_vmlaunch(void); 00337 unsigned char __vmx_vmptrld(unsigned __int64 *); 00338 unsigned char __vmx_vmread(size_t, size_t *); 00339 unsigned char __vmx_vmresume(void); 00340 unsigned char __vmx_vmwrite(size_t, size_t); 00341 void __writegsbyte(unsigned long, unsigned char); 00342 void __writegsdword(unsigned long, unsigned long); 00343 void __writegsqword(unsigned long, unsigned __int64); 00344 void __writegsword(unsigned long, unsigned short); 00345 static __inline__ 00346 unsigned char _BitScanForward64(unsigned long *_Index, unsigned __int64 _Mask); 00347 static __inline__ 00348 unsigned char _BitScanReverse64(unsigned long *_Index, unsigned __int64 _Mask); 00349 static __inline__ 00350 unsigned char _bittest64(__int64 const *, __int64); 00351 static __inline__ 00352 unsigned char _bittestandcomplement64(__int64 *, __int64); 00353 static __inline__ 00354 unsigned char _bittestandreset64(__int64 *, __int64); 00355 static __inline__ 00356 unsigned char _bittestandset64(__int64 *, __int64); 00357 unsigned __int64 __cdecl _byteswap_uint64(unsigned __int64); 00358 void __cdecl _fxrstor64(void const *); 00359 void __cdecl _fxsave64(void *); 00360 long _InterlockedAnd_np(long volatile *_Value, long _Mask); 00361 short _InterlockedAnd16_np(short volatile *_Value, short _Mask); 00362 __int64 _InterlockedAnd64_np(__int64 volatile *_Value, __int64 _Mask); 00363 char _InterlockedAnd8_np(char volatile *_Value, char _Mask); 00364 unsigned char _interlockedbittestandreset64(__int64 volatile *, __int64); 00365 static __inline__ 00366 unsigned char _interlockedbittestandset64(__int64 volatile *, __int64); 00367 long _InterlockedCompareExchange_np(long volatile *_Destination, long _Exchange, 00368 long _Comparand); 00369 unsigned char _InterlockedCompareExchange128(__int64 volatile *_Destination, 00370 __int64 _ExchangeHigh, 00371 __int64 _ExchangeLow, 00372 __int64 *_CompareandResult); 00373 unsigned char _InterlockedCompareExchange128_np(__int64 volatile *_Destination, 00374 __int64 _ExchangeHigh, 00375 __int64 _ExchangeLow, 00376 __int64 *_ComparandResult); 00377 short _InterlockedCompareExchange16_np(short volatile *_Destination, 00378 short _Exchange, short _Comparand); 00379 __int64 _InterlockedCompareExchange64_HLEAcquire(__int64 volatile *, __int64, 00380 __int64); 00381 __int64 _InterlockedCompareExchange64_HLERelease(__int64 volatile *, __int64, 00382 __int64); 00383 __int64 _InterlockedCompareExchange64_np(__int64 volatile *_Destination, 00384 __int64 _Exchange, __int64 _Comparand); 00385 void *_InterlockedCompareExchangePointer(void *volatile *_Destination, 00386 void *_Exchange, void *_Comparand); 00387 void *_InterlockedCompareExchangePointer_np(void *volatile *_Destination, 00388 void *_Exchange, void *_Comparand); 00389 static __inline__ 00390 __int64 _InterlockedDecrement64(__int64 volatile *_Addend); 00391 static __inline__ 00392 __int64 _InterlockedExchange64(__int64 volatile *_Target, __int64 _Value); 00393 static __inline__ 00394 __int64 _InterlockedExchangeAdd64(__int64 volatile *_Addend, __int64 _Value); 00395 void *_InterlockedExchangePointer(void *volatile *_Target, void *_Value); 00396 static __inline__ 00397 __int64 _InterlockedIncrement64(__int64 volatile *_Addend); 00398 long _InterlockedOr_np(long volatile *_Value, long _Mask); 00399 short _InterlockedOr16_np(short volatile *_Value, short _Mask); 00400 static __inline__ 00401 __int64 _InterlockedOr64(__int64 volatile *_Value, __int64 _Mask); 00402 __int64 _InterlockedOr64_np(__int64 volatile *_Value, __int64 _Mask); 00403 char _InterlockedOr8_np(char volatile *_Value, char _Mask); 00404 long _InterlockedXor_np(long volatile *_Value, long _Mask); 00405 short _InterlockedXor16_np(short volatile *_Value, short _Mask); 00406 static __inline__ 00407 __int64 _InterlockedXor64(__int64 volatile *_Value, __int64 _Mask); 00408 __int64 _InterlockedXor64_np(__int64 volatile *_Value, __int64 _Mask); 00409 char _InterlockedXor8_np(char volatile *_Value, char _Mask); 00410 static __inline__ 00411 __int64 _mul128(__int64 _Multiplier, __int64 _Multiplicand, 00412 __int64 *_HighProduct); 00413 unsigned __int64 _rorx_u64(unsigned __int64, const unsigned int); 00414 __int64 _sarx_i64(__int64, unsigned int); 00415 #if __STDC_HOSTED__ 00416 int __cdecl _setjmpex(jmp_buf); 00417 #endif 00418 unsigned __int64 _shlx_u64(unsigned __int64, unsigned int); 00419 unsigned __int64 shrx_u64(unsigned __int64, unsigned int); 00420 unsigned __int64 _umul128(unsigned __int64 _Multiplier, 00421 unsigned __int64 _Multiplicand, 00422 unsigned __int64 *_HighProduct); 00423 void __cdecl _xrstor64(void const *, unsigned __int64); 00424 void __cdecl _xsave64(void *, unsigned __int64); 00425 void __cdecl _xsaveopt64(void *, unsigned __int64); 00426 00427 #endif /* __x86_64__ */ 00428 00429 /*----------------------------------------------------------------------------*\ 00430 |* Bit Twiddling 00431 \*----------------------------------------------------------------------------*/ 00432 static __inline__ unsigned char __attribute__((__always_inline__, __nodebug__)) 00433 _rotl8(unsigned char _Value, unsigned char _Shift) { 00434 _Shift &= 0x7; 00435 return _Shift ? (_Value << _Shift) | (_Value >> (8 - _Shift)) : _Value; 00436 } 00437 static __inline__ unsigned char __attribute__((__always_inline__, __nodebug__)) 00438 _rotr8(unsigned char _Value, unsigned char _Shift) { 00439 _Shift &= 0x7; 00440 return _Shift ? (_Value >> _Shift) | (_Value << (8 - _Shift)) : _Value; 00441 } 00442 static __inline__ unsigned short __attribute__((__always_inline__, __nodebug__)) 00443 _rotl16(unsigned short _Value, unsigned char _Shift) { 00444 _Shift &= 0xf; 00445 return _Shift ? (_Value << _Shift) | (_Value >> (16 - _Shift)) : _Value; 00446 } 00447 static __inline__ unsigned short __attribute__((__always_inline__, __nodebug__)) 00448 _rotr16(unsigned short _Value, unsigned char _Shift) { 00449 _Shift &= 0xf; 00450 return _Shift ? (_Value >> _Shift) | (_Value << (16 - _Shift)) : _Value; 00451 } 00452 static __inline__ unsigned int __attribute__((__always_inline__, __nodebug__)) 00453 _rotl(unsigned int _Value, int _Shift) { 00454 _Shift &= 0x1f; 00455 return _Shift ? (_Value << _Shift) | (_Value >> (32 - _Shift)) : _Value; 00456 } 00457 static __inline__ unsigned int __attribute__((__always_inline__, __nodebug__)) 00458 _rotr(unsigned int _Value, int _Shift) { 00459 _Shift &= 0x1f; 00460 return _Shift ? (_Value >> _Shift) | (_Value << (32 - _Shift)) : _Value; 00461 } 00462 static __inline__ unsigned long __attribute__((__always_inline__, __nodebug__)) 00463 _lrotl(unsigned long _Value, int _Shift) { 00464 _Shift &= 0x1f; 00465 return _Shift ? (_Value << _Shift) | (_Value >> (32 - _Shift)) : _Value; 00466 } 00467 static __inline__ unsigned long __attribute__((__always_inline__, __nodebug__)) 00468 _lrotr(unsigned long _Value, int _Shift) { 00469 _Shift &= 0x1f; 00470 return _Shift ? (_Value >> _Shift) | (_Value << (32 - _Shift)) : _Value; 00471 } 00472 static 00473 __inline__ unsigned __int64 __attribute__((__always_inline__, __nodebug__)) 00474 _rotl64(unsigned __int64 _Value, int _Shift) { 00475 _Shift &= 0x3f; 00476 return _Shift ? (_Value << _Shift) | (_Value >> (64 - _Shift)) : _Value; 00477 } 00478 static 00479 __inline__ unsigned __int64 __attribute__((__always_inline__, __nodebug__)) 00480 _rotr64(unsigned __int64 _Value, int _Shift) { 00481 _Shift &= 0x3f; 00482 return _Shift ? (_Value >> _Shift) | (_Value << (64 - _Shift)) : _Value; 00483 } 00484 /*----------------------------------------------------------------------------*\ 00485 |* Bit Counting and Testing 00486 \*----------------------------------------------------------------------------*/ 00487 static __inline__ unsigned char __attribute__((__always_inline__, __nodebug__)) 00488 _BitScanForward(unsigned long *_Index, unsigned long _Mask) { 00489 if (!_Mask) 00490 return 0; 00491 *_Index = __builtin_ctzl(_Mask); 00492 return 1; 00493 } 00494 static __inline__ unsigned char __attribute__((__always_inline__, __nodebug__)) 00495 _BitScanReverse(unsigned long *_Index, unsigned long _Mask) { 00496 if (!_Mask) 00497 return 0; 00498 *_Index = 31 - __builtin_clzl(_Mask); 00499 return 1; 00500 } 00501 static __inline__ unsigned short __attribute__((__always_inline__, __nodebug__)) 00502 __popcnt16(unsigned short value) { 00503 return __builtin_popcount((int)value); 00504 } 00505 static __inline__ unsigned int __attribute__((__always_inline__, __nodebug__)) 00506 __popcnt(unsigned int value) { 00507 return __builtin_popcount(value); 00508 } 00509 static __inline__ unsigned char __attribute__((__always_inline__, __nodebug__)) 00510 _bittest(long const *a, long b) { 00511 return (*a >> b) & 1; 00512 } 00513 static __inline__ unsigned char __attribute__((__always_inline__, __nodebug__)) 00514 _bittestandcomplement(long *a, long b) { 00515 unsigned char x = (*a >> b) & 1; 00516 *a = *a ^ (1 << b); 00517 return x; 00518 } 00519 static __inline__ unsigned char __attribute__((__always_inline__, __nodebug__)) 00520 _bittestandreset(long *a, long b) { 00521 unsigned char x = (*a >> b) & 1; 00522 *a = *a & ~(1 << b); 00523 return x; 00524 } 00525 static __inline__ unsigned char __attribute__((__always_inline__, __nodebug__)) 00526 _bittestandset(long *a, long b) { 00527 unsigned char x = (*a >> b) & 1; 00528 *a = *a | (1 << b); 00529 return x; 00530 } 00531 #if defined(__i386__) || defined(__x86_64__) 00532 static __inline__ unsigned char __attribute__((__always_inline__, __nodebug__)) 00533 _interlockedbittestandset(long volatile *__BitBase, long __BitPos) { 00534 unsigned char __Res; 00535 __asm__ ("xor %0, %0\n" 00536 "lock bts %2, %1\n" 00537 "setc %0\n" 00538 : "=r" (__Res), "+m"(*__BitBase) 00539 : "Ir"(__BitPos)); 00540 return __Res; 00541 } 00542 #endif 00543 #ifdef __x86_64__ 00544 static __inline__ unsigned char __attribute__((__always_inline__, __nodebug__)) 00545 _BitScanForward64(unsigned long *_Index, unsigned __int64 _Mask) { 00546 if (!_Mask) 00547 return 0; 00548 *_Index = __builtin_ctzll(_Mask); 00549 return 1; 00550 } 00551 static __inline__ unsigned char __attribute__((__always_inline__, __nodebug__)) 00552 _BitScanReverse64(unsigned long *_Index, unsigned __int64 _Mask) { 00553 if (!_Mask) 00554 return 0; 00555 *_Index = 63 - __builtin_clzll(_Mask); 00556 return 1; 00557 } 00558 static __inline__ 00559 unsigned __int64 __attribute__((__always_inline__, __nodebug__)) 00560 __popcnt64(unsigned __int64 value) { 00561 return __builtin_popcountll(value); 00562 } 00563 static __inline__ unsigned char __attribute__((__always_inline__, __nodebug__)) 00564 _bittest64(__int64 const *a, __int64 b) { 00565 return (*a >> b) & 1; 00566 } 00567 static __inline__ unsigned char __attribute__((__always_inline__, __nodebug__)) 00568 _bittestandcomplement64(__int64 *a, __int64 b) { 00569 unsigned char x = (*a >> b) & 1; 00570 *a = *a ^ (1ll << b); 00571 return x; 00572 } 00573 static __inline__ unsigned char __attribute__((__always_inline__, __nodebug__)) 00574 _bittestandreset64(__int64 *a, __int64 b) { 00575 unsigned char x = (*a >> b) & 1; 00576 *a = *a & ~(1ll << b); 00577 return x; 00578 } 00579 static __inline__ unsigned char __attribute__((__always_inline__, __nodebug__)) 00580 _bittestandset64(__int64 *a, __int64 b) { 00581 unsigned char x = (*a >> b) & 1; 00582 *a = *a | (1ll << b); 00583 return x; 00584 } 00585 static __inline__ unsigned char __attribute__((__always_inline__, __nodebug__)) 00586 _interlockedbittestandset64(__int64 volatile *__BitBase, __int64 __BitPos) { 00587 unsigned char __Res; 00588 __asm__ ("xor %0, %0\n" 00589 "lock bts %2, %1\n" 00590 "setc %0\n" 00591 : "=r" (__Res), "+m"(*__BitBase) 00592 : "Ir"(__BitPos)); 00593 return __Res; 00594 } 00595 #endif 00596 /*----------------------------------------------------------------------------*\ 00597 |* Interlocked Exchange Add 00598 \*----------------------------------------------------------------------------*/ 00599 static __inline__ char __attribute__((__always_inline__, __nodebug__)) 00600 _InterlockedExchangeAdd8(char volatile *_Addend, char _Value) { 00601 return __atomic_add_fetch(_Addend, _Value, 0) - _Value; 00602 } 00603 static __inline__ short __attribute__((__always_inline__, __nodebug__)) 00604 _InterlockedExchangeAdd16(short volatile *_Addend, short _Value) { 00605 return __atomic_add_fetch(_Addend, _Value, 0) - _Value; 00606 } 00607 #ifdef __x86_64__ 00608 static __inline__ __int64 __attribute__((__always_inline__, __nodebug__)) 00609 _InterlockedExchangeAdd64(__int64 volatile *_Addend, __int64 _Value) { 00610 return __atomic_add_fetch(_Addend, _Value, 0) - _Value; 00611 } 00612 #endif 00613 /*----------------------------------------------------------------------------*\ 00614 |* Interlocked Exchange Sub 00615 \*----------------------------------------------------------------------------*/ 00616 static __inline__ char __attribute__((__always_inline__, __nodebug__)) 00617 _InterlockedExchangeSub8(char volatile *_Subend, char _Value) { 00618 return __atomic_sub_fetch(_Subend, _Value, 0) + _Value; 00619 } 00620 static __inline__ short __attribute__((__always_inline__, __nodebug__)) 00621 _InterlockedExchangeSub16(short volatile *_Subend, short _Value) { 00622 return __atomic_sub_fetch(_Subend, _Value, 0) + _Value; 00623 } 00624 static __inline__ long __attribute__((__always_inline__, __nodebug__)) 00625 _InterlockedExchangeSub(long volatile *_Subend, long _Value) { 00626 return __atomic_sub_fetch(_Subend, _Value, 0) + _Value; 00627 } 00628 #ifdef __x86_64__ 00629 static __inline__ __int64 __attribute__((__always_inline__, __nodebug__)) 00630 _InterlockedExchangeSub64(__int64 volatile *_Subend, __int64 _Value) { 00631 return __atomic_sub_fetch(_Subend, _Value, 0) + _Value; 00632 } 00633 #endif 00634 /*----------------------------------------------------------------------------*\ 00635 |* Interlocked Increment 00636 \*----------------------------------------------------------------------------*/ 00637 static __inline__ short __attribute__((__always_inline__, __nodebug__)) 00638 _InterlockedIncrement16(short volatile *_Value) { 00639 return __atomic_add_fetch(_Value, 1, 0); 00640 } 00641 #ifdef __x86_64__ 00642 static __inline__ __int64 __attribute__((__always_inline__, __nodebug__)) 00643 _InterlockedIncrement64(__int64 volatile *_Value) { 00644 return __atomic_add_fetch(_Value, 1, 0); 00645 } 00646 #endif 00647 /*----------------------------------------------------------------------------*\ 00648 |* Interlocked Decrement 00649 \*----------------------------------------------------------------------------*/ 00650 static __inline__ short __attribute__((__always_inline__, __nodebug__)) 00651 _InterlockedDecrement16(short volatile *_Value) { 00652 return __atomic_sub_fetch(_Value, 1, 0); 00653 } 00654 #ifdef __x86_64__ 00655 static __inline__ __int64 __attribute__((__always_inline__, __nodebug__)) 00656 _InterlockedDecrement64(__int64 volatile *_Value) { 00657 return __atomic_sub_fetch(_Value, 1, 0); 00658 } 00659 #endif 00660 /*----------------------------------------------------------------------------*\ 00661 |* Interlocked And 00662 \*----------------------------------------------------------------------------*/ 00663 static __inline__ char __attribute__((__always_inline__, __nodebug__)) 00664 _InterlockedAnd8(char volatile *_Value, char _Mask) { 00665 return __atomic_and_fetch(_Value, _Mask, 0); 00666 } 00667 static __inline__ short __attribute__((__always_inline__, __nodebug__)) 00668 _InterlockedAnd16(short volatile *_Value, short _Mask) { 00669 return __atomic_and_fetch(_Value, _Mask, 0); 00670 } 00671 static __inline__ long __attribute__((__always_inline__, __nodebug__)) 00672 _InterlockedAnd(long volatile *_Value, long _Mask) { 00673 return __atomic_and_fetch(_Value, _Mask, 0); 00674 } 00675 #ifdef __x86_64__ 00676 static __inline__ __int64 __attribute__((__always_inline__, __nodebug__)) 00677 _InterlockedAnd64(__int64 volatile *_Value, __int64 _Mask) { 00678 return __atomic_and_fetch(_Value, _Mask, 0); 00679 } 00680 #endif 00681 /*----------------------------------------------------------------------------*\ 00682 |* Interlocked Or 00683 \*----------------------------------------------------------------------------*/ 00684 static __inline__ char __attribute__((__always_inline__, __nodebug__)) 00685 _InterlockedOr8(char volatile *_Value, char _Mask) { 00686 return __atomic_or_fetch(_Value, _Mask, 0); 00687 } 00688 static __inline__ short __attribute__((__always_inline__, __nodebug__)) 00689 _InterlockedOr16(short volatile *_Value, short _Mask) { 00690 return __atomic_or_fetch(_Value, _Mask, 0); 00691 } 00692 static __inline__ long __attribute__((__always_inline__, __nodebug__)) 00693 _InterlockedOr(long volatile *_Value, long _Mask) { 00694 return __atomic_or_fetch(_Value, _Mask, 0); 00695 } 00696 #ifdef __x86_64__ 00697 static __inline__ __int64 __attribute__((__always_inline__, __nodebug__)) 00698 _InterlockedOr64(__int64 volatile *_Value, __int64 _Mask) { 00699 return __atomic_or_fetch(_Value, _Mask, 0); 00700 } 00701 #endif 00702 /*----------------------------------------------------------------------------*\ 00703 |* Interlocked Xor 00704 \*----------------------------------------------------------------------------*/ 00705 static __inline__ char __attribute__((__always_inline__, __nodebug__)) 00706 _InterlockedXor8(char volatile *_Value, char _Mask) { 00707 return __atomic_xor_fetch(_Value, _Mask, 0); 00708 } 00709 static __inline__ short __attribute__((__always_inline__, __nodebug__)) 00710 _InterlockedXor16(short volatile *_Value, short _Mask) { 00711 return __atomic_xor_fetch(_Value, _Mask, 0); 00712 } 00713 static __inline__ long __attribute__((__always_inline__, __nodebug__)) 00714 _InterlockedXor(long volatile *_Value, long _Mask) { 00715 return __atomic_xor_fetch(_Value, _Mask, 0); 00716 } 00717 #ifdef __x86_64__ 00718 static __inline__ __int64 __attribute__((__always_inline__, __nodebug__)) 00719 _InterlockedXor64(__int64 volatile *_Value, __int64 _Mask) { 00720 return __atomic_xor_fetch(_Value, _Mask, 0); 00721 } 00722 #endif 00723 /*----------------------------------------------------------------------------*\ 00724 |* Interlocked Exchange 00725 \*----------------------------------------------------------------------------*/ 00726 static __inline__ char __attribute__((__always_inline__, __nodebug__)) 00727 _InterlockedExchange8(char volatile *_Target, char _Value) { 00728 __atomic_exchange(_Target, &_Value, &_Value, 0); 00729 return _Value; 00730 } 00731 static __inline__ short __attribute__((__always_inline__, __nodebug__)) 00732 _InterlockedExchange16(short volatile *_Target, short _Value) { 00733 __atomic_exchange(_Target, &_Value, &_Value, 0); 00734 return _Value; 00735 } 00736 #ifdef __x86_64__ 00737 static __inline__ __int64 __attribute__((__always_inline__, __nodebug__)) 00738 _InterlockedExchange64(__int64 volatile *_Target, __int64 _Value) { 00739 __atomic_exchange(_Target, &_Value, &_Value, 0); 00740 return _Value; 00741 } 00742 #endif 00743 /*----------------------------------------------------------------------------*\ 00744 |* Interlocked Compare Exchange 00745 \*----------------------------------------------------------------------------*/ 00746 static __inline__ char __attribute__((__always_inline__, __nodebug__)) 00747 _InterlockedCompareExchange8(char volatile *_Destination, 00748 char _Exchange, char _Comparand) { 00749 __atomic_compare_exchange(_Destination, &_Comparand, &_Exchange, 0, 0, 0); 00750 return _Comparand; 00751 } 00752 static __inline__ short __attribute__((__always_inline__, __nodebug__)) 00753 _InterlockedCompareExchange16(short volatile *_Destination, 00754 short _Exchange, short _Comparand) { 00755 __atomic_compare_exchange(_Destination, &_Comparand, &_Exchange, 0, 0, 0); 00756 return _Comparand; 00757 } 00758 static __inline__ __int64 __attribute__((__always_inline__, __nodebug__)) 00759 _InterlockedCompareExchange64(__int64 volatile *_Destination, 00760 __int64 _Exchange, __int64 _Comparand) { 00761 __atomic_compare_exchange(_Destination, &_Comparand, &_Exchange, 0, 0, 0); 00762 return _Comparand; 00763 } 00764 /*----------------------------------------------------------------------------*\ 00765 |* Barriers 00766 \*----------------------------------------------------------------------------*/ 00767 #if defined(__i386__) || defined(__x86_64__) 00768 static __inline__ void __attribute__((__always_inline__, __nodebug__)) 00769 __attribute__((deprecated("use other intrinsics or C++11 atomics instead"))) 00770 _ReadWriteBarrier(void) { 00771 __asm__ volatile ("" : : : "memory"); 00772 } 00773 static __inline__ void __attribute__((__always_inline__, __nodebug__)) 00774 __attribute__((deprecated("use other intrinsics or C++11 atomics instead"))) 00775 _ReadBarrier(void) { 00776 __asm__ volatile ("" : : : "memory"); 00777 } 00778 static __inline__ void __attribute__((__always_inline__, __nodebug__)) 00779 __attribute__((deprecated("use other intrinsics or C++11 atomics instead"))) 00780 _WriteBarrier(void) { 00781 __asm__ volatile ("" : : : "memory"); 00782 } 00783 #endif 00784 #ifdef __x86_64__ 00785 static __inline__ void __attribute__((__always_inline__, __nodebug__)) 00786 __faststorefence(void) { 00787 __asm__ volatile("lock orq $0, (%%rsp)" : : : "memory"); 00788 } 00789 #endif 00790 /*----------------------------------------------------------------------------*\ 00791 |* readfs, readgs 00792 |* (Pointers in address space #256 and #257 are relative to the GS and FS 00793 |* segment registers, respectively.) 00794 \*----------------------------------------------------------------------------*/ 00795 #define __ptr_to_addr_space(__addr_space_nbr, __type, __offset) \ 00796 ((volatile __type __attribute__((__address_space__(__addr_space_nbr)))*) \ 00797 (__offset)) 00798 00799 #ifdef __i386__ 00800 static __inline__ unsigned char __attribute__((__always_inline__, __nodebug__)) 00801 __readfsbyte(unsigned long __offset) { 00802 return *__ptr_to_addr_space(257, unsigned char, __offset); 00803 } 00804 static __inline__ unsigned __int64 __attribute__((__always_inline__, __nodebug__)) 00805 __readfsqword(unsigned long __offset) { 00806 return *__ptr_to_addr_space(257, unsigned __int64, __offset); 00807 } 00808 static __inline__ unsigned short __attribute__((__always_inline__, __nodebug__)) 00809 __readfsword(unsigned long __offset) { 00810 return *__ptr_to_addr_space(257, unsigned short, __offset); 00811 } 00812 #endif 00813 #ifdef __x86_64__ 00814 static __inline__ unsigned char __attribute__((__always_inline__, __nodebug__)) 00815 __readgsbyte(unsigned long __offset) { 00816 return *__ptr_to_addr_space(256, unsigned char, __offset); 00817 } 00818 static __inline__ unsigned long __attribute__((__always_inline__, __nodebug__)) 00819 __readgsdword(unsigned long __offset) { 00820 return *__ptr_to_addr_space(256, unsigned long, __offset); 00821 } 00822 static __inline__ unsigned __int64 __attribute__((__always_inline__, __nodebug__)) 00823 __readgsqword(unsigned long __offset) { 00824 return *__ptr_to_addr_space(256, unsigned __int64, __offset); 00825 } 00826 static __inline__ unsigned short __attribute__((__always_inline__, __nodebug__)) 00827 __readgsword(unsigned long __offset) { 00828 return *__ptr_to_addr_space(256, unsigned short, __offset); 00829 } 00830 #endif 00831 #undef __ptr_to_addr_space 00832 /*----------------------------------------------------------------------------*\ 00833 |* movs, stos 00834 \*----------------------------------------------------------------------------*/ 00835 #if defined(__i386__) || defined(__x86_64__) 00836 static __inline__ void __attribute__((__always_inline__, __nodebug__)) 00837 __movsb(unsigned char *__dst, unsigned char const *__src, size_t __n) { 00838 __asm__("rep movsb" : : "D"(__dst), "S"(__src), "c"(__n) 00839 : "%edi", "%esi", "%ecx"); 00840 } 00841 static __inline__ void __attribute__((__always_inline__, __nodebug__)) 00842 __movsd(unsigned long *__dst, unsigned long const *__src, size_t __n) { 00843 __asm__("rep movsl" : : "D"(__dst), "S"(__src), "c"(__n) 00844 : "%edi", "%esi", "%ecx"); 00845 } 00846 static __inline__ void __attribute__((__always_inline__, __nodebug__)) 00847 __movsw(unsigned short *__dst, unsigned short const *__src, size_t __n) { 00848 __asm__("rep movsh" : : "D"(__dst), "S"(__src), "c"(__n) 00849 : "%edi", "%esi", "%ecx"); 00850 } 00851 static __inline__ void __attribute__((__always_inline__, __nodebug__)) 00852 __stosb(unsigned char *__dst, unsigned char __x, size_t __n) { 00853 __asm__("rep stosb" : : "D"(__dst), "a"(__x), "c"(__n) 00854 : "%edi", "%ecx"); 00855 } 00856 static __inline__ void __attribute__((__always_inline__, __nodebug__)) 00857 __stosd(unsigned long *__dst, unsigned long __x, size_t __n) { 00858 __asm__("rep stosl" : : "D"(__dst), "a"(__x), "c"(__n) 00859 : "%edi", "%ecx"); 00860 } 00861 static __inline__ void __attribute__((__always_inline__, __nodebug__)) 00862 __stosw(unsigned short *__dst, unsigned short __x, size_t __n) { 00863 __asm__("rep stosh" : : "D"(__dst), "a"(__x), "c"(__n) 00864 : "%edi", "%ecx"); 00865 } 00866 #endif 00867 #ifdef __x86_64__ 00868 static __inline__ void __attribute__((__always_inline__, __nodebug__)) 00869 __movsq(unsigned long long *__dst, unsigned long long const *__src, size_t __n) { 00870 __asm__("rep movsq" : : "D"(__dst), "S"(__src), "c"(__n) 00871 : "%edi", "%esi", "%ecx"); 00872 } 00873 static __inline__ void __attribute__((__always_inline__, __nodebug__)) 00874 __stosq(unsigned __int64 *__dst, unsigned __int64 __x, size_t __n) { 00875 __asm__("rep stosq" : : "D"(__dst), "a"(__x), "c"(__n) 00876 : "%edi", "%ecx"); 00877 } 00878 #endif 00879 00880 /*----------------------------------------------------------------------------*\ 00881 |* Misc 00882 \*----------------------------------------------------------------------------*/ 00883 static __inline__ void * __attribute__((__always_inline__, __nodebug__)) 00884 _AddressOfReturnAddress(void) { 00885 return (void*)((char*)__builtin_frame_address(0) + sizeof(void*)); 00886 } 00887 static __inline__ void * __attribute__((__always_inline__, __nodebug__)) 00888 _ReturnAddress(void) { 00889 return __builtin_return_address(0); 00890 } 00891 #if defined(__i386__) || defined(__x86_64__) 00892 static __inline__ void __attribute__((__always_inline__, __nodebug__)) 00893 __cpuid(int __info[4], int __level) { 00894 __asm__ ("cpuid" : "=a"(__info[0]), "=b" (__info[1]), "=c"(__info[2]), "=d"(__info[3]) 00895 : "a"(__level)); 00896 } 00897 static __inline__ void __attribute__((__always_inline__, __nodebug__)) 00898 __cpuidex(int __info[4], int __level, int __ecx) { 00899 __asm__ ("cpuid" : "=a"(__info[0]), "=b" (__info[1]), "=c"(__info[2]), "=d"(__info[3]) 00900 : "a"(__level), "c"(__ecx)); 00901 } 00902 static __inline__ unsigned __int64 __cdecl __attribute__((__always_inline__, __nodebug__)) 00903 _xgetbv(unsigned int __xcr_no) { 00904 unsigned int __eax, __edx; 00905 __asm__ ("xgetbv" : "=a" (__eax), "=d" (__edx) : "c" (__xcr_no)); 00906 return ((unsigned __int64)__edx << 32) | __eax; 00907 } 00908 static __inline__ void __attribute__((__always_inline__, __nodebug__)) 00909 __halt(void) { 00910 __asm__ volatile ("hlt"); 00911 } 00912 #endif 00913 00914 /*----------------------------------------------------------------------------*\ 00915 |* Privileged intrinsics 00916 \*----------------------------------------------------------------------------*/ 00917 #if defined(__i386__) || defined(__x86_64__) 00918 static __inline__ unsigned __int64 __attribute__((__always_inline__, __nodebug__)) 00919 __readmsr(unsigned long __register) { 00920 // Loads the contents of a 64-bit model specific register (MSR) specified in 00921 // the ECX register into registers EDX:EAX. The EDX register is loaded with 00922 // the high-order 32 bits of the MSR and the EAX register is loaded with the 00923 // low-order 32 bits. If less than 64 bits are implemented in the MSR being 00924 // read, the values returned to EDX:EAX in unimplemented bit locations are 00925 // undefined. 00926 unsigned long __edx; 00927 unsigned long __eax; 00928 __asm__ ("rdmsr" : "=d"(__edx), "=a"(__eax) : "c"(__register)); 00929 return (((unsigned __int64)__edx) << 32) | (unsigned __int64)__eax; 00930 } 00931 00932 static __inline__ unsigned long __attribute__((always_inline, __nodebug__)) 00933 __readcr3(void) { 00934 unsigned long __cr3_val; 00935 __asm__ __volatile__ ("mov %%cr3, %0" : "=q"(__cr3_val) : : "memory"); 00936 return __cr3_val; 00937 } 00938 00939 static __inline__ void __attribute__((always_inline, __nodebug__)) 00940 __writecr3(unsigned int __cr3_val) { 00941 __asm__ ("mov %0, %%cr3" : : "q"(__cr3_val) : "memory"); 00942 } 00943 #endif 00944 00945 #ifdef __cplusplus 00946 } 00947 #endif 00948 00949 #endif /* __INTRIN_H */ 00950 #endif /* _MSC_VER */