aboutsummaryrefslogtreecommitdiffstats
path: root/test/monniaux/cycles.h
blob: 4231a34e2f022f9ca9f4ca9342828b741942d89e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
typedef unsigned long cycle_t;

#ifdef __K1C__
#include <../../k1-cos/include/hal/cos_registers.h>

static inline void cycle_count_config(void)
{
        /* config pmc for cycle count */
        cycle_t pmc_value = __builtin_k1_get(COS_SFR_PMC);

        pmc_value &= ~(0xfULL);
        __builtin_k1_set(COS_SFR_PMC, pmc_value);
}

static inline cycle_t get_cycle(void)
{
        return __builtin_k1_get(COS_SFR_PM0);
}

#else // not K1C
static inline void cycle_count_config(void) { }

#ifdef  __x86_64__
#include <x86intrin.h>
static inline cycle_t get_cycle(void) { return __rdtsc(); }

#elif __riscv
static inline cycle_t get_cycle(void) {
  cycle_t cycles;
  asm volatile ("rdcycle %0" : "=r" (cycles));
  return cycles;
}

#else
static inline cycle_t get_cycle(void) { return 0; }
#endif
#endif