cosmopolitan/libc/nexgen32e/rdtsc.h
2023-05-10 04:20:46 -07:00

53 lines
1.6 KiB
C

#ifndef COSMOPOLITAN_LIBC_NEXGEN32E_RDTSC_H_
#define COSMOPOLITAN_LIBC_NEXGEN32E_RDTSC_H_
#if !(__ASSEMBLER__ + __LINKER__ + 0)
COSMOPOLITAN_C_START_
/**
* Reads CPU timestamp counter.
*
* This macro inhibits compiler magic.
* This macro does not inhibit CPU magic.
*
* @see X86_HAVE(INVTSC)
*/
#define rdtsc() __RDTSC("rdtsc")
/**
* Reads CPU timestamp counter w/ full serialization.
*
* This macro inhibits CPU magic.
* This macro inhibits compiler magic.
*
* The clock isn't read until:
*
* 1. previous instructions finish executing; and
* 2. previous loads are globally visible; and
* 3. previous stores are globally visible.
*
* Later instructions won't dispatch until RDTSC completes.
*
* @see X86_HAVE(INVTSC)
*/
#define mfence_lfence_rdtsc_lfence() \
__RDTSC("mfence\n\tlfence\n\trdtsc\n\tlfence")
#ifdef __x86__
#define __RDTSC(ASM) \
({ \
uint64_t Rax, Rdx; \
asm volatile(ASM : "=a"(Rax), "=d"(Rdx) : /* no inputs */ : "memory"); \
Rdx << 32 | Rax; \
})
#elif defined(__aarch64__)
#define __RDTSC(ASM) \
({ \
uint64_t _Ts; \
asm volatile("mrs\t%0,cntvct_el0" : "=r"(_Ts)); \
_Ts * 48; /* the fudge factor */ \
})
#endif
COSMOPOLITAN_C_END_
#endif /* !(__ASSEMBLER__ + __LINKER__ + 0) */
#endif /* COSMOPOLITAN_LIBC_NEXGEN32E_RDTSC_H_ */