3 # ====================================================================
4 # Written by Andy Polyakov <appro@fy.chalmers.se> for the OpenSSL
5 # project. The module is, however, dual licensed under OpenSSL and
6 # CRYPTOGAMS licenses depending on where you obtain it. For further
7 # details see http://www.openssl.org/~appro/cryptogams/.
8 # ====================================================================
10 # sha1_block procedure for x86_64.
12 # It was brought to my attention that on EM64T compiler-generated code
13 # was far behind 32-bit assembler implementation. This is unlike on
14 # Opteron where compiler-generated code was only 15% behind 32-bit
15 # assembler, which originally made it hard to motivate the effort.
16 # There was suggestion to mechanically translate 32-bit code, but I
17 # dismissed it, reasoning that x86_64 offers enough register bank
18 # capacity to fully utilize SHA-1 parallelism. Therefore this fresh
19 # implementation:-) However! While 64-bit code does performs better
20 # on Opteron, I failed to beat 32-bit assembler on EM64T core. Well,
21 # x86_64 does offer larger *addressable* bank, but out-of-order core
22 # reaches for even more registers through dynamic aliasing, and EM64T
23 # core must have managed to run-time optimize even 32-bit code just as
24 # good as 64-bit one. Performance improvement is summarized in the
27 # gcc 3.4 32-bit asm cycles/byte
28 # Opteron +45% +20% 6.8
33 $0 =~ m/(.*[\/\\])[^\/\\]+$/; $dir=$1;
34 ( $xlate="${dir}x86_64-xlate.pl" and -f $xlate ) or
35 ( $xlate="${dir}../../perlasm/x86_64-xlate.pl" and -f $xlate) or
36 die "can't locate x86_64-xlate.pl";
38 open STDOUT,"| $^X $xlate $output";
40 $ctx="%rdi"; # 1st arg
41 $inp="%rsi"; # 2nd arg
42 $num="%rdx"; # 3rd arg
44 # reassign arguments in order to produce more compact code
59 @V=($A,$B,$C,$D,$E,$T);
65 .type $func,\@function,3
72 mov %rdi,$ctx # reassigned argument
74 mov %rsi,$inp # reassigned argument
76 mov %rdx,$num # reassigned argument
100 my ($i,$a,$b,$c,$d,$e,$f,$host)=@_;
102 $code.=<<___ if ($i==0);
104 `"bswap $xi" if(!defined($host))`
107 $code.=<<___ if ($i<15);
108 lea 0x5a827999($xi,$e),$f
113 `"bswap $xi" if(!defined($host))`
122 $code.=<<___ if ($i>=15);
123 lea 0x5a827999($xi,$e),$f
124 mov `4*($j%16)`(%rsp),$xi
127 xor `4*(($j+2)%16)`(%rsp),$xi
130 xor `4*(($j+8)%16)`(%rsp),$xi
133 xor `4*(($j+13)%16)`(%rsp),$xi
138 mov $xi,`4*($j%16)`(%rsp)
143 my ($i,$a,$b,$c,$d,$e,$f)=@_;
145 my $K=($i<40)?0x6ed9eba1:0xca62c1d6;
146 $code.=<<___ if ($i<79);
148 mov `4*($j%16)`(%rsp),$xi
151 xor `4*(($j+2)%16)`(%rsp),$xi
154 xor `4*(($j+8)%16)`(%rsp),$xi
157 xor `4*(($j+13)%16)`(%rsp),$xi
162 $code.=<<___ if ($i<76);
163 mov $xi,`4*($j%16)`(%rsp)
165 $code.=<<___ if ($i==79);
179 my ($i,$a,$b,$c,$d,$e,$f)=@_;
182 lea 0x8f1bbcdc($xi,$e),$f
183 mov `4*($j%16)`(%rsp),$xi
186 xor `4*(($j+2)%16)`(%rsp),$xi
189 xor `4*(($j+8)%16)`(%rsp),$xi
192 xor `4*(($j+13)%16)`(%rsp),$xi
198 mov $xi,`4*($j%16)`(%rsp)
205 &PROLOGUE("sha1_block_data_order");
206 $code.=".align 4\n.Lloop:\n";
207 for($i=0;$i<20;$i++) { &BODY_00_19($i,@V); unshift(@V,pop(@V)); }
208 for(;$i<40;$i++) { &BODY_20_39($i,@V); unshift(@V,pop(@V)); }
209 for(;$i<60;$i++) { &BODY_40_59($i,@V); unshift(@V,pop(@V)); }
210 for(;$i<80;$i++) { &BODY_20_39($i,@V); unshift(@V,pop(@V)); }
223 xchg $E,$A # mov $E,$A
224 xchg $T,$B # mov $T,$B
225 xchg $E,$C # mov $A,$C
226 xchg $T,$D # mov $B,$D
228 lea `16*4`($inp),$inp
232 &EPILOGUE("sha1_block_data_order");
234 .asciz "SHA1 block transform for x86_64, CRYPTOGAMS by <appro\@openssl.org>"
237 ####################################################################
239 $code =~ s/\`([^\`]*)\`/eval $1/gem;