1/* Single-precision floating point square root.
2   Copyright (C) 1997 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 float almost_half = 0.50000006;  /* 0.5 + 2^-24 */
26static const uint32_t a_nan = 0x7fc00000;
27static const uint32_t a_inf = 0x7f800000;
28static const float two48 = 281474976710656.0;
29static const float twom24 = 5.9604644775390625e-8;
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).  */
45float
46__sqrtf(float x)
47{
48  const float inf = *(const float *)&a_inf;
49  /* x = f_washf(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	  float sx;   /* The value of which we're trying to find the square
60			 root.  */
61	  float sg,g; /* Guess of the square root of x.  */
62	  float sd,d; /* Difference between the square of the guess and x.  */
63	  float sy;   /* Estimate of 1/2g (overestimated by 1ulp).  */
64	  float sy2;  /* 2*sy */
65	  float e;    /* Difference between y*g and 1/2 (note that e==se).  */
66	  float shx;  /* == sx * fsg */
67	  float 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 xi, sxi, fsgi;
72	  const float *t_sqrt;
73
74	  GET_FLOAT_WORD (xi, x);
75	  fe = fegetenv_register ();
76	  relax_fenv_state ();
77	  sxi = (xi & 0x3fffffff) | 0x3f000000;
78	  SET_FLOAT_WORD (sx, sxi);
79	  t_sqrt = __t_sqrt + (xi >> (23-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 = (xi + 0x40000000) >> 1 & 0x7f800000;
88	  sy2 = sy + sy;
89	  sg  = sy*sd + sg;  /* 16-bit approximation to sqrt(sx). */
90	  e   = -(sy*sg - almost_half);
91	  SET_FLOAT_WORD (fsg, fsgi);
92	  sd  = -(sg*sg - sx);
93	  sy  = sy + e*sy2;
94	  if ((xi & 0x7f800000) == 0)
95	    goto denorm;
96	  shx = sx * fsg;
97	  sg  = sg + sy*sd;  /* 32-bit approximation to sqrt(sx),
98				but perhaps rounded incorrectly.  */
99	  sy2 = sy + sy;
100	  g   = sg * fsg;
101	  e   = -(sy*sg - almost_half);
102	  d   = -(g*sg - shx);
103	  sy  = sy + e*sy2;
104	  fesetenv_register (fe);
105	  return g + sy*d;
106	denorm:
107	  /* For denormalised numbers, we normalise, calculate the
108	     square root, and return an adjusted result.  */
109	  fesetenv_register (fe);
110	  return __sqrtf(x * two48) * twom24;
111	}
112    }
113  else if (x < 0)
114    {
115#ifdef FE_INVALID_SQRT
116      feraiseexcept (FE_INVALID_SQRT);
117      /* For some reason, some PowerPC processors don't implement
118	 FE_INVALID_SQRT.  I guess no-one ever thought they'd be
119	 used for square roots... :-) */
120      if (!fetestexcept (FE_INVALID))
121#endif
122	feraiseexcept (FE_INVALID);
123#ifndef _IEEE_LIBM
124      if (_LIB_VERSION != _IEEE_)
125	x = __kernel_standard(x,x,126);
126      else
127#endif
128      x = *(const float*)&a_nan;
129    }
130  return f_washf(x);
131}
132
133weak_alias (__sqrtf, sqrtf)
134/* Strictly, this is wrong, but the only places where _ieee754_sqrt is
135   used will not pass in a negative result.  */
136strong_alias(__sqrtf,__ieee754_sqrtf)
137