Blame sysdeps/ieee754/dbl-64/s_fmaf.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 <math-barriers.h>
Packit 6c4009
#include <math_private.h>
Packit 6c4009
#include <libm-alias-float.h>
Packit 6c4009
Packit 6c4009
/* This implementation relies on double being more than twice as
Packit 6c4009
   precise as float 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
float
Packit 6c4009
__fmaf (float x, float y, float z)
Packit 6c4009
{
Packit 6c4009
  fenv_t env;
Packit 6c4009
Packit 6c4009
  /* Multiplication is always exact.  */
Packit 6c4009
  double temp = (double) x * (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 (float) temp + z;
Packit 6c4009
Packit 6c4009
  union ieee754_double u;
Packit 6c4009
Packit 6c4009
  libc_feholdexcept_setround (&env, FE_TOWARDZERO);
Packit 6c4009
Packit 6c4009
  /* Perform addition with round to odd.  */
Packit 6c4009
  u.d = temp + (double) z;
Packit 6c4009
  /* Ensure the addition is not scheduled after fetestexcept call.  */
Packit 6c4009
  math_force_eval (u.d);
Packit 6c4009
Packit 6c4009
  /* Reset rounding mode and test for inexact simultaneously.  */
Packit 6c4009
  int j = libc_feupdateenv_test (&env, FE_INEXACT) != 0;
Packit 6c4009
Packit 6c4009
  if ((u.ieee.mantissa1 & 1) == 0 && u.ieee.exponent != 0x7ff)
Packit 6c4009
    u.ieee.mantissa1 |= j;
Packit 6c4009
Packit 6c4009
  /* And finally truncation with round to nearest.  */
Packit 6c4009
  return (float) u.d;
Packit 6c4009
}
Packit 6c4009
#ifndef __fmaf
Packit 6c4009
libm_alias_float (__fma, fma)
Packit 6c4009
#endif