Add a top-level make clean target.
[riscv-tests.git] / benchmarks / vec-vvadd / vec_vvadd_main.c
1 // See LICENSE for license details.
2
3 //**************************************************************************
4 // Vector-Thread Vector-vector add benchmark
5 //--------------------------------------------------------------------------
6 //
7 // This benchmark uses adds to vectors and writes the results to a third
8 // vector. The input data (and reference data) should be generated using the
9 // vvadd_gendata.pl perl script and dumped to a file named dataset.h.
10
11 // Choose which implementation you wish to test... but leave only one on!
12 // (only the first one will be executed).
13 //#define SCALAR_C
14 //#define SCALAR_ASM
15 #define VT_ASM
16
17 //--------------------------------------------------------------------------
18 // Input/Reference Data
19
20 //#include "dataset_test.h"
21 #include "dataset.h"
22
23 //--------------------------------------------------------------------------
24 // Helper functions
25
26 int verify( int n, float test[], float correct[] )
27 {
28 int i;
29 for ( i = 0; i < n; i++ ) {
30 // if ( test[i] != correct[i] ) {
31 if ( test[i] > 1.02*correct[i]
32 || test[i] < 0.98*correct[i]) {
33 #if HOST_DEBUG
34 printf(" test[%d] : %3.2f\n", i, test[i]);
35 printf(" corr[%d] : %3.2f\n", i, correct[i]);
36 #endif
37 // tell us which index fails + 2
38 // (so that if i==0,i==1 fails, we don't
39 // think it was a 'not-finished yet' or pass)
40 // return i+10;
41 return 2;
42 }
43 }
44 return 1;
45 }
46
47 void finishTest( int correct, long long num_cycles, long long num_retired )
48 {
49 int toHostValue = correct;
50 #if HOST_DEBUG
51 if ( toHostValue == 1 )
52 printf( "*** PASSED ***\n" );
53 else
54 printf( "*** FAILED *** (tohost = %d)\n", toHostValue );
55 exit(0);
56 #else
57 // we no longer run in -testrun mode, which means we can't use
58 // the tohost register to communicate "test is done" and "test results"
59 // so instead we will communicate through print* functions!
60 if ( correct == 1 )
61 {
62 printstr( "*** PASSED *** (num_cycles = 0x" );
63 printhex(num_cycles);
64 printstr( ", num_inst_retired = 0x");
65 printhex(num_retired);
66 printstr( ")\n" );
67 }
68 else
69 {
70 printstr( "*** FAILED *** (num_cycles = 0x");
71 printhex(num_cycles);
72 printstr( ", num_inst_retired = 0x");
73 printhex(num_retired);
74 printstr( ")\n" );
75 }
76 exit();
77 #endif
78 }
79
80
81 // deprecated - cr10/stats-enable register no longer exists
82 void setStats( int enable )
83 {
84 #if ( !HOST_DEBUG && SET_STATS )
85 asm( "mtpcr %0, cr10" : : "r" (enable) );
86 #endif
87 }
88
89 long long getCycles()
90 {
91 long long cycles = 1337;
92 #if ( !HOST_DEBUG && SET_STATS )
93 __asm__ __volatile__( "rdcycle %0" : "=r" (cycles) );
94 #endif
95 return cycles;
96 }
97
98 long long getInstRetired()
99 {
100 long long inst_retired = 1338;
101 #if ( !HOST_DEBUG && SET_STATS )
102 __asm__ __volatile__( "rdinstret %0" : "=r" (inst_retired) );
103 #endif
104 return inst_retired;
105 }
106
107 //--------------------------------------------------------------------------
108 // vvadd function
109
110 // scalar C implementation
111 void vvadd( int n, float a[], float b[], float c[] )
112 {
113 int i;
114 for ( i = 0; i < n; i++ )
115 c[i] = a[i] + b[i];
116 }
117
118 // assembly implementations can be found in *_asm.S
119
120 //--------------------------------------------------------------------------
121 // Main
122
123 int main( int argc, char* argv[] )
124 {
125 float results_data[DATA_SIZE];
126 long long start_cycles = 0;
127 long long stop_cycles = 0;
128 long long num_cycles;
129 long long start_retired = 0;
130 long long stop_retired = 0;
131 long long num_retired;
132
133 // Output the input array
134
135 #if HOST_DEBUG
136 printArray( "input1", DATA_SIZE, input1_data );
137 printArray( "input2", DATA_SIZE, input2_data );
138 printArray( "verify", DATA_SIZE, verify_data );
139 #endif
140
141 // --------------------------------------------------
142 // If needed we preallocate everything in the caches
143
144 #if PREALLOCATE
145
146 #ifdef SCALAR_C
147 vvadd( DATA_SIZE, input1_data, input2_data, results_data );
148 #else
149 #ifdef SCALAR_ASM
150 scalar_vvadd_asm( DATA_SIZE, input1_data, input2_data, results_data );
151 #else
152 #ifdef VT_ASM
153 vt_vvadd_asm( DATA_SIZE, input1_data, input2_data, results_data );
154 #endif
155 #endif
156 #endif
157
158 #endif
159
160 // --------------------------------------------------
161 // Do the vvadd
162 start_cycles = getCycles();
163 start_retired = getInstRetired();
164
165 #ifdef SCALAR_C
166 vvadd( DATA_SIZE, input1_data, input2_data, results_data );
167 #else
168 #ifdef SCALAR_ASM
169 #if HOST_DEBUG==0
170 scalar_vvadd_asm( DATA_SIZE, input1_data, input2_data, results_data );
171 #endif
172 #else
173 #ifdef VT_ASM
174 #if HOST_DEBUG==0
175 vt_vvadd_asm( DATA_SIZE, input1_data, input2_data, results_data );
176 #endif
177 #endif
178 #endif
179 #endif
180
181 stop_cycles = getCycles();
182 stop_retired = getInstRetired();
183 num_cycles = stop_cycles - start_cycles;
184 num_retired = stop_retired - start_retired;
185
186 // printstr("stop_cycles: "); printhex(stop_cycles); printstr("\n");
187 // printstr("star_cycles: "); printhex(start_cycles); printstr("\n");
188
189 // --------------------------------------------------
190 // Print out the results
191
192 #if HOST_DEBUG
193 printArray( "results", DATA_SIZE, results_data );
194 #endif
195
196 // --------------------------------------------------
197 // Check the results
198 int correct = verify( DATA_SIZE, results_data, verify_data );
199 finishTest(correct, num_cycles, num_retired);
200 }