From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from eggs.gnu.org ([2001:4830:134:3::10]:59480) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1YWUsV-0008Iv-Hf for qemu-devel@nongnu.org; Fri, 13 Mar 2015 15:04:12 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1YWUsR-0003Yj-Hb for qemu-devel@nongnu.org; Fri, 13 Mar 2015 15:04:11 -0400 Received: from mail-qg0-x231.google.com ([2607:f8b0:400d:c04::231]:39483) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1YWUsR-0003Ye-CF for qemu-devel@nongnu.org; Fri, 13 Mar 2015 15:04:07 -0400 Received: by qgdq107 with SMTP id q107so28232879qgd.6 for ; Fri, 13 Mar 2015 12:04:07 -0700 (PDT) Sender: Richard Henderson Message-ID: <550334A2.10005@twiddle.net> Date: Fri, 13 Mar 2015 12:04:02 -0700 From: Richard Henderson MIME-Version: 1.0 References: <1424694237-22786-1-git-send-email-aik@ozlabs.ru> <877fv8sp97.fsf@blackfin.pond.sub.org> <54ED19E9.4050503@ozlabs.ru> <877fv6gth5.fsf@blackfin.pond.sub.org> <5501B0CC.3090500@twiddle.net> <5501C2C6.1060501@redhat.com> In-Reply-To: <5501C2C6.1060501@redhat.com> Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 7bit Subject: Re: [Qemu-devel] [PATCH v2] utils: Add pow2ceil() List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: Eric Blake , Markus Armbruster , Alexey Kardashevskiy Cc: Peter Maydell , qemu-devel@nongnu.org On 03/12/2015 09:45 AM, Eric Blake wrote: > On 03/12/2015 09:29 AM, Richard Henderson wrote: >> On 02/25/2015 02:45 AM, Markus Armbruster wrote: >>> return 0x8000000000000000u >> (clz64(value - 1) - 1); >> >> I realize this was weeks ago, but it would certainly be preferable to shift a >> small constant left than a large constant right. >> >> Most RISC machines can't form 0x8000000000000000ull without loading 1 and then >> left shifting to start with. So end the end you're better off with >> >> return 1ull << (63 - clz64(value)); > > Since the value being shifted is a constant either way, can't gcc figure > out the equivalence and generate the optimal code to begin with? If > not, should it be opened as a gcc bug for potential optimization? With the simplest of tests, unsigned long f(unsigned long x) { return 1UL << (63 - x); } unsigned long g(unsigned long x) { return 0x8000000000000000ul >> x; } the code is of similar size: 3 operations each. But if you throw in the whole operation 1ul << (63 - (__builtin_clzl(x - 1) - 1)) vs 0x8...0ul >> (__builtin_clzl(x - 1) - 1) then gcc is able to fold away one of the instructions and the 1UL alternative is shorter. r~