Original Post
How do I find the frequency of the cpu so I can convert the rdtsc into milliseconds.
DWORD CalculateCPUSpeed(){DWORD dwTimerLo, dwTimerHi;const DWORD dwDelay = 500; // We want absolute maximum priority DWORD dwPriorityClass = GetPriorityClass(GetCurrentProcess()); int nPriority = GetThreadPriority(GetCurrentThread()); SetPriorityClass(GetCurrentProcess(),REALTIME_PRIORITY_CLASS); SetThreadPriority(GetCurrentThread(),THREAD_PRIORITY_TIME_CRITICAL); Sleep(0); // Give up the rest of our timeslice so we don't get a context switch // Get the current time stamp counter __asm { rdtsc mov dwTimerLo, eax mov dwTimerHi, edx } // Sleep for a while Sleep(dwDelay); // Get the elapsed time __asm { rdtsc sub eax, dwTimerLo sbb edx, dwTimerHi mov dwTimerLo, eax mov dwTimerHi, edx } // Reset priority and get speed SetThreadPriority(GetCurrentThread(),nPriority); SetPriorityClass(GetCurrentProcess(),dwPriorityClass); return dwTimerLo / (1000*dwDelay);}DWORD ReadCPUSpeedFromRegistry(){HKEY hKey;DWORD dwSpeed; // Open the key if(RegOpenKeyEx(HKEY_LOCAL_MACHINE, "HARDWARE\\DESCRIPTION\\System\\CentralProcessor\\0\\",0, KEY_QUERY_VALUE,&hKey) != ERROR_SUCCESS) { return 0; } // Read the value DWORD dwLen = 4; if(RegQueryValueEx(hKey,"~MHz",NULL,NULL,(LPBYTE)&dwSpeed,&dwLen) != ERROR_SUCCESS) { RegCloseKey(hKey); return 0; } // Cleanup and return RegCloseKey(hKey); return dwSpeed;}#include <iostream>#include <cstdlib>using namespace std ;static unsigned int hi = 0;static unsigned int lo = 0;void set_asm_counter( unsigned & hi , unsigned int & lo ){ __asm__ ( " rdtsc ; movl %%edx , %0 ; movl %%eax , %1" : "=r" (hi) , "=r" (lo) : :"%edx","%eax" );}double get_asm_counter(const unsigned & hi_old , const unsigned & lo_old){ unsigned int new_hi = 0 ; unsigned int new_lo = 0 ; set_asm_counter(new_hi,new_lo) ; unsigned int diff_hi = new_hi - hi_old ; unsigned int diff_lo = new_lo - lo_old ; unsigned int overflow_lo = lo_old > diff_lo ; diff_hi -= overflow_lo ; return static_cast<double> (diff_hi) * ( 0x1 << 30 ) * 4 + static_cast<double>(diff_lo ) ;}// purpose of main is here to give you a approximation of the proc freq int main(int argc,char * argv[]){ double res = 0 ;// = get_asm_counter(hi,lo) ; set_asm_counter(hi,lo) ; cout << " timing value = " << res << endl ; sleep(1) ; res = get_asm_counter(hi,lo) ; cout << " timing value = " << res << endl ; return EXIT_SUCCESS ;}This topic has been locked by a moderator. New replies are not allowed.
With your permission, GameDev.net uses analytics cookies to understand how people use the platform. You can accept analytics or continue with necessary cookies only. Learn more