Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Better support of RDTSC instruction #23

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 34 additions & 4 deletions clock/clock_linux_stubs.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,45 @@ clock_linux_get_time_byte(__unit ())
uint64_t
clock_linux_get_tick(__unit ())
{
// struct timespec ts;
unsigned hi, lo;
#if defined(___i386__)
int64_t ret;
__asm__ __volatile__ ("rdtsc" : "=A"(ret));
return ret;
#elif defined(__x86_64__) || defined(__amd64__)
uint64_t hi, lo;
__asm__ __volatile__ ("rdtsc" : "=a"(lo), "=d"(hi));
return (lo | (hi << 32));
#elif defined(__powerpc__) || defined(__ppc__)

#if defined(__powerpc64__) || defined(__ppc64__)
int64_t tb;
__asm__ __volatile__("mfspr %0, 268" : "=r" (tb));
return tb;
#else
uint32_t tbl, tbu0, tbu1;
__asm__ __volatile__(
"mftbu %0\n"
"mftbl %1\n"
"mftbu %2"
: "=r"(tbu0), "=r"(tbl), "=r"(tbu1));
tbl &= (int32_t)(tbu0 == tbu1);
return (((uint64_t) tbu1 << 32) | ((uint64_t) tbl));
#endif

#elif defined(__ia64__)
int64_t itc;
__asm__("mov %0 = ar.itc" : "=r"(itc));
return itc;
#elif defined(__aarch64__)
int64_t virtual_timer_value;
__asm__ __volatile__("mrs %0, cntvct_el0" : "=r"(virtual_timer_value));
return virtual_timer_value;
#else
#error A Cycle Timer is not available for your OS and CPU
#endif
// XXX(dinosaure): [clock_gettime] costs a lot and are
// not really precise. [rdtsc] (Read Time Stamp Counter)
// is more reliable.

return (((unsigned long long) lo) | (((unsigned long long) hi) << 32));
}

uint64_t
Expand Down