From owner-freebsd-current@FreeBSD.ORG Wed Feb 7 10:25:29 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 85B5A16A401 for ; Wed, 7 Feb 2007 10:25:29 +0000 (UTC) (envelope-from bakul@bitblocks.com) Received: from mail.bitblocks.com (ns1.bitblocks.com [64.142.15.60]) by mx1.freebsd.org (Postfix) with ESMTP id 7145F13C4AC for ; Wed, 7 Feb 2007 10:25:29 +0000 (UTC) (envelope-from bakul@bitblocks.com) Received: from bitblocks.com (localhost.bitblocks.com [127.0.0.1]) by mail.bitblocks.com (Postfix) with ESMTP id 211C95B2E; Wed, 7 Feb 2007 02:01:01 -0800 (PST) To: Luigi Rizzo In-reply-to: Your message of "Wed, 07 Feb 2007 00:41:31 PST." <20070207004131.A62183@xorpc.icir.org> Date: Wed, 07 Feb 2007 02:01:01 -0800 From: Bakul Shah Message-Id: <20070207100101.211C95B2E@mail.bitblocks.com> Cc: 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:25:30 -0000 > #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) ... > But i was wondering if there is some simpler code which > can still be computed as a compile-time constant ? If this works, why bother? let the compiler work harder!