Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > linux.kernel > #1651289 > unrolled thread

[PATCH] ktime: Simplify ktime_* comparison functions

Started byMariusz Skamra <mariuszx.skamra@intel.com>
First post2017-05-26 12:20 +0200
Last post2017-05-26 12:40 +0200
Articles 3 — 3 participants

Back to article view | Back to linux.kernel


Contents

  [PATCH] ktime: Simplify ktime_* comparison functions Mariusz Skamra <mariuszx.skamra@intel.com> - 2017-05-26 12:20 +0200
    Re: [PATCH] ktime: Simplify ktime_* comparison functions Joe Perches <joe@perches.com> - 2017-05-26 12:30 +0200
    Re: [PATCH] ktime: Simplify ktime_* comparison functions Thomas Gleixner <tglx@linutronix.de> - 2017-05-26 12:40 +0200

#1651289 — [PATCH] ktime: Simplify ktime_* comparison functions

FromMariusz Skamra <mariuszx.skamra@intel.com>
Date2017-05-26 12:20 +0200
Subject[PATCH] ktime: Simplify ktime_* comparison functions
Message-ID<tLkvD-4BA-13@gated-at.bofh.it>
This simplifies ktime_compare, ktime_after and ktime_before to be defines.
ktime_compare verified on x86_64 machine, gives a code 4 asm instruction
less compared to the previous solution. Time measured is up to 2 times less.

Signed-off-by: Mariusz Skamra <mariuszx.skamra@intel.com>
Acked-by: Kuppuswamy Sathyanarayanan <sathyanarayanan.kuppuswamy@intel.com>
---
 include/linux/ktime.h | 19 +++----------------
 1 file changed, 3 insertions(+), 16 deletions(-)

diff --git a/include/linux/ktime.h b/include/linux/ktime.h
index 0c8bd45..3f3dfcd 100644
--- a/include/linux/ktime.h
+++ b/include/linux/ktime.h
@@ -106,14 +106,7 @@ static inline ktime_t timeval_to_ktime(struct timeval tv)
  *   cmp1 == cmp2: return 0
  *   cmp1  > cmp2: return >0
  */
-static inline int ktime_compare(const ktime_t cmp1, const ktime_t cmp2)
-{
-	if (cmp1 < cmp2)
-		return -1;
-	if (cmp1 > cmp2)
-		return 1;
-	return 0;
-}
+#define ktime_compare(cmp1, cmp2)	ktime_sub(cmp1, cmp2)
 
 /**
  * ktime_after - Compare if a ktime_t value is bigger than another one.
@@ -122,10 +115,7 @@ static inline int ktime_compare(const ktime_t cmp1, const ktime_t cmp2)
  *
  * Return: true if cmp1 happened after cmp2.
  */
-static inline bool ktime_after(const ktime_t cmp1, const ktime_t cmp2)
-{
-	return ktime_compare(cmp1, cmp2) > 0;
-}
+#define ktime_after(cmp1, cmp2)		(ktime_compare(cmp1, cmp2) > 0)
 
 /**
  * ktime_before - Compare if a ktime_t value is smaller than another one.
@@ -134,10 +124,7 @@ static inline bool ktime_after(const ktime_t cmp1, const ktime_t cmp2)
  *
  * Return: true if cmp1 happened before cmp2.
  */
-static inline bool ktime_before(const ktime_t cmp1, const ktime_t cmp2)
-{
-	return ktime_compare(cmp1, cmp2) < 0;
-}
+#define ktime_before(cmp1, cmp2)	(ktime_compare(cmp1, cmp2) < 0)
 
 #if BITS_PER_LONG < 64
 extern s64 __ktime_divns(const ktime_t kt, s64 div);
-- 
2.1.4

--------------------------------------------------------------------

Intel Technology Poland sp. z o.o.
ul. Slowackiego 173 | 80-298 Gdansk | Sad Rejonowy Gdansk Polnoc | VII Wydzial Gospodarczy Krajowego Rejestru Sadowego - KRS 101882 | NIP 957-07-52-316 | Kapital zakladowy 200.000 PLN.

Ta wiadomosc wraz z zalacznikami jest przeznaczona dla okreslonego adresata i moze zawierac informacje poufne. W razie przypadkowego otrzymania tej wiadomosci, prosimy o powiadomienie nadawcy oraz trwale jej usuniecie; jakiekolwiek
przegladanie lub rozpowszechnianie jest zabronione.
This e-mail and any attachments may contain confidential material for the sole use of the intended recipient(s). If you are not the intended recipient, please contact the sender and delete all copies; any review or distribution by
others is strictly prohibited.

[toc] | [next] | [standalone]


#1651290

FromJoe Perches <joe@perches.com>
Date2017-05-26 12:30 +0200
Message-ID<tLkFj-4Ez-1@gated-at.bofh.it>
In reply to#1651289
On Fri, 2017-05-26 at 12:09 +0200, Mariusz Skamra wrote:
> This simplifies ktime_compare, ktime_after and ktime_before to be defines.
> ktime_compare verified on x86_64 machine, gives a code 4 asm instruction
> less compared to the previous solution. Time measured is up to 2 times less.
> Signed-off-by: Mariusz Skamra <mariuszx.skamra@intel.com>
> Acked-by: Kuppuswamy Sathyanarayanan <sathyanarayanan.kuppuswamy@intel.com>
> ---
>  include/linux/ktime.h | 19 +++----------------
>  1 file changed, 3 insertions(+), 16 deletions(-)
> 
> diff --git a/include/linux/ktime.h b/include/linux/ktime.h
> index 0c8bd45..3f3dfcd 100644
> --- a/include/linux/ktime.h
> +++ b/include/linux/ktime.h
> @@ -106,14 +106,7 @@ static inline ktime_t timeval_to_ktime(struct timeval tv)
>   *   cmp1 == cmp2: return 0
>   *   cmp1  > cmp2: return >0
>   */
> -static inline int ktime_compare(const ktime_t cmp1, const ktime_t cmp2)
> -{
> -	if (cmp1 < cmp2)
> -		return -1;
> -	if (cmp1 > cmp2)
> -		return 1;
> -	return 0;
> -}
> +#define ktime_compare(cmp1, cmp2)	ktime_sub(cmp1, cmp2)

Why not just change ktime_compare to

static inline int ktime_compare(const ktime_t cmp1, const ktime_t cmp2)
{
	return ktime_sub(cmp1, cmp2);
}

> 
>  /**
>   * ktime_after - Compare if a ktime_t value is bigger than another one.
> @@ -122,10 +115,7 @@ static inline int ktime_compare(const ktime_t cmp1, const ktime_t cmp2)
>   *
>   * Return: true if cmp1 happened after cmp2.
>   */
> -static inline bool ktime_after(const ktime_t cmp1, const ktime_t cmp2)
> -{
> -	return ktime_compare(cmp1, cmp2) > 0;
> -}
> +#define ktime_after(cmp1, cmp2)		(ktime_compare(cmp1, cmp2) > 0)

The ktime_after and ktime_before changes shouldn't
matter in generated objects.

I suggest these conversions from static inline to
#define are not changes for the better.
 
>  /**
>   * ktime_before - Compare if a ktime_t value is smaller than another one.
> @@ -134,10 +124,7 @@ static inline bool ktime_after(const ktime_t cmp1, const ktime_t cmp2)
>   *
>   * Return: true if cmp1 happened before cmp2.
>   */
> -static inline bool ktime_before(const ktime_t cmp1, const ktime_t cmp2)
> -{
> -	return ktime_compare(cmp1, cmp2) < 0;
> -}
> +#define ktime_before(cmp1, cmp2)	(ktime_compare(cmp1, cmp2) < 0)
>  
>  #if BITS_PER_LONG < 64
>  extern s64 __ktime_divns(const ktime_t kt, s64 div);

[toc] | [prev] | [next] | [standalone]


#1651294

FromThomas Gleixner <tglx@linutronix.de>
Date2017-05-26 12:40 +0200
Message-ID<tLkOZ-4HI-9@gated-at.bofh.it>
In reply to#1651289
On Fri, 26 May 2017, Mariusz Skamra wrote:
> -static inline int ktime_compare(const ktime_t cmp1, const ktime_t cmp2)
> -{
> -	if (cmp1 < cmp2)
> -		return -1;
> -	if (cmp1 > cmp2)
> -		return 1;
> -	return 0;
> -}
> +#define ktime_compare(cmp1, cmp2)	ktime_sub(cmp1, cmp2)

What's wrong with modifying the inline itself?

> -static inline bool ktime_after(const ktime_t cmp1, const ktime_t cmp2)
> -{
> -	return ktime_compare(cmp1, cmp2) > 0;
> -}
> +#define ktime_after(cmp1, cmp2)		(ktime_compare(cmp1, cmp2) > 0)

> -static inline bool ktime_before(const ktime_t cmp1, const ktime_t cmp2)
> -{
> -	return ktime_compare(cmp1, cmp2) < 0;
> -}
> +#define ktime_before(cmp1, cmp2)	(ktime_compare(cmp1, cmp2) < 0)

Sorry, but those two are just crap. That inline compiles to exactly the
same code as the macro. If not, yell at your compiler people.

Thanks,

	tglx

[toc] | [prev] | [standalone]


Back to top | Article view | linux.kernel


csiph-web