Received: by 2002:a25:7ec1:0:0:0:0:0 with SMTP id z184csp5895986ybc; Wed, 27 Nov 2019 11:13:55 -0800 (PST) X-Google-Smtp-Source: APXvYqyTo7TOfjvTR546gyaEItTbVaZBBlgZ6pSDmbZjfpZBt9Fkwn8Nt4UeHbRYLkIuiHB+SeZ8 X-Received: by 2002:a17:906:2898:: with SMTP id o24mr27325604ejd.102.1574882035472; Wed, 27 Nov 2019 11:13:55 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1574882035; cv=none; d=google.com; s=arc-20160816; b=KPkhkCTE70h5Wb8w1MIJURLPs2u5+bip7gJS/Bn7XDbQFV/9KVjtxzLk0KPRuqCmpL jHixDTs/cLpdN96r5E6r+k/ZpFwXQ/fyxr4ae9Gi8SYS4n76yS+pecrr1hLRoA7gbbEn TFJrN0GaH/4DpbCCI09tMSjU5i4/laP1ZwEiEGytriMvOIZ/9/b1oCcEiudsdDJE4hfy x0IAWrolslXPzgIkLi7jY4SZgD29pntX7++quvsygKBL0+Qw8qLWbk+OjsNIf5yAcZ/6 YbCrz1zz3kPZao95E1yCHtgki41ampLZI2aY2xHN0EOYULESxWMtuSLOyEUwLlUotj7j VNRw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:user-agent:in-reply-to :content-disposition:mime-version:references:message-id:subject:cc :to:from:date:dkim-signature; bh=UKWOAMG9VqZinMa0mSkmkfwdSs8PQWBTnDVmdOr/QeU=; b=REfcCZ8SgvyfM0QzP3oHD5CWSGjVrE5xPh/LpioqSs4GzCcxyEihEnFYKKTuzXyTPg 6lm0aV+5O8Jnh3Hu+MP7qQ5kb4Towj4P/vLp+rYn2KIwlhVT7zU24ITJjZtGdjbYqDng CW848CFtAs+vRol6Uc+IFJ3JkcK9epeSc6Z8GRURiIU7497oLZhw9AxaoDD3FhxMcG1V DsimAMdU+cJO2gAr/5mkHC/v4WaqMi3XITuI4rbmsoMXoCiPupXztoVcFdfK7nGl7PIY MjdoyhEM6q0D4/SXYn0oxs+UObYyrAn+p+oDixunk2MT7RDpbFBckaZa14hSTqnc3DmG 20lA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=dOoHu5Ju; 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=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id y16si3593542ejb.98.2019.11.27.11.13.27; Wed, 27 Nov 2019 11:13:55 -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; dkim=pass header.i=@kernel.org header.s=default header.b=dOoHu5Ju; 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=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727107AbfK0TM2 (ORCPT + 99 others); Wed, 27 Nov 2019 14:12:28 -0500 Received: from mail.kernel.org ([198.145.29.99]:38334 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726593AbfK0TM1 (ORCPT ); Wed, 27 Nov 2019 14:12:27 -0500 Received: from localhost (unknown [5.29.147.182]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id 0161920835; Wed, 27 Nov 2019 19:12:25 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1574881946; bh=Kf/5NxHVSIFobJHUMSeX3+TDTrwFmhqNDPHnh0UqOdo=; h=Date:From:To:Cc:Subject:References:In-Reply-To:From; b=dOoHu5JumTuxbJrzjRT3hV4JQEzObfh08t9Z+gODcqAdZKzTi4Eu2kHYBAqXfytVF nx4i0mymUmYUqUxEpczT4vsroGXuejjIhI9UFlrKBajfdiZ6vo416fak1GdlKtMrhH gHlhrhf3LFVTjSrZaGYZziBl9P1P1NEba5MVXgvk= Date: Wed, 27 Nov 2019 21:12:23 +0200 From: Leon Romanovsky To: Robin Murphy Cc: Nicolas Saenz Julienne , 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 Subject: Re: [PATCH v3 1/7] linux/log2.h: Add roundup/rounddown_pow_two64() family of functions Message-ID: <20191127191223.GF10331@unreal> References: <20191126091946.7970-1-nsaenzjulienne@suse.de> <20191126091946.7970-2-nsaenzjulienne@suse.de> <20191126125137.GA10331@unreal> <6e0b9079-9efd-2884-26d1-3db2d622079d@arm.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: User-Agent: Mutt/1.12.1 (2019-06-15) Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Wed, Nov 27, 2019 at 07:06:12PM +0000, Robin Murphy wrote: > 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 ;) Agree, if code overflowed with 32 bits before this change, the code was already broken. At least now, it won't overflow. > > Robin.