Skip to contentSkip to conversation

ktime: Allow ktime_t comparison using ktime_compare

PATCH New Abhishek Sagar 1 participant 1 message Feb 2, 2008

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 ktime: Allow ktime_t comparison using ktime_compare

Add a timespec style comparison function. Allows two ktime types to be compared without having to convert to timespec/timeval. Useful for modules doing ktime based math, especially the ones using ktime_get heavily.

---

include/linux/ktime.h +34 -0
index a6ddec1..7f9d321 100644
@@ -95,6 +95,23 @@ static inline ktime_t ktime_set(const long secs, const unsigned long nsecs)
 #define ktime_add(lhs, rhs) \
 		({ (ktime_t){ .tv64 = (lhs).tv64 + (rhs).tv64 }; })
 
+/**
+ * ktime_compare - Compares two ktime_t variables
+ *
+ * Return val:
+ * lhs < rhs:  < 0
+ * lhs == rhs: 0
+ * lhs > rhs:  > 0
+ */
+static inline int ktime_compare(const ktime_t lhs, const ktime_t rhs)
+{
+	if (lhs.tv64 < rhs.tv64)
+		return -1;
+	if (lhs.tv64 > rhs.tv64)
+		return 1;
+	return 0;
+}
+
 /*
  * Add a ktime_t variable and a scalar nanosecond value.
  * res = kt + nsval:
@@ -198,6 +215,23 @@ static inline ktime_t ktime_add(const ktime_t add1, const ktime_t add2)
 }
 
 /**
+ * ktime_compare - Compares two ktime_t variables
+ *
+ * Return val:
+ * lhs < rhs:  < 0
+ * lhs == rhs: 0
+ * lhs > rhs:  > 0
+ */
+static inline int ktime_compare(const ktime_t lhs, const ktime_t rhs)
+{
+	if (lhs.tv.sec < rhs.tv.sec)
+		return -1;
+	if (lhs.tv.sec > rhs.tv.sec)
+		return 1;
+	return lhs.tv.nsec - rhs.tv.nsec;
+}
+
+/**
  * ktime_add_ns - Add a scalar nanoseconds value to a ktime_t variable
  * @kt:		addend
  * @nsec:	the scalar nsec value to add