benchmarks initial commit
[riscv-tests.git] / benchmarks / multiply / multiply_main.c
1 // *************************************************************************
2 // multiply filter bencmark
3 // -------------------------------------------------------------------------
4 //
5 // This benchmark tests the software multiply implemenation. The
6 // input data (and reference data) should be generated using the
7 // multiply_gendata.pl perl script and dumped to a file named
8 // dataset1.h You should not change anything except the
9 // HOST_DEBUG and VERIFY macros for your timing run.
10
11 #include "multiply.h"
12
13 //--------------------------------------------------------------------------
14 // Macros
15
16 // Set HOST_DEBUG to 1 if you are going to compile this for a host
17 // machine (ie Athena/Linux) for debug purposes and set HOST_DEBUG
18 // to 0 if you are compiling with the smips-gcc toolchain.
19
20 #ifndef HOST_DEBUG
21 #define HOST_DEBUG 0
22 #endif
23
24 // Set PREALLOCATE to 1 if you want to preallocate the benchmark
25 // function before starting stats. If you have instruction/data
26 // caches and you don't want to count the overhead of misses, then
27 // you will need to use preallocation.
28
29 #ifndef PREALLOCATE
30 #define PREALLOCATE 0
31 #endif
32
33 // Set VERIFY to 1 if you want the program to check that the sort
34 // function returns the right answer. When you are doing your
35 // benchmarking you should set this to 0 so that the verification
36 // is not included in your timing.
37
38 #ifndef VERIFY
39 #define VERIFY 1
40 #endif
41
42 // Set SET_STATS to 1 if you want to carve out the piece that actually
43 // does the computation.
44
45 #ifndef SET_STATS
46 #define SET_STATS 0
47 #endif
48
49 //--------------------------------------------------------------------------
50 // Input/Reference Data
51
52 #include "dataset1.h"
53
54 //--------------------------------------------------------------------------
55 // Helper functions
56
57 int verify( int n, int test[], int correct[] )
58 {
59 int i;
60 for ( i = 0; i < n; i++ ) {
61 if ( test[i] != correct[i] ) {
62 return 2;
63 }
64 }
65 return 1;
66 }
67
68 #if HOST_DEBUG
69 void printArray( char name[], int n, int arr[] )
70 {
71 int i;
72 printf( " %10s :", name );
73 for ( i = 0; i < n; i++ )
74 printf( " %3d ", arr[i] );
75 printf( "\n" );
76 }
77 #endif
78
79 void finishTest( int toHostValue )
80 {
81 #if HOST_DEBUG
82 if ( toHostValue == 1 )
83 printf( "*** PASSED ***\n" );
84 else
85 printf( "*** FAILED *** (tohost = %d)\n", toHostValue );
86 exit(0);
87 #else
88 asm( "mtpcr %0, cr30" : : "r" (toHostValue) );
89 while ( 1 ) { }
90 #endif
91 }
92
93 void setStats( int enable )
94 {
95 #if ( !HOST_DEBUG && SET_STATS )
96 asm( "mtpcr %0, cr10" : : "r" (enable) );
97 #endif
98 }
99
100 //--------------------------------------------------------------------------
101 // Main
102
103 int main( int argc, char* argv[] )
104 {
105 int i;
106 int results_data[DATA_SIZE];
107
108 // Output the input arrays
109
110 #if HOST_DEBUG
111 printArray( "input1", DATA_SIZE, input_data1 );
112 printArray( "input2", DATA_SIZE, input_data2 );
113 printArray( "verify", DATA_SIZE, verify_data );
114 #endif
115
116 #if ( !HOST_DEBUG && PREALLOCATE )
117 for (i = 0; i < DATA_SIZE; i++)
118 {
119 results_data[i] = multiply( input_data1[i], input_data2[i] );
120 }
121 #endif
122
123 #if HOST_DEBUG
124 for (i = 0; i < DATA_SIZE; i++)
125 {
126 results_data[i] = multiply( input_data1[i], input_data2[i] );
127 }
128 #else
129 setStats(1);
130 for (i = 0; i < DATA_SIZE; i++)
131 {
132 results_data[i] = multiply( input_data1[i], input_data2[i] );
133 }
134 setStats(0);
135 #endif
136
137 // Print out the results
138
139 #if HOST_DEBUG
140 printArray( "results", DATA_SIZE, results_data );
141 #endif
142
143 // Check the results
144
145 finishTest(verify( DATA_SIZE, results_data, verify_data ));
146
147 }