6902b52b24fb6234e7f24c4cac87f59757036b83
[openssl.git] / crypto / sha / asm / sha1-sparcv9a.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
10 # January 2009
11 #
12 # Provided that UltraSPARC VIS instructions are pipe-lined(*) and
13 # pairable(*) with IALU ones, offloading of Xupdate to the UltraSPARC
14 # Graphic Unit would make it possible to achieve higher instruction-
15 # level parallelism, ILP, and thus higher performance. It should be
16 # explicitly noted that ILP is the keyword, and it means that this
17 # code would be unsuitable for cores like UltraSPARC-Tx. The idea is
18 # not really novel, Sun had VIS-powered implementation for a while.
19 # Unlike Sun's implementation this one can process multiple unaligned
20 # input blocks, and as such works as drop-in replacement for OpenSSL
21 # sha1_block_data_order. Performance improvement was measured to be
22 # 40% over pure IALU sha1-sparcv9.pl on UltraSPARC-IIi, but 12% on
23 # UltraSPARC-III. See below for discussion...
24 #
25 # (*)   "Pipe-lined" means that even if it takes several cycles to
26 #       complete, next instruction using same functional unit [but not
27 #       depending on the result of the current instruction] can start
28 #       execution without having to wait for the unit. "Pairable"
29 #       means that two [or more] independent instructions can be
30 #       issued at the very same time.
31
32 $bits=32;
33 for (@ARGV)     { $bits=64 if (/\-m64/ || /\-xarch\=v9/); }
34 if ($bits==64)  { $bias=2047; $frame=192; }
35 else            { $bias=0;    $frame=112; }
36
37 $output=shift;
38 open STDOUT,">$output";
39
40 $ctx="%i0";
41 $inp="%i1";
42 $len="%i2";
43 $tmp0="%i3";
44 $tmp1="%i4";
45 $tmp2="%i5";
46 $tmp3="%g5";
47
48 $base="%g1";
49 $align="%g4";
50 $Xfer="%o5";
51 $nXfer=$tmp3;
52 $Xi="%o7";
53
54 $A="%l0";
55 $B="%l1";
56 $C="%l2";
57 $D="%l3";
58 $E="%l4";
59 @V=($A,$B,$C,$D,$E);
60
61 $Actx="%o0";
62 $Bctx="%o1";
63 $Cctx="%o2";
64 $Dctx="%o3";
65 $Ectx="%o4";
66
67 $fmul="%f32";
68 $VK_00_19="%f34";
69 $VK_20_39="%f36";
70 $VK_40_59="%f38";
71 $VK_60_79="%f40";
72 @VK=($VK_00_19,$VK_20_39,$VK_40_59,$VK_60_79);
73 @X=("%f0", "%f1", "%f2", "%f3", "%f4", "%f5", "%f6", "%f7",
74     "%f8", "%f9","%f10","%f11","%f12","%f13","%f14","%f15","%f16");
75
76 # This is reference 2x-parallelized VIS-powered Xupdate procedure. It
77 # covers even K_NN_MM addition...
78 sub Xupdate {
79 my ($i)=@_;
80 my $K=@VK[($i+16)/20];
81 my $j=($i+16)%16;
82
83 #       [ provided that GSR.alignaddr_offset is 5, $mul contains
84 #         0x100ULL<<32|0x100 value and K_NN_MM are pre-loaded to
85 #         chosen registers... ]
86 $code.=<<___;
87         fxors           @X[($j+13)%16],@X[$j],@X[$j]    !-1/-1/-1:X[0]^=X[13]
88         fxors           @X[($j+14)%16],@X[$j+1],@X[$j+1]! 0/ 0/ 0:X[1]^=X[14]
89         fxor            @X[($j+2)%16],@X[($j+8)%16],%f18! 1/ 1/ 1:Tmp=X[2,3]^X[8,9]
90         fxor            %f18,@X[$j],@X[$j]              ! 2/ 4/ 3:X[0,1]^=X[2,3]^X[8,9]
91         faligndata      @X[$j],@X[$j],%f18              ! 3/ 7/ 5:Tmp=X[0,1]>>>24
92         fpadd32         @X[$j],@X[$j],@X[$j]            ! 4/ 8/ 6:X[0,1]<<=1
93         fmul8ulx16      %f18,$fmul,%f18                 ! 5/10/ 7:Tmp>>=7, Tmp&=1
94         ![fxors         %f15,%f2,%f2]
95         for             %f18,@X[$j],@X[$j]              ! 8/14/10:X[0,1]|=Tmp
96         ![fxors         %f0,%f3,%f3]                    !10/17/12:X[0] dependency
97         fpadd32         $K,@X[$j],%f20
98         std             %f20,[$Xfer+`4*$j`]
99 ___
100 # The numbers delimited with slash are the earliest possible dispatch
101 # cycles for given instruction assuming 1 cycle latency for simple VIS
102 # instructions, such as on UltraSPARC-I&II, 3 cycles latency, such as
103 # on UltraSPARC-III&IV, and 2 cycles latency, such as on SPARC64-V[?],
104 # respectively. Being 2x-parallelized the procedure is "worth" 5, 8.5
105 # or 6 ticks per SHA1 round. As FPU/VIS instructions are perfectly
106 # pairable with IALU ones, the round timing is defined by the maximum
107 # between VIS and IALU timings. The latter varies from round to round
108 # and averages out at 6.25 ticks. This means that USI&II and SPARC64-V
109 # should operate at IALU rate, while USIII&IV - at VIS rate. This
110 # explains why performance improvement varies among processors. Well,
111 # it should be noted that pure IALU sha1-sparcv9.pl module exhibits
112 # virtually uniform performance of ~9.3 cycles per SHA1 round. Timings
113 # mentioned above are theoretical lower limits. Real-life performance
114 # was measured to be 6.6 cycles per SHA1 round on USIIi and 8.3 on
115 # USIII. The latter means that processor manual must have an error in
116 # instruction latency table or there is some unmentioned shortcut...
117 }
118
119 # The reference Xupdate procedure is then "strained" over *pairs* of
120 # BODY_NN_MM and kind of modulo-scheduled in respect to X[n]^=X[n+13]
121 # and K_NN_MM addition. It's "running" 15 rounds ahead, which leaves
122 # plenty of room to amortize for read-after-write hazard, as well as
123 # to fetch and align input for the next spin. The VIS instructions are
124 # scheduled for latency of 2 cycles, because there are not enough IALU
125 # instructions to schedule for latency of 3, while scheduling for 1
126 # would give no gain on USI&II, but loss on SPARC64-V.
127
128 sub BODY_00_19 {
129 my ($i,$a,$b,$c,$d,$e)=@_;
130 my $j=$i&~1;
131 my $k=($j+16+2)%16;     # ahead reference
132 my $l=($j+16-2)%16;     # behind reference
133 my $K=@VK[($j+16-2)/20];
134
135 $j=($j+16)%16;
136
137 $code.=<<___ if (!($i&1));
138         sll             $a,5,$tmp0                      !! $i
139         and             $c,$b,$tmp3
140         ld              [$Xfer+`4*($i%16)`],$Xi
141          fxors          @X[($j+14)%16],@X[$j+1],@X[$j+1]! 0/ 0/ 0:X[1]^=X[14]
142         srl             $a,27,$tmp1
143         add             $tmp0,$e,$e
144          fxor           @X[($j+2)%16],@X[($j+8)%16],%f18! 1/ 1/ 1:Tmp=X[2,3]^X[8,9]
145         sll             $b,30,$tmp2
146         add             $tmp1,$e,$e
147         andn            $d,$b,$tmp1
148         add             $Xi,$e,$e
149          fxor           %f18,@X[$j],@X[$j]              ! 2/ 4/ 3:X[0,1]^=X[2,3]^X[8,9]
150         srl             $b,2,$b
151         or              $tmp1,$tmp3,$tmp1
152         or              $tmp2,$b,$b
153         add             $tmp1,$e,$e
154          faligndata     @X[$j],@X[$j],%f18              ! 3/ 7/ 5:Tmp=X[0,1]>>>24
155 ___
156 $code.=<<___ if ($i&1);
157         sll             $a,5,$tmp0                      !! $i
158         and             $c,$b,$tmp3
159         ld              [$Xfer+`4*($i%16)`],$Xi
160          fpadd32        @X[$j],@X[$j],@X[$j]            ! 4/ 8/ 6:X[0,1]<<=1
161         srl             $a,27,$tmp1
162         add             $tmp0,$e,$e
163          fmul8ulx16     %f18,$fmul,%f18                 ! 5/10/ 7:Tmp>>=7, Tmp&=1
164         sll             $b,30,$tmp2
165         add             $tmp1,$e,$e
166          fpadd32        $K,@X[$l],%f20                  !
167         andn            $d,$b,$tmp1
168         add             $Xi,$e,$e
169          fxors          @X[($k+13)%16],@X[$k],@X[$k]    !-1/-1/-1:X[0]^=X[13]
170         srl             $b,2,$b
171         or              $tmp1,$tmp3,$tmp1
172          fxor           %f18,@X[$j],@X[$j]              ! 8/14/10:X[0,1]|=Tmp
173         or              $tmp2,$b,$b
174         add             $tmp1,$e,$e
175 ___
176 $code.=<<___ if ($i&1 && $i>=2);
177          std            %f20,[$Xfer+`4*$l`]             !
178 ___
179 }
180
181 sub BODY_20_39 {
182 my ($i,$a,$b,$c,$d,$e)=@_;
183 my $j=$i&~1;
184 my $k=($j+16+2)%16;     # ahead reference
185 my $l=($j+16-2)%16;     # behind reference
186 my $K=@VK[($j+16-2)/20];
187
188 $j=($j+16)%16;
189
190 $code.=<<___ if (!($i&1) && $i<64);
191         sll             $a,5,$tmp0                      !! $i
192         ld              [$Xfer+`4*($i%16)`],$Xi
193          fxors          @X[($j+14)%16],@X[$j+1],@X[$j+1]! 0/ 0/ 0:X[1]^=X[14]
194         srl             $a,27,$tmp1
195         add             $tmp0,$e,$e
196          fxor           @X[($j+2)%16],@X[($j+8)%16],%f18! 1/ 1/ 1:Tmp=X[2,3]^X[8,9]
197         xor             $c,$b,$tmp0
198         add             $tmp1,$e,$e
199         sll             $b,30,$tmp2
200         xor             $d,$tmp0,$tmp1
201          fxor           %f18,@X[$j],@X[$j]              ! 2/ 4/ 3:X[0,1]^=X[2,3]^X[8,9]
202         srl             $b,2,$b
203         add             $tmp1,$e,$e
204         or              $tmp2,$b,$b
205         add             $Xi,$e,$e
206          faligndata     @X[$j],@X[$j],%f18              ! 3/ 7/ 5:Tmp=X[0,1]>>>24
207 ___
208 $code.=<<___ if ($i&1 && $i<64);
209         sll             $a,5,$tmp0                      !! $i
210         ld              [$Xfer+`4*($i%16)`],$Xi
211          fpadd32        @X[$j],@X[$j],@X[$j]            ! 4/ 8/ 6:X[0,1]<<=1
212         srl             $a,27,$tmp1
213         add             $tmp0,$e,$e
214          fmul8ulx16     %f18,$fmul,%f18                 ! 5/10/ 7:Tmp>>=7, Tmp&=1
215         xor             $c,$b,$tmp0
216         add             $tmp1,$e,$e
217          fpadd32        $K,@X[$l],%f20                  !
218         sll             $b,30,$tmp2
219         xor             $d,$tmp0,$tmp1
220          fxors          @X[($k+13)%16],@X[$k],@X[$k]    !-1/-1/-1:X[0]^=X[13]
221         srl             $b,2,$b
222         add             $tmp1,$e,$e
223          fxor           %f18,@X[$j],@X[$j]              ! 8/14/10:X[0,1]|=Tmp
224         or              $tmp2,$b,$b
225         add             $Xi,$e,$e
226          std            %f20,[$Xfer+`4*$l`]             !
227 ___
228 $code.=<<___ if ($i==64);
229         sll             $a,5,$tmp0                      !! $i
230         ld              [$Xfer+`4*($i%16)`],$Xi
231          fpadd32        $K,@X[$l],%f20
232         srl             $a,27,$tmp1
233         add             $tmp0,$e,$e
234         xor             $c,$b,$tmp0
235         add             $tmp1,$e,$e
236         sll             $b,30,$tmp2
237         xor             $d,$tmp0,$tmp1
238          std            %f20,[$Xfer+`4*$l`]
239         srl             $b,2,$b
240         add             $tmp1,$e,$e
241         or              $tmp2,$b,$b
242         add             $Xi,$e,$e
243 ___
244 $code.=<<___ if ($i>64);
245         sll             $a,5,$tmp0                      !! $i
246         ld              [$Xfer+`4*($i%16)`],$Xi
247         srl             $a,27,$tmp1
248         add             $tmp0,$e,$e
249         xor             $c,$b,$tmp0
250         add             $tmp1,$e,$e
251         sll             $b,30,$tmp2
252         xor             $d,$tmp0,$tmp1
253         srl             $b,2,$b
254         add             $tmp1,$e,$e
255         or              $tmp2,$b,$b
256         add             $Xi,$e,$e
257 ___
258 }
259
260 sub BODY_40_59 {
261 my ($i,$a,$b,$c,$d,$e)=@_;
262 my $j=$i&~1;
263 my $k=($j+16+2)%16;     # ahead reference
264 my $l=($j+16-2)%16;     # behind reference
265 my $K=@VK[($j+16-2)/20];
266
267 $j=($j+16)%16;
268
269 $code.=<<___ if (!($i&1));
270         sll             $a,5,$tmp0                      !! $i
271         ld              [$Xfer+`4*($i%16)`],$Xi
272          fxors          @X[($j+14)%16],@X[$j+1],@X[$j+1]! 0/ 0/ 0:X[1]^=X[14]
273         srl             $a,27,$tmp1
274         add             $tmp0,$e,$e
275          fxor           @X[($j+2)%16],@X[($j+8)%16],%f18! 1/ 1/ 1:Tmp=X[2,3]^X[8,9]
276         and             $c,$b,$tmp0
277         add             $tmp1,$e,$e
278         sll             $b,30,$tmp2
279         or              $c,$b,$tmp1
280          fxor           %f18,@X[$j],@X[$j]              ! 2/ 4/ 3:X[0,1]^=X[2,3]^X[8,9]
281         srl             $b,2,$b
282         and             $d,$tmp1,$tmp1
283         add             $Xi,$e,$e
284         or              $tmp1,$tmp0,$tmp1
285          faligndata     @X[$j],@X[$j],%f18              ! 3/ 7/ 5:Tmp=X[0,1]>>>24
286         or              $tmp2,$b,$b
287         add             $tmp1,$e,$e
288          fpadd32        @X[$j],@X[$j],@X[$j]            ! 4/ 8/ 6:X[0,1]<<=1
289 ___
290 $code.=<<___ if ($i&1);
291         sll             $a,5,$tmp0                      !! $i
292         ld              [$Xfer+`4*($i%16)`],$Xi
293         srl             $a,27,$tmp1
294         add             $tmp0,$e,$e
295          fmul8ulx16     %f18,$fmul,%f18                 ! 5/10/ 7:Tmp>>=7, Tmp&=1
296         and             $c,$b,$tmp0
297         add             $tmp1,$e,$e
298          fpadd32        $K,@X[$l],%f20                  !
299         sll             $b,30,$tmp2
300         or              $c,$b,$tmp1
301          fxors          @X[($k+13)%16],@X[$k],@X[$k]    !-1/-1/-1:X[0]^=X[13]
302         srl             $b,2,$b
303         and             $d,$tmp1,$tmp1
304          fxor           %f18,@X[$j],@X[$j]              ! 8/14/10:X[0,1]|=Tmp
305         add             $Xi,$e,$e
306         or              $tmp1,$tmp0,$tmp1
307         or              $tmp2,$b,$b
308         add             $tmp1,$e,$e
309          std            %f20,[$Xfer+`4*$l`]             !
310 ___
311 }
312
313 # If there is more data to process, then we pre-fetch the data for
314 # next iteration in last ten rounds...
315 sub BODY_70_79 {
316 my ($i,$a,$b,$c,$d,$e)=@_;
317 my $j=$i&~1;
318 my $m=($i%8)*2;
319
320 $j=($j+16)%16;
321
322 $code.=<<___ if ($i==70);
323         sll             $a,5,$tmp0                      !! $i
324         ld              [$Xfer+`4*($i%16)`],$Xi
325         srl             $a,27,$tmp1
326         add             $tmp0,$e,$e
327          ldd            [$inp+64],@X[0]
328         xor             $c,$b,$tmp0
329         add             $tmp1,$e,$e
330         sll             $b,30,$tmp2
331         xor             $d,$tmp0,$tmp1
332         srl             $b,2,$b
333         add             $tmp1,$e,$e
334         or              $tmp2,$b,$b
335         add             $Xi,$e,$e
336
337         and             $inp,-64,$nXfer
338         inc             64,$inp
339         and             $nXfer,255,$nXfer
340         alignaddr       %g0,$align,%g0
341         add             $base,$nXfer,$nXfer
342 ___
343 $code.=<<___ if ($i==71);
344         sll             $a,5,$tmp0                      !! $i
345         ld              [$Xfer+`4*($i%16)`],$Xi
346         srl             $a,27,$tmp1
347         add             $tmp0,$e,$e
348         xor             $c,$b,$tmp0
349         add             $tmp1,$e,$e
350         sll             $b,30,$tmp2
351         xor             $d,$tmp0,$tmp1
352         srl             $b,2,$b
353         add             $tmp1,$e,$e
354         or              $tmp2,$b,$b
355         add             $Xi,$e,$e
356 ___
357 $code.=<<___ if ($i>=72);
358          faligndata     @X[$m],@X[$m+2],@X[$m]
359         sll             $a,5,$tmp0                      !! $i
360         ld              [$Xfer+`4*($i%16)`],$Xi
361         srl             $a,27,$tmp1
362         add             $tmp0,$e,$e
363         xor             $c,$b,$tmp0
364         add             $tmp1,$e,$e
365          fpadd32        $VK_00_19,@X[$m],%f20
366         sll             $b,30,$tmp2
367         xor             $d,$tmp0,$tmp1
368         srl             $b,2,$b
369         add             $tmp1,$e,$e
370         or              $tmp2,$b,$b
371         add             $Xi,$e,$e
372 ___
373 $code.=<<___ if ($i<77);
374          ldd            [$inp+`8*($i+1-70)`],@X[2*($i+1-70)]
375 ___
376 $code.=<<___ if ($i==77);       # redundant if $inp was aligned
377          add            $align,63,$tmp0
378          and            $tmp0,-8,$tmp0
379          ldd            [$inp+$tmp0],@X[16]
380 ___
381 $code.=<<___ if ($i>=72);
382          std            %f20,[$nXfer+`4*$m`]
383 ___
384 }
385
386 $code.=<<___;
387 .section        ".text",#alloc,#execinstr
388
389 .align  64
390 vis_const:
391 .long   0x5a827999,0x5a827999   ! K_00_19
392 .long   0x6ed9eba1,0x6ed9eba1   ! K_20_39
393 .long   0x8f1bbcdc,0x8f1bbcdc   ! K_40_59
394 .long   0xca62c1d6,0xca62c1d6   ! K_60_79
395 .long   0x00000100,0x00000100
396 .align  64
397 .type   vis_const,#object
398 .size   vis_const,(.-vis_const)
399 load_vis_const:
400         ldd     [$tmp0+0],$VK_00_19
401         ldd     [$tmp0+8],$VK_20_39
402         ldd     [$tmp0+16],$VK_40_59
403         ldd     [$tmp0+24],$VK_60_79
404         retl
405         ldd     [$tmp0+32],$fmul
406 .type   load_vis_const,#function
407 .size   load_vis_const,(.-load_vis_const)
408
409 .align  32
410 .globl  sha1_block_data_order
411 sha1_block_data_order:
412         save    %sp,-$frame,%sp
413         add     %fp,$bias-256,$base
414
415 1:      call    load_vis_const
416         sub     %o7,1b-vis_const,$tmp0
417
418         ld      [$ctx+0],$Actx
419         and     $base,-256,$base
420         ld      [$ctx+4],$Bctx
421         sub     $base,$bias+$frame,%sp
422         ld      [$ctx+8],$Cctx
423         and     $inp,7,$align
424         ld      [$ctx+12],$Dctx
425         and     $inp,-8,$inp
426         ld      [$ctx+16],$Ectx
427
428         # X[16] is maintained in FP register bank
429         alignaddr       %g0,$align,%g0
430         ldd             [$inp+0],@X[0]
431         sub             $inp,-64,$Xfer
432         ldd             [$inp+8],@X[2]
433         and             $Xfer,-64,$Xfer
434         ldd             [$inp+16],@X[4]
435         and             $Xfer,255,$Xfer
436         ldd             [$inp+24],@X[6]
437         add             $base,$Xfer,$Xfer
438         ldd             [$inp+32],@X[8]
439         ldd             [$inp+40],@X[10]
440         ldd             [$inp+48],@X[12]
441         brz,pt          $align,.Laligned
442         ldd             [$inp+56],@X[14]
443
444         ldd             [$inp+64],@X[16]
445         faligndata      @X[0],@X[2],@X[0]
446         faligndata      @X[2],@X[4],@X[2]
447         faligndata      @X[4],@X[6],@X[4]
448         faligndata      @X[6],@X[8],@X[6]
449         faligndata      @X[8],@X[10],@X[8]
450         faligndata      @X[10],@X[12],@X[10]
451         faligndata      @X[12],@X[14],@X[12]
452         faligndata      @X[14],@X[16],@X[14]
453
454 .Laligned:
455         mov             5,$tmp0
456         dec             1,$len
457         alignaddr       %g0,$tmp0,%g0
458         fpadd32         $VK_00_19,@X[0],%f16
459         fpadd32         $VK_00_19,@X[2],%f18
460         fpadd32         $VK_00_19,@X[4],%f20
461         fpadd32         $VK_00_19,@X[6],%f22
462         fpadd32         $VK_00_19,@X[8],%f24
463         fpadd32         $VK_00_19,@X[10],%f26
464         fpadd32         $VK_00_19,@X[12],%f28
465         fpadd32         $VK_00_19,@X[14],%f30
466         std             %f16,[$Xfer+0]
467         mov             $Actx,$A
468         std             %f18,[$Xfer+8]
469         mov             $Bctx,$B
470         std             %f20,[$Xfer+16]
471         mov             $Cctx,$C
472         std             %f22,[$Xfer+24]
473         mov             $Dctx,$D
474         std             %f24,[$Xfer+32]
475         mov             $Ectx,$E
476         std             %f26,[$Xfer+40]
477         fxors           @X[13],@X[0],@X[0]
478         std             %f28,[$Xfer+48]
479         ba              .Loop
480         std             %f30,[$Xfer+56]
481 .align  32
482 .Loop:
483 ___
484 for ($i=0;$i<20;$i++)   { &BODY_00_19($i,@V); unshift(@V,pop(@V)); }
485 for (;$i<40;$i++)       { &BODY_20_39($i,@V); unshift(@V,pop(@V)); }
486 for (;$i<60;$i++)       { &BODY_40_59($i,@V); unshift(@V,pop(@V)); }
487 for (;$i<70;$i++)       { &BODY_20_39($i,@V); unshift(@V,pop(@V)); }
488 $code.=<<___;
489         brz,pn          $len,.Ltail
490         nop
491 ___
492 for (;$i<80;$i++)       { &BODY_70_79($i,@V); unshift(@V,pop(@V)); }
493 $code.=<<___;
494         add             $A,$Actx,$Actx
495         add             $B,$Bctx,$Bctx
496         add             $C,$Cctx,$Cctx
497         add             $D,$Dctx,$Dctx
498         add             $E,$Ectx,$Ectx
499         mov             5,$tmp0
500         fxors           @X[13],@X[0],@X[0]
501         mov             $Actx,$A
502         mov             $Bctx,$B
503         mov             $Cctx,$C
504         mov             $Dctx,$D
505         mov             $Ectx,$E
506         alignaddr       %g0,$tmp0,%g0   
507         dec             1,$len
508         ba              .Loop
509         mov             $nXfer,$Xfer
510
511 .align  32
512 .Ltail:
513 ___
514 for($i=70;$i<80;$i++)   { &BODY_20_39($i,@V); unshift(@V,pop(@V)); }
515 $code.=<<___;
516         add     $A,$Actx,$Actx
517         add     $B,$Bctx,$Bctx
518         add     $C,$Cctx,$Cctx
519         add     $D,$Dctx,$Dctx
520         add     $E,$Ectx,$Ectx
521
522         st      $Actx,[$ctx+0]
523         st      $Bctx,[$ctx+4]
524         st      $Cctx,[$ctx+8]
525         st      $Dctx,[$ctx+12]
526         st      $Ectx,[$ctx+16]
527
528         ret
529         restore
530 .type   sha1_block_data_order,#function
531 .size   sha1_block_data_order,(.-sha1_block_data_order)
532 .asciz  "SHA1 block transform for SPARCv9a, CRYPTOGAMS by <appro\@openssl.org>"
533 ___
534
535 # Purpose of these subroutines is to explicitly encode VIS instructions,
536 # so that one can compile the module without having to specify VIS
537 # extentions on compiler command line, e.g. -xarch=v9 vs. -xarch=v9a.
538 # Idea is to reserve for option to produce "universal" binary and let
539 # programmer detect if current CPU is VIS capable at run-time.
540 sub unvis {
541 my ($mnemonic,$rs1,$rs2,$rd)=@_;
542 my $ref,$opf;
543 my %visopf = (  "fmul8ulx16"    => 0x037,
544                 "faligndata"    => 0x048,
545                 "fpadd32"       => 0x052,
546                 "fxor"          => 0x06c,
547                 "fxors"         => 0x06d        );
548
549     $ref = "$mnemonic\t$rs1,$rs2,$rd";
550
551     if ($opf=$visopf{$mnemonic}) {
552         foreach ($rs1,$rs2,$rd) {
553             return $ref if (!/%f([0-9]{1,2})/);
554             $_=$1;
555             if ($1>=32) {
556                 return $ref if ($1&1);
557                 # re-encode for upper double register addressing
558                 $_=($1|$1>>5)&31;
559             }
560         }
561
562         return  sprintf ".word\t0x%08x !%s",
563                         0x81b00000|$rd<<25|$rs1<<14|$opf<<5|$rs2,
564                         $ref;
565     } else {
566         return $ref;
567     }
568 }
569 sub unalignaddr {
570 my ($mnemonic,$rs1,$rs2,$rd)=@_;
571 my %bias = ( "g" => 0, "o" => 8, "l" => 16, "i" => 24 );
572 my $ref="$mnemonic\t$rs1,$rs2,$rd";
573
574     foreach ($rs1,$rs2,$rd) {
575         if (/%([goli])([0-7])/) { $_=$bias{$1}+$2; }
576         else                    { return $ref; }
577     }
578     return  sprintf ".word\t0x%08x !%s",
579                     0x81b00300|$rd<<25|$rs1<<14|$rs2,
580                     $ref;
581 }
582
583 $code =~ s/\`([^\`]*)\`/eval $1/gem;
584 $code =~ s/\b(f[^\s]*)\s+(%f[0-9]{1,2}),(%f[0-9]{1,2}),(%f[0-9]{1,2})/
585                 &unvis($1,$2,$3,$4)
586           /gem;
587 $code =~ s/\b(alignaddr)\s+(%[goli][0-7]),(%[goli][0-7]),(%[goli][0-7])/
588                 &unalignaddr($1,$2,$3,$4)
589           /gem;
590 print $code;
591 close STDOUT;