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/.
9 # Hardware SPARC T4 support by David S. Miller <davem@davemloft.net>.
10 # ====================================================================
12 # Performance improvement is not really impressive on pre-T1 CPU: +8%
13 # over Sun C and +25% over gcc [3.3]. While on T1, a.k.a. Niagara, it
14 # turned to be 40% faster than 64-bit code generated by Sun C 5.8 and
15 # >2x than 64-bit code generated by gcc 3.4. And there is a gimmick.
16 # X[16] vector is packed to 8 64-bit registers and as result nothing
17 # is spilled on stack. In addition input data is loaded in compact
18 # instruction sequence, thus minimizing the window when the code is
19 # subject to [inter-thread] cache-thrashing hazard. The goal is to
20 # ensure scalability on UltraSPARC T1, or rather to avoid decay when
21 # amount of active threads exceeds the number of physical cores.
23 # SPARC T4 SHA1 hardware achieves 3.72 cycles per byte, which is 3.1x
24 # faster than software. Multi-process benchmark saturates at 11x
25 # single-process result on 8-core processor, or ~9GBps per 2.85GHz
29 for (@ARGV) { $bits=64 if (/\-m64/ || /\-xarch\=v9/); }
30 if ($bits==64) { $bias=2047; $frame=192; }
31 else { $bias=0; $frame=112; }
34 open STDOUT,">$output";
36 @X=("%o0","%o1","%o2","%o3","%o4","%o5","%g1","%o7");
50 @K=($K_00_19,$K_20_39,$K_40_59,$K_60_79);
60 my ($i,$a,$b,$c,$d,$e)=@_;
61 my $xi=($i&1)?@X[($i/2)%8]:$Xi;
79 " srlx @X[(($i+1)/2)%8],32,$Xi\n";
87 my ($i,$a,$b,$c,$d,$e)=@_;
98 sllx @X[($j+6)%8],32,$Xi ! Xupdate($i)
99 xor @X[($j+1)%8],@X[$j%8],@X[$j%8]
100 srlx @X[($j+7)%8],32,$tmp1
101 xor @X[($j+4)%8],@X[$j%8],@X[$j%8]
104 add @K[$i/20],$e,$e !!
105 xor $Xi,@X[$j%8],@X[$j%8]
107 add @X[$j%8],@X[$j%8],@X[$j%8]
109 andn @X[$j%8],$rot1m,@X[$j%8]
111 or $Xi,@X[$j%8],@X[$j%8]
117 my ($i,$a,$b,$c,$d,$e)=@_;
124 $code.="\tsrlx @X[($i/2)%8],32,$xi\n";
141 my ($i,$a,$b,$c,$d,$e)=@_;
148 $code.="\tsrlx @X[($i/2)%8],32,$xi\n";
164 my ($i,$a,$b,$c,$d,$e)=@_;
171 $code.="\tsrlx @X[($i/2)%8],32,$xi\n";
188 $code.=<<___ if ($bits==64);
189 .register %g2,#scratch
190 .register %g3,#scratch
193 #include "sparc_arch.h"
195 .section ".text",#alloc,#execinstr
202 .globl sha1_block_data_order
203 sha1_block_data_order:
204 SPARC_LOAD_ADDRESS_LEAF(OPENSSL_sparcv9cap_P,%g1,%g5)
205 ld [%g1+4],%g1 ! OPENSSL_sparcv9cap_P[1]
207 andcc %g1, CFR_SHA1, %g0
211 ld [%o0 + 0x00], %f0 ! load context
216 bne,pn %icc, .Lhwunaligned
220 ldd [%o1 + 0x00], %f8
221 ldd [%o1 + 0x08], %f10
222 ldd [%o1 + 0x10], %f12
223 ldd [%o1 + 0x18], %f14
224 ldd [%o1 + 0x20], %f16
225 ldd [%o1 + 0x28], %f18
226 ldd [%o1 + 0x30], %f20
227 subcc %o2, 1, %o2 ! done yet?
228 ldd [%o1 + 0x38], %f22
231 .word 0x81b02820 ! SHA1
233 bne,pt `$bits==64?"%xcc":"%icc"`, .Lhw_loop
237 st %f0, [%o0 + 0x00] ! store context
246 alignaddr %o1, %g0, %o1
248 ldd [%o1 + 0x00], %f10
250 ldd [%o1 + 0x08], %f12
251 ldd [%o1 + 0x10], %f14
252 ldd [%o1 + 0x18], %f16
253 ldd [%o1 + 0x20], %f18
254 ldd [%o1 + 0x28], %f20
255 ldd [%o1 + 0x30], %f22
256 ldd [%o1 + 0x38], %f24
257 subcc %o2, 1, %o2 ! done yet?
258 ldd [%o1 + 0x40], %f26
261 faligndata %f10, %f12, %f8
262 faligndata %f12, %f14, %f10
263 faligndata %f14, %f16, %f12
264 faligndata %f16, %f18, %f14
265 faligndata %f18, %f20, %f16
266 faligndata %f20, %f22, %f18
267 faligndata %f22, %f24, %f20
268 faligndata %f24, %f26, %f22
270 .word 0x81b02820 ! SHA1
272 bne,pt `$bits==64?"%xcc":"%icc"`, .Lhwunaligned_loop
273 for %f26, %f26, %f10 ! %f10=%f26
285 sllx $rot1m,32,$rot1m
295 sethi %hi(0x5a827999),$K_00_19
296 or $K_00_19,%lo(0x5a827999),$K_00_19
297 sethi %hi(0x6ed9eba1),$K_20_39
298 or $K_20_39,%lo(0x6ed9eba1),$K_20_39
299 sethi %hi(0x8f1bbcdc),$K_40_59
300 or $K_40_59,%lo(0x8f1bbcdc),$K_40_59
301 sethi %hi(0xca62c1d6),$K_60_79
302 or $K_60_79,%lo(0xca62c1d6),$K_60_79
313 subcc %g0,$tmp1,$tmp2 ! should be 64-$tmp1, but -$tmp1 works too
318 sllx @X[0],$tmp1,@X[0]
319 ldx [$tmp0+64],$tmp64
323 srlx @X[$i+1],$tmp2,$Xi
324 sllx @X[$i+1],$tmp1,@X[$i+1]
329 srlx $tmp64,$tmp2,$tmp64
330 or $tmp64,@X[7],@X[7]
334 for ($i=0;$i<16;$i++) { &BODY_00_15($i,@V); unshift(@V,pop(@V)); }
335 for (;$i<20;$i++) { &BODY_16_19($i,@V); unshift(@V,pop(@V)); }
336 for (;$i<40;$i++) { &BODY_20_39($i,@V); unshift(@V,pop(@V)); }
337 for (;$i<60;$i++) { &BODY_40_59($i,@V); unshift(@V,pop(@V)); }
338 for (;$i<80;$i++) { &BODY_20_39($i,@V); unshift(@V,pop(@V)); }
360 bne `$bits==64?"%xcc":"%icc"`,.Lloop
365 .type sha1_block_data_order,#function
366 .size sha1_block_data_order,(.-sha1_block_data_order)
367 .asciz "SHA1 block transform for SPARCv9, CRYPTOGAMS by <appro\@openssl.org>"
371 # Purpose of these subroutines is to explicitly encode VIS instructions,
372 # so that one can compile the module without having to specify VIS
373 # extentions on compiler command line, e.g. -xarch=v9 vs. -xarch=v9a.
374 # Idea is to reserve for option to produce "universal" binary and let
375 # programmer detect if current CPU is VIS capable at run-time.
377 my ($mnemonic,$rs1,$rs2,$rd)=@_;
379 my %visopf = ( "faligndata" => 0x048,
382 $ref = "$mnemonic\t$rs1,$rs2,$rd";
384 if ($opf=$visopf{$mnemonic}) {
385 foreach ($rs1,$rs2,$rd) {
386 return $ref if (!/%f([0-9]{1,2})/);
389 return $ref if ($1&1);
390 # re-encode for upper double register addressing
395 return sprintf ".word\t0x%08x !%s",
396 0x81b00000|$rd<<25|$rs1<<14|$opf<<5|$rs2,
403 my ($mnemonic,$rs1,$rs2,$rd)=@_;
404 my %bias = ( "g" => 0, "o" => 8, "l" => 16, "i" => 24 );
405 my $ref="$mnemonic\t$rs1,$rs2,$rd";
407 foreach ($rs1,$rs2,$rd) {
408 if (/%([goli])([0-7])/) { $_=$bias{$1}+$2; }
409 else { return $ref; }
411 return sprintf ".word\t0x%08x !%s",
412 0x81b00300|$rd<<25|$rs1<<14|$rs2,
416 foreach (split("\n",$code)) {
417 s/\`([^\`]*)\`/eval $1/ge;
419 s/\b(f[^\s]*)\s+(%f[0-9]{1,2}),\s*(%f[0-9]{1,2}),\s*(%f[0-9]{1,2})/
422 s/\b(alignaddr)\s+(%[goli][0-7]),\s*(%[goli][0-7]),\s*(%[goli][0-7])/
423 &unalignaddr($1,$2,$3,$4)