Blame sysdeps/ieee754/ldbl-128/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 <math.h>
Packit 6c4009
#include <fenv.h>
Packit 6c4009
#include <ieee754.h>
Packit 6c4009
#include <libm-alias-double.h>
Packit 6c4009
Packit 6c4009
/* This implementation relies on long double being more than twice as
Packit 6c4009
   precise as double and uses rounding to odd in order to avoid problems
Packit 6c4009
   with double rounding.
Packit 6c4009
   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
  fenv_t env;
Packit 6c4009
  /* Multiplication is always exact.  */
Packit 6c4009
  long double temp = (long double) x * (long double) y;
Packit 6c4009
Packit 6c4009
  /* Ensure correct sign of an exact zero result by performing the
Packit 6c4009
     addition in the original rounding mode in that case.  */
Packit 6c4009
  if (temp == -z)
Packit 6c4009
    return (double) temp + z;
Packit 6c4009
Packit 6c4009
  union ieee854_long_double u;
Packit 6c4009
  feholdexcept (&env;;
Packit 6c4009
  fesetround (FE_TOWARDZERO);
Packit 6c4009
  /* Perform addition with round to odd.  */
Packit 6c4009
  u.d = temp + (long double) z;
Packit 6c4009
  if ((u.ieee.mantissa3 & 1) == 0 && u.ieee.exponent != 0x7fff)
Packit 6c4009
    u.ieee.mantissa3 |= fetestexcept (FE_INEXACT) != 0;
Packit 6c4009
  feupdateenv (&env;;
Packit 6c4009
  /* And finally truncation with round to nearest.  */
Packit 6c4009
  return (double) u.d;
Packit 6c4009
}
Packit 6c4009
#ifndef __fma
Packit 6c4009
libm_alias_double (__fma, fma)
Packit 6c4009
#endif