sha1-armv4-large.pl: add performance data for Cortex A8 core.
[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%       1370/+34%[***]
22 # full unroll   ~5100/+260%     ~1260/+4%       ~1300/+5%
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 # [***] which is also ~35% better than compiler generated code. Dual-
41 #       issue Cortex A8 core was measured to process input block in
42 #       ~990 cycles.
43
44 while (($output=shift) && ($output!~/^\w[\w\-]*\.\w+$/)) {}
45 open STDOUT,">$output";
46
47 $ctx="r0";
48 $inp="r1";
49 $len="r2";
50 $a="r3";
51 $b="r4";
52 $c="r5";
53 $d="r6";
54 $e="r7";
55 $K="r8";
56 $t0="r9";
57 $t1="r10";
58 $t2="r11";
59 $t3="r12";
60 $Xi="r14";
61 @V=($a,$b,$c,$d,$e);
62
63 # One can optimize this for aligned access on big-endian architecture,
64 # but code's endian neutrality makes it too pretty:-)
65 sub Xload {
66 my ($a,$b,$c,$d,$e)=@_;
67 $code.=<<___;
68         ldrb    $t0,[$inp],#4
69         ldrb    $t1,[$inp,#-3]
70         ldrb    $t2,[$inp,#-2]
71         ldrb    $t3,[$inp,#-1]
72         add     $e,$K,$e,ror#2                  @ E+=K_00_19
73         orr     $t0,$t1,$t0,lsl#8
74         add     $e,$e,$a,ror#27                 @ E+=ROR(A,27)
75         orr     $t0,$t2,$t0,lsl#8
76         eor     $t1,$c,$d                       @ F_xx_xx
77         orr     $t0,$t3,$t0,lsl#8
78         add     $e,$e,$t0                       @ E+=X[i]
79         str     $t0,[$Xi,#-4]!
80 ___
81 }
82 sub Xupdate {
83 my ($a,$b,$c,$d,$e,$flag)=@_;
84 $code.=<<___;
85         ldr     $t0,[$Xi,#15*4]
86         ldr     $t1,[$Xi,#13*4]
87         ldr     $t2,[$Xi,#7*4]
88         ldr     $t3,[$Xi,#2*4]
89         add     $e,$K,$e,ror#2                  @ E+=K_xx_xx
90         eor     $t0,$t0,$t1
91         eor     $t2,$t2,$t3
92         eor     $t0,$t0,$t2
93         add     $e,$e,$a,ror#27                 @ E+=ROR(A,27)
94 ___
95 $code.=<<___ if (!defined($flag));
96         eor     $t1,$c,$d                       @ F_xx_xx, but not in 40_59
97 ___
98 $code.=<<___;
99         mov     $t0,$t0,ror#31
100         add     $e,$e,$t0                       @ E+=X[i]
101         str     $t0,[$Xi,#-4]!
102 ___
103 }
104
105 sub BODY_00_15 {
106 my ($a,$b,$c,$d,$e)=@_;
107         &Xload(@_);
108 $code.=<<___;
109         and     $t1,$b,$t1,ror#2
110         eor     $t1,$t1,$d,ror#2                @ F_00_19(B,C,D)
111         add     $e,$e,$t1                       @ E+=F_00_19(B,C,D)
112 ___
113 }
114
115 sub BODY_16_19 {
116 my ($a,$b,$c,$d,$e)=@_;
117         &Xupdate(@_);
118 $code.=<<___;
119         and     $t1,$b,$t1,ror#2
120         eor     $t1,$t1,$d,ror#2                @ F_00_19(B,C,D)
121         add     $e,$e,$t1                       @ E+=F_00_19(B,C,D)
122 ___
123 }
124
125 sub BODY_20_39 {
126 my ($a,$b,$c,$d,$e)=@_;
127         &Xupdate(@_);
128 $code.=<<___;
129         eor     $t1,$b,$t1,ror#2                @ F_20_39(B,C,D)
130         add     $e,$e,$t1                       @ E+=F_20_39(B,C,D)
131 ___
132 }
133
134 sub BODY_40_59 {
135 my ($a,$b,$c,$d,$e)=@_;
136 if (1) {
137         &Xupdate(@_);
138 $code.=<<___;
139         and     $t2,$c,$d
140         and     $t1,$b,$t1,ror#2
141         add     $e,$e,$t2,ror#2
142         add     $e,$e,$t1                       @ E+=F_40_59(B,C,D)
143 ___
144 } else {
145         &Xupdate(@_,1);
146 $code.=<<___;
147         and     $t1,$b,$c,ror#2
148         orr     $t2,$b,$c,ror#2
149         and     $t2,$t2,$d,ror#2
150         orr     $t1,$t1,$t2                     @ F_40_59(B,C,D)
151         add     $e,$e,$t1                       @ E+=F_40_59(B,C,D)
152 ___
153 }
154 }
155
156 $code=<<___;
157 .text
158
159 .global sha1_block_data_order
160 .type   sha1_block_data_order,%function
161
162 .align  2
163 sha1_block_data_order:
164         stmdb   sp!,{r4-r12,lr}
165         add     $len,$inp,$len,lsl#6    @ $len to point at the end of $inp
166         ldmia   $ctx,{$a,$b,$c,$d,$e}
167 .Lloop:
168         ldr     $K,.LK_00_19
169         mov     $Xi,sp
170         sub     sp,sp,#15*4
171         mov     $c,$c,ror#30
172         mov     $d,$d,ror#30
173         mov     $e,$e,ror#30            @ [6]
174 .L_00_15:
175 ___
176 for($i=0;$i<5;$i++) {
177         &BODY_00_15(@V);        unshift(@V,pop(@V));
178 }
179 $code.=<<___;
180         teq     $Xi,sp
181         bne     .L_00_15                @ [((11+4)*5+2)*3]
182 ___
183         &BODY_00_15(@V);        unshift(@V,pop(@V));
184         &BODY_16_19(@V);        unshift(@V,pop(@V));
185         &BODY_16_19(@V);        unshift(@V,pop(@V));
186         &BODY_16_19(@V);        unshift(@V,pop(@V));
187         &BODY_16_19(@V);        unshift(@V,pop(@V));
188 $code.=<<___;
189
190         ldr     $K,.LK_20_39            @ [+15+16*4]
191         sub     sp,sp,#25*4
192         cmn     sp,#0                   @ [+3], clear carry to denote 20_39
193 .L_20_39_or_60_79:
194 ___
195 for($i=0;$i<5;$i++) {
196         &BODY_20_39(@V);        unshift(@V,pop(@V));
197 }
198 $code.=<<___;
199         teq     $Xi,sp                  @ preserve carry
200         bne     .L_20_39_or_60_79       @ [+((12+3)*5+2)*4]
201         bcs     .L_done                 @ [+((12+3)*5+2)*4], spare 300 bytes
202
203         ldr     $K,.LK_40_59
204         sub     sp,sp,#20*4             @ [+2]
205 .L_40_59:
206 ___
207 for($i=0;$i<5;$i++) {
208         &BODY_40_59(@V);        unshift(@V,pop(@V));
209 }
210 $code.=<<___;
211         teq     $Xi,sp
212         bne     .L_40_59                @ [+((12+5)*5+2)*4]
213
214         ldr     $K,.LK_60_79
215         sub     sp,sp,#20*4
216         cmp     sp,#0                   @ set carry to denote 60_79
217         b       .L_20_39_or_60_79       @ [+4], spare 300 bytes
218 .L_done:
219         add     sp,sp,#80*4             @ "deallocate" stack frame
220         ldmia   $ctx,{$K,$t0,$t1,$t2,$t3}
221         add     $a,$K,$a
222         add     $b,$t0,$b
223         add     $c,$t1,$c,ror#2
224         add     $d,$t2,$d,ror#2
225         add     $e,$t3,$e,ror#2
226         stmia   $ctx,{$a,$b,$c,$d,$e}
227         teq     $inp,$len
228         bne     .Lloop                  @ [+18], total 1307
229
230         ldmia   sp!,{r4-r12,lr}
231         tst     lr,#1
232         moveq   pc,lr                   @ be binary compatible with V4, yet
233         bx      lr                      @ interoperable with Thumb ISA:-)
234 .align  2
235 .LK_00_19:      .word   0x5a827999
236 .LK_20_39:      .word   0x6ed9eba1
237 .LK_40_59:      .word   0x8f1bbcdc
238 .LK_60_79:      .word   0xca62c1d6
239 .size   sha1_block_data_order,.-sha1_block_data_order
240 .asciz  "SHA1 block transform for ARMv4, CRYPTOGAMS by <appro\@openssl.org>"
241 .align  2
242 ___
243
244 $code =~ s/\bbx\s+lr\b/.word\t0xe12fff1e/gm;    # make it possible to compile with -march=armv4
245 print $code;
246 close STDOUT; # enforce flush