blob: c2fe9ed64308ea21e982baa5fa34f49dd17cda1b [file] [log] [blame]
James Kuszmaulb13e13f2023-11-22 20:44:04 -08001From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
James Kuszmaulcf324122023-01-14 14:07:17 -08002From: PJ Reiniger <pj.reiniger@gmail.com>
3Date: Tue, 3 May 2022 22:50:24 -0400
James Kuszmaulb13e13f2023-11-22 20:44:04 -08004Subject: [PATCH 15/31] Add lerp and sgn
James Kuszmaulcf324122023-01-14 14:07:17 -08005
6---
7 llvm/include/llvm/Support/MathExtras.h | 20 ++++++++++++++++++++
8 1 file changed, 20 insertions(+)
9
10diff --git a/llvm/include/llvm/Support/MathExtras.h b/llvm/include/llvm/Support/MathExtras.h
James Kuszmaulb13e13f2023-11-22 20:44:04 -080011index cdf859ccfaca22a04b08a351d7c2c9789a70627e..b82d9883c41008dcbbd933709c6e854ad74c5b58 100644
James Kuszmaulcf324122023-01-14 14:07:17 -080012--- a/llvm/include/llvm/Support/MathExtras.h
13+++ b/llvm/include/llvm/Support/MathExtras.h
James Kuszmaulb13e13f2023-11-22 20:44:04 -080014@@ -614,6 +614,26 @@ std::enable_if_t<std::is_signed_v<T>, T> MulOverflow(T X, T Y, T &Result) {
James Kuszmaulcf324122023-01-14 14:07:17 -080015 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