2 dnl Copyright (C) 2011-2020 Free Software Foundation, Inc.
3 dnl This file is free software; the Free Software Foundation
4 dnl gives unlimited permission to copy and/or distribute it,
5 dnl with or without modifications, as long as this notice is preserved.
7 AC_DEFUN([gl_FUNC_FMA],
9 AC_REQUIRE([gl_MATH_H_DEFAULTS])
11 dnl Determine FMA_LIBM.
12 gl_MATHFUNC([fma], [double], [(double, double, double)],
17 double fma (double, double, double);
19 if test $gl_cv_func_fma_no_libm = yes \
20 || test $gl_cv_func_fma_in_libm = yes; then
21 dnl Also check whether it's declared.
22 dnl IRIX 6.5 has fma() in libm but doesn't declare it in <math.h>,
23 dnl and the function is buggy.
24 AC_CHECK_DECL([fma], , [REPLACE_FMA=1], [[#include <math.h>]])
25 if test $REPLACE_FMA = 0; then
27 case "$gl_cv_func_fma_works" in
34 if test $HAVE_FMA = 0 || test $REPLACE_FMA = 1; then
35 dnl Find libraries needed to link lib/fmal.c.
36 AC_REQUIRE([gl_FUNC_FREXP])
37 AC_REQUIRE([gl_FUNC_LDEXP])
38 AC_REQUIRE([gl_FUNC_FEGETROUND])
40 dnl Append $FREXP_LIBM to FMA_LIBM, avoiding gratuitous duplicates.
43 *) FMA_LIBM="$FMA_LIBM $FREXP_LIBM" ;;
45 dnl Append $LDEXP_LIBM to FMA_LIBM, avoiding gratuitous duplicates.
48 *) FMA_LIBM="$FMA_LIBM $LDEXP_LIBM" ;;
50 dnl Append $FEGETROUND_LIBM to FMA_LIBM, avoiding gratuitous duplicates.
52 *" $FEGETROUND_LIBM "*) ;;
53 *) FMA_LIBM="$FMA_LIBM $FEGETROUND_LIBM" ;;
59 dnl Test whether fma() has any of the 7 known bugs of glibc 2.11.3 on x86_64.
60 AC_DEFUN([gl_FUNC_FMA_WORKS],
62 AC_REQUIRE([AC_PROG_CC])
63 AC_REQUIRE([AC_CANONICAL_HOST]) dnl for cross-compiles
64 AC_REQUIRE([gl_FUNC_LDEXP])
66 LIBS="$LIBS $FMA_LIBM $LDEXP_LIBM"
67 AC_CACHE_CHECK([whether fma works], [gl_cv_func_fma_works],
77 /* These tests fail with glibc 2.11.3 on x86_64. */
79 volatile double x = 1.5; /* 3 * 2^-1 */
80 volatile double y = x;
81 volatile double z = ldexp (1.0, DBL_MANT_DIG + 1); /* 2^54 */
82 /* x * y + z with infinite precision: 2^54 + 9 * 2^-2.
83 Lies between (2^52 + 0) * 2^2 and (2^52 + 1) * 2^2
84 and is closer to (2^52 + 1) * 2^2, therefore the rounding
85 must round up and produce (2^52 + 1) * 2^2. */
86 volatile double expected = z + 4.0;
87 volatile double result = fma (x, y, z);
88 if (result != expected)
92 volatile double x = 1.25; /* 2^0 + 2^-2 */
93 volatile double y = - x;
94 volatile double z = ldexp (1.0, DBL_MANT_DIG + 1); /* 2^54 */
95 /* x * y + z with infinite precision: 2^54 - 2^0 - 2^-1 - 2^-4.
96 Lies between (2^53 - 1) * 2^1 and 2^53 * 2^1
97 and is closer to (2^53 - 1) * 2^1, therefore the rounding
98 must round down and produce (2^53 - 1) * 2^1. */
99 volatile double expected = (ldexp (1.0, DBL_MANT_DIG) - 1.0) * 2.0;
100 volatile double result = fma (x, y, z);
101 if (result != expected)
105 volatile double x = 1.0 + ldexp (1.0, 1 - DBL_MANT_DIG); /* 2^0 + 2^-52 */
106 volatile double y = x;
107 volatile double z = 4.0; /* 2^2 */
108 /* x * y + z with infinite precision: 2^2 + 2^0 + 2^-51 + 2^-104.
109 Lies between (2^52 + 2^50) * 2^-50 and (2^52 + 2^50 + 1) * 2^-50
110 and is closer to (2^52 + 2^50 + 1) * 2^-50, therefore the rounding
111 must round up and produce (2^52 + 2^50 + 1) * 2^-50. */
112 volatile double expected = 4.0 + 1.0 + ldexp (1.0, 3 - DBL_MANT_DIG);
113 volatile double result = fma (x, y, z);
114 if (result != expected)
118 volatile double x = 1.0 + ldexp (1.0, 1 - DBL_MANT_DIG); /* 2^0 + 2^-52 */
119 volatile double y = - x;
120 volatile double z = 8.0; /* 2^3 */
121 /* x * y + z with infinite precision: 2^2 + 2^1 + 2^0 - 2^-51 - 2^-104.
122 Lies between (2^52 + 2^51 + 2^50 - 1) * 2^-50 and
123 (2^52 + 2^51 + 2^50) * 2^-50 and is closer to
124 (2^52 + 2^51 + 2^50 - 1) * 2^-50, therefore the rounding
125 must round down and produce (2^52 + 2^51 + 2^50 - 1) * 2^-50. */
126 volatile double expected = 7.0 - ldexp (1.0, 3 - DBL_MANT_DIG);
127 volatile double result = fma (x, y, z);
128 if (result != expected)
132 volatile double x = 1.25; /* 2^0 + 2^-2 */
133 volatile double y = - 0.75; /* - 2^0 + 2^-2 */
134 volatile double z = ldexp (1.0, DBL_MANT_DIG); /* 2^53 */
135 /* x * y + z with infinite precision: 2^53 - 2^0 + 2^-4.
136 Lies between (2^53 - 2^0) and 2^53 and is closer to (2^53 - 2^0),
137 therefore the rounding must round down and produce (2^53 - 2^0). */
138 volatile double expected = ldexp (1.0, DBL_MANT_DIG) - 1.0;
139 volatile double result = fma (x, y, z);
140 if (result != expected)
143 if ((DBL_MANT_DIG % 2) == 1)
145 volatile double x = 1.0 + ldexp (1.0, - (DBL_MANT_DIG + 1) / 2); /* 2^0 + 2^-27 */
146 volatile double y = 1.0 - ldexp (1.0, - (DBL_MANT_DIG + 1) / 2); /* 2^0 - 2^-27 */
147 volatile double z = - ldexp (1.0, DBL_MIN_EXP - DBL_MANT_DIG); /* - 2^-1074 */
148 /* x * y + z with infinite precision: 2^0 - 2^-54 - 2^-1074.
149 Lies between (2^53 - 1) * 2^-53 and 2^53 * 2^-53 and is closer to
150 (2^53 - 1) * 2^-53, therefore the rounding must round down and
151 produce (2^53 - 1) * 2^-53. */
152 volatile double expected = 1.0 - ldexp (1.0, - DBL_MANT_DIG);
153 volatile double result = fma (x, y, z);
154 if (result != expected)
158 double minus_inf = -1.0 / p0;
159 volatile double x = ldexp (1.0, DBL_MAX_EXP - 1);
160 volatile double y = ldexp (1.0, DBL_MAX_EXP - 1);
161 volatile double z = minus_inf;
162 volatile double result = fma (x, y, z);
163 if (!(result == minus_inf))
168 [gl_cv_func_fma_works=yes],
169 [gl_cv_func_fma_works=no],
170 [dnl Guess yes on native Windows with MSVC.
171 dnl Otherwise guess no, even on glibc systems.
172 gl_cv_func_fma_works="$gl_cross_guess_normal"
175 AC_EGREP_CPP([Known], [
179 ], [gl_cv_func_fma_works="guessing yes"])
187 # Prerequisites of lib/fma.c.
188 AC_DEFUN([gl_PREREQ_FMA], [:])