From patchwork Sun Feb 25 16:50:47 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: David Laight X-Patchwork-Id: 206092 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7300:a81b:b0:108:e6aa:91d0 with SMTP id bq27csp1657142dyb; Sun, 25 Feb 2024 08:51:19 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCWfIguzj4qly7KlUWsdQBsT3XT5DnqxU9E9B3qGnosUvnnIvnoJfEH2szQVP7xsaRO6Ct9yObKY70yU941SqUkm5Mu7LQ== X-Google-Smtp-Source: AGHT+IFuKAnpLUw+FhK+HWa/+5q1gaubj5XfXHIKxRMfpYRl3KfK5MuhDwmyVHmO3SchL0p3DG30 X-Received: by 2002:a05:620a:2a12:b0:787:caee:4309 with SMTP id o18-20020a05620a2a1200b00787caee4309mr3196874qkp.31.1708879878871; Sun, 25 Feb 2024 08:51:18 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1708879878; cv=pass; d=google.com; s=arc-20160816; b=KttschBkeCGqQFI2zZy2rqw3mxa6ylcKBaPkPV5Ii6L5DUUku4WkAE0DAKh/lgwxyq CiwlFIYE5X38W3zwytIHkgOHT22PZcztJjHzU7XRXytXxMSviphRFjcg5K6Nq1OsK5W7 4z7Ysud4InTs+JybOnpo/y0dXMVmGznC5KPpQgHi5tClbcaJmt+63TkZhS/asGwwOdSx 3YZgNAVFWg2JDidlcGrGrzAo7WAhwOVizO76TCMj7bAN9gF4WVbtbvBctdQVOFVEIgnK TprOxG3n8Wm9qdmKvvk9Bz5qGUGuy5YJxVlvBZutbp+1p/xekBUqSL7Wi3Y9qo8mtXaf cDkQ== 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=+lOMyrz3Pc3RSh+srEYELe1hGEgmgpaasYGAX0sPieA=; fh=nGE692sbVdRvha6RYbzMUXtm1nr/30sV7frdtDN2z14=; b=lfBUx6rEZdNDfSkAstq+X47BI5NZPMNvL/xm6jTyw/nlCmW3IFmoUzhkYO6fpouG5U WGnudO2qMRjEW+UeuKULAcb/D6/H7z1UaM7M3wV3y9J6YENprS0U7sOkjwYOOtZKogA9 ZG9Z6ujtgdJI/BLNQzKPY7c+6ZlLZML4fCuwryLLbOks1BhMadEAZVqwxiAah+zxfG84 XxC1SmUMz5sqCrCdHLsmKJp2XoLFneF+k3DuvOyBRtkBEFmIVUeKoQBIENY5RtjajsRG ZroO8Jb5thSpfQjBEzk5aQCkLzBZWT91gD8kP+9mwFHO9dGq+FPPG0gwYzQxDc3i4pcw R7/g==; 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-80212-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-80212-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=aculab.com Received: from ny.mirrors.kernel.org (ny.mirrors.kernel.org. [2604:1380:45d1:ec00::1]) by mx.google.com with ESMTPS id i7-20020a37c207000000b00787c0e79d2dsi3387693qkm.572.2024.02.25.08.51.18 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 25 Feb 2024 08:51:18 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-80212-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) client-ip=2604:1380:45d1:ec00::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-80212-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-80212-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 ny.mirrors.kernel.org (Postfix) with ESMTPS id 9A84B1C2112F for ; Sun, 25 Feb 2024 16:51:18 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id D1CF9199B8; Sun, 25 Feb 2024 16:50:56 +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 EF54379D3 for ; Sun, 25 Feb 2024 16:50:51 +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=1708879854; cv=none; b=bn2Pc5xRj/vaCKT9ttr8qkXVmoESE843s+5PfEKBWNnBYO6wiKrkJlrwGvv5Kjnl/XEjS3jWxqQmZfydym8oe70sPO1CTNayvoqAjaAfVgcAg6iuogGTbP7hIE5kAbafTFXJKiBzgnVBNHFem4rgcipjIxU7ivhuoem+8fV7Z68= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1708879854; c=relaxed/simple; bh=+atgDbr45v2v/fK8FAQruXtzObgP16hiBnUi3W5GHsA=; h=From:To:CC:Subject:Date:Message-ID:References:In-Reply-To: MIME-Version:Content-Type; b=I8OqGg3kEWdl0LWSiWRL86+DH6uBOAaLMgJSj1ipEbC0068e9dvOUYb9n3vvdnFxJHYJP6zu1SrL+cgkMBRkt32iFLNtC4CtZVUdCNDqsFCdki+VUYNBj85uTenjEb2GOBzbNn5PIviU6TLhCk26GrHgaf/ZJFiGap/ho9TN3YI= 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-262-6W1dHPmgNxKn61PI8Ye6Yg-1; Sun, 25 Feb 2024 16:50:48 +0000 X-MC-Unique: 6W1dHPmgNxKn61PI8Ye6Yg-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:50:47 +0000 Received: from AcuMS.Aculab.com ([::1]) by AcuMS.aculab.com ([::1]) with mapi id 15.00.1497.048; Sun, 25 Feb 2024 16:50:47 +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 04/11] minmax: Replace multiple __UNIQUE_ID() by directly using __COUNTER__ Thread-Topic: [PATCH next v2 04/11] minmax: Replace multiple __UNIQUE_ID() by directly using __COUNTER__ Thread-Index: AdpoCsQOvIgG9TyzTYmUjuthkRVCRw== Date: Sun, 25 Feb 2024 16:50:47 +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 X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1791890427815977736 X-GMAIL-MSGID: 1791890427815977736 Provided __COUNTER__ is passed through an extra #define it can be pasted onto multiple local variables to give unique names. This saves having 3 __UNIQUE_ID() for #defines with three locals and look less messy in general. Stop the umin()/umax() lines being overlong by factoring out the zero-extension logic. Signed-off-by: David Laight --- include/linux/minmax.h | 48 +++++++++++++++++++++--------------------- 1 file changed, 24 insertions(+), 24 deletions(-) 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 c32b4b40ce01..8ee003d8abaf 100644 --- a/include/linux/minmax.h +++ b/include/linux/minmax.h @@ -8,7 +8,7 @@ #include /* - * min()/max()/clamp() macros must accomplish several things: + * min()/max()/clamp() macros must accomplish three things: * * - Avoid multiple evaluations of the arguments (so side-effects like * "x++" happen only once) when non-constant. @@ -43,31 +43,31 @@ #define __cmp(op, x, y) ((x) __cmp_op_##op (y) ? (x) : (y)) -#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), \ +#define __cmp_once(op, x, y, uniq) ({ \ + typeof(x) __x_##uniq = (x); \ + typeof(y) __y_##uniq = (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); }) + __cmp(op, __x_##uniq, __y_##uniq); }) -#define __careful_cmp(op, x, y) \ +#define __careful_cmp(op, x, y, uniq) \ __builtin_choose_expr(__is_constexpr((x) - (y)), \ __cmp(op, x, y), \ - __cmp_once(op, x, y, __UNIQUE_ID(__x), __UNIQUE_ID(__y))) + __cmp_once(op, x, y, uniq)) /** * min - return minimum of two values of the same or compatible types * @x: first value * @y: second value */ -#define min(x, y) __careful_cmp(min, x, y) +#define min(x, y) __careful_cmp(min, x, y, __COUNTER__) /** * max - return maximum of two values of the same or compatible types * @x: first value * @y: second value */ -#define max(x, y) __careful_cmp(max, x, y) +#define max(x, y) __careful_cmp(max, x, y, __COUNTER__) /** * umin - return minimum of two non-negative values @@ -75,8 +75,9 @@ * @x: first value * @y: second value */ +#define __zero_extend(x) ((x) + 0u + 0ul + 0ull) #define umin(x, y) \ - __careful_cmp(min, (x) + 0u + 0ul + 0ull, (y) + 0u + 0ul + 0ull) + __careful_cmp(min, __zero_extend(x), _zero_extend(y), __COUNTER__) /** * umax - return maximum of two non-negative values @@ -84,7 +85,7 @@ * @y: second value */ #define umax(x, y) \ - __careful_cmp(max, (x) + 0u + 0ul + 0ull, (y) + 0u + 0ul + 0ull) + __careful_cmp(max, __zero_extend(x), _zero_extend(y), __COUNTER__) /** * min3 - return minimum of three values @@ -108,7 +109,7 @@ * @x: first value * @y: second value */ -#define min_t(type, x, y) __careful_cmp(min, (type)(x), (type)(y)) +#define min_t(type, x, y) __careful_cmp(min, (type)(x), (type)(y), __COUNTER__) /** * max_t - return maximum of two values, using the specified type @@ -116,7 +117,7 @@ * @x: first value * @y: second value */ -#define max_t(type, x, y) __careful_cmp(max, (type)(x), (type)(y)) +#define max_t(type, x, y) __careful_cmp(max, (type)(x), (type)(y), __COUNTER__) /** * min_not_zero - return the minimum that is _not_ zero, unless both are zero @@ -131,22 +132,21 @@ #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); \ +#define __clamp_once(val, lo, hi, uniq) ({ \ + typeof(val) __val_##uniq = (val); \ + typeof(lo) __lo_##uniq = (lo); \ + typeof(hi) __hi_##uniq = (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); }) + __clamp(__val_##uniq, __lo_##uniq, __hi_##uniq); }) -#define __careful_clamp(val, lo, hi) ({ \ +#define __careful_clamp(val, lo, hi, uniq) ({ \ __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_once(val, lo, hi, uniq)); }) /** * clamp - return a value clamped to a given range with strict typechecking @@ -156,7 +156,7 @@ * * This macro checks that @val, @lo and @hi have the same signedness. */ -#define clamp(val, lo, hi) __careful_clamp(val, lo, hi) +#define clamp(val, lo, hi) __careful_clamp(val, lo, hi, __COUNTER__) /** * clamp_t - return a value clamped to a given range using a given type @@ -168,7 +168,7 @@ * 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)) +#define clamp_t(type, val, lo, hi) clamp((type)(val), (type)(lo), (type)(hi)) /** * clamp_val - return a value clamped to a given range using val's type