From 89fdb8cda43807a267dc00b76d92cc4f924a68df Mon Sep 17 00:00:00 2001 From: a Date: Thu, 6 Oct 2016 16:35:33 +0200 Subject: [PATCH] Add the cpuid serializing instruction to rdtscp. Thanks to intel docs. --- m4/cflagz.m4 | 10 +++++++--- src/cpu.c | 13 +++++++++++-- 2 files changed, 18 insertions(+), 5 deletions(-) diff --git a/m4/cflagz.m4 b/m4/cflagz.m4 index 49e38bf..de78c52 100644 --- a/m4/cflagz.m4 +++ b/m4/cflagz.m4 @@ -54,9 +54,13 @@ AC_DEFUN([TEST_ASSEMBLY],[ AC_COMPILE_IFELSE([ AC_LANG_PROGRAM([[ - #include ]],[[ - uintmax_t x; - __asm__ __volatile__ (".byte 0x0f, 0x31" : "=A" (x)); + #include ]],[[ + unsigned int x = 1, z = 2; + __asm__ __volatile__ ( + "addl %%ebx, %%eax" + : "=a"(x) + : "a"(x), "b"(z) + ); ]]) ],[supportz_assembly=yes],[supportz_assembly=no]) diff --git a/src/cpu.c b/src/cpu.c index 09ddc38..96e05bf 100644 --- a/src/cpu.c +++ b/src/cpu.c @@ -244,6 +244,12 @@ get_cpu_clock_speed(char *str1) { #elif defined(__x86_64__) +/* + * Thanks to Intel docs for pointing out + * "Out of Order Execution", added + * cpuid after reading it and edited the rdtscp + * code according to the docs + * https://www-ssl.intel.com/content/dam/www/public/us/en/documents/white-papers/ia-32-ia-64-benchmark-code-execution-paper.pdf */ static __inline__ uintmax_t rdtsc(void) { unsigned int tickhi = 0, ticklo = 0; @@ -274,8 +280,11 @@ rdtsc(void) { if (0 != (edx & (1 << 27))) { __asm__ __volatile__ ( "rdtscp\n\t" - : "=a"(ticklo), "=d"(tickhi) - :: "%rbx", "%rcx" + "mov %%edx, %0\n\t" + "mov %%eax, %1\n\t" + "cpuid\n\t" + : "=r"(tickhi), "=r"(ticklo) + :: "%rax", "%rbx", "%rcx", "%rdx" ); } -- 2.11.0