sha512-ia64.pl: 15-20% performance improvement.

This commit is contained in:
Andy Polyakov 2012-07-15 13:36:25 +00:00
parent e09039c01c
commit 46a2b3387a

View File

@ -1,7 +1,7 @@
#!/usr/bin/env perl #!/usr/bin/env perl
# #
# ==================================================================== # ====================================================================
# Written by Andy Polyakov <appro@fy.chalmers.se> for the OpenSSL # Written by Andy Polyakov <appro@openssl.org> for the OpenSSL
# project. The module is, however, dual licensed under OpenSSL and # project. The module is, however, dual licensed under OpenSSL and
# CRYPTOGAMS licenses depending on where you obtain it. For further # CRYPTOGAMS licenses depending on where you obtain it. For further
# details see http://www.openssl.org/~appro/cryptogams/. # details see http://www.openssl.org/~appro/cryptogams/.
@ -48,16 +48,22 @@
# because on Itanium 1 stall on MM result is accompanied by # because on Itanium 1 stall on MM result is accompanied by
# pipeline flush, which takes 6 cycles:-( # pipeline flush, which takes 6 cycles:-(
# #
# Resulting performance numbers for 900MHz Itanium 2 system: # June 2012
# #
# The 'numbers' are in 1000s of bytes per second processed. # Improve performance by 15-20%. Note about "rules of engagement"
# type 16 bytes 64 bytes 256 bytes 1024 bytes 8192 bytes # above. Contemporary cores are equipped with additional shifter,
# sha1(*) 6210.14k 20376.30k 52447.83k 85870.05k 105478.12k # so that they should perform even better than below, presumably
# sha256 7476.45k 20572.05k 41538.34k 56062.29k 62093.18k # by ~10%.
# sha512 4996.56k 20026.28k 47597.20k 85278.79k 111501.31k
# #
# (*) SHA1 numbers are for HP-UX compiler and are presented purely ######################################################################
# for reference purposes. I bet it can improved too... # Current performance in cycles per processed byte for Itanium 2
# pre-9000 series [little-endian] system:
#
# SHA1(*) 5.7
# SHA256 12.6
# SHA512 6.7
#
# (*) SHA1 result is presented purely for reference purposes.
# #
# To generate code, pass the file name with either 256 or 512 in its # To generate code, pass the file name with either 256 or 512 in its
# name and compiler flags. # name and compiler flags.
@ -106,8 +112,8 @@ if (!defined($big_endian))
{ $big_endian=(unpack('L',pack('N',1))==1); } { $big_endian=(unpack('L',pack('N',1))==1); }
$code=<<___; $code=<<___;
.ident \"$output, version 1.1\" .ident \"$output, version 2.0\"
.ident \"IA-64 ISA artwork by Andy Polyakov <appro\@fy.chalmers.se>\" .ident \"IA-64 ISA artwork by Andy Polyakov <appro\@openssl.org>\"
.explicit .explicit
.text .text
@ -115,26 +121,25 @@ pfssave=r2;
lcsave=r3; lcsave=r3;
prsave=r14; prsave=r14;
K=r15; K=r15;
A=r16; B=r17; C=r18; D=r19; A_=r16; B_=r17; C_=r18; D_=r19;
E=r20; F=r21; G=r22; H=r23; E_=r20; F_=r21; G_=r22; H_=r23;
T1=r24; T2=r25; T1=r24; T2=r25;
s0=r26; s1=r27; t0=r28; t1=r29; s0=r26; s1=r27; t0=r28; t1=r29;
Ktbl=r30; Ktbl=r30;
ctx=r31; // 1st arg ctx=r31; // 1st arg
input=r48; // 2nd arg input=r56; // 2nd arg
num=r49; // 3rd arg num=r57; // 3rd arg
sgm0=r50; sgm1=r51; // small constants sgm0=r58; sgm1=r59; // small constants
A_=r54; B_=r55; C_=r56; D_=r57;
E_=r58; F_=r59; G_=r60; H_=r61;
// void $func (SHA_CTX *ctx, const void *in,size_t num[,int host]) // void $func (SHA_CTX *ctx, const void *in,size_t num[,int host])
.global $func# .global $func#
.proc $func# .proc $func#
.align 32 .align 32
.skip 16
$func: $func:
.prologue .prologue
.save ar.pfs,pfssave .save ar.pfs,pfssave
{ .mmi; alloc pfssave=ar.pfs,3,27,0,16 { .mmi; alloc pfssave=ar.pfs,3,25,0,24
$ADDP ctx=0,r32 // 1st arg $ADDP ctx=0,r32 // 1st arg
.save ar.lc,lcsave .save ar.lc,lcsave
mov lcsave=ar.lc } mov lcsave=ar.lc }
@ -145,11 +150,9 @@ $func:
.body .body
{ .mib; add r8=0*$SZ,ctx { .mib; add r8=0*$SZ,ctx
add r9=1*$SZ,ctx add r9=1*$SZ,ctx }
brp.loop.imp .L_first16,.L_first16_end-16 }
{ .mib; add r10=2*$SZ,ctx { .mib; add r10=2*$SZ,ctx
add r11=3*$SZ,ctx add r11=3*$SZ,ctx };;
brp.loop.imp .L_rest,.L_rest_end-16 };;
// load A-H // load A-H
.Lpic_point: .Lpic_point:
@ -164,7 +167,7 @@ $func:
add Ktbl=($TABLE#-.Lpic_point),Ktbl } add Ktbl=($TABLE#-.Lpic_point),Ktbl }
{ .mmi; $LDW G_=[r10] { .mmi; $LDW G_=[r10]
$LDW H_=[r11] $LDW H_=[r11]
cmp.ne p0,p16=0,r0 };; // used in sha256_block cmp.ne p0,p16=0,r0 };;
___ ___
$code.=<<___ if ($BITS==64); $code.=<<___ if ($BITS==64);
{ .mii; and r8=7,input { .mii; and r8=7,input
@ -179,50 +182,26 @@ $code.=<<___ if ($BITS==64);
___ ___
$code.=<<___; $code.=<<___;
.L_outer: .L_outer:
.rotr X[16] .rotr R[8],X[16]
{ .mmi; mov A=A_ A=R[0]; B=R[1]; C=R[2]; D=R[3]; E=R[4]; F=R[5]; G=R[6]; H=R[7]
mov B=B_ { .mmi; ld1 X[15]=[input],$SZ // eliminated in sha512
mov A=A_
mov ar.lc=14 } mov ar.lc=14 }
{ .mmi; mov C=C_ { .mmi; mov B=B_
mov D=D_ mov C=C_
mov E=E_ } mov D=D_ }
{ .mmi; mov F=F_ { .mmi; mov E=E_
mov G=G_ mov F=F_
mov ar.ec=2 } mov ar.ec=2 };;
{ .mmi; ld1 X[15]=[input],$SZ // eliminated in 64-bit { .mmi; mov G=G_
mov H=H_ mov H=H_
mov sgm1=$sigma1[2] };; mov sgm1=$sigma1[2] }
{ .mib; mov r8=0
___ add r9=1-$SZ,input
$t0="t0", $t1="t1", $code.=<<___ if ($BITS==32); brp.loop.imp .L_first16,.L_first16_end-16 };;
.align 32
.L_first16:
{ .mmi; add r9=1-$SZ,input
add r10=2-$SZ,input
add r11=3-$SZ,input };;
{ .mmi; ld1 r9=[r9]
ld1 r10=[r10]
dep.z $t1=E,32,32 }
{ .mmi; $LDW K=[Ktbl],$SZ
ld1 r11=[r11]
zxt4 E=E };;
{ .mii; or $t1=$t1,E
dep X[15]=X[15],r9,8,8
dep r11=r10,r11,8,8 };;
{ .mmi; and T1=F,E
and T2=A,B
dep X[15]=X[15],r11,16,16 }
{ .mmi; andcm r8=G,E
and r9=A,C
mux2 $t0=A,0x44 };; // copy lower half to upper
{ .mmi; (p16) ld1 X[15-1]=[input],$SZ // prefetch
xor T1=T1,r8 // T1=((e & f) ^ (~e & g))
_rotr r11=$t1,$Sigma1[0] } // ROTR(e,14)
{ .mib; and r10=B,C
xor T2=T2,r9 };;
___ ___
$t0="A", $t1="E", $code.=<<___ if ($BITS==64); $t0="A", $t1="E", $code.=<<___ if ($BITS==64);
// in 64-bit mode I load whole X[16] at once and take care of alignment... // in sha512 case I load whole X[16] at once and take care of alignment...
{ .mmi; add r8=1*$SZ,input { .mmi; add r8=1*$SZ,input
add r9=2*$SZ,input add r9=2*$SZ,input
add r10=3*$SZ,input };; add r10=3*$SZ,input };;
@ -248,7 +227,9 @@ $t0="A", $t1="E", $code.=<<___ if ($BITS==64);
$LDW X[ 2]=[r8],4*$SZ $LDW X[ 2]=[r8],4*$SZ
(p15) br.cond.dpnt.many .L7byte };; (p15) br.cond.dpnt.many .L7byte };;
{ .mmb; $LDW X[ 1]=[r9],4*$SZ { .mmb; $LDW X[ 1]=[r9],4*$SZ
$LDW X[ 0]=[r10],4*$SZ $LDW X[ 0]=[r10],4*$SZ }
{ .mib; mov r8=0
mux1 X[15]=X[15],\@rev // eliminated on big-endian
br.many .L_first16 };; br.many .L_first16 };;
.L1byte: .L1byte:
{ .mmi; $LDW X[13]=[r9],4*$SZ { .mmi; $LDW X[13]=[r9],4*$SZ
@ -281,7 +262,9 @@ $t0="A", $t1="E", $code.=<<___ if ($BITS==64);
shrp X[ 3]=X[ 3],X[ 2],56 } shrp X[ 3]=X[ 3],X[ 2],56 }
{ .mii; shrp X[ 2]=X[ 2],X[ 1],56 { .mii; shrp X[ 2]=X[ 2],X[ 1],56
shrp X[ 1]=X[ 1],X[ 0],56 } shrp X[ 1]=X[ 1],X[ 0],56 }
{ .mib; shrp X[ 0]=X[ 0],T1,56 { .mib; shrp X[ 0]=X[ 0],T1,56 }
{ .mib; mov r8=0
mux1 X[15]=X[15],\@rev // eliminated on big-endian
br.many .L_first16 };; br.many .L_first16 };;
.L2byte: .L2byte:
{ .mmi; $LDW X[11]=[input],4*$SZ { .mmi; $LDW X[11]=[input],4*$SZ
@ -313,7 +296,9 @@ $t0="A", $t1="E", $code.=<<___ if ($BITS==64);
shrp X[ 2]=X[ 2],X[ 1],48 } shrp X[ 2]=X[ 2],X[ 1],48 }
{ .mii; shrp X[ 1]=X[ 1],X[ 0],48 { .mii; shrp X[ 1]=X[ 1],X[ 0],48
shrp X[ 0]=X[ 0],T1,48 } shrp X[ 0]=X[ 0],T1,48 }
{ .mfb; br.many .L_first16 };; { .mib; mov r8=0
mux1 X[15]=X[15],\@rev // eliminated on big-endian
br.many .L_first16 };;
.L3byte: .L3byte:
{ .mmi; $LDW X[ 9]=[r9],4*$SZ { .mmi; $LDW X[ 9]=[r9],4*$SZ
$LDW X[ 8]=[r10],4*$SZ $LDW X[ 8]=[r10],4*$SZ
@ -341,7 +326,9 @@ $t0="A", $t1="E", $code.=<<___ if ($BITS==64);
shrp X[ 3]=X[ 3],X[ 2],40 } shrp X[ 3]=X[ 3],X[ 2],40 }
{ .mii; shrp X[ 2]=X[ 2],X[ 1],40 { .mii; shrp X[ 2]=X[ 2],X[ 1],40
shrp X[ 1]=X[ 1],X[ 0],40 } shrp X[ 1]=X[ 1],X[ 0],40 }
{ .mib; shrp X[ 0]=X[ 0],T1,40 { .mib; shrp X[ 0]=X[ 0],T1,40 }
{ .mib; mov r8=0
mux1 X[15]=X[15],\@rev // eliminated on big-endian
br.many .L_first16 };; br.many .L_first16 };;
.L4byte: .L4byte:
{ .mmi; $LDW X[ 7]=[input],4*$SZ { .mmi; $LDW X[ 7]=[input],4*$SZ
@ -369,7 +356,9 @@ $t0="A", $t1="E", $code.=<<___ if ($BITS==64);
shrp X[ 2]=X[ 2],X[ 1],32 } shrp X[ 2]=X[ 2],X[ 1],32 }
{ .mii; shrp X[ 1]=X[ 1],X[ 0],32 { .mii; shrp X[ 1]=X[ 1],X[ 0],32
shrp X[ 0]=X[ 0],T1,32 } shrp X[ 0]=X[ 0],T1,32 }
{ .mfb; br.many .L_first16 };; { .mib; mov r8=0
mux1 X[15]=X[15],\@rev // eliminated on big-endian
br.many .L_first16 };;
.L5byte: .L5byte:
{ .mmi; $LDW X[ 5]=[r9],4*$SZ { .mmi; $LDW X[ 5]=[r9],4*$SZ
$LDW X[ 4]=[r10],4*$SZ $LDW X[ 4]=[r10],4*$SZ
@ -393,7 +382,9 @@ $t0="A", $t1="E", $code.=<<___ if ($BITS==64);
shrp X[ 3]=X[ 3],X[ 2],24 } shrp X[ 3]=X[ 3],X[ 2],24 }
{ .mii; shrp X[ 2]=X[ 2],X[ 1],24 { .mii; shrp X[ 2]=X[ 2],X[ 1],24
shrp X[ 1]=X[ 1],X[ 0],24 } shrp X[ 1]=X[ 1],X[ 0],24 }
{ .mib; shrp X[ 0]=X[ 0],T1,24 { .mib; shrp X[ 0]=X[ 0],T1,24 }
{ .mib; mov r8=0
mux1 X[15]=X[15],\@rev // eliminated on big-endian
br.many .L_first16 };; br.many .L_first16 };;
.L6byte: .L6byte:
{ .mmi; $LDW X[ 3]=[input],4*$SZ { .mmi; $LDW X[ 3]=[input],4*$SZ
@ -417,7 +408,9 @@ $t0="A", $t1="E", $code.=<<___ if ($BITS==64);
shrp X[ 2]=X[ 2],X[ 1],16 } shrp X[ 2]=X[ 2],X[ 1],16 }
{ .mii; shrp X[ 1]=X[ 1],X[ 0],16 { .mii; shrp X[ 1]=X[ 1],X[ 0],16
shrp X[ 0]=X[ 0],T1,16 } shrp X[ 0]=X[ 0],T1,16 }
{ .mfb; br.many .L_first16 };; { .mib; mov r8=0
mux1 X[15]=X[15],\@rev // eliminated on big-endian
br.many .L_first16 };;
.L7byte: .L7byte:
{ .mmi; $LDW X[ 1]=[r9],4*$SZ { .mmi; $LDW X[ 1]=[r9],4*$SZ
$LDW X[ 0]=[r10],4*$SZ $LDW X[ 0]=[r10],4*$SZ
@ -437,128 +430,146 @@ $t0="A", $t1="E", $code.=<<___ if ($BITS==64);
shrp X[ 3]=X[ 3],X[ 2],8 } shrp X[ 3]=X[ 3],X[ 2],8 }
{ .mii; shrp X[ 2]=X[ 2],X[ 1],8 { .mii; shrp X[ 2]=X[ 2],X[ 1],8
shrp X[ 1]=X[ 1],X[ 0],8 } shrp X[ 1]=X[ 1],X[ 0],8 }
{ .mib; shrp X[ 0]=X[ 0],T1,8 { .mib; shrp X[ 0]=X[ 0],T1,8 }
br.many .L_first16 };; { .mib; mov r8=0
mux1 X[15]=X[15],\@rev };; // eliminated on big-endian
.align 32 .align 32
.L_first16: .L_first16:
{ .mmi; $LDW K=[Ktbl],$SZ { .mmi; $LDW K=[Ktbl],$SZ
and T1=F,E add A=A,r8 // H+=Sigma(0) from the past
and T2=A,B } _rotr r10=$t1,$Sigma1[0] } // ROTR(e,14)
{ .mmi; //$LDW X[15]=[input],$SZ // X[i]=*input++ { .mmi; and T1=F,E
andcm r8=G,E andcm r8=G,E
and r9=A,C };; (p16) mux1 X[14]=X[14],\@rev };; // eliminated on big-endian
{ .mmi; xor T1=T1,r8 //T1=((e & f) ^ (~e & g)) { .mmi; and T2=A,B
and r10=B,C and r9=A,C
_rotr r11=$t1,$Sigma1[0] } // ROTR(e,14) _rotr r11=$t1,$Sigma1[1] } // ROTR(e,41)
{ .mmi; xor T2=T2,r9 { .mmi; xor T1=T1,r8 // T1=((e & f) ^ (~e & g))
mux1 X[15]=X[15],\@rev };; // eliminated in big-endian and r8=B,C };;
___
$t0="t0", $t1="t1", $code.=<<___ if ($BITS==32);
.align 32
.L_first16:
{ .mmi; add A=A,r8 // H+=Sigma(0) from the past
add r10=2-$SZ,input
add r11=3-$SZ,input };;
{ .mmi; ld1 r9=[r9]
ld1 r10=[r10]
dep.z $t1=E,32,32 }
{ .mmi; ld1 r11=[r11]
$LDW K=[Ktbl],$SZ
zxt4 E=E };;
{ .mii; or $t1=$t1,E
dep X[15]=X[15],r9,8,8
mux2 $t0=A,0x44 };; // copy lower half to upper
{ .mmi; and T1=F,E
andcm r8=G,E
dep r11=r10,r11,8,8 };;
{ .mmi; and T2=A,B
and r9=A,C
dep X[15]=X[15],r11,16,16 };;
{ .mmi; (p16) ld1 X[15-1]=[input],$SZ // prefetch
xor T1=T1,r8 // T1=((e & f) ^ (~e & g))
_rotr r10=$t1,$Sigma1[0] } // ROTR(e,14)
{ .mmi; and r8=B,C
_rotr r11=$t1,$Sigma1[1] };; // ROTR(e,18)
___ ___
$code.=<<___; $code.=<<___;
{ .mib; add T1=T1,H // T1=Ch(e,f,g)+h { .mmi; add T1=T1,H // T1=Ch(e,f,g)+h
_rotr r8=$t1,$Sigma1[1] } // ROTR(e,18) xor r10=r10,r11
{ .mib; xor T2=T2,r10 // T2=((a & b) ^ (a & c) ^ (b & c)) _rotr r11=$t1,$Sigma1[2] } // ROTR(e,41)
mov H=G };; { .mmi; xor T2=T2,r9
{ .mib; xor r11=r8,r11 add K=K,X[15] };;
_rotr r9=$t1,$Sigma1[2] } // ROTR(e,41) { .mmi; add T1=T1,K // T1+=K[i]+X[i]
{ .mib; mov G=F xor T2=T2,r8 // T2=((a & b) ^ (a & c) ^ (b & c))
mov F=E };; _rotr r8=$t0,$Sigma0[0] } // ROTR(a,28)
{ .mib; xor r9=r9,r11 // r9=Sigma1(e) { .mmi; xor r11=r11,r10 // Sigma1(e)
_rotr r10=$t0,$Sigma0[0] } // ROTR(a,28) _rotr r9=$t0,$Sigma0[1] };; // ROTR(a,34)
{ .mib; add T1=T1,K // T1=Ch(e,f,g)+h+K512[i] { .mmi; add T1=T1,r11 // T+=Sigma1(e)
mov E=D };; xor r8=r8,r9
{ .mib; add T1=T1,r9 // T1+=Sigma1(e) _rotr r9=$t0,$Sigma0[2] };; // ROTR(a,39)
_rotr r11=$t0,$Sigma0[1] } // ROTR(a,34) { .mmi; xor r8=r8,r9 // Sigma0(a)
{ .mib; mov D=C add D=D,T1
mov C=B };; mux2 H=X[15],0x44 } // mov H=X[15] in sha512
{ .mib; add T1=T1,X[15] // T1+=X[i] { .mib; (p16) add r9=1-$SZ,input // not used in sha512
_rotr r8=$t0,$Sigma0[2] } // ROTR(a,39) add X[15]=T1,T2 // H=T1+Maj(a,b,c)
{ .mib; xor r10=r10,r11 br.ctop.sptk .L_first16 };;
mux2 X[15]=X[15],0x44 };; // eliminated in 64-bit
{ .mmi; xor r10=r8,r10 // r10=Sigma0(a)
mov B=A
add A=T1,T2 };;
{ .mib; add E=E,T1
add A=A,r10 // T2=Maj(a,b,c)+Sigma0(a)
br.ctop.sptk .L_first16 };;
.L_first16_end: .L_first16_end:
{ .mii; mov ar.lc=$rounds-17 { .mib; mov ar.lc=$rounds-17
mov ar.ec=1 };; brp.loop.imp .L_rest,.L_rest_end-16 }
{ .mib; mov ar.ec=1
br.many .L_rest };;
.align 32 .align 32
.L_rest: .L_rest:
.rotr X[16] { .mmi; $LDW K=[Ktbl],$SZ
{ .mib; $LDW K=[Ktbl],$SZ add A=A,r8 // H+=Sigma0(a) from the past
_rotr r8=X[15-1],$sigma0[0] } // ROTR(s0,1) _rotr r8=X[15-1],$sigma0[0] } // ROTR(s0,1)
{ .mib; $ADD X[15]=X[15],X[15-9] // X[i&0xF]+=X[(i+9)&0xF] { .mmi; add X[15]=X[15],X[15-9] // X[i&0xF]+=X[(i+9)&0xF]
$SHRU s0=X[15-1],sgm0 };; // s0=X[(i+1)&0xF]>>7 $SHRU s0=X[15-1],sgm0 };; // s0=X[(i+1)&0xF]>>7
{ .mib; and T1=F,E { .mib; and T1=F,E
_rotr r9=X[15-1],$sigma0[1] } // ROTR(s0,8) _rotr r9=X[15-1],$sigma0[1] } // ROTR(s0,8)
{ .mib; andcm r10=G,E { .mib; andcm r10=G,E
$SHRU s1=X[15-14],sgm1 };; // s1=X[(i+14)&0xF]>>6 $SHRU s1=X[15-14],sgm1 };; // s1=X[(i+14)&0xF]>>6
// Pair of mmi; splits on Itanium 1 and prevents pipeline flush
// upon $SHRU output usage
{ .mmi; xor T1=T1,r10 // T1=((e & f) ^ (~e & g)) { .mmi; xor T1=T1,r10 // T1=((e & f) ^ (~e & g))
xor r9=r8,r9 xor r9=r8,r9
_rotr r10=X[15-14],$sigma1[0] };;// ROTR(s1,19) _rotr r10=X[15-14],$sigma1[0] }// ROTR(s1,19)
{ .mib; and T2=A,B { .mmi; and T2=A,B
_rotr r11=X[15-14],$sigma1[1] }// ROTR(s1,61) and r8=A,C
{ .mib; and r8=A,C };; _rotr r11=X[15-14],$sigma1[1] };;// ROTR(s1,61)
___ ___
$t0="t0", $t1="t1", $code.=<<___ if ($BITS==32); $t0="t0", $t1="t1", $code.=<<___ if ($BITS==32);
// I adhere to mmi; in order to hold Itanium 1 back and avoid 6 cycle { .mib; xor s0=s0,r9 // s0=sigma0(X[(i+1)&0xF])
// pipeline flush in last bundle. Note that even on Itanium2 the dep.z $t1=E,32,32 }
// latter stalls for one clock cycle... { .mib; xor r10=r11,r10
{ .mmi; xor s0=s0,r9 // s0=sigma0(X[(i+1)&0xF]) zxt4 E=E };;
dep.z $t1=E,32,32 } { .mii; xor s1=s1,r10 // s1=sigma1(X[(i+14)&0xF])
{ .mmi; xor r10=r11,r10 shrp r9=E,$t1,32+$Sigma1[0] // ROTR(e,14)
zxt4 E=E };; mux2 $t0=A,0x44 };; // copy lower half to upper
{ .mmi; or $t1=$t1,E // Pair of mmi; splits on Itanium 1 and prevents pipeline flush
xor s1=s1,r10 // s1=sigma1(X[(i+14)&0xF]) // upon mux2 output usage
mux2 $t0=A,0x44 };; // copy lower half to upper
{ .mmi; xor T2=T2,r8 { .mmi; xor T2=T2,r8
_rotr r9=$t1,$Sigma1[0] } // ROTR(e,14) shrp r8=E,$t1,32+$Sigma1[1]} // ROTR(e,18)
{ .mmi; and r10=B,C { .mmi; and r10=B,C
add T1=T1,H // T1=Ch(e,f,g)+h add T1=T1,H // T1=Ch(e,f,g)+h
$ADD X[15]=X[15],s0 };; // X[i&0xF]+=sigma0(X[(i+1)&0xF]) or $t1=$t1,E };;
___ ___
$t0="A", $t1="E", $code.=<<___ if ($BITS==64); $t0="A", $t1="E", $code.=<<___ if ($BITS==64);
{ .mib; xor s0=s0,r9 // s0=sigma0(X[(i+1)&0xF]) { .mib; xor s0=s0,r9 // s0=sigma0(X[(i+1)&0xF])
_rotr r9=$t1,$Sigma1[0] } // ROTR(e,14) _rotr r9=$t1,$Sigma1[0] } // ROTR(e,14)
{ .mib; xor r10=r11,r10 { .mib; xor r10=r11,r10
xor T2=T2,r8 };; xor T2=T2,r8 };;
{ .mib; xor s1=s1,r10 // s1=sigma1(X[(i+14)&0xF]) { .mib; xor s1=s1,r10 // s1=sigma1(X[(i+14)&0xF])
add T1=T1,H } _rotr r8=$t1,$Sigma1[1] } // ROTR(e,18)
{ .mib; and r10=B,C { .mib; and r10=B,C
$ADD X[15]=X[15],s0 };; // X[i&0xF]+=sigma0(X[(i+1)&0xF]) add T1=T1,H };; // T1+=H
___ ___
$code.=<<___; $code.=<<___;
{ .mmi; xor T2=T2,r10 // T2=((a & b) ^ (a & c) ^ (b & c)) { .mib; xor r9=r9,r8
mov H=G _rotr r8=$t1,$Sigma1[2] } // ROTR(e,41)
_rotr r8=$t1,$Sigma1[1] };; // ROTR(e,18) { .mib; xor T2=T2,r10 // T2=((a & b) ^ (a & c) ^ (b & c))
{ .mmi; xor r11=r8,r9 add X[15]=X[15],s0 };; // X[i]+=sigma0(X[i+1])
$ADD X[15]=X[15],s1 // X[i&0xF]+=sigma1(X[(i+14)&0xF]) { .mmi; xor r9=r9,r8 // Sigma1(e)
_rotr r9=$t1,$Sigma1[2] } // ROTR(e,41) add X[15]=X[15],s1 // X[i]+=sigma0(X[i+14])
{ .mmi; mov G=F _rotr r8=$t0,$Sigma0[0] };; // ROTR(a,28)
mov F=E };; { .mmi; add K=K,X[15]
{ .mib; xor r9=r9,r11 // r9=Sigma1(e) add T1=T1,r9 // T1+=Sigma1(e)
_rotr r10=$t0,$Sigma0[0] } // ROTR(a,28) _rotr r9=$t0,$Sigma0[1] };; // ROTR(a,34)
{ .mib; add T1=T1,K // T1=Ch(e,f,g)+h+K512[i] { .mmi; add T1=T1,K // T1+=K[i]+X[i]
mov E=D };; xor r8=r8,r9
{ .mib; add T1=T1,r9 // T1+=Sigma1(e) _rotr r9=$t0,$Sigma0[2] };; // ROTR(a,39)
_rotr r11=$t0,$Sigma0[1] } // ROTR(a,34) { .mib; add D=D,T1
{ .mib; mov D=C mux2 H=X[15],0x44 } // mov H=X[15] in sha512
mov C=B };; { .mib; xor r8=r8,r9 // Sigma0(a)
{ .mmi; add T1=T1,X[15] // T1+=X[i] add X[15]=T1,T2 // H=T1+Maj(a,b,c)
xor r10=r10,r11 br.ctop.sptk .L_rest };;
_rotr r8=$t0,$Sigma0[2] };; // ROTR(a,39)
{ .mmi; xor r10=r8,r10 // r10=Sigma0(a)
mov B=A
add A=T1,T2 };;
{ .mib; add E=E,T1
add A=A,r10 // T2=Maj(a,b,c)+Sigma0(a)
br.ctop.sptk .L_rest };;
.L_rest_end: .L_rest_end:
{ .mmi; add A=A,r8 };; // H+=Sigma0(a) from the past
{ .mmi; add A_=A_,A { .mmi; add A_=A_,A
add B_=B_,B add B_=B_,B
add C_=C_,C } add C_=C_,C }
@ -590,17 +601,19 @@ $code.=<<___;
.endp $func# .endp $func#
___ ___
$code =~ s/\`([^\`]*)\`/eval $1/gem; foreach(split($/,$code)) {
$code =~ s/_rotr(\s+)([^=]+)=([^,]+),([0-9]+)/shrp$1$2=$3,$3,$4/gm; s/\`([^\`]*)\`/eval $1/gem;
if ($BITS==64) { s/_rotr(\s+)([^=]+)=([^,]+),([0-9]+)/shrp$1$2=$3,$3,$4/gm;
$code =~ s/mux2(\s+)\S+/nop.i$1 0x0/gm; if ($BITS==64) {
$code =~ s/mux1(\s+)\S+/nop.i$1 0x0/gm if ($big_endian); s/mux2(\s+)([^=]+)=([^,]+),\S+/mov$1 $2=$3/gm;
$code =~ s/(shrp\s+X\[[^=]+)=([^,]+),([^,]+),([1-9]+)/$1=$3,$2,64-$4/gm s/mux1(\s+)\S+/nop.i$1 0x0/gm if ($big_endian);
s/(shrp\s+X\[[^=]+)=([^,]+),([^,]+),([1-9]+)/$1=$3,$2,64-$4/gm
if (!$big_endian); if (!$big_endian);
$code =~ s/ld1(\s+)X\[\S+/nop.m$1 0x0/gm; s/ld1(\s+)X\[\S+/nop.m$1 0x0/gm;
} }
print $code; print $_,"\n";
}
print<<___ if ($BITS==32); print<<___ if ($BITS==32);
.align 64 .align 64