Message ID | 62d82a41b94c475bacceb0f6b11583a1@AcuMS.aculab.com |
---|---|
State | New |
Headers |
Return-Path: <linux-kernel-owner@vger.kernel.org> Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:9010:0:b0:3e4:2afc:c1 with SMTP id l16csp2412927vqg; Tue, 25 Jul 2023 04:56:22 -0700 (PDT) X-Google-Smtp-Source: APBJJlGOhf9PyZDuBwGcgOBVTcasfa7XJh8akVfzRZyQCfjPuhxh5Pcr8MkL9HUvAuNThiiG/gvI X-Received: by 2002:a17:906:76d6:b0:993:f497:adc2 with SMTP id q22-20020a17090676d600b00993f497adc2mr12191750ejn.18.1690286182097; Tue, 25 Jul 2023 04:56:22 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1690286182; cv=none; d=google.com; s=arc-20160816; b=fvYPqFFYv+j+adfPAoNZAW1/mIVrAXtm4m2muIYKqqsbdGghKlUTni6o2+4PBVfnQA FciA1AueoxICf5wnh/vZXt41nEWVBVC+Qfso3VtpW9nJhuuAvYXgZjcJ5XAP/4ymhAhP XV/LVPIjmaDBHjzY3YCGitiQsRPGlm/oA8lyJo2KmZnNwM691aCLm40pGMwuvJ1SfJ2R +jXOlYNb7uA3xrfPzj3f58IL1CP2cP0wFD3okHo0fH6X2D3wFdn3yR+wTYB3+iGnkTYQ zUmYkRmyDzTNX+x+pQvA1DDfSHBhgpUhGRKrrWHYgiLhZbkIZUDZJyREt94xYV/FCw8j CEaA== 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 Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id a8-20020a170906274800b00992b55d152esi7392602ejd.242.2023.07.25.04.55.57; Tue, 25 Jul 2023 04:56:22 -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 S232643AbjGYLsp convert rfc822-to-8bit (ORCPT <rfc822;kloczko.tomasz@gmail.com> + 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 <rfc822;linux-kernel@vger.kernel.org>); 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 <linux-kernel@vger.kernel.org>; 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 <David.Laight@ACULAB.COM> To: "'linux-kernel@vger.kernel.org'" <linux-kernel@vger.kernel.org>, "'Andy Shevchenko'" <andriy.shevchenko@linux.intel.com>, 'Andrew Morton' <akpm@linux-foundation.org>, "'Matthew Wilcox (Oracle)'" <willy@infradead.org>, 'Christoph Hellwig' <hch@infradead.org>, "'Jason A. Donenfeld'" <Jason@zx2c4.com> 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: <caa84582f9414de895ac6c4fe2b53489@AcuMS.aculab.com> In-Reply-To: <caa84582f9414de895ac6c4fe2b53489@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_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: <linux-kernel.vger.kernel.org> X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1772393523751569876 X-GMAIL-MSGID: 1772393523751569876 |
Series |
minmax: Relax type checks in min() and max().
|
|
Commit Message
David Laight
July 25, 2023, 11:48 a.m. UTC
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 <david.laight@aculab.com>
---
include/linux/minmax.h | 17 +++++++++++++++++
1 file changed, 17 insertions(+)
Comments
On Tue, Jul 25, 2023 at 11:48:14AM +0000, David Laight wrote: > +#define min_unsigned(x, y) \ > + __careful_cmp((x) + 0u + 0ul + 0ull, (y) + 0u + 0ul + 0ull, <) What is the point of "+ 0u + 0ul + 0ull"? How is that any different from "+ 0ull"? And why force the compiler to do a 64-bit comparison when it could do a 32-bit comparison?
From: Matthew Wilcox > Sent: 25 July 2023 13:39 > > On Tue, Jul 25, 2023 at 11:48:14AM +0000, David Laight wrote: > > +#define min_unsigned(x, y) \ > > + __careful_cmp((x) + 0u + 0ul + 0ull, (y) + 0u + 0ul + 0ull, <) > > What is the point of "+ 0u + 0ul + 0ull"? How is that any different > from "+ 0ull"? And why force the compiler to do a 64-bit comparison > when it could do a 32-bit comparison? The "+ 0u + 0ul + 0ull" causes a signed 32bit value to be zero extended to 64bit. This is significantly cheaper than the sign extension. (Adding 0ull first converts a signed 32bit value to a signed 64bit one - the same as a cast.) The compiler also then knows that the high 32bit are zero and optimises away any associated compares. So you get a 32bit compare (on both 32bit and 64bit) if both arguments are 32bit. This happens even at -O0. It also has no effect on pointer types. David - Registered Address Lakeside, Bramley Road, Mount Farm, Milton Keynes, MK1 1PT, UK Registration No: 1397386 (Wales)
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