#include #include static uint64_t shift; static uint64_t mult; static uint64_t offset; /** * Converts the input khz cycle counter frequency to a time source multiplier. * The multiplier is used to convert cycle counts to nanoseconds. */ void init_cycles2ns(uint32_t khz) { /* * Shift is used to obtain greater precision. * Linux uses 22 for the x86 time stamp counter. * For now we assume this will work for most cases. */ shift = 22; /* * khz = cyc/(Million ns) * mult/2^shift = ns/cyc * mult = ns/cyc * 2^shift * mult = 1Million/khz * 2^shift * mult = 1000000 * 2^shift / khz * mult = (1000000<> shift; } /** * Returns the current time in nanoseconds. */ uint64_t get_time(void) { return cycles2ns(get_cycles()) + offset; } /** * Sets the current time in nanoseconds. */ void set_time(uint64_t ns) { offset = ns - cycles2ns(get_cycles()); }