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 # Montgomery multiplication routine for x86_64. While it gives modest
13 # 9% improvement of rsa4096 sign on Opteron, rsa512 sign runs more
14 # than twice, >2x, as fast. Most common rsa1024 sign is improved by
15 # respectful 50%. It remains to be seen if loop unrolling and
16 # dedicated squaring routine can provide further improvement...
20 # Add dedicated squaring procedure. Performance improvement varies
21 # from platform to platform, but in average it's ~5%/15%/25%/33%
22 # for 512-/1024-/2048-/4096-bit RSA *sign* benchmarks respectively.
26 # Unroll and modulo-schedule inner loops in such manner that they
27 # are "fallen through" for input lengths of 8, which is critical for
28 # 1024-bit RSA *sign*. Average performance improvement in comparison
29 # to *initial* version of this module from 2005 is ~0%/30%/40%/45%
30 # for 512-/1024-/2048-/4096-bit RSA *sign* benchmarks respectively.
34 if ($flavour =~ /\./) { $output = $flavour; undef $flavour; }
36 $win64=0; $win64=1 if ($flavour =~ /[nm]asm|mingw64/ || $output =~ /\.asm$/);
38 $0 =~ m/(.*[\/\\])[^\
/\\]+$/; $dir=$1;
39 ( $xlate="${dir}x86_64-xlate.pl" and -f
$xlate ) or
40 ( $xlate="${dir}../../perlasm/x86_64-xlate.pl" and -f
$xlate) or
41 die "can't locate x86_64-xlate.pl";
43 open OUT
,"| \"$^X\" $xlate $flavour $output";
47 $rp="%rdi"; # BN_ULONG *rp,
48 $ap="%rsi"; # const BN_ULONG *ap,
49 $bp="%rdx"; # const BN_ULONG *bp,
50 $np="%rcx"; # const BN_ULONG *np,
51 $n0="%r8"; # const BN_ULONG *n0,
52 $num="%r9"; # int num);
65 .type bn_mul_mont
,\
@function,6
89 lea
(%rsp,%r10,8),%rsp # tp=alloca(8*(num+2))
90 and \
$-1024,%rsp # minimize TLB usage
92 mov
%r11,8(%rsp,$num,8) # tp[num+1]=%rsp
94 mov
$bp,%r12 # reassign $bp
98 mov
($n0),$n0 # pull n0[0] value
99 mov
($bp),$m0 # m0=bp[0]
106 mulq
$m0 # ap[0]*bp[0]
110 imulq
$lo0,$m1 # "tp[0]"*n0
114 add
%rax,$lo0 # discarded
127 add
$hi0,$hi1 # np[j]*m1+ap[j]*bp[0]
130 mov
$hi1,-16(%rsp,$j,8) # tp[j-1]
134 mulq
$m0 # ap[j]*bp[0]
146 mov
($ap),%rax # ap[0]
148 add
$hi0,$hi1 # np[j]*m1+ap[j]*bp[0]
150 mov
$hi1,-16(%rsp,$j,8) # tp[j-1]
157 mov
$hi1,-8(%rsp,$num,8)
158 mov
%rdx,(%rsp,$num,8) # store upmost overflow bit
164 mov
($bp,$i,8),$m0 # m0=bp[i]
168 mulq
$m0 # ap[0]*bp[i]
169 add
%rax,$lo0 # ap[0]*bp[i]+tp[0]
173 imulq
$lo0,$m1 # tp[0]*n0
177 add
%rax,$lo0 # discarded
180 mov
8(%rsp),$lo0 # tp[1]
191 add
$lo0,$hi1 # np[j]*m1+ap[j]*bp[i]+tp[j]
194 mov
$hi1,-16(%rsp,$j,8) # tp[j-1]
198 mulq
$m0 # ap[j]*bp[i]
202 add
$hi0,$lo0 # ap[j]*bp[i]+tp[j]
212 mov
($ap),%rax # ap[0]
214 add
$lo0,$hi1 # np[j]*m1+ap[j]*bp[i]+tp[j]
217 mov
$hi1,-16(%rsp,$j,8) # tp[j-1]
223 add
$lo0,$hi1 # pull upmost overflow bit
225 mov
$hi1,-8(%rsp,$num,8)
226 mov
%rdx,(%rsp,$num,8) # store upmost overflow bit
232 xor $i,$i # i=0 and clear CF!
233 mov
(%rsp),%rax # tp[0]
234 lea
(%rsp),$ap # borrow ap for tp
238 .Lsub
: sbb
($np,$i,8),%rax
239 mov
%rax,($rp,$i,8) # rp[i]=tp[i]-np[i]
240 mov
8($ap,$i,8),%rax # tp[i+1]
242 dec
$j # doesnn't affect CF!
245 sbb \
$0,%rax # handle upmost overflow bit
252 or $np,$ap # ap=borrow?tp:rp
254 .Lcopy
: # copy or in-place refresh
256 mov
$i,(%rsp,$i,8) # zap temporary vector
257 mov
%rax,($rp,$i,8) # rp[i]=tp[i]
262 mov
8(%rsp,$num,8),%rsi # restore %rsp
273 .size bn_mul_mont
,.-bn_mul_mont
276 my @A=("%r10","%r11");
277 my @N=("%r13","%rdi");
279 .type bn_mul4x_mont
,\
@function,6
294 lea
(%rsp,%r10,8),%rsp # tp=alloca(8*(num+4))
295 and \
$-1024,%rsp # minimize TLB usage
297 mov
%r11,8(%rsp,$num,8) # tp[num+1]=%rsp
299 mov
$rp,16(%rsp,$num,8) # tp[num+2]=$rp
300 mov
%rdx,%r12 # reassign $bp
304 mov
($n0),$n0 # pull n0[0] value
305 mov
($bp),$m0 # m0=bp[0]
312 mulq
$m0 # ap[0]*bp[0]
316 imulq
$A[0],$m1 # "tp[0]"*n0
320 add
%rax,$A[0] # discarded
343 mulq
$m0 # ap[j]*bp[0]
345 mov
-16($np,$j,8),%rax
351 mov
-8($ap,$j,8),%rax
353 add
$A[0],$N[0] # np[j]*m1+ap[j]*bp[0]
355 mov
$N[0],-24(%rsp,$j,8) # tp[j-1]
358 mulq
$m0 # ap[j]*bp[0]
360 mov
-8($np,$j,8),%rax
368 add
$A[1],$N[1] # np[j]*m1+ap[j]*bp[0]
370 mov
$N[1],-16(%rsp,$j,8) # tp[j-1]
373 mulq
$m0 # ap[j]*bp[0]
383 add
$A[0],$N[0] # np[j]*m1+ap[j]*bp[0]
385 mov
$N[0],-8(%rsp,$j,8) # tp[j-1]
388 mulq
$m0 # ap[j]*bp[0]
397 mov
-16($ap,$j,8),%rax
399 add
$A[1],$N[1] # np[j]*m1+ap[j]*bp[0]
401 mov
$N[1],-32(%rsp,$j,8) # tp[j-1]
406 mulq
$m0 # ap[j]*bp[0]
408 mov
-16($np,$j,8),%rax
414 mov
-8($ap,$j,8),%rax
416 add
$A[0],$N[0] # np[j]*m1+ap[j]*bp[0]
418 mov
$N[0],-24(%rsp,$j,8) # tp[j-1]
421 mulq
$m0 # ap[j]*bp[0]
423 mov
-8($np,$j,8),%rax
429 mov
($ap),%rax # ap[0]
431 add
$A[1],$N[1] # np[j]*m1+ap[j]*bp[0]
433 mov
$N[1],-16(%rsp,$j,8) # tp[j-1]
439 mov
$N[0],-8(%rsp,$j,8)
440 mov
$N[1],(%rsp,$j,8) # store upmost overflow bit
445 mov
($bp,$i,8),$m0 # m0=bp[i]
449 mulq
$m0 # ap[0]*bp[i]
450 add
%rax,$A[0] # ap[0]*bp[i]+tp[0]
454 imulq
$A[0],$m1 # tp[0]*n0
458 add
%rax,$A[0] # "$N[0]", discarded
463 mulq
$m0 # ap[j]*bp[i]
467 add
8(%rsp),$A[1] # +tp[1]
475 add
$A[1],$N[1] # np[j]*m1+ap[j]*bp[i]+tp[j]
478 mov
$N[1],(%rsp) # tp[j-1]
483 mulq
$m0 # ap[j]*bp[i]
485 mov
-16($np,$j,8),%rax
487 add
-16(%rsp,$j,8),$A[0] # ap[j]*bp[i]+tp[j]
493 mov
-8($ap,$j,8),%rax
497 mov
$N[0],-24(%rsp,$j,8) # tp[j-1]
500 mulq
$m0 # ap[j]*bp[i]
502 mov
-8($np,$j,8),%rax
504 add
-8(%rsp,$j,8),$A[1]
514 mov
$N[1],-16(%rsp,$j,8) # tp[j-1]
517 mulq
$m0 # ap[j]*bp[i]
521 add
(%rsp,$j,8),$A[0] # ap[j]*bp[i]+tp[j]
531 mov
$N[0],-8(%rsp,$j,8) # tp[j-1]
534 mulq
$m0 # ap[j]*bp[i]
538 add
8(%rsp,$j,8),$A[1]
545 mov
-16($ap,$j,8),%rax
549 mov
$N[1],-32(%rsp,$j,8) # tp[j-1]
554 mulq
$m0 # ap[j]*bp[i]
556 mov
-16($np,$j,8),%rax
558 add
-16(%rsp,$j,8),$A[0] # ap[j]*bp[i]+tp[j]
564 mov
-8($ap,$j,8),%rax
568 mov
$N[0],-24(%rsp,$j,8) # tp[j-1]
571 mulq
$m0 # ap[j]*bp[i]
573 mov
-8($np,$j,8),%rax
575 add
-8(%rsp,$j,8),$A[1]
582 mov
($ap),%rax # ap[0]
586 mov
$N[1],-16(%rsp,$j,8) # tp[j-1]
592 add
(%rsp,$num,8),$N[0] # pull upmost overflow bit
594 mov
$N[0],-8(%rsp,$j,8)
595 mov
$N[1],(%rsp,$j,8) # store upmost overflow bit
601 my @ri=("%rax","%rdx",$m0,$m1);
603 mov
16(%rsp,$num,8),$rp # restore $rp
604 mov
0(%rsp),@ri[0] # tp[0]
606 mov
8(%rsp),@ri[1] # tp[1]
607 shr \
$2,$num # num/=4
608 lea
(%rsp),$ap # borrow ap for tp
609 xor $i,$i # i=0 and clear CF!
612 mov
16($ap),@ri[2] # tp[2]
613 mov
24($ap),@ri[3] # tp[3]
615 lea
-1($num),$j # j=num/4-1
619 mov
@ri[0],0($rp,$i,8) # rp[i]=tp[i]-np[i]
620 mov
@ri[1],8($rp,$i,8) # rp[i]=tp[i]-np[i]
621 sbb
16($np,$i,8),@ri[2]
622 mov
32($ap,$i,8),@ri[0] # tp[i+1]
623 mov
40($ap,$i,8),@ri[1]
624 sbb
24($np,$i,8),@ri[3]
625 mov
@ri[2],16($rp,$i,8) # rp[i]=tp[i]-np[i]
626 mov
@ri[3],24($rp,$i,8) # rp[i]=tp[i]-np[i]
627 sbb
32($np,$i,8),@ri[0]
628 mov
48($ap,$i,8),@ri[2]
629 mov
56($ap,$i,8),@ri[3]
630 sbb
40($np,$i,8),@ri[1]
632 dec
$j # doesnn't affect CF!
635 mov
@ri[0],0($rp,$i,8) # rp[i]=tp[i]-np[i]
636 mov
32($ap,$i,8),@ri[0] # load overflow bit
637 sbb
16($np,$i,8),@ri[2]
638 mov
@ri[1],8($rp,$i,8) # rp[i]=tp[i]-np[i]
639 sbb
24($np,$i,8),@ri[3]
640 mov
@ri[2],16($rp,$i,8) # rp[i]=tp[i]-np[i]
642 sbb \
$0,@ri[0] # handle upmost overflow bit
643 mov
@ri[3],24($rp,$i,8) # rp[i]=tp[i]-np[i]
650 or $np,$ap # ap=borrow?tp:rp
657 .Lcopy4x
: # copy or in-place refresh
658 movdqu
16($ap,$i),%xmm2
659 movdqu
32($ap,$i),%xmm1
660 movdqa
%xmm0,16(%rsp,$i)
661 movdqu
%xmm2,16($rp,$i)
662 movdqa
%xmm0,32(%rsp,$i)
663 movdqu
%xmm1,32($rp,$i)
669 movdqu
16($ap,$i),%xmm2
670 movdqa
%xmm0,16(%rsp,$i)
671 movdqu
%xmm2,16($rp,$i)
675 mov
8(%rsp,$num,8),%rsi # restore %rsp
686 .size bn_mul4x_mont
,.-bn_mul4x_mont
690 ######################################################################
691 # void bn_sqr4x_mont(
692 my $rptr="%rdi"; # const BN_ULONG *rptr,
693 my $aptr="%rsi"; # const BN_ULONG *aptr,
694 my $bptr="%rdx"; # not used
695 my $nptr="%rcx"; # const BN_ULONG *nptr,
696 my $n0 ="%r8"; # const BN_ULONG *n0);
697 my $num ="%r9"; # int num, has to be divisible by 4 and
700 my ($i,$j,$tptr)=("%rbp","%rcx",$rptr);
701 my @A0=("%r10","%r11");
702 my @A1=("%r12","%r13");
703 my ($a0,$a1,$ai)=("%r14","%r15","%rbx");
706 .type bn_sqr4x_mont
,\
@function,6
717 shl \
$3,${num
}d
# convert $num to bytes
719 mov
%rsp,%r11 # put aside %rsp
720 sub $num,%r10 # -$num
722 lea
-72(%rsp,%r10,2),%rsp # alloca(frame+2*$num)
723 and \
$-1024,%rsp # minimize TLB usage
724 ##############################################################
727 # +0 saved $num, used in reduction section
728 # +8 &t[2*$num], used in reduction section
735 mov
$rptr,32(%rsp) # save $rptr
738 mov
%r11, 56(%rsp) # save original %rsp
740 ##############################################################
743 # a) multiply-n-add everything but a[i]*a[i];
744 # b) shift result of a) by 1 to the left and accumulate
745 # a[i]*a[i] products;
747 lea
32(%r10),$i # $i=-($num-32)
748 lea
($aptr,$num),$aptr # end of a[] buffer, ($aptr,$i)=&ap[2]
750 mov
$num,$j # $j=$num
752 # comments apply to $num==8 case
753 mov
-32($aptr,$i),$a0 # a[0]
754 lea
64(%rsp,$num,2),$tptr # end of tp[] buffer, &tp[2*$num]
755 mov
-24($aptr,$i),%rax # a[1]
756 lea
-32($tptr,$i),$tptr # end of tp[] window, &tp[2*$num-"$i"]
757 mov
-16($aptr,$i),$ai # a[2]
761 mov
%rax,$A0[0] # a[1]*a[0]
764 mov
$A0[0],-24($tptr,$i) # t[1]
771 mov
$A0[1],-16($tptr,$i) # t[2]
773 lea
-16($i),$j # j=-16
776 mov
8($aptr,$j),$ai # a[3]
778 mov
%rax,$A1[0] # a[2]*a[1]+t[3]
787 add
%rax,$A0[0] # a[3]*a[0]+a[2]*a[1]+t[3]
790 mov
$A0[0],-8($tptr,$j) # t[3]
795 mov
($aptr,$j),$ai # a[4]
798 add
%rax,$A1[1] # a[3]*a[1]+t[4]
806 add
%rax,$A0[1] # a[4]*a[0]+a[3]*a[1]+t[4]
809 mov
$A0[1],($tptr,$j) # t[4]
812 mov
8($aptr,$j),$ai # a[5]
815 add
%rax,$A1[0] # a[4]*a[3]+t[5]
823 add
%rax,$A0[0] # a[5]*a[2]+a[4]*a[3]+t[5]
826 mov
$A0[0],8($tptr,$j) # t[5]
828 mov
16($aptr,$j),$ai # a[6]
831 add
%rax,$A1[1] # a[5]*a[3]+t[6]
839 add
%rax,$A0[1] # a[6]*a[2]+a[5]*a[3]+t[6]
842 mov
$A0[1],16($tptr,$j) # t[6]
845 mov
24($aptr,$j),$ai # a[7]
848 add
%rax,$A1[0] # a[6]*a[5]+t[7]
857 add
%rax,$A0[0] # a[7]*a[4]+a[6]*a[5]+t[6]
860 mov
$A0[0],-8($tptr,$j) # t[7]
872 mov
$A1[1],($tptr) # t[8]
874 mov
$A1[0],8($tptr) # t[9]
878 .Lsqr4x_outer
: # comments apply to $num==6 case
879 mov
-32($aptr,$i),$a0 # a[0]
880 lea
64(%rsp,$num,2),$tptr # end of tp[] buffer, &tp[2*$num]
881 mov
-24($aptr,$i),%rax # a[1]
882 lea
-32($tptr,$i),$tptr # end of tp[] window, &tp[2*$num-"$i"]
883 mov
-16($aptr,$i),$ai # a[2]
886 mov
-24($tptr,$i),$A0[0] # t[1]
889 add
%rax,$A0[0] # a[1]*a[0]+t[1]
892 mov
$A0[0],-24($tptr,$i) # t[1]
895 add
-16($tptr,$i),$A0[1] # a[2]*a[0]+t[2]
901 mov
$A0[1],-16($tptr,$i) # t[2]
903 lea
-16($i),$j # j=-16
907 mov
8($aptr,$j),$ai # a[3]
909 add
8($tptr,$j),$A1[0]
912 add
%rax,$A1[0] # a[2]*a[1]+t[3]
920 add
%rax,$A0[0] # a[3]*a[0]+a[2]*a[1]+t[3]
923 mov
$A0[0],8($tptr,$j) # t[3]
930 mov
($aptr,$j),$ai # a[4]
932 add
($tptr,$j),$A1[1]
935 add
%rax,$A1[1] # a[3]*a[1]+t[4]
943 add
%rax,$A0[1] # a[4]*a[0]+a[3]*a[1]+t[4]
946 mov
$A0[1],($tptr,$j) # t[4]
948 mov
8($aptr,$j),$ai # a[5]
950 add
8($tptr,$j),$A1[0]
953 add
%rax,$A1[0] # a[4]*a[3]+t[5]
962 add
%rax,$A0[0] # a[5]*a[2]+a[4]*a[3]+t[5]
965 mov
$A0[0],-8($tptr,$j) # t[5], "preloaded t[1]" below
977 mov
$A1[1],($tptr) # t[6], "preloaded t[2]" below
978 mov
$A1[0],8($tptr) # t[7], "preloaded t[3]" below
983 # comments apply to $num==4 case
984 mov
-32($aptr),$a0 # a[0]
985 lea
64(%rsp,$num,2),$tptr # end of tp[] buffer, &tp[2*$num]
986 mov
-24($aptr),%rax # a[1]
987 lea
-32($tptr,$i),$tptr # end of tp[] window, &tp[2*$num-"$i"]
988 mov
-16($aptr),$ai # a[2]
993 add
%rax,$A0[0] # a[1]*a[0]+t[1], preloaded t[1]
996 mov
$A0[0],-24($tptr) # t[1]
999 add
$A1[1],$A0[1] # a[2]*a[0]+t[2], preloaded t[2]
1005 mov
$A0[1],-16($tptr) # t[2]
1007 mov
-8($aptr),$ai # a[3]
1009 add
%rax,$A1[0] # a[2]*a[1]+t[3], preloaded t[3]
1018 add
%rax,$A0[0] # a[3]*a[0]+a[2]*a[1]+t[3]
1021 mov
$A0[0],-8($tptr) # t[3]
1028 mov
-16($aptr),%rax # a[2]
1031 mov
$A1[1],($tptr) # t[4]
1032 mov
$A1[0],8($tptr) # t[5]
1037 my ($shift,$carry)=($a0,$a1);
1038 my @S=(@A1,$ai,$n0);
1042 sub $num,$i # $i=16-$num
1045 add
$A1[0],%rax # t[5]
1047 mov
%rax,8($tptr) # t[5]
1048 mov
%rdx,16($tptr) # t[6]
1049 mov
$carry,24($tptr) # t[7]
1051 mov
-16($aptr,$i),%rax # a[0]
1052 lea
64(%rsp,$num,2),$tptr
1053 xor $A0[0],$A0[0] # t[0]
1054 mov
-24($tptr,$i,2),$A0[1] # t[1]
1056 lea
($shift,$A0[0],2),$S[0] # t[2*i]<<1 | shift
1058 lea
($j,$A0[1],2),$S[1] # t[2*i+1]<<1 |
1060 or $A0[0],$S[1] # | t[2*i]>>63
1061 mov
-16($tptr,$i,2),$A0[0] # t[2*i+2] # prefetch
1062 mov
$A0[1],$shift # shift=t[2*i+1]>>63
1063 mul
%rax # a[i]*a[i]
1064 neg
$carry # mov $carry,cf
1065 mov
-8($tptr,$i,2),$A0[1] # t[2*i+2+1] # prefetch
1067 mov
-8($aptr,$i),%rax # a[i+1] # prefetch
1068 mov
$S[0],-32($tptr,$i,2)
1071 lea
($shift,$A0[0],2),$S[2] # t[2*i]<<1 | shift
1072 mov
$S[1],-24($tptr,$i,2)
1073 sbb
$carry,$carry # mov cf,$carry
1075 lea
($j,$A0[1],2),$S[3] # t[2*i+1]<<1 |
1077 or $A0[0],$S[3] # | t[2*i]>>63
1078 mov
0($tptr,$i,2),$A0[0] # t[2*i+2] # prefetch
1079 mov
$A0[1],$shift # shift=t[2*i+1]>>63
1080 mul
%rax # a[i]*a[i]
1081 neg
$carry # mov $carry,cf
1082 mov
8($tptr,$i,2),$A0[1] # t[2*i+2+1] # prefetch
1084 mov
0($aptr,$i),%rax # a[i+1] # prefetch
1085 mov
$S[2],-16($tptr,$i,2)
1088 mov
$S[3],-40($tptr,$i,2)
1089 sbb
$carry,$carry # mov cf,$carry
1090 jmp
.Lsqr4x_shift_n_add
1093 .Lsqr4x_shift_n_add
:
1094 lea
($shift,$A0[0],2),$S[0] # t[2*i]<<1 | shift
1096 lea
($j,$A0[1],2),$S[1] # t[2*i+1]<<1 |
1098 or $A0[0],$S[1] # | t[2*i]>>63
1099 mov
-16($tptr,$i,2),$A0[0] # t[2*i+2] # prefetch
1100 mov
$A0[1],$shift # shift=t[2*i+1]>>63
1101 mul
%rax # a[i]*a[i]
1102 neg
$carry # mov $carry,cf
1103 mov
-8($tptr,$i,2),$A0[1] # t[2*i+2+1] # prefetch
1105 mov
-8($aptr,$i),%rax # a[i+1] # prefetch
1106 mov
$S[0],-32($tptr,$i,2)
1109 lea
($shift,$A0[0],2),$S[2] # t[2*i]<<1 | shift
1110 mov
$S[1],-24($tptr,$i,2)
1111 sbb
$carry,$carry # mov cf,$carry
1113 lea
($j,$A0[1],2),$S[3] # t[2*i+1]<<1 |
1115 or $A0[0],$S[3] # | t[2*i]>>63
1116 mov
0($tptr,$i,2),$A0[0] # t[2*i+2] # prefetch
1117 mov
$A0[1],$shift # shift=t[2*i+1]>>63
1118 mul
%rax # a[i]*a[i]
1119 neg
$carry # mov $carry,cf
1120 mov
8($tptr,$i,2),$A0[1] # t[2*i+2+1] # prefetch
1122 mov
0($aptr,$i),%rax # a[i+1] # prefetch
1123 mov
$S[2],-16($tptr,$i,2)
1126 lea
($shift,$A0[0],2),$S[0] # t[2*i]<<1 | shift
1127 mov
$S[3],-8($tptr,$i,2)
1128 sbb
$carry,$carry # mov cf,$carry
1130 lea
($j,$A0[1],2),$S[1] # t[2*i+1]<<1 |
1132 or $A0[0],$S[1] # | t[2*i]>>63
1133 mov
16($tptr,$i,2),$A0[0] # t[2*i+2] # prefetch
1134 mov
$A0[1],$shift # shift=t[2*i+1]>>63
1135 mul
%rax # a[i]*a[i]
1136 neg
$carry # mov $carry,cf
1137 mov
24($tptr,$i,2),$A0[1] # t[2*i+2+1] # prefetch
1139 mov
8($aptr,$i),%rax # a[i+1] # prefetch
1140 mov
$S[0],0($tptr,$i,2)
1143 lea
($shift,$A0[0],2),$S[2] # t[2*i]<<1 | shift
1144 mov
$S[1],8($tptr,$i,2)
1145 sbb
$carry,$carry # mov cf,$carry
1147 lea
($j,$A0[1],2),$S[3] # t[2*i+1]<<1 |
1149 or $A0[0],$S[3] # | t[2*i]>>63
1150 mov
32($tptr,$i,2),$A0[0] # t[2*i+2] # prefetch
1151 mov
$A0[1],$shift # shift=t[2*i+1]>>63
1152 mul
%rax # a[i]*a[i]
1153 neg
$carry # mov $carry,cf
1154 mov
40($tptr,$i,2),$A0[1] # t[2*i+2+1] # prefetch
1156 mov
16($aptr,$i),%rax # a[i+1] # prefetch
1157 mov
$S[2],16($tptr,$i,2)
1159 mov
$S[3],24($tptr,$i,2)
1160 sbb
$carry,$carry # mov cf,$carry
1162 jnz
.Lsqr4x_shift_n_add
1164 lea
($shift,$A0[0],2),$S[0] # t[2*i]<<1 | shift
1166 lea
($j,$A0[1],2),$S[1] # t[2*i+1]<<1 |
1168 or $A0[0],$S[1] # | t[2*i]>>63
1169 mov
-16($tptr),$A0[0] # t[2*i+2] # prefetch
1170 mov
$A0[1],$shift # shift=t[2*i+1]>>63
1171 mul
%rax # a[i]*a[i]
1172 neg
$carry # mov $carry,cf
1173 mov
-8($tptr),$A0[1] # t[2*i+2+1] # prefetch
1175 mov
-8($aptr),%rax # a[i+1] # prefetch
1176 mov
$S[0],-32($tptr)
1179 lea
($shift,$A0[0],2),$S[2] # t[2*i]<<1|shift
1180 mov
$S[1],-24($tptr)
1181 sbb
$carry,$carry # mov cf,$carry
1183 lea
($j,$A0[1],2),$S[3] # t[2*i+1]<<1 |
1185 or $A0[0],$S[3] # | t[2*i]>>63
1186 mul
%rax # a[i]*a[i]
1187 neg
$carry # mov $carry,cf
1190 mov
$S[2],-16($tptr)
1194 ##############################################################
1195 # Montgomery reduction part, "word-by-word" algorithm.
1198 my ($topbit,$nptr)=("%rbp",$aptr);
1199 my ($m0,$m1)=($a0,$a1);
1200 my @Ni=("%rbx","%r9");
1202 mov
40(%rsp),$nptr # restore $nptr
1203 mov
48(%rsp),$n0 # restore *n0
1205 mov
$num,0(%rsp) # save $num
1206 sub $num,$j # $j=-$num
1207 mov
64(%rsp),$A0[0] # t[0] # modsched #
1208 mov
$n0,$m0 # # modsched #
1209 lea
64(%rsp,$num,2),%rax # end of t[] buffer
1210 lea
64(%rsp,$num),$tptr # end of t[] window
1211 mov
%rax,8(%rsp) # save end of t[] buffer
1212 lea
($nptr,$num),$nptr # end of n[] buffer
1213 xor $topbit,$topbit # $topbit=0
1215 mov
0($nptr,$j),%rax # n[0] # modsched #
1216 mov
8($nptr,$j),$Ni[1] # n[1] # modsched #
1217 imulq
$A0[0],$m0 # m0=t[0]*n0 # modsched #
1218 mov
%rax,$Ni[0] # # modsched #
1219 jmp
.Lsqr4x_mont_outer
1225 add
%rax,$A0[0] # n[0]*m0+t[0]
1231 add
8($tptr,$j),$A0[1]
1234 add
%rax,$A0[1] # n[1]*m0+t[1]
1240 mov
16($nptr,$j),$Ni[0] # n[2]
1245 add
%rax,$A1[0] # n[0]*m1+"t[1]"
1248 mov
$A1[0],8($tptr,$j) # "t[1]"
1251 add
16($tptr,$j),$A0[0]
1254 add
%rax,$A0[0] # n[2]*m0+t[2]
1258 mov
24($nptr,$j),$Ni[1] # n[3]
1263 add
%rax,$A1[1] # n[1]*m1+"t[2]"
1266 mov
$A1[1],16($tptr,$j) # "t[2]"
1269 add
24($tptr,$j),$A0[1]
1273 add
%rax,$A0[1] # n[3]*m0+t[3]
1276 jmp
.Lsqr4x_mont_inner
1280 mov
($nptr,$j),$Ni[0] # n[4]
1285 add
%rax,$A1[0] # n[2]*m1+"t[3]"
1288 mov
$A1[0],-8($tptr,$j) # "t[3]"
1291 add
($tptr,$j),$A0[0]
1294 add
%rax,$A0[0] # n[4]*m0+t[4]
1298 mov
8($nptr,$j),$Ni[1] # n[5]
1303 add
%rax,$A1[1] # n[3]*m1+"t[4]"
1306 mov
$A1[1],($tptr,$j) # "t[4]"
1309 add
8($tptr,$j),$A0[1]
1312 add
%rax,$A0[1] # n[5]*m0+t[5]
1317 mov
16($nptr,$j),$Ni[0] # n[6]
1322 add
%rax,$A1[0] # n[4]*m1+"t[5]"
1325 mov
$A1[0],8($tptr,$j) # "t[5]"
1328 add
16($tptr,$j),$A0[0]
1331 add
%rax,$A0[0] # n[6]*m0+t[6]
1335 mov
24($nptr,$j),$Ni[1] # n[7]
1340 add
%rax,$A1[1] # n[5]*m1+"t[6]"
1343 mov
$A1[1],16($tptr,$j) # "t[6]"
1346 add
24($tptr,$j),$A0[1]
1350 add
%rax,$A0[1] # n[7]*m0+t[7]
1354 jne
.Lsqr4x_mont_inner
1356 sub 0(%rsp),$j # $j=-$num # modsched #
1357 mov
$n0,$m0 # # modsched #
1363 add
%rax,$A1[0] # n[6]*m1+"t[7]"
1366 mov
$A1[0],-8($tptr) # "t[7]"
1369 add
($tptr),$A0[0] # +t[8]
1371 mov
0($nptr,$j),$Ni[0] # n[0] # modsched #
1375 imulq
16($tptr,$j),$m0 # m0=t[0]*n0 # modsched #
1377 mov
8($nptr,$j),$Ni[1] # n[1] # modsched #
1379 mov
16($tptr,$j),$A0[0] # t[0] # modsched #
1382 add
%rax,$A1[1] # n[7]*m1+"t[8]"
1383 mov
$Ni[0],%rax # # modsched #
1385 mov
$A1[1],($tptr) # "t[8]"
1388 add
8($tptr),$A1[0] # +t[9]
1391 lea
16($tptr),$tptr # "t[$num]>>128"
1393 mov
$A1[0],-8($tptr) # "t[9]"
1394 cmp 8(%rsp),$tptr # are we done?
1395 jb
.Lsqr4x_mont_outer
1397 mov
0(%rsp),$num # restore $num
1398 mov
$topbit,($tptr) # save $topbit
1401 ##############################################################
1402 # Post-condition, 4x unrolled copy from bn_mul_mont
1405 my ($tptr,$nptr)=("%rbx",$aptr);
1406 my @ri=("%rax","%rdx","%r10","%r11");
1408 mov
64(%rsp,$num),@ri[0] # tp[0]
1409 lea
64(%rsp,$num),$tptr # upper half of t[2*$num] holds result
1410 mov
40(%rsp),$nptr # restore $nptr
1411 shr \
$5,$num # num/4
1412 mov
8($tptr),@ri[1] # t[1]
1413 xor $i,$i # i=0 and clear CF!
1415 mov
32(%rsp),$rptr # restore $rptr
1417 mov
16($tptr),@ri[2] # t[2]
1418 mov
24($tptr),@ri[3] # t[3]
1420 lea
-1($num),$j # j=num/4-1
1424 mov
@ri[0],0($rptr,$i,8) # rp[i]=tp[i]-np[i]
1425 mov
@ri[1],8($rptr,$i,8) # rp[i]=tp[i]-np[i]
1426 sbb
16($nptr,$i,8),@ri[2]
1427 mov
32($tptr,$i,8),@ri[0] # tp[i+1]
1428 mov
40($tptr,$i,8),@ri[1]
1429 sbb
24($nptr,$i,8),@ri[3]
1430 mov
@ri[2],16($rptr,$i,8) # rp[i]=tp[i]-np[i]
1431 mov
@ri[3],24($rptr,$i,8) # rp[i]=tp[i]-np[i]
1432 sbb
32($nptr,$i,8),@ri[0]
1433 mov
48($tptr,$i,8),@ri[2]
1434 mov
56($tptr,$i,8),@ri[3]
1435 sbb
40($nptr,$i,8),@ri[1]
1437 dec
$j # doesn't affect CF!
1440 mov
@ri[0],0($rptr,$i,8) # rp[i]=tp[i]-np[i]
1441 mov
32($tptr,$i,8),@ri[0] # load overflow bit
1442 sbb
16($nptr,$i,8),@ri[2]
1443 mov
@ri[1],8($rptr,$i,8) # rp[i]=tp[i]-np[i]
1444 sbb
24($nptr,$i,8),@ri[3]
1445 mov
@ri[2],16($rptr,$i,8) # rp[i]=tp[i]-np[i]
1447 sbb \
$0,@ri[0] # handle upmost overflow bit
1448 mov
@ri[3],24($rptr,$i,8) # rp[i]=tp[i]-np[i]
1455 or $nptr,$tptr # tp=borrow?tp:rp
1458 lea
64(%rsp,$num,8),$nptr
1459 movdqu
($tptr),%xmm1
1460 lea
($nptr,$num,8),$nptr
1461 movdqa
%xmm0,64(%rsp) # zap lower half of temporary vector
1462 movdqa
%xmm0,($nptr) # zap upper half of temporary vector
1463 movdqu
%xmm1,($rptr)
1466 .Lsqr4x_copy
: # copy or in-place refresh
1467 movdqu
16($tptr,$i),%xmm2
1468 movdqu
32($tptr,$i),%xmm1
1469 movdqa
%xmm0,80(%rsp,$i) # zap lower half of temporary vector
1470 movdqa
%xmm0,96(%rsp,$i) # zap lower half of temporary vector
1471 movdqa
%xmm0,16($nptr,$i) # zap upper half of temporary vector
1472 movdqa
%xmm0,32($nptr,$i) # zap upper half of temporary vector
1473 movdqu
%xmm2,16($rptr,$i)
1474 movdqu
%xmm1,32($rptr,$i)
1479 movdqu
16($tptr,$i),%xmm2
1480 movdqa
%xmm0,80(%rsp,$i) # zap lower half of temporary vector
1481 movdqa
%xmm0,16($nptr,$i) # zap upper half of temporary vector
1482 movdqu
%xmm2,16($rptr,$i)
1486 mov
56(%rsp),%rsi # restore %rsp
1497 .size bn_sqr4x_mont
,.-bn_sqr4x_mont
1501 .asciz
"Montgomery Multiplication for x86_64, CRYPTOGAMS by <appro\@openssl.org>"
1505 # EXCEPTION_DISPOSITION handler (EXCEPTION_RECORD *rec,ULONG64 frame,
1506 # CONTEXT *context,DISPATCHER_CONTEXT *disp)
1514 .extern __imp_RtlVirtualUnwind
1515 .type mul_handler
,\
@abi-omnipotent
1529 mov
120($context),%rax # pull context->Rax
1530 mov
248($context),%rbx # pull context->Rip
1532 mov
8($disp),%rsi # disp->ImageBase
1533 mov
56($disp),%r11 # disp->HandlerData
1535 mov
0(%r11),%r10d # HandlerData[0]
1536 lea
(%rsi,%r10),%r10 # end of prologue label
1537 cmp %r10,%rbx # context->Rip<end of prologue label
1538 jb
.Lcommon_seh_tail
1540 mov
152($context),%rax # pull context->Rsp
1542 mov
4(%r11),%r10d # HandlerData[1]
1543 lea
(%rsi,%r10),%r10 # epilogue label
1544 cmp %r10,%rbx # context->Rip>=epilogue label
1545 jae
.Lcommon_seh_tail
1547 mov
192($context),%r10 # pull $num
1548 mov
8(%rax,%r10,8),%rax # pull saved stack pointer
1557 mov
%rbx,144($context) # restore context->Rbx
1558 mov
%rbp,160($context) # restore context->Rbp
1559 mov
%r12,216($context) # restore context->R12
1560 mov
%r13,224($context) # restore context->R13
1561 mov
%r14,232($context) # restore context->R14
1562 mov
%r15,240($context) # restore context->R15
1564 jmp
.Lcommon_seh_tail
1565 .size mul_handler
,.-mul_handler
1567 .type sqr_handler
,\
@abi-omnipotent
1581 mov
120($context),%rax # pull context->Rax
1582 mov
248($context),%rbx # pull context->Rip
1584 lea
.Lsqr4x_body
(%rip),%r10
1585 cmp %r10,%rbx # context->Rip<.Lsqr_body
1586 jb
.Lcommon_seh_tail
1588 mov
152($context),%rax # pull context->Rsp
1590 lea
.Lsqr4x_epilogue
(%rip),%r10
1591 cmp %r10,%rbx # context->Rip>=.Lsqr_epilogue
1592 jae
.Lcommon_seh_tail
1594 mov
56(%rax),%rax # pull saved stack pointer
1603 mov
%rbx,144($context) # restore context->Rbx
1604 mov
%rbp,160($context) # restore context->Rbp
1605 mov
%r12,216($context) # restore context->R12
1606 mov
%r13,224($context) # restore context->R13
1607 mov
%r14,232($context) # restore context->R14
1608 mov
%r15,240($context) # restore context->R15
1613 mov
%rax,152($context) # restore context->Rsp
1614 mov
%rsi,168($context) # restore context->Rsi
1615 mov
%rdi,176($context) # restore context->Rdi
1617 mov
40($disp),%rdi # disp->ContextRecord
1618 mov
$context,%rsi # context
1619 mov \
$154,%ecx # sizeof(CONTEXT)
1620 .long
0xa548f3fc # cld; rep movsq
1623 xor %rcx,%rcx # arg1, UNW_FLAG_NHANDLER
1624 mov
8(%rsi),%rdx # arg2, disp->ImageBase
1625 mov
0(%rsi),%r8 # arg3, disp->ControlPc
1626 mov
16(%rsi),%r9 # arg4, disp->FunctionEntry
1627 mov
40(%rsi),%r10 # disp->ContextRecord
1628 lea
56(%rsi),%r11 # &disp->HandlerData
1629 lea
24(%rsi),%r12 # &disp->EstablisherFrame
1630 mov
%r10,32(%rsp) # arg5
1631 mov
%r11,40(%rsp) # arg6
1632 mov
%r12,48(%rsp) # arg7
1633 mov
%rcx,56(%rsp) # arg8, (NULL)
1634 call
*__imp_RtlVirtualUnwind
(%rip)
1636 mov \
$1,%eax # ExceptionContinueSearch
1648 .size sqr_handler
,.-sqr_handler
1652 .rva
.LSEH_begin_bn_mul_mont
1653 .rva
.LSEH_end_bn_mul_mont
1654 .rva
.LSEH_info_bn_mul_mont
1656 .rva
.LSEH_begin_bn_mul4x_mont
1657 .rva
.LSEH_end_bn_mul4x_mont
1658 .rva
.LSEH_info_bn_mul4x_mont
1660 .rva
.LSEH_begin_bn_sqr4x_mont
1661 .rva
.LSEH_end_bn_sqr4x_mont
1662 .rva
.LSEH_info_bn_sqr4x_mont
1666 .LSEH_info_bn_mul_mont
:
1669 .rva
.Lmul_body
,.Lmul_epilogue
# HandlerData[]
1670 .LSEH_info_bn_mul4x_mont
:
1673 .rva
.Lmul4x_body
,.Lmul4x_epilogue
# HandlerData[]
1674 .LSEH_info_bn_sqr4x_mont
: