From c876087ee732b8bf301195078693ee107a480638 Mon Sep 17 00:00:00 2001 From: Jakob Bornecrantz Date: Mon, 5 Apr 2021 22:51:26 +0100 Subject: [PATCH] u/time: Add helper comparising functions --- src/xrt/auxiliary/util/u_time.h | 85 +++++++++++++++++++++++++++++++++ 1 file changed, 85 insertions(+) diff --git a/src/xrt/auxiliary/util/u_time.h b/src/xrt/auxiliary/util/u_time.h index 63e8ab185..e5abb5ac9 100644 --- a/src/xrt/auxiliary/util/u_time.h +++ b/src/xrt/auxiliary/util/u_time.h @@ -93,6 +93,91 @@ time_s_to_ns(double duration) return (time_duration_ns)(duration * (double)U_TIME_1S_IN_NS); } +/*! + * Convert nanoseconds to double float milliseconds, useful for printing. + * + * @see timepoint_ns + * @ingroup aux_util + */ +static inline double +time_ns_to_ms_f(time_duration_ns ns) +{ + return (double)(ns) / (double)U_TIME_1MS_IN_NS; +} + +/*! + * Checks if two timepoints are with a certain range of each other. + * + * @see timepoint_ns + * @ingroup aux_util + */ +static inline bool +time_is_within_range_of_each_other(timepoint_ns a, timepoint_ns b, uint64_t range) +{ + int64_t t = (int64_t)a - (int64_t)b; + return (-(int64_t)range < t) && (t < (int64_t)range); +} + +/*! + * Checks if two timepoints are with half a millisecond of each other. + * + * @see timepoint_ns + * @ingroup aux_util + */ +static inline bool +time_is_within_half_ms(timepoint_ns a, timepoint_ns b) +{ + return time_is_within_range_of_each_other(a, b, U_TIME_HALF_MS_IN_NS); +} + +/*! + * Fuzzy comparisons. + * + * @see timepoint_ns + * @ingroup aux_util + */ +static inline bool +time_is_less_then_or_within_range(timepoint_ns a, timepoint_ns b, uint64_t range) +{ + return a < b || time_is_within_range_of_each_other(a, b, range); +} + +/*! + * Fuzzy comparisons. + * + * @see timepoint_ns + * @ingroup aux_util + */ +static inline bool +time_is_less_then_or_within_half_ms(timepoint_ns a, timepoint_ns b) +{ + return time_is_less_then_or_within_range(a, b, U_TIME_HALF_MS_IN_NS); +} + +/*! + * Fuzzy comparisons. + * + * @see timepoint_ns + * @ingroup aux_util + */ +static inline bool +time_is_greater_then_or_within_range(timepoint_ns a, timepoint_ns b, uint64_t range) +{ + return a > b || time_is_within_range_of_each_other(a, b, range); +} + +/*! + * Fuzzy comparisons. + * + * @see timepoint_ns + * @ingroup aux_util + */ +static inline bool +time_is_greater_then_or_within_half_ms(timepoint_ns a, timepoint_ns b) +{ + return time_is_greater_then_or_within_range(a, b, U_TIME_HALF_MS_IN_NS); +} + /*! * @struct time_state util/u_time.h * @brief Time-keeping state structure.