Skip to content

Commit 0b72899

Browse files
authored
[libc][math] Refactor the math_errhandling macro definition (llvm#166350)
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 95c8750 commit 0b72899

File tree

1 file changed

+27
-4
lines changed

1 file changed

+27
-4
lines changed

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

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,14 +42,37 @@
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(__FAST_MATH__) || \
55+
((defined(__arm__) || defined(_M_ARM) || defined(__thumb__) || \
56+
defined(__aarch64__) || defined(_M_ARM64)) && \
57+
!defined(__ARM_FP))
58+
#define __LIBC_SUPPORTS_MATH_ERREXCEPT 0
4959
#else
60+
#define __LIBC_SUPPORTS_MATH_ERREXCEPT 1
61+
#endif
62+
63+
#if __LIBC_SUPPORTS_MATH_ERRNO && __LIBC_SUPPORTS_MATH_ERREXCEPT
5064
#define math_errhandling (MATH_ERRNO | MATH_ERREXCEPT)
65+
#elif __LIBC_SUPPORTS_MATH_ERRNO
66+
#define math_errhandling (MATH_ERRNO)
67+
#elif __LIBC_SUPPORTS_MATH_ERREXCEPT
68+
#define math_errhandling (MATH_ERREXCEPT)
69+
#else
70+
#define math_errhandling 0
5171
#endif
5272

73+
#undef __LIBC_SUPPORTS_MATH_ERRNO
74+
#undef __LIBC_SUPPORTS_MATH_ERREXCEPT
75+
5376
// POSIX math constants
5477
// https://pubs.opengroup.org/onlinepubs/9799919799/basedefs/math.h.html
5578
#define M_E (__extension__ 0x1.5bf0a8b145769p1)

0 commit comments

Comments
 (0)