2010-02-08 17:23:39 +01:00
|
|
|
/* Copyright (c) 2010 Wildfire Games
|
2009-04-18 19:00:33 +02:00
|
|
|
*
|
2010-02-08 17:23:39 +01:00
|
|
|
* 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.
|
2009-04-18 19:00:33 +02:00
|
|
|
*/
|
|
|
|
|
2009-04-18 19:51:05 +02:00
|
|
|
/*
|
|
|
|
* CPU and memory detection.
|
2006-04-12 01:59:08 +02:00
|
|
|
*/
|
|
|
|
|
2007-05-02 14:07:08 +02:00
|
|
|
#ifndef INCLUDED_CPU
|
|
|
|
#define INCLUDED_CPU
|
2005-05-18 07:32:09 +02:00
|
|
|
|
2009-11-10 16:48:41 +01:00
|
|
|
#include "lib/sysdep/compiler.h"
|
|
|
|
|
|
|
|
|
2006-09-22 15:19:40 +02:00
|
|
|
namespace ERR
|
|
|
|
{
|
2011-05-03 14:38:42 +02:00
|
|
|
const Status CPU_FEATURE_MISSING = -130000;
|
|
|
|
const Status CPU_UNKNOWN_OPCODE = -130001;
|
|
|
|
const Status CPU_UNKNOWN_VENDOR = -130002;
|
2008-05-12 20:15:08 +02:00
|
|
|
|
2006-09-22 15:19:40 +02:00
|
|
|
}
|
2004-06-19 16:45:04 +02:00
|
|
|
|
2009-11-10 16:48:41 +01:00
|
|
|
|
2007-09-23 17:36:29 +02:00
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
// CPU detection
|
2004-06-19 16:45:04 +02:00
|
|
|
|
2007-09-23 17:36:29 +02:00
|
|
|
/**
|
|
|
|
* @return string identifying the CPU (usually a cleaned-up version of the
|
|
|
|
* brand string)
|
|
|
|
**/
|
2008-05-01 17:41:42 +02:00
|
|
|
LIB_API const char* cpu_IdentifierString();
|
2007-09-23 17:36:29 +02:00
|
|
|
|
2005-05-03 07:05:16 +02:00
|
|
|
|
2007-09-23 17:36:29 +02:00
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
// lock-free support routines
|
|
|
|
|
2010-11-01 10:54:03 +01:00
|
|
|
/**
|
|
|
|
* add a signed value to a variable without the possibility of interference
|
|
|
|
* from other threads/CPUs.
|
|
|
|
*
|
|
|
|
* @return the previous value.
|
|
|
|
**/
|
|
|
|
LIB_API intptr_t cpu_AtomicAdd(volatile intptr_t* location, intptr_t increment);
|
|
|
|
|
2007-09-23 17:36:29 +02:00
|
|
|
/**
|
|
|
|
* atomic "compare and swap".
|
|
|
|
*
|
|
|
|
* @param location address of the word to compare and possibly overwrite
|
|
|
|
* @param expected its expected value
|
|
|
|
* @param newValue the value with which to replace it
|
|
|
|
* @return false if the target word doesn't match the expected value,
|
|
|
|
* otherwise true (also overwriting the contents of location)
|
|
|
|
**/
|
2010-07-12 14:57:58 +02:00
|
|
|
LIB_API bool cpu_CAS(volatile intptr_t* location, intptr_t expected, intptr_t newValue);
|
2010-11-01 10:54:03 +01:00
|
|
|
LIB_API bool cpu_CAS64(volatile i64* location, i64 expected, i64 newValue);
|
|
|
|
|
2008-05-12 20:15:08 +02:00
|
|
|
/**
|
|
|
|
* specialization of cpu_CAS for pointer types. this avoids error-prone
|
|
|
|
* casting in user code.
|
|
|
|
**/
|
|
|
|
template<typename T>
|
2011-01-04 20:53:38 +01:00
|
|
|
inline bool cpu_CAS(volatile T* location, T expected, T new_value)
|
2008-05-12 20:15:08 +02:00
|
|
|
{
|
2010-07-12 14:57:58 +02:00
|
|
|
return cpu_CAS((volatile intptr_t*)location, (intptr_t)expected, (intptr_t)new_value);
|
2008-05-12 20:15:08 +02:00
|
|
|
}
|
|
|
|
|
2010-07-22 18:17:33 +02:00
|
|
|
|
|
|
|
LIB_API void cpu_Test();
|
2005-05-03 07:05:16 +02:00
|
|
|
|
2010-07-13 00:14:28 +02:00
|
|
|
/**
|
|
|
|
* pause in spin-wait loops, as a performance optimisation.
|
|
|
|
**/
|
|
|
|
inline void cpu_Pause()
|
|
|
|
{
|
2010-07-14 14:23:53 +02:00
|
|
|
#if MSC_VERSION && ARCH_X86_X64
|
2010-07-13 00:14:28 +02:00
|
|
|
_mm_pause();
|
2010-07-14 14:23:53 +02:00
|
|
|
#elif GCC_VERSION && ARCH_X86_X64
|
2010-07-13 00:14:28 +02:00
|
|
|
__asm__ __volatile__( "rep; nop" : : : "memory" );
|
|
|
|
#endif
|
|
|
|
}
|
2007-05-16 17:56:47 +02:00
|
|
|
|
2007-05-02 14:07:08 +02:00
|
|
|
#endif // #ifndef INCLUDED_CPU
|