From patchwork Sun Jan 28 19:32:36 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: David Laight X-Patchwork-Id: 13534607 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 5CFDB31A89 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-btrfs@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 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 @@ #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