| 1 |
#include "quadmath-imp.h" |
| 2 |
#include <math.h> |
| 3 |
#include <float.h> |
| 4 |
#if __has_include("../../libgcc/soft-fp/soft-fp.h") \ |
| 5 |
&& __has_include("../../libgcc/soft-fp/quad.h") \ |
| 6 |
&& defined(FE_TONEAREST) \ |
| 7 |
&& defined(FE_UPWARD) \ |
| 8 |
&& defined(FE_DOWNWARD) \ |
| 9 |
&& defined(FE_TOWARDZERO) \ |
| 10 |
&& defined(FE_INEXACT) |
| 11 |
#define USE_SOFT_FP 1 |
| 12 |
#if defined(_ARCH_PPC) && !defined(__LONG_DOUBLE_IEEE128__) |
| 13 |
#define TFtype __float128 |
| 14 |
#endif |
| 15 |
#include "../../libgcc/soft-fp/soft-fp.h" |
| 16 |
#include "../../libgcc/soft-fp/quad.h" |
| 17 |
#endif |
| 18 |
|
| 19 |
__float128 |
| 20 |
sqrtq (const __float128 x) |
| 21 |
{ |
| 22 |
__float128 y; |
| 23 |
int exp; |
| 24 |
|
| 25 |
if (isnanq (x) || (isinfq (x) && x > 0)) |
| 26 |
return x; |
| 27 |
|
| 28 |
if (x == 0) |
| 29 |
return x; |
| 30 |
|
| 31 |
if (x < 0) |
| 32 |
{ |
| 33 |
/* Return NaN with invalid signal. */ |
| 34 |
return (x - x) / (x - x); |
| 35 |
} |
| 36 |
|
| 37 |
#if USE_SOFT_FP |
| 38 |
FP_DECL_EX; |
| 39 |
FP_DECL_Q (X); |
| 40 |
FP_DECL_Q (Y); |
| 41 |
|
| 42 |
FP_INIT_ROUNDMODE; |
| 43 |
FP_UNPACK_Q (X, x); |
| 44 |
FP_SQRT_Q (Y, X); |
| 45 |
FP_PACK_Q (y, Y); |
| 46 |
FP_HANDLE_EXCEPTIONS; |
| 47 |
return y; |
| 48 |
#else |
| 49 |
if (x <= DBL_MAX && x >= DBL_MIN) |
| 50 |
{ |
| 51 |
/* Use double result as starting point. */ |
| 52 |
y = sqrt ((double) x); |
| 53 |
|
| 54 |
/* Two Newton iterations. */ |
| 55 |
y -= 0.5q * (y - x / y); |
| 56 |
y -= 0.5q * (y - x / y); |
| 57 |
return y; |
| 58 |
} |
| 59 |
|
| 60 |
#ifdef HAVE_SQRTL |
| 61 |
{ |
| 62 |
long double xl = (long double) x; |
| 63 |
if (xl <= LDBL_MAX && xl >= LDBL_MIN) |
| 64 |
{ |
| 65 |
/* Use long double result as starting point. */ |
| 66 |
y = (__float128) sqrtl (xl); |
| 67 |
|
| 68 |
/* One Newton iteration. */ |
| 69 |
y -= 0.5q * (y - x / y); |
| 70 |
return y; |
| 71 |
} |
| 72 |
} |
| 73 |
#endif |
| 74 |
|
| 75 |
/* If we're outside of the range of C types, we have to compute |
| 76 |
the initial guess the hard way. */ |
| 77 |
y = frexpq (x, &exp); |
| 78 |
if (exp % 2) |
| 79 |
y *= 2, exp--; |
| 80 |
|
| 81 |
y = sqrt (y); |
| 82 |
y = scalbnq (y, exp / 2); |
| 83 |
|
| 84 |
/* Two Newton iterations. */ |
| 85 |
y -= 0.5q * (y - x / y); |
| 86 |
y -= 0.5q * (y - x / y); |
| 87 |
return y; |
| 88 |
#endif |
| 89 |
} |