sha1-mips.pl, mips-mont.pl: unify MIPS assembler modules in respect to
[openssl.git] / crypto / sha / asm / sha1-mips.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 # SHA1 block procedure for MIPS.
11
12 # Performance improvement is 30% on unaligned input. The "secret" is
13 # to deploy lwl/lwr pair to load unaligned input. One could have
14 # vectorized Xupdate on MIPSIII/IV, but the goal was to code MIPS32-
15 # compatible subroutine. There is room for minor optimization on
16 # little-endian platforms...
17
18 ######################################################################
19 # There is a number of MIPS ABI in use, O32 and N32/64 are most
20 # widely used. Then there is a new contender: NUBI. It appears that if
21 # one picks the latter, it's possible to arrange code in ABI neutral
22 # manner. Therefore let's stick to NUBI register layout:
23 #
24 ($zero,$at,$t0,$t1,$t2)=map("\$$_",(0..2,24,25));
25 ($a0,$a1,$a2,$a3,$a4,$a5,$a6,$a7)=map("\$$_",(4..11));
26 ($s0,$s1,$s2,$s3,$s4,$s5,$s6,$s7,$s8,$s9,$s10,$s11)=map("\$$_",(12..23));
27 ($gp,$tp,$sp,$fp,$ra)=map("\$$_",(3,28..31));
28 #
29 # The return value is placed in $a0. Following coding rules facilitate
30 # interoperability:
31 #
32 # - never ever touch $tp, "thread pointer", former $gp;
33 # - copy return value to $t0, former $v0 [or to $a0 if you're adapting
34 #   old code];
35 # - on O32 populate $a4-$a7 with 'lw $aN,4*N($sp)' if necessary;
36 #
37 # For reference here is register layout for N32/64 MIPS ABIs:
38 #
39 # ($zero,$at,$v0,$v1)=map("\$$_",(0..3));
40 # ($a0,$a1,$a2,$a3,$a4,$a5,$a6,$a7)=map("\$$_",(4..11));
41 # ($t0,$t1,$t2,$t3,$t8,$t9)=map("\$$_",(12..15,24,25));
42 # ($s0,$s1,$s2,$s3,$s4,$s5,$s6,$s7)=map("\$$_",(16..23));
43 # ($gp,$sp,$fp,$ra)=map("\$$_",(28..31));
44 #
45 $flavour = shift; # supported flavours are o32,n32,64,nubi32,nubi64
46
47 if ($flavour =~ /64|n32/i) {
48         $PTR_ADD="dadd";        # incidentally works even on n32
49         $PTR_SUB="dsub";        # incidentally works even on n32
50         $REG_S="sd";
51         $REG_L="ld";
52         $PTR_SLL="dsll";        # incidentally works even on n32
53         $SZREG=8;
54 } else {
55         $PTR_ADD="add";
56         $PTR_SUB="sub";
57         $REG_S="sw";
58         $REG_L="lw";
59         $PTR_SLL="sll";
60         $SZREG=4;
61 }
62 $SAVED_REGS_MASK = ($flavour =~ /nubi/i) ? 0x00fff008 : 0x00ff0000;
63 #
64 # <appro@openssl.org>
65 #
66 ######################################################################
67
68 for (@ARGV) {   $big_endian=1 if (/\-DB_ENDIAN/);
69                 $big_endian=0 if (/\-DL_ENDIAN/);
70                 $output=$_ if (/^\w[\w\-]*\.\w+$/);   }
71 open STDOUT,">$output";
72
73 if (!defined($big_endian))
74             {   $big_endian=(unpack('L',pack('N',1))==1);   }
75
76 # offsets of the Most and Least Significant Bytes
77 $MSB=$big_endian?0:3;
78 $LSB=3&~$MSB;
79
80 @X=(    "\$8",  "\$9",  "\$10", "\$11", "\$12", "\$13", "\$14", "\$15",
81         "\$16", "\$17", "\$18", "\$19", "\$20", "\$21", "\$22", "\$23");
82 $ctx="\$4";     # a0
83 $inp="\$5";     # a1
84 $num="\$6";     # a2
85 $A="\$1";
86 $B="\$2";
87 $C="\$3";
88 $D="\$7";
89 $E="\$24";      @V=($A,$B,$C,$D,$E);
90 $t0="\$25";
91 $t1=$num;       # $num is offloaded to stack
92 $t2="\$30";     # fp
93 $K="\$31";      # ra
94
95 $FRAMESIZE=16;
96
97 sub BODY_00_14 {
98 my ($i,$a,$b,$c,$d,$e)=@_;
99 my $j=$i+1;
100 $code.=<<___    if (!$big_endian);
101         srl     $t0,@X[$i],24   # byte swap($i)
102         srl     $t1,@X[$i],8
103         andi    $t2,@X[$i],0xFF00
104         sll     @X[$i],@X[$i],24
105         andi    $t1,0xFF00
106         sll     $t2,$t2,8
107         or      @X[$i],$t0
108         or      @X[$i],$t1
109         or      @X[$i],$t2
110 ___
111 $code.=<<___;
112          lwl    @X[$j],$j*4+$MSB($inp)
113         sll     $t0,$a,5        # $i
114         addu    $e,$K
115          lwr    @X[$j],$j*4+$LSB($inp)
116         srl     $t1,$a,27
117         addu    $e,$t0
118         xor     $t0,$c,$d
119         addu    $e,$t1
120         sll     $t2,$b,30
121         and     $t0,$b
122         srl     $b,$b,2
123         xor     $t0,$d
124         addu    $e,@X[$i]
125         or      $b,$t2
126         addu    $e,$t0
127 ___
128 }
129
130 sub BODY_15_19 {
131 my ($i,$a,$b,$c,$d,$e)=@_;
132 my $j=$i+1;
133
134 $code.=<<___    if (!$big_endian && $i==15);
135         srl     $t0,@X[$i],24   # byte swap($i)
136         srl     $t1,@X[$i],8
137         andi    $t2,@X[$i],0xFF00
138         sll     @X[$i],@X[$i],24
139         andi    $t1,0xFF00
140         sll     $t2,$t2,8
141         or      @X[$i],$t0
142         or      @X[$i],$t1
143         or      @X[$i],$t2
144 ___
145 $code.=<<___;
146          xor    @X[$j%16],@X[($j+2)%16]
147         sll     $t0,$a,5        # $i
148         addu    $e,$K
149         srl     $t1,$a,27
150         addu    $e,$t0
151          xor    @X[$j%16],@X[($j+8)%16]
152         xor     $t0,$c,$d
153         addu    $e,$t1
154          xor    @X[$j%16],@X[($j+13)%16]
155         sll     $t2,$b,30
156         and     $t0,$b
157          srl    $t1,@X[$j%16],31
158          addu   @X[$j%16],@X[$j%16]
159         srl     $b,$b,2
160         xor     $t0,$d
161          or     @X[$j%16],$t1
162         addu    $e,@X[$i%16]
163         or      $b,$t2
164         addu    $e,$t0
165 ___
166 }
167
168 sub BODY_20_39 {
169 my ($i,$a,$b,$c,$d,$e)=@_;
170 my $j=$i+1;
171 $code.=<<___ if ($i<79);
172          xor    @X[$j%16],@X[($j+2)%16]
173         sll     $t0,$a,5        # $i
174         addu    $e,$K
175         srl     $t1,$a,27
176         addu    $e,$t0
177          xor    @X[$j%16],@X[($j+8)%16]
178         xor     $t0,$c,$d
179         addu    $e,$t1
180          xor    @X[$j%16],@X[($j+13)%16]
181         sll     $t2,$b,30
182         xor     $t0,$b
183          srl    $t1,@X[$j%16],31
184          addu   @X[$j%16],@X[$j%16]
185         srl     $b,$b,2
186         addu    $e,@X[$i%16]
187          or     @X[$j%16],$t1
188         or      $b,$t2
189         addu    $e,$t0
190 ___
191 $code.=<<___ if ($i==79);
192          lw     @X[0],0($ctx)
193         sll     $t0,$a,5        # $i
194         addu    $e,$K
195          lw     @X[1],4($ctx)
196         srl     $t1,$a,27
197         addu    $e,$t0
198          lw     @X[2],8($ctx)
199         xor     $t0,$c,$d
200         addu    $e,$t1
201          lw     @X[3],12($ctx)
202         sll     $t2,$b,30
203         xor     $t0,$b
204          lw     @X[4],16($ctx)
205         srl     $b,$b,2
206         addu    $e,@X[$i%16]
207         or      $b,$t2
208         addu    $e,$t0
209 ___
210 }
211
212 sub BODY_40_59 {
213 my ($i,$a,$b,$c,$d,$e)=@_;
214 my $j=$i+1;
215 $code.=<<___ if ($i<79);
216          xor    @X[$j%16],@X[($j+2)%16]
217         sll     $t0,$a,5        # $i
218         addu    $e,$K
219         srl     $t1,$a,27
220         addu    $e,$t0
221          xor    @X[$j%16],@X[($j+8)%16]
222         and     $t0,$c,$d
223         addu    $e,$t1
224          xor    @X[$j%16],@X[($j+13)%16]
225         sll     $t2,$b,30
226         addu    $e,$t0
227          srl    $t1,@X[$j%16],31
228         xor     $t0,$c,$d
229          addu   @X[$j%16],@X[$j%16]
230         and     $t0,$b
231         srl     $b,$b,2
232          or     @X[$j%16],$t1
233         addu    $e,@X[$i%16]
234         or      $b,$t2
235         addu    $e,$t0
236 ___
237 }
238
239 $code=<<___;
240 .text
241
242 .set    noat
243 .set    noreorder
244 .align  5
245 .globl  sha1_block_data_order
246 .ent    sha1_block_data_order
247 sha1_block_data_order:
248         .frame  $sp,$FRAMESIZE*$SZREG,$ra
249         .mask   0xd0000000|$SAVED_REGS_MASK,-$SZREG
250         .set    noreorder
251         $PTR_SUB $sp,$FRAMESIZE*$SZREG
252         $REG_S  $ra,($FRAMESIZE-1)*$SZREG($sp)
253         $REG_S  $fp,($FRAMESIZE-2)*$SZREG($sp)
254         $REG_S  $s11,($FRAMESIZE-3)*$SZREG($sp)
255         $REG_S  $s10,($FRAMESIZE-4)*$SZREG($sp)
256         $REG_S  $s9,($FRAMESIZE-5)*$SZREG($sp)
257         $REG_S  $s8,($FRAMESIZE-6)*$SZREG($sp)
258         $REG_S  $s7,($FRAMESIZE-7)*$SZREG($sp)
259         $REG_S  $s6,($FRAMESIZE-8)*$SZREG($sp)
260         $REG_S  $s5,($FRAMESIZE-9)*$SZREG($sp)
261         $REG_S  $s4,($FRAMESIZE-10)*$SZREG($sp)
262 ___
263 $code.=<<___ if ($flavour =~ /nubi/i);
264         $REG_S  $s3,($FRAMESIZE-11)*$SZREG($sp)
265         $REG_S  $s2,($FRAMESIZE-12)*$SZREG($sp)
266         $REG_S  $s1,($FRAMESIZE-13)*$SZREG($sp)
267         $REG_S  $s0,($FRAMESIZE-14)*$SZREG($sp)
268         $REG_S  $gp,($FRAMESIZE-15)*$SZREG($sp)
269 ___
270 $code.=<<___;
271         $PTR_SLL $num,6
272         $PTR_ADD $num,$inp
273         $REG_S  $num,0($sp)
274         lw      $A,0($ctx)
275         lw      $B,4($ctx)
276         lw      $C,8($ctx)
277         lw      $D,12($ctx)
278         b       .Loop
279         lw      $E,16($ctx)
280 .align  4
281 .Loop:
282         .set    reorder
283         lwl     @X[0],$MSB($inp)
284         lui     $K,0x5a82
285         lwr     @X[0],$LSB($inp)
286         ori     $K,0x7999       # K_00_19
287 ___
288 for ($i=0;$i<15;$i++)   { &BODY_00_14($i,@V); unshift(@V,pop(@V)); }
289 for (;$i<20;$i++)       { &BODY_15_19($i,@V); unshift(@V,pop(@V)); }
290 $code.=<<___;
291         lui     $K,0x6ed9
292         ori     $K,0xeba1       # K_20_39
293 ___
294 for (;$i<40;$i++)       { &BODY_20_39($i,@V); unshift(@V,pop(@V)); }
295 $code.=<<___;
296         lui     $K,0x8f1b
297         ori     $K,0xbcdc       # K_40_59
298 ___
299 for (;$i<60;$i++)       { &BODY_40_59($i,@V); unshift(@V,pop(@V)); }
300 $code.=<<___;
301         lui     $K,0xca62
302         ori     $K,0xc1d6       # K_60_79
303 ___
304 for (;$i<80;$i++)       { &BODY_20_39($i,@V); unshift(@V,pop(@V)); }
305 $code.=<<___;
306         $PTR_ADD $inp,64
307         $REG_L  $num,0($sp)
308
309         addu    $A,$X[0]
310         addu    $B,$X[1]
311         sw      $A,0($ctx)
312         addu    $C,$X[2]
313         addu    $D,$X[3]
314         sw      $B,4($ctx)
315         addu    $E,$X[4]
316         sw      $C,8($ctx)
317         sw      $D,12($ctx)
318         sw      $E,16($ctx)
319         .set    noreorder
320         bne     $inp,$num,.Loop
321         nop
322
323         .set    noreorder
324         $REG_L  $ra,($FRAMESIZE-1)*$SZREG($sp)
325         $REG_L  $fp,($FRAMESIZE-2)*$SZREG($sp)
326         $REG_L  $s11,($FRAMESIZE-3)*$SZREG($sp)
327         $REG_L  $s10,($FRAMESIZE-4)*$SZREG($sp)
328         $REG_L  $s9,($FRAMESIZE-5)*$SZREG($sp)
329         $REG_L  $s8,($FRAMESIZE-6)*$SZREG($sp)
330         $REG_L  $s7,($FRAMESIZE-7)*$SZREG($sp)
331         $REG_L  $s6,($FRAMESIZE-8)*$SZREG($sp)
332         $REG_L  $s5,($FRAMESIZE-9)*$SZREG($sp)
333         $REG_L  $s4,($FRAMESIZE-10)*$SZREG($sp)
334 ___
335 $code.=<<___ if ($flavour =~ /nubi/i);
336         $REG_L  $s3,($FRAMESIZE-11)*$SZREG($sp)
337         $REG_L  $s2,($FRAMESIZE-12)*$SZREG($sp)
338         $REG_L  $s1,($FRAMESIZE-13)*$SZREG($sp)
339         $REG_L  $s0,($FRAMESIZE-14)*$SZREG($sp)
340         $REG_L  $gp,($FRAMESIZE-15)*$SZREG($sp)
341 ___
342 $code.=<<___;
343         jr      $ra
344         $PTR_ADD $sp,$FRAMESIZE*$SZREG
345 .end    sha1_block_data_order
346 .rdata
347 .asciiz "SHA1 for MIPS, CRYPTOGAMS by <appro\@openssl.org>"
348 ___
349 print $code;
350 close STDOUT;