hjl / source-git / glibc

Forked from source-git/glibc 3 years ago
Clone

Blame sysdeps/ieee754/ldbl-128ibm/e_sqrtl.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 uroot.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 <math_private.h>
Packit 6c4009
Packit 6c4009
typedef union {int64_t i[2]; long double x; double d[2]; } mynumber;
Packit 6c4009
Packit 6c4009
static const double
Packit 6c4009
  t512 = 0x1p512,
Packit 6c4009
  tm256 = 0x1p-256,
Packit 6c4009
  two54 = 0x1p54,	/* 0x4350000000000000 */
Packit 6c4009
  twom54 = 0x1p-54;	/* 0x3C90000000000000 */
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
long double __ieee754_sqrtl(long double x)
Packit 6c4009
{
Packit 6c4009
  static const long double big = 134217728.0, big1 = 134217729.0;
Packit 6c4009
  long double t,s,i;
Packit 6c4009
  mynumber a,c;
Packit 6c4009
  uint64_t k, l;
Packit 6c4009
  int64_t m, n;
Packit 6c4009
  double d;
Packit 6c4009
Packit 6c4009
  a.x=x;
Packit 6c4009
  k=a.i[0] & INT64_C(0x7fffffffffffffff);
Packit 6c4009
  /*----------------- 2^-1022  <= | x |< 2^1024  -----------------*/
Packit 6c4009
  if (k>INT64_C(0x000fffff00000000) && k
Packit 6c4009
    if (x < 0) return (big1-big1)/(big-big);
Packit 6c4009
    l = (k&INT64_C(0x001fffffffffffff))|INT64_C(0x3fe0000000000000);
Packit 6c4009
    if ((a.i[1] & INT64_C(0x7fffffffffffffff)) != 0) {
Packit 6c4009
      n = (int64_t) ((l - k) * 2) >> 53;
Packit 6c4009
      m = (a.i[1] >> 52) & 0x7ff;
Packit 6c4009
      if (m == 0) {
Packit 6c4009
	a.d[1] *= two54;
Packit 6c4009
	m = ((a.i[1] >> 52) & 0x7ff) - 54;
Packit 6c4009
      }
Packit 6c4009
      m += n;
Packit 6c4009
      if (m > 0)
Packit 6c4009
	a.i[1] = (a.i[1] & INT64_C(0x800fffffffffffff)) | (m << 52);
Packit 6c4009
      else if (m <= -54) {
Packit 6c4009
	a.i[1] &= INT64_C(0x8000000000000000);
Packit 6c4009
      } else {
Packit 6c4009
	m += 54;
Packit 6c4009
	a.i[1] = (a.i[1] & INT64_C(0x800fffffffffffff)) | (m << 52);
Packit 6c4009
	a.d[1] *= twom54;
Packit 6c4009
      }
Packit 6c4009
    }
Packit 6c4009
    a.i[0] = l;
Packit 6c4009
    s = a.x;
Packit 6c4009
    d = __ieee754_sqrt (a.d[0]);
Packit 6c4009
    c.i[0] = INT64_C(0x2000000000000000)+((k&INT64_C(0x7fe0000000000000))>>1);
Packit 6c4009
    c.i[1] = 0;
Packit 6c4009
    i = d;
Packit 6c4009
    t = 0.5L * (i + s / i);
Packit 6c4009
    i = 0.5L * (t + s / t);
Packit 6c4009
    return c.x * i;
Packit 6c4009
  }
Packit 6c4009
  else {
Packit 6c4009
    if (k>=INT64_C(0x7ff0000000000000))
Packit 6c4009
      /* sqrt (-Inf) = NaN, sqrt (NaN) = NaN, sqrt (+Inf) = +Inf.  */
Packit 6c4009
      return x * x + x;
Packit 6c4009
    if (x == 0) return x;
Packit 6c4009
    if (x < 0) return (big1-big1)/(big-big);
Packit 6c4009
    return tm256*__ieee754_sqrtl(x*t512);
Packit 6c4009
  }
Packit 6c4009
}
Packit 6c4009
strong_alias (__ieee754_sqrtl, __sqrtl_finite)