79d9256a7060c0eedfd2919a2ee815e1155b65b5
[riscv-tests.git] / benchmarks / common / util.h
1 // helpful utility and synch functions
2
3 // relies on defining "ncores" before including this file...
4
5 #ifndef __UTIL_H
6 #define __UTIL_H
7
8 #include <machine/syscall.h>
9
10 #define rdcycle() ({ unsigned long _c; asm volatile ("rdcycle %0" : "=r"(_c) :: "memory"); _c; })
11 #define rdinstret() ({ unsigned long _c; asm volatile ("rdinstret %0" : "=r"(_c) :: "memory"); _c; })
12
13 void __attribute__((noinline)) barrier()
14 {
15 static volatile int sense;
16 static volatile int count;
17 static __thread int threadsense;
18
19 __sync_synchronize();
20
21 threadsense = !threadsense;
22 if (__sync_fetch_and_add(&count, 1) == ncores-1)
23 {
24 count = 0;
25 sense = threadsense;
26 }
27 else while(sense != threadsense)
28 ;
29
30 __sync_synchronize();
31 }
32
33
34
35
36
37 void finishTest(int test_result)
38 {
39 #if HOST_DEBUG
40 if ( test_result == 1 )
41 printf( "*** PASSED ***\n" );
42 else
43 printf( "*** FAILED *** (tohost = %d)\n", test_result);
44 exit(0);
45 #else
46 {
47 // perform exit syscall
48 asm volatile(
49 "move a0,%0 ;"
50 "li a1,0 ;"
51 "li a2,0 ;"
52 "li a3,0 ;"
53 "li v0,%1 ;"
54 "syscall" : : "r"(test_result) , "i"(SYS_exit));
55 }
56 #endif
57 }
58
59 #endif //__UTIL_H
60