1 /* Single-precision floating point square root.
2 Copyright (C) 1997-2024 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, see
17 <https://www.gnu.org/licenses/>. */
20 #include <math_private.h>
21 #include <fenv_libc.h>
22 #include <libm-alias-finite.h>
23 #include <math-use-builtins.h>
26 __ieee754_sqrtf (float x
)
29 return __builtin_sqrtf (x
);
31 /* The method is based on a description in
32 Computation of elementary functions on the IBM RISC System/6000 processor,
33 P. W. Markstein, IBM J. Res. Develop, 34(1) 1990.
34 Basically, it consists of two interleaved Newton-Raphson approximations,
35 one to find the actual square root, and one to find its reciprocal
36 without the expense of a division operation. The tricky bit here
37 is the use of the POWER/PowerPC multiply-add operation to get the
38 required accuracy with high speed.
40 The argument reduction works by a combination of table lookup to
41 obtain the initial guesses, and some careful modification of the
42 generated guesses (which mostly runs on the integer unit, while the
43 Newton-Raphson is running on the FPU). */
45 extern const float __t_sqrt
[1024];
51 /* Variables named starting with 's' exist in the
52 argument-reduced space, so that 2 > sx >= 0.5,
53 1.41... > sg >= 0.70.., 0.70.. >= sy > 0.35... .
54 Variables named ending with 'i' are integer versions of
55 floating-point values. */
56 float sx
; /* The value of which we're trying to find the square
58 float sg
, g
; /* Guess of the square root of x. */
59 float sd
, d
; /* Difference between the square of the guess and x. */
60 float sy
; /* Estimate of 1/2g (overestimated by 1ulp). */
62 float e
; /* Difference between y*g and 1/2 (note that e==se). */
63 float shx
; /* == sx * fsg */
64 float fsg
; /* sg*fsg == g. */
65 fenv_t fe
; /* Saved floating-point environment (stores rounding
66 mode and whether the inexact exception is
68 uint32_t xi
, sxi
, fsgi
;
71 GET_FLOAT_WORD (xi
, x
);
72 fe
= fegetenv_register ();
74 sxi
= (xi
& 0x3fffffff) | 0x3f000000;
75 SET_FLOAT_WORD (sx
, sxi
);
76 t_sqrt
= __t_sqrt
+ (xi
>> (23 - 8 - 1) & 0x3fe);
80 /* Here we have three Newton-Raphson iterations each of a
81 division and a square root and the remainder of the
82 argument reduction, all interleaved. */
83 sd
= -__builtin_fmaf (sg
, sg
, -sx
);
84 fsgi
= (xi
+ 0x40000000) >> 1 & 0x7f800000;
86 sg
= __builtin_fmaf (sy
, sd
, sg
); /* 16-bit approximation to
88 e
= -__builtin_fmaf (sy
, sg
, -0x1.0000020365653p
-1);
89 SET_FLOAT_WORD (fsg
, fsgi
);
90 sd
= -__builtin_fmaf (sg
, sg
, -sx
);
91 sy
= __builtin_fmaf (e
, sy2
, sy
);
92 if ((xi
& 0x7f800000) == 0)
95 sg
= __builtin_fmaf (sy
, sd
, sg
); /* 32-bit approximation to
97 rounded incorrectly. */
100 e
= -__builtin_fmaf (sy
, sg
, -0x1.0000020365653p
-1);
101 d
= -__builtin_fmaf (g
, sg
, -shx
);
102 sy
= __builtin_fmaf (e
, sy2
, sy
);
103 fesetenv_register (fe
);
104 return __builtin_fmaf (sy
, d
, g
);
106 /* For denormalised numbers, we normalise, calculate the
107 square root, and return an adjusted result. */
108 fesetenv_register (fe
);
109 return __ieee754_sqrtf (x
* 0x1p
+48) * 0x1p
-24;
114 /* For some reason, some PowerPC32 processors don't implement
116 # ifdef FE_INVALID_SQRT
117 feraiseexcept (FE_INVALID_SQRT
);
119 fenv_union_t u
= { .fenv
= fegetenv_register () };
120 if ((u
.l
& FE_INVALID
) == 0)
122 feraiseexcept (FE_INVALID
);
126 #endif /* USE_SQRTF_BUILTIN */
128 libm_alias_finite (__ieee754_sqrtf
, __sqrtf
)