From 0c5e7922f80eae16b5a89233d5a8c0d1f88c18a7 Mon Sep 17 00:00:00 2001 From: Bruno Haible Date: Tue, 21 Dec 2010 18:30:16 +0100 Subject: [PATCH] New module 'trunc-ieee'. * modules/trunc-ieee: New file. * m4/trunc.m4 (gl_FUNC_TRUNC): If gl_FUNC_TRUNC_IEEE is also used, test whether trunc works according to ISO C 99 with IEC 60559. * m4/trunc-ieee.m4: New file. * lib/math.in.h (trunc): Replace if REPLACE_TRUNC is set. * m4/math_h.m4 (gl_MATH_H_DEFAULTS): Initialize REPLACE_TRUNC. * modules/math (Makefile.am): Substitute REPLACE_TRUNC. * modules/trunc-ieee-tests: New file. * tests/test-trunc-ieee.c: New file, based on tests/test-truncf-ieee.c. * tests/test-trunc1.c (main): Remove signbit tests. * modules/trunc-tests (Depends-on): Remove 'signbit'. * doc/posix-functions/trunc.texi: Mention the new module. --- ChangeLog | 16 ++++++++++++++++ doc/posix-functions/trunc.texi | 12 ++++++++++-- lib/math.in.h | 12 ++++++++++-- m4/math_h.m4 | 3 ++- m4/trunc-ieee.m4 | 15 +++++++++++++++ m4/trunc.m4 | 39 ++++++++++++++++++++++++++++++++++++++- modules/math | 1 + modules/trunc-ieee | 27 +++++++++++++++++++++++++++ modules/trunc-ieee-tests | 15 +++++++++++++++ modules/trunc-tests | 1 - tests/test-trunc-ieee.c | 32 ++++++++++++++++++++++++++++++++ tests/test-trunc1.c | 2 -- 12 files changed, 166 insertions(+), 9 deletions(-) create mode 100644 m4/trunc-ieee.m4 create mode 100644 modules/trunc-ieee create mode 100644 modules/trunc-ieee-tests create mode 100644 tests/test-trunc-ieee.c diff --git a/ChangeLog b/ChangeLog index f00d5d1e6..22c775296 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,5 +1,21 @@ 2010-12-21 Bruno Haible + New module 'trunc-ieee'. + * modules/trunc-ieee: New file. + * m4/trunc.m4 (gl_FUNC_TRUNC): If gl_FUNC_TRUNC_IEEE is also used, test + whether trunc works according to ISO C 99 with IEC 60559. + * m4/trunc-ieee.m4: New file. + * lib/math.in.h (trunc): Replace if REPLACE_TRUNC is set. + * m4/math_h.m4 (gl_MATH_H_DEFAULTS): Initialize REPLACE_TRUNC. + * modules/math (Makefile.am): Substitute REPLACE_TRUNC. + * modules/trunc-ieee-tests: New file. + * tests/test-trunc-ieee.c: New file, based on tests/test-truncf-ieee.c. + * tests/test-trunc1.c (main): Remove signbit tests. + * modules/trunc-tests (Depends-on): Remove 'signbit'. + * doc/posix-functions/trunc.texi: Mention the new module. + +2010-12-21 Bruno Haible + New module 'ceil-ieee'. * modules/ceil-ieee: New file. * m4/ceil.m4 (gl_FUNC_CEIL): Require gl_MATH_H_DEFAULTS. If diff --git a/doc/posix-functions/trunc.texi b/doc/posix-functions/trunc.texi index 10278e320..3379cb6f5 100644 --- a/doc/posix-functions/trunc.texi +++ b/doc/posix-functions/trunc.texi @@ -4,9 +4,9 @@ POSIX specification:@* @url{http://www.opengroup.org/onlinepubs/9699919799/functions/trunc.html} -Gnulib module: trunc +Gnulib module: trunc or trunc-ieee -Portability problems fixed by Gnulib: +Portability problems fixed by either Gnulib module @code{trunc} or @code{trunc-ieee}: @itemize @item This function is missing on some platforms: @@ -16,6 +16,14 @@ This function is not declared (without @code{-D_GNU_SOURCE}) on some platforms: glibc 2.8. @end itemize +Portability problems fixed by Gnulib module @code{trunc-ieee}: +@itemize +@item +This function returns a positive zero for a minus zero argument +on some platforms: +OSF/1 5.1. +@end itemize + Portability problems not fixed by Gnulib: @itemize @end itemize diff --git a/lib/math.in.h b/lib/math.in.h index c933f519c..e3dc54b43 100644 --- a/lib/math.in.h +++ b/lib/math.in.h @@ -541,10 +541,18 @@ _GL_WARN_ON_USE (truncf, "truncf is unportable - " #endif #if @GNULIB_TRUNC@ -# if !@HAVE_DECL_TRUNC@ +# if @REPLACE_TRUNC@ +# if !(defined __cplusplus && defined GNULIB_NAMESPACE) +# define trunc rpl_trunc +# endif +_GL_FUNCDECL_RPL (trunc, double, (double x)); +_GL_CXXALIAS_RPL (trunc, double, (double x)); +# else +# if !@HAVE_DECL_TRUNC@ _GL_FUNCDECL_SYS (trunc, double, (double x)); -# endif +# endif _GL_CXXALIAS_SYS (trunc, double, (double x)); +# endif _GL_CXXALIASWARN (trunc); #elif defined GNULIB_POSIXCHECK # undef trunc diff --git a/m4/math_h.m4 b/m4/math_h.m4 index cbe57b213..c25b722c1 100644 --- a/m4/math_h.m4 +++ b/m4/math_h.m4 @@ -1,4 +1,4 @@ -# math_h.m4 serial 24 +# math_h.m4 serial 25 dnl Copyright (C) 2007-2010 Free Software Foundation, Inc. dnl This file is free software; the Free Software Foundation dnl gives unlimited permission to copy and/or distribute it, @@ -140,6 +140,7 @@ AC_DEFUN([gl_MATH_H_DEFAULTS], REPLACE_ROUNDL=0; AC_SUBST([REPLACE_ROUNDL]) REPLACE_SIGNBIT=0; AC_SUBST([REPLACE_SIGNBIT]) REPLACE_SIGNBIT_USING_GCC=0; AC_SUBST([REPLACE_SIGNBIT_USING_GCC]) + REPLACE_TRUNC=0; AC_SUBST([REPLACE_TRUNC]) REPLACE_TRUNCF=0; AC_SUBST([REPLACE_TRUNCF]) REPLACE_TRUNCL=0; AC_SUBST([REPLACE_TRUNCL]) ]) diff --git a/m4/trunc-ieee.m4 b/m4/trunc-ieee.m4 new file mode 100644 index 000000000..a2f7810cd --- /dev/null +++ b/m4/trunc-ieee.m4 @@ -0,0 +1,15 @@ +# trunc-ieee.m4 serial 1 +dnl Copyright (C) 2010 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. + +dnl This macro is in a separate file (not in trunc.m4 and not inlined in the +dnl module description), so that gl_FUNC_TRUNC can test whether 'aclocal' has +dnl found uses of this macro. + +AC_DEFUN([gl_FUNC_TRUNC_IEEE], +[ + m4_divert_text([INIT_PREPARE], [gl_trunc_required=ieee]) + AC_REQUIRE([gl_FUNC_TRUNC]) +]) diff --git a/m4/trunc.m4 b/m4/trunc.m4 index 0654d9cd2..6197df521 100644 --- a/m4/trunc.m4 +++ b/m4/trunc.m4 @@ -1,4 +1,4 @@ -# trunc.m4 serial 4 +# trunc.m4 serial 5 dnl Copyright (C) 2007, 2010 Free Software Foundation, Inc. dnl This file is free software; the Free Software Foundation dnl gives unlimited permission to copy and/or distribute it, @@ -6,6 +6,7 @@ dnl with or without modifications, as long as this notice is preserved. AC_DEFUN([gl_FUNC_TRUNC], [ + m4_divert_text([DEFAULTS], [gl_trunc_required=plain]) AC_REQUIRE([gl_MATH_H_DEFAULTS]) dnl Persuade glibc to declare trunc(). AC_REQUIRE([gl_USE_SYSTEM_EXTENSIONS]) @@ -40,8 +41,44 @@ AC_DEFUN([gl_FUNC_TRUNC], if test "$TRUNC_LIBM" = "?"; then TRUNC_LIBM= fi + m4_ifdef([gl_FUNC_TRUNC_IEEE], [ + if test $gl_trunc_required = ieee && test $REPLACE_TRUNC = 0; then + AC_CACHE_CHECK([whether trunc works according to ISO C 99 with IEC 60559], + [gl_cv_func_trunc_ieee], + [ + save_LIBS="$LIBS" + LIBS="$LIBS $TRUNC_LIBM" + AC_RUN_IFELSE( + [AC_LANG_SOURCE([[ +#ifndef __NO_MATH_INLINES +# define __NO_MATH_INLINES 1 /* for glibc */ +#endif +#include +]gl_DOUBLE_MINUS_ZERO_CODE[ +]gl_DOUBLE_SIGNBIT_CODE[ +int main() +{ + /* Test whether trunc (-0.0) is -0.0. */ + if (signbitd (minus_zerod) && !signbitd (trunc (minus_zerod))) + return 1; + return 0; +} + ]])], + [gl_cv_func_trunc_ieee=yes], + [gl_cv_func_trunc_ieee=no], + [gl_cv_func_trunc_ieee="guessing no"]) + LIBS="$save_LIBS" + ]) + case "$gl_cv_func_trunc_ieee" in + *yes) ;; + *) REPLACE_TRUNC=1 ;; + esac + fi + ]) else HAVE_DECL_TRUNC=0 + fi + if test $HAVE_DECL_TRUNC = 0 || test $REPLACE_TRUNC = 1; then AC_LIBOBJ([trunc]) TRUNC_LIBM= fi diff --git a/modules/math b/modules/math index a11aeb820..7ac78acf9 100644 --- a/modules/math +++ b/modules/math @@ -111,6 +111,7 @@ math.h: math.in.h $(CXXDEFS_H) $(ARG_NONNULL_H) $(WARN_ON_USE_H) -e 's|@''REPLACE_ROUNDL''@|$(REPLACE_ROUNDL)|g' \ -e 's|@''REPLACE_SIGNBIT''@|$(REPLACE_SIGNBIT)|g' \ -e 's|@''REPLACE_SIGNBIT_USING_GCC''@|$(REPLACE_SIGNBIT_USING_GCC)|g' \ + -e 's|@''REPLACE_TRUNC''@|$(REPLACE_TRUNC)|g' \ -e 's|@''REPLACE_TRUNCF''@|$(REPLACE_TRUNCF)|g' \ -e 's|@''REPLACE_TRUNCL''@|$(REPLACE_TRUNCL)|g' \ -e '/definitions of _GL_FUNCDECL_RPL/r $(CXXDEFS_H)' \ diff --git a/modules/trunc-ieee b/modules/trunc-ieee new file mode 100644 index 000000000..612716e14 --- /dev/null +++ b/modules/trunc-ieee @@ -0,0 +1,27 @@ +Description: +trunc() function according to ISO C 99 with IEC 60559. + +Files: +m4/trunc-ieee.m4 +m4/minus-zero.m4 +m4/signbit.m4 + +Depends-on: +trunc + +configure.ac: +gl_FUNC_TRUNC_IEEE + +Makefile.am: + +Include: + + +Link: +$(TRUNC_LIBM) + +License: +LGPL + +Maintainer: +Bruno Haible diff --git a/modules/trunc-ieee-tests b/modules/trunc-ieee-tests new file mode 100644 index 000000000..80bafef88 --- /dev/null +++ b/modules/trunc-ieee-tests @@ -0,0 +1,15 @@ +Files: +tests/test-trunc-ieee.c +tests/minus-zero.h +tests/macros.h + +Depends-on: +float +signbit + +configure.ac: + +Makefile.am: +TESTS += test-trunc-ieee +check_PROGRAMS += test-trunc-ieee +test_trunc_ieee_LDADD = $(LDADD) @TRUNC_LIBM@ diff --git a/modules/trunc-tests b/modules/trunc-tests index 30b9a6c36..6c254e069 100644 --- a/modules/trunc-tests +++ b/modules/trunc-tests @@ -9,7 +9,6 @@ tests/macros.h Depends-on: float isnand-nolibm -signbit stdbool stdint diff --git a/tests/test-trunc-ieee.c b/tests/test-trunc-ieee.c new file mode 100644 index 000000000..c0fc0f262 --- /dev/null +++ b/tests/test-trunc-ieee.c @@ -0,0 +1,32 @@ +/* Test of rounding towards zero. + Copyright (C) 2010 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 . */ + +#include + +#include + +#include "minus-zero.h" +#include "macros.h" + +int +main () +{ + /* Zero. */ + ASSERT (!signbit (trunc (0.0))); + ASSERT (!!signbit (trunc (minus_zerod)) == !!signbit (minus_zerod)); + + return 0; +} diff --git a/tests/test-trunc1.c b/tests/test-trunc1.c index 5587a41c0..69530b875 100644 --- a/tests/test-trunc1.c +++ b/tests/test-trunc1.c @@ -33,9 +33,7 @@ main () { /* Zero. */ ASSERT (trunc (0.0) == 0.0); - ASSERT (!signbit (trunc (0.0))); ASSERT (trunc (minus_zerod) == 0.0); - ASSERT (!!signbit (minus_zerod) == !!signbit (trunc (minus_zerod))); /* Positive numbers. */ ASSERT (trunc (0.3) == 0.0); ASSERT (trunc (0.7) == 0.0); -- 2.11.0