Subject: Re: bug#42147: 28.0.50; pure vs side-effect-free, missing optimizations?

From: Paul Eggert <[email-masked]>
Date: Sat, 4 Jul 2020 08:57:08 -0700

On 7/3/20 11:47 AM, Mattias Engdegård wrote:
>> Having Emacs use --with-mfpmath=sse should improve performance a bit on x86. But
>> more important, it should make floating point more reproducible. If I get the
>> time I'll look into having 'configure' add it automatically.
> Thank you, this would be a prerequisite for further improvements.

Attached is a patch to do that. I looked at the GCC source code, and x86 is the
only platform where this sort of thing should be necessary. I'll mention this
patch on emacs-devel, to give people a heads-up before installing.
>From df1cd25cefbbbd908efef3023598a6c72d34a176 Mon Sep 17 00:00:00 2001
From: Paul Eggert <[email-masked]>
Date: Sat, 4 Jul 2020 08:43:12 -0700
Subject: [PATCH] Prefer standard rounding on x86

This makes Emacs behavior more consistent across platforms,
and should make some byte-code optimizations safer (Bug#42147).
We can assume SSE2 by now, since it has been out for 20 years.
* configure.ac: On x86 with GCC, default to -msse2 -mfpmath=sse.
* etc/NEWS: Mention this.
---
 configure.ac | 11 +++++++++++
 etc/NEWS     |  6 ++++++
 2 files changed, 17 insertions(+)

diff --git a/configure.ac b/configure.ac
index 6ede6104d3..9af691078f 100644
--- a/configure.ac
+++ b/configure.ac
@@ -943,6 +943,17 @@ AC_DEFUN
   esac
 fi
 
+# Unless configured otherwise, prefer standard rounding on x86 if available.
+# This makes x86 floating-point more consistent with other platforms.
+# To build for older x86 processors not supporting SSE2 (introduced in
+# 2000), invoke 'configure' with -mno-sse2 in CFLAGS.
+case "$host_cpu, $CC $CFLAGS " in
+  *' -mfpmath='* | *' -mno-sse2 '*) ;;
+  i?86,* | x86_64,*' -m32 '*)
+    gl_COMPILER_OPTION_IF([-msse2 -mfpmath=sse],
+      [CFLAGS="$CFLAGS -msse2 -mfpmath=sse"]);;
+esac
+
 # gl_GCC_VERSION_IFELSE([major], [minor], [run-if-found], [run-if-not-found])
 # ---------------------------------------------------------------------------
 # If $CPP is gcc-MAJOR.MINOR or newer, then run RUN-IF-FOUND.
diff --git a/etc/NEWS b/etc/NEWS
index fc5c215d2a..090ebd6312 100644
--- a/etc/NEWS
+++ b/etc/NEWS
@@ -58,6 +58,12 @@ shaping, so 'configure' now recommends that combination.
 ** The ftx font backend driver has been removed.
 It was declared obsolete in Emacs 27.1.
 
+---
+** On x86 with GCC-like compilers, Emac builds now assume SSE2 by default.
+This makes x86 floating-point more consistent with other platforms.
+To build for older x86 processors not supporting SSE2 (introduced in
+2000), invoke 'configure' with -mno-sse2 in CFLAGS.
+
 ---
 ** Emacs no longer supports old OpenBSD systems.
 OpenBSD 5.3 and older releases are no longer supported, as they lack
-- 
2.25.4