Received: by 2002:a05:7412:d1aa:b0:fc:a2b0:25d7 with SMTP id ba42csp22410rdb; Sun, 28 Jan 2024 11:33:21 -0800 (PST) X-Google-Smtp-Source: AGHT+IFUhKDGbJQKZL390ss0sLwbk2iJ2X8r333+suD7dkeKZdoPDtpod2ZwWEqS+7NP6Ld3DGBX X-Received: by 2002:a05:6a20:7b1b:b0:19c:9f98:f514 with SMTP id s27-20020a056a207b1b00b0019c9f98f514mr2294253pzh.10.1706470400663; Sun, 28 Jan 2024 11:33:20 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1706470400; cv=pass; d=google.com; s=arc-20160816; b=FcNsPs06qQg/m2DrkVWPWWzqRTS53qXBRTsCkhREhpvbfgcheEblvvZKqnePxF2L+i hI2YIA/gCW0ZXOgNR2CekhKM8fMIhvVmRrBiypMIAlljuztfWDKNvGt7M74dfqZnECPk Vi6yShO8CQfPKUG09Y6nqD8Z0HukTQ9Rk8Aepf4lKd4CUKAezanmL+wnfaGmcwZzNs+L oUWqu1+wGRlE6HRxd93tCkLJQs+wbPKT7Q+kCWMn/FtqHdoWJhXpfDSU+7IlYjniD9vr i6EcYXwQ4iTQEvY4JCzP1KxL/eMDEGqT22gvbtPYYBJxySzdW5Dv8Cs/2D9SfxrOgXNK t3bg== 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=4irpdVricIiC9VuoboSpnCcJ4JJvncEhwH4b59pDyco=; fh=jAOu/RGlQxfhPk7PcGNzOfYLHKZO+STAaG1daOB/TAU=; b=MAgdHM7RSyWba0qD8XWB96rKwYEeWZkw8DIhVMGj1e7hff8YodbZq93N0uv4VKsz0y pxxkj2xV/G3Yaa1VikeZ0ENeN7CRZgd6bXzVjWbRsFEo3Tk/qfDa7kaCyWpec4zrWU+S XX4XezM9QTEx2sQXVzZUFX4SQOslFHrCKr6IGQyAhNd9Mac7Hlvb2GCV7BJHX3wdO3jn FmEnRy5lMjutYWNRI4tSpwR2XuQEqZQfZYFKMaYTTHXBYPYsQq4Ho75cuYkbEgDEgqJ0 G0wtSnPPwOiAn0hzuQN1SP1fdPWeQkHyiyh+OB4Cpj7HvMnlrUBLFV9Rff3Lck7UrMyV olJQ== 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-41905-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-41905-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=aculab.com Return-Path: Received: from sv.mirrors.kernel.org (sv.mirrors.kernel.org. [2604:1380:45e3:2400::1]) by mx.google.com with ESMTPS id w63-20020a638242000000b005c69765acddsi4463467pgd.87.2024.01.28.11.33.20 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 28 Jan 2024 11:33:20 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-41905-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) client-ip=2604:1380:45e3:2400::1; 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-41905-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-41905-linux.lists.archive=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 sv.mirrors.kernel.org (Postfix) with ESMTPS id 5732F281BF0 for ; Sun, 28 Jan 2024 19:33:20 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id F12AB3BB5E; Sun, 28 Jan 2024 19:33:05 +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 6955031A94 for ; Sun, 28 Jan 2024 19:33:03 +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=1706470385; cv=none; b=mgjVifkmyyFILlxyNKir7t9mtcAXqOVwtD/IA0TMlY5ktmnhiGi2A9MSgjtqhKqRhlP9UfQJLUsvnXtzwOXlGNH0UN+YnPJnW++QuLcZJ4zYBwws1ggnX9xyeud+c+nW8AhFYZP2fF5Gl1RRqT5pYsm2kCpZwga+GU79XjXl+eo= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1706470385; c=relaxed/simple; bh=Cvne2zLpLHEm/ZIJGi/lxJc8QnXVqT2f4MsKcA1INnA=; h=From:To:CC:Subject:Date:Message-ID:References:In-Reply-To: MIME-Version:Content-Type; b=HXTLO4LrM+zF6lypjcXLKNUCdYHZrQJzj3ih5rV+5xghzIbUpkcxAf24/RqVaWkJV57AFLl8WDmB6eO2GLvtfkIWRMPJgKcIYXsA1LNB6jrVl/P1l+Iq2TA67dy4Pl+59q+SK8Z+Rx6xIzWqJ3C9jLVIf3/aj+aX2uZ4abs5Rtk= 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-20-3y7XOynoO5CZtz2J7xeVDg-1; Sun, 28 Jan 2024 19:33:00 +0000 X-MC-Unique: 3y7XOynoO5CZtz2J7xeVDg-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 Jan 2024 19:32:36 +0000 Received: from AcuMS.Aculab.com ([::1]) by AcuMS.aculab.com ([::1]) with mapi id 15.00.1497.048; Sun, 28 Jan 2024 19:32:36 +0000 From: David Laight To: "'linux-kernel@vger.kernel.org'" , "'Linus Torvalds'" , 'Netdev' , "'dri-devel@lists.freedesktop.org'" CC: 'Andy Shevchenko' , 'Andrew Morton' , "'Matthew Wilcox (Oracle)'" , 'Christoph Hellwig' , "'Dan Carpenter'" , 'Linus Walleij' , "'David S . Miller'" , "'linux-btrfs@vger.kernel.org'" , 'Jens Axboe' Subject: [PATCH next 08/11 minmax: Add min_const() and max_const() Thread-Topic: [PATCH next 08/11 minmax: Add min_const() and max_const() Thread-Index: AdpSIMoE/wNceAjoSdeU09EW7XnMUA== Date: Sun, 28 Jan 2024 19:32:36 +0000 Message-ID: <489d199159794ebcbd3d18d4082a88d7@AcuMS.aculab.com> References: <0ca26166dd2a4ff5a674b84704ff1517@AcuMS.aculab.com> In-Reply-To: <0ca26166dd2a4ff5a674b84704ff1517@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-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable 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 --- include/linux/minmax.h | 15 +++++++++++++++ 1 file changed, 15 insertions(+) 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 @@ =09=09=09#op "(" #x ", " #y ") signedness error, fix types or consider u" = #op "() before " #op "_t()"); \ =09=09__cmp_once(op, x, y, uniq); })) =20 +#define __careful_cmp_const(op, x, y)=09=09=09=09\ +=09(BUILD_BUG_ON_ZERO(!__is_constexpr((x) - (y))) +=09\ +=09=09BUILD_BUG_ON_ZERO(!__types_ok(x, y)) +=09=09\ +=09=09__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 'const= ant + * enough' for things like static initialisers. + * min_const(@x, @y) is a constant expression for constant inputs. */ #define min(x, y)=09__careful_cmp(min, x, y, __COUNTER__) +#define min_const(x, y)=09__careful_cmp_const(min, x, y) =20 /** * 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 'const= ant + * enough' for things like static initialisers. + * max_const(@x, @y) is a constant expression for constant inputs. */ #define max(x, y)=09__careful_cmp(max, x, y, __COUNTER__) +#define max_const(x, y)=09__careful_cmp_const(max, x, y) =20 /** * umin - return minimum of two non-negative values --=20 2.17.1 - Registered Address Lakeside, Bramley Road, Mount Farm, Milton Keynes, MK1 1= PT, UK Registration No: 1397386 (Wales)