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