| 1 | //===----------------------------------------------------------------------===// |
| 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 _LIBCPP___CMATH_LERP_H |
| 10 | #define _LIBCPP___CMATH_LERP_H |
| 11 | |
| 12 | #include <__config> |
| 13 | #include <__type_traits/is_arithmetic.h> |
| 14 | #include <__type_traits/is_same.h> |
| 15 | #include <__type_traits/promote.h> |
| 16 | |
| 17 | #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER) |
| 18 | # pragma GCC system_header |
| 19 | #endif |
| 20 | |
| 21 | #if _LIBCPP_STD_VER >= 20 |
| 22 | |
| 23 | _LIBCPP_BEGIN_NAMESPACE_STD |
| 24 | |
| 25 | template <typename _Fp> |
| 26 | _LIBCPP_HIDE_FROM_ABI constexpr _Fp __lerp(_Fp __a, _Fp __b, _Fp __t) noexcept { |
| 27 | if ((__a <= 0 && __b >= 0) || (__a >= 0 && __b <= 0)) |
| 28 | return __t * __b + (1 - __t) * __a; |
| 29 | |
| 30 | if (__t == 1) |
| 31 | return __b; |
| 32 | const _Fp __x = __a + __t * (__b - __a); |
| 33 | if ((__t > 1) == (__b > __a)) |
| 34 | return __b < __x ? __x : __b; |
| 35 | else |
| 36 | return __x < __b ? __x : __b; |
| 37 | } |
| 38 | |
| 39 | _LIBCPP_HIDE_FROM_ABI inline constexpr float lerp(float __a, float __b, float __t) noexcept { |
| 40 | return __lerp(__a, __b, __t); |
| 41 | } |
| 42 | |
| 43 | _LIBCPP_HIDE_FROM_ABI inline constexpr double lerp(double __a, double __b, double __t) noexcept { |
| 44 | return __lerp(__a, __b, __t); |
| 45 | } |
| 46 | |
| 47 | _LIBCPP_HIDE_FROM_ABI inline constexpr long double lerp(long double __a, long double __b, long double __t) noexcept { |
| 48 | return __lerp(__a, __b, __t); |
| 49 | } |
| 50 | |
| 51 | template <class _A1, class _A2, class _A3> |
| 52 | requires(is_arithmetic_v<_A1> && is_arithmetic_v<_A2> && is_arithmetic_v<_A3>) |
| 53 | _LIBCPP_HIDE_FROM_ABI inline constexpr __promote_t<_A1, _A2, _A3> lerp(_A1 __a, _A2 __b, _A3 __t) noexcept { |
| 54 | using __result_type = __promote_t<_A1, _A2, _A3>; |
| 55 | static_assert(!( |
| 56 | _IsSame<_A1, __result_type>::value && _IsSame<_A2, __result_type>::value && _IsSame<_A3, __result_type>::value)); |
| 57 | return std::__lerp((__result_type)__a, (__result_type)__b, (__result_type)__t); |
| 58 | } |
| 59 | |
| 60 | _LIBCPP_END_NAMESPACE_STD |
| 61 | |
| 62 | #endif // _LIBCPP_STD_VER >= 20 |
| 63 | |
| 64 | #endif // _LIBCPP___CMATH_LERP_H |
| 65 | |