This is the mail archive of the gcc-patches@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: [PATCH, i386]: Committed: Prevent NaNs for sqrt(0.0) with -mrecip


tbp wrote:

Attached patch fixes -mrecip for sqrt(0.0). Patch was tested on
i686-pc-linux-gnu, and is committed to mainline SVN.
As i wasn't too sure how to read into that patch and i had doubts
about the way your 'fix' worked, i've built gcc from a fresh svn
checkout.


{ no inlining }
# /usr/local/gcc-4.3-svn/bin/g++ -O3 -ffast-math -mrecip recip.cc -o recip
# ./recip
val +0.00000000 sqrt_gcc +0.00000000 sqrt_intrin +0.00000000
val -0.00000000 sqrt_gcc +nan sqrt_intrin -0.00000000
val +4.00000000 sqrt_gcc +1.99999988 sqrt_intrin +1.99999988
val -4.00000000 sqrt_gcc -inf sqrt_intrin +nan
{ forced inlining }
# /usr/local/gcc-4.3-svn/bin/g++ -O3 -ffast-math -mrecip recip.cc -o recip
# ./recip
val +0.00000000 sqrt_gcc +0.00000000 sqrt_intrin +0.00000000
val +0.00000000 sqrt_gcc +0.00000000 sqrt_intrin -0.00000000
val +4.00000000 sqrt_gcc +2.00000000 sqrt_intrin +1.99999988
val -4.00000000 sqrt_gcc -inf sqrt_intrin +nan


2 problems:
. your min based filtering, unlike the usual cmp+andn, doesn't handle -0.f

Sigh...


Attached is a cmp+andn by-the-book implementation of the NR fix. It works exactly as your sqrt_intrin. I'll commit this patch in a couple of minutes, after the regression testing finish.
. results, and i'm not just talking about precision, depend on whether
inlining happens or not
No, gcc calculated the result of sqrt with constant without ever calling sqrt().

That said i don't know what -ffast-math has to say about +/-0 :)

Look at the value of val (the input!) in the second row in the forced inline testcase.


2007-06-19 Uros Bizjak <ubizjak@gmail.com>

       * config/i386/i386.c (ix86_emit_swsqrtsf): Limit the result of
       rsqrt insn to FLT_MAX to avoid NaN for zero input argument.

Index: i386.c
===================================================================
--- i386.c      (revision 125853)
+++ i386.c      (working copy)
@@ -22593,7 +22593,7 @@
void ix86_emit_swsqrtsf (rtx res, rtx a, enum machine_mode mode,
                        bool recip)
{
-  rtx x0, e0, e1, e2, e3, three, half, bignum;
+  rtx x0, e0, e1, e2, e3, three, half, zero, mask;

  x0 = gen_reg_rtx (mode);
  e0 = gen_reg_rtx (mode);
@@ -22603,29 +22603,41 @@

  three = CONST_DOUBLE_FROM_REAL_VALUE (dconst3, SFmode);
  half = CONST_DOUBLE_FROM_REAL_VALUE (dconsthalf, SFmode);
-  bignum = gen_lowpart (SFmode, GEN_INT (0x7f7fffff));

+  mask = gen_reg_rtx (mode);
+
  if (VECTOR_MODE_P (mode))
    {
      three = ix86_build_const_vector (SFmode, true, three);
      half = ix86_build_const_vector (SFmode, true, half);
-      bignum = ix86_build_const_vector (SFmode, true, bignum);
    }

  three = force_reg (mode, three);
  half = force_reg (mode, half);
-  bignum = force_reg (mode, bignum);

+ zero = force_reg (mode, CONST0_RTX(mode));
+
/* sqrt(a) = 0.5 * a * rsqrtss(a) * (3.0 - a * rsqrtss(a) * rsqrtss(a))
1.0 / sqrt(a) = 0.5 * rsqrtss(a) * (3.0 - a * rsqrtss(a) * rsqrtss(a)) */


+  /* Compare a to zero.  */
+  emit_insn (gen_rtx_SET (VOIDmode, mask,
+                         gen_rtx_NE (mode, a, zero)));
+
  /* x0 = 1./sqrt(a) estimate */
  emit_insn (gen_rtx_SET (VOIDmode, x0,
                         gen_rtx_UNSPEC (mode, gen_rtvec (1, a),
                                         UNSPEC_RSQRT)));
-  emit_insn (gen_rtx_SET (VOIDmode, x0,
-                         gen_rtx_SMIN (mode, x0, bignum)));
-
+  /* Filter out infinity.  */
+  if (VECTOR_MODE_P (mode))
+    emit_insn (gen_rtx_SET (VOIDmode, gen_lowpart (V4SFmode, x0),
+                           gen_rtx_AND (mode,
+                                        gen_lowpart (V4SFmode, x0),
+                                        gen_lowpart (V4SFmode, mask))));
+  else
+    emit_insn (gen_rtx_SET (VOIDmode, x0,
+                           gen_rtx_AND (mode, x0, mask)));
+
  /* e0 = x0 * a */
  emit_insn (gen_rtx_SET (VOIDmode, e0,
                         gen_rtx_MULT (mode, x0, a)));


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]