(CFLAGS-tst-align.c): Add -mpreferred-stack-boundary=4.
[glibc.git] / sysdeps / powerpc / fpu / e_sqrtf.c
blob9b701012afe59c20be3a548cd51d2b274e7357f8
1 /* Single-precision floating point square root.
2 Copyright (C) 1997, 2003, 2004 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, write to the Free
17 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
18 02111-1307 USA. */
20 #include <math.h>
21 #include <math_private.h>
22 #include <fenv_libc.h>
23 #include <inttypes.h>
25 #include <sysdep.h>
26 #include <ldsodefs.h>
27 #include <dl-procinfo.h>
29 static const float almost_half = 0.50000006; /* 0.5 + 2^-24 */
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 two48 = 281474976710656.0;
33 static const float twom24 = 5.9604644775390625e-8;
34 extern const float __t_sqrt[1024];
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.
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). */
50 #ifdef __STDC__
51 float
52 __slow_ieee754_sqrtf (float x)
53 #else
54 float
55 __slow_ieee754_sqrtf (x)
56 float x;
57 #endif
59 const float inf = a_inf.value;
61 if (x > 0)
63 if (x != inf)
65 /* Variables named starting with 's' exist in the
66 argument-reduced space, so that 2 > sx >= 0.5,
67 1.41... > sg >= 0.70.., 0.70.. >= sy > 0.35... .
68 Variables named ending with 'i' are integer versions of
69 floating-point values. */
70 float sx; /* The value of which we're trying to find the square
71 root. */
72 float sg, g; /* Guess of the square root of x. */
73 float sd, d; /* Difference between the square of the guess and x. */
74 float sy; /* Estimate of 1/2g (overestimated by 1ulp). */
75 float sy2; /* 2*sy */
76 float e; /* Difference between y*g and 1/2 (note that e==se). */
77 float shx; /* == sx * fsg */
78 float fsg; /* sg*fsg == g. */
79 fenv_t fe; /* Saved floating-point environment (stores rounding
80 mode and whether the inexact exception is
81 enabled). */
82 uint32_t xi, sxi, fsgi;
83 const float *t_sqrt;
85 GET_FLOAT_WORD (xi, x);
86 fe = fegetenv_register ();
87 relax_fenv_state ();
88 sxi = (xi & 0x3fffffff) | 0x3f000000;
89 SET_FLOAT_WORD (sx, sxi);
90 t_sqrt = __t_sqrt + (xi >> (23 - 8 - 1) & 0x3fe);
91 sg = t_sqrt[0];
92 sy = t_sqrt[1];
94 /* Here we have three Newton-Rhapson iterations each of a
95 division and a square root and the remainder of the
96 argument reduction, all interleaved. */
97 sd = -(sg * sg - sx);
98 fsgi = (xi + 0x40000000) >> 1 & 0x7f800000;
99 sy2 = sy + sy;
100 sg = sy * sd + sg; /* 16-bit approximation to sqrt(sx). */
101 e = -(sy * sg - almost_half);
102 SET_FLOAT_WORD (fsg, fsgi);
103 sd = -(sg * sg - sx);
104 sy = sy + e * sy2;
105 if ((xi & 0x7f800000) == 0)
106 goto denorm;
107 shx = sx * fsg;
108 sg = sg + sy * sd; /* 32-bit approximation to sqrt(sx),
109 but perhaps rounded incorrectly. */
110 sy2 = sy + sy;
111 g = sg * fsg;
112 e = -(sy * sg - almost_half);
113 d = -(g * sg - shx);
114 sy = sy + e * sy2;
115 fesetenv_register (fe);
116 return g + sy * d;
117 denorm:
118 /* For denormalised numbers, we normalise, calculate the
119 square root, and return an adjusted result. */
120 fesetenv_register (fe);
121 return __slow_ieee754_sqrtf (x * two48) * twom24;
124 else if (x < 0)
126 /* For some reason, some PowerPC32 processors don't implement
127 FE_INVALID_SQRT. */
128 #ifdef FE_INVALID_SQRT
129 feraiseexcept (FE_INVALID_SQRT);
130 if (!fetestexcept (FE_INVALID))
131 #endif
132 feraiseexcept (FE_INVALID);
133 x = a_nan.value;
135 return f_washf (x);
139 #ifdef __STDC__
140 float
141 __ieee754_sqrtf (float x)
142 #else
143 float
144 __ieee754_sqrtf (x)
145 float x;
146 #endif
148 double z;
150 /* If the CPU is 64-bit we can use the optional FP instructions we. */
151 if ((GLRO (dl_hwcap) & PPC_FEATURE_64) != 0)
153 /* Volatile is required to prevent the compiler from moving the
154 fsqrt instruction above the branch. */
155 __asm __volatile (" fsqrts %0,%1\n"
156 :"=f" (z):"f" (x));
158 else
159 z = __slow_ieee754_sqrtf (x);
161 return z;