Mail Archives: djgpp/1999/08/22/15:21:05
Hi,
How can I detect the clock speed?
Thomas Harte <T DOT Harte AT btinternet DOT com> sent me this piece of code, but this
method is quite slow (you have to wait for a whole second) and not very
accurate. Any suggestions?
Thiago
-------------------------------------------
#include <allegro.h>
uint64 tsc();
typedef unsigned long long uint64;
// allegro timer callback ///////////////////////////////////////////////////
volatile int timervar;
void timerfunc()
{
timervar++;
}
END_OF_FUNCTION(timerfunc);
// get_clockspeed() /////////////////////////////////////////////////////////
uint64 get_clockspeed()
{
uint64 ticks[3];
int count = 3;
//install an Allegro timer
LOCK_VARIABLE(timervar);
LOCK_FUNCTION(timerfunc);
install_int_ex(timerfunc, BPS_TO_TIMER(50));
//now to wait until the timer has finished one cycle so that our first
//reading is not 'short' . . . also the reason that our timer is beating
//at 50 bps and not 1 - to cut down the length of time waiting for an
//entry point
timervar = 0;
while(!timervar)
;
//reads three RDTSC values - each a second apart. Theoretically it could
//be checked that the Hz values generated between reads 1&2 and 2&3 do not
//vary greatly from half the difference between 1&3 to check that no great
//error has occured (e.g. our program was interrupted in a multitasking
//environment, and missed one timer beat by a great distance, etc etc
do
{
timervar = 0;
count--;
ticks[count] = tsc();
while(timervar < 25)
;
}
while(count);
remove_int(timerfunc);
return (ticks[0] - ticks[2]);
}
uint64 tsc()
{
uint64 ticks, temp;
uint top, bottom;
__asm__ __volatile__("rdtsc":"=d"(top), "=a"(bottom));
temp = top;
ticks = (temp << 32) | bottom;
return ticks;
}
- Raw text -