2 * Copyright(c) 2020-2023 Qualcomm Innovation Center, Inc. All Rights Reserved.
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, see <http://www.gnu.org/licenses/>.
19 * This test checks various FP operations performed on Hexagon
30 static void check_fpstatus_bit(uint32_t usr
, uint32_t expect
, uint32_t flag
,
33 uint32_t bit
= 1 << flag
;
34 if ((usr
& bit
) != (expect
& bit
)) {
35 printf("ERROR %s: usr = %d, expect = %d\n", name
,
36 (usr
>> flag
) & 1, (expect
>> flag
) & 1);
41 static void check_fpstatus(uint32_t usr
, uint32_t expect
)
43 check_fpstatus_bit(usr
, expect
, USR_FPINVF_BIT
, "Invalid");
44 check_fpstatus_bit(usr
, expect
, USR_FPDBZF_BIT
, "Div by zero");
45 check_fpstatus_bit(usr
, expect
, USR_FPOVFF_BIT
, "Overflow");
46 check_fpstatus_bit(usr
, expect
, USR_FPUNFF_BIT
, "Underflow");
47 check_fpstatus_bit(usr
, expect
, USR_FPINPF_BIT
, "Inexact");
50 static void check_compare_exception(void)
55 /* Check that FP compares are quiet (don't raise any exceptions) */
57 "p0 = sfcmp.eq(%2, %3)\n\t"
60 : "=r"(cmp
), "=r"(usr
) : "r"(SF_QNaN
), "r"(SF_any
)
63 check_fpstatus(usr
, 0);
66 "p0 = sfcmp.gt(%2, %3)\n\t"
69 : "=r"(cmp
), "=r"(usr
) : "r"(SF_QNaN
), "r"(SF_any
)
72 check_fpstatus(usr
, 0);
75 "p0 = sfcmp.ge(%2, %3)\n\t"
78 : "=r"(cmp
), "=r"(usr
) : "r"(SF_QNaN
), "r"(SF_any
)
81 check_fpstatus(usr
, 0);
84 "p0 = dfcmp.eq(%2, %3)\n\t"
87 : "=r"(cmp
), "=r"(usr
) : "r"(DF_QNaN
), "r"(DF_any
)
90 check_fpstatus(usr
, 0);
93 "p0 = dfcmp.gt(%2, %3)\n\t"
96 : "=r"(cmp
), "=r"(usr
) : "r"(DF_QNaN
), "r"(DF_any
)
99 check_fpstatus(usr
, 0);
102 "p0 = dfcmp.ge(%2, %3)\n\t"
105 : "=r"(cmp
), "=r"(usr
) : "r"(DF_QNaN
), "r"(DF_any
)
106 : "r2", "p0", "usr");
108 check_fpstatus(usr
, 0);
111 static void check_sfminmax(void)
117 * Execute sfmin/sfmax instructions with one operand as NaN
119 * Result is the other operand
120 * Invalid bit in USR is not set
123 "%0 = sfmin(%2, %3)\n\t"
125 : "=r"(minmax
), "=r"(usr
) : "r"(SF_QNaN
), "r"(SF_any
)
127 check32(minmax
, SF_any
);
128 check_fpstatus(usr
, 0);
131 "%0 = sfmax(%2, %3)\n\t"
133 : "=r"(minmax
), "=r"(usr
) : "r"(SF_QNaN
), "r"(SF_any
)
135 check32(minmax
, SF_any
);
136 check_fpstatus(usr
, 0);
139 * Execute sfmin/sfmax instructions with both operands NaN
141 * Result is SF_HEX_NaN
142 * Invalid bit in USR is set
145 "%0 = sfmin(%2, %3)\n\t"
147 : "=r"(minmax
), "=r"(usr
) : "r"(SF_QNaN
), "r"(SF_QNaN
)
149 check32(minmax
, SF_HEX_NaN
);
150 check_fpstatus(usr
, 0);
153 "%0 = sfmax(%2, %3)\n\t"
155 : "=r"(minmax
), "=r"(usr
) : "r"(SF_QNaN
), "r"(SF_QNaN
)
157 check32(minmax
, SF_HEX_NaN
);
158 check_fpstatus(usr
, 0);
161 static void check_dfminmax(void)
167 * Execute dfmin/dfmax instructions with one operand as SNaN
169 * Result is the other operand
170 * Invalid bit in USR is set
173 "%0 = dfmin(%2, %3)\n\t"
175 : "=r"(minmax
), "=r"(usr
) : "r"(DF_SNaN
), "r"(DF_any
)
177 check64(minmax
, DF_any
);
178 check_fpstatus(usr
, USR_FPINVF
);
181 "%0 = dfmax(%2, %3)\n\t"
183 : "=r"(minmax
), "=r"(usr
) : "r"(DF_SNaN
), "r"(DF_any
)
185 check64(minmax
, DF_any
);
186 check_fpstatus(usr
, USR_FPINVF
);
189 * Execute dfmin/dfmax instructions with one operand as QNaN
191 * Result is the other operand
192 * No bit in USR is set
195 "%0 = dfmin(%2, %3)\n\t"
197 : "=r"(minmax
), "=r"(usr
) : "r"(DF_QNaN
), "r"(DF_any
)
199 check64(minmax
, DF_any
);
200 check_fpstatus(usr
, 0);
203 "%0 = dfmax(%2, %3)\n\t"
205 : "=r"(minmax
), "=r"(usr
) : "r"(DF_QNaN
), "r"(DF_any
)
207 check64(minmax
, DF_any
);
208 check_fpstatus(usr
, 0);
211 * Execute dfmin/dfmax instructions with both operands SNaN
213 * Result is DF_HEX_NaN
214 * Invalid bit in USR is set
217 "%0 = dfmin(%2, %3)\n\t"
219 : "=r"(minmax
), "=r"(usr
) : "r"(DF_SNaN
), "r"(DF_SNaN
)
221 check64(minmax
, DF_HEX_NaN
);
222 check_fpstatus(usr
, USR_FPINVF
);
225 "%0 = dfmax(%2, %3)\n\t"
227 : "=r"(minmax
), "=r"(usr
) : "r"(DF_SNaN
), "r"(DF_SNaN
)
229 check64(minmax
, DF_HEX_NaN
);
230 check_fpstatus(usr
, USR_FPINVF
);
233 * Execute dfmin/dfmax instructions with both operands QNaN
235 * Result is DF_HEX_NaN
236 * No bit in USR is set
239 "%0 = dfmin(%2, %3)\n\t"
241 : "=r"(minmax
), "=r"(usr
) : "r"(DF_QNaN
), "r"(DF_QNaN
)
243 check64(minmax
, DF_HEX_NaN
);
244 check_fpstatus(usr
, 0);
247 "%0 = dfmax(%2, %3)\n\t"
249 : "=r"(minmax
), "=r"(usr
) : "r"(DF_QNaN
), "r"(DF_QNaN
)
251 check64(minmax
, DF_HEX_NaN
);
252 check_fpstatus(usr
, 0);
255 static void check_sfrecipa(void)
262 * Check that sfrecipa doesn't set status bits when
263 * a NaN with bit 22 non-zero is passed
266 "%0,p0 = sfrecipa(%2, %3)\n\t"
268 : "=r"(result
), "=r"(usr
) : "r"(SF_QNaN
), "r"(SF_any
)
269 : "r2", "p0", "usr");
270 check32(result
, SF_HEX_NaN
);
271 check_fpstatus(usr
, 0);
274 "%0,p0 = sfrecipa(%2, %3)\n\t"
276 : "=r"(result
), "=r"(usr
) : "r"(SF_any
), "r"(SF_QNaN
)
277 : "r2", "p0", "usr");
278 check32(result
, SF_HEX_NaN
);
279 check_fpstatus(usr
, 0);
282 "%0,p0 = sfrecipa(%2, %2)\n\t"
284 : "=r"(result
), "=r"(usr
) : "r"(SF_QNaN
)
285 : "r2", "p0", "usr");
286 check32(result
, SF_HEX_NaN
);
287 check_fpstatus(usr
, 0);
290 * Check that sfrecipa doesn't set status bits when
291 * a NaN with bit 22 zero is passed
294 "%0,p0 = sfrecipa(%2, %3)\n\t"
296 : "=r"(result
), "=r"(usr
) : "r"(SF_QNaN_special
), "r"(SF_any
)
297 : "r2", "p0", "usr");
298 check32(result
, SF_HEX_NaN
);
299 check_fpstatus(usr
, USR_FPINVF
);
302 "%0,p0 = sfrecipa(%2, %3)\n\t"
304 : "=r"(result
), "=r"(usr
) : "r"(SF_any
), "r"(SF_QNaN_special
)
305 : "r2", "p0", "usr");
306 check32(result
, SF_HEX_NaN
);
307 check_fpstatus(usr
, USR_FPINVF
);
310 "%0,p0 = sfrecipa(%2, %2)\n\t"
312 : "=r"(result
), "=r"(usr
) : "r"(SF_QNaN_special
)
313 : "r2", "p0", "usr");
314 check32(result
, SF_HEX_NaN
);
315 check_fpstatus(usr
, USR_FPINVF
);
318 * Check that sfrecipa properly sets divid-by-zero
321 "%0,p0 = sfrecipa(%2, %3)\n\t"
323 : "=r"(result
), "=r"(usr
) : "r"(0x885dc960), "r"(0x80000000)
324 : "r2", "p0", "usr");
325 check32(result
, 0x3f800000);
326 check_fpstatus(usr
, USR_FPDBZF
);
329 "%0,p0 = sfrecipa(%2, %3)\n\t"
331 : "=r"(result
), "=r"(usr
) : "r"(0x7f800000), "r"(SF_zero
)
332 : "r2", "p0", "usr");
333 check32(result
, 0x3f800000);
334 check_fpstatus(usr
, 0);
337 * Check that sfrecipa properly handles denorm
340 "%0,p0 = sfrecipa(%2, %3)\n\t"
342 : "=r"(result
), "=r"(pred
) : "r"(SF_denorm
), "r"(SF_random
)
344 check32(result
, 0x6a920001);
348 static void check_canonical_NaN(void)
354 /* Check that each FP instruction properly returns SF_HEX_NaN/DF_HEX_NaN */
356 "%0 = sfadd(%2, %3)\n\t"
358 : "=r"(sf_result
), "=r"(usr
) : "r"(SF_QNaN
), "r"(SF_any
)
360 check32(sf_result
, SF_HEX_NaN
);
361 check_fpstatus(usr
, 0);
364 "%0 = sfsub(%2, %3)\n\t"
366 : "=r"(sf_result
), "=r"(usr
) : "r"(SF_QNaN
), "r"(SF_any
)
368 check32(sf_result
, SF_HEX_NaN
);
369 check_fpstatus(usr
, 0);
372 "%0 = sfmpy(%2, %3)\n\t"
374 : "=r"(sf_result
), "=r"(usr
) : "r"(SF_QNaN
), "r"(SF_any
)
376 check32(sf_result
, SF_HEX_NaN
);
377 check_fpstatus(usr
, 0);
381 "%0 += sfmpy(%2, %3)\n\t"
383 : "+r"(sf_result
), "=r"(usr
) : "r"(SF_QNaN
), "r"(SF_any
)
385 check32(sf_result
, SF_HEX_NaN
);
386 check_fpstatus(usr
, 0);
390 "p0 = !cmp.eq(r0, r0)\n\t"
391 "%0 += sfmpy(%2, %3, p0):scale\n\t"
393 : "+r"(sf_result
), "=r"(usr
) : "r"(SF_QNaN
), "r"(SF_any
)
394 : "r2", "usr", "p0");
395 check32(sf_result
, SF_HEX_NaN
);
396 check_fpstatus(usr
, 0);
400 "%0 -= sfmpy(%2, %3)\n\t"
402 : "+r"(sf_result
), "=r"(usr
) : "r"(SF_QNaN
), "r"(SF_any
)
404 check32(sf_result
, SF_HEX_NaN
);
405 check_fpstatus(usr
, 0);
409 "%0 += sfmpy(%2, %3):lib\n\t"
411 : "+r"(sf_result
), "=r"(usr
) : "r"(SF_QNaN
), "r"(SF_any
)
413 check32(sf_result
, SF_HEX_NaN
);
414 check_fpstatus(usr
, 0);
418 "%0 -= sfmpy(%2, %3):lib\n\t"
420 : "+r"(sf_result
), "=r"(usr
) : "r"(SF_QNaN
), "r"(SF_any
)
422 check32(sf_result
, SF_HEX_NaN
);
423 check_fpstatus(usr
, 0);
426 "%0 = convert_df2sf(%2)\n\t"
428 : "=r"(sf_result
), "=r"(usr
) : "r"(DF_QNaN
)
430 check32(sf_result
, SF_HEX_NaN
);
431 check_fpstatus(usr
, 0);
434 "%0 = dfadd(%2, %3)\n\t"
436 : "=r"(df_result
), "=r"(usr
) : "r"(DF_QNaN
), "r"(DF_any
)
438 check64(df_result
, DF_HEX_NaN
);
439 check_fpstatus(usr
, 0);
442 "%0 = dfsub(%2, %3)\n\t"
444 : "=r"(df_result
), "=r"(usr
) : "r"(DF_QNaN
), "r"(DF_any
)
446 check64(df_result
, DF_HEX_NaN
);
447 check_fpstatus(usr
, 0);
450 "%0 = convert_sf2df(%2)\n\t"
452 : "=r"(df_result
), "=r"(usr
) : "r"(SF_QNaN
)
454 check64(df_result
, DF_HEX_NaN
);
455 check_fpstatus(usr
, 0);
458 static void check_invsqrta(void)
463 asm volatile("%0,p0 = sfinvsqrta(%2)\n\t"
465 : "+r"(result
), "=r"(predval
)
468 check32(result
, 0xff800000);
469 check32(predval
, 0x0);
472 static void check_sffixupn(void)
476 /* Check that sffixupn properly deals with denorm */
477 asm volatile("%0 = sffixupn(%1, %2)\n\t"
479 : "r"(SF_random
), "r"(SF_denorm
));
480 check32(result
, 0x246001d6);
483 static void check_sffixupd(void)
487 /* Check that sffixupd properly deals with denorm */
488 asm volatile("%0 = sffixupd(%1, %2)\n\t"
490 : "r"(SF_denorm
), "r"(SF_random
));
491 check32(result
, 0x146001d6);
494 static void check_sffms(void)
498 /* Check that sffms properly deals with -0 */
499 result
= SF_zero_neg
;
500 asm ("%0 -= sfmpy(%1 , %2)\n\t"
502 : "r"(SF_zero
), "r"(SF_zero
)
504 check32(result
, SF_zero_neg
);
507 asm ("%0 -= sfmpy(%1 , %2)\n\t"
509 : "r"(SF_zero_neg
), "r"(SF_zero
)
511 check32(result
, SF_zero
);
514 asm ("%0 -= sfmpy(%1 , %2)\n\t"
516 : "r"(SF_zero
), "r"(SF_zero_neg
)
518 check32(result
, SF_zero
);
521 static void check_float2int_convs()
528 * Check that the various forms of float-to-unsigned
529 * check sign before rounding
532 "%0 = convert_sf2uw(%2)\n\t"
534 : "=r"(res32
), "=r"(usr
) : "r"(SF_small_neg
)
537 check_fpstatus(usr
, USR_FPINVF
);
540 "%0 = convert_sf2uw(%2):chop\n\t"
542 : "=r"(res32
), "=r"(usr
) : "r"(SF_small_neg
)
545 check_fpstatus(usr
, USR_FPINVF
);
548 "%0 = convert_sf2ud(%2)\n\t"
550 : "=r"(res64
), "=r"(usr
) : "r"(SF_small_neg
)
553 check_fpstatus(usr
, USR_FPINVF
);
556 "%0 = convert_sf2ud(%2):chop\n\t"
558 : "=r"(res64
), "=r"(usr
) : "r"(SF_small_neg
)
561 check_fpstatus(usr
, USR_FPINVF
);
564 "%0 = convert_df2uw(%2)\n\t"
566 : "=r"(res32
), "=r"(usr
) : "r"(DF_small_neg
)
569 check_fpstatus(usr
, USR_FPINVF
);
572 "%0 = convert_df2uw(%2):chop\n\t"
574 : "=r"(res32
), "=r"(usr
) : "r"(DF_small_neg
)
577 check_fpstatus(usr
, USR_FPINVF
);
580 "%0 = convert_df2ud(%2)\n\t"
582 : "=r"(res64
), "=r"(usr
) : "r"(DF_small_neg
)
585 check_fpstatus(usr
, USR_FPINVF
);
588 "%0 = convert_df2ud(%2):chop\n\t"
590 : "=r"(res64
), "=r"(usr
) : "r"(DF_small_neg
)
593 check_fpstatus(usr
, USR_FPINVF
);
596 * Check that the various forms of float-to-signed return -1 for NaN
599 "%0 = convert_sf2w(%2)\n\t"
601 : "=r"(res32
), "=r"(usr
) : "r"(SF_QNaN
)
604 check_fpstatus(usr
, USR_FPINVF
);
607 "%0 = convert_sf2w(%2):chop\n\t"
609 : "=r"(res32
), "=r"(usr
) : "r"(SF_QNaN
)
612 check_fpstatus(usr
, USR_FPINVF
);
615 "%0 = convert_sf2d(%2)\n\t"
617 : "=r"(res64
), "=r"(usr
) : "r"(SF_QNaN
)
620 check_fpstatus(usr
, USR_FPINVF
);
623 "%0 = convert_sf2d(%2):chop\n\t"
625 : "=r"(res64
), "=r"(usr
) : "r"(SF_QNaN
)
628 check_fpstatus(usr
, USR_FPINVF
);
631 "%0 = convert_df2w(%2)\n\t"
633 : "=r"(res32
), "=r"(usr
) : "r"(DF_QNaN
)
636 check_fpstatus(usr
, USR_FPINVF
);
639 "%0 = convert_df2w(%2):chop\n\t"
641 : "=r"(res32
), "=r"(usr
) : "r"(DF_QNaN
)
644 check_fpstatus(usr
, USR_FPINVF
);
647 "%0 = convert_df2d(%2)\n\t"
649 : "=r"(res64
), "=r"(usr
) : "r"(DF_QNaN
)
652 check_fpstatus(usr
, USR_FPINVF
);
655 "%0 = convert_df2d(%2):chop\n\t"
657 : "=r"(res64
), "=r"(usr
) : "r"(DF_QNaN
)
660 check_fpstatus(usr
, USR_FPINVF
);
663 static void check_float_consts(void)
668 asm("%0 = sfmake(#%1):neg\n\t" : "=r"(res32
) : "i"(0xf));
669 check32(res32
, 0xbc9e0000);
671 asm("%0 = sfmake(#%1):pos\n\t" : "=r"(res32
) : "i"(0xf));
672 check32(res32
, 0x3c9e0000);
674 asm("%0 = dfmake(#%1):neg\n\t" : "=r"(res64
) : "i"(0xf));
675 check64(res64
, 0xbf93c00000000000ULL
);
677 asm("%0 = dfmake(#%1):pos\n\t" : "=r"(res64
) : "i"(0xf));
678 check64(res64
, 0x3f93c00000000000ULL
);
681 static inline uint64_t dfmpyll(double x
, double y
)
684 asm("%0 = dfmpyll(%1, %2)" : "=r"(res64
) : "r"(x
), "r"(y
));
688 static inline uint64_t dfmpylh(double acc
, double x
, double y
)
690 uint64_t res64
= *(uint64_t *)&acc
;
691 asm("%0 += dfmpylh(%1, %2)" : "+r"(res64
) : "r"(x
), "r"(y
));
695 static void check_dfmpyxx(void)
699 res64
= dfmpyll(DBL_MIN
, DBL_MIN
);
700 check64(res64
, 0ULL);
701 res64
= dfmpyll(-1.0, DBL_MIN
);
702 check64(res64
, 0ULL);
703 res64
= dfmpyll(DBL_MAX
, DBL_MAX
);
704 check64(res64
, 0x1fffffffdULL
);
706 res64
= dfmpylh(DBL_MIN
, DBL_MIN
, DBL_MIN
);
707 check64(res64
, 0x10000000000000ULL
);
708 res64
= dfmpylh(-1.0, DBL_MAX
, DBL_MIN
);
709 check64(res64
, 0xc00fffffffe00000ULL
);
710 res64
= dfmpylh(DBL_MAX
, 0.0, -1.0);
711 check64(res64
, 0x7fefffffffffffffULL
);
716 check_compare_exception();
720 check_canonical_NaN();
725 check_float2int_convs();
726 check_float_consts();
729 puts(err
? "FAIL" : "PASS");