random pt4: Create new rol32/ror32 bitops
Quoted 1 line · 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:
include/linux/bitops.h 2005-01-22 11:34:55.740239000 -0500 renamed from include/linux/bitops.h~orig 2005-01-22 11:31:20.130239000 -0500 +2 -0
@@ -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
include/asm-i386/bitops.h 2005-01-22 11:42:12.010239000 -0500 renamed from include/asm-i386/bitops.h~orig 2004-08-24 05:08:39.000000000 -0400 +35 -0
@@ -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
Quoted 7 lines · Chuck Ebbert
Quoted 1 line · On Fri, 21 Jan 2005 at 15:41:06 -0600 Matt Mackall wrote:
Add rol32 and ror32 bitops to
bitops.hCan 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.cis 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?
Signature · 1 line
Mathematics is the supreme nostalgia of our time.
Quoted 7 lines · Chuck Ebbert
Quoted 1 line · On Fri, 21 Jan 2005 at 15:41:06 -0600 Matt Mackall wrote:
Add rol32 and ror32 bitops to
bitops.hCan 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.cis 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
Quoted 5 lines · Andi Kleen
Quoted 4 lines · Chuck Ebbert
Quoted 1 line · On Fri, 21 Jan 2005 at 15:41:06 -0600 Matt Mackall wrote:
Add rol32 and ror32 bitops to
bitops.h
Quoted 7 lines
Quoted 3 lines
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.cis 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.
Signature · 1 line
Mathematics is the supreme nostalgia of our time.