2 # Copyright 2007-2016 The OpenSSL Project Authors. All Rights Reserved.
4 # Licensed under the OpenSSL license (the "License"). You may not use
5 # this file except in compliance with the License. You can obtain a copy
6 # in the file LICENSE in the source distribution or at
7 # https://www.openssl.org/source/license.html
10 # ====================================================================
11 # Written by Andy Polyakov <appro@openssl.org> for the OpenSSL
12 # project. The module is, however, dual licensed under OpenSSL and
13 # CRYPTOGAMS licenses depending on where you obtain it. For further
14 # details see http://www.openssl.org/~appro/cryptogams/.
16 # Hardware SPARC T4 support by David S. Miller
17 # ====================================================================
19 # Performance improvement is not really impressive on pre-T1 CPU: +8%
20 # over Sun C and +25% over gcc [3.3]. While on T1, a.k.a. Niagara, it
21 # turned to be 40% faster than 64-bit code generated by Sun C 5.8 and
22 # >2x than 64-bit code generated by gcc 3.4. And there is a gimmick.
23 # X[16] vector is packed to 8 64-bit registers and as result nothing
24 # is spilled on stack. In addition input data is loaded in compact
25 # instruction sequence, thus minimizing the window when the code is
26 # subject to [inter-thread] cache-thrashing hazard. The goal is to
27 # ensure scalability on UltraSPARC T1, or rather to avoid decay when
28 # amount of active threads exceeds the number of physical cores.
30 # SPARC T4 SHA1 hardware achieves 3.72 cycles per byte, which is 3.1x
31 # faster than software. Multi-process benchmark saturates at 11x
32 # single-process result on 8-core processor, or ~9GBps per 2.85GHz
36 open STDOUT,">$output";
38 @X=("%o0","%o1","%o2","%o3","%o4","%o5","%g1","%o7");
52 @K=($K_00_19,$K_20_39,$K_40_59,$K_60_79);
62 my ($i,$a,$b,$c,$d,$e)=@_;
63 my $xi=($i&1)?@X[($i/2)%8]:$Xi;
81 " srlx @X[(($i+1)/2)%8],32,$Xi\n";
89 my ($i,$a,$b,$c,$d,$e)=@_;
100 sllx @X[($j+6)%8],32,$Xi ! Xupdate($i)
101 xor @X[($j+1)%8],@X[$j%8],@X[$j%8]
102 srlx @X[($j+7)%8],32,$tmp1
103 xor @X[($j+4)%8],@X[$j%8],@X[$j%8]
106 add @K[$i/20],$e,$e !!
107 xor $Xi,@X[$j%8],@X[$j%8]
109 add @X[$j%8],@X[$j%8],@X[$j%8]
111 andn @X[$j%8],$rot1m,@X[$j%8]
113 or $Xi,@X[$j%8],@X[$j%8]
119 my ($i,$a,$b,$c,$d,$e)=@_;
126 $code.="\tsrlx @X[($i/2)%8],32,$xi\n";
143 my ($i,$a,$b,$c,$d,$e)=@_;
150 $code.="\tsrlx @X[($i/2)%8],32,$xi\n";
166 my ($i,$a,$b,$c,$d,$e)=@_;
173 $code.="\tsrlx @X[($i/2)%8],32,$xi\n";
191 #include "sparc_arch.h"
194 .register %g2,#scratch
195 .register %g3,#scratch
198 .section ".text",#alloc,#execinstr
205 .globl sha1_block_data_order
206 sha1_block_data_order:
207 SPARC_LOAD_ADDRESS_LEAF(OPENSSL_sparcv9cap_P,%g1,%g5)
208 ld [%g1+4],%g1 ! OPENSSL_sparcv9cap_P[1]
210 andcc %g1, CFR_SHA1, %g0
214 ld [%o0 + 0x00], %f0 ! load context
219 bne,pn %icc, .Lhwunaligned
223 ldd [%o1 + 0x00], %f8
224 ldd [%o1 + 0x08], %f10
225 ldd [%o1 + 0x10], %f12
226 ldd [%o1 + 0x18], %f14
227 ldd [%o1 + 0x20], %f16
228 ldd [%o1 + 0x28], %f18
229 ldd [%o1 + 0x30], %f20
230 subcc %o2, 1, %o2 ! done yet?
231 ldd [%o1 + 0x38], %f22
233 prefetch [%o1 + 63], 20
235 .word 0x81b02820 ! SHA1
237 bne,pt SIZE_T_CC, .Lhw_loop
241 st %f0, [%o0 + 0x00] ! store context
250 alignaddr %o1, %g0, %o1
252 ldd [%o1 + 0x00], %f10
254 ldd [%o1 + 0x08], %f12
255 ldd [%o1 + 0x10], %f14
256 ldd [%o1 + 0x18], %f16
257 ldd [%o1 + 0x20], %f18
258 ldd [%o1 + 0x28], %f20
259 ldd [%o1 + 0x30], %f22
260 ldd [%o1 + 0x38], %f24
261 subcc %o2, 1, %o2 ! done yet?
262 ldd [%o1 + 0x40], %f26
264 prefetch [%o1 + 63], 20
266 faligndata %f10, %f12, %f8
267 faligndata %f12, %f14, %f10
268 faligndata %f14, %f16, %f12
269 faligndata %f16, %f18, %f14
270 faligndata %f18, %f20, %f16
271 faligndata %f20, %f22, %f18
272 faligndata %f22, %f24, %f20
273 faligndata %f24, %f26, %f22
275 .word 0x81b02820 ! SHA1
277 bne,pt SIZE_T_CC, .Lhwunaligned_loop
278 for %f26, %f26, %f10 ! %f10=%f26
285 save %sp,-STACK_FRAME,%sp
290 sllx $rot1m,32,$rot1m
300 sethi %hi(0x5a827999),$K_00_19
301 or $K_00_19,%lo(0x5a827999),$K_00_19
302 sethi %hi(0x6ed9eba1),$K_20_39
303 or $K_20_39,%lo(0x6ed9eba1),$K_20_39
304 sethi %hi(0x8f1bbcdc),$K_40_59
305 or $K_40_59,%lo(0x8f1bbcdc),$K_40_59
306 sethi %hi(0xca62c1d6),$K_60_79
307 or $K_60_79,%lo(0xca62c1d6),$K_60_79
318 subcc %g0,$tmp1,$tmp2 ! should be 64-$tmp1, but -$tmp1 works too
323 sllx @X[0],$tmp1,@X[0]
324 ldx [$tmp0+64],$tmp64
328 srlx @X[$i+1],$tmp2,$Xi
329 sllx @X[$i+1],$tmp1,@X[$i+1]
334 srlx $tmp64,$tmp2,$tmp64
335 or $tmp64,@X[7],@X[7]
339 for ($i=0;$i<16;$i++) { &BODY_00_15($i,@V); unshift(@V,pop(@V)); }
340 for (;$i<20;$i++) { &BODY_16_19($i,@V); unshift(@V,pop(@V)); }
341 for (;$i<40;$i++) { &BODY_20_39($i,@V); unshift(@V,pop(@V)); }
342 for (;$i<60;$i++) { &BODY_40_59($i,@V); unshift(@V,pop(@V)); }
343 for (;$i<80;$i++) { &BODY_20_39($i,@V); unshift(@V,pop(@V)); }
370 .type sha1_block_data_order,#function
371 .size sha1_block_data_order,(.-sha1_block_data_order)
372 .asciz "SHA1 block transform for SPARCv9, CRYPTOGAMS by <appro\@openssl.org>"
376 # Purpose of these subroutines is to explicitly encode VIS instructions,
377 # so that one can compile the module without having to specify VIS
378 # extensions on compiler command line, e.g. -xarch=v9 vs. -xarch=v9a.
379 # Idea is to reserve for option to produce "universal" binary and let
380 # programmer detect if current CPU is VIS capable at run-time.
382 my ($mnemonic,$rs1,$rs2,$rd)=@_;
384 my %visopf = ( "faligndata" => 0x048,
387 $ref = "$mnemonic\t$rs1,$rs2,$rd";
389 if ($opf=$visopf{$mnemonic}) {
390 foreach ($rs1,$rs2,$rd) {
391 return $ref if (!/%f([0-9]{1,2})/);
394 return $ref if ($1&1);
395 # re-encode for upper double register addressing
400 return sprintf ".word\t0x%08x !%s",
401 0x81b00000|$rd<<25|$rs1<<14|$opf<<5|$rs2,
408 my ($mnemonic,$rs1,$rs2,$rd)=@_;
409 my %bias = ( "g" => 0, "o" => 8, "l" => 16, "i" => 24 );
410 my $ref="$mnemonic\t$rs1,$rs2,$rd";
412 foreach ($rs1,$rs2,$rd) {
413 if (/%([goli])([0-7])/) { $_=$bias{$1}+$2; }
414 else { return $ref; }
416 return sprintf ".word\t0x%08x !%s",
417 0x81b00300|$rd<<25|$rs1<<14|$rs2,
421 foreach (split("\n",$code)) {
422 s/\`([^\`]*)\`/eval $1/ge;
424 s/\b(f[^\s]*)\s+(%f[0-9]{1,2}),\s*(%f[0-9]{1,2}),\s*(%f[0-9]{1,2})/
427 s/\b(alignaddr)\s+(%[goli][0-7]),\s*(%[goli][0-7]),\s*(%[goli][0-7])/
428 &unalignaddr($1,$2,$3,$4)