Message ID | c6924533f157497b836bff24073934a6@AcuMS.aculab.com |
---|---|
State | New |
Headers |
Return-Path: <linux-kernel+bounces-80217-ouuuleilei=gmail.com@vger.kernel.org> Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7300:a81b:b0:108:e6aa:91d0 with SMTP id bq27csp1657977dyb; Sun, 25 Feb 2024 08:54:06 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCV+TYOZ0UUxSdyAQ3qxpKgwRxusquZwbR92G3tKsNrHeoM5OUtvVUZsJGqmEQ5APsuzXuwysY425I8oW0eMm42UbsyX5w== X-Google-Smtp-Source: AGHT+IHOJAGb/8pK4YK677rj3CZLT8VxUmvX0zej5tqNpFcgxClHHCWtIdGfdxAEbjlEAqJurCyw X-Received: by 2002:a50:ed98:0:b0:565:dce3:66f6 with SMTP id h24-20020a50ed98000000b00565dce366f6mr936985edr.39.1708880046400; Sun, 25 Feb 2024 08:54:06 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1708880046; cv=pass; d=google.com; s=arc-20160816; b=Gyx4wn8WL/VxqnlliH+OeiOmd87YY0uHe3dsMQkFTVCKz7/acmRiW6xxg/XMJc6ND2 7zFzdDWDNAq7kXPk8kWA29IV2CeMdjFfnIDr9DTer3QEio+96dKt57uT03wI3J7xDL9n yVWAh07t+O2f2/bu4X9LXfWrQHqjVN4Ol3BFEozwmOf9zRpqJObo1BKWHQe9ZRrm9RZk qW9RFg9T8pfjePpyYYgbchEEKtYpcuMtWbCOZQN43uf1uu4oVZpOIanuEIO8R2E2BosK UcFfd0FhvfuUcJRayUIfUT/DhA0prxpbsWEVEa5mWOlEQanJVY+2V2QICjge0s0shtVI YQVg== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:content-language:mime-version :list-unsubscribe:list-subscribe:list-id:precedence:accept-language :in-reply-to:references:message-id:date:thread-index:thread-topic :subject:cc:to:from; bh=JVmH/+xaN9Z7r45x71mh1tq1KbNTZpCh/GRiBQtXED0=; fh=nGE692sbVdRvha6RYbzMUXtm1nr/30sV7frdtDN2z14=; b=DXbSE+GlFQmTN0XXIyvnvwfLl1Q0jJjyVSzJmKBMynHzP3FoIacRhZOagZ2hEyv505 oFx8SwPTSRQ+ugPOEubf8U94iU7LahXRtSugf+LY1aJ17HEptKiCg/0uqzVF9iXGdj0s j/rqqLw6GtLQ4FDM7wU3QukB4XxmzTVEfaYaOFciMAeVPrkphgJpQkN1shs5XC3zcfZT bYG0JEVsHX07zttXEz5ZScYUjfdO7pZAg27FBTTajwbLDFlnmm4L2hBZP8kQD9keZC4C pDB3e8PYp6wukXXaN59NvQGXwotKJS43ReXIUFohTsvHrsHIxTSYWN4puLDUx/oITI8w gDRg==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; arc=pass (i=1 spf=pass spfdomain=aculab.com dmarc=pass fromdomain=aculab.com); spf=pass (google.com: domain of linux-kernel+bounces-80217-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) smtp.mailfrom="linux-kernel+bounces-80217-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=aculab.com Received: from am.mirrors.kernel.org (am.mirrors.kernel.org. [2604:1380:4601:e00::3]) by mx.google.com with ESMTPS id l13-20020a056402254d00b00565af2ed18esi1151965edb.80.2024.02.25.08.54.06 for <ouuuleilei@gmail.com> (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 25 Feb 2024 08:54:06 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-80217-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) client-ip=2604:1380:4601:e00::3; Authentication-Results: mx.google.com; arc=pass (i=1 spf=pass spfdomain=aculab.com dmarc=pass fromdomain=aculab.com); spf=pass (google.com: domain of linux-kernel+bounces-80217-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) smtp.mailfrom="linux-kernel+bounces-80217-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=aculab.com Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by am.mirrors.kernel.org (Postfix) with ESMTPS id 0A6F81F21CFD for <ouuuleilei@gmail.com>; Sun, 25 Feb 2024 16:54:06 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 43CD618EB3; Sun, 25 Feb 2024 16:53:48 +0000 (UTC) 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 1771017BDC for <linux-kernel@vger.kernel.org>; Sun, 25 Feb 2024 16:53:44 +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=1708880026; cv=none; b=EdTmzlKCJBVw8JnohLD2NCRG/a9TgwUiKE3D4kil3qQU2GCq6SRgp9XlcgOCIvrN+59HiegFd/QeTyip+KqWp87OvuLT/la50d9I9xlPomRCS/6dEUJRamgHCvQFWdsB7Q1STU58S5lXuTtYN4Up3WVW6s4ZVP1XPaSgV19c4hY= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1708880026; c=relaxed/simple; bh=yzk8hJGS5EAJboFzprtLjoWXpcFk7Mo9VpW2lpjOHHE=; h=From:To:CC:Subject:Date:Message-ID:References:In-Reply-To: MIME-Version:Content-Type; b=tbhcj8o6+VU53Xnupdx8JdVDr2+BDlpbNQ0DaxZoTmuCnM9PL7edSV7H+7DCC/OWri6ORCh1htHEoGvuIwFhhslbVKVMns0BWCEETh5zYFPN1xIRQaMkj9/Q5YSJm+uR9IJHnMg6QIgZr0uYZaONmPdRFnMZ9w5dLeCE0t7+kNU= 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-66-E8tAd1F8OPi8n5OgxZplNA-1; Sun, 25 Feb 2024 16:53:41 +0000 X-MC-Unique: E8tAd1F8OPi8n5OgxZplNA-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, 25 Feb 2024 16:53:41 +0000 Received: from AcuMS.Aculab.com ([::1]) by AcuMS.aculab.com ([::1]) with mapi id 15.00.1497.048; Sun, 25 Feb 2024 16:53:41 +0000 From: David Laight <David.Laight@ACULAB.COM> To: "'linux-kernel@vger.kernel.org'" <linux-kernel@vger.kernel.org>, "'Linus Torvalds'" <torvalds@linux-foundation.org>, 'Netdev' <netdev@vger.kernel.org>, "'dri-devel@lists.freedesktop.org'" <dri-devel@lists.freedesktop.org> CC: 'Jens Axboe' <axboe@kernel.dk>, "'Matthew Wilcox (Oracle)'" <willy@infradead.org>, 'Christoph Hellwig' <hch@infradead.org>, "'linux-btrfs@vger.kernel.org'" <linux-btrfs@vger.kernel.org>, "'Andrew Morton'" <akpm@linux-foundation.org>, 'Andy Shevchenko' <andriy.shevchenko@linux.intel.com>, "'David S . Miller'" <davem@davemloft.net>, 'Dan Carpenter' <dan.carpenter@linaro.org>, "'Jani Nikula'" <jani.nikula@linux.intel.com> Subject: [PATCH next v2 08/11] minmax: Add min_const() and max_const() Thread-Topic: [PATCH next v2 08/11] minmax: Add min_const() and max_const() Thread-Index: AdpoCy246SYrYUdtTu+AtQRSWe90RA== Date: Sun, 25 Feb 2024 16:53:40 +0000 Message-ID: <c6924533f157497b836bff24073934a6@AcuMS.aculab.com> References: <0fff52305e584036a777f440b5f474da@AcuMS.aculab.com> In-Reply-To: <0fff52305e584036a777f440b5f474da@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: <linux-kernel.vger.kernel.org> List-Subscribe: <mailto:linux-kernel+subscribe@vger.kernel.org> List-Unsubscribe: <mailto:linux-kernel+unsubscribe@vger.kernel.org> MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: aculab.com Content-Language: en-US Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1791890603421629752 X-GMAIL-MSGID: 1791890603421629752 |
Series |
minmax: Optimise to reduce .i line length
|
|
Commit Message
David Laight
Feb. 25, 2024, 4:53 p.m. UTC
The expansions of min() and max() contain statement expressions so are
not valid for static intialisers.
min_const() and max_const() are expressions so can be used for static
initialisers.
The arguments are checked for being constant and for negative signed
values being converted to large unsigned values.
Using these to size on-stack arrays lets min/max be simplified.
Zero is added before the compare to convert enum values to integers
avoinding the need for casts when enums have been used for constants.
Signed-off-by: David Laight <david.laight@aculab.com>
---
include/linux/minmax.h | 15 +++++++++++++++
1 file changed, 15 insertions(+)
Changes for v2:
- Typographical and spelling corrections to the commit messages.
Patches unchanged.
Comments
On Sun, 25 Feb 2024 at 08:53, David Laight <David.Laight@aculab.com> wrote: > > The expansions of min() and max() contain statement expressions so are > not valid for static intialisers. > min_const() and max_const() are expressions so can be used for static > initialisers. I hate the name. Naming shouldn't be about an implementation detail, particularly not an esoteric one like the "C constant expression" rule. That can be useful for some internal helper functions or macros, but not for something that random people are supposed to USE. Telling some random developer that inside an array size declaration or a static initializer you need to use "max_const()" because it needs to syntactically be a constant expression, and our regular "max()" function isn't that, is just *horrid*. No, please just use the traditional C model of just using ALL CAPS for macro names that don't act like a function. Yes, yes, that may end up requiring getting rid of some current users of #define MIN(a,b) ((a)<(b) ? (a):(b)) but dammit, we don't actually have _that_ many of them, and why should we have random drivers doing that anyway? Linus
From: Linus Torvalds > Sent: 25 February 2024 17:14 > > On Sun, 25 Feb 2024 at 08:53, David Laight <David.Laight@aculab.com> wrote: > > > > The expansions of min() and max() contain statement expressions so are > > not valid for static intialisers. > > min_const() and max_const() are expressions so can be used for static > > initialisers. > > I hate the name. Picking name is always hard... > Naming shouldn't be about an implementation detail, particularly not > an esoteric one like the "C constant expression" rule. That can be > useful for some internal helper functions or macros, but not for > something that random people are supposed to USE. > > Telling some random developer that inside an array size declaration or > a static initializer you need to use "max_const()" because it needs to > syntactically be a constant expression, and our regular "max()" > function isn't that, is just *horrid*. > > No, please just use the traditional C model of just using ALL CAPS for > macro names that don't act like a function. > > Yes, yes, that may end up requiring getting rid of some current users of > > #define MIN(a,b) ((a)<(b) ? (a):(b)) > > but dammit, we don't actually have _that_ many of them, and why should > we have random drivers doing that anyway? I'll have a look at what is there. It might take a three part patch though. Unless you apply it as a tree-wide patch? One option is to add as max_const(), then change any existing MAX() to be max() or max_const() and then finally rename to MAX()? David - Registered Address Lakeside, Bramley Road, Mount Farm, Milton Keynes, MK1 1PT, UK Registration No: 1397386 (Wales)
... > Yes, yes, that may end up requiring getting rid of some current users of > > #define MIN(a,b) ((a)<(b) ? (a):(b)) > > but dammit, we don't actually have _that_ many of them, and why should > we have random drivers doing that anyway? They look like they could be changed to min(). It is even likely the header gets pulled in somewhere. I'm not sure about the ones in drivers/gpu/drm/amd/display/*..*/*.c, but it wouldn't surprise me if that code doesn't use any standard kernel headers. Isn't that also the code that manages to pass 42 integer parameters to functions? David - Registered Address Lakeside, Bramley Road, Mount Farm, Milton Keynes, MK1 1PT, UK Registration No: 1397386 (Wales)
On Mon, 26 Feb 2024 at 07:26, David Laight <David.Laight@aculab.com> wrote: > > ... > > Yes, yes, that may end up requiring getting rid of some current users of > > > > #define MIN(a,b) ((a)<(b) ? (a):(b)) > > > > but dammit, we don't actually have _that_ many of them, and why should > > we have random drivers doing that anyway? > > They look like they could be changed to min(). > It is even likely the header gets pulled in somewhere. > > I'm not sure about the ones in drivers/gpu/drm/amd/display/*..*/*.c, but it > wouldn't surprise me if that code doesn't use any standard kernel headers. > Isn't that also the code that manages to pass 42 integer parameters > to functions? They are all separate in C files, I think we could get rid of them pretty easily in favour of the standard ones, Adding Harry to cc. Dave.
diff --git a/include/linux/minmax.h b/include/linux/minmax.h index 278a390b8a4c..c08916588425 100644 --- a/include/linux/minmax.h +++ b/include/linux/minmax.h @@ -60,19 +60,34 @@ #op "(" #x ", " #y ") signedness error, fix types or consider u" #op "() before " #op "_t()"); \ __cmp_once(op, x, y, uniq); })) +#define __careful_cmp_const(op, x, y) \ + (BUILD_BUG_ON_ZERO(!__is_constexpr((x) - (y))) + \ + BUILD_BUG_ON_ZERO(!__types_ok(x, y)) + \ + __cmp(op, (x) + 0, (y) + 0)) + /** * min - return minimum of two values of the same or compatible types * @x: first value * @y: second value + * + * If @x and @y are constants the return value is constant, but not 'constant + * enough' for things like static initialisers. + * min_const(@x, @y) is a constant expression for constant inputs. */ #define min(x, y) __careful_cmp(min, x, y, __COUNTER__) +#define min_const(x, y) __careful_cmp_const(min, x, y) /** * max - return maximum of two values of the same or compatible types * @x: first value * @y: second value + * + * If @x and @y are constants the return value is constant, but not 'constant + * enough' for things like static initialisers. + * max_const(@x, @y) is a constant expression for constant inputs. */ #define max(x, y) __careful_cmp(max, x, y, __COUNTER__) +#define max_const(x, y) __careful_cmp_const(max, x, y) /** * umin - return minimum of two non-negative values