2 # SPDX-License-Identifier: GPL-2.0 OR BSD-3-Clause
4 # Copyright (C) 2017-2018 Samuel Neves <sneves@dei.uc.pt>. All Rights Reserved.
5 # Copyright (C) 2017-2019 Jason A. Donenfeld <Jason@zx2c4.com>. All Rights Reserved.
6 # Copyright (C) 2006-2017 CRYPTOGAMS by <appro@openssl.org>. All Rights Reserved.
8 # This code is taken from the OpenSSL project but the author, Andy Polyakov,
9 # has relicensed it under the licenses specified in the SPDX header above.
10 # The original headers, including the original license headers, are
11 # included below for completeness.
13 # ====================================================================
14 # Written by Andy Polyakov <appro@openssl.org> for the OpenSSL
15 # project. The module is, however, dual licensed under OpenSSL and
16 # CRYPTOGAMS licenses depending on where you obtain it. For further
17 # details see http://www.openssl.org/~appro/cryptogams/.
18 # ====================================================================
20 # This module implements Poly1305 hash for x86_64.
28 # Add AVX512F+VL+BW code path.
32 # Convert AVX512F+VL+BW code path to pure AVX512F, so that it can be
33 # executed even on Knights Landing. Trigger for modification was
34 # observation that AVX512 code paths can negatively affect overall
35 # Skylake-X system performance. Since we are likely to suppress
36 # AVX512F capability flag [at least on Skylake-X], conversion serves
37 # as kind of "investment protection". Note that next *lake processor,
38 # Cannonlake, has AVX512IFMA code path to execute...
40 # Numbers are cycles per processed byte with poly1305_blocks alone,
41 # measured with rdtsc at fixed clock frequency.
43 # IALU/gcc-4.8(*) AVX(**) AVX2 AVX-512
46 # Westmere 1.88/+120% -
47 # Sandy Bridge 1.39/+140% 1.10
48 # Haswell 1.14/+175% 1.11 0.65
49 # Skylake[-X] 1.13/+120% 0.96 0.51 [0.35]
50 # Silvermont 2.83/+95% -
51 # Knights L 3.60/? 1.65 1.10 0.41(***)
52 # Goldmont 1.70/+180% -
53 # VIA Nano 1.82/+150% -
54 # Sledgehammer 1.38/+160% -
55 # Bulldozer 2.30/+130% 0.97
56 # Ryzen 1.15/+200% 1.08 1.18
58 # (*) improvement coefficients relative to clang are more modest and
59 # are ~50% on most processors, in both cases we are comparing to
61 # (**) SSE2 implementation was attempted, but among non-AVX processors
62 # it was faster than integer-only code only on older Intel P4 and
63 # Core processors, 50-30%, less newer processor is, but slower on
64 # contemporary ones, for example almost 2x slower on Atom, and as
65 # former are naturally disappearing, SSE2 is deemed unnecessary;
66 # (***) strangely enough performance seems to vary from core to core,
67 # listed result is best case;
71 if ($flavour =~ /\./) { $output = $flavour; undef $flavour; }
73 $win64=0; $win64=1 if ($flavour =~ /[nm]asm|mingw64/ || $output =~ /\.asm$/);
74 $kernel=0; $kernel=1 if (!$flavour && !$output);
77 $0 =~ m/(.*[\/\\])[^\
/\\]+$/; $dir=$1;
78 ( $xlate="${dir}x86_64-xlate.pl" and -f
$xlate ) or
79 ( $xlate="${dir}../../perlasm/x86_64-xlate.pl" and -f
$xlate) or
80 die "can't locate x86_64-xlate.pl";
82 open OUT
,"| \"$^X\" \"$xlate\" $flavour \"$output\"";
85 if (`$ENV{CC} -Wa,-v -c -o /dev/null -x assembler /dev/null 2>&1`
86 =~ /GNU assembler version ([2-9]\.[0-9]+)/) {
87 $avx = ($1>=2.19) + ($1>=2.22) + ($1>=2.25);
90 if (!$avx && $win64 && ($flavour =~ /nasm/ || $ENV{ASM
} =~ /nasm/) &&
91 `nasm -v 2>&1` =~ /NASM version ([2-9]\.[0-9]+)(?:\.([0-9]+))?/) {
92 $avx = ($1>=2.09) + ($1>=2.10) + ($1>=2.12);
93 $avx += 1 if ($1==2.11 && $2>=8);
96 if (!$avx && $win64 && ($flavour =~ /masm/ || $ENV{ASM
} =~ /ml64/) &&
97 `ml64 2>&1` =~ /Version ([0-9]+)\./) {
98 $avx = ($1>=10) + ($1>=11);
101 if (!$avx && `$ENV{CC} -v 2>&1` =~ /((?:^clang|LLVM) version|.*based on LLVM) ([3-9]\.[0-9]+)/) {
102 $avx = ($2>=3.0) + ($2>3.0);
105 $avx = 4; # The kernel uses ifdefs for this.
108 sub declare_function
() {
109 my ($name, $align, $nargs) = @_;
111 $code .= "SYM_FUNC_START($name)\n";
112 $code .= ".L$name:\n";
114 $code .= ".globl $name\n";
115 $code .= ".type $name,\@function,$nargs\n";
116 $code .= ".align $align\n";
124 $code .= "SYM_FUNC_END($name)\n";
126 $code .= ".size $name,.-$name\n";
130 $code.=<<___
if $kernel;
131 #include <linux/linkage.h>
135 $code.=<<___
if $kernel;
142 .long
0x0ffffff,0,0x0ffffff,0,0x0ffffff,0,0x0ffffff,0
144 .long
`1<<24`,0,`1<<24`,0,`1<<24`,0,`1<<24`,0
146 .long
0x3ffffff,0,0x3ffffff,0,0x3ffffff,0,0x3ffffff,0
148 .long
2,2,2,3,2,0,2,1
150 .long
0,0,0,1, 0,2,0,3, 0,4,0,5, 0,6,0,7
153 .long
0,1,1,2,2,3,7,7
157 .quad
0xfffffffffff,0xfffffffffff,0x3ffffffffff,0xffffffffffffffff
165 .quad
0xfffffffffff,0xfffffffffff,0xfffffffffff,0xfffffffffff
166 .quad
0xfffffffffff,0xfffffffffff,0xfffffffffff,0xfffffffffff
168 .quad
0x3ffffffffff,0x3ffffffffff,0x3ffffffffff,0x3ffffffffff
169 .quad
0x3ffffffffff,0x3ffffffffff,0x3ffffffffff,0x3ffffffffff
172 $code.=<<___
if (!$kernel);
173 .asciz
"Poly1305 for x86_64, CRYPTOGAMS by <appro\@openssl.org>"
177 my ($ctx,$inp,$len,$padbit)=("%rdi","%rsi","%rdx","%rcx");
178 my ($mac,$nonce)=($inp,$len); # *_emit arguments
179 my ($d1,$d2,$d3, $r0,$r1,$s1)=("%r8","%r9","%rdi","%r11","%r12","%r13");
180 my ($h0,$h1,$h2)=("%r14","%rbx","%r10");
182 sub poly1305_iteration
{
183 # input: copy of $r1 in %rax, $h0-$h2, $r0-$r1
184 # output: $h0-$h2 *= $r0-$r1
192 mov
%rax,$h0 # future $h0
202 mov
$h2,$h1 # borrow $h1
206 imulq
$s1,$h1 # h2*s1
211 imulq
$r0,$h2 # h2*r0
213 mov \
$-4,%rax # mask value
216 and $d3,%rax # last reduction step
227 ########################################################################
228 # Layout of opaque area is following.
230 # unsigned __int64 h[3]; # current hash value base 2^64
231 # unsigned __int64 r[2]; # key value base 2^64
236 $code.=<<___
if (!$kernel);
237 .extern OPENSSL_ia32cap_P
239 .globl poly1305_init_x86_64
240 .hidden poly1305_init_x86_64
241 .globl poly1305_blocks_x86_64
242 .hidden poly1305_blocks_x86_64
243 .globl poly1305_emit_x86_64
244 .hidden poly1305_emit_x86_64
246 &declare_function
("poly1305_init_x86_64", 32, 3);
249 mov
%rax,0($ctx) # initialize hash value
256 $code.=<<___
if (!$kernel);
257 lea poly1305_blocks_x86_64
(%rip),%r10
258 lea poly1305_emit_x86_64
(%rip),%r11
260 $code.=<<___
if (!$kernel && $avx);
261 mov OPENSSL_ia32cap_P
+4(%rip),%r9
262 lea poly1305_blocks_avx
(%rip),%rax
263 lea poly1305_emit_avx
(%rip),%rcx
264 bt \
$`60-32`,%r9 # AVX?
268 $code.=<<___
if (!$kernel && $avx>1);
269 lea poly1305_blocks_avx2
(%rip),%rax
270 bt \
$`5+32`,%r9 # AVX2?
273 $code.=<<___
if (!$kernel && $avx>3);
274 mov \
$`(1<<31|1<<21|1<<16)`,%rax
281 mov \
$0x0ffffffc0fffffff,%rax
282 mov \
$0x0ffffffc0ffffffc,%rcx
288 $code.=<<___
if (!$kernel && $flavour !~ /elf32/);
292 $code.=<<___
if (!$kernel && $flavour =~ /elf32/);
301 &end_function
("poly1305_init_x86_64");
303 &declare_function
("poly1305_blocks_x86_64", 32, 4);
308 jz
.Lno_data
# too short
324 mov
$len,%r15 # reassign $len
326 mov
24($ctx),$r0 # load r
329 mov
0($ctx),$h0 # load hash value
336 add
$r1,$s1 # s1 = r1 + (r1 >> 2)
341 add
0($inp),$h0 # accumulate input
347 &poly1305_iteration
();
357 mov
$h0,0($ctx) # store hash value
372 .cfi_adjust_cfa_offset
-48
378 &end_function
("poly1305_blocks_x86_64");
380 &declare_function
("poly1305_emit_x86_64", 32, 3);
383 mov
0($ctx),%r8 # load hash value
388 add \
$5,%r8 # compare to modulus
392 shr \
$2,%r10 # did 130-bit value overflow?
396 add
0($nonce),%rax # accumulate nonce
398 mov
%rax,0($mac) # write result
403 &end_function
("poly1305_emit_x86_64");
406 ########################################################################
407 # Layout of opaque area is following.
409 # unsigned __int32 h[5]; # current hash value base 2^26
410 # unsigned __int32 is_base2_26;
411 # unsigned __int64 r[2]; # key value base 2^64
412 # unsigned __int64 pad;
413 # struct { unsigned __int32 r^2, r^1, r^4, r^3; } r[9];
415 # where r^n are base 2^26 digits of degrees of multiplier key. There are
416 # 5 digits, but last four are interleaved with multiples of 5, totalling
417 # in 9 elements: r0, r1, 5*r1, r2, 5*r2, r3, 5*r3, r4, 5*r4.
419 my ($H0,$H1,$H2,$H3,$H4, $T0,$T1,$T2,$T3,$T4, $D0,$D1,$D2,$D3,$D4, $MASK) =
420 map("%xmm$_",(0..15));
423 .type __poly1305_block
,\
@abi-omnipotent
428 &poly1305_iteration
();
432 .size __poly1305_block
,.-__poly1305_block
434 .type __poly1305_init_avx
,\
@abi-omnipotent
443 lea
48+64($ctx),$ctx # size optimization
446 call __poly1305_block
# r^2
448 mov \
$0x3ffffff,%eax # save interleaved r^2 and r base 2^26
454 mov
%eax,`16*0+0-64`($ctx)
456 mov
%edx,`16*0+4-64`($ctx)
463 mov
%eax,`16*1+0-64`($ctx)
464 lea
(%rax,%rax,4),%eax # *5
465 mov
%edx,`16*1+4-64`($ctx)
466 lea
(%rdx,%rdx,4),%edx # *5
467 mov
%eax,`16*2+0-64`($ctx)
469 mov
%edx,`16*2+4-64`($ctx)
480 mov
%eax,`16*3+0-64`($ctx)
481 lea
(%rax,%rax,4),%eax # *5
482 mov
%edx,`16*3+4-64`($ctx)
483 lea
(%rdx,%rdx,4),%edx # *5
484 mov
%eax,`16*4+0-64`($ctx)
486 mov
%edx,`16*4+4-64`($ctx)
495 mov
%eax,`16*5+0-64`($ctx)
496 lea
(%rax,%rax,4),%eax # *5
497 mov
%edx,`16*5+4-64`($ctx)
498 lea
(%rdx,%rdx,4),%edx # *5
499 mov
%eax,`16*6+0-64`($ctx)
501 mov
%edx,`16*6+4-64`($ctx)
507 mov
$d1#d,`16*7+0-64`($ctx)
508 lea
($d1,$d1,4),$d1 # *5
509 mov
$d2#d,`16*7+4-64`($ctx)
510 lea
($d2,$d2,4),$d2 # *5
511 mov
$d1#d,`16*8+0-64`($ctx)
512 mov
$d2#d,`16*8+4-64`($ctx)
515 call __poly1305_block
# r^3
517 mov \
$0x3ffffff,%eax # save r^3 base 2^26
521 mov
%eax,`16*0+12-64`($ctx)
525 mov
%edx,`16*1+12-64`($ctx)
526 lea
(%rdx,%rdx,4),%edx # *5
528 mov
%edx,`16*2+12-64`($ctx)
534 mov
%eax,`16*3+12-64`($ctx)
535 lea
(%rax,%rax,4),%eax # *5
537 mov
%eax,`16*4+12-64`($ctx)
542 mov
%edx,`16*5+12-64`($ctx)
543 lea
(%rdx,%rdx,4),%edx # *5
545 mov
%edx,`16*6+12-64`($ctx)
550 mov
$d1#d,`16*7+12-64`($ctx)
551 lea
($d1,$d1,4),$d1 # *5
552 mov
$d1#d,`16*8+12-64`($ctx)
555 call __poly1305_block
# r^4
557 mov \
$0x3ffffff,%eax # save r^4 base 2^26
561 mov
%eax,`16*0+8-64`($ctx)
565 mov
%edx,`16*1+8-64`($ctx)
566 lea
(%rdx,%rdx,4),%edx # *5
568 mov
%edx,`16*2+8-64`($ctx)
574 mov
%eax,`16*3+8-64`($ctx)
575 lea
(%rax,%rax,4),%eax # *5
577 mov
%eax,`16*4+8-64`($ctx)
582 mov
%edx,`16*5+8-64`($ctx)
583 lea
(%rdx,%rdx,4),%edx # *5
585 mov
%edx,`16*6+8-64`($ctx)
590 mov
$d1#d,`16*7+8-64`($ctx)
591 lea
($d1,$d1,4),$d1 # *5
592 mov
$d1#d,`16*8+8-64`($ctx)
594 lea
-48-64($ctx),$ctx # size [de-]optimization
597 .size __poly1305_init_avx
,.-__poly1305_init_avx
600 &declare_function
("poly1305_blocks_avx", 32, 4);
603 mov
20($ctx),%r8d # is_base2_26
636 mov
$len,%r15 # reassign $len
638 mov
0($ctx),$d1 # load hash value
642 mov
24($ctx),$r0 # load r
645 ################################# base 2^26 -> base 2^64
647 and \
$`-1*(1<<31)`,$d1
648 mov
$d2,$r1 # borrow $r1
650 and \
$`-1*(1<<31)`,$d2
664 adc \
$0,$h2 # can be partially reduced...
666 mov \
$-4,$d2 # ... so reduce
679 add
$r1,$s1 # s1 = r1 + (r1 >> 2)
681 add
0($inp),$h0 # accumulate input
686 call __poly1305_block
688 test
$padbit,$padbit # if $padbit is zero,
689 jz
.Lstore_base2_64_avx
# store hash in base 2^64 format
691 ################################# base 2^64 -> base 2^26
698 and \
$0x3ffffff,%rax # h[0]
700 and \
$0x3ffffff,%rdx # h[1]
704 and \
$0x3ffffff,$h0 # h[2]
706 and \
$0x3ffffff,$h1 # h[3]
710 jz
.Lstore_base2_26_avx
720 .Lstore_base2_64_avx
:
723 mov
$h2,16($ctx) # note that is_base2_26 is zeroed
727 .Lstore_base2_26_avx
:
728 mov
%rax#d,0($ctx) # store hash value base 2^26
748 .Lblocks_avx_epilogue
:
770 mov
$len,%r15 # reassign $len
772 mov
24($ctx),$r0 # load r
775 mov
0($ctx),$h0 # load hash value
782 add
$r1,$s1 # s1 = r1 + (r1 >> 2)
787 add
0($inp),$h0 # accumulate input
793 call __poly1305_block
796 ################################# base 2^64 -> base 2^26
803 and \
$0x3ffffff,%rax # h[0]
805 and \
$0x3ffffff,%rdx # h[1]
809 and \
$0x3ffffff,$h0 # h[2]
811 and \
$0x3ffffff,$h1 # h[3]
819 movl \
$1,20($ctx) # set is_base2_26
821 call __poly1305_init_avx
837 .Lbase2_64_avx_epilogue
:
844 vmovd
4*0($ctx),$H0 # load hash value
852 $code.=<<___
if (!$win64);
854 .cfi_def_cfa_register
%r10
860 $code.=<<___
if ($win64);
863 vmovdqa
%xmm6,0x50(%r11)
864 vmovdqa
%xmm7,0x60(%r11)
865 vmovdqa
%xmm8,0x70(%r11)
866 vmovdqa
%xmm9,0x80(%r11)
867 vmovdqa
%xmm10,0x90(%r11)
868 vmovdqa
%xmm11,0xa0(%r11)
869 vmovdqa
%xmm12,0xb0(%r11)
870 vmovdqa
%xmm13,0xc0(%r11)
871 vmovdqa
%xmm14,0xd0(%r11)
872 vmovdqa
%xmm15,0xe0(%r11)
880 vmovdqu
`16*3`($ctx),$D4 # preload r0^2
881 lea
`16*3+64`($ctx),$ctx # size optimization
882 lea
.Lconst
(%rip),%rcx
884 ################################################################
886 vmovdqu
16*2($inp),$T0
887 vmovdqu
16*3($inp),$T1
888 vmovdqa
64(%rcx),$MASK # .Lmask26
890 vpsrldq \
$6,$T0,$T2 # splat input
892 vpunpckhqdq
$T1,$T0,$T4 # 4
893 vpunpcklqdq
$T1,$T0,$T0 # 0:1
894 vpunpcklqdq
$T3,$T2,$T3 # 2:3
896 vpsrlq \
$40,$T4,$T4 # 4
898 vpand
$MASK,$T0,$T0 # 0
900 vpand
$MASK,$T1,$T1 # 1
902 vpand
$MASK,$T2,$T2 # 2
903 vpand
$MASK,$T3,$T3 # 3
904 vpor
32(%rcx),$T4,$T4 # padbit, yes, always
908 # expand and copy pre-calculated table to stack
909 vmovdqu
`16*1-64`($ctx),$D1
910 vmovdqu
`16*2-64`($ctx),$D2
911 vpshufd \
$0xEE,$D4,$D3 # 34xx -> 3434
912 vpshufd \
$0x44,$D4,$D0 # xx12 -> 1212
913 vmovdqa
$D3,-0x90(%r11)
914 vmovdqa
$D0,0x00(%rsp)
915 vpshufd \
$0xEE,$D1,$D4
916 vmovdqu
`16*3-64`($ctx),$D0
917 vpshufd \
$0x44,$D1,$D1
918 vmovdqa
$D4,-0x80(%r11)
919 vmovdqa
$D1,0x10(%rsp)
920 vpshufd \
$0xEE,$D2,$D3
921 vmovdqu
`16*4-64`($ctx),$D1
922 vpshufd \
$0x44,$D2,$D2
923 vmovdqa
$D3,-0x70(%r11)
924 vmovdqa
$D2,0x20(%rsp)
925 vpshufd \
$0xEE,$D0,$D4
926 vmovdqu
`16*5-64`($ctx),$D2
927 vpshufd \
$0x44,$D0,$D0
928 vmovdqa
$D4,-0x60(%r11)
929 vmovdqa
$D0,0x30(%rsp)
930 vpshufd \
$0xEE,$D1,$D3
931 vmovdqu
`16*6-64`($ctx),$D0
932 vpshufd \
$0x44,$D1,$D1
933 vmovdqa
$D3,-0x50(%r11)
934 vmovdqa
$D1,0x40(%rsp)
935 vpshufd \
$0xEE,$D2,$D4
936 vmovdqu
`16*7-64`($ctx),$D1
937 vpshufd \
$0x44,$D2,$D2
938 vmovdqa
$D4,-0x40(%r11)
939 vmovdqa
$D2,0x50(%rsp)
940 vpshufd \
$0xEE,$D0,$D3
941 vmovdqu
`16*8-64`($ctx),$D2
942 vpshufd \
$0x44,$D0,$D0
943 vmovdqa
$D3,-0x30(%r11)
944 vmovdqa
$D0,0x60(%rsp)
945 vpshufd \
$0xEE,$D1,$D4
946 vpshufd \
$0x44,$D1,$D1
947 vmovdqa
$D4,-0x20(%r11)
948 vmovdqa
$D1,0x70(%rsp)
949 vpshufd \
$0xEE,$D2,$D3
950 vmovdqa
0x00(%rsp),$D4 # preload r0^2
951 vpshufd \
$0x44,$D2,$D2
952 vmovdqa
$D3,-0x10(%r11)
953 vmovdqa
$D2,0x80(%rsp)
959 ################################################################
960 # ((inp[0]*r^4+inp[2]*r^2+inp[4])*r^4+inp[6]*r^2
961 # ((inp[1]*r^4+inp[3]*r^2+inp[5])*r^3+inp[7]*r
962 # \___________________/
963 # ((inp[0]*r^4+inp[2]*r^2+inp[4])*r^4+inp[6]*r^2+inp[8])*r^2
964 # ((inp[1]*r^4+inp[3]*r^2+inp[5])*r^4+inp[7]*r^2+inp[9])*r
965 # \___________________/ \____________________/
967 # Note that we start with inp[2:3]*r^2. This is because it
968 # doesn't depend on reduction in previous iteration.
969 ################################################################
970 # d4 = h4*r0 + h3*r1 + h2*r2 + h1*r3 + h0*r4
971 # d3 = h3*r0 + h2*r1 + h1*r2 + h0*r3 + h4*5*r4
972 # d2 = h2*r0 + h1*r1 + h0*r2 + h4*5*r3 + h3*5*r4
973 # d1 = h1*r0 + h0*r1 + h4*5*r2 + h3*5*r3 + h2*5*r4
974 # d0 = h0*r0 + h4*5*r1 + h3*5*r2 + h2*5*r3 + h1*5*r4
976 # though note that $Tx and $Hx are "reversed" in this section,
977 # and $D4 is preloaded with r0^2...
979 vpmuludq
$T0,$D4,$D0 # d0 = h0*r0
980 vpmuludq
$T1,$D4,$D1 # d1 = h1*r0
981 vmovdqa
$H2,0x20(%r11) # offload hash
982 vpmuludq
$T2,$D4,$D2 # d3 = h2*r0
983 vmovdqa
0x10(%rsp),$H2 # r1^2
984 vpmuludq
$T3,$D4,$D3 # d3 = h3*r0
985 vpmuludq
$T4,$D4,$D4 # d4 = h4*r0
987 vmovdqa
$H0,0x00(%r11) #
988 vpmuludq
0x20(%rsp),$T4,$H0 # h4*s1
989 vmovdqa
$H1,0x10(%r11) #
990 vpmuludq
$T3,$H2,$H1 # h3*r1
991 vpaddq
$H0,$D0,$D0 # d0 += h4*s1
992 vpaddq
$H1,$D4,$D4 # d4 += h3*r1
993 vmovdqa
$H3,0x30(%r11) #
994 vpmuludq
$T2,$H2,$H0 # h2*r1
995 vpmuludq
$T1,$H2,$H1 # h1*r1
996 vpaddq
$H0,$D3,$D3 # d3 += h2*r1
997 vmovdqa
0x30(%rsp),$H3 # r2^2
998 vpaddq
$H1,$D2,$D2 # d2 += h1*r1
999 vmovdqa
$H4,0x40(%r11) #
1000 vpmuludq
$T0,$H2,$H2 # h0*r1
1001 vpmuludq
$T2,$H3,$H0 # h2*r2
1002 vpaddq
$H2,$D1,$D1 # d1 += h0*r1
1004 vmovdqa
0x40(%rsp),$H4 # s2^2
1005 vpaddq
$H0,$D4,$D4 # d4 += h2*r2
1006 vpmuludq
$T1,$H3,$H1 # h1*r2
1007 vpmuludq
$T0,$H3,$H3 # h0*r2
1008 vpaddq
$H1,$D3,$D3 # d3 += h1*r2
1009 vmovdqa
0x50(%rsp),$H2 # r3^2
1010 vpaddq
$H3,$D2,$D2 # d2 += h0*r2
1011 vpmuludq
$T4,$H4,$H0 # h4*s2
1012 vpmuludq
$T3,$H4,$H4 # h3*s2
1013 vpaddq
$H0,$D1,$D1 # d1 += h4*s2
1014 vmovdqa
0x60(%rsp),$H3 # s3^2
1015 vpaddq
$H4,$D0,$D0 # d0 += h3*s2
1017 vmovdqa
0x80(%rsp),$H4 # s4^2
1018 vpmuludq
$T1,$H2,$H1 # h1*r3
1019 vpmuludq
$T0,$H2,$H2 # h0*r3
1020 vpaddq
$H1,$D4,$D4 # d4 += h1*r3
1021 vpaddq
$H2,$D3,$D3 # d3 += h0*r3
1022 vpmuludq
$T4,$H3,$H0 # h4*s3
1023 vpmuludq
$T3,$H3,$H1 # h3*s3
1024 vpaddq
$H0,$D2,$D2 # d2 += h4*s3
1025 vmovdqu
16*0($inp),$H0 # load input
1026 vpaddq
$H1,$D1,$D1 # d1 += h3*s3
1027 vpmuludq
$T2,$H3,$H3 # h2*s3
1028 vpmuludq
$T2,$H4,$T2 # h2*s4
1029 vpaddq
$H3,$D0,$D0 # d0 += h2*s3
1031 vmovdqu
16*1($inp),$H1 #
1032 vpaddq
$T2,$D1,$D1 # d1 += h2*s4
1033 vpmuludq
$T3,$H4,$T3 # h3*s4
1034 vpmuludq
$T4,$H4,$T4 # h4*s4
1035 vpsrldq \
$6,$H0,$H2 # splat input
1036 vpaddq
$T3,$D2,$D2 # d2 += h3*s4
1037 vpaddq
$T4,$D3,$D3 # d3 += h4*s4
1038 vpsrldq \
$6,$H1,$H3 #
1039 vpmuludq
0x70(%rsp),$T0,$T4 # h0*r4
1040 vpmuludq
$T1,$H4,$T0 # h1*s4
1041 vpunpckhqdq
$H1,$H0,$H4 # 4
1042 vpaddq
$T4,$D4,$D4 # d4 += h0*r4
1043 vmovdqa
-0x90(%r11),$T4 # r0^4
1044 vpaddq
$T0,$D0,$D0 # d0 += h1*s4
1046 vpunpcklqdq
$H1,$H0,$H0 # 0:1
1047 vpunpcklqdq
$H3,$H2,$H3 # 2:3
1049 #vpsrlq \$40,$H4,$H4 # 4
1050 vpsrldq \
$`40/8`,$H4,$H4 # 4
1052 vpand
$MASK,$H0,$H0 # 0
1054 vpand
$MASK,$H1,$H1 # 1
1055 vpand
0(%rcx),$H4,$H4 # .Lmask24
1057 vpand
$MASK,$H2,$H2 # 2
1058 vpand
$MASK,$H3,$H3 # 3
1059 vpor
32(%rcx),$H4,$H4 # padbit, yes, always
1061 vpaddq
0x00(%r11),$H0,$H0 # add hash value
1062 vpaddq
0x10(%r11),$H1,$H1
1063 vpaddq
0x20(%r11),$H2,$H2
1064 vpaddq
0x30(%r11),$H3,$H3
1065 vpaddq
0x40(%r11),$H4,$H4
1072 ################################################################
1073 # Now we accumulate (inp[0:1]+hash)*r^4
1074 ################################################################
1075 # d4 = h4*r0 + h3*r1 + h2*r2 + h1*r3 + h0*r4
1076 # d3 = h3*r0 + h2*r1 + h1*r2 + h0*r3 + h4*5*r4
1077 # d2 = h2*r0 + h1*r1 + h0*r2 + h4*5*r3 + h3*5*r4
1078 # d1 = h1*r0 + h0*r1 + h4*5*r2 + h3*5*r3 + h2*5*r4
1079 # d0 = h0*r0 + h4*5*r1 + h3*5*r2 + h2*5*r3 + h1*5*r4
1081 vpmuludq
$H0,$T4,$T0 # h0*r0
1082 vpmuludq
$H1,$T4,$T1 # h1*r0
1085 vmovdqa
-0x80(%r11),$T2 # r1^4
1086 vpmuludq
$H2,$T4,$T0 # h2*r0
1087 vpmuludq
$H3,$T4,$T1 # h3*r0
1090 vpmuludq
$H4,$T4,$T4 # h4*r0
1091 vpmuludq
-0x70(%r11),$H4,$T0 # h4*s1
1094 vpaddq
$T0,$D0,$D0 # d0 += h4*s1
1095 vpmuludq
$H2,$T2,$T1 # h2*r1
1096 vpmuludq
$H3,$T2,$T0 # h3*r1
1097 vpaddq
$T1,$D3,$D3 # d3 += h2*r1
1098 vmovdqa
-0x60(%r11),$T3 # r2^4
1099 vpaddq
$T0,$D4,$D4 # d4 += h3*r1
1100 vpmuludq
$H1,$T2,$T1 # h1*r1
1101 vpmuludq
$H0,$T2,$T2 # h0*r1
1102 vpaddq
$T1,$D2,$D2 # d2 += h1*r1
1103 vpaddq
$T2,$D1,$D1 # d1 += h0*r1
1105 vmovdqa
-0x50(%r11),$T4 # s2^4
1106 vpmuludq
$H2,$T3,$T0 # h2*r2
1107 vpmuludq
$H1,$T3,$T1 # h1*r2
1108 vpaddq
$T0,$D4,$D4 # d4 += h2*r2
1109 vpaddq
$T1,$D3,$D3 # d3 += h1*r2
1110 vmovdqa
-0x40(%r11),$T2 # r3^4
1111 vpmuludq
$H0,$T3,$T3 # h0*r2
1112 vpmuludq
$H4,$T4,$T0 # h4*s2
1113 vpaddq
$T3,$D2,$D2 # d2 += h0*r2
1114 vpaddq
$T0,$D1,$D1 # d1 += h4*s2
1115 vmovdqa
-0x30(%r11),$T3 # s3^4
1116 vpmuludq
$H3,$T4,$T4 # h3*s2
1117 vpmuludq
$H1,$T2,$T1 # h1*r3
1118 vpaddq
$T4,$D0,$D0 # d0 += h3*s2
1120 vmovdqa
-0x10(%r11),$T4 # s4^4
1121 vpaddq
$T1,$D4,$D4 # d4 += h1*r3
1122 vpmuludq
$H0,$T2,$T2 # h0*r3
1123 vpmuludq
$H4,$T3,$T0 # h4*s3
1124 vpaddq
$T2,$D3,$D3 # d3 += h0*r3
1125 vpaddq
$T0,$D2,$D2 # d2 += h4*s3
1126 vmovdqu
16*2($inp),$T0 # load input
1127 vpmuludq
$H3,$T3,$T2 # h3*s3
1128 vpmuludq
$H2,$T3,$T3 # h2*s3
1129 vpaddq
$T2,$D1,$D1 # d1 += h3*s3
1130 vmovdqu
16*3($inp),$T1 #
1131 vpaddq
$T3,$D0,$D0 # d0 += h2*s3
1133 vpmuludq
$H2,$T4,$H2 # h2*s4
1134 vpmuludq
$H3,$T4,$H3 # h3*s4
1135 vpsrldq \
$6,$T0,$T2 # splat input
1136 vpaddq
$H2,$D1,$D1 # d1 += h2*s4
1137 vpmuludq
$H4,$T4,$H4 # h4*s4
1138 vpsrldq \
$6,$T1,$T3 #
1139 vpaddq
$H3,$D2,$H2 # h2 = d2 + h3*s4
1140 vpaddq
$H4,$D3,$H3 # h3 = d3 + h4*s4
1141 vpmuludq
-0x20(%r11),$H0,$H4 # h0*r4
1142 vpmuludq
$H1,$T4,$H0
1143 vpunpckhqdq
$T1,$T0,$T4 # 4
1144 vpaddq
$H4,$D4,$H4 # h4 = d4 + h0*r4
1145 vpaddq
$H0,$D0,$H0 # h0 = d0 + h1*s4
1147 vpunpcklqdq
$T1,$T0,$T0 # 0:1
1148 vpunpcklqdq
$T3,$T2,$T3 # 2:3
1150 #vpsrlq \$40,$T4,$T4 # 4
1151 vpsrldq \
$`40/8`,$T4,$T4 # 4
1153 vmovdqa
0x00(%rsp),$D4 # preload r0^2
1154 vpand
$MASK,$T0,$T0 # 0
1156 vpand
$MASK,$T1,$T1 # 1
1157 vpand
0(%rcx),$T4,$T4 # .Lmask24
1159 vpand
$MASK,$T2,$T2 # 2
1160 vpand
$MASK,$T3,$T3 # 3
1161 vpor
32(%rcx),$T4,$T4 # padbit, yes, always
1163 ################################################################
1164 # lazy reduction as discussed in "NEON crypto" by D.J. Bernstein
1169 vpaddq
$D3,$H4,$H4 # h3 -> h4
1173 vpaddq
$D0,$D1,$H1 # h0 -> h1
1180 vpaddq
$D1,$H2,$H2 # h1 -> h2
1184 vpaddq
$D0,$H0,$H0 # h4 -> h0
1188 vpaddq
$D2,$H3,$H3 # h2 -> h3
1192 vpaddq
$D0,$H1,$H1 # h0 -> h1
1196 vpaddq
$D3,$H4,$H4 # h3 -> h4
1201 ################################################################
1202 # multiply (inp[0:1]+hash) or inp[2:3] by r^2:r^1
1204 vpshufd \
$0x10,$D4,$D4 # r0^n, xx12 -> x1x2
1215 vmovdqa
$H2,0x20(%r11)
1216 vmovdqa
$H0,0x00(%r11)
1217 vmovdqa
$H1,0x10(%r11)
1218 vmovdqa
$H3,0x30(%r11)
1219 vmovdqa
$H4,0x40(%r11)
1221 # d4 = h4*r0 + h3*r1 + h2*r2 + h1*r3 + h0*r4
1222 # d3 = h3*r0 + h2*r1 + h1*r2 + h0*r3 + h4*5*r4
1223 # d2 = h2*r0 + h1*r1 + h0*r2 + h4*5*r3 + h3*5*r4
1224 # d1 = h1*r0 + h0*r1 + h4*5*r2 + h3*5*r3 + h2*5*r4
1225 # d0 = h0*r0 + h4*5*r1 + h3*5*r2 + h2*5*r3 + h1*5*r4
1227 vpmuludq
$T2,$D4,$D2 # d2 = h2*r0
1228 vpmuludq
$T0,$D4,$D0 # d0 = h0*r0
1229 vpshufd \
$0x10,`16*1-64`($ctx),$H2 # r1^n
1230 vpmuludq
$T1,$D4,$D1 # d1 = h1*r0
1231 vpmuludq
$T3,$D4,$D3 # d3 = h3*r0
1232 vpmuludq
$T4,$D4,$D4 # d4 = h4*r0
1234 vpmuludq
$T3,$H2,$H0 # h3*r1
1235 vpaddq
$H0,$D4,$D4 # d4 += h3*r1
1236 vpshufd \
$0x10,`16*2-64`($ctx),$H3 # s1^n
1237 vpmuludq
$T2,$H2,$H1 # h2*r1
1238 vpaddq
$H1,$D3,$D3 # d3 += h2*r1
1239 vpshufd \
$0x10,`16*3-64`($ctx),$H4 # r2^n
1240 vpmuludq
$T1,$H2,$H0 # h1*r1
1241 vpaddq
$H0,$D2,$D2 # d2 += h1*r1
1242 vpmuludq
$T0,$H2,$H2 # h0*r1
1243 vpaddq
$H2,$D1,$D1 # d1 += h0*r1
1244 vpmuludq
$T4,$H3,$H3 # h4*s1
1245 vpaddq
$H3,$D0,$D0 # d0 += h4*s1
1247 vpshufd \
$0x10,`16*4-64`($ctx),$H2 # s2^n
1248 vpmuludq
$T2,$H4,$H1 # h2*r2
1249 vpaddq
$H1,$D4,$D4 # d4 += h2*r2
1250 vpmuludq
$T1,$H4,$H0 # h1*r2
1251 vpaddq
$H0,$D3,$D3 # d3 += h1*r2
1252 vpshufd \
$0x10,`16*5-64`($ctx),$H3 # r3^n
1253 vpmuludq
$T0,$H4,$H4 # h0*r2
1254 vpaddq
$H4,$D2,$D2 # d2 += h0*r2
1255 vpmuludq
$T4,$H2,$H1 # h4*s2
1256 vpaddq
$H1,$D1,$D1 # d1 += h4*s2
1257 vpshufd \
$0x10,`16*6-64`($ctx),$H4 # s3^n
1258 vpmuludq
$T3,$H2,$H2 # h3*s2
1259 vpaddq
$H2,$D0,$D0 # d0 += h3*s2
1261 vpmuludq
$T1,$H3,$H0 # h1*r3
1262 vpaddq
$H0,$D4,$D4 # d4 += h1*r3
1263 vpmuludq
$T0,$H3,$H3 # h0*r3
1264 vpaddq
$H3,$D3,$D3 # d3 += h0*r3
1265 vpshufd \
$0x10,`16*7-64`($ctx),$H2 # r4^n
1266 vpmuludq
$T4,$H4,$H1 # h4*s3
1267 vpaddq
$H1,$D2,$D2 # d2 += h4*s3
1268 vpshufd \
$0x10,`16*8-64`($ctx),$H3 # s4^n
1269 vpmuludq
$T3,$H4,$H0 # h3*s3
1270 vpaddq
$H0,$D1,$D1 # d1 += h3*s3
1271 vpmuludq
$T2,$H4,$H4 # h2*s3
1272 vpaddq
$H4,$D0,$D0 # d0 += h2*s3
1274 vpmuludq
$T0,$H2,$H2 # h0*r4
1275 vpaddq
$H2,$D4,$D4 # h4 = d4 + h0*r4
1276 vpmuludq
$T4,$H3,$H1 # h4*s4
1277 vpaddq
$H1,$D3,$D3 # h3 = d3 + h4*s4
1278 vpmuludq
$T3,$H3,$H0 # h3*s4
1279 vpaddq
$H0,$D2,$D2 # h2 = d2 + h3*s4
1280 vpmuludq
$T2,$H3,$H1 # h2*s4
1281 vpaddq
$H1,$D1,$D1 # h1 = d1 + h2*s4
1282 vpmuludq
$T1,$H3,$H3 # h1*s4
1283 vpaddq
$H3,$D0,$D0 # h0 = d0 + h1*s4
1287 vmovdqu
16*0($inp),$H0 # load input
1288 vmovdqu
16*1($inp),$H1
1290 vpsrldq \
$6,$H0,$H2 # splat input
1292 vpunpckhqdq
$H1,$H0,$H4 # 4
1293 vpunpcklqdq
$H1,$H0,$H0 # 0:1
1294 vpunpcklqdq
$H3,$H2,$H3 # 2:3
1296 vpsrlq \
$40,$H4,$H4 # 4
1298 vpand
$MASK,$H0,$H0 # 0
1300 vpand
$MASK,$H1,$H1 # 1
1302 vpand
$MASK,$H2,$H2 # 2
1303 vpand
$MASK,$H3,$H3 # 3
1304 vpor
32(%rcx),$H4,$H4 # padbit, yes, always
1306 vpshufd \
$0x32,`16*0-64`($ctx),$T4 # r0^n, 34xx -> x3x4
1307 vpaddq
0x00(%r11),$H0,$H0
1308 vpaddq
0x10(%r11),$H1,$H1
1309 vpaddq
0x20(%r11),$H2,$H2
1310 vpaddq
0x30(%r11),$H3,$H3
1311 vpaddq
0x40(%r11),$H4,$H4
1313 ################################################################
1314 # multiply (inp[0:1]+hash) by r^4:r^3 and accumulate
1316 vpmuludq
$H0,$T4,$T0 # h0*r0
1317 vpaddq
$T0,$D0,$D0 # d0 += h0*r0
1318 vpmuludq
$H1,$T4,$T1 # h1*r0
1319 vpaddq
$T1,$D1,$D1 # d1 += h1*r0
1320 vpmuludq
$H2,$T4,$T0 # h2*r0
1321 vpaddq
$T0,$D2,$D2 # d2 += h2*r0
1322 vpshufd \
$0x32,`16*1-64`($ctx),$T2 # r1^n
1323 vpmuludq
$H3,$T4,$T1 # h3*r0
1324 vpaddq
$T1,$D3,$D3 # d3 += h3*r0
1325 vpmuludq
$H4,$T4,$T4 # h4*r0
1326 vpaddq
$T4,$D4,$D4 # d4 += h4*r0
1328 vpmuludq
$H3,$T2,$T0 # h3*r1
1329 vpaddq
$T0,$D4,$D4 # d4 += h3*r1
1330 vpshufd \
$0x32,`16*2-64`($ctx),$T3 # s1
1331 vpmuludq
$H2,$T2,$T1 # h2*r1
1332 vpaddq
$T1,$D3,$D3 # d3 += h2*r1
1333 vpshufd \
$0x32,`16*3-64`($ctx),$T4 # r2
1334 vpmuludq
$H1,$T2,$T0 # h1*r1
1335 vpaddq
$T0,$D2,$D2 # d2 += h1*r1
1336 vpmuludq
$H0,$T2,$T2 # h0*r1
1337 vpaddq
$T2,$D1,$D1 # d1 += h0*r1
1338 vpmuludq
$H4,$T3,$T3 # h4*s1
1339 vpaddq
$T3,$D0,$D0 # d0 += h4*s1
1341 vpshufd \
$0x32,`16*4-64`($ctx),$T2 # s2
1342 vpmuludq
$H2,$T4,$T1 # h2*r2
1343 vpaddq
$T1,$D4,$D4 # d4 += h2*r2
1344 vpmuludq
$H1,$T4,$T0 # h1*r2
1345 vpaddq
$T0,$D3,$D3 # d3 += h1*r2
1346 vpshufd \
$0x32,`16*5-64`($ctx),$T3 # r3
1347 vpmuludq
$H0,$T4,$T4 # h0*r2
1348 vpaddq
$T4,$D2,$D2 # d2 += h0*r2
1349 vpmuludq
$H4,$T2,$T1 # h4*s2
1350 vpaddq
$T1,$D1,$D1 # d1 += h4*s2
1351 vpshufd \
$0x32,`16*6-64`($ctx),$T4 # s3
1352 vpmuludq
$H3,$T2,$T2 # h3*s2
1353 vpaddq
$T2,$D0,$D0 # d0 += h3*s2
1355 vpmuludq
$H1,$T3,$T0 # h1*r3
1356 vpaddq
$T0,$D4,$D4 # d4 += h1*r3
1357 vpmuludq
$H0,$T3,$T3 # h0*r3
1358 vpaddq
$T3,$D3,$D3 # d3 += h0*r3
1359 vpshufd \
$0x32,`16*7-64`($ctx),$T2 # r4
1360 vpmuludq
$H4,$T4,$T1 # h4*s3
1361 vpaddq
$T1,$D2,$D2 # d2 += h4*s3
1362 vpshufd \
$0x32,`16*8-64`($ctx),$T3 # s4
1363 vpmuludq
$H3,$T4,$T0 # h3*s3
1364 vpaddq
$T0,$D1,$D1 # d1 += h3*s3
1365 vpmuludq
$H2,$T4,$T4 # h2*s3
1366 vpaddq
$T4,$D0,$D0 # d0 += h2*s3
1368 vpmuludq
$H0,$T2,$T2 # h0*r4
1369 vpaddq
$T2,$D4,$D4 # d4 += h0*r4
1370 vpmuludq
$H4,$T3,$T1 # h4*s4
1371 vpaddq
$T1,$D3,$D3 # d3 += h4*s4
1372 vpmuludq
$H3,$T3,$T0 # h3*s4
1373 vpaddq
$T0,$D2,$D2 # d2 += h3*s4
1374 vpmuludq
$H2,$T3,$T1 # h2*s4
1375 vpaddq
$T1,$D1,$D1 # d1 += h2*s4
1376 vpmuludq
$H1,$T3,$T3 # h1*s4
1377 vpaddq
$T3,$D0,$D0 # d0 += h1*s4
1380 ################################################################
1381 # horizontal addition
1394 ################################################################
1399 vpaddq
$H3,$D4,$D4 # h3 -> h4
1403 vpaddq
$H0,$D1,$D1 # h0 -> h1
1410 vpaddq
$H1,$D2,$D2 # h1 -> h2
1414 vpaddq
$H4,$D0,$D0 # h4 -> h0
1418 vpaddq
$H2,$D3,$D3 # h2 -> h3
1422 vpaddq
$H0,$D1,$D1 # h0 -> h1
1426 vpaddq
$H3,$D4,$D4 # h3 -> h4
1428 vmovd
$D0,`4*0-48-64`($ctx) # save partially reduced
1429 vmovd
$D1,`4*1-48-64`($ctx)
1430 vmovd
$D2,`4*2-48-64`($ctx)
1431 vmovd
$D3,`4*3-48-64`($ctx)
1432 vmovd
$D4,`4*4-48-64`($ctx)
1434 $code.=<<___
if ($win64);
1435 vmovdqa
0x50(%r11),%xmm6
1436 vmovdqa
0x60(%r11),%xmm7
1437 vmovdqa
0x70(%r11),%xmm8
1438 vmovdqa
0x80(%r11),%xmm9
1439 vmovdqa
0x90(%r11),%xmm10
1440 vmovdqa
0xa0(%r11),%xmm11
1441 vmovdqa
0xb0(%r11),%xmm12
1442 vmovdqa
0xc0(%r11),%xmm13
1443 vmovdqa
0xd0(%r11),%xmm14
1444 vmovdqa
0xe0(%r11),%xmm15
1448 $code.=<<___
if (!$win64);
1450 .cfi_def_cfa_register
%rsp
1457 &end_function
("poly1305_blocks_avx");
1459 &declare_function
("poly1305_emit_avx", 32, 3);
1461 cmpl \
$0,20($ctx) # is_base2_26?
1464 mov
0($ctx),%eax # load hash value base 2^26
1470 shl \
$26,%rcx # base 2^26 -> base 2^64
1486 mov
%r10,%rax # could be partially reduced, so reduce
1497 add \
$5,%r8 # compare to modulus
1501 shr \
$2,%r10 # did 130-bit value overflow?
1505 add
0($nonce),%rax # accumulate nonce
1507 mov
%rax,0($mac) # write result
1512 &end_function
("poly1305_emit_avx");
1516 my ($H0,$H1,$H2,$H3,$H4, $MASK, $T4,$T0,$T1,$T2,$T3, $D0,$D1,$D2,$D3,$D4) =
1517 map("%ymm$_",(0..15));
1520 sub poly1305_blocks_avxN
{
1522 my $suffix = $avx512 ?
"_avx512" : "";
1525 mov
20($ctx),%r8d # is_base2_26
1527 jae
.Lblocks_avx2
$suffix
1531 .Lblocks_avx2
$suffix:
1533 jz
.Lno_data_avx2
$suffix
1538 jz
.Lbase2_64_avx2
$suffix
1541 jz
.Leven_avx2
$suffix
1556 .Lblocks_avx2_body
$suffix:
1558 mov
$len,%r15 # reassign $len
1560 mov
0($ctx),$d1 # load hash value
1564 mov
24($ctx),$r0 # load r
1567 ################################# base 2^26 -> base 2^64
1569 and \
$`-1*(1<<31)`,$d1
1570 mov
$d2,$r1 # borrow $r1
1572 and \
$`-1*(1<<31)`,$d2
1586 adc \
$0,$h2 # can be partially reduced...
1588 mov \
$-4,$d2 # ... so reduce
1601 add
$r1,$s1 # s1 = r1 + (r1 >> 2)
1603 .Lbase2_26_pre_avx2
$suffix:
1604 add
0($inp),$h0 # accumulate input
1610 call __poly1305_block
1614 jnz
.Lbase2_26_pre_avx2
$suffix
1616 test
$padbit,$padbit # if $padbit is zero,
1617 jz
.Lstore_base2_64_avx2
$suffix # store hash in base 2^64 format
1619 ################################# base 2^64 -> base 2^26
1626 and \
$0x3ffffff,%rax # h[0]
1628 and \
$0x3ffffff,%rdx # h[1]
1632 and \
$0x3ffffff,$h0 # h[2]
1634 and \
$0x3ffffff,$h1 # h[3]
1638 jz
.Lstore_base2_26_avx2
$suffix
1645 jmp
.Lproceed_avx2
$suffix
1648 .Lstore_base2_64_avx2
$suffix:
1651 mov
$h2,16($ctx) # note that is_base2_26 is zeroed
1652 jmp
.Ldone_avx2
$suffix
1655 .Lstore_base2_26_avx2
$suffix:
1656 mov
%rax#d,0($ctx) # store hash value base 2^26
1675 .Lno_data_avx2
$suffix:
1676 .Lblocks_avx2_epilogue
$suffix:
1681 .Lbase2_64_avx2
$suffix:
1696 .Lbase2_64_avx2_body
$suffix:
1698 mov
$len,%r15 # reassign $len
1700 mov
24($ctx),$r0 # load r
1703 mov
0($ctx),$h0 # load hash value
1710 add
$r1,$s1 # s1 = r1 + (r1 >> 2)
1713 jz
.Linit_avx2
$suffix
1715 .Lbase2_64_pre_avx2
$suffix:
1716 add
0($inp),$h0 # accumulate input
1722 call __poly1305_block
1726 jnz
.Lbase2_64_pre_avx2
$suffix
1729 ################################# base 2^64 -> base 2^26
1736 and \
$0x3ffffff,%rax # h[0]
1738 and \
$0x3ffffff,%rdx # h[1]
1742 and \
$0x3ffffff,$h0 # h[2]
1744 and \
$0x3ffffff,$h1 # h[3]
1752 movl \
$1,20($ctx) # set is_base2_26
1754 call __poly1305_init_avx
1756 .Lproceed_avx2
$suffix:
1757 mov
%r15,$len # restore $len
1759 $code.=<<___
if (!$kernel);
1760 mov OPENSSL_ia32cap_P
+8(%rip),%r9d
1761 mov \
$`(1<<31|1<<30|1<<16)`,%r11d
1776 .Lbase2_64_avx2_epilogue
$suffix:
1777 jmp
.Ldo_avx2
$suffix
1784 $code.=<<___
if (!$kernel);
1785 mov OPENSSL_ia32cap_P
+8(%rip),%r9d
1788 vmovd
4*0($ctx),%x#$H0 # load hash value base 2^26
1789 vmovd
4*1($ctx),%x#$H1
1790 vmovd
4*2($ctx),%x#$H2
1791 vmovd
4*3($ctx),%x#$H3
1792 vmovd
4*4($ctx),%x#$H4
1796 $code.=<<___
if (!$kernel && $avx>2);
1800 test \
$`1<<16`,%r9d # check for AVX512F
1802 .Lskip_avx512
$suffix:
1804 $code.=<<___
if ($avx > 2 && $avx512 && $kernel);
1808 $code.=<<___
if (!$win64);
1810 .cfi_def_cfa_register
%r10
1813 $code.=<<___
if ($win64);
1816 vmovdqa
%xmm6,-0xb0(%r10)
1817 vmovdqa
%xmm7,-0xa0(%r10)
1818 vmovdqa
%xmm8,-0x90(%r10)
1819 vmovdqa
%xmm9,-0x80(%r10)
1820 vmovdqa
%xmm10,-0x70(%r10)
1821 vmovdqa
%xmm11,-0x60(%r10)
1822 vmovdqa
%xmm12,-0x50(%r10)
1823 vmovdqa
%xmm13,-0x40(%r10)
1824 vmovdqa
%xmm14,-0x30(%r10)
1825 vmovdqa
%xmm15,-0x20(%r10)
1826 .Ldo_avx2_body
$suffix:
1829 lea
.Lconst
(%rip),%rcx
1830 lea
48+64($ctx),$ctx # size optimization
1831 vmovdqa
96(%rcx),$T0 # .Lpermd_avx2
1833 # expand and copy pre-calculated table to stack
1834 vmovdqu
`16*0-64`($ctx),%x#$T2
1836 vmovdqu
`16*1-64`($ctx),%x#$T3
1837 vmovdqu
`16*2-64`($ctx),%x#$T4
1838 vmovdqu
`16*3-64`($ctx),%x#$D0
1839 vmovdqu
`16*4-64`($ctx),%x#$D1
1840 vmovdqu
`16*5-64`($ctx),%x#$D2
1841 lea
0x90(%rsp),%rax # size optimization
1842 vmovdqu
`16*6-64`($ctx),%x#$D3
1843 vpermd
$T2,$T0,$T2 # 00003412 -> 14243444
1844 vmovdqu
`16*7-64`($ctx),%x#$D4
1846 vmovdqu
`16*8-64`($ctx),%x#$MASK
1848 vmovdqa
$T2,0x00(%rsp)
1850 vmovdqa
$T3,0x20-0x90(%rax)
1852 vmovdqa
$T4,0x40-0x90(%rax)
1854 vmovdqa
$D0,0x60-0x90(%rax)
1856 vmovdqa
$D1,0x80-0x90(%rax)
1858 vmovdqa
$D2,0xa0-0x90(%rax)
1859 vpermd
$MASK,$T0,$MASK
1860 vmovdqa
$D3,0xc0-0x90(%rax)
1861 vmovdqa
$D4,0xe0-0x90(%rax)
1862 vmovdqa
$MASK,0x100-0x90(%rax)
1863 vmovdqa
64(%rcx),$MASK # .Lmask26
1865 ################################################################
1867 vmovdqu
16*0($inp),%x#$T0
1868 vmovdqu
16*1($inp),%x#$T1
1869 vinserti128 \
$1,16*2($inp),$T0,$T0
1870 vinserti128 \
$1,16*3($inp),$T1,$T1
1873 vpsrldq \
$6,$T0,$T2 # splat input
1875 vpunpckhqdq
$T1,$T0,$T4 # 4
1876 vpunpcklqdq
$T3,$T2,$T2 # 2:3
1877 vpunpcklqdq
$T1,$T0,$T0 # 0:1
1882 vpsrlq \
$40,$T4,$T4 # 4
1883 vpand
$MASK,$T2,$T2 # 2
1884 vpand
$MASK,$T0,$T0 # 0
1885 vpand
$MASK,$T1,$T1 # 1
1886 vpand
$MASK,$T3,$T3 # 3
1887 vpor
32(%rcx),$T4,$T4 # padbit, yes, always
1889 vpaddq
$H2,$T2,$H2 # accumulate input
1891 jz
.Ltail_avx2
$suffix
1892 jmp
.Loop_avx2
$suffix
1896 ################################################################
1897 # ((inp[0]*r^4+inp[4])*r^4+inp[ 8])*r^4
1898 # ((inp[1]*r^4+inp[5])*r^4+inp[ 9])*r^3
1899 # ((inp[2]*r^4+inp[6])*r^4+inp[10])*r^2
1900 # ((inp[3]*r^4+inp[7])*r^4+inp[11])*r^1
1901 # \________/\__________/
1902 ################################################################
1903 #vpaddq $H2,$T2,$H2 # accumulate input
1905 vmovdqa
`32*0`(%rsp),$T0 # r0^4
1907 vmovdqa
`32*1`(%rsp),$T1 # r1^4
1909 vmovdqa
`32*3`(%rsp),$T2 # r2^4
1911 vmovdqa
`32*6-0x90`(%rax),$T3 # s3^4
1912 vmovdqa
`32*8-0x90`(%rax),$S4 # s4^4
1914 # d4 = h4*r0 + h3*r1 + h2*r2 + h1*r3 + h0*r4
1915 # d3 = h3*r0 + h2*r1 + h1*r2 + h0*r3 + h4*5*r4
1916 # d2 = h2*r0 + h1*r1 + h0*r2 + h4*5*r3 + h3*5*r4
1917 # d1 = h1*r0 + h0*r1 + h4*5*r2 + h3*5*r3 + h2*5*r4
1918 # d0 = h0*r0 + h4*5*r1 + h3*5*r2 + h2*5*r3 + h1*5*r4
1920 # however, as h2 is "chronologically" first one available pull
1921 # corresponding operations up, so it's
1923 # d4 = h2*r2 + h4*r0 + h3*r1 + h1*r3 + h0*r4
1924 # d3 = h2*r1 + h3*r0 + h1*r2 + h0*r3 + h4*5*r4
1925 # d2 = h2*r0 + h1*r1 + h0*r2 + h4*5*r3 + h3*5*r4
1926 # d1 = h2*5*r4 + h1*r0 + h0*r1 + h4*5*r2 + h3*5*r3
1927 # d0 = h2*5*r3 + h0*r0 + h4*5*r1 + h3*5*r2 + h1*5*r4
1929 vpmuludq
$H2,$T0,$D2 # d2 = h2*r0
1930 vpmuludq
$H2,$T1,$D3 # d3 = h2*r1
1931 vpmuludq
$H2,$T2,$D4 # d4 = h2*r2
1932 vpmuludq
$H2,$T3,$D0 # d0 = h2*s3
1933 vpmuludq
$H2,$S4,$D1 # d1 = h2*s4
1935 vpmuludq
$H0,$T1,$T4 # h0*r1
1936 vpmuludq
$H1,$T1,$H2 # h1*r1, borrow $H2 as temp
1937 vpaddq
$T4,$D1,$D1 # d1 += h0*r1
1938 vpaddq
$H2,$D2,$D2 # d2 += h1*r1
1939 vpmuludq
$H3,$T1,$T4 # h3*r1
1940 vpmuludq
`32*2`(%rsp),$H4,$H2 # h4*s1
1941 vpaddq
$T4,$D4,$D4 # d4 += h3*r1
1942 vpaddq
$H2,$D0,$D0 # d0 += h4*s1
1943 vmovdqa
`32*4-0x90`(%rax),$T1 # s2
1945 vpmuludq
$H0,$T0,$T4 # h0*r0
1946 vpmuludq
$H1,$T0,$H2 # h1*r0
1947 vpaddq
$T4,$D0,$D0 # d0 += h0*r0
1948 vpaddq
$H2,$D1,$D1 # d1 += h1*r0
1949 vpmuludq
$H3,$T0,$T4 # h3*r0
1950 vpmuludq
$H4,$T0,$H2 # h4*r0
1951 vmovdqu
16*0($inp),%x#$T0 # load input
1952 vpaddq
$T4,$D3,$D3 # d3 += h3*r0
1953 vpaddq
$H2,$D4,$D4 # d4 += h4*r0
1954 vinserti128 \
$1,16*2($inp),$T0,$T0
1956 vpmuludq
$H3,$T1,$T4 # h3*s2
1957 vpmuludq
$H4,$T1,$H2 # h4*s2
1958 vmovdqu
16*1($inp),%x#$T1
1959 vpaddq
$T4,$D0,$D0 # d0 += h3*s2
1960 vpaddq
$H2,$D1,$D1 # d1 += h4*s2
1961 vmovdqa
`32*5-0x90`(%rax),$H2 # r3
1962 vpmuludq
$H1,$T2,$T4 # h1*r2
1963 vpmuludq
$H0,$T2,$T2 # h0*r2
1964 vpaddq
$T4,$D3,$D3 # d3 += h1*r2
1965 vpaddq
$T2,$D2,$D2 # d2 += h0*r2
1966 vinserti128 \
$1,16*3($inp),$T1,$T1
1969 vpmuludq
$H1,$H2,$T4 # h1*r3
1970 vpmuludq
$H0,$H2,$H2 # h0*r3
1971 vpsrldq \
$6,$T0,$T2 # splat input
1972 vpaddq
$T4,$D4,$D4 # d4 += h1*r3
1973 vpaddq
$H2,$D3,$D3 # d3 += h0*r3
1974 vpmuludq
$H3,$T3,$T4 # h3*s3
1975 vpmuludq
$H4,$T3,$H2 # h4*s3
1977 vpaddq
$T4,$D1,$D1 # d1 += h3*s3
1978 vpaddq
$H2,$D2,$D2 # d2 += h4*s3
1979 vpunpckhqdq
$T1,$T0,$T4 # 4
1981 vpmuludq
$H3,$S4,$H3 # h3*s4
1982 vpmuludq
$H4,$S4,$H4 # h4*s4
1983 vpunpcklqdq
$T1,$T0,$T0 # 0:1
1984 vpaddq
$H3,$D2,$H2 # h2 = d2 + h3*r4
1985 vpaddq
$H4,$D3,$H3 # h3 = d3 + h4*r4
1986 vpunpcklqdq
$T3,$T2,$T3 # 2:3
1987 vpmuludq
`32*7-0x90`(%rax),$H0,$H4 # h0*r4
1988 vpmuludq
$H1,$S4,$H0 # h1*s4
1989 vmovdqa
64(%rcx),$MASK # .Lmask26
1990 vpaddq
$H4,$D4,$H4 # h4 = d4 + h0*r4
1991 vpaddq
$H0,$D0,$H0 # h0 = d0 + h1*s4
1993 ################################################################
1994 # lazy reduction (interleaved with tail of input splat)
1998 vpaddq
$D3,$H4,$H4 # h3 -> h4
2002 vpaddq
$D0,$D1,$H1 # h0 -> h1
2011 vpaddq
$D1,$H2,$H2 # h1 -> h2
2015 vpaddq
$D4,$H0,$H0 # h4 -> h0
2017 vpand
$MASK,$T2,$T2 # 2
2022 vpaddq
$D2,$H3,$H3 # h2 -> h3
2024 vpaddq
$T2,$H2,$H2 # modulo-scheduled
2029 vpaddq
$D0,$H1,$H1 # h0 -> h1
2031 vpsrlq \
$40,$T4,$T4 # 4
2035 vpaddq
$D3,$H4,$H4 # h3 -> h4
2037 vpand
$MASK,$T0,$T0 # 0
2038 vpand
$MASK,$T1,$T1 # 1
2039 vpand
$MASK,$T3,$T3 # 3
2040 vpor
32(%rcx),$T4,$T4 # padbit, yes, always
2043 jnz
.Loop_avx2
$suffix
2047 ################################################################
2048 # while above multiplications were by r^4 in all lanes, in last
2049 # iteration we multiply least significant lane by r^4 and most
2050 # significant one by r, so copy of above except that references
2051 # to the precomputed table are displaced by 4...
2053 #vpaddq $H2,$T2,$H2 # accumulate input
2055 vmovdqu
`32*0+4`(%rsp),$T0 # r0^4
2057 vmovdqu
`32*1+4`(%rsp),$T1 # r1^4
2059 vmovdqu
`32*3+4`(%rsp),$T2 # r2^4
2061 vmovdqu
`32*6+4-0x90`(%rax),$T3 # s3^4
2062 vmovdqu
`32*8+4-0x90`(%rax),$S4 # s4^4
2064 vpmuludq
$H2,$T0,$D2 # d2 = h2*r0
2065 vpmuludq
$H2,$T1,$D3 # d3 = h2*r1
2066 vpmuludq
$H2,$T2,$D4 # d4 = h2*r2
2067 vpmuludq
$H2,$T3,$D0 # d0 = h2*s3
2068 vpmuludq
$H2,$S4,$D1 # d1 = h2*s4
2070 vpmuludq
$H0,$T1,$T4 # h0*r1
2071 vpmuludq
$H1,$T1,$H2 # h1*r1
2072 vpaddq
$T4,$D1,$D1 # d1 += h0*r1
2073 vpaddq
$H2,$D2,$D2 # d2 += h1*r1
2074 vpmuludq
$H3,$T1,$T4 # h3*r1
2075 vpmuludq
`32*2+4`(%rsp),$H4,$H2 # h4*s1
2076 vpaddq
$T4,$D4,$D4 # d4 += h3*r1
2077 vpaddq
$H2,$D0,$D0 # d0 += h4*s1
2079 vpmuludq
$H0,$T0,$T4 # h0*r0
2080 vpmuludq
$H1,$T0,$H2 # h1*r0
2081 vpaddq
$T4,$D0,$D0 # d0 += h0*r0
2082 vmovdqu
`32*4+4-0x90`(%rax),$T1 # s2
2083 vpaddq
$H2,$D1,$D1 # d1 += h1*r0
2084 vpmuludq
$H3,$T0,$T4 # h3*r0
2085 vpmuludq
$H4,$T0,$H2 # h4*r0
2086 vpaddq
$T4,$D3,$D3 # d3 += h3*r0
2087 vpaddq
$H2,$D4,$D4 # d4 += h4*r0
2089 vpmuludq
$H3,$T1,$T4 # h3*s2
2090 vpmuludq
$H4,$T1,$H2 # h4*s2
2091 vpaddq
$T4,$D0,$D0 # d0 += h3*s2
2092 vpaddq
$H2,$D1,$D1 # d1 += h4*s2
2093 vmovdqu
`32*5+4-0x90`(%rax),$H2 # r3
2094 vpmuludq
$H1,$T2,$T4 # h1*r2
2095 vpmuludq
$H0,$T2,$T2 # h0*r2
2096 vpaddq
$T4,$D3,$D3 # d3 += h1*r2
2097 vpaddq
$T2,$D2,$D2 # d2 += h0*r2
2099 vpmuludq
$H1,$H2,$T4 # h1*r3
2100 vpmuludq
$H0,$H2,$H2 # h0*r3
2101 vpaddq
$T4,$D4,$D4 # d4 += h1*r3
2102 vpaddq
$H2,$D3,$D3 # d3 += h0*r3
2103 vpmuludq
$H3,$T3,$T4 # h3*s3
2104 vpmuludq
$H4,$T3,$H2 # h4*s3
2105 vpaddq
$T4,$D1,$D1 # d1 += h3*s3
2106 vpaddq
$H2,$D2,$D2 # d2 += h4*s3
2108 vpmuludq
$H3,$S4,$H3 # h3*s4
2109 vpmuludq
$H4,$S4,$H4 # h4*s4
2110 vpaddq
$H3,$D2,$H2 # h2 = d2 + h3*r4
2111 vpaddq
$H4,$D3,$H3 # h3 = d3 + h4*r4
2112 vpmuludq
`32*7+4-0x90`(%rax),$H0,$H4 # h0*r4
2113 vpmuludq
$H1,$S4,$H0 # h1*s4
2114 vmovdqa
64(%rcx),$MASK # .Lmask26
2115 vpaddq
$H4,$D4,$H4 # h4 = d4 + h0*r4
2116 vpaddq
$H0,$D0,$H0 # h0 = d0 + h1*s4
2118 ################################################################
2119 # horizontal addition
2132 vpermq \
$0x2,$H3,$T3
2133 vpermq \
$0x2,$H4,$T4
2134 vpermq \
$0x2,$H0,$T0
2135 vpermq \
$0x2,$D1,$T1
2136 vpermq \
$0x2,$H2,$T2
2143 ################################################################
2148 vpaddq
$D3,$H4,$H4 # h3 -> h4
2152 vpaddq
$D0,$D1,$H1 # h0 -> h1
2159 vpaddq
$D1,$H2,$H2 # h1 -> h2
2163 vpaddq
$D4,$H0,$H0 # h4 -> h0
2167 vpaddq
$D2,$H3,$H3 # h2 -> h3
2171 vpaddq
$D0,$H1,$H1 # h0 -> h1
2175 vpaddq
$D3,$H4,$H4 # h3 -> h4
2177 vmovd
%x#$H0,`4*0-48-64`($ctx)# save partially reduced
2178 vmovd
%x#$H1,`4*1-48-64`($ctx)
2179 vmovd
%x#$H2,`4*2-48-64`($ctx)
2180 vmovd
%x#$H3,`4*3-48-64`($ctx)
2181 vmovd
%x#$H4,`4*4-48-64`($ctx)
2183 $code.=<<___
if ($win64);
2184 vmovdqa
-0xb0(%r10),%xmm6
2185 vmovdqa
-0xa0(%r10),%xmm7
2186 vmovdqa
-0x90(%r10),%xmm8
2187 vmovdqa
-0x80(%r10),%xmm9
2188 vmovdqa
-0x70(%r10),%xmm10
2189 vmovdqa
-0x60(%r10),%xmm11
2190 vmovdqa
-0x50(%r10),%xmm12
2191 vmovdqa
-0x40(%r10),%xmm13
2192 vmovdqa
-0x30(%r10),%xmm14
2193 vmovdqa
-0x20(%r10),%xmm15
2195 .Ldo_avx2_epilogue
$suffix:
2197 $code.=<<___
if (!$win64);
2199 .cfi_def_cfa_register
%rsp
2206 if($avx > 2 && $avx512) {
2207 my ($R0,$R1,$R2,$R3,$R4, $S1,$S2,$S3,$S4) = map("%zmm$_",(16..24));
2208 my ($M0,$M1,$M2,$M3,$M4) = map("%zmm$_",(25..29));
2209 my $PADBIT="%zmm30";
2211 map(s/%y/%z/,($T4,$T0,$T1,$T2,$T3)); # switch to %zmm domain
2212 map(s/%y/%z/,($D0,$D1,$D2,$D3,$D4));
2213 map(s/%y/%z/,($H0,$H1,$H2,$H3,$H4));
2214 map(s/%y/%z/,($MASK));
2222 $code.=<<___
if (!$win64);
2224 .cfi_def_cfa_register
%r10
2227 $code.=<<___
if ($win64);
2230 vmovdqa
%xmm6,-0xb0(%r10)
2231 vmovdqa
%xmm7,-0xa0(%r10)
2232 vmovdqa
%xmm8,-0x90(%r10)
2233 vmovdqa
%xmm9,-0x80(%r10)
2234 vmovdqa
%xmm10,-0x70(%r10)
2235 vmovdqa
%xmm11,-0x60(%r10)
2236 vmovdqa
%xmm12,-0x50(%r10)
2237 vmovdqa
%xmm13,-0x40(%r10)
2238 vmovdqa
%xmm14,-0x30(%r10)
2239 vmovdqa
%xmm15,-0x20(%r10)
2243 lea
.Lconst
(%rip),%rcx
2244 lea
48+64($ctx),$ctx # size optimization
2245 vmovdqa
96(%rcx),%y#$T2 # .Lpermd_avx2
2247 # expand pre-calculated table
2248 vmovdqu
`16*0-64`($ctx),%x#$D0 # will become expanded ${R0}
2250 vmovdqu
`16*1-64`($ctx),%x#$D1 # will become ... ${R1}
2252 vmovdqu
`16*2-64`($ctx),%x#$T0 # ... ${S1}
2253 vmovdqu
`16*3-64`($ctx),%x#$D2 # ... ${R2}
2254 vmovdqu
`16*4-64`($ctx),%x#$T1 # ... ${S2}
2255 vmovdqu
`16*5-64`($ctx),%x#$D3 # ... ${R3}
2256 vmovdqu
`16*6-64`($ctx),%x#$T3 # ... ${S3}
2257 vmovdqu
`16*7-64`($ctx),%x#$D4 # ... ${R4}
2258 vmovdqu
`16*8-64`($ctx),%x#$T4 # ... ${S4}
2259 vpermd
$D0,$T2,$R0 # 00003412 -> 14243444
2260 vpbroadcastq
64(%rcx),$MASK # .Lmask26
2264 vmovdqa64
$R0,0x00(%rsp){%k2} # save in case $len%128 != 0
2265 vpsrlq \
$32,$R0,$T0 # 14243444 -> 01020304
2267 vmovdqu64
$R1,0x00(%rsp,%rax){%k2}
2270 vmovdqa64
$S1,0x40(%rsp){%k2}
2273 vmovdqu64
$R2,0x40(%rsp,%rax){%k2}
2275 vmovdqa64
$S2,0x80(%rsp){%k2}
2276 vmovdqu64
$R3,0x80(%rsp,%rax){%k2}
2277 vmovdqa64
$S3,0xc0(%rsp){%k2}
2278 vmovdqu64
$R4,0xc0(%rsp,%rax){%k2}
2279 vmovdqa64
$S4,0x100(%rsp){%k2}
2281 ################################################################
2282 # calculate 5th through 8th powers of the key
2284 # d0 = r0'*r0 + r1'*5*r4 + r2'*5*r3 + r3'*5*r2 + r4'*5*r1
2285 # d1 = r0'*r1 + r1'*r0 + r2'*5*r4 + r3'*5*r3 + r4'*5*r2
2286 # d2 = r0'*r2 + r1'*r1 + r2'*r0 + r3'*5*r4 + r4'*5*r3
2287 # d3 = r0'*r3 + r1'*r2 + r2'*r1 + r3'*r0 + r4'*5*r4
2288 # d4 = r0'*r4 + r1'*r3 + r2'*r2 + r3'*r1 + r4'*r0
2290 vpmuludq
$T0,$R0,$D0 # d0 = r0'*r0
2291 vpmuludq
$T0,$R1,$D1 # d1 = r0'*r1
2292 vpmuludq
$T0,$R2,$D2 # d2 = r0'*r2
2293 vpmuludq
$T0,$R3,$D3 # d3 = r0'*r3
2294 vpmuludq
$T0,$R4,$D4 # d4 = r0'*r4
2297 vpmuludq
$T1,$S4,$M0
2298 vpmuludq
$T1,$R0,$M1
2299 vpmuludq
$T1,$R1,$M2
2300 vpmuludq
$T1,$R2,$M3
2301 vpmuludq
$T1,$R3,$M4
2303 vpaddq
$M0,$D0,$D0 # d0 += r1'*5*r4
2304 vpaddq
$M1,$D1,$D1 # d1 += r1'*r0
2305 vpaddq
$M2,$D2,$D2 # d2 += r1'*r1
2306 vpaddq
$M3,$D3,$D3 # d3 += r1'*r2
2307 vpaddq
$M4,$D4,$D4 # d4 += r1'*r3
2309 vpmuludq
$T2,$S3,$M0
2310 vpmuludq
$T2,$S4,$M1
2311 vpmuludq
$T2,$R1,$M3
2312 vpmuludq
$T2,$R2,$M4
2313 vpmuludq
$T2,$R0,$M2
2315 vpaddq
$M0,$D0,$D0 # d0 += r2'*5*r3
2316 vpaddq
$M1,$D1,$D1 # d1 += r2'*5*r4
2317 vpaddq
$M3,$D3,$D3 # d3 += r2'*r1
2318 vpaddq
$M4,$D4,$D4 # d4 += r2'*r2
2319 vpaddq
$M2,$D2,$D2 # d2 += r2'*r0
2321 vpmuludq
$T3,$S2,$M0
2322 vpmuludq
$T3,$R0,$M3
2323 vpmuludq
$T3,$R1,$M4
2324 vpmuludq
$T3,$S3,$M1
2325 vpmuludq
$T3,$S4,$M2
2326 vpaddq
$M0,$D0,$D0 # d0 += r3'*5*r2
2327 vpaddq
$M3,$D3,$D3 # d3 += r3'*r0
2328 vpaddq
$M4,$D4,$D4 # d4 += r3'*r1
2329 vpaddq
$M1,$D1,$D1 # d1 += r3'*5*r3
2330 vpaddq
$M2,$D2,$D2 # d2 += r3'*5*r4
2332 vpmuludq
$T4,$S4,$M3
2333 vpmuludq
$T4,$R0,$M4
2334 vpmuludq
$T4,$S1,$M0
2335 vpmuludq
$T4,$S2,$M1
2336 vpmuludq
$T4,$S3,$M2
2337 vpaddq
$M3,$D3,$D3 # d3 += r2'*5*r4
2338 vpaddq
$M4,$D4,$D4 # d4 += r2'*r0
2339 vpaddq
$M0,$D0,$D0 # d0 += r2'*5*r1
2340 vpaddq
$M1,$D1,$D1 # d1 += r2'*5*r2
2341 vpaddq
$M2,$D2,$D2 # d2 += r2'*5*r3
2343 ################################################################
2345 vmovdqu64
16*0($inp),%z#$T3
2346 vmovdqu64
16*4($inp),%z#$T4
2349 ################################################################
2353 vpandq
$MASK,$D3,$D3
2354 vpaddq
$M3,$D4,$D4 # d3 -> d4
2357 vpandq
$MASK,$D0,$D0
2358 vpaddq
$M0,$D1,$D1 # d0 -> d1
2361 vpandq
$MASK,$D4,$D4
2364 vpandq
$MASK,$D1,$D1
2365 vpaddq
$M1,$D2,$D2 # d1 -> d2
2369 vpaddq
$M4,$D0,$D0 # d4 -> d0
2372 vpandq
$MASK,$D2,$D2
2373 vpaddq
$M2,$D3,$D3 # d2 -> d3
2376 vpandq
$MASK,$D0,$D0
2377 vpaddq
$M0,$D1,$D1 # d0 -> d1
2380 vpandq
$MASK,$D3,$D3
2381 vpaddq
$M3,$D4,$D4 # d3 -> d4
2383 ################################################################
2384 # at this point we have 14243444 in $R0-$S4 and 05060708 in
2387 vpunpcklqdq
$T4,$T3,$T0 # transpose input
2388 vpunpckhqdq
$T4,$T3,$T4
2390 # ... since input 64-bit lanes are ordered as 73625140, we could
2391 # "vperm" it to 76543210 (here and in each loop iteration), *or*
2392 # we could just flow along, hence the goal for $R0-$S4 is
2393 # 1858286838784888 ...
2395 vmovdqa32
128(%rcx),$M0 # .Lpermd_avx512:
2399 vpermd
$R0,$M0,$R0 # 14243444 -> 1---2---3---4---
2405 vpermd
$D0,$M0,${R0
}{%k1} # 05060708 -> 1858286838784888
2406 vpermd
$D1,$M0,${R1
}{%k1}
2407 vpermd
$D2,$M0,${R2
}{%k1}
2408 vpermd
$D3,$M0,${R3
}{%k1}
2409 vpermd
$D4,$M0,${R4
}{%k1}
2411 vpslld \
$2,$R1,$S1 # *5
2420 vpbroadcastq
32(%rcx),$PADBIT # .L129
2422 vpsrlq \
$52,$T0,$T2 # splat input
2427 vpsrlq \
$40,$T4,$T4 # 4
2428 vpandq
$MASK,$T2,$T2 # 2
2429 vpandq
$MASK,$T0,$T0 # 0
2430 #vpandq $MASK,$T1,$T1 # 1
2431 #vpandq $MASK,$T3,$T3 # 3
2432 #vporq $PADBIT,$T4,$T4 # padbit, yes, always
2434 vpaddq
$H2,$T2,$H2 # accumulate input
2441 ################################################################
2442 # ((inp[0]*r^8+inp[ 8])*r^8+inp[16])*r^8
2443 # ((inp[1]*r^8+inp[ 9])*r^8+inp[17])*r^7
2444 # ((inp[2]*r^8+inp[10])*r^8+inp[18])*r^6
2445 # ((inp[3]*r^8+inp[11])*r^8+inp[19])*r^5
2446 # ((inp[4]*r^8+inp[12])*r^8+inp[20])*r^4
2447 # ((inp[5]*r^8+inp[13])*r^8+inp[21])*r^3
2448 # ((inp[6]*r^8+inp[14])*r^8+inp[22])*r^2
2449 # ((inp[7]*r^8+inp[15])*r^8+inp[23])*r^1
2450 # \________/\___________/
2451 ################################################################
2452 #vpaddq $H2,$T2,$H2 # accumulate input
2454 # d4 = h4*r0 + h3*r1 + h2*r2 + h1*r3 + h0*r4
2455 # d3 = h3*r0 + h2*r1 + h1*r2 + h0*r3 + h4*5*r4
2456 # d2 = h2*r0 + h1*r1 + h0*r2 + h4*5*r3 + h3*5*r4
2457 # d1 = h1*r0 + h0*r1 + h4*5*r2 + h3*5*r3 + h2*5*r4
2458 # d0 = h0*r0 + h4*5*r1 + h3*5*r2 + h2*5*r3 + h1*5*r4
2460 # however, as h2 is "chronologically" first one available pull
2461 # corresponding operations up, so it's
2463 # d3 = h2*r1 + h0*r3 + h1*r2 + h3*r0 + h4*5*r4
2464 # d4 = h2*r2 + h0*r4 + h1*r3 + h3*r1 + h4*r0
2465 # d0 = h2*5*r3 + h0*r0 + h1*5*r4 + h3*5*r2 + h4*5*r1
2466 # d1 = h2*5*r4 + h0*r1 + h1*r0 + h3*5*r3 + h4*5*r2
2467 # d2 = h2*r0 + h0*r2 + h1*r1 + h3*5*r4 + h4*5*r3
2469 vpmuludq
$H2,$R1,$D3 # d3 = h2*r1
2471 vpmuludq
$H2,$R2,$D4 # d4 = h2*r2
2472 vpandq
$MASK,$T1,$T1 # 1
2473 vpmuludq
$H2,$S3,$D0 # d0 = h2*s3
2474 vpandq
$MASK,$T3,$T3 # 3
2475 vpmuludq
$H2,$S4,$D1 # d1 = h2*s4
2476 vporq
$PADBIT,$T4,$T4 # padbit, yes, always
2477 vpmuludq
$H2,$R0,$D2 # d2 = h2*r0
2478 vpaddq
$H1,$T1,$H1 # accumulate input
2482 vmovdqu64
16*0($inp),$T3 # load input
2483 vmovdqu64
16*4($inp),$T4
2485 vpmuludq
$H0,$R3,$M3
2486 vpmuludq
$H0,$R4,$M4
2487 vpmuludq
$H0,$R0,$M0
2488 vpmuludq
$H0,$R1,$M1
2489 vpaddq
$M3,$D3,$D3 # d3 += h0*r3
2490 vpaddq
$M4,$D4,$D4 # d4 += h0*r4
2491 vpaddq
$M0,$D0,$D0 # d0 += h0*r0
2492 vpaddq
$M1,$D1,$D1 # d1 += h0*r1
2494 vpmuludq
$H1,$R2,$M3
2495 vpmuludq
$H1,$R3,$M4
2496 vpmuludq
$H1,$S4,$M0
2497 vpmuludq
$H0,$R2,$M2
2498 vpaddq
$M3,$D3,$D3 # d3 += h1*r2
2499 vpaddq
$M4,$D4,$D4 # d4 += h1*r3
2500 vpaddq
$M0,$D0,$D0 # d0 += h1*s4
2501 vpaddq
$M2,$D2,$D2 # d2 += h0*r2
2503 vpunpcklqdq
$T4,$T3,$T0 # transpose input
2504 vpunpckhqdq
$T4,$T3,$T4
2506 vpmuludq
$H3,$R0,$M3
2507 vpmuludq
$H3,$R1,$M4
2508 vpmuludq
$H1,$R0,$M1
2509 vpmuludq
$H1,$R1,$M2
2510 vpaddq
$M3,$D3,$D3 # d3 += h3*r0
2511 vpaddq
$M4,$D4,$D4 # d4 += h3*r1
2512 vpaddq
$M1,$D1,$D1 # d1 += h1*r0
2513 vpaddq
$M2,$D2,$D2 # d2 += h1*r1
2515 vpmuludq
$H4,$S4,$M3
2516 vpmuludq
$H4,$R0,$M4
2517 vpmuludq
$H3,$S2,$M0
2518 vpmuludq
$H3,$S3,$M1
2519 vpaddq
$M3,$D3,$D3 # d3 += h4*s4
2520 vpmuludq
$H3,$S4,$M2
2521 vpaddq
$M4,$D4,$D4 # d4 += h4*r0
2522 vpaddq
$M0,$D0,$D0 # d0 += h3*s2
2523 vpaddq
$M1,$D1,$D1 # d1 += h3*s3
2524 vpaddq
$M2,$D2,$D2 # d2 += h3*s4
2526 vpmuludq
$H4,$S1,$M0
2527 vpmuludq
$H4,$S2,$M1
2528 vpmuludq
$H4,$S3,$M2
2529 vpaddq
$M0,$D0,$H0 # h0 = d0 + h4*s1
2530 vpaddq
$M1,$D1,$H1 # h1 = d2 + h4*s2
2531 vpaddq
$M2,$D2,$H2 # h2 = d3 + h4*s3
2533 ################################################################
2534 # lazy reduction (interleaved with input splat)
2536 vpsrlq \
$52,$T0,$T2 # splat input
2540 vpandq
$MASK,$D3,$D3
2541 vpaddq
$H3,$D4,$H4 # h3 -> h4
2546 vpandq
$MASK,$H0,$H0
2547 vpaddq
$D0,$H1,$H1 # h0 -> h1
2549 vpandq
$MASK,$T2,$T2 # 2
2552 vpandq
$MASK,$H4,$H4
2555 vpandq
$MASK,$H1,$H1
2556 vpaddq
$D1,$H2,$H2 # h1 -> h2
2560 vpaddq
$D4,$H0,$H0 # h4 -> h0
2562 vpaddq
$T2,$H2,$H2 # modulo-scheduled
2566 vpandq
$MASK,$H2,$H2
2567 vpaddq
$D2,$D3,$H3 # h2 -> h3
2572 vpandq
$MASK,$H0,$H0
2573 vpaddq
$D0,$H1,$H1 # h0 -> h1
2575 vpsrlq \
$40,$T4,$T4 # 4
2578 vpandq
$MASK,$H3,$H3
2579 vpaddq
$D3,$H4,$H4 # h3 -> h4
2581 vpandq
$MASK,$T0,$T0 # 0
2582 #vpandq $MASK,$T1,$T1 # 1
2583 #vpandq $MASK,$T3,$T3 # 3
2584 #vporq $PADBIT,$T4,$T4 # padbit, yes, always
2590 ################################################################
2591 # while above multiplications were by r^8 in all lanes, in last
2592 # iteration we multiply least significant lane by r^8 and most
2593 # significant one by r, that's why table gets shifted...
2595 vpsrlq \
$32,$R0,$R0 # 0105020603070408
2605 ################################################################
2606 # load either next or last 64 byte of input
2607 lea
($inp,$len),$inp
2609 #vpaddq $H2,$T2,$H2 # accumulate input
2612 vpmuludq
$H2,$R1,$D3 # d3 = h2*r1
2613 vpmuludq
$H2,$R2,$D4 # d4 = h2*r2
2614 vpmuludq
$H2,$S3,$D0 # d0 = h2*s3
2615 vpandq
$MASK,$T1,$T1 # 1
2616 vpmuludq
$H2,$S4,$D1 # d1 = h2*s4
2617 vpandq
$MASK,$T3,$T3 # 3
2618 vpmuludq
$H2,$R0,$D2 # d2 = h2*r0
2619 vporq
$PADBIT,$T4,$T4 # padbit, yes, always
2620 vpaddq
$H1,$T1,$H1 # accumulate input
2624 vmovdqu
16*0($inp),%x#$T0
2625 vpmuludq
$H0,$R3,$M3
2626 vpmuludq
$H0,$R4,$M4
2627 vpmuludq
$H0,$R0,$M0
2628 vpmuludq
$H0,$R1,$M1
2629 vpaddq
$M3,$D3,$D3 # d3 += h0*r3
2630 vpaddq
$M4,$D4,$D4 # d4 += h0*r4
2631 vpaddq
$M0,$D0,$D0 # d0 += h0*r0
2632 vpaddq
$M1,$D1,$D1 # d1 += h0*r1
2634 vmovdqu
16*1($inp),%x#$T1
2635 vpmuludq
$H1,$R2,$M3
2636 vpmuludq
$H1,$R3,$M4
2637 vpmuludq
$H1,$S4,$M0
2638 vpmuludq
$H0,$R2,$M2
2639 vpaddq
$M3,$D3,$D3 # d3 += h1*r2
2640 vpaddq
$M4,$D4,$D4 # d4 += h1*r3
2641 vpaddq
$M0,$D0,$D0 # d0 += h1*s4
2642 vpaddq
$M2,$D2,$D2 # d2 += h0*r2
2644 vinserti128 \
$1,16*2($inp),%y#$T0,%y#$T0
2645 vpmuludq
$H3,$R0,$M3
2646 vpmuludq
$H3,$R1,$M4
2647 vpmuludq
$H1,$R0,$M1
2648 vpmuludq
$H1,$R1,$M2
2649 vpaddq
$M3,$D3,$D3 # d3 += h3*r0
2650 vpaddq
$M4,$D4,$D4 # d4 += h3*r1
2651 vpaddq
$M1,$D1,$D1 # d1 += h1*r0
2652 vpaddq
$M2,$D2,$D2 # d2 += h1*r1
2654 vinserti128 \
$1,16*3($inp),%y#$T1,%y#$T1
2655 vpmuludq
$H4,$S4,$M3
2656 vpmuludq
$H4,$R0,$M4
2657 vpmuludq
$H3,$S2,$M0
2658 vpmuludq
$H3,$S3,$M1
2659 vpmuludq
$H3,$S4,$M2
2660 vpaddq
$M3,$D3,$H3 # h3 = d3 + h4*s4
2661 vpaddq
$M4,$D4,$D4 # d4 += h4*r0
2662 vpaddq
$M0,$D0,$D0 # d0 += h3*s2
2663 vpaddq
$M1,$D1,$D1 # d1 += h3*s3
2664 vpaddq
$M2,$D2,$D2 # d2 += h3*s4
2666 vpmuludq
$H4,$S1,$M0
2667 vpmuludq
$H4,$S2,$M1
2668 vpmuludq
$H4,$S3,$M2
2669 vpaddq
$M0,$D0,$H0 # h0 = d0 + h4*s1
2670 vpaddq
$M1,$D1,$H1 # h1 = d2 + h4*s2
2671 vpaddq
$M2,$D2,$H2 # h2 = d3 + h4*s3
2673 ################################################################
2674 # horizontal addition
2677 vpermq \
$0xb1,$H3,$D3
2678 vpermq \
$0xb1,$D4,$H4
2679 vpermq \
$0xb1,$H0,$D0
2680 vpermq \
$0xb1,$H1,$D1
2681 vpermq \
$0xb1,$H2,$D2
2689 vpermq \
$0x2,$H3,$D3
2690 vpermq \
$0x2,$H4,$D4
2691 vpermq \
$0x2,$H0,$D0
2692 vpermq \
$0x2,$H1,$D1
2693 vpermq \
$0x2,$H2,$D2
2700 vextracti64x4 \
$0x1,$H3,%y#$D3
2701 vextracti64x4 \
$0x1,$H4,%y#$D4
2702 vextracti64x4 \
$0x1,$H0,%y#$D0
2703 vextracti64x4 \
$0x1,$H1,%y#$D1
2704 vextracti64x4 \
$0x1,$H2,%y#$D2
2705 vpaddq
$D3,$H3,${H3
}{%k3}{z
} # keep single qword in case
2706 vpaddq
$D4,$H4,${H4
}{%k3}{z
} # it's passed to .Ltail_avx2
2707 vpaddq
$D0,$H0,${H0
}{%k3}{z
}
2708 vpaddq
$D1,$H1,${H1
}{%k3}{z
}
2709 vpaddq
$D2,$H2,${H2
}{%k3}{z
}
2711 map(s/%z/%y/,($T0,$T1,$T2,$T3,$T4, $PADBIT));
2712 map(s/%z/%y/,($H0,$H1,$H2,$H3,$H4, $D0,$D1,$D2,$D3,$D4, $MASK));
2714 ################################################################
2715 # lazy reduction (interleaved with input splat)
2719 vpsrldq \
$6,$T0,$T2 # splat input
2721 vpunpckhqdq
$T1,$T0,$T4 # 4
2722 vpaddq
$D3,$H4,$H4 # h3 -> h4
2726 vpunpcklqdq
$T3,$T2,$T2 # 2:3
2727 vpunpcklqdq
$T1,$T0,$T0 # 0:1
2728 vpaddq
$D0,$H1,$H1 # h0 -> h1
2737 vpaddq
$D1,$H2,$H2 # h1 -> h2
2742 vpsrlq \
$40,$T4,$T4 # 4
2743 vpaddq
$D4,$H0,$H0 # h4 -> h0
2747 vpand
$MASK,$T2,$T2 # 2
2748 vpand
$MASK,$T0,$T0 # 0
2749 vpaddq
$D2,$H3,$H3 # h2 -> h3
2753 vpaddq
$H2,$T2,$H2 # accumulate input for .Ltail_avx2
2754 vpand
$MASK,$T1,$T1 # 1
2755 vpaddq
$D0,$H1,$H1 # h0 -> h1
2759 vpand
$MASK,$T3,$T3 # 3
2760 vpor
32(%rcx),$T4,$T4 # padbit, yes, always
2761 vpaddq
$D3,$H4,$H4 # h3 -> h4
2763 lea
0x90(%rsp),%rax # size optimization for .Ltail_avx2
2765 jnz
.Ltail_avx2
$suffix
2767 vpsubq
$T2,$H2,$H2 # undo input accumulation
2768 vmovd
%x#$H0,`4*0-48-64`($ctx)# save partially reduced
2769 vmovd
%x#$H1,`4*1-48-64`($ctx)
2770 vmovd
%x#$H2,`4*2-48-64`($ctx)
2771 vmovd
%x#$H3,`4*3-48-64`($ctx)
2772 vmovd
%x#$H4,`4*4-48-64`($ctx)
2775 $code.=<<___
if ($win64);
2776 movdqa
-0xb0(%r10),%xmm6
2777 movdqa
-0xa0(%r10),%xmm7
2778 movdqa
-0x90(%r10),%xmm8
2779 movdqa
-0x80(%r10),%xmm9
2780 movdqa
-0x70(%r10),%xmm10
2781 movdqa
-0x60(%r10),%xmm11
2782 movdqa
-0x50(%r10),%xmm12
2783 movdqa
-0x40(%r10),%xmm13
2784 movdqa
-0x30(%r10),%xmm14
2785 movdqa
-0x20(%r10),%xmm15
2787 .Ldo_avx512_epilogue
:
2789 $code.=<<___
if (!$win64);
2791 .cfi_def_cfa_register
%rsp
2802 &declare_function
("poly1305_blocks_avx2", 32, 4);
2803 poly1305_blocks_avxN
(0);
2804 &end_function
("poly1305_blocks_avx2");
2806 #######################################################################
2808 # On entry we have input length divisible by 64. But since inner loop
2809 # processes 128 bytes per iteration, cases when length is not divisible
2810 # by 128 are handled by passing tail 64 bytes to .Ltail_avx2. For this
2811 # reason stack layout is kept identical to poly1305_blocks_avx2. If not
2812 # for this tail, we wouldn't have to even allocate stack frame...
2815 $code .= "#ifdef CONFIG_AS_AVX512\n";
2818 &declare_function
("poly1305_blocks_avx512", 32, 4);
2819 poly1305_blocks_avxN
(1);
2820 &end_function
("poly1305_blocks_avx512");
2823 $code .= "#endif\n";
2826 if (!$kernel && $avx>3) {
2827 ########################################################################
2828 # VPMADD52 version using 2^44 radix.
2830 # One can argue that base 2^52 would be more natural. Well, even though
2831 # some operations would be more natural, one has to recognize couple of
2832 # things. Base 2^52 doesn't provide advantage over base 2^44 if you look
2833 # at amount of multiply-n-accumulate operations. Secondly, it makes it
2834 # impossible to pre-compute multiples of 5 [referred to as s[]/sN in
2835 # reference implementations], which means that more such operations
2836 # would have to be performed in inner loop, which in turn makes critical
2837 # path longer. In other words, even though base 2^44 reduction might
2838 # look less elegant, overall critical path is actually shorter...
2840 ########################################################################
2841 # Layout of opaque area is following.
2843 # unsigned __int64 h[3]; # current hash value base 2^44
2844 # unsigned __int64 s[2]; # key value*20 base 2^44
2845 # unsigned __int64 r[3]; # key value base 2^44
2846 # struct { unsigned __int64 r^1, r^3, r^2, r^4; } R[4];
2847 # # r^n positions reflect
2848 # # placement in register, not
2849 # # memory, R[3] is R[1]*20
2852 .type poly1305_init_base2_44
,\
@function,3
2854 poly1305_init_base2_44
:
2856 mov
%rax,0($ctx) # initialize hash value
2861 lea poly1305_blocks_vpmadd52
(%rip),%r10
2862 lea poly1305_emit_base2_44
(%rip),%r11
2864 mov \
$0x0ffffffc0fffffff,%rax
2865 mov \
$0x0ffffffc0ffffffc,%rcx
2867 mov \
$0x00000fffffffffff,%r8
2869 mov \
$0x00000fffffffffff,%r9
2872 mov
%r8,40($ctx) # r0
2875 mov
%rax,48($ctx) # r1
2876 lea
(%rax,%rax,4),%rax # *5
2877 mov
%rcx,56($ctx) # r2
2878 shl \
$2,%rax # magic <<2
2879 lea
(%rcx,%rcx,4),%rcx # *5
2880 shl \
$2,%rcx # magic <<2
2881 mov
%rax,24($ctx) # s1
2882 mov
%rcx,32($ctx) # s2
2883 movq \
$-1,64($ctx) # write impossible value
2885 $code.=<<___
if ($flavour !~ /elf32/);
2889 $code.=<<___
if ($flavour =~ /elf32/);
2896 .size poly1305_init_base2_44
,.-poly1305_init_base2_44
2899 my ($H0,$H1,$H2,$r2r1r0,$r1r0s2,$r0s2s1,$Dlo,$Dhi) = map("%ymm$_",(0..5,16,17));
2900 my ($T0,$inp_permd,$inp_shift,$PAD) = map("%ymm$_",(18..21));
2901 my ($reduc_mask,$reduc_rght,$reduc_left) = map("%ymm$_",(22..25));
2904 .type poly1305_blocks_vpmadd52
,\
@function,4
2906 poly1305_blocks_vpmadd52
:
2908 jz
.Lno_data_vpmadd52
# too short
2911 mov
64($ctx),%r8 # peek on power of the key
2913 # if powers of the key are not calculated yet, process up to 3
2914 # blocks with this single-block subroutine, otherwise ensure that
2915 # length is divisible by 2 blocks and pass the rest down to next
2920 cmp \
$4,$len # is input long
2922 test
%r8,%r8 # is power value impossible?
2925 and $len,%rax # is input of favourable length?
2926 jz
.Lblocks_vpmadd52_4x
2932 lea
.L2_44_inp_permd
(%rip),%r10
2935 vmovq
$padbit,%x#$PAD
2936 vmovdqa64
0(%r10),$inp_permd # .L2_44_inp_permd
2937 vmovdqa64
32(%r10),$inp_shift # .L2_44_inp_shift
2938 vpermq \
$0xcf,$PAD,$PAD
2939 vmovdqa64
64(%r10),$reduc_mask # .L2_44_mask
2941 vmovdqu64
0($ctx),${Dlo
}{%k7}{z
} # load hash value
2942 vmovdqu64
40($ctx),${r2r1r0
}{%k7}{z
} # load keys
2943 vmovdqu64
32($ctx),${r1r0s2
}{%k7}{z
}
2944 vmovdqu64
24($ctx),${r0s2s1
}{%k7}{z
}
2946 vmovdqa64
96(%r10),$reduc_rght # .L2_44_shift_rgt
2947 vmovdqa64
128(%r10),$reduc_left # .L2_44_shift_lft
2953 vmovdqu32
0($inp),%x#$T0 # load input as ----3210
2956 vpermd
$T0,$inp_permd,$T0 # ----3210 -> --322110
2957 vpsrlvq
$inp_shift,$T0,$T0
2958 vpandq
$reduc_mask,$T0,$T0
2961 vpaddq
$T0,$Dlo,$Dlo # accumulate input
2963 vpermq \
$0,$Dlo,${H0
}{%k7}{z
} # smash hash value
2964 vpermq \
$0b01010101,$Dlo,${H1
}{%k7}{z
}
2965 vpermq \
$0b10101010,$Dlo,${H2
}{%k7}{z
}
2967 vpxord
$Dlo,$Dlo,$Dlo
2968 vpxord
$Dhi,$Dhi,$Dhi
2970 vpmadd52luq
$r2r1r0,$H0,$Dlo
2971 vpmadd52huq
$r2r1r0,$H0,$Dhi
2973 vpmadd52luq
$r1r0s2,$H1,$Dlo
2974 vpmadd52huq
$r1r0s2,$H1,$Dhi
2976 vpmadd52luq
$r0s2s1,$H2,$Dlo
2977 vpmadd52huq
$r0s2s1,$H2,$Dhi
2979 vpsrlvq
$reduc_rght,$Dlo,$T0 # 0 in topmost qword
2980 vpsllvq
$reduc_left,$Dhi,$Dhi # 0 in topmost qword
2981 vpandq
$reduc_mask,$Dlo,$Dlo
2983 vpaddq
$T0,$Dhi,$Dhi
2985 vpermq \
$0b10010011,$Dhi,$Dhi # 0 in lowest qword
2987 vpaddq
$Dhi,$Dlo,$Dlo # note topmost qword :-)
2989 vpsrlvq
$reduc_rght,$Dlo,$T0 # 0 in topmost word
2990 vpandq
$reduc_mask,$Dlo,$Dlo
2992 vpermq \
$0b10010011,$T0,$T0
2994 vpaddq
$T0,$Dlo,$Dlo
2996 vpermq \
$0b10010011,$Dlo,${T0
}{%k1}{z
}
2998 vpaddq
$T0,$Dlo,$Dlo
3001 vpaddq
$T0,$Dlo,$Dlo
3006 vmovdqu64
$Dlo,0($ctx){%k7} # store hash value
3009 jnz
.Lblocks_vpmadd52_4x
3013 .size poly1305_blocks_vpmadd52
,.-poly1305_blocks_vpmadd52
3017 ########################################################################
3018 # As implied by its name 4x subroutine processes 4 blocks in parallel
3019 # (but handles even 4*n+2 blocks lengths). It takes up to 4th key power
3020 # and is handled in 256-bit %ymm registers.
3022 my ($H0,$H1,$H2,$R0,$R1,$R2,$S1,$S2) = map("%ymm$_",(0..5,16,17));
3023 my ($D0lo,$D0hi,$D1lo,$D1hi,$D2lo,$D2hi) = map("%ymm$_",(18..23));
3024 my ($T0,$T1,$T2,$T3,$mask44,$mask42,$tmp,$PAD) = map("%ymm$_",(24..31));
3027 .type poly1305_blocks_vpmadd52_4x
,\
@function,4
3029 poly1305_blocks_vpmadd52_4x
:
3031 jz
.Lno_data_vpmadd52_4x
# too short
3034 mov
64($ctx),%r8 # peek on power of the key
3036 .Lblocks_vpmadd52_4x
:
3037 vpbroadcastq
$padbit,$PAD
3039 vmovdqa64
.Lx_mask44
(%rip),$mask44
3041 vmovdqa64
.Lx_mask42
(%rip),$mask42
3042 kmovw
%eax,%k1 # used in 2x path
3044 test
%r8,%r8 # is power value impossible?
3045 js
.Linit_vpmadd52
# if it is, then init R[4]
3047 vmovq
0($ctx),%x#$H0 # load current hash value
3048 vmovq
8($ctx),%x#$H1
3049 vmovq
16($ctx),%x#$H2
3051 test \
$3,$len # is length 4*n+2?
3052 jnz
.Lblocks_vpmadd52_2x_do
3054 .Lblocks_vpmadd52_4x_do
:
3055 vpbroadcastq
64($ctx),$R0 # load 4th power of the key
3056 vpbroadcastq
96($ctx),$R1
3057 vpbroadcastq
128($ctx),$R2
3058 vpbroadcastq
160($ctx),$S1
3060 .Lblocks_vpmadd52_4x_key_loaded
:
3061 vpsllq \
$2,$R2,$S2 # S2 = R2*5*4
3065 test \
$7,$len # is len 8*n?
3066 jz
.Lblocks_vpmadd52_8x
3068 vmovdqu64
16*0($inp),$T2 # load data
3069 vmovdqu64
16*2($inp),$T3
3072 vpunpcklqdq
$T3,$T2,$T1 # transpose data
3073 vpunpckhqdq
$T3,$T2,$T3
3075 # at this point 64-bit lanes are ordered as 3-1-2-0
3077 vpsrlq \
$24,$T3,$T2 # splat the data
3079 vpaddq
$T2,$H2,$H2 # accumulate input
3080 vpandq
$mask44,$T1,$T0
3084 vpandq
$mask44,$T1,$T1
3087 jz
.Ltail_vpmadd52_4x
3088 jmp
.Loop_vpmadd52_4x
3093 vmovq
24($ctx),%x#$S1 # load key
3094 vmovq
56($ctx),%x#$H2
3095 vmovq
32($ctx),%x#$S2
3096 vmovq
40($ctx),%x#$R0
3097 vmovq
48($ctx),%x#$R1
3105 .Lmul_init_vpmadd52
:
3106 vpxorq
$D0lo,$D0lo,$D0lo
3107 vpmadd52luq
$H2,$S1,$D0lo
3108 vpxorq
$D0hi,$D0hi,$D0hi
3109 vpmadd52huq
$H2,$S1,$D0hi
3110 vpxorq
$D1lo,$D1lo,$D1lo
3111 vpmadd52luq
$H2,$S2,$D1lo
3112 vpxorq
$D1hi,$D1hi,$D1hi
3113 vpmadd52huq
$H2,$S2,$D1hi
3114 vpxorq
$D2lo,$D2lo,$D2lo
3115 vpmadd52luq
$H2,$R0,$D2lo
3116 vpxorq
$D2hi,$D2hi,$D2hi
3117 vpmadd52huq
$H2,$R0,$D2hi
3119 vpmadd52luq
$H0,$R0,$D0lo
3120 vpmadd52huq
$H0,$R0,$D0hi
3121 vpmadd52luq
$H0,$R1,$D1lo
3122 vpmadd52huq
$H0,$R1,$D1hi
3123 vpmadd52luq
$H0,$R2,$D2lo
3124 vpmadd52huq
$H0,$R2,$D2hi
3126 vpmadd52luq
$H1,$S2,$D0lo
3127 vpmadd52huq
$H1,$S2,$D0hi
3128 vpmadd52luq
$H1,$R0,$D1lo
3129 vpmadd52huq
$H1,$R0,$D1hi
3130 vpmadd52luq
$H1,$R1,$D2lo
3131 vpmadd52huq
$H1,$R1,$D2hi
3133 ################################################################
3135 vpsrlq \
$44,$D0lo,$tmp
3136 vpsllq \
$8,$D0hi,$D0hi
3137 vpandq
$mask44,$D0lo,$H0
3138 vpaddq
$tmp,$D0hi,$D0hi
3140 vpaddq
$D0hi,$D1lo,$D1lo
3142 vpsrlq \
$44,$D1lo,$tmp
3143 vpsllq \
$8,$D1hi,$D1hi
3144 vpandq
$mask44,$D1lo,$H1
3145 vpaddq
$tmp,$D1hi,$D1hi
3147 vpaddq
$D1hi,$D2lo,$D2lo
3149 vpsrlq \
$42,$D2lo,$tmp
3150 vpsllq \
$10,$D2hi,$D2hi
3151 vpandq
$mask42,$D2lo,$H2
3152 vpaddq
$tmp,$D2hi,$D2hi
3154 vpaddq
$D2hi,$H0,$H0
3155 vpsllq \
$2,$D2hi,$D2hi
3157 vpaddq
$D2hi,$H0,$H0
3159 vpsrlq \
$44,$H0,$tmp # additional step
3160 vpandq
$mask44,$H0,$H0
3165 jz
.Ldone_init_vpmadd52
3167 vpunpcklqdq
$R1,$H1,$R1 # 1,2
3168 vpbroadcastq
%x#$H1,%x#$H1 # 2,2
3169 vpunpcklqdq
$R2,$H2,$R2
3170 vpbroadcastq
%x#$H2,%x#$H2
3171 vpunpcklqdq
$R0,$H0,$R0
3172 vpbroadcastq
%x#$H0,%x#$H0
3174 vpsllq \
$2,$R1,$S1 # S1 = R1*5*4
3175 vpsllq \
$2,$R2,$S2 # S2 = R2*5*4
3181 jmp
.Lmul_init_vpmadd52
3185 .Ldone_init_vpmadd52
:
3186 vinserti128 \
$1,%x#$R1,$H1,$R1 # 1,2,3,4
3187 vinserti128 \
$1,%x#$R2,$H2,$R2
3188 vinserti128 \
$1,%x#$R0,$H0,$R0
3190 vpermq \
$0b11011000,$R1,$R1 # 1,3,2,4
3191 vpermq \
$0b11011000,$R2,$R2
3192 vpermq \
$0b11011000,$R0,$R0
3194 vpsllq \
$2,$R1,$S1 # S1 = R1*5*4
3198 vmovq
0($ctx),%x#$H0 # load current hash value
3199 vmovq
8($ctx),%x#$H1
3200 vmovq
16($ctx),%x#$H2
3202 test \
$3,$len # is length 4*n+2?
3203 jnz
.Ldone_init_vpmadd52_2x
3205 vmovdqu64
$R0,64($ctx) # save key powers
3206 vpbroadcastq
%x#$R0,$R0 # broadcast 4th power
3207 vmovdqu64
$R1,96($ctx)
3208 vpbroadcastq
%x#$R1,$R1
3209 vmovdqu64
$R2,128($ctx)
3210 vpbroadcastq
%x#$R2,$R2
3211 vmovdqu64
$S1,160($ctx)
3212 vpbroadcastq
%x#$S1,$S1
3214 jmp
.Lblocks_vpmadd52_4x_key_loaded
3218 .Ldone_init_vpmadd52_2x
:
3219 vmovdqu64
$R0,64($ctx) # save key powers
3220 vpsrldq \
$8,$R0,$R0 # 0-1-0-2
3221 vmovdqu64
$R1,96($ctx)
3223 vmovdqu64
$R2,128($ctx)
3225 vmovdqu64
$S1,160($ctx)
3227 jmp
.Lblocks_vpmadd52_2x_key_loaded
3231 .Lblocks_vpmadd52_2x_do
:
3232 vmovdqu64
128+8($ctx),${R2
}{%k1}{z
}# load 2nd and 1st key powers
3233 vmovdqu64
160+8($ctx),${S1
}{%k1}{z
}
3234 vmovdqu64
64+8($ctx),${R0
}{%k1}{z
}
3235 vmovdqu64
96+8($ctx),${R1
}{%k1}{z
}
3237 .Lblocks_vpmadd52_2x_key_loaded
:
3238 vmovdqu64
16*0($inp),$T2 # load data
3242 vpunpcklqdq
$T3,$T2,$T1 # transpose data
3243 vpunpckhqdq
$T3,$T2,$T3
3245 # at this point 64-bit lanes are ordered as x-1-x-0
3247 vpsrlq \
$24,$T3,$T2 # splat the data
3249 vpaddq
$T2,$H2,$H2 # accumulate input
3250 vpandq
$mask44,$T1,$T0
3254 vpandq
$mask44,$T1,$T1
3256 jmp
.Ltail_vpmadd52_2x
3261 #vpaddq $T2,$H2,$H2 # accumulate input
3265 vpxorq
$D0lo,$D0lo,$D0lo
3266 vpmadd52luq
$H2,$S1,$D0lo
3267 vpxorq
$D0hi,$D0hi,$D0hi
3268 vpmadd52huq
$H2,$S1,$D0hi
3269 vpxorq
$D1lo,$D1lo,$D1lo
3270 vpmadd52luq
$H2,$S2,$D1lo
3271 vpxorq
$D1hi,$D1hi,$D1hi
3272 vpmadd52huq
$H2,$S2,$D1hi
3273 vpxorq
$D2lo,$D2lo,$D2lo
3274 vpmadd52luq
$H2,$R0,$D2lo
3275 vpxorq
$D2hi,$D2hi,$D2hi
3276 vpmadd52huq
$H2,$R0,$D2hi
3278 vmovdqu64
16*0($inp),$T2 # load data
3279 vmovdqu64
16*2($inp),$T3
3281 vpmadd52luq
$H0,$R0,$D0lo
3282 vpmadd52huq
$H0,$R0,$D0hi
3283 vpmadd52luq
$H0,$R1,$D1lo
3284 vpmadd52huq
$H0,$R1,$D1hi
3285 vpmadd52luq
$H0,$R2,$D2lo
3286 vpmadd52huq
$H0,$R2,$D2hi
3288 vpunpcklqdq
$T3,$T2,$T1 # transpose data
3289 vpunpckhqdq
$T3,$T2,$T3
3290 vpmadd52luq
$H1,$S2,$D0lo
3291 vpmadd52huq
$H1,$S2,$D0hi
3292 vpmadd52luq
$H1,$R0,$D1lo
3293 vpmadd52huq
$H1,$R0,$D1hi
3294 vpmadd52luq
$H1,$R1,$D2lo
3295 vpmadd52huq
$H1,$R1,$D2hi
3297 ################################################################
3298 # partial reduction (interleaved with data splat)
3299 vpsrlq \
$44,$D0lo,$tmp
3300 vpsllq \
$8,$D0hi,$D0hi
3301 vpandq
$mask44,$D0lo,$H0
3302 vpaddq
$tmp,$D0hi,$D0hi
3306 vpaddq
$D0hi,$D1lo,$D1lo
3308 vpsrlq \
$44,$D1lo,$tmp
3309 vpsllq \
$8,$D1hi,$D1hi
3310 vpandq
$mask44,$D1lo,$H1
3311 vpaddq
$tmp,$D1hi,$D1hi
3313 vpandq
$mask44,$T1,$T0
3316 vpaddq
$D1hi,$D2lo,$D2lo
3318 vpsrlq \
$42,$D2lo,$tmp
3319 vpsllq \
$10,$D2hi,$D2hi
3320 vpandq
$mask42,$D2lo,$H2
3321 vpaddq
$tmp,$D2hi,$D2hi
3323 vpaddq
$T2,$H2,$H2 # accumulate input
3324 vpaddq
$D2hi,$H0,$H0
3325 vpsllq \
$2,$D2hi,$D2hi
3327 vpaddq
$D2hi,$H0,$H0
3329 vpandq
$mask44,$T1,$T1
3331 vpsrlq \
$44,$H0,$tmp # additional step
3332 vpandq
$mask44,$H0,$H0
3336 sub \
$4,$len # len-=64
3337 jnz
.Loop_vpmadd52_4x
3340 vmovdqu64
128($ctx),$R2 # load all key powers
3341 vmovdqu64
160($ctx),$S1
3342 vmovdqu64
64($ctx),$R0
3343 vmovdqu64
96($ctx),$R1
3346 vpsllq \
$2,$R2,$S2 # S2 = R2*5*4
3350 #vpaddq $T2,$H2,$H2 # accumulate input
3354 vpxorq
$D0lo,$D0lo,$D0lo
3355 vpmadd52luq
$H2,$S1,$D0lo
3356 vpxorq
$D0hi,$D0hi,$D0hi
3357 vpmadd52huq
$H2,$S1,$D0hi
3358 vpxorq
$D1lo,$D1lo,$D1lo
3359 vpmadd52luq
$H2,$S2,$D1lo
3360 vpxorq
$D1hi,$D1hi,$D1hi
3361 vpmadd52huq
$H2,$S2,$D1hi
3362 vpxorq
$D2lo,$D2lo,$D2lo
3363 vpmadd52luq
$H2,$R0,$D2lo
3364 vpxorq
$D2hi,$D2hi,$D2hi
3365 vpmadd52huq
$H2,$R0,$D2hi
3367 vpmadd52luq
$H0,$R0,$D0lo
3368 vpmadd52huq
$H0,$R0,$D0hi
3369 vpmadd52luq
$H0,$R1,$D1lo
3370 vpmadd52huq
$H0,$R1,$D1hi
3371 vpmadd52luq
$H0,$R2,$D2lo
3372 vpmadd52huq
$H0,$R2,$D2hi
3374 vpmadd52luq
$H1,$S2,$D0lo
3375 vpmadd52huq
$H1,$S2,$D0hi
3376 vpmadd52luq
$H1,$R0,$D1lo
3377 vpmadd52huq
$H1,$R0,$D1hi
3378 vpmadd52luq
$H1,$R1,$D2lo
3379 vpmadd52huq
$H1,$R1,$D2hi
3381 ################################################################
3382 # horizontal addition
3386 vpsrldq \
$8,$D0lo,$T0
3387 vpsrldq \
$8,$D0hi,$H0
3388 vpsrldq \
$8,$D1lo,$T1
3389 vpsrldq \
$8,$D1hi,$H1
3390 vpaddq
$T0,$D0lo,$D0lo
3391 vpaddq
$H0,$D0hi,$D0hi
3392 vpsrldq \
$8,$D2lo,$T2
3393 vpsrldq \
$8,$D2hi,$H2
3394 vpaddq
$T1,$D1lo,$D1lo
3395 vpaddq
$H1,$D1hi,$D1hi
3396 vpermq \
$0x2,$D0lo,$T0
3397 vpermq \
$0x2,$D0hi,$H0
3398 vpaddq
$T2,$D2lo,$D2lo
3399 vpaddq
$H2,$D2hi,$D2hi
3401 vpermq \
$0x2,$D1lo,$T1
3402 vpermq \
$0x2,$D1hi,$H1
3403 vpaddq
$T0,$D0lo,${D0lo
}{%k1}{z
}
3404 vpaddq
$H0,$D0hi,${D0hi
}{%k1}{z
}
3405 vpermq \
$0x2,$D2lo,$T2
3406 vpermq \
$0x2,$D2hi,$H2
3407 vpaddq
$T1,$D1lo,${D1lo
}{%k1}{z
}
3408 vpaddq
$H1,$D1hi,${D1hi
}{%k1}{z
}
3409 vpaddq
$T2,$D2lo,${D2lo
}{%k1}{z
}
3410 vpaddq
$H2,$D2hi,${D2hi
}{%k1}{z
}
3412 ################################################################
3414 vpsrlq \
$44,$D0lo,$tmp
3415 vpsllq \
$8,$D0hi,$D0hi
3416 vpandq
$mask44,$D0lo,$H0
3417 vpaddq
$tmp,$D0hi,$D0hi
3419 vpaddq
$D0hi,$D1lo,$D1lo
3421 vpsrlq \
$44,$D1lo,$tmp
3422 vpsllq \
$8,$D1hi,$D1hi
3423 vpandq
$mask44,$D1lo,$H1
3424 vpaddq
$tmp,$D1hi,$D1hi
3426 vpaddq
$D1hi,$D2lo,$D2lo
3428 vpsrlq \
$42,$D2lo,$tmp
3429 vpsllq \
$10,$D2hi,$D2hi
3430 vpandq
$mask42,$D2lo,$H2
3431 vpaddq
$tmp,$D2hi,$D2hi
3433 vpaddq
$D2hi,$H0,$H0
3434 vpsllq \
$2,$D2hi,$D2hi
3436 vpaddq
$D2hi,$H0,$H0
3438 vpsrlq \
$44,$H0,$tmp # additional step
3439 vpandq
$mask44,$H0,$H0
3442 # at this point $len is
3443 # either 4*n+2 or 0...
3444 sub \
$2,$len # len-=32
3445 ja
.Lblocks_vpmadd52_4x_do
3447 vmovq
%x#$H0,0($ctx)
3448 vmovq
%x#$H1,8($ctx)
3449 vmovq
%x#$H2,16($ctx)
3452 .Lno_data_vpmadd52_4x
:
3454 .size poly1305_blocks_vpmadd52_4x
,.-poly1305_blocks_vpmadd52_4x
3458 ########################################################################
3459 # As implied by its name 8x subroutine processes 8 blocks in parallel...
3460 # This is intermediate version, as it's used only in cases when input
3461 # length is either 8*n, 8*n+1 or 8*n+2...
3463 my ($H0,$H1,$H2,$R0,$R1,$R2,$S1,$S2) = map("%ymm$_",(0..5,16,17));
3464 my ($D0lo,$D0hi,$D1lo,$D1hi,$D2lo,$D2hi) = map("%ymm$_",(18..23));
3465 my ($T0,$T1,$T2,$T3,$mask44,$mask42,$tmp,$PAD) = map("%ymm$_",(24..31));
3466 my ($RR0,$RR1,$RR2,$SS1,$SS2) = map("%ymm$_",(6..10));
3469 .type poly1305_blocks_vpmadd52_8x
,\
@function,4
3471 poly1305_blocks_vpmadd52_8x
:
3473 jz
.Lno_data_vpmadd52_8x
# too short
3476 mov
64($ctx),%r8 # peek on power of the key
3478 vmovdqa64
.Lx_mask44
(%rip),$mask44
3479 vmovdqa64
.Lx_mask42
(%rip),$mask42
3481 test
%r8,%r8 # is power value impossible?
3482 js
.Linit_vpmadd52
# if it is, then init R[4]
3484 vmovq
0($ctx),%x#$H0 # load current hash value
3485 vmovq
8($ctx),%x#$H1
3486 vmovq
16($ctx),%x#$H2
3488 .Lblocks_vpmadd52_8x
:
3489 ################################################################
3490 # fist we calculate more key powers
3492 vmovdqu64
128($ctx),$R2 # load 1-3-2-4 powers
3493 vmovdqu64
160($ctx),$S1
3494 vmovdqu64
64($ctx),$R0
3495 vmovdqu64
96($ctx),$R1
3497 vpsllq \
$2,$R2,$S2 # S2 = R2*5*4
3501 vpbroadcastq
%x#$R2,$RR2 # broadcast 4th power
3502 vpbroadcastq
%x#$R0,$RR0
3503 vpbroadcastq
%x#$R1,$RR1
3505 vpxorq
$D0lo,$D0lo,$D0lo
3506 vpmadd52luq
$RR2,$S1,$D0lo
3507 vpxorq
$D0hi,$D0hi,$D0hi
3508 vpmadd52huq
$RR2,$S1,$D0hi
3509 vpxorq
$D1lo,$D1lo,$D1lo
3510 vpmadd52luq
$RR2,$S2,$D1lo
3511 vpxorq
$D1hi,$D1hi,$D1hi
3512 vpmadd52huq
$RR2,$S2,$D1hi
3513 vpxorq
$D2lo,$D2lo,$D2lo
3514 vpmadd52luq
$RR2,$R0,$D2lo
3515 vpxorq
$D2hi,$D2hi,$D2hi
3516 vpmadd52huq
$RR2,$R0,$D2hi
3518 vpmadd52luq
$RR0,$R0,$D0lo
3519 vpmadd52huq
$RR0,$R0,$D0hi
3520 vpmadd52luq
$RR0,$R1,$D1lo
3521 vpmadd52huq
$RR0,$R1,$D1hi
3522 vpmadd52luq
$RR0,$R2,$D2lo
3523 vpmadd52huq
$RR0,$R2,$D2hi
3525 vpmadd52luq
$RR1,$S2,$D0lo
3526 vpmadd52huq
$RR1,$S2,$D0hi
3527 vpmadd52luq
$RR1,$R0,$D1lo
3528 vpmadd52huq
$RR1,$R0,$D1hi
3529 vpmadd52luq
$RR1,$R1,$D2lo
3530 vpmadd52huq
$RR1,$R1,$D2hi
3532 ################################################################
3534 vpsrlq \
$44,$D0lo,$tmp
3535 vpsllq \
$8,$D0hi,$D0hi
3536 vpandq
$mask44,$D0lo,$RR0
3537 vpaddq
$tmp,$D0hi,$D0hi
3539 vpaddq
$D0hi,$D1lo,$D1lo
3541 vpsrlq \
$44,$D1lo,$tmp
3542 vpsllq \
$8,$D1hi,$D1hi
3543 vpandq
$mask44,$D1lo,$RR1
3544 vpaddq
$tmp,$D1hi,$D1hi
3546 vpaddq
$D1hi,$D2lo,$D2lo
3548 vpsrlq \
$42,$D2lo,$tmp
3549 vpsllq \
$10,$D2hi,$D2hi
3550 vpandq
$mask42,$D2lo,$RR2
3551 vpaddq
$tmp,$D2hi,$D2hi
3553 vpaddq
$D2hi,$RR0,$RR0
3554 vpsllq \
$2,$D2hi,$D2hi
3556 vpaddq
$D2hi,$RR0,$RR0
3558 vpsrlq \
$44,$RR0,$tmp # additional step
3559 vpandq
$mask44,$RR0,$RR0
3561 vpaddq
$tmp,$RR1,$RR1
3563 ################################################################
3564 # At this point Rx holds 1324 powers, RRx - 5768, and the goal
3565 # is 15263748, which reflects how data is loaded...
3567 vpunpcklqdq
$R2,$RR2,$T2 # 3748
3568 vpunpckhqdq
$R2,$RR2,$R2 # 1526
3569 vpunpcklqdq
$R0,$RR0,$T0
3570 vpunpckhqdq
$R0,$RR0,$R0
3571 vpunpcklqdq
$R1,$RR1,$T1
3572 vpunpckhqdq
$R1,$RR1,$R1
3574 ######## switch to %zmm
3575 map(s/%y/%z/, $H0,$H1,$H2,$R0,$R1,$R2,$S1,$S2);
3576 map(s/%y/%z/, $D0lo,$D0hi,$D1lo,$D1hi,$D2lo,$D2hi);
3577 map(s/%y/%z/, $T0,$T1,$T2,$T3,$mask44,$mask42,$tmp,$PAD);
3578 map(s/%y/%z/, $RR0,$RR1,$RR2,$SS1,$SS2);
3581 vshufi64x2 \
$0x44,$R2,$T2,$RR2 # 15263748
3582 vshufi64x2 \
$0x44,$R0,$T0,$RR0
3583 vshufi64x2 \
$0x44,$R1,$T1,$RR1
3585 vmovdqu64
16*0($inp),$T2 # load data
3586 vmovdqu64
16*4($inp),$T3
3589 vpsllq \
$2,$RR2,$SS2 # S2 = R2*5*4
3590 vpsllq \
$2,$RR1,$SS1 # S1 = R1*5*4
3591 vpaddq
$RR2,$SS2,$SS2
3592 vpaddq
$RR1,$SS1,$SS1
3593 vpsllq \
$2,$SS2,$SS2
3594 vpsllq \
$2,$SS1,$SS1
3596 vpbroadcastq
$padbit,$PAD
3597 vpbroadcastq
%x#$mask44,$mask44
3598 vpbroadcastq
%x#$mask42,$mask42
3600 vpbroadcastq
%x#$SS1,$S1 # broadcast 8th power
3601 vpbroadcastq
%x#$SS2,$S2
3602 vpbroadcastq
%x#$RR0,$R0
3603 vpbroadcastq
%x#$RR1,$R1
3604 vpbroadcastq
%x#$RR2,$R2
3606 vpunpcklqdq
$T3,$T2,$T1 # transpose data
3607 vpunpckhqdq
$T3,$T2,$T3
3609 # at this point 64-bit lanes are ordered as 73625140
3611 vpsrlq \
$24,$T3,$T2 # splat the data
3613 vpaddq
$T2,$H2,$H2 # accumulate input
3614 vpandq
$mask44,$T1,$T0
3618 vpandq
$mask44,$T1,$T1
3621 jz
.Ltail_vpmadd52_8x
3622 jmp
.Loop_vpmadd52_8x
3626 #vpaddq $T2,$H2,$H2 # accumulate input
3630 vpxorq
$D0lo,$D0lo,$D0lo
3631 vpmadd52luq
$H2,$S1,$D0lo
3632 vpxorq
$D0hi,$D0hi,$D0hi
3633 vpmadd52huq
$H2,$S1,$D0hi
3634 vpxorq
$D1lo,$D1lo,$D1lo
3635 vpmadd52luq
$H2,$S2,$D1lo
3636 vpxorq
$D1hi,$D1hi,$D1hi
3637 vpmadd52huq
$H2,$S2,$D1hi
3638 vpxorq
$D2lo,$D2lo,$D2lo
3639 vpmadd52luq
$H2,$R0,$D2lo
3640 vpxorq
$D2hi,$D2hi,$D2hi
3641 vpmadd52huq
$H2,$R0,$D2hi
3643 vmovdqu64
16*0($inp),$T2 # load data
3644 vmovdqu64
16*4($inp),$T3
3646 vpmadd52luq
$H0,$R0,$D0lo
3647 vpmadd52huq
$H0,$R0,$D0hi
3648 vpmadd52luq
$H0,$R1,$D1lo
3649 vpmadd52huq
$H0,$R1,$D1hi
3650 vpmadd52luq
$H0,$R2,$D2lo
3651 vpmadd52huq
$H0,$R2,$D2hi
3653 vpunpcklqdq
$T3,$T2,$T1 # transpose data
3654 vpunpckhqdq
$T3,$T2,$T3
3655 vpmadd52luq
$H1,$S2,$D0lo
3656 vpmadd52huq
$H1,$S2,$D0hi
3657 vpmadd52luq
$H1,$R0,$D1lo
3658 vpmadd52huq
$H1,$R0,$D1hi
3659 vpmadd52luq
$H1,$R1,$D2lo
3660 vpmadd52huq
$H1,$R1,$D2hi
3662 ################################################################
3663 # partial reduction (interleaved with data splat)
3664 vpsrlq \
$44,$D0lo,$tmp
3665 vpsllq \
$8,$D0hi,$D0hi
3666 vpandq
$mask44,$D0lo,$H0
3667 vpaddq
$tmp,$D0hi,$D0hi
3671 vpaddq
$D0hi,$D1lo,$D1lo
3673 vpsrlq \
$44,$D1lo,$tmp
3674 vpsllq \
$8,$D1hi,$D1hi
3675 vpandq
$mask44,$D1lo,$H1
3676 vpaddq
$tmp,$D1hi,$D1hi
3678 vpandq
$mask44,$T1,$T0
3681 vpaddq
$D1hi,$D2lo,$D2lo
3683 vpsrlq \
$42,$D2lo,$tmp
3684 vpsllq \
$10,$D2hi,$D2hi
3685 vpandq
$mask42,$D2lo,$H2
3686 vpaddq
$tmp,$D2hi,$D2hi
3688 vpaddq
$T2,$H2,$H2 # accumulate input
3689 vpaddq
$D2hi,$H0,$H0
3690 vpsllq \
$2,$D2hi,$D2hi
3692 vpaddq
$D2hi,$H0,$H0
3694 vpandq
$mask44,$T1,$T1
3696 vpsrlq \
$44,$H0,$tmp # additional step
3697 vpandq
$mask44,$H0,$H0
3701 sub \
$8,$len # len-=128
3702 jnz
.Loop_vpmadd52_8x
3705 #vpaddq $T2,$H2,$H2 # accumulate input
3709 vpxorq
$D0lo,$D0lo,$D0lo
3710 vpmadd52luq
$H2,$SS1,$D0lo
3711 vpxorq
$D0hi,$D0hi,$D0hi
3712 vpmadd52huq
$H2,$SS1,$D0hi
3713 vpxorq
$D1lo,$D1lo,$D1lo
3714 vpmadd52luq
$H2,$SS2,$D1lo
3715 vpxorq
$D1hi,$D1hi,$D1hi
3716 vpmadd52huq
$H2,$SS2,$D1hi
3717 vpxorq
$D2lo,$D2lo,$D2lo
3718 vpmadd52luq
$H2,$RR0,$D2lo
3719 vpxorq
$D2hi,$D2hi,$D2hi
3720 vpmadd52huq
$H2,$RR0,$D2hi
3722 vpmadd52luq
$H0,$RR0,$D0lo
3723 vpmadd52huq
$H0,$RR0,$D0hi
3724 vpmadd52luq
$H0,$RR1,$D1lo
3725 vpmadd52huq
$H0,$RR1,$D1hi
3726 vpmadd52luq
$H0,$RR2,$D2lo
3727 vpmadd52huq
$H0,$RR2,$D2hi
3729 vpmadd52luq
$H1,$SS2,$D0lo
3730 vpmadd52huq
$H1,$SS2,$D0hi
3731 vpmadd52luq
$H1,$RR0,$D1lo
3732 vpmadd52huq
$H1,$RR0,$D1hi
3733 vpmadd52luq
$H1,$RR1,$D2lo
3734 vpmadd52huq
$H1,$RR1,$D2hi
3736 ################################################################
3737 # horizontal addition
3741 vpsrldq \
$8,$D0lo,$T0
3742 vpsrldq \
$8,$D0hi,$H0
3743 vpsrldq \
$8,$D1lo,$T1
3744 vpsrldq \
$8,$D1hi,$H1
3745 vpaddq
$T0,$D0lo,$D0lo
3746 vpaddq
$H0,$D0hi,$D0hi
3747 vpsrldq \
$8,$D2lo,$T2
3748 vpsrldq \
$8,$D2hi,$H2
3749 vpaddq
$T1,$D1lo,$D1lo
3750 vpaddq
$H1,$D1hi,$D1hi
3751 vpermq \
$0x2,$D0lo,$T0
3752 vpermq \
$0x2,$D0hi,$H0
3753 vpaddq
$T2,$D2lo,$D2lo
3754 vpaddq
$H2,$D2hi,$D2hi
3756 vpermq \
$0x2,$D1lo,$T1
3757 vpermq \
$0x2,$D1hi,$H1
3758 vpaddq
$T0,$D0lo,$D0lo
3759 vpaddq
$H0,$D0hi,$D0hi
3760 vpermq \
$0x2,$D2lo,$T2
3761 vpermq \
$0x2,$D2hi,$H2
3762 vpaddq
$T1,$D1lo,$D1lo
3763 vpaddq
$H1,$D1hi,$D1hi
3764 vextracti64x4 \
$1,$D0lo,%y#$T0
3765 vextracti64x4 \
$1,$D0hi,%y#$H0
3766 vpaddq
$T2,$D2lo,$D2lo
3767 vpaddq
$H2,$D2hi,$D2hi
3769 vextracti64x4 \
$1,$D1lo,%y#$T1
3770 vextracti64x4 \
$1,$D1hi,%y#$H1
3771 vextracti64x4 \
$1,$D2lo,%y#$T2
3772 vextracti64x4 \
$1,$D2hi,%y#$H2
3774 ######## switch back to %ymm
3775 map(s/%z/%y/, $H0,$H1,$H2,$R0,$R1,$R2,$S1,$S2);
3776 map(s/%z/%y/, $D0lo,$D0hi,$D1lo,$D1hi,$D2lo,$D2hi);
3777 map(s/%z/%y/, $T0,$T1,$T2,$T3,$mask44,$mask42,$tmp,$PAD);
3780 vpaddq
$T0,$D0lo,${D0lo
}{%k1}{z
}
3781 vpaddq
$H0,$D0hi,${D0hi
}{%k1}{z
}
3782 vpaddq
$T1,$D1lo,${D1lo
}{%k1}{z
}
3783 vpaddq
$H1,$D1hi,${D1hi
}{%k1}{z
}
3784 vpaddq
$T2,$D2lo,${D2lo
}{%k1}{z
}
3785 vpaddq
$H2,$D2hi,${D2hi
}{%k1}{z
}
3787 ################################################################
3789 vpsrlq \
$44,$D0lo,$tmp
3790 vpsllq \
$8,$D0hi,$D0hi
3791 vpandq
$mask44,$D0lo,$H0
3792 vpaddq
$tmp,$D0hi,$D0hi
3794 vpaddq
$D0hi,$D1lo,$D1lo
3796 vpsrlq \
$44,$D1lo,$tmp
3797 vpsllq \
$8,$D1hi,$D1hi
3798 vpandq
$mask44,$D1lo,$H1
3799 vpaddq
$tmp,$D1hi,$D1hi
3801 vpaddq
$D1hi,$D2lo,$D2lo
3803 vpsrlq \
$42,$D2lo,$tmp
3804 vpsllq \
$10,$D2hi,$D2hi
3805 vpandq
$mask42,$D2lo,$H2
3806 vpaddq
$tmp,$D2hi,$D2hi
3808 vpaddq
$D2hi,$H0,$H0
3809 vpsllq \
$2,$D2hi,$D2hi
3811 vpaddq
$D2hi,$H0,$H0
3813 vpsrlq \
$44,$H0,$tmp # additional step
3814 vpandq
$mask44,$H0,$H0
3818 ################################################################
3820 vmovq
%x#$H0,0($ctx)
3821 vmovq
%x#$H1,8($ctx)
3822 vmovq
%x#$H2,16($ctx)
3825 .Lno_data_vpmadd52_8x
:
3827 .size poly1305_blocks_vpmadd52_8x
,.-poly1305_blocks_vpmadd52_8x
3831 .type poly1305_emit_base2_44
,\
@function,3
3833 poly1305_emit_base2_44
:
3834 mov
0($ctx),%r8 # load hash value
3850 add \
$5,%r8 # compare to modulus
3854 shr \
$2,%r10 # did 130-bit value overflow?
3858 add
0($nonce),%rax # accumulate nonce
3860 mov
%rax,0($mac) # write result
3864 .size poly1305_emit_base2_44
,.-poly1305_emit_base2_44
3870 { # chacha20-poly1305 helpers
3871 my ($out,$inp,$otp,$len)=$win64 ?
("%rcx","%rdx","%r8", "%r9") : # Win64 order
3872 ("%rdi","%rsi","%rdx","%rcx"); # Unix order
3874 .globl xor128_encrypt_n_pad
3875 .type xor128_encrypt_n_pad
,\
@abi-omnipotent
3877 xor128_encrypt_n_pad
:
3880 mov
$len,%r10 # put len aside
3881 shr \
$4,$len # len / 16
3885 movdqu
($inp,$otp),%xmm0
3887 movdqu
%xmm0,($out,$otp)
3893 and \
$15,%r10 # len % 16
3919 .size xor128_encrypt_n_pad
,.-xor128_encrypt_n_pad
3921 .globl xor128_decrypt_n_pad
3922 .type xor128_decrypt_n_pad
,\
@abi-omnipotent
3924 xor128_decrypt_n_pad
:
3927 mov
$len,%r10 # put len aside
3928 shr \
$4,$len # len / 16
3932 movdqu
($inp,$otp),%xmm0
3935 movdqu
%xmm1,($out,$otp)
3942 and \
$15,%r10 # len % 16
3951 mov
($inp,$otp),%r11b
3970 .size xor128_decrypt_n_pad
,.-xor128_decrypt_n_pad
3974 # EXCEPTION_DISPOSITION handler (EXCEPTION_RECORD *rec,ULONG64 frame,
3975 # CONTEXT *context,DISPATCHER_CONTEXT *disp)
3983 .extern __imp_RtlVirtualUnwind
3984 .type se_handler
,\
@abi-omnipotent
3998 mov
120($context),%rax # pull context->Rax
3999 mov
248($context),%rbx # pull context->Rip
4001 mov
8($disp),%rsi # disp->ImageBase
4002 mov
56($disp),%r11 # disp->HandlerData
4004 mov
0(%r11),%r10d # HandlerData[0]
4005 lea
(%rsi,%r10),%r10 # prologue label
4006 cmp %r10,%rbx # context->Rip<.Lprologue
4007 jb
.Lcommon_seh_tail
4009 mov
152($context),%rax # pull context->Rsp
4011 mov
4(%r11),%r10d # HandlerData[1]
4012 lea
(%rsi,%r10),%r10 # epilogue label
4013 cmp %r10,%rbx # context->Rip>=.Lepilogue
4014 jae
.Lcommon_seh_tail
4024 mov
%rbx,144($context) # restore context->Rbx
4025 mov
%rbp,160($context) # restore context->Rbp
4026 mov
%r12,216($context) # restore context->R12
4027 mov
%r13,224($context) # restore context->R13
4028 mov
%r14,232($context) # restore context->R14
4029 mov
%r15,240($context) # restore context->R14
4031 jmp
.Lcommon_seh_tail
4032 .size se_handler
,.-se_handler
4034 .type avx_handler
,\
@abi-omnipotent
4048 mov
120($context),%rax # pull context->Rax
4049 mov
248($context),%rbx # pull context->Rip
4051 mov
8($disp),%rsi # disp->ImageBase
4052 mov
56($disp),%r11 # disp->HandlerData
4054 mov
0(%r11),%r10d # HandlerData[0]
4055 lea
(%rsi,%r10),%r10 # prologue label
4056 cmp %r10,%rbx # context->Rip<prologue label
4057 jb
.Lcommon_seh_tail
4059 mov
152($context),%rax # pull context->Rsp
4061 mov
4(%r11),%r10d # HandlerData[1]
4062 lea
(%rsi,%r10),%r10 # epilogue label
4063 cmp %r10,%rbx # context->Rip>=epilogue label
4064 jae
.Lcommon_seh_tail
4066 mov
208($context),%rax # pull context->R11
4070 lea
512($context),%rdi # &context.Xmm6
4072 .long
0xa548f3fc # cld; rep movsq
4077 mov
%rax,152($context) # restore context->Rsp
4078 mov
%rsi,168($context) # restore context->Rsi
4079 mov
%rdi,176($context) # restore context->Rdi
4081 mov
40($disp),%rdi # disp->ContextRecord
4082 mov
$context,%rsi # context
4083 mov \
$154,%ecx # sizeof(CONTEXT)
4084 .long
0xa548f3fc # cld; rep movsq
4087 xor %ecx,%ecx # arg1, UNW_FLAG_NHANDLER
4088 mov
8(%rsi),%rdx # arg2, disp->ImageBase
4089 mov
0(%rsi),%r8 # arg3, disp->ControlPc
4090 mov
16(%rsi),%r9 # arg4, disp->FunctionEntry
4091 mov
40(%rsi),%r10 # disp->ContextRecord
4092 lea
56(%rsi),%r11 # &disp->HandlerData
4093 lea
24(%rsi),%r12 # &disp->EstablisherFrame
4094 mov
%r10,32(%rsp) # arg5
4095 mov
%r11,40(%rsp) # arg6
4096 mov
%r12,48(%rsp) # arg7
4097 mov
%rcx,56(%rsp) # arg8, (NULL)
4098 call
*__imp_RtlVirtualUnwind
(%rip)
4100 mov \
$1,%eax # ExceptionContinueSearch
4112 .size avx_handler
,.-avx_handler
4116 .rva
.LSEH_begin_poly1305_init_x86_64
4117 .rva
.LSEH_end_poly1305_init_x86_64
4118 .rva
.LSEH_info_poly1305_init_x86_64
4120 .rva
.LSEH_begin_poly1305_blocks_x86_64
4121 .rva
.LSEH_end_poly1305_blocks_x86_64
4122 .rva
.LSEH_info_poly1305_blocks_x86_64
4124 .rva
.LSEH_begin_poly1305_emit_x86_64
4125 .rva
.LSEH_end_poly1305_emit_x86_64
4126 .rva
.LSEH_info_poly1305_emit_x86_64
4128 $code.=<<___
if ($avx);
4129 .rva
.LSEH_begin_poly1305_blocks_avx
4131 .rva
.LSEH_info_poly1305_blocks_avx_1
4135 .rva
.LSEH_info_poly1305_blocks_avx_2
4138 .rva
.LSEH_end_poly1305_blocks_avx
4139 .rva
.LSEH_info_poly1305_blocks_avx_3
4141 .rva
.LSEH_begin_poly1305_emit_avx
4142 .rva
.LSEH_end_poly1305_emit_avx
4143 .rva
.LSEH_info_poly1305_emit_avx
4145 $code.=<<___
if ($avx>1);
4146 .rva
.LSEH_begin_poly1305_blocks_avx2
4147 .rva
.Lbase2_64_avx2
4148 .rva
.LSEH_info_poly1305_blocks_avx2_1
4150 .rva
.Lbase2_64_avx2
4152 .rva
.LSEH_info_poly1305_blocks_avx2_2
4155 .rva
.LSEH_end_poly1305_blocks_avx2
4156 .rva
.LSEH_info_poly1305_blocks_avx2_3
4158 $code.=<<___
if ($avx>2);
4159 .rva
.LSEH_begin_poly1305_blocks_avx512
4160 .rva
.LSEH_end_poly1305_blocks_avx512
4161 .rva
.LSEH_info_poly1305_blocks_avx512
4166 .LSEH_info_poly1305_init_x86_64
:
4169 .rva
.LSEH_begin_poly1305_init_x86_64
,.LSEH_begin_poly1305_init_x86_64
4171 .LSEH_info_poly1305_blocks_x86_64
:
4174 .rva
.Lblocks_body
,.Lblocks_epilogue
4176 .LSEH_info_poly1305_emit_x86_64
:
4179 .rva
.LSEH_begin_poly1305_emit_x86_64
,.LSEH_begin_poly1305_emit_x86_64
4181 $code.=<<___
if ($avx);
4182 .LSEH_info_poly1305_blocks_avx_1
:
4185 .rva
.Lblocks_avx_body
,.Lblocks_avx_epilogue
# HandlerData[]
4187 .LSEH_info_poly1305_blocks_avx_2
:
4190 .rva
.Lbase2_64_avx_body
,.Lbase2_64_avx_epilogue
# HandlerData[]
4192 .LSEH_info_poly1305_blocks_avx_3
:
4195 .rva
.Ldo_avx_body
,.Ldo_avx_epilogue
# HandlerData[]
4197 .LSEH_info_poly1305_emit_avx
:
4200 .rva
.LSEH_begin_poly1305_emit_avx
,.LSEH_begin_poly1305_emit_avx
4202 $code.=<<___
if ($avx>1);
4203 .LSEH_info_poly1305_blocks_avx2_1
:
4206 .rva
.Lblocks_avx2_body
,.Lblocks_avx2_epilogue
# HandlerData[]
4208 .LSEH_info_poly1305_blocks_avx2_2
:
4211 .rva
.Lbase2_64_avx2_body
,.Lbase2_64_avx2_epilogue
# HandlerData[]
4213 .LSEH_info_poly1305_blocks_avx2_3
:
4216 .rva
.Ldo_avx2_body
,.Ldo_avx2_epilogue
# HandlerData[]
4218 $code.=<<___
if ($avx>2);
4219 .LSEH_info_poly1305_blocks_avx512
:
4222 .rva
.Ldo_avx512_body
,.Ldo_avx512_epilogue
# HandlerData[]
4229 last if (!s/^#/\/\
// and !/^$/);
4234 foreach (split('\n',$code)) {
4235 s/\`([^\`]*)\`/eval($1)/ge;
4236 s/%r([a-z]+)#d/%e$1/g;
4237 s/%r([0-9]+)#d/%r$1d/g;
4238 s/%x#%[yz]/%x/g or s/%y#%z/%y/g or s/%z#%[yz]/%z/g;
4241 s/(^\.type.*),[0-9]+$/\1/;
4242 s/(^\.type.*),\@abi-omnipotent+$/\1,\@function/;