[libc-commits] [libc] [libc] Make fenv utility functions constexpr. (PR #150447)
via libc-commits
libc-commits at lists.llvm.org
Thu Jul 24 09:02:25 PDT 2025
https://github.com/lntue created https://github.com/llvm/llvm-project/pull/150447
None
>From 318c2674d24093671f6c63f1619c73e68667456b Mon Sep 17 00:00:00 2001
From: Tue Ly <lntue.h at gmail.com>
Date: Thu, 24 Jul 2025 16:00:22 +0000
Subject: [PATCH] [libc] Make fenv utility functions constexpr.
---
libc/src/__support/FPUtil/CMakeLists.txt | 1 +
libc/src/__support/FPUtil/FEnvImpl.h | 51 ++++++++++++++++--------
2 files changed, 36 insertions(+), 16 deletions(-)
diff --git a/libc/src/__support/FPUtil/CMakeLists.txt b/libc/src/__support/FPUtil/CMakeLists.txt
index 94f8b95542470..6e447fcd47368 100644
--- a/libc/src/__support/FPUtil/CMakeLists.txt
+++ b/libc/src/__support/FPUtil/CMakeLists.txt
@@ -7,6 +7,7 @@ add_header_library(
libc.hdr.fenv_macros
libc.hdr.math_macros
libc.hdr.stdint_proxy
+ libc.src.__support.CPP.type_traits
libc.src.__support.macros.attributes
libc.src.errno.errno
)
diff --git a/libc/src/__support/FPUtil/FEnvImpl.h b/libc/src/__support/FPUtil/FEnvImpl.h
index 76910880eb810..7bd56434e58fe 100644
--- a/libc/src/__support/FPUtil/FEnvImpl.h
+++ b/libc/src/__support/FPUtil/FEnvImpl.h
@@ -12,6 +12,7 @@
#include "hdr/fenv_macros.h"
#include "hdr/math_macros.h"
#include "hdr/types/fenv_t.h"
+#include "src/__support/CPP/type_traits.h"
#include "src/__support/libc_errno.h"
#include "src/__support/macros/attributes.h" // LIBC_INLINE
#include "src/__support/macros/config.h"
@@ -72,40 +73,58 @@ LIBC_INLINE int set_env(const fenv_t *) { return 0; }
namespace LIBC_NAMESPACE_DECL {
namespace fputil {
-LIBC_INLINE static int clear_except_if_required([[maybe_unused]] int excepts) {
+LIBC_INLINE static constexpr int
+clear_except_if_required([[maybe_unused]] int excepts) {
+ if (cpp::is_constant_evaluated()) {
+ return 0;
+ } else {
#ifndef LIBC_MATH_HAS_NO_EXCEPT
- if (math_errhandling & MATH_ERREXCEPT)
- return clear_except(excepts);
+ if (math_errhandling & MATH_ERREXCEPT)
+ return clear_except(excepts);
#endif // LIBC_MATH_HAS_NO_EXCEPT
- return 0;
+ return 0;
+ }
}
-LIBC_INLINE static int set_except_if_required([[maybe_unused]] int excepts) {
+LIBC_INLINE static constexpr int
+set_except_if_required([[maybe_unused]] int excepts) {
+ if (cpp::is_constant_evaluated()) {
+ return 0;
+ } else {
#ifndef LIBC_MATH_HAS_NO_EXCEPT
- if (math_errhandling & MATH_ERREXCEPT)
- return set_except(excepts);
+ if (math_errhandling & MATH_ERREXCEPT)
+ return set_except(excepts);
#endif // LIBC_MATH_HAS_NO_EXCEPT
- return 0;
+ return 0;
+ }
}
-LIBC_INLINE static int raise_except_if_required([[maybe_unused]] int excepts) {
+LIBC_INLINE static constexpr int
+raise_except_if_required([[maybe_unused]] int excepts) {
+ if (cpp::is_constant_evaluated()) {
+ return 0;
+ } else {
#ifndef LIBC_MATH_HAS_NO_EXCEPT
- if (math_errhandling & MATH_ERREXCEPT)
+ if (math_errhandling & MATH_ERREXCEPT)
#ifdef LIBC_TARGET_ARCH_IS_X86_64
- return raise_except</*SKIP_X87_FPU*/ true>(excepts);
+ return raise_except</*SKIP_X87_FPU*/ true>(excepts);
#else // !LIBC_TARGET_ARCH_IS_X86
- return raise_except(excepts);
+ return raise_except(excepts);
#endif // LIBC_TARGET_ARCH_IS_X86
#endif // LIBC_MATH_HAS_NO_EXCEPT
- return 0;
+ return 0;
+ }
}
-LIBC_INLINE static void set_errno_if_required([[maybe_unused]] int err) {
+LIBC_INLINE static constexpr void
+set_errno_if_required([[maybe_unused]] int err) {
+ if (!cpp::is_constant_evaluated()) {
#ifndef LIBC_MATH_HAS_NO_ERRNO
- if (math_errhandling & MATH_ERRNO)
- libc_errno = err;
+ if (math_errhandling & MATH_ERRNO)
+ libc_errno = err;
#endif // LIBC_MATH_HAS_NO_ERRNO
+ }
}
} // namespace fputil
More information about the libc-commits
mailing list