From 8e04e14294e7ce756738625c3be1183be4b0c4eb Mon Sep 17 00:00:00 2001
From: Eric Blake <ebb9@byu.net>
Date: Tue, 11 Dec 2007 11:35:55 -0700
Subject: [PATCH] Detect pseudo-denormals on x86 even when cross-compiling.

* lib/isnan.c (rpl_isnanl) [!KNOWN_EXPBIT0_LOCATION
&& USE_LONG_DOUBLE && x86]: Add one more check to filter out
invalid bit patterns that happen to satisfy ==.

Signed-off-by: Eric Blake <ebb9@byu.net>
---
 ChangeLog   |  5 +++++
 lib/isnan.c | 14 +++++++++++++-
 2 files changed, 18 insertions(+), 1 deletion(-)

diff --git a/ChangeLog b/ChangeLog
index ed8799bbfa..127f9a5469 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,5 +1,10 @@
 2007-12-11  Eric Blake  <ebb9@byu.net>
 
+	Detect pseudo-denormals on x86 even when cross-compiling.
+	* lib/isnan.c (rpl_isnanl) [!KNOWN_EXPBIT0_LOCATION
+	&& USE_LONG_DOUBLE && x86]: Add one more check to filter out
+	invalid bit patterns that happen to satisfy ==.
+
 	Avoid link failures with separate libtests.a.
 	* gnulib-tool (func_emit_tests_Makefile_am): Also list local_ldadd
 	last, to satisfy circular dependencies.
diff --git a/lib/isnan.c b/lib/isnan.c
index 4add6a2eb5..182a7bd5f5 100644
--- a/lib/isnan.c
+++ b/lib/isnan.c
@@ -140,7 +140,19 @@ FUNC (DOUBLE x)
   /* The configuration did not find sufficient information.  Give up about
      the signaling NaNs, handle only the quiet NaNs.  */
   if (x == x)
-    return 0;
+    {
+# if defined USE_LONG_DOUBLE && ((defined __ia64 && LDBL_MANT_DIG == 64) || (defined __x86_64__ || defined __amd64__) || (defined __i386 || defined __i386__ || defined _I386 || defined _M_IX86 || defined _X86_))
+      /* Detect any special bit patterns that pass ==; see comment above.  */
+      memory_double m1;
+      memory_double m2;
+
+      m1.value = x;
+      m2.value = x + 0;
+      if (memcmp (&m1.value, &m2.value, SIZE) != 0)
+        return 1;
+# endif
+      return 0;
+    }
   else
     return 1;
 #endif
-- 
2.30.2