Back to index

glibc  2.9
e_sqrt.c
Go to the documentation of this file.
00001 /* Double-precision floating point square root.
00002    Copyright (C) 1997, 2002, 2003, 2004, 2008 Free Software Foundation, Inc.
00003    This file is part of the GNU C Library.
00004 
00005    The GNU C Library is free software; you can redistribute it and/or
00006    modify it under the terms of the GNU Lesser General Public
00007    License as published by the Free Software Foundation; either
00008    version 2.1 of the License, or (at your option) any later version.
00009 
00010    The GNU C Library is distributed in the hope that it will be useful,
00011    but WITHOUT ANY WARRANTY; without even the implied warranty of
00012    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013    Lesser General Public License for more details.
00014 
00015    You should have received a copy of the GNU Lesser General Public
00016    License along with the GNU C Library; if not, write to the Free
00017    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
00018    02111-1307 USA.  */
00019 
00020 #include <math.h>
00021 #include <math_private.h>
00022 #include <fenv_libc.h>
00023 #include <inttypes.h>
00024 
00025 #include <sysdep.h>
00026 #include <ldsodefs.h>
00027 
00028 static const double almost_half = 0.5000000000000001;   /* 0.5 + 2^-53 */
00029 static const ieee_float_shape_type a_nan = {.word = 0x7fc00000 };
00030 static const ieee_float_shape_type a_inf = {.word = 0x7f800000 };
00031 static const float two108 = 3.245185536584267269e+32;
00032 static const float twom54 = 5.551115123125782702e-17;
00033 extern const float __t_sqrt[1024];
00034 
00035 /* The method is based on a description in
00036    Computation of elementary functions on the IBM RISC System/6000 processor,
00037    P. W. Markstein, IBM J. Res. Develop, 34(1) 1990.
00038    Basically, it consists of two interleaved Newton-Rhapson approximations,
00039    one to find the actual square root, and one to find its reciprocal
00040    without the expense of a division operation.   The tricky bit here
00041    is the use of the POWER/PowerPC multiply-add operation to get the
00042    required accuracy with high speed.
00043 
00044    The argument reduction works by a combination of table lookup to
00045    obtain the initial guesses, and some careful modification of the
00046    generated guesses (which mostly runs on the integer unit, while the
00047    Newton-Rhapson is running on the FPU).  */
00048 
00049 #ifdef __STDC__
00050 double
00051 __slow_ieee754_sqrt (double x)
00052 #else
00053 double
00054 __slow_ieee754_sqrt (x)
00055      double x;
00056 #endif
00057 {
00058   const float inf = a_inf.value;
00059 
00060   if (x > 0)
00061     {
00062       /* schedule the EXTRACT_WORDS to get separation between the store
00063          and the load.  */
00064       ieee_double_shape_type ew_u;
00065       ieee_double_shape_type iw_u;
00066       ew_u.value = (x);
00067       if (x != inf)
00068        {
00069          /* Variables named starting with 's' exist in the
00070             argument-reduced space, so that 2 > sx >= 0.5,
00071             1.41... > sg >= 0.70.., 0.70.. >= sy > 0.35... .
00072             Variables named ending with 'i' are integer versions of
00073             floating-point values.  */
00074          double sx;  /* The value of which we're trying to find the
00075                         square root.  */
00076          double sg, g;      /* Guess of the square root of x.  */
00077          double sd, d;      /* Difference between the square of the guess and x.  */
00078          double sy;  /* Estimate of 1/2g (overestimated by 1ulp).  */
00079          double sy2; /* 2*sy */
00080          double e;   /* Difference between y*g and 1/2 (se = e * fsy).  */
00081          double shx; /* == sx * fsg */
00082          double fsg; /* sg*fsg == g.  */
00083          fenv_t fe;  /* Saved floating-point environment (stores rounding
00084                         mode and whether the inexact exception is
00085                         enabled).  */
00086          uint32_t xi0, xi1, sxi, fsgi;
00087          const float *t_sqrt;
00088 
00089          fe = fegetenv_register ();
00090          /* complete the EXTRACT_WORDS (xi0,xi1,x) operation.  */
00091          xi0 = ew_u.parts.msw;
00092          xi1 = ew_u.parts.lsw;
00093          relax_fenv_state ();
00094          sxi = (xi0 & 0x3fffffff) | 0x3fe00000;
00095          /* schedule the INSERT_WORDS (sx, sxi, xi1) to get separation
00096             between the store and the load.  */
00097          iw_u.parts.msw = sxi;
00098          iw_u.parts.lsw = xi1;
00099          t_sqrt = __t_sqrt + (xi0 >> (52 - 32 - 8 - 1) & 0x3fe);
00100          sg = t_sqrt[0];
00101          sy = t_sqrt[1];
00102          /* complete the INSERT_WORDS (sx, sxi, xi1) operation.  */
00103          sx = iw_u.value;
00104 
00105          /* Here we have three Newton-Rhapson iterations each of a
00106             division and a square root and the remainder of the
00107             argument reduction, all interleaved.   */
00108          sd = -(sg * sg - sx);
00109          fsgi = (xi0 + 0x40000000) >> 1 & 0x7ff00000;
00110          sy2 = sy + sy;
00111          sg = sy * sd + sg; /* 16-bit approximation to sqrt(sx). */
00112 
00113          /* schedule the INSERT_WORDS (fsg, fsgi, 0) to get separation
00114             between the store and the load.  */
00115          INSERT_WORDS (fsg, fsgi, 0);
00116          iw_u.parts.msw = fsgi;
00117          iw_u.parts.lsw = (0);
00118          e = -(sy * sg - almost_half);
00119          sd = -(sg * sg - sx);
00120          if ((xi0 & 0x7ff00000) == 0)
00121            goto denorm;
00122          sy = sy + e * sy2;
00123          sg = sg + sy * sd; /* 32-bit approximation to sqrt(sx).  */
00124          sy2 = sy + sy;
00125          /* complete the INSERT_WORDS (fsg, fsgi, 0) operation.  */
00126          fsg = iw_u.value;
00127          e = -(sy * sg - almost_half);
00128          sd = -(sg * sg - sx);
00129          sy = sy + e * sy2;
00130          shx = sx * fsg;
00131          sg = sg + sy * sd; /* 64-bit approximation to sqrt(sx),
00132                                but perhaps rounded incorrectly.  */
00133          sy2 = sy + sy;
00134          g = sg * fsg;
00135          e = -(sy * sg - almost_half);
00136          d = -(g * sg - shx);
00137          sy = sy + e * sy2;
00138          fesetenv_register (fe);
00139          return g + sy * d;
00140        denorm:
00141          /* For denormalised numbers, we normalise, calculate the
00142             square root, and return an adjusted result.  */
00143          fesetenv_register (fe);
00144          return __slow_ieee754_sqrt (x * two108) * twom54;
00145        }
00146     }
00147   else if (x < 0)
00148     {
00149       /* For some reason, some PowerPC32 processors don't implement
00150          FE_INVALID_SQRT.  */
00151 #ifdef FE_INVALID_SQRT
00152       feraiseexcept (FE_INVALID_SQRT);
00153 
00154       fenv_union_t u = { .fenv = fegetenv_register () };
00155       if ((u.l[1] & FE_INVALID) == 0)
00156 #endif
00157        feraiseexcept (FE_INVALID);
00158       x = a_nan.value;
00159     }
00160   return f_wash (x);
00161 }
00162 
00163 #ifdef __STDC__
00164 double
00165 __ieee754_sqrt (double x)
00166 #else
00167 double
00168 __ieee754_sqrt (x)
00169      double x;
00170 #endif
00171 {
00172   double z;
00173 
00174   /* If the CPU is 64-bit we can use the optional FP instructions.  */
00175   if (__CPU_HAS_FSQRT)
00176     {
00177       /* Volatile is required to prevent the compiler from moving the
00178          fsqrt instruction above the branch.  */
00179       __asm __volatile ("   fsqrt  %0,%1\n"
00180                             :"=f" (z):"f" (x));
00181     }
00182   else
00183     z = __slow_ieee754_sqrt (x);
00184 
00185   return z;
00186 }