[libcxx-commits] [libcxx] 2114567 - [libc++] Move lerp to its own header instead of <cmath> (#213111)
via libcxx-commits
libcxx-commits at lists.llvm.org
Wed Aug 5 18:59:52 PDT 2026
Author: Louis Dionne
Date: 2026-08-05T21:59:47-04:00
New Revision: 211456700cff55124923d6281fd0b4a156c9b3f4
URL: https://github.com/llvm/llvm-project/commit/211456700cff55124923d6281fd0b4a156c9b3f4
DIFF: https://github.com/llvm/llvm-project/commit/211456700cff55124923d6281fd0b4a156c9b3f4.diff
LOG: [libc++] Move lerp to its own header instead of <cmath> (#213111)
This moves <cmath> towards an umbrella header.
Co-authored-by: A. Jiang <de34 at live.cn>
Added:
libcxx/include/__cmath/lerp.h
Modified:
libcxx/include/CMakeLists.txt
libcxx/include/cmath
libcxx/include/complex
libcxx/include/module.modulemap.in
Removed:
################################################################################
diff --git a/libcxx/include/CMakeLists.txt b/libcxx/include/CMakeLists.txt
index 95d371c092983..f6911b8ff05c5 100644
--- a/libcxx/include/CMakeLists.txt
+++ b/libcxx/include/CMakeLists.txt
@@ -294,6 +294,7 @@ set(files
__chrono/year_month_day.h
__chrono/year_month_weekday.h
__chrono/zoned_time.h
+ __cmath/lerp.h
__cmath/special_functions.h
__compare/common_comparison_category.h
__compare/compare_partial_order_fallback.h
diff --git a/libcxx/include/__cmath/lerp.h b/libcxx/include/__cmath/lerp.h
new file mode 100644
index 0000000000000..4cbbcc570fb83
--- /dev/null
+++ b/libcxx/include/__cmath/lerp.h
@@ -0,0 +1,64 @@
+//===----------------------------------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef _LIBCPP___CMATH_LERP_H
+#define _LIBCPP___CMATH_LERP_H
+
+#include <__config>
+#include <__type_traits/is_arithmetic.h>
+#include <__type_traits/is_same.h>
+#include <__type_traits/promote.h>
+
+#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
+# pragma GCC system_header
+#endif
+
+#if _LIBCPP_STD_VER >= 20
+
+_LIBCPP_BEGIN_NAMESPACE_STD
+
+template <typename _Fp>
+_LIBCPP_HIDE_FROM_ABI constexpr _Fp __lerp(_Fp __a, _Fp __b, _Fp __t) noexcept {
+ if ((__a <= 0 && __b >= 0) || (__a >= 0 && __b <= 0))
+ return __t * __b + (1 - __t) * __a;
+
+ if (__t == 1)
+ return __b;
+ const _Fp __x = __a + __t * (__b - __a);
+ if ((__t > 1) == (__b > __a))
+ return __b < __x ? __x : __b;
+ else
+ return __x < __b ? __x : __b;
+}
+
+_LIBCPP_HIDE_FROM_ABI inline constexpr float lerp(float __a, float __b, float __t) _NOEXCEPT {
+ return __lerp(__a, __b, __t);
+}
+
+_LIBCPP_HIDE_FROM_ABI inline constexpr double lerp(double __a, double __b, double __t) _NOEXCEPT {
+ return __lerp(__a, __b, __t);
+}
+
+_LIBCPP_HIDE_FROM_ABI inline constexpr long double lerp(long double __a, long double __b, long double __t) _NOEXCEPT {
+ return __lerp(__a, __b, __t);
+}
+
+template <class _A1, class _A2, class _A3>
+ requires(is_arithmetic_v<_A1> && is_arithmetic_v<_A2> && is_arithmetic_v<_A3>)
+_LIBCPP_HIDE_FROM_ABI inline constexpr __promote_t<_A1, _A2, _A3> lerp(_A1 __a, _A2 __b, _A3 __t) noexcept {
+ using __result_type = __promote_t<_A1, _A2, _A3>;
+ static_assert(!(
+ _IsSame<_A1, __result_type>::value && _IsSame<_A2, __result_type>::value && _IsSame<_A3, __result_type>::value));
+ return std::__lerp((__result_type)__a, (__result_type)__b, (__result_type)__t);
+}
+
+_LIBCPP_END_NAMESPACE_STD
+
+#endif // _LIBCPP_STD_VER >= 20
+
+#endif // _LIBCPP___CMATH_LERP_H
diff --git a/libcxx/include/cmath b/libcxx/include/cmath
index a4139fb8181f8..c4f4030c2049b 100644
--- a/libcxx/include/cmath
+++ b/libcxx/include/cmath
@@ -317,12 +317,10 @@ constexpr long double lerp(long double a, long double b, long double t) noexcept
#else
# include <__config>
# include <__type_traits/enable_if.h>
-# include <__type_traits/is_arithmetic.h>
# include <__type_traits/is_floating_point.h>
-# include <__type_traits/is_same.h>
-# include <__type_traits/promote.h>
# include <version>
+# include <__cmath/lerp.h>
# include <__cmath/special_functions.h>
# include <math.h>
@@ -567,43 +565,6 @@ _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR bool __constexpr_isinf(_A1 __lcpp_x) _NO
return std::isinf(__lcpp_x);
}
-# if _LIBCPP_STD_VER >= 20
-template <typename _Fp>
-_LIBCPP_HIDE_FROM_ABI constexpr _Fp __lerp(_Fp __a, _Fp __b, _Fp __t) noexcept {
- if ((__a <= 0 && __b >= 0) || (__a >= 0 && __b <= 0))
- return __t * __b + (1 - __t) * __a;
-
- if (__t == 1)
- return __b;
- const _Fp __x = __a + __t * (__b - __a);
- if ((__t > 1) == (__b > __a))
- return __b < __x ? __x : __b;
- else
- return __x < __b ? __x : __b;
-}
-
-_LIBCPP_HIDE_FROM_ABI inline constexpr float lerp(float __a, float __b, float __t) _NOEXCEPT {
- return __lerp(__a, __b, __t);
-}
-
-_LIBCPP_HIDE_FROM_ABI inline constexpr double lerp(double __a, double __b, double __t) _NOEXCEPT {
- return __lerp(__a, __b, __t);
-}
-
-_LIBCPP_HIDE_FROM_ABI inline constexpr long double lerp(long double __a, long double __b, long double __t) _NOEXCEPT {
- return __lerp(__a, __b, __t);
-}
-
-template <class _A1, class _A2, class _A3>
- requires(is_arithmetic_v<_A1> && is_arithmetic_v<_A2> && is_arithmetic_v<_A3>)
-_LIBCPP_HIDE_FROM_ABI inline constexpr __promote_t<_A1, _A2, _A3> lerp(_A1 __a, _A2 __b, _A3 __t) noexcept {
- using __result_type = __promote_t<_A1, _A2, _A3>;
- static_assert(!(
- _IsSame<_A1, __result_type>::value && _IsSame<_A2, __result_type>::value && _IsSame<_A3, __result_type>::value));
- return std::__lerp((__result_type)__a, (__result_type)__b, (__result_type)__t);
-}
-# endif // _LIBCPP_STD_VER >= 20
-
_LIBCPP_END_NAMESPACE_STD
_LIBCPP_POP_MACROS
diff --git a/libcxx/include/complex b/libcxx/include/complex
index 03c3bb4686e81..b407bb70156e3 100644
--- a/libcxx/include/complex
+++ b/libcxx/include/complex
@@ -266,6 +266,11 @@ template<class T> complex<T> tanh (const complex<T>&);
# include <__tuple/tuple_element.h>
# include <__tuple/tuple_size.h>
# include <__type_traits/conditional.h>
+# include <__type_traits/is_arithmetic.h>
+# include <__type_traits/is_floating_point.h>
+# include <__type_traits/is_integral.h>
+# include <__type_traits/is_same.h>
+# include <__type_traits/promote.h>
# include <__utility/move.h>
# include <cmath>
# include <version>
diff --git a/libcxx/include/module.modulemap.in b/libcxx/include/module.modulemap.in
index 8e568e04f4d69..a5eac9fc149f0 100644
--- a/libcxx/include/module.modulemap.in
+++ b/libcxx/include/module.modulemap.in
@@ -20,6 +20,7 @@ module std_config {
module std_core {
module cmath {
+ module lerp { header "__cmath/lerp.h" }
module special_functions { header "__cmath/special_functions.h" }
}
More information about the libcxx-commits
mailing list