From: Stephen Hemminger <stephen@networkplumber.org>
To: George Spelvin <lkml@sdf.org>
Cc: linux-kernel@vger.kernel.org,
Hannes Frederic Sowa <hannes@stressinduktion.org>
Subject: Re: [RFC PATCH v1 09/50] <linux/random.h> prandom_u32_max() for power-of-2 ranges
Date: Sat, 28 Mar 2020 10:32:29 -0700 [thread overview]
Message-ID: <20200328103229.132a047f@hermes.lan> (raw)
In-Reply-To: <202003281643.02SGh9n2025458@sdf.org>
On Sat, 16 Mar 2019 02:32:04 -0400
George Spelvin <lkml@sdf.org> wrote:
> +static inline u32 prandom_u32_max(u32 range)
> {
> - return (u32)(((u64) prandom_u32() * ep_ro) >> 32);
> + /*
> + * If the range is a compile-time constant power of 2, then use
> + * a simple shift. This is mathematically equivalent to the
> + * multiplication, but GCC 8.3 doesn't optimize that perfectly.
> + *
> + * We could do an AND with a mask, but
> + * 1) The shift is the same speed on a decent CPU,
> + * 2) It's generally smaller code (smaller immediate), and
> + * 3) Many PRNGs have trouble with their low-order bits;
> + * using the msbits is generaly preferred.
> + */
> + if (__builtin_constant_p(range) && (range & (range - 1)) == 0)
> + return prandom_u32() / (u32)(0x100000000 / range);
> + else
> + return reciprocal_scale(prandom_u32(), range);
The optimization is good, but I don't thin that the compiler
is able to propogate the constant property into the function.
Did you actually check the generated code.
next prev parent reply other threads:[~2020-03-28 17:32 UTC|newest]
Thread overview: 3+ messages / expand[flat|nested] mbox.gz Atom feed top
2019-03-16 6:32 [RFC PATCH v1 09/50] <linux/random.h> prandom_u32_max() for power-of-2 ranges George Spelvin
2020-03-28 17:32 ` Stephen Hemminger [this message]
2020-03-28 18:07 ` George Spelvin
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=20200328103229.132a047f@hermes.lan \
--to=stephen@networkplumber.org \
--cc=hannes@stressinduktion.org \
--cc=linux-kernel@vger.kernel.org \
--cc=lkml@sdf.org \
/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.