Received: by 2002:a05:6358:701b:b0:131:369:b2a3 with SMTP id 27csp4377117rwo; Tue, 25 Jul 2023 05:22:10 -0700 (PDT) X-Google-Smtp-Source: APBJJlGy/4SKOXc6wBit5vUBxtZ9ovKnSYrBL3kcprtFiLvQsVh0MMF/uvXue39VRgYjgv9DKCbO X-Received: by 2002:a17:906:5397:b0:99b:64cb:afa2 with SMTP id g23-20020a170906539700b0099b64cbafa2mr12564776ejo.14.1690287730498; Tue, 25 Jul 2023 05:22:10 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1690287730; cv=none; d=google.com; s=arc-20160816; b=ydlI5BKPUQCTbmmGFJnyy8f0Xc3jfXmzsQCs/7baY0Z7Mn/VdFOpUiYj+Kl+16shIK Oj545YROA/vFnGjCe5uoEk3GhpqIqEtqVYm72CKH3YLqIVaSwQEYFqFJrwP7k22psv8X alHV3gN5mq+Ba++hGImHhKnPKyXmeNB5d5aEqG8emoUMzBXCMAKXF0QQBLHHI2R/ts7b aWJm2W4YUkjDgm0MqA9V7rh5D/rr3aw28dIBEExd0Fx/EMzSb2JUD8GLGo6fhFkUWuep OHfprOyISVdbAsCnwKFXisqzmXux3QJTSoKcYcD8Pv7tqmpgyt/m6LBQ0ZS9+Z1pyR9a rdyQ== 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=6CML+gqwX1HIm/Euus+7vwtgEphx7GwW+W7Dneo+g38=; fh=lQu0FQMQB/KijsOLYPBSZ4oPXp6gJVSgtvs90PKCM00=; b=iz5EzChQmUjRYS5mySlMAyBCJyDh1wO+Wumgdu9gKbToewv0w/lK9GMun9XE/MJGWI xijsqmjZ7CbnZGiCW168EROrTA66fXzLUrIjOQcK2/OWFGJc81zk62cNMYFnEt3vtpzj 9YhqpMWtY6grwejXc444mLZpy2q4rUljRf7aaNweZsNxLPahrWqwYK863b0OgkfAdM1p DBbkZh14bsctwDqXgTNdr/UnzVJ7ciABEsh9ySOlIgoqFM3GE/YJd2VfeF7m3NwTWTZi 4iSfhBX8xZ+sZwQch3X6hoMNbq9kwkSJY1pENHABu/A8fzXkl8VSpfD6rRbUqxW2Vp5x v0Qg== 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 e1-20020a1709062c0100b0099027b40d82si7837148ejh.243.2023.07.25.05.21.45; Tue, 25 Jul 2023 05:22:10 -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 S232625AbjGYLsp convert rfc822-to-8bit (ORCPT + 99 others); Tue, 25 Jul 2023 07:48:45 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:55978 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235098AbjGYLsT (ORCPT ); Tue, 25 Jul 2023 07:48: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 747F4E74 for ; Tue, 25 Jul 2023 04:48:18 -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-261-6tnbdoQ-OpiVapn2n1orTg-1; Tue, 25 Jul 2023 12:48:15 +0100 X-MC-Unique: 6tnbdoQ-OpiVapn2n1orTg-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 12:48:14 +0100 Received: from AcuMS.Aculab.com ([::1]) by AcuMS.aculab.com ([::1]) with mapi id 15.00.1497.048; Tue, 25 Jul 2023 12:48:14 +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 resend 1/5] minmax: Add min_unsigned(a, b) and max_unsigned(a, b) Thread-Topic: [PATCH next resend 1/5] minmax: Add min_unsigned(a, b) and max_unsigned(a, b) Thread-Index: Adm+7eUTa4I5Whq+QJmE/BdQ09ksJA== Date: Tue, 25 Jul 2023 11:48:14 +0000 Message-ID: <62d82a41b94c475bacceb0f6b11583a1@AcuMS.aculab.com> 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. 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)