3 # ====================================================================
4 # Written by Andy Polyakov <appro@openssl.org> for the OpenSSL
5 # project. The module is, however, dual licensed under OpenSSL and
6 # CRYPTOGAMS licenses depending on where you obtain it. For further
7 # details see http://www.openssl.org/~appro/cryptogams/.
8 # ====================================================================
12 # Companion to x86_64-mont.pl that optimizes cache-timing attack
13 # countermeasures. The subroutines are produced by replacing bp[i]
14 # references in their x86_64-mont.pl counterparts with cache-neutral
15 # references to powers table computed in BN_mod_exp_mont_consttime.
16 # In addition subroutine that scatters elements of the powers table
17 # is implemented, so that scatter-/gathering can be tuned without
18 # bn_exp.c modifications.
22 # Add MULX/AD*X code paths and additional interfaces to optimize for
23 # branch prediction unit. For input lengths that are multiples of 8
24 # the np argument is not just modulus value, but one interleaved
25 # with 0. This is to optimize post-condition...
29 if ($flavour =~ /\./) { $output = $flavour; undef $flavour; }
31 $win64=0; $win64=1 if ($flavour =~ /[nm]asm|mingw64/ || $output =~ /\.asm$/);
33 $0 =~ m/(.*[\/\\])[^\/\\]+$/; $dir=$1;
34 ( $xlate="${dir}x86_64-xlate.pl" and -f $xlate ) or
35 ( $xlate="${dir}../../perlasm/x86_64-xlate.pl" and -f $xlate) or
36 die "can't locate x86_64-xlate.pl";
38 open OUT,"| \"$^X\" $xlate $flavour $output";
41 if (`$ENV{CC} -Wa,-v -c -o /dev/null -x assembler /dev/null 2>&1`
42 =~ /GNU assembler version ([2-9]\.[0-9]+)/) {
46 if (!$addx && $win64 && ($flavour =~ /nasm/ || $ENV{ASM} =~ /nasm/) &&
47 `nasm -v 2>&1` =~ /NASM version ([2-9]\.[0-9]+)/) {
51 if (!$addx && $win64 && ($flavour =~ /masm/ || $ENV{ASM} =~ /ml64/) &&
52 `ml64 2>&1` =~ /Version ([0-9]+)\./) {
56 if (!$addx && `$ENV{CC} -v 2>&1` =~ /((?:^clang|LLVM) version|.*based on LLVM) ([3-9])\.([0-9]+)/) {
57 my $ver = $2 + $3/100.0; # 3.1->3.01, 3.10->3.10
61 # int bn_mul_mont_gather5(
62 $rp="%rdi"; # BN_ULONG *rp,
63 $ap="%rsi"; # const BN_ULONG *ap,
64 $bp="%rdx"; # const BN_ULONG *bp,
65 $np="%rcx"; # const BN_ULONG *np,
66 $n0="%r8"; # const BN_ULONG *n0,
67 $num="%r9"; # int num,
68 # int idx); # 0 to 2^5-1, "index" in $bp holding
69 # pre-computed powers of a', interlaced
70 # in such manner that b[0] is $bp[idx],
71 # b[1] is [2^5+idx], etc.
83 .extern OPENSSL_ia32cap_P
85 .globl bn_mul_mont_gather5
86 .type bn_mul_mont_gather5,\@function,6
92 $code.=<<___ if ($addx);
93 mov OPENSSL_ia32cap_P+8(%rip),%r11d
102 movd `($win64?56:8)`(%rsp),%xmm5 # load 7th argument
113 lea -264(%rsp,%r11,8),%rsp # tp=alloca(8*(num+2)+256+8)
114 and \$-1024,%rsp # minimize TLB usage
116 mov %rax,8(%rsp,$num,8) # tp[num+1]=%rsp
118 # Some OSes (Windows) insist on stack being "wired" to
119 # physical memory in strictly sequential manner, i.e. if stack
120 # allocation spans two pages, then reference to farmost one can
121 # be punishable by SEGV. But page walking can do good even on
122 # other OSes, because it guarantees that villain thread hits
123 # the guard page before it can make damage to innocent one...
129 .byte 0x2e # predict non-taken
132 lea 128($bp),%r12 # reassign $bp (+size optimization)
135 $STRIDE=2**5*8; # 5 is "window size"
136 $N=$STRIDE/4; # should match cache line size
138 movdqa 0(%r10),%xmm0 # 00000001000000010000000000000000
139 movdqa 16(%r10),%xmm1 # 00000002000000020000000200000002
140 lea 24-112(%rsp,$num,8),%r10# place the mask after tp[num+3] (+ICache optimization)
143 pshufd \$0,%xmm5,%xmm5 # broadcast index
147 ########################################################################
148 # calculate mask by comparing 0..31 to index and save result to stack
152 pcmpeqd %xmm5,%xmm0 # compare to 1,0
156 for($k=0;$k<$STRIDE/16-4;$k+=4) {
159 pcmpeqd %xmm5,%xmm1 # compare to 3,2
160 movdqa %xmm0,`16*($k+0)+112`(%r10)
164 pcmpeqd %xmm5,%xmm2 # compare to 5,4
165 movdqa %xmm1,`16*($k+1)+112`(%r10)
169 pcmpeqd %xmm5,%xmm3 # compare to 7,6
170 movdqa %xmm2,`16*($k+2)+112`(%r10)
175 movdqa %xmm3,`16*($k+3)+112`(%r10)
179 $code.=<<___; # last iteration can be optimized
182 movdqa %xmm0,`16*($k+0)+112`(%r10)
187 movdqa %xmm1,`16*($k+1)+112`(%r10)
190 movdqa %xmm2,`16*($k+2)+112`(%r10)
191 pand `16*($k+0)-128`($bp),%xmm0 # while it's still in register
193 pand `16*($k+1)-128`($bp),%xmm1
194 pand `16*($k+2)-128`($bp),%xmm2
195 movdqa %xmm3,`16*($k+3)+112`(%r10)
196 pand `16*($k+3)-128`($bp),%xmm3
200 for($k=0;$k<$STRIDE/16-4;$k+=4) {
202 movdqa `16*($k+0)-128`($bp),%xmm4
203 movdqa `16*($k+1)-128`($bp),%xmm5
204 movdqa `16*($k+2)-128`($bp),%xmm2
205 pand `16*($k+0)+112`(%r10),%xmm4
206 movdqa `16*($k+3)-128`($bp),%xmm3
207 pand `16*($k+1)+112`(%r10),%xmm5
209 pand `16*($k+2)+112`(%r10),%xmm2
211 pand `16*($k+3)+112`(%r10),%xmm3
218 pshufd \$0x4e,%xmm0,%xmm1
221 movq %xmm0,$m0 # m0=bp[0]
223 mov ($n0),$n0 # pull n0[0] value
230 mulq $m0 # ap[0]*bp[0]
234 imulq $lo0,$m1 # "tp[0]"*n0
238 add %rax,$lo0 # discarded
251 add $hi0,$hi1 # np[j]*m1+ap[j]*bp[0]
254 mov $hi1,-16(%rsp,$j,8) # tp[j-1]
258 mulq $m0 # ap[j]*bp[0]
267 jne .L1st # note that upon exit $j==$num, so
268 # they can be used interchangeably
272 add $hi0,$hi1 # np[j]*m1+ap[j]*bp[0]
274 mov $hi1,-16(%rsp,$num,8) # tp[num-1]
281 mov $hi1,-8(%rsp,$num,8)
282 mov %rdx,(%rsp,$num,8) # store upmost overflow bit
288 lea 24+128(%rsp,$num,8),%rdx # where 256-byte mask is (+size optimization)
293 for($k=0;$k<$STRIDE/16;$k+=4) {
295 movdqa `16*($k+0)-128`($bp),%xmm0
296 movdqa `16*($k+1)-128`($bp),%xmm1
297 movdqa `16*($k+2)-128`($bp),%xmm2
298 movdqa `16*($k+3)-128`($bp),%xmm3
299 pand `16*($k+0)-128`(%rdx),%xmm0
300 pand `16*($k+1)-128`(%rdx),%xmm1
302 pand `16*($k+2)-128`(%rdx),%xmm2
304 pand `16*($k+3)-128`(%rdx),%xmm3
311 pshufd \$0x4e,%xmm4,%xmm0
315 mov ($ap),%rax # ap[0]
316 movq %xmm0,$m0 # m0=bp[i]
322 mulq $m0 # ap[0]*bp[i]
323 add %rax,$lo0 # ap[0]*bp[i]+tp[0]
327 imulq $lo0,$m1 # tp[0]*n0
331 add %rax,$lo0 # discarded
334 mov 8(%rsp),$lo0 # tp[1]
345 add $lo0,$hi1 # np[j]*m1+ap[j]*bp[i]+tp[j]
348 mov $hi1,-16(%rsp,$j,8) # tp[j-1]
352 mulq $m0 # ap[j]*bp[i]
356 add $hi0,$lo0 # ap[j]*bp[i]+tp[j]
363 jne .Linner # note that upon exit $j==$num, so
364 # they can be used interchangeably
367 add $lo0,$hi1 # np[j]*m1+ap[j]*bp[i]+tp[j]
368 mov (%rsp,$num,8),$lo0
370 mov $hi1,-16(%rsp,$num,8) # tp[num-1]
376 add $lo0,$hi1 # pull upmost overflow bit
378 mov $hi1,-8(%rsp,$num,8)
379 mov %rdx,(%rsp,$num,8) # store upmost overflow bit
385 xor $i,$i # i=0 and clear CF!
386 mov (%rsp),%rax # tp[0]
387 lea (%rsp),$ap # borrow ap for tp
391 .Lsub: sbb ($np,$i,8),%rax
392 mov %rax,($rp,$i,8) # rp[i]=tp[i]-np[i]
393 mov 8($ap,$i,8),%rax # tp[i+1]
395 dec $j # doesnn't affect CF!
398 sbb \$0,%rax # handle upmost overflow bit
405 or $np,$ap # ap=borrow?tp:rp
407 .Lcopy: # copy or in-place refresh
409 mov $i,(%rsp,$i,8) # zap temporary vector
410 mov %rax,($rp,$i,8) # rp[i]=tp[i]
415 mov 8(%rsp,$num,8),%rsi # restore %rsp
427 .size bn_mul_mont_gather5,.-bn_mul_mont_gather5
430 my @A=("%r10","%r11");
431 my @N=("%r13","%rdi");
433 .type bn_mul4x_mont_gather5,\@function,6
435 bn_mul4x_mont_gather5:
438 $code.=<<___ if ($addx);
440 cmp \$0x80108,%r11d # check for AD*X+BMI2+BMI1
454 shl \$3,${num}d # convert $num to bytes
455 lea ($num,$num,2),%r10 # 3*$num in bytes
458 ##############################################################
459 # Ensure that stack frame doesn't alias with $rptr+3*$num
460 # modulo 4096, which covers ret[num], am[num] and n[num]
461 # (see bn_exp.c). This is done to allow memory disambiguation
462 # logic do its magic. [Extra [num] is allocated in order
463 # to align with bn_power5's frame, which is cleansed after
464 # completing exponentiation. Extra 256 bytes is for power mask
465 # calculated from 7th argument, the index.]
467 lea -320(%rsp,$num,2),%r11
472 sub %r11,%rsp # align with $rp
473 lea -320(%rsp,$num,2),%rsp # alloca(frame+2*num*8+256)
478 lea 4096-320(,$num,2),%r10
479 lea -320(%rsp,$num,2),%rsp # alloca(frame+2*num*8+256)
492 .byte 0x2e # predict non-taken
493 jnc .Lmul4x_page_walk
502 mov 40(%rsp),%rsi # restore %rsp
514 .size bn_mul4x_mont_gather5,.-bn_mul4x_mont_gather5
516 .type mul4x_internal,\@abi-omnipotent
519 shl \$5,$num # $num was in bytes
520 movd `($win64?56:8)`(%rax),%xmm5 # load 7th argument, index
522 lea 128(%rdx,$num),%r13 # end of powers table (+size optimization)
523 shr \$5,$num # restore $num
526 $STRIDE=2**5*8; # 5 is "window size"
527 $N=$STRIDE/4; # should match cache line size
530 movdqa 0(%rax),%xmm0 # 00000001000000010000000000000000
531 movdqa 16(%rax),%xmm1 # 00000002000000020000000200000002
532 lea 88-112(%rsp,$num),%r10 # place the mask after tp[num+1] (+ICache optimization)
533 lea 128(%rdx),$bp # size optimization
535 pshufd \$0,%xmm5,%xmm5 # broadcast index
540 ########################################################################
541 # calculate mask by comparing 0..31 to index and save result to stack
545 pcmpeqd %xmm5,%xmm0 # compare to 1,0
549 for($i=0;$i<$STRIDE/16-4;$i+=4) {
552 pcmpeqd %xmm5,%xmm1 # compare to 3,2
553 movdqa %xmm0,`16*($i+0)+112`(%r10)
557 pcmpeqd %xmm5,%xmm2 # compare to 5,4
558 movdqa %xmm1,`16*($i+1)+112`(%r10)
562 pcmpeqd %xmm5,%xmm3 # compare to 7,6
563 movdqa %xmm2,`16*($i+2)+112`(%r10)
568 movdqa %xmm3,`16*($i+3)+112`(%r10)
572 $code.=<<___; # last iteration can be optimized
575 movdqa %xmm0,`16*($i+0)+112`(%r10)
580 movdqa %xmm1,`16*($i+1)+112`(%r10)
583 movdqa %xmm2,`16*($i+2)+112`(%r10)
584 pand `16*($i+0)-128`($bp),%xmm0 # while it's still in register
586 pand `16*($i+1)-128`($bp),%xmm1
587 pand `16*($i+2)-128`($bp),%xmm2
588 movdqa %xmm3,`16*($i+3)+112`(%r10)
589 pand `16*($i+3)-128`($bp),%xmm3
593 for($i=0;$i<$STRIDE/16-4;$i+=4) {
595 movdqa `16*($i+0)-128`($bp),%xmm4
596 movdqa `16*($i+1)-128`($bp),%xmm5
597 movdqa `16*($i+2)-128`($bp),%xmm2
598 pand `16*($i+0)+112`(%r10),%xmm4
599 movdqa `16*($i+3)-128`($bp),%xmm3
600 pand `16*($i+1)+112`(%r10),%xmm5
602 pand `16*($i+2)+112`(%r10),%xmm2
604 pand `16*($i+3)+112`(%r10),%xmm3
611 pshufd \$0x4e,%xmm0,%xmm1
614 movq %xmm0,$m0 # m0=bp[0]
616 mov %r13,16+8(%rsp) # save end of b[num]
617 mov $rp, 56+8(%rsp) # save $rp
619 mov ($n0),$n0 # pull n0[0] value
621 lea ($ap,$num),$ap # end of a[num]
625 mulq $m0 # ap[0]*bp[0]
629 imulq $A[0],$m1 # "tp[0]"*n0
634 add %rax,$A[0] # discarded
647 mov 16($ap,$num),%rax
650 lea 4*8($num),$j # j=4
659 mulq $m0 # ap[j]*bp[0]
670 add $A[0],$N[0] # np[j]*m1+ap[j]*bp[0]
672 mov $N[0],-24($tp) # tp[j-1]
675 mulq $m0 # ap[j]*bp[0]
685 add $A[1],$N[1] # np[j]*m1+ap[j]*bp[0]
687 mov $N[1],-16($tp) # tp[j-1]
690 mulq $m0 # ap[j]*bp[0]
700 add $A[0],$N[0] # np[j]*m1+ap[j]*bp[0]
702 mov $N[0],-8($tp) # tp[j-1]
705 mulq $m0 # ap[j]*bp[0]
715 add $A[1],$N[1] # np[j]*m1+ap[j]*bp[0]
718 mov $N[1],($tp) # tp[j-1]
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]
748 mov ($ap,$num),%rax # ap[0]
750 add $A[1],$N[1] # np[j]*m1+ap[j]*bp[0]
752 mov $N[1],-16($tp) # tp[j-1]
755 lea ($np,$num),$np # rewind $np
766 lea 16+128($tp),%rdx # where 256-byte mask is (+size optimization)
770 for($i=0;$i<$STRIDE/16;$i+=4) {
772 movdqa `16*($i+0)-128`($bp),%xmm0
773 movdqa `16*($i+1)-128`($bp),%xmm1
774 movdqa `16*($i+2)-128`($bp),%xmm2
775 movdqa `16*($i+3)-128`($bp),%xmm3
776 pand `16*($i+0)-128`(%rdx),%xmm0
777 pand `16*($i+1)-128`(%rdx),%xmm1
779 pand `16*($i+2)-128`(%rdx),%xmm2
781 pand `16*($i+3)-128`(%rdx),%xmm3
788 pshufd \$0x4e,%xmm4,%xmm0
791 movq %xmm0,$m0 # m0=bp[i]
795 mulq $m0 # ap[0]*bp[i]
796 add %rax,$A[0] # ap[0]*bp[i]+tp[0]
800 imulq $A[0],$m1 # tp[0]*n0
802 mov $N[1],($tp) # store upmost overflow bit
804 lea ($tp,$num),$tp # rewind $tp
807 add %rax,$A[0] # "$N[0]", discarded
812 mulq $m0 # ap[j]*bp[i]
816 add 8($tp),$A[1] # +tp[1]
822 mov 16($ap,$num),%rax
824 add $A[1],$N[1] # np[j]*m1+ap[j]*bp[i]+tp[j]
825 lea 4*8($num),$j # j=4
833 mulq $m0 # ap[j]*bp[i]
837 add 16($tp),$A[0] # ap[j]*bp[i]+tp[j]
848 mov $N[1],-32($tp) # tp[j-1]
851 mulq $m0 # ap[j]*bp[i]
865 mov $N[0],-24($tp) # tp[j-1]
868 mulq $m0 # ap[j]*bp[i]
872 add ($tp),$A[0] # ap[j]*bp[i]+tp[j]
882 mov $N[1],-16($tp) # tp[j-1]
885 mulq $m0 # ap[j]*bp[i]
900 mov $N[0],-8($tp) # tp[j-1]
906 mulq $m0 # ap[j]*bp[i]
910 add 16($tp),$A[0] # ap[j]*bp[i]+tp[j]
921 mov $N[1],-32($tp) # tp[j-1]
924 mulq $m0 # ap[j]*bp[i]
935 mov ($ap,$num),%rax # ap[0]
939 mov $N[0],-24($tp) # tp[j-1]
942 mov $N[1],-16($tp) # tp[j-1]
943 lea ($np,$num),$np # rewind $np
948 add ($tp),$N[0] # pull upmost overflow bit
949 adc \$0,$N[1] # upmost overflow bit
958 sub $N[0],$m1 # compare top-most words
959 adc $j,$j # $j is zero
961 sub $N[1],%rax # %rax=-$N[1]
962 lea ($tp,$num),%rbx # tptr in .sqr4x_sub
964 lea ($np),%rbp # nptr in .sqr4x_sub
967 mov 56+8(%rsp),%rdi # rptr in .sqr4x_sub
968 dec %r12 # so that after 'not' we get -n[0]
973 jmp .Lsqr4x_sub_entry
976 my @ri=("%rax",$bp,$m0,$m1);
980 lea ($tp,$num),$tp # rewind $tp
982 lea ($np,$N[1],8),$np
983 mov 56+8(%rsp),$rp # restore $rp
1013 .size mul4x_internal,.-mul4x_internal
1017 ######################################################################
1019 my $rptr="%rdi"; # BN_ULONG *rptr,
1020 my $aptr="%rsi"; # const BN_ULONG *aptr,
1021 my $bptr="%rdx"; # const void *table,
1022 my $nptr="%rcx"; # const BN_ULONG *nptr,
1023 my $n0 ="%r8"; # const BN_ULONG *n0);
1024 my $num ="%r9"; # int num, has to be divisible by 8
1027 my ($i,$j,$tptr)=("%rbp","%rcx",$rptr);
1028 my @A0=("%r10","%r11");
1029 my @A1=("%r12","%r13");
1030 my ($a0,$a1,$ai)=("%r14","%r15","%rbx");
1034 .type bn_power5,\@function,6
1038 $code.=<<___ if ($addx);
1039 mov OPENSSL_ia32cap_P+8(%rip),%r11d
1041 cmp \$0x80108,%r11d # check for AD*X+BMI2+BMI1
1053 shl \$3,${num}d # convert $num to bytes
1054 lea ($num,$num,2),%r10d # 3*$num
1058 ##############################################################
1059 # Ensure that stack frame doesn't alias with $rptr+3*$num
1060 # modulo 4096, which covers ret[num], am[num] and n[num]
1061 # (see bn_exp.c). This is done to allow memory disambiguation
1062 # logic do its magic. [Extra 256 bytes is for power mask
1063 # calculated from 7th argument, the index.]
1065 lea -320(%rsp,$num,2),%r11
1070 sub %r11,%rsp # align with $aptr
1071 lea -320(%rsp,$num,2),%rsp # alloca(frame+2*num*8+256)
1076 lea 4096-320(,$num,2),%r10
1077 lea -320(%rsp,$num,2),%rsp # alloca(frame+2*num*8+256)
1088 mov (%rsp,%r11),%r10
1090 .byte 0x2e # predict non-taken
1096 ##############################################################
1099 # +0 saved $num, used in reduction section
1100 # +8 &t[2*$num], used in reduction section
1106 mov %rax, 40(%rsp) # save original %rsp
1108 movq $rptr,%xmm1 # save $rptr, used in sqr8x
1109 movq $nptr,%xmm2 # save $nptr
1110 movq %r10, %xmm3 # -$num, used in sqr8x
1113 call __bn_sqr8x_internal
1114 call __bn_post4x_internal
1115 call __bn_sqr8x_internal
1116 call __bn_post4x_internal
1117 call __bn_sqr8x_internal
1118 call __bn_post4x_internal
1119 call __bn_sqr8x_internal
1120 call __bn_post4x_internal
1121 call __bn_sqr8x_internal
1122 call __bn_post4x_internal
1132 mov 40(%rsp),%rsi # restore %rsp
1143 .size bn_power5,.-bn_power5
1145 .globl bn_sqr8x_internal
1146 .hidden bn_sqr8x_internal
1147 .type bn_sqr8x_internal,\@abi-omnipotent
1150 __bn_sqr8x_internal:
1151 ##############################################################
1154 # a) multiply-n-add everything but a[i]*a[i];
1155 # b) shift result of a) by 1 to the left and accumulate
1156 # a[i]*a[i] products;
1158 ##############################################################
1224 lea 32(%r10),$i # $i=-($num-32)
1225 lea ($aptr,$num),$aptr # end of a[] buffer, ($aptr,$i)=&ap[2]
1227 mov $num,$j # $j=$num
1229 # comments apply to $num==8 case
1230 mov -32($aptr,$i),$a0 # a[0]
1231 lea 48+8(%rsp,$num,2),$tptr # end of tp[] buffer, &tp[2*$num]
1232 mov -24($aptr,$i),%rax # a[1]
1233 lea -32($tptr,$i),$tptr # end of tp[] window, &tp[2*$num-"$i"]
1234 mov -16($aptr,$i),$ai # a[2]
1238 mov %rax,$A0[0] # a[1]*a[0]
1241 mov $A0[0],-24($tptr,$i) # t[1]
1247 mov $A0[1],-16($tptr,$i) # t[2]
1251 mov -8($aptr,$i),$ai # a[3]
1253 mov %rax,$A1[0] # a[2]*a[1]+t[3]
1259 add %rax,$A0[0] # a[3]*a[0]+a[2]*a[1]+t[3]
1265 mov $A0[0],-8($tptr,$j) # t[3]
1270 mov ($aptr,$j),$ai # a[4]
1272 add %rax,$A1[1] # a[3]*a[1]+t[4]
1278 add %rax,$A0[1] # a[4]*a[0]+a[3]*a[1]+t[4]
1280 mov 8($aptr,$j),$ai # a[5]
1288 add %rax,$A1[0] # a[4]*a[3]+t[5]
1290 mov $A0[1],($tptr,$j) # t[4]
1295 add %rax,$A0[0] # a[5]*a[2]+a[4]*a[3]+t[5]
1297 mov 16($aptr,$j),$ai # a[6]
1304 add %rax,$A1[1] # a[5]*a[3]+t[6]
1306 mov $A0[0],8($tptr,$j) # t[5]
1311 add %rax,$A0[1] # a[6]*a[2]+a[5]*a[3]+t[6]
1313 mov 24($aptr,$j),$ai # a[7]
1321 add %rax,$A1[0] # a[6]*a[5]+t[7]
1323 mov $A0[1],16($tptr,$j) # t[6]
1329 add %rax,$A0[0] # a[7]*a[4]+a[6]*a[5]+t[6]
1335 mov $A0[0],-8($tptr,$j) # t[7]
1347 mov $A1[1],($tptr) # t[8]
1349 mov %rdx,8($tptr) # t[9]
1353 .Lsqr4x_outer: # comments apply to $num==6 case
1354 mov -32($aptr,$i),$a0 # a[0]
1355 lea 48+8(%rsp,$num,2),$tptr # end of tp[] buffer, &tp[2*$num]
1356 mov -24($aptr,$i),%rax # a[1]
1357 lea -32($tptr,$i),$tptr # end of tp[] window, &tp[2*$num-"$i"]
1358 mov -16($aptr,$i),$ai # a[2]
1362 mov -24($tptr,$i),$A0[0] # t[1]
1363 add %rax,$A0[0] # a[1]*a[0]+t[1]
1366 mov $A0[0],-24($tptr,$i) # t[1]
1373 add -16($tptr,$i),$A0[1] # a[2]*a[0]+t[2]
1376 mov $A0[1],-16($tptr,$i) # t[2]
1380 mov -8($aptr,$i),$ai # a[3]
1382 add %rax,$A1[0] # a[2]*a[1]+t[3]
1385 add -8($tptr,$i),$A1[0]
1390 add %rax,$A0[0] # a[3]*a[0]+a[2]*a[1]+t[3]
1396 mov $A0[0],-8($tptr,$i) # t[3]
1403 mov ($aptr,$j),$ai # a[4]
1405 add %rax,$A1[1] # a[3]*a[1]+t[4]
1409 add ($tptr,$j),$A1[1]
1414 add %rax,$A0[1] # a[4]*a[0]+a[3]*a[1]+t[4]
1416 mov 8($aptr,$j),$ai # a[5]
1423 add %rax,$A1[0] # a[4]*a[3]+t[5]
1424 mov $A0[1],($tptr,$j) # t[4]
1428 add 8($tptr,$j),$A1[0]
1433 add %rax,$A0[0] # a[5]*a[2]+a[4]*a[3]+t[5]
1439 mov $A0[0],-8($tptr,$j) # t[5], "preloaded t[1]" below
1451 mov $A1[1],($tptr) # t[6], "preloaded t[2]" below
1453 mov %rdx,8($tptr) # t[7], "preloaded t[3]" below
1458 # comments apply to $num==4 case
1459 mov -32($aptr),$a0 # a[0]
1460 lea 48+8(%rsp,$num,2),$tptr # end of tp[] buffer, &tp[2*$num]
1461 mov -24($aptr),%rax # a[1]
1462 lea -32($tptr,$i),$tptr # end of tp[] window, &tp[2*$num-"$i"]
1463 mov -16($aptr),$ai # a[2]
1467 add %rax,$A0[0] # a[1]*a[0]+t[1], preloaded t[1]
1475 mov $A0[0],-24($tptr) # t[1]
1478 add $A1[1],$A0[1] # a[2]*a[0]+t[2], preloaded t[2]
1479 mov -8($aptr),$ai # a[3]
1483 add %rax,$A1[0] # a[2]*a[1]+t[3], preloaded t[3]
1485 mov $A0[1],-16($tptr) # t[2]
1490 add %rax,$A0[0] # a[3]*a[0]+a[2]*a[1]+t[3]
1496 mov $A0[0],-8($tptr) # t[3]
1500 mov -16($aptr),%rax # a[2]
1505 mov $A1[1],($tptr) # t[4]
1507 mov %rdx,8($tptr) # t[5]
1512 my ($shift,$carry)=($a0,$a1);
1513 my @S=(@A1,$ai,$n0);
1517 sub $num,$i # $i=16-$num
1520 add $A1[0],%rax # t[5]
1522 mov %rax,8($tptr) # t[5]
1523 mov %rdx,16($tptr) # t[6]
1524 mov $carry,24($tptr) # t[7]
1526 mov -16($aptr,$i),%rax # a[0]
1527 lea 48+8(%rsp),$tptr
1528 xor $A0[0],$A0[0] # t[0]
1529 mov 8($tptr),$A0[1] # t[1]
1531 lea ($shift,$A0[0],2),$S[0] # t[2*i]<<1 | shift
1533 lea ($j,$A0[1],2),$S[1] # t[2*i+1]<<1 |
1535 or $A0[0],$S[1] # | t[2*i]>>63
1536 mov 16($tptr),$A0[0] # t[2*i+2] # prefetch
1537 mov $A0[1],$shift # shift=t[2*i+1]>>63
1538 mul %rax # a[i]*a[i]
1539 neg $carry # mov $carry,cf
1540 mov 24($tptr),$A0[1] # t[2*i+2+1] # prefetch
1542 mov -8($aptr,$i),%rax # a[i+1] # prefetch
1546 lea ($shift,$A0[0],2),$S[2] # t[2*i]<<1 | shift
1548 sbb $carry,$carry # mov cf,$carry
1550 lea ($j,$A0[1],2),$S[3] # t[2*i+1]<<1 |
1552 or $A0[0],$S[3] # | t[2*i]>>63
1553 mov 32($tptr),$A0[0] # t[2*i+2] # prefetch
1554 mov $A0[1],$shift # shift=t[2*i+1]>>63
1555 mul %rax # a[i]*a[i]
1556 neg $carry # mov $carry,cf
1557 mov 40($tptr),$A0[1] # t[2*i+2+1] # prefetch
1559 mov 0($aptr,$i),%rax # a[i+1] # prefetch
1564 sbb $carry,$carry # mov cf,$carry
1566 jmp .Lsqr4x_shift_n_add
1569 .Lsqr4x_shift_n_add:
1570 lea ($shift,$A0[0],2),$S[0] # t[2*i]<<1 | shift
1572 lea ($j,$A0[1],2),$S[1] # t[2*i+1]<<1 |
1574 or $A0[0],$S[1] # | t[2*i]>>63
1575 mov -16($tptr),$A0[0] # t[2*i+2] # prefetch
1576 mov $A0[1],$shift # shift=t[2*i+1]>>63
1577 mul %rax # a[i]*a[i]
1578 neg $carry # mov $carry,cf
1579 mov -8($tptr),$A0[1] # t[2*i+2+1] # prefetch
1581 mov -8($aptr,$i),%rax # a[i+1] # prefetch
1582 mov $S[0],-32($tptr)
1585 lea ($shift,$A0[0],2),$S[2] # t[2*i]<<1 | shift
1586 mov $S[1],-24($tptr)
1587 sbb $carry,$carry # mov cf,$carry
1589 lea ($j,$A0[1],2),$S[3] # t[2*i+1]<<1 |
1591 or $A0[0],$S[3] # | t[2*i]>>63
1592 mov 0($tptr),$A0[0] # t[2*i+2] # prefetch
1593 mov $A0[1],$shift # shift=t[2*i+1]>>63
1594 mul %rax # a[i]*a[i]
1595 neg $carry # mov $carry,cf
1596 mov 8($tptr),$A0[1] # t[2*i+2+1] # prefetch
1598 mov 0($aptr,$i),%rax # a[i+1] # prefetch
1599 mov $S[2],-16($tptr)
1602 lea ($shift,$A0[0],2),$S[0] # t[2*i]<<1 | shift
1604 sbb $carry,$carry # mov cf,$carry
1606 lea ($j,$A0[1],2),$S[1] # t[2*i+1]<<1 |
1608 or $A0[0],$S[1] # | t[2*i]>>63
1609 mov 16($tptr),$A0[0] # t[2*i+2] # prefetch
1610 mov $A0[1],$shift # shift=t[2*i+1]>>63
1611 mul %rax # a[i]*a[i]
1612 neg $carry # mov $carry,cf
1613 mov 24($tptr),$A0[1] # t[2*i+2+1] # prefetch
1615 mov 8($aptr,$i),%rax # a[i+1] # prefetch
1619 lea ($shift,$A0[0],2),$S[2] # t[2*i]<<1 | shift
1621 sbb $carry,$carry # mov cf,$carry
1623 lea ($j,$A0[1],2),$S[3] # t[2*i+1]<<1 |
1625 or $A0[0],$S[3] # | t[2*i]>>63
1626 mov 32($tptr),$A0[0] # t[2*i+2] # prefetch
1627 mov $A0[1],$shift # shift=t[2*i+1]>>63
1628 mul %rax # a[i]*a[i]
1629 neg $carry # mov $carry,cf
1630 mov 40($tptr),$A0[1] # t[2*i+2+1] # prefetch
1632 mov 16($aptr,$i),%rax # a[i+1] # prefetch
1636 sbb $carry,$carry # mov cf,$carry
1639 jnz .Lsqr4x_shift_n_add
1641 lea ($shift,$A0[0],2),$S[0] # t[2*i]<<1 | shift
1644 lea ($j,$A0[1],2),$S[1] # t[2*i+1]<<1 |
1646 or $A0[0],$S[1] # | t[2*i]>>63
1647 mov -16($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 -8($tptr),$A0[1] # t[2*i+2+1] # prefetch
1653 mov -8($aptr),%rax # a[i+1] # prefetch
1654 mov $S[0],-32($tptr)
1657 lea ($shift,$A0[0],2),$S[2] # t[2*i]<<1|shift
1658 mov $S[1],-24($tptr)
1659 sbb $carry,$carry # mov cf,$carry
1661 lea ($j,$A0[1],2),$S[3] # t[2*i+1]<<1 |
1663 or $A0[0],$S[3] # | t[2*i]>>63
1664 mul %rax # a[i]*a[i]
1665 neg $carry # mov $carry,cf
1668 mov $S[2],-16($tptr)
1672 ######################################################################
1673 # Montgomery reduction part, "word-by-word" algorithm.
1675 # This new path is inspired by multiple submissions from Intel, by
1676 # Shay Gueron, Vlad Krasnov, Erdinc Ozturk, James Guilford,
1679 my ($nptr,$tptr,$carry,$m0)=("%rbp","%rdi","%rsi","%rbx");
1683 __bn_sqr8x_reduction:
1685 lea ($nptr,$num),%rcx # end of n[]
1686 lea 48+8(%rsp,$num,2),%rdx # end of t[] buffer
1688 lea 48+8(%rsp,$num),$tptr # end of initial t[] window
1691 jmp .L8x_reduction_loop
1694 .L8x_reduction_loop:
1695 lea ($tptr,$num),$tptr # start of current t[] window
1705 mov %rax,(%rdx) # store top-most carry bit
1706 lea 8*8($tptr),$tptr
1710 imulq 32+8(%rsp),$m0 # n0*a[0]
1711 mov 8*0($nptr),%rax # n[0]
1718 mov 8*1($nptr),%rax # n[1]
1728 mov $m0,48-8+8(%rsp,%rcx,8) # put aside n0*a[i]
1737 mov 32+8(%rsp),$carry # pull n0, borrow $carry
1745 imulq %r8,$carry # modulo-scheduled
1775 mov $carry,$m0 # n0*a[i]
1777 mov 8*0($nptr),%rax # n[0]
1786 lea 8*8($nptr),$nptr
1788 mov 8+8(%rsp),%rdx # pull end of t[]
1789 cmp 0+8(%rsp),$nptr # end of n[]?
1801 sbb $carry,$carry # top carry
1803 mov 48+56+8(%rsp),$m0 # pull n0*a[0]
1813 mov %r8,($tptr) # save result
1822 lea 8($tptr),$tptr # $tptr++
1867 mov 48-16+8(%rsp,%rcx,8),$m0# pull n0*a[i]
1871 mov 8*0($nptr),%rax # pull n[0]
1878 lea 8*8($nptr),$nptr
1879 mov 8+8(%rsp),%rdx # pull end of t[]
1880 cmp 0+8(%rsp),$nptr # end of n[]?
1881 jae .L8x_tail_done # break out of loop
1883 mov 48+56+8(%rsp),$m0 # pull n0*a[0]
1885 mov 8*0($nptr),%rax # pull n[0]
1894 sbb $carry,$carry # top carry
1901 add (%rdx),%r8 # can this overflow?
1908 adc \$0,%r15 # can't overflow, because we
1909 # started with "overhung" part
1923 adc \$0,%rax # top-most carry
1924 mov -8($nptr),%rcx # np[num-1]
1927 movq %xmm2,$nptr # restore $nptr
1929 mov %r8,8*0($tptr) # store top 512 bits
1931 movq %xmm3,$num # $num is %r9, can't be moved upwards
1938 lea 8*8($tptr),$tptr
1940 cmp %rdx,$tptr # end of t[]?
1941 jb .L8x_reduction_loop
1943 .size bn_sqr8x_internal,.-bn_sqr8x_internal
1946 ##############################################################
1947 # Post-condition, 4x unrolled
1950 my ($tptr,$nptr)=("%rbx","%rbp");
1952 .type __bn_post4x_internal,\@abi-omnipotent
1954 __bn_post4x_internal:
1956 lea (%rdi,$num),$tptr # %rdi was $tptr above
1958 movq %xmm1,$rptr # restore $rptr
1960 movq %xmm1,$aptr # prepare for back-to-back call
1962 dec %r12 # so that after 'not' we get -n[0]
1967 jmp .Lsqr4x_sub_entry
1976 lea 8*4($nptr),$nptr
1986 neg %r10 # mov %r10,%cf
1992 lea 8*4($tptr),$tptr
1994 sbb %r10,%r10 # mov %cf,%r10
1997 lea 8*4($rptr),$rptr
2002 mov $num,%r10 # prepare for back-to-back call
2003 neg $num # restore $num
2005 .size __bn_post4x_internal,.-__bn_post4x_internal
2010 .globl bn_from_montgomery
2011 .type bn_from_montgomery,\@abi-omnipotent
2014 testl \$7,`($win64?"48(%rsp)":"%r9d")`
2018 .size bn_from_montgomery,.-bn_from_montgomery
2020 .type bn_from_mont8x,\@function,6
2032 shl \$3,${num}d # convert $num to bytes
2033 lea ($num,$num,2),%r10 # 3*$num in bytes
2037 ##############################################################
2038 # Ensure that stack frame doesn't alias with $rptr+3*$num
2039 # modulo 4096, which covers ret[num], am[num] and n[num]
2040 # (see bn_exp.c). The stack is allocated to aligned with
2041 # bn_power5's frame, and as bn_from_montgomery happens to be
2042 # last operation, we use the opportunity to cleanse it.
2044 lea -320(%rsp,$num,2),%r11
2049 sub %r11,%rsp # align with $aptr
2050 lea -320(%rsp,$num,2),%rsp # alloca(frame+2*$num*8+256)
2055 lea 4096-320(,$num,2),%r10
2056 lea -320(%rsp,$num,2),%rsp # alloca(frame+2*$num*8+256)
2067 mov (%rsp,%r11),%r10
2069 .byte 0x2e # predict non-taken
2070 jnc .Lfrom_page_walk
2075 ##############################################################
2078 # +0 saved $num, used in reduction section
2079 # +8 &t[2*$num], used in reduction section
2085 mov %rax, 40(%rsp) # save original %rsp
2094 movdqu ($aptr),%xmm1
2095 movdqu 16($aptr),%xmm2
2096 movdqu 32($aptr),%xmm3
2097 movdqa %xmm0,(%rax,$num)
2098 movdqu 48($aptr),%xmm4
2099 movdqa %xmm0,16(%rax,$num)
2100 .byte 0x48,0x8d,0xb6,0x40,0x00,0x00,0x00 # lea 64($aptr),$aptr
2102 movdqa %xmm0,32(%rax,$num)
2103 movdqa %xmm2,16(%rax)
2104 movdqa %xmm0,48(%rax,$num)
2105 movdqa %xmm3,32(%rax)
2106 movdqa %xmm4,48(%rax)
2115 movq %r10, %xmm3 # -num
2117 $code.=<<___ if ($addx);
2118 mov OPENSSL_ia32cap_P+8(%rip),%r11d
2120 cmp \$0x80108,%r11d # check for AD*X+BMI2+BMI1
2123 lea (%rax,$num),$rptr
2124 call __bn_sqrx8x_reduction
2125 call __bn_postx4x_internal
2129 mov 40(%rsp),%rsi # restore %rsp
2130 jmp .Lfrom_mont_zero
2136 call __bn_sqr8x_reduction
2137 call __bn_post4x_internal
2141 mov 40(%rsp),%rsi # restore %rsp
2142 jmp .Lfrom_mont_zero
2146 movdqa %xmm0,16*0(%rax)
2147 movdqa %xmm0,16*1(%rax)
2148 movdqa %xmm0,16*2(%rax)
2149 movdqa %xmm0,16*3(%rax)
2152 jnz .Lfrom_mont_zero
2164 .size bn_from_mont8x,.-bn_from_mont8x
2170 my $bp="%rdx"; # restore original value
2173 .type bn_mulx4x_mont_gather5,\@function,6
2175 bn_mulx4x_mont_gather5:
2185 shl \$3,${num}d # convert $num to bytes
2186 lea ($num,$num,2),%r10 # 3*$num in bytes
2190 ##############################################################
2191 # Ensure that stack frame doesn't alias with $rptr+3*$num
2192 # modulo 4096, which covers ret[num], am[num] and n[num]
2193 # (see bn_exp.c). This is done to allow memory disambiguation
2194 # logic do its magic. [Extra [num] is allocated in order
2195 # to align with bn_power5's frame, which is cleansed after
2196 # completing exponentiation. Extra 256 bytes is for power mask
2197 # calculated from 7th argument, the index.]
2199 lea -320(%rsp,$num,2),%r11
2204 sub %r11,%rsp # align with $aptr
2205 lea -320(%rsp,$num,2),%rsp # alloca(frame+2*$num*8+256)
2209 lea 4096-320(,$num,2),%r10
2210 lea -320(%rsp,$num,2),%rsp # alloca(frame+2*$num*8+256)
2216 and \$-64,%rsp # ensure alignment
2221 mov (%rsp,%r11),%r10
2223 .byte 0x2e # predict non-taken
2224 jnc .Lmulx4x_page_walk
2226 ##############################################################
2229 # +8 off-loaded &b[i]
2238 mov $n0, 32(%rsp) # save *n0
2239 mov %rax,40(%rsp) # save original %rsp
2241 call mulx4x_internal
2243 mov 40(%rsp),%rsi # restore %rsp
2255 .size bn_mulx4x_mont_gather5,.-bn_mulx4x_mont_gather5
2257 .type mulx4x_internal,\@abi-omnipotent
2260 mov $num,8(%rsp) # save -$num (it was in bytes)
2262 neg $num # restore $num
2264 neg %r10 # restore $num
2265 lea 128($bp,$num),%r13 # end of powers table (+size optimization)
2267 movd `($win64?56:8)`(%rax),%xmm5 # load 7th argument
2269 lea .Linc(%rip),%rax
2270 mov %r13,16+8(%rsp) # end of b[num]
2271 mov $num,24+8(%rsp) # inner counter
2272 mov $rp, 56+8(%rsp) # save $rp
2274 my ($aptr, $bptr, $nptr, $tptr, $mi, $bi, $zero, $num)=
2275 ("%rsi","%rdi","%rcx","%rbx","%r8","%r9","%rbp","%rax");
2277 my $STRIDE=2**5*8; # 5 is "window size"
2278 my $N=$STRIDE/4; # should match cache line size
2280 movdqa 0(%rax),%xmm0 # 00000001000000010000000000000000
2281 movdqa 16(%rax),%xmm1 # 00000002000000020000000200000002
2282 lea 88-112(%rsp,%r10),%r10 # place the mask after tp[num+1] (+ICache optimizaton)
2283 lea 128($bp),$bptr # size optimization
2285 pshufd \$0,%xmm5,%xmm5 # broadcast index
2290 ########################################################################
2291 # calculate mask by comparing 0..31 to index and save result to stack
2296 pcmpeqd %xmm5,%xmm0 # compare to 1,0
2299 for($i=0;$i<$STRIDE/16-4;$i+=4) {
2302 pcmpeqd %xmm5,%xmm1 # compare to 3,2
2303 movdqa %xmm0,`16*($i+0)+112`(%r10)
2307 pcmpeqd %xmm5,%xmm2 # compare to 5,4
2308 movdqa %xmm1,`16*($i+1)+112`(%r10)
2312 pcmpeqd %xmm5,%xmm3 # compare to 7,6
2313 movdqa %xmm2,`16*($i+2)+112`(%r10)
2318 movdqa %xmm3,`16*($i+3)+112`(%r10)
2322 $code.=<<___; # last iteration can be optimized
2326 movdqa %xmm0,`16*($i+0)+112`(%r10)
2330 movdqa %xmm1,`16*($i+1)+112`(%r10)
2333 movdqa %xmm2,`16*($i+2)+112`(%r10)
2335 pand `16*($i+0)-128`($bptr),%xmm0 # while it's still in register
2336 pand `16*($i+1)-128`($bptr),%xmm1
2337 pand `16*($i+2)-128`($bptr),%xmm2
2338 movdqa %xmm3,`16*($i+3)+112`(%r10)
2339 pand `16*($i+3)-128`($bptr),%xmm3
2343 for($i=0;$i<$STRIDE/16-4;$i+=4) {
2345 movdqa `16*($i+0)-128`($bptr),%xmm4
2346 movdqa `16*($i+1)-128`($bptr),%xmm5
2347 movdqa `16*($i+2)-128`($bptr),%xmm2
2348 pand `16*($i+0)+112`(%r10),%xmm4
2349 movdqa `16*($i+3)-128`($bptr),%xmm3
2350 pand `16*($i+1)+112`(%r10),%xmm5
2352 pand `16*($i+2)+112`(%r10),%xmm2
2354 pand `16*($i+3)+112`(%r10),%xmm3
2361 pshufd \$0x4e,%xmm0,%xmm1
2363 lea $STRIDE($bptr),$bptr
2364 movq %xmm0,%rdx # bp[0]
2365 lea 64+8*4+8(%rsp),$tptr
2368 mulx 0*8($aptr),$mi,%rax # a[0]*b[0]
2369 mulx 1*8($aptr),%r11,%r12 # a[1]*b[0]
2371 mulx 2*8($aptr),%rax,%r13 # ...
2374 mulx 3*8($aptr),%rax,%r14
2377 imulq 32+8(%rsp),$mi # "t[0]"*n0
2378 xor $zero,$zero # cf=0, of=0
2381 mov $bptr,8+8(%rsp) # off-load &b[i]
2383 lea 4*8($aptr),$aptr
2385 adcx $zero,%r14 # cf=0
2387 mulx 0*8($nptr),%rax,%r10
2388 adcx %rax,%r15 # discarded
2390 mulx 1*8($nptr),%rax,%r11
2393 mulx 2*8($nptr),%rax,%r12
2394 mov 24+8(%rsp),$bptr # counter value
2395 mov %r10,-8*4($tptr)
2398 mulx 3*8($nptr),%rax,%r15
2400 mov %r11,-8*3($tptr)
2402 adox $zero,%r15 # of=0
2403 lea 4*8($nptr),$nptr
2404 mov %r12,-8*2($tptr)
2409 adcx $zero,%r15 # cf=0, modulo-scheduled
2410 mulx 0*8($aptr),%r10,%rax # a[4]*b[0]
2412 mulx 1*8($aptr),%r11,%r14 # a[5]*b[0]
2414 mulx 2*8($aptr),%r12,%rax # ...
2416 mulx 3*8($aptr),%r13,%r14
2420 adcx $zero,%r14 # cf=0
2421 lea 4*8($aptr),$aptr
2422 lea 4*8($tptr),$tptr
2425 mulx 0*8($nptr),%rax,%r15
2428 mulx 1*8($nptr),%rax,%r15
2431 mulx 2*8($nptr),%rax,%r15
2432 mov %r10,-5*8($tptr)
2434 mov %r11,-4*8($tptr)
2436 mulx 3*8($nptr),%rax,%r15
2438 mov %r12,-3*8($tptr)
2441 lea 4*8($nptr),$nptr
2442 mov %r13,-2*8($tptr)
2444 dec $bptr # of=0, pass cf
2447 mov 8(%rsp),$num # load -num
2448 adc $zero,%r15 # modulo-scheduled
2449 lea ($aptr,$num),$aptr # rewind $aptr
2451 mov 8+8(%rsp),$bptr # re-load &b[i]
2452 adc $zero,$zero # top-most carry
2453 mov %r14,-1*8($tptr)
2458 lea 16-256($tptr),%r10 # where 256-byte mask is (+density control)
2463 for($i=0;$i<$STRIDE/16;$i+=4) {
2465 movdqa `16*($i+0)-128`($bptr),%xmm0
2466 movdqa `16*($i+1)-128`($bptr),%xmm1
2467 movdqa `16*($i+2)-128`($bptr),%xmm2
2468 pand `16*($i+0)+256`(%r10),%xmm0
2469 movdqa `16*($i+3)-128`($bptr),%xmm3
2470 pand `16*($i+1)+256`(%r10),%xmm1
2472 pand `16*($i+2)+256`(%r10),%xmm2
2474 pand `16*($i+3)+256`(%r10),%xmm3
2481 pshufd \$0x4e,%xmm4,%xmm0
2483 lea $STRIDE($bptr),$bptr
2484 movq %xmm0,%rdx # m0=bp[i]
2486 mov $zero,($tptr) # save top-most carry
2487 lea 4*8($tptr,$num),$tptr # rewind $tptr
2488 mulx 0*8($aptr),$mi,%r11 # a[0]*b[i]
2489 xor $zero,$zero # cf=0, of=0
2491 mulx 1*8($aptr),%r14,%r12 # a[1]*b[i]
2492 adox -4*8($tptr),$mi # +t[0]
2494 mulx 2*8($aptr),%r15,%r13 # ...
2495 adox -3*8($tptr),%r11
2497 mulx 3*8($aptr),%rdx,%r14
2498 adox -2*8($tptr),%r12
2500 lea ($nptr,$num),$nptr # rewind $nptr
2501 lea 4*8($aptr),$aptr
2502 adox -1*8($tptr),%r13
2507 imulq 32+8(%rsp),$mi # "t[0]"*n0
2510 xor $zero,$zero # cf=0, of=0
2511 mov $bptr,8+8(%rsp) # off-load &b[i]
2513 mulx 0*8($nptr),%rax,%r10
2514 adcx %rax,%r15 # discarded
2516 mulx 1*8($nptr),%rax,%r11
2519 mulx 2*8($nptr),%rax,%r12
2522 mulx 3*8($nptr),%rax,%r15
2524 mov 24+8(%rsp),$bptr # counter value
2525 mov %r10,-8*4($tptr)
2527 mov %r11,-8*3($tptr)
2528 adox $zero,%r15 # of=0
2529 mov %r12,-8*2($tptr)
2530 lea 4*8($nptr),$nptr
2535 mulx 0*8($aptr),%r10,%rax # a[4]*b[i]
2536 adcx $zero,%r15 # cf=0, modulo-scheduled
2538 mulx 1*8($aptr),%r11,%r14 # a[5]*b[i]
2539 adcx 0*8($tptr),%r10
2541 mulx 2*8($aptr),%r12,%rax # ...
2542 adcx 1*8($tptr),%r11
2544 mulx 3*8($aptr),%r13,%r14
2546 adcx 2*8($tptr),%r12
2548 adcx 3*8($tptr),%r13
2549 adox $zero,%r14 # of=0
2550 lea 4*8($aptr),$aptr
2551 lea 4*8($tptr),$tptr
2552 adcx $zero,%r14 # cf=0
2555 mulx 0*8($nptr),%rax,%r15
2558 mulx 1*8($nptr),%rax,%r15
2561 mulx 2*8($nptr),%rax,%r15
2562 mov %r10,-5*8($tptr)
2565 mov %r11,-4*8($tptr)
2566 mulx 3*8($nptr),%rax,%r15
2568 lea 4*8($nptr),$nptr
2569 mov %r12,-3*8($tptr)
2572 mov %r13,-2*8($tptr)
2574 dec $bptr # of=0, pass cf
2577 mov 0+8(%rsp),$num # load -num
2578 adc $zero,%r15 # modulo-scheduled
2579 sub 0*8($tptr),$bptr # pull top-most carry to %cf
2580 mov 8+8(%rsp),$bptr # re-load &b[i]
2583 lea ($aptr,$num),$aptr # rewind $aptr
2584 adc $zero,$zero # top-most carry
2585 mov %r14,-1*8($tptr)
2592 mov ($nptr,$num),%r12
2593 lea ($nptr,$num),%rbp # rewind $nptr
2595 lea ($tptr,$num),%rdi # rewind $tptr
2598 sub %r14,%r10 # compare top-most words
2602 sub %r8,%rax # %rax=-%r8
2603 mov 56+8(%rsp),%rdx # restore rp
2604 dec %r12 # so that after 'not' we get -n[0]
2609 jmp .Lsqrx4x_sub_entry # common post-condition
2610 .size mulx4x_internal,.-mulx4x_internal
2613 ######################################################################
2615 my $rptr="%rdi"; # BN_ULONG *rptr,
2616 my $aptr="%rsi"; # const BN_ULONG *aptr,
2617 my $bptr="%rdx"; # const void *table,
2618 my $nptr="%rcx"; # const BN_ULONG *nptr,
2619 my $n0 ="%r8"; # const BN_ULONG *n0);
2620 my $num ="%r9"; # int num, has to be divisible by 8
2623 my ($i,$j,$tptr)=("%rbp","%rcx",$rptr);
2624 my @A0=("%r10","%r11");
2625 my @A1=("%r12","%r13");
2626 my ($a0,$a1,$ai)=("%r14","%r15","%rbx");
2629 .type bn_powerx5,\@function,6
2641 shl \$3,${num}d # convert $num to bytes
2642 lea ($num,$num,2),%r10 # 3*$num in bytes
2646 ##############################################################
2647 # Ensure that stack frame doesn't alias with $rptr+3*$num
2648 # modulo 4096, which covers ret[num], am[num] and n[num]
2649 # (see bn_exp.c). This is done to allow memory disambiguation
2650 # logic do its magic. [Extra 256 bytes is for power mask
2651 # calculated from 7th argument, the index.]
2653 lea -320(%rsp,$num,2),%r11
2658 sub %r11,%rsp # align with $aptr
2659 lea -320(%rsp,$num,2),%rsp # alloca(frame+2*$num*8+256)
2664 lea 4096-320(,$num,2),%r10
2665 lea -320(%rsp,$num,2),%rsp # alloca(frame+2*$num*8+256)
2676 mov (%rsp,%r11),%r10
2678 .byte 0x2e # predict non-taken
2679 jnc .Lpwrx_page_walk
2684 ##############################################################
2687 # +0 saved $num, used in reduction section
2688 # +8 &t[2*$num], used in reduction section
2689 # +16 intermediate carry bit
2690 # +24 top-most carry bit, used in reduction section
2696 movq $rptr,%xmm1 # save $rptr
2697 movq $nptr,%xmm2 # save $nptr
2698 movq %r10, %xmm3 # -$num
2701 mov %rax, 40(%rsp) # save original %rsp
2704 call __bn_sqrx8x_internal
2705 call __bn_postx4x_internal
2706 call __bn_sqrx8x_internal
2707 call __bn_postx4x_internal
2708 call __bn_sqrx8x_internal
2709 call __bn_postx4x_internal
2710 call __bn_sqrx8x_internal
2711 call __bn_postx4x_internal
2712 call __bn_sqrx8x_internal
2713 call __bn_postx4x_internal
2715 mov %r10,$num # -num
2721 call mulx4x_internal
2723 mov 40(%rsp),%rsi # restore %rsp
2735 .size bn_powerx5,.-bn_powerx5
2737 .globl bn_sqrx8x_internal
2738 .hidden bn_sqrx8x_internal
2739 .type bn_sqrx8x_internal,\@abi-omnipotent
2742 __bn_sqrx8x_internal:
2743 ##################################################################
2746 # a) multiply-n-add everything but a[i]*a[i];
2747 # b) shift result of a) by 1 to the left and accumulate
2748 # a[i]*a[i] products;
2750 ##################################################################
2751 # 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]
2782 # 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]
2785 my ($zero,$carry)=("%rbp","%rcx");
2788 lea 48+8(%rsp),$tptr
2789 lea ($aptr,$num),$aaptr
2790 mov $num,0+8(%rsp) # save $num
2791 mov $aaptr,8+8(%rsp) # save end of $aptr
2792 jmp .Lsqr8x_zero_start
2795 .byte 0x66,0x66,0x66,0x2e,0x0f,0x1f,0x84,0x00,0x00,0x00,0x00,0x00
2798 movdqa %xmm0,0*8($tptr)
2799 movdqa %xmm0,2*8($tptr)
2800 movdqa %xmm0,4*8($tptr)
2801 movdqa %xmm0,6*8($tptr)
2802 .Lsqr8x_zero_start: # aligned at 32
2803 movdqa %xmm0,8*8($tptr)
2804 movdqa %xmm0,10*8($tptr)
2805 movdqa %xmm0,12*8($tptr)
2806 movdqa %xmm0,14*8($tptr)
2807 lea 16*8($tptr),$tptr
2811 mov 0*8($aptr),%rdx # a[0], modulo-scheduled
2812 #xor %r9,%r9 # t[1], ex-$num, zero already
2819 lea 48+8(%rsp),$tptr
2820 xor $zero,$zero # cf=0, cf=0
2821 jmp .Lsqrx8x_outer_loop
2824 .Lsqrx8x_outer_loop:
2825 mulx 1*8($aptr),%r8,%rax # a[1]*a[0]
2826 adcx %r9,%r8 # a[1]*a[0]+=t[1]
2828 mulx 2*8($aptr),%r9,%rax # a[2]*a[0]
2831 .byte 0xc4,0xe2,0xab,0xf6,0x86,0x18,0x00,0x00,0x00 # mulx 3*8($aptr),%r10,%rax # ...
2834 .byte 0xc4,0xe2,0xa3,0xf6,0x86,0x20,0x00,0x00,0x00 # mulx 4*8($aptr),%r11,%rax
2837 mulx 5*8($aptr),%r12,%rax
2840 mulx 6*8($aptr),%r13,%rax
2843 mulx 7*8($aptr),%r14,%r15
2844 mov 1*8($aptr),%rdx # a[1]
2848 mov %r8,1*8($tptr) # t[1]
2849 mov %r9,2*8($tptr) # t[2]
2850 sbb $carry,$carry # mov %cf,$carry
2851 xor $zero,$zero # cf=0, of=0
2854 mulx 2*8($aptr),%r8,%rbx # a[2]*a[1]
2855 mulx 3*8($aptr),%r9,%rax # a[3]*a[1]
2858 mulx 4*8($aptr),%r10,%rbx # ...
2861 .byte 0xc4,0xe2,0xa3,0xf6,0x86,0x28,0x00,0x00,0x00 # mulx 5*8($aptr),%r11,%rax
2864 .byte 0xc4,0xe2,0x9b,0xf6,0x9e,0x30,0x00,0x00,0x00 # mulx 6*8($aptr),%r12,%rbx
2867 .byte 0xc4,0x62,0x93,0xf6,0xb6,0x38,0x00,0x00,0x00 # mulx 7*8($aptr),%r13,%r14
2868 mov 2*8($aptr),%rdx # a[2]
2872 adox $zero,%r14 # of=0
2873 adcx $zero,%r14 # cf=0
2875 mov %r8,3*8($tptr) # t[3]
2876 mov %r9,4*8($tptr) # t[4]
2878 mulx 3*8($aptr),%r8,%rbx # a[3]*a[2]
2879 mulx 4*8($aptr),%r9,%rax # a[4]*a[2]
2882 mulx 5*8($aptr),%r10,%rbx # ...
2885 .byte 0xc4,0xe2,0xa3,0xf6,0x86,0x30,0x00,0x00,0x00 # mulx 6*8($aptr),%r11,%rax
2888 .byte 0xc4,0x62,0x9b,0xf6,0xae,0x38,0x00,0x00,0x00 # mulx 7*8($aptr),%r12,%r13
2890 mov 3*8($aptr),%rdx # a[3]
2894 mov %r8,5*8($tptr) # t[5]
2895 mov %r9,6*8($tptr) # t[6]
2896 mulx 4*8($aptr),%r8,%rax # a[4]*a[3]
2897 adox $zero,%r13 # of=0
2898 adcx $zero,%r13 # cf=0
2900 mulx 5*8($aptr),%r9,%rbx # a[5]*a[3]
2903 mulx 6*8($aptr),%r10,%rax # ...
2906 mulx 7*8($aptr),%r11,%r12
2907 mov 4*8($aptr),%rdx # a[4]
2908 mov 5*8($aptr),%r14 # a[5]
2911 mov 6*8($aptr),%r15 # a[6]
2913 adox $zero,%r12 # of=0
2914 adcx $zero,%r12 # cf=0
2916 mov %r8,7*8($tptr) # t[7]
2917 mov %r9,8*8($tptr) # t[8]
2919 mulx %r14,%r9,%rax # a[5]*a[4]
2920 mov 7*8($aptr),%r8 # a[7]
2922 mulx %r15,%r10,%rbx # a[6]*a[4]
2925 mulx %r8,%r11,%rax # a[7]*a[4]
2926 mov %r14,%rdx # a[5]
2929 #adox $zero,%rax # of=0
2930 adcx $zero,%rax # cf=0
2932 mulx %r15,%r14,%rbx # a[6]*a[5]
2933 mulx %r8,%r12,%r13 # a[7]*a[5]
2934 mov %r15,%rdx # a[6]
2935 lea 8*8($aptr),$aptr
2942 mulx %r8,%r8,%r14 # a[7]*a[6]
2947 je .Lsqrx8x_outer_break
2949 neg $carry # mov $carry,%cf
2953 adcx 9*8($tptr),%r9 # +=t[9]
2954 adcx 10*8($tptr),%r10 # ...
2955 adcx 11*8($tptr),%r11
2956 adc 12*8($tptr),%r12
2957 adc 13*8($tptr),%r13
2958 adc 14*8($tptr),%r14
2959 adc 15*8($tptr),%r15
2961 lea 2*64($tptr),$tptr
2962 sbb %rax,%rax # mov %cf,$carry
2964 mov -64($aptr),%rdx # a[0]
2965 mov %rax,16+8(%rsp) # offload $carry
2966 mov $tptr,24+8(%rsp)
2968 #lea 8*8($tptr),$tptr # see 2*8*8($tptr) above
2969 xor %eax,%eax # cf=0, of=0
2975 mulx 0*8($aaptr),%rax,%r8 # a[8]*a[i]
2976 adcx %rax,%rbx # +=t[8]
2979 mulx 1*8($aaptr),%rax,%r9 # ...
2983 mulx 2*8($aaptr),%rax,%r10
2987 mulx 3*8($aaptr),%rax,%r11
2991 .byte 0xc4,0x62,0xfb,0xf6,0xa5,0x20,0x00,0x00,0x00 # mulx 4*8($aaptr),%rax,%r12
2995 mulx 5*8($aaptr),%rax,%r13
2999 mulx 6*8($aaptr),%rax,%r14
3000 mov %rbx,($tptr,%rcx,8) # store t[8+i]
3005 .byte 0xc4,0x62,0xfb,0xf6,0xbd,0x38,0x00,0x00,0x00 # mulx 7*8($aaptr),%rax,%r15
3006 mov 8($aptr,%rcx,8),%rdx # a[i]
3008 adox %rbx,%r15 # %rbx is 0, of=0
3009 adcx %rbx,%r15 # cf=0
3015 lea 8*8($aaptr),$aaptr
3017 cmp 8+8(%rsp),$aaptr # done?
3020 sub 16+8(%rsp),%rbx # mov 16(%rsp),%cf
3031 lea 8*8($tptr),$tptr
3033 sbb %rax,%rax # mov %cf,%rax
3034 xor %ebx,%ebx # cf=0, of=0
3035 mov %rax,16+8(%rsp) # offload carry
3040 sub 16+8(%rsp),%r8 # consume last carry
3041 mov 24+8(%rsp),$carry # initial $tptr, borrow $carry
3042 mov 0*8($aptr),%rdx # a[8], modulo-scheduled
3043 xor %ebp,%ebp # xor $zero,$zero
3045 cmp $carry,$tptr # cf=0, of=0
3046 je .Lsqrx8x_outer_loop
3051 mov 2*8($carry),%r10
3053 mov 3*8($carry),%r11
3055 mov 4*8($carry),%r12
3057 mov 5*8($carry),%r13
3059 mov 6*8($carry),%r14
3061 mov 7*8($carry),%r15
3063 jmp .Lsqrx8x_outer_loop
3066 .Lsqrx8x_outer_break:
3067 mov %r9,9*8($tptr) # t[9]
3068 movq %xmm3,%rcx # -$num
3069 mov %r10,10*8($tptr) # ...
3070 mov %r11,11*8($tptr)
3071 mov %r12,12*8($tptr)
3072 mov %r13,13*8($tptr)
3073 mov %r14,14*8($tptr)
3078 lea 48+8(%rsp),$tptr
3079 mov ($aptr,$i),%rdx # a[0]
3081 mov 8($tptr),$A0[1] # t[1]
3082 xor $A0[0],$A0[0] # t[0], of=0, cf=0
3083 mov 0+8(%rsp),$num # restore $num
3085 mov 16($tptr),$A1[0] # t[2] # prefetch
3086 mov 24($tptr),$A1[1] # t[3] # prefetch
3087 #jmp .Lsqrx4x_shift_n_add # happens to be aligned
3090 .Lsqrx4x_shift_n_add:
3094 .byte 0x48,0x8b,0x94,0x0e,0x08,0x00,0x00,0x00 # mov 8($aptr,$i),%rdx # a[i+1] # prefetch
3095 .byte 0x4c,0x8b,0x97,0x20,0x00,0x00,0x00 # mov 32($tptr),$A0[0] # t[2*i+4] # prefetch
3098 mov 40($tptr),$A0[1] # t[2*i+4+1] # prefetch
3105 mov 16($aptr,$i),%rdx # a[i+2] # prefetch
3106 mov 48($tptr),$A1[0] # t[2*i+6] # prefetch
3109 mov 56($tptr),$A1[1] # t[2*i+6+1] # prefetch
3116 mov 24($aptr,$i),%rdx # a[i+3] # prefetch
3118 mov 64($tptr),$A0[0] # t[2*i+8] # prefetch
3121 mov 72($tptr),$A0[1] # t[2*i+8+1] # prefetch
3128 jrcxz .Lsqrx4x_shift_n_add_break
3129 .byte 0x48,0x8b,0x94,0x0e,0x00,0x00,0x00,0x00 # mov 0($aptr,$i),%rdx # a[i+4] # prefetch
3132 mov 80($tptr),$A1[0] # t[2*i+10] # prefetch
3133 mov 88($tptr),$A1[1] # t[2*i+10+1] # prefetch
3138 jmp .Lsqrx4x_shift_n_add
3141 .Lsqrx4x_shift_n_add_break:
3145 lea 64($tptr),$tptr # end of t[] buffer
3148 ######################################################################
3149 # Montgomery reduction part, "word-by-word" algorithm.
3151 # This new path is inspired by multiple submissions from Intel, by
3152 # Shay Gueron, Vlad Krasnov, Erdinc Ozturk, James Guilford,
3155 my ($nptr,$carry,$m0)=("%rbp","%rsi","%rdx");
3159 __bn_sqrx8x_reduction:
3160 xor %eax,%eax # initial top-most carry bit
3161 mov 32+8(%rsp),%rbx # n0
3162 mov 48+8(%rsp),%rdx # "%r8", 8*0($tptr)
3163 lea -8*8($nptr,$num),%rcx # end of n[]
3164 #lea 48+8(%rsp,$num,2),$tptr # end of t[] buffer
3165 mov %rcx, 0+8(%rsp) # save end of n[]
3166 mov $tptr,8+8(%rsp) # save end of t[]
3168 lea 48+8(%rsp),$tptr # initial t[] window
3169 jmp .Lsqrx8x_reduction_loop
3172 .Lsqrx8x_reduction_loop:
3178 imulq %rbx,%rdx # n0*a[i]
3182 mov %rax,24+8(%rsp) # store top-most carry bit
3184 lea 8*8($tptr),$tptr
3185 xor $carry,$carry # cf=0,of=0
3192 mulx 8*0($nptr),%rax,%r8 # n[0]
3193 adcx %rbx,%rax # discarded
3196 mulx 8*1($nptr),%rbx,%r9 # n[1]
3200 mulx 8*2($nptr),%rbx,%r10
3204 mulx 8*3($nptr),%rbx,%r11
3208 .byte 0xc4,0x62,0xe3,0xf6,0xa5,0x20,0x00,0x00,0x00 # mulx 8*4($nptr),%rbx,%r12
3214 mulx 32+8(%rsp),%rbx,%rdx # %rdx discarded
3216 mov %rax,64+48+8(%rsp,%rcx,8) # put aside n0*a[i]
3218 mulx 8*5($nptr),%rax,%r13
3222 mulx 8*6($nptr),%rax,%r14
3226 mulx 8*7($nptr),%rax,%r15
3229 adox $carry,%r15 # $carry is 0
3230 adcx $carry,%r15 # cf=0
3232 .byte 0x67,0x67,0x67
3236 mov $carry,%rax # xor %rax,%rax
3237 cmp 0+8(%rsp),$nptr # end of n[]?
3238 jae .Lsqrx8x_no_tail
3240 mov 48+8(%rsp),%rdx # pull n0*a[0]
3242 lea 8*8($nptr),$nptr
3245 adcx 8*2($tptr),%r10
3251 lea 8*8($tptr),$tptr
3252 sbb %rax,%rax # top carry
3254 xor $carry,$carry # of=0, cf=0
3261 mulx 8*0($nptr),%rax,%r8
3265 mulx 8*1($nptr),%rax,%r9
3269 mulx 8*2($nptr),%rax,%r10
3273 mulx 8*3($nptr),%rax,%r11
3277 .byte 0xc4,0x62,0xfb,0xf6,0xa5,0x20,0x00,0x00,0x00 # mulx 8*4($nptr),%rax,%r12
3281 mulx 8*5($nptr),%rax,%r13
3285 mulx 8*6($nptr),%rax,%r14
3289 mulx 8*7($nptr),%rax,%r15
3290 mov 72+48+8(%rsp,%rcx,8),%rdx # pull n0*a[i]
3293 mov %rbx,($tptr,%rcx,8) # save result
3295 adcx $carry,%r15 # cf=0
3300 cmp 0+8(%rsp),$nptr # end of n[]?
3301 jae .Lsqrx8x_tail_done # break out of loop
3303 sub 16+8(%rsp),$carry # mov 16(%rsp),%cf
3304 mov 48+8(%rsp),%rdx # pull n0*a[0]
3305 lea 8*8($nptr),$nptr
3314 lea 8*8($tptr),$tptr
3316 sub \$8,%rcx # mov \$-8,%rcx
3318 xor $carry,$carry # of=0, cf=0
3324 add 24+8(%rsp),%r8 # can this overflow?
3331 adc \$0,%r15 # can't overflow, because we
3332 # started with "overhung" part
3334 mov $carry,%rax # xor %rax,%rax
3336 sub 16+8(%rsp),$carry # mov 16(%rsp),%cf
3337 .Lsqrx8x_no_tail: # %cf is 0 if jumped here
3341 mov 8*7($nptr),$carry
3342 movq %xmm2,$nptr # restore $nptr
3349 adc %rax,%rax # top-most carry
3351 mov 32+8(%rsp),%rbx # n0
3352 mov 8*8($tptr,%rcx),%rdx # modulo-scheduled "%r8"
3354 mov %r8,8*0($tptr) # store top 512 bits
3355 lea 8*8($tptr),%r8 # borrow %r8
3364 lea 8*8($tptr,%rcx),$tptr # start of current t[] window
3365 cmp 8+8(%rsp),%r8 # end of t[]?
3366 jb .Lsqrx8x_reduction_loop
3368 .size bn_sqrx8x_internal,.-bn_sqrx8x_internal
3371 ##############################################################
3372 # Post-condition, 4x unrolled
3375 my ($rptr,$nptr)=("%rdx","%rbp");
3378 __bn_postx4x_internal:
3380 mov %rcx,%r10 # -$num
3381 mov %rcx,%r9 # -$num
3384 #lea 48+8(%rsp,%r9),$tptr
3385 movq %xmm1,$rptr # restore $rptr
3386 movq %xmm1,$aptr # prepare for back-to-back call
3387 dec %r12 # so that after 'not' we get -n[0]
3392 jmp .Lsqrx4x_sub_entry
3402 lea 8*4($nptr),$nptr
3407 neg %r8 # mov %r8,%cf
3413 lea 8*4($tptr),$tptr
3415 sbb %r8,%r8 # mov %cf,%r8
3418 lea 8*4($rptr),$rptr
3423 neg %r9 # restore $num
3426 .size __bn_postx4x_internal,.-__bn_postx4x_internal
3431 my ($inp,$num,$tbl,$idx)=$win64?("%rcx","%edx","%r8", "%r9d") : # Win64 order
3432 ("%rdi","%esi","%rdx","%ecx"); # Unix order
3439 .type bn_get_bits5,\@abi-omnipotent
3451 movzw (%r10,$num,2),%eax
3455 .size bn_get_bits5,.-bn_get_bits5
3458 .type bn_scatter5,\@abi-omnipotent
3462 jz .Lscatter_epilogue
3463 lea ($tbl,$idx,8),$tbl
3473 .size bn_scatter5,.-bn_scatter5
3476 .type bn_gather5,\@abi-omnipotent
3479 .LSEH_begin_bn_gather5: # Win64 thing, but harmless in other cases
3480 # I can't trust assembler to use specific encoding:-(
3481 .byte 0x4c,0x8d,0x14,0x24 #lea (%rsp),%r10
3482 .byte 0x48,0x81,0xec,0x08,0x01,0x00,0x00 #sub $0x108,%rsp
3483 lea .Linc(%rip),%rax
3484 and \$-16,%rsp # shouldn't be formally required
3487 movdqa 0(%rax),%xmm0 # 00000001000000010000000000000000
3488 movdqa 16(%rax),%xmm1 # 00000002000000020000000200000002
3489 lea 128($tbl),%r11 # size optimization
3490 lea 128(%rsp),%rax # size optimization
3492 pshufd \$0,%xmm5,%xmm5 # broadcast $idx
3496 ########################################################################
3497 # calculate mask by comparing 0..31 to $idx and save result to stack
3499 for($i=0;$i<$STRIDE/16;$i+=4) {
3502 pcmpeqd %xmm5,%xmm0 # compare to 1,0
3504 $code.=<<___ if ($i);
3505 movdqa %xmm3,`16*($i-1)-128`(%rax)
3511 pcmpeqd %xmm5,%xmm1 # compare to 3,2
3512 movdqa %xmm0,`16*($i+0)-128`(%rax)
3516 pcmpeqd %xmm5,%xmm2 # compare to 5,4
3517 movdqa %xmm1,`16*($i+1)-128`(%rax)
3521 pcmpeqd %xmm5,%xmm3 # compare to 7,6
3522 movdqa %xmm2,`16*($i+2)-128`(%rax)
3527 movdqa %xmm3,`16*($i-1)-128`(%rax)
3535 for($i=0;$i<$STRIDE/16;$i+=4) {
3537 movdqa `16*($i+0)-128`(%r11),%xmm0
3538 movdqa `16*($i+1)-128`(%r11),%xmm1
3539 movdqa `16*($i+2)-128`(%r11),%xmm2
3540 pand `16*($i+0)-128`(%rax),%xmm0
3541 movdqa `16*($i+3)-128`(%r11),%xmm3
3542 pand `16*($i+1)-128`(%rax),%xmm1
3544 pand `16*($i+2)-128`(%rax),%xmm2
3546 pand `16*($i+3)-128`(%rax),%xmm3
3553 lea $STRIDE(%r11),%r11
3554 pshufd \$0x4e,%xmm4,%xmm0
3556 movq %xmm0,($out) # m0=bp[0]
3563 .LSEH_end_bn_gather5:
3564 .size bn_gather5,.-bn_gather5
3572 .asciz "Montgomery Multiplication with scatter/gather for x86_64, CRYPTOGAMS by <appro\@openssl.org>"
3575 # EXCEPTION_DISPOSITION handler (EXCEPTION_RECORD *rec,ULONG64 frame,
3576 # CONTEXT *context,DISPATCHER_CONTEXT *disp)
3584 .extern __imp_RtlVirtualUnwind
3585 .type mul_handler,\@abi-omnipotent
3599 mov 120($context),%rax # pull context->Rax
3600 mov 248($context),%rbx # pull context->Rip
3602 mov 8($disp),%rsi # disp->ImageBase
3603 mov 56($disp),%r11 # disp->HandlerData
3605 mov 0(%r11),%r10d # HandlerData[0]
3606 lea (%rsi,%r10),%r10 # end of prologue label
3607 cmp %r10,%rbx # context->Rip<end of prologue label
3608 jb .Lcommon_seh_tail
3610 mov 152($context),%rax # pull context->Rsp
3612 mov 4(%r11),%r10d # HandlerData[1]
3613 lea (%rsi,%r10),%r10 # epilogue label
3614 cmp %r10,%rbx # context->Rip>=epilogue label
3615 jae .Lcommon_seh_tail
3617 lea .Lmul_epilogue(%rip),%r10
3621 mov 192($context),%r10 # pull $num
3622 mov 8(%rax,%r10,8),%rax # pull saved stack pointer
3627 mov 40(%rax),%rax # pull saved stack pointer
3635 mov %rbx,144($context) # restore context->Rbx
3636 mov %rbp,160($context) # restore context->Rbp
3637 mov %r12,216($context) # restore context->R12
3638 mov %r13,224($context) # restore context->R13
3639 mov %r14,232($context) # restore context->R14
3640 mov %r15,240($context) # restore context->R15
3645 mov %rax,152($context) # restore context->Rsp
3646 mov %rsi,168($context) # restore context->Rsi
3647 mov %rdi,176($context) # restore context->Rdi
3649 mov 40($disp),%rdi # disp->ContextRecord
3650 mov $context,%rsi # context
3651 mov \$154,%ecx # sizeof(CONTEXT)
3652 .long 0xa548f3fc # cld; rep movsq
3655 xor %rcx,%rcx # arg1, UNW_FLAG_NHANDLER
3656 mov 8(%rsi),%rdx # arg2, disp->ImageBase
3657 mov 0(%rsi),%r8 # arg3, disp->ControlPc
3658 mov 16(%rsi),%r9 # arg4, disp->FunctionEntry
3659 mov 40(%rsi),%r10 # disp->ContextRecord
3660 lea 56(%rsi),%r11 # &disp->HandlerData
3661 lea 24(%rsi),%r12 # &disp->EstablisherFrame
3662 mov %r10,32(%rsp) # arg5
3663 mov %r11,40(%rsp) # arg6
3664 mov %r12,48(%rsp) # arg7
3665 mov %rcx,56(%rsp) # arg8, (NULL)
3666 call *__imp_RtlVirtualUnwind(%rip)
3668 mov \$1,%eax # ExceptionContinueSearch
3680 .size mul_handler,.-mul_handler
3684 .rva .LSEH_begin_bn_mul_mont_gather5
3685 .rva .LSEH_end_bn_mul_mont_gather5
3686 .rva .LSEH_info_bn_mul_mont_gather5
3688 .rva .LSEH_begin_bn_mul4x_mont_gather5
3689 .rva .LSEH_end_bn_mul4x_mont_gather5
3690 .rva .LSEH_info_bn_mul4x_mont_gather5
3692 .rva .LSEH_begin_bn_power5
3693 .rva .LSEH_end_bn_power5
3694 .rva .LSEH_info_bn_power5
3696 .rva .LSEH_begin_bn_from_mont8x
3697 .rva .LSEH_end_bn_from_mont8x
3698 .rva .LSEH_info_bn_from_mont8x
3700 $code.=<<___ if ($addx);
3701 .rva .LSEH_begin_bn_mulx4x_mont_gather5
3702 .rva .LSEH_end_bn_mulx4x_mont_gather5
3703 .rva .LSEH_info_bn_mulx4x_mont_gather5
3705 .rva .LSEH_begin_bn_powerx5
3706 .rva .LSEH_end_bn_powerx5
3707 .rva .LSEH_info_bn_powerx5
3710 .rva .LSEH_begin_bn_gather5
3711 .rva .LSEH_end_bn_gather5
3712 .rva .LSEH_info_bn_gather5
3716 .LSEH_info_bn_mul_mont_gather5:
3719 .rva .Lmul_body,.Lmul_epilogue # HandlerData[]
3721 .LSEH_info_bn_mul4x_mont_gather5:
3724 .rva .Lmul4x_body,.Lmul4x_epilogue # HandlerData[]
3726 .LSEH_info_bn_power5:
3729 .rva .Lpower5_body,.Lpower5_epilogue # HandlerData[]
3731 .LSEH_info_bn_from_mont8x:
3734 .rva .Lfrom_body,.Lfrom_epilogue # HandlerData[]
3736 $code.=<<___ if ($addx);
3738 .LSEH_info_bn_mulx4x_mont_gather5:
3741 .rva .Lmulx4x_body,.Lmulx4x_epilogue # HandlerData[]
3743 .LSEH_info_bn_powerx5:
3746 .rva .Lpowerx5_body,.Lpowerx5_epilogue # HandlerData[]
3750 .LSEH_info_bn_gather5:
3751 .byte 0x01,0x0b,0x03,0x0a
3752 .byte 0x0b,0x01,0x21,0x00 # sub rsp,0x108
3753 .byte 0x04,0xa3,0x00,0x00 # lea r10,(rsp)
3758 $code =~ s/\`([^\`]*)\`/eval($1)/gem;