mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Hannes Frederic Sowa <hannes@stressinduktion.org>
To: George Spelvin <linux@horizon.com>,
	davem@davemloft.net, dborkman@redhat.com, shemminger@osdl.org,
	tytso@mit.edu
Cc: linux-kernel@vger.kernel.org
Subject: Re: [PATCH 5/7] lib/random32.c: Make prandom_u32_max efficient for powers of 2
Date: Mon, 09 Jun 2014 03:30:27 -0700	[thread overview]
Message-ID: <1402309827.7242.126672901.053EFD6E@webmail.messagingengine.com> (raw)
In-Reply-To: <20140608204800.7610.qmail@ns.horizon.com>

Thanks for your detailed explanation!

On Sun, Jun 8, 2014, at 13:48, George Spelvin wrote:
> Thank you for your comments!
> 
> > Have you checked assembler output if this helps anything at all? Constant
> > propagation in the compiler should be able to figure that out all by
> > itself. The only places I use __builtin_constant_p today are where I
> > also make use of inline assembler.
> 
> Yes, I did.  (I'll expand the commit comment for v2; my bad.)
> 
> It seems that GCC isn't smart enough to reduce this to a single shift.
> With the multiply and reduce, the code looks like:
>         call    prandom_u32
>         xorl    %edx, %edx
>         shldl   $4, %eax, %edx
>         movl    %edx, %eax
> 
> Instead of the hoped-for
>         call    prandom_u32
> 	shrl	$28, %eax

On x86_64 I get the above result. Seems like gcc doesn't see the downcast to u32 far enough ahead and stays in DI mode on i386, thus the shldl. It shouldn't matter that much... ;)

> Converting to a single mask is something the compiler can't do,
> because it doesn't understand that using the lsbits instead of the
> msbits is okay.

Yep, sure.

> With the mask, it turns into the spectacularly simple:
>         call    prandom_u32
>         andl    $15, %eax
> 
> An interesting question is which is preferred in general.
> 
> The AND allows non-constant powers of 2 without requiring CLZ.  But I
> don't recall seeing that actually happen anywhere.  And the shift allows
> a smaller encoding (8-bit rather than 32-bit immediate constant) when
> the power of 2 is known at compile time and is larger than 128 (for
> example, PAGE_SIZE).

I actually don't know if folding logic in gcc is so enhanced to see that coming. ;)
Would be interesting tough, maybe I'll try that later.

> Me, I thought it was in the noise and not worth stressing about,
> but I also understand the hackers's urge for maximum tweaking.

Totally ok. ;)

I don't have any problems with the patch, although such a detailed changelog would be nice + some approx. numbers of how many times we run into the new optimization.

Bye,
Hannes

  reply	other threads:[~2014-06-09 10:30 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
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 [this message]
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=1402309827.7242.126672901.053EFD6E@webmail.messagingengine.com \
    --to=hannes@stressinduktion.org \
    --cc=davem@davemloft.net \
    --cc=dborkman@redhat.com \
    --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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®