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.
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.
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.
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/>. */
19 #define NO_MATH_REDIRECT
23 #include <math-barriers.h>
24 #include <fenv_private.h>
25 #include <libm-alias-float.h>
26 #include <math-use-builtins.h>
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
31 See a paper by Boldo and Melquiond:
32 http://www.lri.fr/~melquion/doc/08-tc.pdf */
35 __fmaf (float x
, float y
, float z
)
38 return __builtin_fmaf (x
, y
, z
);
40 /* Use generic implementation. */
43 /* Multiplication is always exact. */
44 double temp
= (double) x
* (double) y
;
46 /* Ensure correct sign of an exact zero result by performing the
47 addition in the original rounding mode in that case. */
49 return (float) temp
+ z
;
51 union ieee754_double u
;
53 libc_feholdexcept_setround (&env
, FE_TOWARDZERO
);
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
);
60 /* Reset rounding mode and test for inexact simultaneously. */
61 int j
= libc_feupdateenv_test (&env
, FE_INEXACT
) != 0;
63 if ((u
.ieee
.mantissa1
& 1) == 0 && u
.ieee
.exponent
!= 0x7ff)
64 u
.ieee
.mantissa1
|= j
;
66 /* And finally truncation with round to nearest. */
68 #endif /* ! USE_FMAF_BUILTIN */
71 libm_alias_float (__fma
, fma
)