[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-ID: <489d199159794ebcbd3d18d4082a88d7@AcuMS.aculab.com>
Date: Sun, 28 Jan 2024 19:32:36 +0000
From: David Laight <David.Laight@...LAB.COM>
To: "'linux-kernel@...r.kernel.org'" <linux-kernel@...r.kernel.org>, "'Linus
Torvalds'" <torvalds@...ux-foundation.org>, 'Netdev'
<netdev@...r.kernel.org>, "'dri-devel@...ts.freedesktop.org'"
<dri-devel@...ts.freedesktop.org>
CC: 'Andy Shevchenko' <andriy.shevchenko@...ux.intel.com>, 'Andrew Morton'
<akpm@...ux-foundation.org>, "'Matthew Wilcox (Oracle)'"
<willy@...radead.org>, 'Christoph Hellwig' <hch@...radead.org>, "'Dan
Carpenter'" <dan.carpenter@...aro.org>, 'Linus Walleij'
<linus.walleij@...aro.org>, "'David S . Miller'" <davem@...emloft.net>,
"'linux-btrfs@...r.kernel.org'" <linux-btrfs@...r.kernel.org>, 'Jens Axboe'
<axboe@...nel.dk>
Subject: [PATCH next 08/11 minmax: Add min_const() and max_const()
The expansions of min() and max() contain statement expressions so are
not valid for static intialisers.
min_const() and max_const() are expressions so can be used for static
initialisers.
The arguments are checked for being constant and for negative signed
values being converted to large unsigned values.
Using these to size on-stack arrays lets min/max be simplified.
Zero is added before the compare to convert enum values to integers
avoinding the need for casts when enums have been used for constants.
Signed-off-by: David Laight <david.laight@...lab.com>
---
include/linux/minmax.h | 15 +++++++++++++++
1 file changed, 15 insertions(+)
diff --git a/include/linux/minmax.h b/include/linux/minmax.h
index 278a390b8a4c..c08916588425 100644
--- a/include/linux/minmax.h
+++ b/include/linux/minmax.h
@@ -60,19 +60,34 @@
#op "(" #x ", " #y ") signedness error, fix types or consider u" #op "() before " #op "_t()"); \
__cmp_once(op, x, y, uniq); }))
+#define __careful_cmp_const(op, x, y) \
+ (BUILD_BUG_ON_ZERO(!__is_constexpr((x) - (y))) + \
+ BUILD_BUG_ON_ZERO(!__types_ok(x, y)) + \
+ __cmp(op, (x) + 0, (y) + 0))
+
/**
* min - return minimum of two values of the same or compatible types
* @x: first value
* @y: second value
+ *
+ * If @x and @y are constants the return value is constant, but not 'constant
+ * enough' for things like static initialisers.
+ * min_const(@x, @y) is a constant expression for constant inputs.
*/
#define min(x, y) __careful_cmp(min, x, y, __COUNTER__)
+#define min_const(x, y) __careful_cmp_const(min, x, y)
/**
* max - return maximum of two values of the same or compatible types
* @x: first value
* @y: second value
+ *
+ * If @x and @y are constants the return value is constant, but not 'constant
+ * enough' for things like static initialisers.
+ * max_const(@x, @y) is a constant expression for constant inputs.
*/
#define max(x, y) __careful_cmp(max, x, y, __COUNTER__)
+#define max_const(x, y) __careful_cmp_const(max, x, y)
/**
* umin - return minimum of two non-negative values
--
2.17.1
-
Registered Address Lakeside, Bramley Road, Mount Farm, Milton Keynes, MK1 1PT, UK
Registration No: 1397386 (Wales)
Powered by blists - more mailing lists