2010-02-08 08:23:39 -08:00
|
|
|
/* Copyright (c) 2010 Wildfire Games
|
2009-04-18 10:00:33 -07:00
|
|
|
*
|
2010-02-08 08:23:39 -08: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 10:00:33 -07:00
|
|
|
*/
|
|
|
|
|
|
2009-04-18 10:51:05 -07:00
|
|
|
/*
|
|
|
|
|
* CPU and memory detection.
|
2006-04-11 16:59:08 -07:00
|
|
|
*/
|
|
|
|
|
|
2007-05-02 05:07:08 -07:00
|
|
|
#ifndef INCLUDED_CPU
|
|
|
|
|
#define INCLUDED_CPU
|
2005-05-17 22:32:09 -07:00
|
|
|
|
2009-11-10 07:48:41 -08:00
|
|
|
#include "lib/sysdep/compiler.h"
|
|
|
|
|
|
|
|
|
|
|
2006-09-22 06:19:40 -07:00
|
|
|
namespace ERR
|
|
|
|
|
{
|
|
|
|
|
const LibError CPU_FEATURE_MISSING = -130000;
|
|
|
|
|
const LibError CPU_UNKNOWN_OPCODE = -130001;
|
2007-04-25 11:19:35 -07:00
|
|
|
const LibError CPU_UNKNOWN_VENDOR = -130002;
|
2008-05-12 11:15:08 -07:00
|
|
|
|
2006-09-22 06:19:40 -07:00
|
|
|
}
|
2004-06-19 07:45:04 -07:00
|
|
|
|
2009-11-10 07:48:41 -08:00
|
|
|
|
2007-09-23 08:36:29 -07:00
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
// CPU detection
|
2004-06-19 07:45:04 -07:00
|
|
|
|
2007-09-23 08:36:29 -07:00
|
|
|
/**
|
|
|
|
|
* @return string identifying the CPU (usually a cleaned-up version of the
|
|
|
|
|
* brand string)
|
|
|
|
|
**/
|
2008-05-01 08:41:42 -07:00
|
|
|
LIB_API const char* cpu_IdentifierString();
|
2007-09-23 08:36:29 -07:00
|
|
|
|
2005-05-02 22:05:16 -07:00
|
|
|
|
2007-09-23 08:36:29 -07:00
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
// lock-free support routines
|
|
|
|
|
|
2009-11-10 07:48:41 -08:00
|
|
|
/**
|
|
|
|
|
* prevent the CPU from reordering previous loads or stores past the barrier,
|
|
|
|
|
* thus ensuring they retire before any subsequent memory operations.
|
|
|
|
|
* this also prevents compiler reordering.
|
|
|
|
|
**/
|
|
|
|
|
#if MSC_VERSION
|
|
|
|
|
# include <intrin.h>
|
2009-11-16 15:45:56 -08:00
|
|
|
# if !ICC_VERSION
|
|
|
|
|
# pragma intrinsic(_ReadWriteBarrier)
|
|
|
|
|
# endif
|
2009-11-10 07:48:41 -08:00
|
|
|
# define cpu_MemoryBarrier() _ReadWriteBarrier()
|
|
|
|
|
#elif GCC_VERSION
|
2009-11-12 11:35:28 -08:00
|
|
|
# define cpu_MemoryBarrier() asm volatile("" : : : "memory")
|
2009-11-10 07:48:41 -08:00
|
|
|
#else
|
|
|
|
|
# define cpu_MemoryBarrier()
|
|
|
|
|
#endif
|
|
|
|
|
|
2007-09-23 08:36:29 -07: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)
|
|
|
|
|
**/
|
2008-05-01 08:41:42 -07:00
|
|
|
LIB_API bool cpu_CAS(volatile uintptr_t* location, uintptr_t expected, uintptr_t newValue);
|
2005-05-02 22:05:16 -07:00
|
|
|
|
2008-05-12 11:15:08 -07:00
|
|
|
/**
|
|
|
|
|
* specialization of cpu_CAS for pointer types. this avoids error-prone
|
|
|
|
|
* casting in user code.
|
|
|
|
|
**/
|
|
|
|
|
template<typename T>
|
|
|
|
|
bool cpu_CAS(volatile T* location, T expected, T new_value)
|
|
|
|
|
{
|
|
|
|
|
return cpu_CAS((volatile uintptr_t*)location, (uintptr_t)expected, (uintptr_t)new_value);
|
|
|
|
|
}
|
|
|
|
|
|
2007-04-30 12:58:04 -07:00
|
|
|
/**
|
|
|
|
|
* add a signed value to a variable without the possibility of interference
|
|
|
|
|
* from other threads/CPUs.
|
|
|
|
|
**/
|
2008-05-01 08:41:42 -07:00
|
|
|
LIB_API void cpu_AtomicAdd(volatile intptr_t* location, intptr_t increment);
|
2005-05-02 22:05:16 -07:00
|
|
|
|
2007-09-23 08:36:29 -07:00
|
|
|
/**
|
|
|
|
|
* enforce strict instruction ordering in the CPU pipeline.
|
|
|
|
|
**/
|
2008-05-01 08:41:42 -07:00
|
|
|
LIB_API void cpu_Serialize();
|
2007-05-16 08:56:47 -07:00
|
|
|
|
|
|
|
|
|
2007-09-23 08:36:29 -07:00
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
// misc
|
2007-04-25 11:19:35 -07:00
|
|
|
|
2007-09-23 08:36:29 -07:00
|
|
|
/**
|
2008-05-01 08:41:42 -07:00
|
|
|
* drop-in replacement for POSIX memcpy().
|
2007-09-23 08:36:29 -07:00
|
|
|
**/
|
2008-05-01 08:41:42 -07:00
|
|
|
LIB_API void* cpu_memcpy(void* RESTRICT dst, const void* RESTRICT src, size_t size);
|
2008-03-24 08:44:37 -07:00
|
|
|
|
2007-04-25 11:19:35 -07:00
|
|
|
|
2007-09-23 08:36:29 -07:00
|
|
|
/**
|
|
|
|
|
* set the FPU control word to "desirable" values (see implementation)
|
|
|
|
|
**/
|
2008-05-01 08:41:42 -07:00
|
|
|
LIB_API void cpu_ConfigureFloatingPoint();
|
2007-04-25 11:19:35 -07:00
|
|
|
|
|
|
|
|
// convert float to int much faster than _ftol2, which would normally be
|
|
|
|
|
// used by (int) casts.
|
2008-04-19 11:10:00 -07:00
|
|
|
// VC8 and GCC with -ffast-math now manage to generate SSE instructions,
|
|
|
|
|
// so our implementation is no longer needed.
|
2008-07-05 01:27:33 -07:00
|
|
|
#define cpu_i32FromFloat(f) ((i32)(f))
|
|
|
|
|
#define cpu_i32FromDouble(d) ((i32)(d))
|
|
|
|
|
#define cpu_i64FromDouble(d) ((i64)(d))
|
2005-05-02 22:05:16 -07:00
|
|
|
|
2007-05-02 05:07:08 -07:00
|
|
|
#endif // #ifndef INCLUDED_CPU
|