phire/doom3.gpl — Issue #11: Sys_GetClockTicks() defective on x86_64
Author: DanielGibson
Created: 2011-12-03 22:34:21+00:00
State: open

https://github.com/phire/doom3.gpl/blob/master/neo/sys/linux/main.cpp#L259
This code doesn't work. (Probably because unsigned long is 64bits long on Linux/amd64, but rdtsc uses uint32_t values).
Use the following (from Kai Blaschke in the iodoom3 mailing list) instead:

uint32_t lo, hi;

__asm__ __volatile__ ( // serialize
"xorl %%eax,%%eax \n cpuid"
::: "%rax", "%rbx", "%rcx", "%rdx");
/* We cannot use "=A", since this would use %rax on x86_64 and return only the lower 32bits of the TSC */
__asm__ __volatile__ ("rdtsc" : "=a" (lo), "=d" (hi));
return (double) lo + (double) 0xFFFFFFFF * hi;

Cheers,
- Daniel