changeset 9414:1c5d0b856e8b

Implement 'isfinite' module.
author Ben Pfaff <blp@cs.stanford.edu>
date Sat, 27 Oct 2007 22:12:07 -0700
parents 9572edc3cc57
children 8ea12c7d7d59
files ChangeLog MODULES.html.sh doc/functions/isfinite.texi lib/isfinite.c lib/math.in.h m4/isfinite.m4 m4/math_h.m4 modules/isfinite modules/isfinite-tests modules/math tests/test-isfinite.c
diffstat 11 files changed, 331 insertions(+), 3 deletions(-) [+]
line wrap: on
line diff
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,18 @@
+2007-10-27  Ben Pfaff  <blp@gnu.org>
+
+	* lib/math.in.h: Define isfinite macro and prototypes for
+	gl_isfinitef, gl_isfinited, gl_isfinitel if we are providing
+	implementations.
+	* m4/math_h.m4: New substitutions for isfinite module.
+	* lib/isfinite.c: New file.
+	* m4/isfinite.m4: New file.
+	* modules/math: Replace isfinite-related @VARS@ in math.in.h.
+	* modules/isfinite: New file.
+	* modules/isfinite-tests: New file.
+	* tests/tests-isfinite.c: New file.
+	* doc/functions/isfinite.texi: Mention isfinite module.
+	* MODULES.html.sh: Mention new module.
+
 2007-10-27  Ben Pfaff  <blp@gnu.org>
 
 	Ralf Wildenhues reported that Tru64 4.0D declares the round
--- a/MODULES.html.sh
+++ b/MODULES.html.sh
@@ -1970,6 +1970,7 @@
   func_module frexp
   func_module frexpl
   func_module frexpl-nolibm
+  func_module isfinite
   func_module isnan-nolibm
   func_module isnanf-nolibm
   func_module isnanl
--- a/doc/functions/isfinite.texi
+++ b/doc/functions/isfinite.texi
@@ -4,15 +4,18 @@
 
 POSIX specification: @url{http://www.opengroup.org/susv3xsh/isfinite.html}
 
-Gnulib module: ---
+Gnulib module: isfinite
 
 Portability problems fixed by Gnulib:
 @itemize
+@item
+This macro is missing on some platforms:
+MacOS X 10.3, OpenBSD 3.8, AIX 5.1, IRIX 6.5, OSF/1 5.1, Solaris 10, Interix 3.5.
 @end itemize
 
 Portability problems not fixed by Gnulib:
 @itemize
 @item
-This function is missing on some platforms:
-MacOS X 10.3, OpenBSD 3.8, AIX 5.1, IRIX 6.5, OSF/1 5.1, Solaris 10, Interix 3.5.
+It is implementation-dependent whether @code{isfinite} raises an
+exception given a signaling NaN operand.
 @end itemize
new file mode 100644
--- /dev/null
+++ b/lib/isfinite.c
@@ -0,0 +1,39 @@
+/* Test for finite value (zero, subnormal, or normal, and not infinite or NaN).
+   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 Ben Pfaff <blp@gnu.org>, 2007. */
+
+#include <config.h>
+
+#include "isnan.h"
+#include "isnanf.h"
+#include "isnanl-nolibm.h"
+
+int gl_isfinitef (float x)
+{
+  return !isnanf (x) && x - x == 0.f;
+}
+
+int gl_isfinited (double x)
+{
+  return !isnan (x) && x - x == 0.;
+}
+
+int gl_isfinitel (long double x)
+{
+  return !isnanl (x) && x - x == 0.L;
+}
--- a/lib/math.in.h
+++ b/lib/math.in.h
@@ -337,6 +337,20 @@
      truncl (x))
 #endif
 
+#if @GNULIB_ISFINITE@
+# if !@HAVE_DECL_ISFINITE@
+extern int gl_isfinitef (float x);
+extern int gl_isfinited (double x);
+extern int gl_isfinitel (long double x);
+#  undef isfinite
+#  define isfinite(x) \
+   (sizeof (x) == sizeof (long double) ? gl_isfinitel (x) : \
+    sizeof (x) == sizeof (double) ? gl_isfinited (x) : \
+    gl_isfinitef (x))
+# endif
+#elif defined GNULIB_POSIXCHECK
+  /* How to override a macro?  */
+#endif
 
 #if @GNULIB_SIGNBIT@
 # if @REPLACE_SIGNBIT@
new file mode 100644
--- /dev/null
+++ b/m4/isfinite.m4
@@ -0,0 +1,24 @@
+# isfinite.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_ISFINITE],
+[
+  AC_REQUIRE([gl_MATH_H_DEFAULTS])
+  dnl Persuade glibc <math.h> to declare isfinite.
+  AC_REQUIRE([gl_USE_SYSTEM_EXTENSIONS])
+  AC_CHECK_DECLS([isfinite], , , [#include <math.h>])
+  if test "$ac_cv_have_decl_isfinite" = yes; then
+    gl_CHECK_MATH_LIB([ISFINITE_LIBM], [x = isfinite (x);])
+  fi
+  if test "$ac_cv_have_decl_isfinite" != yes ||
+     test "$ISFINITE_LIBM" = missing; then
+    ISFINITE_LIBM=
+    HAVE_DECL_ISFINITE=0
+    AC_LIBOBJ([isfinite])
+  fi
+  AC_SUBST([HAVE_DECL_ISFINITE])
+  AC_SUBST([ISFINITE_LIBM])
+])
--- a/m4/math_h.m4
+++ b/m4/math_h.m4
@@ -34,6 +34,7 @@
   GNULIB_TRUNC=0;   AC_SUBST([GNULIB_TRUNC])
   GNULIB_TRUNCF=0;  AC_SUBST([GNULIB_TRUNCF])
   GNULIB_TRUNCL=0;  AC_SUBST([GNULIB_TRUNCL])
+  GNULIB_ISFINITE=0; AC_SUBST([GNULIB_ISFINITE])
   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])
@@ -56,6 +57,7 @@
   HAVE_DECL_TRUNC=1;  AC_SUBST([HAVE_DECL_TRUNC])
   HAVE_DECL_TRUNCF=1; AC_SUBST([HAVE_DECL_TRUNCF])
   HAVE_DECL_TRUNCL=1; AC_SUBST([HAVE_DECL_TRUNCL])
+  HAVE_DECL_ISFINITE=1; AC_SUBST([HAVE_DECL_ISFINITE])
   REPLACE_FREXP=0;    AC_SUBST([REPLACE_FREXP])
   REPLACE_FREXPL=0;   AC_SUBST([REPLACE_FREXPL])
   REPLACE_LDEXPL=0;   AC_SUBST([REPLACE_LDEXPL])
new file mode 100644
--- /dev/null
+++ b/modules/isfinite
@@ -0,0 +1,29 @@
+Description:
+isfinite macro: test for finite value (zero, subnormal, or normal, and not infinite or NaN).
+
+Files:
+lib/isfinite.c
+m4/isfinite.m4
+m4/check-math-lib.m4
+
+Depends-on:
+isnan-nolibm
+isnanf-nolibm
+isnanl-nolibm
+math
+extensions
+
+configure.ac:
+gl_ISFINITE
+gl_MATH_MODULE_INDICATOR([isfinite])
+
+Makefile.am:
+
+Include:
+<math.h>
+
+License:
+GPL
+
+Maintainer:
+Ben Pfaff
new file mode 100644
--- /dev/null
+++ b/modules/isfinite-tests
@@ -0,0 +1,18 @@
+Files:
+tests/test-isfinite.c
+
+Depends-on:
+float
+
+configure.ac:
+gl_FLOAT_EXPONENT_LOCATION
+gl_DOUBLE_EXPONENT_LOCATION
+gl_LONG_DOUBLE_EXPONENT_LOCATION
+
+Makefile.am:
+TESTS += test-isfinite
+check_PROGRAMS += test-isfinite
+test_isfinite_LDADD = $(LDADD) @ISFINITE_LIBM@
+
+License:
+GPL
--- a/modules/math
+++ b/modules/math
@@ -37,6 +37,7 @@
 	      -e 's|@''GNULIB_TRUNC''@|$(GNULIB_TRUNC)|g' \
 	      -e 's|@''GNULIB_TRUNCF''@|$(GNULIB_TRUNCF)|g' \
 	      -e 's|@''GNULIB_TRUNCL''@|$(GNULIB_TRUNCL)|g' \
+	      -e 's|@''GNULIB_ISFINITE''@|$(GNULIB_ISFINITE)|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' \
@@ -58,6 +59,7 @@
 	      -e 's|@''HAVE_DECL_TRUNC''@|$(HAVE_DECL_TRUNC)|g' \
 	      -e 's|@''HAVE_DECL_TRUNCF''@|$(HAVE_DECL_TRUNCF)|g' \
 	      -e 's|@''HAVE_DECL_TRUNCL''@|$(HAVE_DECL_TRUNCL)|g' \
+	      -e 's|@''HAVE_DECL_ISFINITE''@|$(HAVE_DECL_ISFINITE)|g' \
 	      -e 's|@''REPLACE_FREXP''@|$(REPLACE_FREXP)|g' \
 	      -e 's|@''REPLACE_FREXPL''@|$(REPLACE_FREXPL)|g' \
 	      -e 's|@''REPLACE_LDEXPL''@|$(REPLACE_LDEXPL)|g' \
new file mode 100644
--- /dev/null
+++ b/tests/test-isfinite.c
@@ -0,0 +1,181 @@
+/* Test of isfinite() substitute.
+   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 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 <http://www.gnu.org/licenses/>.  */
+
+/* Written by Ben Pfaff, 2007, using Bruno Haible's code as a
+   template. */
+
+#include <config.h>
+
+#include <float.h>
+#include <limits.h>
+#include <math.h>
+
+#include <stdio.h>
+#include <stdlib.h>
+
+#define ASSERT(expr) \
+  do									     \
+    {									     \
+      if (!(expr))							     \
+        {								     \
+          fprintf (stderr, "%s:%d: assertion failed\n", __FILE__, __LINE__); \
+          abort ();							     \
+        }								     \
+    }									     \
+  while (0)
+
+float zerof = 0.0f;
+double zerod = 0.0;
+long double zerol = 0.0L;
+
+static void
+test_isfinitef ()
+{
+  /* Zero. */
+  ASSERT (isfinite (0.0f));
+  /* Subnormal values. */
+  ASSERT (isfinite (FLT_MIN / 2));
+  ASSERT (isfinite (-FLT_MIN / 2));
+  /* Finite values.  */
+  ASSERT (isfinite (3.141f));
+  ASSERT (isfinite (3.141e30f));
+  ASSERT (isfinite (3.141e-30f));
+  ASSERT (isfinite (-2.718f));
+  ASSERT (isfinite (-2.718e30f));
+  ASSERT (isfinite (-2.718e-30f));
+  /* Infinite values.  */
+  ASSERT (!isfinite (1.0f / 0.0f));
+  ASSERT (!isfinite (-1.0f / 0.0f));
+  /* Quiet NaN.  */
+  ASSERT (!isfinite (zerof / zerof));
+#if defined FLT_EXPBIT0_WORD && defined FLT_EXPBIT0_BIT
+  /* Signalling NaN.  */
+  {
+    #define NWORDS \
+      ((sizeof (float) + sizeof (unsigned int) - 1) / sizeof (unsigned int))
+    typedef union { float value; unsigned int word[NWORDS]; } memory_float;
+    memory_float m;
+    m.value = zerof / zerof;
+# if FLT_EXPBIT0_BIT > 0
+    m.word[FLT_EXPBIT0_WORD] ^= (unsigned int) 1 << (FLT_EXPBIT0_BIT - 1);
+# else
+    m.word[FLT_EXPBIT0_WORD + (FLT_EXPBIT0_WORD < NWORDS / 2 ? 1 : - 1)]
+      ^= (unsigned int) 1 << (sizeof (unsigned int) * CHAR_BIT - 1);
+# endif
+    if (FLT_EXPBIT0_WORD < NWORDS / 2)
+      m.word[FLT_EXPBIT0_WORD + 1] |= (unsigned int) 1 << FLT_EXPBIT0_BIT;
+    else
+      m.word[0] |= (unsigned int) 1;
+    ASSERT (!isfinite (m.value));
+    #undef NWORDS
+  }
+#endif
+}
+
+static void
+test_isfinited ()
+{
+  /* Zero. */
+  ASSERT (isfinite (0.0));
+  /* Subnormal values. */
+  ASSERT (isfinite (DBL_MIN / 2));
+  ASSERT (isfinite (-DBL_MIN / 2));
+  /* Finite values. */
+  ASSERT (isfinite (3.141));
+  ASSERT (isfinite (3.141e30));
+  ASSERT (isfinite (3.141e-30));
+  ASSERT (isfinite (-2.718));
+  ASSERT (isfinite (-2.718e30));
+  ASSERT (isfinite (-2.718e-30));
+  /* Infinite values.  */
+  ASSERT (!isfinite (1.0 / 0.0));
+  ASSERT (!isfinite (-1.0 / 0.0));
+  /* Quiet NaN.  */
+  ASSERT (!isfinite (zerod / zerod));
+#if defined DBL_EXPBIT0_WORD && defined DBL_EXPBIT0_BIT
+  /* Signalling NaN.  */
+  {
+    #define NWORDS \
+      ((sizeof (double) + sizeof (unsigned int) - 1) / sizeof (unsigned int))
+    typedef union { double value; unsigned int word[NWORDS]; } memory_double;
+    memory_double m;
+    m.value = zerod / zerod;
+# if DBL_EXPBIT0_BIT > 0
+    m.word[DBL_EXPBIT0_WORD] ^= (unsigned int) 1 << (DBL_EXPBIT0_BIT - 1);
+# else
+    m.word[DBL_EXPBIT0_WORD + (DBL_EXPBIT0_WORD < NWORDS / 2 ? 1 : - 1)]
+      ^= (unsigned int) 1 << (sizeof (unsigned int) * CHAR_BIT - 1);
+# endif
+    m.word[DBL_EXPBIT0_WORD + (DBL_EXPBIT0_WORD < NWORDS / 2 ? 1 : - 1)]
+      |= (unsigned int) 1 << DBL_EXPBIT0_BIT;
+    ASSERT (!isfinite (m.value));
+    #undef NWORDS
+  }
+#endif
+}
+
+static void
+test_isfinitel ()
+{
+  /* Zero. */
+  ASSERT (isfinite (0.0L));
+  /* Subnormal values. */
+  ASSERT (isfinite (LDBL_MIN / 2));
+  ASSERT (isfinite (-LDBL_MIN / 2));
+  /* Finite values. */
+  ASSERT (isfinite (3.141L));
+  ASSERT (isfinite (3.141e30L));
+  ASSERT (isfinite (3.141e-30L));
+  ASSERT (isfinite (-2.718L));
+  ASSERT (isfinite (-2.718e30L));
+  ASSERT (isfinite (-2.718e-30L));
+  /* Infinite values.  */
+  ASSERT (!isfinite (1.0L / 0.0L));
+  ASSERT (!isfinite (-1.0L / 0.0L));
+  /* Quiet NaN.  */
+  ASSERT (!isfinite (zerol / zerol));
+#if defined LDBL_EXPBIT0_WORD && defined LDBL_EXPBIT0_BIT
+  /* A bit pattern that is different from a Quiet NaN.  With a bit of luck,
+     it's a Signalling NaN.  */
+  {
+    #define NWORDS \
+      ((sizeof (long double) + sizeof (unsigned int) - 1) / sizeof (unsigned int))
+    typedef union { unsigned int word[NWORDS]; long double value; }
+          memory_long_double;
+    memory_long_double m;
+    m.value = zerol / zerol;
+# if LDBL_EXPBIT0_BIT > 0
+    m.word[LDBL_EXPBIT0_WORD] ^= (unsigned int) 1 << (LDBL_EXPBIT0_BIT - 1);
+# else
+    m.word[LDBL_EXPBIT0_WORD + (LDBL_EXPBIT0_WORD < NWORDS / 2 ? 1 : - 1)]
+      ^= (unsigned int) 1 << (sizeof (unsigned int) * CHAR_BIT - 1);
+# endif
+    m.word[LDBL_EXPBIT0_WORD + (LDBL_EXPBIT0_WORD < NWORDS / 2 ? 1 : - 1)]
+      |= (unsigned int) 1 << LDBL_EXPBIT0_BIT;
+    ASSERT (!isfinite (m.value));
+    #undef NWORDS
+  }
+#endif
+}
+
+int
+main ()
+{
+  test_isfinitef ();
+  test_isfinited ();
+  test_isfinitel ();
+  return 0;
+}