1//===-- Floating point environment manipulation functions -------*- C++ -*-===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8
9#ifndef LLVM_LIBC_SRC___SUPPORT_FPUTIL_FENVIMPL_H
10#define LLVM_LIBC_SRC___SUPPORT_FPUTIL_FENVIMPL_H
11
12#include "hdr/fenv_macros.h"
13#include "hdr/math_macros.h"
14#include "hdr/types/fenv_t.h"
15#include "src/__support/CPP/type_traits.h"
16#include "src/__support/libc_errno.h"
17#include "src/__support/macros/attributes.h" // LIBC_INLINE
18#include "src/__support/macros/config.h"
19#include "src/__support/macros/optimization.h"
20#include "src/__support/macros/properties/architectures.h"
21#include "src/__support/macros/properties/compiler.h"
22#include "src/__support/macros/properties/cpu_features.h"
23
24#ifdef LIBC_COMPILER_HAS_STDC_FENV_ACCESS
25#define LIBC_FENV_ACCESS_ON _Pragma("STDC FENV_ACCESS ON")
26#else
27#define LIBC_FENV_ACCESS_ON
28#endif
29
30// In full build mode we are the system fenv in libc.
31#if defined(LIBC_FULL_BUILD)
32#undef LIBC_MATH_USE_SYSTEM_FENV
33#endif // LIBC_FULL_BUILD
34
35#if defined(LIBC_MATH_USE_SYSTEM_FENV) && !defined(LIBC_MATH_HAS_NO_EXCEPT)
36
37// Simply call the system libc fenv.h functions, only for those that are used in
38// math function implementations.
39// To be used as an option for math function implementation, not to be used to
40// implement fenv.h functions themselves.
41
42#include <fenv.h>
43
44namespace LIBC_NAMESPACE_DECL {
45namespace fputil {
46
47LIBC_INLINE int clear_except(int excepts) {
48 LIBC_FENV_ACCESS_ON
49 return feclearexcept(excepts: excepts);
50}
51
52LIBC_INLINE int test_except(int excepts) {
53 LIBC_FENV_ACCESS_ON
54 return fetestexcept(excepts: excepts);
55}
56
57LIBC_INLINE int get_except() {
58 LIBC_FENV_ACCESS_ON
59 fexcept_t excepts = 0;
60 fegetexceptflag(flagp: &excepts, FE_ALL_EXCEPT);
61 return static_cast<int>(excepts);
62}
63
64LIBC_INLINE int set_except(int excepts) {
65 LIBC_FENV_ACCESS_ON
66 fexcept_t exc = static_cast<fexcept_t>(excepts);
67 return fesetexceptflag(flagp: &exc, FE_ALL_EXCEPT);
68}
69
70LIBC_INLINE int raise_except(int excepts) {
71 LIBC_FENV_ACCESS_ON
72 return feraiseexcept(excepts: excepts);
73}
74
75LIBC_INLINE int enable_except(int) { return 0; }
76
77LIBC_INLINE int disable_except(int) { return 0; }
78
79LIBC_INLINE int get_round() {
80 LIBC_FENV_ACCESS_ON
81 return fegetround();
82}
83
84LIBC_INLINE int set_round(int rounding_mode) {
85 LIBC_FENV_ACCESS_ON
86 return fesetround(rounding_direction: rounding_mode);
87}
88
89LIBC_INLINE int get_env(fenv_t *env) {
90 LIBC_FENV_ACCESS_ON
91 return fegetenv(envp: env);
92}
93
94LIBC_INLINE int set_env(const fenv_t *env) {
95 LIBC_FENV_ACCESS_ON
96 return fesetenv(envp: env);
97}
98
99} // namespace fputil
100} // namespace LIBC_NAMESPACE_DECL
101
102#else // !LIBC_MATH_USE_SYSTEM_FENV
103
104#if defined(LIBC_TARGET_ARCH_IS_AARCH64) && defined(__ARM_FP)
105#if defined(__APPLE__)
106#include "src/__support/FPUtil/aarch64/fenv_darwin_impl.h"
107#else
108#include "src/__support/FPUtil/aarch64/FEnvImpl.h"
109#endif
110
111// The extra !defined(APPLE) condition is to cause x86_64 MacOS builds to use
112// the dummy implementations below. Once a proper x86_64 darwin fenv is set up,
113// the apple condition here should be removed.
114// TODO: fully support fenv for MSVC.
115#elif defined(LIBC_TARGET_ARCH_IS_X86) && !defined(__APPLE__)
116#include "src/__support/FPUtil/x86_64/FEnvImpl.h"
117#elif defined(LIBC_TARGET_ARCH_IS_ARM) && defined(__ARM_FP) && \
118 !defined(LIBC_COMPILER_IS_MSVC)
119#include "src/__support/FPUtil/arm/FEnvImpl.h"
120#elif defined(LIBC_TARGET_ARCH_IS_ANY_RISCV) && defined(__riscv_flen)
121#include "src/__support/FPUtil/riscv/FEnvImpl.h"
122#elif defined(LIBC_TARGET_ARCH_IS_HEXAGON)
123#include "src/__support/FPUtil/hexagon/FEnvImpl.h"
124#else
125#define LIBC_FPUTIL_HAS_DUMMY_FENV_IMPL
126
127namespace LIBC_NAMESPACE_DECL {
128namespace fputil {
129
130// All dummy functions silently succeed.
131
132LIBC_INLINE int clear_except(int) { return 0; }
133
134LIBC_INLINE int test_except(int) { return 0; }
135
136LIBC_INLINE int get_except() { return 0; }
137
138LIBC_INLINE int set_except(int) { return 0; }
139
140LIBC_INLINE int raise_except(int) { return 0; }
141
142LIBC_INLINE int enable_except(int) { return 0; }
143
144LIBC_INLINE int disable_except(int) { return 0; }
145
146LIBC_INLINE int get_round() { return FE_TONEAREST; }
147
148LIBC_INLINE int set_round(int rounding_mode) {
149 return (rounding_mode == FE_TONEAREST) ? 0 : 1;
150}
151
152LIBC_INLINE int get_env(fenv_t *) { return 0; }
153
154LIBC_INLINE int set_env(const fenv_t *) { return 0; }
155
156} // namespace fputil
157} // namespace LIBC_NAMESPACE_DECL
158#endif
159
160#endif // LIBC_MATH_USE_SYSTEM_FENV
161
162namespace LIBC_NAMESPACE_DECL {
163namespace fputil {
164
165LIBC_INLINE LIBC_CONSTEXPR_DEFAULT int
166clear_except_if_required([[maybe_unused]] int excepts) {
167 if (cpp::is_constant_evaluated()) {
168 return 0;
169 } else {
170#ifndef LIBC_MATH_HAS_NO_EXCEPT
171 if (math_errhandling & MATH_ERREXCEPT)
172 return clear_except(excepts);
173#endif // LIBC_MATH_HAS_NO_EXCEPT
174 return 0;
175 }
176}
177
178LIBC_INLINE LIBC_CONSTEXPR_DEFAULT int
179set_except_if_required([[maybe_unused]] int excepts) {
180 if (cpp::is_constant_evaluated()) {
181 return 0;
182 } else {
183#ifndef LIBC_MATH_HAS_NO_EXCEPT
184 if (math_errhandling & MATH_ERREXCEPT)
185 return set_except(excepts);
186#endif // LIBC_MATH_HAS_NO_EXCEPT
187 return 0;
188 }
189}
190
191LIBC_INLINE LIBC_CONSTEXPR_DEFAULT int
192raise_except_if_required([[maybe_unused]] int excepts) {
193 if (cpp::is_constant_evaluated()) {
194 return 0;
195 } else {
196#ifndef LIBC_MATH_HAS_NO_EXCEPT
197 if (math_errhandling & MATH_ERREXCEPT)
198 return raise_except(excepts);
199#endif // LIBC_MATH_HAS_NO_EXCEPT
200 return 0;
201 }
202}
203
204LIBC_INLINE LIBC_CONSTEXPR_DEFAULT void
205set_errno_if_required([[maybe_unused]] int err) {
206 if (!cpp::is_constant_evaluated()) {
207#ifndef LIBC_MATH_HAS_NO_ERRNO
208 if (math_errhandling & MATH_ERRNO)
209 libc_errno = err;
210#endif // LIBC_MATH_HAS_NO_ERRNO
211 }
212}
213
214namespace internal {
215
216template <typename T> LIBC_INLINE_VAR constexpr bool HAS_HW_FPU = false;
217template <typename T> LIBC_INLINE_VAR constexpr T OVERFLOW_FACTOR = T(0);
218template <typename T> LIBC_INLINE_VAR constexpr T UNDERFLOW_FACTOR = T(0);
219
220#if defined(LIBC_TARGET_CPU_HAS_FPU_FLOAT)
221template <> LIBC_INLINE_VAR constexpr bool HAS_HW_FPU<float> = true;
222// 2^97 * (+/-2^97) = +/-2^194 (> max_normal = (2 - 2^-23) * 2^127).
223template <> LIBC_INLINE_VAR constexpr float OVERFLOW_FACTOR<float> = 0x1.0p+97f;
224// 2^-95 * (+/-2^-95) = +/-2^-190 (strictly inside (-2^-150, 2^-150)).
225template <>
226LIBC_INLINE_VAR constexpr float UNDERFLOW_FACTOR<float> = 0x1.0p-95f;
227#endif // LIBC_TARGET_CPU_HAS_FPU_FLOAT
228
229#if defined(LIBC_TARGET_CPU_HAS_FPU_DOUBLE)
230template <> LIBC_INLINE_VAR constexpr bool HAS_HW_FPU<double> = true;
231// 2^769 * (+/-2^769) = +/-2^1538 (> max_normal = (2 - 2^-52) * 2^1023).
232template <>
233LIBC_INLINE_VAR constexpr double OVERFLOW_FACTOR<double> = 0x1.0p+769;
234// 2^-767 * (+/-2^-767) = +/-2^-1534 (strictly inside (-2^-1075, 2^-1075)).
235template <>
236LIBC_INLINE_VAR constexpr double UNDERFLOW_FACTOR<double> = 0x1.0p-767;
237#endif // LIBC_TARGET_CPU_HAS_FPU_DOUBLE
238
239template <typename T>
240LIBC_INLINE_VAR const volatile T OVERFLOW_VAL[2] = {OVERFLOW_FACTOR<T>,
241 -OVERFLOW_FACTOR<T>};
242
243template <typename T>
244LIBC_INLINE_VAR const volatile T UNDERFLOW_VAL[2] = {UNDERFLOW_FACTOR<T>,
245 -UNDERFLOW_FACTOR<T>};
246
247} // namespace internal
248
249template <typename T>
250LIBC_INLINE LIBC_CONSTEXPR_DEFAULT void raise_overflow_except_if_required() {
251 raise_except_if_required(FE_OVERFLOW | FE_INEXACT);
252}
253
254template <typename T>
255LIBC_INLINE LIBC_CONSTEXPR_DEFAULT void raise_underflow_except_if_required() {
256 raise_except_if_required(FE_UNDERFLOW | FE_INEXACT);
257}
258
259#if !defined(LIBC_MATH_HAS_NO_EXCEPT) && \
260 !defined(LIBC_MATH_USE_SYSTEM_FENV) && \
261 !defined(LIBC_FPUTIL_HAS_DUMMY_FENV_IMPL)
262#if defined(LIBC_TARGET_CPU_HAS_FPU_FLOAT)
263template <>
264LIBC_INLINE LIBC_CONSTEXPR_DEFAULT void
265raise_overflow_except_if_required<float>() {
266 if (!cpp::is_constant_evaluated()) {
267 if (math_errhandling & MATH_ERREXCEPT) {
268 float v = internal::OVERFLOW_VAL<float>[0];
269 [[maybe_unused]] volatile float r = v * v;
270 }
271 }
272}
273
274template <>
275LIBC_INLINE LIBC_CONSTEXPR_DEFAULT void
276raise_underflow_except_if_required<float>() {
277 if (!cpp::is_constant_evaluated()) {
278 if (math_errhandling & MATH_ERREXCEPT) {
279 float v = internal::UNDERFLOW_VAL<float>[0];
280 [[maybe_unused]] volatile float r = v * v;
281 }
282 }
283}
284#endif // LIBC_TARGET_CPU_HAS_FPU_FLOAT
285
286#if defined(LIBC_TARGET_CPU_HAS_FPU_DOUBLE)
287template <>
288LIBC_INLINE LIBC_CONSTEXPR_DEFAULT void
289raise_overflow_except_if_required<double>() {
290 if (!cpp::is_constant_evaluated()) {
291 if (math_errhandling & MATH_ERREXCEPT) {
292 double v = internal::OVERFLOW_VAL<double>[0];
293 [[maybe_unused]] volatile double r = v * v;
294 }
295 }
296}
297
298template <>
299LIBC_INLINE LIBC_CONSTEXPR_DEFAULT void
300raise_underflow_except_if_required<double>() {
301 if (!cpp::is_constant_evaluated()) {
302 if (math_errhandling & MATH_ERREXCEPT) {
303 double v = internal::UNDERFLOW_VAL<double>[0];
304 [[maybe_unused]] volatile double r = v * v;
305 }
306 }
307}
308#endif // LIBC_TARGET_CPU_HAS_FPU_DOUBLE
309#endif // !LIBC_MATH_HAS_NO_EXCEPT && !LIBC_MATH_USE_SYSTEM_FENV &&
310 // !LIBC_FPUTIL_HAS_DUMMY_FENV_IMPL
311
312#undef LIBC_FPUTIL_HAS_DUMMY_FENV_IMPL
313
314} // namespace fputil
315} // namespace LIBC_NAMESPACE_DECL
316
317#endif // LLVM_LIBC_SRC___SUPPORT_FPUTIL_FENVIMPL_H
318