436eb36d70b5dab2e76146514846ffed9f9d1c45
[openssl.git] / crypto / sha / asm / sha1-armv4-large.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 ARMv4.
11 #
12 # January 2007.
13
14 # Size/performance trade-off
15 # ====================================================================
16 # impl          size in bytes   comp cycles[*]  measured performance
17 # ====================================================================
18 # thumb         304             3212            4420
19 # armv4-small   392/+29%        1958/+64%       2250/+96%
20 # armv4-compact 740/+89%        1552/+26%       1840/+22%
21 # armv4-large   1420/+92%       1307/+19%       1500/+23%
22 # full unroll   ~5100/+260%     ~1260/+4%       ~1500/+0%
23 # ====================================================================
24 # thumb         = same as 'small' but in Thumb instructions[**] and
25 #                 with recurring code in two private functions;
26 # small         = detached Xload/update, loops are folded;
27 # compact       = detached Xload/update, 5x unroll;
28 # large         = interleaved Xload/update, 5x unroll;
29 # full unroll   = interleaved Xload/update, full unroll, estimated[!];
30 #
31 # [*]   Manually counted instructions in "grand" loop body. Measured
32 #       performance is affected by prologue and epilogue overhead,
33 #       i-cache availability, branch penalties, etc.
34 # [**]  While each Thumb instruction is twice smaller, they are not as
35 #       diverse as ARM ones: e.g., there are only two arithmetic
36 #       instructions with 3 arguments, no [fixed] rotate, addressing
37 #       modes are limited. As result it takes more instructions to do
38 #       the same job in Thumb, therefore the code is never twice as
39 #       small and always slower.
40
41 $output=shift;
42 open STDOUT,">$output";
43
44 $ctx="r0";
45 $inp="r1";
46 $len="r2";
47 $a="r3";
48 $b="r4";
49 $c="r5";
50 $d="r6";
51 $e="r7";
52 $K="r8";
53 $t0="r10";
54 $t1="r11";
55 $t2="r12";
56 $Xi="r14";
57 @V=($a,$b,$c,$d,$e);
58
59 # One can optimize this for aligned access on big-endian architecture,
60 # but code's endian neutrality makes it too pretty:-)
61 sub Xload {
62 my ($a,$b,$c,$d,$e)=@_;
63 $code.=<<___;
64         ldrb    $t0,[$inp],#4
65         ldrb    $t1,[$inp,#-3]
66         ldrb    $t2,[$inp,#-2]
67         add     $e,$K,$e,ror#2                  @ E+=K_00_19
68         orr     $t0,$t1,$t0,lsl#8
69         ldrb    $t1,[$inp,#-1]
70         orr     $t0,$t2,$t0,lsl#8
71         add     $e,$e,$a,ror#27                 @ E+=ROR(A,27)
72         orr     $t0,$t1,$t0,lsl#8
73         add     $e,$e,$t0                       @ E+=X[i]
74         eor     $t1,$c,$d                       @ F_xx_xx
75         str     $t0,[$Xi,#-4]!
76 ___
77 }
78 sub Xupdate {
79 my ($a,$b,$c,$d,$e,$flag)=@_;
80 $code.=<<___;
81         ldr     $t0,[$Xi,#15*4]
82         ldr     $t1,[$Xi,#13*4]
83         ldr     $t2,[$Xi,#7*4]
84         add     $e,$K,$e,ror#2                  @ E+=K_xx_xx
85         eor     $t0,$t0,$t1
86         ldr     $t1,[$Xi,#2*4]
87         add     $e,$e,$a,ror#27                 @ E+=ROR(A,27)
88         eor     $t0,$t0,$t2
89         eor     $t0,$t0,$t1
90 ___
91 $code.=<<___ if (!defined($flag));
92         eor     $t1,$c,$d                       @ F_xx_xx, but not in 40_59
93 ___
94 $code.=<<___;
95         mov     $t0,$t0,ror#31
96         add     $e,$e,$t0                       @ E+=X[i]
97         str     $t0,[$Xi,#-4]!
98 ___
99 }
100
101 sub BODY_00_15 {
102 my ($a,$b,$c,$d,$e)=@_;
103         &Xload(@_);
104 $code.=<<___;
105         and     $t1,$b,$t1,ror#2
106         eor     $t1,$t1,$d,ror#2                @ F_00_19(B,C,D)
107         add     $e,$e,$t1                       @ E+=F_00_19(B,C,D)
108 ___
109 }
110
111 sub BODY_16_19 {
112 my ($a,$b,$c,$d,$e)=@_;
113         &Xupdate(@_);
114 $code.=<<___;
115         and     $t1,$b,$t1,ror#2
116         eor     $t1,$t1,$d,ror#2                @ F_00_19(B,C,D)
117         add     $e,$e,$t1                       @ E+=F_00_19(B,C,D)
118 ___
119 }
120
121 sub BODY_20_39 {
122 my ($a,$b,$c,$d,$e)=@_;
123         &Xupdate(@_);
124 $code.=<<___;
125         eor     $t1,$b,$t1,ror#2                @ F_20_39(B,C,D)
126         add     $e,$e,$t1                       @ E+=F_20_39(B,C,D)
127 ___
128 }
129
130 sub BODY_40_59 {
131 my ($a,$b,$c,$d,$e)=@_;
132         &Xupdate(@_,1);
133 $code.=<<___;
134         and     $t1,$b,$c,ror#2
135         orr     $t2,$b,$c,ror#2
136         and     $t2,$t2,$d,ror#2
137         orr     $t1,$t1,$t2                     @ F_40_59(B,C,D)
138         add     $e,$e,$t1                       @ E+=F_40_59(B,C,D)
139 ___
140 }
141
142 $code=<<___;
143 .text
144
145 .global sha1_block_data_order
146 .type   sha1_block_data_order,%function
147
148 .align  2
149 sha1_block_data_order:
150         stmdb   sp!,{r4-r12,lr}
151         add     $len,$inp,$len,lsl#6    @ $len to point at the end of $inp
152         ldmia   $ctx,{$a,$b,$c,$d,$e}
153 .Lloop:
154         ldr     $K,.LK_00_19
155         mov     $Xi,sp
156         sub     sp,sp,#15*4
157         mov     $c,$c,ror#30
158         mov     $d,$d,ror#30
159         mov     $e,$e,ror#30            @ [6]
160 .L_00_15:
161 ___
162 for($i=0;$i<5;$i++) {
163         &BODY_00_15(@V);        unshift(@V,pop(@V));
164 }
165 $code.=<<___;
166         teq     $Xi,sp
167         bne     .L_00_15                @ [((11+4)*5+2)*3]
168 ___
169         &BODY_00_15(@V);        unshift(@V,pop(@V));
170         &BODY_16_19(@V);        unshift(@V,pop(@V));
171         &BODY_16_19(@V);        unshift(@V,pop(@V));
172         &BODY_16_19(@V);        unshift(@V,pop(@V));
173         &BODY_16_19(@V);        unshift(@V,pop(@V));
174 $code.=<<___;
175
176         ldr     $K,.LK_20_39            @ [+15+16*4]
177         sub     sp,sp,#25*4
178         cmn     sp,#0                   @ [+3], clear carry to denote 20_39
179 .L_20_39_or_60_79:
180 ___
181 for($i=0;$i<5;$i++) {
182         &BODY_20_39(@V);        unshift(@V,pop(@V));
183 }
184 $code.=<<___;
185         teq     $Xi,sp                  @ preserve carry
186         bne     .L_20_39_or_60_79       @ [+((12+3)*5+2)*4]
187         bcs     .L_done                 @ [+((12+3)*5+2)*4], spare 300 bytes
188
189         ldr     $K,.LK_40_59
190         sub     sp,sp,#20*4             @ [+2]
191 .L_40_59:
192 ___
193 for($i=0;$i<5;$i++) {
194         &BODY_40_59(@V);        unshift(@V,pop(@V));
195 }
196 $code.=<<___;
197         teq     $Xi,sp
198         bne     .L_40_59                @ [+((12+5)*5+2)*4]
199
200         ldr     $K,.LK_60_79
201         sub     sp,sp,#20*4
202         cmp     sp,#0                   @ set carry to denote 60_79
203         b       .L_20_39_or_60_79       @ [+4], spare 300 bytes
204 .L_done:
205         add     sp,sp,#80*4             @ "deallocate" stack frame
206         ldmia   $ctx,{$K,$t0,$t1,$t2,$Xi}
207         add     $a,$K,$a
208         add     $b,$t0,$b
209         add     $c,$t1,$c,ror#2
210         add     $d,$t2,$d,ror#2
211         add     $e,$Xi,$e,ror#2
212         stmia   $ctx,{$a,$b,$c,$d,$e}
213         teq     $inp,$len
214         bne     .Lloop                  @ [+18], total 1307
215
216         ldmia   sp!,{r4-r12,lr}
217         tst     lr,#1
218         moveq   pc,lr                   @ be binary compatible with V4, yet
219         bx      lr                      @ interoperable with Thumb ISA:-)
220 .align  2
221 .LK_00_19:      .word   0x5a827999
222 .LK_20_39:      .word   0x6ed9eba1
223 .LK_40_59:      .word   0x8f1bbcdc
224 .LK_60_79:      .word   0xca62c1d6
225 .size   sha1_block_data_order,.-sha1_block_data_order
226 .asciz  "SHA1 block transform for ARMv4, CRYPTOGAMS by <appro\@openssl.org>"
227 ___
228
229 print $code;
230 close STDOUT; # enforce flush