Test FMIN/FMAX NaN behavior
[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 qNaNf 0f:7fc00000
378 #define sNaNf 0f:7f800001
379 #define qNaN 0d:7ff8000000000000
380 #define sNaN 0d:7ff0000000000001
381
382 #define TEST_FP_OP_S_INTERNAL( testnum, flags, result, val1, val2, val3, code... ) \
383 test_ ## testnum: \
384 li TESTNUM, testnum; \
385 la a0, test_ ## testnum ## _data ;\
386 flw f0, 0(a0); \
387 flw f1, 4(a0); \
388 flw f2, 8(a0); \
389 lw a3, 12(a0); \
390 code; \
391 fsflags a1, x0; \
392 li a2, flags; \
393 bne a0, a3, fail; \
394 bne a1, a2, fail; \
395 .pushsection .data; \
396 .align 2; \
397 test_ ## testnum ## _data: \
398 .float val1; \
399 .float val2; \
400 .float val3; \
401 .result; \
402 .popsection
403
404 #define TEST_FP_OP_D_INTERNAL( testnum, flags, result, val1, val2, val3, code... ) \
405 test_ ## testnum: \
406 li TESTNUM, testnum; \
407 la a0, test_ ## testnum ## _data ;\
408 fld f0, 0(a0); \
409 fld f1, 8(a0); \
410 fld f2, 16(a0); \
411 ld a3, 24(a0); \
412 code; \
413 fsflags a1, x0; \
414 li a2, flags; \
415 bne a0, a3, fail; \
416 bne a1, a2, fail; \
417 .pushsection .data; \
418 .align 3; \
419 test_ ## testnum ## _data: \
420 .double val1; \
421 .double val2; \
422 .double val3; \
423 .result; \
424 .popsection
425
426 #define TEST_FCVT_S_D( testnum, result, val1 ) \
427 TEST_FP_OP_D_INTERNAL( testnum, 0, double result, val1, 0.0, 0.0, \
428 fcvt.s.d f3, f0; fcvt.d.s f3, f3; fmv.x.d a0, f3)
429
430 #define TEST_FCVT_D_S( testnum, result, val1 ) \
431 TEST_FP_OP_S_INTERNAL( testnum, 0, float result, val1, 0.0, 0.0, \
432 fcvt.d.s f3, f0; fcvt.s.d f3, f3; fmv.x.s a0, f3)
433
434 #define TEST_FP_OP1_S( testnum, inst, flags, result, val1 ) \
435 TEST_FP_OP_S_INTERNAL( testnum, flags, float result, val1, 0.0, 0.0, \
436 inst f3, f0; fmv.x.s a0, f3)
437
438 #define TEST_FP_OP1_D( testnum, inst, flags, result, val1 ) \
439 TEST_FP_OP_D_INTERNAL( testnum, flags, double result, val1, 0.0, 0.0, \
440 inst f3, f0; fmv.x.d a0, f3)
441
442 #define TEST_FP_OP1_S_DWORD_RESULT( testnum, inst, flags, result, val1 ) \
443 TEST_FP_OP_S_INTERNAL( testnum, flags, dword result, val1, 0.0, 0.0, \
444 inst f3, f0; fmv.x.s a0, f3)
445
446 #define TEST_FP_OP1_D_DWORD_RESULT( testnum, inst, flags, result, val1 ) \
447 TEST_FP_OP_D_INTERNAL( testnum, flags, dword result, val1, 0.0, 0.0, \
448 inst f3, f0; fmv.x.d a0, f3)
449
450 #define TEST_FP_OP2_S( testnum, inst, flags, result, val1, val2 ) \
451 TEST_FP_OP_S_INTERNAL( testnum, flags, float result, val1, val2, 0.0, \
452 inst f3, f0, f1; fmv.x.s a0, f3)
453
454 #define TEST_FP_OP2_D( testnum, inst, flags, result, val1, val2 ) \
455 TEST_FP_OP_D_INTERNAL( testnum, flags, double result, val1, val2, 0.0, \
456 inst f3, f0, f1; fmv.x.d a0, f3)
457
458 #define TEST_FP_OP3_S( testnum, inst, flags, result, val1, val2, val3 ) \
459 TEST_FP_OP_S_INTERNAL( testnum, flags, float result, val1, val2, val3, \
460 inst f3, f0, f1, f2; fmv.x.s a0, f3)
461
462 #define TEST_FP_OP3_D( testnum, inst, flags, result, val1, val2, val3 ) \
463 TEST_FP_OP_D_INTERNAL( testnum, flags, double result, val1, val2, val3, \
464 inst f3, f0, f1, f2; fmv.x.d a0, f3)
465
466 #define TEST_FP_INT_OP_S( testnum, inst, flags, result, val1, rm ) \
467 TEST_FP_OP_S_INTERNAL( testnum, flags, word result, val1, 0.0, 0.0, \
468 inst a0, f0, rm)
469
470 #define TEST_FP_INT_OP_D( testnum, inst, flags, result, val1, rm ) \
471 TEST_FP_OP_D_INTERNAL( testnum, flags, dword result, val1, 0.0, 0.0, \
472 inst a0, f0, rm)
473
474 #define TEST_FP_CMP_OP_S( testnum, inst, flags, result, val1, val2 ) \
475 TEST_FP_OP_S_INTERNAL( testnum, flags, word result, val1, val2, 0.0, \
476 inst a0, f0, f1)
477
478 #define TEST_FP_CMP_OP_D( testnum, inst, flags, result, val1, val2 ) \
479 TEST_FP_OP_D_INTERNAL( testnum, flags, dword result, val1, val2, 0.0, \
480 inst a0, f0, f1)
481
482 #define TEST_FCLASS_S(testnum, correct, input) \
483 TEST_CASE(testnum, a0, correct, li a0, input; fmv.s.x fa0, a0; \
484 fclass.s a0, fa0)
485
486 #define TEST_FCLASS_D(testnum, correct, input) \
487 TEST_CASE(testnum, a0, correct, li a0, input; fmv.d.x fa0, a0; \
488 fclass.d a0, fa0)
489
490 #define TEST_INT_FP_OP_S( testnum, inst, result, val1 ) \
491 test_ ## testnum: \
492 li TESTNUM, testnum; \
493 la a0, test_ ## testnum ## _data ;\
494 lw a3, 0(a0); \
495 li a0, val1; \
496 inst f0, a0; \
497 fsflags x0; \
498 fmv.x.s a0, f0; \
499 bne a0, a3, fail; \
500 .pushsection .data; \
501 .align 2; \
502 test_ ## testnum ## _data: \
503 .float result; \
504 .popsection
505
506 #define TEST_INT_FP_OP_D( testnum, inst, result, val1 ) \
507 test_ ## testnum: \
508 li TESTNUM, testnum; \
509 la a0, test_ ## testnum ## _data ;\
510 ld a3, 0(a0); \
511 li a0, val1; \
512 inst f0, a0; \
513 fsflags x0; \
514 fmv.x.d a0, f0; \
515 bne a0, a3, fail; \
516 .pushsection .data; \
517 .align 3; \
518 test_ ## testnum ## _data: \
519 .double result; \
520 .popsection
521
522 #-----------------------------------------------------------------------
523 # Pass and fail code (assumes test num is in TESTNUM)
524 #-----------------------------------------------------------------------
525
526 #define TEST_PASSFAIL \
527 bne x0, TESTNUM, pass; \
528 fail: \
529 RVTEST_FAIL; \
530 pass: \
531 RVTEST_PASS \
532
533
534 #-----------------------------------------------------------------------
535 # Test data section
536 #-----------------------------------------------------------------------
537
538 #define TEST_DATA
539
540 #endif