2.5-18.1
[glibc.git] / sysdeps / powerpc / fpu / e_sqrt.c
blob540b924656e7001515e3b202f4346630f32cd459
1 /* Double-precision floating point square root.
2 Copyright (C) 1997, 2002, 2003, 2004 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 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.
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.
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. */
20 #include <math.h>
21 #include <math_private.h>
22 #include <fenv_libc.h>
23 #include <inttypes.h>
25 #include <sysdep.h>
26 #include <ldsodefs.h>
28 static const double almost_half = 0.5000000000000001; /* 0.5 + 2^-53 */
29 static const ieee_float_shape_type a_nan = {.word = 0x7fc00000 };
30 static const ieee_float_shape_type a_inf = {.word = 0x7f800000 };
31 static const float two108 = 3.245185536584267269e+32;
32 static const float twom54 = 5.551115123125782702e-17;
33 extern const float __t_sqrt[1024];
35 /* The method is based on a description in
36 Computation of elementary functions on the IBM RISC System/6000 processor,
37 P. W. Markstein, IBM J. Res. Develop, 34(1) 1990.
38 Basically, it consists of two interleaved Newton-Rhapson approximations,
39 one to find the actual square root, and one to find its reciprocal
40 without the expense of a division operation. The tricky bit here
41 is the use of the POWER/PowerPC multiply-add operation to get the
42 required accuracy with high speed.
44 The argument reduction works by a combination of table lookup to
45 obtain the initial guesses, and some careful modification of the
46 generated guesses (which mostly runs on the integer unit, while the
47 Newton-Rhapson is running on the FPU). */
49 #ifdef __STDC__
50 double
51 __slow_ieee754_sqrt (double x)
52 #else
53 double
54 __slow_ieee754_sqrt (x)
55 double x;
56 #endif
58 const float inf = a_inf.value;
60 if (x > 0)
62 /* schedule the EXTRACT_WORDS to get separation between the store
63 and the load. */
64 ieee_double_shape_type ew_u;
65 ieee_double_shape_type iw_u;
66 ew_u.value = (x);
67 if (x != inf)
69 /* Variables named starting with 's' exist in the
70 argument-reduced space, so that 2 > sx >= 0.5,
71 1.41... > sg >= 0.70.., 0.70.. >= sy > 0.35... .
72 Variables named ending with 'i' are integer versions of
73 floating-point values. */
74 double sx; /* The value of which we're trying to find the
75 square root. */
76 double sg, g; /* Guess of the square root of x. */
77 double sd, d; /* Difference between the square of the guess and x. */
78 double sy; /* Estimate of 1/2g (overestimated by 1ulp). */
79 double sy2; /* 2*sy */
80 double e; /* Difference between y*g and 1/2 (se = e * fsy). */
81 double shx; /* == sx * fsg */
82 double fsg; /* sg*fsg == g. */
83 fenv_t fe; /* Saved floating-point environment (stores rounding
84 mode and whether the inexact exception is
85 enabled). */
86 uint32_t xi0, xi1, sxi, fsgi;
87 const float *t_sqrt;
89 fe = fegetenv_register ();
90 /* complete the EXTRACT_WORDS (xi0,xi1,x) operation. */
91 xi0 = ew_u.parts.msw;
92 xi1 = ew_u.parts.lsw;
93 relax_fenv_state ();
94 sxi = (xi0 & 0x3fffffff) | 0x3fe00000;
95 /* schedule the INSERT_WORDS (sx, sxi, xi1) to get separation
96 between the store and the load. */
97 iw_u.parts.msw = sxi;
98 iw_u.parts.lsw = xi1;
99 t_sqrt = __t_sqrt + (xi0 >> (52 - 32 - 8 - 1) & 0x3fe);
100 sg = t_sqrt[0];
101 sy = t_sqrt[1];
102 /* complete the INSERT_WORDS (sx, sxi, xi1) operation. */
103 sx = iw_u.value;
105 /* Here we have three Newton-Rhapson iterations each of a
106 division and a square root and the remainder of the
107 argument reduction, all interleaved. */
108 sd = -(sg * sg - sx);
109 fsgi = (xi0 + 0x40000000) >> 1 & 0x7ff00000;
110 sy2 = sy + sy;
111 sg = sy * sd + sg; /* 16-bit approximation to sqrt(sx). */
113 /* schedule the INSERT_WORDS (fsg, fsgi, 0) to get separation
114 between the store and the load. */
115 INSERT_WORDS (fsg, fsgi, 0);
116 iw_u.parts.msw = fsgi;
117 iw_u.parts.lsw = (0);
118 e = -(sy * sg - almost_half);
119 sd = -(sg * sg - sx);
120 if ((xi0 & 0x7ff00000) == 0)
121 goto denorm;
122 sy = sy + e * sy2;
123 sg = sg + sy * sd; /* 32-bit approximation to sqrt(sx). */
124 sy2 = sy + sy;
125 /* complete the INSERT_WORDS (fsg, fsgi, 0) operation. */
126 fsg = iw_u.value;
127 e = -(sy * sg - almost_half);
128 sd = -(sg * sg - sx);
129 sy = sy + e * sy2;
130 shx = sx * fsg;
131 sg = sg + sy * sd; /* 64-bit approximation to sqrt(sx),
132 but perhaps rounded incorrectly. */
133 sy2 = sy + sy;
134 g = sg * fsg;
135 e = -(sy * sg - almost_half);
136 d = -(g * sg - shx);
137 sy = sy + e * sy2;
138 fesetenv_register (fe);
139 return g + sy * d;
140 denorm:
141 /* For denormalised numbers, we normalise, calculate the
142 square root, and return an adjusted result. */
143 fesetenv_register (fe);
144 return __slow_ieee754_sqrt (x * two108) * twom54;
147 else if (x < 0)
149 /* For some reason, some PowerPC32 processors don't implement
150 FE_INVALID_SQRT. */
151 #ifdef FE_INVALID_SQRT
152 feraiseexcept (FE_INVALID_SQRT);
153 if (!fetestexcept (FE_INVALID))
154 #endif
155 feraiseexcept (FE_INVALID);
156 x = a_nan.value;
158 return f_wash (x);
161 #ifdef __STDC__
162 double
163 __ieee754_sqrt (double x)
164 #else
165 double
166 __ieee754_sqrt (x)
167 double x;
168 #endif
170 double z;
172 /* If the CPU is 64-bit we can use the optional FP instructions. */
173 if (__CPU_HAS_FSQRT)
175 /* Volatile is required to prevent the compiler from moving the
176 fsqrt instruction above the branch. */
177 __asm __volatile (" fsqrt %0,%1\n"
178 :"=f" (z):"f" (x));
180 else
181 z = __slow_ieee754_sqrt (x);
183 return z;