/* Compatibility header for GCC -- GCC equivalents of intrinsic Microsoft Visual C++ functions. Originally developed for the ReactOS () and TinyKrnl () projects. Copyright (c) 2006 KJK::Hyperion Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ #pragma once /* NOTE: this is a *compatibility* header. Some functions may look wrong at first, but they're only "as wrong" as they would be on Visual C++. Our priority is compatibility NOTE: unlike most people who write inline asm for GCC, I didn't pull the constraints and the uses of __volatile__ out of my... hat. Do not touch them. I hate cargo cult programming NOTE: review all intrinsics with a return value, add/remove __volatile__ where necessary. If an intrinsic whose value is ignored generates a no-op under Visual C++, __volatile__ must be omitted; if it always generates code (for example, if it has side effects), __volatile__ must be specified. GCC will only optimize out non-volatile asm blocks with outputs, so input-only blocks are safe. Oddities such as the non-volatile 'rdmsr' are intentional and follow Visual C++ behavior NOTE: GCC and Clang both support __sync_* built-ins for barriers and atomic operations. Pay attention to the type of barrier. Make it match with what Visual C++ would use in the same case */ /*** Atomic operations ***/ static __inline__ __attribute__((always_inline)) s32 _InterlockedCompareExchange(volatile s32 * const Destination, const s32 Exchange, const s32 Comperand) { return __sync_val_compare_and_swap(Destination, Comperand, Exchange); } static __inline__ __attribute__((always_inline)) s64 _InterlockedCompareExchange64(volatile s64 * const Destination, const s64 Exchange, const s64 Comperand) { return __sync_val_compare_and_swap(Destination, Comperand, Exchange); } static __inline__ __attribute__((always_inline)) s32 _InterlockedExchange(volatile s32 * const Target, const s32 Value) { /* NOTE: __sync_lock_test_and_set would be an acquire barrier, so we force a full barrier */ __sync_synchronize(); return __sync_lock_test_and_set(Target, Value); } static __inline__ __attribute__((always_inline)) s64 _InterlockedExchange64(volatile s64 * const Target, const s64 Value) { /* NOTE: __sync_lock_test_and_set would be an acquire barrier, so we force a full barrier */ __sync_synchronize(); return __sync_lock_test_and_set(Target, Value); } static __inline__ __attribute__((always_inline)) s32 _InterlockedExchangeAdd(volatile s32 * const Addend, const s32 Value) { return __sync_fetch_and_add(Addend, Value); } static __inline__ __attribute__((always_inline)) s32 _InterlockedDecrement(volatile s32 * const lpAddend) { return _InterlockedExchangeAdd(lpAddend, -1) - 1; } static __inline__ __attribute__((always_inline)) s32 _InterlockedIncrement(volatile s32 * const lpAddend) { return _InterlockedExchangeAdd(lpAddend, 1) + 1; } /*** System information ***/ static __inline__ __attribute__((always_inline)) void __cpuid(int CPUInfo[], const int InfoType) { __asm__ __volatile__("cpuid": "=a" (CPUInfo[0]), "=b" (CPUInfo[1]), "=c" (CPUInfo[2]), "=d" (CPUInfo[3]) : "a" (InfoType)); } static __inline__ __attribute__((always_inline)) void __cpuidex(int CPUInfo[], const int level, const int count) { __asm__ __volatile__("cpuid": "=a" (CPUInfo[0]), "=b" (CPUInfo[1]), "=c" (CPUInfo[2]), "=d" (CPUInfo[3]) : "a" (level), "c" (count)); } static __inline__ __attribute__((always_inline)) unsigned long long _xgetbv(unsigned int index) { unsigned int eax, edx; __asm__ __volatile__("xgetbv" : "=a"(eax), "=d"(edx) : "c"(index)); return ((unsigned long long)edx << 32) | eax; } /*** Interrupts ***/ #ifndef __linux__ static __inline__ __attribute__((always_inline)) void __debugbreak(void) { __asm__("int $3"); } #endif