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
29 # Xeon P4 +65% +0% 9.9
34 if ($flavour =~ /\./) { $output = $flavour; undef $flavour; }
36 $win64=0; $win64=1 if ($flavour =~ /[nm]asm|mingw64/ || $output =~ /\.asm$/);
38 $0 =~ m/(.*[\/\\])[^\/\\]+$/; $dir=$1;
39 ( $xlate="${dir}x86_64-xlate.pl" and -f $xlate ) or
40 ( $xlate="${dir}../../perlasm/x86_64-xlate.pl" and -f $xlate) or
41 die "can't locate x86_64-xlate.pl";
43 open OUT,"| \"$^X\" $xlate $flavour $output";
46 $ctx="%rdi"; # 1st arg
47 $inp="%rsi"; # 2nd arg
48 $num="%rdx"; # 3rd arg
50 # reassign arguments in order to produce more compact code
65 @V=($A,$B,$C,$D,$E,$T);
71 .type $func,\@function,3
78 mov %rdi,$ctx # reassigned argument
80 mov %rsi,$inp # reassigned argument
82 mov %rdx,$num # reassigned argument
109 my ($i,$a,$b,$c,$d,$e,$f,$host)=@_;
111 $code.=<<___ if ($i==0);
113 `"bswap $xi" if(!defined($host))`
116 $code.=<<___ if ($i<15);
117 lea 0x5a827999($xi,$e),$f
122 `"bswap $xi" if(!defined($host))`
131 $code.=<<___ if ($i>=15);
132 lea 0x5a827999($xi,$e),$f
133 mov `4*($j%16)`(%rsp),$xi
136 xor `4*(($j+2)%16)`(%rsp),$xi
139 xor `4*(($j+8)%16)`(%rsp),$xi
142 xor `4*(($j+13)%16)`(%rsp),$xi
147 mov $xi,`4*($j%16)`(%rsp)
152 my ($i,$a,$b,$c,$d,$e,$f)=@_;
154 my $K=($i<40)?0x6ed9eba1:0xca62c1d6;
155 $code.=<<___ if ($i<79);
157 mov `4*($j%16)`(%rsp),$xi
160 xor `4*(($j+2)%16)`(%rsp),$xi
163 xor `4*(($j+8)%16)`(%rsp),$xi
166 xor `4*(($j+13)%16)`(%rsp),$xi
171 $code.=<<___ if ($i<76);
172 mov $xi,`4*($j%16)`(%rsp)
174 $code.=<<___ if ($i==79);
188 my ($i,$a,$b,$c,$d,$e,$f)=@_;
191 lea 0x8f1bbcdc($xi,$e),$f
192 mov `4*($j%16)`(%rsp),$xi
195 xor `4*(($j+2)%16)`(%rsp),$xi
198 xor `4*(($j+8)%16)`(%rsp),$xi
201 xor `4*(($j+13)%16)`(%rsp),$xi
207 mov $xi,`4*($j%16)`(%rsp)
214 &PROLOGUE("sha1_block_data_order");
215 $code.=".align 4\n.Lloop:\n";
216 for($i=0;$i<20;$i++) { &BODY_00_19($i,@V); unshift(@V,pop(@V)); }
217 for(;$i<40;$i++) { &BODY_20_39($i,@V); unshift(@V,pop(@V)); }
218 for(;$i<60;$i++) { &BODY_40_59($i,@V); unshift(@V,pop(@V)); }
219 for(;$i<80;$i++) { &BODY_20_39($i,@V); unshift(@V,pop(@V)); }
232 xchg $E,$A # mov $E,$A
233 xchg $T,$B # mov $T,$B
234 xchg $E,$C # mov $A,$C
235 xchg $T,$D # mov $B,$D
237 lea `16*4`($inp),$inp
241 &EPILOGUE("sha1_block_data_order");
243 .asciz "SHA1 block transform for x86_64, CRYPTOGAMS by <appro\@openssl.org>"
247 # EXCEPTION_DISPOSITION handler (EXCEPTION_RECORD *rec,ULONG64 frame,
248 # CONTEXT *context,DISPATCHER_CONTEXT *disp)
256 .extern __imp_RtlVirtualUnwind
257 .type se_handler,\@abi-omnipotent
271 mov 120($context),%rax # pull context->Rax
272 mov 248($context),%rbx # pull context->Rip
274 lea .Lprologue(%rip),%r10
275 cmp %r10,%rbx # context->Rip<.Lprologue
278 mov 152($context),%rax # pull context->Rsp
280 lea .Lepilogue(%rip),%r10
281 cmp %r10,%rbx # context->Rip>=.Lepilogue
284 mov `16*4`(%rax),%rax # pull saved stack pointer
290 mov %rbx,144($context) # restore context->Rbx
291 mov %rbp,160($context) # restore context->Rbp
292 mov %r12,216($context) # restore context->R12
297 mov %rax,152($context) # restore context->Rsp
298 mov %rsi,168($context) # restore context->Rsi
299 mov %rdi,176($context) # restore context->Rdi
301 mov 40($disp),%rdi # disp->ContextRecord
302 mov $context,%rsi # context
303 mov \$154,%ecx # sizeof(CONTEXT)
304 .long 0xa548f3fc # cld; rep movsq
307 xor %rcx,%rcx # arg1, UNW_FLAG_NHANDLER
308 mov 8(%rsi),%rdx # arg2, disp->ImageBase
309 mov 0(%rsi),%r8 # arg3, disp->ControlPc
310 mov 16(%rsi),%r9 # arg4, disp->FunctionEntry
311 mov 40(%rsi),%r10 # disp->ContextRecord
312 lea 56(%rsi),%r11 # &disp->HandlerData
313 lea 24(%rsi),%r12 # &disp->EstablisherFrame
314 mov %r10,32(%rsp) # arg5
315 mov %r11,40(%rsp) # arg6
316 mov %r12,48(%rsp) # arg7
317 mov %rcx,56(%rsp) # arg8, (NULL)
318 call *__imp_RtlVirtualUnwind(%rip)
320 mov \$1,%eax # ExceptionContinueSearch
332 .size se_handler,.-se_handler
336 .rva .LSEH_begin_sha1_block_data_order
337 .rva .LSEH_end_sha1_block_data_order
338 .rva .LSEH_info_sha1_block_data_order
342 .LSEH_info_sha1_block_data_order:
348 ####################################################################
350 $code =~ s/\`([^\`]*)\`/eval $1/gem;