From 11368c16b7faaff172abbfd8b25303389c7031e5 Mon Sep 17 00:00:00 2001
From: Bruno Haible <bruno@clisp.org>
Date: Mon, 10 Oct 2011 01:08:51 +0200
Subject: [PATCH] rintl: Simplify for platforms where 'long double' ==
 'double'.

* lib/rintl.c: Include <config.h>.
(rintl) [HAVE_SAME_LONG_DOUBLE_AS_DOUBLE]: New function.
* lib/rint.c [!USE_LONG_DOUBLE]: Don't include <config.h> a second
time.
* m4/rintl.m4 (gl_FUNC_RINTL): Require gl_LONG_DOUBLE_VS_DOUBLE.
Determine RINTL_LIBM according to HAVE_SAME_LONG_DOUBLE_AS_DOUBLE.
* modules/rintl (Depends-on): Add rint. Update conditions.
---
 ChangeLog     | 11 +++++++++++
 lib/rint.c    |  4 +++-
 lib/rintl.c   | 21 +++++++++++++++++++--
 m4/rintl.m4   | 11 +++++++++--
 modules/rintl |  1 +
 5 files changed, 43 insertions(+), 5 deletions(-)

diff --git a/ChangeLog b/ChangeLog
index a342bb0ac5..95f2b1d8cd 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,14 @@
+2011-10-09  Bruno Haible  <bruno@clisp.org>
+
+	rintl: Simplify for platforms where 'long double' == 'double'.
+	* lib/rintl.c: Include <config.h>.
+	(rintl) [HAVE_SAME_LONG_DOUBLE_AS_DOUBLE]: New function.
+	* lib/rint.c [!USE_LONG_DOUBLE]: Don't include <config.h> a second
+	time.
+	* m4/rintl.m4 (gl_FUNC_RINTL): Require gl_LONG_DOUBLE_VS_DOUBLE.
+	Determine RINTL_LIBM according to HAVE_SAME_LONG_DOUBLE_AS_DOUBLE.
+	* modules/rintl (Depends-on): Add rint. Update conditions.
+
 2011-10-09  Bruno Haible  <bruno@clisp.org>
 
 	roundl: Simplify for platforms where 'long double' == 'double'.
diff --git a/lib/rint.c b/lib/rint.c
index 4ba3fe5547..7eefe7aabb 100644
--- a/lib/rint.c
+++ b/lib/rint.c
@@ -15,7 +15,9 @@
    with this program; if not, write to the Free Software Foundation,
    Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.  */
 
-#include <config.h>
+#if ! defined USE_LONG_DOUBLE
+# include <config.h>
+#endif
 
 /* Specification.  */
 #include <math.h>
diff --git a/lib/rintl.c b/lib/rintl.c
index 6c0e9999cb..d26935ef89 100644
--- a/lib/rintl.c
+++ b/lib/rintl.c
@@ -15,5 +15,22 @@
    with this program; if not, write to the Free Software Foundation,
    Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.  */
 
-#define USE_LONG_DOUBLE
-#include "rint.c"
+#include <config.h>
+
+#if HAVE_SAME_LONG_DOUBLE_AS_DOUBLE
+
+/* Specification.  */
+# include <math.h>
+
+long double
+rintl (long double x)
+{
+  return rint (x);
+}
+
+#else
+
+# define USE_LONG_DOUBLE
+# include "rint.c"
+
+#endif
diff --git a/m4/rintl.m4 b/m4/rintl.m4
index df83341034..bb88ee8063 100644
--- a/m4/rintl.m4
+++ b/m4/rintl.m4
@@ -1,4 +1,4 @@
-# rintl.m4 serial 2
+# rintl.m4 serial 3
 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,
@@ -7,13 +7,20 @@ dnl with or without modifications, as long as this notice is preserved.
 AC_DEFUN([gl_FUNC_RINTL],
 [
   AC_REQUIRE([gl_MATH_H_DEFAULTS])
+  AC_REQUIRE([gl_LONG_DOUBLE_VS_DOUBLE])
 
   dnl Determine RINTL_LIBM.
   gl_MATHFUNC([rintl], [long double], [(long double)])
   if test $gl_cv_func_rintl_no_libm = no \
      && test $gl_cv_func_rintl_in_libm = no; then
     HAVE_RINTL=0
-    RINTL_LIBM=
+    dnl Find libraries needed to link lib/rintl.c.
+    if test $HAVE_SAME_LONG_DOUBLE_AS_DOUBLE = 1; then
+      AC_REQUIRE([gl_FUNC_RINT])
+      RINTL_LIBM="$RINT_LIBM"
+    else
+      RINTL_LIBM=
+    fi
   fi
   AC_SUBST([RINTL_LIBM])
 ])
diff --git a/modules/rintl b/modules/rintl
index 80a0f528ae..dac26185f2 100644
--- a/modules/rintl
+++ b/modules/rintl
@@ -9,6 +9,7 @@ m4/mathfunc.m4
 
 Depends-on:
 math
+rint            [test $HAVE_RINTL = 0 && test $HAVE_SAME_LONG_DOUBLE_AS_DOUBLE = 1]
 
 configure.ac:
 gl_FUNC_RINTL
-- 
2.39.5