From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id CAF91C001E0 for ; Tue, 25 Jul 2023 13:20:34 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230075AbjGYNUd convert rfc822-to-8bit (ORCPT ); Tue, 25 Jul 2023 09:20:33 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:51300 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230018AbjGYNU3 (ORCPT ); Tue, 25 Jul 2023 09:20:29 -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 A7F661BC2 for ; Tue, 25 Jul 2023 06:20:05 -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-mtapsc-3-QArLaFZZPIyQRdYbCeCJSQ-1; Tue, 25 Jul 2023 14:20:02 +0100 X-MC-Unique: QArLaFZZPIyQRdYbCeCJSQ-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 14:20:01 +0100 Received: from AcuMS.Aculab.com ([::1]) by AcuMS.aculab.com ([::1]) with mapi id 15.00.1497.048; Tue, 25 Jul 2023 14:20:01 +0100 From: David Laight To: 'Matthew Wilcox' CC: "'linux-kernel@vger.kernel.org'" , "'Andy Shevchenko'" , 'Andrew Morton' , 'Christoph Hellwig' , "'Jason A. Donenfeld'" Subject: RE: [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/BdQ09ksJP///VUA///lOfA= Date: Tue, 25 Jul 2023 13:20:01 +0000 Message-ID: <66e58cd2cffa462a979ac5415874a570@AcuMS.aculab.com> References: <62d82a41b94c475bacceb0f6b11583a1@AcuMS.aculab.com> 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 Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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)