Received: by 2002:a05:6358:701b:b0:131:369:b2a3 with SMTP id 27csp4277353rwo; Tue, 25 Jul 2023 03:44:21 -0700 (PDT) X-Google-Smtp-Source: APBJJlEofbBp3555px0mXFT2aS8VZO+wZu0tMRc7QkpW8qeGNOkk9QFK4KSSuPnQ2clYBljjWXGR X-Received: by 2002:a05:6358:7e87:b0:12b:c378:b090 with SMTP id o7-20020a0563587e8700b0012bc378b090mr6739512rwn.26.1690281860939; Tue, 25 Jul 2023 03:44:20 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1690281860; cv=none; d=google.com; s=arc-20160816; b=h/oCxJPgrqx7gyx+PcNZEUYbcsdVIduA/UoYCpZdcVmiXix52fpkZJq7NXL/g6Coer bcOuY+mAVykKJCFPInnV++Um8eU9Z8ItX2J4NCpfh520nDswnufHHUBvtLdL1PRMVEX3 kj9LBUZuAceP1jNB25wxHccWUmIHSuOQfO/ioN9V8E7vEyFnWxQI2MR1donh/jA4Nhh3 J32Z+ZzF81stVnUOwvVJGdP4d7aMOCewQXkniuxb26ul0SsuwN4h2BsrFk+bM/I7jywR bntusvLYQQMbC4ID2LdcaqZIh3v0IN9HTy3GiGfuwUW2NQsZFukFOsSEPAUvDzX6coOy PRuw== 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:message-id:date:thread-index :thread-topic:subject:to:from; bh=6CML+gqwX1HIm/Euus+7vwtgEphx7GwW+W7Dneo+g38=; fh=6LDawf1JAWamBn08sBajPnNnIEu8LybObHHZW9kFGho=; b=R+dgP/dR9lg1nw7e5Zz6616JEeOa0KsBeURTzxiCt87leyezD/1F9AcRgxLKXJ0tTt TgjiVd01z7maGQJWdeRhub30JI6g9CNuHCzL0sgYODKfmIq1Gfa+zm9FcddNYy3JVpH2 j1dBjGyOAJ8YGzAvMsq2VQbXOypWrykOScBKGcI2LABZRlpgD/YkU87aEUPQtaExfl8h m6i3apuStbnt+4yxLCfuTPkMbxVWM+aos2cDyd6ffkO59ufJ4mswWRmVugNcu+VOE0Hz rL4wa+GIHVhrOX05zcZEx6rsSRpu4DF8pvbg/Jhcgu/Jkonaeajb1VHs9+3ZyISEe+dQ h8JQ== 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 l1-20020a633e01000000b00543a881db09si10548722pga.402.2023.07.25.03.44.07; Tue, 25 Jul 2023 03:44:20 -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 S231533AbjGYKBF convert rfc822-to-8bit (ORCPT + 99 others); Tue, 25 Jul 2023 06:01:05 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:59464 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233488AbjGYKA4 (ORCPT ); Tue, 25 Jul 2023 06:00:56 -0400 Received: from eu-smtp-delivery-151.mimecast.com (eu-smtp-delivery-151.mimecast.com [185.58.85.151]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 337F710E3 for ; Tue, 25 Jul 2023 03:00:49 -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-124-SA4uouzBO965Nt6ACHgVHg-1; Tue, 25 Jul 2023 11:00:46 +0100 X-MC-Unique: SA4uouzBO965Nt6ACHgVHg-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; Tue, 25 Jul 2023 11:00:45 +0100 Received: from AcuMS.Aculab.com ([::1]) by AcuMS.aculab.com ([::1]) with mapi id 15.00.1497.048; Tue, 25 Jul 2023 11:00:45 +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 1/5] minmax: Add min_unsigned(a, b) and max_unsigned(a, b) Thread-Topic: [PATCH next 1/5] minmax: Add min_unsigned(a, b) and max_unsigned(a, b) Thread-Index: Adm+2zJY+ejT7PjHQ52rHHfvsNPoKA== Date: Tue, 25 Jul 2023 10:00:45 +0000 Message-ID: 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_BLOCKED,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. Signed-off-by: David Laight --- 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)