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