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