Run benchmarks in user mode
[riscv-tests.git] / benchmarks / common / util.h
1 #ifndef __UTIL_H
2 #define __UTIL_H
3
4 //--------------------------------------------------------------------------
5 // Macros
6
7 // Set HOST_DEBUG to 1 if you are going to compile this for a host
8 // machine (ie Athena/Linux) for debug purposes and set HOST_DEBUG
9 // to 0 if you are compiling with the smips-gcc toolchain.
10
11 #ifndef HOST_DEBUG
12 #define HOST_DEBUG 0
13 #endif
14
15 // Set PREALLOCATE to 1 if you want to preallocate the benchmark
16 // function before starting stats. If you have instruction/data
17 // caches and you don't want to count the overhead of misses, then
18 // you will need to use preallocation.
19
20 #ifndef PREALLOCATE
21 #define PREALLOCATE 0
22 #endif
23
24 // Set SET_STATS to 1 if you want to carve out the piece that actually
25 // does the computation.
26
27 #ifndef SET_STATS
28 #define SET_STATS 0
29 #endif
30
31 #if HOST_DEBUG
32 #include <stdio.h>
33 static void setStats(int enable) {}
34 #else
35 extern void setStats(int enable);
36 #endif
37
38 static void printArray(const char name[], int n, const int arr[])
39 {
40 #if HOST_DEBUG
41 int i;
42 printf( " %10s :", name );
43 for ( i = 0; i < n; i++ )
44 printf( " %3d ", arr[i] );
45 printf( "\n" );
46 #endif
47 }
48
49 static void printDoubleArray(const char name[], int n, const double arr[])
50 {
51 #if HOST_DEBUG
52 int i;
53 printf( " %10s :", name );
54 for ( i = 0; i < n; i++ )
55 printf( " %g ", arr[i] );
56 printf( "\n" );
57 #endif
58 }
59
60 static int verify(int n, const int test[], const int verify[])
61 {
62 int i;
63 // Unrolled for faster verification
64 for (i = 0; i < n/2*2; i+=2)
65 {
66 int t0 = test[i], t1 = test[i+1];
67 int v0 = verify[i], v1 = verify[i+1];
68 if (t0 != v0) return i+1;
69 if (t1 != v1) return i+2;
70 }
71 if (n % 2 != 0 && test[n-1] != verify[n-1])
72 return n;
73 return 0;
74 }
75
76 static int verifyDouble(int n, const double test[], const double verify[])
77 {
78 int i;
79 // Unrolled for faster verification
80 for (i = 0; i < n/2*2; i+=2)
81 {
82 double t0 = test[i], t1 = test[i+1];
83 double v0 = verify[i], v1 = verify[i+1];
84 int eq1 = t0 == v0, eq2 = t1 == v1;
85 if (!(eq1 & eq2)) return i+1+eq1;
86 }
87 if (n % 2 != 0 && test[n-1] != verify[n-1])
88 return n;
89 return 0;
90 }
91
92 #ifndef ncores
93 #define ncores 1
94 #endif
95
96 static void __attribute__((noinline)) barrier()
97 {
98 static volatile int sense;
99 static volatile int count;
100 static __thread int threadsense;
101
102 __sync_synchronize();
103
104 threadsense = !threadsense;
105 if (__sync_fetch_and_add(&count, 1) == ncores-1)
106 {
107 count = 0;
108 sense = threadsense;
109 }
110 else while(sense != threadsense)
111 ;
112
113 __sync_synchronize();
114 }
115
116 #ifdef __riscv
117 #include "encoding.h"
118 #endif
119
120 #endif //__UTIL_H