Add login.
[glibc/history.git] / sysdeps / powerpc / e_sqrt.c
blobdf80973f585de9401d5da10760bab95489458010
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.
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Library General Public License as
7 published by the Free Software Foundation; either version 2 of the
8 License, or (at your option) any later version.
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 Library General Public License for more details.
15 You should have received a copy of the GNU Library General Public
16 License along with the GNU C Library; see the file COPYING.LIB. If not,
17 write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA. */
20 #include <math.h>
21 #include <math_private.h>
22 #include <fenv_libc.h>
23 #include <inttypes.h>
25 static const double almost_half = 0.5000000000000001; /* 0.5 + 2^-53 */
26 static const uint32_t a_nan = 0x7fc00000;
27 static const uint32_t a_inf = 0x7f800000;
28 static const float two108 = 3.245185536584267269e+32;
29 static const float twom54 = 5.551115123125782702e-17;
30 extern const float __t_sqrt[1024];
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.
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). */
45 double
46 __sqrt(double x)
48 const float inf = *(const float *)&a_inf;
49 /* x = f_wash(x); *//* This ensures only one exception for SNaN. */
50 if (x > 0)
52 if (x != inf)
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;
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];
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;
118 else if (x < 0)
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;
135 return f_wash(x);
138 weak_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. */
141 strong_alias(__sqrt,__ieee754_sqrt)