long __lrintl (long double x) { double xh, xl; long res, hi, lo; int save_round; ldbl_unpack (x, &xh, &xl); /* Limit the range of values handled by the conversion to long. We do this because we aren't sure whether that conversion properly raises FE_INVALID. */ if ( #if __LONG_MAX__ == 2147483647 __builtin_expect ((__builtin_fabs (xh) <= (double) __LONG_MAX__ + 2), 1) #else __builtin_expect ((__builtin_fabs (xh) <= -(double) (-__LONG_MAX__ - 1)), 1) #endif #if !defined (FE_INVALID) || 1 #endif ) { save_round = __fegetround (); #if __LONG_MAX__ == 2147483647 long long llhi = (long long) xh; if (llhi != (long) llhi) hi = llhi < 0 ? -__LONG_MAX__ - 1 : __LONG_MAX__; else hi = llhi; xh -= hi; #else if (__glibc_unlikely ((xh == -(double) (-__LONG_MAX__ - 1)))) { /* When XH is 9223372036854775808.0, converting to long long will overflow, resulting in an invalid operation. However, XL might be negative and of sufficient magnitude that the overall long double is in fact in range. Avoid raising an exception. In any case we need to convert this value specially, because the converted value is not exactly represented as a double thus subtracting HI from XH suffers rounding error. */ hi = __LONG_MAX__; xh = 1.0; } else { hi = (long) xh; xh -= hi; } #endif ldbl_canonicalize (&xh, &xl); lo = (long) xh; /* Peg at max/min values, assuming that the above conversions do so. Strictly speaking, we can return anything for values that overflow, but this is more useful. */ res = hi + lo; /* This is just sign(hi) == sign(lo) && sign(res) != sign(hi). */ if (__glibc_unlikely (((~(hi ^ lo) & (res ^ hi)) < 0))) goto overflow; xh -= lo; ldbl_canonicalize (&xh, &xl); hi = res; switch (save_round) { case FE_TONEAREST: if (fabs (xh) < 0.5 || (fabs (xh) == 0.5 && ((xh > 0.0 && xl < 0.0) || (xh < 0.0 && xl > 0.0) || (xl == 0.0 && (res & 1) == 0)))) return res; if (xh < 0.0) res -= 1; else res += 1; break; case FE_TOWARDZERO: if (res > 0 && (xh < 0.0 || (xh == 0.0 && xl < 0.0))) res -= 1; else if (res < 0 && (xh > 0.0 || (xh == 0.0 && xl > 0.0))) res += 1; return res; break; case FE_UPWARD: if (xh > 0.0 || (xh == 0.0 && xl > 0.0)) res += 1; break; case FE_DOWNWARD: if (xh < 0.0 || (xh == 0.0 && xl < 0.0)) res -= 1; break; } if (__glibc_unlikely (((~(hi ^ (res - hi)) & (res ^ hi)) < 0))) goto overflow; return res; } else { if (xh > 0.0) hi = __LONG_MAX__; else if (xh < 0.0) hi = -__LONG_MAX__ - 1; else /* Nan */ hi = 0; } overflow: #ifdef FE_INVALID feraiseexcept (FE_INVALID); #endif return hi; }
double __ieee754_sqrt (double x) { #include "uroot.h" static const double rt0 = 9.99999999859990725855365213134618E-01, rt1 = 4.99999999495955425917856814202739E-01, rt2 = 3.75017500867345182581453026130850E-01, rt3 = 3.12523626554518656309172508769531E-01; static const double big = 134217728.0; double y, t, del, res, res1, hy, z, zz, p, hx, tx, ty, s; mynumber a, c = { { 0, 0 } }; int4 k; a.x = x; k = a.i[HIGH_HALF]; a.i[HIGH_HALF] = (k & 0x001fffff) | 0x3fe00000; t = inroot[(k & 0x001fffff) >> 14]; s = a.x; /*----------------- 2^-1022 <= | x |< 2^1024 -----------------*/ if (k > 0x000fffff && k < 0x7ff00000) { int rm = __fegetround (); fenv_t env; libc_feholdexcept_setround (&env, FE_TONEAREST); double ret; y = 1.0 - t * (t * s); t = t * (rt0 + y * (rt1 + y * (rt2 + y * rt3))); c.i[HIGH_HALF] = 0x20000000 + ((k & 0x7fe00000) >> 1); y = t * s; hy = (y + big) - big; del = 0.5 * t * ((s - hy * hy) - (y - hy) * (y + hy)); res = y + del; if (res == (res + 1.002 * ((y - res) + del))) ret = res * c.x; else { res1 = res + 1.5 * ((y - res) + del); EMULV (res, res1, z, zz, p, hx, tx, hy, ty); /* (z+zz)=res*res1 */ res = ((((z - s) + zz) < 0) ? max (res, res1) : min (res, res1)); ret = res * c.x; } math_force_eval (ret); libc_fesetenv (&env); double dret = x / ret; if (dret != ret) { double force_inexact = 1.0 / 3.0; math_force_eval (force_inexact); /* The square root is inexact, ret is the round-to-nearest value which may need adjusting for other rounding modes. */ switch (rm) { #ifdef FE_UPWARD case FE_UPWARD: if (dret > ret) ret = (res + 0x1p-1022) * c.x; break; #endif #ifdef FE_DOWNWARD case FE_DOWNWARD: #endif #ifdef FE_TOWARDZERO case FE_TOWARDZERO: #endif #if defined FE_DOWNWARD || defined FE_TOWARDZERO if (dret < ret) ret = (res - 0x1p-1022) * c.x; break; #endif default: break; } } /* Otherwise (x / ret == ret), either the square root was exact or the division was inexact. */ return ret; }