[libc-commits] [libc] [libc][math] Refactor the `math_errhandling` macro definition (PR #166350)
Victor Campos via libc-commits
libc-commits at lists.llvm.org
Wed Nov 5 02:48:54 PST 2025
https://github.com/vhscampos updated https://github.com/llvm/llvm-project/pull/166350
>From 1de422da92213fa16176750f9421c2ae84fd5c2e Mon Sep 17 00:00:00 2001
From: Victor Campos <victor.campos at arm.com>
Date: Mon, 3 Nov 2025 11:37:54 +0000
Subject: [PATCH 1/3] [libc][math] Refactor the `math_errhandling` macro
definition
This patch refactors the logic to define each component of the
`math_errhandling` macro.
It assumes that math error handling is supported by the target and the C
library unless otherwise disabled in the preprocessor logic.
In addition to the refactoring, the support for error handling via
exceptions is explicitly disabled for Arm targets with no FPU, that is,
where `__ARM_FP` is not defined. This is because LLVM libc does not
provide a floating-point environment for Arm no-FP configurations (or at
least one with support for FP exceptions).
---
libc/include/llvm-libc-macros/math-macros.h | 30 ++++++++++++++++++---
1 file changed, 26 insertions(+), 4 deletions(-)
diff --git a/libc/include/llvm-libc-macros/math-macros.h b/libc/include/llvm-libc-macros/math-macros.h
index 6697ce5b03851..9b9014b6dda6f 100644
--- a/libc/include/llvm-libc-macros/math-macros.h
+++ b/libc/include/llvm-libc-macros/math-macros.h
@@ -42,14 +42,36 @@
#define FP_LLOGBNAN LONG_MAX
#endif
-#if defined(__NVPTX__) || defined(__AMDGPU__) || defined(__FAST_MATH__)
-#define math_errhandling 0
-#elif defined(__NO_MATH_ERRNO__)
-#define math_errhandling (MATH_ERREXCEPT)
+// Math error handling. Target support is assumed to be existent unless
+// explicitly disabled.
+#if defined(__NVPTX__) || defined(__AMDGPU__) || defined(__FAST_MATH__) || \
+ defined(__NO_MATH_ERRNO__)
+#define __LIBC_SUPPORTS_MATH_ERRNO 0
+#else
+#define __LIBC_SUPPORTS_MATH_ERRNO 1
+#endif
+
+#if (defined(__arm__) || defined(_M_ARM) || defined(__thumb__) || \
+ defined(__aarch64__) || defined(_M_ARM64)) && \
+ !defined(__ARM_FP)
+#define __LIBC_SUPPORTS_MATH_ERREXCEPT 0
#else
+#define __LIBC_SUPPORTS_MATH_ERREXCEPT 1
+#endif
+
+#if __LIBC_SUPPORTS_MATH_ERRNO && __LIBC_SUPPORTS_MATH_ERREXCEPT
#define math_errhandling (MATH_ERRNO | MATH_ERREXCEPT)
+#elif __LIBC_SUPPORTS_MATH_ERRNO
+#define math_errhandling (MATH_ERRNO)
+#elif __LIBC_SUPPORTS_MATH_ERREXCEPT
+#define math_errhandling (MATH_ERREXCEPT)
+#else
+#define math_errhandling 0
#endif
+#undef __LIBC_SUPPORTS_MATH_ERRNO
+#undef __LIBC_SUPPORTS_MATH_ERREXCEPT
+
// POSIX math constants
// https://pubs.opengroup.org/onlinepubs/9799919799/basedefs/math.h.html
#define M_E (__extension__ 0x1.5bf0a8b145769p1)
>From caf8a66e590428cea456d7c5c1d47ec24a386c40 Mon Sep 17 00:00:00 2001
From: Victor Campos <victor.campos at arm.com>
Date: Wed, 5 Nov 2025 09:54:26 +0000
Subject: [PATCH 2/3] Add fast math as a condition for errhandling via
exceptions
---
libc/include/llvm-libc-macros/math-macros.h | 4 ++--
1 file changed, 2 insertions(+), 2 deletions(-)
diff --git a/libc/include/llvm-libc-macros/math-macros.h b/libc/include/llvm-libc-macros/math-macros.h
index 9b9014b6dda6f..9fda20f1144ff 100644
--- a/libc/include/llvm-libc-macros/math-macros.h
+++ b/libc/include/llvm-libc-macros/math-macros.h
@@ -51,9 +51,9 @@
#define __LIBC_SUPPORTS_MATH_ERRNO 1
#endif
-#if (defined(__arm__) || defined(_M_ARM) || defined(__thumb__) || \
+#if defined(__FAST_MATH__) || ((defined(__arm__) || defined(_M_ARM) || defined(__thumb__) || \
defined(__aarch64__) || defined(_M_ARM64)) && \
- !defined(__ARM_FP)
+ !defined(__ARM_FP))
#define __LIBC_SUPPORTS_MATH_ERREXCEPT 0
#else
#define __LIBC_SUPPORTS_MATH_ERREXCEPT 1
>From 8aa89bf4795c4982bf5766d7e5305b02eb208caf Mon Sep 17 00:00:00 2001
From: Victor Campos <victor.campos at arm.com>
Date: Wed, 5 Nov 2025 10:46:29 +0000
Subject: [PATCH 3/3] clang-format
---
libc/include/llvm-libc-macros/math-macros.h | 7 ++++---
1 file changed, 4 insertions(+), 3 deletions(-)
diff --git a/libc/include/llvm-libc-macros/math-macros.h b/libc/include/llvm-libc-macros/math-macros.h
index 9fda20f1144ff..e1b12e3010fe9 100644
--- a/libc/include/llvm-libc-macros/math-macros.h
+++ b/libc/include/llvm-libc-macros/math-macros.h
@@ -51,9 +51,10 @@
#define __LIBC_SUPPORTS_MATH_ERRNO 1
#endif
-#if defined(__FAST_MATH__) || ((defined(__arm__) || defined(_M_ARM) || defined(__thumb__) || \
- defined(__aarch64__) || defined(_M_ARM64)) && \
- !defined(__ARM_FP))
+#if defined(__FAST_MATH__) || \
+ ((defined(__arm__) || defined(_M_ARM) || defined(__thumb__) || \
+ defined(__aarch64__) || defined(_M_ARM64)) && \
+ !defined(__ARM_FP))
#define __LIBC_SUPPORTS_MATH_ERREXCEPT 0
#else
#define __LIBC_SUPPORTS_MATH_ERREXCEPT 1
More information about the libc-commits
mailing list