Reflect changes to ISA
[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 //--------------------------------------------------------------------------
15 // Macros
16
17 // Set HOST_DEBUG to 1 if you are going to compile this for a host
18 // machine (ie Athena/Linux) for debug purposes and set HOST_DEBUG
19 // to 0 if you are compiling with the smips-gcc toolchain.
20
21 #ifndef HOST_DEBUG
22 #define HOST_DEBUG 0
23 #endif
24
25 // Set PREALLOCATE to 1 if you want to preallocate the benchmark
26 // function before starting stats. If you have instruction/data
27 // caches and you don't want to count the overhead of misses, then
28 // you will need to use preallocation.
29
30 #ifndef PREALLOCATE
31 #define PREALLOCATE 0
32 #endif
33
34 // Set SET_STATS to 1 if you want to carve out the piece that actually
35 // does the computation.
36
37 #ifndef SET_STATS
38 #define SET_STATS 0
39 #endif
40
41 //--------------------------------------------------------------------------
42 // Input/Reference Data
43
44 #include "dataset1.h"
45
46 //--------------------------------------------------------------------------
47 // Helper functions
48
49 int verify( int n, int test[], int correct[] )
50 {
51 int i;
52 for ( i = 0; i < n; i++ ) {
53 if ( test[i] != correct[i] ) {
54 return 2;
55 }
56 }
57 return 1;
58 }
59
60 #if HOST_DEBUG
61 void printArray( char name[], int n, int arr[] )
62 {
63 int i;
64 printf( " %10s :", name );
65 for ( i = 0; i < n; i++ )
66 printf( " %3d ", arr[i] );
67 printf( "\n" );
68 }
69 #endif
70
71 void finishTest( int toHostValue )
72 {
73 #if HOST_DEBUG
74 if ( toHostValue == 1 )
75 printf( "*** PASSED ***\n" );
76 else
77 printf( "*** FAILED *** (tohost = %d)\n", toHostValue );
78 exit(0);
79 #else
80 asm( "mtpcr %0, tohost" : : "r" (toHostValue) );
81 while ( 1 ) { }
82 #endif
83 }
84
85 void setStats( int enable )
86 {
87 #if ( !HOST_DEBUG && SET_STATS )
88 asm( "mtpcr %0, cr10" : : "r" (enable) );
89 #endif
90 }
91
92 //--------------------------------------------------------------------------
93 // vvadd function
94
95 void vvadd( int n, int a[], int b[], int c[] )
96 {
97 int i;
98 for ( i = 0; i < n; i++ )
99 c[i] = a[i] + b[i];
100 }
101
102 //--------------------------------------------------------------------------
103 // Main
104
105 int main( int argc, char* argv[] )
106 {
107 int results_data[DATA_SIZE];
108
109 // Output the input array
110
111 #if HOST_DEBUG
112 printArray( "input1", DATA_SIZE, input1_data );
113 printArray( "input2", DATA_SIZE, input2_data );
114 printArray( "verify", DATA_SIZE, verify_data );
115 #endif
116
117 // If needed we preallocate everything in the caches
118
119 #if PREALLOCATE
120 vvadd( DATA_SIZE, input1_data, input2_data, results_data );
121 #endif
122
123 // Do the vvadd
124
125 setStats(1);
126 vvadd( DATA_SIZE, input1_data, input2_data, results_data );
127 setStats(0);
128
129 // Print out the results
130
131 #if HOST_DEBUG
132 printArray( "results", DATA_SIZE, results_data );
133 #endif
134
135 // Check the results
136
137 finishTest(verify( DATA_SIZE, results_data, verify_data ));
138
139 }