Debian Codegraph @

debian/rust-vendor/aws-lc-sys/aws-lc/crypto/chacha/asm/chacha-armv8.pl

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