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 gabe.freedesktop.org (gabe.freedesktop.org [131.252.210.177]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id C49D0C76196 for ; Fri, 31 Mar 2023 07:33:45 +0000 (UTC) Received: from gabe.freedesktop.org (localhost [127.0.0.1]) by gabe.freedesktop.org (Postfix) with ESMTP id 084D910E306; Fri, 31 Mar 2023 07:33:45 +0000 (UTC) X-Greylist: delayed 34835 seconds by postgrey-1.36 at gabe; Fri, 31 Mar 2023 07:33:43 UTC Received: from eu-smtp-delivery-151.mimecast.com (eu-smtp-delivery-151.mimecast.com [185.58.86.151]) by gabe.freedesktop.org (Postfix) with ESMTPS id 5FC1610E306 for ; Fri, 31 Mar 2023 07:33:43 +0000 (UTC) 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-159-jibixo4mNCSA74dvMNHr1Q-1; Fri, 31 Mar 2023 08:33:40 +0100 X-MC-Unique: jibixo4mNCSA74dvMNHr1Q-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, 31 Mar 2023 08:33:39 +0100 Received: from AcuMS.Aculab.com ([::1]) by AcuMS.aculab.com ([::1]) with mapi id 15.00.1497.048; Fri, 31 Mar 2023 08:33:38 +0100 From: David Laight To: 'Andrew Morton' Thread-Topic: [PATCH 0/4] log2: make is_power_of_2() more generic Thread-Index: AQHZY0DwWI19nDCca0eIs2l7T1UA9K8T1qTw///9RgCAAKpk8A== Date: Fri, 31 Mar 2023 07:33:38 +0000 Message-ID: <37671dff9b6b4e6bb07862c11cb69874@AcuMS.aculab.com> References: <20230330104243.2120761-1-jani.nikula@intel.com> <20230330125041.83b0f39fa3a4ec1a42dfd95f@linux-foundation.org> <549987e4967d45159573901d330c96a0@AcuMS.aculab.com> <20230330151846.fdbc8edbfbaa6eaddb056dc7@linux-foundation.org> In-Reply-To: <20230330151846.fdbc8edbfbaa6eaddb056dc7@linux-foundation.org> 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: quoted-printable Subject: Re: [Intel-gfx] [PATCH 0/4] log2: make is_power_of_2() more generic X-BeenThere: intel-gfx@lists.freedesktop.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Intel graphics driver community testing & development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Jani Nikula , "intel-gfx@lists.freedesktop.org" , "linux-kernel@vger.kernel.org" , "dri-devel@lists.freedesktop.org" , David Gow , =?iso-8859-1?Q?Christian_K=F6nig?= Errors-To: intel-gfx-bounces@lists.freedesktop.org Sender: "Intel-gfx" From: Andrew Morton > Sent: 30 March 2023 23:19 >=20 > On Thu, 30 Mar 2023 21:53:03 +0000 David Laight = wrote: >=20 > > > But wouldn't all these issues be addressed by simply doing > > > > > > #define is_power_of_2(n) (n !=3D 0 && ((n & (n - 1)) =3D=3D 0)) > > > > > > ? > > > > > > (With suitable tweaks to avoid evaluating `n' more than once) > > > > I think you need to use the 'horrid tricks' from min() to get > > a constant expression from constant inputs. >=20 > This >=20 > --- a/include/linux/log2.h~a > +++ a/include/linux/log2.h > @@ -41,11 +41,11 @@ int __ilog2_u64(u64 n) > * *not* considered a power of two. > * Return: true if @n is a power of 2, otherwise false. > */ > -static inline __attribute__((const)) > -bool is_power_of_2(unsigned long n) > -{ > -=09return (n !=3D 0 && ((n & (n - 1)) =3D=3D 0)); > -} > +#define is_power_of_2(_n)=09=09=09=09\ > +=09({=09=09=09=09=09=09\ > +=09=09typeof(_n) n =3D (_n);=09=09=09\ > +=09=09n !=3D 0 && ((n & (n - 1)) =3D=3D 0);=09=09\ > +=09}) >=20 > /** > * __roundup_pow_of_two() - round up to nearest power of two > _ >=20 > worked for me in a simple test. >=20 > --- a/fs/open.c~b > +++ a/fs/open.c > @@ -1564,3 +1564,10 @@ int stream_open(struct inode *inode, str > } >=20 > EXPORT_SYMBOL(stream_open); > + > +#include > + > +int foo(void) > +{ > +=09return is_power_of_2(43); > +} > _ >=20 >=20 > foo: > # fs/open.c:1573: } > =09xorl=09%eax, %eax=09# > =09ret >=20 >=20 > Is there some more tricky situation where it breaks? Try: static int x =3D is_power_of_2(43); I suspect that some (all?) of the compile-time assert checks won't like ({...}) either. =09David - Registered Address Lakeside, Bramley Road, Mount Farm, Milton Keynes, MK1 1= PT, UK Registration No: 1397386 (Wales)