Skip to content

Instantly share code, notes, and snippets.

@Bobbias
Created August 11, 2013 00:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Bobbias/6202836 to your computer and use it in GitHub Desktop.
Save Bobbias/6202836 to your computer and use it in GitHub Desktop.
enemy nations CPU speed check
// get the CPU speed (needed before screen res)
// FIXME: Excise this monstrosity. This has NO reason to exist any more.
if ( (m_iCpuSpeed = GetProfileInt ("Advanced", "CPUspeed", 0)) < 60 )
{
BOOL bGotSpeed = FALSE;
SYSTEM_INFO si;
::GetSystemInfo ( &si );
if ( (si.wProcessorArchitecture == PROCESSOR_ARCHITECTURE_INTEL) && (si.wProcessorLevel >= 5) )
{
// in case processor can't handle rdtsc
try
{
static int dwStart, dwHigh, dwEnd;
m_iCpuSpeed = INT_MAX;
for (int iTry=0; iTry<4; iTry++)
{
::Sleep (10);
// get the clock count
__asm
{
_emit 0fh
_emit 31h // rdtsc
mov [dwStart], eax
mov [dwHigh], edx
}
// wait 250 ms
DWORD dwTime = timeGetTime () + 250;
while ( timeGetTime () < dwTime )
;
__asm
{
_emit 0fh
_emit 31h // rdtsc
mov [dwEnd], eax
sub [dwHigh], edx
}
if ( dwHigh == 0 )
{
int iTime = dwEnd - dwStart;
m_iCpuSpeed = __min ( iTime, m_iCpuSpeed );
}
}
m_iCpuSpeed = ( (m_iCpuSpeed >> 12) * 133 + (0x1FAD / 2) ) / 0x1FAD;
bGotSpeed = TRUE;
}
catch (...)
{
}
}
if ( ! bGotSpeed )
{
m_iCpuSpeed = INT_MAX;
int iPri = GetThreadPriority ();
::Sleep (100);
for (int iTry=0; iTry<16; iTry++)
{
SetThreadPriority ( THREAD_PRIORITY_HIGHEST );
::Sleep (10);
DWORD dwStart = timeGetTime ();
_asm
{
push ebx
mov ecx, 100000h
jmp _flush2
_flush2:
mov eax, 01234h
mov edx, 10h
mov ebx, 100
div ebx
div ebx
loop _flush2
pop ebx
}
DWORD dwTime = timeGetTime () - dwStart;
SetThreadPriority ( iPri );
::Sleep (10);
m_iCpuSpeed = __min ( m_iCpuSpeed, (int) dwTime );
}
m_iCpuSpeed = __max ( 1, m_iCpuSpeed );
m_iCpuSpeed = ( 133 * 703 + 351 ) / m_iCpuSpeed;
}
}
m_iCpuSpeed = __max ( 60, m_iCpuSpeed );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment