These two patches add a benchmark for the totalorder* functions.

2023-10-15  Bruno Haible  <br...@clisp.org>

        totalorder-bench-tests: New module.
        * tests/bench-totalorder.c: New file, based on tests/test-totalorder.h.
        * modules/totalorder-bench-tests: New file.

2023-10-15  Bruno Haible  <br...@clisp.org>

        snan: Simplify a type.
        * lib/snan.h (memory_long_double): Put the 'value' field first.
        * tests/test-totalorder.h (main): No need for designated initializer
        syntax any more.

>From 0e1db78e3d79699cde002dde1db1a217e6f0529b Mon Sep 17 00:00:00 2001
From: Bruno Haible <br...@clisp.org>
Date: Sun, 15 Oct 2023 15:53:01 +0200
Subject: [PATCH 1/2] snan: Simplify a type.

* lib/snan.h (memory_long_double): Put the 'value' field first.
* tests/test-totalorder.h (main): No need for designated initializer
syntax any more.
---
 ChangeLog               |  7 +++++++
 lib/snan.h              |  2 +-
 tests/test-totalorder.h | 24 ++++++++++++------------
 3 files changed, 20 insertions(+), 13 deletions(-)

diff --git a/ChangeLog b/ChangeLog
index ef440addaf..6aedd3af73 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,10 @@
+2023-10-15  Bruno Haible  <br...@clisp.org>
+
+	snan: Simplify a type.
+	* lib/snan.h (memory_long_double): Put the 'value' field first.
+	* tests/test-totalorder.h (main): No need for designated initializer
+	syntax any more.
+
 2023-10-15  Bruno Haible  <br...@clisp.org>
 
 	vma-iter: Fix compilation error on FreeBSD 5.2.1.
diff --git a/lib/snan.h b/lib/snan.h
index abd95318d7..b89f13a8de 100644
--- a/lib/snan.h
+++ b/lib/snan.h
@@ -181,7 +181,7 @@ SNaNd ()
 
 #define NWORDS \
   ((sizeof (long double) + sizeof (unsigned int) - 1) / sizeof (unsigned int))
-typedef union { unsigned int word[NWORDS]; long double value; }
+typedef union { long double value; unsigned int word[NWORDS]; }
         memory_long_double;
 
 #if defined LDBL_EXPBIT0_WORD && defined LDBL_EXPBIT0_BIT
diff --git a/tests/test-totalorder.h b/tests/test-totalorder.h
index 6292ea130a..e00d51c4c4 100644
--- a/tests/test-totalorder.h
+++ b/tests/test-totalorder.h
@@ -27,24 +27,24 @@ main ()
 {
   TOTALORDER_TYPE x[] =
     {
-      { .value = TOTALORDER_NEGATIVE_NAN () },
+      { TOTALORDER_NEGATIVE_NAN () },
 #if TOTALORDER_HAVE_SNAN
       TOTALORDER_NEGATIVE_SNAN (),
 #endif
-      { .value = -TOTALORDER_INF () },
-      { .value = -1e37 },
-      { .value = -1 },
-      { .value = -1e-5 },
-      { .value = TOTALORDER_MINUS_ZERO },
-      { .value = 0 },
-      { .value = 1e-5 },
-      { .value = 1 },
-      { .value = 1e37 },
-      { .value = TOTALORDER_INF () },
+      { -TOTALORDER_INF () },
+      { -1e37 },
+      { -1 },
+      { -1e-5 },
+      { TOTALORDER_MINUS_ZERO },
+      { 0 },
+      { 1e-5 },
+      { 1 },
+      { 1e37 },
+      { TOTALORDER_INF () },
 #if TOTALORDER_HAVE_SNAN
       TOTALORDER_POSITIVE_SNAN (),
 #endif
-      { .value = TOTALORDER_POSITIVE_NAN () }
+      { TOTALORDER_POSITIVE_NAN () }
     };
   int n = SIZEOF (x);
   int result = 0;
-- 
2.34.1

>From eb12a778b3d7710fd283f3d28ee9f88c926da0d8 Mon Sep 17 00:00:00 2001
From: Bruno Haible <br...@clisp.org>
Date: Sun, 15 Oct 2023 15:54:11 +0200
Subject: [PATCH 2/2] totalorder-bench-tests: New module.

* tests/bench-totalorder.c: New file, based on tests/test-totalorder.h.
* modules/totalorder-bench-tests: New file.
---
 ChangeLog                      |   6 +
 modules/totalorder-bench-tests |  18 +++
 tests/bench-totalorder.c       | 204 +++++++++++++++++++++++++++++++++
 3 files changed, 228 insertions(+)
 create mode 100644 modules/totalorder-bench-tests
 create mode 100644 tests/bench-totalorder.c

diff --git a/ChangeLog b/ChangeLog
index 6aedd3af73..0f078166bb 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,9 @@
+2023-10-15  Bruno Haible  <br...@clisp.org>
+
+	totalorder-bench-tests: New module.
+	* tests/bench-totalorder.c: New file, based on tests/test-totalorder.h.
+	* modules/totalorder-bench-tests: New file.
+
 2023-10-15  Bruno Haible  <br...@clisp.org>
 
 	snan: Simplify a type.
diff --git a/modules/totalorder-bench-tests b/modules/totalorder-bench-tests
new file mode 100644
index 0000000000..b875561fd9
--- /dev/null
+++ b/modules/totalorder-bench-tests
@@ -0,0 +1,18 @@
+Files:
+tests/bench-totalorder.c
+tests/bench.h
+
+Depends-on:
+totalorderf
+totalorder
+totalorderl
+getrusage
+gettimeofday
+signed-nan
+signed-snan
+
+configure.ac:
+
+Makefile.am:
+noinst_PROGRAMS += bench-totalorder
+bench_totalorder_LDADD = $(LDADD) $(TOTALORDERF_LIBM) $(TOTALORDER_LIBM) $(TOTALORDERL_LIBM)
diff --git a/tests/bench-totalorder.c b/tests/bench-totalorder.c
new file mode 100644
index 0000000000..c2fdca4d19
--- /dev/null
+++ b/tests/bench-totalorder.c
@@ -0,0 +1,204 @@
+/* Benchmarks for totalorder*().
+   Copyright (C) 2023 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 <https://www.gnu.org/licenses/>.  */
+
+#include <config.h>
+
+#include <math.h>
+#include <stdlib.h>
+#include <string.h>
+
+#include "bench.h"
+#include "infinity.h"
+#include "macros.h"
+#include "minus-zero.h"
+#include "signed-nan.h"
+#include "signed-snan.h"
+
+int volatile tmp;
+
+#define FINITE_VALUES \
+      { -1e37 }, \
+      { -1 }, \
+      { -1e-5 }, \
+      { 0 }, \
+      { 1e-5 }, \
+      { 0.5772156649015328606 }, \
+      { 0.6931471805599453094 }, \
+      { 0.8346268416740731863 }, \
+      { 0.91596559417721901505 }, \
+      { 1 }, \
+      { 1.2020569031595942854 }, \
+      { 1.6066951524152917638 }, \
+      { 1.6180339887498948482 }, \
+      { 1.6449340668482264365 }, \
+      { 2.6220575542921198105 }, \
+      { 2.7182818284590452354 }, \
+      { 3.1415926535897932385 }, \
+      { 4.66920160910299067185 }, \
+      { 262537412640768743.99999999999925007 }, \
+      { 1e37 }
+
+static void
+do_float_test (char test, int repeat)
+{
+  printf ("Test %c\n", test);
+
+  memory_float data[] =
+    {
+#if HAVE_SNANF
+      memory_negative_SNaNf (),
+      memory_positive_SNaNf (),
+#endif
+      { negative_NaNf () },
+      { positive_NaNf () },
+      { -Infinityf () },
+      { Infinityf () },
+      { minus_zerof },
+      FINITE_VALUES
+    };
+  int n = sizeof (data) / sizeof (data[0]);
+
+  struct timings_state ts;
+  timing_start (&ts);
+
+  int count;
+  for (count = 0; count < repeat; count++)
+    {
+      for (int i = 0; i < n; i++)
+        for (int j = 0; j < n; j++)
+          tmp = totalorderf (&data[i].value, &data[j].value);
+    }
+
+  timing_end (&ts);
+  timing_output (&ts);
+  printf ("\n");
+}
+
+static void
+do_double_test (char test, int repeat)
+{
+  printf ("Test %c\n", test);
+
+  memory_double data[] =
+    {
+#if HAVE_SNAND
+      memory_negative_SNaNd (),
+      memory_positive_SNaNd (),
+#endif
+      { negative_NaNd () },
+      { positive_NaNd () },
+      { -Infinityd () },
+      { Infinityd () },
+      { minus_zerod },
+      FINITE_VALUES
+    };
+  int n = sizeof (data) / sizeof (data[0]);
+
+  struct timings_state ts;
+  timing_start (&ts);
+
+  int count;
+  for (count = 0; count < repeat; count++)
+    {
+      for (int i = 0; i < n; i++)
+        for (int j = 0; j < n; j++)
+          tmp = totalorder (&data[i].value, &data[j].value);
+    }
+
+  timing_end (&ts);
+  timing_output (&ts);
+  printf ("\n");
+}
+
+static void
+do_long_double_test (char test, int repeat)
+{
+  printf ("Test %c\n", test);
+
+  memory_long_double data[] =
+    {
+#if HAVE_SNANL
+      memory_negative_SNaNl (),
+      memory_positive_SNaNl (),
+#endif
+      { negative_NaNl () },
+      { positive_NaNl () },
+      { -Infinityl () },
+      { Infinityl () },
+      { minus_zerol },
+      FINITE_VALUES
+    };
+  int n = sizeof (data) / sizeof (data[0]);
+
+  struct timings_state ts;
+  timing_start (&ts);
+
+  int count;
+  for (count = 0; count < repeat; count++)
+    {
+      for (int i = 0; i < n; i++)
+        for (int j = 0; j < n; j++)
+          tmp = totalorderl (&data[i].value, &data[j].value);
+    }
+
+  timing_end (&ts);
+  timing_output (&ts);
+  printf ("\n");
+}
+
+/* Performs some or all of the following tests:
+     f - float
+     d - double
+     l - long double
+   Pass the tests to be performed as first argument.  */
+int
+main (int argc, char *argv[])
+{
+  if (argc != 3)
+    {
+      fprintf (stderr, "Usage: %s TESTS REPETITIONS\n", argv[0]);
+      fprintf (stderr, "Example: %s fdl 1000000\n", argv[0]);
+      exit (1);
+    }
+
+  const char *tests = argv[1];
+  int repeat = atoi (argv[2]);
+
+  /* Execute each test.  */
+  size_t i;
+  for (i = 0; i < strlen (tests); i++)
+    {
+      char test = tests[i];
+
+      switch (test)
+        {
+        case 'f':
+          do_float_test (test, repeat);
+          break;
+        case 'd':
+          do_double_test (test, repeat);
+          break;
+        case 'l':
+          do_long_double_test (test, repeat);
+          break;
+        default:
+          /* Ignore.  */
+          ;
+        }
+    }
+
+  return 0;
+}
-- 
2.34.1

Reply via email to