Commit fa7913f2 authored by Sam Lantinga's avatar Sam Lantinga

Added UNIX RDTSC code by Lompak (disabled by default)

--HG--
extra : convert_revision : svn%3Ac70aab31-4412-0410-b14c-859654838e24/trunk%40317
parent b553c23e
...@@ -18,6 +18,8 @@ ...@@ -18,6 +18,8 @@
Sam Lantinga Sam Lantinga
slouken@libsdl.org slouken@libsdl.org
RDTSC stuff by lompik (lompik@voila.fr) 20/03/2002
*/ */
#ifdef SAVE_RCSID #ifdef SAVE_RCSID
...@@ -54,24 +56,87 @@ static char rcsid = ...@@ -54,24 +56,87 @@ static char rcsid =
#define USE_NANOSLEEP #define USE_NANOSLEEP
#endif #endif
#if defined(i386) || defined(__i386__)
/* Actually, this isn't reliable on multi-cpu systems, so is disabled */
/*#define USE_RDTSC*/
#endif
#ifdef USE_RDTSC
/* The first ticks value of the application */
static unsigned long long start;
static float cpu_mhz1000 = 0.0f;
#if 1
/* This is for old binutils version that don't recognize rdtsc mnemonics.
But all binutils version supports this.
*/
#define rdtsc(t) asm(".byte 0x0f, 0x31; " : "=A" (t));
#else
#define rdtsc(t) asm("rdtsc" : "=A" (t));
#endif
static float calc_cpu_mhz(void)
{
float cpu_mhz;
unsigned long long tsc_start;
unsigned long long tsc_end;
struct timeval tv_start, tv_end;
long usec_delay;
rdtsc(tsc_start);
gettimeofday(&tv_start, NULL);
sleep(1);
rdtsc(tsc_end);
gettimeofday(&tv_end, NULL);
usec_delay = 1000000L * (tv_end.tv_sec - tv_start.tv_sec) +
(tv_end.tv_usec - tv_start.tv_usec);
cpu_mhz = (float)(tsc_end-tsc_start) / usec_delay;
#if 0
printf("cpu MHz\t\t: %.3f\n", cpu_mhz);
#endif
return cpu_mhz;
}
#else
/* The first ticks value of the application */ /* The first ticks value of the application */
static struct timeval start; static struct timeval start;
#endif /* USE_RDTSC */
void SDL_StartTicks(void) void SDL_StartTicks(void)
{ {
/* Set first ticks value */ /* Set first ticks value */
#ifdef USE_RDTSC
if ( ! cpu_mhz1000 ) {
cpu_mhz1000 = calc_cpu_mhz() * 1000.0f;
}
rdtsc(start);
#else
gettimeofday(&start, NULL); gettimeofday(&start, NULL);
#endif /* USE_RDTSC */
} }
Uint32 SDL_GetTicks (void) Uint32 SDL_GetTicks (void)
{ {
#ifdef USE_RDTSC
unsigned long long now;
if ( ! cpu_mhz1000 ) {
return 0; /* Shouldn't happen. BUG!! */
}
rdtsc(now);
return (Uint32)((now-start)/cpu_mhz1000);
#else
struct timeval now; struct timeval now;
Uint32 ticks; Uint32 ticks;
gettimeofday(&now, NULL); gettimeofday(&now, NULL);
ticks=(now.tv_sec-start.tv_sec)*1000+(now.tv_usec-start.tv_usec)/1000; ticks=(now.tv_sec-start.tv_sec)*1000+(now.tv_usec-start.tv_usec)/1000;
return(ticks); return(ticks);
#endif /* USE_RDTSC */
} }
void SDL_Delay (Uint32 ms) void SDL_Delay (Uint32 ms)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment