Received: from eggs.gnu.org ([2001:4830:134:3::10]:38556) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1YPx04-0007rg-2X for qemu-devel@nongnu.org; Mon, 23 Feb 2015 12:41:01 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1YPx02-0001B4-Ke for qemu-devel@nongnu.org; Mon, 23 Feb 2015 12:40:56 -0500 Received: from mx1.redhat.com ([209.132.183.28]:52556) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1YPx02-0001As-C5 for qemu-devel@nongnu.org; Mon, 23 Feb 2015 12:40:54 -0500 From: Markus Armbruster References: <1424694237-22786-1-git-send-email-aik@ozlabs.ru> <877fv8sp97.fsf@blackfin.pond.sub.org> <54EB52AE.4030002@redhat.com> Date: Mon, 23 Feb 2015 18:40:48 +0100 In-Reply-To: <54EB52AE.4030002@redhat.com> (Eric Blake's message of "Mon, 23 Feb 2015 09:17:50 -0700") Message-ID: <87sidwo7a7.fsf@blackfin.pond.sub.org> MIME-Version: 1.0 Content-Type: text/plain Subject: Re: [Qemu-devel] [PATCH v2] utils: Add pow2ceil() List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: Eric Blake Cc: Alexey Kardashevskiy , Peter Maydell , qemu-devel@nongnu.org Eric Blake writes: > On 02/23/2015 06:59 AM, Markus Armbruster wrote: >> Alexey Kardashevskiy writes: >> >>> This adds a helper to get closest bigger power-of-two value. >>> > >> >> Here's how I'd do these functions: >> >> int64_t pow2floor(int64_t value) >> { >> assert(value > 0); >> return 0x8000000000000000u >> clz64(value); >> } > > Needs to be 0x8000000000000000ull for 32-bit machines to compile correctly. Why? > Why is the parameter int64_t? Wouldn't it be more useful to have: > > uint64_t pow2floor(uint64_t value) Crossed my mind, too. However, the existing callers pass *signed* arguments. >> int64_t pow2ceil(int64_t value) >> { > > Again, why allow signed inputs? > >> assert(value <= 0x4000000000000000) >> if (value <= 1) >> return 1; > > In particular, this slams all negative values to a result of 1, which > doesn't necessarily make sense. It implements a straightforward contract: return the smallest power of two greater or equal to the argument. The function's domain is the set of int64_t arguments where this value can be represented in int64_t, i.e. [-2^63..2^62]. Feel free to suggest a more sensible contract. >> return 0x8000000000000000u >> (clz64(value - 1) - 1); >> } >> >> >>