James Kuszmaul | cf32412 | 2023-01-14 14:07:17 -0800 | [diff] [blame^] | 1 | From 9ef10e5b331d00d7d5822afdb70c1f2d9981d772 Mon Sep 17 00:00:00 2001 |
| 2 | From: PJ Reiniger <pj.reiniger@gmail.com> |
| 3 | Date: Tue, 3 May 2022 22:50:24 -0400 |
| 4 | Subject: [PATCH 16/28] Add lerp and sgn |
| 5 | |
| 6 | --- |
| 7 | llvm/include/llvm/Support/MathExtras.h | 20 ++++++++++++++++++++ |
| 8 | 1 file changed, 20 insertions(+) |
| 9 | |
| 10 | diff --git a/llvm/include/llvm/Support/MathExtras.h b/llvm/include/llvm/Support/MathExtras.h |
| 11 | index e8f1f2aca..8116c58bd 100644 |
| 12 | --- a/llvm/include/llvm/Support/MathExtras.h |
| 13 | +++ b/llvm/include/llvm/Support/MathExtras.h |
| 14 | @@ -930,6 +930,26 @@ std::enable_if_t<std::is_signed<T>::value, T> MulOverflow(T X, T Y, T &Result) { |
| 15 | return UX > (static_cast<U>((std::numeric_limits<T>::max)())) / UY; |
| 16 | } |
| 17 | |
| 18 | +// Typesafe implementation of the signum function. |
| 19 | +// Returns -1 if negative, 1 if positive, 0 if 0. |
| 20 | +template <typename T> |
| 21 | +constexpr int sgn(T val) { |
| 22 | + return (T(0) < val) - (val < T(0)); |
| 23 | +} |
| 24 | + |
| 25 | +/** |
| 26 | + * Linearly interpolates between two values. |
| 27 | + * |
| 28 | + * @param startValue The start value. |
| 29 | + * @param endValue The end value. |
| 30 | + * @param t The fraction for interpolation. |
| 31 | + * |
| 32 | + * @return The interpolated value. |
| 33 | + */ |
| 34 | +template <typename T> |
| 35 | +constexpr T Lerp(const T& startValue, const T& endValue, double t) { |
| 36 | + return startValue + (endValue - startValue) * t; |
| 37 | +} |
| 38 | } // End llvm namespace |
| 39 | |
| 40 | #endif |