Fix bug in vtable initialization
[official-gcc.git] / libquadmath / math / sqrtq.c
blob6ed4605ed5ca346fd8fc3c476fc77aa4946fc4b4
1 #include "quadmath-imp.h"
2 #include <math.h>
3 #include <float.h>
5 __float128
6 sqrtq (const __float128 x)
8 __float128 y;
9 int exp;
11 if (x == 0)
12 return x;
14 if (isnanq (x))
15 return x;
17 if (x < 0)
18 return nanq ("");
20 if (x <= DBL_MAX && x >= DBL_MIN)
22 /* Use double result as starting point. */
23 y = sqrt ((double) x);
25 /* Two Newton iterations. */
26 y -= 0.5q * (y - x / y);
27 y -= 0.5q * (y - x / y);
28 return y;
31 #ifdef HAVE_SQRTL
32 if (x <= LDBL_MAX && x >= LDBL_MIN)
34 /* Use long double result as starting point. */
35 y = sqrtl ((long double) x);
37 /* One Newton iteration. */
38 y -= 0.5q * (y - x / y);
39 return y;
41 #endif
43 /* If we're outside of the range of C types, we have to compute
44 the initial guess the hard way. */
45 y = frexpq (x, &exp);
46 if (exp % 2)
47 y *= 2, exp--;
49 y = sqrt (y);
50 y = scalbnq (y, exp / 2);
52 /* Two Newton iterations. */
53 y -= 0.5q * (y - x / y);
54 y -= 0.5q * (y - x / y);
55 return y;