]> Savannah Git Hosting - gnulib.git/commitdiff
totalorder, totalorderf: Fix handling of SNaN on i386 and x86_64 CPUs.
authorBruno Haible <bruno@clisp.org>
Tue, 9 Apr 2024 11:08:32 +0000 (13:08 +0200)
committerBruno Haible <bruno@clisp.org>
Fri, 19 Apr 2024 12:41:26 +0000 (14:41 +0200)
* lib/totalorder.c: Include <string.h>.
(totalorder): Use memcpy to copy the 'double' values into the union.
Drop 'volatile'.
* lib/totalorderf.c: Include <string.h>.
(totalorderf): Use memcpy to copy the 'float' values into the union.
Drop 'volatile'.

ChangeLog
lib/totalorder.c
lib/totalorderf.c

index 3325c626e946096b8845c3d0940fa079b67eaa0f..c3eae2e396f5c7bad8082885aaa77decf21da32c 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,13 @@
+2024-04-09  Bruno Haible  <bruno@clisp.org>
+
+       totalorder, totalorderf: Fix handling of SNaN on i386 and x86_64 CPUs.
+       * lib/totalorder.c: Include <string.h>.
+       (totalorder): Use memcpy to copy the 'double' values into the union.
+       Drop 'volatile'.
+       * lib/totalorderf.c: Include <string.h>.
+       (totalorderf): Use memcpy to copy the 'float' values into the union.
+       Drop 'volatile'.
+
 2024-04-09  Bruno Haible  <bruno@clisp.org>
 
        totalorder tests: Fix signature test.
index 1fb8f0d24dbec22be7ecf02f2c8d171b61874cdb..635e3cb2761dbc2030d6a3f5720b3b6c238451c4 100644 (file)
@@ -21,6 +21,8 @@
 /* Specification.  */
 #include <math.h>
 
+#include <string.h>
+
 int
 totalorder (double const *x, double const *y)
 {
@@ -50,8 +52,18 @@ totalorder (double const *x, double const *y)
   /* Invert the most significant bit of the mantissa field.  Cf. snan.h.  */
   extended_sign ^= (1ULL << 51);
 #endif
-  union { unsigned long long i; double f; } volatile xu = {0}, yu = {0};
+  union { unsigned long long i; double f; } xu = {0}, yu = {0};
+#if 0
   xu.f = *x;
   yu.f = *y;
+#else
+  /* On 32-bit x86 processors, as well as on x86_64 processors with
+     CC="gcc -mfpmath=387", the evaluation of *x and *y above is done through
+     an 'fldl' instruction, which converts a signalling NaN to a quiet NaN. See
+     <https://lists.gnu.org/archive/html/bug-gnulib/2023-10/msg00060.html>
+     for details.  Use memcpy to avoid this.  */
+  memcpy (&xu.f, x, sizeof (double));
+  memcpy (&yu.f, y, sizeof (double));
+#endif
   return (xu.i ^ extended_sign) <= (yu.i ^ extended_sign);
 }
index d235bc98bc4aac8e519273a80defe4a1e9f7fc00..75024b683966123dc90d523f6bf413cc66668e7b 100644 (file)
@@ -21,6 +21,8 @@
 /* Specification.  */
 #include <math.h>
 
+#include <string.h>
+
 int
 totalorderf (float const *x, float const *y)
 {
@@ -50,8 +52,18 @@ totalorderf (float const *x, float const *y)
   /* Invert the most significant bit of the mantissa field.  Cf. snan.h.  */
   extended_sign ^= (1U << 22);
 #endif
-  union { unsigned int i; float f; } volatile xu = {0}, yu = {0};
+  union { unsigned int i; float f; } xu = {0}, yu = {0};
+#if 0
   xu.f = *x;
   yu.f = *y;
+#else
+  /* On 32-bit x86 processors, as well as on x86_64 processors with
+     CC="gcc -mfpmath=387", the evaluation of *x and *y above is done through
+     an 'flds' instruction, which converts a signalling NaN to a quiet NaN. See
+     <https://lists.gnu.org/archive/html/bug-gnulib/2023-10/msg00060.html>
+     for details.  Use memcpy to avoid this.  */
+  memcpy (&xu.f, x, sizeof (float));
+  memcpy (&yu.f, y, sizeof (float));
+#endif
   return (xu.i ^ extended_sign) <= (yu.i ^ extended_sign);
 }