From e657a45c9599559dead7bbadb320764d7362d209 Mon Sep 17 00:00:00 2001 From: Bruno Haible Date: Mon, 17 Oct 2011 23:51:21 +0200 Subject: [PATCH] New module 'fmaf'. * lib/math.in.h (fmaf): New declaration. * lib/fmaf.c: New file. * m4/fmaf.m4: New file. * m4/math_h.m4 (gl_MATH_H): Test whethern fmaf is declared. (gl_MATH_H_DEFAULTS): Initialize GNULIB_FMAF, HAVE_FMAF, REPLACE_FMAF. * modules/math (Makefile.am): Substitute GNULIB_FMAF, HAVE_FMAF, REPLACE_FMAF. * modules/fmaf: New file. * doc/posix-functions/fmaf.texi: Mention the new module and the various bugs. --- ChangeLog | 12 +++ doc/posix-functions/fmaf.texi | 11 ++- lib/fmaf.c | 20 ++++ lib/math.in.h | 23 +++++ m4/fmaf.m4 | 167 ++++++++++++++++++++++++++++++++++ m4/math_h.m4 | 7 +- modules/fmaf | 42 +++++++++ modules/math | 3 + 8 files changed, 279 insertions(+), 6 deletions(-) create mode 100644 lib/fmaf.c create mode 100644 m4/fmaf.m4 create mode 100644 modules/fmaf diff --git a/ChangeLog b/ChangeLog index d9af09c354..0897630525 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,5 +1,17 @@ 2011-11-05 Bruno Haible + New module 'fmaf'. + * lib/math.in.h (fmaf): New declaration. + * lib/fmaf.c: New file. + * m4/fmaf.m4: New file. + * m4/math_h.m4 (gl_MATH_H): Test whethern fmaf is declared. + (gl_MATH_H_DEFAULTS): Initialize GNULIB_FMAF, HAVE_FMAF, REPLACE_FMAF. + * modules/math (Makefile.am): Substitute GNULIB_FMAF, HAVE_FMAF, + REPLACE_FMAF. + * modules/fmaf: New file. + * doc/posix-functions/fmaf.texi: Mention the new module and the various + bugs. + Tests for module 'fma'. * modules/fma-tests: New file. * tests/test-fma1.c: New file. diff --git a/doc/posix-functions/fmaf.texi b/doc/posix-functions/fmaf.texi index 4b12d866a9..97d9705905 100644 --- a/doc/posix-functions/fmaf.texi +++ b/doc/posix-functions/fmaf.texi @@ -4,15 +4,18 @@ POSIX specification:@* @url{http://www.opengroup.org/onlinepubs/9699919799/functions/fmaf.html} -Gnulib module: --- +Gnulib module: fmaf Portability problems fixed by Gnulib: @itemize +@item +This function is missing on some platforms: +FreeBSD 5.2.1, NetBSD 5.0, OpenBSD 3.8, Minix 3.1.8, AIX 5.1, HP-UX 11, IRIX 6.5, OSF/1 4.0, Solaris 9, MSVC 9, Interix 3.5. +@item +This function produces wrong results on some platforms: +glibc 2.11, MacOS X 10.5, FreeBSD 6.4/x86, OSF/1 5.1, Cygwin 1.5, mingw. @end itemize Portability problems not fixed by Gnulib: @itemize -@item -This function is missing on some platforms: -FreeBSD 5.2.1, NetBSD 5.0, OpenBSD 3.8, Minix 3.1.8, AIX 5.1, HP-UX 11, IRIX 6.5, OSF/1 4.0, Solaris 9, MSVC 9, Interix 3.5. @end itemize diff --git a/lib/fmaf.c b/lib/fmaf.c new file mode 100644 index 0000000000..d1f531059b --- /dev/null +++ b/lib/fmaf.c @@ -0,0 +1,20 @@ +/* Fused multiply-add. + Copyright (C) 2011 Free Software Foundation, Inc. + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . */ + +/* Written by Bruno Haible , 2011. */ + +#define USE_FLOAT +#include "fma.c" diff --git a/lib/math.in.h b/lib/math.in.h index ab4f41163e..41107a9eff 100644 --- a/lib/math.in.h +++ b/lib/math.in.h @@ -507,6 +507,29 @@ _GL_WARN_ON_USE (floorl, "floorl is unportable - " #endif +#if @GNULIB_FMAF@ +# if @REPLACE_FMAF@ +# if !(defined __cplusplus && defined GNULIB_NAMESPACE) +# undef fmaf +# define fmaf rpl_fmaf +# endif +_GL_FUNCDECL_RPL (fmaf, float, (float x, float y, float z)); +_GL_CXXALIAS_RPL (fmaf, float, (float x, float y, float z)); +# else +# if !@HAVE_FMAF@ +_GL_FUNCDECL_SYS (fmaf, float, (float x, float y, float z)); +# endif +_GL_CXXALIAS_SYS (fmaf, float, (float x, float y, float z)); +# endif +_GL_CXXALIASWARN (fmaf); +#elif defined GNULIB_POSIXCHECK +# undef fmaf +# if HAVE_RAW_DECL_FMAF +_GL_WARN_ON_USE (fmaf, "fmaf is unportable - " + "use gnulib module fmaf for portability"); +# endif +#endif + #if @GNULIB_FMA@ # if @REPLACE_FMA@ # if !(defined __cplusplus && defined GNULIB_NAMESPACE) diff --git a/m4/fmaf.m4 b/m4/fmaf.m4 new file mode 100644 index 0000000000..9d6eb37958 --- /dev/null +++ b/m4/fmaf.m4 @@ -0,0 +1,167 @@ +# fmaf.m4 serial 1 +dnl Copyright (C) 2011 Free Software Foundation, Inc. +dnl This file is free software; the Free Software Foundation +dnl gives unlimited permission to copy and/or distribute it, +dnl with or without modifications, as long as this notice is preserved. + +AC_DEFUN([gl_FUNC_FMAF], +[ + AC_REQUIRE([gl_MATH_H_DEFAULTS]) + + dnl Determine FMAF_LIBM. + gl_MATHFUNC([fmaf], [float], [(float, float, float)]) + if test $gl_cv_func_fmaf_no_libm = yes \ + || test $gl_cv_func_fmaf_in_libm = yes; then + gl_FUNC_FMAF_WORKS + case "$gl_cv_func_fmaf_works" in + *no) REPLACE_FMAF=1 ;; + esac + else + HAVE_FMAF=0 + fi + if test $HAVE_FMAF = 0 || test $REPLACE_FMAF = 1; then + dnl Find libraries needed to link lib/fmaf.c. + AC_REQUIRE([gl_FUNC_FREXPF]) + AC_REQUIRE([gl_FUNC_LDEXPF]) + AC_REQUIRE([gl_FUNC_FEGETROUND]) + FMAF_LIBM= + dnl Append $FREXPF_LIBM to FMAF_LIBM, avoiding gratuitous duplicates. + case " $FMAF_LIBM " in + *" $FREXPF_LIBM "*) ;; + *) FMAF_LIBM="$FMAF_LIBM $FREXPF_LIBM" ;; + esac + dnl Append $LDEXPF_LIBM to FMAF_LIBM, avoiding gratuitous duplicates. + case " $FMAF_LIBM " in + *" $LDEXPF_LIBM "*) ;; + *) FMAF_LIBM="$FMAF_LIBM $LDEXPF_LIBM" ;; + esac + dnl Append $FEGETROUND_LIBM to FMAF_LIBM, avoiding gratuitous duplicates. + case " $FMAF_LIBM " in + *" $FEGETROUND_LIBM "*) ;; + *) FMAF_LIBM="$FMAF_LIBM $FEGETROUND_LIBM" ;; + esac + fi + AC_SUBST([FMAF_LIBM]) +]) + +dnl Test whether fmaf() has any of the 7 known bugs of glibc 2.11.3 on x86_64. +AC_DEFUN([gl_FUNC_FMAF_WORKS], +[ + AC_REQUIRE([AC_PROG_CC]) + AC_REQUIRE([AC_CANONICAL_HOST]) dnl for cross-compiles + AC_REQUIRE([gl_FUNC_LDEXPF]) + save_LIBS="$LIBS" + LIBS="$LIBS $FMAF_LIBM $LDEXPF_LIBM" + AC_CACHE_CHECK([whether fmaf works], [gl_cv_func_fmaf_works], + [ + AC_RUN_IFELSE( + [AC_LANG_SOURCE([[ +#include +#include +float p0 = 0.0f; +int main() +{ + int failed_tests = 0; + /* These tests fail with glibc 2.11.3 on x86_64. */ + { + volatile float x = 1.5f; /* 3 * 2^-1 */ + volatile float y = x; + volatile float z = ldexpf (1.0f, FLT_MANT_DIG + 1); /* 2^25 */ + /* x * y + z with infinite precision: 2^25 + 9 * 2^-2. + Lies between (2^23 + 0) * 2^2 and (2^23 + 1) * 2^2 + and is closer to (2^23 + 1) * 2^2, therefore the rounding + must round up and produce (2^23 + 1) * 2^2. */ + volatile float expected = z + 4.0f; + volatile float result = fmaf (x, y, z); + if (result != expected) + failed_tests |= 1; + } + { + volatile float x = 1.25f; /* 2^0 + 2^-2 */ + volatile float y = - x; + volatile float z = ldexpf (1.0f, FLT_MANT_DIG + 1); /* 2^25 */ + /* x * y + z with infinite precision: 2^25 - 2^0 - 2^-1 - 2^-4. + Lies between (2^24 - 1) * 2^1 and 2^24 * 2^1 + and is closer to (2^24 - 1) * 2^1, therefore the rounding + must round down and produce (2^24 - 1) * 2^1. */ + volatile float expected = (ldexpf (1.0f, FLT_MANT_DIG) - 1.0f) * 2.0f; + volatile float result = fmaf (x, y, z); + if (result != expected) + failed_tests |= 2; + } + { + volatile float x = 1.0f + ldexpf (1.0f, 1 - FLT_MANT_DIG); /* 2^0 + 2^-23 */ + volatile float y = x; + volatile float z = 4.0f; /* 2^2 */ + /* x * y + z with infinite precision: 2^2 + 2^0 + 2^-22 + 2^-46. + Lies between (2^23 + 2^21) * 2^-21 and (2^23 + 2^21 + 1) * 2^-21 + and is closer to (2^23 + 2^21 + 1) * 2^-21, therefore the rounding + must round up and produce (2^23 + 2^21 + 1) * 2^-21. */ + volatile float expected = 4.0f + 1.0f + ldexpf (1.0f, 3 - FLT_MANT_DIG); + volatile float result = fmaf (x, y, z); + if (result != expected) + failed_tests |= 4; + } + { + volatile float x = 1.0f + ldexpf (1.0f, 1 - FLT_MANT_DIG); /* 2^0 + 2^-23 */ + volatile float y = - x; + volatile float z = 8.0f; /* 2^3 */ + /* x * y + z with infinite precision: 2^2 + 2^1 + 2^0 - 2^-22 - 2^-46. + Lies between (2^23 + 2^22 + 2^21 - 1) * 2^-21 and + (2^23 + 2^22 + 2^21) * 2^-21 and is closer to + (2^23 + 2^22 + 2^21 - 1) * 2^-21, therefore the rounding + must round down and produce (2^23 + 2^22 + 2^21 - 1) * 2^-21. */ + volatile float expected = 7.0f - ldexpf (1.0f, 3 - FLT_MANT_DIG); + volatile float result = fmaf (x, y, z); + if (result != expected) + failed_tests |= 8; + } + { + volatile float x = 1.25f; /* 2^0 + 2^-2 */ + volatile float y = - 0.75f; /* - 2^0 + 2^-2 */ + volatile float z = ldexpf (1.0f, FLT_MANT_DIG); /* 2^24 */ + /* x * y + z with infinite precision: 2^24 - 2^0 + 2^-4. + Lies between (2^24 - 2^0) and 2^24 and is closer to (2^24 - 2^0), + therefore the rounding must round down and produce (2^24 - 2^0). */ + volatile float expected = ldexpf (1.0f, FLT_MANT_DIG) - 1.0f; + volatile float result = fmaf (x, y, z); + if (result != expected) + failed_tests |= 16; + } + if ((FLT_MANT_DIG % 2) == 0) + { + volatile float x = 1.0f + ldexpf (1.0f, - FLT_MANT_DIG / 2); /* 2^0 + 2^-12 */ + volatile float y = x; + volatile float z = ldexpf (1.0f, FLT_MIN_EXP - FLT_MANT_DIG); /* 2^-149 */ + /* x * y + z with infinite precision: 2^0 + 2^-11 + 2^-24 + 2^-149. + Lies between (2^23 + 2^12 + 0) * 2^-23 and (2^23 + 2^12 + 1) * 2^-23 + and is closer to (2^23 + 2^12 + 1) * 2^-23, therefore the rounding + must round up and produce (2^23 + 2^12 + 1) * 2^-23. */ + volatile float expected = + 1.0f + ldexpf (1.0f, 1 - FLT_MANT_DIG / 2) + ldexpf (1.0f, 1 - FLT_MANT_DIG); + volatile float result = fmaf (x, y, z); + if (result != expected) + failed_tests |= 32; + } + { + float minus_inf = -1.0f / p0; + volatile float x = ldexpf (1.0f, FLT_MAX_EXP - 1); + volatile float y = ldexpf (1.0f, FLT_MAX_EXP - 1); + volatile float z = minus_inf; + volatile float result = fmaf (x, y, z); + if (!(result == minus_inf)) + failed_tests |= 64; + } + return failed_tests; +}]])], + [gl_cv_func_fmaf_works=yes], + [gl_cv_func_fmaf_works=no], + [dnl Guess no, even on glibc systems. + gl_cv_func_fmaf_works="guessing no" + ]) + ]) + LIBS="$save_LIBS" +]) + +# Prerequisites of lib/fmaf.c. +AC_DEFUN([gl_PREREQ_FMAF], [:]) diff --git a/m4/math_h.m4 b/m4/math_h.m4 index 878aaff3a1..8c65173033 100644 --- a/m4/math_h.m4 +++ b/m4/math_h.m4 @@ -1,4 +1,4 @@ -# math_h.m4 serial 54 +# math_h.m4 serial 55 dnl Copyright (C) 2007-2011 Free Software Foundation, Inc. dnl This file is free software; the Free Software Foundation dnl gives unlimited permission to copy and/or distribute it, @@ -41,7 +41,7 @@ AC_DEFUN([gl_MATH_H], gl_WARN_ON_USE_PREPARE([[#include ]], [acosf acosl asinf asinl atanf atanl ceilf ceill copysign copysignf copysignl cosf cosl coshf - expf expl fabsf floorf floorl fma fmodf frexpf frexpl + expf expl fabsf floorf floorl fma fmaf fmodf frexpf frexpl ldexpf ldexpl logb logf logl log10f modff powf rint rintf rintl round roundf roundl sinf sinl sinhf sqrtf sqrtl tanf tanl tanhf trunc truncf truncl]) @@ -81,6 +81,7 @@ AC_DEFUN([gl_MATH_H_DEFAULTS], GNULIB_FLOORF=0; AC_SUBST([GNULIB_FLOORF]) GNULIB_FLOORL=0; AC_SUBST([GNULIB_FLOORL]) GNULIB_FMA=0; AC_SUBST([GNULIB_FMA]) + GNULIB_FMAF=0; AC_SUBST([GNULIB_FMAF]) GNULIB_FMODF=0; AC_SUBST([GNULIB_FMODF]) GNULIB_FREXPF=0; AC_SUBST([GNULIB_FREXPF]) GNULIB_FREXP=0; AC_SUBST([GNULIB_FREXP]) @@ -135,6 +136,7 @@ AC_DEFUN([gl_MATH_H_DEFAULTS], HAVE_EXPL=1; AC_SUBST([HAVE_EXPL]) HAVE_FABSF=1; AC_SUBST([HAVE_FABSF]) HAVE_FMA=1; AC_SUBST([HAVE_FMA]) + HAVE_FMAF=1; AC_SUBST([HAVE_FMAF]) HAVE_FMODF=1; AC_SUBST([HAVE_FMODF]) HAVE_FREXPF=1; AC_SUBST([HAVE_FREXPF]) HAVE_ISNANF=1; AC_SUBST([HAVE_ISNANF]) @@ -186,6 +188,7 @@ AC_DEFUN([gl_MATH_H_DEFAULTS], REPLACE_FLOORF=0; AC_SUBST([REPLACE_FLOORF]) REPLACE_FLOORL=0; AC_SUBST([REPLACE_FLOORL]) REPLACE_FMA=0; AC_SUBST([REPLACE_FMA]) + REPLACE_FMAF=0; AC_SUBST([REPLACE_FMAF]) REPLACE_FREXPF=0; AC_SUBST([REPLACE_FREXPF]) REPLACE_FREXP=0; AC_SUBST([REPLACE_FREXP]) REPLACE_FREXPL=0; AC_SUBST([REPLACE_FREXPL]) diff --git a/modules/fmaf b/modules/fmaf new file mode 100644 index 0000000000..fe80913e4c --- /dev/null +++ b/modules/fmaf @@ -0,0 +1,42 @@ +Description: +fmaf() function: fused multiply-add. + +Files: +lib/fmaf.c +lib/fma.c +lib/float+.h +m4/fmaf.m4 +m4/fegetround.m4 +m4/mathfunc.m4 + +Depends-on: +math +float [test $HAVE_FMAF = 0 || test $REPLACE_FMAF = 1] +stdbool [test $HAVE_FMAF = 0 || test $REPLACE_FMAF = 1] +verify [test $HAVE_FMAF = 0 || test $REPLACE_FMAF = 1] +isfinite [test $HAVE_FMAF = 0 || test $REPLACE_FMAF = 1] +integer_length [test $HAVE_FMAF = 0 || test $REPLACE_FMAF = 1] +frexpf [test $HAVE_FMAF = 0 || test $REPLACE_FMAF = 1] +ldexpf [test $HAVE_FMAF = 0 || test $REPLACE_FMAF = 1] + +configure.ac: +gl_FUNC_FMAF +if test $HAVE_FMAF = 0 || test $REPLACE_FMAF = 1; then + AC_LIBOBJ([fmaf]) + gl_PREREQ_FMAF +fi +gl_MATH_MODULE_INDICATOR([fmaf]) + +Makefile.am: + +Include: + + +Link: +$(FMAF_LIBM) + +License: +LGPL + +Maintainer: +Bruno Haible diff --git a/modules/math b/modules/math index 6b1958b754..2af9b685b7 100644 --- a/modules/math +++ b/modules/math @@ -51,6 +51,7 @@ math.h: math.in.h $(top_builddir)/config.status $(CXXDEFS_H) $(ARG_NONNULL_H) $( -e 's/@''GNULIB_FLOORF''@/$(GNULIB_FLOORF)/g' \ -e 's/@''GNULIB_FLOORL''@/$(GNULIB_FLOORL)/g' \ -e 's/@''GNULIB_FMA''@/$(GNULIB_FMA)/g' \ + -e 's/@''GNULIB_FMAF''@/$(GNULIB_FMAF)/g' \ -e 's/@''GNULIB_FMODF''@/$(GNULIB_FMODF)/g' \ -e 's/@''GNULIB_FREXPF''@/$(GNULIB_FREXPF)/g' \ -e 's/@''GNULIB_FREXP''@/$(GNULIB_FREXP)/g' \ @@ -105,6 +106,7 @@ math.h: math.in.h $(top_builddir)/config.status $(CXXDEFS_H) $(ARG_NONNULL_H) $( -e 's|@''HAVE_EXPL''@|$(HAVE_EXPL)|g' \ -e 's|@''HAVE_FABSF''@|$(HAVE_FABSF)|g' \ -e 's|@''HAVE_FMA''@|$(HAVE_FMA)|g' \ + -e 's|@''HAVE_FMAF''@|$(HAVE_FMAF)|g' \ -e 's|@''HAVE_FMODF''@|$(HAVE_FMODF)|g' \ -e 's|@''HAVE_FREXPF''@|$(HAVE_FREXPF)|g' \ -e 's|@''HAVE_ISNANF''@|$(HAVE_ISNANF)|g' \ @@ -157,6 +159,7 @@ math.h: math.in.h $(top_builddir)/config.status $(CXXDEFS_H) $(ARG_NONNULL_H) $( -e 's|@''REPLACE_FLOORF''@|$(REPLACE_FLOORF)|g' \ -e 's|@''REPLACE_FLOORL''@|$(REPLACE_FLOORL)|g' \ -e 's|@''REPLACE_FMA''@|$(REPLACE_FMA)|g' \ + -e 's|@''REPLACE_FMAF''@|$(REPLACE_FMAF)|g' \ -e 's|@''REPLACE_FREXPF''@|$(REPLACE_FREXPF)|g' \ -e 's|@''REPLACE_FREXP''@|$(REPLACE_FREXP)|g' \ -e 's|@''REPLACE_FREXPL''@|$(REPLACE_FREXPL)|g' \ -- 2.39.5