SNaN & QNaN on mips
在 2002-02-03 22:54:00 you wrote:
Quoted 11 lines · Zhang Fuxin
Quoted 8 lines
hi,
Gcc (2.96 20000731,H.J.LU's rh port for mips) think
0x7fc00000is QNaN and optimize 0.0/0.0 as0x7fc00000for single precision ops,while for my cpu (maybe most mips cpu) is a SNaN. R4k user's manual and "See Mips Run" both
say so.And experiments confirm this.Should we correct it?
Yes. Do you have a patch?
Not currently but I will have a try. glibc seems having the same problem.
Quoted 5 lines
Thanks.
H.J.
Regards
Zhang Fuxin
[email protected]
endless recursive loop in pow()
I found that pow() of glibc 2.2.4 causes stack overflow on some testcase with NaN operand. Try this simple (and dirty) test program (for little endian).
main()
{
double x, y, z;
*(int*)(&x) = 0x00000000;
*((int*)(&x) + 1) = 0xfff00001;
*(int*)(&y) = 0x80000000;
*((int*)(&y) + 1) = 0xcff00000;
z = pow(x, y);
printf("%x %x\n", *((int*)&z + 1), *(int*)&z);
return 0;
}This program fall into endless recursive call at line 128 in sysdeps/ieee754/dbl-64/e_pow.c.
128: return (k==1)?__ieee754_pow(-x,y):-__ieee754_pow(-x,y); /* if y even or odd */
The problem occur if sign-bit of a NaN and negation of the NaN is same. I saw this problem only if the neg.d instruction was interpreted by kernel fp emulation.
The patch below is a fix for kernel fp emulation. Is this a right fix? Or glibc should be fixed?
---
Atsushi Nemoto
arch/mips/math-emu/dp_simple.c +6 -4
@@ -48,16 +48,18 @@CLEARCX ;FLUSHXDP ; +/* quick fix up */ +DPSIGN (x) ^=1 ; +if (xc ==IEEE754_CLASS_SNAN ) { +ieee754dp y =ieee754dp_indef ();SETCX (IEEE754_INVALID_OPERATION ); -return ieee754dp_nanxcpt (ieee754dp_indef (),"neg" ); +DPSIGN (y) =DPSIGN (x); +return ieee754dp_nanxcpt (y,"neg" ); }if (ieee754dp_isnan (x))/* but not infinity */ return ieee754dp_nanxcpt (x,"neg" , x); - -/* quick fix up */ -DPSIGN (x) ^=1 ;return x; }
arch/mips/math-emu/sp_simple.c +6 -4
@@ -48,16 +48,18 @@CLEARCX ;FLUSHXSP ; +/* quick fix up */ +SPSIGN (x) ^=1 ; +if (xc ==IEEE754_CLASS_SNAN ) { +ieee754sp y =ieee754sp_indef ();SETCX (IEEE754_INVALID_OPERATION ); -return ieee754sp_nanxcpt (ieee754sp_indef (),"neg" ); +SPSIGN (y) =SPSIGN (x); +return ieee754sp_nanxcpt (y,"neg" ); }if (ieee754sp_isnan (x))/* but not infinity */ return ieee754sp_nanxcpt (x,"neg" , x); - -/* quick fix up */ -SPSIGN (x) ^=1 ;return x; }