www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Bit rotation intrinsics

reply "Todor Totev" <umbra.tenebris list.ru> writes:
Hello,
I'm playing with some crypto algorithms, mainly to understand what's
going on :-)
I'm trying to implement SHA-2 & friends algorithms in D.
But they are using bit rotations extensively!
So I have 3 choices:
  * emulate rol/ror with shifts & ors - i didn't like this because
i feel uncomfortable replacing one processor instruction with
multi-line function :-(
  * use inline assembler - well, the truth is that I didn't understand
how exactly to use this beast but I love to self-delude that it'll
hurt code portability ;-)
  * kindly request compiler intrinsics for these operations in D.
What is your opinion? Obviously this cannot be done for 1.0 but is it
easy enough to be done at all? Obviously bit rotation is heavily used
in crypto code but it is used occasionally for other purposes too.
And frankly I think that the compiler knows better than me how to order
the instructions for best results so this is a pro-intrinsics too.
Regards,
Todor
Dec 29 2006
parent Don Clugston <dac nospam.com.au> writes:
Todor Totev wrote:
 
 Hello,
 I'm playing with some crypto algorithms, mainly to understand what's
 going on :-)
 I'm trying to implement SHA-2 & friends algorithms in D.
 But they are using bit rotations extensively!
 So I have 3 choices:
  * emulate rol/ror with shifts & ors - i didn't like this because
 i feel uncomfortable replacing one processor instruction with
 multi-line function :-(
  * use inline assembler - well, the truth is that I didn't understand
 how exactly to use this beast but I love to self-delude that it'll
 hurt code portability ;-)
  * kindly request compiler intrinsics for these operations in D.
 What is your opinion? Obviously this cannot be done for 1.0 but is it
 easy enough to be done at all? Obviously bit rotation is heavily used
 in crypto code but it is used occasionally for other purposes too.
 And frankly I think that the compiler knows better than me how to order
 the instructions for best results so this is a pro-intrinsics too.
Yes, and this has been requested before. Walter has said that intrinsics are quite tricky to implement but that it probably will happen eventually. Best approach is probably to use inline asm where available, but resort to emulation where it doesn't. Something like this (untested): uint ror(uint x) { version (D_InlineAsm_X86) { asm { mov eax, x; ror eax, 1; } } else { static assert(0, "Use shifts & OR"); } } ushort ror(ushort x) { asm { movzx eax, x; // zero extend in case someone assigns it to an int; // might be unnecessary ror ax, 1; } } etc.
Jan 02 2007