From: David Laight <David.Laight@ACULAB.COM>
To: "'linux-kernel@vger.kernel.org'" <linux-kernel@vger.kernel.org>,
"'Linus Torvalds'" <torvalds@linux-foundation.org>,
'Netdev' <netdev@vger.kernel.org>,
"'dri-devel@lists.freedesktop.org'"
<dri-devel@lists.freedesktop.org>
Cc: 'Andy Shevchenko' <andriy.shevchenko@linux.intel.com>,
'Andrew Morton' <akpm@linux-foundation.org>,
"'Matthew Wilcox (Oracle)'" <willy@infradead.org>,
'Christoph Hellwig' <hch@infradead.org>,
"'Dan Carpenter'" <dan.carpenter@linaro.org>,
'Linus Walleij' <linus.walleij@linaro.org>,
"'David S . Miller'" <davem@davemloft.net>,
"'linux-btrfs@vger.kernel.org'" <linux-btrfs@vger.kernel.org>,
'Jens Axboe' <axboe@kernel.dk>
Subject: RE: [PATCH next 04/11] minmax: Replace multiple __UNIQUE_ID() by directly using __COUNTER__
Date: Sun, 28 Jan 2024 19:28:51 +0000 [thread overview]
Message-ID: <24dad5d81c5e4eb0af03ff9ab7748e48@AcuMS.aculab.com> (raw)
In-Reply-To: <0ca26166dd2a4ff5a674b84704ff1517@AcuMS.aculab.com>
Provided __COUNTER__ is passed through an extra #define it can be pasted
onto multiple local variables to give unique names.
This saves having 3 __UNIQUE_ID() for #defines with three locals and
look less messy in general.
Stop the umin()/umax() lines being overlong by factoring out the
zero-extension logic.
Signed-off-by: David Laight <david.laight@aculab.com>
---
include/linux/minmax.h | 48 +++++++++++++++++++++---------------------
1 file changed, 24 insertions(+), 24 deletions(-)
diff --git a/include/linux/minmax.h b/include/linux/minmax.h
index c32b4b40ce01..8ee003d8abaf 100644
--- a/include/linux/minmax.h
+++ b/include/linux/minmax.h
@@ -8,7 +8,7 @@
#include <linux/types.h>
/*
- * min()/max()/clamp() macros must accomplish several things:
+ * min()/max()/clamp() macros must accomplish three things:
*
* - Avoid multiple evaluations of the arguments (so side-effects like
* "x++" happen only once) when non-constant.
@@ -43,31 +43,31 @@
#define __cmp(op, x, y) ((x) __cmp_op_##op (y) ? (x) : (y))
-#define __cmp_once(op, x, y, unique_x, unique_y) ({ \
- typeof(x) unique_x = (x); \
- typeof(y) unique_y = (y); \
- _Static_assert(__types_ok(x, y), \
+#define __cmp_once(op, x, y, uniq) ({ \
+ typeof(x) __x_##uniq = (x); \
+ typeof(y) __y_##uniq = (y); \
+ _Static_assert(__types_ok(x, y), \
#op "(" #x ", " #y ") signedness error, fix types or consider u" #op "() before " #op "_t()"); \
- __cmp(op, unique_x, unique_y); })
+ __cmp(op, __x_##uniq, __y_##uniq); })
-#define __careful_cmp(op, x, y) \
+#define __careful_cmp(op, x, y, uniq) \
__builtin_choose_expr(__is_constexpr((x) - (y)), \
__cmp(op, x, y), \
- __cmp_once(op, x, y, __UNIQUE_ID(__x), __UNIQUE_ID(__y)))
+ __cmp_once(op, x, y, uniq))
/**
* min - return minimum of two values of the same or compatible types
* @x: first value
* @y: second value
*/
-#define min(x, y) __careful_cmp(min, x, y)
+#define min(x, y) __careful_cmp(min, x, y, __COUNTER__)
/**
* max - return maximum of two values of the same or compatible types
* @x: first value
* @y: second value
*/
-#define max(x, y) __careful_cmp(max, x, y)
+#define max(x, y) __careful_cmp(max, x, y, __COUNTER__)
/**
* umin - return minimum of two non-negative values
@@ -75,8 +75,9 @@
* @x: first value
* @y: second value
*/
+#define __zero_extend(x) ((x) + 0u + 0ul + 0ull)
#define umin(x, y) \
- __careful_cmp(min, (x) + 0u + 0ul + 0ull, (y) + 0u + 0ul + 0ull)
+ __careful_cmp(min, __zero_extend(x), _zero_extend(y), __COUNTER__)
/**
* umax - return maximum of two non-negative values
@@ -84,7 +85,7 @@
* @y: second value
*/
#define umax(x, y) \
- __careful_cmp(max, (x) + 0u + 0ul + 0ull, (y) + 0u + 0ul + 0ull)
+ __careful_cmp(max, __zero_extend(x), _zero_extend(y), __COUNTER__)
/**
* min3 - return minimum of three values
@@ -108,7 +109,7 @@
* @x: first value
* @y: second value
*/
-#define min_t(type, x, y) __careful_cmp(min, (type)(x), (type)(y))
+#define min_t(type, x, y) __careful_cmp(min, (type)(x), (type)(y), __COUNTER__)
/**
* max_t - return maximum of two values, using the specified type
@@ -116,7 +117,7 @@
* @x: first value
* @y: second value
*/
-#define max_t(type, x, y) __careful_cmp(max, (type)(x), (type)(y))
+#define max_t(type, x, y) __careful_cmp(max, (type)(x), (type)(y), __COUNTER__)
/**
* min_not_zero - return the minimum that is _not_ zero, unless both are zero
@@ -131,22 +132,21 @@
#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); \
+#define __clamp_once(val, lo, hi, uniq) ({ \
+ typeof(val) __val_##uniq = (val); \
+ typeof(lo) __lo_##uniq = (lo); \
+ typeof(hi) __hi_##uniq = (hi); \
_Static_assert(__builtin_choose_expr(__is_constexpr((lo) > (hi)), \
(lo) <= (hi), true), \
"clamp() low limit " #lo " greater than high limit " #hi); \
_Static_assert(__types_ok(val, lo), "clamp() 'lo' signedness error"); \
_Static_assert(__types_ok(val, hi), "clamp() 'hi' signedness error"); \
- __clamp(unique_val, unique_lo, unique_hi); })
+ __clamp(__val_##uniq, __lo_##uniq, __hi_##uniq); })
-#define __careful_clamp(val, lo, hi) ({ \
+#define __careful_clamp(val, lo, hi, uniq) ({ \
__builtin_choose_expr(__is_constexpr((val) - (lo) + (hi)), \
__clamp(val, lo, hi), \
- __clamp_once(val, lo, hi, __UNIQUE_ID(__val), \
- __UNIQUE_ID(__lo), __UNIQUE_ID(__hi))); })
+ __clamp_once(val, lo, hi, uniq)); })
/**
* clamp - return a value clamped to a given range with strict typechecking
@@ -156,7 +156,7 @@
*
* This macro checks that @val, @lo and @hi have the same signedness.
*/
-#define clamp(val, lo, hi) __careful_clamp(val, lo, hi)
+#define clamp(val, lo, hi) __careful_clamp(val, lo, hi, __COUNTER__)
/**
* clamp_t - return a value clamped to a given range using a given type
@@ -168,7 +168,7 @@
* This macro does no typechecking and uses temporary variables of type
* @type to make all the comparisons.
*/
-#define clamp_t(type, val, lo, hi) __careful_clamp((type)(val), (type)(lo), (type)(hi))
+#define clamp_t(type, val, lo, hi) clamp((type)(val), (type)(lo), (type)(hi))
/**
* clamp_val - return a value clamped to a given range using val's type
--
2.17.1
-
Registered Address Lakeside, Bramley Road, Mount Farm, Milton Keynes, MK1 1PT, UK
Registration No: 1397386 (Wales)
next prev parent reply other threads:[~2024-01-28 19:29 UTC|newest]
Thread overview: 20+ messages / expand[flat|nested] mbox.gz Atom feed top
2024-01-28 19:24 [PATCH next 00/11] minmax: Optimise to reduce .i line length David Laight
2024-01-28 19:26 ` [PATCH next 01/11] minmax: Put all the clamp() definitions together David Laight
2024-01-28 19:27 ` [PATCH next 02/11] minmax: Use _Static_assert() instead of static_assert() David Laight
2024-01-28 19:27 ` [PATCH next 03/11] minmax: Simplify signedness check David Laight
2024-01-28 19:28 ` David Laight [this message]
2024-01-28 19:29 ` [PATCH next 05/11] minmax: Move the signedness check out of __cmp_once() and __clamp_once() David Laight
2024-01-28 19:30 ` [PATCH next 00611] minmax: Remove 'constexpr' check from __careful_clamp() David Laight
2024-01-28 19:31 ` [PATCH next 0711] minmax: minmax: Add __types_ok3() and optimise defines with 3 arguments David Laight
2024-01-28 19:32 ` [PATCH next 08/11 minmax: Add min_const() and max_const() David Laight
2024-01-28 19:34 ` [PATCH next 09/11] tree-wide: minmax: Replace all the uses of max() for array sizes with max_const() David Laight
2024-01-29 7:54 ` David Sterba
2024-01-28 19:35 ` [PATCH next 10/11] block: Use a boolean expression instead of max() on booleans David Laight
2024-01-28 19:59 ` Linus Torvalds
2024-01-28 22:21 ` David Laight
2024-01-28 22:32 ` Linus Torvalds
2024-01-29 9:07 ` Jani Nikula
2024-01-29 9:22 ` David Laight
2024-01-29 9:47 ` Jani Nikula
2024-01-29 10:16 ` Dan Carpenter
2024-01-28 19:36 ` [PATCH next 11/11] minmax: min() and max() don't need to return constant expressions David Laight
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=24dad5d81c5e4eb0af03ff9ab7748e48@AcuMS.aculab.com \
--to=david.laight@aculab.com \
--cc=akpm@linux-foundation.org \
--cc=andriy.shevchenko@linux.intel.com \
--cc=axboe@kernel.dk \
--cc=dan.carpenter@linaro.org \
--cc=davem@davemloft.net \
--cc=dri-devel@lists.freedesktop.org \
--cc=hch@infradead.org \
--cc=linus.walleij@linaro.org \
--cc=linux-btrfs@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=netdev@vger.kernel.org \
--cc=torvalds@linux-foundation.org \
--cc=willy@infradead.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;
as well as URLs for NNTP newsgroup(s).