All of lore.kernel.org
 help / color / mirror / Atom feed
From: Daniel Borkmann <dborkman@redhat.com>
To: George Spelvin <linux@horizon.com>
Cc: davem@davemloft.net, shemminger@osdl.org, tytso@mit.edu,
	linux-kernel@vger.kernel.org, hannes@stressinduktion.org
Subject: Re: [PATCH 5/7] lib/random32.c: Make prandom_u32_max efficient for powers of 2
Date: Sun, 08 Jun 2014 14:03:33 +0200	[thread overview]
Message-ID: <53945115.7060600@redhat.com> (raw)
In-Reply-To: <20140607082805.10120.qmail@ns.horizon.com>

On 06/07/2014 10:28 AM, George Spelvin wrote:
> The multiply-and-shift is efficient in the general case, but slower than
> a simple bitwise AND if the range is a power of 2.  Make the function
> handle this case so callers don't have to worry about micro-optimizing it.
>
> Signed-off-by: George Spelvin <linux@horizon.com>
> ---
>   include/linux/random.h | 14 +++++++++++++-
>   1 file changed, 13 insertions(+), 1 deletion(-)
>
> diff --git a/include/linux/random.h b/include/linux/random.h
> index 57fbbffd..e1f3ec9a 100644
> --- a/include/linux/random.h
> +++ b/include/linux/random.h
> @@ -47,11 +47,23 @@ void prandom_bytes_state(struct rnd_state *state, void *buf, int nbytes);
>    * generator, that is, prandom_u32(). This is useful when requesting a
>    * random index of an array containing ep_ro elements, for example.
>    *
> + * If ep_ro is a power of 2 known at compile time, a modulo operation
> + * reduces to a simple mask to extract the low order bits.  Otherwise,
> + * it uses a multiply and shift, which is faster than a general modulus.
> + *
>    * Returns: pseudo-random number in interval [0, ep_ro)
>    */
>   static inline u32 prandom_u32_max(u32 ep_ro)
>   {
> -	return (u32)(((u64) prandom_u32() * ep_ro) >> 32);
> +	/*
> +	 * Instead of just __builtin_constant_p(ep_ro), this test is
> +	 * "is it known at compile time that ep_ro is a power of 2?", and
> +	 * can in theory handle the case that it's an unknown power of 2.
> +	 */
> +	if (__builtin_constant_p(ep_ro & (ep_ro-1)) && !(ep_ro & (ep_ro-1)))
> +		return prandom_u32() & (ep_ro-1);
> +	else
> +		return (u32)((u64)prandom_u32() * ep_ro >> 32);

Okay, I guess it's fine since we expect a random number here anyway, so
it doesn't matter much how we get to the result; otherwise I'd have
complained as both function don't do the same thing. Please leave some
whitespace, i.e. I mean "ep_ro-1" -> "ep_ro - 1".

Btw, there are many other users which hard code prandom_u32_max() resp.
reciprocal_scale() function in the source tree. If you have some cycles,
feel free to migrate them and let them use these functions.

>   }
>
>   /*
>

  reply	other threads:[~2014-06-08 12:03 UTC|newest]

Thread overview: 31+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2014-06-07  8:18 [PATCH 0/7] random32: Various minor cleanups George Spelvin
2014-06-07  8:19 ` [PATCH 1/7] lib/random32.c: Mark self-test data as __initconst George Spelvin
2014-06-08 12:06   ` Daniel Borkmann
2014-06-07  8:20 ` [PATCH 2/7] lib/random32.c: Remove excess calls to prandom_u32_state in initialization George Spelvin
2014-06-08 12:11   ` Daniel Borkmann
2014-06-08 12:19     ` George Spelvin
2014-06-07  8:22 ` [PATCH 3/7] lib/random32.c: Replace an #ifdef with a stub prandom_state_selftest() George Spelvin
2014-06-08 12:16   ` Daniel Borkmann
2014-06-08 12:27     ` George Spelvin
2014-06-07  8:25 ` [PATCH 4/7] lib/random32.c: Use <asm/unaligned.h> instead of hand-rolling it George Spelvin
2014-06-08 12:25   ` Daniel Borkmann
2014-06-08 12:40     ` George Spelvin
2014-06-08 20:26       ` Hannes Frederic Sowa
2014-06-10 15:13       ` Daniel Borkmann
2014-06-07  8:28 ` [PATCH 5/7] lib/random32.c: Make prandom_u32_max efficient for powers of 2 George Spelvin
2014-06-08 12:03   ` Daniel Borkmann [this message]
2014-06-08 17:34   ` Hannes Frederic Sowa
2014-06-08 20:02     ` Daniel Borkmann
2014-06-09  0:28       ` George Spelvin
2014-06-08 20:48     ` George Spelvin
2014-06-09 10:30       ` Hannes Frederic Sowa
2014-06-07  8:28 ` [PATCH 6/7] lib/random32.c: Randomize timeout to the millisecond, not the second George Spelvin
2014-06-08 10:43   ` Daniel Borkmann
2014-06-08 11:30     ` George Spelvin
2014-06-08 12:28       ` Daniel Borkmann
2014-06-08 12:42         ` George Spelvin
2014-06-08 20:01           ` Daniel Borkmann
2014-06-07  8:31 ` [PATCH 7/7] lib/random32.c: Remove redundant U suffixes on integers George Spelvin
2014-06-08 10:36   ` Daniel Borkmann
2014-06-08 11:14     ` George Spelvin
2014-06-08 12:05       ` Daniel Borkmann

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=53945115.7060600@redhat.com \
    --to=dborkman@redhat.com \
    --cc=davem@davemloft.net \
    --cc=hannes@stressinduktion.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux@horizon.com \
    --cc=shemminger@osdl.org \
    --cc=tytso@mit.edu \
    /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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.