From a42a02ac22aa41ff9222e9e46ede25f8cf43b823 Mon Sep 17 00:00:00 2001
From: Bruno Haible <bruno@clisp.org>
Date: Fri, 19 Apr 2024 02:14:37 +0200
Subject: [PATCH] totalorder*: Support newer MIPS CPUs.

* lib/totalorder.c (totalorder): On MIPS CPUs with MIPS_NAN2008_DOUBLE,
don't invert the most significant bit of the mantissa field.
* lib/totalorderf.c (totalorderf): On MIPS CPUs with MIPS_NAN2008_FLOAT,
don't invert the most significant bit of the mantissa field.
* lib/totalorderl.c (totalorderl): On MIPS CPUs with
MIPS_NAN2008_LONG_DOUBLE, don't invert the most significant bit of the
mantissa field.
* m4/totalorder.m4 (gl_FUNC_TOTALORDERF, gl_FUNC_TOTALORDER,
gl_FUNC_TOTALORDERL): Invoke gl_NAN_MIPS.
* modules/totalorder (Files): Add m4/nan-mips.m4.
* modules/totalorderf (Files): Likewise.
* modules/totalorderl (Files): Likewise.
---
 ChangeLog           | 16 ++++++++++++++++
 lib/totalorder.c    |  2 +-
 lib/totalorderf.c   |  2 +-
 lib/totalorderl.c   |  2 +-
 m4/totalorder.m4    |  7 ++++++-
 modules/totalorder  |  1 +
 modules/totalorderf |  1 +
 modules/totalorderl |  1 +
 8 files changed, 28 insertions(+), 4 deletions(-)

diff --git a/ChangeLog b/ChangeLog
index 3fe1847897..825968df32 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,19 @@
+2024-04-18  Bruno Haible  <bruno@clisp.org>
+
+	totalorder*: Support newer MIPS CPUs.
+	* lib/totalorder.c (totalorder): On MIPS CPUs with MIPS_NAN2008_DOUBLE,
+	don't invert the most significant bit of the mantissa field.
+	* lib/totalorderf.c (totalorderf): On MIPS CPUs with MIPS_NAN2008_FLOAT,
+	don't invert the most significant bit of the mantissa field.
+	* lib/totalorderl.c (totalorderl): On MIPS CPUs with
+	MIPS_NAN2008_LONG_DOUBLE, don't invert the most significant bit of the
+	mantissa field.
+	* m4/totalorder.m4 (gl_FUNC_TOTALORDERF, gl_FUNC_TOTALORDER,
+	gl_FUNC_TOTALORDERL): Invoke gl_NAN_MIPS.
+	* modules/totalorder (Files): Add m4/nan-mips.m4.
+	* modules/totalorderf (Files): Likewise.
+	* modules/totalorderl (Files): Likewise.
+
 2024-04-18  Bruno Haible  <bruno@clisp.org>
 
 	snan: Add support for m68k.
diff --git a/lib/totalorder.c b/lib/totalorder.c
index 445b8f84d6..6d1b2c4444 100644
--- a/lib/totalorder.c
+++ b/lib/totalorder.c
@@ -48,7 +48,7 @@ totalorder (double const *x, double const *y)
   /* At this point, *X and *Y are NaNs with the same sign bit.  */
 
   unsigned long long extended_sign = -!!xs;
-#if defined __hppa || defined __mips__ || defined __sh__
+#if defined __hppa || (defined __mips__ && !MIPS_NAN2008_DOUBLE) || defined __sh__
   /* Invert the most significant bit of the mantissa field.  Cf. snan.h.  */
   extended_sign ^= (1ULL << 51);
 #endif
diff --git a/lib/totalorderf.c b/lib/totalorderf.c
index 70bc02cd90..f8af4b0241 100644
--- a/lib/totalorderf.c
+++ b/lib/totalorderf.c
@@ -48,7 +48,7 @@ totalorderf (float const *x, float const *y)
   /* At this point, *X and *Y are NaNs with the same sign bit.  */
 
   unsigned int extended_sign = -!!xs;
-#if defined __hppa || defined __mips__ || defined __sh__
+#if defined __hppa || (defined __mips__ && !MIPS_NAN2008_FLOAT) || defined __sh__
   /* Invert the most significant bit of the mantissa field.  Cf. snan.h.  */
   extended_sign ^= (1U << 22);
 #endif
diff --git a/lib/totalorderl.c b/lib/totalorderl.c
index 4ccb7e7e23..21669f4af8 100644
--- a/lib/totalorderl.c
+++ b/lib/totalorderl.c
@@ -63,7 +63,7 @@ totalorderl (long double const *x, long double const *y)
 
   unsigned long long extended_sign = -!!xs;
   unsigned long long extended_sign_hi = extended_sign;
-# if defined __hppa || defined __mips__ || defined __sh__
+# if defined __hppa || (defined __mips__ && !MIPS_NAN2008_LONG_DOUBLE) || defined __sh__
   /* Invert the most significant bit of the mantissa field.  Cf. snan.h.  */
   extended_sign_hi ^=
     (1ULL << (LDBL_MANT_DIG == 106
diff --git a/m4/totalorder.m4 b/m4/totalorder.m4
index 51012b8503..72921d6357 100644
--- a/m4/totalorder.m4
+++ b/m4/totalorder.m4
@@ -1,5 +1,5 @@
 # totalorder.m4
-# serial 2
+# serial 3
 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,
@@ -39,6 +39,8 @@ AC_DEFUN([gl_FUNC_TOTALORDERF],
   fi
   if test $HAVE_TOTALORDERF = 0 || test $REPLACE_TOTALORDERF = 1; then
     TOTALORDERF_LIBM='$(ISNANF_LIBM)'
+    dnl Prerequisite of lib/totalorderf.c.
+    gl_NAN_MIPS
   fi
   AC_SUBST([TOTALORDERF_LIBM])
 ])
@@ -77,6 +79,8 @@ AC_DEFUN([gl_FUNC_TOTALORDER],
   fi
   if test $HAVE_TOTALORDER = 0 || test $REPLACE_TOTALORDER = 1; then
     TOTALORDER_LIBM='$(ISNAND_LIBM)'
+    dnl Prerequisite of lib/totalorder.c.
+    gl_NAN_MIPS
   fi
   AC_SUBST([TOTALORDER_LIBM])
 ])
@@ -125,6 +129,7 @@ AC_DEFUN([gl_FUNC_TOTALORDERL],
     fi
     dnl Prerequisite of lib/totalorderl.c.
     gl_LONG_DOUBLE_SIGN_LOCATION
+    gl_NAN_MIPS
   fi
   AC_SUBST([TOTALORDERL_LIBM])
 ])
diff --git a/modules/totalorder b/modules/totalorder
index 00f190c42e..d21708e44e 100644
--- a/modules/totalorder
+++ b/modules/totalorder
@@ -5,6 +5,7 @@ Files:
 lib/totalorder.c
 m4/mathfunc.m4
 m4/totalorder.m4
+m4/nan-mips.m4
 
 Depends-on:
 math
diff --git a/modules/totalorderf b/modules/totalorderf
index ab1569c9cc..7125c8a57c 100644
--- a/modules/totalorderf
+++ b/modules/totalorderf
@@ -5,6 +5,7 @@ Files:
 lib/totalorderf.c
 m4/mathfunc.m4
 m4/totalorder.m4
+m4/nan-mips.m4
 
 Depends-on:
 math
diff --git a/modules/totalorderl b/modules/totalorderl
index d153f4c8cc..46452842af 100644
--- a/modules/totalorderl
+++ b/modules/totalorderl
@@ -5,6 +5,7 @@ Files:
 lib/totalorderl.c
 m4/mathfunc.m4
 m4/totalorder.m4
+m4/nan-mips.m4
 m4/signbit.m4
 
 Depends-on:
-- 
2.39.5