Add more entropy to matrix multiplication input
[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 #include <stdint.h>
35
36 extern int have_vec;
37
38 #define static_assert(cond) switch(0) { case 0: case !!(long)(cond): ; }
39
40 static void printArray(const char name[], int n, const int arr[])
41 {
42 #if HOST_DEBUG
43 int i;
44 printf( " %10s :", name );
45 for ( i = 0; i < n; i++ )
46 printf( " %3d ", arr[i] );
47 printf( "\n" );
48 #endif
49 }
50
51 static void printDoubleArray(const char name[], int n, const double arr[])
52 {
53 #if HOST_DEBUG
54 int i;
55 printf( " %10s :", name );
56 for ( i = 0; i < n; i++ )
57 printf( " %g ", arr[i] );
58 printf( "\n" );
59 #endif
60 }
61
62 static int verify(int n, const volatile int* test, const int* verify)
63 {
64 int i;
65 // Unrolled for faster verification
66 for (i = 0; i < n/2*2; i+=2)
67 {
68 int t0 = test[i], t1 = test[i+1];
69 int v0 = verify[i], v1 = verify[i+1];
70 if (t0 != v0) return i+1;
71 if (t1 != v1) return i+2;
72 }
73 if (n % 2 != 0 && test[n-1] != verify[n-1])
74 return n;
75 return 0;
76 }
77
78 static int verifyDouble(int n, const volatile double* test, const double* verify)
79 {
80 int i;
81 // Unrolled for faster verification
82 for (i = 0; i < n/2*2; i+=2)
83 {
84 double t0 = test[i], t1 = test[i+1];
85 double v0 = verify[i], v1 = verify[i+1];
86 int eq1 = t0 == v0, eq2 = t1 == v1;
87 if (!(eq1 & eq2)) return i+1+eq1;
88 }
89 if (n % 2 != 0 && test[n-1] != verify[n-1])
90 return n;
91 return 0;
92 }
93
94 static void __attribute__((noinline)) barrier(int ncores)
95 {
96 static volatile int sense;
97 static volatile int count;
98 static __thread int threadsense;
99
100 __sync_synchronize();
101
102 threadsense = !threadsense;
103 if (__sync_fetch_and_add(&count, 1) == ncores-1)
104 {
105 count = 0;
106 sense = threadsense;
107 }
108 else while(sense != threadsense)
109 ;
110
111 __sync_synchronize();
112 }
113
114 static uint64_t lfsr(uint64_t x)
115 {
116 uint64_t bit = (x ^ (x >> 1)) & 1;
117 return (x >> 1) | (bit << 62);
118 }
119
120 #ifdef __riscv
121 #include "encoding.h"
122 #endif
123
124 #define stringify_1(s) #s
125 #define stringify(s) stringify_1(s)
126 #define stats(code, iter) do { \
127 unsigned long _c = -rdcycle(), _i = -rdinstret(); \
128 code; \
129 _c += rdcycle(), _i += rdinstret(); \
130 if (cid == 0) \
131 printf("\n%s: %ld cycles, %ld.%ld cycles/iter, %ld.%ld CPI\n", \
132 stringify(code), _c, _c/iter, 10*_c/iter%10, _c/_i, 10*_c/_i%10); \
133 } while(0)
134
135 #endif //__UTIL_H