setStats in benchmarks now should set and unset the stats register. Also, removed...
[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 #if HOST_DEBUG
28 #include <stdio.h>
29 static void setStats(int enable) {}
30 #else
31 extern void setStats(int enable);
32 #endif
33
34 static void printArray(const char name[], int n, const int arr[])
35 {
36 #if HOST_DEBUG
37 int i;
38 printf( " %10s :", name );
39 for ( i = 0; i < n; i++ )
40 printf( " %3d ", arr[i] );
41 printf( "\n" );
42 #endif
43 }
44
45 static void printDoubleArray(const char name[], int n, const double arr[])
46 {
47 #if HOST_DEBUG
48 int i;
49 printf( " %10s :", name );
50 for ( i = 0; i < n; i++ )
51 printf( " %g ", arr[i] );
52 printf( "\n" );
53 #endif
54 }
55
56 static int verify(int n, const volatile int* test, const int* verify)
57 {
58 int i;
59 // Unrolled for faster verification
60 for (i = 0; i < n/2*2; i+=2)
61 {
62 int t0 = test[i], t1 = test[i+1];
63 int v0 = verify[i], v1 = verify[i+1];
64 if (t0 != v0) return i+1;
65 if (t1 != v1) return i+2;
66 }
67 if (n % 2 != 0 && test[n-1] != verify[n-1])
68 return n;
69 return 0;
70 }
71
72 static int verifyDouble(int n, const volatile double* test, const double* verify)
73 {
74 int i;
75 // Unrolled for faster verification
76 for (i = 0; i < n/2*2; i+=2)
77 {
78 double t0 = test[i], t1 = test[i+1];
79 double v0 = verify[i], v1 = verify[i+1];
80 int eq1 = t0 == v0, eq2 = t1 == v1;
81 if (!(eq1 & eq2)) return i+1+eq1;
82 }
83 if (n % 2 != 0 && test[n-1] != verify[n-1])
84 return n;
85 return 0;
86 }
87
88 #ifndef ncores
89 #define ncores 1
90 #endif
91
92 static void __attribute__((noinline)) barrier()
93 {
94 static volatile int sense;
95 static volatile int count;
96 static __thread int threadsense;
97
98 __sync_synchronize();
99
100 threadsense = !threadsense;
101 if (__sync_fetch_and_add(&count, 1) == ncores-1)
102 {
103 count = 0;
104 sense = threadsense;
105 }
106 else while(sense != threadsense)
107 ;
108
109 __sync_synchronize();
110 }
111
112 #ifdef __riscv
113 #include "encoding.h"
114 #endif
115
116 #endif //__UTIL_H