/* 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 _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); } /*** System information ***/ 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