2 # Copyright 2016-2020 The OpenSSL Project Authors. All Rights Reserved.
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
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 # ====================================================================
21 # Performance in cycles per byte out of large buffer.
23 # IALU/gcc-4.9 3xNEON+1xIALU 6xNEON+2xIALU
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 # Mongoose 8.00/+44% 3.64 3.25
31 # Kryo 8.17/+50% 4.83 4.65
33 # (*) it's expected that doubling interleave factor doesn't help
34 # all processors, only those with higher NEON latency and
35 # higher instruction issue rate;
36 # (**) expected improvement was actually higher;
41 $0 =~ m/(.*[\/\\])[^\/\\]+$/; $dir=$1;
42 ( $xlate="${dir}arm-xlate.pl" and -f $xlate ) or
43 ( $xlate="${dir}../../perlasm/arm-xlate.pl" and -f $xlate) or
44 die "can't locate arm-xlate.pl";
46 open OUT,"| \"$^X\" $xlate $flavour $output";
49 sub AUTOLOAD() # thunk [simplified] x86-style perlasm
50 { my $opcode = $AUTOLOAD; $opcode =~ s/.*:://; $opcode =~ s/_/\./;
52 $arg = "#$arg" if ($arg*1 eq $arg);
53 $code .= "\t$opcode\t".join(',',@_,$arg)."\n";
56 my ($out,$inp,$len,$key,$ctr) = map("x$_",(0..4));
58 my @x=map("x$_",(5..17,19..21));
59 my @d=map("x$_",(22..28,30));
62 my ($a0,$b0,$c0,$d0)=@_;
63 my ($a1,$b1,$c1,$d1)=map(($_&~3)+(($_+1)&3),($a0,$b0,$c0,$d0));
64 my ($a2,$b2,$c2,$d2)=map(($_&~3)+(($_+1)&3),($a1,$b1,$c1,$d1));
65 my ($a3,$b3,$c3,$d3)=map(($_&~3)+(($_+1)&3),($a2,$b2,$c2,$d2));
68 "&add_32 (@x[$a0],@x[$a0],@x[$b0])",
69 "&add_32 (@x[$a1],@x[$a1],@x[$b1])",
70 "&add_32 (@x[$a2],@x[$a2],@x[$b2])",
71 "&add_32 (@x[$a3],@x[$a3],@x[$b3])",
72 "&eor_32 (@x[$d0],@x[$d0],@x[$a0])",
73 "&eor_32 (@x[$d1],@x[$d1],@x[$a1])",
74 "&eor_32 (@x[$d2],@x[$d2],@x[$a2])",
75 "&eor_32 (@x[$d3],@x[$d3],@x[$a3])",
76 "&ror_32 (@x[$d0],@x[$d0],16)",
77 "&ror_32 (@x[$d1],@x[$d1],16)",
78 "&ror_32 (@x[$d2],@x[$d2],16)",
79 "&ror_32 (@x[$d3],@x[$d3],16)",
81 "&add_32 (@x[$c0],@x[$c0],@x[$d0])",
82 "&add_32 (@x[$c1],@x[$c1],@x[$d1])",
83 "&add_32 (@x[$c2],@x[$c2],@x[$d2])",
84 "&add_32 (@x[$c3],@x[$c3],@x[$d3])",
85 "&eor_32 (@x[$b0],@x[$b0],@x[$c0])",
86 "&eor_32 (@x[$b1],@x[$b1],@x[$c1])",
87 "&eor_32 (@x[$b2],@x[$b2],@x[$c2])",
88 "&eor_32 (@x[$b3],@x[$b3],@x[$c3])",
89 "&ror_32 (@x[$b0],@x[$b0],20)",
90 "&ror_32 (@x[$b1],@x[$b1],20)",
91 "&ror_32 (@x[$b2],@x[$b2],20)",
92 "&ror_32 (@x[$b3],@x[$b3],20)",
94 "&add_32 (@x[$a0],@x[$a0],@x[$b0])",
95 "&add_32 (@x[$a1],@x[$a1],@x[$b1])",
96 "&add_32 (@x[$a2],@x[$a2],@x[$b2])",
97 "&add_32 (@x[$a3],@x[$a3],@x[$b3])",
98 "&eor_32 (@x[$d0],@x[$d0],@x[$a0])",
99 "&eor_32 (@x[$d1],@x[$d1],@x[$a1])",
100 "&eor_32 (@x[$d2],@x[$d2],@x[$a2])",
101 "&eor_32 (@x[$d3],@x[$d3],@x[$a3])",
102 "&ror_32 (@x[$d0],@x[$d0],24)",
103 "&ror_32 (@x[$d1],@x[$d1],24)",
104 "&ror_32 (@x[$d2],@x[$d2],24)",
105 "&ror_32 (@x[$d3],@x[$d3],24)",
107 "&add_32 (@x[$c0],@x[$c0],@x[$d0])",
108 "&add_32 (@x[$c1],@x[$c1],@x[$d1])",
109 "&add_32 (@x[$c2],@x[$c2],@x[$d2])",
110 "&add_32 (@x[$c3],@x[$c3],@x[$d3])",
111 "&eor_32 (@x[$b0],@x[$b0],@x[$c0])",
112 "&eor_32 (@x[$b1],@x[$b1],@x[$c1])",
113 "&eor_32 (@x[$b2],@x[$b2],@x[$c2])",
114 "&eor_32 (@x[$b3],@x[$b3],@x[$c3])",
115 "&ror_32 (@x[$b0],@x[$b0],25)",
116 "&ror_32 (@x[$b1],@x[$b1],25)",
117 "&ror_32 (@x[$b2],@x[$b2],25)",
118 "&ror_32 (@x[$b3],@x[$b3],25)"
123 #include "arm_arch.h"
127 .extern OPENSSL_armcap_P
128 .hidden OPENSSL_armcap_P
132 .quad 0x3320646e61707865,0x6b20657479622d32 // endian-neutral
137 .long OPENSSL_armcap_P-.
139 .quad OPENSSL_armcap_P-.
141 .asciz "ChaCha20 for ARMv8, CRYPTOGAMS by <appro\@openssl.org>"
143 .globl ChaCha20_ctr32
144 .type ChaCha20_ctr32,%function
148 adr @x[0],.LOPENSSL_armcap_P
156 ldr w17,[@x[1],@x[0]]
161 .inst 0xd503233f // paciasp
162 stp x29,x30,[sp,#-96]!
173 ldp @d[0],@d[1],[@x[0]] // load sigma
174 ldp @d[2],@d[3],[$key] // load key
175 ldp @d[4],@d[5],[$key,#16]
176 ldp @d[6],@d[7],[$ctr] // load counter
187 mov.32 @x[0],@d[0] // unpack key block
209 foreach (&ROUND(0, 4, 8,12)) { eval; }
210 foreach (&ROUND(0, 5,10,15)) { eval; }
214 add.32 @x[0],@x[0],@d[0] // accumulate key block
215 add @x[1],@x[1],@d[0],lsr#32
216 add.32 @x[2],@x[2],@d[1]
217 add @x[3],@x[3],@d[1],lsr#32
218 add.32 @x[4],@x[4],@d[2]
219 add @x[5],@x[5],@d[2],lsr#32
220 add.32 @x[6],@x[6],@d[3]
221 add @x[7],@x[7],@d[3],lsr#32
222 add.32 @x[8],@x[8],@d[4]
223 add @x[9],@x[9],@d[4],lsr#32
224 add.32 @x[10],@x[10],@d[5]
225 add @x[11],@x[11],@d[5],lsr#32
226 add.32 @x[12],@x[12],@d[6]
227 add @x[13],@x[13],@d[6],lsr#32
228 add.32 @x[14],@x[14],@d[7]
229 add @x[15],@x[15],@d[7],lsr#32
233 add @x[0],@x[0],@x[1],lsl#32 // pack
234 add @x[2],@x[2],@x[3],lsl#32
235 ldp @x[1],@x[3],[$inp,#0] // load input
236 add @x[4],@x[4],@x[5],lsl#32
237 add @x[6],@x[6],@x[7],lsl#32
238 ldp @x[5],@x[7],[$inp,#16]
239 add @x[8],@x[8],@x[9],lsl#32
240 add @x[10],@x[10],@x[11],lsl#32
241 ldp @x[9],@x[11],[$inp,#32]
242 add @x[12],@x[12],@x[13],lsl#32
243 add @x[14],@x[14],@x[15],lsl#32
244 ldp @x[13],@x[15],[$inp,#48]
256 eor @x[0],@x[0],@x[1]
257 eor @x[2],@x[2],@x[3]
258 eor @x[4],@x[4],@x[5]
259 eor @x[6],@x[6],@x[7]
260 eor @x[8],@x[8],@x[9]
261 eor @x[10],@x[10],@x[11]
262 eor @x[12],@x[12],@x[13]
263 eor @x[14],@x[14],@x[15]
265 stp @x[0],@x[2],[$out,#0] // store output
266 add @d[6],@d[6],#1 // increment counter
267 stp @x[4],@x[6],[$out,#16]
268 stp @x[8],@x[10],[$out,#32]
269 stp @x[12],@x[14],[$out,#48]
274 ldp x19,x20,[x29,#16]
276 ldp x21,x22,[x29,#32]
277 ldp x23,x24,[x29,#48]
278 ldp x25,x26,[x29,#64]
279 ldp x27,x28,[x29,#80]
281 .inst 0xd50323bf // autiasp
295 add @x[0],@x[0],@x[1],lsl#32 // pack
296 add @x[2],@x[2],@x[3],lsl#32
297 add @x[4],@x[4],@x[5],lsl#32
298 add @x[6],@x[6],@x[7],lsl#32
299 add @x[8],@x[8],@x[9],lsl#32
300 add @x[10],@x[10],@x[11],lsl#32
301 add @x[12],@x[12],@x[13],lsl#32
302 add @x[14],@x[14],@x[15],lsl#32
313 stp @x[0],@x[2],[sp,#0]
314 stp @x[4],@x[6],[sp,#16]
315 stp @x[8],@x[10],[sp,#32]
316 stp @x[12],@x[14],[sp,#48]
331 ldp x19,x20,[x29,#16]
333 ldp x21,x22,[x29,#32]
334 ldp x23,x24,[x29,#48]
335 ldp x25,x26,[x29,#64]
336 ldp x27,x28,[x29,#80]
338 .inst 0xd50323bf // autiasp
340 .size ChaCha20_ctr32,.-ChaCha20_ctr32
344 my ($A0,$B0,$C0,$D0,$A1,$B1,$C1,$D1,$A2,$B2,$C2,$D2,$T0,$T1,$T2,$T3) =
345 map("v$_.4s",(0..7,16..23));
346 my (@K)=map("v$_.4s",(24..30));
351 my ($a,$b,$c,$d,$t)=@_;
354 "&add ('$a','$a','$b')",
355 "&eor ('$d','$d','$a')",
356 "&rev32_16 ('$d','$d')", # vrot ($d,16)
358 "&add ('$c','$c','$d')",
359 "&eor ('$t','$b','$c')",
360 "&ushr ('$b','$t',20)",
361 "&sli ('$b','$t',12)",
363 "&add ('$a','$a','$b')",
364 "&eor ('$t','$d','$a')",
365 "&ushr ('$d','$t',24)",
366 "&sli ('$d','$t',8)",
368 "&add ('$c','$c','$d')",
369 "&eor ('$t','$b','$c')",
370 "&ushr ('$b','$t',25)",
371 "&sli ('$b','$t',7)",
373 "&ext ('$c','$c','$c',8)",
374 "&ext ('$d','$d','$d',$odd?4:12)",
375 "&ext ('$b','$b','$b',$odd?12:4)"
381 .type ChaCha20_neon,%function
384 .inst 0xd503233f // paciasp
385 stp x29,x30,[sp,#-96]!
395 b.hs .L512_or_more_neon
399 ldp @d[0],@d[1],[@x[0]] // load sigma
400 ld1 {@K[0]},[@x[0]],#16
401 ldp @d[2],@d[3],[$key] // load key
402 ldp @d[4],@d[5],[$key,#16]
403 ld1 {@K[1],@K[2]},[$key]
404 ldp @d[6],@d[7],[$ctr] // load counter
416 add @K[3],@K[3],$ONE // += 1
419 shl $ONE,$ONE,#2 // 1 -> 4
422 mov.32 @x[0],@d[0] // unpack key block
456 my @thread0=&NEONROUND($A0,$B0,$C0,$D0,$T0,0);
457 my @thread1=&NEONROUND($A1,$B1,$C1,$D1,$T1,0);
458 my @thread2=&NEONROUND($A2,$B2,$C2,$D2,$T2,0);
459 my @thread3=&ROUND(0,4,8,12);
462 eval; eval(shift(@thread3));
463 eval(shift(@thread1)); eval(shift(@thread3));
464 eval(shift(@thread2)); eval(shift(@thread3));
467 @thread0=&NEONROUND($A0,$B0,$C0,$D0,$T0,1);
468 @thread1=&NEONROUND($A1,$B1,$C1,$D1,$T1,1);
469 @thread2=&NEONROUND($A2,$B2,$C2,$D2,$T2,1);
470 @thread3=&ROUND(0,5,10,15);
473 eval; eval(shift(@thread3));
474 eval(shift(@thread1)); eval(shift(@thread3));
475 eval(shift(@thread2)); eval(shift(@thread3));
480 add.32 @x[0],@x[0],@d[0] // accumulate key block
482 add @x[1],@x[1],@d[0],lsr#32
484 add.32 @x[2],@x[2],@d[1]
486 add @x[3],@x[3],@d[1],lsr#32
488 add.32 @x[4],@x[4],@d[2]
490 add @x[5],@x[5],@d[2],lsr#32
492 add.32 @x[6],@x[6],@d[3]
494 add @x[7],@x[7],@d[3],lsr#32
495 add.32 @x[8],@x[8],@d[4]
497 add @x[9],@x[9],@d[4],lsr#32
498 add.32 @x[10],@x[10],@d[5]
500 add @x[11],@x[11],@d[5],lsr#32
501 add.32 @x[12],@x[12],@d[6]
503 add @x[13],@x[13],@d[6],lsr#32
504 add.32 @x[14],@x[14],@d[7]
506 add @x[15],@x[15],@d[7],lsr#32
511 add @x[0],@x[0],@x[1],lsl#32 // pack
512 add @x[2],@x[2],@x[3],lsl#32
513 ldp @x[1],@x[3],[$inp,#0] // load input
514 add @x[4],@x[4],@x[5],lsl#32
515 add @x[6],@x[6],@x[7],lsl#32
516 ldp @x[5],@x[7],[$inp,#16]
517 add @x[8],@x[8],@x[9],lsl#32
518 add @x[10],@x[10],@x[11],lsl#32
519 ldp @x[9],@x[11],[$inp,#32]
520 add @x[12],@x[12],@x[13],lsl#32
521 add @x[14],@x[14],@x[15],lsl#32
522 ldp @x[13],@x[15],[$inp,#48]
534 ld1.8 {$T0-$T3},[$inp],#64
535 eor @x[0],@x[0],@x[1]
536 eor @x[2],@x[2],@x[3]
537 eor @x[4],@x[4],@x[5]
538 eor @x[6],@x[6],@x[7]
539 eor @x[8],@x[8],@x[9]
541 eor @x[10],@x[10],@x[11]
543 eor @x[12],@x[12],@x[13]
545 eor @x[14],@x[14],@x[15]
547 ld1.8 {$T0-$T3},[$inp],#64
549 stp @x[0],@x[2],[$out,#0] // store output
550 add @d[6],@d[6],#4 // increment counter
551 stp @x[4],@x[6],[$out,#16]
552 add @K[3],@K[3],$ONE // += 4
553 stp @x[8],@x[10],[$out,#32]
555 stp @x[12],@x[14],[$out,#48]
559 st1.8 {$A0-$D0},[$out],#64
560 ld1.8 {$A0-$D0},[$inp],#64
566 st1.8 {$A1-$D1},[$out],#64
572 st1.8 {$A2-$D2},[$out],#64
574 b.hi .Loop_outer_neon
576 ldp x19,x20,[x29,#16]
578 ldp x21,x22,[x29,#32]
579 ldp x23,x24,[x29,#48]
580 ldp x25,x26,[x29,#64]
581 ldp x27,x28,[x29,#80]
583 .inst 0xd50323bf // autiasp
591 add @x[0],@x[0],@x[1],lsl#32 // pack
592 add @x[2],@x[2],@x[3],lsl#32
593 ldp @x[1],@x[3],[$inp,#0] // load input
594 add @x[4],@x[4],@x[5],lsl#32
595 add @x[6],@x[6],@x[7],lsl#32
596 ldp @x[5],@x[7],[$inp,#16]
597 add @x[8],@x[8],@x[9],lsl#32
598 add @x[10],@x[10],@x[11],lsl#32
599 ldp @x[9],@x[11],[$inp,#32]
600 add @x[12],@x[12],@x[13],lsl#32
601 add @x[14],@x[14],@x[15],lsl#32
602 ldp @x[13],@x[15],[$inp,#48]
614 eor @x[0],@x[0],@x[1]
615 eor @x[2],@x[2],@x[3]
616 eor @x[4],@x[4],@x[5]
617 eor @x[6],@x[6],@x[7]
618 eor @x[8],@x[8],@x[9]
619 eor @x[10],@x[10],@x[11]
620 eor @x[12],@x[12],@x[13]
621 eor @x[14],@x[14],@x[15]
623 stp @x[0],@x[2],[$out,#0] // store output
624 add @d[6],@d[6],#4 // increment counter
625 stp @x[4],@x[6],[$out,#16]
626 stp @x[8],@x[10],[$out,#32]
627 stp @x[12],@x[14],[$out,#48]
634 ld1.8 {$T0-$T3},[$inp],#64
639 st1.8 {$A0-$D0},[$out],#64
645 ld1.8 {$T0-$T3},[$inp],#64
650 st1.8 {$A1-$D1},[$out],#64
678 cbnz $len,.Loop_tail_neon
686 ldp x19,x20,[x29,#16]
688 ldp x21,x22,[x29,#32]
689 ldp x23,x24,[x29,#48]
690 ldp x25,x26,[x29,#64]
691 ldp x27,x28,[x29,#80]
693 .inst 0xd50323bf // autiasp
695 .size ChaCha20_neon,.-ChaCha20_neon
698 my ($T0,$T1,$T2,$T3,$T4,$T5)=@K;
699 my ($A0,$B0,$C0,$D0,$A1,$B1,$C1,$D1,$A2,$B2,$C2,$D2,
700 $A3,$B3,$C3,$D3,$A4,$B4,$C4,$D4,$A5,$B5,$C5,$D5) = map("v$_.4s",(0..23));
703 .type ChaCha20_512_neon,%function
706 .inst 0xd503233f // paciasp
707 stp x29,x30,[sp,#-96]!
720 ldp @d[0],@d[1],[@x[0]] // load sigma
721 ld1 {@K[0]},[@x[0]],#16
722 ldp @d[2],@d[3],[$key] // load key
723 ldp @d[4],@d[5],[$key,#16]
724 ld1 {@K[1],@K[2]},[$key]
725 ldp @d[6],@d[7],[$ctr] // load counter
737 add @K[3],@K[3],$ONE // += 1
738 stp @K[0],@K[1],[sp,#0] // off-load key block, invariant part
739 add @K[3],@K[3],$ONE // not typo
744 shl $ONE,$ONE,#2 // 1 -> 4
746 stp d8,d9,[sp,#128+0] // meet ABI requirements
747 stp d10,d11,[sp,#128+16]
748 stp d12,d13,[sp,#128+32]
749 stp d14,d15,[sp,#128+48]
751 sub $len,$len,#512 // not typo
753 .Loop_outer_512_neon:
761 mov.32 @x[0],@d[0] // unpack key block
784 add $D4,$D0,$ONE // +4
786 add $D5,$D1,$ONE // +4
793 stp @K[3],@K[4],[sp,#48] // off-load key block, variable part
802 my @thread0=&NEONROUND($A0,$B0,$C0,$D0,$T0,0);
803 my @thread1=&NEONROUND($A1,$B1,$C1,$D1,$T1,0);
804 my @thread2=&NEONROUND($A2,$B2,$C2,$D2,$T2,0);
805 my @thread3=&NEONROUND($A3,$B3,$C3,$D3,$T3,0);
806 my @thread4=&NEONROUND($A4,$B4,$C4,$D4,$T4,0);
807 my @thread5=&NEONROUND($A5,$B5,$C5,$D5,$T5,0);
808 my @thread67=(&ROUND(0,4,8,12),&ROUND(0,5,10,15));
809 my $diff = ($#thread0+1)*6 - $#thread67 - 1;
813 eval; eval(shift(@thread67));
814 eval(shift(@thread1)); eval(shift(@thread67));
815 eval(shift(@thread2)); eval(shift(@thread67));
816 eval(shift(@thread3)); eval(shift(@thread67));
817 eval(shift(@thread4)); eval(shift(@thread67));
818 eval(shift(@thread5)); eval(shift(@thread67));
821 @thread0=&NEONROUND($A0,$B0,$C0,$D0,$T0,1);
822 @thread1=&NEONROUND($A1,$B1,$C1,$D1,$T1,1);
823 @thread2=&NEONROUND($A2,$B2,$C2,$D2,$T2,1);
824 @thread3=&NEONROUND($A3,$B3,$C3,$D3,$T3,1);
825 @thread4=&NEONROUND($A4,$B4,$C4,$D4,$T4,1);
826 @thread5=&NEONROUND($A5,$B5,$C5,$D5,$T5,1);
827 @thread67=(&ROUND(0,4,8,12),&ROUND(0,5,10,15));
830 eval; eval(shift(@thread67));
831 eval(shift(@thread1)); eval(shift(@thread67));
832 eval(shift(@thread2)); eval(shift(@thread67));
833 eval(shift(@thread3)); eval(shift(@thread67));
834 eval(shift(@thread4)); eval(shift(@thread67));
835 eval(shift(@thread5)); eval(shift(@thread67));
838 cbnz $ctr,.Loop_upper_neon
840 add.32 @x[0],@x[0],@d[0] // accumulate key block
841 add @x[1],@x[1],@d[0],lsr#32
842 add.32 @x[2],@x[2],@d[1]
843 add @x[3],@x[3],@d[1],lsr#32
844 add.32 @x[4],@x[4],@d[2]
845 add @x[5],@x[5],@d[2],lsr#32
846 add.32 @x[6],@x[6],@d[3]
847 add @x[7],@x[7],@d[3],lsr#32
848 add.32 @x[8],@x[8],@d[4]
849 add @x[9],@x[9],@d[4],lsr#32
850 add.32 @x[10],@x[10],@d[5]
851 add @x[11],@x[11],@d[5],lsr#32
852 add.32 @x[12],@x[12],@d[6]
853 add @x[13],@x[13],@d[6],lsr#32
854 add.32 @x[14],@x[14],@d[7]
855 add @x[15],@x[15],@d[7],lsr#32
857 add @x[0],@x[0],@x[1],lsl#32 // pack
858 add @x[2],@x[2],@x[3],lsl#32
859 ldp @x[1],@x[3],[$inp,#0] // load input
860 add @x[4],@x[4],@x[5],lsl#32
861 add @x[6],@x[6],@x[7],lsl#32
862 ldp @x[5],@x[7],[$inp,#16]
863 add @x[8],@x[8],@x[9],lsl#32
864 add @x[10],@x[10],@x[11],lsl#32
865 ldp @x[9],@x[11],[$inp,#32]
866 add @x[12],@x[12],@x[13],lsl#32
867 add @x[14],@x[14],@x[15],lsl#32
868 ldp @x[13],@x[15],[$inp,#48]
880 eor @x[0],@x[0],@x[1]
881 eor @x[2],@x[2],@x[3]
882 eor @x[4],@x[4],@x[5]
883 eor @x[6],@x[6],@x[7]
884 eor @x[8],@x[8],@x[9]
885 eor @x[10],@x[10],@x[11]
886 eor @x[12],@x[12],@x[13]
887 eor @x[14],@x[14],@x[15]
889 stp @x[0],@x[2],[$out,#0] // store output
890 add @d[6],@d[6],#1 // increment counter
891 mov.32 @x[0],@d[0] // unpack key block
893 stp @x[4],@x[6],[$out,#16]
896 stp @x[8],@x[10],[$out,#32]
899 stp @x[12],@x[14],[$out,#48]
916 @thread0=&NEONROUND($A0,$B0,$C0,$D0,$T0,0);
917 @thread1=&NEONROUND($A1,$B1,$C1,$D1,$T1,0);
918 @thread2=&NEONROUND($A2,$B2,$C2,$D2,$T2,0);
919 @thread3=&NEONROUND($A3,$B3,$C3,$D3,$T3,0);
920 @thread4=&NEONROUND($A4,$B4,$C4,$D4,$T4,0);
921 @thread5=&NEONROUND($A5,$B5,$C5,$D5,$T5,0);
922 @thread67=(&ROUND(0,4,8,12),&ROUND(0,5,10,15));
925 eval; eval(shift(@thread67));
926 eval(shift(@thread1)); eval(shift(@thread67));
927 eval(shift(@thread2)); eval(shift(@thread67));
928 eval(shift(@thread3)); eval(shift(@thread67));
929 eval(shift(@thread4)); eval(shift(@thread67));
930 eval(shift(@thread5)); eval(shift(@thread67));
933 @thread0=&NEONROUND($A0,$B0,$C0,$D0,$T0,1);
934 @thread1=&NEONROUND($A1,$B1,$C1,$D1,$T1,1);
935 @thread2=&NEONROUND($A2,$B2,$C2,$D2,$T2,1);
936 @thread3=&NEONROUND($A3,$B3,$C3,$D3,$T3,1);
937 @thread4=&NEONROUND($A4,$B4,$C4,$D4,$T4,1);
938 @thread5=&NEONROUND($A5,$B5,$C5,$D5,$T5,1);
939 @thread67=(&ROUND(0,4,8,12),&ROUND(0,5,10,15));
942 eval; eval(shift(@thread67));
943 eval(shift(@thread1)); eval(shift(@thread67));
944 eval(shift(@thread2)); eval(shift(@thread67));
945 eval(shift(@thread3)); eval(shift(@thread67));
946 eval(shift(@thread4)); eval(shift(@thread67));
947 eval(shift(@thread5)); eval(shift(@thread67));
950 cbnz $ctr,.Loop_lower_neon
952 add.32 @x[0],@x[0],@d[0] // accumulate key block
953 ldp @K[0],@K[1],[sp,#0]
954 add @x[1],@x[1],@d[0],lsr#32
955 ldp @K[2],@K[3],[sp,#32]
956 add.32 @x[2],@x[2],@d[1]
957 ldp @K[4],@K[5],[sp,#64]
958 add @x[3],@x[3],@d[1],lsr#32
960 add.32 @x[4],@x[4],@d[2]
962 add @x[5],@x[5],@d[2],lsr#32
964 add.32 @x[6],@x[6],@d[3]
966 add @x[7],@x[7],@d[3],lsr#32
968 add.32 @x[8],@x[8],@d[4]
970 add @x[9],@x[9],@d[4],lsr#32
972 add.32 @x[10],@x[10],@d[5]
974 add @x[11],@x[11],@d[5],lsr#32
976 add.32 @x[12],@x[12],@d[6]
978 add @x[13],@x[13],@d[6],lsr#32
980 add.32 @x[14],@x[14],@d[7]
982 add @x[15],@x[15],@d[7],lsr#32
983 add $D4,$D4,$ONE // +4
984 add @x[0],@x[0],@x[1],lsl#32 // pack
985 add $D5,$D5,$ONE // +4
986 add @x[2],@x[2],@x[3],lsl#32
988 ldp @x[1],@x[3],[$inp,#0] // load input
990 add @x[4],@x[4],@x[5],lsl#32
992 add @x[6],@x[6],@x[7],lsl#32
994 ldp @x[5],@x[7],[$inp,#16]
996 add @x[8],@x[8],@x[9],lsl#32
998 add @x[10],@x[10],@x[11],lsl#32
1000 ldp @x[9],@x[11],[$inp,#32]
1002 add @x[12],@x[12],@x[13],lsl#32
1004 add @x[14],@x[14],@x[15],lsl#32
1006 ldp @x[13],@x[15],[$inp,#48]
1021 ld1.8 {$T0-$T3},[$inp],#64
1022 eor @x[0],@x[0],@x[1]
1023 eor @x[2],@x[2],@x[3]
1024 eor @x[4],@x[4],@x[5]
1025 eor @x[6],@x[6],@x[7]
1026 eor @x[8],@x[8],@x[9]
1028 eor @x[10],@x[10],@x[11]
1030 eor @x[12],@x[12],@x[13]
1032 eor @x[14],@x[14],@x[15]
1034 ld1.8 {$T0-$T3},[$inp],#64
1036 stp @x[0],@x[2],[$out,#0] // store output
1037 add @d[6],@d[6],#7 // increment counter
1038 stp @x[4],@x[6],[$out,#16]
1039 stp @x[8],@x[10],[$out,#32]
1040 stp @x[12],@x[14],[$out,#48]
1042 st1.8 {$A0-$D0},[$out],#64
1044 ld1.8 {$A0-$D0},[$inp],#64
1049 st1.8 {$A1-$D1},[$out],#64
1051 ld1.8 {$A1-$D1},[$inp],#64
1053 ldp @K[0],@K[1],[sp,#0]
1055 ldp @K[2],@K[3],[sp,#32]
1058 st1.8 {$A2-$D2},[$out],#64
1060 ld1.8 {$A2-$D2},[$inp],#64
1065 st1.8 {$A3-$D3},[$out],#64
1067 ld1.8 {$A3-$D3},[$inp],#64
1072 st1.8 {$A4-$D4},[$out],#64
1074 shl $A0,$ONE,#1 // 4 -> 8
1079 st1.8 {$A5-$D5},[$out],#64
1081 add @K[3],@K[3],$A0 // += 8
1086 b.hs .Loop_outer_512_neon
1089 ushr $A0,$ONE,#2 // 4 -> 1
1091 ldp d8,d9,[sp,#128+0] // meet ABI requirements
1092 ldp d10,d11,[sp,#128+16]
1093 ldp d12,d13,[sp,#128+32]
1094 ldp d14,d15,[sp,#128+48]
1096 stp @K[0],$ONE,[sp,#0] // wipe off-load area
1097 stp @K[0],$ONE,[sp,#32]
1098 stp @K[0],$ONE,[sp,#64]
1100 b.eq .Ldone_512_neon
1103 sub @K[3],@K[3],$A0 // -= 1
1107 b.hs .Loop_outer_neon
1109 eor @K[1],@K[1],@K[1]
1110 eor @K[2],@K[2],@K[2]
1111 eor @K[3],@K[3],@K[3]
1112 eor @K[4],@K[4],@K[4]
1113 eor @K[5],@K[5],@K[5]
1114 eor @K[6],@K[6],@K[6]
1118 ldp x19,x20,[x29,#16]
1120 ldp x21,x22,[x29,#32]
1121 ldp x23,x24,[x29,#48]
1122 ldp x25,x26,[x29,#64]
1123 ldp x27,x28,[x29,#80]
1124 ldp x29,x30,[sp],#96
1125 .inst 0xd50323bf // autiasp
1127 .size ChaCha20_512_neon,.-ChaCha20_512_neon
1132 foreach (split("\n",$code)) {
1133 s/\`([^\`]*)\`/eval $1/geo;
1135 (s/\b([a-z]+)\.32\b/$1/ and (s/x([0-9]+)/w$1/g or 1)) or
1136 (m/\b(eor|ext|mov)\b/ and (s/\.4s/\.16b/g or 1)) or
1137 (s/\b((?:ld|st)1)\.8\b/$1/ and (s/\.4s/\.16b/g or 1)) or
1138 (m/\b(ld|st)[rp]\b/ and (s/v([0-9]+)\.4s/q$1/g or 1)) or
1139 (s/\brev32\.16\b/rev32/ and (s/\.4s/\.8h/g or 1));
1141 #s/\bq([0-9]+)#(lo|hi)/sprintf "d%d",2*$1+($2 eq "hi")/geo;
1145 close STDOUT or die "error closing STDOUT: $!"; # flush