From owner-freebsd-current@FreeBSD.ORG Wed Feb 7 10:55:38 2007 Return-Path: X-Original-To: current@freebsd.org Delivered-To: freebsd-current@FreeBSD.ORG Received: from mx1.freebsd.org (mx1.freebsd.org [69.147.83.52]) by hub.freebsd.org (Postfix) with ESMTP id DBCF316A415 for ; Wed, 7 Feb 2007 10:55:38 +0000 (UTC) (envelope-from asmrookie@gmail.com) Received: from nf-out-0910.google.com (nf-out-0910.google.com [64.233.182.190]) by mx1.freebsd.org (Postfix) with ESMTP id DB42F13C4B6 for ; Wed, 7 Feb 2007 10:55:37 +0000 (UTC) (envelope-from asmrookie@gmail.com) Received: by nf-out-0910.google.com with SMTP id m19so429888nfc for ; Wed, 07 Feb 2007 02:55:36 -0800 (PST) DomainKey-Signature: a=rsa-sha1; c=nofws; d=gmail.com; s=beta; h=received:message-id:date:from:sender:to:subject:cc:in-reply-to:mime-version:content-type:content-transfer-encoding:content-disposition:references:x-google-sender-auth; b=MozX4O1pxMWu8sHX7O7g8HZOMWzg7v21L6kv08JTppYWGWO9yJtBLFhol7mqQL2fI78fS/0arj9cNTUB3jIqmHh+jYN7gNcaSW8JJqS0UG2ujNdZ7AlnnuiHzZh1ZNs3aGP5zos5IoS1Axr3KBZsEtS5eSuWCSuZLLhwAHfz4Ls= Received: by 10.82.114.3 with SMTP id m3mr759337buc.1170845727939; Wed, 07 Feb 2007 02:55:27 -0800 (PST) Received: by 10.48.238.14 with HTTP; Wed, 7 Feb 2007 02:55:27 -0800 (PST) Message-ID: <3bbf2fe10702070255q58ab6c9fn2b393cfbc75a3fb@mail.gmail.com> Date: Wed, 7 Feb 2007 11:55:27 +0100 From: "Attilio Rao" Sender: asmrookie@gmail.com To: "Luigi Rizzo" In-Reply-To: <20070207024918.D63529@xorpc.icir.org> MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit Content-Disposition: inline References: <20070207004131.A62183@xorpc.icir.org> <15241.SV0BLFJbGwk=.1170843949.squirrel@koef.zs64.net> <20070207024918.D63529@xorpc.icir.org> X-Google-Sender-Auth: 1e9482dbd682f349 Cc: Stefan Bethke , current@freebsd.org Subject: Re: C macro to find the next power of 2 ? X-BeenThere: freebsd-current@freebsd.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: Discussions about the use of FreeBSD-current List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Wed, 07 Feb 2007 10:55:40 -0000 2007/2/7, Luigi Rizzo : > On Wed, Feb 07, 2007 at 11:25:49AM +0100, Stefan Bethke wrote: > > On Wed, February 7, 2007 09:41, Luigi Rizzo wrote: > > > My problem is that some hardware wants data structures aligned to > > > the next power of 2 with respect to their size, and the code > > > in question (the ehci driver in case you care) has hardwired > > > constants for this, and possibly wrong ones. > > > > > > It would be nice if one could write > > > > > > struct foo_desc { > > > ... > > > }; > > > > > > #define FOO_ALIGN next_power_of_2(sizeof(struct foo_desc)) > > > > > > instead of having to count fields and make guesses on the size > > > of pointers and so on. > > > > _Hacker's Delight_ contains many cool tricks, including multiple solutions > > for this, IIRC. I'll have a look tonight when I'm back home. > > funny it looks like the same code that i posted, > except that mine is functional and theirs procedural: > > my version: > > #define b2(x) ( (x) | ( (x) >> 1) ) > #define b4(x) ( b2(x) | ( b2(x) >> 2) ) > #define b8(x) ( b4(x) | ( b4(x) >> 4) ) > #define b16(x) ( b8(x) | ( b8(x) >> 8) ) > #define b32(x) (b16(x) | (b16(x) >>16) ) > #define next_power_of_2(x) (b32(x-1) + 1) > > Hacker's Delight version > > unsigned clp2(unsigned x) { > x = x - 1; > x = x | (x >> 1); > x = x | (x >> 2); > x = x | (x >> 4); > x = x | (x >> 8); > x = x | (x >>16); > return x + 1; > } You cannot use this unless you don't rewrite as a preprocessing stub. Attilio -- Peace can only be achieved by understanding - A. Einstein