2009-06-18 08:20:19 +00:00
|
|
|
/*
|
|
|
|
Compatibility <intrin_x86.h> header for GCC -- GCC equivalents of intrinsic
|
|
|
|
Microsoft Visual C++ functions. Originally developed for the ReactOS
|
|
|
|
(<http://www.reactos.org/>) and TinyKrnl (<http://www.tinykrnl.org/>)
|
|
|
|
projects.
|
|
|
|
|
|
|
|
Copyright (c) 2006 KJK::Hyperion <hackbunny@reactos.com>
|
|
|
|
|
|
|
|
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.
|
|
|
|
*/
|
|
|
|
|
2014-08-13 03:08:19 +00:00
|
|
|
#pragma once
|
2009-06-18 08:20:19 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
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
|
|
|
|
|
2014-08-13 03:08:19 +00:00
|
|
|
NOTE: GCC and Clang both support __sync_* built-ins for barriers and
|
|
|
|
atomic operations.
|
2009-06-18 08:20:19 +00:00
|
|
|
|
|
|
|
Pay attention to the type of barrier. Make it match with what Visual C++
|
|
|
|
would use in the same case
|
|
|
|
*/
|
|
|
|
|
2009-06-18 09:32:35 +00:00
|
|
|
|
2009-06-18 08:20:19 +00:00
|
|
|
/*** Atomic operations ***/
|
|
|
|
|
2015-01-04 23:25:37 +00:00
|
|
|
static __inline__ __attribute__((always_inline)) s32 _InterlockedCompareExchange(volatile s32 * const Destination, const s32 Exchange, const s32 Comperand)
|
2009-06-18 08:20:19 +00:00
|
|
|
{
|
|
|
|
return __sync_val_compare_and_swap(Destination, Comperand, Exchange);
|
|
|
|
}
|
|
|
|
|
2015-01-04 23:25:37 +00:00
|
|
|
static __inline__ __attribute__((always_inline)) s64 _InterlockedCompareExchange64(volatile s64 * const Destination, const s64 Exchange, const s64 Comperand)
|
2009-06-18 08:20:19 +00:00
|
|
|
{
|
|
|
|
return __sync_val_compare_and_swap(Destination, Comperand, Exchange);
|
|
|
|
}
|
|
|
|
|
2015-01-04 23:25:37 +00:00
|
|
|
static __inline__ __attribute__((always_inline)) s32 _InterlockedExchange(volatile s32 * const Target, const s32 Value)
|
2009-06-18 08:20:19 +00:00
|
|
|
{
|
|
|
|
/* 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);
|
|
|
|
}
|
|
|
|
|
2015-01-04 23:25:37 +00:00
|
|
|
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)
|
2009-06-18 08:20:19 +00:00
|
|
|
{
|
|
|
|
return __sync_fetch_and_add(Addend, Value);
|
|
|
|
}
|
|
|
|
|
2015-01-04 23:25:37 +00:00
|
|
|
static __inline__ __attribute__((always_inline)) s32 _InterlockedDecrement(volatile s32 * const lpAddend)
|
2009-06-18 08:20:19 +00:00
|
|
|
{
|
|
|
|
return _InterlockedExchangeAdd(lpAddend, -1) - 1;
|
|
|
|
}
|
|
|
|
|
2015-01-04 23:25:37 +00:00
|
|
|
static __inline__ __attribute__((always_inline)) s32 _InterlockedIncrement(volatile s32 * const lpAddend)
|
2009-06-18 08:20:19 +00:00
|
|
|
{
|
|
|
|
return _InterlockedExchangeAdd(lpAddend, 1) + 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*** System information ***/
|
|
|
|
static __inline__ __attribute__((always_inline)) void __cpuid(int CPUInfo[], const int InfoType)
|
|
|
|
{
|
2015-11-29 10:52:50 +00:00
|
|
|
__asm__ __volatile__("cpuid": "=a" (CPUInfo[0]), "=b" (CPUInfo[1]), "=c" (CPUInfo[2]), "=d" (CPUInfo[3]) : "a" (InfoType));
|
2009-06-18 08:20:19 +00:00
|
|
|
}
|
|
|
|
|
2015-11-29 10:52:50 +00:00
|
|
|
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)
|
2011-02-12 21:45:16 +00:00
|
|
|
{
|
|
|
|
unsigned int eax, edx;
|
|
|
|
__asm__ __volatile__("xgetbv" : "=a"(eax), "=d"(edx) : "c"(index));
|
|
|
|
return ((unsigned long long)edx << 32) | eax;
|
|
|
|
}
|
|
|
|
|
2009-06-18 08:20:19 +00:00
|
|
|
/*** Interrupts ***/
|
2014-08-03 18:11:22 +00:00
|
|
|
#ifndef __linux__
|
2009-06-18 08:20:19 +00:00
|
|
|
static __inline__ __attribute__((always_inline)) void __debugbreak(void)
|
|
|
|
{
|
|
|
|
__asm__("int $3");
|
|
|
|
}
|
2013-07-03 18:42:05 +00:00
|
|
|
#endif
|