03e8616a035f81c772851cbcb0dc70003e1d25cf
[openssl.git] / crypto / chacha / asm / chacha-armv8.pl
1 #! /usr/bin/env perl
2 # Copyright 2016 The OpenSSL Project Authors. All Rights Reserved.
3 #
4 # Licensed under the OpenSSL license (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 # June 2015
18
19 # ChaCha20 for ARMv8.
20 #
21 # Performance in cycles per byte out of large buffer.
22 #
23 #                       IALU/gcc-4.9    3xNEON+1xIALU   6xNEON+2xIALU
24 #
25 # Apple A7              5.50/+49%       3.33            1.70
26 # Cortex-A53            8.40/+80%       4.72            4.72(*)
27 # Cortex-A57            8.06/+43%       4.90            4.43(**)
28 # Denver                4.50/+82%       2.63            2.67(*)
29 # X-Gene                9.50/+46%       8.82            8.89(*)
30 #
31 # (*)   it's expected that doubling interleave factor doesn't help
32 #       all processors, only those with higher NEON latency and
33 #       higher instruction issue rate;
34 # (**)  expected improvement was actually higher;
35
36 $flavour=shift;
37 $output=shift;
38
39 $0 =~ m/(.*[\/\\])[^\/\\]+$/; $dir=$1;
40 ( $xlate="${dir}arm-xlate.pl" and -f $xlate ) or
41 ( $xlate="${dir}../../perlasm/arm-xlate.pl" and -f $xlate) or
42 die "can't locate arm-xlate.pl";
43
44 open OUT,"| \"$^X\" $xlate $flavour $output";
45 *STDOUT=*OUT;
46
47 sub AUTOLOAD()          # thunk [simplified] x86-style perlasm
48 { my $opcode = $AUTOLOAD; $opcode =~ s/.*:://; $opcode =~ s/_/\./;
49   my $arg = pop;
50     $arg = "#$arg" if ($arg*1 eq $arg);
51     $code .= "\t$opcode\t".join(',',@_,$arg)."\n";
52 }
53
54 my ($out,$inp,$len,$key,$ctr) = map("x$_",(0..4));
55
56 my @x=map("x$_",(5..17,19..21));
57 my @d=map("x$_",(22..28,30));
58
59 sub ROUND {
60 my ($a0,$b0,$c0,$d0)=@_;
61 my ($a1,$b1,$c1,$d1)=map(($_&~3)+(($_+1)&3),($a0,$b0,$c0,$d0));
62 my ($a2,$b2,$c2,$d2)=map(($_&~3)+(($_+1)&3),($a1,$b1,$c1,$d1));
63 my ($a3,$b3,$c3,$d3)=map(($_&~3)+(($_+1)&3),($a2,$b2,$c2,$d2));
64
65     (
66         "&add_32        (@x[$a0],@x[$a0],@x[$b0])",
67          "&add_32       (@x[$a1],@x[$a1],@x[$b1])",
68           "&add_32      (@x[$a2],@x[$a2],@x[$b2])",
69            "&add_32     (@x[$a3],@x[$a3],@x[$b3])",
70         "&eor_32        (@x[$d0],@x[$d0],@x[$a0])",
71          "&eor_32       (@x[$d1],@x[$d1],@x[$a1])",
72           "&eor_32      (@x[$d2],@x[$d2],@x[$a2])",
73            "&eor_32     (@x[$d3],@x[$d3],@x[$a3])",
74         "&ror_32        (@x[$d0],@x[$d0],16)",
75          "&ror_32       (@x[$d1],@x[$d1],16)",
76           "&ror_32      (@x[$d2],@x[$d2],16)",
77            "&ror_32     (@x[$d3],@x[$d3],16)",
78
79         "&add_32        (@x[$c0],@x[$c0],@x[$d0])",
80          "&add_32       (@x[$c1],@x[$c1],@x[$d1])",
81           "&add_32      (@x[$c2],@x[$c2],@x[$d2])",
82            "&add_32     (@x[$c3],@x[$c3],@x[$d3])",
83         "&eor_32        (@x[$b0],@x[$b0],@x[$c0])",
84          "&eor_32       (@x[$b1],@x[$b1],@x[$c1])",
85           "&eor_32      (@x[$b2],@x[$b2],@x[$c2])",
86            "&eor_32     (@x[$b3],@x[$b3],@x[$c3])",
87         "&ror_32        (@x[$b0],@x[$b0],20)",
88          "&ror_32       (@x[$b1],@x[$b1],20)",
89           "&ror_32      (@x[$b2],@x[$b2],20)",
90            "&ror_32     (@x[$b3],@x[$b3],20)",
91
92         "&add_32        (@x[$a0],@x[$a0],@x[$b0])",
93          "&add_32       (@x[$a1],@x[$a1],@x[$b1])",
94           "&add_32      (@x[$a2],@x[$a2],@x[$b2])",
95            "&add_32     (@x[$a3],@x[$a3],@x[$b3])",
96         "&eor_32        (@x[$d0],@x[$d0],@x[$a0])",
97          "&eor_32       (@x[$d1],@x[$d1],@x[$a1])",
98           "&eor_32      (@x[$d2],@x[$d2],@x[$a2])",
99            "&eor_32     (@x[$d3],@x[$d3],@x[$a3])",
100         "&ror_32        (@x[$d0],@x[$d0],24)",
101          "&ror_32       (@x[$d1],@x[$d1],24)",
102           "&ror_32      (@x[$d2],@x[$d2],24)",
103            "&ror_32     (@x[$d3],@x[$d3],24)",
104
105         "&add_32        (@x[$c0],@x[$c0],@x[$d0])",
106          "&add_32       (@x[$c1],@x[$c1],@x[$d1])",
107           "&add_32      (@x[$c2],@x[$c2],@x[$d2])",
108            "&add_32     (@x[$c3],@x[$c3],@x[$d3])",
109         "&eor_32        (@x[$b0],@x[$b0],@x[$c0])",
110          "&eor_32       (@x[$b1],@x[$b1],@x[$c1])",
111           "&eor_32      (@x[$b2],@x[$b2],@x[$c2])",
112            "&eor_32     (@x[$b3],@x[$b3],@x[$c3])",
113         "&ror_32        (@x[$b0],@x[$b0],25)",
114          "&ror_32       (@x[$b1],@x[$b1],25)",
115           "&ror_32      (@x[$b2],@x[$b2],25)",
116            "&ror_32     (@x[$b3],@x[$b3],25)"
117     );
118 }
119
120 $code.=<<___;
121 #include "arm_arch.h"
122
123 .text
124
125 .extern OPENSSL_armcap_P
126
127 .align  5
128 .Lsigma:
129 .quad   0x3320646e61707865,0x6b20657479622d32           // endian-neutral
130 .Lone:
131 .long   1,0,0,0
132 .LOPENSSL_armcap_P:
133 #ifdef  __ILP32__
134 .long   OPENSSL_armcap_P-.
135 #else
136 .quad   OPENSSL_armcap_P-.
137 #endif
138 .asciz  "ChaCha20 for ARMv8, CRYPTOGAMS by <appro\@openssl.org>"
139
140 .globl  ChaCha20_ctr32
141 .type   ChaCha20_ctr32,%function
142 .align  5
143 ChaCha20_ctr32:
144         cbz     $len,.Labort
145         adr     @x[0],.LOPENSSL_armcap_P
146         cmp     $len,#192
147         b.lo    .Lshort
148 #ifdef  __ILP32__
149         ldrsw   @x[1],[@x[0]]
150 #else
151         ldr     @x[1],[@x[0]]
152 #endif
153         ldr     w17,[@x[1],@x[0]]
154         tst     w17,#ARMV7_NEON
155         b.ne    ChaCha20_neon
156
157 .Lshort:
158         stp     x29,x30,[sp,#-96]!
159         add     x29,sp,#0
160
161         adr     @x[0],.Lsigma
162         stp     x19,x20,[sp,#16]
163         stp     x21,x22,[sp,#32]
164         stp     x23,x24,[sp,#48]
165         stp     x25,x26,[sp,#64]
166         stp     x27,x28,[sp,#80]
167         sub     sp,sp,#64
168
169         ldp     @d[0],@d[1],[@x[0]]             // load sigma
170         ldp     @d[2],@d[3],[$key]              // load key
171         ldp     @d[4],@d[5],[$key,#16]
172         ldp     @d[6],@d[7],[$ctr]              // load counter
173 #ifdef  __ARMEB__
174         ror     @d[2],@d[2],#32
175         ror     @d[3],@d[3],#32
176         ror     @d[4],@d[4],#32
177         ror     @d[5],@d[5],#32
178         ror     @d[6],@d[6],#32
179         ror     @d[7],@d[7],#32
180 #endif
181
182 .Loop_outer:
183         mov.32  @x[0],@d[0]                     // unpack key block
184         lsr     @x[1],@d[0],#32
185         mov.32  @x[2],@d[1]
186         lsr     @x[3],@d[1],#32
187         mov.32  @x[4],@d[2]
188         lsr     @x[5],@d[2],#32
189         mov.32  @x[6],@d[3]
190         lsr     @x[7],@d[3],#32
191         mov.32  @x[8],@d[4]
192         lsr     @x[9],@d[4],#32
193         mov.32  @x[10],@d[5]
194         lsr     @x[11],@d[5],#32
195         mov.32  @x[12],@d[6]
196         lsr     @x[13],@d[6],#32
197         mov.32  @x[14],@d[7]
198         lsr     @x[15],@d[7],#32
199
200         mov     $ctr,#10
201         subs    $len,$len,#64
202 .Loop:
203         sub     $ctr,$ctr,#1    
204 ___
205         foreach (&ROUND(0, 4, 8,12)) { eval; }
206         foreach (&ROUND(0, 5,10,15)) { eval; }
207 $code.=<<___;
208         cbnz    $ctr,.Loop
209
210         add.32  @x[0],@x[0],@d[0]               // accumulate key block
211         add     @x[1],@x[1],@d[0],lsr#32
212         add.32  @x[2],@x[2],@d[1]
213         add     @x[3],@x[3],@d[1],lsr#32
214         add.32  @x[4],@x[4],@d[2]
215         add     @x[5],@x[5],@d[2],lsr#32
216         add.32  @x[6],@x[6],@d[3]
217         add     @x[7],@x[7],@d[3],lsr#32
218         add.32  @x[8],@x[8],@d[4]
219         add     @x[9],@x[9],@d[4],lsr#32
220         add.32  @x[10],@x[10],@d[5]
221         add     @x[11],@x[11],@d[5],lsr#32
222         add.32  @x[12],@x[12],@d[6]
223         add     @x[13],@x[13],@d[6],lsr#32
224         add.32  @x[14],@x[14],@d[7]
225         add     @x[15],@x[15],@d[7],lsr#32
226
227         b.lo    .Ltail
228
229         add     @x[0],@x[0],@x[1],lsl#32        // pack
230         add     @x[2],@x[2],@x[3],lsl#32
231         ldp     @x[1],@x[3],[$inp,#0]           // load input
232         add     @x[4],@x[4],@x[5],lsl#32
233         add     @x[6],@x[6],@x[7],lsl#32
234         ldp     @x[5],@x[7],[$inp,#16]
235         add     @x[8],@x[8],@x[9],lsl#32
236         add     @x[10],@x[10],@x[11],lsl#32
237         ldp     @x[9],@x[11],[$inp,#32]
238         add     @x[12],@x[12],@x[13],lsl#32
239         add     @x[14],@x[14],@x[15],lsl#32
240         ldp     @x[13],@x[15],[$inp,#48]
241         add     $inp,$inp,#64
242 #ifdef  __ARMEB__
243         rev     @x[0],@x[0]
244         rev     @x[2],@x[2]
245         rev     @x[4],@x[4]
246         rev     @x[6],@x[6]
247         rev     @x[8],@x[8]
248         rev     @x[10],@x[10]
249         rev     @x[12],@x[12]
250         rev     @x[14],@x[14]
251 #endif
252         eor     @x[0],@x[0],@x[1]
253         eor     @x[2],@x[2],@x[3]
254         eor     @x[4],@x[4],@x[5]
255         eor     @x[6],@x[6],@x[7]
256         eor     @x[8],@x[8],@x[9]
257         eor     @x[10],@x[10],@x[11]
258         eor     @x[12],@x[12],@x[13]
259         eor     @x[14],@x[14],@x[15]
260
261         stp     @x[0],@x[2],[$out,#0]           // store output
262          add    @d[6],@d[6],#1                  // increment counter
263         stp     @x[4],@x[6],[$out,#16]
264         stp     @x[8],@x[10],[$out,#32]
265         stp     @x[12],@x[14],[$out,#48]
266         add     $out,$out,#64
267
268         b.hi    .Loop_outer
269
270         ldp     x19,x20,[x29,#16]
271         add     sp,sp,#64
272         ldp     x21,x22,[x29,#32]
273         ldp     x23,x24,[x29,#48]
274         ldp     x25,x26,[x29,#64]
275         ldp     x27,x28,[x29,#80]
276         ldp     x29,x30,[sp],#96
277 .Labort:
278         ret
279
280 .align  4
281 .Ltail:
282         add     $len,$len,#64
283 .Less_than_64:
284         sub     $out,$out,#1
285         add     $inp,$inp,$len
286         add     $out,$out,$len
287         add     $ctr,sp,$len
288         neg     $len,$len
289
290         add     @x[0],@x[0],@x[1],lsl#32        // pack
291         add     @x[2],@x[2],@x[3],lsl#32
292         add     @x[4],@x[4],@x[5],lsl#32
293         add     @x[6],@x[6],@x[7],lsl#32
294         add     @x[8],@x[8],@x[9],lsl#32
295         add     @x[10],@x[10],@x[11],lsl#32
296         add     @x[12],@x[12],@x[13],lsl#32
297         add     @x[14],@x[14],@x[15],lsl#32
298 #ifdef  __ARMEB__
299         rev     @x[0],@x[0]
300         rev     @x[2],@x[2]
301         rev     @x[4],@x[4]
302         rev     @x[6],@x[6]
303         rev     @x[8],@x[8]
304         rev     @x[10],@x[10]
305         rev     @x[12],@x[12]
306         rev     @x[14],@x[14]
307 #endif
308         stp     @x[0],@x[2],[sp,#0]
309         stp     @x[4],@x[6],[sp,#16]
310         stp     @x[8],@x[10],[sp,#32]
311         stp     @x[12],@x[14],[sp,#48]
312
313 .Loop_tail:
314         ldrb    w10,[$inp,$len]
315         ldrb    w11,[$ctr,$len]
316         add     $len,$len,#1
317         eor     w10,w10,w11
318         strb    w10,[$out,$len]
319         cbnz    $len,.Loop_tail
320
321         stp     xzr,xzr,[sp,#0]
322         stp     xzr,xzr,[sp,#16]
323         stp     xzr,xzr,[sp,#32]
324         stp     xzr,xzr,[sp,#48]
325
326         ldp     x19,x20,[x29,#16]
327         add     sp,sp,#64
328         ldp     x21,x22,[x29,#32]
329         ldp     x23,x24,[x29,#48]
330         ldp     x25,x26,[x29,#64]
331         ldp     x27,x28,[x29,#80]
332         ldp     x29,x30,[sp],#96
333         ret
334 .size   ChaCha20_ctr32,.-ChaCha20_ctr32
335 ___
336
337 {{{
338 my ($A0,$B0,$C0,$D0,$A1,$B1,$C1,$D1,$A2,$B2,$C2,$D2,$T0,$T1,$T2,$T3) =
339     map("v$_.4s",(0..7,16..23));
340 my (@K)=map("v$_.4s",(24..30));
341 my $ONE="v31.4s";
342
343 sub NEONROUND {
344 my $odd = pop;
345 my ($a,$b,$c,$d,$t)=@_;
346
347         (
348         "&add           ('$a','$a','$b')",
349         "&eor           ('$d','$d','$a')",
350         "&rev32_16      ('$d','$d')",           # vrot ($d,16)
351
352         "&add           ('$c','$c','$d')",
353         "&eor           ('$t','$b','$c')",
354         "&ushr          ('$b','$t',20)",
355         "&sli           ('$b','$t',12)",
356
357         "&add           ('$a','$a','$b')",
358         "&eor           ('$t','$d','$a')",
359         "&ushr          ('$d','$t',24)",
360         "&sli           ('$d','$t',8)",
361
362         "&add           ('$c','$c','$d')",
363         "&eor           ('$t','$b','$c')",
364         "&ushr          ('$b','$t',25)",
365         "&sli           ('$b','$t',7)",
366
367         "&ext           ('$c','$c','$c',8)",
368         "&ext           ('$d','$d','$d',$odd?4:12)",
369         "&ext           ('$b','$b','$b',$odd?12:4)"
370         );
371 }
372
373 $code.=<<___;
374
375 .type   ChaCha20_neon,%function
376 .align  5
377 ChaCha20_neon:
378         stp     x29,x30,[sp,#-96]!
379         add     x29,sp,#0
380
381         adr     @x[0],.Lsigma
382         stp     x19,x20,[sp,#16]
383         stp     x21,x22,[sp,#32]
384         stp     x23,x24,[sp,#48]
385         stp     x25,x26,[sp,#64]
386         stp     x27,x28,[sp,#80]
387         cmp     $len,#512
388         b.hs    .L512_or_more_neon
389
390         sub     sp,sp,#64
391
392         ldp     @d[0],@d[1],[@x[0]]             // load sigma
393         ld1     {@K[0]},[@x[0]],#16
394         ldp     @d[2],@d[3],[$key]              // load key
395         ldp     @d[4],@d[5],[$key,#16]
396         ld1     {@K[1],@K[2]},[$key]
397         ldp     @d[6],@d[7],[$ctr]              // load counter
398         ld1     {@K[3]},[$ctr]
399         ld1     {$ONE},[@x[0]]
400 #ifdef  __ARMEB__
401         rev64   @K[0],@K[0]
402         ror     @d[2],@d[2],#32
403         ror     @d[3],@d[3],#32
404         ror     @d[4],@d[4],#32
405         ror     @d[5],@d[5],#32
406         ror     @d[6],@d[6],#32
407         ror     @d[7],@d[7],#32
408 #endif
409         add     @K[3],@K[3],$ONE                // += 1
410         add     @K[4],@K[3],$ONE
411         add     @K[5],@K[4],$ONE
412         shl     $ONE,$ONE,#2                    // 1 -> 4
413
414 .Loop_outer_neon:
415         mov.32  @x[0],@d[0]                     // unpack key block
416         lsr     @x[1],@d[0],#32
417          mov    $A0,@K[0]
418         mov.32  @x[2],@d[1]
419         lsr     @x[3],@d[1],#32
420          mov    $A1,@K[0]
421         mov.32  @x[4],@d[2]
422         lsr     @x[5],@d[2],#32
423          mov    $A2,@K[0]
424         mov.32  @x[6],@d[3]
425          mov    $B0,@K[1]
426         lsr     @x[7],@d[3],#32
427          mov    $B1,@K[1]
428         mov.32  @x[8],@d[4]
429          mov    $B2,@K[1]
430         lsr     @x[9],@d[4],#32
431          mov    $D0,@K[3]
432         mov.32  @x[10],@d[5]
433          mov    $D1,@K[4]
434         lsr     @x[11],@d[5],#32
435          mov    $D2,@K[5]
436         mov.32  @x[12],@d[6]
437          mov    $C0,@K[2]
438         lsr     @x[13],@d[6],#32
439          mov    $C1,@K[2]
440         mov.32  @x[14],@d[7]
441          mov    $C2,@K[2]
442         lsr     @x[15],@d[7],#32
443
444         mov     $ctr,#10
445         subs    $len,$len,#256
446 .Loop_neon:
447         sub     $ctr,$ctr,#1
448 ___
449         my @thread0=&NEONROUND($A0,$B0,$C0,$D0,$T0,0);
450         my @thread1=&NEONROUND($A1,$B1,$C1,$D1,$T1,0);
451         my @thread2=&NEONROUND($A2,$B2,$C2,$D2,$T2,0);
452         my @thread3=&ROUND(0,4,8,12);
453
454         foreach (@thread0) {
455                 eval;                   eval(shift(@thread3));
456                 eval(shift(@thread1));  eval(shift(@thread3));
457                 eval(shift(@thread2));  eval(shift(@thread3));
458         }
459
460         @thread0=&NEONROUND($A0,$B0,$C0,$D0,$T0,1);
461         @thread1=&NEONROUND($A1,$B1,$C1,$D1,$T1,1);
462         @thread2=&NEONROUND($A2,$B2,$C2,$D2,$T2,1);
463         @thread3=&ROUND(0,5,10,15);
464
465         foreach (@thread0) {
466                 eval;                   eval(shift(@thread3));
467                 eval(shift(@thread1));  eval(shift(@thread3));
468                 eval(shift(@thread2));  eval(shift(@thread3));
469         }
470 $code.=<<___;
471         cbnz    $ctr,.Loop_neon
472
473         add.32  @x[0],@x[0],@d[0]               // accumulate key block
474          add    $A0,$A0,@K[0]
475         add     @x[1],@x[1],@d[0],lsr#32
476          add    $A1,$A1,@K[0]
477         add.32  @x[2],@x[2],@d[1]
478          add    $A2,$A2,@K[0]
479         add     @x[3],@x[3],@d[1],lsr#32
480          add    $C0,$C0,@K[2]
481         add.32  @x[4],@x[4],@d[2]
482          add    $C1,$C1,@K[2]
483         add     @x[5],@x[5],@d[2],lsr#32
484          add    $C2,$C2,@K[2]
485         add.32  @x[6],@x[6],@d[3]
486          add    $D0,$D0,@K[3]
487         add     @x[7],@x[7],@d[3],lsr#32
488         add.32  @x[8],@x[8],@d[4]
489          add    $D1,$D1,@K[4]
490         add     @x[9],@x[9],@d[4],lsr#32
491         add.32  @x[10],@x[10],@d[5]
492          add    $D2,$D2,@K[5]
493         add     @x[11],@x[11],@d[5],lsr#32
494         add.32  @x[12],@x[12],@d[6]
495          add    $B0,$B0,@K[1]
496         add     @x[13],@x[13],@d[6],lsr#32
497         add.32  @x[14],@x[14],@d[7]
498          add    $B1,$B1,@K[1]
499         add     @x[15],@x[15],@d[7],lsr#32
500          add    $B2,$B2,@K[1]
501
502         b.lo    .Ltail_neon
503
504         add     @x[0],@x[0],@x[1],lsl#32        // pack
505         add     @x[2],@x[2],@x[3],lsl#32
506         ldp     @x[1],@x[3],[$inp,#0]           // load input
507         add     @x[4],@x[4],@x[5],lsl#32
508         add     @x[6],@x[6],@x[7],lsl#32
509         ldp     @x[5],@x[7],[$inp,#16]
510         add     @x[8],@x[8],@x[9],lsl#32
511         add     @x[10],@x[10],@x[11],lsl#32
512         ldp     @x[9],@x[11],[$inp,#32]
513         add     @x[12],@x[12],@x[13],lsl#32
514         add     @x[14],@x[14],@x[15],lsl#32
515         ldp     @x[13],@x[15],[$inp,#48]
516         add     $inp,$inp,#64
517 #ifdef  __ARMEB__
518         rev     @x[0],@x[0]
519         rev     @x[2],@x[2]
520         rev     @x[4],@x[4]
521         rev     @x[6],@x[6]
522         rev     @x[8],@x[8]
523         rev     @x[10],@x[10]
524         rev     @x[12],@x[12]
525         rev     @x[14],@x[14]
526 #endif
527         ld1.8   {$T0-$T3},[$inp],#64
528         eor     @x[0],@x[0],@x[1]
529         eor     @x[2],@x[2],@x[3]
530         eor     @x[4],@x[4],@x[5]
531         eor     @x[6],@x[6],@x[7]
532         eor     @x[8],@x[8],@x[9]
533          eor    $A0,$A0,$T0
534         eor     @x[10],@x[10],@x[11]
535          eor    $B0,$B0,$T1
536         eor     @x[12],@x[12],@x[13]
537          eor    $C0,$C0,$T2
538         eor     @x[14],@x[14],@x[15]
539          eor    $D0,$D0,$T3
540          ld1.8  {$T0-$T3},[$inp],#64
541
542         stp     @x[0],@x[2],[$out,#0]           // store output
543          add    @d[6],@d[6],#4                  // increment counter
544         stp     @x[4],@x[6],[$out,#16]
545          add    @K[3],@K[3],$ONE                // += 4
546         stp     @x[8],@x[10],[$out,#32]
547          add    @K[4],@K[4],$ONE
548         stp     @x[12],@x[14],[$out,#48]
549          add    @K[5],@K[5],$ONE
550         add     $out,$out,#64
551
552         st1.8   {$A0-$D0},[$out],#64
553         ld1.8   {$A0-$D0},[$inp],#64
554
555         eor     $A1,$A1,$T0
556         eor     $B1,$B1,$T1
557         eor     $C1,$C1,$T2
558         eor     $D1,$D1,$T3
559         st1.8   {$A1-$D1},[$out],#64
560
561         eor     $A2,$A2,$A0
562         eor     $B2,$B2,$B0
563         eor     $C2,$C2,$C0
564         eor     $D2,$D2,$D0
565         st1.8   {$A2-$D2},[$out],#64
566
567         b.hi    .Loop_outer_neon
568
569         ldp     x19,x20,[x29,#16]
570         add     sp,sp,#64
571         ldp     x21,x22,[x29,#32]
572         ldp     x23,x24,[x29,#48]
573         ldp     x25,x26,[x29,#64]
574         ldp     x27,x28,[x29,#80]
575         ldp     x29,x30,[sp],#96
576         ret
577
578 .Ltail_neon:
579         add     $len,$len,#256
580         cmp     $len,#64
581         b.lo    .Less_than_64
582
583         add     @x[0],@x[0],@x[1],lsl#32        // pack
584         add     @x[2],@x[2],@x[3],lsl#32
585         ldp     @x[1],@x[3],[$inp,#0]           // load input
586         add     @x[4],@x[4],@x[5],lsl#32
587         add     @x[6],@x[6],@x[7],lsl#32
588         ldp     @x[5],@x[7],[$inp,#16]
589         add     @x[8],@x[8],@x[9],lsl#32
590         add     @x[10],@x[10],@x[11],lsl#32
591         ldp     @x[9],@x[11],[$inp,#32]
592         add     @x[12],@x[12],@x[13],lsl#32
593         add     @x[14],@x[14],@x[15],lsl#32
594         ldp     @x[13],@x[15],[$inp,#48]
595         add     $inp,$inp,#64
596 #ifdef  __ARMEB__
597         rev     @x[0],@x[0]
598         rev     @x[2],@x[2]
599         rev     @x[4],@x[4]
600         rev     @x[6],@x[6]
601         rev     @x[8],@x[8]
602         rev     @x[10],@x[10]
603         rev     @x[12],@x[12]
604         rev     @x[14],@x[14]
605 #endif
606         eor     @x[0],@x[0],@x[1]
607         eor     @x[2],@x[2],@x[3]
608         eor     @x[4],@x[4],@x[5]
609         eor     @x[6],@x[6],@x[7]
610         eor     @x[8],@x[8],@x[9]
611         eor     @x[10],@x[10],@x[11]
612         eor     @x[12],@x[12],@x[13]
613         eor     @x[14],@x[14],@x[15]
614
615         stp     @x[0],@x[2],[$out,#0]           // store output
616          add    @d[6],@d[6],#4                  // increment counter
617         stp     @x[4],@x[6],[$out,#16]
618         stp     @x[8],@x[10],[$out,#32]
619         stp     @x[12],@x[14],[$out,#48]
620         add     $out,$out,#64
621         b.eq    .Ldone_neon
622         sub     $len,$len,#64
623         cmp     $len,#64
624         b.lo    .Less_than_128
625
626         ld1.8   {$T0-$T3},[$inp],#64
627         eor     $A0,$A0,$T0
628         eor     $B0,$B0,$T1
629         eor     $C0,$C0,$T2
630         eor     $D0,$D0,$T3
631         st1.8   {$A0-$D0},[$out],#64
632         b.eq    .Ldone_neon
633         sub     $len,$len,#64
634         cmp     $len,#64
635         b.lo    .Less_than_192
636
637         ld1.8   {$T0-$T3},[$inp],#64
638         eor     $A1,$A1,$T0
639         eor     $B1,$B1,$T1
640         eor     $C1,$C1,$T2
641         eor     $D1,$D1,$T3
642         st1.8   {$A1-$D1},[$out],#64
643         b.eq    .Ldone_neon
644         sub     $len,$len,#64
645
646         st1.8   {$A2-$D2},[sp]
647         b       .Last_neon
648
649 .Less_than_128:
650         st1.8   {$A0-$D0},[sp]
651         b       .Last_neon
652 .Less_than_192:
653         st1.8   {$A1-$D1},[sp]
654         b       .Last_neon
655
656 .align  4
657 .Last_neon:
658         sub     $out,$out,#1
659         add     $inp,$inp,$len
660         add     $out,$out,$len
661         add     $ctr,sp,$len
662         neg     $len,$len
663
664 .Loop_tail_neon:
665         ldrb    w10,[$inp,$len]
666         ldrb    w11,[$ctr,$len]
667         add     $len,$len,#1
668         eor     w10,w10,w11
669         strb    w10,[$out,$len]
670         cbnz    $len,.Loop_tail_neon
671
672         stp     xzr,xzr,[sp,#0]
673         stp     xzr,xzr,[sp,#16]
674         stp     xzr,xzr,[sp,#32]
675         stp     xzr,xzr,[sp,#48]
676
677 .Ldone_neon:
678         ldp     x19,x20,[x29,#16]
679         add     sp,sp,#64
680         ldp     x21,x22,[x29,#32]
681         ldp     x23,x24,[x29,#48]
682         ldp     x25,x26,[x29,#64]
683         ldp     x27,x28,[x29,#80]
684         ldp     x29,x30,[sp],#96
685         ret
686 .size   ChaCha20_neon,.-ChaCha20_neon
687 ___
688 {
689 my ($T0,$T1,$T2,$T3,$T4,$T5)=@K;
690 my ($A0,$B0,$C0,$D0,$A1,$B1,$C1,$D1,$A2,$B2,$C2,$D2,
691     $A3,$B3,$C3,$D3,$A4,$B4,$C4,$D4,$A5,$B5,$C5,$D5) = map("v$_.4s",(0..23));
692
693 $code.=<<___;
694 .type   ChaCha20_512_neon,%function
695 .align  5
696 ChaCha20_512_neon:
697         stp     x29,x30,[sp,#-96]!
698         add     x29,sp,#0
699
700         adr     @x[0],.Lsigma
701         stp     x19,x20,[sp,#16]
702         stp     x21,x22,[sp,#32]
703         stp     x23,x24,[sp,#48]
704         stp     x25,x26,[sp,#64]
705         stp     x27,x28,[sp,#80]
706
707 .L512_or_more_neon:
708         sub     sp,sp,#128+64
709
710         ldp     @d[0],@d[1],[@x[0]]             // load sigma
711         ld1     {@K[0]},[@x[0]],#16
712         ldp     @d[2],@d[3],[$key]              // load key
713         ldp     @d[4],@d[5],[$key,#16]
714         ld1     {@K[1],@K[2]},[$key]
715         ldp     @d[6],@d[7],[$ctr]              // load counter
716         ld1     {@K[3]},[$ctr]
717         ld1     {$ONE},[@x[0]]
718 #ifdef  __ARMEB__
719         rev64   @K[0],@K[0]
720         ror     @d[2],@d[2],#32
721         ror     @d[3],@d[3],#32
722         ror     @d[4],@d[4],#32
723         ror     @d[5],@d[5],#32
724         ror     @d[6],@d[6],#32
725         ror     @d[7],@d[7],#32
726 #endif
727         add     @K[3],@K[3],$ONE                // += 1
728         stp     @K[0],@K[1],[sp,#0]             // off-load key block, invariant part
729         add     @K[3],@K[3],$ONE                // not typo
730         str     @K[2],[sp,#32]
731         add     @K[4],@K[3],$ONE
732         add     @K[5],@K[4],$ONE
733         add     @K[6],@K[5],$ONE
734         shl     $ONE,$ONE,#2                    // 1 -> 4
735
736         stp     d8,d9,[sp,#128+0]               // meet ABI requirements
737         stp     d10,d11,[sp,#128+16]
738         stp     d12,d13,[sp,#128+32]
739         stp     d14,d15,[sp,#128+48]
740
741         sub     $len,$len,#512                  // not typo
742
743 .Loop_outer_512_neon:
744          mov    $A0,@K[0]
745          mov    $A1,@K[0]
746          mov    $A2,@K[0]
747          mov    $A3,@K[0]
748          mov    $A4,@K[0]
749          mov    $A5,@K[0]
750          mov    $B0,@K[1]
751         mov.32  @x[0],@d[0]                     // unpack key block
752          mov    $B1,@K[1]
753         lsr     @x[1],@d[0],#32
754          mov    $B2,@K[1]
755         mov.32  @x[2],@d[1]
756          mov    $B3,@K[1]
757         lsr     @x[3],@d[1],#32
758          mov    $B4,@K[1]
759         mov.32  @x[4],@d[2]
760          mov    $B5,@K[1]
761         lsr     @x[5],@d[2],#32
762          mov    $D0,@K[3]
763         mov.32  @x[6],@d[3]
764          mov    $D1,@K[4]
765         lsr     @x[7],@d[3],#32
766          mov    $D2,@K[5]
767         mov.32  @x[8],@d[4]
768          mov    $D3,@K[6]
769         lsr     @x[9],@d[4],#32
770          mov    $C0,@K[2]
771         mov.32  @x[10],@d[5]
772          mov    $C1,@K[2]
773         lsr     @x[11],@d[5],#32
774          add    $D4,$D0,$ONE                    // +4
775         mov.32  @x[12],@d[6]
776          add    $D5,$D1,$ONE                    // +4
777         lsr     @x[13],@d[6],#32
778          mov    $C2,@K[2]
779         mov.32  @x[14],@d[7]
780          mov    $C3,@K[2]
781         lsr     @x[15],@d[7],#32
782          mov    $C4,@K[2]
783          stp    @K[3],@K[4],[sp,#48]            // off-load key block, variable part
784          mov    $C5,@K[2]
785          str    @K[5],[sp,#80]
786
787         mov     $ctr,#5
788         subs    $len,$len,#512
789 .Loop_upper_neon:
790         sub     $ctr,$ctr,#1
791 ___
792         my @thread0=&NEONROUND($A0,$B0,$C0,$D0,$T0,0);
793         my @thread1=&NEONROUND($A1,$B1,$C1,$D1,$T1,0);
794         my @thread2=&NEONROUND($A2,$B2,$C2,$D2,$T2,0);
795         my @thread3=&NEONROUND($A3,$B3,$C3,$D3,$T3,0);
796         my @thread4=&NEONROUND($A4,$B4,$C4,$D4,$T4,0);
797         my @thread5=&NEONROUND($A5,$B5,$C5,$D5,$T5,0);
798         my @thread67=(&ROUND(0,4,8,12),&ROUND(0,5,10,15));
799         my $diff = ($#thread0+1)*6 - $#thread67 - 1;
800         my $i = 0;
801
802         foreach (@thread0) {
803                 eval;                   eval(shift(@thread67));
804                 eval(shift(@thread1));  eval(shift(@thread67));
805                 eval(shift(@thread2));  eval(shift(@thread67));
806                 eval(shift(@thread3));  eval(shift(@thread67));
807                 eval(shift(@thread4));  eval(shift(@thread67));
808                 eval(shift(@thread5));  eval(shift(@thread67));
809         }
810
811         @thread0=&NEONROUND($A0,$B0,$C0,$D0,$T0,1);
812         @thread1=&NEONROUND($A1,$B1,$C1,$D1,$T1,1);
813         @thread2=&NEONROUND($A2,$B2,$C2,$D2,$T2,1);
814         @thread3=&NEONROUND($A3,$B3,$C3,$D3,$T3,1);
815         @thread4=&NEONROUND($A4,$B4,$C4,$D4,$T4,1);
816         @thread5=&NEONROUND($A5,$B5,$C5,$D5,$T5,1);
817         @thread67=(&ROUND(0,4,8,12),&ROUND(0,5,10,15));
818
819         foreach (@thread0) {
820                 eval;                   eval(shift(@thread67));
821                 eval(shift(@thread1));  eval(shift(@thread67));
822                 eval(shift(@thread2));  eval(shift(@thread67));
823                 eval(shift(@thread3));  eval(shift(@thread67));
824                 eval(shift(@thread4));  eval(shift(@thread67));
825                 eval(shift(@thread5));  eval(shift(@thread67));
826         }
827 $code.=<<___;
828         cbnz    $ctr,.Loop_upper_neon
829
830         add.32  @x[0],@x[0],@d[0]               // accumulate key block
831         add     @x[1],@x[1],@d[0],lsr#32
832         add.32  @x[2],@x[2],@d[1]
833         add     @x[3],@x[3],@d[1],lsr#32
834         add.32  @x[4],@x[4],@d[2]
835         add     @x[5],@x[5],@d[2],lsr#32
836         add.32  @x[6],@x[6],@d[3]
837         add     @x[7],@x[7],@d[3],lsr#32
838         add.32  @x[8],@x[8],@d[4]
839         add     @x[9],@x[9],@d[4],lsr#32
840         add.32  @x[10],@x[10],@d[5]
841         add     @x[11],@x[11],@d[5],lsr#32
842         add.32  @x[12],@x[12],@d[6]
843         add     @x[13],@x[13],@d[6],lsr#32
844         add.32  @x[14],@x[14],@d[7]
845         add     @x[15],@x[15],@d[7],lsr#32
846
847         add     @x[0],@x[0],@x[1],lsl#32        // pack
848         add     @x[2],@x[2],@x[3],lsl#32
849         ldp     @x[1],@x[3],[$inp,#0]           // load input
850         add     @x[4],@x[4],@x[5],lsl#32
851         add     @x[6],@x[6],@x[7],lsl#32
852         ldp     @x[5],@x[7],[$inp,#16]
853         add     @x[8],@x[8],@x[9],lsl#32
854         add     @x[10],@x[10],@x[11],lsl#32
855         ldp     @x[9],@x[11],[$inp,#32]
856         add     @x[12],@x[12],@x[13],lsl#32
857         add     @x[14],@x[14],@x[15],lsl#32
858         ldp     @x[13],@x[15],[$inp,#48]
859         add     $inp,$inp,#64
860 #ifdef  __ARMEB__
861         rev     @x[0],@x[0]
862         rev     @x[2],@x[2]
863         rev     @x[4],@x[4]
864         rev     @x[6],@x[6]
865         rev     @x[8],@x[8]
866         rev     @x[10],@x[10]
867         rev     @x[12],@x[12]
868         rev     @x[14],@x[14]
869 #endif
870         eor     @x[0],@x[0],@x[1]
871         eor     @x[2],@x[2],@x[3]
872         eor     @x[4],@x[4],@x[5]
873         eor     @x[6],@x[6],@x[7]
874         eor     @x[8],@x[8],@x[9]
875         eor     @x[10],@x[10],@x[11]
876         eor     @x[12],@x[12],@x[13]
877         eor     @x[14],@x[14],@x[15]
878
879          stp    @x[0],@x[2],[$out,#0]           // store output
880          add    @d[6],@d[6],#1                  // increment counter
881         mov.32  @x[0],@d[0]                     // unpack key block
882         lsr     @x[1],@d[0],#32
883          stp    @x[4],@x[6],[$out,#16]
884         mov.32  @x[2],@d[1]
885         lsr     @x[3],@d[1],#32
886          stp    @x[8],@x[10],[$out,#32]
887         mov.32  @x[4],@d[2]
888         lsr     @x[5],@d[2],#32
889          stp    @x[12],@x[14],[$out,#48]
890          add    $out,$out,#64
891         mov.32  @x[6],@d[3]
892         lsr     @x[7],@d[3],#32
893         mov.32  @x[8],@d[4]
894         lsr     @x[9],@d[4],#32
895         mov.32  @x[10],@d[5]
896         lsr     @x[11],@d[5],#32
897         mov.32  @x[12],@d[6]
898         lsr     @x[13],@d[6],#32
899         mov.32  @x[14],@d[7]
900         lsr     @x[15],@d[7],#32
901
902         mov     $ctr,#5
903 .Loop_lower_neon:
904         sub     $ctr,$ctr,#1
905 ___
906         @thread0=&NEONROUND($A0,$B0,$C0,$D0,$T0,0);
907         @thread1=&NEONROUND($A1,$B1,$C1,$D1,$T1,0);
908         @thread2=&NEONROUND($A2,$B2,$C2,$D2,$T2,0);
909         @thread3=&NEONROUND($A3,$B3,$C3,$D3,$T3,0);
910         @thread4=&NEONROUND($A4,$B4,$C4,$D4,$T4,0);
911         @thread5=&NEONROUND($A5,$B5,$C5,$D5,$T5,0);
912         @thread67=(&ROUND(0,4,8,12),&ROUND(0,5,10,15));
913
914         foreach (@thread0) {
915                 eval;                   eval(shift(@thread67));
916                 eval(shift(@thread1));  eval(shift(@thread67));
917                 eval(shift(@thread2));  eval(shift(@thread67));
918                 eval(shift(@thread3));  eval(shift(@thread67));
919                 eval(shift(@thread4));  eval(shift(@thread67));
920                 eval(shift(@thread5));  eval(shift(@thread67));
921         }
922
923         @thread0=&NEONROUND($A0,$B0,$C0,$D0,$T0,1);
924         @thread1=&NEONROUND($A1,$B1,$C1,$D1,$T1,1);
925         @thread2=&NEONROUND($A2,$B2,$C2,$D2,$T2,1);
926         @thread3=&NEONROUND($A3,$B3,$C3,$D3,$T3,1);
927         @thread4=&NEONROUND($A4,$B4,$C4,$D4,$T4,1);
928         @thread5=&NEONROUND($A5,$B5,$C5,$D5,$T5,1);
929         @thread67=(&ROUND(0,4,8,12),&ROUND(0,5,10,15));
930
931         foreach (@thread0) {
932                 eval;                   eval(shift(@thread67));
933                 eval(shift(@thread1));  eval(shift(@thread67));
934                 eval(shift(@thread2));  eval(shift(@thread67));
935                 eval(shift(@thread3));  eval(shift(@thread67));
936                 eval(shift(@thread4));  eval(shift(@thread67));
937                 eval(shift(@thread5));  eval(shift(@thread67));
938         }
939 $code.=<<___;
940         cbnz    $ctr,.Loop_lower_neon
941
942         add.32  @x[0],@x[0],@d[0]               // accumulate key block
943          ldp    @K[0],@K[1],[sp,#0]
944         add     @x[1],@x[1],@d[0],lsr#32
945          ldp    @K[2],@K[3],[sp,#32]
946         add.32  @x[2],@x[2],@d[1]
947          ldp    @K[4],@K[5],[sp,#64]
948         add     @x[3],@x[3],@d[1],lsr#32
949          add    $A0,$A0,@K[0]
950         add.32  @x[4],@x[4],@d[2]
951          add    $A1,$A1,@K[0]
952         add     @x[5],@x[5],@d[2],lsr#32
953          add    $A2,$A2,@K[0]
954         add.32  @x[6],@x[6],@d[3]
955          add    $A3,$A3,@K[0]
956         add     @x[7],@x[7],@d[3],lsr#32
957          add    $A4,$A4,@K[0]
958         add.32  @x[8],@x[8],@d[4]
959          add    $A5,$A5,@K[0]
960         add     @x[9],@x[9],@d[4],lsr#32
961          add    $C0,$C0,@K[2]
962         add.32  @x[10],@x[10],@d[5]
963          add    $C1,$C1,@K[2]
964         add     @x[11],@x[11],@d[5],lsr#32
965          add    $C2,$C2,@K[2]
966         add.32  @x[12],@x[12],@d[6]
967          add    $C3,$C3,@K[2]
968         add     @x[13],@x[13],@d[6],lsr#32
969          add    $C4,$C4,@K[2]
970         add.32  @x[14],@x[14],@d[7]
971          add    $C5,$C5,@K[2]
972         add     @x[15],@x[15],@d[7],lsr#32
973          add    $D4,$D4,$ONE                    // +4
974         add     @x[0],@x[0],@x[1],lsl#32        // pack
975          add    $D5,$D5,$ONE                    // +4
976         add     @x[2],@x[2],@x[3],lsl#32
977          add    $D0,$D0,@K[3]
978         ldp     @x[1],@x[3],[$inp,#0]           // load input
979          add    $D1,$D1,@K[4]
980         add     @x[4],@x[4],@x[5],lsl#32
981          add    $D2,$D2,@K[5]
982         add     @x[6],@x[6],@x[7],lsl#32
983          add    $D3,$D3,@K[6]
984         ldp     @x[5],@x[7],[$inp,#16]
985          add    $D4,$D4,@K[3]
986         add     @x[8],@x[8],@x[9],lsl#32
987          add    $D5,$D5,@K[4]
988         add     @x[10],@x[10],@x[11],lsl#32
989          add    $B0,$B0,@K[1]
990         ldp     @x[9],@x[11],[$inp,#32]
991          add    $B1,$B1,@K[1]
992         add     @x[12],@x[12],@x[13],lsl#32
993          add    $B2,$B2,@K[1]
994         add     @x[14],@x[14],@x[15],lsl#32
995          add    $B3,$B3,@K[1]
996         ldp     @x[13],@x[15],[$inp,#48]
997          add    $B4,$B4,@K[1]
998         add     $inp,$inp,#64
999          add    $B5,$B5,@K[1]
1000
1001 #ifdef  __ARMEB__
1002         rev     @x[0],@x[0]
1003         rev     @x[2],@x[2]
1004         rev     @x[4],@x[4]
1005         rev     @x[6],@x[6]
1006         rev     @x[8],@x[8]
1007         rev     @x[10],@x[10]
1008         rev     @x[12],@x[12]
1009         rev     @x[14],@x[14]
1010 #endif
1011         ld1.8   {$T0-$T3},[$inp],#64
1012         eor     @x[0],@x[0],@x[1]
1013         eor     @x[2],@x[2],@x[3]
1014         eor     @x[4],@x[4],@x[5]
1015         eor     @x[6],@x[6],@x[7]
1016         eor     @x[8],@x[8],@x[9]
1017          eor    $A0,$A0,$T0
1018         eor     @x[10],@x[10],@x[11]
1019          eor    $B0,$B0,$T1
1020         eor     @x[12],@x[12],@x[13]
1021          eor    $C0,$C0,$T2
1022         eor     @x[14],@x[14],@x[15]
1023          eor    $D0,$D0,$T3
1024          ld1.8  {$T0-$T3},[$inp],#64
1025
1026         stp     @x[0],@x[2],[$out,#0]           // store output
1027          add    @d[6],@d[6],#7                  // increment counter
1028         stp     @x[4],@x[6],[$out,#16]
1029         stp     @x[8],@x[10],[$out,#32]
1030         stp     @x[12],@x[14],[$out,#48]
1031         add     $out,$out,#64
1032         st1.8   {$A0-$D0},[$out],#64
1033
1034         ld1.8   {$A0-$D0},[$inp],#64
1035         eor     $A1,$A1,$T0
1036         eor     $B1,$B1,$T1
1037         eor     $C1,$C1,$T2
1038         eor     $D1,$D1,$T3
1039         st1.8   {$A1-$D1},[$out],#64
1040
1041         ld1.8   {$A1-$D1},[$inp],#64
1042         eor     $A2,$A2,$A0
1043          ldp    @K[0],@K[1],[sp,#0]
1044         eor     $B2,$B2,$B0
1045          ldp    @K[2],@K[3],[sp,#32]
1046         eor     $C2,$C2,$C0
1047         eor     $D2,$D2,$D0
1048         st1.8   {$A2-$D2},[$out],#64
1049
1050         ld1.8   {$A2-$D2},[$inp],#64
1051         eor     $A3,$A3,$A1
1052         eor     $B3,$B3,$B1
1053         eor     $C3,$C3,$C1
1054         eor     $D3,$D3,$D1
1055         st1.8   {$A3-$D3},[$out],#64
1056
1057         ld1.8   {$A3-$D3},[$inp],#64
1058         eor     $A4,$A4,$A2
1059         eor     $B4,$B4,$B2
1060         eor     $C4,$C4,$C2
1061         eor     $D4,$D4,$D2
1062         st1.8   {$A4-$D4},[$out],#64
1063
1064         shl     $A0,$ONE,#1                     // 4 -> 8
1065         eor     $A5,$A5,$A3
1066         eor     $B5,$B5,$B3
1067         eor     $C5,$C5,$C3
1068         eor     $D5,$D5,$D3
1069         st1.8   {$A5-$D5},[$out],#64
1070
1071         add     @K[3],@K[3],$A0                 // += 8
1072         add     @K[4],@K[4],$A0
1073         add     @K[5],@K[5],$A0
1074         add     @K[6],@K[6],$A0
1075
1076         b.hs    .Loop_outer_512_neon
1077
1078         adds    $len,$len,#512
1079         ushr    $A0,$ONE,#2                     // 4 -> 1
1080
1081         ldp     d8,d9,[sp,#128+0]               // meet ABI requirements
1082         ldp     d10,d11,[sp,#128+16]
1083         ldp     d12,d13,[sp,#128+32]
1084         ldp     d14,d15,[sp,#128+48]
1085
1086         stp     @K[0],$ONE,[sp,#0]              // wipe off-load area
1087         stp     @K[0],$ONE,[sp,#32]
1088         stp     @K[0],$ONE,[sp,#64]
1089
1090         b.eq    .Ldone_512_neon
1091
1092         cmp     $len,#192
1093         sub     @K[3],@K[3],$A0                 // -= 1
1094         sub     @K[4],@K[4],$A0
1095         sub     @K[5],@K[5],$A0
1096         add     sp,sp,#128
1097         b.hs    .Loop_outer_neon
1098
1099         eor     @K[1],@K[1],@K[1]
1100         eor     @K[2],@K[2],@K[2]
1101         eor     @K[3],@K[3],@K[3]
1102         eor     @K[4],@K[4],@K[4]
1103         eor     @K[5],@K[5],@K[5]
1104         eor     @K[6],@K[6],@K[6]
1105         b       .Loop_outer
1106
1107 .Ldone_512_neon:
1108         ldp     x19,x20,[x29,#16]
1109         add     sp,sp,#128+64
1110         ldp     x21,x22,[x29,#32]
1111         ldp     x23,x24,[x29,#48]
1112         ldp     x25,x26,[x29,#64]
1113         ldp     x27,x28,[x29,#80]
1114         ldp     x29,x30,[sp],#96
1115         ret
1116 .size   ChaCha20_512_neon,.-ChaCha20_512_neon
1117 ___
1118 }
1119 }}}
1120
1121 foreach (split("\n",$code)) {
1122         s/\`([^\`]*)\`/eval $1/geo;
1123
1124         (s/\b([a-z]+)\.32\b/$1/ and (s/x([0-9]+)/w$1/g or 1))   or
1125         (m/\b(eor|ext|mov)\b/ and (s/\.4s/\.16b/g or 1))        or
1126         (s/\b((?:ld|st)1)\.8\b/$1/ and (s/\.4s/\.16b/g or 1))   or
1127         (m/\b(ld|st)[rp]\b/ and (s/v([0-9]+)\.4s/q$1/g or 1))   or
1128         (s/\brev32\.16\b/rev32/ and (s/\.4s/\.8h/g or 1));
1129
1130         #s/\bq([0-9]+)#(lo|hi)/sprintf "d%d",2*$1+($2 eq "hi")/geo;
1131
1132         print $_,"\n";
1133 }
1134 close STDOUT;   # flush