From ae7dca3107d11b604e3f50940d1d9f9cbb91d80f Mon Sep 17 00:00:00 2001
From: Simon Zeni <simon.zeni@collabora.com>
Date: Thu, 1 Aug 2024 16:42:26 -0400
Subject: [PATCH] aux/math: use int64_t for timestamps in m_relation_history

Part-of: <https://gitlab.freedesktop.org/monado/monado/-/merge_requests/2298>
---
 src/xrt/auxiliary/math/m_relation_history.cpp | 14 +++++++-------
 src/xrt/auxiliary/math/m_relation_history.h   | 16 +++++++---------
 src/xrt/auxiliary/tracking/t_tracker_slam.cpp |  4 ++--
 src/xrt/drivers/steamvr_lh/device.cpp         |  6 +++---
 tests/tests_history_buf.cpp                   |  4 ++--
 5 files changed, 21 insertions(+), 23 deletions(-)

diff --git a/src/xrt/auxiliary/math/m_relation_history.cpp b/src/xrt/auxiliary/math/m_relation_history.cpp
index a9617fcf2..0dfc4bdd3 100644
--- a/src/xrt/auxiliary/math/m_relation_history.cpp
+++ b/src/xrt/auxiliary/math/m_relation_history.cpp
@@ -37,7 +37,7 @@ namespace os = xrt::auxiliary::os;
 struct relation_history_entry
 {
 	struct xrt_space_relation relation;
-	uint64_t timestamp;
+	int64_t timestamp;
 };
 
 static constexpr size_t BufLen = 4096;
@@ -57,7 +57,7 @@ m_relation_history_create(struct m_relation_history **rh_ptr)
 }
 
 bool
-m_relation_history_push(struct m_relation_history *rh, struct xrt_space_relation const *in_relation, uint64_t timestamp)
+m_relation_history_push(struct m_relation_history *rh, struct xrt_space_relation const *in_relation, int64_t timestamp)
 {
 	XRT_TRACE_MARKER();
 	struct relation_history_entry rhe;
@@ -82,7 +82,7 @@ m_relation_history_push(struct m_relation_history *rh, struct xrt_space_relation
 
 enum m_relation_history_result
 m_relation_history_get(const struct m_relation_history *rh,
-                       uint64_t at_timestamp_ns,
+                       int64_t at_timestamp_ns,
                        struct xrt_space_relation *out_relation)
 {
 	XRT_TRACE_MARKER();
@@ -99,7 +99,7 @@ m_relation_history_get(const struct m_relation_history *rh,
 		// Find the first element *not less than* our value. the lambda we pass is the comparison
 		// function, to compare against timestamps.
 		const auto it =
-		    std::lower_bound(b, e, at_timestamp_ns, [](const relation_history_entry &rhe, uint64_t timestamp) {
+		    std::lower_bound(b, e, at_timestamp_ns, [](const relation_history_entry &rhe, int64_t timestamp) {
 			    return rhe.timestamp < timestamp;
 		    });
 
@@ -183,11 +183,11 @@ m_relation_history_get(const struct m_relation_history *rh,
 bool
 m_relation_history_estimate_motion(struct m_relation_history *rh,
                                    const struct xrt_space_relation *in_relation,
-                                   uint64_t timestamp,
+                                   int64_t timestamp,
                                    struct xrt_space_relation *out_relation)
 {
 
-	uint64_t last_time_ns;
+	int64_t last_time_ns;
 	struct xrt_space_relation last_relation;
 	if (!m_relation_history_get_latest(rh, &last_time_ns, &last_relation)) {
 		return false;
@@ -229,7 +229,7 @@ m_relation_history_estimate_motion(struct m_relation_history *rh,
 
 bool
 m_relation_history_get_latest(const struct m_relation_history *rh,
-                              uint64_t *out_time_ns,
+                              int64_t *out_time_ns,
                               struct xrt_space_relation *out_relation)
 {
 	std::unique_lock<os::Mutex> lock(rh->mutex);
diff --git a/src/xrt/auxiliary/math/m_relation_history.h b/src/xrt/auxiliary/math/m_relation_history.h
index 359877a8b..4a04b7109 100644
--- a/src/xrt/auxiliary/math/m_relation_history.h
+++ b/src/xrt/auxiliary/math/m_relation_history.h
@@ -60,9 +60,7 @@ m_relation_history_create(struct m_relation_history **rh);
  * @public @memberof m_relation_history
  */
 bool
-m_relation_history_push(struct m_relation_history *rh,
-                        struct xrt_space_relation const *in_relation,
-                        uint64_t timestamp);
+m_relation_history_push(struct m_relation_history *rh, struct xrt_space_relation const *in_relation, int64_t timestamp);
 
 /*!
  * Interpolates or extrapolates to the desired timestamp.
@@ -74,7 +72,7 @@ m_relation_history_push(struct m_relation_history *rh,
  */
 enum m_relation_history_result
 m_relation_history_get(const struct m_relation_history *rh,
-                       uint64_t at_timestamp_ns,
+                       int64_t at_timestamp_ns,
                        struct xrt_space_relation *out_relation);
 
 /*!
@@ -90,7 +88,7 @@ m_relation_history_get(const struct m_relation_history *rh,
 bool
 m_relation_history_estimate_motion(struct m_relation_history *rh,
                                    const struct xrt_space_relation *in_relation,
-                                   uint64_t timestamp,
+                                   int64_t timestamp,
                                    struct xrt_space_relation *out_relation);
 
 /*!
@@ -106,7 +104,7 @@ m_relation_history_estimate_motion(struct m_relation_history *rh,
  */
 bool
 m_relation_history_get_latest(const struct m_relation_history *rh,
-                              uint64_t *out_time_ns,
+                              int64_t *out_time_ns,
                               struct xrt_space_relation *out_relation);
 
 /*!
@@ -178,7 +176,7 @@ public:
 	 * @copydoc m_relation_history_push
 	 */
 	bool
-	push(xrt_space_relation const &relation, uint64_t ts) noexcept
+	push(xrt_space_relation const &relation, int64_t ts) noexcept
 	{
 		return m_relation_history_push(mPtr, &relation, ts);
 	}
@@ -187,7 +185,7 @@ public:
 	 * @copydoc m_relation_history_get
 	 */
 	Result
-	get(uint64_t at_time_ns, xrt_space_relation *out_relation) const noexcept
+	get(int64_t at_time_ns, xrt_space_relation *out_relation) const noexcept
 	{
 		return m_relation_history_get(mPtr, at_time_ns, out_relation);
 	}
@@ -196,7 +194,7 @@ public:
 	 * @copydoc m_relation_history_get_latest
 	 */
 	bool
-	get_latest(uint64_t *out_time_ns, xrt_space_relation *out_relation) const noexcept
+	get_latest(int64_t *out_time_ns, xrt_space_relation *out_relation) const noexcept
 	{
 		return m_relation_history_get_latest(mPtr, out_time_ns, out_relation);
 	}
diff --git a/src/xrt/auxiliary/tracking/t_tracker_slam.cpp b/src/xrt/auxiliary/tracking/t_tracker_slam.cpp
index 04e3b1355..72bbd3cf7 100644
--- a/src/xrt/auxiliary/tracking/t_tracker_slam.cpp
+++ b/src/xrt/auxiliary/tracking/t_tracker_slam.cpp
@@ -762,7 +762,7 @@ flush_poses(TrackerSlam &t)
 
 		// Last relation
 		xrt_space_relation lr = XRT_SPACE_RELATION_ZERO;
-		uint64_t lts;
+		int64_t lts;
 		t.slam_rels.get_latest(&lts, &lr);
 		xrt_vec3 lpos = lr.pose.position;
 		xrt_quat lrot = lr.pose.orientation;
@@ -907,7 +907,7 @@ predict_pose(TrackerSlam &t, timepoint_ns when_ns, struct xrt_space_relation *ou
 
 	// Get last relation computed purely from SLAM data
 	xrt_space_relation rel{};
-	uint64_t rel_ts;
+	int64_t rel_ts;
 	bool empty = !t.slam_rels.get_latest(&rel_ts, &rel);
 
 	// Stop if there is no previous relation to use for prediction
diff --git a/src/xrt/drivers/steamvr_lh/device.cpp b/src/xrt/drivers/steamvr_lh/device.cpp
index d983dbbec..72eefb4bd 100644
--- a/src/xrt/drivers/steamvr_lh/device.cpp
+++ b/src/xrt/drivers/steamvr_lh/device.cpp
@@ -146,11 +146,11 @@ const std::unordered_map<std::string_view, InputClass> controller_classes{
     },
 };
 
-uint64_t
+int64_t
 chrono_timestamp_ns()
 {
 	auto now = std::chrono::steady_clock::now().time_since_epoch();
-	uint64_t ts = std::chrono::duration_cast<std::chrono::nanoseconds>(now).count();
+	int64_t ts = std::chrono::duration_cast<std::chrono::nanoseconds>(now).count();
 	return ts;
 }
 
@@ -317,7 +317,7 @@ ControllerDevice::update_hand_tracking(struct xrt_hand_joint_set *out)
 	auto curl_values = u_hand_tracking_curl_values{pinky, ring, middle, index, thumb};
 
 	struct xrt_space_relation hand_relation = {};
-	uint64_t ts = chrono_timestamp_ns();
+	int64_t ts = chrono_timestamp_ns();
 	m_relation_history_get_latest(relation_hist, &ts, &hand_relation);
 
 	u_hand_sim_simulate_for_valve_index_knuckles(&curl_values, get_xrt_hand(), &hand_relation, out);
diff --git a/tests/tests_history_buf.cpp b/tests/tests_history_buf.cpp
index 4f8b23fb8..d39ec39da 100644
--- a/tests/tests_history_buf.cpp
+++ b/tests/tests_history_buf.cpp
@@ -57,7 +57,7 @@ TEST_CASE("m_relation_history")
 		constexpr auto T2 = T1 + (uint64_t)U_TIME_1S_IN_NS;
 
 		xrt_space_relation out_relation = XRT_SPACE_RELATION_ZERO;
-		uint64_t out_time = 0;
+		int64_t out_time = 0;
 
 		CHECK(m_relation_history_get_size(rh) == 0);
 		CHECK_FALSE(m_relation_history_get_latest(rh, &out_time, &out_relation));
@@ -152,7 +152,7 @@ TEST_CASE("RelationHistory")
 		constexpr auto T2 = T1 + (uint64_t)U_TIME_1S_IN_NS;
 
 		xrt_space_relation out_relation = XRT_SPACE_RELATION_ZERO;
-		uint64_t out_time = 0;
+		int64_t out_time = 0;
 
 		CHECK(rh.size() == 0);
 		CHECK_FALSE(rh.get_latest(&out_time, &out_relation));