Implement 'lerp'; which is the last bit of P0811. Mark that paper as complete.
llvm-svn: 359211
Cr-Mirrored-From: sso://chromium.googlesource.com/_direct/external/github.com/llvm/llvm-project
Cr-Mirrored-Commit: b0e2daf64b8167dc7729cc7486f0c94c8902e9c6
diff --git a/include/cmath b/include/cmath
index 3af9f54..f618b1b 100644
--- a/include/cmath
+++ b/include/cmath
@@ -302,6 +302,7 @@
#include <__config>
#include <math.h>
+#include <algorithm>
#include <version>
#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
@@ -606,6 +607,32 @@
return isfinite(__lcpp_x);
}
+#if _LIBCPP_STD_VER > 17
+template <typename _Fp>
+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;
+}
+
+constexpr float
+lerp(float __a, float __b, float __t) _NOEXCEPT { return __lerp(__a, __b, __t); }
+
+constexpr double
+lerp(double __a, double __b, double __t) _NOEXCEPT { return __lerp(__a, __b, __t); }
+
+constexpr long double
+lerp(long double __a, long double __b, long double __t) _NOEXCEPT { return __lerp(__a, __b, __t); }
+
+#endif // _LIBCPP_STD_VER > 17
+
_LIBCPP_END_NAMESPACE_STD
#endif // _LIBCPP_CMATH