From nobody Thu Dec 18 11:55:57 2025 Received: from eu-smtp-delivery-151.mimecast.com (eu-smtp-delivery-151.mimecast.com [185.58.85.151]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 6FCFE134DE for ; Wed, 24 Jul 2024 14:28:58 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=185.58.85.151 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1721831340; cv=none; b=VWRgwcjVFdizFLddmnwplidszK53QuuYrJjzjf7mrTs0wh2daVwc7FtfyB0J52DX2+zFlxcXGyTNIryDxR1QxcJV2xaaVp+u43pOKb1QuzGTQVL+p1Dut6qP2HyHFjp2bgyqiPWCfQrFB2d8iYu583rN7F/e0DgYoY/KoXX/uyo= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1721831340; c=relaxed/simple; bh=y4rtEWAXru0KLn5/0HBoBnc39VArj/Sc52842nH+Rcc=; h=From:To:CC:Subject:Date:Message-ID:References:In-Reply-To: MIME-Version:Content-Type; b=lqDlZj6AaGBraQnAkcF1C5TFEBPTeJA6up2engCcRMTqLkJya5DpLY6k6lXSNpbrFnFS4kHozx1Fib8eXrdCB0B4CFmuSkMmNW4bZ2fM5jDE8/AR+jKW1Xmz9WFB7xydcL7ET+fgPeo5XsNCL9Q1ADmOSZw1YLwUC6olnoIRhAQ= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=ACULAB.COM; spf=pass smtp.mailfrom=aculab.com; arc=none smtp.client-ip=185.58.85.151 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=ACULAB.COM Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=aculab.com Received: from AcuMS.aculab.com (156.67.243.121 [156.67.243.121]) by relay.mimecast.com with ESMTP with both STARTTLS and AUTH (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384) id uk-mta-198-CxVAjfcuPEWgOlgQfy2h4w-1; Wed, 24 Jul 2024 15:28:55 +0100 X-MC-Unique: CxVAjfcuPEWgOlgQfy2h4w-1 Received: from AcuMS.Aculab.com (10.202.163.4) by AcuMS.aculab.com (10.202.163.4) with Microsoft SMTP Server (TLS) id 15.0.1497.48; Wed, 24 Jul 2024 15:28:14 +0100 Received: from AcuMS.Aculab.com ([::1]) by AcuMS.aculab.com ([::1]) with mapi id 15.00.1497.048; Wed, 24 Jul 2024 15:28:14 +0100 From: David Laight To: "'linux-kernel@vger.kernel.org'" , "'Linus Torvalds'" CC: "'Matthew Wilcox (Oracle)'" , 'Christoph Hellwig' , 'Andrew Morton' , "'Andy Shevchenko'" , 'Dan Carpenter' , 'Arnd Bergmann' , "'Jason@zx2c4.com'" , "'hch@infradead.org'" , "'pedro.falcato@gmail.com'" , 'Mateusz Guzik' , "'linux-mm@kvack.org'" Subject: [PATCH 1/7] minmax: Put all the clamp() definitions together Thread-Topic: [PATCH 1/7] minmax: Put all the clamp() definitions together Thread-Index: Adrd1c0Cjv5okNExTC+hbu71EtjxQw== Date: Wed, 24 Jul 2024 14:28:14 +0000 Message-ID: References: <23bdb6fc8d884ceebeb6e8b8653b8cfe@AcuMS.aculab.com> In-Reply-To: <23bdb6fc8d884ceebeb6e8b8653b8cfe@AcuMS.aculab.com> Accept-Language: en-GB, en-US X-MS-Has-Attach: X-MS-TNEF-Correlator: x-ms-exchange-transport-fromentityheader: Hosted Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: aculab.com Content-Language: en-US Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" The defines for clamp() have got separated, move togther for readability. Update description of signedness check. Signed-off-by: David Laight Reviewed-by: Lorenzo Stoakes Reviewed-by: Lorenzo Stoakes fwiw on that :) --- include/linux/minmax.h | 120 +++++++++++++++++++---------------------- 1 file changed, 56 insertions(+), 64 deletions(-) diff --git a/include/linux/minmax.h b/include/linux/minmax.h index 2ec559284a9f..63c45865b48a 100644 --- a/include/linux/minmax.h +++ b/include/linux/minmax.h @@ -57,26 +57,6 @@ __cmp(op, x, y), \ __cmp_once(op, x, y, __UNIQUE_ID(__x), __UNIQUE_ID(__y))) =20 -#define __clamp(val, lo, hi) \ - ((val) >=3D (hi) ? (hi) : ((val) <=3D (lo) ? (lo) : (val))) - -#define __clamp_once(val, lo, hi, unique_val, unique_lo, unique_hi) ({ \ - typeof(val) unique_val =3D (val); \ - typeof(lo) unique_lo =3D (lo); \ - typeof(hi) unique_hi =3D (hi); \ - static_assert(__builtin_choose_expr(__is_constexpr((lo) > (hi)), \ - (lo) <=3D (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); }) - -#define __careful_clamp(val, lo, hi) ({ \ - __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))); }) - /** * min - return minimum of two values of the same or compatible types * @x: first value @@ -124,6 +104,22 @@ */ #define max3(x, y, z) max((typeof(x))max(x, y), z) =20 +/** + * 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(min, (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(max, (type)(x), (type)(y)) + /** * min_not_zero - return the minimum that is _not_ zero, unless both are z= ero * @x: value1 @@ -134,39 +130,60 @@ typeof(y) __y =3D (y); \ __x =3D=3D 0 ? __y : ((__y =3D=3D 0) ? __x : min(__x, __y)); }) =20 +#define __clamp(val, lo, hi) \ + ((val) >=3D (hi) ? (hi) : ((val) <=3D (lo) ? (lo) : (val))) + +#define __clamp_once(val, lo, hi, unique_val, unique_lo, unique_hi) ({ \ + typeof(val) unique_val =3D (val); \ + typeof(lo) unique_lo =3D (lo); \ + typeof(hi) unique_hi =3D (hi); \ + static_assert(__builtin_choose_expr(__is_constexpr((lo) > (hi)), \ + (lo) <=3D (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); }) + +#define __careful_clamp(val, lo, hi) ({ \ + __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 - 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. + * This macro checks that @val, @lo and @hi have the same signedness. */ #define clamp(val, lo, hi) __careful_clamp(val, lo, hi) =20 -/* - * ..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 + * 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 min_t(type, x, y) __careful_cmp(min, (type)(x), (type)(y)) +#define clamp_t(type, val, lo, hi) __careful_clamp((type)(val), (type)(lo)= , (type)(hi)) =20 /** - * max_t - return maximum of two values, using the specified type - * @type: data type to use - * @x: first value - * @y: second value + * 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 unsign= ed + * type and @lo and @hi are literals that will otherwise be assigned a sig= ned + * integer type. */ -#define max_t(type, x, y) __careful_cmp(max, (type)(x), (type)(y)) +#define clamp_val(val, lo, hi) clamp_t(typeof(val), val, lo, hi) =20 /* * Do not check the array parameter using __must_be_array(). @@ -211,31 +228,6 @@ */ #define max_array(array, len) __minmax_array(max, array, len) =20 -/** - * 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) __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 unsign= ed - * type and @lo and @hi are literals that will otherwise be assigned a sig= ned - * integer type. - */ -#define clamp_val(val, lo, hi) clamp_t(typeof(val), val, lo, hi) - static inline bool in_range64(u64 val, u64 start, u64 len) { return (val - start) < len; --=20 2.17.1 - Registered Address Lakeside, Bramley Road, Mount Farm, Milton Keynes, MK1 1= PT, UK Registration No: 1397386 (Wales) From nobody Thu Dec 18 11:55:57 2025 Received: from eu-smtp-delivery-151.mimecast.com (eu-smtp-delivery-151.mimecast.com [185.58.86.151]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 888D3134DE for ; Wed, 24 Jul 2024 14:29:45 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=185.58.86.151 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1721831387; cv=none; b=lX8q75vc1hRNaUCZuw197CI10d3fdfqVu0oZOp0ro4T2Ee/i4Aw1TRvRYRzi0d5mPavjTUy4uk3fZJn+Og7fCph/ckMT5IUMPvteTjBk6jSzfkWZlWBRNz7wi3TnytE3fEg3F1ON+y1Zv/n5rtEyUWsiRT396P7nOfJR0uhv43Y= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1721831387; c=relaxed/simple; bh=EhqLrJgqYxdlTb2Zl2yShcG1KzJBygJskqHd8w4qHqo=; h=From:To:CC:Subject:Date:Message-ID:References:In-Reply-To: MIME-Version:Content-Type; b=VxxHKB96nAW9NTFArodA96GlYF+0I6S79ZkVLVIbBfM4eKgy6AsHBzipx8DgN9NagYRxgDbSxNJEsBZhig9hwYvRYkNmbJ7Wm86Udo4HYVCLgQCumcsG4x3Y0aeqEZvyJUUaNdlrQsjUbLaDAaLDHafBS+izRhVGdccCnqH0LkU= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=ACULAB.COM; spf=pass smtp.mailfrom=aculab.com; arc=none smtp.client-ip=185.58.86.151 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=ACULAB.COM Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=aculab.com Received: from AcuMS.aculab.com (156.67.243.121 [156.67.243.121]) by relay.mimecast.com with ESMTP with both STARTTLS and AUTH (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384) id uk-mta-267-KJPviE2xP9qjIHrECg06Og-1; Wed, 24 Jul 2024 15:29:42 +0100 X-MC-Unique: KJPviE2xP9qjIHrECg06Og-1 Received: from AcuMS.Aculab.com (10.202.163.4) by AcuMS.aculab.com (10.202.163.4) with Microsoft SMTP Server (TLS) id 15.0.1497.48; Wed, 24 Jul 2024 15:29:02 +0100 Received: from AcuMS.Aculab.com ([::1]) by AcuMS.aculab.com ([::1]) with mapi id 15.00.1497.048; Wed, 24 Jul 2024 15:29:02 +0100 From: David Laight To: "'linux-kernel@vger.kernel.org'" , "'Linus Torvalds'" CC: "'Matthew Wilcox (Oracle)'" , 'Christoph Hellwig' , 'Andrew Morton' , "'Andy Shevchenko'" , 'Dan Carpenter' , 'Arnd Bergmann' , "'Jason@zx2c4.com'" , "'hch@infradead.org'" , "'pedro.falcato@gmail.com'" , 'Mateusz Guzik' , "'linux-mm@kvack.org'" Subject: [PATCH 2/7] minmax: Use _Static_assert() instead of static_assert() Thread-Topic: [PATCH 2/7] minmax: Use _Static_assert() instead of static_assert() Thread-Index: Adrd1elaiPvTR2LWQ4qjY5Mia/iOEQ== Date: Wed, 24 Jul 2024 14:29:02 +0000 Message-ID: <171c2c31e16841bca0c4fbb085877d7e@AcuMS.aculab.com> References: <23bdb6fc8d884ceebeb6e8b8653b8cfe@AcuMS.aculab.com> In-Reply-To: <23bdb6fc8d884ceebeb6e8b8653b8cfe@AcuMS.aculab.com> Accept-Language: en-GB, en-US X-MS-Has-Attach: X-MS-TNEF-Correlator: x-ms-exchange-transport-fromentityheader: Hosted Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: aculab.com Content-Language: en-US Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" The static_assert() wrapper provides the text of the expression as the error message, this isn't needed here as an explicit message is provided. If there is an error (quite likely for min/max) the wrapper also adds two more lines of error output that just make it harder to read. Since it gives no benefit and actually makes things worse directly using _Static_assert() is much better. Signed-off-by: David Laight Reviewed-by: Lorenzo Stoakes Reviewed-by: Lorenzo Stoakes fwiw on that :) --- include/linux/minmax.h | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/include/linux/minmax.h b/include/linux/minmax.h index 63c45865b48a..900eec7a28e5 100644 --- a/include/linux/minmax.h +++ b/include/linux/minmax.h @@ -48,7 +48,7 @@ #define __cmp_once(op, x, y, unique_x, unique_y) ({ \ typeof(x) unique_x =3D (x); \ typeof(y) unique_y =3D (y); \ - static_assert(__types_ok(x, 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); }) =20 @@ -137,11 +137,11 @@ typeof(val) unique_val =3D (val); \ typeof(lo) unique_lo =3D (lo); \ typeof(hi) unique_hi =3D (hi); \ - static_assert(__builtin_choose_expr(__is_constexpr((lo) > (hi)), \ + _Static_assert(__builtin_choose_expr(__is_constexpr((lo) > (hi)), \ (lo) <=3D (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_ok(val, lo), "clamp() 'lo' signedness error"); \ + _Static_assert(__types_ok(val, hi), "clamp() 'hi' signedness error"); \ __clamp(unique_val, unique_lo, unique_hi); }) =20 #define __careful_clamp(val, lo, hi) ({ \ --=20 2.17.1 - Registered Address Lakeside, Bramley Road, Mount Farm, Milton Keynes, MK1 1= PT, UK Registration No: 1397386 (Wales) From nobody Thu Dec 18 11:55:57 2025 Received: from eu-smtp-delivery-151.mimecast.com (eu-smtp-delivery-151.mimecast.com [185.58.85.151]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id BD41D158DC1 for ; Wed, 24 Jul 2024 14:30:35 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=185.58.85.151 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1721831437; cv=none; b=Lf+SYFVnn/U22cnSS21cjDuv/fbU59KhZoJ6bjtv2sj2R6nf9ZkSBnuBy0gOCKQGHvgVnKS/A3uaCOkEROxepTAluhyqwv819VR6fKgShLtt7pcyWQdOkMjyEeQkLPSqdtUomSx49UsV/8iR+bvcOziU1aoKC9w8KU4Gi81ROFo= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1721831437; c=relaxed/simple; bh=AaKdN6HcqgfCCFAVvz/Al+Lrum6JH6Mv01fYiGr+egQ=; h=From:To:CC:Subject:Date:Message-ID:References:In-Reply-To: MIME-Version:Content-Type; b=cyFpKrH9dV2ihFoyQTEcypZfO5+XIoskyC9rf3aC9oCgrA+QK2u794whEphBtNEFQNeK721OfZEEEU00VOpYs+Y45nnK2rgF7oS2k8DFt8k9xr7AKOb3NJHCueBQHw1fEX+y+BbTd4yDDDOF+WPpbr3GCBsWJaidfYXGU7Nu4M8= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=ACULAB.COM; spf=pass smtp.mailfrom=aculab.com; arc=none smtp.client-ip=185.58.85.151 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=ACULAB.COM Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=aculab.com Received: from AcuMS.aculab.com (156.67.243.121 [156.67.243.121]) by relay.mimecast.com with ESMTP with both STARTTLS and AUTH (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384) id uk-mta-178-jMukQN13NBi_V9QrS7w-jQ-1; Wed, 24 Jul 2024 15:30:32 +0100 X-MC-Unique: jMukQN13NBi_V9QrS7w-jQ-1 Received: from AcuMS.Aculab.com (10.202.163.4) by AcuMS.aculab.com (10.202.163.4) with Microsoft SMTP Server (TLS) id 15.0.1497.48; Wed, 24 Jul 2024 15:29:52 +0100 Received: from AcuMS.Aculab.com ([::1]) by AcuMS.aculab.com ([::1]) with mapi id 15.00.1497.048; Wed, 24 Jul 2024 15:29:52 +0100 From: David Laight To: "'linux-kernel@vger.kernel.org'" , "'Linus Torvalds'" CC: "'Matthew Wilcox (Oracle)'" , 'Christoph Hellwig' , 'Andrew Morton' , "'Andy Shevchenko'" , 'Dan Carpenter' , 'Arnd Bergmann' , "'Jason@zx2c4.com'" , "'hch@infradead.org'" , "'pedro.falcato@gmail.com'" , 'Mateusz Guzik' , "'linux-mm@kvack.org'" Subject: [PATCH 3/7] compiler.h: Add __if_constexpr(expr, if_const, if_not_const) Thread-Topic: [PATCH 3/7] compiler.h: Add __if_constexpr(expr, if_const, if_not_const) Thread-Index: Adrd1gZ7oS190yPcQj+hKNIa4uSuaQ== Date: Wed, 24 Jul 2024 14:29:52 +0000 Message-ID: <9751d18defea406fa698630637d8e7db@AcuMS.aculab.com> References: <23bdb6fc8d884ceebeb6e8b8653b8cfe@AcuMS.aculab.com> In-Reply-To: <23bdb6fc8d884ceebeb6e8b8653b8cfe@AcuMS.aculab.com> Accept-Language: en-GB, en-US X-MS-Has-Attach: X-MS-TNEF-Correlator: x-ms-exchange-transport-fromentityheader: Hosted Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: aculab.com Content-Language: en-US Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" __if_constexpr(expr, if_const, if_not_const) returns 'if_const' if 'expr' is a 'constant integer expression' otherwise 'if_not_const'. The two values may have different types. Redefine __is_constextpr(expr) as __if_constexpr(expr, 1, 0). Implemented using _Generic() for portibility. Add proper kerndoc comments. Signed-off-by: David Laight Reviewed-by: Lorenzo Stoakes Reviewed-by: Lorenzo Stoakes fwiw on that :) --- include/linux/compiler.h | 65 +++++++++++++--------------------------- 1 file changed, 21 insertions(+), 44 deletions(-) diff --git a/include/linux/compiler.h b/include/linux/compiler.h index 2594553bb30b..7d559e390011 100644 --- a/include/linux/compiler.h +++ b/include/linux/compiler.h @@ -242,52 +242,29 @@ static inline void *offset_to_ptr(const int *off) /* &a[0] degrades to a pointer: a different type from an array */ #define __must_be_array(a) BUILD_BUG_ON_ZERO(__same_type((a), &(a)[0])) =20 -/* - * This returns a constant expression while determining if an argument is - * a constant expression, most importantly without evaluating the argument. - * Glory to Martin Uecker +/** + * __if_constexpr - Check whether an expression is an 'integer + * constant expression'=20 + * @expr: Expression to test, not evaluated, can be a pointer + * @if_const: return value if constant + * @if_not_const: return value if not constant + * + * The return values @if_const and @if_not_const can have different types. * - * Details: - * - sizeof() return an integer constant expression, and does not evaluate - * the value of its operand; it only examines the type of its operand. - * - The results of comparing two integer constant expressions is also - * an integer constant expression. - * - The first literal "8" isn't important. It could be any literal value. - * - The second literal "8" is to avoid warnings about unaligned pointers; - * this could otherwise just be "1". - * - (long)(x) is used to avoid warnings about 64-bit types on 32-bit - * architectures. - * - The C Standard defines "null pointer constant", "(void *)0", as - * distinct from other void pointers. - * - If (x) is an integer constant expression, then the "* 0l" resolves - * it into an integer constant expression of value 0. Since it is cast to - * "void *", this makes the second operand a null pointer constant. - * - If (x) is not an integer constant expression, then the second operand - * resolves to a void pointer (but not a null pointer constant: the value - * is not an integer constant 0). - * - The conditional operator's third operand, "(int *)8", is an object - * pointer (to type "int"). - * - The behavior (including the return type) of the conditional operator - * ("operand1 ? operand2 : operand3") depends on the kind of expressions - * given for the second and third operands. This is the central mechanism - * of the macro: - * - When one operand is a null pointer constant (i.e. when x is an inte= ger - * constant expression) and the other is an object pointer (i.e. our - * third operand), the conditional operator returns the type of the - * object pointer operand (i.e. "int *"). Here, within the sizeof(), we - * would then get: - * sizeof(*((int *)(...)) =3D=3D sizeof(int) =3D=3D 4 - * - When one operand is a void pointer (i.e. when x is not an integer - * constant expression) and the other is an object pointer (i.e. our - * third operand), the conditional operator returns a "void *" type. - * Here, within the sizeof(), we would then get: - * sizeof(*((void *)(...)) =3D=3D sizeof(void) =3D=3D 1 - * - The equality comparison to "sizeof(int)" therefore depends on (x): - * sizeof(int) =3D=3D sizeof(int) (x) was a constant expression - * sizeof(int) !=3D sizeof(void) (x) was not a constant expression + * Relies on typeof(x ? NULL : ptr_type) being ptr_type and + * typeof(x ? (void *)y : ptr_type) being 'void *'. + */ +#define __if_constexpr(expr, if_const, if_not_const) \ + _Generic(0 ? ((void *)((long)(expr) * 0l)) : (char *)0, \ + char *: (if_const), \ + void *: (if_not_const)) + +/** + * __is_constexpr - Return 1 for an 'integer constant expression' + * 0 otherwise. + * @expr: expression to check, not evaluated */ -#define __is_constexpr(x) \ - (sizeof(int) =3D=3D sizeof(*(8 ? ((void *)((long)(x) * 0l)) : (int *)8))) +#define __is_constexpr(expr) __if_constexpr((expr), 1, 0) =20 /* * Whether 'type' is a signed type or an unsigned type. Supports scalar ty= pes, --=20 2.17.1 - Registered Address Lakeside, Bramley Road, Mount Farm, Milton Keynes, MK1 1= PT, UK Registration No: 1397386 (Wales) From nobody Thu Dec 18 11:55:57 2025 Received: from eu-smtp-delivery-151.mimecast.com (eu-smtp-delivery-151.mimecast.com [185.58.86.151]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 0DFA64C6D for ; Wed, 24 Jul 2024 14:31:35 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=185.58.86.151 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1721831497; cv=none; b=SF2Q2wsdsbjLVmMn/2LaxX2lYYJC70D2HyRJqSHnpyS2lcC5V1bgMaysoofDEUbF4UJFWipoVZUVIKpRqm45yIaDpvsO/XVfcBOW+r7AHZhqTkLJ4N2zz1Vvepe1EImog8HsWbJGJbWGaMdnqoCVIYZRkRW2QbkCBtechhBMPDc= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1721831497; c=relaxed/simple; bh=oSxGQle+W5noOa3xjHkEranHgL72w3YI6tsrzK9FGF8=; h=From:To:CC:Subject:Date:Message-ID:References:In-Reply-To: MIME-Version:Content-Type; b=MpBSlbtBCf1AnShM4g/TSQ1VjdQ3aUjY573wSQPTkrnCrVcamC5/VJR1i+YQyWZrU5qzE7NgMRKebkP4pO+Ay4ZGbsqAmhZiK1FlAzEClpZmaFfh6EVNDD7Wd0cJbvqM++L61WZjg3tGNnL6Ht9vSDeLjX+1kQGwFU6t/KX2fTM= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=ACULAB.COM; spf=pass smtp.mailfrom=aculab.com; arc=none smtp.client-ip=185.58.86.151 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=ACULAB.COM Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=aculab.com Received: from AcuMS.aculab.com (156.67.243.121 [156.67.243.121]) by relay.mimecast.com with ESMTP with both STARTTLS and AUTH (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384) id uk-mta-45-vGdKy7opNf27HweZ6J__xg-1; Wed, 24 Jul 2024 15:31:32 +0100 X-MC-Unique: vGdKy7opNf27HweZ6J__xg-1 Received: from AcuMS.Aculab.com (10.202.163.4) by AcuMS.aculab.com (10.202.163.4) with Microsoft SMTP Server (TLS) id 15.0.1497.48; Wed, 24 Jul 2024 15:30:52 +0100 Received: from AcuMS.Aculab.com ([::1]) by AcuMS.aculab.com ([::1]) with mapi id 15.00.1497.048; Wed, 24 Jul 2024 15:30:52 +0100 From: David Laight To: "'linux-kernel@vger.kernel.org'" , "'Linus Torvalds'" CC: "'Matthew Wilcox (Oracle)'" , 'Christoph Hellwig' , 'Andrew Morton' , "'Andy Shevchenko'" , 'Dan Carpenter' , 'Arnd Bergmann' , "'Jason@zx2c4.com'" , "'hch@infradead.org'" , "'pedro.falcato@gmail.com'" , 'Mateusz Guzik' , "'linux-mm@kvack.org'" Subject: [PATCH 4/7] minmax: Simplify signedness check Thread-Topic: [PATCH 4/7] minmax: Simplify signedness check Thread-Index: Adrd1i0k/JcX2h1sSAO9D37F5HIFAA== Date: Wed, 24 Jul 2024 14:30:52 +0000 Message-ID: <03601661326c4efba4e618ead15fa0e2@AcuMS.aculab.com> References: <23bdb6fc8d884ceebeb6e8b8653b8cfe@AcuMS.aculab.com> In-Reply-To: <23bdb6fc8d884ceebeb6e8b8653b8cfe@AcuMS.aculab.com> Accept-Language: en-GB, en-US X-MS-Has-Attach: X-MS-TNEF-Correlator: x-ms-exchange-transport-fromentityheader: Hosted Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: aculab.com Content-Language: en-US Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" It is enough to check that both 'x' and 'y' are valid for either a signed compare or an unsigned compare. For unsigned they must be an unsigned type or a positive constant. For signed they must be signed after unsigned char/short are promoted. Order the expressions to avoid warnings about comparisons that are always true. Signed-off-by: David Laight Reviewed-by: Lorenzo Stoakes Reviewed-by: Lorenzo Stoakes fwiw on that :) --- include/linux/minmax.h | 22 ++++++++++------------ 1 file changed, 10 insertions(+), 12 deletions(-) diff --git a/include/linux/minmax.h b/include/linux/minmax.h index 900eec7a28e5..d3ac65c1add7 100644 --- a/include/linux/minmax.h +++ b/include/linux/minmax.h @@ -8,7 +8,7 @@ #include =20 /* - * min()/max()/clamp() macros must accomplish three things: + * min()/max()/clamp() macros must accomplish several things: * * - Avoid multiple evaluations of the arguments (so side-effects like * "x++" happen only once) when non-constant. @@ -26,19 +26,17 @@ #define __typecheck(x, y) \ (!!(sizeof((typeof(x) *)1 =3D=3D (typeof(y) *)1))) =20 -/* is_signed_type() isn't a constexpr for pointer types */ -#define __is_signed(x) \ - __builtin_choose_expr(__is_constexpr(is_signed_type(typeof(x))), \ - is_signed_type(typeof(x)), 0) +/* Allow unsigned compares against non-negative signed constants. */ +#define __is_ok_unsigned(x) \ + ((is_unsigned_type(typeof(x)) ? 0 : __if_constexpr(x, (x) + 0, -1)) >=3D = 0) =20 -/* True for a non-negative signed int constant */ -#define __is_noneg_int(x) \ - (__builtin_choose_expr(__is_constexpr(x) && __is_signed(x), x, -1) >=3D 0) +/* Check for signed after promoting unsigned char/short to int */ +#define __is_ok_signed(x) is_signed_type(typeof((x) + 0)) =20 -#define __types_ok(x, y) \ - (__is_signed(x) =3D=3D __is_signed(y) || \ - __is_signed((x) + 0) =3D=3D __is_signed((y) + 0) || \ - __is_noneg_int(x) || __is_noneg_int(y)) +/* Allow if both x and y are valid for either signed or unsigned compares.= */ +#define __types_ok(x, y) \ + ((__is_ok_signed(x) && __is_ok_signed(y)) || \ + (__is_ok_unsigned(x) && __is_ok_unsigned(y))) =20 #define __cmp_op_min < #define __cmp_op_max > --=20 2.17.1 - Registered Address Lakeside, Bramley Road, Mount Farm, Milton Keynes, MK1 1= PT, UK Registration No: 1397386 (Wales) From nobody Thu Dec 18 11:55:57 2025 Received: from eu-smtp-delivery-151.mimecast.com (eu-smtp-delivery-151.mimecast.com [185.58.86.151]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id B8A241E50F for ; Wed, 24 Jul 2024 14:32:28 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=185.58.86.151 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1721831550; cv=none; b=Y8WQkpqxNJC6o201bI8QHWC2unuzzMf6pzBQu1uiDKl16GIWhkNDTHSxw612MaVdkx2YDk/pedlIisW7qEPMP6SWHowtrUc0cy8A0BG4xWcBpT6ZJqxrYdLhjv+YDiUM/i7fWc2CugAXz2w5g6gUa+VFOZ9lNDxCgyg/T9UW5go= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1721831550; c=relaxed/simple; bh=uSPYPEqvPzAncnkNVy7BRec1JU2+johr/G+OLgn9r/k=; h=From:To:CC:Subject:Date:Message-ID:References:In-Reply-To: MIME-Version:Content-Type; b=Jdreq/62RYKlURtgUOITBiiv9WQoX4UNIv0gZBGWJpPMpM0VwhhBNe3tNkAV4VSwMnQfiW31IB1p+0MoGjOJKf4PBGu94P7nIAzJrrgf/OLSi7Z7diuflc5JZRwXKrR7wqfqa0eKt72E9A6hdRHSzC+Y8vYz08ljIISzV3B/h7Y= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=ACULAB.COM; spf=pass smtp.mailfrom=aculab.com; arc=none smtp.client-ip=185.58.86.151 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=ACULAB.COM Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=aculab.com Received: from AcuMS.aculab.com (156.67.243.121 [156.67.243.121]) by relay.mimecast.com with ESMTP with both STARTTLS and AUTH (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384) id uk-mta-201-p-9t2K8xPV2o1h-xKKZUHA-1; Wed, 24 Jul 2024 15:32:25 +0100 X-MC-Unique: p-9t2K8xPV2o1h-xKKZUHA-1 Received: from AcuMS.Aculab.com (10.202.163.4) by AcuMS.aculab.com (10.202.163.4) with Microsoft SMTP Server (TLS) id 15.0.1497.48; Wed, 24 Jul 2024 15:31:45 +0100 Received: from AcuMS.Aculab.com ([::1]) by AcuMS.aculab.com ([::1]) with mapi id 15.00.1497.048; Wed, 24 Jul 2024 15:31:45 +0100 From: David Laight To: "'linux-kernel@vger.kernel.org'" , "'Linus Torvalds'" CC: "'Matthew Wilcox (Oracle)'" , 'Christoph Hellwig' , 'Andrew Morton' , "'Andy Shevchenko'" , 'Dan Carpenter' , 'Arnd Bergmann' , "'Jason@zx2c4.com'" , "'hch@infradead.org'" , "'pedro.falcato@gmail.com'" , 'Mateusz Guzik' , "'linux-mm@kvack.org'" Subject: [PATCH 5/7] minmax: Factor out the zero-extension logic from umin/umax Thread-Topic: [PATCH 5/7] minmax: Factor out the zero-extension logic from umin/umax Thread-Index: Adrd1kXn5nCrisyjTAW42k9g3Yw8yA== Date: Wed, 24 Jul 2024 14:31:45 +0000 Message-ID: References: <23bdb6fc8d884ceebeb6e8b8653b8cfe@AcuMS.aculab.com> In-Reply-To: <23bdb6fc8d884ceebeb6e8b8653b8cfe@AcuMS.aculab.com> Accept-Language: en-GB, en-US X-MS-Has-Attach: X-MS-TNEF-Correlator: x-ms-exchange-transport-fromentityheader: Hosted Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: aculab.com Content-Language: en-US Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" The '+ 0u + 0ul + 0ull' to zero extend to 64bit on both 32bit and 64bit systems was replicated 4 times. Factor out and then join up some 'not overlong' lines. Signed-off-by: David Laight Reviewed-by: Lorenzo Stoakes Reviewed-by: Lorenzo Stoakes fwiw on that :) --- include/linux/minmax.h | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/include/linux/minmax.h b/include/linux/minmax.h index d3ac65c1add7..e250957036a1 100644 --- a/include/linux/minmax.h +++ b/include/linux/minmax.h @@ -28,7 +28,7 @@ =20 /* Allow unsigned compares against non-negative signed constants. */ #define __is_ok_unsigned(x) \ - ((is_unsigned_type(typeof(x)) ? 0 : __if_constexpr(x, (x) + 0, -1)) >=3D = 0) + ((is_unsigned_type(typeof(x)) ? 0 : __if_constexpr(x, x, -1)) >=3D 0) =20 /* Check for signed after promoting unsigned char/short to int */ #define __is_ok_signed(x) is_signed_type(typeof((x) + 0)) @@ -69,22 +69,26 @@ */ #define max(x, y) __careful_cmp(max, x, y) =20 +/* + * Zero extend a non-negative value to 64bits. + * Undefined for negative values. + * The extension to 64 bits is often optimised away. + */ +#define __zero_extend(x) ((x) + 0u + 0ul + 0ull) + /** * umin - return minimum of two non-negative values - * Signed types are zero extended to match a larger unsigned type. * @x: first value * @y: second value */ -#define umin(x, y) \ - __careful_cmp(min, (x) + 0u + 0ul + 0ull, (y) + 0u + 0ul + 0ull) +#define umin(x, y) __careful_cmp(min, __zero_extend(x), __zero_extend(y)) =20 /** * umax - return maximum of two non-negative values * @x: first value * @y: second value */ -#define umax(x, y) \ - __careful_cmp(max, (x) + 0u + 0ul + 0ull, (y) + 0u + 0ul + 0ull) +#define umax(x, y) __careful_cmp(max, __zero_extend(x), __zero_extend(y)) =20 /** * min3 - return minimum of three values --=20 2.17.1 - Registered Address Lakeside, Bramley Road, Mount Farm, Milton Keynes, MK1 1= PT, UK Registration No: 1397386 (Wales) From nobody Thu Dec 18 11:55:57 2025 Received: from eu-smtp-delivery-151.mimecast.com (eu-smtp-delivery-151.mimecast.com [185.58.85.151]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 934FC1586DB for ; Wed, 24 Jul 2024 14:33:08 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=185.58.85.151 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1721831590; cv=none; b=GrTyLFFsEzHanx+H+bSXy13Wuh7d+LWEV9XcXdyUleHTurU0n0VwjDJIsT5WkyMvoxwxNRppB47MzpMVbV8nRqhkvJJenwKnGAcRIg0aIVuZtvB5Nr9oLJ8CM7ZZWRRnZ4P9y9IJjwIrIZNaEbx5Of0gF/UL3s6062wMkS2S2yw= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1721831590; c=relaxed/simple; bh=QDy57ZVQoNG35uF/IJ1jWNERroyPlmeJLF5V2Pdwlk8=; h=From:To:CC:Subject:Date:Message-ID:References:In-Reply-To: MIME-Version:Content-Type; b=OuS770DfQ8+65OkY+n53UCkcRQlJy7GOIBxzbBArE/sWsTmlcv3ioYT0YCtvspyyLSnmTuSJ0boit5Vp//i+EG+uQB88NEOBJSqIL1sHlSjHIphWPNkD46vMP2MhmlF8vZrka7kEg3oJUE2gC1vvvCZPtRg8qCDneGX6UJVQPN0= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=ACULAB.COM; spf=pass smtp.mailfrom=aculab.com; arc=none smtp.client-ip=185.58.85.151 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=ACULAB.COM Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=aculab.com Received: from AcuMS.aculab.com (156.67.243.121 [156.67.243.121]) by relay.mimecast.com with ESMTP with both STARTTLS and AUTH (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384) id uk-mta-312-401NOTxeOC-NAXlBON6hgw-1; Wed, 24 Jul 2024 15:33:03 +0100 X-MC-Unique: 401NOTxeOC-NAXlBON6hgw-1 Received: from AcuMS.Aculab.com (10.202.163.4) by AcuMS.aculab.com (10.202.163.4) with Microsoft SMTP Server (TLS) id 15.0.1497.48; Wed, 24 Jul 2024 15:32:23 +0100 Received: from AcuMS.Aculab.com ([::1]) by AcuMS.aculab.com ([::1]) with mapi id 15.00.1497.048; Wed, 24 Jul 2024 15:32:23 +0100 From: David Laight To: "'linux-kernel@vger.kernel.org'" , "'Linus Torvalds'" CC: "'Matthew Wilcox (Oracle)'" , 'Christoph Hellwig' , 'Andrew Morton' , "'Andy Shevchenko'" , 'Dan Carpenter' , 'Arnd Bergmann' , "'Jason@zx2c4.com'" , "'hch@infradead.org'" , "'pedro.falcato@gmail.com'" , 'Mateusz Guzik' , "'linux-mm@kvack.org'" Subject: [PATCH 6/7] minmax: Optimise _Static_assert() check in clamp() Thread-Topic: [PATCH 6/7] minmax: Optimise _Static_assert() check in clamp() Thread-Index: Adrd1mBpxXCRV5VDT5OkO29yw5aIOQ== Date: Wed, 24 Jul 2024 14:32:22 +0000 Message-ID: <46f446f61c4f4304b756e38a19782ed9@AcuMS.aculab.com> References: <23bdb6fc8d884ceebeb6e8b8653b8cfe@AcuMS.aculab.com> In-Reply-To: <23bdb6fc8d884ceebeb6e8b8653b8cfe@AcuMS.aculab.com> Accept-Language: en-GB, en-US X-MS-Has-Attach: X-MS-TNEF-Correlator: x-ms-exchange-transport-fromentityheader: Hosted Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: aculab.com Content-Language: en-US Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Use __if_constexpr() instead of __builtin_choose_expr(__is_constexpr()). Signed-off-by: David Laight Reviewed-by: Lorenzo Stoakes Reviewed-by: Lorenzo Stoakes fwiw on that :) --- include/linux/minmax.h | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/include/linux/minmax.h b/include/linux/minmax.h index e250957036a1..2fb63efbeb0e 100644 --- a/include/linux/minmax.h +++ b/include/linux/minmax.h @@ -139,8 +139,7 @@ typeof(val) unique_val =3D (val); \ typeof(lo) unique_lo =3D (lo); \ typeof(hi) unique_hi =3D (hi); \ - _Static_assert(__builtin_choose_expr(__is_constexpr((lo) > (hi)), \ - (lo) <=3D (hi), true), \ + _Static_assert(__if_constexpr((lo) <=3D (hi), (lo) <=3D (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"); \ --=20 2.17.1 - Registered Address Lakeside, Bramley Road, Mount Farm, Milton Keynes, MK1 1= PT, UK Registration No: 1397386 (Wales) From nobody Thu Dec 18 11:55:57 2025 Received: from eu-smtp-delivery-151.mimecast.com (eu-smtp-delivery-151.mimecast.com [185.58.85.151]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 2ADB12D030 for ; Wed, 24 Jul 2024 14:33:53 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=185.58.85.151 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1721831636; cv=none; b=L494222RoD1yP96sGal2a+6TSjdzwSRc3XLHbcw3QdzZKOuk0KZiZHDa66TWhbgQ/k4TokL75TmKT9thuV2N0p60uofZneyKNx9qV1dVSd3cI40G2wn/y0ouE9WxbFeorzbaIfisz7ztfaQYY2nkof8oxy6Hlzv6dsydqgbcHXA= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1721831636; c=relaxed/simple; bh=jLnKvSZlPOAnSOf1fn/4TEqyIhl6sYZr0GkcFdPm6IY=; h=From:To:CC:Subject:Date:Message-ID:References:In-Reply-To: MIME-Version:Content-Type; b=YDJZynY4eUOwrybIDW6fmMBJ2793Z5Ict1+Uz59g30ye+B9FL+sXQYw+IS1vsLgUWzByvixnZSNE5zaq2pQD+Q4TGLVdo8Cy9vR+OCZkYrEhZFD4nxj1cReCnRiDo3OCkDkIGJfwz/8vvxtdKcT4SZ/+X0PdDq4oyN8FsyLs1pY= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=ACULAB.COM; spf=pass smtp.mailfrom=aculab.com; arc=none smtp.client-ip=185.58.85.151 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=ACULAB.COM Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=aculab.com Received: from AcuMS.aculab.com (156.67.243.121 [156.67.243.121]) by relay.mimecast.com with ESMTP with both STARTTLS and AUTH (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384) id uk-mta-310-eKrR_BFuOPKLqYqDBV76gg-1; Wed, 24 Jul 2024 15:33:51 +0100 X-MC-Unique: eKrR_BFuOPKLqYqDBV76gg-1 Received: from AcuMS.Aculab.com (10.202.163.4) by AcuMS.aculab.com (10.202.163.4) with Microsoft SMTP Server (TLS) id 15.0.1497.48; Wed, 24 Jul 2024 15:33:11 +0100 Received: from AcuMS.Aculab.com ([::1]) by AcuMS.aculab.com ([::1]) with mapi id 15.00.1497.048; Wed, 24 Jul 2024 15:33:11 +0100 From: David Laight To: "'linux-kernel@vger.kernel.org'" , "'Linus Torvalds'" CC: "'Matthew Wilcox (Oracle)'" , 'Christoph Hellwig' , 'Andrew Morton' , "'Andy Shevchenko'" , 'Dan Carpenter' , 'Arnd Bergmann' , "'Jason@zx2c4.com'" , "'hch@infradead.org'" , "'pedro.falcato@gmail.com'" , 'Mateusz Guzik' , "'linux-mm@kvack.org'" Subject: [PATCH 7/7] minmax: minmax: Add __types_ok3() and optimise defines with 3 arguments Thread-Topic: [PATCH 7/7] minmax: minmax: Add __types_ok3() and optimise defines with 3 arguments Thread-Index: Adrd1nwhwSRYGwj9RxiIgqAQhw9FiA== Date: Wed, 24 Jul 2024 14:33:11 +0000 Message-ID: <3484b7fcd2c74655bd685e5a7030c284@AcuMS.aculab.com> References: <23bdb6fc8d884ceebeb6e8b8653b8cfe@AcuMS.aculab.com> In-Reply-To: <23bdb6fc8d884ceebeb6e8b8653b8cfe@AcuMS.aculab.com> Accept-Language: en-GB, en-US X-MS-Has-Attach: X-MS-TNEF-Correlator: x-ms-exchange-transport-fromentityheader: Hosted Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: aculab.com Content-Language: en-US Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" 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 Reviewed-by: Lorenzo Stoakes Reviewed-by: Lorenzo Stoakes fwiw on that :) --- include/linux/minmax.h | 25 ++++++++++++++++++++----- 1 file changed, 20 insertions(+), 5 deletions(-) diff --git a/include/linux/minmax.h b/include/linux/minmax.h index 2fb63efbeb0e..4bbc82c589cf 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))) =20 +/* 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 > =20 @@ -90,13 +95,24 @@ */ #define umax(x, y) __careful_cmp(max, __zero_extend(x), __zero_extend(y)) =20 +#define __cmp_once3(op, x, y, z, uniq) ({ \ + typeof(x) __x_##uniq =3D (x); \ + typeof(x) __y_##uniq =3D (y); \ + typeof(x) __z_##uniq =3D (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__) =20 /** * max3 - return maximum of three values @@ -104,7 +120,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__) =20 /** * min_t - return minimum of two values, using the specified type @@ -139,10 +155,9 @@ typeof(val) unique_val =3D (val); \ typeof(lo) unique_lo =3D (lo); \ typeof(hi) unique_hi =3D (hi); \ - _Static_assert(__if_constexpr((lo) <=3D (hi), (lo) <=3D (hi), true), \ + _Static_assert(__if_constexpr((lo) <=3D (hi), (lo) <=3D (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); }) =20 #define __careful_clamp(val, lo, hi) ({ \ --=20 2.17.1 - Registered Address Lakeside, Bramley Road, Mount Farm, Milton Keynes, MK1 1= PT, UK Registration No: 1397386 (Wales)