]>
Commit | Line | Data |
---|---|---|
ffdd5e50 | 1 | /* Double-precision floating point square root. |
688903eb | 2 | Copyright (C) 1997-2018 Free Software Foundation, Inc. |
ffdd5e50 UD |
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 | |
59ba27a6 PE |
16 | License along with the GNU C Library; if not, see |
17 | <http://www.gnu.org/licenses/>. */ | |
ffdd5e50 UD |
18 | |
19 | #include <math.h> | |
20 | #include <math_private.h> | |
21 | #include <fenv_libc.h> | |
22 | #include <inttypes.h> | |
e054f494 | 23 | #include <stdint.h> |
ffdd5e50 UD |
24 | #include <sysdep.h> |
25 | #include <ldsodefs.h> | |
ffdd5e50 | 26 | |
08cee2a4 | 27 | #ifndef _ARCH_PPCSQ |
ffdd5e50 UD |
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]; | |
34 | ||
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. | |
868f7a40 | 38 | Basically, it consists of two interleaved Newton-Raphson approximations, |
ffdd5e50 UD |
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. | |
43 | ||
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 | |
868f7a40 | 47 | Newton-Raphson is running on the FPU). */ |
ffdd5e50 | 48 | |
ffdd5e50 UD |
49 | double |
50 | __slow_ieee754_sqrt (double x) | |
ffdd5e50 UD |
51 | { |
52 | const float inf = a_inf.value; | |
53 | ||
54 | if (x > 0) | |
55 | { | |
56 | /* schedule the EXTRACT_WORDS to get separation between the store | |
0ac5ae23 | 57 | and the load. */ |
ffdd5e50 UD |
58 | ieee_double_shape_type ew_u; |
59 | ieee_double_shape_type iw_u; | |
60 | ew_u.value = (x); | |
61 | if (x != inf) | |
62 | { | |
63 | /* Variables named starting with 's' exist in the | |
64 | argument-reduced space, so that 2 > sx >= 0.5, | |
65 | 1.41... > sg >= 0.70.., 0.70.. >= sy > 0.35... . | |
66 | Variables named ending with 'i' are integer versions of | |
67 | floating-point values. */ | |
68 | double sx; /* The value of which we're trying to find the | |
69 | square root. */ | |
70 | double sg, g; /* Guess of the square root of x. */ | |
71 | double sd, d; /* Difference between the square of the guess and x. */ | |
72 | double sy; /* Estimate of 1/2g (overestimated by 1ulp). */ | |
73 | double sy2; /* 2*sy */ | |
74 | double e; /* Difference between y*g and 1/2 (se = e * fsy). */ | |
75 | double shx; /* == sx * fsg */ | |
76 | double fsg; /* sg*fsg == g. */ | |
77 | fenv_t fe; /* Saved floating-point environment (stores rounding | |
78 | mode and whether the inexact exception is | |
79 | enabled). */ | |
80 | uint32_t xi0, xi1, sxi, fsgi; | |
81 | const float *t_sqrt; | |
82 | ||
83 | fe = fegetenv_register (); | |
84 | /* complete the EXTRACT_WORDS (xi0,xi1,x) operation. */ | |
85 | xi0 = ew_u.parts.msw; | |
86 | xi1 = ew_u.parts.lsw; | |
87 | relax_fenv_state (); | |
88 | sxi = (xi0 & 0x3fffffff) | 0x3fe00000; | |
89 | /* schedule the INSERT_WORDS (sx, sxi, xi1) to get separation | |
90 | between the store and the load. */ | |
91 | iw_u.parts.msw = sxi; | |
92 | iw_u.parts.lsw = xi1; | |
93 | t_sqrt = __t_sqrt + (xi0 >> (52 - 32 - 8 - 1) & 0x3fe); | |
94 | sg = t_sqrt[0]; | |
95 | sy = t_sqrt[1]; | |
96 | /* complete the INSERT_WORDS (sx, sxi, xi1) operation. */ | |
97 | sx = iw_u.value; | |
98 | ||
868f7a40 | 99 | /* Here we have three Newton-Raphson iterations each of a |
ffdd5e50 UD |
100 | division and a square root and the remainder of the |
101 | argument reduction, all interleaved. */ | |
e8bd5286 | 102 | sd = -__builtin_fma (sg, sg, -sx); |
ffdd5e50 UD |
103 | fsgi = (xi0 + 0x40000000) >> 1 & 0x7ff00000; |
104 | sy2 = sy + sy; | |
e8bd5286 JM |
105 | sg = __builtin_fma (sy, sd, sg); /* 16-bit approximation to |
106 | sqrt(sx). */ | |
ffdd5e50 UD |
107 | |
108 | /* schedule the INSERT_WORDS (fsg, fsgi, 0) to get separation | |
109 | between the store and the load. */ | |
110 | INSERT_WORDS (fsg, fsgi, 0); | |
111 | iw_u.parts.msw = fsgi; | |
112 | iw_u.parts.lsw = (0); | |
e8bd5286 JM |
113 | e = -__builtin_fma (sy, sg, -almost_half); |
114 | sd = -__builtin_fma (sg, sg, -sx); | |
ffdd5e50 UD |
115 | if ((xi0 & 0x7ff00000) == 0) |
116 | goto denorm; | |
e8bd5286 JM |
117 | sy = __builtin_fma (e, sy2, sy); |
118 | sg = __builtin_fma (sy, sd, sg); /* 32-bit approximation to | |
119 | sqrt(sx). */ | |
ffdd5e50 UD |
120 | sy2 = sy + sy; |
121 | /* complete the INSERT_WORDS (fsg, fsgi, 0) operation. */ | |
122 | fsg = iw_u.value; | |
e8bd5286 JM |
123 | e = -__builtin_fma (sy, sg, -almost_half); |
124 | sd = -__builtin_fma (sg, sg, -sx); | |
125 | sy = __builtin_fma (e, sy2, sy); | |
ffdd5e50 | 126 | shx = sx * fsg; |
e8bd5286 JM |
127 | sg = __builtin_fma (sy, sd, sg); /* 64-bit approximation to |
128 | sqrt(sx), but perhaps | |
129 | rounded incorrectly. */ | |
ffdd5e50 UD |
130 | sy2 = sy + sy; |
131 | g = sg * fsg; | |
e8bd5286 JM |
132 | e = -__builtin_fma (sy, sg, -almost_half); |
133 | d = -__builtin_fma (g, sg, -shx); | |
134 | sy = __builtin_fma (e, sy2, sy); | |
ffdd5e50 | 135 | fesetenv_register (fe); |
e8bd5286 | 136 | return __builtin_fma (sy, d, g); |
ffdd5e50 UD |
137 | denorm: |
138 | /* For denormalised numbers, we normalise, calculate the | |
139 | square root, and return an adjusted result. */ | |
140 | fesetenv_register (fe); | |
141 | return __slow_ieee754_sqrt (x * two108) * twom54; | |
142 | } | |
143 | } | |
144 | else if (x < 0) | |
145 | { | |
146 | /* For some reason, some PowerPC32 processors don't implement | |
0ac5ae23 | 147 | FE_INVALID_SQRT. */ |
ffdd5e50 | 148 | #ifdef FE_INVALID_SQRT |
0747f818 | 149 | __feraiseexcept (FE_INVALID_SQRT); |
c3a0ead4 UD |
150 | |
151 | fenv_union_t u = { .fenv = fegetenv_register () }; | |
4a28b3ca | 152 | if ((u.l & FE_INVALID) == 0) |
ffdd5e50 | 153 | #endif |
0747f818 | 154 | __feraiseexcept (FE_INVALID); |
ffdd5e50 UD |
155 | x = a_nan.value; |
156 | } | |
157 | return f_wash (x); | |
158 | } | |
08cee2a4 | 159 | #endif /* _ARCH_PPCSQ */ |
ffdd5e50 | 160 | |
8a6d5255 | 161 | #undef __ieee754_sqrt |
ffdd5e50 UD |
162 | double |
163 | __ieee754_sqrt (double x) | |
ffdd5e50 UD |
164 | { |
165 | double z; | |
166 | ||
08cee2a4 AZ |
167 | #ifdef _ARCH_PPCSQ |
168 | asm ("fsqrt %0,%1\n" :"=f" (z):"f" (x)); | |
169 | #else | |
170 | z = __slow_ieee754_sqrt (x); | |
171 | #endif | |
ffdd5e50 UD |
172 | |
173 | return z; | |
174 | } | |
0ac5ae23 | 175 | strong_alias (__ieee754_sqrt, __sqrt_finite) |