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