Received: by 2002:a05:6358:c692:b0:131:369:b2a3 with SMTP id fe18csp1405720rwb; Fri, 28 Jul 2023 09:00:06 -0700 (PDT) X-Google-Smtp-Source: APBJJlHAv/ywmJzJDHW6upfTug9pcHh04wNGkjXLkgM5zdqvYbF2V4hlJccvYNgud/EKjIO3Ix5D X-Received: by 2002:a05:6a21:6811:b0:12e:7ff9:b612 with SMTP id wr17-20020a056a21681100b0012e7ff9b612mr2066241pzb.45.1690560005825; Fri, 28 Jul 2023 09:00:05 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1690560005; cv=none; d=google.com; s=arc-20160816; b=ks2OoznRXAhyX+VHq6wAgwZp0lPgDS8K5ZWqdmKwUTPiKzGtqYmrK+JR3++9qtpNjy lxDEJtT0zfaOYC60FfL8v8Gqjl3cg7vFIHpH52TpWtgMgXPd1d6MuGM4nNZVJLQVVrM8 TEHeIR4N5Jxlv5Skn2vw7/ZOt34cXKIVEjpyllnbq7oViI7dqwDltQ3CoYk/52zWwaQ9 bZPGh39JTxLSE8NSnE2YR9AB70L+frD6hrw72FSMlYup3LPZgwNbaIW40SHJ5IDoFxiI JB32XW1nl0jQTD6z5Mo2FhNo2Sr42WcPRlzvpkI/bw2zzXULrLbtP+x/w1Rcr2tu5g4H uY7w== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:content-language :mime-version:accept-language:in-reply-to:references:message-id:date :thread-index:thread-topic:subject:to:from; bh=K2+eEhDurKFPMPNEWAeOj/ntZFV4NI/4Si3PD24gHX0=; fh=lQu0FQMQB/KijsOLYPBSZ4oPXp6gJVSgtvs90PKCM00=; b=t4nez6Cu9R7DT6QXH37kiCD5NNg/Z73KDTdoQbF3YzjFems5Y71ibm79gvLxFv/xUe dbqc0Rcx+3mjk6luRkPbHsSox0nJNcvxmX2xjNncVksLrtE204ypTRLjzP7MOrfvCfW7 +3OmcpY2ni0moVh02aeH6zIZ0zeIUypPB8p9RG0ptljOFUNW4HnO0XD8sXgF9pwOM812 z+ju7t8GfvkCAq9x/BP1Df+lQmd5jyZsqOq4xzAGOWPa5TyHgKIfJ3ul4zb8s+Sr8/Ip TjJCmucjYRaodtdnhbQXzjGJjxeS2TSCy1WESTbFf2k+h0ClcrnoVTONcvgbKoGMXPGm QkYg== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=aculab.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id fh9-20020a056a00390900b0067fea30cd05si3354379pfb.79.2023.07.28.08.59.53; Fri, 28 Jul 2023 09:00:05 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=aculab.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S237269AbjG1Ouv convert rfc822-to-8bit (ORCPT + 99 others); Fri, 28 Jul 2023 10:50:51 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:35546 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S237241AbjG1Out (ORCPT ); Fri, 28 Jul 2023 10:50:49 -0400 Received: from eu-smtp-delivery-151.mimecast.com (eu-smtp-delivery-151.mimecast.com [185.58.86.151]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 31A4E4220 for ; Fri, 28 Jul 2023 07:50:44 -0700 (PDT) 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-7-phKjDKwJNyO08x5yzEPeiQ-1; Fri, 28 Jul 2023 15:50:41 +0100 X-MC-Unique: phKjDKwJNyO08x5yzEPeiQ-1 Received: from AcuMS.Aculab.com (10.202.163.4) by AcuMS.aculab.com (10.202.163.4) with Microsoft SMTP Server (TLS) id 15.0.1497.48; Fri, 28 Jul 2023 15:50:40 +0100 Received: from AcuMS.Aculab.com ([::1]) by AcuMS.aculab.com ([::1]) with mapi id 15.00.1497.048; Fri, 28 Jul 2023 15:50:40 +0100 From: David Laight To: "'linux-kernel@vger.kernel.org'" , "'Andy Shevchenko'" , 'Andrew Morton' , "'Matthew Wilcox (Oracle)'" , 'Christoph Hellwig' , "'Jason A. Donenfeld'" Subject: [PATCH next v2 1/5] minmax: Add min_unsigned(a, b) and max_unsigned(a, b) Thread-Topic: [PATCH next v2 1/5] minmax: Add min_unsigned(a, b) and max_unsigned(a, b) Thread-Index: AdnBYtpf8rFndSOOTs6KhZSYh6/0Dg== Date: Fri, 28 Jul 2023 14:50:40 +0000 Message-ID: References: In-Reply-To: Accept-Language: en-GB, en-US X-MS-Has-Attach: X-MS-TNEF-Correlator: x-ms-exchange-transport-fromentityheader: Hosted x-originating-ip: [10.202.205.107] 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: 8BIT X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00,RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H5,RCVD_IN_MSPIKE_WL,SPF_HELO_NONE,SPF_PASS, T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org These can be used when min()/max() errors a signed v unsigned compare when the signed value is known to be non-negative. Unlike min_t(some_unsigned_type, a, b) min_unsigned() will never mask off high bits if an inappropriate type is selected. The '+ 0u + 0ul + 0ull' may look strange. The '+ 0u' is needed for 'signed int' on 64bit systems. The '+ 0ul' is needed for 'signed long' on 32bit systems. The '+ 0ull' is needed for 'signed long long'. Signed-off-by: David Laight --- v2: Updated commit message. include/linux/minmax.h | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/include/linux/minmax.h b/include/linux/minmax.h index 396df1121bff..531860e9cc55 100644 --- a/include/linux/minmax.h +++ b/include/linux/minmax.h @@ -73,6 +73,23 @@ */ #define max(x, y) __careful_cmp(x, y, >) +/** + * min_unsigned - return minimum of two non-negative values + * Signed types are zero extended to match a larger unsigned type. + * @x: first value + * @y: second value + */ +#define min_unsigned(x, y) \ + __careful_cmp((x) + 0u + 0ul + 0ull, (y) + 0u + 0ul + 0ull, <) + +/** + * max_unsigned - return maximum of two non-negative values + * @x: first value + * @y: second value + */ +#define max_unsigned(x, y) \ + __careful_cmp((x) + 0u + 0ul + 0ull, (y) + 0u + 0ul + 0ull, >) + /** * min3 - return minimum of three values * @x: first value -- 2.17.1 - Registered Address Lakeside, Bramley Road, Mount Farm, Milton Keynes, MK1 1PT, UK Registration No: 1397386 (Wales)