ARM assembly pack: make it Windows-friendly.
[oweals/openssl.git] / crypto / bn / asm / armv4-gf2m.pl
1 #! /usr/bin/env perl
2 # Copyright 2011-2016 The OpenSSL Project Authors. All Rights Reserved.
3 #
4 # Licensed under the Apache License 2.0 (the "License").  You may not use
5 # this file except in compliance with the License.  You can obtain a copy
6 # in the file LICENSE in the source distribution or at
7 # https://www.openssl.org/source/license.html
8
9 #
10 # ====================================================================
11 # Written by Andy Polyakov <appro@openssl.org> for the OpenSSL
12 # project. The module is, however, dual licensed under OpenSSL and
13 # CRYPTOGAMS licenses depending on where you obtain it. For further
14 # details see http://www.openssl.org/~appro/cryptogams/.
15 # ====================================================================
16 #
17 # May 2011
18 #
19 # The module implements bn_GF2m_mul_2x2 polynomial multiplication
20 # used in bn_gf2m.c. It's kind of low-hanging mechanical port from
21 # C for the time being... Except that it has two code paths: pure
22 # integer code suitable for any ARMv4 and later CPU and NEON code
23 # suitable for ARMv7. Pure integer 1x1 multiplication subroutine runs
24 # in ~45 cycles on dual-issue core such as Cortex A8, which is ~50%
25 # faster than compiler-generated code. For ECDH and ECDSA verify (but
26 # not for ECDSA sign) it means 25%-45% improvement depending on key
27 # length, more for longer keys. Even though NEON 1x1 multiplication
28 # runs in even less cycles, ~30, improvement is measurable only on
29 # longer keys. One has to optimize code elsewhere to get NEON glow...
30 #
31 # April 2014
32 #
33 # Double bn_GF2m_mul_2x2 performance by using algorithm from paper
34 # referred below, which improves ECDH and ECDSA verify benchmarks
35 # by 18-40%.
36 #
37 # Câmara, D.; Gouvêa, C. P. L.; López, J. & Dahab, R.: Fast Software
38 # Polynomial Multiplication on ARM Processors using the NEON Engine.
39 #
40 # http://conradoplg.cryptoland.net/files/2010/12/mocrysen13.pdf
41
42 $flavour = shift;
43 if ($flavour=~/\w[\w\-]*\.\w+$/) { $output=$flavour; undef $flavour; }
44 else { while (($output=shift) && ($output!~/\w[\w\-]*\.\w+$/)) {} }
45
46 if ($flavour && $flavour ne "void") {
47     $0 =~ m/(.*[\/\\])[^\/\\]+$/; $dir=$1;
48     ( $xlate="${dir}arm-xlate.pl" and -f $xlate ) or
49     ( $xlate="${dir}../../perlasm/arm-xlate.pl" and -f $xlate) or
50     die "can't locate arm-xlate.pl";
51
52     open STDOUT,"| \"$^X\" $xlate $flavour $output";
53 } else {
54     open STDOUT,">$output";
55 }
56
57 $code=<<___;
58 #include "arm_arch.h"
59
60 #if defined(__thumb2__)
61 .syntax unified
62 .thumb
63 #else
64 .code   32
65 #endif
66
67 .text
68 ___
69 ################
70 # private interface to mul_1x1_ialu
71 #
72 $a="r1";
73 $b="r0";
74
75 ($a0,$a1,$a2,$a12,$a4,$a14)=
76 ($hi,$lo,$t0,$t1, $i0,$i1 )=map("r$_",(4..9),12);
77
78 $mask="r12";
79
80 $code.=<<___;
81 .type   mul_1x1_ialu,%function
82 .align  5
83 mul_1x1_ialu:
84         mov     $a0,#0
85         bic     $a1,$a,#3<<30           @ a1=a&0x3fffffff
86         str     $a0,[sp,#0]             @ tab[0]=0
87         add     $a2,$a1,$a1             @ a2=a1<<1
88         str     $a1,[sp,#4]             @ tab[1]=a1
89         eor     $a12,$a1,$a2            @ a1^a2
90         str     $a2,[sp,#8]             @ tab[2]=a2
91         mov     $a4,$a1,lsl#2           @ a4=a1<<2
92         str     $a12,[sp,#12]           @ tab[3]=a1^a2
93         eor     $a14,$a1,$a4            @ a1^a4
94         str     $a4,[sp,#16]            @ tab[4]=a4
95         eor     $a0,$a2,$a4             @ a2^a4
96         str     $a14,[sp,#20]           @ tab[5]=a1^a4
97         eor     $a12,$a12,$a4           @ a1^a2^a4
98         str     $a0,[sp,#24]            @ tab[6]=a2^a4
99         and     $i0,$mask,$b,lsl#2
100         str     $a12,[sp,#28]           @ tab[7]=a1^a2^a4
101
102         and     $i1,$mask,$b,lsr#1
103         ldr     $lo,[sp,$i0]            @ tab[b       & 0x7]
104         and     $i0,$mask,$b,lsr#4
105         ldr     $t1,[sp,$i1]            @ tab[b >>  3 & 0x7]
106         and     $i1,$mask,$b,lsr#7
107         ldr     $t0,[sp,$i0]            @ tab[b >>  6 & 0x7]
108         eor     $lo,$lo,$t1,lsl#3       @ stall
109         mov     $hi,$t1,lsr#29
110         ldr     $t1,[sp,$i1]            @ tab[b >>  9 & 0x7]
111
112         and     $i0,$mask,$b,lsr#10
113         eor     $lo,$lo,$t0,lsl#6
114         eor     $hi,$hi,$t0,lsr#26
115         ldr     $t0,[sp,$i0]            @ tab[b >> 12 & 0x7]
116
117         and     $i1,$mask,$b,lsr#13
118         eor     $lo,$lo,$t1,lsl#9
119         eor     $hi,$hi,$t1,lsr#23
120         ldr     $t1,[sp,$i1]            @ tab[b >> 15 & 0x7]
121
122         and     $i0,$mask,$b,lsr#16
123         eor     $lo,$lo,$t0,lsl#12
124         eor     $hi,$hi,$t0,lsr#20
125         ldr     $t0,[sp,$i0]            @ tab[b >> 18 & 0x7]
126
127         and     $i1,$mask,$b,lsr#19
128         eor     $lo,$lo,$t1,lsl#15
129         eor     $hi,$hi,$t1,lsr#17
130         ldr     $t1,[sp,$i1]            @ tab[b >> 21 & 0x7]
131
132         and     $i0,$mask,$b,lsr#22
133         eor     $lo,$lo,$t0,lsl#18
134         eor     $hi,$hi,$t0,lsr#14
135         ldr     $t0,[sp,$i0]            @ tab[b >> 24 & 0x7]
136
137         and     $i1,$mask,$b,lsr#25
138         eor     $lo,$lo,$t1,lsl#21
139         eor     $hi,$hi,$t1,lsr#11
140         ldr     $t1,[sp,$i1]            @ tab[b >> 27 & 0x7]
141
142         tst     $a,#1<<30
143         and     $i0,$mask,$b,lsr#28
144         eor     $lo,$lo,$t0,lsl#24
145         eor     $hi,$hi,$t0,lsr#8
146         ldr     $t0,[sp,$i0]            @ tab[b >> 30      ]
147
148 #ifdef  __thumb2__
149         itt     ne
150 #endif
151         eorne   $lo,$lo,$b,lsl#30
152         eorne   $hi,$hi,$b,lsr#2
153         tst     $a,#1<<31
154         eor     $lo,$lo,$t1,lsl#27
155         eor     $hi,$hi,$t1,lsr#5
156 #ifdef  __thumb2__
157         itt     ne
158 #endif
159         eorne   $lo,$lo,$b,lsl#31
160         eorne   $hi,$hi,$b,lsr#1
161         eor     $lo,$lo,$t0,lsl#30
162         eor     $hi,$hi,$t0,lsr#2
163
164         mov     pc,lr
165 .size   mul_1x1_ialu,.-mul_1x1_ialu
166 ___
167 ################
168 # void  bn_GF2m_mul_2x2(BN_ULONG *r,
169 #       BN_ULONG a1,BN_ULONG a0,
170 #       BN_ULONG b1,BN_ULONG b0);       # r[3..0]=a1a0·b1b0
171 {
172 $code.=<<___;
173 .global bn_GF2m_mul_2x2
174 .type   bn_GF2m_mul_2x2,%function
175 .align  5
176 bn_GF2m_mul_2x2:
177 #if __ARM_MAX_ARCH__>=7
178         stmdb   sp!,{r10,lr}
179         ldr     r12,.LOPENSSL_armcap
180 # if !defined(_WIN32)
181         adr     r10,.LOPENSSL_armcap
182         ldr     r12,[r12,r10]
183 # endif
184 # if defined(__APPLE__) || defined(_WIN32)
185         ldr     r12,[r12]
186 # endif
187         tst     r12,#ARMV7_NEON
188         itt     ne
189         ldrne   r10,[sp],#8
190         bne     .LNEON
191         stmdb   sp!,{r4-r9}
192 #else
193         stmdb   sp!,{r4-r10,lr}
194 #endif
195 ___
196 $ret="r10";     # reassigned 1st argument
197 $code.=<<___;
198         mov     $ret,r0                 @ reassign 1st argument
199         mov     $b,r3                   @ $b=b1
200         sub     r7,sp,#36
201         mov     r8,sp
202         and     r7,r7,#-32
203         ldr     r3,[sp,#32]             @ load b0
204         mov     $mask,#7<<2
205         mov     sp,r7                   @ allocate tab[8]
206         str     r8,[r7,#32]
207
208         bl      mul_1x1_ialu            @ a1·b1
209         str     $lo,[$ret,#8]
210         str     $hi,[$ret,#12]
211
212         eor     $b,$b,r3                @ flip b0 and b1
213          eor    $a,$a,r2                @ flip a0 and a1
214         eor     r3,r3,$b
215          eor    r2,r2,$a
216         eor     $b,$b,r3
217          eor    $a,$a,r2
218         bl      mul_1x1_ialu            @ a0·b0
219         str     $lo,[$ret]
220         str     $hi,[$ret,#4]
221
222         eor     $a,$a,r2
223         eor     $b,$b,r3
224         bl      mul_1x1_ialu            @ (a1+a0)·(b1+b0)
225 ___
226 @r=map("r$_",(6..9));
227 $code.=<<___;
228         ldmia   $ret,{@r[0]-@r[3]}
229         eor     $lo,$lo,$hi
230         ldr     sp,[sp,#32]             @ destroy tab[8]
231         eor     $hi,$hi,@r[1]
232         eor     $lo,$lo,@r[0]
233         eor     $hi,$hi,@r[2]
234         eor     $lo,$lo,@r[3]
235         eor     $hi,$hi,@r[3]
236         str     $hi,[$ret,#8]
237         eor     $lo,$lo,$hi
238         str     $lo,[$ret,#4]
239
240 #if __ARM_ARCH__>=5
241         ldmia   sp!,{r4-r10,pc}
242 #else
243         ldmia   sp!,{r4-r10,lr}
244         tst     lr,#1
245         moveq   pc,lr                   @ be binary compatible with V4, yet
246         bx      lr                      @ interoperable with Thumb ISA:-)
247 #endif
248 ___
249 }
250 {
251 my ($r,$t0,$t1,$t2,$t3)=map("q$_",(0..3,8..12));
252 my ($a,$b,$k48,$k32,$k16)=map("d$_",(26..31));
253
254 $code.=<<___;
255 #if __ARM_MAX_ARCH__>=7
256 .arch   armv7-a
257 .fpu    neon
258
259 .align  5
260 .LNEON:
261         ldr             r12, [sp]               @ 5th argument
262         vmov            $a, r2, r1
263         vmov            $b, r12, r3
264         vmov.i64        $k48, #0x0000ffffffffffff
265         vmov.i64        $k32, #0x00000000ffffffff
266         vmov.i64        $k16, #0x000000000000ffff
267
268         vext.8          $t0#lo, $a, $a, #1      @ A1
269         vmull.p8        $t0, $t0#lo, $b         @ F = A1*B
270         vext.8          $r#lo, $b, $b, #1       @ B1
271         vmull.p8        $r, $a, $r#lo           @ E = A*B1
272         vext.8          $t1#lo, $a, $a, #2      @ A2
273         vmull.p8        $t1, $t1#lo, $b         @ H = A2*B
274         vext.8          $t3#lo, $b, $b, #2      @ B2
275         vmull.p8        $t3, $a, $t3#lo         @ G = A*B2
276         vext.8          $t2#lo, $a, $a, #3      @ A3
277         veor            $t0, $t0, $r            @ L = E + F
278         vmull.p8        $t2, $t2#lo, $b         @ J = A3*B
279         vext.8          $r#lo, $b, $b, #3       @ B3
280         veor            $t1, $t1, $t3           @ M = G + H
281         vmull.p8        $r, $a, $r#lo           @ I = A*B3
282         veor            $t0#lo, $t0#lo, $t0#hi  @ t0 = (L) (P0 + P1) << 8
283         vand            $t0#hi, $t0#hi, $k48
284         vext.8          $t3#lo, $b, $b, #4      @ B4
285         veor            $t1#lo, $t1#lo, $t1#hi  @ t1 = (M) (P2 + P3) << 16
286         vand            $t1#hi, $t1#hi, $k32
287         vmull.p8        $t3, $a, $t3#lo         @ K = A*B4
288         veor            $t2, $t2, $r            @ N = I + J
289         veor            $t0#lo, $t0#lo, $t0#hi
290         veor            $t1#lo, $t1#lo, $t1#hi
291         veor            $t2#lo, $t2#lo, $t2#hi  @ t2 = (N) (P4 + P5) << 24
292         vand            $t2#hi, $t2#hi, $k16
293         vext.8          $t0, $t0, $t0, #15
294         veor            $t3#lo, $t3#lo, $t3#hi  @ t3 = (K) (P6 + P7) << 32
295         vmov.i64        $t3#hi, #0
296         vext.8          $t1, $t1, $t1, #14
297         veor            $t2#lo, $t2#lo, $t2#hi
298         vmull.p8        $r, $a, $b              @ D = A*B
299         vext.8          $t3, $t3, $t3, #12
300         vext.8          $t2, $t2, $t2, #13
301         veor            $t0, $t0, $t1
302         veor            $t2, $t2, $t3
303         veor            $r, $r, $t0
304         veor            $r, $r, $t2
305
306         vst1.32         {$r}, [r0]
307         ret             @ bx lr
308 #endif
309 ___
310 }
311 $code.=<<___;
312 .size   bn_GF2m_mul_2x2,.-bn_GF2m_mul_2x2
313 #if __ARM_MAX_ARCH__>=7
314 .align  5
315 .LOPENSSL_armcap:
316 # ifdef _WIN32
317 .word   OPENSSL_armcap_P
318 # else
319 .word   OPENSSL_armcap_P-.
320 # endif
321 #endif
322 .asciz  "GF(2^m) Multiplication for ARMv4/NEON, CRYPTOGAMS by <appro\@openssl.org>"
323 .align  5
324
325 #if __ARM_MAX_ARCH__>=7
326 .comm   OPENSSL_armcap_P,4,4
327 #endif
328 ___
329
330 foreach (split("\n",$code)) {
331         s/\`([^\`]*)\`/eval $1/geo;
332
333         s/\bq([0-9]+)#(lo|hi)/sprintf "d%d",2*$1+($2 eq "hi")/geo       or
334         s/\bret\b/bx    lr/go           or
335         s/\bbx\s+lr\b/.word\t0xe12fff1e/go;    # make it possible to compile with -march=armv4
336
337         print $_,"\n";
338 }
339 close STDOUT;   # enforce flush