From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001 From: PJ Reiniger Date: Tue, 3 May 2022 22:50:24 -0400 Subject: [PATCH 15/36] Add lerp and sgn --- llvm/include/llvm/Support/MathExtras.h | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/llvm/include/llvm/Support/MathExtras.h b/llvm/include/llvm/Support/MathExtras.h index 60b3b9012a32dda5e7c3ba1dc5923ffa68df8215..daa79d99578e934ca001d1de5d2772ff961b8fc3 100644 --- a/llvm/include/llvm/Support/MathExtras.h +++ b/llvm/include/llvm/Support/MathExtras.h @@ -617,6 +617,26 @@ std::enable_if_t, T> MulOverflow(T X, T Y, T &Result) { return UX > (static_cast((std::numeric_limits::max)())) / UY; } +// Typesafe implementation of the signum function. +// Returns -1 if negative, 1 if positive, 0 if 0. +template +constexpr int sgn(T val) { + return (T(0) < val) - (val < T(0)); +} + +/** + * Linearly interpolates between two values. + * + * @param startValue The start value. + * @param endValue The end value. + * @param t The fraction for interpolation. + * + * @return The interpolated value. + */ +template +constexpr T Lerp(const T& startValue, const T& endValue, double t) { + return startValue + (endValue - startValue) * t; +} } // End llvm namespace #endif