From e7d18411dfc6a74ebb59fb3d57f524bbd6e993e2 Mon Sep 17 00:00:00 2001 From: Sebastian Berg Date: Wed, 14 Jul 2021 16:53:34 -0500 Subject: [PATCH 1/3] BLD: Use `-ftrapping-math` for clang and for `_so` Apparently, there is a second `compiler_so` that is actually the main compiler being used, and modifying only the first one is just futile. The old try was `-ffp-exception-behavior=strict`, but that seems to be a fairly new addition (and doesn't even work on MacOS with 10.0) `-ftrapping-math` seems to be the older, equivalent option. --- numpy/distutils/ccompiler.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/numpy/distutils/ccompiler.py b/numpy/distutils/ccompiler.py index 061f4862dc19..6d063ee4e0b8 100644 --- a/numpy/distutils/ccompiler.py +++ b/numpy/distutils/ccompiler.py @@ -388,7 +388,8 @@ def CCompiler_customize_cmd(self, cmd, ignore=()): if hasattr(self, 'compiler') and 'clang' in self.compiler[0]: # clang defaults to a non-strict floating error point model. # Since NumPy and most Python libs give warnings for these, override: - self.compiler.append('-ffp-exception-behavior=strict') + self.compiler.append('-ftrapping-math') + self.compiler_so.append('-ftrapping-math') def allow(attr): return getattr(cmd, attr, None) is not None and attr not in ignore From 3b564f7defe5bbc427bbe407e4af752fa2d7e24b Mon Sep 17 00:00:00 2001 From: Sebastian Berg Date: Wed, 14 Jul 2021 21:03:43 -0500 Subject: [PATCH 2/3] DOC: Modify comment to note that the volatile can eventually be removed --- numpy/core/src/umath/loops_exponent_log.dispatch.c.src | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/numpy/core/src/umath/loops_exponent_log.dispatch.c.src b/numpy/core/src/umath/loops_exponent_log.dispatch.c.src index 41e0bf37b6f8..9970ad2ea994 100644 --- a/numpy/core/src/umath/loops_exponent_log.dispatch.c.src +++ b/numpy/core/src/umath/loops_exponent_log.dispatch.c.src @@ -149,8 +149,8 @@ fma_get_exponent(__m256 x) __m256 normal_mask = _mm256_cmp_ps(x, _mm256_set1_ps(FLT_MIN), _CMP_GE_OQ); /* - * It is necessary for temp1 to be volatile, a bug in clang optimizes it out which leads - * to an overflow warning in some cases. See https://github.com/numpy/numpy/issues/18005 + * The volatile is probably unnecessary now since we compile clang with + * `-ftrapping-math`: https://github.com/numpy/numpy/issues/18005 */ volatile __m256 temp1 = _mm256_blendv_ps(x, _mm256_set1_ps(0.0f), normal_mask); __m256 temp = _mm256_mul_ps(temp1, two_power_100); @@ -180,8 +180,8 @@ fma_get_mantissa(__m256 x) __m256 normal_mask = _mm256_cmp_ps(x, _mm256_set1_ps(FLT_MIN), _CMP_GE_OQ); /* - * It is necessary for temp1 to be volatile, a bug in clang optimizes it out which leads - * to an overflow warning in some cases. See https://github.com/numpy/numpy/issues/18005 + * The volatile is probably unnecessary now since we compile clang with + * `-ftrapping-math`: https://github.com/numpy/numpy/issues/18005 */ volatile __m256 temp1 = _mm256_blendv_ps(x, _mm256_set1_ps(0.0f), normal_mask); __m256 temp = _mm256_mul_ps(temp1, two_power_100); From f1d082cf69c1d3df5e40af0ac1b544b8ed56c52b Mon Sep 17 00:00:00 2001 From: Sebastian Berg Date: Thu, 15 Jul 2021 12:30:15 -0500 Subject: [PATCH 3/3] DOC: Add release note for `-ftrapping-math` on clang --- doc/release/upcoming_changes/19479.compatibility.rst | 7 +++++++ 1 file changed, 7 insertions(+) create mode 100644 doc/release/upcoming_changes/19479.compatibility.rst diff --git a/doc/release/upcoming_changes/19479.compatibility.rst b/doc/release/upcoming_changes/19479.compatibility.rst new file mode 100644 index 000000000000..83533a305749 --- /dev/null +++ b/doc/release/upcoming_changes/19479.compatibility.rst @@ -0,0 +1,7 @@ +Distutils forces strict floating point model on clang +----------------------------------------------------- +NumPy now sets the ``-ftrapping-math`` option on clang to enforce correct +floating point error handling for universal functions. +Clang defaults to non-IEEE and C99 conform behaviour otherwise. +This change (using the equivalent but newer ``-ffp-exception-behavior=strict``) +was attempted in NumPy 1.21, but was effectively never used.