c35bf7cde831b39354e7da5791737694840adb54
[riscv-tests.git] / benchmarks / common / util.h
1 // See LICENSE for license details.
2
3 #ifndef __UTIL_H
4 #define __UTIL_H
5
6 //--------------------------------------------------------------------------
7 // Macros
8
9 // Set HOST_DEBUG to 1 if you are going to compile this for a host
10 // machine (ie Athena/Linux) for debug purposes and set HOST_DEBUG
11 // to 0 if you are compiling with the smips-gcc toolchain.
12
13 #ifndef HOST_DEBUG
14 #define HOST_DEBUG 0
15 #endif
16
17 // Set PREALLOCATE to 1 if you want to preallocate the benchmark
18 // function before starting stats. If you have instruction/data
19 // caches and you don't want to count the overhead of misses, then
20 // you will need to use preallocation.
21
22 #ifndef PREALLOCATE
23 #define PREALLOCATE 0
24 #endif
25
26 // Set SET_STATS to 1 if you want to carve out the piece that actually
27 // does the computation.
28
29 #if HOST_DEBUG
30 #include <stdio.h>
31 static void setStats(int enable) {}
32 #else
33 extern void setStats(int enable);
34 #endif
35
36 #include <stdint.h>
37
38 extern int have_vec;
39
40 #define static_assert(cond) switch(0) { case 0: case !!(long)(cond): ; }
41
42 static void printArray(const char name[], int n, const int arr[])
43 {
44 #if HOST_DEBUG
45 int i;
46 printf( " %10s :", name );
47 for ( i = 0; i < n; i++ )
48 printf( " %3d ", arr[i] );
49 printf( "\n" );
50 #endif
51 }
52
53 static void printDoubleArray(const char name[], int n, const double arr[])
54 {
55 #if HOST_DEBUG
56 int i;
57 printf( " %10s :", name );
58 for ( i = 0; i < n; i++ )
59 printf( " %g ", arr[i] );
60 printf( "\n" );
61 #endif
62 }
63
64 static int verify(int n, const volatile int* test, const int* verify)
65 {
66 int i;
67 // Unrolled for faster verification
68 for (i = 0; i < n/2*2; i+=2)
69 {
70 int t0 = test[i], t1 = test[i+1];
71 int v0 = verify[i], v1 = verify[i+1];
72 if (t0 != v0) return i+1;
73 if (t1 != v1) return i+2;
74 }
75 if (n % 2 != 0 && test[n-1] != verify[n-1])
76 return n;
77 return 0;
78 }
79
80 static int verifyDouble(int n, const volatile double* test, const double* verify)
81 {
82 int i;
83 // Unrolled for faster verification
84 for (i = 0; i < n/2*2; i+=2)
85 {
86 double t0 = test[i], t1 = test[i+1];
87 double v0 = verify[i], v1 = verify[i+1];
88 int eq1 = t0 == v0, eq2 = t1 == v1;
89 if (!(eq1 & eq2)) return i+1+eq1;
90 }
91 if (n % 2 != 0 && test[n-1] != verify[n-1])
92 return n;
93 return 0;
94 }
95
96 static void __attribute__((noinline)) barrier(int ncores)
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 static uint64_t lfsr(uint64_t x)
117 {
118 uint64_t bit = (x ^ (x >> 1)) & 1;
119 return (x >> 1) | (bit << 62);
120 }
121
122 #ifdef __riscv
123 #include "encoding.h"
124 #endif
125
126 #define stringify_1(s) #s
127 #define stringify(s) stringify_1(s)
128 #define stats(code, iter) do { \
129 unsigned long _c = -read_csr(mcycle), _i = -read_csr(minstret); \
130 code; \
131 _c += read_csr(mcycle), _i += read_csr(minstret); \
132 if (cid == 0) \
133 printf("\n%s: %ld cycles, %ld.%ld cycles/iter, %ld.%ld CPI\n", \
134 stringify(code), _c, _c/iter, 10*_c/iter%10, _c/_i, 10*_c/_i%10); \
135 } while(0)
136
137 #endif //__UTIL_H