cosmopolitan/libc/nexgen32e/rdtsc.h
Justine Tunney 8f522cb702
Make improvements
This change progresses our AARCH64 support:

- The AARCH64 build and tests are now passing
- Add 128-bit floating-point support to printf()
- Fix clone() so it initializes cosmo's x28 TLS register
- Fix TLS memory layout issue with aarch64 _Alignas vars
- Revamp microbenchmarking tools so they work on aarch64
- Make some subtle improvements to aarch64 crash reporting
- Make kisdangerous() memory checks more accurate on aarch64
- Remove sys_open() since it's not available on Linux AARCH64

This change makes general improvements to Cosmo and Redbean:

- Introduce GetHostIsa() function in Redbean
- You can now feature check using pledge(0, 0)
- You can now feature check using unveil("",0)
- Refactor some more x86-specific asm comments
- Refactor and write docs for some libm functions
- Make the mmap() API behave more similar to Linux
- Fix WIFSIGNALED() which wrongly returned true for zero
- Rename some obscure cosmo keywords from noFOO to dontFOO
2023-06-03 08:12:22 -07:00

67 lines
2.2 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 */ \
})
#elif defined(__powerpc64__)
#define __RDTSC(ASM) \
({ \
uint64_t _Ts; \
asm volatile("mfspr\t%0,268" : "=r"(_Ts)); \
_Ts; \
})
#elif defined(__riscv)
#define __RDTSC(ASM) \
({ \
uint64_t _Ts; \
asm volatile("rdcycle\t%0" : "=r"(_Ts)); \
_Ts; \
})
#endif
COSMOPOLITAN_C_END_
#endif /* !(__ASSEMBLER__ + __LINKER__ + 0) */
#endif /* COSMOPOLITAN_LIBC_NEXGEN32E_RDTSC_H_ */