Merge pull request #21 from sifive/add_freedom_sim_targets
[riscv-tests.git] / isa / macros / scalar / test_macros.h
1 // See LICENSE for license details.
2
3 #ifndef __TEST_MACROS_SCALAR_H
4 #define __TEST_MACROS_SCALAR_H
5
6
7 #-----------------------------------------------------------------------
8 # Helper macros
9 #-----------------------------------------------------------------------
10
11 #define TEST_CASE( testnum, testreg, correctval, code... ) \
12 test_ ## testnum: \
13 code; \
14 li x29, correctval; \
15 li TESTNUM, testnum; \
16 bne testreg, x29, fail;
17
18 # We use a macro hack to simpify code generation for various numbers
19 # of bubble cycles.
20
21 #define TEST_INSERT_NOPS_0
22 #define TEST_INSERT_NOPS_1 nop; TEST_INSERT_NOPS_0
23 #define TEST_INSERT_NOPS_2 nop; TEST_INSERT_NOPS_1
24 #define TEST_INSERT_NOPS_3 nop; TEST_INSERT_NOPS_2
25 #define TEST_INSERT_NOPS_4 nop; TEST_INSERT_NOPS_3
26 #define TEST_INSERT_NOPS_5 nop; TEST_INSERT_NOPS_4
27 #define TEST_INSERT_NOPS_6 nop; TEST_INSERT_NOPS_5
28 #define TEST_INSERT_NOPS_7 nop; TEST_INSERT_NOPS_6
29 #define TEST_INSERT_NOPS_8 nop; TEST_INSERT_NOPS_7
30 #define TEST_INSERT_NOPS_9 nop; TEST_INSERT_NOPS_8
31 #define TEST_INSERT_NOPS_10 nop; TEST_INSERT_NOPS_9
32
33
34 #-----------------------------------------------------------------------
35 # RV64UI MACROS
36 #-----------------------------------------------------------------------
37
38 #-----------------------------------------------------------------------
39 # Tests for instructions with immediate operand
40 #-----------------------------------------------------------------------
41
42 #define SEXT_IMM(x) ((x) | (-(((x) >> 11) & 1) << 11))
43
44 #define TEST_IMM_OP( testnum, inst, result, val1, imm ) \
45 TEST_CASE( testnum, x3, result, \
46 li x1, val1; \
47 inst x3, x1, SEXT_IMM(imm); \
48 )
49
50 #define TEST_IMM_SRC1_EQ_DEST( testnum, inst, result, val1, imm ) \
51 TEST_CASE( testnum, x1, result, \
52 li x1, val1; \
53 inst x1, x1, SEXT_IMM(imm); \
54 )
55
56 #define TEST_IMM_DEST_BYPASS( testnum, nop_cycles, inst, result, val1, imm ) \
57 TEST_CASE( testnum, x6, result, \
58 li x4, 0; \
59 1: li x1, val1; \
60 inst x3, x1, SEXT_IMM(imm); \
61 TEST_INSERT_NOPS_ ## nop_cycles \
62 addi x6, x3, 0; \
63 addi x4, x4, 1; \
64 li x5, 2; \
65 bne x4, x5, 1b \
66 )
67
68 #define TEST_IMM_SRC1_BYPASS( testnum, nop_cycles, inst, result, val1, imm ) \
69 TEST_CASE( testnum, x3, result, \
70 li x4, 0; \
71 1: li x1, val1; \
72 TEST_INSERT_NOPS_ ## nop_cycles \
73 inst x3, x1, SEXT_IMM(imm); \
74 addi x4, x4, 1; \
75 li x5, 2; \
76 bne x4, x5, 1b \
77 )
78
79 #define TEST_IMM_ZEROSRC1( testnum, inst, result, imm ) \
80 TEST_CASE( testnum, x1, result, \
81 inst x1, x0, SEXT_IMM(imm); \
82 )
83
84 #define TEST_IMM_ZERODEST( testnum, inst, val1, imm ) \
85 TEST_CASE( testnum, x0, 0, \
86 li x1, val1; \
87 inst x0, x1, SEXT_IMM(imm); \
88 )
89
90 #-----------------------------------------------------------------------
91 # Tests for vector config instructions
92 #-----------------------------------------------------------------------
93
94 #define TEST_VSETCFGIVL( testnum, nxpr, nfpr, bank, vl, result ) \
95 TEST_CASE( testnum, x1, result, \
96 li x1, (bank << 12); \
97 vsetcfg x1,nxpr,nfpr; \
98 li x1, vl; \
99 vsetvl x1,x1; \
100 )
101
102 #define TEST_VVCFG( testnum, nxpr, nfpr, bank, vl, result ) \
103 TEST_CASE( testnum, x1, result, \
104 li x1, (bank << 12) | (nfpr << 6) | nxpr; \
105 vsetcfg x1; \
106 li x1, vl; \
107 vsetvl x1,x1; \
108 )
109
110 #define TEST_VSETVL( testnum, nxpr, nfpr, bank, vl, result ) \
111 TEST_CASE( testnum, x1, result, \
112 li x1, (bank << 12); \
113 vsetcfg x1,nxpr,nfpr; \
114 li x1, vl; \
115 vsetvl x1, x1; \
116 )
117
118 #-----------------------------------------------------------------------
119 # Tests for an instruction with register operands
120 #-----------------------------------------------------------------------
121
122 #define TEST_R_OP( testnum, inst, result, val1 ) \
123 TEST_CASE( testnum, x3, result, \
124 li x1, val1; \
125 inst x3, x1; \
126 )
127
128 #define TEST_R_SRC1_EQ_DEST( testnum, inst, result, val1 ) \
129 TEST_CASE( testnum, x1, result, \
130 li x1, val1; \
131 inst x1, x1; \
132 )
133
134 #define TEST_R_DEST_BYPASS( testnum, nop_cycles, inst, result, val1 ) \
135 TEST_CASE( testnum, x6, result, \
136 li x4, 0; \
137 1: li x1, val1; \
138 inst x3, x1; \
139 TEST_INSERT_NOPS_ ## nop_cycles \
140 addi x6, x3, 0; \
141 addi x4, x4, 1; \
142 li x5, 2; \
143 bne x4, x5, 1b \
144 )
145
146 #-----------------------------------------------------------------------
147 # Tests for an instruction with register-register operands
148 #-----------------------------------------------------------------------
149
150 #define TEST_RR_OP( testnum, inst, result, val1, val2 ) \
151 TEST_CASE( testnum, x3, result, \
152 li x1, val1; \
153 li x2, val2; \
154 inst x3, x1, x2; \
155 )
156
157 #define TEST_RR_SRC1_EQ_DEST( testnum, inst, result, val1, val2 ) \
158 TEST_CASE( testnum, x1, result, \
159 li x1, val1; \
160 li x2, val2; \
161 inst x1, x1, x2; \
162 )
163
164 #define TEST_RR_SRC2_EQ_DEST( testnum, inst, result, val1, val2 ) \
165 TEST_CASE( testnum, x2, result, \
166 li x1, val1; \
167 li x2, val2; \
168 inst x2, x1, x2; \
169 )
170
171 #define TEST_RR_SRC12_EQ_DEST( testnum, inst, result, val1 ) \
172 TEST_CASE( testnum, x1, result, \
173 li x1, val1; \
174 inst x1, x1, x1; \
175 )
176
177 #define TEST_RR_DEST_BYPASS( testnum, nop_cycles, inst, result, val1, val2 ) \
178 TEST_CASE( testnum, x6, result, \
179 li x4, 0; \
180 1: li x1, val1; \
181 li x2, val2; \
182 inst x3, x1, x2; \
183 TEST_INSERT_NOPS_ ## nop_cycles \
184 addi x6, x3, 0; \
185 addi x4, x4, 1; \
186 li x5, 2; \
187 bne x4, x5, 1b \
188 )
189
190 #define TEST_RR_SRC12_BYPASS( testnum, src1_nops, src2_nops, inst, result, val1, val2 ) \
191 TEST_CASE( testnum, x3, result, \
192 li x4, 0; \
193 1: li x1, val1; \
194 TEST_INSERT_NOPS_ ## src1_nops \
195 li x2, val2; \
196 TEST_INSERT_NOPS_ ## src2_nops \
197 inst x3, x1, x2; \
198 addi x4, x4, 1; \
199 li x5, 2; \
200 bne x4, x5, 1b \
201 )
202
203 #define TEST_RR_SRC21_BYPASS( testnum, src1_nops, src2_nops, inst, result, val1, val2 ) \
204 TEST_CASE( testnum, x3, result, \
205 li x4, 0; \
206 1: li x2, val2; \
207 TEST_INSERT_NOPS_ ## src1_nops \
208 li x1, val1; \
209 TEST_INSERT_NOPS_ ## src2_nops \
210 inst x3, x1, x2; \
211 addi x4, x4, 1; \
212 li x5, 2; \
213 bne x4, x5, 1b \
214 )
215
216 #define TEST_RR_ZEROSRC1( testnum, inst, result, val ) \
217 TEST_CASE( testnum, x2, result, \
218 li x1, val; \
219 inst x2, x0, x1; \
220 )
221
222 #define TEST_RR_ZEROSRC2( testnum, inst, result, val ) \
223 TEST_CASE( testnum, x2, result, \
224 li x1, val; \
225 inst x2, x1, x0; \
226 )
227
228 #define TEST_RR_ZEROSRC12( testnum, inst, result ) \
229 TEST_CASE( testnum, x1, result, \
230 inst x1, x0, x0; \
231 )
232
233 #define TEST_RR_ZERODEST( testnum, inst, val1, val2 ) \
234 TEST_CASE( testnum, x0, 0, \
235 li x1, val1; \
236 li x2, val2; \
237 inst x0, x1, x2; \
238 )
239
240 #-----------------------------------------------------------------------
241 # Test memory instructions
242 #-----------------------------------------------------------------------
243
244 #define TEST_LD_OP( testnum, inst, result, offset, base ) \
245 TEST_CASE( testnum, x3, result, \
246 la x1, base; \
247 inst x3, offset(x1); \
248 )
249
250 #define TEST_ST_OP( testnum, load_inst, store_inst, result, offset, base ) \
251 TEST_CASE( testnum, x3, result, \
252 la x1, base; \
253 li x2, result; \
254 store_inst x2, offset(x1); \
255 load_inst x3, offset(x1); \
256 )
257
258 #define TEST_LD_DEST_BYPASS( testnum, nop_cycles, inst, result, offset, base ) \
259 test_ ## testnum: \
260 li TESTNUM, testnum; \
261 li x4, 0; \
262 1: la x1, base; \
263 inst x3, offset(x1); \
264 TEST_INSERT_NOPS_ ## nop_cycles \
265 addi x6, x3, 0; \
266 li x29, result; \
267 bne x6, x29, fail; \
268 addi x4, x4, 1; \
269 li x5, 2; \
270 bne x4, x5, 1b; \
271
272 #define TEST_LD_SRC1_BYPASS( testnum, nop_cycles, inst, result, offset, base ) \
273 test_ ## testnum: \
274 li TESTNUM, testnum; \
275 li x4, 0; \
276 1: la x1, base; \
277 TEST_INSERT_NOPS_ ## nop_cycles \
278 inst x3, offset(x1); \
279 li x29, result; \
280 bne x3, x29, fail; \
281 addi x4, x4, 1; \
282 li x5, 2; \
283 bne x4, x5, 1b \
284
285 #define TEST_ST_SRC12_BYPASS( testnum, src1_nops, src2_nops, load_inst, store_inst, result, offset, base ) \
286 test_ ## testnum: \
287 li TESTNUM, testnum; \
288 li x4, 0; \
289 1: li x1, result; \
290 TEST_INSERT_NOPS_ ## src1_nops \
291 la x2, base; \
292 TEST_INSERT_NOPS_ ## src2_nops \
293 store_inst x1, offset(x2); \
294 load_inst x3, offset(x2); \
295 li x29, result; \
296 bne x3, x29, fail; \
297 addi x4, x4, 1; \
298 li x5, 2; \
299 bne x4, x5, 1b \
300
301 #define TEST_ST_SRC21_BYPASS( testnum, src1_nops, src2_nops, load_inst, store_inst, result, offset, base ) \
302 test_ ## testnum: \
303 li TESTNUM, testnum; \
304 li x4, 0; \
305 1: la x2, base; \
306 TEST_INSERT_NOPS_ ## src1_nops \
307 li x1, result; \
308 TEST_INSERT_NOPS_ ## src2_nops \
309 store_inst x1, offset(x2); \
310 load_inst x3, offset(x2); \
311 li x29, result; \
312 bne x3, x29, fail; \
313 addi x4, x4, 1; \
314 li x5, 2; \
315 bne x4, x5, 1b \
316
317 #define TEST_BR2_OP_TAKEN( testnum, inst, val1, val2 ) \
318 test_ ## testnum: \
319 li TESTNUM, testnum; \
320 li x1, val1; \
321 li x2, val2; \
322 inst x1, x2, 2f; \
323 bne x0, TESTNUM, fail; \
324 1: bne x0, TESTNUM, 3f; \
325 2: inst x1, x2, 1b; \
326 bne x0, TESTNUM, fail; \
327 3:
328
329 #define TEST_BR2_OP_NOTTAKEN( testnum, inst, val1, val2 ) \
330 test_ ## testnum: \
331 li TESTNUM, testnum; \
332 li x1, val1; \
333 li x2, val2; \
334 inst x1, x2, 1f; \
335 bne x0, TESTNUM, 2f; \
336 1: bne x0, TESTNUM, fail; \
337 2: inst x1, x2, 1b; \
338 3:
339
340 #define TEST_BR2_SRC12_BYPASS( testnum, src1_nops, src2_nops, inst, val1, val2 ) \
341 test_ ## testnum: \
342 li TESTNUM, testnum; \
343 li x4, 0; \
344 1: li x1, val1; \
345 TEST_INSERT_NOPS_ ## src1_nops \
346 li x2, val2; \
347 TEST_INSERT_NOPS_ ## src2_nops \
348 inst x1, x2, fail; \
349 addi x4, x4, 1; \
350 li x5, 2; \
351 bne x4, x5, 1b \
352
353 #define TEST_BR2_SRC21_BYPASS( testnum, src1_nops, src2_nops, inst, val1, val2 ) \
354 test_ ## testnum: \
355 li TESTNUM, testnum; \
356 li x4, 0; \
357 1: li x2, val2; \
358 TEST_INSERT_NOPS_ ## src1_nops \
359 li x1, val1; \
360 TEST_INSERT_NOPS_ ## src2_nops \
361 inst x1, x2, fail; \
362 addi x4, x4, 1; \
363 li x5, 2; \
364 bne x4, x5, 1b \
365
366 #-----------------------------------------------------------------------
367 # Test jump instructions
368 #-----------------------------------------------------------------------
369
370 #define TEST_JR_SRC1_BYPASS( testnum, nop_cycles, inst ) \
371 test_ ## testnum: \
372 li TESTNUM, testnum; \
373 li x4, 0; \
374 1: la x6, 2f; \
375 TEST_INSERT_NOPS_ ## nop_cycles \
376 inst x6; \
377 bne x0, TESTNUM, fail; \
378 2: addi x4, x4, 1; \
379 li x5, 2; \
380 bne x4, x5, 1b \
381
382 #define TEST_JALR_SRC1_BYPASS( testnum, nop_cycles, inst ) \
383 test_ ## testnum: \
384 li TESTNUM, testnum; \
385 li x4, 0; \
386 1: la x6, 2f; \
387 TEST_INSERT_NOPS_ ## nop_cycles \
388 inst x19, x6, 0; \
389 bne x0, TESTNUM, fail; \
390 2: addi x4, x4, 1; \
391 li x5, 2; \
392 bne x4, x5, 1b \
393
394
395 #-----------------------------------------------------------------------
396 # RV64UF MACROS
397 #-----------------------------------------------------------------------
398
399 #-----------------------------------------------------------------------
400 # Tests floating-point instructions
401 #-----------------------------------------------------------------------
402
403 #define TEST_FP_OP_S_INTERNAL( testnum, flags, result, val1, val2, val3, code... ) \
404 test_ ## testnum: \
405 li TESTNUM, testnum; \
406 la a0, test_ ## testnum ## _data ;\
407 flw f0, 0(a0); \
408 flw f1, 4(a0); \
409 flw f2, 8(a0); \
410 lw a3, 12(a0); \
411 code; \
412 fsflags a1, x0; \
413 li a2, flags; \
414 bne a0, a3, fail; \
415 bne a1, a2, fail; \
416 j 2f; \
417 .align 2; \
418 .data; \
419 test_ ## testnum ## _data: \
420 .float val1; \
421 .float val2; \
422 .float val3; \
423 .result; \
424 .text; \
425 2:
426
427 #define TEST_FP_OP_D_INTERNAL( testnum, flags, result, val1, val2, val3, code... ) \
428 test_ ## testnum: \
429 li TESTNUM, testnum; \
430 la a0, test_ ## testnum ## _data ;\
431 fld f0, 0(a0); \
432 fld f1, 8(a0); \
433 fld f2, 16(a0); \
434 ld a3, 24(a0); \
435 code; \
436 fsflags a1, x0; \
437 li a2, flags; \
438 bne a0, a3, fail; \
439 bne a1, a2, fail; \
440 j 2f; \
441 .data; \
442 .align 3; \
443 test_ ## testnum ## _data: \
444 .double val1; \
445 .double val2; \
446 .double val3; \
447 .result; \
448 .text; \
449 2:
450
451 #define TEST_FCVT_S_D( testnum, result, val1 ) \
452 TEST_FP_OP_D_INTERNAL( testnum, 0, double result, val1, 0.0, 0.0, \
453 fcvt.s.d f3, f0; fcvt.d.s f3, f3; fmv.x.d a0, f3)
454
455 #define TEST_FCVT_D_S( testnum, result, val1 ) \
456 TEST_FP_OP_S_INTERNAL( testnum, 0, float result, val1, 0.0, 0.0, \
457 fcvt.d.s f3, f0; fcvt.s.d f3, f3; fmv.x.s a0, f3)
458
459 #define TEST_FP_OP1_S( testnum, inst, flags, result, val1 ) \
460 TEST_FP_OP_S_INTERNAL( testnum, flags, float result, val1, 0.0, 0.0, \
461 inst f3, f0; fmv.x.s a0, f3)
462
463 #define TEST_FP_OP1_D( testnum, inst, flags, result, val1 ) \
464 TEST_FP_OP_D_INTERNAL( testnum, flags, double result, val1, 0.0, 0.0, \
465 inst f3, f0; fmv.x.d a0, f3)
466
467 #define TEST_FP_OP1_S_DWORD_RESULT( testnum, inst, flags, result, val1 ) \
468 TEST_FP_OP_S_INTERNAL( testnum, flags, dword result, val1, 0.0, 0.0, \
469 inst f3, f0; fmv.x.s a0, f3)
470
471 #define TEST_FP_OP1_D_DWORD_RESULT( testnum, inst, flags, result, val1 ) \
472 TEST_FP_OP_D_INTERNAL( testnum, flags, dword result, val1, 0.0, 0.0, \
473 inst f3, f0; fmv.x.d a0, f3)
474
475 #define TEST_FP_OP2_S( testnum, inst, flags, result, val1, val2 ) \
476 TEST_FP_OP_S_INTERNAL( testnum, flags, float result, val1, val2, 0.0, \
477 inst f3, f0, f1; fmv.x.s a0, f3)
478
479 #define TEST_FP_OP2_D( testnum, inst, flags, result, val1, val2 ) \
480 TEST_FP_OP_D_INTERNAL( testnum, flags, double result, val1, val2, 0.0, \
481 inst f3, f0, f1; fmv.x.d a0, f3)
482
483 #define TEST_FP_OP3_S( testnum, inst, flags, result, val1, val2, val3 ) \
484 TEST_FP_OP_S_INTERNAL( testnum, flags, float result, val1, val2, val3, \
485 inst f3, f0, f1, f2; fmv.x.s a0, f3)
486
487 #define TEST_FP_OP3_D( testnum, inst, flags, result, val1, val2, val3 ) \
488 TEST_FP_OP_D_INTERNAL( testnum, flags, double result, val1, val2, val3, \
489 inst f3, f0, f1, f2; fmv.x.d a0, f3)
490
491 #define TEST_FP_INT_OP_S( testnum, inst, flags, result, val1, rm ) \
492 TEST_FP_OP_S_INTERNAL( testnum, flags, word result, val1, 0.0, 0.0, \
493 inst a0, f0, rm)
494
495 #define TEST_FP_INT_OP_D( testnum, inst, flags, result, val1, rm ) \
496 TEST_FP_OP_D_INTERNAL( testnum, flags, dword result, val1, 0.0, 0.0, \
497 inst a0, f0, rm)
498
499 #define TEST_FP_CMP_OP_S( testnum, inst, result, val1, val2 ) \
500 TEST_FP_OP_S_INTERNAL( testnum, 0, word result, val1, val2, 0.0, \
501 inst a0, f0, f1)
502
503 #define TEST_FP_CMP_OP_D( testnum, inst, result, val1, val2 ) \
504 TEST_FP_OP_D_INTERNAL( testnum, 0, dword result, val1, val2, 0.0, \
505 inst a0, f0, f1)
506
507 #define TEST_FCLASS_S(testnum, correct, input) \
508 TEST_CASE(testnum, a0, correct, li a0, input; fmv.s.x fa0, a0; \
509 fclass.s a0, fa0)
510
511 #define TEST_FCLASS_D(testnum, correct, input) \
512 TEST_CASE(testnum, a0, correct, li a0, input; fmv.d.x fa0, a0; \
513 fclass.d a0, fa0)
514
515 #define TEST_INT_FP_OP_S( testnum, inst, result, val1 ) \
516 test_ ## testnum: \
517 li TESTNUM, testnum; \
518 la a0, test_ ## testnum ## _data ;\
519 lw a3, 0(a0); \
520 li a0, val1; \
521 inst f0, a0; \
522 fsflags x0; \
523 fmv.x.s a0, f0; \
524 bne a0, a3, fail; \
525 j 1f; \
526 .align 2; \
527 test_ ## testnum ## _data: \
528 .float result; \
529 1:
530
531 #define TEST_INT_FP_OP_D( testnum, inst, result, val1 ) \
532 test_ ## testnum: \
533 li TESTNUM, testnum; \
534 la a0, test_ ## testnum ## _data ;\
535 ld a3, 0(a0); \
536 li a0, val1; \
537 inst f0, a0; \
538 fsflags x0; \
539 fmv.x.d a0, f0; \
540 bne a0, a3, fail; \
541 j 1f; \
542 .align 3; \
543 test_ ## testnum ## _data: \
544 .double result; \
545 1:
546
547 #-----------------------------------------------------------------------
548 # Pass and fail code (assumes test num is in TESTNUM)
549 #-----------------------------------------------------------------------
550
551 #define TEST_PASSFAIL \
552 bne x0, TESTNUM, pass; \
553 fail: \
554 RVTEST_FAIL; \
555 pass: \
556 RVTEST_PASS \
557
558
559 #-----------------------------------------------------------------------
560 # Test data section
561 #-----------------------------------------------------------------------
562
563 #define TEST_DATA
564
565 #endif