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. */
21 #include <math_private.h>
22 #include <fenv_libc.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). */
48 const float inf
= *(const float *)&a_inf
;
49 /* x = f_wash(x); *//* This ensures only one exception for SNaN. */
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
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
71 uint32_t xi0
, xi1
, sxi
, fsgi
;
74 fe
= fegetenv_register();
75 EXTRACT_WORDS (xi0
,xi1
,x
);
77 sxi
= xi0
& 0x3fffffff | 0x3fe00000;
78 INSERT_WORDS (sx
, sxi
, xi1
);
79 t_sqrt
= __t_sqrt
+ (xi0
>> 52-32-8-1 & 0x3fe);
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. */
87 fsgi
= xi0
+ 0x40000000 >> 1 & 0x7ff00000;
89 sg
= sy
*sd
+ sg
; /* 16-bit approximation to sqrt(sx). */
90 INSERT_WORDS (fsg
, fsgi
, 0);
91 e
= -(sy
*sg
- almost_half
);
93 if ((xi0
& 0x7ff00000) == 0)
96 sg
= sg
+ sy
*sd
; /* 32-bit approximation to sqrt(sx). */
98 e
= -(sy
*sg
- almost_half
);
102 sg
= sg
+ sy
*sd
; /* 64-bit approximation to sqrt(sx),
103 but perhaps rounded incorrectly. */
106 e
= -(sy
*sg
- almost_half
);
109 fesetenv_register (fe
);
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
;
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
))
127 feraiseexcept (FE_INVALID
);
129 if (_LIB_VERSION
!= _IEEE_
)
130 x
= __kernel_standard(x
,x
,26);
133 x
= *(const float*)&a_nan
;
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
)