be7da91e483c6d587bf203343c241c5c18dd1f44
[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 is lower than half-round VIS timing, because there
116 # are 16 Xupdate-free rounds, which "push down" average theoretical
117 # timing to 8 cycles...
118 }
119
120 # The reference Xupdate procedure is then "strained" over *pairs* of
121 # BODY_NN_MM and kind of modulo-scheduled in respect to X[n]^=X[n+13]
122 # and K_NN_MM addition. It's "running" 15 rounds ahead, which leaves
123 # plenty of room to amortize for read-after-write hazard, as well as
124 # to fetch and align input for the next spin. The VIS instructions are
125 # scheduled for latency of 2 cycles, because there are not enough IALU
126 # instructions to schedule for latency of 3, while scheduling for 1
127 # would give no gain on USI&II, but loss on SPARC64-V.
128
129 sub BODY_00_19 {
130 my ($i,$a,$b,$c,$d,$e)=@_;
131 my $j=$i&~1;
132 my $k=($j+16+2)%16;     # ahead reference
133 my $l=($j+16-2)%16;     # behind reference
134 my $K=@VK[($j+16-2)/20];
135
136 $j=($j+16)%16;
137
138 $code.=<<___ if (!($i&1));
139         sll             $a,5,$tmp0                      !! $i
140         and             $c,$b,$tmp3
141         ld              [$Xfer+`4*($i%16)`],$Xi
142          fxors          @X[($j+14)%16],@X[$j+1],@X[$j+1]! 0/ 0/ 0:X[1]^=X[14]
143         srl             $a,27,$tmp1
144         add             $tmp0,$e,$e
145          fxor           @X[($j+2)%16],@X[($j+8)%16],%f18! 1/ 1/ 1:Tmp=X[2,3]^X[8,9]
146         sll             $b,30,$tmp2
147         add             $tmp1,$e,$e
148         andn            $d,$b,$tmp1
149         add             $Xi,$e,$e
150          fxor           %f18,@X[$j],@X[$j]              ! 2/ 4/ 3:X[0,1]^=X[2,3]^X[8,9]
151         srl             $b,2,$b
152         or              $tmp1,$tmp3,$tmp1
153         or              $tmp2,$b,$b
154         add             $tmp1,$e,$e
155          faligndata     @X[$j],@X[$j],%f18              ! 3/ 7/ 5:Tmp=X[0,1]>>>24
156 ___
157 $code.=<<___ if ($i&1);
158         sll             $a,5,$tmp0                      !! $i
159         and             $c,$b,$tmp3
160         ld              [$Xfer+`4*($i%16)`],$Xi
161          fpadd32        @X[$j],@X[$j],@X[$j]            ! 4/ 8/ 6:X[0,1]<<=1
162         srl             $a,27,$tmp1
163         add             $tmp0,$e,$e
164          fmul8ulx16     %f18,$fmul,%f18                 ! 5/10/ 7:Tmp>>=7, Tmp&=1
165         sll             $b,30,$tmp2
166         add             $tmp1,$e,$e
167          fpadd32        $K,@X[$l],%f20                  !
168         andn            $d,$b,$tmp1
169         add             $Xi,$e,$e
170          fxors          @X[($k+13)%16],@X[$k],@X[$k]    !-1/-1/-1:X[0]^=X[13]
171         srl             $b,2,$b
172         or              $tmp1,$tmp3,$tmp1
173          fxor           %f18,@X[$j],@X[$j]              ! 8/14/10:X[0,1]|=Tmp
174         or              $tmp2,$b,$b
175         add             $tmp1,$e,$e
176 ___
177 $code.=<<___ if ($i&1 && $i>=2);
178          std            %f20,[$Xfer+`4*$l`]             !
179 ___
180 }
181
182 sub BODY_20_39 {
183 my ($i,$a,$b,$c,$d,$e)=@_;
184 my $j=$i&~1;
185 my $k=($j+16+2)%16;     # ahead reference
186 my $l=($j+16-2)%16;     # behind reference
187 my $K=@VK[($j+16-2)/20];
188
189 $j=($j+16)%16;
190
191 $code.=<<___ if (!($i&1) && $i<64);
192         sll             $a,5,$tmp0                      !! $i
193         ld              [$Xfer+`4*($i%16)`],$Xi
194          fxors          @X[($j+14)%16],@X[$j+1],@X[$j+1]! 0/ 0/ 0:X[1]^=X[14]
195         srl             $a,27,$tmp1
196         add             $tmp0,$e,$e
197          fxor           @X[($j+2)%16],@X[($j+8)%16],%f18! 1/ 1/ 1:Tmp=X[2,3]^X[8,9]
198         xor             $c,$b,$tmp0
199         add             $tmp1,$e,$e
200         sll             $b,30,$tmp2
201         xor             $d,$tmp0,$tmp1
202          fxor           %f18,@X[$j],@X[$j]              ! 2/ 4/ 3:X[0,1]^=X[2,3]^X[8,9]
203         srl             $b,2,$b
204         add             $tmp1,$e,$e
205         or              $tmp2,$b,$b
206         add             $Xi,$e,$e
207          faligndata     @X[$j],@X[$j],%f18              ! 3/ 7/ 5:Tmp=X[0,1]>>>24
208 ___
209 $code.=<<___ if ($i&1 && $i<64);
210         sll             $a,5,$tmp0                      !! $i
211         ld              [$Xfer+`4*($i%16)`],$Xi
212          fpadd32        @X[$j],@X[$j],@X[$j]            ! 4/ 8/ 6:X[0,1]<<=1
213         srl             $a,27,$tmp1
214         add             $tmp0,$e,$e
215          fmul8ulx16     %f18,$fmul,%f18                 ! 5/10/ 7:Tmp>>=7, Tmp&=1
216         xor             $c,$b,$tmp0
217         add             $tmp1,$e,$e
218          fpadd32        $K,@X[$l],%f20                  !
219         sll             $b,30,$tmp2
220         xor             $d,$tmp0,$tmp1
221          fxors          @X[($k+13)%16],@X[$k],@X[$k]    !-1/-1/-1:X[0]^=X[13]
222         srl             $b,2,$b
223         add             $tmp1,$e,$e
224          fxor           %f18,@X[$j],@X[$j]              ! 8/14/10:X[0,1]|=Tmp
225         or              $tmp2,$b,$b
226         add             $Xi,$e,$e
227          std            %f20,[$Xfer+`4*$l`]             !
228 ___
229 $code.=<<___ if ($i==64);
230         sll             $a,5,$tmp0                      !! $i
231         ld              [$Xfer+`4*($i%16)`],$Xi
232          fpadd32        $K,@X[$l],%f20
233         srl             $a,27,$tmp1
234         add             $tmp0,$e,$e
235         xor             $c,$b,$tmp0
236         add             $tmp1,$e,$e
237         sll             $b,30,$tmp2
238         xor             $d,$tmp0,$tmp1
239          std            %f20,[$Xfer+`4*$l`]
240         srl             $b,2,$b
241         add             $tmp1,$e,$e
242         or              $tmp2,$b,$b
243         add             $Xi,$e,$e
244 ___
245 $code.=<<___ if ($i>64);
246         sll             $a,5,$tmp0                      !! $i
247         ld              [$Xfer+`4*($i%16)`],$Xi
248         srl             $a,27,$tmp1
249         add             $tmp0,$e,$e
250         xor             $c,$b,$tmp0
251         add             $tmp1,$e,$e
252         sll             $b,30,$tmp2
253         xor             $d,$tmp0,$tmp1
254         srl             $b,2,$b
255         add             $tmp1,$e,$e
256         or              $tmp2,$b,$b
257         add             $Xi,$e,$e
258 ___
259 }
260
261 sub BODY_40_59 {
262 my ($i,$a,$b,$c,$d,$e)=@_;
263 my $j=$i&~1;
264 my $k=($j+16+2)%16;     # ahead reference
265 my $l=($j+16-2)%16;     # behind reference
266 my $K=@VK[($j+16-2)/20];
267
268 $j=($j+16)%16;
269
270 $code.=<<___ if (!($i&1));
271         sll             $a,5,$tmp0                      !! $i
272         ld              [$Xfer+`4*($i%16)`],$Xi
273          fxors          @X[($j+14)%16],@X[$j+1],@X[$j+1]! 0/ 0/ 0:X[1]^=X[14]
274         srl             $a,27,$tmp1
275         add             $tmp0,$e,$e
276          fxor           @X[($j+2)%16],@X[($j+8)%16],%f18! 1/ 1/ 1:Tmp=X[2,3]^X[8,9]
277         and             $c,$b,$tmp0
278         add             $tmp1,$e,$e
279         sll             $b,30,$tmp2
280         or              $c,$b,$tmp1
281          fxor           %f18,@X[$j],@X[$j]              ! 2/ 4/ 3:X[0,1]^=X[2,3]^X[8,9]
282         srl             $b,2,$b
283         and             $d,$tmp1,$tmp1
284         add             $Xi,$e,$e
285         or              $tmp1,$tmp0,$tmp1
286          faligndata     @X[$j],@X[$j],%f18              ! 3/ 7/ 5:Tmp=X[0,1]>>>24
287         or              $tmp2,$b,$b
288         add             $tmp1,$e,$e
289          fpadd32        @X[$j],@X[$j],@X[$j]            ! 4/ 8/ 6:X[0,1]<<=1
290 ___
291 $code.=<<___ if ($i&1);
292         sll             $a,5,$tmp0                      !! $i
293         ld              [$Xfer+`4*($i%16)`],$Xi
294         srl             $a,27,$tmp1
295         add             $tmp0,$e,$e
296          fmul8ulx16     %f18,$fmul,%f18                 ! 5/10/ 7:Tmp>>=7, Tmp&=1
297         and             $c,$b,$tmp0
298         add             $tmp1,$e,$e
299          fpadd32        $K,@X[$l],%f20                  !
300         sll             $b,30,$tmp2
301         or              $c,$b,$tmp1
302          fxors          @X[($k+13)%16],@X[$k],@X[$k]    !-1/-1/-1:X[0]^=X[13]
303         srl             $b,2,$b
304         and             $d,$tmp1,$tmp1
305          fxor           %f18,@X[$j],@X[$j]              ! 8/14/10:X[0,1]|=Tmp
306         add             $Xi,$e,$e
307         or              $tmp1,$tmp0,$tmp1
308         or              $tmp2,$b,$b
309         add             $tmp1,$e,$e
310          std            %f20,[$Xfer+`4*$l`]             !
311 ___
312 }
313
314 # If there is more data to process, then we pre-fetch the data for
315 # next iteration in last ten rounds...
316 sub BODY_70_79 {
317 my ($i,$a,$b,$c,$d,$e)=@_;
318 my $j=$i&~1;
319 my $m=($i%8)*2;
320
321 $j=($j+16)%16;
322
323 $code.=<<___ if ($i==70);
324         sll             $a,5,$tmp0                      !! $i
325         ld              [$Xfer+`4*($i%16)`],$Xi
326         srl             $a,27,$tmp1
327         add             $tmp0,$e,$e
328          ldd            [$inp+64],@X[0]
329         xor             $c,$b,$tmp0
330         add             $tmp1,$e,$e
331         sll             $b,30,$tmp2
332         xor             $d,$tmp0,$tmp1
333         srl             $b,2,$b
334         add             $tmp1,$e,$e
335         or              $tmp2,$b,$b
336         add             $Xi,$e,$e
337
338         and             $inp,-64,$nXfer
339         inc             64,$inp
340         and             $nXfer,255,$nXfer
341         alignaddr       %g0,$align,%g0
342         add             $base,$nXfer,$nXfer
343 ___
344 $code.=<<___ if ($i==71);
345         sll             $a,5,$tmp0                      !! $i
346         ld              [$Xfer+`4*($i%16)`],$Xi
347         srl             $a,27,$tmp1
348         add             $tmp0,$e,$e
349         xor             $c,$b,$tmp0
350         add             $tmp1,$e,$e
351         sll             $b,30,$tmp2
352         xor             $d,$tmp0,$tmp1
353         srl             $b,2,$b
354         add             $tmp1,$e,$e
355         or              $tmp2,$b,$b
356         add             $Xi,$e,$e
357 ___
358 $code.=<<___ if ($i>=72);
359          faligndata     @X[$m],@X[$m+2],@X[$m]
360         sll             $a,5,$tmp0                      !! $i
361         ld              [$Xfer+`4*($i%16)`],$Xi
362         srl             $a,27,$tmp1
363         add             $tmp0,$e,$e
364         xor             $c,$b,$tmp0
365         add             $tmp1,$e,$e
366          fpadd32        $VK_00_19,@X[$m],%f20
367         sll             $b,30,$tmp2
368         xor             $d,$tmp0,$tmp1
369         srl             $b,2,$b
370         add             $tmp1,$e,$e
371         or              $tmp2,$b,$b
372         add             $Xi,$e,$e
373 ___
374 $code.=<<___ if ($i<77);
375          ldd            [$inp+`8*($i+1-70)`],@X[2*($i+1-70)]
376 ___
377 $code.=<<___ if ($i==77);       # redundant if $inp was aligned
378          add            $align,63,$tmp0
379          and            $tmp0,-8,$tmp0
380          ldd            [$inp+$tmp0],@X[16]
381 ___
382 $code.=<<___ if ($i>=72);
383          std            %f20,[$nXfer+`4*$m`]
384 ___
385 }
386
387 $code.=<<___;
388 .section        ".text",#alloc,#execinstr
389
390 .align  64
391 vis_const:
392 .long   0x5a827999,0x5a827999   ! K_00_19
393 .long   0x6ed9eba1,0x6ed9eba1   ! K_20_39
394 .long   0x8f1bbcdc,0x8f1bbcdc   ! K_40_59
395 .long   0xca62c1d6,0xca62c1d6   ! K_60_79
396 .long   0x00000100,0x00000100
397 .align  64
398 .type   vis_const,#object
399 .size   vis_const,(.-vis_const)
400 load_vis_const:
401         ldd     [$tmp0+0],$VK_00_19
402         ldd     [$tmp0+8],$VK_20_39
403         ldd     [$tmp0+16],$VK_40_59
404         ldd     [$tmp0+24],$VK_60_79
405         retl
406         ldd     [$tmp0+32],$fmul
407 .type   load_vis_const,#function
408 .size   load_vis_const,(.-load_vis_const)
409
410 .align  32
411 .globl  sha1_block_data_order
412 sha1_block_data_order:
413         save    %sp,-$frame,%sp
414         add     %fp,$bias-256,$base
415
416 1:      call    load_vis_const
417         sub     %o7,1b-vis_const,$tmp0
418
419         ld      [$ctx+0],$Actx
420         and     $base,-256,$base
421         ld      [$ctx+4],$Bctx
422         sub     $base,$bias+$frame,%sp
423         ld      [$ctx+8],$Cctx
424         and     $inp,7,$align
425         ld      [$ctx+12],$Dctx
426         and     $inp,-8,$inp
427         ld      [$ctx+16],$Ectx
428
429         # X[16] is maintained in FP register bank
430         alignaddr       %g0,$align,%g0
431         ldd             [$inp+0],@X[0]
432         sub             $inp,-64,$Xfer
433         ldd             [$inp+8],@X[2]
434         and             $Xfer,-64,$Xfer
435         ldd             [$inp+16],@X[4]
436         and             $Xfer,255,$Xfer
437         ldd             [$inp+24],@X[6]
438         add             $base,$Xfer,$Xfer
439         ldd             [$inp+32],@X[8]
440         ldd             [$inp+40],@X[10]
441         ldd             [$inp+48],@X[12]
442         brz,pt          $align,.Laligned
443         ldd             [$inp+56],@X[14]
444
445         ldd             [$inp+64],@X[16]
446         faligndata      @X[0],@X[2],@X[0]
447         faligndata      @X[2],@X[4],@X[2]
448         faligndata      @X[4],@X[6],@X[4]
449         faligndata      @X[6],@X[8],@X[6]
450         faligndata      @X[8],@X[10],@X[8]
451         faligndata      @X[10],@X[12],@X[10]
452         faligndata      @X[12],@X[14],@X[12]
453         faligndata      @X[14],@X[16],@X[14]
454
455 .Laligned:
456         mov             5,$tmp0
457         dec             1,$len
458         alignaddr       %g0,$tmp0,%g0
459         fpadd32         $VK_00_19,@X[0],%f16
460         fpadd32         $VK_00_19,@X[2],%f18
461         fpadd32         $VK_00_19,@X[4],%f20
462         fpadd32         $VK_00_19,@X[6],%f22
463         fpadd32         $VK_00_19,@X[8],%f24
464         fpadd32         $VK_00_19,@X[10],%f26
465         fpadd32         $VK_00_19,@X[12],%f28
466         fpadd32         $VK_00_19,@X[14],%f30
467         std             %f16,[$Xfer+0]
468         mov             $Actx,$A
469         std             %f18,[$Xfer+8]
470         mov             $Bctx,$B
471         std             %f20,[$Xfer+16]
472         mov             $Cctx,$C
473         std             %f22,[$Xfer+24]
474         mov             $Dctx,$D
475         std             %f24,[$Xfer+32]
476         mov             $Ectx,$E
477         std             %f26,[$Xfer+40]
478         fxors           @X[13],@X[0],@X[0]
479         std             %f28,[$Xfer+48]
480         ba              .Loop
481         std             %f30,[$Xfer+56]
482 .align  32
483 .Loop:
484 ___
485 for ($i=0;$i<20;$i++)   { &BODY_00_19($i,@V); unshift(@V,pop(@V)); }
486 for (;$i<40;$i++)       { &BODY_20_39($i,@V); unshift(@V,pop(@V)); }
487 for (;$i<60;$i++)       { &BODY_40_59($i,@V); unshift(@V,pop(@V)); }
488 for (;$i<70;$i++)       { &BODY_20_39($i,@V); unshift(@V,pop(@V)); }
489 $code.=<<___;
490         brz,pn          $len,.Ltail
491         nop
492 ___
493 for (;$i<80;$i++)       { &BODY_70_79($i,@V); unshift(@V,pop(@V)); }
494 $code.=<<___;
495         add             $A,$Actx,$Actx
496         add             $B,$Bctx,$Bctx
497         add             $C,$Cctx,$Cctx
498         add             $D,$Dctx,$Dctx
499         add             $E,$Ectx,$Ectx
500         mov             5,$tmp0
501         fxors           @X[13],@X[0],@X[0]
502         mov             $Actx,$A
503         mov             $Bctx,$B
504         mov             $Cctx,$C
505         mov             $Dctx,$D
506         mov             $Ectx,$E
507         alignaddr       %g0,$tmp0,%g0   
508         dec             1,$len
509         ba              .Loop
510         mov             $nXfer,$Xfer
511
512 .align  32
513 .Ltail:
514 ___
515 for($i=70;$i<80;$i++)   { &BODY_20_39($i,@V); unshift(@V,pop(@V)); }
516 $code.=<<___;
517         add     $A,$Actx,$Actx
518         add     $B,$Bctx,$Bctx
519         add     $C,$Cctx,$Cctx
520         add     $D,$Dctx,$Dctx
521         add     $E,$Ectx,$Ectx
522
523         st      $Actx,[$ctx+0]
524         st      $Bctx,[$ctx+4]
525         st      $Cctx,[$ctx+8]
526         st      $Dctx,[$ctx+12]
527         st      $Ectx,[$ctx+16]
528
529         ret
530         restore
531 .type   sha1_block_data_order,#function
532 .size   sha1_block_data_order,(.-sha1_block_data_order)
533 .asciz  "SHA1 block transform for SPARCv9a, CRYPTOGAMS by <appro\@openssl.org>"
534 ___
535
536 # Purpose of these subroutines is to explicitly encode VIS instructions,
537 # so that one can compile the module without having to specify VIS
538 # extentions on compiler command line, e.g. -xarch=v9 vs. -xarch=v9a.
539 # Idea is to reserve for option to produce "universal" binary and let
540 # programmer detect if current CPU is VIS capable at run-time.
541 sub unvis {
542 my ($mnemonic,$rs1,$rs2,$rd)=@_;
543 my $ref,$opf;
544 my %visopf = (  "fmul8ulx16"    => 0x037,
545                 "faligndata"    => 0x048,
546                 "fpadd32"       => 0x052,
547                 "fxor"          => 0x06c,
548                 "fxors"         => 0x06d        );
549
550     $ref = "$mnemonic\t$rs1,$rs2,$rd";
551
552     if ($opf=$visopf{$mnemonic}) {
553         foreach ($rs1,$rs2,$rd) {
554             return $ref if (!/%f([0-9]{1,2})/);
555             $_=$1;
556             if ($1>=32) {
557                 return $ref if ($1&1);
558                 # re-encode for upper double register addressing
559                 $_=($1|$1>>5)&31;
560             }
561         }
562
563         return  sprintf ".word\t0x%08x !%s",
564                         0x81b00000|$rd<<25|$rs1<<14|$opf<<5|$rs2,
565                         $ref;
566     } else {
567         return $ref;
568     }
569 }
570 sub unalignaddr {
571 my ($mnemonic,$rs1,$rs2,$rd)=@_;
572 my %bias = ( "g" => 0, "o" => 8, "l" => 16, "i" => 24 );
573 my $ref="$mnemonic\t$rs1,$rs2,$rd";
574
575     foreach ($rs1,$rs2,$rd) {
576         if (/%([goli])([0-7])/) { $_=$bias{$1}+$2; }
577         else                    { return $ref; }
578     }
579     return  sprintf ".word\t0x%08x !%s",
580                     0x81b00300|$rd<<25|$rs1<<14|$rs2,
581                     $ref;
582 }
583
584 $code =~ s/\`([^\`]*)\`/eval $1/gem;
585 $code =~ s/\b(f[^\s]*)\s+(%f[0-9]{1,2}),(%f[0-9]{1,2}),(%f[0-9]{1,2})/
586                 &unvis($1,$2,$3,$4)
587           /gem;
588 $code =~ s/\b(alignaddr)\s+(%[goli][0-7]),(%[goli][0-7]),(%[goli][0-7])/
589                 &unalignaddr($1,$2,$3,$4)
590           /gem;
591 print $code;
592 close STDOUT;