Received: by 2002:a05:7208:9594:b0:7e:5202:c8b4 with SMTP id gs20csp897437rbb; Sun, 25 Feb 2024 08:53:55 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCW52wzVrKvtEgiWldDTlEcDT4f7CVwfCAdt0Rj1+rdV+/227KQjPd3erVD7yNawW1lmHPEuXP0f5/C/499JvhTf8gFy+nEpu0HSD7h6Lg== X-Google-Smtp-Source: AGHT+IF6J/YK/LU7Myta0RyTHvbBZoFMyqFZBL6uONc3AAnETVx3xcCl7p7P9VXUdKe1QUvmNhNj X-Received: by 2002:a17:903:1cd:b0:1dc:876d:7b1e with SMTP id e13-20020a17090301cd00b001dc876d7b1emr4903533plh.2.1708880034742; Sun, 25 Feb 2024 08:53:54 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1708880034; cv=pass; d=google.com; s=arc-20160816; b=UOR+7sXhWTGYA3GwA63G/OcYcpUAQ7Yjsf7OLyf0WN65GuwGs2wu1CGmBal+KMwdAs WR44TimQqRnZT7GPn3vp9MPF9BI5bpi0WVddZYrcW+a11fgg6JI5XXZkrwMSbpGLWCHH H35VjCFsrHemd+0pJed+ur/c932l8LlQnlJacAa4UvYh463cTNQVD4XSzYOdBHJQAVJZ Plzsh7oDuSLwMvEIJAdW8p8sVyqsxJj1sSPZBYy3ZxMk2HJCORMQ20+Z4AjjzuAGmBqZ KtE8ml1cUhGYO8Vkm2tUIR+/eU65N1SksAg+lR0SiS2XBLmNtoZKM1UjMFlL+mp95gd5 q+ag== 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-linux.lists.archive=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) smtp.mailfrom="linux-kernel+bounces-80217-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. [139.178.88.99]) by mx.google.com with ESMTPS id lk7-20020a17090308c700b001dbfefe66fbsi2357734plb.591.2024.02.25.08.53.54 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 25 Feb 2024 08:53:54 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-80217-linux.lists.archive=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) client-ip=139.178.88.99; 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-linux.lists.archive=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) smtp.mailfrom="linux-kernel+bounces-80217-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 7063C281A55 for ; Sun, 25 Feb 2024 16:53:54 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 39BD51804E; Sun, 25 Feb 2024 16:53:47 +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 ; 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 To: "'linux-kernel@vger.kernel.org'" , "'Linus Torvalds'" , 'Netdev' , "'dri-devel@lists.freedesktop.org'" CC: 'Jens Axboe' , "'Matthew Wilcox (Oracle)'" , 'Christoph Hellwig' , "'linux-btrfs@vger.kernel.org'" , "'Andrew Morton'" , 'Andy Shevchenko' , "'David S . Miller'" , 'Dan Carpenter' , "'Jani Nikula'" 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: 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: 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(+) Changes for v2: - Typographical and spelling corrections to the commit messages. Patches unchanged. 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)