linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] minmax: clamp more efficiently by avoiding extra comparison
@ 2022-09-23 10:06 Jason A. Donenfeld
  2022-09-23 10:35 ` Andy Shevchenko
  0 siblings, 1 reply; 30+ messages in thread
From: Jason A. Donenfeld @ 2022-09-23 10:06 UTC (permalink / raw)
  To: linux-kernel
  Cc: Jason A. Donenfeld, Andy Shevchenko, Andrew Morton, Kees Cook

Currently the clamp algorithm does:

	if (val > hi)
		val = hi;
	if (val < lo)
		val = lo;

But since hi > lo by definition, this can be made more efficient with:

	if (val > hi)
		val = hi;
	else if (val < lo)
		val = lo;

So fix up the clamp and clamp_t functions to do this, adding the same
argument checking as for min and min_t.

Cc: Andy Shevchenko <andriy.shevchenko@linux.intel.com>
Cc: Andrew Morton <akpm@linux-foundation.org>
Cc: Kees Cook <keescook@chromium.org>
Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
---
 include/linux/minmax.h | 20 ++++++++++++++++++--
 1 file changed, 18 insertions(+), 2 deletions(-)

diff --git a/include/linux/minmax.h b/include/linux/minmax.h
index 5433c08fcc68..0153c9eba013 100644
--- a/include/linux/minmax.h
+++ b/include/linux/minmax.h
@@ -19,42 +19,58 @@
 #define __typecheck(x, y) \
 	(!!(sizeof((typeof(x) *)1 == (typeof(y) *)1)))
 
 #define __no_side_effects(x, y) \
 		(__is_constexpr(x) && __is_constexpr(y))
 
 #define __safe_cmp(x, y) \
 		(__typecheck(x, y) && __no_side_effects(x, y))
 
 #define __cmp(x, y, op)	((x) op (y) ? (x) : (y))
 
 #define __cmp_once(x, y, unique_x, unique_y, op) ({	\
 		typeof(x) unique_x = (x);		\
 		typeof(y) unique_y = (y);		\
 		__cmp(unique_x, unique_y, op); })
 
 #define __careful_cmp(x, y, op) \
 	__builtin_choose_expr(__safe_cmp(x, y), \
 		__cmp(x, y, op), \
 		__cmp_once(x, y, __UNIQUE_ID(__x), __UNIQUE_ID(__y), op))
 
+#define __clamp(val, lo, hi) \
+	((val) >= (hi) ? (hi) : ((val) <= (lo) ? (lo) : (val)))
+
+#define __clamp_once(val, lo, hi, unique_val, unique_lo, unique_hi) ({		\
+		typeof(val) unique_val = (val);					\
+		typeof(lo) unique_lo = (lo);					\
+		typeof(hi) unique_hi = (hi);					\
+		__clamp(unique_val, unique_lo, unique_hi); })
+
+#define __careful_clamp(val, lo, hi)						\
+	__builtin_choose_expr(__typecheck(val, lo) && __typecheck(val, hi) &&	\
+			      __is_constexpr(val) && 				\
+			      __is_constexpr(lo) && __is_constexpr(hi),		\
+		__clamp(val, lo, hi),						\
+		__clamp_once(val, lo, hi, __UNIQUE_ID(__val), __UNIQUE_ID(__lo), __UNIQUE_ID(__hi)))
+
 /**
  * min - return minimum of two values of the same or compatible types
  * @x: first value
  * @y: second value
  */
 #define min(x, y)	__careful_cmp(x, y, <)
 
 /**
  * max - return maximum of two values of the same or compatible types
  * @x: first value
  * @y: second value
  */
 #define max(x, y)	__careful_cmp(x, y, >)
 
 /**
  * min3 - return minimum of three values
  * @x: first value
  * @y: second value
  * @z: third value
  */
 #define min3(x, y, z) min((typeof(x))min(x, y), z)
@@ -68,78 +84,78 @@
 #define max3(x, y, z) max((typeof(x))max(x, y), z)
 
 /**
  * min_not_zero - return the minimum that is _not_ zero, unless both are zero
  * @x: value1
  * @y: value2
  */
 #define min_not_zero(x, y) ({			\
 	typeof(x) __x = (x);			\
 	typeof(y) __y = (y);			\
 	__x == 0 ? __y : ((__y == 0) ? __x : min(__x, __y)); })
 
 /**
  * clamp - return a value clamped to a given range with strict typechecking
  * @val: current value
  * @lo: lowest allowable value
  * @hi: highest allowable value
  *
  * 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)
+#define clamp(val, lo, hi) __careful_clamp(val, lo, hi)
 
 /*
  * ..and if you can't take the strict
  * types, you can specify one yourself.
  *
  * Or not use min/max/clamp at all, of course.
  */
 
 /**
  * min_t - return minimum of two values, using the specified type
  * @type: data type to use
  * @x: first value
  * @y: second value
  */
 #define min_t(type, x, y)	__careful_cmp((type)(x), (type)(y), <)
 
 /**
  * max_t - return maximum of two values, using the specified type
  * @type: data type to use
  * @x: first value
  * @y: second value
  */
 #define max_t(type, x, y)	__careful_cmp((type)(x), (type)(y), >)
 
 /**
  * clamp_t - return a value clamped to a given range using a given type
  * @type: the type of variable to use
  * @val: current 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, lo, hi) min_t(type, max_t(type, val, lo), hi)
+#define clamp_t(type, val, lo, hi) __careful_clamp((type)(val), (type)(lo), (type)(hi))
 
 /**
  * clamp_val - return a value clamped to a given range using val's type
  * @val: current 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 @lo and @hi are literals that will otherwise be assigned a signed
  * integer type.
  */
 #define clamp_val(val, lo, hi) clamp_t(typeof(val), val, lo, hi)
 
 /**
  * swap - swap values of @a and @b
  * @a: first value
  * @b: second value
  */
 #define swap(a, b) \
 	do { typeof(a) __tmp = (a); (a) = (b); (b) = __tmp; } while (0)
-- 
2.37.3


^ permalink raw reply related	[flat|nested] 30+ messages in thread

end of thread, other threads:[~2022-10-04 15:01 UTC | newest]

Thread overview: 30+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-09-23 10:06 [PATCH] minmax: clamp more efficiently by avoiding extra comparison Jason A. Donenfeld
2022-09-23 10:35 ` Andy Shevchenko
2022-09-23 10:40   ` Jason A. Donenfeld
2022-09-23 10:48     ` Jason A. Donenfeld
2022-09-23 15:12       ` Andy Shevchenko
2022-09-23 15:13         ` Jason A. Donenfeld
2022-09-23 15:40           ` [PATCH v2] " Jason A. Donenfeld
2022-09-23 16:41             ` Kees Cook
2022-09-23 16:42               ` Jason A. Donenfeld
2022-09-23 16:53               ` Andy Shevchenko
2022-09-23 16:54                 ` Jason A. Donenfeld
2022-09-23 19:47             ` Kees Cook
2022-09-23 22:54             ` Andrew Morton
2022-09-24  0:02               ` Kees Cook
2022-09-24 10:37               ` Jason A. Donenfeld
2022-09-25 16:29                 ` Andrew Morton
2022-09-26 10:00                 ` Andy Shevchenko
2022-09-26 12:23                   ` Jason A. Donenfeld
2022-09-26 13:34                     ` [PATCH v3 1/2] minmax: sanity check constant bounds when clamping Jason A. Donenfeld
2022-09-26 13:34                       ` [PATCH v3 2/2] minmax: clamp more efficiently by avoiding extra comparison Jason A. Donenfeld
2022-09-26 18:30                         ` Kees Cook
2022-09-26 21:33                           ` Jason A. Donenfeld
2022-09-26 13:46                       ` [PATCH v3 1/2] minmax: sanity check constant bounds when clamping Andy Shevchenko
2022-09-26 18:26                       ` Kees Cook
2022-10-04 13:41                         ` Jason A. Donenfeld
2022-10-04 14:39                           ` Kees Cook
2022-10-04 15:01                             ` Jason A. Donenfeld
2022-09-26 18:30                     ` [PATCH v2] minmax: clamp more efficiently by avoiding extra comparison Kees Cook
2022-09-23 15:10     ` [PATCH] " Andy Shevchenko
2022-09-23 15:11       ` Jason A. Donenfeld

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).