[libc-commits] [libc] [libc] Add config option for fast math optimizations (PR #98029)
Joseph Huber via libc-commits
libc-commits at lists.llvm.org
Mon Jul 8 08:56:37 PDT 2024
https://github.com/jhuber6 updated https://github.com/llvm/llvm-project/pull/98029
>From 814ee280db0a60e647c3a598f59834ffcd8aa1e4 Mon Sep 17 00:00:00 2001
From: Joseph Huber <huberjn at outlook.com>
Date: Mon, 8 Jul 2024 09:37:10 -0500
Subject: [PATCH] [libc] Add config option for fast math optimizations
Summary:
This patch adds `LIBC_COPT_MATH` that allows users to configure the
different math optimizations.
---
libc/cmake/modules/LLVMLibCCompileOptionRules.cmake | 3 +++
libc/config/config.json | 6 ++++++
libc/config/gpu/config.json | 5 +++++
libc/docs/configure.rst | 2 ++
libc/src/math/generic/tan.cpp | 5 +++--
5 files changed, 19 insertions(+), 2 deletions(-)
diff --git a/libc/cmake/modules/LLVMLibCCompileOptionRules.cmake b/libc/cmake/modules/LLVMLibCCompileOptionRules.cmake
index 6d38bb491044e..58813f50d101c 100644
--- a/libc/cmake/modules/LLVMLibCCompileOptionRules.cmake
+++ b/libc/cmake/modules/LLVMLibCCompileOptionRules.cmake
@@ -104,6 +104,9 @@ function(_get_common_compile_options output_var flags)
list(APPEND compile_options "-Wthread-safety")
list(APPEND compile_options "-Wglobal-constructors")
endif()
+ if(LIBC_CONF_MATH_OPTIMIZATIONS)
+ list(APPEND compile_options "-DLIBC_MATH=${LIBC_CONF_MATH_OPTIMIZATIONS}")
+ endif()
elseif(MSVC)
list(APPEND compile_options "/EHs-c-")
list(APPEND compile_options "/GR-")
diff --git a/libc/config/config.json b/libc/config/config.json
index 11433c15d762e..cf80f9993e595 100644
--- a/libc/config/config.json
+++ b/libc/config/config.json
@@ -60,5 +60,11 @@
"value": 1073741824,
"doc": "Default size for the constinit freelist buffer used for the freelist malloc implementation (default 1o 1GB)."
}
+ },
+ "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."
+ }
}
}
diff --git a/libc/config/gpu/config.json b/libc/config/gpu/config.json
index 53f232e31cc8a..73bf810efbac6 100644
--- a/libc/config/gpu/config.json
+++ b/libc/config/gpu/config.json
@@ -12,5 +12,10 @@
"LIBC_CONF_PRINTF_FLOAT_TO_STR_USE_MEGA_LONG_DOUBLE_TABLE": {
"value": false
}
+ },
+ "math": {
+ "LIBC_CONF_MATH_OPTIMIZATIONS": {
+ "value": "(LIBC_MATH_SKIP_ACCURATE_PASS | LIBC_MATH_SMALL_TABLES | LIBC_MATH_NO_ERRNO | LIBC_MATH_NO_EXCEPT)"
+ }
}
}
diff --git a/libc/docs/configure.rst b/libc/docs/configure.rst
index 016e2e5aa5876..d309d5b772038 100644
--- a/libc/docs/configure.rst
+++ b/libc/docs/configure.rst
@@ -30,6 +30,8 @@ to learn about the defaults for your platform and target.
- ``LIBC_CONF_KEEP_FRAME_POINTER``: Keep frame pointer in functions for better debugging experience.
* **"malloc" options**
- ``LIBC_CONF_FREELIST_MALLOC_BUFFER_SIZE``: Default size for the constinit freelist buffer used for the freelist malloc implementation (default 1o 1GB).
+* **"math" options**
+ - ``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.
* **"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.
diff --git a/libc/src/math/generic/tan.cpp b/libc/src/math/generic/tan.cpp
index 7f1385c4717dd..d153cc53acc80 100644
--- a/libc/src/math/generic/tan.cpp
+++ b/libc/src/math/generic/tan.cpp
@@ -127,7 +127,8 @@ LIBC_INLINE DoubleDouble tan_eval(const DoubleDouble &u) {
// Calculation a / b = a * (1/b) for Float128.
// Using the initial approximation of q ~ (1/b), then apply 2 Newton-Raphson
// iterations, before multiplying by a.
-[[maybe_unused]] Float128 newton_raphson_div(const Float128 &a, Float128 b, double q) {
+[[maybe_unused]] Float128 newton_raphson_div(const Float128 &a, Float128 b,
+ double q) {
Float128 q0(q);
constexpr Float128 TWO(2.0);
b.sign = (b.sign == Sign::POS) ? Sign::NEG : Sign::POS;
@@ -158,7 +159,7 @@ LLVM_LIBC_FUNCTION(double, tan, (double x)) {
if (LIBC_UNLIKELY(x == 0.0))
return x;
- // For |x| < 2^-27, |tan(x) - x| < ulp(x)/2.
+ // For |x| < 2^-27, |tan(x) - x| < ulp(x)/2.
#ifdef LIBC_TARGET_CPU_HAS_FMA
return fputil::multiply_add(x, 0x1.0p-54, x);
#else
More information about the libc-commits
mailing list