f661e96524eb2b1f77bc6cd376917d14fd6b79ed
[openssl.git] / crypto / sha / asm / sha1-sparcv9.pl
1 #!/usr/bin/env perl
2
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/.
8 #
9 # Hardware SPARC T4 support by David S. Miller <davem@davemloft.net>.
10 # ====================================================================
11
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.
22
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
26 # socket.
27
28 $bits=32;
29 for (@ARGV)     { $bits=64 if (/\-m64/ || /\-xarch\=v9/); }
30 if ($bits==64)  { $bias=2047; $frame=192; }
31 else            { $bias=0;    $frame=112; }
32
33 $output=shift;
34 open STDOUT,">$output";
35
36 @X=("%o0","%o1","%o2","%o3","%o4","%o5","%g1","%o7");
37 $rot1m="%g2";
38 $tmp64="%g3";
39 $Xi="%g4";
40 $A="%l0";
41 $B="%l1";
42 $C="%l2";
43 $D="%l3";
44 $E="%l4";
45 @V=($A,$B,$C,$D,$E);
46 $K_00_19="%l5";
47 $K_20_39="%l6";
48 $K_40_59="%l7";
49 $K_60_79="%g5";
50 @K=($K_00_19,$K_20_39,$K_40_59,$K_60_79);
51
52 $ctx="%i0";
53 $inp="%i1";
54 $len="%i2";
55 $tmp0="%i3";
56 $tmp1="%i4";
57 $tmp2="%i5";
58
59 sub BODY_00_15 {
60 my ($i,$a,$b,$c,$d,$e)=@_;
61 my $xi=($i&1)?@X[($i/2)%8]:$Xi;
62
63 $code.=<<___;
64         sll     $a,5,$tmp0              !! $i
65         add     @K[$i/20],$e,$e
66         srl     $a,27,$tmp1
67         add     $tmp0,$e,$e
68         and     $c,$b,$tmp0
69         add     $tmp1,$e,$e
70         sll     $b,30,$tmp2
71         andn    $d,$b,$tmp1
72         srl     $b,2,$b
73         or      $tmp1,$tmp0,$tmp1
74         or      $tmp2,$b,$b
75         add     $xi,$e,$e
76 ___
77 if ($i&1 && $i<15) {
78         $code.=
79         "       srlx    @X[(($i+1)/2)%8],32,$Xi\n";
80 }
81 $code.=<<___;
82         add     $tmp1,$e,$e
83 ___
84 }
85
86 sub Xupdate {
87 my ($i,$a,$b,$c,$d,$e)=@_;
88 my $j=$i/2;
89
90 if ($i&1) {
91 $code.=<<___;
92         sll     $a,5,$tmp0              !! $i
93         add     @K[$i/20],$e,$e
94         srl     $a,27,$tmp1
95 ___
96 } else {
97 $code.=<<___;
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]
102         sll     $a,5,$tmp0              !! $i
103         or      $tmp1,$Xi,$Xi
104         add     @K[$i/20],$e,$e         !!
105         xor     $Xi,@X[$j%8],@X[$j%8]
106         srlx    @X[$j%8],31,$Xi
107         add     @X[$j%8],@X[$j%8],@X[$j%8]
108         and     $Xi,$rot1m,$Xi
109         andn    @X[$j%8],$rot1m,@X[$j%8]
110         srl     $a,27,$tmp1             !!
111         or      $Xi,@X[$j%8],@X[$j%8]
112 ___
113 }
114 }
115
116 sub BODY_16_19 {
117 my ($i,$a,$b,$c,$d,$e)=@_;
118
119         &Xupdate(@_);
120     if ($i&1) {
121         $xi=@X[($i/2)%8];
122     } else {
123         $xi=$Xi;
124         $code.="\tsrlx  @X[($i/2)%8],32,$xi\n";
125     }
126 $code.=<<___;
127         add     $tmp0,$e,$e             !!
128         and     $c,$b,$tmp0
129         add     $tmp1,$e,$e
130         sll     $b,30,$tmp2
131         add     $xi,$e,$e
132         andn    $d,$b,$tmp1
133         srl     $b,2,$b
134         or      $tmp1,$tmp0,$tmp1
135         or      $tmp2,$b,$b
136         add     $tmp1,$e,$e
137 ___
138 }
139
140 sub BODY_20_39 {
141 my ($i,$a,$b,$c,$d,$e)=@_;
142 my $xi;
143         &Xupdate(@_);
144     if ($i&1) {
145         $xi=@X[($i/2)%8];
146     } else {
147         $xi=$Xi;
148         $code.="\tsrlx  @X[($i/2)%8],32,$xi\n";
149     }
150 $code.=<<___;
151         add     $tmp0,$e,$e             !!
152         xor     $c,$b,$tmp0
153         add     $tmp1,$e,$e
154         sll     $b,30,$tmp2
155         xor     $d,$tmp0,$tmp1
156         srl     $b,2,$b
157         add     $tmp1,$e,$e
158         or      $tmp2,$b,$b
159         add     $xi,$e,$e
160 ___
161 }
162
163 sub BODY_40_59 {
164 my ($i,$a,$b,$c,$d,$e)=@_;
165 my $xi;
166         &Xupdate(@_);
167     if ($i&1) {
168         $xi=@X[($i/2)%8];
169     } else {
170         $xi=$Xi;
171         $code.="\tsrlx  @X[($i/2)%8],32,$xi\n";
172     }
173 $code.=<<___;
174         add     $tmp0,$e,$e             !!
175         and     $c,$b,$tmp0
176         add     $tmp1,$e,$e
177         sll     $b,30,$tmp2
178         or      $c,$b,$tmp1
179         srl     $b,2,$b
180         and     $d,$tmp1,$tmp1
181         add     $xi,$e,$e
182         or      $tmp1,$tmp0,$tmp1
183         or      $tmp2,$b,$b
184         add     $tmp1,$e,$e
185 ___
186 }
187
188 $code.=<<___ if ($bits==64);
189 .register       %g2,#scratch
190 .register       %g3,#scratch
191 ___
192 $code.=<<___;
193 #include "sparc_arch.h"
194
195 .section        ".text",#alloc,#execinstr
196
197 #ifdef __PIC__
198 SPARC_PIC_THUNK(%g1)
199 #endif
200
201 .align  32
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]
206
207         andcc   %g1, CFR_SHA1, %g0
208         be      .Lsoftware
209         nop
210
211         ld      [%o0 + 0x00], %f0       ! load context
212         ld      [%o0 + 0x04], %f1
213         ld      [%o0 + 0x08], %f2
214         andcc   %o1, 0x7, %g0
215         ld      [%o0 + 0x0c], %f3
216         bne,pn  %icc, .Lhwunaligned
217          ld     [%o0 + 0x10], %f4
218
219 .Lhw_loop:
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
229         add     %o1, 0x40, %o1
230
231         .word   0x81b02820              ! SHA1
232
233         bne,pt  `$bits==64?"%xcc":"%icc"`, .Lhw_loop
234         nop
235
236 .Lhwfinish:
237         st      %f0, [%o0 + 0x00]       ! store context
238         st      %f1, [%o0 + 0x04]
239         st      %f2, [%o0 + 0x08]
240         st      %f3, [%o0 + 0x0c]
241         retl
242         st      %f4, [%o0 + 0x10]
243
244 .align  8
245 .Lhwunaligned:
246         alignaddr %o1, %g0, %o1
247
248         ldd     [%o1 + 0x00], %f10
249 .Lhwunaligned_loop:
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
259         add     %o1, 0x40, %o1
260
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
269
270         .word   0x81b02820              ! SHA1
271
272         bne,pt  `$bits==64?"%xcc":"%icc"`, .Lhwunaligned_loop
273         for     %f26, %f26, %f10        ! %f10=%f26
274
275         ba      .Lhwfinish
276         nop
277
278 .align  16
279 .Lsoftware:
280         save    %sp,-$frame,%sp
281         sllx    $len,6,$len
282         add     $inp,$len,$len
283
284         or      %g0,1,$rot1m
285         sllx    $rot1m,32,$rot1m
286         or      $rot1m,1,$rot1m
287
288         ld      [$ctx+0],$A
289         ld      [$ctx+4],$B
290         ld      [$ctx+8],$C
291         ld      [$ctx+12],$D
292         ld      [$ctx+16],$E
293         andn    $inp,7,$tmp0
294
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
303
304 .Lloop:
305         ldx     [$tmp0+0],@X[0]
306         ldx     [$tmp0+16],@X[2]
307         ldx     [$tmp0+32],@X[4]
308         ldx     [$tmp0+48],@X[6]
309         and     $inp,7,$tmp1
310         ldx     [$tmp0+8],@X[1]
311         sll     $tmp1,3,$tmp1
312         ldx     [$tmp0+24],@X[3]
313         subcc   %g0,$tmp1,$tmp2 ! should be 64-$tmp1, but -$tmp1 works too
314         ldx     [$tmp0+40],@X[5]
315         bz,pt   %icc,.Laligned
316         ldx     [$tmp0+56],@X[7]
317
318         sllx    @X[0],$tmp1,@X[0]
319         ldx     [$tmp0+64],$tmp64
320 ___
321 for($i=0;$i<7;$i++)
322 {   $code.=<<___;
323         srlx    @X[$i+1],$tmp2,$Xi
324         sllx    @X[$i+1],$tmp1,@X[$i+1]
325         or      $Xi,@X[$i],@X[$i]
326 ___
327 }
328 $code.=<<___;
329         srlx    $tmp64,$tmp2,$tmp64
330         or      $tmp64,@X[7],@X[7]
331 .Laligned:
332         srlx    @X[0],32,$Xi
333 ___
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)); }
339 $code.=<<___;
340
341         ld      [$ctx+0],@X[0]
342         ld      [$ctx+4],@X[1]
343         ld      [$ctx+8],@X[2]
344         ld      [$ctx+12],@X[3]
345         add     $inp,64,$inp
346         ld      [$ctx+16],@X[4]
347         cmp     $inp,$len
348
349         add     $A,@X[0],$A
350         st      $A,[$ctx+0]
351         add     $B,@X[1],$B
352         st      $B,[$ctx+4]
353         add     $C,@X[2],$C
354         st      $C,[$ctx+8]
355         add     $D,@X[3],$D
356         st      $D,[$ctx+12]
357         add     $E,@X[4],$E
358         st      $E,[$ctx+16]
359
360         bne     `$bits==64?"%xcc":"%icc"`,.Lloop
361         andn    $inp,7,$tmp0
362
363         ret
364         restore
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>"
368 .align  4
369 ___
370
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.
376 sub unvis {
377 my ($mnemonic,$rs1,$rs2,$rd)=@_;
378 my $ref,$opf;
379 my %visopf = (  "faligndata"    => 0x048,
380                 "for"           => 0x07c        );
381
382     $ref = "$mnemonic\t$rs1,$rs2,$rd";
383
384     if ($opf=$visopf{$mnemonic}) {
385         foreach ($rs1,$rs2,$rd) {
386             return $ref if (!/%f([0-9]{1,2})/);
387             $_=$1;
388             if ($1>=32) {
389                 return $ref if ($1&1);
390                 # re-encode for upper double register addressing
391                 $_=($1|$1>>5)&31;
392             }
393         }
394
395         return  sprintf ".word\t0x%08x !%s",
396                         0x81b00000|$rd<<25|$rs1<<14|$opf<<5|$rs2,
397                         $ref;
398     } else {
399         return $ref;
400     }
401 }
402 sub unalignaddr {
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";
406
407     foreach ($rs1,$rs2,$rd) {
408         if (/%([goli])([0-7])/) { $_=$bias{$1}+$2; }
409         else                    { return $ref; }
410     }
411     return  sprintf ".word\t0x%08x !%s",
412                     0x81b00300|$rd<<25|$rs1<<14|$rs2,
413                     $ref;
414 }
415
416 foreach (split("\n",$code)) {
417         s/\`([^\`]*)\`/eval $1/ge;
418
419         s/\b(f[^\s]*)\s+(%f[0-9]{1,2}),\s*(%f[0-9]{1,2}),\s*(%f[0-9]{1,2})/
420                 &unvis($1,$2,$3,$4)
421          /ge;
422         s/\b(alignaddr)\s+(%[goli][0-7]),\s*(%[goli][0-7]),\s*(%[goli][0-7])/
423                 &unalignaddr($1,$2,$3,$4)
424          /ge;
425
426         print $_,"\n";
427 }
428
429 close STDOUT;