From ec0af5d579654eb83aafc76940412b3bc106d558 Mon Sep 17 00:00:00 2001 From: Bruno Haible Date: Thu, 4 Oct 2007 03:22:14 +0200 Subject: [PATCH] New module 'trunc'. --- doc/functions/trunc.texi | 8 +++--- lib/math.in.h | 13 +++++++++ lib/trunc.c | 68 ++++++++++++++++++++++++++++++++++++++++++++++++ m4/math_h.m4 | 4 ++- m4/trunc.m4 | 46 ++++++++++++++++++++++++++++++++ modules/math | 2 ++ modules/trunc | 29 +++++++++++++++++++++ 7 files changed, 165 insertions(+), 5 deletions(-) create mode 100644 lib/trunc.c create mode 100644 m4/trunc.m4 create mode 100644 modules/trunc diff --git a/doc/functions/trunc.texi b/doc/functions/trunc.texi index ecb17e575..42e1b9dd7 100644 --- a/doc/functions/trunc.texi +++ b/doc/functions/trunc.texi @@ -4,15 +4,15 @@ POSIX specification: @url{http://www.opengroup.org/susv3xsh/trunc.html} -Gnulib module: --- +Gnulib module: trunc Portability problems fixed by Gnulib: @itemize +@item +This function is missing on some platforms: +FreeBSD 5.2.1, NetBSD 3.0, OpenBSD 3.8, Solaris 9, Interix 3.5. @end itemize Portability problems not fixed by Gnulib: @itemize -@item -This function is missing on some platforms: -FreeBSD 5.2.1, NetBSD 3.0, OpenBSD 3.8, Solaris 9, Interix 3.5. @end itemize diff --git a/lib/math.in.h b/lib/math.in.h index 5138c95c7..2b6c11be1 100644 --- a/lib/math.in.h +++ b/lib/math.in.h @@ -211,6 +211,19 @@ extern long double tanl (long double x); tanl (x)) #endif +#if @GNULIB_TRUNC@ +# if !@HAVE_DECL_TRUNC@ +# define trunc rpl_trunc +extern double trunc (double x); +# endif +#elif defined GNULIB_POSIXCHECK +# undef trunc +# define trunc(x) \ + (GL_LINK_WARNING ("trunc is unportable - " \ + "use gnulib module trunc for portability"), \ + trunc (x)) +#endif + #if @GNULIB_SIGNBIT@ # if @REPLACE_SIGNBIT@ diff --git a/lib/trunc.c b/lib/trunc.c new file mode 100644 index 000000000..b74021ee1 --- /dev/null +++ b/lib/trunc.c @@ -0,0 +1,68 @@ +/* Round towards zero. + Copyright (C) 2007 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 2, 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, write to the Free Software Foundation, + Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ + +/* Written by Bruno Haible , 2007. */ + +#include + +/* Specification. */ +#include + +#include + +/* 2^(DBL_MANT_DIG-1). */ +static const double TWO_MANT_DIG = + /* Assume DBL_MANT_DIG <= 4 * 31. + Use the identity + n = floor(n/4) + floor((n+1)/4) + floor((n+2)/4) + floor((n+3)/4). */ + (double) (1U << ((DBL_MANT_DIG - 1) / 4)) + * (double) (1U << ((DBL_MANT_DIG - 1 + 1) / 4)) + * (double) (1U << ((DBL_MANT_DIG - 1 + 2) / 4)) + * (double) (1U << ((DBL_MANT_DIG - 1 + 3) / 4)); + +double +trunc (double x) +{ + /* The use of 'volatile' guarantees that excess precision bits are dropped + at each addition step and before the following comparison at the caller's + site. It is necessary on x86 systems where double-floats are not IEEE + compliant by default, to avoid that the results become platform and compiler + option dependent. 'volatile' is a portable alternative to gcc's + -ffloat-store option. */ + volatile double y = x; + volatile double z = y; + + if (z > 0) + { + /* Round to the next integer (nearest or up or down, doesn't matter). */ + z += TWO_MANT_DIG; + z -= TWO_MANT_DIG; + /* Enforce rounding down. */ + if (z > y) + z -= 1.0; + } + else if (z < 0) + { + /* Round to the next integer (nearest or up or down, doesn't matter). */ + z -= TWO_MANT_DIG; + z += TWO_MANT_DIG; + /* Enforce rounding up. */ + if (z < y) + z += 1.0; + } + return z; +} diff --git a/m4/math_h.m4 b/m4/math_h.m4 index 21f3449c6..e57883a9b 100644 --- a/m4/math_h.m4 +++ b/m4/math_h.m4 @@ -1,4 +1,4 @@ -# math_h.m4 serial 5 +# math_h.m4 serial 6 dnl Copyright (C) 2007 Free Software Foundation, Inc. dnl This file is free software; the Free Software Foundation dnl gives unlimited permission to copy and/or distribute it, @@ -24,6 +24,7 @@ AC_DEFUN([gl_MATH_H_DEFAULTS], GNULIB_LDEXPL=0; AC_SUBST([GNULIB_LDEXPL]) GNULIB_MATHL=0; AC_SUBST([GNULIB_MATHL]) GNULIB_SIGNBIT=0; AC_SUBST([GNULIB_SIGNBIT]) + GNULIB_TRUNC=0; AC_SUBST([GNULIB_TRUNC]) dnl Assume proper GNU behavior unless another module says otherwise. HAVE_DECL_ACOSL=1; AC_SUBST([HAVE_DECL_ACOSL]) HAVE_DECL_ASINL=1; AC_SUBST([HAVE_DECL_ASINL]) @@ -38,6 +39,7 @@ AC_DEFUN([gl_MATH_H_DEFAULTS], HAVE_DECL_SINL=1; AC_SUBST([HAVE_DECL_SINL]) HAVE_DECL_SQRTL=1; AC_SUBST([HAVE_DECL_SQRTL]) HAVE_DECL_TANL=1; AC_SUBST([HAVE_DECL_TANL]) + HAVE_DECL_TRUNC=1; AC_SUBST([HAVE_DECL_TRUNC]) REPLACE_FREXP=0; AC_SUBST([REPLACE_FREXP]) REPLACE_FREXPL=0; AC_SUBST([REPLACE_FREXPL]) REPLACE_LDEXPL=0; AC_SUBST([REPLACE_LDEXPL]) diff --git a/m4/trunc.m4 b/m4/trunc.m4 new file mode 100644 index 000000000..58db4a19d --- /dev/null +++ b/m4/trunc.m4 @@ -0,0 +1,46 @@ +# trunc.m4 serial 1 +dnl Copyright (C) 2007 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_TRUNC], +[ + AC_REQUIRE([gl_MATH_H_DEFAULTS]) + dnl Test whether trunc() is declared. + AC_CHECK_DECLS([trunc], , , [#include ]) + if test "$ac_cv_have_decl_trunc" = yes; then + dnl Test whether trunc() can be used without libm. + TRUNC_LIBM=? + AC_TRY_LINK([ + #ifndef __NO_MATH_INLINES + # define __NO_MATH_INLINES 1 /* for glibc */ + #endif + #include + double x;], + [x = trunc(x);], + [TRUNC_LIBM=]) + if test "$TRUNC_LIBM" = "?"; then + save_LIBS="$LIBS" + LIBS="$LIBS -lm" + AC_TRY_LINK([ + #ifndef __NO_MATH_INLINES + # define __NO_MATH_INLINES 1 /* for glibc */ + #endif + #include + double x;], + [x = trunc(x);], + [TRUNC_LIBM="-lm"]) + LIBS="$save_LIBS" + fi + if test "$TRUNC_LIBM" = "?"; then + TRUNC_LIBM= + fi + else + HAVE_DECL_TRUNC=0 + AC_LIBOBJ([trunc]) + TRUNC_LIBM= + fi + AC_SUBST([HAVE_DECL_TRUNC]) + AC_SUBST([TRUNC_LIBM]) +]) diff --git a/modules/math b/modules/math index ffb13da82..581899916 100644 --- a/modules/math +++ b/modules/math @@ -27,6 +27,7 @@ math.h: math.in.h -e 's|@''GNULIB_LDEXPL''@|$(GNULIB_LDEXPL)|g' \ -e 's|@''GNULIB_MATHL''@|$(GNULIB_MATHL)|g' \ -e 's|@''GNULIB_SIGNBIT''@|$(GNULIB_SIGNBIT)|g' \ + -e 's|@''GNULIB_TRUNC''@|$(GNULIB_TRUNC)|g' \ -e 's|@''HAVE_DECL_ACOSL''@|$(HAVE_DECL_ACOSL)|g' \ -e 's|@''HAVE_DECL_ASINL''@|$(HAVE_DECL_ASINL)|g' \ -e 's|@''HAVE_DECL_ATANL''@|$(HAVE_DECL_ATANL)|g' \ @@ -40,6 +41,7 @@ math.h: math.in.h -e 's|@''HAVE_DECL_SINL''@|$(HAVE_DECL_SINL)|g' \ -e 's|@''HAVE_DECL_SQRTL''@|$(HAVE_DECL_SQRTL)|g' \ -e 's|@''HAVE_DECL_TANL''@|$(HAVE_DECL_TANL)|g' \ + -e 's|@''HAVE_DECL_TRUNC''@|$(HAVE_DECL_TRUNC)|g' \ -e 's|@''REPLACE_FREXP''@|$(REPLACE_FREXP)|g' \ -e 's|@''REPLACE_FREXPL''@|$(REPLACE_FREXPL)|g' \ -e 's|@''REPLACE_LDEXPL''@|$(REPLACE_LDEXPL)|g' \ diff --git a/modules/trunc b/modules/trunc new file mode 100644 index 000000000..8153edafc --- /dev/null +++ b/modules/trunc @@ -0,0 +1,29 @@ +Description: +trunc() function: round towards zero. + +Files: +lib/trunc.c +m4/trunc.m4 + +Depends-on: +math +float + +configure.ac: +gl_FUNC_TRUNC +gl_MATH_MODULE_INDICATOR([trunc]) + +Makefile.am: + +Include: + + +Link: +$(TRUNC_LIBM) + +License: +LGPL + +Maintainer: +Bruno Haible + -- 2.11.0