Received: by 2002:a05:7412:e794:b0:fa:551:50a7 with SMTP id o20csp2328181rdd; Fri, 12 Jan 2024 06:27:53 -0800 (PST) X-Google-Smtp-Source: AGHT+IE0mXH4I7aUsBNYG0YM4VgGrHg98R6WUSHi2vhhuPReQbEjEMzAuEs3edwIDmE4sWnt5rDx X-Received: by 2002:a05:6402:22e8:b0:557:e06e:47b4 with SMTP id dn8-20020a05640222e800b00557e06e47b4mr432209edb.50.1705069673079; Fri, 12 Jan 2024 06:27:53 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1705069673; cv=none; d=google.com; s=arc-20160816; b=BV2sX8saDaiG0bsrfMBXPnm2mzxvUkrAOzkao5XpN3EMVH1AhOx3P3/yXEMUSXR62J +06lwLbAzSiuovSi1PJNkj9VdOmtisF/Bw4IrsLFsJvO+0ijVR3TZR/NNlz/xtuT/Tqt cXOPIgPk4PRCN7+6d9ZejflJ+pN9J96eg5uKI2S0UsvW28viO54f230sh6P+PDv0tiPD 58CKtUNRF1Mjv+6nKf97z1QiyWZCDV3aZ0W/WwwqddvzGr0fK2XIQmOibbcln0/3dYyR lHbuZInpCYy7Yc2AzYWCZYT4lPk96OibBZL5S+9LAUWmqzaaT0QRCHWW3C6EPg8h6hGU chyQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:content-language:mime-version :list-unsubscribe:list-subscribe:list-id:precedence:accept-language :in-reply-to:references:message-id:date:thread-index:thread-topic :subject:cc:to:from; bh=3NWf3oRELIurr90K3cusleDVpI3Yleov1lnIV9u6oOs=; fh=X9mAGPl2ev/UYMtscTnkIwJ7XY2XMCpCjy/CeoerKjY=; b=Z2tN0UcpuQQ5BYGKXhwoio8y5xAQwVAOGgpXfxtCq5JROk5l/KGdO1duoPt6b34TmC LmU3hqfTOXTOLEl2dplXQfF7k85K1VsTstc0dK/IcU1ReMaQWqFawN7JWCOFnq1+wuOO vy9Jd6iQX3VevYgeuxCyofQxE1UY+ERLQ7NxjExZLHXXplPP6nLe4Wuj7Ly0SqPlLMMN 3hiCHO58xquxjzyUHcRhSy+6aA+niHNCFU6RaQXwrlZTdLvim04PzObwAhpo2gJsn3NE s4hsbnlfWfpImb3dDaV/u6iKmMXiw5nzbgB32GmK8INr9dvShDj49nL58w9mYs8wSiLz U+Mw== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel+bounces-24749-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) smtp.mailfrom="linux-kernel+bounces-24749-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=aculab.com Return-Path: Received: from am.mirrors.kernel.org (am.mirrors.kernel.org. [2604:1380:4601:e00::3]) by mx.google.com with ESMTPS id p30-20020a056402501e00b0055762193718si1566083eda.136.2024.01.12.06.27.53 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 12 Jan 2024 06:27:53 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-24749-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) client-ip=2604:1380:4601:e00::3; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel+bounces-24749-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) smtp.mailfrom="linux-kernel+bounces-24749-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=aculab.com Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by am.mirrors.kernel.org (Postfix) with ESMTPS id CF0EF1F24D92 for ; Fri, 12 Jan 2024 14:27:52 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id B6E696DCFC; Fri, 12 Jan 2024 14:26:56 +0000 (UTC) Received: from eu-smtp-delivery-151.mimecast.com (eu-smtp-delivery-151.mimecast.com [185.58.85.151]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 55B3E6DCEC for ; Fri, 12 Jan 2024 14:26:54 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=ACULAB.COM Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=aculab.com 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-6-Q8eILX2xM8GtnaZZwK-k-w-1; Fri, 12 Jan 2024 14:26:51 +0000 X-MC-Unique: Q8eILX2xM8GtnaZZwK-k-w-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, 12 Jan 2024 14:26:33 +0000 Received: from AcuMS.Aculab.com ([::1]) by AcuMS.aculab.com ([::1]) with mapi id 15.00.1497.048; Fri, 12 Jan 2024 14:26:33 +0000 From: David Laight To: 'Dan Carpenter' CC: "linux-kernel@vger.kernel.org" , "Linus Torvalds" , 'Andy Shevchenko' , 'Andrew Morton' , "'Matthew Wilcox (Oracle)'" , 'Christoph Hellwig' , "'Jason A. Donenfeld'" Subject: RE: [PATCH next v4 1/5] minmax: Add umin(a, b) and umax(a, b) Thread-Topic: [PATCH next v4 1/5] minmax: Add umin(a, b) and umax(a, b) Thread-Index: AdnqCG3fyWBHnOXsRX2exERoRDa2+hbTWv2AAAC2xJAAAdpggAAAPeZw Date: Fri, 12 Jan 2024 14:26:33 +0000 Message-ID: References: <41d93ca827a248698ec64bf57e0c05a5@AcuMS.aculab.com> <737627fd-b68b-4c9d-8700-f0e0d6d9cec8@moroto.mountain> <8e45b321c49b4c27a61b2db076ed5383@AcuMS.aculab.com> <02701430-65cf-44ab-8a8b-752c5d973d21@moroto.mountain> In-Reply-To: <02701430-65cf-44ab-8a8b-752c5d973d21@moroto.mountain> Accept-Language: en-GB, en-US X-MS-Has-Attach: X-MS-TNEF-Correlator: x-ms-exchange-transport-fromentityheader: Hosted Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: 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: quoted-printable From: Dan Carpenter > Sent: 12 January 2024 14:03 >=20 > On Fri, Jan 12, 2024 at 01:40:30PM +0000, David Laight wrote: > > From: Dan Carpenter > > > Sent: 12 January 2024 12:50 > > > > > > On Mon, Sep 18, 2023 at 08:16:30AM +0000, David Laight wrote: > > > > +/** > > > > + * umin - return minimum of two non-negative values > > > > + * Signed types are zero extended to match a larger unsigned typ= e. > > > > + * @x: first value > > > > + * @y: second value > > > > + */ > > > > +#define umin(x, y)=09\ > > > > +=09__careful_cmp((x) + 0u + 0ul + 0ull, (y) + 0u + 0ul + 0ull, <) > > > > > > Why do we match "a larger unsigned type" instead of ULL_MAX? Presuma= bly > > > it helps performance somehow... I agree that it's probably fine but = I > > > would be more comfortable if it skipped UINT_MAX and jumped directly = to > > > ULONG_MAX. These days 4 gigs is small potatoes. The vmalloc() funct= ion > > > can allocate 4G so we've had integer overflow bugs with this before. > > > > The '+ 0ul*' carefully zero extend signed values without changing > > unsigned values. > > The compiler detects when it has zero-extended both sides and > > uses the smaller compare. > > In essence: > > =09x + 0u converts 'int' to 'unsigned int'. > > =09=09Avoids the sign extension adding 0ul on 64bit. > > =09x + 0ul converts a 'long' to 'unsigned long'. > > =09=09Avoids the sign extension adding 0ull on 32bit > > =09x + 0ull converts a 'long long' to 'unsigned long long'. > > You need all three to avoid sign extensions and get an unsigned > > compare. >=20 > So unsigned int compares are faster than unsigned long compares? >=20 > It's just sort of weird how it works. >=20 > =09min_t(unsigned long, -1, 10000000000)); =3D> 10000000000 > =09umin(umin(-1, 10000000000)); =3D> UINT_MAX >=20 > UINT_MAX is just kind of a random value. I would have prefered > ULONG_MAX, it's equally random but it's more safe because nothing can > allocate ULONG_MAX bytes. umin() is only defined for non-negative values. So that example is really outside the domain of the function. Consider: =09int x =3D some_positive_value; =09unsigned long long y; then: =09min_t(unsigned long long, x, y); =09Does (unsigned long long)x which is (unsigned long long)(long long)x =09and requires that x be sign extended to 64bits. =09On 32bit that is quite horrid. whereas: =09umin(x, y); =09Only has to zero extend x. =09So is compiled as: =09=09y:hi || y:lo > x ? x ; y If both values are 32bit the compiler generates a 32bit compare (even on 64bit). =09David - Registered Address Lakeside, Bramley Road, Mount Farm, Milton Keynes, MK1 1= PT, UK Registration No: 1397386 (Wales)