2 # Copyright 2011-2020 The OpenSSL Project Authors. All Rights Reserved.
4 # Licensed under the OpenSSL license (the "License"). You may not use
5 # this file except in compliance with the License. You can obtain a copy
6 # in the file LICENSE in the source distribution or at
7 # https://www.openssl.org/source/license.html
10 # ====================================================================
11 # Written by Andy Polyakov <appro@openssl.org> for the OpenSSL
12 # project. The module is, however, dual licensed under OpenSSL and
13 # CRYPTOGAMS licenses depending on where you obtain it. For further
14 # details see http://www.openssl.org/~appro/cryptogams/.
15 # ====================================================================
19 # Companion to x86_64-mont.pl that optimizes cache-timing attack
20 # countermeasures. The subroutines are produced by replacing bp[i]
21 # references in their x86_64-mont.pl counterparts with cache-neutral
22 # references to powers table computed in BN_mod_exp_mont_consttime.
23 # In addition subroutine that scatters elements of the powers table
24 # is implemented, so that scatter-/gathering can be tuned without
25 # bn_exp.c modifications.
29 # Add MULX/AD*X code paths and additional interfaces to optimize for
30 # branch prediction unit. For input lengths that are multiples of 8
31 # the np argument is not just modulus value, but one interleaved
32 # with 0. This is to optimize post-condition...
36 if ($flavour =~ /\./) { $output = $flavour; undef $flavour; }
38 $win64=0; $win64=1 if ($flavour =~ /[nm]asm|mingw64/ || $output =~ /\.asm$/);
40 $0 =~ m/(.*[\/\\])[^\/\\]+$/; $dir=$1;
41 ( $xlate="${dir}x86_64-xlate.pl" and -f $xlate ) or
42 ( $xlate="${dir}../../perlasm/x86_64-xlate.pl" and -f $xlate) or
43 die "can't locate x86_64-xlate.pl";
45 open OUT,"| \"$^X\" \"$xlate\" $flavour \"$output\"";
48 if (`$ENV{CC} -Wa,-v -c -o /dev/null -x assembler /dev/null 2>&1`
49 =~ /GNU assembler version ([2-9]\.[0-9]+)/) {
53 if (!$addx && $win64 && ($flavour =~ /nasm/ || $ENV{ASM} =~ /nasm/) &&
54 `nasm -v 2>&1` =~ /NASM version ([2-9]\.[0-9]+)/) {
58 if (!$addx && $win64 && ($flavour =~ /masm/ || $ENV{ASM} =~ /ml64/) &&
59 `ml64 2>&1` =~ /Version ([0-9]+)\./) {
63 if (!$addx && `$ENV{CC} -v 2>&1` =~ /((?:clang|LLVM) version|.*based on LLVM) ([0-9]+)\.([0-9]+)/) {
64 my $ver = $2 + $3/100.0; # 3.1->3.01, 3.10->3.10
68 # int bn_mul_mont_gather5(
69 $rp="%rdi"; # BN_ULONG *rp,
70 $ap="%rsi"; # const BN_ULONG *ap,
71 $bp="%rdx"; # const BN_ULONG *bp,
72 $np="%rcx"; # const BN_ULONG *np,
73 $n0="%r8"; # const BN_ULONG *n0,
74 $num="%r9"; # int num,
75 # int idx); # 0 to 2^5-1, "index" in $bp holding
76 # pre-computed powers of a', interlaced
77 # in such manner that b[0] is $bp[idx],
78 # b[1] is [2^5+idx], etc.
90 .extern OPENSSL_ia32cap_P
92 .globl bn_mul_mont_gather5
93 .type bn_mul_mont_gather5,\@function,6
99 .cfi_def_cfa_register %rax
103 $code.=<<___ if ($addx);
104 mov OPENSSL_ia32cap_P+8(%rip),%r11d
111 movd `($win64?56:8)`(%rsp),%xmm5 # load 7th argument
127 lea -280(%rsp,$num,8),%r10 # future alloca(8*(num+2)+256+8)
128 neg $num # restore $num
129 and \$-1024,%r10 # minimize TLB usage
131 # An OS-agnostic version of __chkstk.
133 # Some OSes (Windows) insist on stack being "wired" to
134 # physical memory in strictly sequential manner, i.e. if stack
135 # allocation spans two pages, then reference to farmost one can
136 # be punishable by SEGV. But page walking can do good even on
137 # other OSes, because it guarantees that villain thread hits
138 # the guard page before it can make damage to innocent one...
145 jmp .Lmul_page_walk_done
152 .Lmul_page_walk_done:
155 mov %rax,8(%rsp,$num,8) # tp[num+1]=%rsp
156 .cfi_cfa_expression %rsp+8,$num,8,mul,plus,deref,+8
159 lea 128($bp),%r12 # reassign $bp (+size optimization)
162 $STRIDE=2**5*8; # 5 is "window size"
163 $N=$STRIDE/4; # should match cache line size
165 movdqa 0(%r10),%xmm0 # 00000001000000010000000000000000
166 movdqa 16(%r10),%xmm1 # 00000002000000020000000200000002
167 lea 24-112(%rsp,$num,8),%r10# place the mask after tp[num+3] (+ICache optimization)
170 pshufd \$0,%xmm5,%xmm5 # broadcast index
174 ########################################################################
175 # calculate mask by comparing 0..31 to index and save result to stack
179 pcmpeqd %xmm5,%xmm0 # compare to 1,0
183 for($k=0;$k<$STRIDE/16-4;$k+=4) {
186 pcmpeqd %xmm5,%xmm1 # compare to 3,2
187 movdqa %xmm0,`16*($k+0)+112`(%r10)
191 pcmpeqd %xmm5,%xmm2 # compare to 5,4
192 movdqa %xmm1,`16*($k+1)+112`(%r10)
196 pcmpeqd %xmm5,%xmm3 # compare to 7,6
197 movdqa %xmm2,`16*($k+2)+112`(%r10)
202 movdqa %xmm3,`16*($k+3)+112`(%r10)
206 $code.=<<___; # last iteration can be optimized
209 movdqa %xmm0,`16*($k+0)+112`(%r10)
214 movdqa %xmm1,`16*($k+1)+112`(%r10)
217 movdqa %xmm2,`16*($k+2)+112`(%r10)
218 pand `16*($k+0)-128`($bp),%xmm0 # while it's still in register
220 pand `16*($k+1)-128`($bp),%xmm1
221 pand `16*($k+2)-128`($bp),%xmm2
222 movdqa %xmm3,`16*($k+3)+112`(%r10)
223 pand `16*($k+3)-128`($bp),%xmm3
227 for($k=0;$k<$STRIDE/16-4;$k+=4) {
229 movdqa `16*($k+0)-128`($bp),%xmm4
230 movdqa `16*($k+1)-128`($bp),%xmm5
231 movdqa `16*($k+2)-128`($bp),%xmm2
232 pand `16*($k+0)+112`(%r10),%xmm4
233 movdqa `16*($k+3)-128`($bp),%xmm3
234 pand `16*($k+1)+112`(%r10),%xmm5
236 pand `16*($k+2)+112`(%r10),%xmm2
238 pand `16*($k+3)+112`(%r10),%xmm3
245 pshufd \$0x4e,%xmm0,%xmm1
248 movq %xmm0,$m0 # m0=bp[0]
250 mov ($n0),$n0 # pull n0[0] value
257 mulq $m0 # ap[0]*bp[0]
261 imulq $lo0,$m1 # "tp[0]"*n0
265 add %rax,$lo0 # discarded
278 add $hi0,$hi1 # np[j]*m1+ap[j]*bp[0]
281 mov $hi1,-16(%rsp,$j,8) # tp[j-1]
285 mulq $m0 # ap[j]*bp[0]
294 jne .L1st # note that upon exit $j==$num, so
295 # they can be used interchangeably
299 add $hi0,$hi1 # np[j]*m1+ap[j]*bp[0]
301 mov $hi1,-16(%rsp,$num,8) # tp[num-1]
308 mov $hi1,-8(%rsp,$num,8)
309 mov %rdx,(%rsp,$num,8) # store upmost overflow bit
315 lea 24+128(%rsp,$num,8),%rdx # where 256-byte mask is (+size optimization)
320 for($k=0;$k<$STRIDE/16;$k+=4) {
322 movdqa `16*($k+0)-128`($bp),%xmm0
323 movdqa `16*($k+1)-128`($bp),%xmm1
324 movdqa `16*($k+2)-128`($bp),%xmm2
325 movdqa `16*($k+3)-128`($bp),%xmm3
326 pand `16*($k+0)-128`(%rdx),%xmm0
327 pand `16*($k+1)-128`(%rdx),%xmm1
329 pand `16*($k+2)-128`(%rdx),%xmm2
331 pand `16*($k+3)-128`(%rdx),%xmm3
338 pshufd \$0x4e,%xmm4,%xmm0
342 mov ($ap),%rax # ap[0]
343 movq %xmm0,$m0 # m0=bp[i]
349 mulq $m0 # ap[0]*bp[i]
350 add %rax,$lo0 # ap[0]*bp[i]+tp[0]
354 imulq $lo0,$m1 # tp[0]*n0
358 add %rax,$lo0 # discarded
361 mov 8(%rsp),$lo0 # tp[1]
372 add $lo0,$hi1 # np[j]*m1+ap[j]*bp[i]+tp[j]
375 mov $hi1,-16(%rsp,$j,8) # tp[j-1]
379 mulq $m0 # ap[j]*bp[i]
383 add $hi0,$lo0 # ap[j]*bp[i]+tp[j]
390 jne .Linner # note that upon exit $j==$num, so
391 # they can be used interchangeably
394 add $lo0,$hi1 # np[j]*m1+ap[j]*bp[i]+tp[j]
395 mov (%rsp,$num,8),$lo0
397 mov $hi1,-16(%rsp,$num,8) # tp[num-1]
403 add $lo0,$hi1 # pull upmost overflow bit
405 mov $hi1,-8(%rsp,$num,8)
406 mov %rdx,(%rsp,$num,8) # store upmost overflow bit
412 xor $i,$i # i=0 and clear CF!
413 mov (%rsp),%rax # tp[0]
414 lea (%rsp),$ap # borrow ap for tp
418 .Lsub: sbb ($np,$i,8),%rax
419 mov %rax,($rp,$i,8) # rp[i]=tp[i]-np[i]
420 mov 8($ap,$i,8),%rax # tp[i+1]
422 dec $j # doesn't affect CF!
425 sbb \$0,%rax # handle upmost overflow bit
431 .Lcopy: # conditional copy
436 mov $i,(%rsp,$i,8) # zap temporary vector
438 mov %rdx,($rp,$i,8) # rp[i]=tp[i]
443 mov 8(%rsp,$num,8),%rsi # restore %rsp
460 .cfi_def_cfa_register %rsp
464 .size bn_mul_mont_gather5,.-bn_mul_mont_gather5
467 my @A=("%r10","%r11");
468 my @N=("%r13","%rdi");
470 .type bn_mul4x_mont_gather5,\@function,6
472 bn_mul4x_mont_gather5:
476 .cfi_def_cfa_register %rax
479 $code.=<<___ if ($addx);
481 cmp \$0x80108,%r11d # check for AD*X+BMI2+BMI1
500 shl \$3,${num}d # convert $num to bytes
501 lea ($num,$num,2),%r10 # 3*$num in bytes
504 ##############################################################
505 # Ensure that stack frame doesn't alias with $rptr+3*$num
506 # modulo 4096, which covers ret[num], am[num] and n[num]
507 # (see bn_exp.c). This is done to allow memory disambiguation
508 # logic do its magic. [Extra [num] is allocated in order
509 # to align with bn_power5's frame, which is cleansed after
510 # completing exponentiation. Extra 256 bytes is for power mask
511 # calculated from 7th argument, the index.]
513 lea -320(%rsp,$num,2),%r11
519 sub %r11,%rbp # align with $rp
520 lea -320(%rbp,$num,2),%rbp # future alloca(frame+2*num*8+256)
525 lea 4096-320(,$num,2),%r10
526 lea -320(%rbp,$num,2),%rbp # future alloca(frame+2*num*8+256)
540 jmp .Lmul4x_page_walk_done
547 .Lmul4x_page_walk_done:
552 .cfi_cfa_expression %rsp+40,deref,+8
557 mov 40(%rsp),%rsi # restore %rsp
574 .cfi_def_cfa_register %rsp
578 .size bn_mul4x_mont_gather5,.-bn_mul4x_mont_gather5
580 .type mul4x_internal,\@abi-omnipotent
584 shl \$5,$num # $num was in bytes
585 movd `($win64?56:8)`(%rax),%xmm5 # load 7th argument, index
587 lea 128(%rdx,$num),%r13 # end of powers table (+size optimization)
588 shr \$5,$num # restore $num
591 $STRIDE=2**5*8; # 5 is "window size"
592 $N=$STRIDE/4; # should match cache line size
595 movdqa 0(%rax),%xmm0 # 00000001000000010000000000000000
596 movdqa 16(%rax),%xmm1 # 00000002000000020000000200000002
597 lea 88-112(%rsp,$num),%r10 # place the mask after tp[num+1] (+ICache optimization)
598 lea 128(%rdx),$bp # size optimization
600 pshufd \$0,%xmm5,%xmm5 # broadcast index
605 ########################################################################
606 # calculate mask by comparing 0..31 to index and save result to stack
610 pcmpeqd %xmm5,%xmm0 # compare to 1,0
614 for($i=0;$i<$STRIDE/16-4;$i+=4) {
617 pcmpeqd %xmm5,%xmm1 # compare to 3,2
618 movdqa %xmm0,`16*($i+0)+112`(%r10)
622 pcmpeqd %xmm5,%xmm2 # compare to 5,4
623 movdqa %xmm1,`16*($i+1)+112`(%r10)
627 pcmpeqd %xmm5,%xmm3 # compare to 7,6
628 movdqa %xmm2,`16*($i+2)+112`(%r10)
633 movdqa %xmm3,`16*($i+3)+112`(%r10)
637 $code.=<<___; # last iteration can be optimized
640 movdqa %xmm0,`16*($i+0)+112`(%r10)
645 movdqa %xmm1,`16*($i+1)+112`(%r10)
648 movdqa %xmm2,`16*($i+2)+112`(%r10)
649 pand `16*($i+0)-128`($bp),%xmm0 # while it's still in register
651 pand `16*($i+1)-128`($bp),%xmm1
652 pand `16*($i+2)-128`($bp),%xmm2
653 movdqa %xmm3,`16*($i+3)+112`(%r10)
654 pand `16*($i+3)-128`($bp),%xmm3
658 for($i=0;$i<$STRIDE/16-4;$i+=4) {
660 movdqa `16*($i+0)-128`($bp),%xmm4
661 movdqa `16*($i+1)-128`($bp),%xmm5
662 movdqa `16*($i+2)-128`($bp),%xmm2
663 pand `16*($i+0)+112`(%r10),%xmm4
664 movdqa `16*($i+3)-128`($bp),%xmm3
665 pand `16*($i+1)+112`(%r10),%xmm5
667 pand `16*($i+2)+112`(%r10),%xmm2
669 pand `16*($i+3)+112`(%r10),%xmm3
676 pshufd \$0x4e,%xmm0,%xmm1
679 movq %xmm0,$m0 # m0=bp[0]
681 mov %r13,16+8(%rsp) # save end of b[num]
682 mov $rp, 56+8(%rsp) # save $rp
684 mov ($n0),$n0 # pull n0[0] value
686 lea ($ap,$num),$ap # end of a[num]
690 mulq $m0 # ap[0]*bp[0]
694 imulq $A[0],$m1 # "tp[0]"*n0
699 add %rax,$A[0] # discarded
712 mov 16($ap,$num),%rax
715 lea 4*8($num),$j # j=4
724 mulq $m0 # ap[j]*bp[0]
735 add $A[0],$N[0] # np[j]*m1+ap[j]*bp[0]
737 mov $N[0],-24($tp) # tp[j-1]
740 mulq $m0 # ap[j]*bp[0]
750 add $A[1],$N[1] # np[j]*m1+ap[j]*bp[0]
752 mov $N[1],-16($tp) # tp[j-1]
755 mulq $m0 # ap[j]*bp[0]
765 add $A[0],$N[0] # np[j]*m1+ap[j]*bp[0]
767 mov $N[0],-8($tp) # tp[j-1]
770 mulq $m0 # ap[j]*bp[0]
780 add $A[1],$N[1] # np[j]*m1+ap[j]*bp[0]
783 mov $N[1],($tp) # tp[j-1]
789 mulq $m0 # ap[j]*bp[0]
800 add $A[0],$N[0] # np[j]*m1+ap[j]*bp[0]
802 mov $N[0],-24($tp) # tp[j-1]
805 mulq $m0 # ap[j]*bp[0]
813 mov ($ap,$num),%rax # ap[0]
815 add $A[1],$N[1] # np[j]*m1+ap[j]*bp[0]
817 mov $N[1],-16($tp) # tp[j-1]
820 lea ($np,$num),$np # rewind $np
831 lea 16+128($tp),%rdx # where 256-byte mask is (+size optimization)
835 for($i=0;$i<$STRIDE/16;$i+=4) {
837 movdqa `16*($i+0)-128`($bp),%xmm0
838 movdqa `16*($i+1)-128`($bp),%xmm1
839 movdqa `16*($i+2)-128`($bp),%xmm2
840 movdqa `16*($i+3)-128`($bp),%xmm3
841 pand `16*($i+0)-128`(%rdx),%xmm0
842 pand `16*($i+1)-128`(%rdx),%xmm1
844 pand `16*($i+2)-128`(%rdx),%xmm2
846 pand `16*($i+3)-128`(%rdx),%xmm3
853 pshufd \$0x4e,%xmm4,%xmm0
856 movq %xmm0,$m0 # m0=bp[i]
860 mulq $m0 # ap[0]*bp[i]
861 add %rax,$A[0] # ap[0]*bp[i]+tp[0]
865 imulq $A[0],$m1 # tp[0]*n0
867 mov $N[1],($tp) # store upmost overflow bit
869 lea ($tp,$num),$tp # rewind $tp
872 add %rax,$A[0] # "$N[0]", discarded
877 mulq $m0 # ap[j]*bp[i]
881 add 8($tp),$A[1] # +tp[1]
887 mov 16($ap,$num),%rax
889 add $A[1],$N[1] # np[j]*m1+ap[j]*bp[i]+tp[j]
890 lea 4*8($num),$j # j=4
898 mulq $m0 # ap[j]*bp[i]
902 add 16($tp),$A[0] # ap[j]*bp[i]+tp[j]
913 mov $N[1],-32($tp) # tp[j-1]
916 mulq $m0 # ap[j]*bp[i]
930 mov $N[0],-24($tp) # tp[j-1]
933 mulq $m0 # ap[j]*bp[i]
937 add ($tp),$A[0] # ap[j]*bp[i]+tp[j]
947 mov $N[1],-16($tp) # tp[j-1]
950 mulq $m0 # ap[j]*bp[i]
965 mov $N[0],-8($tp) # tp[j-1]
971 mulq $m0 # ap[j]*bp[i]
975 add 16($tp),$A[0] # ap[j]*bp[i]+tp[j]
986 mov $N[1],-32($tp) # tp[j-1]
989 mulq $m0 # ap[j]*bp[i]
1000 mov ($ap,$num),%rax # ap[0]
1004 mov $N[0],-24($tp) # tp[j-1]
1007 mov $N[1],-16($tp) # tp[j-1]
1008 lea ($np,$num),$np # rewind $np
1013 add ($tp),$N[0] # pull upmost overflow bit
1014 adc \$0,$N[1] # upmost overflow bit
1023 sub $N[0],$m1 # compare top-most words
1024 adc $j,$j # $j is zero
1026 sub $N[1],%rax # %rax=-$N[1]
1027 lea ($tp,$num),%rbx # tptr in .sqr4x_sub
1029 lea ($np),%rbp # nptr in .sqr4x_sub
1032 mov 56+8(%rsp),%rdi # rptr in .sqr4x_sub
1033 dec %r12 # so that after 'not' we get -n[0]
1038 jmp .Lsqr4x_sub_entry
1041 my @ri=("%rax",$bp,$m0,$m1);
1045 lea ($tp,$num),$tp # rewind $tp
1047 lea ($np,$N[1],8),$np
1048 mov 56+8(%rsp),$rp # restore $rp
1057 sbb 16*0($np),@ri[0]
1059 sbb 16*1($np),@ri[1]
1062 sbb 16*2($np),@ri[2]
1064 sbb 16*3($np),@ri[3]
1079 .size mul4x_internal,.-mul4x_internal
1083 ######################################################################
1085 my $rptr="%rdi"; # BN_ULONG *rptr,
1086 my $aptr="%rsi"; # const BN_ULONG *aptr,
1087 my $bptr="%rdx"; # const void *table,
1088 my $nptr="%rcx"; # const BN_ULONG *nptr,
1089 my $n0 ="%r8"; # const BN_ULONG *n0);
1090 my $num ="%r9"; # int num, has to be divisible by 8
1093 my ($i,$j,$tptr)=("%rbp","%rcx",$rptr);
1094 my @A0=("%r10","%r11");
1095 my @A1=("%r12","%r13");
1096 my ($a0,$a1,$ai)=("%r14","%r15","%rbx");
1100 .type bn_power5,\@function,6
1105 .cfi_def_cfa_register %rax
1107 $code.=<<___ if ($addx);
1108 mov OPENSSL_ia32cap_P+8(%rip),%r11d
1110 cmp \$0x80108,%r11d # check for AD*X+BMI2+BMI1
1128 shl \$3,${num}d # convert $num to bytes
1129 lea ($num,$num,2),%r10d # 3*$num
1133 ##############################################################
1134 # Ensure that stack frame doesn't alias with $rptr+3*$num
1135 # modulo 4096, which covers ret[num], am[num] and n[num]
1136 # (see bn_exp.c). This is done to allow memory disambiguation
1137 # logic do its magic. [Extra 256 bytes is for power mask
1138 # calculated from 7th argument, the index.]
1140 lea -320(%rsp,$num,2),%r11
1146 sub %r11,%rbp # align with $aptr
1147 lea -320(%rbp,$num,2),%rbp # future alloca(frame+2*num*8+256)
1152 lea 4096-320(,$num,2),%r10
1153 lea -320(%rbp,$num,2),%rbp # future alloca(frame+2*num*8+256)
1163 lea (%rbp,%r11),%rsp
1167 jmp .Lpwr_page_walk_done
1170 lea -4096(%rsp),%rsp
1174 .Lpwr_page_walk_done:
1179 ##############################################################
1182 # +0 saved $num, used in reduction section
1183 # +8 &t[2*$num], used in reduction section
1189 mov %rax, 40(%rsp) # save original %rsp
1190 .cfi_cfa_expression %rsp+40,deref,+8
1192 movq $rptr,%xmm1 # save $rptr, used in sqr8x
1193 movq $nptr,%xmm2 # save $nptr
1194 movq %r10, %xmm3 # -$num, used in sqr8x
1197 call __bn_sqr8x_internal
1198 call __bn_post4x_internal
1199 call __bn_sqr8x_internal
1200 call __bn_post4x_internal
1201 call __bn_sqr8x_internal
1202 call __bn_post4x_internal
1203 call __bn_sqr8x_internal
1204 call __bn_post4x_internal
1205 call __bn_sqr8x_internal
1206 call __bn_post4x_internal
1216 mov 40(%rsp),%rsi # restore %rsp
1232 .cfi_def_cfa_register %rsp
1236 .size bn_power5,.-bn_power5
1238 .globl bn_sqr8x_internal
1239 .hidden bn_sqr8x_internal
1240 .type bn_sqr8x_internal,\@abi-omnipotent
1243 __bn_sqr8x_internal:
1245 ##############################################################
1248 # a) multiply-n-add everything but a[i]*a[i];
1249 # b) shift result of a) by 1 to the left and accumulate
1250 # a[i]*a[i] products;
1252 ##############################################################
1318 lea 32(%r10),$i # $i=-($num-32)
1319 lea ($aptr,$num),$aptr # end of a[] buffer, ($aptr,$i)=&ap[2]
1321 mov $num,$j # $j=$num
1323 # comments apply to $num==8 case
1324 mov -32($aptr,$i),$a0 # a[0]
1325 lea 48+8(%rsp,$num,2),$tptr # end of tp[] buffer, &tp[2*$num]
1326 mov -24($aptr,$i),%rax # a[1]
1327 lea -32($tptr,$i),$tptr # end of tp[] window, &tp[2*$num-"$i"]
1328 mov -16($aptr,$i),$ai # a[2]
1332 mov %rax,$A0[0] # a[1]*a[0]
1335 mov $A0[0],-24($tptr,$i) # t[1]
1341 mov $A0[1],-16($tptr,$i) # t[2]
1345 mov -8($aptr,$i),$ai # a[3]
1347 mov %rax,$A1[0] # a[2]*a[1]+t[3]
1353 add %rax,$A0[0] # a[3]*a[0]+a[2]*a[1]+t[3]
1359 mov $A0[0],-8($tptr,$j) # t[3]
1364 mov ($aptr,$j),$ai # a[4]
1366 add %rax,$A1[1] # a[3]*a[1]+t[4]
1372 add %rax,$A0[1] # a[4]*a[0]+a[3]*a[1]+t[4]
1374 mov 8($aptr,$j),$ai # a[5]
1382 add %rax,$A1[0] # a[4]*a[3]+t[5]
1384 mov $A0[1],($tptr,$j) # t[4]
1389 add %rax,$A0[0] # a[5]*a[2]+a[4]*a[3]+t[5]
1391 mov 16($aptr,$j),$ai # a[6]
1398 add %rax,$A1[1] # a[5]*a[3]+t[6]
1400 mov $A0[0],8($tptr,$j) # t[5]
1405 add %rax,$A0[1] # a[6]*a[2]+a[5]*a[3]+t[6]
1407 mov 24($aptr,$j),$ai # a[7]
1415 add %rax,$A1[0] # a[6]*a[5]+t[7]
1417 mov $A0[1],16($tptr,$j) # t[6]
1423 add %rax,$A0[0] # a[7]*a[4]+a[6]*a[5]+t[6]
1429 mov $A0[0],-8($tptr,$j) # t[7]
1441 mov $A1[1],($tptr) # t[8]
1443 mov %rdx,8($tptr) # t[9]
1447 .Lsqr4x_outer: # comments apply to $num==6 case
1448 mov -32($aptr,$i),$a0 # a[0]
1449 lea 48+8(%rsp,$num,2),$tptr # end of tp[] buffer, &tp[2*$num]
1450 mov -24($aptr,$i),%rax # a[1]
1451 lea -32($tptr,$i),$tptr # end of tp[] window, &tp[2*$num-"$i"]
1452 mov -16($aptr,$i),$ai # a[2]
1456 mov -24($tptr,$i),$A0[0] # t[1]
1457 add %rax,$A0[0] # a[1]*a[0]+t[1]
1460 mov $A0[0],-24($tptr,$i) # t[1]
1467 add -16($tptr,$i),$A0[1] # a[2]*a[0]+t[2]
1470 mov $A0[1],-16($tptr,$i) # t[2]
1474 mov -8($aptr,$i),$ai # a[3]
1476 add %rax,$A1[0] # a[2]*a[1]+t[3]
1479 add -8($tptr,$i),$A1[0]
1484 add %rax,$A0[0] # a[3]*a[0]+a[2]*a[1]+t[3]
1490 mov $A0[0],-8($tptr,$i) # t[3]
1497 mov ($aptr,$j),$ai # a[4]
1499 add %rax,$A1[1] # a[3]*a[1]+t[4]
1503 add ($tptr,$j),$A1[1]
1508 add %rax,$A0[1] # a[4]*a[0]+a[3]*a[1]+t[4]
1510 mov 8($aptr,$j),$ai # a[5]
1517 add %rax,$A1[0] # a[4]*a[3]+t[5]
1518 mov $A0[1],($tptr,$j) # t[4]
1522 add 8($tptr,$j),$A1[0]
1527 add %rax,$A0[0] # a[5]*a[2]+a[4]*a[3]+t[5]
1533 mov $A0[0],-8($tptr,$j) # t[5], "preloaded t[1]" below
1545 mov $A1[1],($tptr) # t[6], "preloaded t[2]" below
1547 mov %rdx,8($tptr) # t[7], "preloaded t[3]" below
1552 # comments apply to $num==4 case
1553 mov -32($aptr),$a0 # a[0]
1554 lea 48+8(%rsp,$num,2),$tptr # end of tp[] buffer, &tp[2*$num]
1555 mov -24($aptr),%rax # a[1]
1556 lea -32($tptr,$i),$tptr # end of tp[] window, &tp[2*$num-"$i"]
1557 mov -16($aptr),$ai # a[2]
1561 add %rax,$A0[0] # a[1]*a[0]+t[1], preloaded t[1]
1569 mov $A0[0],-24($tptr) # t[1]
1572 add $A1[1],$A0[1] # a[2]*a[0]+t[2], preloaded t[2]
1573 mov -8($aptr),$ai # a[3]
1577 add %rax,$A1[0] # a[2]*a[1]+t[3], preloaded t[3]
1579 mov $A0[1],-16($tptr) # t[2]
1584 add %rax,$A0[0] # a[3]*a[0]+a[2]*a[1]+t[3]
1590 mov $A0[0],-8($tptr) # t[3]
1594 mov -16($aptr),%rax # a[2]
1599 mov $A1[1],($tptr) # t[4]
1601 mov %rdx,8($tptr) # t[5]
1606 my ($shift,$carry)=($a0,$a1);
1607 my @S=(@A1,$ai,$n0);
1611 sub $num,$i # $i=16-$num
1614 add $A1[0],%rax # t[5]
1616 mov %rax,8($tptr) # t[5]
1617 mov %rdx,16($tptr) # t[6]
1618 mov $carry,24($tptr) # t[7]
1620 mov -16($aptr,$i),%rax # a[0]
1621 lea 48+8(%rsp),$tptr
1622 xor $A0[0],$A0[0] # t[0]
1623 mov 8($tptr),$A0[1] # t[1]
1625 lea ($shift,$A0[0],2),$S[0] # t[2*i]<<1 | shift
1627 lea ($j,$A0[1],2),$S[1] # t[2*i+1]<<1 |
1629 or $A0[0],$S[1] # | t[2*i]>>63
1630 mov 16($tptr),$A0[0] # t[2*i+2] # prefetch
1631 mov $A0[1],$shift # shift=t[2*i+1]>>63
1632 mul %rax # a[i]*a[i]
1633 neg $carry # mov $carry,cf
1634 mov 24($tptr),$A0[1] # t[2*i+2+1] # prefetch
1636 mov -8($aptr,$i),%rax # a[i+1] # prefetch
1640 lea ($shift,$A0[0],2),$S[2] # t[2*i]<<1 | shift
1642 sbb $carry,$carry # mov cf,$carry
1644 lea ($j,$A0[1],2),$S[3] # t[2*i+1]<<1 |
1646 or $A0[0],$S[3] # | t[2*i]>>63
1647 mov 32($tptr),$A0[0] # t[2*i+2] # prefetch
1648 mov $A0[1],$shift # shift=t[2*i+1]>>63
1649 mul %rax # a[i]*a[i]
1650 neg $carry # mov $carry,cf
1651 mov 40($tptr),$A0[1] # t[2*i+2+1] # prefetch
1653 mov 0($aptr,$i),%rax # a[i+1] # prefetch
1658 sbb $carry,$carry # mov cf,$carry
1660 jmp .Lsqr4x_shift_n_add
1663 .Lsqr4x_shift_n_add:
1664 lea ($shift,$A0[0],2),$S[0] # t[2*i]<<1 | shift
1666 lea ($j,$A0[1],2),$S[1] # t[2*i+1]<<1 |
1668 or $A0[0],$S[1] # | t[2*i]>>63
1669 mov -16($tptr),$A0[0] # t[2*i+2] # prefetch
1670 mov $A0[1],$shift # shift=t[2*i+1]>>63
1671 mul %rax # a[i]*a[i]
1672 neg $carry # mov $carry,cf
1673 mov -8($tptr),$A0[1] # t[2*i+2+1] # prefetch
1675 mov -8($aptr,$i),%rax # a[i+1] # prefetch
1676 mov $S[0],-32($tptr)
1679 lea ($shift,$A0[0],2),$S[2] # t[2*i]<<1 | shift
1680 mov $S[1],-24($tptr)
1681 sbb $carry,$carry # mov cf,$carry
1683 lea ($j,$A0[1],2),$S[3] # t[2*i+1]<<1 |
1685 or $A0[0],$S[3] # | t[2*i]>>63
1686 mov 0($tptr),$A0[0] # t[2*i+2] # prefetch
1687 mov $A0[1],$shift # shift=t[2*i+1]>>63
1688 mul %rax # a[i]*a[i]
1689 neg $carry # mov $carry,cf
1690 mov 8($tptr),$A0[1] # t[2*i+2+1] # prefetch
1692 mov 0($aptr,$i),%rax # a[i+1] # prefetch
1693 mov $S[2],-16($tptr)
1696 lea ($shift,$A0[0],2),$S[0] # t[2*i]<<1 | shift
1698 sbb $carry,$carry # mov cf,$carry
1700 lea ($j,$A0[1],2),$S[1] # t[2*i+1]<<1 |
1702 or $A0[0],$S[1] # | t[2*i]>>63
1703 mov 16($tptr),$A0[0] # t[2*i+2] # prefetch
1704 mov $A0[1],$shift # shift=t[2*i+1]>>63
1705 mul %rax # a[i]*a[i]
1706 neg $carry # mov $carry,cf
1707 mov 24($tptr),$A0[1] # t[2*i+2+1] # prefetch
1709 mov 8($aptr,$i),%rax # a[i+1] # prefetch
1713 lea ($shift,$A0[0],2),$S[2] # t[2*i]<<1 | shift
1715 sbb $carry,$carry # mov cf,$carry
1717 lea ($j,$A0[1],2),$S[3] # t[2*i+1]<<1 |
1719 or $A0[0],$S[3] # | t[2*i]>>63
1720 mov 32($tptr),$A0[0] # t[2*i+2] # prefetch
1721 mov $A0[1],$shift # shift=t[2*i+1]>>63
1722 mul %rax # a[i]*a[i]
1723 neg $carry # mov $carry,cf
1724 mov 40($tptr),$A0[1] # t[2*i+2+1] # prefetch
1726 mov 16($aptr,$i),%rax # a[i+1] # prefetch
1730 sbb $carry,$carry # mov cf,$carry
1733 jnz .Lsqr4x_shift_n_add
1735 lea ($shift,$A0[0],2),$S[0] # t[2*i]<<1 | shift
1738 lea ($j,$A0[1],2),$S[1] # t[2*i+1]<<1 |
1740 or $A0[0],$S[1] # | t[2*i]>>63
1741 mov -16($tptr),$A0[0] # t[2*i+2] # prefetch
1742 mov $A0[1],$shift # shift=t[2*i+1]>>63
1743 mul %rax # a[i]*a[i]
1744 neg $carry # mov $carry,cf
1745 mov -8($tptr),$A0[1] # t[2*i+2+1] # prefetch
1747 mov -8($aptr),%rax # a[i+1] # prefetch
1748 mov $S[0],-32($tptr)
1751 lea ($shift,$A0[0],2),$S[2] # t[2*i]<<1|shift
1752 mov $S[1],-24($tptr)
1753 sbb $carry,$carry # mov cf,$carry
1755 lea ($j,$A0[1],2),$S[3] # t[2*i+1]<<1 |
1757 or $A0[0],$S[3] # | t[2*i]>>63
1758 mul %rax # a[i]*a[i]
1759 neg $carry # mov $carry,cf
1762 mov $S[2],-16($tptr)
1766 ######################################################################
1767 # Montgomery reduction part, "word-by-word" algorithm.
1769 # This new path is inspired by multiple submissions from Intel, by
1770 # Shay Gueron, Vlad Krasnov, Erdinc Ozturk, James Guilford,
1773 my ($nptr,$tptr,$carry,$m0)=("%rbp","%rdi","%rsi","%rbx");
1777 __bn_sqr8x_reduction:
1779 lea ($nptr,$num),%rcx # end of n[]
1780 lea 48+8(%rsp,$num,2),%rdx # end of t[] buffer
1782 lea 48+8(%rsp,$num),$tptr # end of initial t[] window
1785 jmp .L8x_reduction_loop
1788 .L8x_reduction_loop:
1789 lea ($tptr,$num),$tptr # start of current t[] window
1799 mov %rax,(%rdx) # store top-most carry bit
1800 lea 8*8($tptr),$tptr
1804 imulq 32+8(%rsp),$m0 # n0*a[0]
1805 mov 8*0($nptr),%rax # n[0]
1812 mov 8*1($nptr),%rax # n[1]
1822 mov $m0,48-8+8(%rsp,%rcx,8) # put aside n0*a[i]
1831 mov 32+8(%rsp),$carry # pull n0, borrow $carry
1839 imulq %r8,$carry # modulo-scheduled
1869 mov $carry,$m0 # n0*a[i]
1871 mov 8*0($nptr),%rax # n[0]
1880 lea 8*8($nptr),$nptr
1882 mov 8+8(%rsp),%rdx # pull end of t[]
1883 cmp 0+8(%rsp),$nptr # end of n[]?
1895 sbb $carry,$carry # top carry
1897 mov 48+56+8(%rsp),$m0 # pull n0*a[0]
1907 mov %r8,($tptr) # save result
1916 lea 8($tptr),$tptr # $tptr++
1961 mov 48-16+8(%rsp,%rcx,8),$m0# pull n0*a[i]
1965 mov 8*0($nptr),%rax # pull n[0]
1972 lea 8*8($nptr),$nptr
1973 mov 8+8(%rsp),%rdx # pull end of t[]
1974 cmp 0+8(%rsp),$nptr # end of n[]?
1975 jae .L8x_tail_done # break out of loop
1977 mov 48+56+8(%rsp),$m0 # pull n0*a[0]
1979 mov 8*0($nptr),%rax # pull n[0]
1988 sbb $carry,$carry # top carry
1996 add (%rdx),%r8 # can this overflow?
2016 adc \$0,%rax # top-most carry
2017 mov -8($nptr),%rcx # np[num-1]
2020 movq %xmm2,$nptr # restore $nptr
2022 mov %r8,8*0($tptr) # store top 512 bits
2024 movq %xmm3,$num # $num is %r9, can't be moved upwards
2031 lea 8*8($tptr),$tptr
2033 cmp %rdx,$tptr # end of t[]?
2034 jb .L8x_reduction_loop
2037 .size bn_sqr8x_internal,.-bn_sqr8x_internal
2040 ##############################################################
2041 # Post-condition, 4x unrolled
2044 my ($tptr,$nptr)=("%rbx","%rbp");
2046 .type __bn_post4x_internal,\@abi-omnipotent
2048 __bn_post4x_internal:
2051 lea (%rdi,$num),$tptr # %rdi was $tptr above
2053 movq %xmm1,$rptr # restore $rptr
2055 movq %xmm1,$aptr # prepare for back-to-back call
2057 dec %r12 # so that after 'not' we get -n[0]
2062 jmp .Lsqr4x_sub_entry
2071 lea 8*4($nptr),$nptr
2081 neg %r10 # mov %r10,%cf
2087 lea 8*4($tptr),$tptr
2089 sbb %r10,%r10 # mov %cf,%r10
2092 lea 8*4($rptr),$rptr
2097 mov $num,%r10 # prepare for back-to-back call
2098 neg $num # restore $num
2101 .size __bn_post4x_internal,.-__bn_post4x_internal
2106 .globl bn_from_montgomery
2107 .type bn_from_montgomery,\@abi-omnipotent
2111 testl \$7,`($win64?"48(%rsp)":"%r9d")`
2116 .size bn_from_montgomery,.-bn_from_montgomery
2118 .type bn_from_mont8x,\@function,6
2124 .cfi_def_cfa_register %rax
2139 shl \$3,${num}d # convert $num to bytes
2140 lea ($num,$num,2),%r10 # 3*$num in bytes
2144 ##############################################################
2145 # Ensure that stack frame doesn't alias with $rptr+3*$num
2146 # modulo 4096, which covers ret[num], am[num] and n[num]
2147 # (see bn_exp.c). The stack is allocated to aligned with
2148 # bn_power5's frame, and as bn_from_montgomery happens to be
2149 # last operation, we use the opportunity to cleanse it.
2151 lea -320(%rsp,$num,2),%r11
2157 sub %r11,%rbp # align with $aptr
2158 lea -320(%rbp,$num,2),%rbp # future alloca(frame+2*$num*8+256)
2163 lea 4096-320(,$num,2),%r10
2164 lea -320(%rbp,$num,2),%rbp # future alloca(frame+2*$num*8+256)
2174 lea (%rbp,%r11),%rsp
2178 jmp .Lfrom_page_walk_done
2181 lea -4096(%rsp),%rsp
2185 .Lfrom_page_walk_done:
2190 ##############################################################
2193 # +0 saved $num, used in reduction section
2194 # +8 &t[2*$num], used in reduction section
2200 mov %rax, 40(%rsp) # save original %rsp
2201 .cfi_cfa_expression %rsp+40,deref,+8
2210 movdqu ($aptr),%xmm1
2211 movdqu 16($aptr),%xmm2
2212 movdqu 32($aptr),%xmm3
2213 movdqa %xmm0,(%rax,$num)
2214 movdqu 48($aptr),%xmm4
2215 movdqa %xmm0,16(%rax,$num)
2216 .byte 0x48,0x8d,0xb6,0x40,0x00,0x00,0x00 # lea 64($aptr),$aptr
2218 movdqa %xmm0,32(%rax,$num)
2219 movdqa %xmm2,16(%rax)
2220 movdqa %xmm0,48(%rax,$num)
2221 movdqa %xmm3,32(%rax)
2222 movdqa %xmm4,48(%rax)
2231 movq %r10, %xmm3 # -num
2233 $code.=<<___ if ($addx);
2234 mov OPENSSL_ia32cap_P+8(%rip),%r11d
2236 cmp \$0x80108,%r11d # check for AD*X+BMI2+BMI1
2239 lea (%rax,$num),$rptr
2240 call __bn_sqrx8x_reduction
2241 call __bn_postx4x_internal
2245 jmp .Lfrom_mont_zero
2251 call __bn_sqr8x_reduction
2252 call __bn_post4x_internal
2256 jmp .Lfrom_mont_zero
2260 mov 40(%rsp),%rsi # restore %rsp
2262 movdqa %xmm0,16*0(%rax)
2263 movdqa %xmm0,16*1(%rax)
2264 movdqa %xmm0,16*2(%rax)
2265 movdqa %xmm0,16*3(%rax)
2268 jnz .Lfrom_mont_zero
2284 .cfi_def_cfa_register %rsp
2288 .size bn_from_mont8x,.-bn_from_mont8x
2294 my $bp="%rdx"; # restore original value
2297 .type bn_mulx4x_mont_gather5,\@function,6
2299 bn_mulx4x_mont_gather5:
2302 .cfi_def_cfa_register %rax
2318 shl \$3,${num}d # convert $num to bytes
2319 lea ($num,$num,2),%r10 # 3*$num in bytes
2323 ##############################################################
2324 # Ensure that stack frame doesn't alias with $rptr+3*$num
2325 # modulo 4096, which covers ret[num], am[num] and n[num]
2326 # (see bn_exp.c). This is done to allow memory disambiguation
2327 # logic do its magic. [Extra [num] is allocated in order
2328 # to align with bn_power5's frame, which is cleansed after
2329 # completing exponentiation. Extra 256 bytes is for power mask
2330 # calculated from 7th argument, the index.]
2332 lea -320(%rsp,$num,2),%r11
2338 sub %r11,%rbp # align with $aptr
2339 lea -320(%rbp,$num,2),%rbp # future alloca(frame+2*$num*8+256)
2343 lea 4096-320(,$num,2),%r10
2344 lea -320(%rbp,$num,2),%rbp # future alloca(frame+2*$num*8+256)
2350 and \$-64,%rbp # ensure alignment
2354 lea (%rbp,%r11),%rsp
2357 ja .Lmulx4x_page_walk
2358 jmp .Lmulx4x_page_walk_done
2361 lea -4096(%rsp),%rsp
2364 ja .Lmulx4x_page_walk
2365 .Lmulx4x_page_walk_done:
2367 ##############################################################
2370 # +8 off-loaded &b[i]
2379 mov $n0, 32(%rsp) # save *n0
2380 mov %rax,40(%rsp) # save original %rsp
2381 .cfi_cfa_expression %rsp+40,deref,+8
2383 call mulx4x_internal
2385 mov 40(%rsp),%rsi # restore %rsp
2402 .cfi_def_cfa_register %rsp
2406 .size bn_mulx4x_mont_gather5,.-bn_mulx4x_mont_gather5
2408 .type mulx4x_internal,\@abi-omnipotent
2412 mov $num,8(%rsp) # save -$num (it was in bytes)
2414 neg $num # restore $num
2416 neg %r10 # restore $num
2417 lea 128($bp,$num),%r13 # end of powers table (+size optimization)
2419 movd `($win64?56:8)`(%rax),%xmm5 # load 7th argument
2421 lea .Linc(%rip),%rax
2422 mov %r13,16+8(%rsp) # end of b[num]
2423 mov $num,24+8(%rsp) # inner counter
2424 mov $rp, 56+8(%rsp) # save $rp
2426 my ($aptr, $bptr, $nptr, $tptr, $mi, $bi, $zero, $num)=
2427 ("%rsi","%rdi","%rcx","%rbx","%r8","%r9","%rbp","%rax");
2429 my $STRIDE=2**5*8; # 5 is "window size"
2430 my $N=$STRIDE/4; # should match cache line size
2432 movdqa 0(%rax),%xmm0 # 00000001000000010000000000000000
2433 movdqa 16(%rax),%xmm1 # 00000002000000020000000200000002
2434 lea 88-112(%rsp,%r10),%r10 # place the mask after tp[num+1] (+ICache optimization)
2435 lea 128($bp),$bptr # size optimization
2437 pshufd \$0,%xmm5,%xmm5 # broadcast index
2442 ########################################################################
2443 # calculate mask by comparing 0..31 to index and save result to stack
2448 pcmpeqd %xmm5,%xmm0 # compare to 1,0
2451 for($i=0;$i<$STRIDE/16-4;$i+=4) {
2454 pcmpeqd %xmm5,%xmm1 # compare to 3,2
2455 movdqa %xmm0,`16*($i+0)+112`(%r10)
2459 pcmpeqd %xmm5,%xmm2 # compare to 5,4
2460 movdqa %xmm1,`16*($i+1)+112`(%r10)
2464 pcmpeqd %xmm5,%xmm3 # compare to 7,6
2465 movdqa %xmm2,`16*($i+2)+112`(%r10)
2470 movdqa %xmm3,`16*($i+3)+112`(%r10)
2474 $code.=<<___; # last iteration can be optimized
2478 movdqa %xmm0,`16*($i+0)+112`(%r10)
2482 movdqa %xmm1,`16*($i+1)+112`(%r10)
2485 movdqa %xmm2,`16*($i+2)+112`(%r10)
2487 pand `16*($i+0)-128`($bptr),%xmm0 # while it's still in register
2488 pand `16*($i+1)-128`($bptr),%xmm1
2489 pand `16*($i+2)-128`($bptr),%xmm2
2490 movdqa %xmm3,`16*($i+3)+112`(%r10)
2491 pand `16*($i+3)-128`($bptr),%xmm3
2495 for($i=0;$i<$STRIDE/16-4;$i+=4) {
2497 movdqa `16*($i+0)-128`($bptr),%xmm4
2498 movdqa `16*($i+1)-128`($bptr),%xmm5
2499 movdqa `16*($i+2)-128`($bptr),%xmm2
2500 pand `16*($i+0)+112`(%r10),%xmm4
2501 movdqa `16*($i+3)-128`($bptr),%xmm3
2502 pand `16*($i+1)+112`(%r10),%xmm5
2504 pand `16*($i+2)+112`(%r10),%xmm2
2506 pand `16*($i+3)+112`(%r10),%xmm3
2513 pshufd \$0x4e,%xmm0,%xmm1
2515 lea $STRIDE($bptr),$bptr
2516 movq %xmm0,%rdx # bp[0]
2517 lea 64+8*4+8(%rsp),$tptr
2520 mulx 0*8($aptr),$mi,%rax # a[0]*b[0]
2521 mulx 1*8($aptr),%r11,%r12 # a[1]*b[0]
2523 mulx 2*8($aptr),%rax,%r13 # ...
2526 mulx 3*8($aptr),%rax,%r14
2529 imulq 32+8(%rsp),$mi # "t[0]"*n0
2530 xor $zero,$zero # cf=0, of=0
2533 mov $bptr,8+8(%rsp) # off-load &b[i]
2535 lea 4*8($aptr),$aptr
2537 adcx $zero,%r14 # cf=0
2539 mulx 0*8($nptr),%rax,%r10
2540 adcx %rax,%r15 # discarded
2542 mulx 1*8($nptr),%rax,%r11
2545 mulx 2*8($nptr),%rax,%r12
2546 mov 24+8(%rsp),$bptr # counter value
2547 mov %r10,-8*4($tptr)
2550 mulx 3*8($nptr),%rax,%r15
2552 mov %r11,-8*3($tptr)
2554 adox $zero,%r15 # of=0
2555 lea 4*8($nptr),$nptr
2556 mov %r12,-8*2($tptr)
2561 adcx $zero,%r15 # cf=0, modulo-scheduled
2562 mulx 0*8($aptr),%r10,%rax # a[4]*b[0]
2564 mulx 1*8($aptr),%r11,%r14 # a[5]*b[0]
2566 mulx 2*8($aptr),%r12,%rax # ...
2568 mulx 3*8($aptr),%r13,%r14
2572 adcx $zero,%r14 # cf=0
2573 lea 4*8($aptr),$aptr
2574 lea 4*8($tptr),$tptr
2577 mulx 0*8($nptr),%rax,%r15
2580 mulx 1*8($nptr),%rax,%r15
2583 mulx 2*8($nptr),%rax,%r15
2584 mov %r10,-5*8($tptr)
2586 mov %r11,-4*8($tptr)
2588 mulx 3*8($nptr),%rax,%r15
2590 mov %r12,-3*8($tptr)
2593 lea 4*8($nptr),$nptr
2594 mov %r13,-2*8($tptr)
2596 dec $bptr # of=0, pass cf
2599 mov 8(%rsp),$num # load -num
2600 adc $zero,%r15 # modulo-scheduled
2601 lea ($aptr,$num),$aptr # rewind $aptr
2603 mov 8+8(%rsp),$bptr # re-load &b[i]
2604 adc $zero,$zero # top-most carry
2605 mov %r14,-1*8($tptr)
2610 lea 16-256($tptr),%r10 # where 256-byte mask is (+density control)
2615 for($i=0;$i<$STRIDE/16;$i+=4) {
2617 movdqa `16*($i+0)-128`($bptr),%xmm0
2618 movdqa `16*($i+1)-128`($bptr),%xmm1
2619 movdqa `16*($i+2)-128`($bptr),%xmm2
2620 pand `16*($i+0)+256`(%r10),%xmm0
2621 movdqa `16*($i+3)-128`($bptr),%xmm3
2622 pand `16*($i+1)+256`(%r10),%xmm1
2624 pand `16*($i+2)+256`(%r10),%xmm2
2626 pand `16*($i+3)+256`(%r10),%xmm3
2633 pshufd \$0x4e,%xmm4,%xmm0
2635 lea $STRIDE($bptr),$bptr
2636 movq %xmm0,%rdx # m0=bp[i]
2638 mov $zero,($tptr) # save top-most carry
2639 lea 4*8($tptr,$num),$tptr # rewind $tptr
2640 mulx 0*8($aptr),$mi,%r11 # a[0]*b[i]
2641 xor $zero,$zero # cf=0, of=0
2643 mulx 1*8($aptr),%r14,%r12 # a[1]*b[i]
2644 adox -4*8($tptr),$mi # +t[0]
2646 mulx 2*8($aptr),%r15,%r13 # ...
2647 adox -3*8($tptr),%r11
2649 mulx 3*8($aptr),%rdx,%r14
2650 adox -2*8($tptr),%r12
2652 lea ($nptr,$num),$nptr # rewind $nptr
2653 lea 4*8($aptr),$aptr
2654 adox -1*8($tptr),%r13
2659 imulq 32+8(%rsp),$mi # "t[0]"*n0
2662 xor $zero,$zero # cf=0, of=0
2663 mov $bptr,8+8(%rsp) # off-load &b[i]
2665 mulx 0*8($nptr),%rax,%r10
2666 adcx %rax,%r15 # discarded
2668 mulx 1*8($nptr),%rax,%r11
2671 mulx 2*8($nptr),%rax,%r12
2674 mulx 3*8($nptr),%rax,%r15
2676 mov 24+8(%rsp),$bptr # counter value
2677 mov %r10,-8*4($tptr)
2679 mov %r11,-8*3($tptr)
2680 adox $zero,%r15 # of=0
2681 mov %r12,-8*2($tptr)
2682 lea 4*8($nptr),$nptr
2687 mulx 0*8($aptr),%r10,%rax # a[4]*b[i]
2688 adcx $zero,%r15 # cf=0, modulo-scheduled
2690 mulx 1*8($aptr),%r11,%r14 # a[5]*b[i]
2691 adcx 0*8($tptr),%r10
2693 mulx 2*8($aptr),%r12,%rax # ...
2694 adcx 1*8($tptr),%r11
2696 mulx 3*8($aptr),%r13,%r14
2698 adcx 2*8($tptr),%r12
2700 adcx 3*8($tptr),%r13
2701 adox $zero,%r14 # of=0
2702 lea 4*8($aptr),$aptr
2703 lea 4*8($tptr),$tptr
2704 adcx $zero,%r14 # cf=0
2707 mulx 0*8($nptr),%rax,%r15
2710 mulx 1*8($nptr),%rax,%r15
2713 mulx 2*8($nptr),%rax,%r15
2714 mov %r10,-5*8($tptr)
2717 mov %r11,-4*8($tptr)
2718 mulx 3*8($nptr),%rax,%r15
2720 lea 4*8($nptr),$nptr
2721 mov %r12,-3*8($tptr)
2724 mov %r13,-2*8($tptr)
2726 dec $bptr # of=0, pass cf
2729 mov 0+8(%rsp),$num # load -num
2730 adc $zero,%r15 # modulo-scheduled
2731 sub 0*8($tptr),$bptr # pull top-most carry to %cf
2732 mov 8+8(%rsp),$bptr # re-load &b[i]
2735 lea ($aptr,$num),$aptr # rewind $aptr
2736 adc $zero,$zero # top-most carry
2737 mov %r14,-1*8($tptr)
2744 mov ($nptr,$num),%r12
2745 lea ($nptr,$num),%rbp # rewind $nptr
2747 lea ($tptr,$num),%rdi # rewind $tptr
2750 sub %r14,%r10 # compare top-most words
2754 sub %r8,%rax # %rax=-%r8
2755 mov 56+8(%rsp),%rdx # restore rp
2756 dec %r12 # so that after 'not' we get -n[0]
2761 jmp .Lsqrx4x_sub_entry # common post-condition
2763 .size mulx4x_internal,.-mulx4x_internal
2766 ######################################################################
2768 my $rptr="%rdi"; # BN_ULONG *rptr,
2769 my $aptr="%rsi"; # const BN_ULONG *aptr,
2770 my $bptr="%rdx"; # const void *table,
2771 my $nptr="%rcx"; # const BN_ULONG *nptr,
2772 my $n0 ="%r8"; # const BN_ULONG *n0);
2773 my $num ="%r9"; # int num, has to be divisible by 8
2776 my ($i,$j,$tptr)=("%rbp","%rcx",$rptr);
2777 my @A0=("%r10","%r11");
2778 my @A1=("%r12","%r13");
2779 my ($a0,$a1,$ai)=("%r14","%r15","%rbx");
2782 .type bn_powerx5,\@function,6
2787 .cfi_def_cfa_register %rax
2803 shl \$3,${num}d # convert $num to bytes
2804 lea ($num,$num,2),%r10 # 3*$num in bytes
2808 ##############################################################
2809 # Ensure that stack frame doesn't alias with $rptr+3*$num
2810 # modulo 4096, which covers ret[num], am[num] and n[num]
2811 # (see bn_exp.c). This is done to allow memory disambiguation
2812 # logic do its magic. [Extra 256 bytes is for power mask
2813 # calculated from 7th argument, the index.]
2815 lea -320(%rsp,$num,2),%r11
2821 sub %r11,%rbp # align with $aptr
2822 lea -320(%rbp,$num,2),%rbp # future alloca(frame+2*$num*8+256)
2827 lea 4096-320(,$num,2),%r10
2828 lea -320(%rbp,$num,2),%rbp # alloca(frame+2*$num*8+256)
2838 lea (%rbp,%r11),%rsp
2842 jmp .Lpwrx_page_walk_done
2845 lea -4096(%rsp),%rsp
2849 .Lpwrx_page_walk_done:
2854 ##############################################################
2857 # +0 saved $num, used in reduction section
2858 # +8 &t[2*$num], used in reduction section
2859 # +16 intermediate carry bit
2860 # +24 top-most carry bit, used in reduction section
2866 movq $rptr,%xmm1 # save $rptr
2867 movq $nptr,%xmm2 # save $nptr
2868 movq %r10, %xmm3 # -$num
2871 mov %rax, 40(%rsp) # save original %rsp
2872 .cfi_cfa_expression %rsp+40,deref,+8
2875 call __bn_sqrx8x_internal
2876 call __bn_postx4x_internal
2877 call __bn_sqrx8x_internal
2878 call __bn_postx4x_internal
2879 call __bn_sqrx8x_internal
2880 call __bn_postx4x_internal
2881 call __bn_sqrx8x_internal
2882 call __bn_postx4x_internal
2883 call __bn_sqrx8x_internal
2884 call __bn_postx4x_internal
2886 mov %r10,$num # -num
2892 call mulx4x_internal
2894 mov 40(%rsp),%rsi # restore %rsp
2911 .cfi_def_cfa_register %rsp
2915 .size bn_powerx5,.-bn_powerx5
2917 .globl bn_sqrx8x_internal
2918 .hidden bn_sqrx8x_internal
2919 .type bn_sqrx8x_internal,\@abi-omnipotent
2922 __bn_sqrx8x_internal:
2924 ##################################################################
2927 # a) multiply-n-add everything but a[i]*a[i];
2928 # b) shift result of a) by 1 to the left and accumulate
2929 # a[i]*a[i] products;
2931 ##################################################################
2932 # a[7]a[7]a[6]a[6]a[5]a[5]a[4]a[4]a[3]a[3]a[2]a[2]a[1]a[1]a[0]a[0]
2963 # a[7]a[7]a[6]a[6]a[5]a[5]a[4]a[4]a[3]a[3]a[2]a[2]a[1]a[1]a[0]a[0]
2966 my ($zero,$carry)=("%rbp","%rcx");
2969 lea 48+8(%rsp),$tptr
2970 lea ($aptr,$num),$aaptr
2971 mov $num,0+8(%rsp) # save $num
2972 mov $aaptr,8+8(%rsp) # save end of $aptr
2973 jmp .Lsqr8x_zero_start
2976 .byte 0x66,0x66,0x66,0x2e,0x0f,0x1f,0x84,0x00,0x00,0x00,0x00,0x00
2979 movdqa %xmm0,0*8($tptr)
2980 movdqa %xmm0,2*8($tptr)
2981 movdqa %xmm0,4*8($tptr)
2982 movdqa %xmm0,6*8($tptr)
2983 .Lsqr8x_zero_start: # aligned at 32
2984 movdqa %xmm0,8*8($tptr)
2985 movdqa %xmm0,10*8($tptr)
2986 movdqa %xmm0,12*8($tptr)
2987 movdqa %xmm0,14*8($tptr)
2988 lea 16*8($tptr),$tptr
2992 mov 0*8($aptr),%rdx # a[0], modulo-scheduled
2993 #xor %r9,%r9 # t[1], ex-$num, zero already
3000 lea 48+8(%rsp),$tptr
3001 xor $zero,$zero # cf=0, cf=0
3002 jmp .Lsqrx8x_outer_loop
3005 .Lsqrx8x_outer_loop:
3006 mulx 1*8($aptr),%r8,%rax # a[1]*a[0]
3007 adcx %r9,%r8 # a[1]*a[0]+=t[1]
3009 mulx 2*8($aptr),%r9,%rax # a[2]*a[0]
3012 .byte 0xc4,0xe2,0xab,0xf6,0x86,0x18,0x00,0x00,0x00 # mulx 3*8($aptr),%r10,%rax # ...
3015 .byte 0xc4,0xe2,0xa3,0xf6,0x86,0x20,0x00,0x00,0x00 # mulx 4*8($aptr),%r11,%rax
3018 mulx 5*8($aptr),%r12,%rax
3021 mulx 6*8($aptr),%r13,%rax
3024 mulx 7*8($aptr),%r14,%r15
3025 mov 1*8($aptr),%rdx # a[1]
3029 mov %r8,1*8($tptr) # t[1]
3030 mov %r9,2*8($tptr) # t[2]
3031 sbb $carry,$carry # mov %cf,$carry
3032 xor $zero,$zero # cf=0, of=0
3035 mulx 2*8($aptr),%r8,%rbx # a[2]*a[1]
3036 mulx 3*8($aptr),%r9,%rax # a[3]*a[1]
3039 mulx 4*8($aptr),%r10,%rbx # ...
3042 .byte 0xc4,0xe2,0xa3,0xf6,0x86,0x28,0x00,0x00,0x00 # mulx 5*8($aptr),%r11,%rax
3045 .byte 0xc4,0xe2,0x9b,0xf6,0x9e,0x30,0x00,0x00,0x00 # mulx 6*8($aptr),%r12,%rbx
3048 .byte 0xc4,0x62,0x93,0xf6,0xb6,0x38,0x00,0x00,0x00 # mulx 7*8($aptr),%r13,%r14
3049 mov 2*8($aptr),%rdx # a[2]
3053 adox $zero,%r14 # of=0
3054 adcx $zero,%r14 # cf=0
3056 mov %r8,3*8($tptr) # t[3]
3057 mov %r9,4*8($tptr) # t[4]
3059 mulx 3*8($aptr),%r8,%rbx # a[3]*a[2]
3060 mulx 4*8($aptr),%r9,%rax # a[4]*a[2]
3063 mulx 5*8($aptr),%r10,%rbx # ...
3066 .byte 0xc4,0xe2,0xa3,0xf6,0x86,0x30,0x00,0x00,0x00 # mulx 6*8($aptr),%r11,%rax
3069 .byte 0xc4,0x62,0x9b,0xf6,0xae,0x38,0x00,0x00,0x00 # mulx 7*8($aptr),%r12,%r13
3071 mov 3*8($aptr),%rdx # a[3]
3075 mov %r8,5*8($tptr) # t[5]
3076 mov %r9,6*8($tptr) # t[6]
3077 mulx 4*8($aptr),%r8,%rax # a[4]*a[3]
3078 adox $zero,%r13 # of=0
3079 adcx $zero,%r13 # cf=0
3081 mulx 5*8($aptr),%r9,%rbx # a[5]*a[3]
3084 mulx 6*8($aptr),%r10,%rax # ...
3087 mulx 7*8($aptr),%r11,%r12
3088 mov 4*8($aptr),%rdx # a[4]
3089 mov 5*8($aptr),%r14 # a[5]
3092 mov 6*8($aptr),%r15 # a[6]
3094 adox $zero,%r12 # of=0
3095 adcx $zero,%r12 # cf=0
3097 mov %r8,7*8($tptr) # t[7]
3098 mov %r9,8*8($tptr) # t[8]
3100 mulx %r14,%r9,%rax # a[5]*a[4]
3101 mov 7*8($aptr),%r8 # a[7]
3103 mulx %r15,%r10,%rbx # a[6]*a[4]
3106 mulx %r8,%r11,%rax # a[7]*a[4]
3107 mov %r14,%rdx # a[5]
3110 #adox $zero,%rax # of=0
3111 adcx $zero,%rax # cf=0
3113 mulx %r15,%r14,%rbx # a[6]*a[5]
3114 mulx %r8,%r12,%r13 # a[7]*a[5]
3115 mov %r15,%rdx # a[6]
3116 lea 8*8($aptr),$aptr
3123 mulx %r8,%r8,%r14 # a[7]*a[6]
3128 je .Lsqrx8x_outer_break
3130 neg $carry # mov $carry,%cf
3134 adcx 9*8($tptr),%r9 # +=t[9]
3135 adcx 10*8($tptr),%r10 # ...
3136 adcx 11*8($tptr),%r11
3137 adc 12*8($tptr),%r12
3138 adc 13*8($tptr),%r13
3139 adc 14*8($tptr),%r14
3140 adc 15*8($tptr),%r15
3142 lea 2*64($tptr),$tptr
3143 sbb %rax,%rax # mov %cf,$carry
3145 mov -64($aptr),%rdx # a[0]
3146 mov %rax,16+8(%rsp) # offload $carry
3147 mov $tptr,24+8(%rsp)
3149 #lea 8*8($tptr),$tptr # see 2*8*8($tptr) above
3150 xor %eax,%eax # cf=0, of=0
3156 mulx 0*8($aaptr),%rax,%r8 # a[8]*a[i]
3157 adcx %rax,%rbx # +=t[8]
3160 mulx 1*8($aaptr),%rax,%r9 # ...
3164 mulx 2*8($aaptr),%rax,%r10
3168 mulx 3*8($aaptr),%rax,%r11
3172 .byte 0xc4,0x62,0xfb,0xf6,0xa5,0x20,0x00,0x00,0x00 # mulx 4*8($aaptr),%rax,%r12
3176 mulx 5*8($aaptr),%rax,%r13
3180 mulx 6*8($aaptr),%rax,%r14
3181 mov %rbx,($tptr,%rcx,8) # store t[8+i]
3186 .byte 0xc4,0x62,0xfb,0xf6,0xbd,0x38,0x00,0x00,0x00 # mulx 7*8($aaptr),%rax,%r15
3187 mov 8($aptr,%rcx,8),%rdx # a[i]
3189 adox %rbx,%r15 # %rbx is 0, of=0
3190 adcx %rbx,%r15 # cf=0
3196 lea 8*8($aaptr),$aaptr
3198 cmp 8+8(%rsp),$aaptr # done?
3201 sub 16+8(%rsp),%rbx # mov 16(%rsp),%cf
3212 lea 8*8($tptr),$tptr
3214 sbb %rax,%rax # mov %cf,%rax
3215 xor %ebx,%ebx # cf=0, of=0
3216 mov %rax,16+8(%rsp) # offload carry
3222 sub 16+8(%rsp),%rbx # mov 16(%rsp),%cf
3224 mov 24+8(%rsp),$carry # initial $tptr, borrow $carry
3226 mov 0*8($aptr),%rdx # a[8], modulo-scheduled
3234 cmp $carry,$tptr # cf=0, of=0
3235 je .Lsqrx8x_outer_loop
3240 mov 2*8($carry),%r10
3242 mov 3*8($carry),%r11
3244 mov 4*8($carry),%r12
3246 mov 5*8($carry),%r13
3248 mov 6*8($carry),%r14
3250 mov 7*8($carry),%r15
3252 jmp .Lsqrx8x_outer_loop
3255 .Lsqrx8x_outer_break:
3256 mov %r9,9*8($tptr) # t[9]
3257 movq %xmm3,%rcx # -$num
3258 mov %r10,10*8($tptr) # ...
3259 mov %r11,11*8($tptr)
3260 mov %r12,12*8($tptr)
3261 mov %r13,13*8($tptr)
3262 mov %r14,14*8($tptr)
3267 lea 48+8(%rsp),$tptr
3268 mov ($aptr,$i),%rdx # a[0]
3270 mov 8($tptr),$A0[1] # t[1]
3271 xor $A0[0],$A0[0] # t[0], of=0, cf=0
3272 mov 0+8(%rsp),$num # restore $num
3274 mov 16($tptr),$A1[0] # t[2] # prefetch
3275 mov 24($tptr),$A1[1] # t[3] # prefetch
3276 #jmp .Lsqrx4x_shift_n_add # happens to be aligned
3279 .Lsqrx4x_shift_n_add:
3283 .byte 0x48,0x8b,0x94,0x0e,0x08,0x00,0x00,0x00 # mov 8($aptr,$i),%rdx # a[i+1] # prefetch
3284 .byte 0x4c,0x8b,0x97,0x20,0x00,0x00,0x00 # mov 32($tptr),$A0[0] # t[2*i+4] # prefetch
3287 mov 40($tptr),$A0[1] # t[2*i+4+1] # prefetch
3294 mov 16($aptr,$i),%rdx # a[i+2] # prefetch
3295 mov 48($tptr),$A1[0] # t[2*i+6] # prefetch
3298 mov 56($tptr),$A1[1] # t[2*i+6+1] # prefetch
3305 mov 24($aptr,$i),%rdx # a[i+3] # prefetch
3307 mov 64($tptr),$A0[0] # t[2*i+8] # prefetch
3310 mov 72($tptr),$A0[1] # t[2*i+8+1] # prefetch
3317 jrcxz .Lsqrx4x_shift_n_add_break
3318 .byte 0x48,0x8b,0x94,0x0e,0x00,0x00,0x00,0x00 # mov 0($aptr,$i),%rdx # a[i+4] # prefetch
3321 mov 80($tptr),$A1[0] # t[2*i+10] # prefetch
3322 mov 88($tptr),$A1[1] # t[2*i+10+1] # prefetch
3327 jmp .Lsqrx4x_shift_n_add
3330 .Lsqrx4x_shift_n_add_break:
3334 lea 64($tptr),$tptr # end of t[] buffer
3337 ######################################################################
3338 # Montgomery reduction part, "word-by-word" algorithm.
3340 # This new path is inspired by multiple submissions from Intel, by
3341 # Shay Gueron, Vlad Krasnov, Erdinc Ozturk, James Guilford,
3344 my ($nptr,$carry,$m0)=("%rbp","%rsi","%rdx");
3348 __bn_sqrx8x_reduction:
3349 xor %eax,%eax # initial top-most carry bit
3350 mov 32+8(%rsp),%rbx # n0
3351 mov 48+8(%rsp),%rdx # "%r8", 8*0($tptr)
3352 lea -8*8($nptr,$num),%rcx # end of n[]
3353 #lea 48+8(%rsp,$num,2),$tptr # end of t[] buffer
3354 mov %rcx, 0+8(%rsp) # save end of n[]
3355 mov $tptr,8+8(%rsp) # save end of t[]
3357 lea 48+8(%rsp),$tptr # initial t[] window
3358 jmp .Lsqrx8x_reduction_loop
3361 .Lsqrx8x_reduction_loop:
3367 imulq %rbx,%rdx # n0*a[i]
3371 mov %rax,24+8(%rsp) # store top-most carry bit
3373 lea 8*8($tptr),$tptr
3374 xor $carry,$carry # cf=0,of=0
3381 mulx 8*0($nptr),%rax,%r8 # n[0]
3382 adcx %rbx,%rax # discarded
3385 mulx 8*1($nptr),%rbx,%r9 # n[1]
3389 mulx 8*2($nptr),%rbx,%r10
3393 mulx 8*3($nptr),%rbx,%r11
3397 .byte 0xc4,0x62,0xe3,0xf6,0xa5,0x20,0x00,0x00,0x00 # mulx 8*4($nptr),%rbx,%r12
3403 mulx 32+8(%rsp),%rbx,%rdx # %rdx discarded
3405 mov %rax,64+48+8(%rsp,%rcx,8) # put aside n0*a[i]
3407 mulx 8*5($nptr),%rax,%r13
3411 mulx 8*6($nptr),%rax,%r14
3415 mulx 8*7($nptr),%rax,%r15
3418 adox $carry,%r15 # $carry is 0
3419 adcx $carry,%r15 # cf=0
3421 .byte 0x67,0x67,0x67
3425 mov $carry,%rax # xor %rax,%rax
3426 cmp 0+8(%rsp),$nptr # end of n[]?
3427 jae .Lsqrx8x_no_tail
3429 mov 48+8(%rsp),%rdx # pull n0*a[0]
3431 lea 8*8($nptr),$nptr
3434 adcx 8*2($tptr),%r10
3440 lea 8*8($tptr),$tptr
3441 sbb %rax,%rax # top carry
3443 xor $carry,$carry # of=0, cf=0
3450 mulx 8*0($nptr),%rax,%r8
3454 mulx 8*1($nptr),%rax,%r9
3458 mulx 8*2($nptr),%rax,%r10
3462 mulx 8*3($nptr),%rax,%r11
3466 .byte 0xc4,0x62,0xfb,0xf6,0xa5,0x20,0x00,0x00,0x00 # mulx 8*4($nptr),%rax,%r12
3470 mulx 8*5($nptr),%rax,%r13
3474 mulx 8*6($nptr),%rax,%r14
3478 mulx 8*7($nptr),%rax,%r15
3479 mov 72+48+8(%rsp,%rcx,8),%rdx # pull n0*a[i]
3482 mov %rbx,($tptr,%rcx,8) # save result
3484 adcx $carry,%r15 # cf=0
3489 cmp 0+8(%rsp),$nptr # end of n[]?
3490 jae .Lsqrx8x_tail_done # break out of loop
3492 sub 16+8(%rsp),$carry # mov 16(%rsp),%cf
3493 mov 48+8(%rsp),%rdx # pull n0*a[0]
3494 lea 8*8($nptr),$nptr
3503 lea 8*8($tptr),$tptr
3505 sub \$8,%rcx # mov \$-8,%rcx
3507 xor $carry,$carry # of=0, cf=0
3514 add 24+8(%rsp),%r8 # can this overflow?
3524 sub 16+8(%rsp),$carry # mov 16(%rsp),%cf
3525 .Lsqrx8x_no_tail: # %cf is 0 if jumped here
3529 mov 8*7($nptr),$carry
3530 movq %xmm2,$nptr # restore $nptr
3537 adc \$0,%rax # top-most carry
3539 mov 32+8(%rsp),%rbx # n0
3540 mov 8*8($tptr,%rcx),%rdx # modulo-scheduled "%r8"
3542 mov %r8,8*0($tptr) # store top 512 bits
3543 lea 8*8($tptr),%r8 # borrow %r8
3552 lea 8*8($tptr,%rcx),$tptr # start of current t[] window
3553 cmp 8+8(%rsp),%r8 # end of t[]?
3554 jb .Lsqrx8x_reduction_loop
3557 .size bn_sqrx8x_internal,.-bn_sqrx8x_internal
3560 ##############################################################
3561 # Post-condition, 4x unrolled
3564 my ($rptr,$nptr)=("%rdx","%rbp");
3567 __bn_postx4x_internal:
3570 mov %rcx,%r10 # -$num
3571 mov %rcx,%r9 # -$num
3574 #lea 48+8(%rsp,%r9),$tptr
3575 movq %xmm1,$rptr # restore $rptr
3576 movq %xmm1,$aptr # prepare for back-to-back call
3577 dec %r12 # so that after 'not' we get -n[0]
3582 jmp .Lsqrx4x_sub_entry
3592 lea 8*4($nptr),$nptr
3597 neg %r8 # mov %r8,%cf
3603 lea 8*4($tptr),$tptr
3605 sbb %r8,%r8 # mov %cf,%r8
3608 lea 8*4($rptr),$rptr
3613 neg %r9 # restore $num
3617 .size __bn_postx4x_internal,.-__bn_postx4x_internal
3622 my ($inp,$num,$tbl,$idx)=$win64?("%rcx","%edx","%r8", "%r9d") : # Win64 order
3623 ("%rdi","%esi","%rdx","%ecx"); # Unix order
3630 .type bn_get_bits5,\@abi-omnipotent
3643 movzw (%r10,$num,2),%eax
3648 .size bn_get_bits5,.-bn_get_bits5
3651 .type bn_scatter5,\@abi-omnipotent
3656 jz .Lscatter_epilogue
3657 lea ($tbl,$idx,8),$tbl
3668 .size bn_scatter5,.-bn_scatter5
3671 .type bn_gather5,\@abi-omnipotent
3674 .LSEH_begin_bn_gather5: # Win64 thing, but harmless in other cases
3676 # I can't trust assembler to use specific encoding:-(
3677 .byte 0x4c,0x8d,0x14,0x24 #lea (%rsp),%r10
3678 .byte 0x48,0x81,0xec,0x08,0x01,0x00,0x00 #sub $0x108,%rsp
3679 lea .Linc(%rip),%rax
3680 and \$-16,%rsp # shouldn't be formally required
3683 movdqa 0(%rax),%xmm0 # 00000001000000010000000000000000
3684 movdqa 16(%rax),%xmm1 # 00000002000000020000000200000002
3685 lea 128($tbl),%r11 # size optimization
3686 lea 128(%rsp),%rax # size optimization
3688 pshufd \$0,%xmm5,%xmm5 # broadcast $idx
3692 ########################################################################
3693 # calculate mask by comparing 0..31 to $idx and save result to stack
3695 for($i=0;$i<$STRIDE/16;$i+=4) {
3698 pcmpeqd %xmm5,%xmm0 # compare to 1,0
3700 $code.=<<___ if ($i);
3701 movdqa %xmm3,`16*($i-1)-128`(%rax)
3707 pcmpeqd %xmm5,%xmm1 # compare to 3,2
3708 movdqa %xmm0,`16*($i+0)-128`(%rax)
3712 pcmpeqd %xmm5,%xmm2 # compare to 5,4
3713 movdqa %xmm1,`16*($i+1)-128`(%rax)
3717 pcmpeqd %xmm5,%xmm3 # compare to 7,6
3718 movdqa %xmm2,`16*($i+2)-128`(%rax)
3723 movdqa %xmm3,`16*($i-1)-128`(%rax)
3731 for($i=0;$i<$STRIDE/16;$i+=4) {
3733 movdqa `16*($i+0)-128`(%r11),%xmm0
3734 movdqa `16*($i+1)-128`(%r11),%xmm1
3735 movdqa `16*($i+2)-128`(%r11),%xmm2
3736 pand `16*($i+0)-128`(%rax),%xmm0
3737 movdqa `16*($i+3)-128`(%r11),%xmm3
3738 pand `16*($i+1)-128`(%rax),%xmm1
3740 pand `16*($i+2)-128`(%rax),%xmm2
3742 pand `16*($i+3)-128`(%rax),%xmm3
3749 lea $STRIDE(%r11),%r11
3750 pshufd \$0x4e,%xmm4,%xmm0
3752 movq %xmm0,($out) # m0=bp[0]
3759 .LSEH_end_bn_gather5:
3761 .size bn_gather5,.-bn_gather5
3769 .asciz "Montgomery Multiplication with scatter/gather for x86_64, CRYPTOGAMS by <appro\@openssl.org>"
3772 # EXCEPTION_DISPOSITION handler (EXCEPTION_RECORD *rec,ULONG64 frame,
3773 # CONTEXT *context,DISPATCHER_CONTEXT *disp)
3781 .extern __imp_RtlVirtualUnwind
3782 .type mul_handler,\@abi-omnipotent
3796 mov 120($context),%rax # pull context->Rax
3797 mov 248($context),%rbx # pull context->Rip
3799 mov 8($disp),%rsi # disp->ImageBase
3800 mov 56($disp),%r11 # disp->HandlerData
3802 mov 0(%r11),%r10d # HandlerData[0]
3803 lea (%rsi,%r10),%r10 # end of prologue label
3804 cmp %r10,%rbx # context->Rip<end of prologue label
3805 jb .Lcommon_seh_tail
3807 mov 4(%r11),%r10d # HandlerData[1]
3808 lea (%rsi,%r10),%r10 # beginning of body label
3809 cmp %r10,%rbx # context->Rip<body label
3810 jb .Lcommon_pop_regs
3812 mov 152($context),%rax # pull context->Rsp
3814 mov 8(%r11),%r10d # HandlerData[2]
3815 lea (%rsi,%r10),%r10 # epilogue label
3816 cmp %r10,%rbx # context->Rip>=epilogue label
3817 jae .Lcommon_seh_tail
3819 lea .Lmul_epilogue(%rip),%r10
3823 mov 192($context),%r10 # pull $num
3824 mov 8(%rax,%r10,8),%rax # pull saved stack pointer
3826 jmp .Lcommon_pop_regs
3829 mov 40(%rax),%rax # pull saved stack pointer
3837 mov %rbx,144($context) # restore context->Rbx
3838 mov %rbp,160($context) # restore context->Rbp
3839 mov %r12,216($context) # restore context->R12
3840 mov %r13,224($context) # restore context->R13
3841 mov %r14,232($context) # restore context->R14
3842 mov %r15,240($context) # restore context->R15
3847 mov %rax,152($context) # restore context->Rsp
3848 mov %rsi,168($context) # restore context->Rsi
3849 mov %rdi,176($context) # restore context->Rdi
3851 mov 40($disp),%rdi # disp->ContextRecord
3852 mov $context,%rsi # context
3853 mov \$154,%ecx # sizeof(CONTEXT)
3854 .long 0xa548f3fc # cld; rep movsq
3857 xor %rcx,%rcx # arg1, UNW_FLAG_NHANDLER
3858 mov 8(%rsi),%rdx # arg2, disp->ImageBase
3859 mov 0(%rsi),%r8 # arg3, disp->ControlPc
3860 mov 16(%rsi),%r9 # arg4, disp->FunctionEntry
3861 mov 40(%rsi),%r10 # disp->ContextRecord
3862 lea 56(%rsi),%r11 # &disp->HandlerData
3863 lea 24(%rsi),%r12 # &disp->EstablisherFrame
3864 mov %r10,32(%rsp) # arg5
3865 mov %r11,40(%rsp) # arg6
3866 mov %r12,48(%rsp) # arg7
3867 mov %rcx,56(%rsp) # arg8, (NULL)
3868 call *__imp_RtlVirtualUnwind(%rip)
3870 mov \$1,%eax # ExceptionContinueSearch
3882 .size mul_handler,.-mul_handler
3886 .rva .LSEH_begin_bn_mul_mont_gather5
3887 .rva .LSEH_end_bn_mul_mont_gather5
3888 .rva .LSEH_info_bn_mul_mont_gather5
3890 .rva .LSEH_begin_bn_mul4x_mont_gather5
3891 .rva .LSEH_end_bn_mul4x_mont_gather5
3892 .rva .LSEH_info_bn_mul4x_mont_gather5
3894 .rva .LSEH_begin_bn_power5
3895 .rva .LSEH_end_bn_power5
3896 .rva .LSEH_info_bn_power5
3898 .rva .LSEH_begin_bn_from_mont8x
3899 .rva .LSEH_end_bn_from_mont8x
3900 .rva .LSEH_info_bn_from_mont8x
3902 $code.=<<___ if ($addx);
3903 .rva .LSEH_begin_bn_mulx4x_mont_gather5
3904 .rva .LSEH_end_bn_mulx4x_mont_gather5
3905 .rva .LSEH_info_bn_mulx4x_mont_gather5
3907 .rva .LSEH_begin_bn_powerx5
3908 .rva .LSEH_end_bn_powerx5
3909 .rva .LSEH_info_bn_powerx5
3912 .rva .LSEH_begin_bn_gather5
3913 .rva .LSEH_end_bn_gather5
3914 .rva .LSEH_info_bn_gather5
3918 .LSEH_info_bn_mul_mont_gather5:
3921 .rva .Lmul_body,.Lmul_body,.Lmul_epilogue # HandlerData[]
3923 .LSEH_info_bn_mul4x_mont_gather5:
3926 .rva .Lmul4x_prologue,.Lmul4x_body,.Lmul4x_epilogue # HandlerData[]
3928 .LSEH_info_bn_power5:
3931 .rva .Lpower5_prologue,.Lpower5_body,.Lpower5_epilogue # HandlerData[]
3933 .LSEH_info_bn_from_mont8x:
3936 .rva .Lfrom_prologue,.Lfrom_body,.Lfrom_epilogue # HandlerData[]
3938 $code.=<<___ if ($addx);
3940 .LSEH_info_bn_mulx4x_mont_gather5:
3943 .rva .Lmulx4x_prologue,.Lmulx4x_body,.Lmulx4x_epilogue # HandlerData[]
3945 .LSEH_info_bn_powerx5:
3948 .rva .Lpowerx5_prologue,.Lpowerx5_body,.Lpowerx5_epilogue # HandlerData[]
3952 .LSEH_info_bn_gather5:
3953 .byte 0x01,0x0b,0x03,0x0a
3954 .byte 0x0b,0x01,0x21,0x00 # sub rsp,0x108
3955 .byte 0x04,0xa3,0x00,0x00 # lea r10,(rsp)
3960 $code =~ s/\`([^\`]*)\`/eval($1)/gem;
3963 close STDOUT or die "error closing STDOUT: $!";