lists.openwall.net   lists  /  announce  owl-users  owl-dev  john-users  john-dev  passwdqc-users  yescrypt  popa3d-users  /  oss-security  kernel-hardening  musl  sabotage  tlsify  passwords  /  crypt-dev  xvendor  /  Bugtraq  Full-Disclosure  linux-kernel  linux-netdev  linux-ext4  linux-hardening  linux-cve-announce  PHC 
Open Source and information security mailing list archives
 
Hash Suite: Windows password security audit tool. GUI, reports in PDF.
[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-ID: <75f18af7162c4414be28a890e9504c8d@AcuMS.aculab.com>
Date: Sun, 28 Jul 2024 14:24:46 +0000
From: David Laight <David.Laight@...LAB.COM>
To: "'linux-kernel@...r.kernel.org'" <linux-kernel@...r.kernel.org>
CC: 'Linus Torvalds' <torvalds@...uxfoundation.org>, 'Jens Axboe'
	<axboe@...nel.dk>, "'Matthew Wilcox (Oracle)'" <willy@...radead.org>,
	'Christoph Hellwig' <hch@...radead.org>, 'Andrew Morton'
	<akpm@...ux-foundation.org>, 'Andy Shevchenko'
	<andriy.shevchenko@...ux.intel.com>, 'Dan Carpenter'
	<dan.carpenter@...aro.org>, 'Arnd Bergmann' <arnd@...nel.org>,
	"'Jason@...c4.com'" <Jason@...c4.com>, "'pedro.falcato@...il.com'"
	<pedro.falcato@...il.com>, 'Mateusz Guzik' <mjguzik@...il.com>,
	"'linux-mm@...ck.org'" <linux-mm@...ck.org>, 'Lorenzo Stoakes'
	<lorenzo.stoakes@...cle.com>
Subject: [PATCH v2 8/8] 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, but only moved where the expansion was requested.

Add a separate implementation for 3 argument calls.
These are never required to generate constant expressiions so
remove that logic.

Signed-off-by: David Laight <david.laight@...lab.com>
---
v2 (was pacth 7/7):
- Use __auto_type.
- Use an extra __xy local to slightly reduce the expansion.
- Fix typos in the commit message.

 include/linux/minmax.h | 24 ++++++++++++++++++++----
 1 file changed, 20 insertions(+), 4 deletions(-)

diff --git a/include/linux/minmax.h b/include/linux/minmax.h
index cb3515824a64..e1e31a827547 100644
--- a/include/linux/minmax.h
+++ b/include/linux/minmax.h
@@ -41,6 +41,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 >
 
@@ -93,13 +98,25 @@
  */
 #define umax(x, y)	__careful_cmp(max, __zero_extend(x), __zero_extend(y))
 
+#define __cmp_once3(op, x, y, z, uniq) ({				\
+	__auto_type __x_##uniq = (x);					\
+	__auto_type __y_##uniq = (y);					\
+	__auto_type __z_##uniq = (z);					\
+	__auto_type __xy_##uniq = __cmp(op, __x_##uniq, __y_##uniq);	\
+	__cmp(op, __xy_##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
@@ -107,7 +124,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
@@ -144,8 +161,7 @@
 	__auto_type unique_hi = (hi);						\
 	_Static_assert(__if_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");	\
+	_Static_assert(__types_ok3(val, lo, hi), "clamp() signedness error");	\
 	__clamp(unique_val, unique_lo, unique_hi); })
 
 #define __careful_clamp(val, lo, hi) ({					\
-- 
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

Powered by Openwall GNU/*/Linux Powered by OpenVZ