Skip to content

Commit 252119e

Browse files
authored
[libc] Make fenv utility functions constexpr. (#150447)
1 parent 237a485 commit 252119e

File tree

2 files changed

+36
-16
lines changed

2 files changed

+36
-16
lines changed

libc/src/__support/FPUtil/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ add_header_library(
77
libc.hdr.fenv_macros
88
libc.hdr.math_macros
99
libc.hdr.stdint_proxy
10+
libc.src.__support.CPP.type_traits
1011
libc.src.__support.macros.attributes
1112
libc.src.errno.errno
1213
)

libc/src/__support/FPUtil/FEnvImpl.h

Lines changed: 35 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
#include "hdr/fenv_macros.h"
1313
#include "hdr/math_macros.h"
1414
#include "hdr/types/fenv_t.h"
15+
#include "src/__support/CPP/type_traits.h"
1516
#include "src/__support/libc_errno.h"
1617
#include "src/__support/macros/attributes.h" // LIBC_INLINE
1718
#include "src/__support/macros/config.h"
@@ -72,40 +73,58 @@ LIBC_INLINE int set_env(const fenv_t *) { return 0; }
7273
namespace LIBC_NAMESPACE_DECL {
7374
namespace fputil {
7475

75-
LIBC_INLINE static int clear_except_if_required([[maybe_unused]] int excepts) {
76+
LIBC_INLINE static constexpr int
77+
clear_except_if_required([[maybe_unused]] int excepts) {
78+
if (cpp::is_constant_evaluated()) {
79+
return 0;
80+
} else {
7681
#ifndef LIBC_MATH_HAS_NO_EXCEPT
77-
if (math_errhandling & MATH_ERREXCEPT)
78-
return clear_except(excepts);
82+
if (math_errhandling & MATH_ERREXCEPT)
83+
return clear_except(excepts);
7984
#endif // LIBC_MATH_HAS_NO_EXCEPT
80-
return 0;
85+
return 0;
86+
}
8187
}
8288

83-
LIBC_INLINE static int set_except_if_required([[maybe_unused]] int excepts) {
89+
LIBC_INLINE static constexpr int
90+
set_except_if_required([[maybe_unused]] int excepts) {
91+
if (cpp::is_constant_evaluated()) {
92+
return 0;
93+
} else {
8494
#ifndef LIBC_MATH_HAS_NO_EXCEPT
85-
if (math_errhandling & MATH_ERREXCEPT)
86-
return set_except(excepts);
95+
if (math_errhandling & MATH_ERREXCEPT)
96+
return set_except(excepts);
8797
#endif // LIBC_MATH_HAS_NO_EXCEPT
88-
return 0;
98+
return 0;
99+
}
89100
}
90101

91-
LIBC_INLINE static int raise_except_if_required([[maybe_unused]] int excepts) {
102+
LIBC_INLINE static constexpr int
103+
raise_except_if_required([[maybe_unused]] int excepts) {
104+
if (cpp::is_constant_evaluated()) {
105+
return 0;
106+
} else {
92107
#ifndef LIBC_MATH_HAS_NO_EXCEPT
93-
if (math_errhandling & MATH_ERREXCEPT)
108+
if (math_errhandling & MATH_ERREXCEPT)
94109
#ifdef LIBC_TARGET_ARCH_IS_X86_64
95-
return raise_except</*SKIP_X87_FPU*/ true>(excepts);
110+
return raise_except</*SKIP_X87_FPU*/ true>(excepts);
96111
#else // !LIBC_TARGET_ARCH_IS_X86
97-
return raise_except(excepts);
112+
return raise_except(excepts);
98113
#endif // LIBC_TARGET_ARCH_IS_X86
99114

100115
#endif // LIBC_MATH_HAS_NO_EXCEPT
101-
return 0;
116+
return 0;
117+
}
102118
}
103119

104-
LIBC_INLINE static void set_errno_if_required([[maybe_unused]] int err) {
120+
LIBC_INLINE static constexpr void
121+
set_errno_if_required([[maybe_unused]] int err) {
122+
if (!cpp::is_constant_evaluated()) {
105123
#ifndef LIBC_MATH_HAS_NO_ERRNO
106-
if (math_errhandling & MATH_ERRNO)
107-
libc_errno = err;
124+
if (math_errhandling & MATH_ERRNO)
125+
libc_errno = err;
108126
#endif // LIBC_MATH_HAS_NO_ERRNO
127+
}
109128
}
110129

111130
} // namespace fputil

0 commit comments

Comments
 (0)