Received: by 2002:a05:6358:11c7:b0:104:8066:f915 with SMTP id i7csp852183rwl; Wed, 5 Apr 2023 08:31:06 -0700 (PDT) X-Google-Smtp-Source: AKy350ZRdQOApT4Z3WLlRDrqx6vE6Ewo8y3/LLM9uQWsnYaCrer+o5FQLUmxSkltIFfGmQme65mx X-Received: by 2002:aa7:d148:0:b0:502:100c:531 with SMTP id r8-20020aa7d148000000b00502100c0531mr2102590edo.40.1680708666264; Wed, 05 Apr 2023 08:31:06 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1680708666; cv=none; d=google.com; s=arc-20160816; b=MJOILPF/Esq/0MlGTcoD9rrRq8sbjUdJMHRGMqCdsFL1wQC4KWNXzZVB47DUxZyjMv H/+/d3eUk4xgqlhjftzHSaHF9DVaf8aph0NolA7DOntesLTrPSNyQBVRz0g/YGEWi4Oj kCPX/uPjWtD8/9HXSZbSQVxMOnNmfpGsBIz9mzaXQCkZcThuhiSJOPSYc+fTxicX8WJP RHa6rWABZbisioBZpct2IAfXojf2ZEYXXxGlY09mfJ3pFavC3gz8AwVHkebluGmnikZs ZNY1PkvYYdvjUdC5qG5tku2yDsCwjxfOCzn9vvL4YH1ZnXNMACPbfEl0Z2SR3LpYimO8 Zlcg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:in-reply-to:from :content-language:references:cc:to:subject:user-agent:mime-version :date:message-id; bh=k7f0AKq8XHTgKc49V6y3xRlnifFajNQV7DTJ8ga8KlY=; b=CWzmY+c4Ai4BucvU+oEOxZ3cHHe8FQdcxWmfVTCI785iL8LCP75qAI85WAvZoLJfF1 WmBdJ4tAdXvYH1djx9n7XisWgRbiEFtsGa/5sDF9lnBVyyk7cXdrls/FRWNcT+mh5TQl WHI2j1EMUEIfFf4H+Z0w3ofYw/74NXdegv4Sn2H1eOJY7LKpBid0+tPG5lDwz61hJEbr /jAAtp8ZkSgihdTTtDBCwahBvo/kvN3FZfIyZj8ZEvoUa15spdoxZHP8DZgxfMkWI4XO nmgSX8g1W2lCJ38yb9jcLlYWKKgJ79FN1p97xkmNInrYcDMPb/7hNXmZ90EtkpL7Wup6 hE1Q== 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=arm.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id s5-20020aa7d785000000b00501d4d937e5si13033697edq.25.2023.04.05.08.30.35; Wed, 05 Apr 2023 08:31:06 -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=arm.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S238604AbjDEP2A (ORCPT + 99 others); Wed, 5 Apr 2023 11:28:00 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:57078 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S238455AbjDEP1x (ORCPT ); Wed, 5 Apr 2023 11:27:53 -0400 Received: from foss.arm.com (foss.arm.com [217.140.110.172]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id 731AC4C31 for ; Wed, 5 Apr 2023 08:27:49 -0700 (PDT) Received: from usa-sjc-imap-foss1.foss.arm.com (unknown [10.121.207.14]) by usa-sjc-mx-foss1.foss.arm.com (Postfix) with ESMTP id 4B9FB113E; Wed, 5 Apr 2023 08:28:33 -0700 (PDT) Received: from [10.57.94.33] (unknown [10.57.94.33]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPSA id 1BDF93F6C4; Wed, 5 Apr 2023 08:27:46 -0700 (PDT) Message-ID: <8905800b-a977-e821-01ea-a43333f46904@arm.com> Date: Wed, 5 Apr 2023 16:27:50 +0100 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101 Thunderbird/102.9.0 Subject: Re: [PATCH 0/4] log2: make is_power_of_2() more generic To: Jani Nikula , Andrew Morton , David Laight Cc: David Gow , "intel-gfx@lists.freedesktop.org" , "linux-kernel@vger.kernel.org" , "dri-devel@lists.freedesktop.org" , =?UTF-8?Q?Christian_K=c3=b6nig?= References: <20230330104243.2120761-1-jani.nikula@intel.com> <20230330125041.83b0f39fa3a4ec1a42dfd95f@linux-foundation.org> <549987e4967d45159573901d330c96a0@AcuMS.aculab.com> <20230330151846.fdbc8edbfbaa6eaddb056dc7@linux-foundation.org> <87edp52ufk.fsf@intel.com> Content-Language: en-GB From: Steven Price In-Reply-To: <87edp52ufk.fsf@intel.com> Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 7bit X-Spam-Status: No, score=-3.7 required=5.0 tests=NICE_REPLY_A, RCVD_IN_DNSWL_MED,SPF_HELO_NONE,SPF_NONE autolearn=unavailable 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 On 31/03/2023 09:31, Jani Nikula wrote: > On Thu, 30 Mar 2023, Andrew Morton wrote: >> On Thu, 30 Mar 2023 21:53:03 +0000 David Laight wrote: >> >>>> But wouldn't all these issues be addressed by simply doing >>>> >>>> #define is_power_of_2(n) (n != 0 && ((n & (n - 1)) == 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. >> >> This >> >> --- 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) >> -{ >> - return (n != 0 && ((n & (n - 1)) == 0)); >> -} >> +#define is_power_of_2(_n) \ >> + ({ \ >> + typeof(_n) n = (_n); \ >> + n != 0 && ((n & (n - 1)) == 0); \ >> + }) >> >> /** >> * __roundup_pow_of_two() - round up to nearest power of two >> _ >> >> worked for me in a simple test. >> >> --- a/fs/open.c~b >> +++ a/fs/open.c >> @@ -1564,3 +1564,10 @@ int stream_open(struct inode *inode, str >> } >> >> EXPORT_SYMBOL(stream_open); >> + >> +#include >> + >> +int foo(void) >> +{ >> + return is_power_of_2(43); >> +} >> _ >> >> >> foo: >> # fs/open.c:1573: } >> xorl %eax, %eax # >> ret >> >> >> Is there some more tricky situation where it breaks? > > It doesn't work with BUILD_BUG_ON_ZERO(). Like most programming problems, you just need another layer of indirection! The below works for me in all the cases I could think of (including __uint128_t). #define __IS_POWER_OF_2(n) (n != 0 && ((n & (n - 1)) == 0)) #define _IS_POWER_OF_2(n, unique_n) \ ({ \ typeof(n) unique_n = (n); \ __IS_POWER_OF_2(unique_n); \ }) #define is_power_of_2(n) \ __builtin_choose_expr(__is_constexpr((n)), \ __IS_POWER_OF_2((n)), \ _IS_POWER_OF_2(n, __UNIQUE_ID(_n))) Although Jani's original might be easier to understand. Steve