Received: by 2002:ac0:aed5:0:0:0:0:0 with SMTP id t21csp455952imb; Fri, 1 Mar 2019 05:22:34 -0800 (PST) X-Google-Smtp-Source: APXvYqy6grd7woAwht6AmHyzcthkZuoD1amgWw5QDVPQ1BWkqSmaFTQMo1zJdoi8KABvCVNE/TOM X-Received: by 2002:a62:1283:: with SMTP id 3mr5672631pfs.122.1551446554148; Fri, 01 Mar 2019 05:22:34 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1551446554; cv=none; d=google.com; s=arc-20160816; b=sVQX9BaBd8PfSgPSIvsoHucuRRUX6o6Fhe0b3Fi1M8Hfd8Gi2TyPsur/AB6TTy52ga NP3NMnZi89nAzDdr/odMkGdUgzEGCjrwYP/VRLmIVAOxMkwDUVzVPoAGBSCyJHTuqLpr 7aCJpt9Epvu+udJm15srmCsxFRznve0yODne7ngrQn0HeY1eDMvNgDMZsqX+idftgmeJ Hi+ADmOWtyifIX2tcfKDFWCdfbeGEmWLbn60ij9MbZWq2kgJ2x9xw19v5lsB4xC8vwR+ AEWGZuyP+VCDDbmvzOBaKccS/03A19x+sGdKTzGp9+TXxmexyi6YyOki//NlW4lS7B8H gybQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:organization :mime-version:message-id:date:subject:cc:to:from; bh=ZSsXVkuuSxXxYNzEggh4T6XncVTTchr802KngRphZyU=; b=j3Zdt1ljdC5k2WzJ+oOveTI3esOtc31rNbW2BP1SHPCrkW/ryCbh5jCySfqioQEPKL K5HfNQcxSt/S+3bYc1PZNRRh/HrT0ovTzV3zX1FNT+jJ6VG5CCy3R9rKUZlPnMj1IqQd RC5HTXSL7xOiy19u5aGvzZF5CMkyzT4/W1TRtlZXl+OvDVs8/HUPA/TXtuvW/njgEael V2oLy22NCfkwaTMNR3k7U6IYDw2jCrEyAQceExDhvA/jPkiQbWVZhQdR640EkCVV7ehC AYrlLsdHSYxnywnAn08JwvpJWV5X9BXyz3YaBm/tEOKfv7dYBTRpCmwVFWNEXpJz8qM/ I6Yw== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=intel.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id d22si20883982plr.37.2019.03.01.05.22.05; Fri, 01 Mar 2019 05:22:34 -0800 (PST) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1732637AbfCAMuO (ORCPT + 99 others); Fri, 1 Mar 2019 07:50:14 -0500 Received: from mga06.intel.com ([134.134.136.31]:25314 "EHLO mga06.intel.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726001AbfCAMuO (ORCPT ); Fri, 1 Mar 2019 07:50:14 -0500 X-Amp-Result: SKIPPED(no attachment in message) X-Amp-File-Uploaded: False Received: from fmsmga001.fm.intel.com ([10.253.24.23]) by orsmga104.jf.intel.com with ESMTP/TLS/DHE-RSA-AES256-GCM-SHA384; 01 Mar 2019 04:50:10 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.58,427,1544515200"; d="scan'208";a="151149153" Received: from jnikula-mobl3.fi.intel.com (HELO localhost) ([10.237.66.172]) by fmsmga001.fm.intel.com with ESMTP; 01 Mar 2019 04:50:08 -0800 From: Jani Nikula To: linux-kernel@vger.kernel.org Cc: Andrew Morton , jani.nikula@intel.com, Chris Wilson Subject: [PATCH] log2: make is_power_of_2() integer constant expression when possible Date: Fri, 1 Mar 2019 14:52:07 +0200 Message-Id: <20190301125207.30973-1-jani.nikula@intel.com> X-Mailer: git-send-email 2.20.1 MIME-Version: 1.0 Organization: Intel Finland Oy - BIC 0357606-4 - Westendinkatu 7, 02160 Espoo Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org While is_power_of_2() is an inline function and likely gets optimized for compile time constant arguments, it still doesn't produce an integer constant expression that could be used in, say, static data initialization or case labels. Make is_power_of_2() an integer constant expression when possible, otherwise using the inline function to avoid multiple evaluation of the parameter. Cc: Chris Wilson Signed-off-by: Jani Nikula --- The alternative would be to define both a function and a macro version of is_power_of_2(), and let the callers decide what to use. --- include/linux/log2.h | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/include/linux/log2.h b/include/linux/log2.h index 2af7f77866d0..035932f52aeb 100644 --- a/include/linux/log2.h +++ b/include/linux/log2.h @@ -37,6 +37,14 @@ int __ilog2_u64(u64 n) } #endif +#define __IS_POWER_OF_2(__n) ((__n) != 0 && (((__n) & ((__n) - 1)) == 0)) + +static inline __attribute__((const)) +bool __is_power_of_2(unsigned long n) +{ + return __IS_POWER_OF_2(n); +} + /** * is_power_of_2() - check if a value is a power of two * @n: the value to check @@ -45,11 +53,8 @@ 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) \ + __builtin_choose_expr(__builtin_constant_p(n), __IS_POWER_OF_2(n), __is_power_of_2(n)) /** * __roundup_pow_of_two() - round up to nearest power of two -- 2.20.1