[libc][math] Add LIBC_CONF_MATH_USE_SYSTEM_FENV / LIBC_MATH_USE_SYSTEM_FENV (#172902)

This is to allow math function implementations to use system libc's
fenv.h instead of internal fenv implementations.
This commit is contained in:
lntue 2025-12-22 13:19:56 -05:00 committed by GitHub
parent f60eec59fb
commit e2956fefd9
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
41 changed files with 141 additions and 2 deletions

View file

@ -123,6 +123,14 @@ function(_get_compile_options_from_config output_var)
list(APPEND config_options "-DLIBC_COPT_RAW_MUTEX_DEFAULT_SPIN_COUNT=${LIBC_CONF_RAW_MUTEX_DEFAULT_SPIN_COUNT}")
endif()
if(LIBC_CONF_MATH_USE_SYSTEM_FENV)
if(MSVC)
list(APPEND config_options "/DLIBC_MATH_USE_SYSTEM_FENV")
else()
list(APPEND config_options "-DLIBC_MATH_USE_SYSTEM_FENV")
endif()
endif()
set(${output_var} ${config_options} PARENT_SCOPE)
endfunction(_get_compile_options_from_config)

View file

@ -105,11 +105,15 @@
"math": {
"LIBC_CONF_MATH_OPTIMIZATIONS": {
"value": 0,
"doc": "Configures optimizations for math functions. Values accepted are LIBC_MATH_SKIP_ACCURATE_PASS, LIBC_MATH_SMALL_TABLES, LIBC_MATH_NO_ERRNO, LIBC_MATH_NO_EXCEPT, and LIBC_MATH_FAST."
"doc": "Configure optimizations for math functions. Values accepted are LIBC_MATH_SKIP_ACCURATE_PASS, LIBC_MATH_SMALL_TABLES, LIBC_MATH_NO_ERRNO, LIBC_MATH_NO_EXCEPT, and LIBC_MATH_FAST."
},
"LIBC_CONF_FREXP_INF_NAN_EXPONENT": {
"value": "",
"doc": "The value written back to the second parameter when calling frexp/frexpf/frexpl` with `+/-Inf`/`NaN` is unspecified. Configure an explicit exp value for Inf/NaN inputs."
},
"LIBC_CONF_MATH_USE_SYSTEM_FENV": {
"value": false,
"doc": "Use C standard fenv.h calls from the system libc instead our internal fenv implementations."
}
},
"qsort": {

View file

@ -36,7 +36,8 @@ to learn about the defaults for your platform and target.
- ``LIBC_ADD_NULL_CHECKS``: Add nullptr checks in the library's implementations to some functions for which passing nullptr is undefined behavior.
* **"math" options**
- ``LIBC_CONF_FREXP_INF_NAN_EXPONENT``: The value written back to the second parameter when calling frexp/frexpf/frexpl` with `+/-Inf`/`NaN` is unspecified. Configure an explicit exp value for Inf/NaN inputs.
- ``LIBC_CONF_MATH_OPTIMIZATIONS``: Configures optimizations for math functions. Values accepted are LIBC_MATH_SKIP_ACCURATE_PASS, LIBC_MATH_SMALL_TABLES, LIBC_MATH_NO_ERRNO, LIBC_MATH_NO_EXCEPT, and LIBC_MATH_FAST.
- ``LIBC_CONF_MATH_OPTIMIZATIONS``: Configure optimizations for math functions. Values accepted are LIBC_MATH_SKIP_ACCURATE_PASS, LIBC_MATH_SMALL_TABLES, LIBC_MATH_NO_ERRNO, LIBC_MATH_NO_EXCEPT, and LIBC_MATH_FAST.
- ``LIBC_CONF_MATH_USE_SYSTEM_FENV``: Use C standard fenv.h calls from the system libc instead our internal fenv implementations.
* **"printf" options**
- ``LIBC_CONF_PRINTF_DISABLE_FIXED_POINT``: Disable printing fixed point values in printf and friends.
- ``LIBC_CONF_PRINTF_DISABLE_FLOAT``: Disable printing floating point values in printf and friends.

View file

@ -19,6 +19,11 @@
#define LIBC_ERRNO_MODE LIBC_ERRNO_MODE_SYSTEM_INLINE
#endif // LIBC_ERRNO_MODE
// Use system fenv functions in math implementations.
#ifndef LIBC_MATH_USE_SYSTEM_FENV
#define LIBC_MATH_USE_SYSTEM_FENV
#endif // LIBC_MATH_USE_SYSTEM_FENV
#ifndef LIBC_NAMESPACE
#define LIBC_NAMESPACE __llvm_libc
#endif // LIBC_NAMESPACE

View file

@ -20,6 +20,51 @@
#include "src/__support/macros/properties/architectures.h"
#include "src/__support/macros/properties/compiler.h"
// In full build mode we are the system fenv in libc.
#if defined(LIBC_FULL_BUILD)
#undef LIBC_MATH_USE_SYSTEM_FENV
#endif // LIBC_FULL_BUILD
#if defined(LIBC_MATH_USE_SYSTEM_FENV)
// Simply call the system libc fenv.h functions, only for those that are used in
// math function implementations.
// To be used as an option for math function implementation, not to be used to
// implement fenv.h functions themselves.
#include <fenv.h>
namespace LIBC_NAMESPACE_DECL {
namespace fputil {
LIBC_INLINE int clear_except(int excepts) { return feclearexcept(excepts); }
LIBC_INLINE int test_except(int excepts) { return fetestexcept(excepts); }
LIBC_INLINE int get_except() {
fexcept_t excepts = 0;
fegetexceptflag(&excepts, FE_ALL_EXCEPT);
return static_cast<int>(excepts);
}
LIBC_INLINE int set_except(int excepts) {
fexcept_t exc = static_cast<fexcept_t>(excepts);
return fesetexceptflag(&exc, FE_ALL_EXCEPT);
}
LIBC_INLINE int raise_except(int excepts) { return feraiseexcept(excepts); }
LIBC_INLINE int get_round() { return fegetround(); }
LIBC_INLINE int set_round(int rounding_mode) {
return fesetround(rounding_mode);
}
} // namespace fputil
} // namespace LIBC_NAMESPACE_DECL
#else // !LIBC_MATH_USE_SYSTEM_FENV
#if defined(LIBC_TARGET_ARCH_IS_AARCH64) && defined(__ARM_FP)
#if defined(__APPLE__)
#include "aarch64/fenv_darwin_impl.h"
@ -73,6 +118,8 @@ LIBC_INLINE int set_env(const fenv_t *) { return 0; }
} // namespace LIBC_NAMESPACE_DECL
#endif
#endif // LIBC_MATH_USE_SYSTEM_FENV
namespace LIBC_NAMESPACE_DECL {
namespace fputil {

View file

@ -6,6 +6,8 @@
//
//===----------------------------------------------------------------------===//
#undef LIBC_MATH_USE_SYSTEM_FENV
#include "src/fenv/feclearexcept.h"
#include "src/__support/FPUtil/FEnvImpl.h"
#include "src/__support/common.h"

View file

@ -6,6 +6,8 @@
//
//===----------------------------------------------------------------------===//
#undef LIBC_MATH_USE_SYSTEM_FENV
#include "src/fenv/fedisableexcept.h"
#include "src/__support/FPUtil/FEnvImpl.h"
#include "src/__support/common.h"

View file

@ -6,6 +6,8 @@
//
//===----------------------------------------------------------------------===//
#undef LIBC_MATH_USE_SYSTEM_FENV
#include "src/fenv/feenableexcept.h"
#include "src/__support/FPUtil/FEnvImpl.h"
#include "src/__support/common.h"

View file

@ -6,6 +6,8 @@
//
//===----------------------------------------------------------------------===//
#undef LIBC_MATH_USE_SYSTEM_FENV
#include "src/fenv/fegetenv.h"
#include "src/__support/FPUtil/FEnvImpl.h"
#include "src/__support/common.h"

View file

@ -6,6 +6,8 @@
//
//===----------------------------------------------------------------------===//
#undef LIBC_MATH_USE_SYSTEM_FENV
#include "src/fenv/fegetexcept.h"
#include "src/__support/FPUtil/FEnvImpl.h"
#include "src/__support/common.h"

View file

@ -6,6 +6,8 @@
//
//===----------------------------------------------------------------------===//
#undef LIBC_MATH_USE_SYSTEM_FENV
#include "src/fenv/fegetexceptflag.h"
#include "hdr/types/fexcept_t.h"
#include "src/__support/FPUtil/FEnvImpl.h"

View file

@ -6,6 +6,8 @@
//
//===----------------------------------------------------------------------===//
#undef LIBC_MATH_USE_SYSTEM_FENV
#include "src/fenv/fegetround.h"
#include "src/__support/FPUtil/FEnvImpl.h"
#include "src/__support/common.h"

View file

@ -6,6 +6,8 @@
//
//===----------------------------------------------------------------------===//
#undef LIBC_MATH_USE_SYSTEM_FENV
#include "src/fenv/feholdexcept.h"
#include "hdr/types/fenv_t.h"
#include "src/__support/FPUtil/FEnvImpl.h"

View file

@ -6,6 +6,8 @@
//
//===----------------------------------------------------------------------===//
#undef LIBC_MATH_USE_SYSTEM_FENV
#include "src/fenv/feraiseexcept.h"
#include "src/__support/FPUtil/FEnvImpl.h"
#include "src/__support/common.h"

View file

@ -6,6 +6,8 @@
//
//===----------------------------------------------------------------------===//
#undef LIBC_MATH_USE_SYSTEM_FENV
#include "src/fenv/fesetenv.h"
#include "src/__support/FPUtil/FEnvImpl.h"
#include "src/__support/common.h"

View file

@ -6,6 +6,8 @@
//
//===----------------------------------------------------------------------===//
#undef LIBC_MATH_USE_SYSTEM_FENV
#include "src/fenv/fesetexcept.h"
#include "src/__support/FPUtil/FEnvImpl.h"
#include "src/__support/common.h"

View file

@ -6,6 +6,8 @@
//
//===----------------------------------------------------------------------===//
#undef LIBC_MATH_USE_SYSTEM_FENV
#include "src/fenv/fesetexceptflag.h"
#include "hdr/types/fexcept_t.h"
#include "src/__support/FPUtil/FEnvImpl.h"

View file

@ -6,6 +6,8 @@
//
//===----------------------------------------------------------------------===//
#undef LIBC_MATH_USE_SYSTEM_FENV
#include "src/fenv/fesetround.h"
#include "src/__support/FPUtil/FEnvImpl.h"
#include "src/__support/common.h"

View file

@ -6,6 +6,8 @@
//
//===----------------------------------------------------------------------===//
#undef LIBC_MATH_USE_SYSTEM_FENV
#include "src/fenv/fetestexcept.h"
#include "src/__support/FPUtil/FEnvImpl.h"
#include "src/__support/common.h"

View file

@ -6,6 +6,8 @@
//
//===----------------------------------------------------------------------===//
#undef LIBC_MATH_USE_SYSTEM_FENV
#include "src/fenv/fetestexceptflag.h"
#include "hdr/types/fexcept_t.h"
#include "src/__support/FPUtil/FEnvImpl.h"

View file

@ -6,6 +6,8 @@
//
//===----------------------------------------------------------------------===//
#undef LIBC_MATH_USE_SYSTEM_FENV
#include "src/fenv/feupdateenv.h"
#include "src/__support/FPUtil/FEnvImpl.h"
#include "src/__support/common.h"

View file

@ -6,6 +6,10 @@
//
//===---------------------------------------------------------------------===//
#ifdef LIBC_MATH_USE_SYSTEM_FENV
#undef LIBC_MATH_USE_SYSTEM_FENV
#endif // LIBC_MATH_USE_SYSTEM_FENV
#include "FEnvSafeTest.h"
#include "src/__support/FPUtil/FEnvImpl.h"

View file

@ -6,6 +6,8 @@
//
//===----------------------------------------------------------------------===//
#undef LIBC_MATH_USE_SYSTEM_FENV
#include "FPExceptMatcher.h"
#include "src/__support/macros/config.h"

View file

@ -9,6 +9,8 @@
#ifndef LLVM_LIBC_TEST_UNITTEST_FPMATCHER_H
#define LLVM_LIBC_TEST_UNITTEST_FPMATCHER_H
#undef LIBC_MATH_USE_SYSTEM_FENV
#include "src/__support/CPP/array.h"
#include "src/__support/CPP/type_traits.h"
#include "src/__support/FPUtil/FEnvImpl.h"

View file

@ -6,6 +6,8 @@
//
//===----------------------------------------------------------------------===//
#undef LIBC_MATH_USE_SYSTEM_FENV
#include "RoundingModeUtils.h"
#include "src/__support/FPUtil/FEnvImpl.h"
#include "src/__support/FPUtil/rounding_mode.h"

View file

@ -6,6 +6,8 @@
//
//===----------------------------------------------------------------------===//
#undef LIBC_MATH_USE_SYSTEM_FENV
#include "src/fenv/feclearexcept.h"
#include "src/fenv/feraiseexcept.h"
#include "src/fenv/fetestexcept.h"

View file

@ -6,6 +6,8 @@
//
//===----------------------------------------------------------------------===//
#undef LIBC_MATH_USE_SYSTEM_FENV
#include "hdr/types/fexcept_t.h"
#include "src/fenv/fegetexceptflag.h"
#include "src/fenv/fesetexceptflag.h"

View file

@ -7,6 +7,8 @@
//
//===----------------------------------------------------------------------===//
#undef LIBC_MATH_USE_SYSTEM_FENV
#include "src/fenv/feclearexcept.h"
#include "src/fenv/feraiseexcept.h"
#include "src/fenv/fesetexcept.h"

View file

@ -6,6 +6,8 @@
//
//===----------------------------------------------------------------------===//
#undef LIBC_MATH_USE_SYSTEM_FENV
#include "src/fenv/feclearexcept.h"
#include "src/__support/FPUtil/FEnvImpl.h"

View file

@ -6,6 +6,8 @@
//
//===----------------------------------------------------------------------===//
#undef LIBC_MATH_USE_SYSTEM_FENV
#include "hdr/types/fenv_t.h"
#include "src/fenv/feholdexcept.h"

View file

@ -6,6 +6,8 @@
//
//===----------------------------------------------------------------------===//
#undef LIBC_MATH_USE_SYSTEM_FENV
#include "hdr/types/fenv_t.h"
#include "src/fenv/feupdateenv.h"

View file

@ -6,6 +6,8 @@
//
//===----------------------------------------------------------------------===//
#undef LIBC_MATH_USE_SYSTEM_FENV
#include "hdr/types/fenv_t.h"
#include "src/fenv/fegetenv.h"
#include "src/fenv/fegetround.h"

View file

@ -9,6 +9,8 @@
#ifndef LLVM_LIBC_TEST_SRC_MATH_RINTTEST_H
#define LLVM_LIBC_TEST_SRC_MATH_RINTTEST_H
#undef LIBC_MATH_USE_SYSTEM_FENV
#include "src/__support/CPP/algorithm.h"
#include "src/__support/FPUtil/FEnvImpl.h"
#include "src/__support/FPUtil/FPBits.h"

View file

@ -9,6 +9,8 @@
#ifndef LLVM_LIBC_TEST_SRC_MATH_ROUNDTOINTEGERTEST_H
#define LLVM_LIBC_TEST_SRC_MATH_ROUNDTOINTEGERTEST_H
#undef LIBC_MATH_USE_SYSTEM_FENV
#include "src/__support/CPP/algorithm.h"
#include "src/__support/FPUtil/FEnvImpl.h"
#include "src/__support/FPUtil/FPBits.h"

View file

@ -9,6 +9,8 @@
#ifndef LLVM_LIBC_TEST_SRC_MATH_SMOKE_CANONICALIZETEST_H
#define LLVM_LIBC_TEST_SRC_MATH_SMOKE_CANONICALIZETEST_H
#undef LIBC_MATH_USE_SYSTEM_FENV
#include "src/__support/FPUtil/FEnvImpl.h"
#include "src/__support/FPUtil/FPBits.h"
#include "src/__support/integer_literals.h"

View file

@ -9,6 +9,8 @@
#ifndef LLVM_LIBC_TEST_SRC_MATH_FMODTEST_H
#define LLVM_LIBC_TEST_SRC_MATH_FMODTEST_H
#undef LIBC_MATH_USE_SYSTEM_FENV
#include "hdr/errno_macros.h"
#include "hdr/fenv_macros.h"
#include "src/__support/FPUtil/FEnvImpl.h"

View file

@ -9,6 +9,8 @@
#ifndef LLVM_LIBC_TEST_SRC_MATH_NEARBYINTTEST_H
#define LLVM_LIBC_TEST_SRC_MATH_NEARBYINTTEST_H
#undef LIBC_MATH_USE_SYSTEM_FENV
#include "hdr/fenv_macros.h"
#include "src/__support/FPUtil/FEnvImpl.h"
#include "src/__support/FPUtil/FPBits.h"

View file

@ -9,6 +9,8 @@
#ifndef LLVM_LIBC_TEST_SRC_MATH_NEXTAFTERTEST_H
#define LLVM_LIBC_TEST_SRC_MATH_NEXTAFTERTEST_H
#undef LIBC_MATH_USE_SYSTEM_FENV
#include "src/__support/CPP/bit.h"
#include "src/__support/FPUtil/FEnvImpl.h"
#include "src/__support/FPUtil/FPBits.h"

View file

@ -9,6 +9,8 @@
#ifndef LLVM_LIBC_TEST_SRC_MATH_NEXTTOWARDTEST_H
#define LLVM_LIBC_TEST_SRC_MATH_NEXTTOWARDTEST_H
#undef LIBC_MATH_USE_SYSTEM_FENV
#include "src/__support/CPP/bit.h"
#include "src/__support/FPUtil/FEnvImpl.h"
#include "src/__support/FPUtil/FPBits.h"

View file

@ -9,6 +9,8 @@
#ifndef LLVM_LIBC_TEST_SRC_MATH_SMOKE_RINTTEST_H
#define LLVM_LIBC_TEST_SRC_MATH_SMOKE_RINTTEST_H
#undef LIBC_MATH_USE_SYSTEM_FENV
#include "src/__support/FPUtil/FEnvImpl.h"
#include "src/__support/FPUtil/FPBits.h"
#include "test/UnitTest/FEnvSafeTest.h"

View file

@ -9,6 +9,8 @@
#ifndef LLVM_LIBC_TEST_SRC_MATH_SMOKE_ROUNDTOINTEGERTEST_H
#define LLVM_LIBC_TEST_SRC_MATH_SMOKE_ROUNDTOINTEGERTEST_H
#undef LIBC_MATH_USE_SYSTEM_FENV
#include "src/__support/CPP/algorithm.h"
#include "src/__support/FPUtil/FEnvImpl.h"
#include "src/__support/FPUtil/FPBits.h"