platformIntrinsics.gcc.h
Engine/source/platform/platformIntrinsics.gcc.h
Compiler intrinsics for GCC.
Public Functions
dAtomicRead(volatile U32 & ref)
Performs an atomic read operation.
bool
dCompareAndSwap(volatile U32 & ref, U32 oldVal, U32 newVal)
bool
dCompareAndSwap(volatile U64 & ref, U64 oldVal, U64 newVal)
dFetchAndAdd(volatile S32 & ref, S32 val)
dFetchAndAdd(volatile U32 & ref, U32 val)
Detailed Description
Compiler intrinsics for GCC.
Public Functions
dAtomicRead(volatile U32 & ref)
Performs an atomic read operation.
dCompareAndSwap(volatile U32 & ref, U32 oldVal, U32 newVal)
dCompareAndSwap(volatile U64 & ref, U64 oldVal, U64 newVal)
dFetchAndAdd(volatile S32 & ref, S32 val)
dFetchAndAdd(volatile U32 & ref, U32 val)
1 2//----------------------------------------------------------------------------- 3// Copyright (c) 2012 GarageGames, LLC 4// 5// Permission is hereby granted, free of charge, to any person obtaining a copy 6// of this software and associated documentation files (the "Software"), to 7// deal in the Software without restriction, including without limitation the 8// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or 9// sell copies of the Software, and to permit persons to whom the Software is 10// furnished to do so, subject to the following conditions: 11// 12// The above copyright notice and this permission notice shall be included in 13// all copies or substantial portions of the Software. 14// 15// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 20// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS 21// IN THE SOFTWARE. 22//----------------------------------------------------------------------------- 23 24#ifndef _TORQUE_PLATFORM_PLATFORMINTRINSICS_GCC_H_ 25#define _TORQUE_PLATFORM_PLATFORMINTRINSICS_GCC_H_ 26 27/// @file 28/// Compiler intrinsics for GCC. 29 30#ifdef TORQUE_OS_MAC 31#include <libkern/OSAtomic.h> 32#elif defined(TORQUE_OS_PS3) 33#include <cell/atomic.h> 34#endif 35 36// Fetch-And-Add 37// 38// NOTE: These do not return the pre-add value because 39// not all platforms (damn you OSX) can do that. 40// 41inline void dFetchAndAdd( volatile U32& ref, U32 val ) 42{ 43 #if defined(TORQUE_OS_PS3) 44 cellAtomicAdd32( (std::uint32_t *)&ref, val ); 45 #elif !defined(TORQUE_OS_MAC) 46 __sync_fetch_and_add(&ref, val ); 47 #else 48 OSAtomicAdd32( val, (int32_t* ) &ref); 49 #endif 50} 51 52inline void dFetchAndAdd( volatile S32& ref, S32 val ) 53{ 54 #if defined(TORQUE_OS_PS3) 55 cellAtomicAdd32( (std::uint32_t *)&ref, val ); 56 #elif !defined(TORQUE_OS_MAC) 57 __sync_fetch_and_add( &ref, val ); 58 #else 59 OSAtomicAdd32( val, (int32_t* ) &ref); 60 #endif 61} 62 63// Compare-And-Swap 64 65inline bool dCompareAndSwap( volatile U32& ref, U32 oldVal, U32 newVal ) 66{ 67 // bool 68 //OSAtomicCompareAndSwap32(int32_t oldValue, int32_t newValue, volatile int32_t *theValue); 69 #if defined(TORQUE_OS_PS3) 70 return ( cellAtomicCompareAndSwap32( (std::uint32_t *)&ref, newVal, oldVal ) == oldVal ); 71 #elif !defined(TORQUE_OS_MAC) 72 return ( __sync_val_compare_and_swap( &ref, oldVal, newVal ) == oldVal ); 73 #else 74 return OSAtomicCompareAndSwap32(oldVal, newVal, (int32_t *) &ref); 75 #endif 76} 77 78inline bool dCompareAndSwap( volatile U64& ref, U64 oldVal, U64 newVal ) 79{ 80 #if defined(TORQUE_OS_PS3) 81 return ( cellAtomicCompareAndSwap32( (std::uint32_t *)&ref, newVal, oldVal ) == oldVal ); 82 #elif !defined(TORQUE_OS_MAC) 83 return ( __sync_val_compare_and_swap( &ref, oldVal, newVal ) == oldVal ); 84 #else 85 return OSAtomicCompareAndSwap64(oldVal, newVal, (int64_t *) &ref); 86 #endif 87 88} 89 90/// Performs an atomic read operation. 91inline U32 dAtomicRead( volatile U32 &ref ) 92{ 93 #if defined(TORQUE_OS_PS3) 94 return cellAtomicAdd32( (std::uint32_t *)&ref, 0 ); 95 #elif !defined(TORQUE_OS_MAC) 96 return __sync_fetch_and_add( ( volatile long* ) &ref, 0 ); 97 #else 98 return OSAtomicAdd32( 0, (int32_t* ) &ref); 99 #endif 100} 101 102#endif // _TORQUE_PLATFORM_PLATFORMINTRINSICS_GCC_H_ 103
