#include #include #include #include static inline unsigned long rdtsc () { unsigned long var; unsigned int hi, lo; asm volatile ("rdtsc" : "=a" (lo), "=d" (hi)); var = ((unsigned long long int) hi << 32) | lo; return var; } typedef unsigned long long u64; int main() { struct timespec ts; u64 start, end, delta, mid, p12; do { start= rdtsc(); clock_gettime(CLOCK_MONOTONIC_RAW, &ts); end = rdtsc(); delta = end-start; } while (delta > 20000); // make sure the reads were not preempted printf("start: %llu end: %llu delta: %llu\n", start, end, delta); printf("MONO_RAW: %llu\n", (u64)ts.tv_sec * 1000000000 + ts.tv_nsec); mid = start + (delta + 1)/2; //round-closest printf("MID: %llu\n", mid); p12 = start + ((delta * 12) + (100/2))/100; printf("P12: %llu\n", p12); }