Skip to content

Conversation

@vhscampos
Copy link
Member

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).

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).
@llvmbot llvmbot added the libc label Nov 4, 2025
@llvmbot
Copy link
Member

llvmbot commented Nov 4, 2025

@llvm/pr-subscribers-libc

Author: Victor Campos (vhscampos)

Changes

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).


Full diff: https://github.com/llvm/llvm-project/pull/166350.diff

1 Files Affected:

  • (modified) libc/include/llvm-libc-macros/math-macros.h (+26-4)
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)

@vhscampos vhscampos requested a review from lntue November 4, 2025 13:16
@github-actions
Copy link

github-actions bot commented Nov 5, 2025

✅ With the latest revision this PR passed the C/C++ code formatter.

@vhscampos vhscampos merged commit 0b72899 into llvm:main Nov 5, 2025
20 checks passed
@vhscampos vhscampos deleted the libc-matherrhandling branch November 5, 2025 17:00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants