Blame sysdeps/ieee754/dbl-64/e_sqrt.c

Packit 6c4009
/*
Packit 6c4009
 * IBM Accurate Mathematical Library
Packit 6c4009
 * written by International Business Machines Corp.
Packit 6c4009
 * Copyright (C) 2001-2018 Free Software Foundation, Inc.
Packit 6c4009
 *
Packit 6c4009
 * This program is free software; you can redistribute it and/or modify
Packit 6c4009
 * it under the terms of the GNU Lesser General Public License as published by
Packit 6c4009
 * the Free Software Foundation; either version 2.1 of the License, or
Packit 6c4009
 * (at your option) any later version.
Packit 6c4009
 *
Packit 6c4009
 * This program is distributed in the hope that it will be useful,
Packit 6c4009
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 6c4009
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
Packit 6c4009
 * GNU Lesser General Public License for more details.
Packit 6c4009
 *
Packit 6c4009
 * You should have received a copy of the GNU Lesser General Public License
Packit 6c4009
 * along with this program; if not, see <http://www.gnu.org/licenses/>.
Packit 6c4009
 */
Packit 6c4009
/*********************************************************************/
Packit 6c4009
/* MODULE_NAME: uroot.c                                              */
Packit 6c4009
/*                                                                   */
Packit 6c4009
/* FUNCTION:    usqrt                                                */
Packit 6c4009
/*                                                                   */
Packit 6c4009
/* FILES NEEDED: dla.h endian.h mydefs.h                             */
Packit 6c4009
/*               uroot.tbl                                           */
Packit 6c4009
/*                                                                   */
Packit 6c4009
/* An ultimate sqrt routine. Given an IEEE double machine number x   */
Packit 6c4009
/* it computes the correctly rounded (to nearest) value of square    */
Packit 6c4009
/* root of x.                                                        */
Packit 6c4009
/* Assumption: Machine arithmetic operations are performed in        */
Packit 6c4009
/* round to nearest mode of IEEE 754 standard.                       */
Packit 6c4009
/*                                                                   */
Packit 6c4009
/*********************************************************************/
Packit 6c4009
Packit 6c4009
#include "endian.h"
Packit 6c4009
#include "mydefs.h"
Packit 6c4009
#include <dla.h>
Packit 6c4009
#include "MathLib.h"
Packit 6c4009
#include "root.tbl"
Packit 6c4009
#include <math-barriers.h>
Packit 6c4009
#include <math_private.h>
Packit 6c4009
Packit 6c4009
/*********************************************************************/
Packit 6c4009
/* An ultimate sqrt routine. Given an IEEE double machine number x   */
Packit 6c4009
/* it computes the correctly rounded (to nearest) value of square    */
Packit 6c4009
/* root of x.                                                        */
Packit 6c4009
/*********************************************************************/
Packit 6c4009
double
Packit 6c4009
__ieee754_sqrt (double x)
Packit 6c4009
{
Packit 6c4009
  static const double
Packit 6c4009
    rt0 = 9.99999999859990725855365213134618E-01,
Packit 6c4009
    rt1 = 4.99999999495955425917856814202739E-01,
Packit 6c4009
    rt2 = 3.75017500867345182581453026130850E-01,
Packit 6c4009
    rt3 = 3.12523626554518656309172508769531E-01;
Packit 6c4009
  static const double big = 134217728.0;
Packit 6c4009
  double y, t, del, res, res1, hy, z, zz, p, hx, tx, ty, s;
Packit 6c4009
  mynumber a, c = { { 0, 0 } };
Packit 6c4009
  int4 k;
Packit 6c4009
Packit 6c4009
  a.x = x;
Packit 6c4009
  k = a.i[HIGH_HALF];
Packit 6c4009
  a.i[HIGH_HALF] = (k & 0x001fffff) | 0x3fe00000;
Packit 6c4009
  t = inroot[(k & 0x001fffff) >> 14];
Packit 6c4009
  s = a.x;
Packit 6c4009
  /*----------------- 2^-1022  <= | x |< 2^1024  -----------------*/
Packit 6c4009
  if (k > 0x000fffff && k < 0x7ff00000)
Packit 6c4009
    {
Packit 6c4009
      int rm = __fegetround ();
Packit 6c4009
      fenv_t env;
Packit 6c4009
      libc_feholdexcept_setround (&env, FE_TONEAREST);
Packit 6c4009
      double ret;
Packit 6c4009
      y = 1.0 - t * (t * s);
Packit 6c4009
      t = t * (rt0 + y * (rt1 + y * (rt2 + y * rt3)));
Packit 6c4009
      c.i[HIGH_HALF] = 0x20000000 + ((k & 0x7fe00000) >> 1);
Packit 6c4009
      y = t * s;
Packit 6c4009
      hy = (y + big) - big;
Packit 6c4009
      del = 0.5 * t * ((s - hy * hy) - (y - hy) * (y + hy));
Packit 6c4009
      res = y + del;
Packit 6c4009
      if (res == (res + 1.002 * ((y - res) + del)))
Packit 6c4009
	ret = res * c.x;
Packit 6c4009
      else
Packit 6c4009
	{
Packit 6c4009
	  res1 = res + 1.5 * ((y - res) + del);
Packit 6c4009
	  EMULV (res, res1, z, zz, p, hx, tx, hy, ty); /* (z+zz)=res*res1 */
Packit 6c4009
	  res = ((((z - s) + zz) < 0) ? max (res, res1) :
Packit 6c4009
					min (res, res1));
Packit 6c4009
	  ret = res * c.x;
Packit 6c4009
	}
Packit 6c4009
      math_force_eval (ret);
Packit 6c4009
      libc_fesetenv (&env;;
Packit 6c4009
      double dret = x / ret;
Packit 6c4009
      if (dret != ret)
Packit 6c4009
	{
Packit 6c4009
	  double force_inexact = 1.0 / 3.0;
Packit 6c4009
	  math_force_eval (force_inexact);
Packit 6c4009
	  /* The square root is inexact, ret is the round-to-nearest
Packit 6c4009
	     value which may need adjusting for other rounding
Packit 6c4009
	     modes.  */
Packit 6c4009
	  switch (rm)
Packit 6c4009
	    {
Packit 6c4009
#ifdef FE_UPWARD
Packit 6c4009
	    case FE_UPWARD:
Packit 6c4009
	      if (dret > ret)
Packit 6c4009
		ret = (res + 0x1p-1022) * c.x;
Packit 6c4009
	      break;
Packit 6c4009
#endif
Packit 6c4009
Packit 6c4009
#ifdef FE_DOWNWARD
Packit 6c4009
	    case FE_DOWNWARD:
Packit 6c4009
#endif
Packit 6c4009
#ifdef FE_TOWARDZERO
Packit 6c4009
	    case FE_TOWARDZERO:
Packit 6c4009
#endif
Packit 6c4009
#if defined FE_DOWNWARD || defined FE_TOWARDZERO
Packit 6c4009
	      if (dret < ret)
Packit 6c4009
		ret = (res - 0x1p-1022) * c.x;
Packit 6c4009
	      break;
Packit 6c4009
#endif
Packit 6c4009
Packit 6c4009
	    default:
Packit 6c4009
	      break;
Packit 6c4009
	    }
Packit 6c4009
	}
Packit 6c4009
      /* Otherwise (x / ret == ret), either the square root was exact or
Packit 6c4009
         the division was inexact.  */
Packit 6c4009
      return ret;
Packit 6c4009
    }
Packit 6c4009
  else
Packit 6c4009
    {
Packit 6c4009
      if ((k & 0x7ff00000) == 0x7ff00000)
Packit 6c4009
	return x * x + x; /* sqrt(NaN)=NaN, sqrt(+inf)=+inf, sqrt(-inf)=sNaN */
Packit 6c4009
      if (x == 0)
Packit 6c4009
	return x;       /* sqrt(+0)=+0, sqrt(-0)=-0 */
Packit 6c4009
      if (k < 0)
Packit 6c4009
	return (x - x) / (x - x); /* sqrt(-ve)=sNaN */
Packit 6c4009
      return 0x1p-256 * __ieee754_sqrt (x * 0x1p512);
Packit 6c4009
    }
Packit 6c4009
}
Packit 6c4009
strong_alias (__ieee754_sqrt, __sqrt_finite)