sha1-x86_64.pl revision 9b79392525856301c6f8962f189c2a32242af618
#
# ====================================================================
# Written by Andy Polyakov <appro@fy.chalmers.se> for the OpenSSL
# project. The module is, however, dual licensed under OpenSSL and
# CRYPTOGAMS licenses depending on where you obtain it. For further
# details see http://www.openssl.org/~appro/cryptogams/.
# ====================================================================
#
# sha1_block procedure for x86_64.
#
# It was brought to my attention that on EM64T compiler-generated code
# was far behind 32-bit assembler implementation. This is unlike on
# Opteron where compiler-generated code was only 15% behind 32-bit
# assembler, which originally made it hard to motivate the effort.
# There was suggestion to mechanically translate 32-bit code, but I
# dismissed it, reasoning that x86_64 offers enough register bank
# capacity to fully utilize SHA-1 parallelism. Therefore this fresh
# implementation:-) However! While 64-bit code does performs better
# on Opteron, I failed to beat 32-bit assembler on EM64T core. Well,
# x86_64 does offer larger *addressable* bank, but out-of-order core
# reaches for even more registers through dynamic aliasing, and EM64T
# core must have managed to run-time optimize even 32-bit code just as
# good as 64-bit one. Performance improvement is summarized in the
# following table:
#
# Opteron +45% +20% 6.8
# Xeon P4 +65% +0% 9.9
# Core2 +60% +10% 7.0
#
# OpenSolaris OS modifications
#
# Sun elects to use this software under the BSD license.
#
# This source originates from OpenSSL file sha1-x86_64.pl at
# (presumably for future OpenSSL release 0.9.8h), with these changes:
#
# 1. Added perl "use strict" and declared variables.
#
# /usr/include/sys/asm_linkage.h, .ident keywords, and lint(1B) guards.
#
# 3. Removed x86_64-xlate.pl script (not needed for as(1) or gas(1) assemblers).
#
use strict;
my $output = shift;
open STDOUT,">$output";
#
# void sha1_block_data_order(SHA1_CTX *ctx, const void *inpp, size_t blocks);
#
# Arguments:
# reassign arguments in order to produce more compact code
$ctx="%r8";
$inp="%r9";
$num="%r10";
# Temporaries:
$xi="%eax";
$t0="%ebx";
$t1="%ecx";
# State information from SHA-1 context:
$A="%edx";
$B="%esi";
$C="%edi";
$D="%ebp";
$E="%r11d";
# Temporary:
$T="%r12d";
@V=($A,$B,$C,$D,$E,$T);
sub PROLOGUE {
my $func=shift;
push %rbx
push %rbp
push %r12
and \$-64,%rsp
}
sub EPILOGUE {
my $func=shift;
pop %r12
pop %rbp
pop %rbx
}
sub BODY_00_19 {
my ($i,$a,$b,$c,$d,$e,$f,$host)=@_;
my $j=$i+1;
`"bswap $xi" if(!defined($host))`
mov $a,$e
xor $d,$t0
`"bswap $xi" if(!defined($host))`
rol \$5,$e
and $b,$t0
add $e,$f
xor $d,$t0
rol \$30,$b
mov $a,$e
xor $d,$t0
rol \$5,$e
and $b,$t0
add $e,$f
xor $d,$t0
rol \$30,$b
}
sub BODY_20_39 {
my ($i,$a,$b,$c,$d,$e,$f)=@_;
my $j=$i+1;
my $K=($i<40)?"0x6ed9eba1":"-0x359d3e2a";
mov $a,$e
xor $b,$t0
rol \$5,$e
xor $d,$t0
add $e,$f
rol \$30,$b
mov $a,$e
xor $b,$t0
rol \$5,$e
xor $d,$t0
add $e,$f
rol \$30,$b
}
sub BODY_40_59 {
my ($i,$a,$b,$c,$d,$e,$f)=@_;
my $j=$i+1;
mov $a,$e
and $c,$t0
or $c,$t1
rol \$5,$e
and $d,$t1
add $e,$f
rol \$30,$b
}
#
# Execution begins here
#
#if defined(lint) || defined(__lint)
/* ARGSUSED */
{
}
#else
#include <sys/asm_linkage.h>
&PROLOGUE("sha1_block_data_order");
$code.=".align 4\n.Lloop:\n";
for(;$i<40;$i++) { &BODY_20_39($i,@V); unshift(@V,pop(@V)); }
for(;$i<60;$i++) { &BODY_40_59($i,@V); unshift(@V,pop(@V)); }
for(;$i<80;$i++) { &BODY_20_39($i,@V); unshift(@V,pop(@V)); }
xchg $E,$A # mov $E,$A
xchg $T,$B # mov $T,$B
xchg $E,$C # mov $A,$C
xchg $T,$D # mov $B,$D
# mov $C,$E
sub \$1,$num
&EPILOGUE("sha1_block_data_order");
.asciz "SHA1 block transform for x86_64, CRYPTOGAMS by <appro\@openssl.org>"
#endif /* lint || __lint */
####################################################################
print $code;
close STDOUT;