Merge pull request #1 from smirolo/configure
[riscv-tests.git] / mt / 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 #define rdcycle() ({ unsigned long _c; asm volatile ("rdcycle %0" : "=r"(_c) :: "memory"); _c; })
9 #define rdinstret() ({ unsigned long _c; asm volatile ("rdinstret %0" : "=r"(_c) :: "memory"); _c; })
10
11 void __attribute__((noinline)) barrier()
12 {
13 static volatile int sense;
14 static volatile int count;
15 static __thread int threadsense;
16
17 __sync_synchronize();
18
19 threadsense = !threadsense;
20 if (__sync_fetch_and_add(&count, 1) == ncores-1)
21 {
22 count = 0;
23 sense = threadsense;
24 }
25 else while(sense != threadsense)
26 ;
27
28 __sync_synchronize();
29 }
30
31 #endif //__UTIL_H
32