mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Michal Nazarewicz <mina86@mina86.com>
To: Andrew Morton <akpm@linux-foundation.org>
Cc: Mark Rustad <mark.d.rustad@intel.com>,
	"Kirsher, Jeffrey T" <jeffrey.t.kirsher@intel.com>,
	"linux-kernel@vger.kernel.org" <linux-kernel@vger.kernel.org>,
	Hagen Paul Pfeifer <hagen@jauu.net>,
	Steven Rostedt <rostedt@goodmis.org>,
	Michal Nazarewicz <mina86@mina86.com>
Subject: [PATCH] include: kernel.h: deduplicate code implementing clamp* macros
Date: Thu, 25 Sep 2014 17:50:11 +0200	[thread overview]
Message-ID: <1411660211-8303-1-git-send-email-mina86@mina86.com> (raw)
In-Reply-To: <1410471576-12053-1-git-send-email-mina86@mina86.com>

Instead of open-coding clamp_t macro min_t and max_t the way clamp
macro does and instead of open-coding clamp_val simply use clamp_t.
Furthermore, normalise argument naming in the macros to be lo and
hi.

Signed-off-by: Michal Nazarewicz <mina86@mina86.com>
---
 include/linux/kernel.h | 24 +++++++-----------------
 1 file changed, 7 insertions(+), 17 deletions(-)

This is an exceprt from the other bigger patch which had some
controversy I assume because of the way it reimplemented min and max.
This only contains refactoring of clamp which I believe should be
uncontroversial.

diff --git a/include/linux/kernel.h b/include/linux/kernel.h
index c39c69d..40728cf 100644
--- a/include/linux/kernel.h
+++ b/include/linux/kernel.h
@@ -731,7 +731,7 @@ static inline void ftrace_dump(enum ftrace_dump_mode oops_dump_mode) { }
  * @lo: lowest allowable value
  * @hi: highest allowable value
  *
- * This macro does strict typechecking of min/max to make sure they are of the
+ * This macro does strict typechecking of lo/hi to make sure they are of the
  * same type as val.  See the unnecessary pointer comparisons.
  */
 #define clamp(val, lo, hi) min((typeof(val))max(val, lo), hi)
@@ -756,36 +756,26 @@ static inline void ftrace_dump(enum ftrace_dump_mode oops_dump_mode) { }
  * clamp_t - return a value clamped to a given range using a given type
  * @type: the type of variable to use
  * @val: current value
- * @min: minimum allowable value
- * @max: maximum allowable value
+ * @lo: minimum allowable value
+ * @hi: maximum allowable value
  *
  * This macro does no typechecking and uses temporary variables of type
  * 'type' to make all the comparisons.
  */
-#define clamp_t(type, val, min, max) ({		\
-	type __val = (val);			\
-	type __min = (min);			\
-	type __max = (max);			\
-	__val = __val < __min ? __min: __val;	\
-	__val > __max ? __max: __val; })
+#define clamp_t(type, val, lo, hi) min_t(type, max_t(type, val, lo), hi)
 
 /**
  * clamp_val - return a value clamped to a given range using val's type
  * @val: current value
- * @min: minimum allowable value
- * @max: maximum allowable value
+ * @lo: minimum allowable value
+ * @hi: maximum allowable value
  *
  * This macro does no typechecking and uses temporary variables of whatever
  * type the input argument 'val' is.  This is useful when val is an unsigned
  * type and min and max are literals that will otherwise be assigned a signed
  * integer type.
  */
-#define clamp_val(val, min, max) ({		\
-	typeof(val) __val = (val);		\
-	typeof(val) __min = (min);		\
-	typeof(val) __max = (max);		\
-	__val = __val < __min ? __min: __val;	\
-	__val > __max ? __max: __val; })
+#define clamp_val(val, lo, hi) clamp_t(typeof(val), val, lo, hi)
 
 
 /*
-- 
2.1.0.rc2.206.gedb03e5


  parent reply	other threads:[~2014-09-25 15:50 UTC|newest]

Thread overview: 12+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <1409837300.2460.10.camel@jtkirshe-mobl>
2014-09-04 21:38 ` kernel: Resolve a shadow warning Andrew Morton
2014-09-04 23:03   ` Rustad, Mark D
2014-09-11 21:39     ` [PATCH 1/2] include: kernel.h: deduplicate code implementing min*, max* and clamp* macros Michal Nazarewicz
2014-09-11 21:39       ` [PATCH 2/2] kernel.h: use __COUNTER__ in min and max macros to avoid -Wshadow warnings Michal Nazarewicz
2014-09-12 22:40         ` Andrew Morton
2014-09-12 23:37           ` Michal Nazarewicz
2014-09-12 23:48             ` Rustad, Mark D
2014-09-12 23:43           ` Rustad, Mark D
2014-09-13  0:12             ` Andrew Morton
2014-09-25 15:50       ` Michal Nazarewicz [this message]
2014-09-25 19:46         ` [PATCH] include: kernel.h: deduplicate code implementing clamp* macros Hagen Paul Pfeifer
2014-09-26 20:04           ` Michal Nazarewicz

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=1411660211-8303-1-git-send-email-mina86@mina86.com \
    --to=mina86@mina86.com \
    --cc=akpm@linux-foundation.org \
    --cc=hagen@jauu.net \
    --cc=jeffrey.t.kirsher@intel.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=mark.d.rustad@intel.com \
    --cc=rostedt@goodmis.org \
    /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

all inboxes | Powered by JetHome®