(root)/
glibc-2.38/
sysdeps/
ieee754/
dbl-64/
s_fmaf.c
       1  /* Compute x * y + z as ternary operation.
       2     Copyright (C) 2010-2023 Free Software Foundation, Inc.
       3     This file is part of the GNU C Library.
       4  
       5     The GNU C Library is free software; you can redistribute it and/or
       6     modify it under the terms of the GNU Lesser General Public
       7     License as published by the Free Software Foundation; either
       8     version 2.1 of the License, or (at your option) any later version.
       9  
      10     The GNU C Library is distributed in the hope that it will be useful,
      11     but WITHOUT ANY WARRANTY; without even the implied warranty of
      12     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
      13     Lesser General Public License for more details.
      14  
      15     You should have received a copy of the GNU Lesser General Public
      16     License along with the GNU C Library; if not, see
      17     <https://www.gnu.org/licenses/>.  */
      18  
      19  #define NO_MATH_REDIRECT
      20  #include <math.h>
      21  #include <fenv.h>
      22  #include <ieee754.h>
      23  #include <math-barriers.h>
      24  #include <fenv_private.h>
      25  #include <libm-alias-float.h>
      26  #include <math-use-builtins.h>
      27  
      28  /* This implementation relies on double being more than twice as
      29     precise as float and uses rounding to odd in order to avoid problems
      30     with double rounding.
      31     See a paper by Boldo and Melquiond:
      32     http://www.lri.fr/~melquion/doc/08-tc.pdf  */
      33  
      34  float
      35  __fmaf (float x, float y, float z)
      36  {
      37  #if USE_FMAF_BUILTIN
      38    return __builtin_fmaf (x, y, z);
      39  #else
      40    /* Use generic implementation.  */
      41    fenv_t env;
      42  
      43    /* Multiplication is always exact.  */
      44    double temp = (double) x * (double) y;
      45  
      46    /* Ensure correct sign of an exact zero result by performing the
      47       addition in the original rounding mode in that case.  */
      48    if (temp == -z)
      49      return (float) temp + z;
      50  
      51    union ieee754_double u;
      52  
      53    libc_feholdexcept_setround (&env, FE_TOWARDZERO);
      54  
      55    /* Perform addition with round to odd.  */
      56    u.d = temp + (double) z;
      57    /* Ensure the addition is not scheduled after fetestexcept call.  */
      58    math_force_eval (u.d);
      59  
      60    /* Reset rounding mode and test for inexact simultaneously.  */
      61    int j = libc_feupdateenv_test (&env, FE_INEXACT) != 0;
      62  
      63    if ((u.ieee.mantissa1 & 1) == 0 && u.ieee.exponent != 0x7ff)
      64      u.ieee.mantissa1 |= j;
      65  
      66    /* And finally truncation with round to nearest.  */
      67    return (float) u.d;
      68  #endif /* ! USE_FMAF_BUILTIN  */
      69  }
      70  #ifndef __fmaf
      71  libm_alias_float (__fma, fma)
      72  #endif