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