Received: by 2002:a05:6a10:16a7:0:0:0:0 with SMTP id gp39csp1239044pxb; Fri, 20 Nov 2020 04:56:15 -0800 (PST) X-Google-Smtp-Source: ABdhPJyQvID0y5pCbWX/ZohExhADzdbidcWlJmhKjA+BLBCyKmVAKHxN8zT6/7vmnoGpvAbCG/mJ X-Received: by 2002:a17:906:4547:: with SMTP id s7mr1536669ejq.279.1605876974915; Fri, 20 Nov 2020 04:56:14 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1605876974; cv=none; d=google.com; s=arc-20160816; b=Ej/lpnSZNGqrui1Lwfpmn+2SNEmVePMmt7x56ecENjax/KeEjEDxD0YmD0TZx8SyXS PSRDxXgIWPQqYGkL3yFjRlPcGuN8mVIwcY/wNbBkvKAmqEGGVKyNf8moAhSx55yFSKVi FQvRNsq3EmWr1y0Fsso/lWyHb35C+W7hUafS6OH62oE3ANwGOmliQE8xhuD9SGPoP/ub 4XIxCH0WvO8uNxOnrvTvrhkY/MbMubQSvdF75zOh/Nna4c4iBmwMLcbJAbwPWHWdddLR 1sitCb4nNYfMglNRlSsBkQKMJrIxbxsHlF7gcr7iiwnBb7kqZ4mZubpA5IohH7sZFCBM 2WqQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-disposition:mime-version:message-id :subject:cc:to:from:date:dkim-signature; bh=w8tWVD7LdEH5Iapb8dd0CG6GtxCHUaaVQtuBmEoXo/k=; b=GBFIVzAqyt4zFL4yFz6ovMiPkPHakQFTwaJfJCi+D7GXy1HM5PBitRd9zPOZw8IScH 1aUD3Mp0ETKHPpU/fRNApHdCb3d/I8iL7UE16D5yESIipLUyD3yidCMJVcPt26hynZzX lbeO143nxsQT9BhQLzMW7Mwexgd3avtJfjc/iL3f94m+PvbAHPVsUyAJnJ4e810ews8l SKUZeM3fqgPVn86E091j+6ldQjRfQiuU95FKiRpsoQp39di2r5Dk9KQ6GpYx1HqPZRXc 3lJGYoaaacREwtR/S6DGukipbSJOinCpoZ6C+lBZjFptU19qvQgj0Tf5p13y8o4jaa3Z +NEw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@infradead.org header.s=merlin.20170209 header.b=sDWpWMaM; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id c13si452081edy.288.2020.11.20.04.55.52; Fri, 20 Nov 2020 04:56:14 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@infradead.org header.s=merlin.20170209 header.b=sDWpWMaM; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727939AbgKTMwD (ORCPT + 99 others); Fri, 20 Nov 2020 07:52:03 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:45210 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726898AbgKTMwD (ORCPT ); Fri, 20 Nov 2020 07:52:03 -0500 Received: from merlin.infradead.org (merlin.infradead.org [IPv6:2001:8b0:10b:1231::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id CEF55C0613CF for ; Fri, 20 Nov 2020 04:52:02 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=merlin.20170209; h=Content-Type:MIME-Version:Message-ID: Subject:Cc:To:From:Date:Sender:Reply-To:Content-Transfer-Encoding:Content-ID: Content-Description:In-Reply-To:References; bh=w8tWVD7LdEH5Iapb8dd0CG6GtxCHUaaVQtuBmEoXo/k=; b=sDWpWMaMlYX96L2HJWxU81bEsx WGoBBfaObZ8OxW4Np7wTFfmTIB2pLzTU3BkkUo2tSKv4Berit7C+xkhckULK8mWDCINX2XXw27+0m /BJdP8uhAK21HLEbLL5uWy8fb89ZrTukXtZO9XX+LpRRy/Vmg+EH/ODNZBf7xLYPvT+yYbepyseq+ tE1rrgnLpME9eTbIl9+qPswQnW5SJstbtef0BVIE72nbf8cnnghE7v26y6qLTf+5TEKl4XSDQvQAf UnjdwAiBCceSwOgeHlT6anM2K6XIiPWhLN+S/PfmNJYgpqJPa1egLGbZVva2sNUWIs41KSn1IxQcP tXf6UYew==; Received: from j217100.upc-j.chello.nl ([24.132.217.100] helo=noisy.programming.kicks-ass.net) by merlin.infradead.org with esmtpsa (Exim 4.92.3 #3 (Red Hat Linux)) id 1kg5tM-0000Qq-UJ; Fri, 20 Nov 2020 12:51:57 +0000 Received: from hirez.programming.kicks-ass.net (hirez.programming.kicks-ass.net [192.168.1.225]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (Client did not present a certificate) by noisy.programming.kicks-ass.net (Postfix) with ESMTPS id 5C580304D28; Fri, 20 Nov 2020 13:51:54 +0100 (CET) Received: by hirez.programming.kicks-ass.net (Postfix, from userid 1000) id 4629F20244762; Fri, 20 Nov 2020 13:51:54 +0100 (CET) Date: Fri, 20 Nov 2020 13:51:54 +0100 From: Peter Zijlstra To: Andrew Morton , Linus Torvalds Cc: linux-kernel@vger.kernel.org, christophe.leroy@csgroup.eu, Jakub Jelinek , rdunlap@infradead.org Subject: [PATCH] ilog2: Improve ilog2 for constant arguments Message-ID: <20201120125154.GB3040@hirez.programming.kicks-ass.net> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Jakub Jelinek As discussed in https://gcc.gnu.org/bugzilla/show_bug.cgi?id=97445 the const_ilog2 macro generates a lot of code which interferes badly with GCC inlining heuristics, until it can be proven that the ilog2 argument can or can't be simplified into a constant. It can be expressed using __builtin_clzll builtin which is supported by GCC 3.4 and later and when used only in the __builtin_constant_p guarded code it ought to always fold back to a constant. Other compilers support the same builtin for many years too. Other option would be to change the const_ilog2 macro, though as the description says it is meant to be used also in C constant expressions, and while GCC will fold it to constant with constant argument even in those, perhaps it is better to avoid using extensions in that case. Signed-off-by: Jakub Jelinek Signed-off-by: Peter Zijlstra (Intel) Link: https://lkml.kernel.org/r/20201021132718.GB2176@tucnak --- include/linux/log2.h | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) --- a/include/linux/log2.h +++ b/include/linux/log2.h @@ -156,7 +156,8 @@ unsigned long __rounddown_pow_of_two(uns #define ilog2(n) \ ( \ __builtin_constant_p(n) ? \ - const_ilog2(n) : \ + ((n) < 2 ? 0 : \ + 63 - __builtin_clzll (n)) : \ (sizeof(n) <= 4) ? \ __ilog2_u32(n) : \ __ilog2_u64(n) \