0
0
Fork 0
mirror of https://github.com/id-Software/DOOM-3-BFG.git synced 2024-12-15 15:01:19 +00:00
doom3-bfg/neo/sys/sdl/sdl_cpu.cpp

485 lines
10 KiB
C++
Raw Normal View History

2012-12-03 22:55:27 +00:00
/*
===========================================================================
Doom 3 BFG Edition GPL Source Code
Copyright (C) 1993-2012 id Software LLC, a ZeniMax Media company.
2012-12-03 22:55:27 +00:00
Copyright (C) 2012 Robert Beckebans
This file is part of the Doom 3 BFG Edition GPL Source Code ("Doom 3 BFG Edition Source Code").
2012-12-03 22:55:27 +00:00
Doom 3 BFG Edition Source Code is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Doom 3 BFG Edition Source Code is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Doom 3 BFG Edition Source Code. If not, see <http://www.gnu.org/licenses/>.
In addition, the Doom 3 BFG Edition Source Code is also subject to certain additional terms. You should have received a copy of these additional terms immediately following the terms and conditions of the GNU General Public License which accompanied the Doom 3 BFG Edition Source Code. If not, please request a copy in writing from id Software at the address below.
If you have questions concerning this license or the applicable additional terms, you may contact in writing id Software LLC, c/o ZeniMax Media Inc., Suite 120, Rockville, Maryland 20850 USA.
===========================================================================
*/
#pragma hdrstop
#include "../../idlib/precompiled.h"
// DG: SDL_*.h somehow needs the following functions, so #undef those silly
// "don't use" #defines from Str.h
#undef strcasecmp
#undef strncmp
#undef vsnprintf
// DG end
#include <SDL_cpuinfo.h>
2012-12-03 22:55:27 +00:00
#pragma warning(disable:4740) // warning C4740: flow in or out of inline asm code suppresses global optimization
#pragma warning(disable:4731) // warning C4731: 'XXX' : frame pointer register 'ebx' modified by inline assembly code
/*
==============================================================
Clock ticks
==============================================================
*/
/*
================
Sys_GetClockTicks
================
*/
#if defined(_WIN32)
double Sys_GetClockTicks()
{
2012-12-03 22:55:27 +00:00
// RB begin
#if defined(_MSC_VER)
unsigned long lo, hi;
__asm
{
2012-12-03 22:55:27 +00:00
push ebx
xor eax, eax
cpuid
rdtsc
mov lo, eax
mov hi, edx
pop ebx
}
return ( double ) lo + ( double ) 0xFFFFFFFF * hi;
2012-12-03 22:55:27 +00:00
#elif defined(__GNUC__) && defined( __i386__ )
unsigned long lo, hi;
2012-12-03 22:55:27 +00:00
__asm__ __volatile__(
"push %%ebx\n" \
"xor %%eax,%%eax\n" \
"cpuid\n" \
"rdtsc\n" \
"mov %%eax,%0\n" \
"mov %%edx,%1\n" \
"pop %%ebx\n"
: "=r"( lo ), "=r"( hi ) );
return ( double ) lo + ( double ) 0xFFFFFFFF * hi;
#else
#error unsupported CPU
#endif
// RB end
}
#endif
2012-12-03 22:55:27 +00:00
/*
================
Sys_ClockTicksPerSecond
================
*/
#if defined(_WIN32)
double Sys_ClockTicksPerSecond()
{
2012-12-03 22:55:27 +00:00
static double ticks = 0;
#if 0
if( !ticks )
{
2012-12-03 22:55:27 +00:00
LARGE_INTEGER li;
QueryPerformanceFrequency( &li );
ticks = li.QuadPart;
}
2012-12-03 22:55:27 +00:00
#else
if( !ticks )
{
2012-12-03 22:55:27 +00:00
HKEY hKey;
LPBYTE ProcSpeed;
DWORD buflen, ret;
if( !RegOpenKeyEx( HKEY_LOCAL_MACHINE, "HARDWARE\\DESCRIPTION\\System\\CentralProcessor\\0", 0, KEY_READ, &hKey ) )
{
2012-12-03 22:55:27 +00:00
ProcSpeed = 0;
buflen = sizeof( ProcSpeed );
ret = RegQueryValueEx( hKey, "~MHz", NULL, NULL, ( LPBYTE ) &ProcSpeed, &buflen );
2012-12-03 22:55:27 +00:00
// If we don't succeed, try some other spellings.
if( ret != ERROR_SUCCESS )
{
ret = RegQueryValueEx( hKey, "~Mhz", NULL, NULL, ( LPBYTE ) &ProcSpeed, &buflen );
2012-12-03 22:55:27 +00:00
}
if( ret != ERROR_SUCCESS )
{
ret = RegQueryValueEx( hKey, "~mhz", NULL, NULL, ( LPBYTE ) &ProcSpeed, &buflen );
2012-12-03 22:55:27 +00:00
}
RegCloseKey( hKey );
if( ret == ERROR_SUCCESS )
{
ticks = ( double )( ( unsigned long )ProcSpeed ) * 1000000;
2012-12-03 22:55:27 +00:00
}
}
}
2012-12-03 22:55:27 +00:00
#endif
return ticks;
}
#endif
2012-12-03 22:55:27 +00:00
/*
==============================================================
CPU
==============================================================
*/
/*
========================
Sys_CPUCount
numLogicalCPUCores - the number of logical CPU per core
numPhysicalCPUCores - the total number of cores per package
numCPUPackages - the total number of packages (physical processors)
========================
*/
#if defined(_WIN32)
void Sys_CPUCount( int& numLogicalCPUCores, int& numPhysicalCPUCores, int& numCPUPackages )
{
numPhysicalCPUCores = 1;
numLogicalCPUCores = SDL_GetCPUCount();
numCPUPackages = 1;
2012-12-03 22:55:27 +00:00
}
#endif
2012-12-03 22:55:27 +00:00
/*
================
Sys_GetCPUId
================
*/
cpuid_t Sys_GetCPUId()
{
2012-12-03 22:55:27 +00:00
int flags;
2012-12-03 22:55:27 +00:00
// check for an AMD
flags = CPUID_GENERIC;
2012-12-03 22:55:27 +00:00
// check for Multi Media Extensions
if( SDL_HasMMX() )
{
2012-12-03 22:55:27 +00:00
flags |= CPUID_MMX;
}
2012-12-03 22:55:27 +00:00
// check for 3DNow!
if( SDL_Has3DNow() )
{
2012-12-03 22:55:27 +00:00
flags |= CPUID_3DNOW;
}
2012-12-03 22:55:27 +00:00
// check for Streaming SIMD Extensions
if( SDL_HasSSE() )
{
2012-12-03 22:55:27 +00:00
flags |= CPUID_SSE | CPUID_FTZ;
}
2012-12-03 22:55:27 +00:00
// check for Streaming SIMD Extensions 2
if( SDL_HasSSE2() )
{
2012-12-03 22:55:27 +00:00
flags |= CPUID_SSE2;
}
2012-12-03 22:55:27 +00:00
// check for Streaming SIMD Extensions 3 aka Prescott's New Instructions
#if 0 //SDL_VERSION_ATLEAST(2,0,0)
if( SDL_HasSSE3() )
{
2012-12-03 22:55:27 +00:00
flags |= CPUID_SSE3;
}
#endif
/*
2012-12-03 22:55:27 +00:00
// check for Hyper-Threading Technology
if( HasHTT() )
{
2012-12-03 22:55:27 +00:00
flags |= CPUID_HTT;
}
2012-12-03 22:55:27 +00:00
// check for Conditional Move (CMOV) and fast floating point comparison (FCOMI) instructions
if( HasCMOV() )
{
2012-12-03 22:55:27 +00:00
flags |= CPUID_CMOV;
}
2012-12-03 22:55:27 +00:00
// check for Denormals-Are-Zero mode
if( HasDAZ() )
{
2012-12-03 22:55:27 +00:00
flags |= CPUID_DAZ;
}
*/
return ( cpuid_t )flags;
2012-12-03 22:55:27 +00:00
}
/*
===============================================================================
FPU
===============================================================================
*/
typedef struct bitFlag_s
{
const char* name;
2012-12-03 22:55:27 +00:00
int bit;
} bitFlag_t;
static byte fpuState[128], *statePtr = fpuState;
static char fpuString[2048];
static bitFlag_t controlWordFlags[] =
{
2012-12-03 22:55:27 +00:00
{ "Invalid operation", 0 },
{ "Denormalized operand", 1 },
{ "Divide-by-zero", 2 },
{ "Numeric overflow", 3 },
{ "Numeric underflow", 4 },
{ "Inexact result (precision)", 5 },
{ "Infinity control", 12 },
{ "", 0 }
};
static const char* precisionControlField[] =
{
2012-12-03 22:55:27 +00:00
"Single Precision (24-bits)",
"Reserved",
"Double Precision (53-bits)",
"Double Extended Precision (64-bits)"
};
static const char* roundingControlField[] =
{
2012-12-03 22:55:27 +00:00
"Round to nearest",
"Round down",
"Round up",
"Round toward zero"
};
static bitFlag_t statusWordFlags[] =
{
2012-12-03 22:55:27 +00:00
{ "Invalid operation", 0 },
{ "Denormalized operand", 1 },
{ "Divide-by-zero", 2 },
{ "Numeric overflow", 3 },
{ "Numeric underflow", 4 },
{ "Inexact result (precision)", 5 },
{ "Stack fault", 6 },
{ "Error summary status", 7 },
{ "FPU busy", 15 },
{ "", 0 }
};
/*
===============
Sys_FPU_PrintStateFlags
===============
*/
int Sys_FPU_PrintStateFlags( char* ptr, int ctrl, int stat, int tags, int inof, int inse, int opof, int opse )
{
2012-12-03 22:55:27 +00:00
int i, length = 0;
length += sprintf( ptr + length, "CTRL = %08x\n"
"STAT = %08x\n"
"TAGS = %08x\n"
"INOF = %08x\n"
"INSE = %08x\n"
"OPOF = %08x\n"
"OPSE = %08x\n"
"\n",
ctrl, stat, tags, inof, inse, opof, opse );
length += sprintf( ptr + length, "Control Word:\n" );
for( i = 0; controlWordFlags[i].name[0]; i++ )
{
length += sprintf( ptr + length, " %-30s = %s\n", controlWordFlags[i].name, ( ctrl & ( 1 << controlWordFlags[i].bit ) ) ? "true" : "false" );
2012-12-03 22:55:27 +00:00
}
length += sprintf( ptr + length, " %-30s = %s\n", "Precision control", precisionControlField[( ctrl >> 8 ) & 3] );
length += sprintf( ptr + length, " %-30s = %s\n", "Rounding control", roundingControlField[( ctrl >> 10 ) & 3] );
length += sprintf( ptr + length, "Status Word:\n" );
for( i = 0; statusWordFlags[i].name[0]; i++ )
{
ptr += sprintf( ptr + length, " %-30s = %s\n", statusWordFlags[i].name, ( stat & ( 1 << statusWordFlags[i].bit ) ) ? "true" : "false" );
2012-12-03 22:55:27 +00:00
}
length += sprintf( ptr + length, " %-30s = %d%d%d%d\n", "Condition code", ( stat >> 8 ) & 1, ( stat >> 9 ) & 1, ( stat >> 10 ) & 1, ( stat >> 14 ) & 1 );
length += sprintf( ptr + length, " %-30s = %d\n", "Top of stack pointer", ( stat >> 11 ) & 7 );
2012-12-03 22:55:27 +00:00
return length;
}
/*
===============
Sys_FPU_StackIsEmpty
===============
*/
bool Sys_FPU_StackIsEmpty()
{
// TODO
2012-12-03 22:55:27 +00:00
return true;
}
/*
===============
Sys_FPU_ClearStack
===============
*/
void Sys_FPU_ClearStack()
{
// TODO
2012-12-03 22:55:27 +00:00
}
/*
===============
Sys_FPU_GetState
gets the FPU state without changing the state
===============
*/
const char* Sys_FPU_GetState()
{
return "TODO Sys_FPU_GetState()";
2012-12-03 22:55:27 +00:00
}
/*
===============
Sys_FPU_EnableExceptions
===============
*/
void Sys_FPU_EnableExceptions( int exceptions )
{
// TODO
2012-12-03 22:55:27 +00:00
}
/*
===============
Sys_FPU_SetPrecision
===============
*/
void Sys_FPU_SetPrecision( int precision )
{
// TODO
/*
2012-12-03 22:55:27 +00:00
short precisionBitTable[4] = { 0, 1, 3, 0 };
short precisionBits = precisionBitTable[precision & 3] << 8;
short precisionMask = ~( ( 1 << 9 ) | ( 1 << 8 ) );
2012-12-03 22:55:27 +00:00
__asm {
mov eax, statePtr
mov cx, precisionBits
fnstcw word ptr [eax]
mov bx, word ptr [eax]
and bx, precisionMask
or bx, cx
mov word ptr [eax], bx
fldcw word ptr [eax]
}
*/
2012-12-03 22:55:27 +00:00
}
/*
================
Sys_FPU_SetRounding
================
*/
void Sys_FPU_SetRounding( int rounding )
{
// TODO
/*
2012-12-03 22:55:27 +00:00
short roundingBitTable[4] = { 0, 1, 2, 3 };
short roundingBits = roundingBitTable[rounding & 3] << 10;
short roundingMask = ~( ( 1 << 11 ) | ( 1 << 10 ) );
2012-12-03 22:55:27 +00:00
__asm {
mov eax, statePtr
mov cx, roundingBits
fnstcw word ptr [eax]
mov bx, word ptr [eax]
and bx, roundingMask
or bx, cx
mov word ptr [eax], bx
fldcw word ptr [eax]
}
*/
2012-12-03 22:55:27 +00:00
}
/*
================
Sys_FPU_SetDAZ
================
*/
void Sys_FPU_SetDAZ( bool enable )
{
/*
2012-12-03 22:55:27 +00:00
DWORD dwData;
2012-12-03 22:55:27 +00:00
_asm {
movzx ecx, byte ptr enable
and ecx, 1
shl ecx, 6
STMXCSR dword ptr dwData
mov eax, dwData
and eax, ~(1<<6) // clear DAX bit
or eax, ecx // set the DAZ bit
mov dwData, eax
LDMXCSR dword ptr dwData
}
*/
2012-12-03 22:55:27 +00:00
}
/*
================
Sys_FPU_SetFTZ
================
*/
void Sys_FPU_SetFTZ( bool enable )
{
/*
2012-12-03 22:55:27 +00:00
DWORD dwData;
2012-12-03 22:55:27 +00:00
_asm {
movzx ecx, byte ptr enable
and ecx, 1
shl ecx, 15
STMXCSR dword ptr dwData
mov eax, dwData
and eax, ~(1<<15) // clear FTZ bit
or eax, ecx // set the FTZ bit
mov dwData, eax
LDMXCSR dword ptr dwData
}
*/
2012-12-03 22:55:27 +00:00
}