[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-ID: <78f9c15ad67940fe91ac39b9c26113be@AcuMS.aculab.com>
Date: Sun, 28 Jan 2024 19:31:22 +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: RE: [PATCH next 0711] minmax: minmax: Add __types_ok3() and optimise
defines with 3 arguments
min3() and max3() were added to optimise nested min(x, min(y, z))
sequences, bit only moved where the expansion was requiested.
Add a separate implementation for 3 argument calls.
These are never required to generate constant expressiions to
remove that logic.
Signed-off-by: David Laight <david.laight@...lab.com>
---
include/linux/minmax.h | 23 +++++++++++++++++++----
1 file changed, 19 insertions(+), 4 deletions(-)
diff --git a/include/linux/minmax.h b/include/linux/minmax.h
index 5c7fce76abe5..278a390b8a4c 100644
--- a/include/linux/minmax.h
+++ b/include/linux/minmax.h
@@ -38,6 +38,11 @@
((__is_ok_signed(x) && __is_ok_signed(y)) || \
(__is_ok_unsigned(x) && __is_ok_unsigned(y)))
+/* Check three values for min3(), max3() and clamp() */
+#define __types_ok3(x, y, z) \
+ ((__is_ok_signed(x) && __is_ok_signed(y) && __is_ok_signed(z)) || \
+ (__is_ok_unsigned(x) && __is_ok_unsigned(y) && __is_ok_unsigned(z)))
+
#define __cmp_op_min <
#define __cmp_op_max >
@@ -87,13 +92,24 @@
#define umax(x, y) \
__careful_cmp(max, __zero_extend(x), _zero_extend(y), __COUNTER__)
+#define __cmp_once3(op, x, y, z, uniq) ({ \
+ typeof(x) __x_##uniq = (x); \
+ typeof(x) __y_##uniq = (y); \
+ typeof(x) __z_##uniq = (z); \
+ __cmp(op, __cmp(op, __x_##uniq, __y_##uniq), __z_##uniq); })
+
+#define __careful_cmp3(op, x, y, z, uniq) ({ \
+ static_assert(__types_ok3(x, y, z), \
+ #op "3(" #x ", " #y ", " #z ") signedness error"); \
+ __cmp_once3(op, x, y, z, uniq); })
+
/**
* 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)
+#define min3(x, y, z) __careful_cmp3(min, x, y, z, __COUNTER__)
/**
* max3 - return maximum of three values
@@ -101,7 +117,7 @@
* @y: second value
* @z: third value
*/
-#define max3(x, y, z) max((typeof(x))max(x, y), z)
+#define max3(x, y, z) __careful_cmp3(max, x, y, z, __COUNTER__)
/**
* min_t - return minimum of two values, using the specified type
@@ -142,8 +158,7 @@
__clamp(__val_##uniq, __lo_##uniq, __hi_##uniq); })
#define __careful_clamp(val, lo, hi, uniq) ({ \
- _Static_assert(__types_ok(val, lo), "clamp() 'lo' signedness error"); \
- _Static_assert(__types_ok(val, hi), "clamp() 'hi' signedness error"); \
+ _Static_assert(__types_ok3(val, lo, hi), "clamp() signedness error"); \
__clamp_once(val, lo, hi, uniq); })
/**
--
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