public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
From: Abhishek Sagar <sagar.abhishek@gmail.com>
To: Ingo Molnar <mingo@elte.hu>
Cc: Thomas Gleixner <tglx@linutronix.de>,
	LKML <linux-kernel@vger.kernel.org>
Subject: [PATCH] ktime: Allow ktime_t comparison using ktime_compare
Date: Sat, 02 Feb 2008 14:37:38 +0530	[thread overview]
Message-ID: <47A432DA.6080307@gmail.com> (raw)

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.

Signed-off-by: Abhishek Sagar <sagar.abhishek@gmail.com>
---

diff --git a/include/linux/ktime.h b/include/linux/ktime.h
index a6ddec1..7f9d321 100644
--- a/include/linux/ktime.h
+++ b/include/linux/ktime.h
@@ -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

                 reply	other threads:[~2008-02-02  9:12 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=47A432DA.6080307@gmail.com \
    --to=sagar.abhishek@gmail.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=mingo@elte.hu \
    --cc=tglx@linutronix.de \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox