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 # Some OSes, *cough*-dows, insist on stack being "wired" to
95 # physical memory in strictly sequential manner, i.e. if stack
96 # allocation spans two pages, then reference to farmost one can
97 # be punishable by SEGV. But page walking can do good even on
98 # other OSes, because it guarantees that villain thread hits
99 # the guard page before it can make damage to innocent one...
105 .byte 0x66,0x2e # predict non-taken
108 mov $bp,%r12 # reassign $bp
112 mov ($n0),$n0 # pull n0[0] value
113 mov ($bp),$m0 # m0=bp[0]
120 mulq $m0 # ap[0]*bp[0]
124 imulq $lo0,$m1 # "tp[0]"*n0
128 add %rax,$lo0 # discarded
141 add $hi0,$hi1 # np[j]*m1+ap[j]*bp[0]
144 mov $hi1,-16(%rsp,$j,8) # tp[j-1]
148 mulq $m0 # ap[j]*bp[0]
160 mov ($ap),%rax # ap[0]
162 add $hi0,$hi1 # np[j]*m1+ap[j]*bp[0]
164 mov $hi1,-16(%rsp,$j,8) # tp[j-1]
171 mov $hi1,-8(%rsp,$num,8)
172 mov %rdx,(%rsp,$num,8) # store upmost overflow bit
178 mov ($bp,$i,8),$m0 # m0=bp[i]
182 mulq $m0 # ap[0]*bp[i]
183 add %rax,$lo0 # ap[0]*bp[i]+tp[0]
187 imulq $lo0,$m1 # tp[0]*n0
191 add %rax,$lo0 # discarded
194 mov 8(%rsp),$lo0 # tp[1]
205 add $lo0,$hi1 # np[j]*m1+ap[j]*bp[i]+tp[j]
208 mov $hi1,-16(%rsp,$j,8) # tp[j-1]
212 mulq $m0 # ap[j]*bp[i]
216 add $hi0,$lo0 # ap[j]*bp[i]+tp[j]
226 mov ($ap),%rax # ap[0]
228 add $lo0,$hi1 # np[j]*m1+ap[j]*bp[i]+tp[j]
231 mov $hi1,-16(%rsp,$j,8) # tp[j-1]
237 add $lo0,$hi1 # pull upmost overflow bit
239 mov $hi1,-8(%rsp,$num,8)
240 mov %rdx,(%rsp,$num,8) # store upmost overflow bit
246 xor $i,$i # i=0 and clear CF!
247 mov (%rsp),%rax # tp[0]
248 lea (%rsp),$ap # borrow ap for tp
252 .Lsub: sbb ($np,$i,8),%rax
253 mov %rax,($rp,$i,8) # rp[i]=tp[i]-np[i]
254 mov 8($ap,$i,8),%rax # tp[i+1]
256 dec $j # doesnn't affect CF!
259 sbb \$0,%rax # handle upmost overflow bit
266 or $np,$ap # ap=borrow?tp:rp
268 .Lcopy: # copy or in-place refresh
270 mov $i,(%rsp,$i,8) # zap temporary vector
271 mov %rax,($rp,$i,8) # rp[i]=tp[i]
276 mov 8(%rsp,$num,8),%rsi # restore %rsp
287 .size bn_mul_mont,.-bn_mul_mont
290 my @A=("%r10","%r11");
291 my @N=("%r13","%rdi");
293 .type bn_mul4x_mont,\@function,6
308 lea (%rsp,%r10,8),%rsp # tp=alloca(8*(num+4))
309 and \$-1024,%rsp # minimize TLB usage
311 mov %r11,8(%rsp,$num,8) # tp[num+1]=%rsp
318 .byte 0x2e # predict non-taken
319 jnc .Lmul4x_page_walk
321 mov $rp,16(%rsp,$num,8) # tp[num+2]=$rp
322 mov %rdx,%r12 # reassign $bp
326 mov ($n0),$n0 # pull n0[0] value
327 mov ($bp),$m0 # m0=bp[0]
334 mulq $m0 # ap[0]*bp[0]
338 imulq $A[0],$m1 # "tp[0]"*n0
342 add %rax,$A[0] # discarded
365 mulq $m0 # ap[j]*bp[0]
367 mov -16($np,$j,8),%rax
373 mov -8($ap,$j,8),%rax
375 add $A[0],$N[0] # np[j]*m1+ap[j]*bp[0]
377 mov $N[0],-24(%rsp,$j,8) # tp[j-1]
380 mulq $m0 # ap[j]*bp[0]
382 mov -8($np,$j,8),%rax
390 add $A[1],$N[1] # np[j]*m1+ap[j]*bp[0]
392 mov $N[1],-16(%rsp,$j,8) # tp[j-1]
395 mulq $m0 # ap[j]*bp[0]
405 add $A[0],$N[0] # np[j]*m1+ap[j]*bp[0]
407 mov $N[0],-8(%rsp,$j,8) # tp[j-1]
410 mulq $m0 # ap[j]*bp[0]
419 mov -16($ap,$j,8),%rax
421 add $A[1],$N[1] # np[j]*m1+ap[j]*bp[0]
423 mov $N[1],-32(%rsp,$j,8) # tp[j-1]
428 mulq $m0 # ap[j]*bp[0]
430 mov -16($np,$j,8),%rax
436 mov -8($ap,$j,8),%rax
438 add $A[0],$N[0] # np[j]*m1+ap[j]*bp[0]
440 mov $N[0],-24(%rsp,$j,8) # tp[j-1]
443 mulq $m0 # ap[j]*bp[0]
445 mov -8($np,$j,8),%rax
451 mov ($ap),%rax # ap[0]
453 add $A[1],$N[1] # np[j]*m1+ap[j]*bp[0]
455 mov $N[1],-16(%rsp,$j,8) # tp[j-1]
461 mov $N[0],-8(%rsp,$j,8)
462 mov $N[1],(%rsp,$j,8) # store upmost overflow bit
467 mov ($bp,$i,8),$m0 # m0=bp[i]
471 mulq $m0 # ap[0]*bp[i]
472 add %rax,$A[0] # ap[0]*bp[i]+tp[0]
476 imulq $A[0],$m1 # tp[0]*n0
480 add %rax,$A[0] # "$N[0]", discarded
485 mulq $m0 # ap[j]*bp[i]
489 add 8(%rsp),$A[1] # +tp[1]
497 add $A[1],$N[1] # np[j]*m1+ap[j]*bp[i]+tp[j]
500 mov $N[1],(%rsp) # tp[j-1]
505 mulq $m0 # ap[j]*bp[i]
507 mov -16($np,$j,8),%rax
509 add -16(%rsp,$j,8),$A[0] # ap[j]*bp[i]+tp[j]
515 mov -8($ap,$j,8),%rax
519 mov $N[0],-24(%rsp,$j,8) # tp[j-1]
522 mulq $m0 # ap[j]*bp[i]
524 mov -8($np,$j,8),%rax
526 add -8(%rsp,$j,8),$A[1]
536 mov $N[1],-16(%rsp,$j,8) # tp[j-1]
539 mulq $m0 # ap[j]*bp[i]
543 add (%rsp,$j,8),$A[0] # ap[j]*bp[i]+tp[j]
553 mov $N[0],-8(%rsp,$j,8) # tp[j-1]
556 mulq $m0 # ap[j]*bp[i]
560 add 8(%rsp,$j,8),$A[1]
567 mov -16($ap,$j,8),%rax
571 mov $N[1],-32(%rsp,$j,8) # tp[j-1]
576 mulq $m0 # ap[j]*bp[i]
578 mov -16($np,$j,8),%rax
580 add -16(%rsp,$j,8),$A[0] # ap[j]*bp[i]+tp[j]
586 mov -8($ap,$j,8),%rax
590 mov $N[0],-24(%rsp,$j,8) # tp[j-1]
593 mulq $m0 # ap[j]*bp[i]
595 mov -8($np,$j,8),%rax
597 add -8(%rsp,$j,8),$A[1]
604 mov ($ap),%rax # ap[0]
608 mov $N[1],-16(%rsp,$j,8) # tp[j-1]
614 add (%rsp,$num,8),$N[0] # pull upmost overflow bit
616 mov $N[0],-8(%rsp,$j,8)
617 mov $N[1],(%rsp,$j,8) # store upmost overflow bit
623 my @ri=("%rax","%rdx",$m0,$m1);
625 mov 16(%rsp,$num,8),$rp # restore $rp
626 mov 0(%rsp),@ri[0] # tp[0]
628 mov 8(%rsp),@ri[1] # tp[1]
629 shr \$2,$num # num/=4
630 lea (%rsp),$ap # borrow ap for tp
631 xor $i,$i # i=0 and clear CF!
634 mov 16($ap),@ri[2] # tp[2]
635 mov 24($ap),@ri[3] # tp[3]
637 lea -1($num),$j # j=num/4-1
641 mov @ri[0],0($rp,$i,8) # rp[i]=tp[i]-np[i]
642 mov @ri[1],8($rp,$i,8) # rp[i]=tp[i]-np[i]
643 sbb 16($np,$i,8),@ri[2]
644 mov 32($ap,$i,8),@ri[0] # tp[i+1]
645 mov 40($ap,$i,8),@ri[1]
646 sbb 24($np,$i,8),@ri[3]
647 mov @ri[2],16($rp,$i,8) # rp[i]=tp[i]-np[i]
648 mov @ri[3],24($rp,$i,8) # rp[i]=tp[i]-np[i]
649 sbb 32($np,$i,8),@ri[0]
650 mov 48($ap,$i,8),@ri[2]
651 mov 56($ap,$i,8),@ri[3]
652 sbb 40($np,$i,8),@ri[1]
654 dec $j # doesnn't affect CF!
657 mov @ri[0],0($rp,$i,8) # rp[i]=tp[i]-np[i]
658 mov 32($ap,$i,8),@ri[0] # load overflow bit
659 sbb 16($np,$i,8),@ri[2]
660 mov @ri[1],8($rp,$i,8) # rp[i]=tp[i]-np[i]
661 sbb 24($np,$i,8),@ri[3]
662 mov @ri[2],16($rp,$i,8) # rp[i]=tp[i]-np[i]
664 sbb \$0,@ri[0] # handle upmost overflow bit
665 mov @ri[3],24($rp,$i,8) # rp[i]=tp[i]-np[i]
672 or $np,$ap # ap=borrow?tp:rp
679 .Lcopy4x: # copy or in-place refresh
680 movdqu 16($ap,$i),%xmm2
681 movdqu 32($ap,$i),%xmm1
682 movdqa %xmm0,16(%rsp,$i)
683 movdqu %xmm2,16($rp,$i)
684 movdqa %xmm0,32(%rsp,$i)
685 movdqu %xmm1,32($rp,$i)
691 movdqu 16($ap,$i),%xmm2
692 movdqa %xmm0,16(%rsp,$i)
693 movdqu %xmm2,16($rp,$i)
697 mov 8(%rsp,$num,8),%rsi # restore %rsp
708 .size bn_mul4x_mont,.-bn_mul4x_mont
712 ######################################################################
713 # void bn_sqr4x_mont(
714 my $rptr="%rdi"; # const BN_ULONG *rptr,
715 my $aptr="%rsi"; # const BN_ULONG *aptr,
716 my $bptr="%rdx"; # not used
717 my $nptr="%rcx"; # const BN_ULONG *nptr,
718 my $n0 ="%r8"; # const BN_ULONG *n0);
719 my $num ="%r9"; # int num, has to be divisible by 4 and
722 my ($i,$j,$tptr)=("%rbp","%rcx",$rptr);
723 my @A0=("%r10","%r11");
724 my @A1=("%r12","%r13");
725 my ($a0,$a1,$ai)=("%r14","%r15","%rbx");
728 .type bn_sqr4x_mont,\@function,6
740 shl \$3,${num}d # convert $num to bytes
741 mov %rsp,%r11 # put aside %rsp
744 lea -72(%rsp,$num,2),%rsp # alloca(frame+2*$num)
745 and \$-1024,%rsp # minimize TLB usage
752 .byte 0x2e # predict non-taken
753 jnc .Lsqr4x_page_walk
756 neg $num # restore $num
757 lea -48(%rax),%r11 # restore saved %rsp
758 ##############################################################
761 # +0 saved $num, used in reduction section
762 # +8 &t[2*$num], used in reduction section
769 mov $rptr,32(%rsp) # save $rptr
772 mov %r11, 56(%rsp) # save original %rsp
774 ##############################################################
777 # a) multiply-n-add everything but a[i]*a[i];
778 # b) shift result of a) by 1 to the left and accumulate
779 # a[i]*a[i] products;
781 lea 32(%r10),$i # $i=-($num-32)
782 lea ($aptr,$num),$aptr # end of a[] buffer, ($aptr,$i)=&ap[2]
784 mov $num,$j # $j=$num
786 # comments apply to $num==8 case
787 mov -32($aptr,$i),$a0 # a[0]
788 lea 64(%rsp,$num,2),$tptr # end of tp[] buffer, &tp[2*$num]
789 mov -24($aptr,$i),%rax # a[1]
790 lea -32($tptr,$i),$tptr # end of tp[] window, &tp[2*$num-"$i"]
791 mov -16($aptr,$i),$ai # a[2]
795 mov %rax,$A0[0] # a[1]*a[0]
798 mov $A0[0],-24($tptr,$i) # t[1]
805 mov $A0[1],-16($tptr,$i) # t[2]
807 lea -16($i),$j # j=-16
810 mov 8($aptr,$j),$ai # a[3]
812 mov %rax,$A1[0] # a[2]*a[1]+t[3]
821 add %rax,$A0[0] # a[3]*a[0]+a[2]*a[1]+t[3]
824 mov $A0[0],-8($tptr,$j) # t[3]
829 mov ($aptr,$j),$ai # a[4]
832 add %rax,$A1[1] # a[3]*a[1]+t[4]
840 add %rax,$A0[1] # a[4]*a[0]+a[3]*a[1]+t[4]
843 mov $A0[1],($tptr,$j) # t[4]
846 mov 8($aptr,$j),$ai # a[5]
849 add %rax,$A1[0] # a[4]*a[3]+t[5]
857 add %rax,$A0[0] # a[5]*a[2]+a[4]*a[3]+t[5]
860 mov $A0[0],8($tptr,$j) # t[5]
862 mov 16($aptr,$j),$ai # a[6]
865 add %rax,$A1[1] # a[5]*a[3]+t[6]
873 add %rax,$A0[1] # a[6]*a[2]+a[5]*a[3]+t[6]
876 mov $A0[1],16($tptr,$j) # t[6]
879 mov 24($aptr,$j),$ai # a[7]
882 add %rax,$A1[0] # a[6]*a[5]+t[7]
891 add %rax,$A0[0] # a[7]*a[4]+a[6]*a[5]+t[6]
894 mov $A0[0],-8($tptr,$j) # t[7]
906 mov $A1[1],($tptr) # t[8]
908 mov $A1[0],8($tptr) # t[9]
912 .Lsqr4x_outer: # comments apply to $num==6 case
913 mov -32($aptr,$i),$a0 # a[0]
914 lea 64(%rsp,$num,2),$tptr # end of tp[] buffer, &tp[2*$num]
915 mov -24($aptr,$i),%rax # a[1]
916 lea -32($tptr,$i),$tptr # end of tp[] window, &tp[2*$num-"$i"]
917 mov -16($aptr,$i),$ai # a[2]
920 mov -24($tptr,$i),$A0[0] # t[1]
923 add %rax,$A0[0] # a[1]*a[0]+t[1]
926 mov $A0[0],-24($tptr,$i) # t[1]
929 add -16($tptr,$i),$A0[1] # a[2]*a[0]+t[2]
935 mov $A0[1],-16($tptr,$i) # t[2]
937 lea -16($i),$j # j=-16
941 mov 8($aptr,$j),$ai # a[3]
943 add 8($tptr,$j),$A1[0]
946 add %rax,$A1[0] # a[2]*a[1]+t[3]
954 add %rax,$A0[0] # a[3]*a[0]+a[2]*a[1]+t[3]
957 mov $A0[0],8($tptr,$j) # t[3]
964 mov ($aptr,$j),$ai # a[4]
966 add ($tptr,$j),$A1[1]
969 add %rax,$A1[1] # a[3]*a[1]+t[4]
977 add %rax,$A0[1] # a[4]*a[0]+a[3]*a[1]+t[4]
980 mov $A0[1],($tptr,$j) # t[4]
982 mov 8($aptr,$j),$ai # a[5]
984 add 8($tptr,$j),$A1[0]
987 add %rax,$A1[0] # a[4]*a[3]+t[5]
996 add %rax,$A0[0] # a[5]*a[2]+a[4]*a[3]+t[5]
999 mov $A0[0],-8($tptr,$j) # t[5], "preloaded t[1]" below
1011 mov $A1[1],($tptr) # t[6], "preloaded t[2]" below
1012 mov $A1[0],8($tptr) # t[7], "preloaded t[3]" below
1017 # comments apply to $num==4 case
1018 mov -32($aptr),$a0 # a[0]
1019 lea 64(%rsp,$num,2),$tptr # end of tp[] buffer, &tp[2*$num]
1020 mov -24($aptr),%rax # a[1]
1021 lea -32($tptr,$i),$tptr # end of tp[] window, &tp[2*$num-"$i"]
1022 mov -16($aptr),$ai # a[2]
1027 add %rax,$A0[0] # a[1]*a[0]+t[1], preloaded t[1]
1030 mov $A0[0],-24($tptr) # t[1]
1033 add $A1[1],$A0[1] # a[2]*a[0]+t[2], preloaded t[2]
1039 mov $A0[1],-16($tptr) # t[2]
1041 mov -8($aptr),$ai # a[3]
1043 add %rax,$A1[0] # a[2]*a[1]+t[3], preloaded t[3]
1052 add %rax,$A0[0] # a[3]*a[0]+a[2]*a[1]+t[3]
1055 mov $A0[0],-8($tptr) # t[3]
1062 mov -16($aptr),%rax # a[2]
1065 mov $A1[1],($tptr) # t[4]
1066 mov $A1[0],8($tptr) # t[5]
1071 my ($shift,$carry)=($a0,$a1);
1072 my @S=(@A1,$ai,$n0);
1076 sub $num,$i # $i=16-$num
1079 add $A1[0],%rax # t[5]
1081 mov %rax,8($tptr) # t[5]
1082 mov %rdx,16($tptr) # t[6]
1083 mov $carry,24($tptr) # t[7]
1085 mov -16($aptr,$i),%rax # a[0]
1086 lea 64(%rsp,$num,2),$tptr
1087 xor $A0[0],$A0[0] # t[0]
1088 mov -24($tptr,$i,2),$A0[1] # t[1]
1090 lea ($shift,$A0[0],2),$S[0] # t[2*i]<<1 | shift
1092 lea ($j,$A0[1],2),$S[1] # t[2*i+1]<<1 |
1094 or $A0[0],$S[1] # | t[2*i]>>63
1095 mov -16($tptr,$i,2),$A0[0] # t[2*i+2] # prefetch
1096 mov $A0[1],$shift # shift=t[2*i+1]>>63
1097 mul %rax # a[i]*a[i]
1098 neg $carry # mov $carry,cf
1099 mov -8($tptr,$i,2),$A0[1] # t[2*i+2+1] # prefetch
1101 mov -8($aptr,$i),%rax # a[i+1] # prefetch
1102 mov $S[0],-32($tptr,$i,2)
1105 lea ($shift,$A0[0],2),$S[2] # t[2*i]<<1 | shift
1106 mov $S[1],-24($tptr,$i,2)
1107 sbb $carry,$carry # mov cf,$carry
1109 lea ($j,$A0[1],2),$S[3] # t[2*i+1]<<1 |
1111 or $A0[0],$S[3] # | t[2*i]>>63
1112 mov 0($tptr,$i,2),$A0[0] # t[2*i+2] # prefetch
1113 mov $A0[1],$shift # shift=t[2*i+1]>>63
1114 mul %rax # a[i]*a[i]
1115 neg $carry # mov $carry,cf
1116 mov 8($tptr,$i,2),$A0[1] # t[2*i+2+1] # prefetch
1118 mov 0($aptr,$i),%rax # a[i+1] # prefetch
1119 mov $S[2],-16($tptr,$i,2)
1122 mov $S[3],-40($tptr,$i,2)
1123 sbb $carry,$carry # mov cf,$carry
1124 jmp .Lsqr4x_shift_n_add
1127 .Lsqr4x_shift_n_add:
1128 lea ($shift,$A0[0],2),$S[0] # t[2*i]<<1 | shift
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 -8($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],-32($tptr,$i,2)
1143 lea ($shift,$A0[0],2),$S[2] # t[2*i]<<1 | shift
1144 mov $S[1],-24($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 0($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 8($tptr,$i,2),$A0[1] # t[2*i+2+1] # prefetch
1156 mov 0($aptr,$i),%rax # a[i+1] # prefetch
1157 mov $S[2],-16($tptr,$i,2)
1160 lea ($shift,$A0[0],2),$S[0] # t[2*i]<<1 | shift
1161 mov $S[3],-8($tptr,$i,2)
1162 sbb $carry,$carry # mov cf,$carry
1164 lea ($j,$A0[1],2),$S[1] # t[2*i+1]<<1 |
1166 or $A0[0],$S[1] # | t[2*i]>>63
1167 mov 16($tptr,$i,2),$A0[0] # t[2*i+2] # prefetch
1168 mov $A0[1],$shift # shift=t[2*i+1]>>63
1169 mul %rax # a[i]*a[i]
1170 neg $carry # mov $carry,cf
1171 mov 24($tptr,$i,2),$A0[1] # t[2*i+2+1] # prefetch
1173 mov 8($aptr,$i),%rax # a[i+1] # prefetch
1174 mov $S[0],0($tptr,$i,2)
1177 lea ($shift,$A0[0],2),$S[2] # t[2*i]<<1 | shift
1178 mov $S[1],8($tptr,$i,2)
1179 sbb $carry,$carry # mov cf,$carry
1181 lea ($j,$A0[1],2),$S[3] # t[2*i+1]<<1 |
1183 or $A0[0],$S[3] # | t[2*i]>>63
1184 mov 32($tptr,$i,2),$A0[0] # t[2*i+2] # prefetch
1185 mov $A0[1],$shift # shift=t[2*i+1]>>63
1186 mul %rax # a[i]*a[i]
1187 neg $carry # mov $carry,cf
1188 mov 40($tptr,$i,2),$A0[1] # t[2*i+2+1] # prefetch
1190 mov 16($aptr,$i),%rax # a[i+1] # prefetch
1191 mov $S[2],16($tptr,$i,2)
1193 mov $S[3],24($tptr,$i,2)
1194 sbb $carry,$carry # mov cf,$carry
1196 jnz .Lsqr4x_shift_n_add
1198 lea ($shift,$A0[0],2),$S[0] # t[2*i]<<1 | shift
1200 lea ($j,$A0[1],2),$S[1] # t[2*i+1]<<1 |
1202 or $A0[0],$S[1] # | t[2*i]>>63
1203 mov -16($tptr),$A0[0] # t[2*i+2] # prefetch
1204 mov $A0[1],$shift # shift=t[2*i+1]>>63
1205 mul %rax # a[i]*a[i]
1206 neg $carry # mov $carry,cf
1207 mov -8($tptr),$A0[1] # t[2*i+2+1] # prefetch
1209 mov -8($aptr),%rax # a[i+1] # prefetch
1210 mov $S[0],-32($tptr)
1213 lea ($shift,$A0[0],2),$S[2] # t[2*i]<<1|shift
1214 mov $S[1],-24($tptr)
1215 sbb $carry,$carry # mov cf,$carry
1217 lea ($j,$A0[1],2),$S[3] # t[2*i+1]<<1 |
1219 or $A0[0],$S[3] # | t[2*i]>>63
1220 mul %rax # a[i]*a[i]
1221 neg $carry # mov $carry,cf
1224 mov $S[2],-16($tptr)
1228 ##############################################################
1229 # Montgomery reduction part, "word-by-word" algorithm.
1232 my ($topbit,$nptr)=("%rbp",$aptr);
1233 my ($m0,$m1)=($a0,$a1);
1234 my @Ni=("%rbx","%r9");
1236 mov 40(%rsp),$nptr # restore $nptr
1237 mov 48(%rsp),$n0 # restore *n0
1239 mov $num,0(%rsp) # save $num
1240 sub $num,$j # $j=-$num
1241 mov 64(%rsp),$A0[0] # t[0] # modsched #
1242 mov $n0,$m0 # # modsched #
1243 lea 64(%rsp,$num,2),%rax # end of t[] buffer
1244 lea 64(%rsp,$num),$tptr # end of t[] window
1245 mov %rax,8(%rsp) # save end of t[] buffer
1246 lea ($nptr,$num),$nptr # end of n[] buffer
1247 xor $topbit,$topbit # $topbit=0
1249 mov 0($nptr,$j),%rax # n[0] # modsched #
1250 mov 8($nptr,$j),$Ni[1] # n[1] # modsched #
1251 imulq $A0[0],$m0 # m0=t[0]*n0 # modsched #
1252 mov %rax,$Ni[0] # # modsched #
1253 jmp .Lsqr4x_mont_outer
1259 add %rax,$A0[0] # n[0]*m0+t[0]
1265 add 8($tptr,$j),$A0[1]
1268 add %rax,$A0[1] # n[1]*m0+t[1]
1274 mov 16($nptr,$j),$Ni[0] # n[2]
1279 add %rax,$A1[0] # n[0]*m1+"t[1]"
1282 mov $A1[0],8($tptr,$j) # "t[1]"
1285 add 16($tptr,$j),$A0[0]
1288 add %rax,$A0[0] # n[2]*m0+t[2]
1292 mov 24($nptr,$j),$Ni[1] # n[3]
1297 add %rax,$A1[1] # n[1]*m1+"t[2]"
1300 mov $A1[1],16($tptr,$j) # "t[2]"
1303 add 24($tptr,$j),$A0[1]
1307 add %rax,$A0[1] # n[3]*m0+t[3]
1310 jmp .Lsqr4x_mont_inner
1314 mov ($nptr,$j),$Ni[0] # n[4]
1319 add %rax,$A1[0] # n[2]*m1+"t[3]"
1322 mov $A1[0],-8($tptr,$j) # "t[3]"
1325 add ($tptr,$j),$A0[0]
1328 add %rax,$A0[0] # n[4]*m0+t[4]
1332 mov 8($nptr,$j),$Ni[1] # n[5]
1337 add %rax,$A1[1] # n[3]*m1+"t[4]"
1340 mov $A1[1],($tptr,$j) # "t[4]"
1343 add 8($tptr,$j),$A0[1]
1346 add %rax,$A0[1] # n[5]*m0+t[5]
1351 mov 16($nptr,$j),$Ni[0] # n[6]
1356 add %rax,$A1[0] # n[4]*m1+"t[5]"
1359 mov $A1[0],8($tptr,$j) # "t[5]"
1362 add 16($tptr,$j),$A0[0]
1365 add %rax,$A0[0] # n[6]*m0+t[6]
1369 mov 24($nptr,$j),$Ni[1] # n[7]
1374 add %rax,$A1[1] # n[5]*m1+"t[6]"
1377 mov $A1[1],16($tptr,$j) # "t[6]"
1380 add 24($tptr,$j),$A0[1]
1384 add %rax,$A0[1] # n[7]*m0+t[7]
1388 jne .Lsqr4x_mont_inner
1390 sub 0(%rsp),$j # $j=-$num # modsched #
1391 mov $n0,$m0 # # modsched #
1397 add %rax,$A1[0] # n[6]*m1+"t[7]"
1400 mov $A1[0],-8($tptr) # "t[7]"
1403 add ($tptr),$A0[0] # +t[8]
1405 mov 0($nptr,$j),$Ni[0] # n[0] # modsched #
1409 imulq 16($tptr,$j),$m0 # m0=t[0]*n0 # modsched #
1411 mov 8($nptr,$j),$Ni[1] # n[1] # modsched #
1413 mov 16($tptr,$j),$A0[0] # t[0] # modsched #
1416 add %rax,$A1[1] # n[7]*m1+"t[8]"
1417 mov $Ni[0],%rax # # modsched #
1419 mov $A1[1],($tptr) # "t[8]"
1422 add 8($tptr),$A1[0] # +t[9]
1425 lea 16($tptr),$tptr # "t[$num]>>128"
1427 mov $A1[0],-8($tptr) # "t[9]"
1428 cmp 8(%rsp),$tptr # are we done?
1429 jb .Lsqr4x_mont_outer
1431 mov 0(%rsp),$num # restore $num
1432 mov $topbit,($tptr) # save $topbit
1435 ##############################################################
1436 # Post-condition, 4x unrolled copy from bn_mul_mont
1439 my ($tptr,$nptr)=("%rbx",$aptr);
1440 my @ri=("%rax","%rdx","%r10","%r11");
1442 mov 64(%rsp,$num),@ri[0] # tp[0]
1443 lea 64(%rsp,$num),$tptr # upper half of t[2*$num] holds result
1444 mov 40(%rsp),$nptr # restore $nptr
1445 shr \$5,$num # num/4
1446 mov 8($tptr),@ri[1] # t[1]
1447 xor $i,$i # i=0 and clear CF!
1449 mov 32(%rsp),$rptr # restore $rptr
1451 mov 16($tptr),@ri[2] # t[2]
1452 mov 24($tptr),@ri[3] # t[3]
1454 lea -1($num),$j # j=num/4-1
1458 mov @ri[0],0($rptr,$i,8) # rp[i]=tp[i]-np[i]
1459 mov @ri[1],8($rptr,$i,8) # rp[i]=tp[i]-np[i]
1460 sbb 16($nptr,$i,8),@ri[2]
1461 mov 32($tptr,$i,8),@ri[0] # tp[i+1]
1462 mov 40($tptr,$i,8),@ri[1]
1463 sbb 24($nptr,$i,8),@ri[3]
1464 mov @ri[2],16($rptr,$i,8) # rp[i]=tp[i]-np[i]
1465 mov @ri[3],24($rptr,$i,8) # rp[i]=tp[i]-np[i]
1466 sbb 32($nptr,$i,8),@ri[0]
1467 mov 48($tptr,$i,8),@ri[2]
1468 mov 56($tptr,$i,8),@ri[3]
1469 sbb 40($nptr,$i,8),@ri[1]
1471 dec $j # doesn't affect CF!
1474 mov @ri[0],0($rptr,$i,8) # rp[i]=tp[i]-np[i]
1475 mov 32($tptr,$i,8),@ri[0] # load overflow bit
1476 sbb 16($nptr,$i,8),@ri[2]
1477 mov @ri[1],8($rptr,$i,8) # rp[i]=tp[i]-np[i]
1478 sbb 24($nptr,$i,8),@ri[3]
1479 mov @ri[2],16($rptr,$i,8) # rp[i]=tp[i]-np[i]
1481 sbb \$0,@ri[0] # handle upmost overflow bit
1482 mov @ri[3],24($rptr,$i,8) # rp[i]=tp[i]-np[i]
1489 or $nptr,$tptr # tp=borrow?tp:rp
1492 lea 64(%rsp,$num,8),$nptr
1493 movdqu ($tptr),%xmm1
1494 lea ($nptr,$num,8),$nptr
1495 movdqa %xmm0,64(%rsp) # zap lower half of temporary vector
1496 movdqa %xmm0,($nptr) # zap upper half of temporary vector
1497 movdqu %xmm1,($rptr)
1500 .Lsqr4x_copy: # copy or in-place refresh
1501 movdqu 16($tptr,$i),%xmm2
1502 movdqu 32($tptr,$i),%xmm1
1503 movdqa %xmm0,80(%rsp,$i) # zap lower half of temporary vector
1504 movdqa %xmm0,96(%rsp,$i) # zap lower half of temporary vector
1505 movdqa %xmm0,16($nptr,$i) # zap upper half of temporary vector
1506 movdqa %xmm0,32($nptr,$i) # zap upper half of temporary vector
1507 movdqu %xmm2,16($rptr,$i)
1508 movdqu %xmm1,32($rptr,$i)
1513 movdqu 16($tptr,$i),%xmm2
1514 movdqa %xmm0,80(%rsp,$i) # zap lower half of temporary vector
1515 movdqa %xmm0,16($nptr,$i) # zap upper half of temporary vector
1516 movdqu %xmm2,16($rptr,$i)
1520 mov 56(%rsp),%rsi # restore %rsp
1531 .size bn_sqr4x_mont,.-bn_sqr4x_mont
1535 .asciz "Montgomery Multiplication for x86_64, CRYPTOGAMS by <appro\@openssl.org>"
1539 # EXCEPTION_DISPOSITION handler (EXCEPTION_RECORD *rec,ULONG64 frame,
1540 # CONTEXT *context,DISPATCHER_CONTEXT *disp)
1548 .extern __imp_RtlVirtualUnwind
1549 .type mul_handler,\@abi-omnipotent
1563 mov 120($context),%rax # pull context->Rax
1564 mov 248($context),%rbx # pull context->Rip
1566 mov 8($disp),%rsi # disp->ImageBase
1567 mov 56($disp),%r11 # disp->HandlerData
1569 mov 0(%r11),%r10d # HandlerData[0]
1570 lea (%rsi,%r10),%r10 # end of prologue label
1571 cmp %r10,%rbx # context->Rip<end of prologue label
1572 jb .Lcommon_seh_tail
1574 mov 152($context),%rax # pull context->Rsp
1576 mov 4(%r11),%r10d # HandlerData[1]
1577 lea (%rsi,%r10),%r10 # epilogue label
1578 cmp %r10,%rbx # context->Rip>=epilogue label
1579 jae .Lcommon_seh_tail
1581 mov 192($context),%r10 # pull $num
1582 mov 8(%rax,%r10,8),%rax # pull saved stack pointer
1591 mov %rbx,144($context) # restore context->Rbx
1592 mov %rbp,160($context) # restore context->Rbp
1593 mov %r12,216($context) # restore context->R12
1594 mov %r13,224($context) # restore context->R13
1595 mov %r14,232($context) # restore context->R14
1596 mov %r15,240($context) # restore context->R15
1598 jmp .Lcommon_seh_tail
1599 .size mul_handler,.-mul_handler
1601 .type sqr_handler,\@abi-omnipotent
1615 mov 120($context),%rax # pull context->Rax
1616 mov 248($context),%rbx # pull context->Rip
1618 lea .Lsqr4x_body(%rip),%r10
1619 cmp %r10,%rbx # context->Rip<.Lsqr_body
1620 jb .Lcommon_seh_tail
1622 mov 152($context),%rax # pull context->Rsp
1624 lea .Lsqr4x_epilogue(%rip),%r10
1625 cmp %r10,%rbx # context->Rip>=.Lsqr_epilogue
1626 jae .Lcommon_seh_tail
1628 mov 56(%rax),%rax # pull saved stack pointer
1637 mov %rbx,144($context) # restore context->Rbx
1638 mov %rbp,160($context) # restore context->Rbp
1639 mov %r12,216($context) # restore context->R12
1640 mov %r13,224($context) # restore context->R13
1641 mov %r14,232($context) # restore context->R14
1642 mov %r15,240($context) # restore context->R15
1647 mov %rax,152($context) # restore context->Rsp
1648 mov %rsi,168($context) # restore context->Rsi
1649 mov %rdi,176($context) # restore context->Rdi
1651 mov 40($disp),%rdi # disp->ContextRecord
1652 mov $context,%rsi # context
1653 mov \$154,%ecx # sizeof(CONTEXT)
1654 .long 0xa548f3fc # cld; rep movsq
1657 xor %rcx,%rcx # arg1, UNW_FLAG_NHANDLER
1658 mov 8(%rsi),%rdx # arg2, disp->ImageBase
1659 mov 0(%rsi),%r8 # arg3, disp->ControlPc
1660 mov 16(%rsi),%r9 # arg4, disp->FunctionEntry
1661 mov 40(%rsi),%r10 # disp->ContextRecord
1662 lea 56(%rsi),%r11 # &disp->HandlerData
1663 lea 24(%rsi),%r12 # &disp->EstablisherFrame
1664 mov %r10,32(%rsp) # arg5
1665 mov %r11,40(%rsp) # arg6
1666 mov %r12,48(%rsp) # arg7
1667 mov %rcx,56(%rsp) # arg8, (NULL)
1668 call *__imp_RtlVirtualUnwind(%rip)
1670 mov \$1,%eax # ExceptionContinueSearch
1682 .size sqr_handler,.-sqr_handler
1686 .rva .LSEH_begin_bn_mul_mont
1687 .rva .LSEH_end_bn_mul_mont
1688 .rva .LSEH_info_bn_mul_mont
1690 .rva .LSEH_begin_bn_mul4x_mont
1691 .rva .LSEH_end_bn_mul4x_mont
1692 .rva .LSEH_info_bn_mul4x_mont
1694 .rva .LSEH_begin_bn_sqr4x_mont
1695 .rva .LSEH_end_bn_sqr4x_mont
1696 .rva .LSEH_info_bn_sqr4x_mont
1700 .LSEH_info_bn_mul_mont:
1703 .rva .Lmul_body,.Lmul_epilogue # HandlerData[]
1704 .LSEH_info_bn_mul4x_mont:
1707 .rva .Lmul4x_body,.Lmul4x_epilogue # HandlerData[]
1708 .LSEH_info_bn_sqr4x_mont: