Skip to content

Commit 1de422d

Browse files
committed
[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).
1 parent 85c7827 commit 1de422d

File tree

1 file changed

+26
-4
lines changed

1 file changed

+26
-4
lines changed

libc/include/llvm-libc-macros/math-macros.h

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,14 +42,36 @@
4242
#define FP_LLOGBNAN LONG_MAX
4343
#endif
4444

45-
#if defined(__NVPTX__) || defined(__AMDGPU__) || defined(__FAST_MATH__)
46-
#define math_errhandling 0
47-
#elif defined(__NO_MATH_ERRNO__)
48-
#define math_errhandling (MATH_ERREXCEPT)
45+
// Math error handling. Target support is assumed to be existent unless
46+
// explicitly disabled.
47+
#if defined(__NVPTX__) || defined(__AMDGPU__) || defined(__FAST_MATH__) || \
48+
defined(__NO_MATH_ERRNO__)
49+
#define __LIBC_SUPPORTS_MATH_ERRNO 0
50+
#else
51+
#define __LIBC_SUPPORTS_MATH_ERRNO 1
52+
#endif
53+
54+
#if (defined(__arm__) || defined(_M_ARM) || defined(__thumb__) || \
55+
defined(__aarch64__) || defined(_M_ARM64)) && \
56+
!defined(__ARM_FP)
57+
#define __LIBC_SUPPORTS_MATH_ERREXCEPT 0
4958
#else
59+
#define __LIBC_SUPPORTS_MATH_ERREXCEPT 1
60+
#endif
61+
62+
#if __LIBC_SUPPORTS_MATH_ERRNO && __LIBC_SUPPORTS_MATH_ERREXCEPT
5063
#define math_errhandling (MATH_ERRNO | MATH_ERREXCEPT)
64+
#elif __LIBC_SUPPORTS_MATH_ERRNO
65+
#define math_errhandling (MATH_ERRNO)
66+
#elif __LIBC_SUPPORTS_MATH_ERREXCEPT
67+
#define math_errhandling (MATH_ERREXCEPT)
68+
#else
69+
#define math_errhandling 0
5170
#endif
5271

72+
#undef __LIBC_SUPPORTS_MATH_ERRNO
73+
#undef __LIBC_SUPPORTS_MATH_ERREXCEPT
74+
5375
// POSIX math constants
5476
// https://pubs.opengroup.org/onlinepubs/9799919799/basedefs/math.h.html
5577
#define M_E (__extension__ 0x1.5bf0a8b145769p1)

0 commit comments

Comments
 (0)