Cygwin: mmap: use 64K pages for bookkeeping, second attempt
[newlib-cygwin.git] / newlib / libm / mathfp / sf_sqrt.c
blobede24b9876e4e51445206822f85d626b21655a2c
2 /* @(#)z_sqrtf.c 1.0 98/08/13 */
3 /*****************************************************************
4 * The following routines are coded directly from the algorithms
5 * and coefficients given in "Software Manual for the Elementary
6 * Functions" by William J. Cody, Jr. and William Waite, Prentice
7 * Hall, 1980.
8 *****************************************************************/
9 /******************************************************************
10 * Square Root
12 * Input:
13 * x - floating point value
15 * Output:
16 * square-root of x
18 * Description:
19 * This routine performs floating point square root.
21 * The initial approximation is computed as
22 * y0 = 0.41731 + 0.59016 * f
23 * where f is a fraction such that x = f * 2^exp.
25 * Three Newton iterations in the form of Heron's formula
26 * are then performed to obtain the final value:
27 * y[i] = (y[i-1] + f / y[i-1]) / 2, i = 1, 2, 3.
29 *****************************************************************/
31 #include "fdlibm.h"
32 #include "zmath.h"
34 float
35 sqrtf (float x)
37 float f, y;
38 int exp, i, odd;
40 /* Check for special values. */
41 switch (numtestf (x))
43 case NAN:
44 errno = EDOM;
45 return (x);
46 case INF:
47 if (isposf (x))
49 errno = EDOM;
50 return (z_notanum_f.f);
52 else
54 errno = ERANGE;
55 return (z_infinity_f.f);
59 /* Initial checks are performed here. */
60 if (x == 0.0)
61 return (0.0);
62 if (x < 0)
64 errno = EDOM;
65 return (z_notanum_f.f);
68 /* Find the exponent and mantissa for the form x = f * 2^exp. */
69 f = frexpf (x, &exp);
70 odd = exp & 1;
72 /* Get the initial approximation. */
73 y = 0.41731 + 0.59016 * f;
75 f *= 0.5;
76 /* Calculate the remaining iterations. */
77 for (i = 0; i < 2; ++i)
78 y = y * 0.5 + f / y;
80 /* Calculate the final value. */
81 if (odd)
83 y *= __SQRT_HALF;
84 exp++;
86 exp >>= 1;
87 y = ldexpf (y, exp);
89 return (y);
92 #ifdef _DOUBLE_IS_32BITS
94 double sqrt (double x)
96 return (double) sqrtf ((float) x);
99 #endif /* _DOUBLE_IS_32BITS */