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 STDOUT,"| $^X $xlate $flavour $output";
45 $ctx="%rdi"; # 1st arg
46 $inp="%rsi"; # 2nd arg
47 $num="%rdx"; # 3rd arg
49 # reassign arguments in order to produce more compact code
64 @V=($A,$B,$C,$D,$E,$T);
70 .type $func,\@function,3
77 mov %rdi,$ctx # reassigned argument
79 mov %rsi,$inp # reassigned argument
81 mov %rdx,$num # reassigned argument
108 my ($i,$a,$b,$c,$d,$e,$f,$host)=@_;
110 $code.=<<___ if ($i==0);
112 `"bswap $xi" if(!defined($host))`
115 $code.=<<___ if ($i<15);
116 lea 0x5a827999($xi,$e),$f
121 `"bswap $xi" if(!defined($host))`
130 $code.=<<___ if ($i>=15);
131 lea 0x5a827999($xi,$e),$f
132 mov `4*($j%16)`(%rsp),$xi
135 xor `4*(($j+2)%16)`(%rsp),$xi
138 xor `4*(($j+8)%16)`(%rsp),$xi
141 xor `4*(($j+13)%16)`(%rsp),$xi
146 mov $xi,`4*($j%16)`(%rsp)
151 my ($i,$a,$b,$c,$d,$e,$f)=@_;
153 my $K=($i<40)?0x6ed9eba1:0xca62c1d6;
154 $code.=<<___ if ($i<79);
156 mov `4*($j%16)`(%rsp),$xi
159 xor `4*(($j+2)%16)`(%rsp),$xi
162 xor `4*(($j+8)%16)`(%rsp),$xi
165 xor `4*(($j+13)%16)`(%rsp),$xi
170 $code.=<<___ if ($i<76);
171 mov $xi,`4*($j%16)`(%rsp)
173 $code.=<<___ if ($i==79);
187 my ($i,$a,$b,$c,$d,$e,$f)=@_;
190 lea 0x8f1bbcdc($xi,$e),$f
191 mov `4*($j%16)`(%rsp),$xi
194 xor `4*(($j+2)%16)`(%rsp),$xi
197 xor `4*(($j+8)%16)`(%rsp),$xi
200 xor `4*(($j+13)%16)`(%rsp),$xi
206 mov $xi,`4*($j%16)`(%rsp)
213 &PROLOGUE("sha1_block_data_order");
214 $code.=".align 4\n.Lloop:\n";
215 for($i=0;$i<20;$i++) { &BODY_00_19($i,@V); unshift(@V,pop(@V)); }
216 for(;$i<40;$i++) { &BODY_20_39($i,@V); unshift(@V,pop(@V)); }
217 for(;$i<60;$i++) { &BODY_40_59($i,@V); unshift(@V,pop(@V)); }
218 for(;$i<80;$i++) { &BODY_20_39($i,@V); unshift(@V,pop(@V)); }
231 xchg $E,$A # mov $E,$A
232 xchg $T,$B # mov $T,$B
233 xchg $E,$C # mov $A,$C
234 xchg $T,$D # mov $B,$D
236 lea `16*4`($inp),$inp
240 &EPILOGUE("sha1_block_data_order");
242 .asciz "SHA1 block transform for x86_64, CRYPTOGAMS by <appro\@openssl.org>"
246 # EXCEPTION_DISPOSITION handler (EXCEPTION_RECORD *rec,ULONG64 frame,
247 # CONTEXT *context,DISPATCHER_CONTEXT *disp)
255 .extern __imp_RtlVirtualUnwind
256 .type se_handler,\@abi-omnipotent
270 mov 120($context),%rax # pull context->Rax
271 mov 248($context),%rbx # pull context->Rip
273 lea .Lprologue(%rip),%r10
274 cmp %r10,%rbx # context->Rip<.Lprologue
277 mov 152($context),%rax # pull context->Rsp
279 lea .Lepilogue(%rip),%r10
280 cmp %r10,%rbx # context->Rip>=.Lepilogue
283 mov `16*4`(%rax),%rax # pull saved stack pointer
289 mov %rbx,144($context) # restore context->Rbx
290 mov %rbp,160($context) # restore context->Rbp
291 mov %r12,216($context) # restore context->R12
296 mov %rax,152($context) # restore context->Rsp
297 mov %rsi,168($context) # restore context->Rsi
298 mov %rdi,176($context) # restore context->Rdi
300 mov 40($disp),%rdi # disp->ContextRecord
301 mov $context,%rsi # context
302 mov \$154,%ecx # sizeof(CONTEXT)
303 .long 0xa548f3fc # cld; rep movsq
306 xor %rcx,%rcx # arg1, UNW_FLAG_NHANDLER
307 mov 8(%rsi),%rdx # arg2, disp->ImageBase
308 mov 0(%rsi),%r8 # arg3, disp->ControlPc
309 mov 16(%rsi),%r9 # arg4, disp->FunctionEntry
310 mov 40(%rsi),%r10 # disp->ContextRecord
311 lea 56(%rsi),%r11 # &disp->HandlerData
312 lea 24(%rsi),%r12 # &disp->EstablisherFrame
313 mov %r10,32(%rsp) # arg5
314 mov %r11,40(%rsp) # arg6
315 mov %r12,48(%rsp) # arg7
316 mov %rcx,56(%rsp) # arg8, (NULL)
317 call *__imp_RtlVirtualUnwind(%rip)
319 mov \$1,%eax # ExceptionContinueSearch
331 .size se_handler,.-se_handler
335 .rva .LSEH_begin_sha1_block_data_order
336 .rva .LSEH_end_sha1_block_data_order
337 .rva .LSEH_info_sha1_block_data_order
341 .LSEH_info_sha1_block_data_order:
347 ####################################################################
349 $code =~ s/\`([^\`]*)\`/eval $1/gem;