public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
* [PATCH 1/12] random pt4: Create new rol32/ror32 bitops
  2005-01-21 21:41 [PATCH 0/12] random pt4: Moving and sharing code Matt Mackall
@ 2005-01-21 21:41 ` Matt Mackall
  2005-01-25 21:02   ` Denis Vlasenko
  0 siblings, 1 reply; 8+ messages in thread
From: Matt Mackall @ 2005-01-21 21:41 UTC (permalink / raw)
  To: Andrew Morton, Theodore Ts'o; +Cc: linux-kernel

Add rol32 and ror32 bitops to bitops.h

Signed-off-by: Matt Mackall <mpm@selenic.com>

Index: rnd2/drivers/char/random.c
===================================================================
--- rnd2.orig/drivers/char/random.c	2005-01-19 22:59:59.000000000 -0800
+++ rnd2/drivers/char/random.c	2005-01-20 09:18:47.000000000 -0800
@@ -374,11 +374,6 @@
 static DECLARE_WAIT_QUEUE_HEAD(random_read_wait);
 static DECLARE_WAIT_QUEUE_HEAD(random_write_wait);
 
-static inline __u32 rol32(__u32 word, int shift)
-{
-	return (word << shift) | (word >> (32 - shift));
-}
-
 #if 0
 static int debug = 0;
 module_param(debug, bool, 0644);
Index: rnd2/include/linux/bitops.h
===================================================================
--- rnd2.orig/include/linux/bitops.h	2005-01-19 22:57:54.000000000 -0800
+++ rnd2/include/linux/bitops.h	2005-01-20 09:20:24.641660815 -0800
@@ -134,4 +134,26 @@
 	return sizeof(w) == 4 ? generic_hweight32(w) : generic_hweight64(w);
 }
 
+/*
+ * rol32 - rotate a 32-bit value left
+ *
+ * @word: value to rotate
+ * @shift: bits to roll
+ */
+static inline __u32 rol32(__u32 word, int shift)
+{
+	return (word << shift) | (word >> (32 - shift));
+}
+
+/*
+ * ror32 - rotate a 32-bit value right
+ *
+ * @word: value to rotate
+ * @shift: bits to roll
+ */
+static inline __u32 ror32(__u32 word, int shift)
+{
+	return (word >> shift) | (word << (32 - shift));
+}
+
 #endif

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [PATCH 1/12] random pt4: Create new rol32/ror32 bitops
@ 2005-01-23  2:10 Chuck Ebbert
  2005-01-23  2:45 ` Matt Mackall
  2005-01-23  3:19 ` Andi Kleen
  0 siblings, 2 replies; 8+ messages in thread
From: Chuck Ebbert @ 2005-01-23  2:10 UTC (permalink / raw)
  To: Matt Mackall; +Cc: Theodore Ts'o, Andrew Morton, linux-kernel, Andi Kleen

On Fri, 21 Jan 2005 at 15:41:06 -0600 Matt Mackall wrote:

> Add rol32 and ror32 bitops to bitops.h

Can you test this patch on top of yours?  I did it on 2.6.10-ac10 but it
should apply OK.  Compile tested and booted, but only random.c is using it
in my kernel.

x86-64 could use this too...

Add i386 bitops for rol32/ror32:
Signed-off-by: Chuck Ebbert <76306.1226@compuserve.com>

--- 2.6.10-ac10/include/linux/bitops.h~orig     2005-01-22 11:31:20.130239000 -0500
+++ 2.6.10-ac10/include/linux/bitops.h  2005-01-22 11:34:55.740239000 -0500
@@ -129,6 +129,7 @@
        return sizeof(w) == 4 ? generic_hweight32(w) : generic_hweight64(w);
 }
 
+#ifndef __HAVE_ARCH_ROTATE_32
 /*
  * rol32 - rotate a 32-bit value left
  *
@@ -150,5 +151,6 @@
 {
        return (word >> shift) | (word << (32 - shift));
 }
+#endif /* ndef __HAVE_ARCH_ROTATE_32 */
 
 #endif
--- 2.6.10-ac10/include/asm-i386/bitops.h~orig  2004-08-24 05:08:39.000000000 -0400
+++ 2.6.10-ac10/include/asm-i386/bitops.h       2005-01-22 11:42:12.010239000 -0500
@@ -431,6 +431,41 @@
 #define hweight16(x) generic_hweight16(x)
 #define hweight8(x) generic_hweight8(x)
 
+#define __HAVE_ARCH_ROTATE_32
+/*
+ * rol32 - rotate a 32-bit value left
+ *
+ * @word: value to rotate
+ * @shift: bits to roll
+ */
+static inline __u32 rol32(__u32 word, int shift)
+{
+       __u32 res;
+
+       asm("roll %%cl,%0"
+           : "=r" (res)
+           : "0" (word), "c" (shift)
+           : "cc");
+       return res;
+}
+
+/*
+ * ror32 - rotate a 32-bit value right
+ *
+ * @word: value to rotate
+ * @shift: bits to roll
+ */
+static inline __u32 ror32(__u32 word, int shift)
+{
+       __u32 res;
+
+       asm("rorl %%cl,%0"
+           : "=r" (res)
+           : "0" (word), "c" (shift)
+           : "cc");
+       return res;
+}
+
 #endif /* __KERNEL__ */
 
 #ifdef __KERNEL__

Chuck

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [PATCH 1/12] random pt4: Create new rol32/ror32 bitops
  2005-01-23  2:10 Chuck Ebbert
@ 2005-01-23  2:45 ` Matt Mackall
  2005-01-23  3:19 ` Andi Kleen
  1 sibling, 0 replies; 8+ messages in thread
From: Matt Mackall @ 2005-01-23  2:45 UTC (permalink / raw)
  To: Chuck Ebbert; +Cc: Theodore Ts'o, Andrew Morton, linux-kernel, Andi Kleen

On Sat, Jan 22, 2005 at 09:10:40PM -0500, Chuck Ebbert wrote:
> On Fri, 21 Jan 2005 at 15:41:06 -0600 Matt Mackall wrote:
> 
> > Add rol32 and ror32 bitops to bitops.h
> 
> Can you test this patch on top of yours?  I did it on 2.6.10-ac10 but it
> should apply OK.  Compile tested and booted, but only random.c is using it
> in my kernel.

If I recall correctly from my testing of this about a year ago,
compilers were already generating the appropriate rol/ror
instructions. Have you checked the disassembly and found it wanting?

-- 
Mathematics is the supreme nostalgia of our time.

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [PATCH 1/12] random pt4: Create new rol32/ror32 bitops
  2005-01-23  2:10 Chuck Ebbert
  2005-01-23  2:45 ` Matt Mackall
@ 2005-01-23  3:19 ` Andi Kleen
  2005-01-23  4:13   ` Matt Mackall
  1 sibling, 1 reply; 8+ messages in thread
From: Andi Kleen @ 2005-01-23  3:19 UTC (permalink / raw)
  To: Chuck Ebbert
  Cc: Matt Mackall, Theodore Ts'o, Andrew Morton, linux-kernel,
	Andi Kleen

On Sat, Jan 22, 2005 at 09:10:40PM -0500, Chuck Ebbert wrote:
> On Fri, 21 Jan 2005 at 15:41:06 -0600 Matt Mackall wrote:
> 
> > Add rol32 and ror32 bitops to bitops.h
> 
> Can you test this patch on top of yours?  I did it on 2.6.10-ac10 but it
> should apply OK.  Compile tested and booted, but only random.c is using it
> in my kernel.

Does random really use variable rotates? For constant rotates 
gcc detects the usual C idiom and turns it transparently into
the right machine instruction.

If that's the case I would use it because it'll avoid some messy 
code everywhere.

-Andi

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [PATCH 1/12] random pt4: Create new rol32/ror32 bitops
  2005-01-23  3:19 ` Andi Kleen
@ 2005-01-23  4:13   ` Matt Mackall
  0 siblings, 0 replies; 8+ messages in thread
From: Matt Mackall @ 2005-01-23  4:13 UTC (permalink / raw)
  To: Andi Kleen; +Cc: Chuck Ebbert, Theodore Ts'o, Andrew Morton, linux-kernel

On Sun, Jan 23, 2005 at 04:19:21AM +0100, Andi Kleen wrote:
> On Sat, Jan 22, 2005 at 09:10:40PM -0500, Chuck Ebbert wrote:
> > On Fri, 21 Jan 2005 at 15:41:06 -0600 Matt Mackall wrote:
> > 
> > > Add rol32 and ror32 bitops to bitops.h
> > 
> > Can you test this patch on top of yours?  I did it on 2.6.10-ac10 but it
> > should apply OK.  Compile tested and booted, but only random.c is using it
> > in my kernel.
> 
> Does random really use variable rotates? For constant rotates 
> gcc detects the usual C idiom and turns it transparently into
> the right machine instruction.

Nope, random doesn't. The only thing I converted in my sweep that did
were CAST5 and CAST6, which are fairly unique in doing key-based
rotations. On the other hand:

typedef unsigned int __u32;

static inline __u32 rol32(__u32 word, int shift)
{
        return (word << shift) | (word >> (32 - shift));
}

int foo(int val, int rot)
{
        return rol32(val, rot);
}

With 2.95:

   0:   55                      push   %ebp
   1:   89 e5                   mov    %esp,%ebp
   3:   8b 4d 0c                mov    0xc(%ebp),%ecx
   6:   8b 45 08                mov    0x8(%ebp),%eax
   9:   d3 c0                   rol    %cl,%eax
   b:   c9                      leave
   c:   c3                      ret

With 3.3.5:

   0:   55                      push   %ebp
   1:   89 e5                   mov    %esp,%ebp
   3:   8b 45 08                mov    0x8(%ebp),%eax
   6:   8b 4d 0c                mov    0xc(%ebp),%ecx
   9:   5d                      pop    %ebp
   a:   d3 c0                   rol    %cl,%eax
   c:   c3                      ret

With gcc-snapshot:

   0:   55                      push   %ebp
   1:   89 e5                   mov    %esp,%ebp
   3:   8b 45 08                mov    0x8(%ebp),%eax
   6:   8b 4d 0c                mov    0xc(%ebp),%ecx
   9:   d3 c0                   rol    %cl,%eax
   b:   5d                      pop    %ebp
   c:   c3                      ret

So I think tweaks for x86 at least are unnecessary. 

-- 
Mathematics is the supreme nostalgia of our time.

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [PATCH 1/12] random pt4: Create new rol32/ror32 bitops
@ 2005-01-23  7:44 Chuck Ebbert
  2005-01-24 22:16 ` H. Peter Anvin
  0 siblings, 1 reply; 8+ messages in thread
From: Chuck Ebbert @ 2005-01-23  7:44 UTC (permalink / raw)
  To: Matt Mackall; +Cc: linux-kernel, Andrew Morton, Theodore Ts'o

On Sat, 22 Jan 2005 at 20:13:24 -0800 Matt Mackall wrote:

> So I think tweaks for x86 at least are unnecessary. 

 So the compiler looks for that specific sequence of instructions:

        (a << b) | (a >> (sizeof(a) * 8 - b)

and recognizes that it means rotation?  Wow.


Chuck

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [PATCH 1/12] random pt4: Create new rol32/ror32 bitops
  2005-01-23  7:44 [PATCH 1/12] random pt4: Create new rol32/ror32 bitops Chuck Ebbert
@ 2005-01-24 22:16 ` H. Peter Anvin
  0 siblings, 0 replies; 8+ messages in thread
From: H. Peter Anvin @ 2005-01-24 22:16 UTC (permalink / raw)
  To: linux-kernel

Followup to:  <200501230247_MC3-1-93FA-7A4E@compuserve.com>
By author:    Chuck Ebbert <76306.1226@compuserve.com>
In newsgroup: linux.dev.kernel
>
> On Sat, 22 Jan 2005 at 20:13:24 -0800 Matt Mackall wrote:
> 
> > So I think tweaks for x86 at least are unnecessary. 
> 
>  So the compiler looks for that specific sequence of instructions:
> 
>         (a << b) | (a >> (sizeof(a) * 8 - b)
> 
> and recognizes that it means rotation?  Wow.
> 

You know, there is a LOT of instructions like that.  x86 has a single
instruction to do:

	a = b + (c << 3) + 3600;

(Assuming a, b and c are in eax, ebx, and ecx, respecively, the
instruction is "leal 3600(%ebx,%ecx,3),%eax".)

The C compiler really needs to recognize these kinds of idioms, not
just in the source, but that occur natually as a result of code
generation and optimizations.  The compiler lingo for this is
"peephole optimization."

	-hpa

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [PATCH 1/12] random pt4: Create new rol32/ror32 bitops
  2005-01-21 21:41 ` [PATCH 1/12] random pt4: Create new rol32/ror32 bitops Matt Mackall
@ 2005-01-25 21:02   ` Denis Vlasenko
  0 siblings, 0 replies; 8+ messages in thread
From: Denis Vlasenko @ 2005-01-25 21:02 UTC (permalink / raw)
  To: Matt Mackall, Andrew Morton, Theodore Ts'o; +Cc: linux-kernel

On Friday 21 January 2005 23:41, Matt Mackall wrote:
> --- rnd2.orig/include/linux/bitops.h	2005-01-19 22:57:54.000000000 -0800
> +++ rnd2/include/linux/bitops.h	2005-01-20 09:20:24.641660815 -0800
> @@ -134,4 +134,26 @@
>  	return sizeof(w) == 4 ? generic_hweight32(w) : generic_hweight64(w);
>  }
>  
> +/*
> + * rol32 - rotate a 32-bit value left
> + *
> + * @word: value to rotate
> + * @shift: bits to roll
> + */
> +static inline __u32 rol32(__u32 word, int shift)
> +{
> +	return (word << shift) | (word >> (32 - shift));
> +}
> +
> +/*
> + * ror32 - rotate a 32-bit value right
> + *
> + * @word: value to rotate
> + * @shift: bits to roll
> + */
> +static inline __u32 ror32(__u32 word, int shift)
> +{
> +	return (word >> shift) | (word << (32 - shift));
> +}
> +
>  #endif

gcc generates surprisingly good assembly for this,
I coudn't beat it for 32bit rotations. Cool!

So you are absolutely right here with not trying
to asm optimize it.

OTOH 64bit gcc rol is worse.
--
vda


^ permalink raw reply	[flat|nested] 8+ messages in thread

end of thread, other threads:[~2005-01-25 21:15 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2005-01-23  7:44 [PATCH 1/12] random pt4: Create new rol32/ror32 bitops Chuck Ebbert
2005-01-24 22:16 ` H. Peter Anvin
  -- strict thread matches above, loose matches on Subject: below --
2005-01-23  2:10 Chuck Ebbert
2005-01-23  2:45 ` Matt Mackall
2005-01-23  3:19 ` Andi Kleen
2005-01-23  4:13   ` Matt Mackall
2005-01-21 21:41 [PATCH 0/12] random pt4: Moving and sharing code Matt Mackall
2005-01-21 21:41 ` [PATCH 1/12] random pt4: Create new rol32/ror32 bitops Matt Mackall
2005-01-25 21:02   ` Denis Vlasenko

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox