James Kuszmaul | b13e13f | 2023-11-22 20:44:04 -0800 | [diff] [blame^] | 1 | From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001 |
James Kuszmaul | cf32412 | 2023-01-14 14:07:17 -0800 | [diff] [blame] | 2 | From: PJ Reiniger <pj.reiniger@gmail.com> |
| 3 | Date: Tue, 3 May 2022 22:50:24 -0400 |
James Kuszmaul | b13e13f | 2023-11-22 20:44:04 -0800 | [diff] [blame^] | 4 | Subject: [PATCH 15/31] Add lerp and sgn |
James Kuszmaul | cf32412 | 2023-01-14 14:07:17 -0800 | [diff] [blame] | 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 |
James Kuszmaul | b13e13f | 2023-11-22 20:44:04 -0800 | [diff] [blame^] | 11 | index cdf859ccfaca22a04b08a351d7c2c9789a70627e..b82d9883c41008dcbbd933709c6e854ad74c5b58 100644 |
James Kuszmaul | cf32412 | 2023-01-14 14:07:17 -0800 | [diff] [blame] | 12 | --- a/llvm/include/llvm/Support/MathExtras.h |
| 13 | +++ b/llvm/include/llvm/Support/MathExtras.h |
James Kuszmaul | b13e13f | 2023-11-22 20:44:04 -0800 | [diff] [blame^] | 14 | @@ -614,6 +614,26 @@ std::enable_if_t<std::is_signed_v<T>, T> MulOverflow(T X, T Y, T &Result) { |
James Kuszmaul | cf32412 | 2023-01-14 14:07:17 -0800 | [diff] [blame] | 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 |