Blame sysdeps/ieee754/ldbl-96/s_fma.c

Packit 6c4009
/* Compute x * y + z as ternary operation.
Packit 6c4009
   Copyright (C) 2010-2018 Free Software Foundation, Inc.
Packit 6c4009
   This file is part of the GNU C Library.
Packit 6c4009
   Contributed by Jakub Jelinek <jakub@redhat.com>, 2010.
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 <float.h>
Packit 6c4009
#include <math.h>
Packit 6c4009
#include <fenv.h>
Packit 6c4009
#include <ieee754.h>
Packit 6c4009
#include <math-barriers.h>
Packit 6c4009
#include <math_private.h>
Packit 6c4009
#include <libm-alias-double.h>
Packit 6c4009
Packit 6c4009
/* This implementation uses rounding to odd to avoid problems with
Packit 6c4009
   double rounding.  See a paper by Boldo and Melquiond:
Packit 6c4009
   http://www.lri.fr/~melquion/doc/08-tc.pdf  */
Packit 6c4009
Packit 6c4009
double
Packit 6c4009
__fma (double x, double y, double z)
Packit 6c4009
{
Packit 6c4009
  if (__glibc_unlikely (!isfinite (x) || !isfinite (y)))
Packit 6c4009
    return x * y + z;
Packit 6c4009
  else if (__glibc_unlikely (!isfinite (z)))
Packit 6c4009
    /* If z is Inf, but x and y are finite, the result should be z
Packit 6c4009
       rather than NaN.  */
Packit 6c4009
    return (z + x) + y;
Packit 6c4009
Packit 6c4009
  /* Ensure correct sign of exact 0 + 0.  */
Packit 6c4009
  if (__glibc_unlikely ((x == 0 || y == 0) && z == 0))
Packit 6c4009
    {
Packit 6c4009
      x = math_opt_barrier (x);
Packit 6c4009
      return x * y + z;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  fenv_t env;
Packit 6c4009
  feholdexcept (&env;;
Packit 6c4009
  fesetround (FE_TONEAREST);
Packit 6c4009
Packit 6c4009
  /* Multiplication m1 + m2 = x * y using Dekker's algorithm.  */
Packit 6c4009
#define C ((1ULL << (LDBL_MANT_DIG + 1) / 2) + 1)
Packit 6c4009
  long double x1 = (long double) x * C;
Packit 6c4009
  long double y1 = (long double) y * C;
Packit 6c4009
  long double m1 = (long double) x * y;
Packit 6c4009
  x1 = (x - x1) + x1;
Packit 6c4009
  y1 = (y - y1) + y1;
Packit 6c4009
  long double x2 = x - x1;
Packit 6c4009
  long double y2 = y - y1;
Packit 6c4009
  long double m2 = (((x1 * y1 - m1) + x1 * y2) + x2 * y1) + x2 * y2;
Packit 6c4009
Packit 6c4009
  /* Addition a1 + a2 = z + m1 using Knuth's algorithm.  */
Packit 6c4009
  long double a1 = z + m1;
Packit 6c4009
  long double t1 = a1 - z;
Packit 6c4009
  long double t2 = a1 - t1;
Packit 6c4009
  t1 = m1 - t1;
Packit 6c4009
  t2 = z - t2;
Packit 6c4009
  long double a2 = t1 + t2;
Packit 6c4009
  /* Ensure the arithmetic is not scheduled after feclearexcept call.  */
Packit 6c4009
  math_force_eval (m2);
Packit 6c4009
  math_force_eval (a2);
Packit 6c4009
  feclearexcept (FE_INEXACT);
Packit 6c4009
Packit 6c4009
  /* If the result is an exact zero, ensure it has the correct sign.  */
Packit 6c4009
  if (a1 == 0 && m2 == 0)
Packit 6c4009
    {
Packit 6c4009
      feupdateenv (&env;;
Packit 6c4009
      /* Ensure that round-to-nearest value of z + m1 is not reused.  */
Packit 6c4009
      z = math_opt_barrier (z);
Packit 6c4009
      return z + m1;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  fesetround (FE_TOWARDZERO);
Packit 6c4009
  /* Perform m2 + a2 addition with round to odd.  */
Packit 6c4009
  a2 = a2 + m2;
Packit 6c4009
Packit 6c4009
  /* Add that to a1 again using rounding to odd.  */
Packit 6c4009
  union ieee854_long_double u;
Packit 6c4009
  u.d = a1 + a2;
Packit 6c4009
  if ((u.ieee.mantissa1 & 1) == 0 && u.ieee.exponent != 0x7fff)
Packit 6c4009
    u.ieee.mantissa1 |= fetestexcept (FE_INEXACT) != 0;
Packit 6c4009
  feupdateenv (&env;;
Packit 6c4009
Packit 6c4009
  /* Add finally round to double precision.  */
Packit 6c4009
  return u.d;
Packit 6c4009
}
Packit 6c4009
#ifndef __fma
Packit 6c4009
libm_alias_double (__fma, fma)
Packit 6c4009
#endif