Blame sysdeps/ieee754/ldbl-128/x2y2m1l.c

Packit 6c4009
/* Compute x^2 + y^2 - 1, without large cancellation error.
Packit 6c4009
   Copyright (C) 2012-2018 Free Software Foundation, Inc.
Packit 6c4009
   This file is part of the GNU C Library.
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 Lesser General Public
Packit 6c4009
   License as published by the Free Software Foundation; either
Packit 6c4009
   version 2.1 of the 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
   Lesser General Public License for more details.
Packit 6c4009
Packit 6c4009
   You should have received a copy of the GNU Lesser General Public
Packit 6c4009
   License along with the GNU C Library; if not, see
Packit 6c4009
   <http://www.gnu.org/licenses/>.  */
Packit 6c4009
Packit 6c4009
#include <math.h>
Packit 6c4009
#include <math_private.h>
Packit 6c4009
#include <mul_splitl.h>
Packit 6c4009
#include <stdlib.h>
Packit 6c4009
Packit 6c4009
Packit 6c4009
/* Calculate X + Y exactly and store the result in *HI + *LO.  It is
Packit 6c4009
   given that |X| >= |Y| and the values are small enough that no
Packit 6c4009
   overflow occurs.  */
Packit 6c4009
Packit 6c4009
static inline void
Packit 6c4009
add_split (_Float128 *hi, _Float128 *lo, _Float128 x, _Float128 y)
Packit 6c4009
{
Packit 6c4009
  /* Apply Dekker's algorithm.  */
Packit 6c4009
  *hi = x + y;
Packit 6c4009
  *lo = (x - *hi) + y;
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
/* Compare absolute values of floating-point values pointed to by P
Packit 6c4009
   and Q for qsort.  */
Packit 6c4009
Packit 6c4009
static int
Packit 6c4009
compare (const void *p, const void *q)
Packit 6c4009
{
Packit 6c4009
  _Float128 pld = fabsl (*(const _Float128 *) p);
Packit 6c4009
  _Float128 qld = fabsl (*(const _Float128 *) q);
Packit 6c4009
  if (pld < qld)
Packit 6c4009
    return -1;
Packit 6c4009
  else if (pld == qld)
Packit 6c4009
    return 0;
Packit 6c4009
  else
Packit 6c4009
    return 1;
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
/* Return X^2 + Y^2 - 1, computed without large cancellation error.
Packit 6c4009
   It is given that 1 > X >= Y >= epsilon / 2, and that X^2 + Y^2 >=
Packit 6c4009
   0.5.  */
Packit 6c4009
Packit 6c4009
_Float128
Packit 6c4009
__x2y2m1l (_Float128 x, _Float128 y)
Packit 6c4009
{
Packit 6c4009
  _Float128 vals[5];
Packit 6c4009
  SET_RESTORE_ROUNDL (FE_TONEAREST);
Packit 6c4009
  mul_splitl (&vals[1], &vals[0], x, x);
Packit 6c4009
  mul_splitl (&vals[3], &vals[2], y, y);
Packit 6c4009
  vals[4] = -1;
Packit 6c4009
  qsort (vals, 5, sizeof (_Float128), compare);
Packit 6c4009
  /* Add up the values so that each element of VALS has absolute value
Packit 6c4009
     at most equal to the last set bit of the next nonzero
Packit 6c4009
     element.  */
Packit 6c4009
  for (size_t i = 0; i <= 3; i++)
Packit 6c4009
    {
Packit 6c4009
      add_split (&vals[i + 1], &vals[i], vals[i + 1], vals[i]);
Packit 6c4009
      qsort (vals + i + 1, 4 - i, sizeof (_Float128), compare);
Packit 6c4009
    }
Packit 6c4009
  /* Now any error from this addition will be small.  */
Packit 6c4009
  return vals[4] + vals[3] + vals[2] + vals[1] + vals[0];
Packit 6c4009
}