Merge branch 'master' of github.com:ucb-bar/riscv-tests
[riscv-tests.git] / benchmarks / median / median_main.c
1 //**************************************************************************
2 // Median filter bencmark
3 //--------------------------------------------------------------------------
4 //
5 // This benchmark performs a 1D three element median filter. The
6 // input data (and reference data) should be generated using the
7 // median_gendata.pl perl script and dumped to a file named
8 // dataset1.h You should not change anything except the
9 // HOST_DEBUG and PREALLOCATE macros for your timing run.
10
11 #include "median.h"
12
13 int ncores = 1;
14 #include "util.h"
15
16 //--------------------------------------------------------------------------
17 // Macros
18
19 // Set HOST_DEBUG to 1 if you are going to compile this for a host
20 // machine (ie Athena/Linux) for debug purposes and set HOST_DEBUG
21 // to 0 if you are compiling with the smips-gcc toolchain.
22
23 #ifndef HOST_DEBUG
24 #define HOST_DEBUG 0
25 #endif
26
27 // Set PREALLOCATE to 1 if you want to preallocate the benchmark
28 // function before starting stats. If you have instruction/data
29 // caches and you don't want to count the overhead of misses, then
30 // you will need to use preallocation.
31
32 #ifndef PREALLOCATE
33 #define PREALLOCATE 0
34 #endif
35
36 // Set SET_STATS to 1 if you want to carve out the piece that actually
37 // does the computation.
38
39 #ifndef SET_STATS
40 #define SET_STATS 0
41 #endif
42
43 //--------------------------------------------------------------------------
44 // Input/Reference Data
45
46 #include "dataset1.h"
47
48 //--------------------------------------------------------------------------
49 // Helper functions
50
51 int verify( int n, int test[], int correct[] )
52 {
53 int i;
54 for ( i = 0; i < n; i++ ) {
55 if ( test[i] != correct[i] ) {
56 return 2;
57 }
58 }
59 return 1;
60 }
61
62 #if HOST_DEBUG
63 void printArray( char name[], int n, int arr[] )
64 {
65 int i;
66 printf( " %10s :", name );
67 for ( i = 0; i < n; i++ )
68 printf( " %3d ", arr[i] );
69 printf( "\n" );
70 }
71 #endif
72
73 void setStats( int enable )
74 {
75 #if ( !HOST_DEBUG && SET_STATS )
76 asm( "mtpcr %0, cr10" : : "r" (enable) );
77 #endif
78 }
79
80 //--------------------------------------------------------------------------
81 // Main
82
83 int main( int argc, char* argv[] )
84 {
85 int results_data[DATA_SIZE];
86
87 // Output the input array
88
89 #if HOST_DEBUG
90 printArray( "input", DATA_SIZE, input_data );
91 printArray( "verify", DATA_SIZE, verify_data );
92 #endif
93
94 // If needed we preallocate everything in the caches
95
96 #if ( !HOST_DEBUG && PREALLOCATE )
97 median( DATA_SIZE, input_data, results_data );
98 #endif
99
100 // Do the filter
101
102 #if HOST_DEBUG
103 median( DATA_SIZE, input_data, results_data );
104 #else
105 setStats(1);
106 median( DATA_SIZE, input_data, results_data );
107 setStats(0);
108 #endif
109
110 // Print out the results
111
112 #if HOST_DEBUG
113 printArray( "results", DATA_SIZE, results_data );
114 #endif
115
116 // Check the results
117
118 finishTest(verify( DATA_SIZE, results_data, verify_data ));
119
120 }