Skip to content

Commit 12e47aa

Browse files
authored
[libc] Add config option for fast math optimizations (#98029)
Summary: This patch adds `LIBC_COPT_MATH_OPTIMIZATIONS` that allows users to configure the different math optimizations.
1 parent 0577cda commit 12e47aa

File tree

5 files changed

+19
-2
lines changed

5 files changed

+19
-2
lines changed

libc/cmake/modules/LLVMLibCCompileOptionRules.cmake

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,9 @@ function(_get_common_compile_options output_var flags)
104104
list(APPEND compile_options "-Wthread-safety")
105105
list(APPEND compile_options "-Wglobal-constructors")
106106
endif()
107+
if(LIBC_CONF_MATH_OPTIMIZATIONS)
108+
list(APPEND compile_options "-DLIBC_MATH=${LIBC_CONF_MATH_OPTIMIZATIONS}")
109+
endif()
107110
elseif(MSVC)
108111
list(APPEND compile_options "/EHs-c-")
109112
list(APPEND compile_options "/GR-")

libc/config/config.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,5 +60,11 @@
6060
"value": 1073741824,
6161
"doc": "Default size for the constinit freelist buffer used for the freelist malloc implementation (default 1o 1GB)."
6262
}
63+
},
64+
"math": {
65+
"LIBC_CONF_MATH_OPTIMIZATIONS": {
66+
"value": 0,
67+
"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."
68+
}
6369
}
6470
}

libc/config/gpu/config.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,10 @@
1212
"LIBC_CONF_PRINTF_FLOAT_TO_STR_USE_MEGA_LONG_DOUBLE_TABLE": {
1313
"value": false
1414
}
15+
},
16+
"math": {
17+
"LIBC_CONF_MATH_OPTIMIZATIONS": {
18+
"value": "(LIBC_MATH_SKIP_ACCURATE_PASS | LIBC_MATH_SMALL_TABLES | LIBC_MATH_NO_ERRNO | LIBC_MATH_NO_EXCEPT)"
19+
}
1520
}
1621
}

libc/docs/configure.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ to learn about the defaults for your platform and target.
3030
- ``LIBC_CONF_KEEP_FRAME_POINTER``: Keep frame pointer in functions for better debugging experience.
3131
* **"malloc" options**
3232
- ``LIBC_CONF_FREELIST_MALLOC_BUFFER_SIZE``: Default size for the constinit freelist buffer used for the freelist malloc implementation (default 1o 1GB).
33+
* **"math" options**
34+
- ``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.
3335
* **"printf" options**
3436
- ``LIBC_CONF_PRINTF_DISABLE_FIXED_POINT``: Disable printing fixed point values in printf and friends.
3537
- ``LIBC_CONF_PRINTF_DISABLE_FLOAT``: Disable printing floating point values in printf and friends.

libc/src/math/generic/tan.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,8 @@ LIBC_INLINE DoubleDouble tan_eval(const DoubleDouble &u) {
127127
// Calculation a / b = a * (1/b) for Float128.
128128
// Using the initial approximation of q ~ (1/b), then apply 2 Newton-Raphson
129129
// iterations, before multiplying by a.
130-
[[maybe_unused]] Float128 newton_raphson_div(const Float128 &a, Float128 b, double q) {
130+
[[maybe_unused]] Float128 newton_raphson_div(const Float128 &a, Float128 b,
131+
double q) {
131132
Float128 q0(q);
132133
constexpr Float128 TWO(2.0);
133134
b.sign = (b.sign == Sign::POS) ? Sign::NEG : Sign::POS;
@@ -158,7 +159,7 @@ LLVM_LIBC_FUNCTION(double, tan, (double x)) {
158159
if (LIBC_UNLIKELY(x == 0.0))
159160
return x;
160161

161-
// For |x| < 2^-27, |tan(x) - x| < ulp(x)/2.
162+
// For |x| < 2^-27, |tan(x) - x| < ulp(x)/2.
162163
#ifdef LIBC_TARGET_CPU_HAS_FMA
163164
return fputil::multiply_add(x, 0x1.0p-54, x);
164165
#else

0 commit comments

Comments
 (0)