Precise timing

Hello,
i was trying to do precise timing and i found something useful in sdl
sources. It uses rdtsc on intel but i need it for PPC.
So i added
#ifdef PPC
#define rdtsc(t)
{ uint32 hilo = (uint32)(&t);
asm volatile (
“1: \n”
“mftbu 4 \n”
“mftb %1 \n”
“mftbu %0 \n”
“sub. 4, 4, %0 \n”
“bne 1b \n”
:: “r” (hilo), “r”((hilo+1)) : “4”, “cc” ); }
#else

it seems like it is working but doesn’t someone know about some
easyier way to do this?

And is it possible to make this part of SDL?

Thanks
Jan Kratochvil–

Btw, I would make a couple changes to the assembly itself:

  • use a temp variable instead of always overwriting r4. This way the
    compiler is free to give you anything.
  • those should be output variables, not input variables. That’s
    serious, and requires some slightly different syntax (loosely tested):

#include <stdint.h>
void mftb(uint64_t *tb)
{
uint32_t *tbwords = (uint32_t *)tb;
uint32_t tbu, tbl, temp;

 /* from section 2.2.1 of the 32-bit PowerPC PEM */
 __asm__ __volatile__(
     "1:\n"
     "mftbu  %2\n"
     "mftb   %0\n"
     "mftbu  %1\n"
     "cmpw   %2,%1\n"
     "bne    1b\n"
 : "=r"(tbl), "=r"(tbu), "=r"(temp)
 :
 : "cc");

 tbwords[0] = tbu;
 tbwords[1] = tbl;

}

-HollisOn Apr 11, 2004, at 1:24 PM, Jan Kratochvil wrote:

#define rdtsc(t)
{ uint32 hilo = (uint32)(&t);
asm volatile (
“1: \n”
“mftbu 4 \n”
“mftb %1 \n”
“mftbu %0 \n”
“sub. 4, 4, %0 \n”
“bne 1b \n”
:: “r” (hilo), “r”((hilo+1)) : “4”, “cc” ); }

Please don’t call it “rdtsc”. You wouldn’t call it “mftb” on an x86
would you…?

Looking at the ifdefs in SDL_systimer.c, I’d say SDL_StartTicks and
SDL_GetTicks should be broken into their own conditionally-compiled
files, e.g. ticks.c and ticks_x86_tsc.c. Then you can write
ticks_ppc_tb.c and everyone’s happy.

Of course, notice that USE_RDTSC is commented out on x86. The same
issue exists on SMP PPC: the timebases are close, but not perfectly
synced. Consecutive calls to mftb could appear to go backwards if your
process is switched to another CPU in between. I see “Use RDTSC” is
still on the TODO list for x86…

-HollisOn Apr 11, 2004, at 1:24 PM, Jan Kratochvil wrote:

i was trying to do precise timing and i found something useful in sdl
sources. It uses rdtsc on intel but i need it for PPC.
So i added
#ifdef PPC
#define rdtsc(t)
{ uint32 hilo = (uint32)(&t);
asm volatile (
“1: \n”
“mftbu 4 \n”
“mftb %1 \n”
“mftbu %0 \n”
“sub. 4, 4, %0 \n”
“bne 1b \n”
:: “r” (hilo), “r”((hilo+1)) : “4”, “cc” ); }
#else

it seems like it is working but doesn’t someone know about some
easyier way to do this?