From bbf1c1c9b959444b799e14f0e101636efa38699b Mon Sep 17 00:00:00 2001
From: Bertrand Drouvot <bertranddrouvot.pg@gmail.com>
Date: Fri, 13 Mar 2026 07:47:29 +0000
Subject: [PATCH v2] Move -ffast-math defense to float.c and remove the
 autoconf check

Move the defense to float.c, which is a more appropriate place: -ffast-math
breaks isnan() and isinf() tests and near-overflow handling, and b6aa17e0ae3
de-supported floating-point timestamps.

The check is placed in a .c file rather than a header file to allow third-party
extensions to use -ffast-math if they need to.

Also remove the check from configure as it is no longer needed (it was added to
catch Linux distributions that compiled everything with -ffast-math but those
distributions are gone).

Author: Bertrand Drouvot <bertranddrouvot.pg@gmail.com>
Suggested-by: Tom Lane <tgl@sss.pgh.pa.us>
Suggested-by: Andres Freund <andres@anarazel.de>
Suggested-by: Peter Eisentraut <peter@eisentraut.org>
Discussion: https://postgr.es/m/abFXfKC8zR0Oclon%40ip-10-97-1-34.eu-west-3.compute.internal
---
 configure                                  | 23 ----------------------
 configure.ac                               |  7 -------
 src/backend/utils/adt/date.c               |  9 ---------
 src/backend/utils/adt/float.c              |  9 +++++++++
 src/backend/utils/adt/timestamp.c          |  8 --------
 src/interfaces/ecpg/pgtypeslib/interval.c  |  4 ----
 src/interfaces/ecpg/pgtypeslib/timestamp.c |  4 ----
 7 files changed, 9 insertions(+), 55 deletions(-)
  50.7% src/backend/utils/adt/
  10.0% src/interfaces/ecpg/pgtypeslib/

diff --git a/configure b/configure
index 42621ecd051..af381018201 100755
--- a/configure
+++ b/configure
@@ -7700,29 +7700,6 @@ fi
 rm -f core conftest.err conftest.$ac_objext \
     conftest$ac_exeext conftest.$ac_ext
 
-# Defend against gcc -ffast-math
-if test "$GCC" = yes; then
-cat confdefs.h - <<_ACEOF >conftest.$ac_ext
-/* end confdefs.h.  */
-
-int
-main ()
-{
-#ifdef __FAST_MATH__
-choke me
-#endif
-  ;
-  return 0;
-}
-_ACEOF
-if ac_fn_c_try_compile "$LINENO"; then :
-
-else
-  as_fn_error $? "do not put -ffast-math in CFLAGS" "$LINENO" 5
-fi
-rm -f core conftest.err conftest.$ac_objext conftest.$ac_ext
-fi
-
 # Defend against clang being used on x86-32 without SSE2 enabled.  As current
 # versions of clang do not understand -fexcess-precision=standard, the use of
 # x87 floating point operations leads to problems like isinf possibly returning
diff --git a/configure.ac b/configure.ac
index 61ec895d23c..4e5ee933024 100644
--- a/configure.ac
+++ b/configure.ac
@@ -786,13 +786,6 @@ AC_LINK_IFELSE([AC_LANG_PROGRAM([], [return 0;])],
   [AC_MSG_RESULT(no)
    AC_MSG_ERROR([cannot proceed])])
 
-# Defend against gcc -ffast-math
-if test "$GCC" = yes; then
-AC_COMPILE_IFELSE([AC_LANG_PROGRAM([], [@%:@ifdef __FAST_MATH__
-choke me
-@%:@endif])], [], [AC_MSG_ERROR([do not put -ffast-math in CFLAGS])])
-fi
-
 # Defend against clang being used on x86-32 without SSE2 enabled.  As current
 # versions of clang do not understand -fexcess-precision=standard, the use of
 # x87 floating point operations leads to problems like isinf possibly returning
diff --git a/src/backend/utils/adt/date.c b/src/backend/utils/adt/date.c
index 621b9175c12..933766ef128 100644
--- a/src/backend/utils/adt/date.c
+++ b/src/backend/utils/adt/date.c
@@ -38,15 +38,6 @@
 #include "utils/skipsupport.h"
 #include "utils/sortsupport.h"
 
-/*
- * gcc's -ffast-math switch breaks routines that expect exact results from
- * expressions like timeval / SECS_PER_HOUR, where timeval is double.
- */
-#ifdef __FAST_MATH__
-#error -ffast-math is known to break this code
-#endif
-
-
 /* common code for timetypmodin and timetztypmodin */
 static int32
 anytime_typmodin(bool istz, ArrayType *ta)
diff --git a/src/backend/utils/adt/float.c b/src/backend/utils/adt/float.c
index 641e7de21a0..9a09c27a429 100644
--- a/src/backend/utils/adt/float.c
+++ b/src/backend/utils/adt/float.c
@@ -28,6 +28,15 @@
 #include "utils/fmgrprotos.h"
 #include "utils/sortsupport.h"
 
+/*
+ * -ffast-math switch breaks isnan() and isinf() tests and near-overflow
+ * handling.
+ * This test is not done in the header file because we still want third-party
+ * extensions to be able to use -ffast-math if they need to.
+ */
+#ifdef __FAST_MATH__
+#error -ffast-math is known to break this code
+#endif
 
 /*
  * Configurable GUC parameter
diff --git a/src/backend/utils/adt/timestamp.c b/src/backend/utils/adt/timestamp.c
index 6f8cf29c910..afa15e8df08 100644
--- a/src/backend/utils/adt/timestamp.c
+++ b/src/backend/utils/adt/timestamp.c
@@ -40,14 +40,6 @@
 #include "utils/skipsupport.h"
 #include "utils/sortsupport.h"
 
-/*
- * gcc's -ffast-math switch breaks routines that expect exact results from
- * expressions like timeval / SECS_PER_HOUR, where timeval is double.
- */
-#ifdef __FAST_MATH__
-#error -ffast-math is known to break this code
-#endif
-
 /* Set at postmaster start */
 TimestampTz PgStartTime;
 
diff --git a/src/interfaces/ecpg/pgtypeslib/interval.c b/src/interfaces/ecpg/pgtypeslib/interval.c
index e452a088f9e..463455398f1 100644
--- a/src/interfaces/ecpg/pgtypeslib/interval.c
+++ b/src/interfaces/ecpg/pgtypeslib/interval.c
@@ -6,10 +6,6 @@
 #include <math.h>
 #include <limits.h>
 
-#ifdef __FAST_MATH__
-#error -ffast-math is known to break this code
-#endif
-
 #include "common/string.h"
 #include "dt.h"
 #include "pgtypes_error.h"
diff --git a/src/interfaces/ecpg/pgtypeslib/timestamp.c b/src/interfaces/ecpg/pgtypeslib/timestamp.c
index 7cf433266f4..9bf1b914553 100644
--- a/src/interfaces/ecpg/pgtypeslib/timestamp.c
+++ b/src/interfaces/ecpg/pgtypeslib/timestamp.c
@@ -7,10 +7,6 @@
 #include <limits.h>
 #include <math.h>
 
-#ifdef __FAST_MATH__
-#error -ffast-math is known to break this code
-#endif
-
 #include "common/int.h"
 #include "dt.h"
 #include "pgtypes_date.h"
-- 
2.34.1

