Subject: Re: bug#72145: rare Emacs screwups on x86 due to GCC bug 58416

From: Paul Eggert <[email-masked]>
Date: Wed, 17 Jul 2024 14:56:49 -0700

On 2024-07-16 22:01, Paul Eggert wrote:
> We could add an AC_RUN_IFELSE test for SSE2, though I doubt whether it 
> would affect builds significantly in practice.

On second thought the rare Arch or Gentoo user could still be building 
Emacs for the Pentium III, and for such a user a run-time test on the 
build host would be a win. This can be done via the attached revised 
patch. It uses AC_LINK_IFELSE to compile and run a single program, 
instead of AC_RUN_IFELSE which (when combined with AC_COMPILE_IFELSE) 
would mean compiling two test programs and running one.

From 5b14689f2389df1a4573c36ef9b597c1bd7c6326 Mon Sep 17 00:00:00 2001
From: Paul Eggert <[email-masked]>
Date: Mon, 15 Jul 2024 10:26:47 -0700
Subject: [PATCH] Work around GCC bug 58416 when building for x86

* configure.ac (C_SWITCH_MACHINE): Add -mfpmath=sse and perhaps
-msse2 to work around GCC bug 58416.
---
 configure.ac | 53 ++++++++++++++++++++++++++++++++++++++++++++++++++++
 etc/NEWS     | 10 ++++++++++
 2 files changed, 63 insertions(+)

diff --git a/configure.ac b/configure.ac
index e2b6dc2fc4d..8d6ff2c4db3 100644
--- a/configure.ac
+++ b/configure.ac
@@ -2325,6 +2325,59 @@ AC_DEFUN
     fi
   ;;
 esac
+
+AC_CACHE_CHECK([for flags to work around GCC bug 58416],
+  [emacs_cv_SSE2_CFLAGS],
+  [emacs_cv_SSE2_CFLAGS='none needed'
+   AS_CASE([$canonical],
+     [[i[3456]86-* | x86_64-*]],
+	[AS_IF([test "$GCC" = yes],
+	   [old_CFLAGS=$CFLAGS
+	    for emacs_cv_SSE2_CFLAGS in \
+		'none needed' '-mfpmath=sse' '-msse2 -mfpmath=sse' 'none work'
+	    do
+	      AS_CASE([$emacs_cv_SSE2_CFLAGS],
+	        ['none work'], [break],
+		['none needed'], [],
+		[CFLAGS="$old_CFLAGS $emacs_cv_SSE2_CFLAGS"])
+	      AC_LINK_IFELSE(
+		[AC_LANG_DEFINES_PROVIDED
+		 [/* Work around GCC bug with double in unions on x86,
+		     where the generated insns copy non-floating-point data
+		     via fldl/fstpl instruction pairs.  This can misbehave
+		     the data's bit pattern looks like a NaN.  See, e.g.:
+			https://gcc.gnu.org/bugzilla/show_bug.cgi?id=58416
+			https://gcc.gnu.org/bugzilla/show_bug.cgi?id=93271
+			https://gcc.gnu.org/bugzilla/show_bug.cgi?id=114659
+		     Problem observed with 'gcc -m32' with GCC 14.1.1
+		     20240607 (Red Hat 14.1.1-5) on x86-64.  */
+		  #include <float.h>
+		  #if \
+		      ((defined __GNUC__ && !defined __clang__) \
+		       && (defined __i386__ || defined __x86_64__) \
+		       && ! (defined FLT_EVAL_METHOD \
+			     && 0 <= FLT_EVAL_METHOD \
+			     && FLT_EVAL_METHOD <= 1))
+		  # error "GCC bug 58416 is possibly present"
+		  #endif
+		  int
+		  main (int argc, char **argv)
+		  {
+		    return argc / 1.3;
+		  }
+		]],
+		[# When not cross-compiling, test that the SSE2 code runs.
+		 # This lets native builds work even on ancient systems
+		 # (e.g., Pentium III, last new model introduced 2003).
+		 AS_CASE([$cross,_compiling,$emacs_cv_SSE2_CFLAGS],
+		   [no,-*],
+		     [./conftest$EXEEXT]) && break])
+	    done
+	    CFLAGS=$old_CFLAGS])])])
+AS_CASE([$emacs_cv_SSE2_CFLAGS],
+  [-*],
+    [C_SWITCH_MACHINE="$C_SWITCH_MACHINE $emacs_cv_SSE2_CFLAGS"])
+
 AC_SUBST([C_SWITCH_MACHINE])
 
 C_SWITCH_SYSTEM=
diff --git a/etc/NEWS b/etc/NEWS
index 60bde2abb40..8ab60503ba4 100644
--- a/etc/NEWS
+++ b/etc/NEWS
@@ -24,6 +24,16 @@ applies, and please also update docstrings as needed.
 
 * Installation Changes in Emacs 31.1
 
+** When using GCC to build Emacs on 32-bit x86 systems, 'configure' now
+defaults to specifying the GCC options -msse2 and -mfpmath=sse to work
+around GCC bug 58416.  As a result, the resulting Emacs executable now
+requires support for SSE2, introduced by Intel in 2000 for the Pentium 4
+and by AMD in 2003 for the Opteron and Athlon 64.  To build Emacs with
+GCC for older x86, either build natively on a system that lacks SSE2, or
+pass 'emacs_cv_SSE2_CFLAGS=no' to 'configure'; although the resulting
+Emacs may generate incorrect results or dump core, any such misbehavior
+should be rare.
+
 
 * Startup Changes in Emacs 31.1
 
-- 
2.45.2