From patchwork Wed Jul 24 14:28:14 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: David Laight X-Patchwork-Id: 13741012 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 90DABC3DA63 for ; Wed, 24 Jul 2024 14:29:01 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id F25FF6B0082; Wed, 24 Jul 2024 10:29:00 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id EAEBE6B0085; Wed, 24 Jul 2024 10:29:00 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id D4FCC6B0088; Wed, 24 Jul 2024 10:29:00 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0011.hostedemail.com [216.40.44.11]) by kanga.kvack.org (Postfix) with ESMTP id AC9F26B0082 for ; Wed, 24 Jul 2024 10:29:00 -0400 (EDT) Received: from smtpin29.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay03.hostedemail.com (Postfix) with ESMTP id 5DD19A0970 for ; Wed, 24 Jul 2024 14:29:00 +0000 (UTC) X-FDA: 82374878040.29.713F214 Received: from eu-smtp-delivery-151.mimecast.com (eu-smtp-delivery-151.mimecast.com [185.58.85.151]) by imf18.hostedemail.com (Postfix) with ESMTP id 180881C0030 for ; Wed, 24 Jul 2024 14:28:57 +0000 (UTC) Authentication-Results: imf18.hostedemail.com; dkim=none; spf=pass (imf18.hostedemail.com: domain of david.laight@aculab.com designates 185.58.85.151 as permitted sender) smtp.mailfrom=david.laight@aculab.com; dmarc=pass (policy=none) header.from=aculab.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1721831274; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=m3WkPOx6tQxNXMeRli1rAKWLGKZTCm3EXd2zn1Oz0R0=; b=Ljsnp9Z0sJIu16ZVF/Tn+V3kit7ueKvykTEdhnKuAAyc1cfMV+vJj7D9w/jnKYGnGQCA4J hOEd6WcmGHefSU//hUmROrcPt/LQYABjcO7Fgo3Nq3KU82jq7AkhxNK0wUjaiN2tIgONe5 YAqostODqqItstK5IErWl4BKUYuMIa8= ARC-Authentication-Results: i=1; imf18.hostedemail.com; dkim=none; spf=pass (imf18.hostedemail.com: domain of david.laight@aculab.com designates 185.58.85.151 as permitted sender) smtp.mailfrom=david.laight@aculab.com; dmarc=pass (policy=none) header.from=aculab.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1721831274; a=rsa-sha256; cv=none; b=N8ja6b65QI9+ncmuJgs+Mv3Fh9SqTdH5SNg6gofQDHcUE7yMIs5Ze8UXQuy/Pend68GGS1 Wj93zJ8+TRndbNBPOv42IXbWNmOxYmMjDS6XCXPX5nI5bq6SndkMZmSLjYCyke5GEFQq1l 1CtPmFqkB1QkBzP5/csrogw5Yn4Si6Q= 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 x-originating-ip: [10.202.205.107] MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: aculab.com Content-Language: en-US X-Rspamd-Server: rspam06 X-Rspamd-Queue-Id: 180881C0030 X-Stat-Signature: rakrrjhfoz67daznftr8z4kihzsp1fne X-Rspam-User: X-HE-Tag: 1721831337-287204 X-HE-Meta: U2FsdGVkX19bYnL6Z5bnE43d2P7EUDWHe/VE+itFkviz6QV4LDlEeZsMI1z3Cr8CqnE+pGDpSDmLO2r04wFHdaKhs8W+4Xe2F6wKzOku2GVA5UcvpM9TSwf6HqzrEQqcvfK3D9JZjR9VUCT/ZhGed8EQ9ct+zmMeauu5+f69WSP7iyBzF9L7pWDiSmpj7NeMCivD1FuriFk0xJvGqdOyyNtmYFaqxcri+MY7B2LnT9hz98AhJjiKTI020x4xJYuA0BxTCuP613cD8JLJo8OSpv3GbmFAEif6xRP4pXul+lceuJ5/F2R77xH2SewaG7YG74nU96A8zzRmRlRzqgn0zXGP6eKxOTHZbp+oZSA3Jbrl7FbLGf9ceNpuu+x4GTj3VoNpAmgKbsoCYpXsG10JNPrQfG2Zb5dV/99tJfHvMM2wpJ4/XPMznJ/W06o14Yc2WyCWsMxA/iHViip7IqSPpSO2ujYiUvKFr6d3AprmFwno7BJX6bgxMvc6niiACxOP4AteEQkvh/t1NE3egjDugAxfPGVdXi3owhAWEsMV8b3xoqZ33u/YHfg/asuAvudHslxUZBMBPc5lV/HGkca9L7o8FyUkz3U/F0abYIpz8CQrwLv8DYtH0mV7bABjB5+8XH0BnY9uQIoig5J+0wERlkb2Zp0fuhQf0Qil8Nr2AV/IH9PzXu8K3AVZLOotoGnO+Uw6FtMMWgv1a8Z002lo+1h/1TSdKF/jSRxP2yuP6cTaPKJaiPEFwEd7dHs6WF2oeU1Ktk2AL3oDoWpiuXO/IZOXawUz3VGfSs5TWLNvKw7hANuejTd++sHH7nTfvQh5ihFJgQUCnQMGpj1/QGHFib7QHEo63nTr/QxGJL0WJeckvYSOvsWfJwGumGtku35yotZSJPRjJ1qJPyyyW0IyQWRUjseoptuloVIwBzrGAudYiXFQ4WpNwzl1x4yFLT8of3V/W9CL7lkDGlC0yPj Zh+hGNJ+ ckR80fkusFzhjRVHCm8enBzxLFE0DJ9X72uVbaUDBFHVlI+rzaF8Q2W7gotfP5WvniJIq2ZHD0Q5dvp7SAa+JsCvuycXDBCt5EadQ6N72t7FcMO3qb/jSV+dt4pLMe7kbqY8qzeDsyn+jBJibDIsTBkoqvKe/cvhdPGIsJNspxmWK6n3c2RMleOaGxeqLoLHzzhAvDMvqII46uPaNTUJu/DO1aRq1f8nHRrhbH5jDqEKcDY/emXB5sOC/TyZCPtDj8mhHVTYpLiSy8rmvfxwgznZcr+22xexQHJVtmxkNpMKX+cCqEV+cifju+icdJMto0wXvGdxIVmMKtpu5JHOhSYl1mZKp4gl6xJQO+amZY4fx0b2Mmr9RxByiLhpOuPfN/+y/35zO685BJnrxnFhgm3G5Ow== X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: The defines for clamp() have got separated, move togther for readability. Update description of signedness check. Signed-off-by: David Laight --- 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))) -#define __clamp(val, lo, hi) \ - ((val) >= (hi) ? (hi) : ((val) <= (lo) ? (lo) : (val))) - -#define __clamp_once(val, lo, hi, unique_val, unique_lo, unique_hi) ({ \ - typeof(val) unique_val = (val); \ - typeof(lo) unique_lo = (lo); \ - typeof(hi) unique_hi = (hi); \ - static_assert(__builtin_choose_expr(__is_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"); \ - __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) +/** + * 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 zero * @x: value1 @@ -134,39 +130,60 @@ typeof(y) __y = (y); \ __x == 0 ? __y : ((__y == 0) ? __x : min(__x, __y)); }) +#define __clamp(val, lo, hi) \ + ((val) >= (hi) ? (hi) : ((val) <= (lo) ? (lo) : (val))) + +#define __clamp_once(val, lo, hi, unique_val, unique_lo, unique_hi) ({ \ + typeof(val) unique_val = (val); \ + typeof(lo) unique_lo = (lo); \ + typeof(hi) unique_hi = (hi); \ + static_assert(__builtin_choose_expr(__is_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"); \ + __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) -/* - * ..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)) /** - * 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 unsigned + * type and @lo and @hi are literals that will otherwise be assigned a signed + * 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) /* * Do not check the array parameter using __must_be_array(). @@ -211,31 +228,6 @@ */ #define max_array(array, len) __minmax_array(max, array, len) -/** - * 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 unsigned - * type and @lo and @hi are literals that will otherwise be assigned a signed - * 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; From patchwork Wed Jul 24 14:29:02 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: David Laight X-Patchwork-Id: 13741013 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 8952CC3DA61 for ; Wed, 24 Jul 2024 14:29:48 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 059BA6B0088; Wed, 24 Jul 2024 10:29:48 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 0094A6B0089; Wed, 24 Jul 2024 10:29:47 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id DEC9F6B008A; Wed, 24 Jul 2024 10:29:47 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0012.hostedemail.com [216.40.44.12]) by kanga.kvack.org (Postfix) with ESMTP id BFDBC6B0088 for ; Wed, 24 Jul 2024 10:29:47 -0400 (EDT) Received: from smtpin01.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay07.hostedemail.com (Postfix) with ESMTP id 7005A1609FB for ; Wed, 24 Jul 2024 14:29:47 +0000 (UTC) X-FDA: 82374880014.01.3F430BC Received: from eu-smtp-delivery-151.mimecast.com (eu-smtp-delivery-151.mimecast.com [185.58.86.151]) by imf13.hostedemail.com (Postfix) with ESMTP id 314B620011 for ; Wed, 24 Jul 2024 14:29:44 +0000 (UTC) Authentication-Results: imf13.hostedemail.com; dkim=none; spf=pass (imf13.hostedemail.com: domain of david.laight@aculab.com designates 185.58.86.151 as permitted sender) smtp.mailfrom=david.laight@aculab.com; dmarc=pass (policy=none) header.from=aculab.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1721831361; a=rsa-sha256; cv=none; b=irQS1AjP+fMxlCXpWt1mLNSV6sRgHrBH/JrmQ27g/m/Ew5sOPsAl0MCoZUvGFnmw5eOml7 jsfS2Wuf+vOaUT4SXWkJ64jAJxvDzdQ/JbJaUzf8pzV9AiyhMTfg+rZboIfFEblrY24MGP h/qYeUpZui9pXs/CBZhUGzBop+xMH9c= ARC-Authentication-Results: i=1; imf13.hostedemail.com; dkim=none; spf=pass (imf13.hostedemail.com: domain of david.laight@aculab.com designates 185.58.86.151 as permitted sender) smtp.mailfrom=david.laight@aculab.com; dmarc=pass (policy=none) header.from=aculab.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1721831361; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=EhqLrJgqYxdlTb2Zl2yShcG1KzJBygJskqHd8w4qHqo=; b=WuLBAe1z4hOw+As8cgkqSivEkPQQ+jemh0oGaQD1597dF5oSA1tGfj6+7aQLpldRBjOP1P tbJ3qL4ALy905myWTSxsotWBwQXWUvcfPLTwlBKTO7Q6nGcfmBKDWXdHlkdfsVcX1vnb/A SXncVM29n8p+eYbCGt6XL6vj+8CEq6k= 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 x-originating-ip: [10.202.205.107] MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: aculab.com Content-Language: en-US X-Stat-Signature: z4hsxe48wbqtq35ibt8r5i8abgkecrqj X-Rspamd-Queue-Id: 314B620011 X-Rspam-User: X-Rspamd-Server: rspam10 X-HE-Tag: 1721831384-286681 X-HE-Meta: U2FsdGVkX18nKYP6h0NkCUfd004D+r+YsYDB/HQC6D5wJsilDJ7wx3JxVqvQQNddU8l5PggWS4fSEjnfrJ5TajideFG1AZj5cd5D0gAAEr8RTPh+rfBkxLfq9IZxfEThs7j9Tc8sk6X5F2b4AH2h3WJJ6yPL1fc13ASr9X/SV3+nL7ciudT8HTlQcFGDLkM1Ia2S1/0FggWi3/71Hbc3LcVu+BbAF+PrjJreTe8T9imsUV0wVseZJFMJ20Rb8mR/2Egg7P6Ygn8WV3XIsF/biqlHgzi6jgVF1z+6IpVJhnySxs2VWc8Qcee51oq+sX2Jo9ptsjFV3SD0zWM+TgYgCULAEJfhnH9Mm4rU81VPFTcuU2eu5UPUCUBrFT8Kr5zsIaLNGNMYlw8Z0EZS4ktJH0oq9QwMvhZX1MpfogcCQZ8EbavGQ/rq1aosW9rMaq3K7S0Ef1+0VfoBpRy3//MOrQcXoLZxWomEJufOIpNs+EY2yMDMTBfJOd3MGIhA60qdBjiN5GWF0j0ANm1YTq/LscL26QaghBtXGOalEpxXbgs6/2FzGri09+PslBWgzs7garG2HYQSuuA7tqsQIIalWn+JNm8jyVJoHnpl1gWSwAS0nA5b2Ysvfnv+JCSk4/lGd6ka0WoVPEJfrcdXhLS4YZHeDEf9hFP/mBSj9rRGi5168L5ayQxvj3Tfw9UtZ1KiOHaAtJrpCIBxGggg3Z+kpFNavnUaK+a76cNnx/hXiaDU+MEgHEsNM7Ej2uLc/nxGRHkvzkF28ESMM7EVf7obDcF7HynQzNnLihjB4ilDkZwjgMrg9RrhCEWwZ0Ay3uMqm/tGEf3+XJWShPtsxMC8rFbS4FR09yOk0i2dttLBemJPU1RU927g868v66BliWEOgBxiE7WQn0jWUHw40dtB4gI8PdVBvw+S+BA7UcdXBPXQbWgKMJ/g+5KuIYZ1DF01PKDLJUYJ6itnpYDoQzt aBOoLloq Vq0lCDNqUG4Ei+ZfMKFc1XY5AWwyLDfxjnXA+tFHn9oVBbEz3Hb6nejtnPxIMfsb3g1ZaoX6QLDAcFK76jgpenKZAtNNWWfDE8GyRQh3UDULltCj743MWHhiLuE81LIsAnagQtuYAkGD3Ms7cT/nlvpTMF8EjX7ANIlVHSlvujYCZLKQv4gIiWD4+/x9nuiM4UJfMPtjeUjvwwaz33obI9xQY4GVXCnqFGbRRpPiHAvyUvMdLxKd/zKdP8DzR/P6cggZZj3+QhMm6Ubr4Q62bYI2kzSjnPCMro+JkIBqA/GCDShV2BvSbUi81/ugTJQj84OOIm/bQfB6yqeBdtrTn9eQBoPaDgBrJ7v15t8PfFxVKlJLWrdNfsNQ/EGGQ7csrksGmNZfDoktPoCkqBICCWN2Qjdp3ZkmNr0No5jXh+/0XdkJd+bJhYZ7rQqXpxnz8FJ5Y X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: 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 --- 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 = (x); \ typeof(y) unique_y = (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); }) @@ -137,11 +137,11 @@ typeof(val) unique_val = (val); \ typeof(lo) unique_lo = (lo); \ typeof(hi) unique_hi = (hi); \ - static_assert(__builtin_choose_expr(__is_constexpr((lo) > (hi)), \ + _Static_assert(__builtin_choose_expr(__is_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_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) ({ \ From patchwork Wed Jul 24 14:29:52 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: David Laight X-Patchwork-Id: 13741014 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 44BECC3DA61 for ; Wed, 24 Jul 2024 14:30:40 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id C67806B008A; Wed, 24 Jul 2024 10:30:39 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id C17CA6B008C; Wed, 24 Jul 2024 10:30:39 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id ADF906B0092; Wed, 24 Jul 2024 10:30:39 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0013.hostedemail.com [216.40.44.13]) by kanga.kvack.org (Postfix) with ESMTP id 8F54F6B008A for ; Wed, 24 Jul 2024 10:30:39 -0400 (EDT) Received: from smtpin14.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay03.hostedemail.com (Postfix) with ESMTP id 2A9FFA0A1E for ; Wed, 24 Jul 2024 14:30:39 +0000 (UTC) X-FDA: 82374882198.14.0396D09 Received: from eu-smtp-delivery-151.mimecast.com (eu-smtp-delivery-151.mimecast.com [185.58.85.151]) by imf16.hostedemail.com (Postfix) with ESMTP id BAD81180002 for ; Wed, 24 Jul 2024 14:30:35 +0000 (UTC) Authentication-Results: imf16.hostedemail.com; dkim=none; dmarc=pass (policy=none) header.from=aculab.com; spf=pass (imf16.hostedemail.com: domain of david.laight@aculab.com designates 185.58.85.151 as permitted sender) smtp.mailfrom=david.laight@aculab.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1721831400; a=rsa-sha256; cv=none; b=sGV1VDUUOZID+8APsn4rTEV8seqIU13wVig4wofPbw107XbHWB9KEs+V7ZeSYimBKrv38q IGUgXJjfJZlX77zXz3uAv6PKthZDzZckhJq5pDgW3tuI2gmLPLM5umAF0HEBg72iUObDLI 1DVxAADO+eoK+r33NtgEWgZuGe65HiM= ARC-Authentication-Results: i=1; imf16.hostedemail.com; dkim=none; dmarc=pass (policy=none) header.from=aculab.com; spf=pass (imf16.hostedemail.com: domain of david.laight@aculab.com designates 185.58.85.151 as permitted sender) smtp.mailfrom=david.laight@aculab.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1721831400; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=ar7j+3KCIrodjDu2pdRMw+d11CVEJAnE4zkCQL7OOdg=; b=5oGFg9I4kl1noqJ0eXJ264EVjAJ2GTCQJhy+Zf2iaVHheC+65JEKLJQqslH8jkbDpcgqDS UvR1imgqiSgUHw3wifz1YVLJNrgruJwTPdpGjaOvcbZHo5uX/wbw5Gmka267EnsvaL/Zty GGYz0/tTzK2P4XoYHUXMcSDUPZAORVo= 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 x-originating-ip: [10.202.205.107] MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: aculab.com Content-Language: en-US X-Rspamd-Queue-Id: BAD81180002 X-Rspam-User: X-Rspamd-Server: rspam05 X-Stat-Signature: tzr7k7h8cik4gjbpoofozcr95e4s9nii X-HE-Tag: 1721831435-585945 X-HE-Meta: U2FsdGVkX1/Vj94AOxDqaUffV/0XruxVEl/24GJfye5oj2NrSK5tmlTb8EeNPSDIBbZFmgs3GRHEGZRCY3cgEJrT9RREOGRCbUfQ0OQiN8KVarrv815yv+MKP8MtNZzFYh97b9nyf37uFUpXYS6OxZSt9SjCZy2igSFlxBgwRecI9ExOAAid5t9ofLjhilKWg1CRg2InKXo9iooUKFi3Bk1+Wt733lhyguxjiKSCcv21mlr0FzU0f+zoTBDU5ljuuytuo+4QSsxPKLQXPrQcFyiYMYmGRSw/sqJmRoKITOf9Bb7EFFFjgDoihhqbgFXLH+ayhZhAo79rGKtILUC8jeWf9KwEpUnIQF+xFdPMUocsQi/zKcJKxVj9sjJQJkHjCq3UOSeg3D7H9lADZWWbO9rqkp4tEb0+hXaS/0/FAZQuM/3KMS3U9PwUyo/yfiOAmkJgG8x0sRmWu4wx/HNv9fbuEKFVrS2ImZEOkfRr2eq30OaQWSgotXV5Hjb6txTM/U4S/iHktYOKQsBeYqxTgBPuDjcb0VzVWnT2DUogOxvKXsexknhUTBW5naelHgUAF5JFN5D3hod90gpBSXSGOl0Zw4c59WsJRSNBN/Ac0Plb3WC3AULFc370ywZRjgMzRk2kHMeRs8MeyJov3JdgqLLJ0bFOm4KgTmgf3Sxr3mageaDgj4Hs5Oi/dvquvjmdP46wlyGYnGnl6KfhMgboRDTlQZjG5ztDGc5tYaA38XSy+O2HZNVmKxd3a+0+2dAz5gn+RYRKMzu5Gx+HsUiOgBVOkXfprokThI9BBsK18sM0BDUD2iIm1V1ay5ODcQblpKpUaBoWR64Kz/WtfCkc1Vgv5Rn0tqhhDZzEuEdU/uLxU2bKq7K5bqpGXcr3qY7qPOaPS4y26mBqq3Np2s7I7Y9iLjZudSl27/JRZyeuK7rSuRMfzcYzuC0V7dQO4AyLcP1WgIB7BexzYzmDSXZ BSB8op6H 82YTEQ10mvdV22dPoLBhj5QT2rXnhpbScZjSHaYJ60yslyOCYekF6hj7i/4uT/EeCZjOXuDVjSq5/cKjBeKJUQoXuxxZsg7yICfVu8anXchYYvnfsuEWADvyIFoM6nlQe7HpgqlcDJNj1L0OJB50Z0fjhyfA9GN5It1wphTlJio6fvetqxXtusr2pJkvielyMOR94hOaxVWtCKwhyoITs0zu/cz+Z6pbvzHIgkiMVwartgcxyRQtt988QoPAtS2BgrjZwP8Ry84x8PEIicgPivK/OkCQCLVAcFgxR6RCuelOwwEIP3J0XgV1hK968UtioUY5Pk4lE+2Pp08Jjdns4TTgCMwcRNqut0DtwdSflTZ7rYXQrlaOflUWeLeRym5bV94tE2MCsbzsq6G62y9IpNxQHBCmenPfFdaNJjNJNMmbgX0PSME2FXj8MGPHyT8zjMLYU X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: __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 --- 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])) -/* - * 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' + * @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 integer - * 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 *)(...)) == sizeof(int) == 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 *)(...)) == sizeof(void) == 1 - * - The equality comparison to "sizeof(int)" therefore depends on (x): - * sizeof(int) == sizeof(int) (x) was a constant expression - * sizeof(int) != 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) == sizeof(*(8 ? ((void *)((long)(x) * 0l)) : (int *)8))) +#define __is_constexpr(expr) __if_constexpr((expr), 1, 0) /* * Whether 'type' is a signed type or an unsigned type. Supports scalar types, From patchwork Wed Jul 24 14:30:52 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: David Laight X-Patchwork-Id: 13741015 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 4A9F5C3DA63 for ; Wed, 24 Jul 2024 14:31:39 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id DEF1A6B0089; Wed, 24 Jul 2024 10:31:38 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id D9F3A6B008A; Wed, 24 Jul 2024 10:31:38 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id C67106B008C; Wed, 24 Jul 2024 10:31:38 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0014.hostedemail.com [216.40.44.14]) by kanga.kvack.org (Postfix) with ESMTP id A6B886B0089 for ; Wed, 24 Jul 2024 10:31:38 -0400 (EDT) Received: from smtpin01.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay10.hostedemail.com (Postfix) with ESMTP id 15302C0A4E for ; Wed, 24 Jul 2024 14:31:38 +0000 (UTC) X-FDA: 82374884676.01.23C44C0 Received: from eu-smtp-delivery-151.mimecast.com (eu-smtp-delivery-151.mimecast.com [185.58.86.151]) by imf22.hostedemail.com (Postfix) with ESMTP id B95F4C002A for ; Wed, 24 Jul 2024 14:31:35 +0000 (UTC) Authentication-Results: imf22.hostedemail.com; dkim=none; dmarc=pass (policy=none) header.from=aculab.com; spf=pass (imf22.hostedemail.com: domain of david.laight@aculab.com designates 185.58.86.151 as permitted sender) smtp.mailfrom=david.laight@aculab.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1721831460; a=rsa-sha256; cv=none; b=JCynUWCrmOILKzHQ6i0r2UgzQIStEbIviDlVjYu2JznKNHk/Kj0bUFqQKPkaXbHDnj19CI 5N8re1j/89DIG7m76mRphvOdhp1yhCGWEU9vO4s4l2Zf15cuW0Wg84FmdYgXj2wUEmbs6b Hy3XdEq/BXjFKs63oLN93vybiLqecXQ= ARC-Authentication-Results: i=1; imf22.hostedemail.com; dkim=none; dmarc=pass (policy=none) header.from=aculab.com; spf=pass (imf22.hostedemail.com: domain of david.laight@aculab.com designates 185.58.86.151 as permitted sender) smtp.mailfrom=david.laight@aculab.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1721831460; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=0i4IP2NajFZH0aPZhVutvsRIxZ3rsE/RmZYLnb8vip0=; b=Pa9iV6hfTAzfKGRXNIxGpGnb+MJRb9mMMHVn658vFvk8XQEOHfVg62UIJpKOwICKpe5s+h 9hk9NCdCsHIoJrfWKtSxh3atwvwwb5SRaxJxVYpB8PrzhuuP4X80CDptYx48gOfZ7Kky7/ RCtAAIfrk1fbx5JDogb1OpjBfGZofTI= 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 x-originating-ip: [10.202.205.107] MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: aculab.com Content-Language: en-US X-Rspamd-Queue-Id: B95F4C002A X-Rspam-User: X-Rspamd-Server: rspam05 X-Stat-Signature: 4d6wpohf66sf5tmzyq985mwbdkyqrtwx X-HE-Tag: 1721831495-363206 X-HE-Meta: U2FsdGVkX1/Z86cE7kxsv7MS7mVa2Z74poHJn/fE/SUMuSVjpyeFyglE15TGblGwWFUz3xacTwv3os2cBM81T8W+p+VYMXbqZBe8mQfbJCb/mpbl0V7BV3fW5NHc9Zg5yoBDV+GLaveD4ScuVzvy+uKYV7gaJAV+irarCSKyZ6FK/TxJxdxqATgbqnMZKjhQxBlzit20W1gvYOfX0aRupEQ7eZGnT2zxx2pcSLEhVoRZNjwPSnWu/S2ySnskn/YoDiK0ddONmvGDhshAXI3R0NIL3iP/ZaD89a8j8uSu7ZRiy17C0P9vFm8Ps5tlW/DT+gDy9CNdBtiKDtlKaNi+8uG+O8ShicoUCZA3JVdaGpv4stmfcyprQCQqMRfFH2nQeh6puXkzjEUQbOLw/amBr02JcqIHjH/WFY1BJRaOXbLGvIMoLbImu0A27B2ZqNKW9cGkxMjbW48ynJLP/op0VueJg0Mw8iAToKM1XvYk4sPzkj6ig9RC7CdLNtOkbhMdeIScH7zdbyrWvzEWUgfJPxlkjB9gITNAwUkBP5j9I/uJbTnX4wBxhA6NwszQZpH/BdRhMTi0KxZgYYHw20gNgVrP+PNJrjmFhSY0VsjUfwg0r6vL4igdYPmkrSIODjdkMa96CvtnU6Oop9kPm4sZHlRvCsH8aaso5J7v+TkfnQvLYlagYw/32hVvOocvGH/MVLBeKeRvXF6hagvSFMn/yrmyzS+FC0gqYX0v7RJXcmBE4g6KRkLyWnsFrP1JMoEirN4YYnq6ahbwLgWuAu68eFDKBgv/G8Vdyrz+g/aRRnF7bQ7xlNl0Q1cKcFGRCNgU6+xAHMGOdGJdZngarmtHhwz0l+HTCVq4wtmAV7cP2UIM0kLNO9I0mMKpWod4QiziC3XmWFaKtVcYQxM31EmxFj6zd/Gz/ST58w2wAsjZB6S2YU6hx/f5bFQsBV/Zg08Nqjj7pvwip4ZGeLbCaW0 1fd+OPD0 VFRBgWqyE+0h2lg35YhjRKaxGre9rGbqjhR0/wGek6auGi07h+S23+SIvsxN0b9bp5Jkc1yv3oJck8GkjIi9YyhA17fNtp0MepXKVUWt3gebJryZok/DNP5r6s5wXajvXm7E9vYNAWamyrddtLHc7SqfqQwTu3kQgRJmwF0iFGWYK3BKZLyz8edrYU+23rHL1BdNmR0DcfVRZG4zuFmdK0CpVNf1rlN9lAlE6jCFRtwxBMm+xv06M5fq8R2AjcfnPen2bnL4MDMltCPrUT2HDGtSd27WTjxAF7SM0FdEyAODpo7SaPaDnFbQG+N8CM6Rlu2Bw1M2wVaLv2TAMqCIssZJO6QTENlbOus0VjBcvR3NSRc8cOYdVuhevnqnkPlau7mT9qb23vrQk7d/ShGo57Qha3g== X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: 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 --- 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 /* - * 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 == (typeof(y) *)1))) -/* 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)) >= 0) -/* True for a non-negative signed int constant */ -#define __is_noneg_int(x) \ - (__builtin_choose_expr(__is_constexpr(x) && __is_signed(x), x, -1) >= 0) +/* Check for signed after promoting unsigned char/short to int */ +#define __is_ok_signed(x) is_signed_type(typeof((x) + 0)) -#define __types_ok(x, y) \ - (__is_signed(x) == __is_signed(y) || \ - __is_signed((x) + 0) == __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))) #define __cmp_op_min < #define __cmp_op_max > From patchwork Wed Jul 24 14:31:45 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: David Laight X-Patchwork-Id: 13741016 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 1EF26C3DA63 for ; Wed, 24 Jul 2024 14:32:32 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id A95236B007B; Wed, 24 Jul 2024 10:32:31 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id A44D76B008C; Wed, 24 Jul 2024 10:32:31 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 90CFD6B0092; Wed, 24 Jul 2024 10:32:31 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0013.hostedemail.com [216.40.44.13]) by kanga.kvack.org (Postfix) with ESMTP id 71BDA6B007B for ; Wed, 24 Jul 2024 10:32:31 -0400 (EDT) Received: from smtpin19.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay02.hostedemail.com (Postfix) with ESMTP id 1EC1D120A5A for ; Wed, 24 Jul 2024 14:32:31 +0000 (UTC) X-FDA: 82374886902.19.DF1E324 Received: from eu-smtp-delivery-151.mimecast.com (eu-smtp-delivery-151.mimecast.com [185.58.86.151]) by imf04.hostedemail.com (Postfix) with ESMTP id AD63A40026 for ; Wed, 24 Jul 2024 14:32:28 +0000 (UTC) Authentication-Results: imf04.hostedemail.com; dkim=none; spf=pass (imf04.hostedemail.com: domain of david.laight@aculab.com designates 185.58.86.151 as permitted sender) smtp.mailfrom=david.laight@aculab.com; dmarc=pass (policy=none) header.from=aculab.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1721831485; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=jOCelJGLnUetgcH5pzDT6pVXE8U8ugE/QL3ccQvCF2c=; b=RtL6gqOjs0KvCKyat24CNpSazumz+tovwqr0Du0yY32L8FxGCQ0v/FjQOERA2Fs4cpdBZF A6eLyWIFBXE5i7PiBblkYa2TU8ihr33NTv9ggwkudDsO+/rqqlmQvBNWbuVFflkE1WPOPv dnKTgGsgMLg9Cj9l71NhxboA96w7sM8= ARC-Authentication-Results: i=1; imf04.hostedemail.com; dkim=none; spf=pass (imf04.hostedemail.com: domain of david.laight@aculab.com designates 185.58.86.151 as permitted sender) smtp.mailfrom=david.laight@aculab.com; dmarc=pass (policy=none) header.from=aculab.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1721831485; a=rsa-sha256; cv=none; b=Ii35qi/i32uMMXN9/7MtzeoFGCKlvJAvlVWWJc/Mg6DqfBV7EcLYcwCQDZFvTwSRcX1hcR sIgg4q5Umm4/JEh1vEnMabH/Gi2lsZZXS7A5x9k//aWeLIHQWpqpAPguR+QKphOxtWn3fR n0MUUpc/7APZ+W2iKBCe/b4B0Oq2fxE= 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 x-originating-ip: [10.202.205.107] MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: aculab.com Content-Language: en-US X-Rspamd-Server: rspam06 X-Rspamd-Queue-Id: AD63A40026 X-Stat-Signature: 6watda3bfu8twitgob1qoxau3o4pk19z X-Rspam-User: X-HE-Tag: 1721831548-871878 X-HE-Meta: U2FsdGVkX19Po5LnsRDfg2Tu+W0ZCRv5pck+SeL+lWpapHXnCp+oWwC9AlnRR9Z8sYx8oG8U4FwnioeBaMW7tZ8HEeGlOU8XrOBjD185wXNk6b8/RwTbKF2XTPjHE7Xj16L5U5aq5xrPlfn06S92yD9l/JX5Se+fZ02jHMGh2h2t6hyFA4kC3BJz27L/TmFKeg8nAjg1qPdEEfeFnsMq6OW+bI0QCurHX2J2SEMveBmzTCBO8LkEXHGainAe9pxuQwQxxsqPVrk9f7/FXDDtt470AtQNhcRXVmobxtorMqS3t1wfnQw3yPTMaBh58p4mPyQ9iWRbyVekhqFPbSiXe7NCfdtKb3dWeMU/a8TlfmO/xfHIMELAN2emR+c4TFMSNata5O3FHscsHTh9DIlEGRr1V1it5b/cQub6F36ppnbfaYwXzaoXCPB6f0pOHQ0ALux3HrIr0kEoAWAEr5xUpPCLeSIWaHYpgyjaDMMmB3fb9ZtKKyXNLqR7+kOvqpMkZhyIn9L47h3jugIJXJXEc6NfkZFzWQ2w9QqYBDvdisJ1dzu22AURQt2Q3GLEsResESjSgP94yMogcU44rNLkP4IFfhsBfJ36oLdur825BFzVxVM7nk6+e5s+V8s57HtSaNB4xP8cMNgsNM8N0u3v8SEvmxcnDai/WewWkObSFTEi3NOR+mioiYlNUt1ItXIigVy3Ymiam3DYPnPZxY+aqoj8GLpxLbETHFckB0vyK6KfGLVQnjo92/AdVPsgzuu4YiHneq3YfK+SCmQHh5k/Tx+lmY7WcLqh120/GYyBUPQZlK5NBF9ClyNYrufswsk2KnQskLhkLaQGLrAjmh8Hq980fm5lQvqcGEgq/SxMMbtx8kobHoQMOP1sm+IEjp7U2oDs0RlkAfjgZUQoq3+hrhBvdiSO/9LlQC5hWPVSuoIFjLbI1sXYqfjG6Vtd7xaZqI4Ax4bzkV11/R44YmK d1Xa0TTZ xxopUJt02W3PvGlA6cROTWM3dP9ByNZtUmQGwePxvJwAhHInM/Q8X0kz9flG8qL6zRnf9NLY/YmeGG5LXd0qySjXbuhbV5DzgMc75jXtrN433l81cqIGAUcH6roVdjCGZE6+F6DSRwAtFMnvQvSsn/GLw8qwbtUZDHw6xJdAO0Unz+TiasI9NM0dNwTmIEk/kiCv6+OcdbLUtjfTSirDczkDa8Al6tl+Rra5stS6kriV3Rzy80VVpFg1IPns61sq6qOEbh+L/fjNmNn8zPDE2hNN/UW9hkhqHTnNk064RBmCrV9oIV5cvy9iFk7rtIfDayR16+Wq8Ng4L5iGJKPYr38xoINDhFMgHiW7DJNmSIoJG6fRL9J8F7DTaOOd0InGXRqNJKXkqi2Sv+SbyoJ2JNuKRyg== X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: 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 --- 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 @@ /* 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)) >= 0) + ((is_unsigned_type(typeof(x)) ? 0 : __if_constexpr(x, x, -1)) >= 0) /* 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) +/* + * 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)) /** * 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)) /** * min3 - return minimum of three values From patchwork Wed Jul 24 14:32:22 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: David Laight X-Patchwork-Id: 13741017 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 69772C3DA63 for ; Wed, 24 Jul 2024 14:33:11 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 083AE6B0093; Wed, 24 Jul 2024 10:33:11 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 033996B0095; Wed, 24 Jul 2024 10:33:10 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id E3D836B0096; Wed, 24 Jul 2024 10:33:10 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0011.hostedemail.com [216.40.44.11]) by kanga.kvack.org (Postfix) with ESMTP id C4CE16B0093 for ; Wed, 24 Jul 2024 10:33:10 -0400 (EDT) Received: from smtpin16.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay02.hostedemail.com (Postfix) with ESMTP id 7F87D120A3F for ; Wed, 24 Jul 2024 14:33:10 +0000 (UTC) X-FDA: 82374888540.16.55965AF Received: from eu-smtp-delivery-151.mimecast.com (eu-smtp-delivery-151.mimecast.com [185.58.85.151]) by imf09.hostedemail.com (Postfix) with ESMTP id 2D138140036 for ; Wed, 24 Jul 2024 14:33:07 +0000 (UTC) Authentication-Results: imf09.hostedemail.com; dkim=none; dmarc=pass (policy=none) header.from=aculab.com; spf=pass (imf09.hostedemail.com: domain of david.laight@aculab.com designates 185.58.85.151 as permitted sender) smtp.mailfrom=david.laight@aculab.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1721831553; a=rsa-sha256; cv=none; b=vpo1yLvqJYF3vC6GjLSC0eqo6yaay+dZ++7miEmzu//MPh89ILFwFsENvHfBmesourLHW/ BbZLxgsGWxu6bK5iur8H1F38KC5qpUSs40GpY9JxorFh/N7ZZlQv/buO4SQs0P05lekYQn 7nEuM8JWx4uZ7BZT34lV8ta7pZ8zDtg= ARC-Authentication-Results: i=1; imf09.hostedemail.com; dkim=none; dmarc=pass (policy=none) header.from=aculab.com; spf=pass (imf09.hostedemail.com: domain of david.laight@aculab.com designates 185.58.85.151 as permitted sender) smtp.mailfrom=david.laight@aculab.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1721831553; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=QDy57ZVQoNG35uF/IJ1jWNERroyPlmeJLF5V2Pdwlk8=; b=K/Vvpb6BeoAPU11knYE8lj8zyxS5Fez9m22wz3SPJ3oLd6kc3Oe0m3DmSCw4FRBu5F9gp1 tz8Id7xbSJGenUOdQXMT29a6qPkaAuNCl1OmgK6999mhP8S1/E5bSFkTqXRRd24Vxcpb6p hmWdnJma0KndeHMoWFfMv02awFVHyv0= 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 x-originating-ip: [10.202.205.107] MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: aculab.com Content-Language: en-US X-Rspamd-Queue-Id: 2D138140036 X-Rspam-User: X-Rspamd-Server: rspam05 X-Stat-Signature: 5nj6ruf3smj6aeex319buf79swsuyyym X-HE-Tag: 1721831587-698555 X-HE-Meta: U2FsdGVkX1/RMRRT9DCyhSgZe4d0kK26Y8MVFKy9QsZQFi7GCPk7e1AN3wIljxybuCBq7TRW6umeAQhx9S++K+wBx5zjmmqyYa54IgLLMwmOGlr+gBtdxKz/ubAtYbiPdIdaihSAD01SMNFb4LIvK7QQv3/g1QkXWzSpzHsWo9xSo0JWDVp2oxpqUTeRooha9EHsraRbWtYzD8ASGYIhA5lxuXvZzsmAN1fk3mw+5wbZvFidVQG+LvG1ba333XtmTwURQfACfPYh1FXeFaO8SNdS+HLmhur0QnaEtlG6AGAOIB02zdepdbFOPo/j+hc7pg11ryyQfTyDPiWlFRMhU6fWZQaFNUKXXvNO6yKIqK3k9IRPdaEkE+B/3HH1h0Jx5F9t29PDZYVwGDwdKte3uRqxTfkv9TGlyylhbosedg2iF+WqdhMHELmgfrrUJBSXHiESoPQ7MPgZ6rn7UOjBfJ7BIQvEyL/DFNx9cX+MnUC/R7fQ0i72wqVF8hRscTnEZbvNpWluH1x5sUb7g7L2y9aDA6U8lypkx9o69VktkDLqUw1xkaj7G+D1xffj/+agj9UyZ5MHLJ9Y0WSM2qgQyqMkEjzTYVpS4EKfjxXnpD0KNmfNlNyn3cYmIwtDZL6syYWtB4JifQpV6huXZJa2QQIubtBDUO2FvRyHujqw791wOka3bJJTkv0CZdJh8jmbEFyKyl4VIAQNLcaI3PbxjwjBwxNBokTl4ZsA2JOk4k2llS/fVuXOyqUt9cGyyD2/tCkylFBJcAKb5ErHjhN1zFDpMqbbJStJB49osSiKnJ9HUiKSO4nyFSbgWJa3Xj8AXx7fQLULrRsKU/6BZHZDOhAyfsYByt86HlpIeg6htymPFB8jGmZSZkMVNsgb713CmRH64WfdNyk4ANsKJhFsEVveMpU264xZVucI00kSIjAdq7TUbi0oTc3wm3XU4V80G3CLYE7mvHVJP5KjsOj LtG8uLFj KqTyigPxvjO93DIE1MUPttYla9Z6D8VbD5Lp/dcyhA3yTSSxc5Ao89AgpiZzt8N2ilJfjR7GCXJCG0G/fk6HsBXJmtYnawyEHHPSmZilXulSobJDQgTRU4NhI/EgOw97j+eVnIxrt673sZ2SpCCqVbQ00pe3MQrva5fL2cfAY2ws1O57ukWqXZGo8ae+m9IxU13o5zommlTfp0RhGbK6WEw4ylligcsUiGLxm5IPqk7niuR89ilMnoDGC9Qz2I+zBNwffAxfuB218CqC9HH0VECZDg5FC2y/XfsvB4MkaGkFwYLXD2dCUCgvYe8IGwYbA8qx6NNJIplAgcptOzxq9WVPiykWcjuHojSsIMecya9ahk91/8ihMyjfXem0Oxld9/5yGtzYEkX4efXsS0Bw2zeZ4+U3kdqu/XlVyqpuHRwje48ZTJPYoLI9C6ojfoc44ErDL X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: Use __if_constexpr() instead of __builtin_choose_expr(__is_constexpr()). Signed-off-by: David Laight --- 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 = (val); \ typeof(lo) unique_lo = (lo); \ typeof(hi) unique_hi = (hi); \ - _Static_assert(__builtin_choose_expr(__is_constexpr((lo) > (hi)), \ - (lo) <= (hi), true), \ + _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"); \ From patchwork Wed Jul 24 14:33:11 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: David Laight X-Patchwork-Id: 13741018 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 7D222C3DA63 for ; Wed, 24 Jul 2024 14:33:57 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 1961C6B007B; Wed, 24 Jul 2024 10:33:57 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 146796B0093; Wed, 24 Jul 2024 10:33:57 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id F281C6B0095; Wed, 24 Jul 2024 10:33:56 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0013.hostedemail.com [216.40.44.13]) by kanga.kvack.org (Postfix) with ESMTP id D396D6B007B for ; Wed, 24 Jul 2024 10:33:56 -0400 (EDT) Received: from smtpin24.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay02.hostedemail.com (Postfix) with ESMTP id 7CE47120A1A for ; Wed, 24 Jul 2024 14:33:56 +0000 (UTC) X-FDA: 82374890472.24.C77B64B Received: from eu-smtp-delivery-151.mimecast.com (eu-smtp-delivery-151.mimecast.com [185.58.85.151]) by imf01.hostedemail.com (Postfix) with ESMTP id 29E624003E for ; Wed, 24 Jul 2024 14:33:53 +0000 (UTC) Authentication-Results: imf01.hostedemail.com; dkim=none; dmarc=pass (policy=none) header.from=aculab.com; spf=pass (imf01.hostedemail.com: domain of david.laight@aculab.com designates 185.58.85.151 as permitted sender) smtp.mailfrom=david.laight@aculab.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1721831595; a=rsa-sha256; cv=none; b=mExjfPEP6y0BSSxymYaZfIiciOCQttM93HQ27qXnD0ElZl/8bzX9pBp7A8E03JJtNhXw6r gFOr9ufIhAZoLqllmt1+qQAkbxHCKG0FUjWjXZSbpLicMZJ5i0/o4A7s9Wt8nbS2e6r3Fv ExfTTg/dL3PPpE5z48umHZivcKVoIe8= ARC-Authentication-Results: i=1; imf01.hostedemail.com; dkim=none; dmarc=pass (policy=none) header.from=aculab.com; spf=pass (imf01.hostedemail.com: domain of david.laight@aculab.com designates 185.58.85.151 as permitted sender) smtp.mailfrom=david.laight@aculab.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1721831595; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=ia6SmT3DZLa6vhYN89Py8rxkJqhB7jo1tQHkEeKAQLU=; b=1nTC19OA/e6VKFEiC5P+mksMJcJUac0jkp8XqwKjjv73+HqKg5jBBTDv5TyGk1FeC57Dbv tYT/ynSqOavx+M8svuQBaFWyejixPQtACCEfX4q/zEeFKIP0GaLckpZMKHtAsoT4+bZSgr Y10HKaEcigGtIectolAEqoY/WbDeKL4= 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 x-originating-ip: [10.202.205.107] MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: aculab.com Content-Language: en-US X-Rspamd-Server: rspam12 X-Rspamd-Queue-Id: 29E624003E X-Stat-Signature: xpzim6xb9butaftkm3fpsybxmo3iz9sq X-Rspam-User: X-HE-Tag: 1721831633-485815 X-HE-Meta: U2FsdGVkX18/0hTjJa2b34HgrKUmfxKyKpVp3LbsNMg9CzXj2F4AMGWFhixuRo49spSmD/rpAn8bhm71aSkccCDNcbcHKWr3xbLzX7Ud6D1JglPdpJFj0S2L5COUdTr4f6CUT3w+yfMbqq258Qr2HvGoHJFpSvVuKLaEcmZynprIz8ZOPRSe7G1igk4vY2eanHjjBfn+ja0T+WWIkGWMT2o0terRmmTbB0EyYsQiRW9AjT7YgNK6oOHknH7/81kEknfbVry2eHfd7cJGc/idlIiU4pvPUEoYvk2/FVb7EOI3zl0uAfceBuypqxKvvztuOBr2Rg/hLN50jmHspSyFaXl3Kj8AznV6EcV/NCG1yYJWEJu0MdVY6CPKb83nd2LNLNM6H6O4DBp2uEWXoQfCzZkK+PWvZ0+YO6gAOpuObziIplDWBbxaQvJRnhNpSwHCuKJC0SWBcoE3oYM3xgps6OPCtvYyN6z3FEf+CgefBCHW8Jy3jOrwbH3CAjhws+Xlp7f/Q/sxzbnnWRUuA08o2QgZ7AL0xlV4lRHCh/WcVtuzX0zq33i3dheVGbsHxQ6Q7Ph5yQ98mDSA3wkFT5PIGVm/U8GkdihNRDbLcmFB/pCekIP9oUZC/09oNLNcaFNNcGdnJQBjRiPBP8vUSj6FCEPyrij0iZn9dJnlmTniLej1Ka774vjnKQoCN0DZpbGK6MMlNcciIItbjS8DGvk3g+dLTbAgyyxmUBRxlU09izFm4CUon1J8gv65xrf/Wd9/Ru+/Ji4KQfjLTkfkrMcdJk/LgE1PUA7t8xUBnUBgVmJU8iozSmVjlinFOk6/CA4EbdgnZT0wJdFVBpI+gR8gOBWnmzgbtb7evpjtYdBPNdXyOsSCijlEslpbD0yLWnsIgd8DlcmwL+lgccNlyhVbm4TGgn19yWe+O0CX/uCRvauGvKYOxgodzvTld3xYPWfXrua7+eoNyWl+ZGV1NAr 3EVq6KDE 5lJgjfSQbcRvjApMqTuYFrE3QtKCHqyiA9rFJbTLvP0ebKwcmykGpqY1zlzjsZ1gO3bl2pjt35cOX/swFkwlQ0uR5CLPd2ZvO7yBlZHiZLdfmHS7bWCB0PB2oz8ryhfl4U95MGFMons8c0LUq4wD2fCRqglujwcxTNFiJPFKUuo76U4TTOv5agU/7MsvwJM1zLYVRsbY/UO0mcTIFH7kdTrl5VYgYCXMIFAXvfqFeRLcVzq3hicQqXJMa4JWsnK6G/2+3w+jgP0ZbySSgRCZiY8YNNEzWEAH1yOji2mk9scbfLfHT/9uv9ZAd9avUtsvcAfJFQtsTbT36Nn1dcXeTCSiAYPKQbbo4oaCMHueFhMiOjbkMTapraG47Smc4JYNoD0oZWh9BKoK0Rm/wdjbwtCNXMg== X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: 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 --- 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))) +/* 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 > @@ -90,13 +95,24 @@ */ #define umax(x, y) __careful_cmp(max, __zero_extend(x), __zero_extend(y)) +#define __cmp_once3(op, x, y, z, uniq) ({ \ + typeof(x) __x_##uniq = (x); \ + typeof(x) __y_##uniq = (y); \ + typeof(x) __z_##uniq = (z); \ + __cmp(op, __cmp(op, __x_##uniq, __y_##uniq), __z_##uniq); }) + +#define __careful_cmp3(op, x, y, z, uniq) ({ \ + static_assert(__types_ok3(x, y, z), \ + #op "3(" #x ", " #y ", " #z ") signedness error"); \ + __cmp_once3(op, x, y, z, uniq); }) + /** * min3 - return minimum of three values * @x: first value * @y: second value * @z: third value */ -#define min3(x, y, z) min((typeof(x))min(x, y), z) +#define min3(x, y, z) __careful_cmp3(min, x, y, z, __COUNTER__) /** * max3 - return maximum of three values @@ -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__) /** * min_t - return minimum of two values, using the specified type @@ -139,10 +155,9 @@ typeof(val) unique_val = (val); \ typeof(lo) unique_lo = (lo); \ typeof(hi) unique_hi = (hi); \ - _Static_assert(__if_constexpr((lo) <= (hi), (lo) <= (hi), true), \ + _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) ({ \