Received: by 2002:a25:7ec1:0:0:0:0:0 with SMTP id z184csp5889708ybc; Wed, 27 Nov 2019 11:07:52 -0800 (PST) X-Google-Smtp-Source: APXvYqy5s3z7Lky9IukI8PKd5YuB09+pnQKhlexe5GsQI6lC2REpvCpkTVRiC4SqqibYoFH5qlbS X-Received: by 2002:a17:906:8590:: with SMTP id v16mr4267327ejx.202.1574881672717; Wed, 27 Nov 2019 11:07:52 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1574881672; cv=none; d=google.com; s=arc-20160816; b=L6/DyvUF9Y4B9XQXU/wC0piWtIECU3YjBjnJrAIRVCyDVGid08bQpeVkudjphj+8is EcCkyt3Oj4euNu9QD6R10+e6HqWr/F7wJOFdx1eddqJwBUU9LQ5xNlSll5RoCDCawYku UKNfFLaR5MdPCYGUn/bjkoA13gI5mVXkOLyHisx9d6n9ivcCkdILg3KX1WkuJHHuECo3 ZwVvx03wr+O5uHdABs1K17odDx4WDJtBsdYCTmEJ8xBg//b0Ac/B37SKSDpDiq4NtmF+ It0X7F0WPLj6i1awqUxhsKuJCffCa6N2RWrHOuGlkDAluvBkH2CZN/vx7/TqBQx2Fs/H ALbg== 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 :content-language:in-reply-to:mime-version:user-agent:date :message-id:from:references:cc:to:subject; bh=YweyAWjnlAV9GnUHr0YS3IA8kaNmWJ5Hyvl4/EGjk7g=; b=ax0C9F9yK+XNPu684ctWp/YTzwECLZly1mklTKtF8VeYpLBrT9lQFTP6veN4t0ZLMX 7R7ZyQYRWO9wealLYYyNmEcPZbrhKU4SO6KgOrQIkJqQ6Vf7YPKQyydglvSowVgZsioP JztyokiKPptKw9DS5Io/lovOVK/VwD07vwXwY9GP6bx6tCagYDSQu/4UI4nCj9JL1IUd xtV3RIj4plrGMBp/7xH6YvOsMq4dsMyX/i8SCg1cjVyb5orWcEQgaGyQg5szP9J1GcVg stB1U8y0szOmVGFaPWWTTJeiHchad8WqAh398eI2Kx4bW3QbbjsKYF/1fIEwXkM1qr6x xtzw== 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 Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id v13si1958150edl.174.2019.11.27.11.07.29; Wed, 27 Nov 2019 11:07:52 -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 Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727172AbfK0TGV (ORCPT + 99 others); Wed, 27 Nov 2019 14:06:21 -0500 Received: from foss.arm.com ([217.140.110.172]:52062 "EHLO foss.arm.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727010AbfK0TGV (ORCPT ); Wed, 27 Nov 2019 14:06:21 -0500 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 04C6F31B; Wed, 27 Nov 2019 11:06:20 -0800 (PST) Received: from [10.1.196.37] (e121345-lin.cambridge.arm.com [10.1.196.37]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPSA id D3B673F6C4; Wed, 27 Nov 2019 11:06:14 -0800 (PST) Subject: Re: [PATCH v3 1/7] linux/log2.h: Add roundup/rounddown_pow_two64() family of functions To: Nicolas Saenz Julienne , Leon Romanovsky Cc: andrew.murray@arm.com, maz@kernel.org, linux-kernel@vger.kernel.org, Lorenzo Pieralisi , Hanjun Guo , Sudeep Holla , Tariq Toukan , Rob Herring , Frank Rowand , Shawn Lin , Heiko Stuebner , Christoph Hellwig , Marek Szyprowski , james.quinlan@broadcom.com, mbrugger@suse.com, f.fainelli@gmail.com, phil@raspberrypi.org, wahrenst@gmx.net, jeremy.linton@arm.com, linux-pci@vger.kernel.org, linux-rpi-kernel@lists.infradead.org, "Rafael J. Wysocki" , Len Brown , "David S. Miller" , Bjorn Helgaas , linux-acpi@vger.kernel.org, linux-arm-kernel@lists.infradead.org, netdev@vger.kernel.org, linux-rdma@vger.kernel.org, devicetree@vger.kernel.org, linux-rockchip@lists.infradead.org, iommu@lists.linux-foundation.org References: <20191126091946.7970-1-nsaenzjulienne@suse.de> <20191126091946.7970-2-nsaenzjulienne@suse.de> <20191126125137.GA10331@unreal> <6e0b9079-9efd-2884-26d1-3db2d622079d@arm.com> From: Robin Murphy Message-ID: Date: Wed, 27 Nov 2019 19:06:12 +0000 User-Agent: Mozilla/5.0 (X11; Linux aarch64; rv:60.0) Gecko/20100101 Thunderbird/60.9.0 MIME-Version: 1.0 In-Reply-To: Content-Type: text/plain; charset=utf-8; format=flowed Content-Language: en-GB Content-Transfer-Encoding: 7bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On 27/11/2019 6:24 pm, Nicolas Saenz Julienne wrote: > On Wed, 2019-11-27 at 18:06 +0000, Robin Murphy wrote: >> On 26/11/2019 12:51 pm, Leon Romanovsky wrote: >>> On Tue, Nov 26, 2019 at 10:19:39AM +0100, Nicolas Saenz Julienne wrote: >>>> Some users need to make sure their rounding function accepts and returns >>>> 64bit long variables regardless of the architecture. Sadly >>>> roundup/rounddown_pow_two() takes and returns unsigned longs. Create a >>>> new generic 64bit variant of the function and cleanup rougue custom >>>> implementations. >>> >>> Is it possible to create general roundup/rounddown_pow_two() which will >>> work correctly for any type of variables, instead of creating special >>> variant for every type? >> >> In fact, that is sort of the case already - roundup_pow_of_two() itself >> wraps ilog2() such that the constant case *is* type-independent. And >> since ilog2() handles non-constant values anyway, might it be reasonable >> to just take the strongly-typed __roundup_pow_of_two() helper out of the >> loop as below? >> >> Robin >> > > That looks way better that's for sure. Some questions. > >> ----->8----- >> diff --git a/include/linux/log2.h b/include/linux/log2.h >> index 83a4a3ca3e8a..e825f8a6e8b5 100644 >> --- a/include/linux/log2.h >> +++ b/include/linux/log2.h >> @@ -172,11 +172,8 @@ unsigned long __rounddown_pow_of_two(unsigned long n) >> */ >> #define roundup_pow_of_two(n) \ >> ( \ >> - __builtin_constant_p(n) ? ( \ >> - (n == 1) ? 1 : \ >> - (1UL << (ilog2((n) - 1) + 1)) \ >> - ) : \ >> - __roundup_pow_of_two(n) \ >> + (__builtin_constant_p(n) && (n == 1)) ? \ >> + 1 : (1UL << (ilog2((n) - 1) + 1)) \ > > Then here you'd have to use ULL instead of UL, right? I want my 64bit value > everywhere regardless of the CPU arch. The downside is that would affect > performance to some extent (i.e. returning a 64bit value where you used to have > a 32bit one)? True, although it's possible that 1ULL might result in the same codegen if the compiler can see that the result is immediately truncated back to long anyway. Or at worst, I suppose "(typeof(n))1" could suffice, however ugly. Either way, this diff was only an illustration rather than a concrete proposal, but it might be an interesting diversion to investigate. On that note, though, you should probably be using ULL in your current patch too. > Also, what about callers to this function on platforms with 32bit 'unsigned > longs' that happen to input a 64bit value into this. IIUC we'd have a change of > behaviour. Indeed, although the change in such a case would be "start getting the expected value instead of nonsense", so it might very well be welcome ;) Robin.