Received: by 2002:a05:6358:700f:b0:131:369:b2a3 with SMTP id 15csp3424633rwo; Fri, 4 Aug 2023 04:56:39 -0700 (PDT) X-Google-Smtp-Source: AGHT+IGzn266pEzy7Na/FZkPW2wZb3GNyWm2SpKnRSEYoLcaSniW7gou0saQtAYPjgzZ1A9A0daE X-Received: by 2002:a17:907:2e0d:b0:99c:4c94:1aa5 with SMTP id ig13-20020a1709072e0d00b0099c4c941aa5mr1173524ejc.51.1691150198754; Fri, 04 Aug 2023 04:56:38 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1691150198; cv=none; d=google.com; s=arc-20160816; b=tOFt1MVen0oNR6BXe17Vf/oFLUmA15oBLPYRwAPtBjAmgslL0pCUh7aUixcx2RXeYe G5n+8EiY4ejRllWHayw2FWqXjI1oDi4C1HJum6DvbdGxc0PyMqbdyxdzKFXRVJY1GNIq EBfCff2YQUMkoWkpQrFwmsGi/DHu6iUE7YVsAatjK2wUPaURGH2929k02Kxy3y+PXCfI jE4UoVAStpRlJdz5fiiBtMIUMv0ak0jo+Db+/C0FOSfW1cVmue0yelnIEdEEYJJlr8YF nZEaIXoVsWsZTpyC6gcuRhNHfN8j0+I6ZW3fCzbiiDCuRMEzztBkcfz3tke89vX7Ygel iLbg== 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=vke6jtXeDUN3p/F59TMgr6Xh2wuROBp4vw6FeTO2r2g=; fh=pN9u3RSY7mSaVAE5ETLbHLyNgii5TPzFbem5G9NhfYQ=; b=aR43wczus8+Eq9XtZ4VCVl8UA/KouT6OLn/H4vmZ+HGUPKMODIkJlnd4AUn/ZLcVG6 0tKNJCyQi6hdazFsGCT8EgMt2z37BLSiGLGtuthjBdMC+W7jol3K/T3TRq/YSOoQVGhz hs54FQTrIitd1L8Tfa9NoVagi3L8rgPmbXTQdEzmesT66bn4cHNz6F2Rch5mkuu+Ws1e 1Ne2Zsv8qmMxkblUKTXVwLsz9WHIzPtf6pX05V2jYiMgmJKqwvjk6ECGTAnGDC1kVt+p SMUAqkE74Y4tzpGisZeHCY36KQgSLFYFd/RWWSzyFgdtgE30gWtVwfkbAyEQkLQY6Q79 owaQ== 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 g17-20020a170906395100b00992b521a3f6si1476101eje.24.2023.08.04.04.56.13; Fri, 04 Aug 2023 04:56:38 -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 S231241AbjHDK4k convert rfc822-to-8bit (ORCPT + 99 others); Fri, 4 Aug 2023 06:56:40 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:55168 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231185AbjHDK4T (ORCPT ); Fri, 4 Aug 2023 06:56:19 -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 690896A7D for ; Fri, 4 Aug 2023 03:53:52 -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-317-u60WpdTSM-qAirDp1yuD6g-1; Fri, 04 Aug 2023 11:53:35 +0100 X-MC-Unique: u60WpdTSM-qAirDp1yuD6g-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; Fri, 4 Aug 2023 11:53:33 +0100 Received: from AcuMS.Aculab.com ([::1]) by AcuMS.aculab.com ([::1]) with mapi id 15.00.1497.048; Fri, 4 Aug 2023 11:53:33 +0100 From: David Laight To: "'linux-kernel@vger.kernel.org'" , "'Andy Shevchenko'" , 'Andrew Morton' , "'Matthew Wilcox (Oracle)'" , 'Christoph Hellwig' , "'Jason A. Donenfeld'" , 'Linus Torvalds' Subject: [PATCH v3 1/5] minmax: Add min_unsigned(a, b) and max_unsigned(a, b) Thread-Topic: [PATCH v3 1/5] minmax: Add min_unsigned(a, b) and max_unsigned(a, b) Thread-Index: AdnGwea6QKJE/BTwROarSQggyisG+w== Date: Fri, 4 Aug 2023 10:53:33 +0000 Message-ID: References: <01e3e09005e9434b8f558a893a47c053@AcuMS.aculab.com> In-Reply-To: <01e3e09005e9434b8f558a893a47c053@AcuMS.aculab.com> 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 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 --- v3: No change. 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)