]> Savannah Git Hosting - gnulib.git/commitdiff
totalorder* tests: Refactor.
authorBruno Haible <bruno@clisp.org>
Wed, 4 Oct 2023 11:35:11 +0000 (13:35 +0200)
committerBruno Haible <bruno@clisp.org>
Wed, 4 Oct 2023 11:35:11 +0000 (13:35 +0200)
* tests/test-totalorder.c (positive_nan, negative_nan): New functions,
extracted from main.
(main): Use them when initializing the array.

ChangeLog
tests/test-totalorder.c

index e9de2bc1d0beef022f66d3233f0e2e0b41cbe91a..31e02bbf4a9014adf2ab85bcf004dc1e86c066d7 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,10 @@
+2023-10-04  Bruno Haible  <bruno@clisp.org>
+
+       totalorder* tests: Refactor.
+       * tests/test-totalorder.c (positive_nan, negative_nan): New functions,
+       extracted from main.
+       (main): Use them when initializing the array.
+
 2023-10-04  Bruno Haible  <bruno@clisp.org>
 
        totalorderl: Optimize.
index 1fc1b845688c0eae49dbd0677497aa3d23059235..b4a121c2a26c277a9f250c1b5946e1c231f04e1d 100644 (file)
 # define TOTALORDER_TYPE double
 #endif
 
+/* Return a quiet NaN with sign bit == 0.  */
+static TOTALORDER_TYPE
+positive_nan ()
+{
+  /* 'volatile' works around a GCC bug:
+     <https://gcc.gnu.org/bugzilla/show_bug.cgi?id=111655>  */
+  TOTALORDER_TYPE volatile nan = TOTALORDER_NAN ();
+  return (signbit (nan) ? - nan : nan);
+}
+
+/* Return a quiet NaN with sign bit == 1.  */
+static TOTALORDER_TYPE
+negative_nan ()
+{
+  /* 'volatile' works around a GCC bug:
+     <https://gcc.gnu.org/bugzilla/show_bug.cgi?id=111655>  */
+  TOTALORDER_TYPE volatile nan = TOTALORDER_NAN ();
+  return (signbit (nan) ? nan : - nan);
+}
+
 int
 main ()
 {
   TOTALORDER_TYPE x[] =
     {
-      -TOTALORDER_NAN (), -TOTALORDER_INF (), -1e37, -1, -1e-5,
+      negative_nan (), -TOTALORDER_INF (), -1e37, -1, -1e-5,
       TOTALORDER_MINUS_ZERO, 0,
-      1e-5, 1, 1e37, TOTALORDER_INF (), TOTALORDER_NAN ()
+      1e-5, 1, 1e37, TOTALORDER_INF (), positive_nan ()
     };
   int n = sizeof x / sizeof *x;
 
-  /* TOTALORDER_NAN () yields a NaN of unknown sign, so fix the
-     first NaN to be negative and the last NaN to be nonnegative.
-     Do this via volatile accesses, to work around GCC bug 111655.  */
-  TOTALORDER_TYPE volatile a = x[0], z = x[n - 1];
-  if (! signbit (a)) a = -a;
-  if (  signbit (z)) z = -z;
-  x[0] = a, x[n - 1] = z;
-
   for (int i = 0; i < n; i++)
     for (int j = 0; j < n; j++)
       ASSERT (!!TOTALORDER (&x[i], &x[j]) == (i <= j));