// SafeC Standard Library — Performance Counters
// Hardware performance counter access (x86-64 RDTSC, AArch64 PMCCNTR, RISC-V cycle).
// Freestanding-safe.
#pragma once
namespace std {
struct PerfCounter {
unsigned long long start_val;
unsigned long long end_val;
unsigned long long freq_hz; // counter frequency (0 = unknown)
// A zeroed counter with freq_hz detected from arch-specific sources
// (calibrated against clock() when hosted; 0 when freestanding).
static struct PerfCounter new();
// Start measurement.
void start();
// Stop measurement.
void stop();
// Return elapsed counter ticks.
unsigned long long ticks() const;
// Return elapsed nanoseconds (0 if freq unknown).
unsigned long long ns() const;
};
// Read the raw cycle counter for the current arch.
unsigned long long perf_read_cycle();
// Read timestamp in nanoseconds (best-effort; returns 0 in freestanding without freq).
unsigned long long perf_read_ns();
#ifdef __SAFEC_COMPAT_PREPROCESSOR__ // function-like macro: compat mode only
#define PERF_BEGIN(ctr) do { (ctr).start(); } while(0)
#endif
#ifdef __SAFEC_COMPAT_PREPROCESSOR__ // function-like macro: compat mode only
#define PERF_END(ctr) do { (ctr).stop(); } while(0)
#endif
} // namespace std