Blame sysdeps/powerpc/fpu/e_hypot.c

Packit 6c4009
/* Pythagorean addition using doubles
Packit 6c4009
   Copyright (C) 2011-2018 Free Software Foundation, Inc.
Packit 6c4009
   This file is part of the GNU C Library
Packit 6c4009
   Contributed by Adhemerval Zanella <azanella@br.ibm.com>, 2011
Packit 6c4009
Packit 6c4009
   The GNU C Library is free software; you can redistribute it and/or
Packit 6c4009
   modify it under the terms of the GNU Library General Public License as
Packit 6c4009
   published by the Free Software Foundation; either version 2 of the
Packit 6c4009
   License, or (at your option) any later version.
Packit 6c4009
Packit 6c4009
   The GNU C Library 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 GNU
Packit 6c4009
   Library General Public License for more details.
Packit 6c4009
Packit 6c4009
   You should have received a copy of the GNU Library General Public
Packit 6c4009
   License along with the GNU C Library; see the file COPYING.LIB.  If
Packit 6c4009
   not, see <http://www.gnu.org/licenses/>.  */
Packit 6c4009
Packit 6c4009
#include <math.h>
Packit 6c4009
#include <math_private.h>
Packit 6c4009
#include <math-underflow.h>
Packit 6c4009
#include <stdint.h>
Packit 6c4009
Packit 6c4009
static const double two60   = 1.152921504606847e+18;
Packit 6c4009
static const double two500  = 3.2733906078961419e+150;
Packit 6c4009
static const double two600  = 4.149515568880993e+180;
Packit 6c4009
static const double two1022 = 4.49423283715579e+307;
Packit 6c4009
static const double twoM500 = 3.054936363499605e-151;
Packit 6c4009
static const double twoM600 = 2.4099198651028841e-181;
Packit 6c4009
static const double two60factor = 1.5592502418239997e+290;
Packit 6c4009
static const double pdnum   = 2.225073858507201e-308;
Packit 6c4009
Packit 6c4009
/* __ieee754_hypot(x,y)
Packit 6c4009
 *
Packit 6c4009
 * This a FP only version without any FP->INT conversion.
Packit 6c4009
 * It is similar to default C version, making appropriates
Packit 6c4009
 * overflow and underflows checks as well scaling when it
Packit 6c4009
 * is needed.
Packit 6c4009
 */
Packit 6c4009
Packit 6c4009
#ifdef _ARCH_PWR7
Packit 6c4009
/* POWER7 isinf and isnan optimization are fast. */
Packit 6c4009
# define TEST_INF_NAN(x, y)                                       \
Packit 6c4009
   if ((isinf(x) || isinf(y))					  \
Packit 6c4009
       && !issignaling (x) && !issignaling (y))			  \
Packit 6c4009
       return INFINITY;                                           \
Packit 6c4009
   if (isnan(x) || isnan(y))                                      \
Packit 6c4009
       return x + y;
Packit 6c4009
# else
Packit 6c4009
/* For POWER6 and below isinf/isnan triggers LHS and PLT calls are
Packit 6c4009
 * costly (especially for POWER6). */
Packit 6c4009
# define GET_TW0_HIGH_WORD(d1,d2,i1,i2)                           \
Packit 6c4009
 do {                                                             \
Packit 6c4009
   ieee_double_shape_type gh_u1;                                  \
Packit 6c4009
   ieee_double_shape_type gh_u2;                                  \
Packit 6c4009
   gh_u1.value = (d1);                                            \
Packit 6c4009
   gh_u2.value = (d2);                                            \
Packit 6c4009
   (i1) = gh_u1.parts.msw & 0x7fffffff;                           \
Packit 6c4009
   (i2) = gh_u2.parts.msw & 0x7fffffff;                           \
Packit 6c4009
 } while (0)
Packit 6c4009
Packit 6c4009
# define TEST_INF_NAN(x, y)                                      \
Packit 6c4009
 do {                                                            \
Packit 6c4009
   uint32_t hx, hy;                                              \
Packit 6c4009
   GET_TW0_HIGH_WORD(x, y, hx, hy);                              \
Packit 6c4009
   if (hy > hx) {                                                \
Packit 6c4009
     uint32_t ht = hx; hx = hy; hy = ht;                         \
Packit 6c4009
   }                                                             \
Packit 6c4009
   if (hx >= 0x7ff00000) {                                       \
Packit 6c4009
     if ((hx == 0x7ff00000 || hy == 0x7ff00000)			 \
Packit 6c4009
	 && !issignaling (x) && !issignaling (y))		 \
Packit 6c4009
       return INFINITY;                                          \
Packit 6c4009
     return x + y;						 \
Packit 6c4009
   }                                                             \
Packit 6c4009
 } while (0)
Packit 6c4009
Packit 6c4009
#endif
Packit 6c4009
Packit 6c4009
Packit 6c4009
double
Packit 6c4009
__ieee754_hypot (double x, double y)
Packit 6c4009
{
Packit 6c4009
  x = fabs (x);
Packit 6c4009
  y = fabs (y);
Packit 6c4009
Packit 6c4009
  TEST_INF_NAN (x, y);
Packit 6c4009
Packit 6c4009
  if (y > x)
Packit 6c4009
    {
Packit 6c4009
      double t = x;
Packit 6c4009
      x = y;
Packit 6c4009
      y = t;
Packit 6c4009
    }
Packit 6c4009
  if (y == 0.0)
Packit 6c4009
    return x;
Packit 6c4009
  /* if y is higher enough, y * 2^60 might overflow. The tests if
Packit 6c4009
     y >= 1.7976931348623157e+308/2^60 (two60factor) and uses the
Packit 6c4009
     appropriate check to avoid the overflow exception generation.  */
Packit 6c4009
  if (y > two60factor)
Packit 6c4009
    {
Packit 6c4009
      if ((x / y) > two60)
Packit 6c4009
	return x + y;
Packit 6c4009
    }
Packit 6c4009
  else
Packit 6c4009
    {
Packit 6c4009
      if (x > (y * two60))
Packit 6c4009
	return x + y;
Packit 6c4009
    }
Packit 6c4009
  if (x > two500)
Packit 6c4009
    {
Packit 6c4009
      x *= twoM600;
Packit 6c4009
      y *= twoM600;
Packit 6c4009
      return sqrt (x * x + y * y) / twoM600;
Packit 6c4009
    }
Packit 6c4009
  if (y < twoM500)
Packit 6c4009
    {
Packit 6c4009
      if (y <= pdnum)
Packit 6c4009
	{
Packit 6c4009
	  x *= two1022;
Packit 6c4009
	  y *= two1022;
Packit 6c4009
	  double ret = sqrt (x * x + y * y) / two1022;
Packit 6c4009
	  math_check_force_underflow_nonneg (ret);
Packit 6c4009
	  return ret;
Packit 6c4009
	}
Packit 6c4009
      else
Packit 6c4009
	{
Packit 6c4009
	  x *= two600;
Packit 6c4009
	  y *= two600;
Packit 6c4009
	  return sqrt (x * x + y * y) / two600;
Packit 6c4009
	}
Packit 6c4009
    }
Packit 6c4009
  return sqrt (x * x + y * y);
Packit 6c4009
}
Packit 6c4009
strong_alias (__ieee754_hypot, __hypot_finite)