Add radix sort benchmark
[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 #define static_assert(cond) switch(0) { case 0: case !!(long)(cond): ; }
35
36 static void printArray(const char name[], int n, const int arr[])
37 {
38 #if HOST_DEBUG
39 int i;
40 printf( " %10s :", name );
41 for ( i = 0; i < n; i++ )
42 printf( " %3d ", arr[i] );
43 printf( "\n" );
44 #endif
45 }
46
47 static void printDoubleArray(const char name[], int n, const double arr[])
48 {
49 #if HOST_DEBUG
50 int i;
51 printf( " %10s :", name );
52 for ( i = 0; i < n; i++ )
53 printf( " %g ", arr[i] );
54 printf( "\n" );
55 #endif
56 }
57
58 static int verify(int n, const volatile int* test, const int* verify)
59 {
60 int i;
61 // Unrolled for faster verification
62 for (i = 0; i < n/2*2; i+=2)
63 {
64 int t0 = test[i], t1 = test[i+1];
65 int v0 = verify[i], v1 = verify[i+1];
66 if (t0 != v0) return i+1;
67 if (t1 != v1) return i+2;
68 }
69 if (n % 2 != 0 && test[n-1] != verify[n-1])
70 return n;
71 return 0;
72 }
73
74 static int verifyDouble(int n, const volatile double* test, const double* verify)
75 {
76 int i;
77 // Unrolled for faster verification
78 for (i = 0; i < n/2*2; i+=2)
79 {
80 double t0 = test[i], t1 = test[i+1];
81 double v0 = verify[i], v1 = verify[i+1];
82 int eq1 = t0 == v0, eq2 = t1 == v1;
83 if (!(eq1 & eq2)) return i+1+eq1;
84 }
85 if (n % 2 != 0 && test[n-1] != verify[n-1])
86 return n;
87 return 0;
88 }
89
90 #ifndef ncores
91 #define ncores 1
92 #endif
93
94 static void __attribute__((noinline)) barrier()
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 #ifdef __riscv
115 #include "encoding.h"
116 #endif
117
118 #endif //__UTIL_H