Created
November 27, 2021 06:32
-
-
Save XieJiSS/18acd12685ecc0913d43d6bcfb3baefd to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <stdint.h> | |
#include <sys/time.h> | |
#include <time.h> | |
#include <inttypes.h> | |
static inline uint64_t toku_time_now(void) { | |
#if defined(__x86_64__) || defined(__i386__) | |
uint32_t lo, hi; | |
__asm__ __volatile__("rdtsc" : "=a"(lo), "=d"(hi)); | |
return (uint64_t)hi << 32 | lo; | |
#elif defined(__aarch64__) | |
uint64_t result; | |
__asm __volatile__("mrs %[rt], cntvct_el0" : [ rt ] "=r"(result)); | |
return result; | |
#elif defined(__powerpc__) | |
return __ppc_get_timebase(); | |
#elif defined(__s390x__) | |
uint64_t result; | |
asm volatile("stckf %0" : "=Q"(result) : : "cc"); | |
return result; | |
#elif defined(__riscv) && __riscv_xlen == 32 | |
uint32_t cycles_lo, cycles_hi0, cycles_hi1; | |
// Implemented in assembly because Clang insisted on branching. | |
asm volatile( | |
"rdcycleh %0\n" | |
"rdcycle %1\n" | |
"rdcycleh %2\n" | |
"sub %0, %0, %2\n" | |
"seqz %0, %0\n" | |
"sub %0, zero, %0\n" | |
"and %1, %1, %0\n" | |
: "=r"(cycles_hi0), "=r"(cycles_lo), "=r"(cycles_hi1)); | |
return (static_cast<uint64_t>(cycles_hi1) << 32) | cycles_lo; | |
#elif defined(__riscv) && __riscv_xlen == 64 | |
uint64_t cycles; | |
asm volatile("rdcycle %0" : "=r"(cycles)); | |
return cycles; | |
#else | |
#error No timer implementation for this platform | |
#endif | |
} | |
int main() { | |
printf("%" PRIu64 "\n", toku_time_now()); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment