public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
From: Andrew Morton <akpm@osdl.org>
To: "Robert P. J. Day" <rpjday@mindspring.com>
Cc: Linux kernel mailing list <linux-kernel@vger.kernel.org>,
	dhowells@redhat.com
Subject: Re: [PATCH] Add a rounddown_pow_of_two() macro to log2.h.
Date: Thu, 25 Jan 2007 19:20:44 -0800	[thread overview]
Message-ID: <20070125192044.c9c2a093.akpm@osdl.org> (raw)
In-Reply-To: <Pine.LNX.4.64.0701250426400.7715@CPE00045a9c397f-CM001225dbafb6>

On Thu, 25 Jan 2007 04:32:12 -0500 (EST)
"Robert P. J. Day" <rpjday@mindspring.com> wrote:

> 
>   In the same way that include/linux/log2.h defines the
> roundup_pow_of_two() macro, define the rounddown_pow_of_two() macro so
> peopls can stop re-implementing this operation using a loop.
> 
> Signed-off-by: Robert P. J. Day <rpjday@mindspring.com>
> 
> ---
> 
>   compile tested on x86 using "make allyesconfig", but there wasn't
> much chance of the build failing anyway since the patch only adds the
> macro definition, it doesn't change any existing code to use it.
> those patches will be submitted later, bit by bit.
> 
> diff --git a/include/linux/log2.h b/include/linux/log2.h
> index d02e1a5..6cf7081 100644
> --- a/include/linux/log2.h
> +++ b/include/linux/log2.h
> @@ -52,6 +63,15 @@ unsigned long __roundup_pow_of_two(unsigned long n)
>  	return 1UL << fls_long(n - 1);
>  }
> 
> +/*
> + * round down to nearest power of two
> + */
> +static inline __attribute__((const))
> +unsigned long __rounddown_pow_of_two(unsigned long n)
> +{
> +	return 1UL << (fls_long(n) - 1);
> +}

So __rounddown_pow_of_two(16) returns 8?

>  /**
>   * ilog2 - log of base 2 of 32-bit or a 64-bit unsigned value
>   * @n - parameter
> @@ -154,4 +174,20 @@ unsigned long __roundup_pow_of_two(unsigned long n)
>  	__roundup_pow_of_two(n)			\
>   )
> 
> +/**
> + * rounddown_pow_of_two - round the given value down to nearest power of two
> + * @n - parameter
> + *
> + * round the given value down to the nearest power of two
> + * - the result is undefined when n == 0
> + * - this can be used to initialise global variables from constant data
> + */
> +#define rounddown_pow_of_two(n)			\
> +(						\
> +	__builtin_constant_p(n) ? (		\
> +		(n == 1) ? 0 :			\
> +		(1UL << ilog2(n)) :		\
> +	__rounddown_pow_of_two(n)		\
> + )

But (1UL << ilog2(16)) returns 16?


And, afiact, your __rounddown_pow_of_two() is basically equivalent to (1UL
<< ilog2(n)) anyway.  So a suitable (and less buggy) implementation might be

static inline unsigned long rounddown_pow_of_two(unsigned long n)
{
	return (n == 1) ? 0 : (1UL << ilog2(n));
}

But I'm not sure.  Please create a userspace test harness to test this
patch.

  reply	other threads:[~2007-01-26  3:20 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2007-01-25  9:32 [PATCH] Add a rounddown_pow_of_two() macro to log2.h Robert P. J. Day
2007-01-26  3:20 ` Andrew Morton [this message]
2007-01-26  7:24   ` Robert P. J. Day
2007-01-26 15:57     ` Kyle Moffett

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20070125192044.c9c2a093.akpm@osdl.org \
    --to=akpm@osdl.org \
    --cc=dhowells@redhat.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=rpjday@mindspring.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox