9f2d15951433e97adc4147611ac312d340e0051e
[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
10 # Performance improvement is not really impressive on pre-T1 CPU: +8%
11 # over Sun C and +25% over gcc [3.3]. While on T1, ... And there
12 # is a gimmick. X[16] vector is packed to 8 64-bit registers and as
13 # result nothing is spilled on stack. In addition input data is loaded
14 # in compact instruction sequence, thus minimizing the window when the
15 # code is subject to [inter-thread] cache-thrashing hazard. The goal
16 # is to ensure scalability on UltraSPARC T1, or rather to avoid decay
17 # when amount of active threads exceeds the number of physical cores.
18
19 $bits=32;
20 for (@ARGV)     { $bits=64 if (/\-m64/ || /\-xarch\=v9/); }
21 if ($bits==64)  { $bias=2047; $frame=192; }
22 else            { $bias=0;    $frame=112; }
23
24 $output=shift;
25 open STDOUT,">$output";
26
27 @X=("%o0","%o1","%o2","%o3","%o4","%o5","%g1","%o7");
28 $rot1m="%g2";
29 $tmp64="%g3";
30 $Xi="%g4";
31 $A="%l0";
32 $B="%l1";
33 $C="%l2";
34 $D="%l3";
35 $E="%l4";
36 @V=($A,$B,$C,$D,$E);
37 $K_00_19="%l5";
38 $K_20_39="%l6";
39 $K_40_59="%l7";
40 $K_60_79="%g5";
41 @K=($K_00_19,$K_20_39,$K_40_59,$K_60_79);
42
43 $ctx="%i0";
44 $inp="%i1";
45 $len="%i2";
46 $tmp0="%i3";
47 $tmp1="%i4";
48 $tmp2="%i5";
49
50 sub BODY_00_15 {
51 my ($i,$a,$b,$c,$d,$e)=@_;
52 my $xi=($i&1)?@X[($i/2)%8]:$Xi;
53
54 $code.=<<___;
55         sll     $a,5,$tmp0              !! $i
56         add     @K[$i/20],$e,$e
57         srl     $a,27,$tmp1
58         add     $tmp0,$e,$e
59         and     $c,$b,$tmp0
60         add     $tmp1,$e,$e
61         sll     $b,30,$tmp2
62         andn    $d,$b,$tmp1
63         srl     $b,2,$b
64         or      $tmp1,$tmp0,$tmp1
65         or      $tmp2,$b,$b
66         add     $xi,$e,$e
67 ___
68 if ($i&1 && $i<15) {
69         $code.=
70         "       srlx    @X[(($i+1)/2)%8],32,$Xi\n";
71 }
72 $code.=<<___;
73         add     $tmp1,$e,$e
74 ___
75 }
76
77 sub Xupdate {
78 my ($i,$a,$b,$c,$d,$e)=@_;
79 my $j=$i/2;
80
81 if ($i&1) {
82 $code.=<<___;
83         sll     $a,5,$tmp0              !! $i
84         add     @K[$i/20],$e,$e
85         srl     $a,27,$tmp1
86 ___
87 } else {
88 $code.=<<___;
89         sllx    @X[($j+6)%8],32,$Xi     ! Xupdate($i)
90         xor     @X[($j+1)%8],@X[$j%8],@X[$j%8]
91         srlx    @X[($j+7)%8],32,$tmp1
92         xor     @X[($j+4)%8],@X[$j%8],@X[$j%8]
93         sll     $a,5,$tmp0              !! $i
94         or      $tmp1,$Xi,$Xi
95         add     @K[$i/20],$e,$e         !!
96         xor     $Xi,@X[$j%8],@X[$j%8]
97         srlx    @X[$j%8],31,$Xi
98         add     @X[$j%8],@X[$j%8],@X[$j%8]
99         and     $Xi,$rot1m,$Xi
100         andn    @X[$j%8],$rot1m,@X[$j%8]
101         srl     $a,27,$tmp1             !!
102         or      $Xi,@X[$j%8],@X[$j%8]
103 ___
104 }
105 }
106
107 sub BODY_16_19 {
108 my ($i,$a,$b,$c,$d,$e)=@_;
109
110         &Xupdate(@_);
111     if ($i&1) {
112         $xi=@X[($i/2)%8];
113     } else {
114         $xi=$Xi;
115         $code.="\tsrlx  @X[($i/2)%8],32,$xi\n";
116     }
117 $code.=<<___;
118         add     $tmp0,$e,$e             !!
119         and     $c,$b,$tmp0
120         add     $tmp1,$e,$e
121         sll     $b,30,$tmp2
122         add     $xi,$e,$e
123         andn    $d,$b,$tmp1
124         srl     $b,2,$b
125         or      $tmp1,$tmp0,$tmp1
126         or      $tmp2,$b,$b
127         add     $tmp1,$e,$e
128 ___
129 }
130
131 sub BODY_20_39 {
132 my ($i,$a,$b,$c,$d,$e)=@_;
133 my $xi;
134         &Xupdate(@_);
135     if ($i&1) {
136         $xi=@X[($i/2)%8];
137     } else {
138         $xi=$Xi;
139         $code.="\tsrlx  @X[($i/2)%8],32,$xi\n";
140     }
141 $code.=<<___;
142         add     $tmp0,$e,$e             !!
143         xor     $c,$b,$tmp0
144         add     $tmp1,$e,$e
145         sll     $b,30,$tmp2
146         xor     $d,$tmp0,$tmp1
147         srl     $b,2,$b
148         add     $tmp1,$e,$e
149         or      $tmp2,$b,$b
150         add     $xi,$e,$e
151 ___
152 }
153
154 sub BODY_40_59 {
155 my ($i,$a,$b,$c,$d,$e)=@_;
156 my $xi;
157         &Xupdate(@_);
158     if ($i&1) {
159         $xi=@X[($i/2)%8];
160     } else {
161         $xi=$Xi;
162         $code.="\tsrlx  @X[($i/2)%8],32,$xi\n";
163     }
164 $code.=<<___;
165         add     $tmp0,$e,$e             !!
166         and     $c,$b,$tmp0
167         add     $tmp1,$e,$e
168         sll     $b,30,$tmp2
169         or      $c,$b,$tmp1
170         srl     $b,2,$b
171         and     $d,$tmp1,$tmp1
172         add     $xi,$e,$e
173         or      $tmp1,$tmp0,$tmp1
174         or      $tmp2,$b,$b
175         add     $tmp1,$e,$e
176 ___
177 }
178
179 $code.=<<___ if ($bits==64);
180 .register       %g2,#scratch
181 .register       %g3,#scratch
182 ___
183 $code.=<<___;
184 .section        ".text",#alloc,#execinstr
185
186 .align  32
187 .globl  sha1_block_data_order
188 sha1_block_data_order:
189         save    %sp,-$frame,%sp
190         sllx    $len,6,$len
191         add     $inp,$len,$len
192
193         or      %g0,1,$rot1m
194         sllx    $rot1m,32,$rot1m
195         or      $rot1m,1,$rot1m
196
197         ld      [$ctx+0],$A
198         ld      [$ctx+4],$B
199         ld      [$ctx+8],$C
200         ld      [$ctx+12],$D
201         ld      [$ctx+16],$E
202         andn    $inp,7,$tmp0
203
204         sethi   %hi(0x5a827999),$K_00_19
205         or      $K_00_19,%lo(0x5a827999),$K_00_19
206         sethi   %hi(0x6ed9eba1),$K_20_39
207         or      $K_20_39,%lo(0x6ed9eba1),$K_20_39
208         sethi   %hi(0x8f1bbcdc),$K_40_59
209         or      $K_40_59,%lo(0x8f1bbcdc),$K_40_59
210         sethi   %hi(0xca62c1d6),$K_60_79
211         or      $K_60_79,%lo(0xca62c1d6),$K_60_79
212
213 .Lloop:
214         ldx     [$tmp0+0],@X[0]
215         ldx     [$tmp0+16],@X[2]
216         ldx     [$tmp0+32],@X[4]
217         ldx     [$tmp0+48],@X[6]
218         and     $inp,7,$tmp1
219         ldx     [$tmp0+8],@X[1]
220         sll     $tmp1,3,$tmp1
221         ldx     [$tmp0+24],@X[3]
222         subcc   %g0,$tmp1,$tmp2 ! should be 64-$tmp1, but -$tmp1 works too
223         ldx     [$tmp0+40],@X[5]
224         bz,pt   %icc,.Laligned
225         ldx     [$tmp0+56],@X[7]
226
227         sllx    @X[0],$tmp1,@X[0]
228         ldx     [$tmp0+64],$tmp64
229 ___
230 for($i=0;$i<7;$i++)
231 {   $code.=<<___;
232         srlx    @X[$i+1],$tmp2,$Xi
233         sllx    @X[$i+1],$tmp1,@X[$i+1]
234         or      $Xi,@X[$i],@X[$i]
235 ___
236 }
237 $code.=<<___;
238         srlx    $tmp64,$tmp2,$tmp64
239         or      $tmp64,@X[7],@X[7]
240 .Laligned:
241         srlx    @X[0],32,$Xi
242 ___
243 for ($i=0;$i<16;$i++)   { &BODY_00_15($i,@V); unshift(@V,pop(@V)); }
244 for (;$i<20;$i++)       { &BODY_16_19($i,@V); unshift(@V,pop(@V)); }
245 for (;$i<40;$i++)       { &BODY_20_39($i,@V); unshift(@V,pop(@V)); }
246 for (;$i<60;$i++)       { &BODY_40_59($i,@V); unshift(@V,pop(@V)); }
247 for (;$i<80;$i++)       { &BODY_20_39($i,@V); unshift(@V,pop(@V)); }
248 $code.=<<___;
249
250         ld      [$ctx+0],@X[0]
251         ld      [$ctx+4],@X[1]
252         ld      [$ctx+8],@X[2]
253         ld      [$ctx+12],@X[3]
254         add     $inp,64,$inp
255         ld      [$ctx+16],@X[4]
256         cmp     $inp,$len
257
258         add     $A,@X[0],$A
259         st      $A,[$ctx+0]
260         add     $B,@X[1],$B
261         st      $B,[$ctx+4]
262         add     $C,@X[2],$C
263         st      $C,[$ctx+8]
264         add     $D,@X[3],$D
265         st      $D,[$ctx+12]
266         add     $E,@X[4],$E
267         st      $E,[$ctx+16]
268
269         bne     `$bits==64?"%xcc":"%icc"`,.Lloop
270         andn    $inp,7,$tmp0
271
272         ret
273         restore
274 .type   sha1_block_data_order,#function
275 .size   sha1_block_data_order,(.-sha1_block_data_order)
276 .asciz  "SHA1 block transform for SPARCv9, CRYPTOGAMS by <appro\@openssl.org>"
277 ___
278
279 $code =~ s/\`([^\`]*)\`/eval $1/gem;
280 print $code;
281 close STDOUT;