Netdev List
 help / color / mirror / Atom feed
From: Joe Perches <joe@perches.com>
To: Daniel Borkmann <dborkman@redhat.com>
Cc: davem@davemloft.net, netdev@vger.kernel.org,
	linux-kernel@vger.kernel.org, Theodore Ts'o <tytso@mit.edu>
Subject: Re: [PATCH net-next v2 1/2] random: add prandom_u32_range and prandom_u32_max helpers
Date: Wed, 04 Sep 2013 08:54:59 -0700	[thread overview]
Message-ID: <1378310099.1787.9.camel@joe-AO722> (raw)
In-Reply-To: <1378298247-29364-2-git-send-email-dborkman@redhat.com>

On Wed, 2013-09-04 at 14:37 +0200, Daniel Borkmann wrote:
> We have implemented the same function over and over, so introduce
> generic helpers that unify these implementations in order to migrate
> such code to use them. Make the API similarly to randomize_range()
> for consistency. prandom_u32_range() generates numbers in [start, end]
> interval and prandom_u32_max() generates numbers in [0, end] interval.

I think these helpers can in many cases cause
poorer compiler generated object code.

> +/**
> + * prandom_u32_range - return a random number in interval [start, end]
> + * @start: lower interval endpoint
> + * @end: higher interval endpoint
> + *
> + * Returns a number that is in the given interval:
> + *
> + *     [...... <range> .....]
> + *   start                  end
> + *
> + * Callers need to make sure that start <= end. Note that the result
> + * depends on PRNG being well distributed in [0, ~0U] space. Here we
> + * use maximally equidistributed combined Tausworthe generator.
> + */
> +static inline u32 prandom_u32_range(u32 start, u32 end)
> +{
> +	return (u32)(((u64) prandom_u32() * (end + 1 - start)) >> 32) + start;
> +}

This is effectively:

	return (prandom_u32() % (end - start)) + start;

and if start and end are constant, gcc can optimize the
division by constant to a 32 bit multiply/shift/add.

I think if you add __builtin_constant_p tests for start
and end and expand the code a little you can still get
the optimizations done.

  reply	other threads:[~2013-09-04 15:54 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2013-09-04 12:37 [PATCH net-next v2 0/2] prandom_u32_range, prandom_u32_max helpers Daniel Borkmann
2013-09-04 12:37 ` [PATCH net-next v2 1/2] random: add prandom_u32_range and " Daniel Borkmann
2013-09-04 15:54   ` Joe Perches [this message]
2013-09-04 12:37 ` [PATCH net-next v2 2/2] net: migrate direct users to prandom_u32_max Daniel Borkmann
2013-09-04 12:52   ` Eric Dumazet
2013-09-04 12:58     ` 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=1378310099.1787.9.camel@joe-AO722 \
    --to=joe@perches.com \
    --cc=davem@davemloft.net \
    --cc=dborkman@redhat.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=netdev@vger.kernel.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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox