]> Savannah Git Hosting - gnulib.git/commitdiff
totalorderl: Simplify on MSVC.
authorBruno Haible <bruno@clisp.org>
Thu, 18 Apr 2024 14:29:28 +0000 (16:29 +0200)
committerBruno Haible <bruno@clisp.org>
Thu, 18 Apr 2024 14:30:09 +0000 (16:30 +0200)
* lib/totalorderl.c (totalorderl): If 'long double' is the same as
'double', just invoke totalorder. Otherwise, drop the optimized code
for small 'long double' since it does not occur on any platform.
* m4/totalorder.m4 (gl_FUNC_TOTALORDERL): Require
gl_LONG_DOUBLE_VS_DOUBLE. If 'long double' is the same as 'double', use
TOTALORDER_LIBM.
* modules/totalorderl (Depends-on): Add totalorder.

ChangeLog
lib/totalorderl.c
m4/totalorder.m4
modules/totalorderl

index aa8533e40da69fad828dba632e775dd14ee3e51e..6a925743911847adc00315d0cc96d2b4c0a248ba 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,14 @@
+2024-04-18  Bruno Haible  <bruno@clisp.org>
+
+       totalorderl: Simplify on MSVC.
+       * lib/totalorderl.c (totalorderl): If 'long double' is the same as
+       'double', just invoke totalorder. Otherwise, drop the optimized code
+       for small 'long double' since it does not occur on any platform.
+       * m4/totalorder.m4 (gl_FUNC_TOTALORDERL): Require
+       gl_LONG_DOUBLE_VS_DOUBLE. If 'long double' is the same as 'double', use
+       TOTALORDER_LIBM.
+       * modules/totalorderl (Depends-on): Add totalorder.
+
 2024-04-18  Bruno Haible  <bruno@clisp.org>
 
        signbit: Fix typos.
index 1532a84c10b43e88925d260c6761d4b0de0ee7b4..4ccb7e7e2364ac70b4e8fff22acd55136e4352fc 100644 (file)
 /* Specification.  */
 #include <math.h>
 
-#include <float.h>
+#if HAVE_SAME_LONG_DOUBLE_AS_DOUBLE
 
-#ifndef LDBL_SIGNBIT_WORD
-# define LDBL_SIGNBIT_WORD (-1)
-#endif
+int
+totalorderl (long double const *x, long double const *y)
+{
+  return totalorder ((double const *) x, (double const *) y);
+}
+
+#else
+
+# include <float.h>
+
+# ifndef LDBL_SIGNBIT_WORD
+#  define LDBL_SIGNBIT_WORD (-1)
+# endif
 
 int
 totalorderl (long double const *x, long double const *y)
@@ -52,28 +62,14 @@ totalorderl (long double const *x, long double const *y)
   /* At this point, *X and *Y are NaNs with the same sign bit.  */
 
   unsigned long long extended_sign = -!!xs;
-
-  if (sizeof (long double) <= sizeof (unsigned long long))
-    {
-#if defined __hppa || defined __mips__ || defined __sh__
-      /* Invert the most significant bit of the mantissa field.  Cf. snan.h.  */
-      extended_sign ^= (1ULL << 51);
-#endif
-      union { unsigned long long i; long double f; } volatile
-        xu = {0}, yu = {0};
-      xu.f = *x;
-      yu.f = *y;
-      return (xu.i ^ extended_sign) <= (yu.i ^ extended_sign);
-    }
-
   unsigned long long extended_sign_hi = extended_sign;
-#if defined __hppa || defined __mips__ || defined __sh__
+# if defined __hppa || defined __mips__ || defined __sh__
   /* Invert the most significant bit of the mantissa field.  Cf. snan.h.  */
   extended_sign_hi ^=
     (1ULL << (LDBL_MANT_DIG == 106
               ? 51                          /* double-double representation */
               : (LDBL_MANT_DIG - 2) - 64)); /* quad precision representation */
-#endif
+# endif
   union u { unsigned long long i[2]; long double f; } volatile xu, yu;
   /* Although it is tempting to initialize with {0}, Solaris cc (Sun C 5.8)
      on x86_64 miscompiles {0}: it initializes only the lower 80 bits,
@@ -104,3 +100,5 @@ totalorderl (long double const *x, long double const *y)
     ylo = yu.i[ bigendian] ^ extended_sign;
   return (xhi < yhi) | ((xhi == yhi) & (xlo <= ylo));
 }
+
+#endif
index 92166de4843368f92a299a91fec00db525157af7..51012b8503d21578c765f12a6776365abaa02fbc 100644 (file)
@@ -1,5 +1,5 @@
 # totalorder.m4
-# serial 1
+# serial 2
 dnl Copyright 2023-2024 Free Software Foundation, Inc.
 dnl This file is free software; the Free Software Foundation
 dnl gives unlimited permission to copy and/or distribute it,
@@ -85,6 +85,7 @@ AC_DEFUN([gl_FUNC_TOTALORDERL],
 [
   AC_REQUIRE([gl_MATH_H_DEFAULTS])
   AC_REQUIRE([gl_USE_SYSTEM_EXTENSIONS])
+  AC_REQUIRE([gl_LONG_DOUBLE_VS_DOUBLE])
 
   dnl glibc versions < 2.31 had an incompatible declaration of this function,
   dnl see <https://sourceware.org/git/?p=glibc.git;a=commitdiff;h=42760d764649ad82f5fe45a26cbdf2c2500409f7>
@@ -115,7 +116,13 @@ AC_DEFUN([gl_FUNC_TOTALORDERL],
     fi
   fi
   if test $HAVE_TOTALORDERL = 0 || test $REPLACE_TOTALORDERL = 1; then
-    TOTALORDERL_LIBM='$(ISNANL_LIBM)'
+    dnl Find libraries needed to link lib/totalorderl.c.
+    if test $HAVE_SAME_LONG_DOUBLE_AS_DOUBLE = 1; then
+      AC_REQUIRE([gl_FUNC_TOTALORDER])
+      TOTALORDERL_LIBM="$TOTALORDER_LIBM"
+    else
+      TOTALORDERL_LIBM='$(ISNANL_LIBM)'
+    fi
     dnl Prerequisite of lib/totalorderl.c.
     gl_LONG_DOUBLE_SIGN_LOCATION
   fi
index 76dafb8ecd021b443e0eca6bb16c7c942b58a163..d153f4c8ccb85bbd6a803f29f0f73b1792d723a6 100644 (file)
@@ -10,10 +10,11 @@ m4/signbit.m4
 Depends-on:
 math
 extensions
-float           [test $HAVE_TOTALORDERL = 0 || test $REPLACE_TOTALORDERL = 1]
-stdbool         [test $HAVE_TOTALORDERL = 0 || test $REPLACE_TOTALORDERL = 1]
-isnanl          [test $HAVE_TOTALORDERL = 0 || test $REPLACE_TOTALORDERL = 1]
-signbit         [test $HAVE_TOTALORDERL = 0 || test $REPLACE_TOTALORDERL = 1]
+totalorder      [{ test $HAVE_TOTALORDERL = 0 || test $REPLACE_TOTALORDERL = 1; } && test $HAVE_SAME_LONG_DOUBLE_AS_DOUBLE = 1]
+float           [{ test $HAVE_TOTALORDERL = 0 || test $REPLACE_TOTALORDERL = 1; } && test $HAVE_SAME_LONG_DOUBLE_AS_DOUBLE = 0]
+stdbool         [{ test $HAVE_TOTALORDERL = 0 || test $REPLACE_TOTALORDERL = 1; } && test $HAVE_SAME_LONG_DOUBLE_AS_DOUBLE = 0]
+isnanl          [{ test $HAVE_TOTALORDERL = 0 || test $REPLACE_TOTALORDERL = 1; } && test $HAVE_SAME_LONG_DOUBLE_AS_DOUBLE = 0]
+signbit         [{ test $HAVE_TOTALORDERL = 0 || test $REPLACE_TOTALORDERL = 1; } && test $HAVE_SAME_LONG_DOUBLE_AS_DOUBLE = 0]
 
 configure.ac:
 gl_FUNC_TOTALORDERL