From owner-freebsd-current@FreeBSD.ORG Wed Feb 7 10:59:41 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 1BD9016A4D2; Wed, 7 Feb 2007 10:59:34 +0000 (UTC) (envelope-from rizzo@icir.org) Received: from xorpc.icir.org (xorpc.icir.org [192.150.187.68]) by mx1.freebsd.org (Postfix) with ESMTP id B1CFE13C522; Wed, 7 Feb 2007 10:59:16 +0000 (UTC) (envelope-from rizzo@icir.org) Received: from xorpc.icir.org (localhost [127.0.0.1]) by xorpc.icir.org (8.12.11/8.13.6) with ESMTP id l17AxGvi064004; Wed, 7 Feb 2007 02:59:16 -0800 (PST) (envelope-from rizzo@xorpc.icir.org) Received: (from rizzo@localhost) by xorpc.icir.org (8.12.11/8.12.3/Submit) id l17AxGDl064003; Wed, 7 Feb 2007 02:59:16 -0800 (PST) (envelope-from rizzo) Date: Wed, 7 Feb 2007 02:59:16 -0800 From: Luigi Rizzo To: Attilio Rao Message-ID: <20070207025916.A63989@xorpc.icir.org> References: <20070207004131.A62183@xorpc.icir.org> <15241.SV0BLFJbGwk=.1170843949.squirrel@koef.zs64.net> <20070207024918.D63529@xorpc.icir.org> <3bbf2fe10702070255q58ab6c9fn2b393cfbc75a3fb@mail.gmail.com> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline User-Agent: Mutt/1.2.5.1i In-Reply-To: <3bbf2fe10702070255q58ab6c9fn2b393cfbc75a3fb@mail.gmail.com>; from attilio@freebsd.org on Wed, Feb 07, 2007 at 11:55:27AM +0100 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:59:41 -0000 On Wed, Feb 07, 2007 at 11:55:27AM +0100, Attilio Rao wrote: > 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. that's why i am using my version and wrote it functionally. luigi