6c4f963f01dc79c961f6d43f1d058450c5c19d02
[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 extern int have_vec;
35
36 #define static_assert(cond) switch(0) { case 0: case !!(long)(cond): ; }
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 volatile 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 volatile 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 static void __attribute__((noinline)) barrier(int ncores)
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