Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Jan 24th, 2005, 3:16 PM   #1
saxman
Newbie
 
Join Date: Dec 2004
Posts: 7
Rep Power: 0 saxman is on a distinguished road
Question Using RDTSC In C/C++

I'm trying to use the RDTSC (Read Time-Stamp Counter) x86 instruction in my C program. Now I have the instruction working... but I want to take the entire 64-bits and divide them by 400,900,000 (400.9M) to convert the cps rate into seconds in a floating-point format of some type. Then I want to display these seconds in a string using the PRINTF function. Can anyone offer some example code of how I might do this? The fact that the time-stamp is 64-bits long is what keeps throwing me for a loop since I'm obviously programming on a 32-bit platform.
saxman is offline   Reply With Quote
Old Jan 30th, 2005, 2:38 PM   #2
saxman
Newbie
 
Join Date: Dec 2004
Posts: 7
Rep Power: 0 saxman is on a distinguished road
Re:

This comes from Ken Silverman:

#if 0 //To compile, type: "nmake getmhz.c"
getmhz.exe: getmhz.c; cl getmhz.c /MD /G6y /Ox /nologo /link /opt:nowin98
!if 0
#endif

#include <windows.h>
#include <stdio.h>

static __forceinline __int64 rdtsc64 () { _asm rdtsc }

void main ()
{
	__int64 q0, q1, t0, t1, qfrq;

	QueryPerformanceFrequency((LARGE_INTEGER *)&qfrq);

	QueryPerformanceCounter((LARGE_INTEGER *)&q0);
	t0 = rdtsc64();

	while (1) //Force at least a 1/32 second delay (use longer for more accuracy)
	{
		QueryPerformanceCounter((LARGE_INTEGER *)&q1);
		if (q1-q0 >= (qfrq>>5)) break;
	}

	QueryPerformanceCounter((LARGE_INTEGER *)&q1);
	t1 = rdtsc64();

	printf("Detected frequency: %.0fhz",((double)qfrq) * ((double)(t1-t0)) / ((double)(q1-q0)));

	getch();
}

#if 0
!endif
#endif

Problem solved.
saxman is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 12:09 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC