public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
From: Sandy Harris <sandy@storm.ca>
To: Karel Kulhavy <clock@atrey.karlin.mff.cuni.cz>
Cc: linux-kernel@vger.kernel.org
Subject: Re: random.c patch
Date: Sun, 17 Dec 2000 20:08:41 -0500	[thread overview]
Message-ID: <3A3D6399.C213729E@storm.ca> (raw)
In-Reply-To: <20001217224452.A8635@atrey.karlin.mff.cuni.cz>

Karel Kulhavy wrote:
> 
> There are several places where the rotation yields garbage according to ANSI
> C definition when called with 0 bit position argument.
> 
> diff -Pur linux_reference/drivers/char/random.c linux/drivers/char/random.c
> --- linux_reference/drivers/char/random.c       Wed Jul 19 00:58:13 2000
> +++ linux/drivers/char/random.c Sun Dec 17 22:42:59 2000
> @@ -411,7 +411,7 @@
>  #if (!defined (__i386__))
>  extern inline __u32 rotate_left(int i, __u32 word)
>  {
> -       return (word << i) | (word >> (32 - i));
> +       return (word << i) | (word >> ((-i)&31));
> 
If the calling code guarantees 0 < i < 32, then the patch is unnecessary.
In the kernel I have to hand (2.2.6), grepping gives:

	r->input_rotate = j & 31 ;

and then both calls to the function use r->input_rotate as the first argument,
so the guarantee from higher level code seems to be 0 <= i < 32, in which case
your patch seems needed.

On the other hand, why not put the &= inside the function with something
like:

 extern inline __u32 rotate_left(int i, __u32 word)
{
	switch( i &= 31 )	{ /* cheap version of i %= 32 */
		case 0 :
			return word ;
		default :
			return (word << i) | (word >> (32 - i)) ;
	}

or some faster alternative along the lines of:

{
	i &= 31 ;
	return( i ? ((word << i) | (word >> (32 - i))) : word ) ;

This works right for any i. Yours fails for i >= 32 unless you make it:

       return (word << (i&31)) | (word >> ((-i)&31));

Whichever way is fastest is fine, but I'd advocate doing the range
manipulation inside the function in any case. Why trust the caller?
If the code is maintained or modified, you're almost guaranteed to
be called with bad argumantes in some version.
-
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@vger.kernel.org
Please read the FAQ at http://www.tux.org/lkml/

      reply	other threads:[~2000-12-18  1:41 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2000-12-17 21:44 random.c patch Karel Kulhavy
2000-12-18  1:08 ` Sandy Harris [this message]

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=3A3D6399.C213729E@storm.ca \
    --to=sandy@storm.ca \
    --cc=clock@atrey.karlin.mff.cuni.cz \
    --cc=linux-kernel@vger.kernel.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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox