Skip to content

Commit 1597288

Browse files
committed
tests: Add an example using cycle clock counts
Close #167
1 parent 5ba1a8f commit 1597288

File tree

1 file changed

+37
-0
lines changed

1 file changed

+37
-0
lines changed

tests/ticks.c

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
#include <inttypes.h>
2+
#include <stdint.h>
3+
#include <stdio.h>
4+
5+
typedef uint64_t ticks;
6+
static inline ticks getticks(void)
7+
{
8+
uint64_t result;
9+
uint32_t l, h, h2;
10+
asm volatile(
11+
"rdcycleh %0\n"
12+
"rdcycle %1\n"
13+
"rdcycleh %2\n"
14+
"sub %0, %0, %2\n"
15+
"seqz %0, %0\n"
16+
"sub %0, zero, %0\n"
17+
"and %1, %1, %0\n"
18+
: "=r"(h), "=r"(l), "=r"(h2));
19+
result = (((uint64_t) h) << 32) | ((uint64_t) l);
20+
return result;
21+
}
22+
23+
static uint64_t fib(uint64_t n)
24+
{
25+
if (n <= 1)
26+
return n;
27+
return fib(n - 1) + fib(n - 2);
28+
}
29+
30+
int main()
31+
{
32+
ticks t0 = getticks();
33+
fib(19);
34+
ticks t1 = getticks();
35+
printf("elapsed cycle: %" PRIu64 "\n", t1 - t0);
36+
return 0;
37+
}

0 commit comments

Comments
 (0)