7237N/A#!/usr/bin/env perl
7237N/A
7237N/A# ====================================================================
7237N/A# Written by Andy Polyakov <appro@fy.chalmers.se> for the OpenSSL
7237N/A# project. The module is, however, dual licensed under OpenSSL and
7237N/A# CRYPTOGAMS licenses depending on where you obtain it. For further
7237N/A# details see http://www.openssl.org/~appro/cryptogams/.
7237N/A#
7237N/A# Hardware SPARC T4 support by David S. Miller <davem@davemloft.net>.
7237N/A# ====================================================================
7237N/A
7237N/A# Performance improvement is not really impressive on pre-T1 CPU: +8%
7237N/A# over Sun C and +25% over gcc [3.3]. While on T1, a.k.a. Niagara, it
7237N/A# turned to be 40% faster than 64-bit code generated by Sun C 5.8 and
7237N/A# >2x than 64-bit code generated by gcc 3.4. And there is a gimmick.
7237N/A# X[16] vector is packed to 8 64-bit registers and as result nothing
7237N/A# is spilled on stack. In addition input data is loaded in compact
7237N/A# instruction sequence, thus minimizing the window when the code is
7237N/A# subject to [inter-thread] cache-thrashing hazard. The goal is to
7237N/A# ensure scalability on UltraSPARC T1, or rather to avoid decay when
7237N/A# amount of active threads exceeds the number of physical cores.
7237N/A
7237N/A# SPARC T4 SHA1 hardware achieves 3.72 cycles per byte, which is 3.1x
7237N/A# faster than software. Multi-process benchmark saturates at 11x
7237N/A# single-process result on 8-core processor, or ~9GBps per 2.85GHz
7237N/A# socket.
7237N/A
7237N/A$output=shift;
7237N/Aopen STDOUT,">$output";
7237N/A
7237N/A@X=("%o0","%o1","%o2","%o3","%o4","%o5","%g1","%o7");
7237N/A$rot1m="%g2";
7237N/A$tmp64="%g3";
7237N/A$Xi="%g4";
7237N/A$A="%l0";
7237N/A$B="%l1";
7237N/A$C="%l2";
7237N/A$D="%l3";
7237N/A$E="%l4";
7237N/A@V=($A,$B,$C,$D,$E);
7237N/A$K_00_19="%l5";
7237N/A$K_20_39="%l6";
7237N/A$K_40_59="%l7";
7237N/A$K_60_79="%g5";
7237N/A@K=($K_00_19,$K_20_39,$K_40_59,$K_60_79);
7237N/A
7237N/A$ctx="%i0";
7237N/A$inp="%i1";
7237N/A$len="%i2";
7237N/A$tmp0="%i3";
7237N/A$tmp1="%i4";
7237N/A$tmp2="%i5";
7237N/A
7237N/Asub BODY_00_15 {
7237N/Amy ($i,$a,$b,$c,$d,$e)=@_;
7237N/Amy $xi=($i&1)?@X[($i/2)%8]:$Xi;
7237N/A
7237N/A$code.=<<___;
7237N/A sll $a,5,$tmp0 !! $i
7237N/A add @K[$i/20],$e,$e
7237N/A srl $a,27,$tmp1
7237N/A add $tmp0,$e,$e
7237N/A and $c,$b,$tmp0
7237N/A add $tmp1,$e,$e
7237N/A sll $b,30,$tmp2
7237N/A andn $d,$b,$tmp1
7237N/A srl $b,2,$b
7237N/A or $tmp1,$tmp0,$tmp1
7237N/A or $tmp2,$b,$b
7237N/A add $xi,$e,$e
7237N/A___
7237N/Aif ($i&1 && $i<15) {
7237N/A $code.=
7237N/A " srlx @X[(($i+1)/2)%8],32,$Xi\n";
7237N/A}
7237N/A$code.=<<___;
7237N/A add $tmp1,$e,$e
7237N/A___
7237N/A}
7237N/A
7237N/Asub Xupdate {
7237N/Amy ($i,$a,$b,$c,$d,$e)=@_;
7237N/Amy $j=$i/2;
7237N/A
7237N/Aif ($i&1) {
7237N/A$code.=<<___;
7237N/A sll $a,5,$tmp0 !! $i
7237N/A add @K[$i/20],$e,$e
7237N/A srl $a,27,$tmp1
7237N/A___
7237N/A} else {
7237N/A$code.=<<___;
7237N/A sllx @X[($j+6)%8],32,$Xi ! Xupdate($i)
7237N/A xor @X[($j+1)%8],@X[$j%8],@X[$j%8]
7237N/A srlx @X[($j+7)%8],32,$tmp1
7237N/A xor @X[($j+4)%8],@X[$j%8],@X[$j%8]
7237N/A sll $a,5,$tmp0 !! $i
7237N/A or $tmp1,$Xi,$Xi
7237N/A add @K[$i/20],$e,$e !!
7237N/A xor $Xi,@X[$j%8],@X[$j%8]
7237N/A srlx @X[$j%8],31,$Xi
7237N/A add @X[$j%8],@X[$j%8],@X[$j%8]
7237N/A and $Xi,$rot1m,$Xi
7237N/A andn @X[$j%8],$rot1m,@X[$j%8]
7237N/A srl $a,27,$tmp1 !!
7237N/A or $Xi,@X[$j%8],@X[$j%8]
7237N/A___
7237N/A}
7237N/A}
7237N/A
7237N/Asub BODY_16_19 {
7237N/Amy ($i,$a,$b,$c,$d,$e)=@_;
7237N/A
7237N/A &Xupdate(@_);
7237N/A if ($i&1) {
7237N/A $xi=@X[($i/2)%8];
7237N/A } else {
7237N/A $xi=$Xi;
7237N/A $code.="\tsrlx @X[($i/2)%8],32,$xi\n";
7237N/A }
7237N/A$code.=<<___;
7237N/A add $tmp0,$e,$e !!
7237N/A and $c,$b,$tmp0
7237N/A add $tmp1,$e,$e
7237N/A sll $b,30,$tmp2
7237N/A add $xi,$e,$e
7237N/A andn $d,$b,$tmp1
7237N/A srl $b,2,$b
7237N/A or $tmp1,$tmp0,$tmp1
7237N/A or $tmp2,$b,$b
7237N/A add $tmp1,$e,$e
7237N/A___
7237N/A}
7237N/A
7237N/Asub BODY_20_39 {
7237N/Amy ($i,$a,$b,$c,$d,$e)=@_;
7237N/Amy $xi;
7237N/A &Xupdate(@_);
7237N/A if ($i&1) {
7237N/A $xi=@X[($i/2)%8];
7237N/A } else {
7237N/A $xi=$Xi;
7237N/A $code.="\tsrlx @X[($i/2)%8],32,$xi\n";
7237N/A }
7237N/A$code.=<<___;
7237N/A add $tmp0,$e,$e !!
7237N/A xor $c,$b,$tmp0
7237N/A add $tmp1,$e,$e
7237N/A sll $b,30,$tmp2
7237N/A xor $d,$tmp0,$tmp1
7237N/A srl $b,2,$b
7237N/A add $tmp1,$e,$e
7237N/A or $tmp2,$b,$b
7237N/A add $xi,$e,$e
7237N/A___
7237N/A}
7237N/A
7237N/Asub BODY_40_59 {
7237N/Amy ($i,$a,$b,$c,$d,$e)=@_;
7237N/Amy $xi;
7237N/A &Xupdate(@_);
7237N/A if ($i&1) {
7237N/A $xi=@X[($i/2)%8];
7237N/A } else {
7237N/A $xi=$Xi;
7237N/A $code.="\tsrlx @X[($i/2)%8],32,$xi\n";
7237N/A }
7237N/A$code.=<<___;
7237N/A add $tmp0,$e,$e !!
7237N/A and $c,$b,$tmp0
7237N/A add $tmp1,$e,$e
7237N/A sll $b,30,$tmp2
7237N/A or $c,$b,$tmp1
7237N/A srl $b,2,$b
7237N/A and $d,$tmp1,$tmp1
7237N/A add $xi,$e,$e
7237N/A or $tmp1,$tmp0,$tmp1
7237N/A or $tmp2,$b,$b
7237N/A add $tmp1,$e,$e
7237N/A___
7237N/A}
7237N/A
7237N/A$code.=<<___;
7237N/A#include "sparc_arch.h"
7237N/A#include <openssl/fipssyms.h>
7237N/A
7237N/A#ifdef __arch64__
7237N/A.register %g2,#scratch
7237N/A.register %g3,#scratch
7237N/A#endif
7237N/A
7237N/A.section ".text",#alloc,#execinstr
7237N/A
7237N/A#ifdef __PIC__
7237N/ASPARC_PIC_THUNK(%g1)
7237N/A#endif
7237N/A
7237N/A.align 32
7237N/A.globl sha1_block_data_order
7237N/Asha1_block_data_order:
7237N/A SPARC_LOAD_ADDRESS_LEAF(OPENSSL_sparcv9cap_P,%g1,%g5)
7237N/A ld [%g1+4],%g1 ! OPENSSL_sparcv9cap_P[1]
7237N/A
7237N/A andcc %g1, CFR_SHA1, %g0
7237N/A be .Lsoftware
7237N/A nop
7237N/A
7237N/A ld [%o0 + 0x00], %f0 ! load context
7237N/A ld [%o0 + 0x04], %f1
7237N/A ld [%o0 + 0x08], %f2
7237N/A andcc %o1, 0x7, %g0
7237N/A ld [%o0 + 0x0c], %f3
7237N/A bne,pn %icc, .Lhwunaligned
7237N/A ld [%o0 + 0x10], %f4
7237N/A
7237N/A.Lhw_loop:
7237N/A ldd [%o1 + 0x00], %f8
7237N/A ldd [%o1 + 0x08], %f10
7237N/A ldd [%o1 + 0x10], %f12
7237N/A ldd [%o1 + 0x18], %f14
7237N/A ldd [%o1 + 0x20], %f16
7237N/A ldd [%o1 + 0x28], %f18
7237N/A ldd [%o1 + 0x30], %f20
7237N/A subcc %o2, 1, %o2 ! done yet?
7237N/A ldd [%o1 + 0x38], %f22
7237N/A add %o1, 0x40, %o1
7237N/A prefetch [%o1 + 63], 20
7237N/A
7237N/A .word 0x81b02820 ! SHA1
7237N/A
7237N/A bne,pt SIZE_T_CC, .Lhw_loop
7237N/A nop
7237N/A
7237N/A.Lhwfinish:
7237N/A st %f0, [%o0 + 0x00] ! store context
7237N/A st %f1, [%o0 + 0x04]
7237N/A st %f2, [%o0 + 0x08]
7237N/A st %f3, [%o0 + 0x0c]
7237N/A retl
7237N/A st %f4, [%o0 + 0x10]
7237N/A
7237N/A.align 8
7237N/A.Lhwunaligned:
7237N/A alignaddr %o1, %g0, %o1
7237N/A
7237N/A ldd [%o1 + 0x00], %f10
7237N/A.Lhwunaligned_loop:
7237N/A ldd [%o1 + 0x08], %f12
7237N/A ldd [%o1 + 0x10], %f14
7237N/A ldd [%o1 + 0x18], %f16
7237N/A ldd [%o1 + 0x20], %f18
7237N/A ldd [%o1 + 0x28], %f20
7237N/A ldd [%o1 + 0x30], %f22
7237N/A ldd [%o1 + 0x38], %f24
7237N/A subcc %o2, 1, %o2 ! done yet?
7237N/A ldd [%o1 + 0x40], %f26
7237N/A add %o1, 0x40, %o1
7237N/A prefetch [%o1 + 63], 20
7237N/A
7237N/A faligndata %f10, %f12, %f8
7237N/A faligndata %f12, %f14, %f10
7237N/A faligndata %f14, %f16, %f12
7237N/A faligndata %f16, %f18, %f14
7237N/A faligndata %f18, %f20, %f16
7237N/A faligndata %f20, %f22, %f18
7237N/A faligndata %f22, %f24, %f20
7237N/A faligndata %f24, %f26, %f22
7237N/A
7237N/A .word 0x81b02820 ! SHA1
7237N/A
7237N/A bne,pt SIZE_T_CC, .Lhwunaligned_loop
7237N/A for %f26, %f26, %f10 ! %f10=%f26
7237N/A
7237N/A ba .Lhwfinish
7237N/A nop
7237N/A
7237N/A.align 16
7237N/A.Lsoftware:
7237N/A save %sp,-STACK_FRAME,%sp
7237N/A sllx $len,6,$len
7237N/A add $inp,$len,$len
7237N/A
7237N/A or %g0,1,$rot1m
7237N/A sllx $rot1m,32,$rot1m
7237N/A or $rot1m,1,$rot1m
7237N/A
7237N/A ld [$ctx+0],$A
7237N/A ld [$ctx+4],$B
7237N/A ld [$ctx+8],$C
7237N/A ld [$ctx+12],$D
7237N/A ld [$ctx+16],$E
7237N/A andn $inp,7,$tmp0
7237N/A
7237N/A sethi %hi(0x5a827999),$K_00_19
7237N/A or $K_00_19,%lo(0x5a827999),$K_00_19
7237N/A sethi %hi(0x6ed9eba1),$K_20_39
7237N/A or $K_20_39,%lo(0x6ed9eba1),$K_20_39
7237N/A sethi %hi(0x8f1bbcdc),$K_40_59
7237N/A or $K_40_59,%lo(0x8f1bbcdc),$K_40_59
7237N/A sethi %hi(0xca62c1d6),$K_60_79
7237N/A or $K_60_79,%lo(0xca62c1d6),$K_60_79
7237N/A
7237N/A.Lloop:
7237N/A ldx [$tmp0+0],@X[0]
7237N/A ldx [$tmp0+16],@X[2]
7237N/A ldx [$tmp0+32],@X[4]
7237N/A ldx [$tmp0+48],@X[6]
7237N/A and $inp,7,$tmp1
7237N/A ldx [$tmp0+8],@X[1]
7237N/A sll $tmp1,3,$tmp1
7237N/A ldx [$tmp0+24],@X[3]
7237N/A subcc %g0,$tmp1,$tmp2 ! should be 64-$tmp1, but -$tmp1 works too
7237N/A ldx [$tmp0+40],@X[5]
7237N/A bz,pt %icc,.Laligned
7237N/A ldx [$tmp0+56],@X[7]
7237N/A
7237N/A sllx @X[0],$tmp1,@X[0]
7237N/A ldx [$tmp0+64],$tmp64
7237N/A___
7237N/Afor($i=0;$i<7;$i++)
7237N/A{ $code.=<<___;
7237N/A srlx @X[$i+1],$tmp2,$Xi
7237N/A sllx @X[$i+1],$tmp1,@X[$i+1]
7237N/A or $Xi,@X[$i],@X[$i]
7237N/A___
7237N/A}
7237N/A$code.=<<___;
7237N/A srlx $tmp64,$tmp2,$tmp64
7237N/A or $tmp64,@X[7],@X[7]
7237N/A.Laligned:
7237N/A srlx @X[0],32,$Xi
7237N/A___
7237N/Afor ($i=0;$i<16;$i++) { &BODY_00_15($i,@V); unshift(@V,pop(@V)); }
7237N/Afor (;$i<20;$i++) { &BODY_16_19($i,@V); unshift(@V,pop(@V)); }
7237N/Afor (;$i<40;$i++) { &BODY_20_39($i,@V); unshift(@V,pop(@V)); }
7237N/Afor (;$i<60;$i++) { &BODY_40_59($i,@V); unshift(@V,pop(@V)); }
7237N/Afor (;$i<80;$i++) { &BODY_20_39($i,@V); unshift(@V,pop(@V)); }
7237N/A$code.=<<___;
7237N/A
7237N/A ld [$ctx+0],@X[0]
7237N/A ld [$ctx+4],@X[1]
7237N/A ld [$ctx+8],@X[2]
7237N/A ld [$ctx+12],@X[3]
7237N/A add $inp,64,$inp
7237N/A ld [$ctx+16],@X[4]
7237N/A cmp $inp,$len
7237N/A
7237N/A add $A,@X[0],$A
7237N/A st $A,[$ctx+0]
7237N/A add $B,@X[1],$B
7237N/A st $B,[$ctx+4]
7237N/A add $C,@X[2],$C
7237N/A st $C,[$ctx+8]
7237N/A add $D,@X[3],$D
7237N/A st $D,[$ctx+12]
7237N/A add $E,@X[4],$E
7237N/A st $E,[$ctx+16]
7237N/A
7237N/A bne SIZE_T_CC,.Lloop
7237N/A andn $inp,7,$tmp0
7237N/A
7237N/A ret
7237N/A restore
7237N/A.type sha1_block_data_order,#function
7237N/A.size sha1_block_data_order,(.-sha1_block_data_order)
7237N/A.asciz "SHA1 block transform for SPARCv9, CRYPTOGAMS by <appro\@openssl.org>"
7237N/A.align 4
7237N/A___
7237N/A
7237N/A# Purpose of these subroutines is to explicitly encode VIS instructions,
7237N/A# so that one can compile the module without having to specify VIS
7237N/A# extentions on compiler command line, e.g. -xarch=v9 vs. -xarch=v9a.
7237N/A# Idea is to reserve for option to produce "universal" binary and let
7237N/A# programmer detect if current CPU is VIS capable at run-time.
7237N/Asub unvis {
7237N/Amy ($mnemonic,$rs1,$rs2,$rd)=@_;
7237N/Amy $ref,$opf;
7237N/Amy %visopf = ( "faligndata" => 0x048,
7237N/A "for" => 0x07c );
7237N/A
7237N/A $ref = "$mnemonic\t$rs1,$rs2,$rd";
7237N/A
7237N/A if ($opf=$visopf{$mnemonic}) {
7237N/A foreach ($rs1,$rs2,$rd) {
7237N/A return $ref if (!/%f([0-9]{1,2})/);
7237N/A $_=$1;
7237N/A if ($1>=32) {
7237N/A return $ref if ($1&1);
7237N/A # re-encode for upper double register addressing
7237N/A $_=($1|$1>>5)&31;
7237N/A }
7237N/A }
7237N/A
7237N/A return sprintf ".word\t0x%08x !%s",
7237N/A 0x81b00000|$rd<<25|$rs1<<14|$opf<<5|$rs2,
7237N/A $ref;
7237N/A } else {
7237N/A return $ref;
7237N/A }
7237N/A}
7237N/Asub unalignaddr {
7237N/Amy ($mnemonic,$rs1,$rs2,$rd)=@_;
7237N/Amy %bias = ( "g" => 0, "o" => 8, "l" => 16, "i" => 24 );
7237N/Amy $ref="$mnemonic\t$rs1,$rs2,$rd";
7237N/A
7237N/A foreach ($rs1,$rs2,$rd) {
7237N/A if (/%([goli])([0-7])/) { $_=$bias{$1}+$2; }
7237N/A else { return $ref; }
7237N/A }
7237N/A return sprintf ".word\t0x%08x !%s",
7237N/A 0x81b00300|$rd<<25|$rs1<<14|$rs2,
7237N/A $ref;
7237N/A}
7237N/A
7237N/Aforeach (split("\n",$code)) {
7237N/A s/\`([^\`]*)\`/eval $1/ge;
7237N/A
7237N/A s/\b(f[^\s]*)\s+(%f[0-9]{1,2}),\s*(%f[0-9]{1,2}),\s*(%f[0-9]{1,2})/
7237N/A &unvis($1,$2,$3,$4)
7237N/A /ge;
7237N/A s/\b(alignaddr)\s+(%[goli][0-7]),\s*(%[goli][0-7]),\s*(%[goli][0-7])/
7237N/A &unalignaddr($1,$2,$3,$4)
7237N/A /ge;
7237N/A
7237N/A print $_,"\n";
7237N/A}
7237N/A
7237N/Aclose STDOUT;