2 # Copyright 2016 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
131 .quad 0x3320646e61707865,0x6b20657479622d32 // endian-neutral
136 .long OPENSSL_armcap_P-.
138 .quad OPENSSL_armcap_P-.
140 .asciz "ChaCha20 for ARMv8, CRYPTOGAMS by <appro\@openssl.org>"
142 .globl ChaCha20_ctr32
143 .type ChaCha20_ctr32,%function
147 adr @x[0],.LOPENSSL_armcap_P
155 ldr w17,[@x[1],@x[0]]
160 stp x29,x30,[sp,#-96]!
171 ldp @d[0],@d[1],[@x[0]] // load sigma
172 ldp @d[2],@d[3],[$key] // load key
173 ldp @d[4],@d[5],[$key,#16]
174 ldp @d[6],@d[7],[$ctr] // load counter
185 mov.32 @x[0],@d[0] // unpack key block
207 foreach (&ROUND(0, 4, 8,12)) { eval; }
208 foreach (&ROUND(0, 5,10,15)) { eval; }
212 add.32 @x[0],@x[0],@d[0] // accumulate key block
213 add @x[1],@x[1],@d[0],lsr#32
214 add.32 @x[2],@x[2],@d[1]
215 add @x[3],@x[3],@d[1],lsr#32
216 add.32 @x[4],@x[4],@d[2]
217 add @x[5],@x[5],@d[2],lsr#32
218 add.32 @x[6],@x[6],@d[3]
219 add @x[7],@x[7],@d[3],lsr#32
220 add.32 @x[8],@x[8],@d[4]
221 add @x[9],@x[9],@d[4],lsr#32
222 add.32 @x[10],@x[10],@d[5]
223 add @x[11],@x[11],@d[5],lsr#32
224 add.32 @x[12],@x[12],@d[6]
225 add @x[13],@x[13],@d[6],lsr#32
226 add.32 @x[14],@x[14],@d[7]
227 add @x[15],@x[15],@d[7],lsr#32
231 add @x[0],@x[0],@x[1],lsl#32 // pack
232 add @x[2],@x[2],@x[3],lsl#32
233 ldp @x[1],@x[3],[$inp,#0] // load input
234 add @x[4],@x[4],@x[5],lsl#32
235 add @x[6],@x[6],@x[7],lsl#32
236 ldp @x[5],@x[7],[$inp,#16]
237 add @x[8],@x[8],@x[9],lsl#32
238 add @x[10],@x[10],@x[11],lsl#32
239 ldp @x[9],@x[11],[$inp,#32]
240 add @x[12],@x[12],@x[13],lsl#32
241 add @x[14],@x[14],@x[15],lsl#32
242 ldp @x[13],@x[15],[$inp,#48]
254 eor @x[0],@x[0],@x[1]
255 eor @x[2],@x[2],@x[3]
256 eor @x[4],@x[4],@x[5]
257 eor @x[6],@x[6],@x[7]
258 eor @x[8],@x[8],@x[9]
259 eor @x[10],@x[10],@x[11]
260 eor @x[12],@x[12],@x[13]
261 eor @x[14],@x[14],@x[15]
263 stp @x[0],@x[2],[$out,#0] // store output
264 add @d[6],@d[6],#1 // increment counter
265 stp @x[4],@x[6],[$out,#16]
266 stp @x[8],@x[10],[$out,#32]
267 stp @x[12],@x[14],[$out,#48]
272 ldp x19,x20,[x29,#16]
274 ldp x21,x22,[x29,#32]
275 ldp x23,x24,[x29,#48]
276 ldp x25,x26,[x29,#64]
277 ldp x27,x28,[x29,#80]
292 add @x[0],@x[0],@x[1],lsl#32 // pack
293 add @x[2],@x[2],@x[3],lsl#32
294 add @x[4],@x[4],@x[5],lsl#32
295 add @x[6],@x[6],@x[7],lsl#32
296 add @x[8],@x[8],@x[9],lsl#32
297 add @x[10],@x[10],@x[11],lsl#32
298 add @x[12],@x[12],@x[13],lsl#32
299 add @x[14],@x[14],@x[15],lsl#32
310 stp @x[0],@x[2],[sp,#0]
311 stp @x[4],@x[6],[sp,#16]
312 stp @x[8],@x[10],[sp,#32]
313 stp @x[12],@x[14],[sp,#48]
328 ldp x19,x20,[x29,#16]
330 ldp x21,x22,[x29,#32]
331 ldp x23,x24,[x29,#48]
332 ldp x25,x26,[x29,#64]
333 ldp x27,x28,[x29,#80]
336 .size ChaCha20_ctr32,.-ChaCha20_ctr32
340 my ($A0,$B0,$C0,$D0,$A1,$B1,$C1,$D1,$A2,$B2,$C2,$D2,$T0,$T1,$T2,$T3) =
341 map("v$_.4s",(0..7,16..23));
342 my (@K)=map("v$_.4s",(24..30));
347 my ($a,$b,$c,$d,$t)=@_;
350 "&add ('$a','$a','$b')",
351 "&eor ('$d','$d','$a')",
352 "&rev32_16 ('$d','$d')", # vrot ($d,16)
354 "&add ('$c','$c','$d')",
355 "&eor ('$t','$b','$c')",
356 "&ushr ('$b','$t',20)",
357 "&sli ('$b','$t',12)",
359 "&add ('$a','$a','$b')",
360 "&eor ('$t','$d','$a')",
361 "&ushr ('$d','$t',24)",
362 "&sli ('$d','$t',8)",
364 "&add ('$c','$c','$d')",
365 "&eor ('$t','$b','$c')",
366 "&ushr ('$b','$t',25)",
367 "&sli ('$b','$t',7)",
369 "&ext ('$c','$c','$c',8)",
370 "&ext ('$d','$d','$d',$odd?4:12)",
371 "&ext ('$b','$b','$b',$odd?12:4)"
377 .type ChaCha20_neon,%function
380 stp x29,x30,[sp,#-96]!
390 b.hs .L512_or_more_neon
394 ldp @d[0],@d[1],[@x[0]] // load sigma
395 ld1 {@K[0]},[@x[0]],#16
396 ldp @d[2],@d[3],[$key] // load key
397 ldp @d[4],@d[5],[$key,#16]
398 ld1 {@K[1],@K[2]},[$key]
399 ldp @d[6],@d[7],[$ctr] // load counter
411 add @K[3],@K[3],$ONE // += 1
414 shl $ONE,$ONE,#2 // 1 -> 4
417 mov.32 @x[0],@d[0] // unpack key block
451 my @thread0=&NEONROUND($A0,$B0,$C0,$D0,$T0,0);
452 my @thread1=&NEONROUND($A1,$B1,$C1,$D1,$T1,0);
453 my @thread2=&NEONROUND($A2,$B2,$C2,$D2,$T2,0);
454 my @thread3=&ROUND(0,4,8,12);
457 eval; eval(shift(@thread3));
458 eval(shift(@thread1)); eval(shift(@thread3));
459 eval(shift(@thread2)); eval(shift(@thread3));
462 @thread0=&NEONROUND($A0,$B0,$C0,$D0,$T0,1);
463 @thread1=&NEONROUND($A1,$B1,$C1,$D1,$T1,1);
464 @thread2=&NEONROUND($A2,$B2,$C2,$D2,$T2,1);
465 @thread3=&ROUND(0,5,10,15);
468 eval; eval(shift(@thread3));
469 eval(shift(@thread1)); eval(shift(@thread3));
470 eval(shift(@thread2)); eval(shift(@thread3));
475 add.32 @x[0],@x[0],@d[0] // accumulate key block
477 add @x[1],@x[1],@d[0],lsr#32
479 add.32 @x[2],@x[2],@d[1]
481 add @x[3],@x[3],@d[1],lsr#32
483 add.32 @x[4],@x[4],@d[2]
485 add @x[5],@x[5],@d[2],lsr#32
487 add.32 @x[6],@x[6],@d[3]
489 add @x[7],@x[7],@d[3],lsr#32
490 add.32 @x[8],@x[8],@d[4]
492 add @x[9],@x[9],@d[4],lsr#32
493 add.32 @x[10],@x[10],@d[5]
495 add @x[11],@x[11],@d[5],lsr#32
496 add.32 @x[12],@x[12],@d[6]
498 add @x[13],@x[13],@d[6],lsr#32
499 add.32 @x[14],@x[14],@d[7]
501 add @x[15],@x[15],@d[7],lsr#32
506 add @x[0],@x[0],@x[1],lsl#32 // pack
507 add @x[2],@x[2],@x[3],lsl#32
508 ldp @x[1],@x[3],[$inp,#0] // load input
509 add @x[4],@x[4],@x[5],lsl#32
510 add @x[6],@x[6],@x[7],lsl#32
511 ldp @x[5],@x[7],[$inp,#16]
512 add @x[8],@x[8],@x[9],lsl#32
513 add @x[10],@x[10],@x[11],lsl#32
514 ldp @x[9],@x[11],[$inp,#32]
515 add @x[12],@x[12],@x[13],lsl#32
516 add @x[14],@x[14],@x[15],lsl#32
517 ldp @x[13],@x[15],[$inp,#48]
529 ld1.8 {$T0-$T3},[$inp],#64
530 eor @x[0],@x[0],@x[1]
531 eor @x[2],@x[2],@x[3]
532 eor @x[4],@x[4],@x[5]
533 eor @x[6],@x[6],@x[7]
534 eor @x[8],@x[8],@x[9]
536 eor @x[10],@x[10],@x[11]
538 eor @x[12],@x[12],@x[13]
540 eor @x[14],@x[14],@x[15]
542 ld1.8 {$T0-$T3},[$inp],#64
544 stp @x[0],@x[2],[$out,#0] // store output
545 add @d[6],@d[6],#4 // increment counter
546 stp @x[4],@x[6],[$out,#16]
547 add @K[3],@K[3],$ONE // += 4
548 stp @x[8],@x[10],[$out,#32]
550 stp @x[12],@x[14],[$out,#48]
554 st1.8 {$A0-$D0},[$out],#64
555 ld1.8 {$A0-$D0},[$inp],#64
561 st1.8 {$A1-$D1},[$out],#64
567 st1.8 {$A2-$D2},[$out],#64
569 b.hi .Loop_outer_neon
571 ldp x19,x20,[x29,#16]
573 ldp x21,x22,[x29,#32]
574 ldp x23,x24,[x29,#48]
575 ldp x25,x26,[x29,#64]
576 ldp x27,x28,[x29,#80]
585 add @x[0],@x[0],@x[1],lsl#32 // pack
586 add @x[2],@x[2],@x[3],lsl#32
587 ldp @x[1],@x[3],[$inp,#0] // load input
588 add @x[4],@x[4],@x[5],lsl#32
589 add @x[6],@x[6],@x[7],lsl#32
590 ldp @x[5],@x[7],[$inp,#16]
591 add @x[8],@x[8],@x[9],lsl#32
592 add @x[10],@x[10],@x[11],lsl#32
593 ldp @x[9],@x[11],[$inp,#32]
594 add @x[12],@x[12],@x[13],lsl#32
595 add @x[14],@x[14],@x[15],lsl#32
596 ldp @x[13],@x[15],[$inp,#48]
608 eor @x[0],@x[0],@x[1]
609 eor @x[2],@x[2],@x[3]
610 eor @x[4],@x[4],@x[5]
611 eor @x[6],@x[6],@x[7]
612 eor @x[8],@x[8],@x[9]
613 eor @x[10],@x[10],@x[11]
614 eor @x[12],@x[12],@x[13]
615 eor @x[14],@x[14],@x[15]
617 stp @x[0],@x[2],[$out,#0] // store output
618 add @d[6],@d[6],#4 // increment counter
619 stp @x[4],@x[6],[$out,#16]
620 stp @x[8],@x[10],[$out,#32]
621 stp @x[12],@x[14],[$out,#48]
628 ld1.8 {$T0-$T3},[$inp],#64
633 st1.8 {$A0-$D0},[$out],#64
639 ld1.8 {$T0-$T3},[$inp],#64
644 st1.8 {$A1-$D1},[$out],#64
672 cbnz $len,.Loop_tail_neon
680 ldp x19,x20,[x29,#16]
682 ldp x21,x22,[x29,#32]
683 ldp x23,x24,[x29,#48]
684 ldp x25,x26,[x29,#64]
685 ldp x27,x28,[x29,#80]
688 .size ChaCha20_neon,.-ChaCha20_neon
691 my ($T0,$T1,$T2,$T3,$T4,$T5)=@K;
692 my ($A0,$B0,$C0,$D0,$A1,$B1,$C1,$D1,$A2,$B2,$C2,$D2,
693 $A3,$B3,$C3,$D3,$A4,$B4,$C4,$D4,$A5,$B5,$C5,$D5) = map("v$_.4s",(0..23));
696 .type ChaCha20_512_neon,%function
699 stp x29,x30,[sp,#-96]!
712 ldp @d[0],@d[1],[@x[0]] // load sigma
713 ld1 {@K[0]},[@x[0]],#16
714 ldp @d[2],@d[3],[$key] // load key
715 ldp @d[4],@d[5],[$key,#16]
716 ld1 {@K[1],@K[2]},[$key]
717 ldp @d[6],@d[7],[$ctr] // load counter
729 add @K[3],@K[3],$ONE // += 1
730 stp @K[0],@K[1],[sp,#0] // off-load key block, invariant part
731 add @K[3],@K[3],$ONE // not typo
736 shl $ONE,$ONE,#2 // 1 -> 4
738 stp d8,d9,[sp,#128+0] // meet ABI requirements
739 stp d10,d11,[sp,#128+16]
740 stp d12,d13,[sp,#128+32]
741 stp d14,d15,[sp,#128+48]
743 sub $len,$len,#512 // not typo
745 .Loop_outer_512_neon:
753 mov.32 @x[0],@d[0] // unpack key block
776 add $D4,$D0,$ONE // +4
778 add $D5,$D1,$ONE // +4
785 stp @K[3],@K[4],[sp,#48] // off-load key block, variable part
794 my @thread0=&NEONROUND($A0,$B0,$C0,$D0,$T0,0);
795 my @thread1=&NEONROUND($A1,$B1,$C1,$D1,$T1,0);
796 my @thread2=&NEONROUND($A2,$B2,$C2,$D2,$T2,0);
797 my @thread3=&NEONROUND($A3,$B3,$C3,$D3,$T3,0);
798 my @thread4=&NEONROUND($A4,$B4,$C4,$D4,$T4,0);
799 my @thread5=&NEONROUND($A5,$B5,$C5,$D5,$T5,0);
800 my @thread67=(&ROUND(0,4,8,12),&ROUND(0,5,10,15));
801 my $diff = ($#thread0+1)*6 - $#thread67 - 1;
805 eval; eval(shift(@thread67));
806 eval(shift(@thread1)); eval(shift(@thread67));
807 eval(shift(@thread2)); eval(shift(@thread67));
808 eval(shift(@thread3)); eval(shift(@thread67));
809 eval(shift(@thread4)); eval(shift(@thread67));
810 eval(shift(@thread5)); eval(shift(@thread67));
813 @thread0=&NEONROUND($A0,$B0,$C0,$D0,$T0,1);
814 @thread1=&NEONROUND($A1,$B1,$C1,$D1,$T1,1);
815 @thread2=&NEONROUND($A2,$B2,$C2,$D2,$T2,1);
816 @thread3=&NEONROUND($A3,$B3,$C3,$D3,$T3,1);
817 @thread4=&NEONROUND($A4,$B4,$C4,$D4,$T4,1);
818 @thread5=&NEONROUND($A5,$B5,$C5,$D5,$T5,1);
819 @thread67=(&ROUND(0,4,8,12),&ROUND(0,5,10,15));
822 eval; eval(shift(@thread67));
823 eval(shift(@thread1)); eval(shift(@thread67));
824 eval(shift(@thread2)); eval(shift(@thread67));
825 eval(shift(@thread3)); eval(shift(@thread67));
826 eval(shift(@thread4)); eval(shift(@thread67));
827 eval(shift(@thread5)); eval(shift(@thread67));
830 cbnz $ctr,.Loop_upper_neon
832 add.32 @x[0],@x[0],@d[0] // accumulate key block
833 add @x[1],@x[1],@d[0],lsr#32
834 add.32 @x[2],@x[2],@d[1]
835 add @x[3],@x[3],@d[1],lsr#32
836 add.32 @x[4],@x[4],@d[2]
837 add @x[5],@x[5],@d[2],lsr#32
838 add.32 @x[6],@x[6],@d[3]
839 add @x[7],@x[7],@d[3],lsr#32
840 add.32 @x[8],@x[8],@d[4]
841 add @x[9],@x[9],@d[4],lsr#32
842 add.32 @x[10],@x[10],@d[5]
843 add @x[11],@x[11],@d[5],lsr#32
844 add.32 @x[12],@x[12],@d[6]
845 add @x[13],@x[13],@d[6],lsr#32
846 add.32 @x[14],@x[14],@d[7]
847 add @x[15],@x[15],@d[7],lsr#32
849 add @x[0],@x[0],@x[1],lsl#32 // pack
850 add @x[2],@x[2],@x[3],lsl#32
851 ldp @x[1],@x[3],[$inp,#0] // load input
852 add @x[4],@x[4],@x[5],lsl#32
853 add @x[6],@x[6],@x[7],lsl#32
854 ldp @x[5],@x[7],[$inp,#16]
855 add @x[8],@x[8],@x[9],lsl#32
856 add @x[10],@x[10],@x[11],lsl#32
857 ldp @x[9],@x[11],[$inp,#32]
858 add @x[12],@x[12],@x[13],lsl#32
859 add @x[14],@x[14],@x[15],lsl#32
860 ldp @x[13],@x[15],[$inp,#48]
872 eor @x[0],@x[0],@x[1]
873 eor @x[2],@x[2],@x[3]
874 eor @x[4],@x[4],@x[5]
875 eor @x[6],@x[6],@x[7]
876 eor @x[8],@x[8],@x[9]
877 eor @x[10],@x[10],@x[11]
878 eor @x[12],@x[12],@x[13]
879 eor @x[14],@x[14],@x[15]
881 stp @x[0],@x[2],[$out,#0] // store output
882 add @d[6],@d[6],#1 // increment counter
883 mov.32 @x[0],@d[0] // unpack key block
885 stp @x[4],@x[6],[$out,#16]
888 stp @x[8],@x[10],[$out,#32]
891 stp @x[12],@x[14],[$out,#48]
908 @thread0=&NEONROUND($A0,$B0,$C0,$D0,$T0,0);
909 @thread1=&NEONROUND($A1,$B1,$C1,$D1,$T1,0);
910 @thread2=&NEONROUND($A2,$B2,$C2,$D2,$T2,0);
911 @thread3=&NEONROUND($A3,$B3,$C3,$D3,$T3,0);
912 @thread4=&NEONROUND($A4,$B4,$C4,$D4,$T4,0);
913 @thread5=&NEONROUND($A5,$B5,$C5,$D5,$T5,0);
914 @thread67=(&ROUND(0,4,8,12),&ROUND(0,5,10,15));
917 eval; eval(shift(@thread67));
918 eval(shift(@thread1)); eval(shift(@thread67));
919 eval(shift(@thread2)); eval(shift(@thread67));
920 eval(shift(@thread3)); eval(shift(@thread67));
921 eval(shift(@thread4)); eval(shift(@thread67));
922 eval(shift(@thread5)); eval(shift(@thread67));
925 @thread0=&NEONROUND($A0,$B0,$C0,$D0,$T0,1);
926 @thread1=&NEONROUND($A1,$B1,$C1,$D1,$T1,1);
927 @thread2=&NEONROUND($A2,$B2,$C2,$D2,$T2,1);
928 @thread3=&NEONROUND($A3,$B3,$C3,$D3,$T3,1);
929 @thread4=&NEONROUND($A4,$B4,$C4,$D4,$T4,1);
930 @thread5=&NEONROUND($A5,$B5,$C5,$D5,$T5,1);
931 @thread67=(&ROUND(0,4,8,12),&ROUND(0,5,10,15));
934 eval; eval(shift(@thread67));
935 eval(shift(@thread1)); eval(shift(@thread67));
936 eval(shift(@thread2)); eval(shift(@thread67));
937 eval(shift(@thread3)); eval(shift(@thread67));
938 eval(shift(@thread4)); eval(shift(@thread67));
939 eval(shift(@thread5)); eval(shift(@thread67));
942 cbnz $ctr,.Loop_lower_neon
944 add.32 @x[0],@x[0],@d[0] // accumulate key block
945 ldp @K[0],@K[1],[sp,#0]
946 add @x[1],@x[1],@d[0],lsr#32
947 ldp @K[2],@K[3],[sp,#32]
948 add.32 @x[2],@x[2],@d[1]
949 ldp @K[4],@K[5],[sp,#64]
950 add @x[3],@x[3],@d[1],lsr#32
952 add.32 @x[4],@x[4],@d[2]
954 add @x[5],@x[5],@d[2],lsr#32
956 add.32 @x[6],@x[6],@d[3]
958 add @x[7],@x[7],@d[3],lsr#32
960 add.32 @x[8],@x[8],@d[4]
962 add @x[9],@x[9],@d[4],lsr#32
964 add.32 @x[10],@x[10],@d[5]
966 add @x[11],@x[11],@d[5],lsr#32
968 add.32 @x[12],@x[12],@d[6]
970 add @x[13],@x[13],@d[6],lsr#32
972 add.32 @x[14],@x[14],@d[7]
974 add @x[15],@x[15],@d[7],lsr#32
975 add $D4,$D4,$ONE // +4
976 add @x[0],@x[0],@x[1],lsl#32 // pack
977 add $D5,$D5,$ONE // +4
978 add @x[2],@x[2],@x[3],lsl#32
980 ldp @x[1],@x[3],[$inp,#0] // load input
982 add @x[4],@x[4],@x[5],lsl#32
984 add @x[6],@x[6],@x[7],lsl#32
986 ldp @x[5],@x[7],[$inp,#16]
988 add @x[8],@x[8],@x[9],lsl#32
990 add @x[10],@x[10],@x[11],lsl#32
992 ldp @x[9],@x[11],[$inp,#32]
994 add @x[12],@x[12],@x[13],lsl#32
996 add @x[14],@x[14],@x[15],lsl#32
998 ldp @x[13],@x[15],[$inp,#48]
1013 ld1.8 {$T0-$T3},[$inp],#64
1014 eor @x[0],@x[0],@x[1]
1015 eor @x[2],@x[2],@x[3]
1016 eor @x[4],@x[4],@x[5]
1017 eor @x[6],@x[6],@x[7]
1018 eor @x[8],@x[8],@x[9]
1020 eor @x[10],@x[10],@x[11]
1022 eor @x[12],@x[12],@x[13]
1024 eor @x[14],@x[14],@x[15]
1026 ld1.8 {$T0-$T3},[$inp],#64
1028 stp @x[0],@x[2],[$out,#0] // store output
1029 add @d[6],@d[6],#7 // increment counter
1030 stp @x[4],@x[6],[$out,#16]
1031 stp @x[8],@x[10],[$out,#32]
1032 stp @x[12],@x[14],[$out,#48]
1034 st1.8 {$A0-$D0},[$out],#64
1036 ld1.8 {$A0-$D0},[$inp],#64
1041 st1.8 {$A1-$D1},[$out],#64
1043 ld1.8 {$A1-$D1},[$inp],#64
1045 ldp @K[0],@K[1],[sp,#0]
1047 ldp @K[2],@K[3],[sp,#32]
1050 st1.8 {$A2-$D2},[$out],#64
1052 ld1.8 {$A2-$D2},[$inp],#64
1057 st1.8 {$A3-$D3},[$out],#64
1059 ld1.8 {$A3-$D3},[$inp],#64
1064 st1.8 {$A4-$D4},[$out],#64
1066 shl $A0,$ONE,#1 // 4 -> 8
1071 st1.8 {$A5-$D5},[$out],#64
1073 add @K[3],@K[3],$A0 // += 8
1078 b.hs .Loop_outer_512_neon
1081 ushr $A0,$ONE,#2 // 4 -> 1
1083 ldp d8,d9,[sp,#128+0] // meet ABI requirements
1084 ldp d10,d11,[sp,#128+16]
1085 ldp d12,d13,[sp,#128+32]
1086 ldp d14,d15,[sp,#128+48]
1088 stp @K[0],$ONE,[sp,#0] // wipe off-load area
1089 stp @K[0],$ONE,[sp,#32]
1090 stp @K[0],$ONE,[sp,#64]
1092 b.eq .Ldone_512_neon
1095 sub @K[3],@K[3],$A0 // -= 1
1099 b.hs .Loop_outer_neon
1101 eor @K[1],@K[1],@K[1]
1102 eor @K[2],@K[2],@K[2]
1103 eor @K[3],@K[3],@K[3]
1104 eor @K[4],@K[4],@K[4]
1105 eor @K[5],@K[5],@K[5]
1106 eor @K[6],@K[6],@K[6]
1110 ldp x19,x20,[x29,#16]
1112 ldp x21,x22,[x29,#32]
1113 ldp x23,x24,[x29,#48]
1114 ldp x25,x26,[x29,#64]
1115 ldp x27,x28,[x29,#80]
1116 ldp x29,x30,[sp],#96
1118 .size ChaCha20_512_neon,.-ChaCha20_512_neon
1123 foreach (split("\n",$code)) {
1124 s/\`([^\`]*)\`/eval $1/geo;
1126 (s/\b([a-z]+)\.32\b/$1/ and (s/x([0-9]+)/w$1/g or 1)) or
1127 (m/\b(eor|ext|mov)\b/ and (s/\.4s/\.16b/g or 1)) or
1128 (s/\b((?:ld|st)1)\.8\b/$1/ and (s/\.4s/\.16b/g or 1)) or
1129 (m/\b(ld|st)[rp]\b/ and (s/v([0-9]+)\.4s/q$1/g or 1)) or
1130 (s/\brev32\.16\b/rev32/ and (s/\.4s/\.8h/g or 1));
1132 #s/\bq([0-9]+)#(lo|hi)/sprintf "d%d",2*$1+($2 eq "hi")/geo;
1136 close STDOUT; # flush