Skip to main content
GameDev.net gamedev.net
🔒 Locked

Clock cycle counting?

Started by c0nfUzEd Nov 28, 2001 at 7:22 PM 1 replies 350+ views
Original Post
c0nfUzEd
c0nfUzEd
If I have some asm code, how can I count the number of clock cycles it takes?
Shannon Barber
Shannon Barber
RDTSC is an 586+ opcode that puts the current CPU tick into edx:eax

  
__int64 __declspec( naked ) RDTSC()
{
_asm
{
//Prolog

push ebp
mov ebp, esp
sub esp, __LOCAL_SIZE

//Get CPU Ticks

cpuid
//_emit 0x0f // CPUID

//_emit 0xa2

rdtsc
//_emit 0x0f // RDTSC

//_emit 0x31



//Epilog

mov esp, ebp
pop ebp
ret
}
}


If you have the processor pack it will recongize rdtsc and cpuid, otherwise you need to use the _emit directive.

This isn''t the best way to it, but it''s a start. Ideally you''d want to inline the cpuid and rdtsc opcodes and move the edx:eax result to where you want to store it.
The trade-off between price and quality does not exist in Japan. Rather, the idea that high quality brings on cost reduction is widely accepted.-- Tajima & Matsubara
c0nfUzEd
c0nfUzEd
Thanks, it worked in MSVC, but I also wanted to use it in DJGPP. I looked around and found this:

  
long long readTSC (void)
{
long long t;
asm (".byte 0x0f,0x31" /* opcode 0x0f31 */
: "=A" (t) /* output parameters : edx:eax -> t */
: /* no input register */
: "%eax", "%edx"); /* registers destroyed in asm */

return t;
}


But the compiler says "''asm'' needs too many reloads". Does anyone know what this means and how to fix it?

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.