1/* Single-precision floating point square root.
2   Copyright (C) 1997, 2002 Free Software Foundation, Inc.
3   This file is part of the GNU C Library.
4
5   The GNU C Library is free software; you can redistribute it and/or
6   modify it under the terms of the GNU Lesser General Public
7   License as published by the Free Software Foundation; either
8   version 2.1 of the License, or (at your option) any later version.
9
10   The GNU C Library is distributed in the hope that it will be useful,
11   but WITHOUT ANY WARRANTY; without even the implied warranty of
12   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13   Lesser General Public License for more details.
14
15   You should have received a copy of the GNU Lesser General Public
16   License along with the GNU C Library; if not, write to the Free
17   Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
18   02111-1307 USA.  */
19
20#include <math.h>
21#include <math_private.h>
22#include <fenv_libc.h>
23#include <inttypes.h>
24
25static const double almost_half = 0.5000000000000001;  /* 0.5 + 2^-53 */
26static const uint32_t a_nan = 0x7fc00000;
27static const uint32_t a_inf = 0x7f800000;
28static const float two108 = 3.245185536584267269e+32;
29static const float twom54 = 5.551115123125782702e-17;
30extern const float __t_sqrt[1024];
31
32/* The method is based on a description in
33   Computation of elementary functions on the IBM RISC System/6000 processor,
34   P. W. Markstein, IBM J. Res. Develop, 34(1) 1990.
35   Basically, it consists of two interleaved Newton-Rhapson approximations,
36   one to find the actual square root, and one to find its reciprocal
37   without the expense of a division operation.   The tricky bit here
38   is the use of the POWER/PowerPC multiply-add operation to get the
39   required accuracy with high speed.
40
41   The argument reduction works by a combination of table lookup to
42   obtain the initial guesses, and some careful modification of the
43   generated guesses (which mostly runs on the integer unit, while the
44   Newton-Rhapson is running on the FPU).  */
45double
46__sqrt(double x)
47{
48  const float inf = *(const float *)&a_inf;
49  /* x = f_wash(x); *//* This ensures only one exception for SNaN. */
50  if (x > 0)
51    {
52      if (x != inf)
53	{
54	  /* Variables named starting with 's' exist in the
55	     argument-reduced space, so that 2 > sx >= 0.5,
56	     1.41... > sg >= 0.70.., 0.70.. >= sy > 0.35... .
57	     Variables named ending with 'i' are integer versions of
58	     floating-point values.  */
59	  double sx;   /* The value of which we're trying to find the
60			  square root.  */
61	  double sg,g; /* Guess of the square root of x.  */
62	  double sd,d; /* Difference between the square of the guess and x.  */
63	  double sy;   /* Estimate of 1/2g (overestimated by 1ulp).  */
64	  double sy2;  /* 2*sy */
65	  double e;    /* Difference between y*g and 1/2 (se = e * fsy).  */
66	  double shx;  /* == sx * fsg */
67	  double fsg;  /* sg*fsg == g.  */
68	  fenv_t fe;  /* Saved floating-point environment (stores rounding
69			 mode and whether the inexact exception is
70			 enabled).  */
71	  uint32_t xi0, xi1, sxi, fsgi;
72	  const float *t_sqrt;
73
74	  fe = fegetenv_register();
75	  EXTRACT_WORDS (xi0,xi1,x);
76	  relax_fenv_state();
77	  sxi = (xi0 & 0x3fffffff) | 0x3fe00000;
78	  INSERT_WORDS (sx, sxi, xi1);
79	  t_sqrt = __t_sqrt + (xi0 >> (52-32-8-1)  & 0x3fe);
80	  sg = t_sqrt[0];
81	  sy = t_sqrt[1];
82
83	  /* Here we have three Newton-Rhapson iterations each of a
84	     division and a square root and the remainder of the
85	     argument reduction, all interleaved.   */
86	  sd  = -(sg*sg - sx);
87	  fsgi = (xi0 + 0x40000000) >> 1 & 0x7ff00000;
88	  sy2 = sy + sy;
89	  sg  = sy*sd + sg;  /* 16-bit approximation to sqrt(sx). */
90	  INSERT_WORDS (fsg, fsgi, 0);
91	  e   = -(sy*sg - almost_half);
92	  sd  = -(sg*sg - sx);
93	  if ((xi0 & 0x7ff00000) == 0)
94	    goto denorm;
95	  sy  = sy + e*sy2;
96	  sg  = sg + sy*sd;  /* 32-bit approximation to sqrt(sx).  */
97	  sy2 = sy + sy;
98	  e   = -(sy*sg - almost_half);
99	  sd  = -(sg*sg - sx);
100	  sy  = sy + e*sy2;
101	  shx = sx * fsg;
102	  sg  = sg + sy*sd;  /* 64-bit approximation to sqrt(sx),
103				but perhaps rounded incorrectly.  */
104	  sy2 = sy + sy;
105	  g   = sg * fsg;
106	  e   = -(sy*sg - almost_half);
107	  d   = -(g*sg - shx);
108	  sy  = sy + e*sy2;
109	  fesetenv_register (fe);
110	  return g + sy*d;
111	denorm:
112	  /* For denormalised numbers, we normalise, calculate the
113	     square root, and return an adjusted result.  */
114	  fesetenv_register (fe);
115	  return __sqrt(x * two108) * twom54;
116	}
117    }
118  else if (x < 0)
119    {
120#ifdef FE_INVALID_SQRT
121      feraiseexcept (FE_INVALID_SQRT);
122      /* For some reason, some PowerPC processors don't implement
123	 FE_INVALID_SQRT.  I guess no-one ever thought they'd be
124	 used for square roots... :-) */
125      if (!fetestexcept (FE_INVALID))
126#endif
127	feraiseexcept (FE_INVALID);
128#ifndef _IEEE_LIBM
129      if (_LIB_VERSION != _IEEE_)
130	x = __kernel_standard(x,x,26);
131      else
132#endif
133      x = *(const float*)&a_nan;
134    }
135  return f_wash(x);
136}
137
138weak_alias (__sqrt, sqrt)
139/* Strictly, this is wrong, but the only places where _ieee754_sqrt is
140   used will not pass in a negative result.  */
141strong_alias(__sqrt,__ieee754_sqrt)
142
143#ifdef NO_LONG_DOUBLE
144weak_alias (__sqrt, __sqrtl)
145weak_alias (__sqrt, sqrtl)
146#endif
147