From nobody Thu Dec 18 00:48:15 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 CB9683F9EC for ; Sun, 28 Jul 2024 14:18:24 +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=1722176307; cv=none; b=YdLE8GQqpLquURMtqj+VmttdkvMp5+5WfBW/M7/JPK1UuRdQv+ARpAIjOYOCmb5VjkJkxpU9IHdED9nxREcPoFPlF2r8gGiTtoUZQh9ybqDQCv6Xp6S7R4enoJgMwup60CoOTVu60/A/eJ96kMG/Kg32tJzPh3rOu0ETkH8q4d8= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1722176307; c=relaxed/simple; bh=E8+TeBARjX1dBlV0YiDV6t+Yt+NzsvpygUkkzqnB8WA=; h=From:To:CC:Subject:Date:Message-ID:References:In-Reply-To: MIME-Version:Content-Type; b=KmmX7xFhlD9Xlr0yviSHUOHBsyktKIVJFNqtaCLrTLYNQ2vJ4SO1Akj5V/SKSKez37GJKZ914sWiHo8XjPo1UeTzzNaPu5ii6uBVnK/hkO2qJ8sM3mNP7IYrQq2q77SG0Z8T/Pk09gGx9yCL5SZTPRrC27k8wQ/ER0jW2oL3TfU= 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-68-U7LTviH9N2qGDn-NaZVzAQ-1; Sun, 28 Jul 2024 15:18:20 +0100 X-MC-Unique: U7LTviH9N2qGDn-NaZVzAQ-1 Received: from AcuMS.Aculab.com (10.202.163.6) by AcuMS.aculab.com (10.202.163.6) with Microsoft SMTP Server (TLS) id 15.0.1497.48; Sun, 28 Jul 2024 15:17:36 +0100 Received: from AcuMS.Aculab.com ([::1]) by AcuMS.aculab.com ([::1]) with mapi id 15.00.1497.048; Sun, 28 Jul 2024 15:17:36 +0100 From: David Laight To: "'linux-kernel@vger.kernel.org'" CC: 'Linus Torvalds' , 'Jens Axboe' , "'Matthew Wilcox (Oracle)'" , 'Christoph Hellwig' , 'Andrew Morton' , 'Andy Shevchenko' , 'Dan Carpenter' , 'Arnd Bergmann' , "'Jason@zx2c4.com'" , "'pedro.falcato@gmail.com'" , 'Mateusz Guzik' , "'linux-mm@kvack.org'" , 'Lorenzo Stoakes' Subject: [PATCH v2 1/8] minmax: Put all the clamp() definitions together Thread-Topic: [PATCH v2 1/8] minmax: Put all the clamp() definitions together Thread-Index: Adrg+PsJWBOjJVDsQ/+qaYGHKHGutQ== Date: Sun, 28 Jul 2024 14:17:35 +0000 Message-ID: <5cd3e11780df40b0b771da5548966ebd@AcuMS.aculab.com> References: <402c3c617c29465c898b1af55e3c6095@AcuMS.aculab.com> In-Reply-To: <402c3c617c29465c898b1af55e3c6095@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 --- v2: - No change. 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 a7ef65f78933..cea63a8ac80f 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 00:48:15 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 20823224DC for ; Sun, 28 Jul 2024 14:19:37 +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=1722176379; cv=none; b=Q+GxHd/mxz1Qi1U7+JOft1RJ8/5ortGbx04wEzMADkNlr+FOgo4Oyf6jRrMZOIkH/WxHTGh/2jOrHaikHbmbQFu6twVUREYkLPa3b74jZfScT52UsVHip1ATojc3DANp9REP5RFQlKfCWcfO96QZhUCCl43NOceERoFnTVuF30s= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1722176379; c=relaxed/simple; bh=MuvoUgdtghS6rQkUL1LMIng5Fy8TT6pQDoAeXgc4g8Y=; h=From:To:CC:Subject:Date:Message-ID:References:In-Reply-To: MIME-Version:Content-Type; b=UK6Q3Xf5XzuhP9e9SlXuwlQ+VmBKl7JEpPIXr2txyc0FVnuz2c2TIUwGzebqTXK+HO6GedpehUhu1AlWjb/lRVF22aXba5Gh/7VQ8vQeIp4ye6taG9ZSqhjFhLPnI3b9dDqDTfH9ay3CUKil4VhcO6/jmVrdtPRIWRyno8ElS2k= 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-44-u0K9Dgy5NXe-98MhLF4xEg-1; Sun, 28 Jul 2024 15:19:32 +0100 X-MC-Unique: u0K9Dgy5NXe-98MhLF4xEg-1 Received: from AcuMS.Aculab.com (10.202.163.6) by AcuMS.aculab.com (10.202.163.6) with Microsoft SMTP Server (TLS) id 15.0.1497.48; Sun, 28 Jul 2024 15:18:47 +0100 Received: from AcuMS.Aculab.com ([::1]) by AcuMS.aculab.com ([::1]) with mapi id 15.00.1497.048; Sun, 28 Jul 2024 15:18:47 +0100 From: David Laight To: "'linux-kernel@vger.kernel.org'" CC: 'Linus Torvalds' , 'Jens Axboe' , "'Matthew Wilcox (Oracle)'" , 'Christoph Hellwig' , 'Andrew Morton' , 'Andy Shevchenko' , 'Dan Carpenter' , 'Arnd Bergmann' , "'Jason@zx2c4.com'" , "'pedro.falcato@gmail.com'" , 'Mateusz Guzik' , "'linux-mm@kvack.org'" , 'Lorenzo Stoakes' Subject: [PATCH v2 2/8] minmax: Use _Static_assert() instead of static_assert() Thread-Topic: [PATCH v2 2/8] minmax: Use _Static_assert() instead of static_assert() Thread-Index: Adrg+SQVeb05XypLRkeZ+5Y+23noEA== Date: Sun, 28 Jul 2024 14:18:47 +0000 Message-ID: References: <402c3c617c29465c898b1af55e3c6095@AcuMS.aculab.com> In-Reply-To: <402c3c617c29465c898b1af55e3c6095@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 --- v2: - No change. 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 cea63a8ac80f..ab64b2e73ae5 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 00:48:15 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 0E5EA224DC for ; Sun, 28 Jul 2024 14:20:41 +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=1722176443; cv=none; b=hRLV1RSg5t/SlV20MyKO/d3CWw5gy48/3XY4OqLb7A0G1Cbav46gMIEH6IJXJxEZQ8K5zi58AZQ15P2xGbGBqTHvUcdDVtNnEzyHzx5zSl0ulinst+UK65L7DanEzY5QMA+WgM4km49N8b7/OxcXUGcYnTkV1qTnWNXwUxa5aOI= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1722176443; c=relaxed/simple; bh=DCcbZwPczLti18O5aV/kiKBtWPntq5y66aaO5tNPqbE=; h=From:To:CC:Subject:Date:Message-ID:References:In-Reply-To: MIME-Version:Content-Type; b=mnoJLNzOg/fHl/txfBBRyyGAP8s7yr3y/h0XM9YPkXV83qv4Y/fzWggKWg8wd6qTa3g0xUk7ruiOG6biBa3W4eTSia2xIixEK8MY9hHFB+jxzPv/+U8jgtObc3+3YZ3ji2ODVZn968cP6UYtl2sEGVd0Pj6Gv2e7vs0F/A86s8U= 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-176-ASX4DscfNW6QCi1PP5Mc6g-1; Sun, 28 Jul 2024 15:20:36 +0100 X-MC-Unique: ASX4DscfNW6QCi1PP5Mc6g-1 Received: from AcuMS.Aculab.com (10.202.163.6) by AcuMS.aculab.com (10.202.163.6) with Microsoft SMTP Server (TLS) id 15.0.1497.48; Sun, 28 Jul 2024 15:19:52 +0100 Received: from AcuMS.Aculab.com ([::1]) by AcuMS.aculab.com ([::1]) with mapi id 15.00.1497.048; Sun, 28 Jul 2024 15:19:52 +0100 From: David Laight To: "'linux-kernel@vger.kernel.org'" CC: 'Linus Torvalds' , 'Jens Axboe' , "'Matthew Wilcox (Oracle)'" , 'Christoph Hellwig' , 'Andrew Morton' , 'Andy Shevchenko' , 'Dan Carpenter' , 'Arnd Bergmann' , "'Jason@zx2c4.com'" , "'pedro.falcato@gmail.com'" , 'Mateusz Guzik' , "'linux-mm@kvack.org'" , 'Lorenzo Stoakes' Subject: [PATCH v2 3/8] compiler.h: Add __if_constexpr(expr, if_const, if_not_const) Thread-Topic: [PATCH v2 3/8] compiler.h: Add __if_constexpr(expr, if_const, if_not_const) Thread-Index: Adrg+UWL9B3xHw3xQ5CeKplMo1TYvg== Date: Sun, 28 Jul 2024 14:19:52 +0000 Message-ID: <2e12aefe29884e578283129411e1df26@AcuMS.aculab.com> References: <402c3c617c29465c898b1af55e3c6095@AcuMS.aculab.com> In-Reply-To: <402c3c617c29465c898b1af55e3c6095@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. __is_constexpr(expr) is equivalent to __if_constexpr(expr, 1, 0). Signed-off-by: David Laight --- v2: - Don't change __is_constexpr() include/linux/compiler.h | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/include/linux/compiler.h b/include/linux/compiler.h index 2594553bb30b..35d5b2fa4786 100644 --- a/include/linux/compiler.h +++ b/include/linux/compiler.h @@ -242,6 +242,23 @@ 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 +/** + * __if_constexpr - Check whether an expression is an 'integer + * constant expression' + * @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. + * + * 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)) + /* * This returns a constant expression while determining if an argument is * a constant expression, most importantly without evaluating the argument. --=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 00:48:15 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 41F53224DC for ; Sun, 28 Jul 2024 14:21:29 +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=1722176492; cv=none; b=rsICHphIKTO5VwhXiqF97v4UZI8gzjiwsUzNVObDtOuCUa1iBDh+o8dcf/4NL3mWzEttbnHTzo0eGsjrIa1CQce/NMFLyAa+P+ENJ22MREA/Xob/bhe+ZJXur+760WbYiQWw0E1gwt6wYUsmUqFFntTy97FswtZQHfUKr8f7FNs= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1722176492; c=relaxed/simple; bh=ykQS/edmALW5ljsOYoJ0rfRiirxSWs7UOZic33Q3p7A=; h=From:To:CC:Subject:Date:Message-ID:References:In-Reply-To: MIME-Version:Content-Type; b=P3/AlvDnD13RQNwZGSw/vbBxn5zyE2QpnB4z5thkImo2h3fQjiLMze8vHxIYjhJcsp5j0fCiR8KW5M0woT+2SLYbxkjPtkWM3dXPcMfr2X7ygUuj6iIAk31Np8aMCqnALfQRcGtV4TCKFapkrk5/LRZfk1KGhy0ilpJDW2ag700= 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-240-gyu3qikDN8SBJ-lv0Ijmhg-1; Sun, 28 Jul 2024 15:21:25 +0100 X-MC-Unique: gyu3qikDN8SBJ-lv0Ijmhg-1 Received: from AcuMS.Aculab.com (10.202.163.6) by AcuMS.aculab.com (10.202.163.6) with Microsoft SMTP Server (TLS) id 15.0.1497.48; Sun, 28 Jul 2024 15:20:41 +0100 Received: from AcuMS.Aculab.com ([::1]) by AcuMS.aculab.com ([::1]) with mapi id 15.00.1497.048; Sun, 28 Jul 2024 15:20:41 +0100 From: David Laight To: "'linux-kernel@vger.kernel.org'" CC: 'Linus Torvalds' , 'Jens Axboe' , "'Matthew Wilcox (Oracle)'" , 'Christoph Hellwig' , 'Andrew Morton' , 'Andy Shevchenko' , 'Dan Carpenter' , 'Arnd Bergmann' , "'Jason@zx2c4.com'" , "'pedro.falcato@gmail.com'" , 'Mateusz Guzik' , "'linux-mm@kvack.org'" , 'Lorenzo Stoakes' Subject: [PATCH v2 4/8] minmax: Simplify signedness check Thread-Topic: [PATCH v2 4/8] minmax: Simplify signedness check Thread-Index: Adrg+WoBXlsaZZWKRzqVZpRDUvrsDQ== Date: Sun, 28 Jul 2024 14:20:41 +0000 Message-ID: <74e0b027a908461da879b69b0e12c0de@AcuMS.aculab.com> References: <402c3c617c29465c898b1af55e3c6095@AcuMS.aculab.com> In-Reply-To: <402c3c617c29465c898b1af55e3c6095@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 --- Changes for v2: - Wrap is_signed_type() to avoid issues with pointer types because (foo *)1 isn't a compile time constant. - Remove the '+ 0' from __is_ok_unsigned(). This converted 'bool' to 'int' to avoid a compiler warning and is no longer needed because of the implicit conversion dome by ?:. include/linux/minmax.h | 25 +++++++++++++------------ 1 file changed, 13 insertions(+), 12 deletions(-) diff --git a/include/linux/minmax.h b/include/linux/minmax.h index ab64b2e73ae5..b9b5348a3879 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,20 @@ #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) +#define __is_signed(x) \ + __if_constexpr((typeof(x))1, is_signed_type(typeof(x)), 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) +/* Allow unsigned compares against non-negative signed constants. */ +#define __is_ok_unsigned(x) \ + ((!__is_signed((x)) ? 0 : __if_constexpr(x, x, -1)) >=3D 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)) +/* Check for signed after promoting unsigned char/short to int */ +#define __is_ok_signed(x) __is_signed((x) + 0) + +/* 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 00:48:15 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 8E1C939FFE for ; Sun, 28 Jul 2024 14:22:22 +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=1722176544; cv=none; b=KNdYSQ9dxWc7LVM+JYy3zUk9LPmN7jQ4wIQZrcqItTLxO9Z2n+tpymVufsZ81/IumCYCMemfW1jLk57MoLjeWtjJFLIAP2HXPX0VyERRDVcClUo7Dcwhw11A7gnRiaqVAXvtuEeI9K0C5jOjQ/6wf2bnVcgozDQBpDMP9K306ns= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1722176544; c=relaxed/simple; bh=dTucdDYiXIZVZl/15Og0dB3SqMzYWkLnvO7HIcNkJR4=; h=From:To:CC:Subject:Date:Message-ID:References:In-Reply-To: MIME-Version:Content-Type; b=boHBpc2A+muOP7NnmngCpxJGLzIhDv/cztqb2Tzab2+4bMH2rG7vZ/X5qUjkaezshN3U0CceHdYs2I4BYj6gHlXagR+PtvvNLo/1ZEewN7J15YMY0166rXy+aMTuvCLT2EzagU0LKPVpEJWjjpu9ewRtypSSV0+GWY5L1MYWWEU= 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-55-06CRokqUOX-KjYRbAhL1jw-1; Sun, 28 Jul 2024 15:22:11 +0100 X-MC-Unique: 06CRokqUOX-KjYRbAhL1jw-1 Received: from AcuMS.Aculab.com (10.202.163.6) by AcuMS.aculab.com (10.202.163.6) with Microsoft SMTP Server (TLS) id 15.0.1497.48; Sun, 28 Jul 2024 15:21:27 +0100 Received: from AcuMS.Aculab.com ([::1]) by AcuMS.aculab.com ([::1]) with mapi id 15.00.1497.048; Sun, 28 Jul 2024 15:21:27 +0100 From: David Laight To: "'linux-kernel@vger.kernel.org'" CC: 'Linus Torvalds' , 'Jens Axboe' , "'Matthew Wilcox (Oracle)'" , 'Christoph Hellwig' , 'Andrew Morton' , 'Andy Shevchenko' , 'Dan Carpenter' , 'Arnd Bergmann' , "'Jason@zx2c4.com'" , "'pedro.falcato@gmail.com'" , 'Mateusz Guzik' , "'linux-mm@kvack.org'" , 'Lorenzo Stoakes' Subject: [PATCH v2 5/8] minmax: Factor out the zero-extension logic from umin/umax. Thread-Topic: [PATCH v2 5/8] minmax: Factor out the zero-extension logic from umin/umax. Thread-Index: Adrg+YXyDMjC+R5zRZ+wfV++JCCsqA== Date: Sun, 28 Jul 2024 14:21:27 +0000 Message-ID: <3e11fa68f33b4fb5beec1fc3f41b123e@AcuMS.aculab.com> References: <402c3c617c29465c898b1af55e3c6095@AcuMS.aculab.com> In-Reply-To: <402c3c617c29465c898b1af55e3c6095@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 --- v2 - no change include/linux/minmax.h | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/include/linux/minmax.h b/include/linux/minmax.h index b9b5348a3879..a0c948ad576d 100644 --- a/include/linux/minmax.h +++ b/include/linux/minmax.h @@ -72,22 +72,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 00:48:15 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 AAAF339FFE for ; Sun, 28 Jul 2024 14:23:07 +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=1722176589; cv=none; b=qbiCB2DJUWNfcPXL7VC2kZL1K9/BGF9ZeKVOrTrDh5tgyndPVrwN4KWMA14OctL4sJRNPQd4JTygG4+7gqI+kHWLmpTN1jDRiPQNn4Y2QNbq49njjYhmHBTLVO4ZUL0h1Wmun3+7tJvFwiW2w5xR/kV06IlASfmolWmgeVKtdts= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1722176589; c=relaxed/simple; bh=0tAgnZnNa7Oxzz94ggec/aR6GTvZMg8hnsCjUYlG5YQ=; h=From:To:CC:Subject:Date:Message-ID:References:In-Reply-To: MIME-Version:Content-Type; b=d5mO9JqIbNhq3vwgzj/INAMhjq34+6YqFRhyJUVboRcj2TS9zvv3kWDRHuLn3BA367NUC1ixdN7vnl7k2h1khog1r2cwUo4MB0o5LRjPe6tbgFRl45nPCS3D60fEWzDxMzeSH/xd4ql1OBPYsK+dn3Ho5+XthrThdKY5siBreXk= 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-69-SvVKtw3EPaGCg6lHdtAGIQ-1; Sun, 28 Jul 2024 15:23:03 +0100 X-MC-Unique: SvVKtw3EPaGCg6lHdtAGIQ-1 Received: from AcuMS.Aculab.com (10.202.163.6) by AcuMS.aculab.com (10.202.163.6) with Microsoft SMTP Server (TLS) id 15.0.1497.48; Sun, 28 Jul 2024 15:22:19 +0100 Received: from AcuMS.Aculab.com ([::1]) by AcuMS.aculab.com ([::1]) with mapi id 15.00.1497.048; Sun, 28 Jul 2024 15:22:19 +0100 From: David Laight To: "'linux-kernel@vger.kernel.org'" CC: 'Linus Torvalds' , 'Jens Axboe' , "'Matthew Wilcox (Oracle)'" , 'Christoph Hellwig' , 'Andrew Morton' , 'Andy Shevchenko' , 'Dan Carpenter' , 'Arnd Bergmann' , "'Jason@zx2c4.com'" , "'pedro.falcato@gmail.com'" , 'Mateusz Guzik' , "'linux-mm@kvack.org'" , 'Lorenzo Stoakes' Subject: [PATCH v2 6/8] minmax: Optimise _Static_assert() check in clamp() Thread-Topic: [PATCH v2 6/8] minmax: Optimise _Static_assert() check in clamp() Thread-Index: Adrg+aI2g2sJGNTJQ4aVVAXDWlMAag== Date: Sun, 28 Jul 2024 14:22:19 +0000 Message-ID: References: <402c3c617c29465c898b1af55e3c6095@AcuMS.aculab.com> In-Reply-To: <402c3c617c29465c898b1af55e3c6095@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 --- v2 - no change The misaligned \ is fixed in a later path. 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 a0c948ad576d..ad57c8eddc8a 100644 --- a/include/linux/minmax.h +++ b/include/linux/minmax.h @@ -142,8 +142,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 00:48:15 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 0E2544174C for ; Sun, 28 Jul 2024 14:24:44 +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=1722176686; cv=none; b=LXiWffumo5wIEm7gA+vDJtteW/449Qar1HhBRy2dIuDy9So0fmH0CWJw7eViX5Fhp3xeZFokDm3t6yPhxg3elomcYjjz52Mkea+zuVQfP67C7mjH6rtZvXwyw/JptE6wS7xz+LwjFQmHekdVm4Hn3cIDDx3bA5QsOF4D6a7T+mY= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1722176686; c=relaxed/simple; bh=lLxFQwdnAuRPbrJit6Ocd1M+MA5j9LSLm1WrWa6k+SA=; h=From:To:CC:Subject:Date:Message-ID:References:In-Reply-To: MIME-Version:Content-Type; b=dCwkLLHEBZZcLveTUe11lLKqie1Y2l69/dfMi66O8JIO/xRLyO8ryb93RSMx5goaz1fP9MWNbe5dUX60uFeQLnjgFcczg3JyySwdDaqOTaXR14CImJOmyyp+73Mnfz6O0VlKcJUKfCO0r/Pc5k/qQqC5RyuM+jKSOMisSHBBSjs= 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-277-nlUHsBjeMUSsZCF2tSpzXw-1; Sun, 28 Jul 2024 15:24:37 +0100 X-MC-Unique: nlUHsBjeMUSsZCF2tSpzXw-1 Received: from AcuMS.Aculab.com (10.202.163.6) by AcuMS.aculab.com (10.202.163.6) with Microsoft SMTP Server (TLS) id 15.0.1497.48; Sun, 28 Jul 2024 15:23:52 +0100 Received: from AcuMS.Aculab.com ([::1]) by AcuMS.aculab.com ([::1]) with mapi id 15.00.1497.048; Sun, 28 Jul 2024 15:23:52 +0100 From: David Laight To: "'linux-kernel@vger.kernel.org'" CC: 'Linus Torvalds' , 'Jens Axboe' , "'Matthew Wilcox (Oracle)'" , 'Christoph Hellwig' , 'Andrew Morton' , 'Andy Shevchenko' , 'Dan Carpenter' , 'Arnd Bergmann' , "'Jason@zx2c4.com'" , "'pedro.falcato@gmail.com'" , 'Mateusz Guzik' , "'linux-mm@kvack.org'" , 'Lorenzo Stoakes' Subject: [PATCH v2 7/8] minmax: Use __auto_type Thread-Topic: [PATCH v2 7/8] minmax: Use __auto_type Thread-Index: Adrg+b/Mt8JjzpFvR/SgdqY3jGnUug== Date: Sun, 28 Jul 2024 14:23:52 +0000 Message-ID: <431f7c45e7294d4da4f8abcd57ce7b5e@AcuMS.aculab.com> References: <402c3c617c29465c898b1af55e3c6095@AcuMS.aculab.com> In-Reply-To: <402c3c617c29465c898b1af55e3c6095@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" Replacing 'typeof(x) _x =3D (x)' with '__auto_type _x =3D (x)' removes one expansion of 'x'. Signed-off-by: David Laight --- New patch for v2 include/linux/minmax.h | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/include/linux/minmax.h b/include/linux/minmax.h index ad57c8eddc8a..cb3515824a64 100644 --- a/include/linux/minmax.h +++ b/include/linux/minmax.h @@ -47,9 +47,9 @@ #define __cmp(op, x, y) ((x) __cmp_op_##op (y) ? (x) : (y)) =20 #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), \ + __auto_type unique_x =3D (x); \ + __auto_type unique_y =3D (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 @@ -131,18 +131,18 @@ * @y: value2 */ #define min_not_zero(x, y) ({ \ - typeof(x) __x =3D (x); \ - typeof(y) __y =3D (y); \ + __auto_type __x =3D (x); \ + __auto_type __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))) =20 #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(__if_constexpr((lo) <=3D (hi), (lo) <=3D (hi), true), \ + __auto_type unique_val =3D (val); \ + __auto_type unique_lo =3D (lo); \ + __auto_type unique_hi =3D (hi); \ + _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 00:48:15 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 D952341A84 for ; Sun, 28 Jul 2024 14:25:37 +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=1722176739; cv=none; b=Cxvc7nhfal3KnLtgyI5KJ5LaopnxEa/OMdzK9Vzsn9GiqgCDxLhbE4LO7nb3p3a7qtRYrlZJ1Vc+YJi2hALY3BakNZ/bGuBN7cGuZSxPdeC7rZUYC9VUaAfupxx39x8UIy2RJ3XoWnfCR0ISdVNPQfySGsHqP+en4as7Nd5IcXM= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1722176739; c=relaxed/simple; bh=1dbgvmdaF1nAa6KOYKC34cBRcp6an9l5zjBHwas93PM=; h=From:To:CC:Subject:Date:Message-ID:References:In-Reply-To: MIME-Version:Content-Type; b=i1YziI28lZQMqXmEZAbDSWlNrknG9e46qGmfNwySkKeuyjY1qnAypjSFB1lBjJ0Mk+bsAVJ6PNcfjurNGLjnePohDAxJuHxrCjy016rxa0a5Fecex5kfBGVgKm1tD0C0RrrBMFSnphQn2xLPOnognk2RQIea4H0K+TGDTwdtIOI= 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-225-Ran_uoH8NAunlrVN1XlTGw-1; Sun, 28 Jul 2024 15:25:31 +0100 X-MC-Unique: Ran_uoH8NAunlrVN1XlTGw-1 Received: from AcuMS.Aculab.com (10.202.163.6) by AcuMS.aculab.com (10.202.163.6) with Microsoft SMTP Server (TLS) id 15.0.1497.48; Sun, 28 Jul 2024 15:24:46 +0100 Received: from AcuMS.Aculab.com ([::1]) by AcuMS.aculab.com ([::1]) with mapi id 15.00.1497.048; Sun, 28 Jul 2024 15:24:46 +0100 From: David Laight To: "'linux-kernel@vger.kernel.org'" CC: 'Linus Torvalds' , 'Jens Axboe' , "'Matthew Wilcox (Oracle)'" , 'Christoph Hellwig' , 'Andrew Morton' , 'Andy Shevchenko' , 'Dan Carpenter' , 'Arnd Bergmann' , "'Jason@zx2c4.com'" , "'pedro.falcato@gmail.com'" , 'Mateusz Guzik' , "'linux-mm@kvack.org'" , 'Lorenzo Stoakes' Subject: [PATCH v2 8/8] minmax: minmax: Add __types_ok3() and optimise defines with 3 arguments Thread-Topic: [PATCH v2 8/8] minmax: minmax: Add __types_ok3() and optimise defines with 3 arguments Thread-Index: Adrg+fzAGOYJcfIuRPCxFJC4Oy7+aw== Date: Sun, 28 Jul 2024 14:24:46 +0000 Message-ID: <75f18af7162c4414be28a890e9504c8d@AcuMS.aculab.com> References: <402c3c617c29465c898b1af55e3c6095@AcuMS.aculab.com> In-Reply-To: <402c3c617c29465c898b1af55e3c6095@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, 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 --- 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))) =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 @@ -93,13 +98,25 @@ */ #define umax(x, y) __careful_cmp(max, __zero_extend(x), __zero_extend(y)) =20 +#define __cmp_once3(op, x, y, z, uniq) ({ \ + __auto_type __x_##uniq =3D (x); \ + __auto_type __y_##uniq =3D (y); \ + __auto_type __z_##uniq =3D (z); \ + __auto_type __xy_##uniq =3D __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__) =20 /** * 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__) =20 /** * min_t - return minimum of two values, using the specified type @@ -144,8 +161,7 @@ __auto_type unique_hi =3D (hi); \ _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)