2 # Copyright 2011-2016 The OpenSSL Project Authors. All Rights Reserved.
4 # Licensed under the OpenSSL license (the "License"). You may not use
5 # this file except in compliance with the License. You can obtain a copy
6 # in the file LICENSE in the source distribution or at
7 # https://www.openssl.org/source/license.html
10 # ====================================================================
11 # Written by Andy Polyakov <appro@openssl.org> for the OpenSSL
12 # project. The module is, however, dual licensed under OpenSSL and
13 # CRYPTOGAMS licenses depending on where you obtain it. For further
14 # details see http://www.openssl.org/~appro/cryptogams/.
15 # ====================================================================
19 # Companion to x86_64-mont.pl that optimizes cache-timing attack
20 # countermeasures. The subroutines are produced by replacing bp[i]
21 # references in their x86_64-mont.pl counterparts with cache-neutral
22 # references to powers table computed in BN_mod_exp_mont_consttime.
23 # In addition subroutine that scatters elements of the powers table
24 # is implemented, so that scatter-/gathering can be tuned without
25 # bn_exp.c modifications.
29 # Add MULX/AD*X code paths and additional interfaces to optimize for
30 # branch prediction unit. For input lengths that are multiples of 8
31 # the np argument is not just modulus value, but one interleaved
32 # with 0. This is to optimize post-condition...
36 if ($flavour =~ /\./) { $output = $flavour; undef $flavour; }
38 $win64=0; $win64=1 if ($flavour =~ /[nm]asm|mingw64/ || $output =~ /\.asm$/);
40 $0 =~ m/(.*[\/\\])[^\/\\]+$/; $dir=$1;
41 ( $xlate="${dir}x86_64-xlate.pl" and -f $xlate ) or
42 ( $xlate="${dir}../../perlasm/x86_64-xlate.pl" and -f $xlate) or
43 die "can't locate x86_64-xlate.pl";
45 open OUT,"| \"$^X\" \"$xlate\" $flavour \"$output\"";
48 if (`$ENV{CC} -Wa,-v -c -o /dev/null -x assembler /dev/null 2>&1`
49 =~ /GNU assembler version ([2-9]\.[0-9]+)/) {
53 if (!$addx && $win64 && ($flavour =~ /nasm/ || $ENV{ASM} =~ /nasm/) &&
54 `nasm -v 2>&1` =~ /NASM version ([2-9]\.[0-9]+)/) {
58 if (!$addx && $win64 && ($flavour =~ /masm/ || $ENV{ASM} =~ /ml64/) &&
59 `ml64 2>&1` =~ /Version ([0-9]+)\./) {
63 if (!$addx && `$ENV{CC} -v 2>&1` =~ /((?:^clang|LLVM) version|.*based on LLVM) ([3-9])\.([0-9]+)/) {
64 my $ver = $2 + $3/100.0; # 3.1->3.01, 3.10->3.10
68 # int bn_mul_mont_gather5(
69 $rp="%rdi"; # BN_ULONG *rp,
70 $ap="%rsi"; # const BN_ULONG *ap,
71 $bp="%rdx"; # const BN_ULONG *bp,
72 $np="%rcx"; # const BN_ULONG *np,
73 $n0="%r8"; # const BN_ULONG *n0,
74 $num="%r9"; # int num,
75 # int idx); # 0 to 2^5-1, "index" in $bp holding
76 # pre-computed powers of a', interlaced
77 # in such manner that b[0] is $bp[idx],
78 # b[1] is [2^5+idx], etc.
90 .extern OPENSSL_ia32cap_P
92 .globl bn_mul_mont_gather5
93 .type bn_mul_mont_gather5,\@function,6
101 $code.=<<___ if ($addx);
102 mov OPENSSL_ia32cap_P+8(%rip),%r11d
109 movd `($win64?56:8)`(%rsp),%xmm5 # load 7th argument
119 lea -280(%rsp,$num,8),%r10 # future alloca(8*(num+2)+256+8)
120 neg $num # restore $num
121 and \$-1024,%r10 # minimize TLB usage
123 # An OS-agnostic version of __chkstk.
125 # Some OSes (Windows) insist on stack being "wired" to
126 # physical memory in strictly sequential manner, i.e. if stack
127 # allocation spans two pages, then reference to farmost one can
128 # be punishable by SEGV. But page walking can do good even on
129 # other OSes, because it guarantees that villain thread hits
130 # the guard page before it can make damage to innocent one...
137 jmp .Lmul_page_walk_done
144 .Lmul_page_walk_done:
147 mov %rax,8(%rsp,$num,8) # tp[num+1]=%rsp
150 lea 128($bp),%r12 # reassign $bp (+size optimization)
153 $STRIDE=2**5*8; # 5 is "window size"
154 $N=$STRIDE/4; # should match cache line size
156 movdqa 0(%r10),%xmm0 # 00000001000000010000000000000000
157 movdqa 16(%r10),%xmm1 # 00000002000000020000000200000002
158 lea 24-112(%rsp,$num,8),%r10# place the mask after tp[num+3] (+ICache optimization)
161 pshufd \$0,%xmm5,%xmm5 # broadcast index
165 ########################################################################
166 # calculate mask by comparing 0..31 to index and save result to stack
170 pcmpeqd %xmm5,%xmm0 # compare to 1,0
174 for($k=0;$k<$STRIDE/16-4;$k+=4) {
177 pcmpeqd %xmm5,%xmm1 # compare to 3,2
178 movdqa %xmm0,`16*($k+0)+112`(%r10)
182 pcmpeqd %xmm5,%xmm2 # compare to 5,4
183 movdqa %xmm1,`16*($k+1)+112`(%r10)
187 pcmpeqd %xmm5,%xmm3 # compare to 7,6
188 movdqa %xmm2,`16*($k+2)+112`(%r10)
193 movdqa %xmm3,`16*($k+3)+112`(%r10)
197 $code.=<<___; # last iteration can be optimized
200 movdqa %xmm0,`16*($k+0)+112`(%r10)
205 movdqa %xmm1,`16*($k+1)+112`(%r10)
208 movdqa %xmm2,`16*($k+2)+112`(%r10)
209 pand `16*($k+0)-128`($bp),%xmm0 # while it's still in register
211 pand `16*($k+1)-128`($bp),%xmm1
212 pand `16*($k+2)-128`($bp),%xmm2
213 movdqa %xmm3,`16*($k+3)+112`(%r10)
214 pand `16*($k+3)-128`($bp),%xmm3
218 for($k=0;$k<$STRIDE/16-4;$k+=4) {
220 movdqa `16*($k+0)-128`($bp),%xmm4
221 movdqa `16*($k+1)-128`($bp),%xmm5
222 movdqa `16*($k+2)-128`($bp),%xmm2
223 pand `16*($k+0)+112`(%r10),%xmm4
224 movdqa `16*($k+3)-128`($bp),%xmm3
225 pand `16*($k+1)+112`(%r10),%xmm5
227 pand `16*($k+2)+112`(%r10),%xmm2
229 pand `16*($k+3)+112`(%r10),%xmm3
236 pshufd \$0x4e,%xmm0,%xmm1
239 movq %xmm0,$m0 # m0=bp[0]
241 mov ($n0),$n0 # pull n0[0] value
248 mulq $m0 # ap[0]*bp[0]
252 imulq $lo0,$m1 # "tp[0]"*n0
256 add %rax,$lo0 # discarded
269 add $hi0,$hi1 # np[j]*m1+ap[j]*bp[0]
272 mov $hi1,-16(%rsp,$j,8) # tp[j-1]
276 mulq $m0 # ap[j]*bp[0]
285 jne .L1st # note that upon exit $j==$num, so
286 # they can be used interchangeably
290 add $hi0,$hi1 # np[j]*m1+ap[j]*bp[0]
292 mov $hi1,-16(%rsp,$num,8) # tp[num-1]
299 mov $hi1,-8(%rsp,$num,8)
300 mov %rdx,(%rsp,$num,8) # store upmost overflow bit
306 lea 24+128(%rsp,$num,8),%rdx # where 256-byte mask is (+size optimization)
311 for($k=0;$k<$STRIDE/16;$k+=4) {
313 movdqa `16*($k+0)-128`($bp),%xmm0
314 movdqa `16*($k+1)-128`($bp),%xmm1
315 movdqa `16*($k+2)-128`($bp),%xmm2
316 movdqa `16*($k+3)-128`($bp),%xmm3
317 pand `16*($k+0)-128`(%rdx),%xmm0
318 pand `16*($k+1)-128`(%rdx),%xmm1
320 pand `16*($k+2)-128`(%rdx),%xmm2
322 pand `16*($k+3)-128`(%rdx),%xmm3
329 pshufd \$0x4e,%xmm4,%xmm0
333 mov ($ap),%rax # ap[0]
334 movq %xmm0,$m0 # m0=bp[i]
340 mulq $m0 # ap[0]*bp[i]
341 add %rax,$lo0 # ap[0]*bp[i]+tp[0]
345 imulq $lo0,$m1 # tp[0]*n0
349 add %rax,$lo0 # discarded
352 mov 8(%rsp),$lo0 # tp[1]
363 add $lo0,$hi1 # np[j]*m1+ap[j]*bp[i]+tp[j]
366 mov $hi1,-16(%rsp,$j,8) # tp[j-1]
370 mulq $m0 # ap[j]*bp[i]
374 add $hi0,$lo0 # ap[j]*bp[i]+tp[j]
381 jne .Linner # note that upon exit $j==$num, so
382 # they can be used interchangeably
385 add $lo0,$hi1 # np[j]*m1+ap[j]*bp[i]+tp[j]
386 mov (%rsp,$num,8),$lo0
388 mov $hi1,-16(%rsp,$num,8) # tp[num-1]
394 add $lo0,$hi1 # pull upmost overflow bit
396 mov $hi1,-8(%rsp,$num,8)
397 mov %rdx,(%rsp,$num,8) # store upmost overflow bit
403 xor $i,$i # i=0 and clear CF!
404 mov (%rsp),%rax # tp[0]
405 lea (%rsp),$ap # borrow ap for tp
409 .Lsub: sbb ($np,$i,8),%rax
410 mov %rax,($rp,$i,8) # rp[i]=tp[i]-np[i]
411 mov 8($ap,$i,8),%rax # tp[i+1]
413 dec $j # doesnn't affect CF!
416 sbb \$0,%rax # handle upmost overflow bit
422 .Lcopy: # conditional copy
427 mov $i,(%rsp,$i,8) # zap temporary vector
429 mov %rdx,($rp,$i,8) # rp[i]=tp[i]
434 mov 8(%rsp,$num,8),%rsi # restore %rsp
446 .size bn_mul_mont_gather5,.-bn_mul_mont_gather5
449 my @A=("%r10","%r11");
450 my @N=("%r13","%rdi");
452 .type bn_mul4x_mont_gather5,\@function,6
454 bn_mul4x_mont_gather5:
459 $code.=<<___ if ($addx);
461 cmp \$0x80108,%r11d # check for AD*X+BMI2+BMI1
474 shl \$3,${num}d # convert $num to bytes
475 lea ($num,$num,2),%r10 # 3*$num in bytes
478 ##############################################################
479 # Ensure that stack frame doesn't alias with $rptr+3*$num
480 # modulo 4096, which covers ret[num], am[num] and n[num]
481 # (see bn_exp.c). This is done to allow memory disambiguation
482 # logic do its magic. [Extra [num] is allocated in order
483 # to align with bn_power5's frame, which is cleansed after
484 # completing exponentiation. Extra 256 bytes is for power mask
485 # calculated from 7th argument, the index.]
487 lea -320(%rsp,$num,2),%r11
493 sub %r11,%rbp # align with $rp
494 lea -320(%rbp,$num,2),%rbp # future alloca(frame+2*num*8+256)
499 lea 4096-320(,$num,2),%r10
500 lea -320(%rbp,$num,2),%rbp # future alloca(frame+2*num*8+256)
514 jmp .Lmul4x_page_walk_done
521 .Lmul4x_page_walk_done:
530 mov 40(%rsp),%rsi # restore %rsp
542 .size bn_mul4x_mont_gather5,.-bn_mul4x_mont_gather5
544 .type mul4x_internal,\@abi-omnipotent
547 shl \$5,$num # $num was in bytes
548 movd `($win64?56:8)`(%rax),%xmm5 # load 7th argument, index
550 lea 128(%rdx,$num),%r13 # end of powers table (+size optimization)
551 shr \$5,$num # restore $num
554 $STRIDE=2**5*8; # 5 is "window size"
555 $N=$STRIDE/4; # should match cache line size
558 movdqa 0(%rax),%xmm0 # 00000001000000010000000000000000
559 movdqa 16(%rax),%xmm1 # 00000002000000020000000200000002
560 lea 88-112(%rsp,$num),%r10 # place the mask after tp[num+1] (+ICache optimization)
561 lea 128(%rdx),$bp # size optimization
563 pshufd \$0,%xmm5,%xmm5 # broadcast index
568 ########################################################################
569 # calculate mask by comparing 0..31 to index and save result to stack
573 pcmpeqd %xmm5,%xmm0 # compare to 1,0
577 for($i=0;$i<$STRIDE/16-4;$i+=4) {
580 pcmpeqd %xmm5,%xmm1 # compare to 3,2
581 movdqa %xmm0,`16*($i+0)+112`(%r10)
585 pcmpeqd %xmm5,%xmm2 # compare to 5,4
586 movdqa %xmm1,`16*($i+1)+112`(%r10)
590 pcmpeqd %xmm5,%xmm3 # compare to 7,6
591 movdqa %xmm2,`16*($i+2)+112`(%r10)
596 movdqa %xmm3,`16*($i+3)+112`(%r10)
600 $code.=<<___; # last iteration can be optimized
603 movdqa %xmm0,`16*($i+0)+112`(%r10)
608 movdqa %xmm1,`16*($i+1)+112`(%r10)
611 movdqa %xmm2,`16*($i+2)+112`(%r10)
612 pand `16*($i+0)-128`($bp),%xmm0 # while it's still in register
614 pand `16*($i+1)-128`($bp),%xmm1
615 pand `16*($i+2)-128`($bp),%xmm2
616 movdqa %xmm3,`16*($i+3)+112`(%r10)
617 pand `16*($i+3)-128`($bp),%xmm3
621 for($i=0;$i<$STRIDE/16-4;$i+=4) {
623 movdqa `16*($i+0)-128`($bp),%xmm4
624 movdqa `16*($i+1)-128`($bp),%xmm5
625 movdqa `16*($i+2)-128`($bp),%xmm2
626 pand `16*($i+0)+112`(%r10),%xmm4
627 movdqa `16*($i+3)-128`($bp),%xmm3
628 pand `16*($i+1)+112`(%r10),%xmm5
630 pand `16*($i+2)+112`(%r10),%xmm2
632 pand `16*($i+3)+112`(%r10),%xmm3
639 pshufd \$0x4e,%xmm0,%xmm1
642 movq %xmm0,$m0 # m0=bp[0]
644 mov %r13,16+8(%rsp) # save end of b[num]
645 mov $rp, 56+8(%rsp) # save $rp
647 mov ($n0),$n0 # pull n0[0] value
649 lea ($ap,$num),$ap # end of a[num]
653 mulq $m0 # ap[0]*bp[0]
657 imulq $A[0],$m1 # "tp[0]"*n0
662 add %rax,$A[0] # discarded
675 mov 16($ap,$num),%rax
678 lea 4*8($num),$j # j=4
687 mulq $m0 # ap[j]*bp[0]
698 add $A[0],$N[0] # np[j]*m1+ap[j]*bp[0]
700 mov $N[0],-24($tp) # tp[j-1]
703 mulq $m0 # ap[j]*bp[0]
713 add $A[1],$N[1] # np[j]*m1+ap[j]*bp[0]
715 mov $N[1],-16($tp) # tp[j-1]
718 mulq $m0 # ap[j]*bp[0]
728 add $A[0],$N[0] # np[j]*m1+ap[j]*bp[0]
730 mov $N[0],-8($tp) # tp[j-1]
733 mulq $m0 # ap[j]*bp[0]
743 add $A[1],$N[1] # np[j]*m1+ap[j]*bp[0]
746 mov $N[1],($tp) # tp[j-1]
752 mulq $m0 # ap[j]*bp[0]
763 add $A[0],$N[0] # np[j]*m1+ap[j]*bp[0]
765 mov $N[0],-24($tp) # tp[j-1]
768 mulq $m0 # ap[j]*bp[0]
776 mov ($ap,$num),%rax # ap[0]
778 add $A[1],$N[1] # np[j]*m1+ap[j]*bp[0]
780 mov $N[1],-16($tp) # tp[j-1]
783 lea ($np,$num),$np # rewind $np
794 lea 16+128($tp),%rdx # where 256-byte mask is (+size optimization)
798 for($i=0;$i<$STRIDE/16;$i+=4) {
800 movdqa `16*($i+0)-128`($bp),%xmm0
801 movdqa `16*($i+1)-128`($bp),%xmm1
802 movdqa `16*($i+2)-128`($bp),%xmm2
803 movdqa `16*($i+3)-128`($bp),%xmm3
804 pand `16*($i+0)-128`(%rdx),%xmm0
805 pand `16*($i+1)-128`(%rdx),%xmm1
807 pand `16*($i+2)-128`(%rdx),%xmm2
809 pand `16*($i+3)-128`(%rdx),%xmm3
816 pshufd \$0x4e,%xmm4,%xmm0
819 movq %xmm0,$m0 # m0=bp[i]
823 mulq $m0 # ap[0]*bp[i]
824 add %rax,$A[0] # ap[0]*bp[i]+tp[0]
828 imulq $A[0],$m1 # tp[0]*n0
830 mov $N[1],($tp) # store upmost overflow bit
832 lea ($tp,$num),$tp # rewind $tp
835 add %rax,$A[0] # "$N[0]", discarded
840 mulq $m0 # ap[j]*bp[i]
844 add 8($tp),$A[1] # +tp[1]
850 mov 16($ap,$num),%rax
852 add $A[1],$N[1] # np[j]*m1+ap[j]*bp[i]+tp[j]
853 lea 4*8($num),$j # j=4
861 mulq $m0 # ap[j]*bp[i]
865 add 16($tp),$A[0] # ap[j]*bp[i]+tp[j]
876 mov $N[1],-32($tp) # tp[j-1]
879 mulq $m0 # ap[j]*bp[i]
893 mov $N[0],-24($tp) # tp[j-1]
896 mulq $m0 # ap[j]*bp[i]
900 add ($tp),$A[0] # ap[j]*bp[i]+tp[j]
910 mov $N[1],-16($tp) # tp[j-1]
913 mulq $m0 # ap[j]*bp[i]
928 mov $N[0],-8($tp) # tp[j-1]
934 mulq $m0 # ap[j]*bp[i]
938 add 16($tp),$A[0] # ap[j]*bp[i]+tp[j]
949 mov $N[1],-32($tp) # tp[j-1]
952 mulq $m0 # ap[j]*bp[i]
963 mov ($ap,$num),%rax # ap[0]
967 mov $N[0],-24($tp) # tp[j-1]
970 mov $N[1],-16($tp) # tp[j-1]
971 lea ($np,$num),$np # rewind $np
976 add ($tp),$N[0] # pull upmost overflow bit
977 adc \$0,$N[1] # upmost overflow bit
986 sub $N[0],$m1 # compare top-most words
987 adc $j,$j # $j is zero
989 sub $N[1],%rax # %rax=-$N[1]
990 lea ($tp,$num),%rbx # tptr in .sqr4x_sub
992 lea ($np),%rbp # nptr in .sqr4x_sub
995 mov 56+8(%rsp),%rdi # rptr in .sqr4x_sub
996 dec %r12 # so that after 'not' we get -n[0]
1001 jmp .Lsqr4x_sub_entry
1004 my @ri=("%rax",$bp,$m0,$m1);
1008 lea ($tp,$num),$tp # rewind $tp
1010 lea ($np,$N[1],8),$np
1011 mov 56+8(%rsp),$rp # restore $rp
1020 sbb 16*0($np),@ri[0]
1022 sbb 16*1($np),@ri[1]
1025 sbb 16*2($np),@ri[2]
1027 sbb 16*3($np),@ri[3]
1041 .size mul4x_internal,.-mul4x_internal
1045 ######################################################################
1047 my $rptr="%rdi"; # BN_ULONG *rptr,
1048 my $aptr="%rsi"; # const BN_ULONG *aptr,
1049 my $bptr="%rdx"; # const void *table,
1050 my $nptr="%rcx"; # const BN_ULONG *nptr,
1051 my $n0 ="%r8"; # const BN_ULONG *n0);
1052 my $num ="%r9"; # int num, has to be divisible by 8
1055 my ($i,$j,$tptr)=("%rbp","%rcx",$rptr);
1056 my @A0=("%r10","%r11");
1057 my @A1=("%r12","%r13");
1058 my ($a0,$a1,$ai)=("%r14","%r15","%rbx");
1062 .type bn_power5,\@function,6
1067 $code.=<<___ if ($addx);
1068 mov OPENSSL_ia32cap_P+8(%rip),%r11d
1070 cmp \$0x80108,%r11d # check for AD*X+BMI2+BMI1
1082 shl \$3,${num}d # convert $num to bytes
1083 lea ($num,$num,2),%r10d # 3*$num
1087 ##############################################################
1088 # Ensure that stack frame doesn't alias with $rptr+3*$num
1089 # modulo 4096, which covers ret[num], am[num] and n[num]
1090 # (see bn_exp.c). This is done to allow memory disambiguation
1091 # logic do its magic. [Extra 256 bytes is for power mask
1092 # calculated from 7th argument, the index.]
1094 lea -320(%rsp,$num,2),%r11
1100 sub %r11,%rbp # align with $aptr
1101 lea -320(%rbp,$num,2),%rbp # future alloca(frame+2*num*8+256)
1106 lea 4096-320(,$num,2),%r10
1107 lea -320(%rbp,$num,2),%rbp # future alloca(frame+2*num*8+256)
1117 lea (%rbp,%r11),%rsp
1121 jmp .Lpwr_page_walk_done
1124 lea -4096(%rsp),%rsp
1128 .Lpwr_page_walk_done:
1133 ##############################################################
1136 # +0 saved $num, used in reduction section
1137 # +8 &t[2*$num], used in reduction section
1143 mov %rax, 40(%rsp) # save original %rsp
1145 movq $rptr,%xmm1 # save $rptr, used in sqr8x
1146 movq $nptr,%xmm2 # save $nptr
1147 movq %r10, %xmm3 # -$num, used in sqr8x
1150 call __bn_sqr8x_internal
1151 call __bn_post4x_internal
1152 call __bn_sqr8x_internal
1153 call __bn_post4x_internal
1154 call __bn_sqr8x_internal
1155 call __bn_post4x_internal
1156 call __bn_sqr8x_internal
1157 call __bn_post4x_internal
1158 call __bn_sqr8x_internal
1159 call __bn_post4x_internal
1169 mov 40(%rsp),%rsi # restore %rsp
1180 .size bn_power5,.-bn_power5
1182 .globl bn_sqr8x_internal
1183 .hidden bn_sqr8x_internal
1184 .type bn_sqr8x_internal,\@abi-omnipotent
1187 __bn_sqr8x_internal:
1188 ##############################################################
1191 # a) multiply-n-add everything but a[i]*a[i];
1192 # b) shift result of a) by 1 to the left and accumulate
1193 # a[i]*a[i] products;
1195 ##############################################################
1261 lea 32(%r10),$i # $i=-($num-32)
1262 lea ($aptr,$num),$aptr # end of a[] buffer, ($aptr,$i)=&ap[2]
1264 mov $num,$j # $j=$num
1266 # comments apply to $num==8 case
1267 mov -32($aptr,$i),$a0 # a[0]
1268 lea 48+8(%rsp,$num,2),$tptr # end of tp[] buffer, &tp[2*$num]
1269 mov -24($aptr,$i),%rax # a[1]
1270 lea -32($tptr,$i),$tptr # end of tp[] window, &tp[2*$num-"$i"]
1271 mov -16($aptr,$i),$ai # a[2]
1275 mov %rax,$A0[0] # a[1]*a[0]
1278 mov $A0[0],-24($tptr,$i) # t[1]
1284 mov $A0[1],-16($tptr,$i) # t[2]
1288 mov -8($aptr,$i),$ai # a[3]
1290 mov %rax,$A1[0] # a[2]*a[1]+t[3]
1296 add %rax,$A0[0] # a[3]*a[0]+a[2]*a[1]+t[3]
1302 mov $A0[0],-8($tptr,$j) # t[3]
1307 mov ($aptr,$j),$ai # a[4]
1309 add %rax,$A1[1] # a[3]*a[1]+t[4]
1315 add %rax,$A0[1] # a[4]*a[0]+a[3]*a[1]+t[4]
1317 mov 8($aptr,$j),$ai # a[5]
1325 add %rax,$A1[0] # a[4]*a[3]+t[5]
1327 mov $A0[1],($tptr,$j) # t[4]
1332 add %rax,$A0[0] # a[5]*a[2]+a[4]*a[3]+t[5]
1334 mov 16($aptr,$j),$ai # a[6]
1341 add %rax,$A1[1] # a[5]*a[3]+t[6]
1343 mov $A0[0],8($tptr,$j) # t[5]
1348 add %rax,$A0[1] # a[6]*a[2]+a[5]*a[3]+t[6]
1350 mov 24($aptr,$j),$ai # a[7]
1358 add %rax,$A1[0] # a[6]*a[5]+t[7]
1360 mov $A0[1],16($tptr,$j) # t[6]
1366 add %rax,$A0[0] # a[7]*a[4]+a[6]*a[5]+t[6]
1372 mov $A0[0],-8($tptr,$j) # t[7]
1384 mov $A1[1],($tptr) # t[8]
1386 mov %rdx,8($tptr) # t[9]
1390 .Lsqr4x_outer: # comments apply to $num==6 case
1391 mov -32($aptr,$i),$a0 # a[0]
1392 lea 48+8(%rsp,$num,2),$tptr # end of tp[] buffer, &tp[2*$num]
1393 mov -24($aptr,$i),%rax # a[1]
1394 lea -32($tptr,$i),$tptr # end of tp[] window, &tp[2*$num-"$i"]
1395 mov -16($aptr,$i),$ai # a[2]
1399 mov -24($tptr,$i),$A0[0] # t[1]
1400 add %rax,$A0[0] # a[1]*a[0]+t[1]
1403 mov $A0[0],-24($tptr,$i) # t[1]
1410 add -16($tptr,$i),$A0[1] # a[2]*a[0]+t[2]
1413 mov $A0[1],-16($tptr,$i) # t[2]
1417 mov -8($aptr,$i),$ai # a[3]
1419 add %rax,$A1[0] # a[2]*a[1]+t[3]
1422 add -8($tptr,$i),$A1[0]
1427 add %rax,$A0[0] # a[3]*a[0]+a[2]*a[1]+t[3]
1433 mov $A0[0],-8($tptr,$i) # t[3]
1440 mov ($aptr,$j),$ai # a[4]
1442 add %rax,$A1[1] # a[3]*a[1]+t[4]
1446 add ($tptr,$j),$A1[1]
1451 add %rax,$A0[1] # a[4]*a[0]+a[3]*a[1]+t[4]
1453 mov 8($aptr,$j),$ai # a[5]
1460 add %rax,$A1[0] # a[4]*a[3]+t[5]
1461 mov $A0[1],($tptr,$j) # t[4]
1465 add 8($tptr,$j),$A1[0]
1470 add %rax,$A0[0] # a[5]*a[2]+a[4]*a[3]+t[5]
1476 mov $A0[0],-8($tptr,$j) # t[5], "preloaded t[1]" below
1488 mov $A1[1],($tptr) # t[6], "preloaded t[2]" below
1490 mov %rdx,8($tptr) # t[7], "preloaded t[3]" below
1495 # comments apply to $num==4 case
1496 mov -32($aptr),$a0 # a[0]
1497 lea 48+8(%rsp,$num,2),$tptr # end of tp[] buffer, &tp[2*$num]
1498 mov -24($aptr),%rax # a[1]
1499 lea -32($tptr,$i),$tptr # end of tp[] window, &tp[2*$num-"$i"]
1500 mov -16($aptr),$ai # a[2]
1504 add %rax,$A0[0] # a[1]*a[0]+t[1], preloaded t[1]
1512 mov $A0[0],-24($tptr) # t[1]
1515 add $A1[1],$A0[1] # a[2]*a[0]+t[2], preloaded t[2]
1516 mov -8($aptr),$ai # a[3]
1520 add %rax,$A1[0] # a[2]*a[1]+t[3], preloaded t[3]
1522 mov $A0[1],-16($tptr) # t[2]
1527 add %rax,$A0[0] # a[3]*a[0]+a[2]*a[1]+t[3]
1533 mov $A0[0],-8($tptr) # t[3]
1537 mov -16($aptr),%rax # a[2]
1542 mov $A1[1],($tptr) # t[4]
1544 mov %rdx,8($tptr) # t[5]
1549 my ($shift,$carry)=($a0,$a1);
1550 my @S=(@A1,$ai,$n0);
1554 sub $num,$i # $i=16-$num
1557 add $A1[0],%rax # t[5]
1559 mov %rax,8($tptr) # t[5]
1560 mov %rdx,16($tptr) # t[6]
1561 mov $carry,24($tptr) # t[7]
1563 mov -16($aptr,$i),%rax # a[0]
1564 lea 48+8(%rsp),$tptr
1565 xor $A0[0],$A0[0] # t[0]
1566 mov 8($tptr),$A0[1] # t[1]
1568 lea ($shift,$A0[0],2),$S[0] # t[2*i]<<1 | shift
1570 lea ($j,$A0[1],2),$S[1] # t[2*i+1]<<1 |
1572 or $A0[0],$S[1] # | t[2*i]>>63
1573 mov 16($tptr),$A0[0] # t[2*i+2] # prefetch
1574 mov $A0[1],$shift # shift=t[2*i+1]>>63
1575 mul %rax # a[i]*a[i]
1576 neg $carry # mov $carry,cf
1577 mov 24($tptr),$A0[1] # t[2*i+2+1] # prefetch
1579 mov -8($aptr,$i),%rax # a[i+1] # prefetch
1583 lea ($shift,$A0[0],2),$S[2] # t[2*i]<<1 | shift
1585 sbb $carry,$carry # mov cf,$carry
1587 lea ($j,$A0[1],2),$S[3] # t[2*i+1]<<1 |
1589 or $A0[0],$S[3] # | t[2*i]>>63
1590 mov 32($tptr),$A0[0] # t[2*i+2] # prefetch
1591 mov $A0[1],$shift # shift=t[2*i+1]>>63
1592 mul %rax # a[i]*a[i]
1593 neg $carry # mov $carry,cf
1594 mov 40($tptr),$A0[1] # t[2*i+2+1] # prefetch
1596 mov 0($aptr,$i),%rax # a[i+1] # prefetch
1601 sbb $carry,$carry # mov cf,$carry
1603 jmp .Lsqr4x_shift_n_add
1606 .Lsqr4x_shift_n_add:
1607 lea ($shift,$A0[0],2),$S[0] # t[2*i]<<1 | shift
1609 lea ($j,$A0[1],2),$S[1] # t[2*i+1]<<1 |
1611 or $A0[0],$S[1] # | t[2*i]>>63
1612 mov -16($tptr),$A0[0] # t[2*i+2] # prefetch
1613 mov $A0[1],$shift # shift=t[2*i+1]>>63
1614 mul %rax # a[i]*a[i]
1615 neg $carry # mov $carry,cf
1616 mov -8($tptr),$A0[1] # t[2*i+2+1] # prefetch
1618 mov -8($aptr,$i),%rax # a[i+1] # prefetch
1619 mov $S[0],-32($tptr)
1622 lea ($shift,$A0[0],2),$S[2] # t[2*i]<<1 | shift
1623 mov $S[1],-24($tptr)
1624 sbb $carry,$carry # mov cf,$carry
1626 lea ($j,$A0[1],2),$S[3] # t[2*i+1]<<1 |
1628 or $A0[0],$S[3] # | t[2*i]>>63
1629 mov 0($tptr),$A0[0] # t[2*i+2] # prefetch
1630 mov $A0[1],$shift # shift=t[2*i+1]>>63
1631 mul %rax # a[i]*a[i]
1632 neg $carry # mov $carry,cf
1633 mov 8($tptr),$A0[1] # t[2*i+2+1] # prefetch
1635 mov 0($aptr,$i),%rax # a[i+1] # prefetch
1636 mov $S[2],-16($tptr)
1639 lea ($shift,$A0[0],2),$S[0] # t[2*i]<<1 | shift
1641 sbb $carry,$carry # mov cf,$carry
1643 lea ($j,$A0[1],2),$S[1] # t[2*i+1]<<1 |
1645 or $A0[0],$S[1] # | t[2*i]>>63
1646 mov 16($tptr),$A0[0] # t[2*i+2] # prefetch
1647 mov $A0[1],$shift # shift=t[2*i+1]>>63
1648 mul %rax # a[i]*a[i]
1649 neg $carry # mov $carry,cf
1650 mov 24($tptr),$A0[1] # t[2*i+2+1] # prefetch
1652 mov 8($aptr,$i),%rax # a[i+1] # prefetch
1656 lea ($shift,$A0[0],2),$S[2] # t[2*i]<<1 | shift
1658 sbb $carry,$carry # mov cf,$carry
1660 lea ($j,$A0[1],2),$S[3] # t[2*i+1]<<1 |
1662 or $A0[0],$S[3] # | t[2*i]>>63
1663 mov 32($tptr),$A0[0] # t[2*i+2] # prefetch
1664 mov $A0[1],$shift # shift=t[2*i+1]>>63
1665 mul %rax # a[i]*a[i]
1666 neg $carry # mov $carry,cf
1667 mov 40($tptr),$A0[1] # t[2*i+2+1] # prefetch
1669 mov 16($aptr,$i),%rax # a[i+1] # prefetch
1673 sbb $carry,$carry # mov cf,$carry
1676 jnz .Lsqr4x_shift_n_add
1678 lea ($shift,$A0[0],2),$S[0] # t[2*i]<<1 | shift
1681 lea ($j,$A0[1],2),$S[1] # t[2*i+1]<<1 |
1683 or $A0[0],$S[1] # | t[2*i]>>63
1684 mov -16($tptr),$A0[0] # t[2*i+2] # prefetch
1685 mov $A0[1],$shift # shift=t[2*i+1]>>63
1686 mul %rax # a[i]*a[i]
1687 neg $carry # mov $carry,cf
1688 mov -8($tptr),$A0[1] # t[2*i+2+1] # prefetch
1690 mov -8($aptr),%rax # a[i+1] # prefetch
1691 mov $S[0],-32($tptr)
1694 lea ($shift,$A0[0],2),$S[2] # t[2*i]<<1|shift
1695 mov $S[1],-24($tptr)
1696 sbb $carry,$carry # mov cf,$carry
1698 lea ($j,$A0[1],2),$S[3] # t[2*i+1]<<1 |
1700 or $A0[0],$S[3] # | t[2*i]>>63
1701 mul %rax # a[i]*a[i]
1702 neg $carry # mov $carry,cf
1705 mov $S[2],-16($tptr)
1709 ######################################################################
1710 # Montgomery reduction part, "word-by-word" algorithm.
1712 # This new path is inspired by multiple submissions from Intel, by
1713 # Shay Gueron, Vlad Krasnov, Erdinc Ozturk, James Guilford,
1716 my ($nptr,$tptr,$carry,$m0)=("%rbp","%rdi","%rsi","%rbx");
1720 __bn_sqr8x_reduction:
1722 lea ($nptr,$num),%rcx # end of n[]
1723 lea 48+8(%rsp,$num,2),%rdx # end of t[] buffer
1725 lea 48+8(%rsp,$num),$tptr # end of initial t[] window
1728 jmp .L8x_reduction_loop
1731 .L8x_reduction_loop:
1732 lea ($tptr,$num),$tptr # start of current t[] window
1742 mov %rax,(%rdx) # store top-most carry bit
1743 lea 8*8($tptr),$tptr
1747 imulq 32+8(%rsp),$m0 # n0*a[0]
1748 mov 8*0($nptr),%rax # n[0]
1755 mov 8*1($nptr),%rax # n[1]
1765 mov $m0,48-8+8(%rsp,%rcx,8) # put aside n0*a[i]
1774 mov 32+8(%rsp),$carry # pull n0, borrow $carry
1782 imulq %r8,$carry # modulo-scheduled
1812 mov $carry,$m0 # n0*a[i]
1814 mov 8*0($nptr),%rax # n[0]
1823 lea 8*8($nptr),$nptr
1825 mov 8+8(%rsp),%rdx # pull end of t[]
1826 cmp 0+8(%rsp),$nptr # end of n[]?
1838 sbb $carry,$carry # top carry
1840 mov 48+56+8(%rsp),$m0 # pull n0*a[0]
1850 mov %r8,($tptr) # save result
1859 lea 8($tptr),$tptr # $tptr++
1904 mov 48-16+8(%rsp,%rcx,8),$m0# pull n0*a[i]
1908 mov 8*0($nptr),%rax # pull n[0]
1915 lea 8*8($nptr),$nptr
1916 mov 8+8(%rsp),%rdx # pull end of t[]
1917 cmp 0+8(%rsp),$nptr # end of n[]?
1918 jae .L8x_tail_done # break out of loop
1920 mov 48+56+8(%rsp),$m0 # pull n0*a[0]
1922 mov 8*0($nptr),%rax # pull n[0]
1931 sbb $carry,$carry # top carry
1939 add (%rdx),%r8 # can this overflow?
1959 adc \$0,%rax # top-most carry
1960 mov -8($nptr),%rcx # np[num-1]
1963 movq %xmm2,$nptr # restore $nptr
1965 mov %r8,8*0($tptr) # store top 512 bits
1967 movq %xmm3,$num # $num is %r9, can't be moved upwards
1974 lea 8*8($tptr),$tptr
1976 cmp %rdx,$tptr # end of t[]?
1977 jb .L8x_reduction_loop
1979 .size bn_sqr8x_internal,.-bn_sqr8x_internal
1982 ##############################################################
1983 # Post-condition, 4x unrolled
1986 my ($tptr,$nptr)=("%rbx","%rbp");
1988 .type __bn_post4x_internal,\@abi-omnipotent
1990 __bn_post4x_internal:
1992 lea (%rdi,$num),$tptr # %rdi was $tptr above
1994 movq %xmm1,$rptr # restore $rptr
1996 movq %xmm1,$aptr # prepare for back-to-back call
1998 dec %r12 # so that after 'not' we get -n[0]
2003 jmp .Lsqr4x_sub_entry
2012 lea 8*4($nptr),$nptr
2022 neg %r10 # mov %r10,%cf
2028 lea 8*4($tptr),$tptr
2030 sbb %r10,%r10 # mov %cf,%r10
2033 lea 8*4($rptr),$rptr
2038 mov $num,%r10 # prepare for back-to-back call
2039 neg $num # restore $num
2041 .size __bn_post4x_internal,.-__bn_post4x_internal
2046 .globl bn_from_montgomery
2047 .type bn_from_montgomery,\@abi-omnipotent
2050 testl \$7,`($win64?"48(%rsp)":"%r9d")`
2054 .size bn_from_montgomery,.-bn_from_montgomery
2056 .type bn_from_mont8x,\@function,6
2069 shl \$3,${num}d # convert $num to bytes
2070 lea ($num,$num,2),%r10 # 3*$num in bytes
2074 ##############################################################
2075 # Ensure that stack frame doesn't alias with $rptr+3*$num
2076 # modulo 4096, which covers ret[num], am[num] and n[num]
2077 # (see bn_exp.c). The stack is allocated to aligned with
2078 # bn_power5's frame, and as bn_from_montgomery happens to be
2079 # last operation, we use the opportunity to cleanse it.
2081 lea -320(%rsp,$num,2),%r11
2087 sub %r11,%rbp # align with $aptr
2088 lea -320(%rbp,$num,2),%rbp # future alloca(frame+2*$num*8+256)
2093 lea 4096-320(,$num,2),%r10
2094 lea -320(%rbp,$num,2),%rbp # future alloca(frame+2*$num*8+256)
2104 lea (%rbp,%r11),%rsp
2108 jmp .Lfrom_page_walk_done
2111 lea -4096(%rsp),%rsp
2115 .Lfrom_page_walk_done:
2120 ##############################################################
2123 # +0 saved $num, used in reduction section
2124 # +8 &t[2*$num], used in reduction section
2130 mov %rax, 40(%rsp) # save original %rsp
2139 movdqu ($aptr),%xmm1
2140 movdqu 16($aptr),%xmm2
2141 movdqu 32($aptr),%xmm3
2142 movdqa %xmm0,(%rax,$num)
2143 movdqu 48($aptr),%xmm4
2144 movdqa %xmm0,16(%rax,$num)
2145 .byte 0x48,0x8d,0xb6,0x40,0x00,0x00,0x00 # lea 64($aptr),$aptr
2147 movdqa %xmm0,32(%rax,$num)
2148 movdqa %xmm2,16(%rax)
2149 movdqa %xmm0,48(%rax,$num)
2150 movdqa %xmm3,32(%rax)
2151 movdqa %xmm4,48(%rax)
2160 movq %r10, %xmm3 # -num
2162 $code.=<<___ if ($addx);
2163 mov OPENSSL_ia32cap_P+8(%rip),%r11d
2165 cmp \$0x80108,%r11d # check for AD*X+BMI2+BMI1
2168 lea (%rax,$num),$rptr
2169 call __bn_sqrx8x_reduction
2170 call __bn_postx4x_internal
2174 mov 40(%rsp),%rsi # restore %rsp
2175 jmp .Lfrom_mont_zero
2181 call __bn_sqr8x_reduction
2182 call __bn_post4x_internal
2186 mov 40(%rsp),%rsi # restore %rsp
2187 jmp .Lfrom_mont_zero
2191 movdqa %xmm0,16*0(%rax)
2192 movdqa %xmm0,16*1(%rax)
2193 movdqa %xmm0,16*2(%rax)
2194 movdqa %xmm0,16*3(%rax)
2197 jnz .Lfrom_mont_zero
2209 .size bn_from_mont8x,.-bn_from_mont8x
2215 my $bp="%rdx"; # restore original value
2218 .type bn_mulx4x_mont_gather5,\@function,6
2220 bn_mulx4x_mont_gather5:
2231 shl \$3,${num}d # convert $num to bytes
2232 lea ($num,$num,2),%r10 # 3*$num in bytes
2236 ##############################################################
2237 # Ensure that stack frame doesn't alias with $rptr+3*$num
2238 # modulo 4096, which covers ret[num], am[num] and n[num]
2239 # (see bn_exp.c). This is done to allow memory disambiguation
2240 # logic do its magic. [Extra [num] is allocated in order
2241 # to align with bn_power5's frame, which is cleansed after
2242 # completing exponentiation. Extra 256 bytes is for power mask
2243 # calculated from 7th argument, the index.]
2245 lea -320(%rsp,$num,2),%r11
2251 sub %r11,%rbp # align with $aptr
2252 lea -320(%rbp,$num,2),%rbp # future alloca(frame+2*$num*8+256)
2256 lea 4096-320(,$num,2),%r10
2257 lea -320(%rbp,$num,2),%rbp # future alloca(frame+2*$num*8+256)
2263 and \$-64,%rbp # ensure alignment
2267 lea (%rbp,%r11),%rsp
2270 ja .Lmulx4x_page_walk
2271 jmp .Lmulx4x_page_walk_done
2274 lea -4096(%rsp),%rsp
2277 ja .Lmulx4x_page_walk
2278 .Lmulx4x_page_walk_done:
2280 ##############################################################
2283 # +8 off-loaded &b[i]
2292 mov $n0, 32(%rsp) # save *n0
2293 mov %rax,40(%rsp) # save original %rsp
2295 call mulx4x_internal
2297 mov 40(%rsp),%rsi # restore %rsp
2309 .size bn_mulx4x_mont_gather5,.-bn_mulx4x_mont_gather5
2311 .type mulx4x_internal,\@abi-omnipotent
2314 mov $num,8(%rsp) # save -$num (it was in bytes)
2316 neg $num # restore $num
2318 neg %r10 # restore $num
2319 lea 128($bp,$num),%r13 # end of powers table (+size optimization)
2321 movd `($win64?56:8)`(%rax),%xmm5 # load 7th argument
2323 lea .Linc(%rip),%rax
2324 mov %r13,16+8(%rsp) # end of b[num]
2325 mov $num,24+8(%rsp) # inner counter
2326 mov $rp, 56+8(%rsp) # save $rp
2328 my ($aptr, $bptr, $nptr, $tptr, $mi, $bi, $zero, $num)=
2329 ("%rsi","%rdi","%rcx","%rbx","%r8","%r9","%rbp","%rax");
2331 my $STRIDE=2**5*8; # 5 is "window size"
2332 my $N=$STRIDE/4; # should match cache line size
2334 movdqa 0(%rax),%xmm0 # 00000001000000010000000000000000
2335 movdqa 16(%rax),%xmm1 # 00000002000000020000000200000002
2336 lea 88-112(%rsp,%r10),%r10 # place the mask after tp[num+1] (+ICache optimizaton)
2337 lea 128($bp),$bptr # size optimization
2339 pshufd \$0,%xmm5,%xmm5 # broadcast index
2344 ########################################################################
2345 # calculate mask by comparing 0..31 to index and save result to stack
2350 pcmpeqd %xmm5,%xmm0 # compare to 1,0
2353 for($i=0;$i<$STRIDE/16-4;$i+=4) {
2356 pcmpeqd %xmm5,%xmm1 # compare to 3,2
2357 movdqa %xmm0,`16*($i+0)+112`(%r10)
2361 pcmpeqd %xmm5,%xmm2 # compare to 5,4
2362 movdqa %xmm1,`16*($i+1)+112`(%r10)
2366 pcmpeqd %xmm5,%xmm3 # compare to 7,6
2367 movdqa %xmm2,`16*($i+2)+112`(%r10)
2372 movdqa %xmm3,`16*($i+3)+112`(%r10)
2376 $code.=<<___; # last iteration can be optimized
2380 movdqa %xmm0,`16*($i+0)+112`(%r10)
2384 movdqa %xmm1,`16*($i+1)+112`(%r10)
2387 movdqa %xmm2,`16*($i+2)+112`(%r10)
2389 pand `16*($i+0)-128`($bptr),%xmm0 # while it's still in register
2390 pand `16*($i+1)-128`($bptr),%xmm1
2391 pand `16*($i+2)-128`($bptr),%xmm2
2392 movdqa %xmm3,`16*($i+3)+112`(%r10)
2393 pand `16*($i+3)-128`($bptr),%xmm3
2397 for($i=0;$i<$STRIDE/16-4;$i+=4) {
2399 movdqa `16*($i+0)-128`($bptr),%xmm4
2400 movdqa `16*($i+1)-128`($bptr),%xmm5
2401 movdqa `16*($i+2)-128`($bptr),%xmm2
2402 pand `16*($i+0)+112`(%r10),%xmm4
2403 movdqa `16*($i+3)-128`($bptr),%xmm3
2404 pand `16*($i+1)+112`(%r10),%xmm5
2406 pand `16*($i+2)+112`(%r10),%xmm2
2408 pand `16*($i+3)+112`(%r10),%xmm3
2415 pshufd \$0x4e,%xmm0,%xmm1
2417 lea $STRIDE($bptr),$bptr
2418 movq %xmm0,%rdx # bp[0]
2419 lea 64+8*4+8(%rsp),$tptr
2422 mulx 0*8($aptr),$mi,%rax # a[0]*b[0]
2423 mulx 1*8($aptr),%r11,%r12 # a[1]*b[0]
2425 mulx 2*8($aptr),%rax,%r13 # ...
2428 mulx 3*8($aptr),%rax,%r14
2431 imulq 32+8(%rsp),$mi # "t[0]"*n0
2432 xor $zero,$zero # cf=0, of=0
2435 mov $bptr,8+8(%rsp) # off-load &b[i]
2437 lea 4*8($aptr),$aptr
2439 adcx $zero,%r14 # cf=0
2441 mulx 0*8($nptr),%rax,%r10
2442 adcx %rax,%r15 # discarded
2444 mulx 1*8($nptr),%rax,%r11
2447 mulx 2*8($nptr),%rax,%r12
2448 mov 24+8(%rsp),$bptr # counter value
2449 mov %r10,-8*4($tptr)
2452 mulx 3*8($nptr),%rax,%r15
2454 mov %r11,-8*3($tptr)
2456 adox $zero,%r15 # of=0
2457 lea 4*8($nptr),$nptr
2458 mov %r12,-8*2($tptr)
2463 adcx $zero,%r15 # cf=0, modulo-scheduled
2464 mulx 0*8($aptr),%r10,%rax # a[4]*b[0]
2466 mulx 1*8($aptr),%r11,%r14 # a[5]*b[0]
2468 mulx 2*8($aptr),%r12,%rax # ...
2470 mulx 3*8($aptr),%r13,%r14
2474 adcx $zero,%r14 # cf=0
2475 lea 4*8($aptr),$aptr
2476 lea 4*8($tptr),$tptr
2479 mulx 0*8($nptr),%rax,%r15
2482 mulx 1*8($nptr),%rax,%r15
2485 mulx 2*8($nptr),%rax,%r15
2486 mov %r10,-5*8($tptr)
2488 mov %r11,-4*8($tptr)
2490 mulx 3*8($nptr),%rax,%r15
2492 mov %r12,-3*8($tptr)
2495 lea 4*8($nptr),$nptr
2496 mov %r13,-2*8($tptr)
2498 dec $bptr # of=0, pass cf
2501 mov 8(%rsp),$num # load -num
2502 adc $zero,%r15 # modulo-scheduled
2503 lea ($aptr,$num),$aptr # rewind $aptr
2505 mov 8+8(%rsp),$bptr # re-load &b[i]
2506 adc $zero,$zero # top-most carry
2507 mov %r14,-1*8($tptr)
2512 lea 16-256($tptr),%r10 # where 256-byte mask is (+density control)
2517 for($i=0;$i<$STRIDE/16;$i+=4) {
2519 movdqa `16*($i+0)-128`($bptr),%xmm0
2520 movdqa `16*($i+1)-128`($bptr),%xmm1
2521 movdqa `16*($i+2)-128`($bptr),%xmm2
2522 pand `16*($i+0)+256`(%r10),%xmm0
2523 movdqa `16*($i+3)-128`($bptr),%xmm3
2524 pand `16*($i+1)+256`(%r10),%xmm1
2526 pand `16*($i+2)+256`(%r10),%xmm2
2528 pand `16*($i+3)+256`(%r10),%xmm3
2535 pshufd \$0x4e,%xmm4,%xmm0
2537 lea $STRIDE($bptr),$bptr
2538 movq %xmm0,%rdx # m0=bp[i]
2540 mov $zero,($tptr) # save top-most carry
2541 lea 4*8($tptr,$num),$tptr # rewind $tptr
2542 mulx 0*8($aptr),$mi,%r11 # a[0]*b[i]
2543 xor $zero,$zero # cf=0, of=0
2545 mulx 1*8($aptr),%r14,%r12 # a[1]*b[i]
2546 adox -4*8($tptr),$mi # +t[0]
2548 mulx 2*8($aptr),%r15,%r13 # ...
2549 adox -3*8($tptr),%r11
2551 mulx 3*8($aptr),%rdx,%r14
2552 adox -2*8($tptr),%r12
2554 lea ($nptr,$num),$nptr # rewind $nptr
2555 lea 4*8($aptr),$aptr
2556 adox -1*8($tptr),%r13
2561 imulq 32+8(%rsp),$mi # "t[0]"*n0
2564 xor $zero,$zero # cf=0, of=0
2565 mov $bptr,8+8(%rsp) # off-load &b[i]
2567 mulx 0*8($nptr),%rax,%r10
2568 adcx %rax,%r15 # discarded
2570 mulx 1*8($nptr),%rax,%r11
2573 mulx 2*8($nptr),%rax,%r12
2576 mulx 3*8($nptr),%rax,%r15
2578 mov 24+8(%rsp),$bptr # counter value
2579 mov %r10,-8*4($tptr)
2581 mov %r11,-8*3($tptr)
2582 adox $zero,%r15 # of=0
2583 mov %r12,-8*2($tptr)
2584 lea 4*8($nptr),$nptr
2589 mulx 0*8($aptr),%r10,%rax # a[4]*b[i]
2590 adcx $zero,%r15 # cf=0, modulo-scheduled
2592 mulx 1*8($aptr),%r11,%r14 # a[5]*b[i]
2593 adcx 0*8($tptr),%r10
2595 mulx 2*8($aptr),%r12,%rax # ...
2596 adcx 1*8($tptr),%r11
2598 mulx 3*8($aptr),%r13,%r14
2600 adcx 2*8($tptr),%r12
2602 adcx 3*8($tptr),%r13
2603 adox $zero,%r14 # of=0
2604 lea 4*8($aptr),$aptr
2605 lea 4*8($tptr),$tptr
2606 adcx $zero,%r14 # cf=0
2609 mulx 0*8($nptr),%rax,%r15
2612 mulx 1*8($nptr),%rax,%r15
2615 mulx 2*8($nptr),%rax,%r15
2616 mov %r10,-5*8($tptr)
2619 mov %r11,-4*8($tptr)
2620 mulx 3*8($nptr),%rax,%r15
2622 lea 4*8($nptr),$nptr
2623 mov %r12,-3*8($tptr)
2626 mov %r13,-2*8($tptr)
2628 dec $bptr # of=0, pass cf
2631 mov 0+8(%rsp),$num # load -num
2632 adc $zero,%r15 # modulo-scheduled
2633 sub 0*8($tptr),$bptr # pull top-most carry to %cf
2634 mov 8+8(%rsp),$bptr # re-load &b[i]
2637 lea ($aptr,$num),$aptr # rewind $aptr
2638 adc $zero,$zero # top-most carry
2639 mov %r14,-1*8($tptr)
2646 mov ($nptr,$num),%r12
2647 lea ($nptr,$num),%rbp # rewind $nptr
2649 lea ($tptr,$num),%rdi # rewind $tptr
2652 sub %r14,%r10 # compare top-most words
2656 sub %r8,%rax # %rax=-%r8
2657 mov 56+8(%rsp),%rdx # restore rp
2658 dec %r12 # so that after 'not' we get -n[0]
2663 jmp .Lsqrx4x_sub_entry # common post-condition
2664 .size mulx4x_internal,.-mulx4x_internal
2667 ######################################################################
2669 my $rptr="%rdi"; # BN_ULONG *rptr,
2670 my $aptr="%rsi"; # const BN_ULONG *aptr,
2671 my $bptr="%rdx"; # const void *table,
2672 my $nptr="%rcx"; # const BN_ULONG *nptr,
2673 my $n0 ="%r8"; # const BN_ULONG *n0);
2674 my $num ="%r9"; # int num, has to be divisible by 8
2677 my ($i,$j,$tptr)=("%rbp","%rcx",$rptr);
2678 my @A0=("%r10","%r11");
2679 my @A1=("%r12","%r13");
2680 my ($a0,$a1,$ai)=("%r14","%r15","%rbx");
2683 .type bn_powerx5,\@function,6
2696 shl \$3,${num}d # convert $num to bytes
2697 lea ($num,$num,2),%r10 # 3*$num in bytes
2701 ##############################################################
2702 # Ensure that stack frame doesn't alias with $rptr+3*$num
2703 # modulo 4096, which covers ret[num], am[num] and n[num]
2704 # (see bn_exp.c). This is done to allow memory disambiguation
2705 # logic do its magic. [Extra 256 bytes is for power mask
2706 # calculated from 7th argument, the index.]
2708 lea -320(%rsp,$num,2),%r11
2714 sub %r11,%rbp # align with $aptr
2715 lea -320(%rbp,$num,2),%rbp # future alloca(frame+2*$num*8+256)
2720 lea 4096-320(,$num,2),%r10
2721 lea -320(%rbp,$num,2),%rbp # alloca(frame+2*$num*8+256)
2731 lea (%rbp,%r11),%rsp
2735 jmp .Lpwrx_page_walk_done
2738 lea -4096(%rsp),%rsp
2742 .Lpwrx_page_walk_done:
2747 ##############################################################
2750 # +0 saved $num, used in reduction section
2751 # +8 &t[2*$num], used in reduction section
2752 # +16 intermediate carry bit
2753 # +24 top-most carry bit, used in reduction section
2759 movq $rptr,%xmm1 # save $rptr
2760 movq $nptr,%xmm2 # save $nptr
2761 movq %r10, %xmm3 # -$num
2764 mov %rax, 40(%rsp) # save original %rsp
2767 call __bn_sqrx8x_internal
2768 call __bn_postx4x_internal
2769 call __bn_sqrx8x_internal
2770 call __bn_postx4x_internal
2771 call __bn_sqrx8x_internal
2772 call __bn_postx4x_internal
2773 call __bn_sqrx8x_internal
2774 call __bn_postx4x_internal
2775 call __bn_sqrx8x_internal
2776 call __bn_postx4x_internal
2778 mov %r10,$num # -num
2784 call mulx4x_internal
2786 mov 40(%rsp),%rsi # restore %rsp
2798 .size bn_powerx5,.-bn_powerx5
2800 .globl bn_sqrx8x_internal
2801 .hidden bn_sqrx8x_internal
2802 .type bn_sqrx8x_internal,\@abi-omnipotent
2805 __bn_sqrx8x_internal:
2806 ##################################################################
2809 # a) multiply-n-add everything but a[i]*a[i];
2810 # b) shift result of a) by 1 to the left and accumulate
2811 # a[i]*a[i] products;
2813 ##################################################################
2814 # 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]
2845 # 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]
2848 my ($zero,$carry)=("%rbp","%rcx");
2851 lea 48+8(%rsp),$tptr
2852 lea ($aptr,$num),$aaptr
2853 mov $num,0+8(%rsp) # save $num
2854 mov $aaptr,8+8(%rsp) # save end of $aptr
2855 jmp .Lsqr8x_zero_start
2858 .byte 0x66,0x66,0x66,0x2e,0x0f,0x1f,0x84,0x00,0x00,0x00,0x00,0x00
2861 movdqa %xmm0,0*8($tptr)
2862 movdqa %xmm0,2*8($tptr)
2863 movdqa %xmm0,4*8($tptr)
2864 movdqa %xmm0,6*8($tptr)
2865 .Lsqr8x_zero_start: # aligned at 32
2866 movdqa %xmm0,8*8($tptr)
2867 movdqa %xmm0,10*8($tptr)
2868 movdqa %xmm0,12*8($tptr)
2869 movdqa %xmm0,14*8($tptr)
2870 lea 16*8($tptr),$tptr
2874 mov 0*8($aptr),%rdx # a[0], modulo-scheduled
2875 #xor %r9,%r9 # t[1], ex-$num, zero already
2882 lea 48+8(%rsp),$tptr
2883 xor $zero,$zero # cf=0, cf=0
2884 jmp .Lsqrx8x_outer_loop
2887 .Lsqrx8x_outer_loop:
2888 mulx 1*8($aptr),%r8,%rax # a[1]*a[0]
2889 adcx %r9,%r8 # a[1]*a[0]+=t[1]
2891 mulx 2*8($aptr),%r9,%rax # a[2]*a[0]
2894 .byte 0xc4,0xe2,0xab,0xf6,0x86,0x18,0x00,0x00,0x00 # mulx 3*8($aptr),%r10,%rax # ...
2897 .byte 0xc4,0xe2,0xa3,0xf6,0x86,0x20,0x00,0x00,0x00 # mulx 4*8($aptr),%r11,%rax
2900 mulx 5*8($aptr),%r12,%rax
2903 mulx 6*8($aptr),%r13,%rax
2906 mulx 7*8($aptr),%r14,%r15
2907 mov 1*8($aptr),%rdx # a[1]
2911 mov %r8,1*8($tptr) # t[1]
2912 mov %r9,2*8($tptr) # t[2]
2913 sbb $carry,$carry # mov %cf,$carry
2914 xor $zero,$zero # cf=0, of=0
2917 mulx 2*8($aptr),%r8,%rbx # a[2]*a[1]
2918 mulx 3*8($aptr),%r9,%rax # a[3]*a[1]
2921 mulx 4*8($aptr),%r10,%rbx # ...
2924 .byte 0xc4,0xe2,0xa3,0xf6,0x86,0x28,0x00,0x00,0x00 # mulx 5*8($aptr),%r11,%rax
2927 .byte 0xc4,0xe2,0x9b,0xf6,0x9e,0x30,0x00,0x00,0x00 # mulx 6*8($aptr),%r12,%rbx
2930 .byte 0xc4,0x62,0x93,0xf6,0xb6,0x38,0x00,0x00,0x00 # mulx 7*8($aptr),%r13,%r14
2931 mov 2*8($aptr),%rdx # a[2]
2935 adox $zero,%r14 # of=0
2936 adcx $zero,%r14 # cf=0
2938 mov %r8,3*8($tptr) # t[3]
2939 mov %r9,4*8($tptr) # t[4]
2941 mulx 3*8($aptr),%r8,%rbx # a[3]*a[2]
2942 mulx 4*8($aptr),%r9,%rax # a[4]*a[2]
2945 mulx 5*8($aptr),%r10,%rbx # ...
2948 .byte 0xc4,0xe2,0xa3,0xf6,0x86,0x30,0x00,0x00,0x00 # mulx 6*8($aptr),%r11,%rax
2951 .byte 0xc4,0x62,0x9b,0xf6,0xae,0x38,0x00,0x00,0x00 # mulx 7*8($aptr),%r12,%r13
2953 mov 3*8($aptr),%rdx # a[3]
2957 mov %r8,5*8($tptr) # t[5]
2958 mov %r9,6*8($tptr) # t[6]
2959 mulx 4*8($aptr),%r8,%rax # a[4]*a[3]
2960 adox $zero,%r13 # of=0
2961 adcx $zero,%r13 # cf=0
2963 mulx 5*8($aptr),%r9,%rbx # a[5]*a[3]
2966 mulx 6*8($aptr),%r10,%rax # ...
2969 mulx 7*8($aptr),%r11,%r12
2970 mov 4*8($aptr),%rdx # a[4]
2971 mov 5*8($aptr),%r14 # a[5]
2974 mov 6*8($aptr),%r15 # a[6]
2976 adox $zero,%r12 # of=0
2977 adcx $zero,%r12 # cf=0
2979 mov %r8,7*8($tptr) # t[7]
2980 mov %r9,8*8($tptr) # t[8]
2982 mulx %r14,%r9,%rax # a[5]*a[4]
2983 mov 7*8($aptr),%r8 # a[7]
2985 mulx %r15,%r10,%rbx # a[6]*a[4]
2988 mulx %r8,%r11,%rax # a[7]*a[4]
2989 mov %r14,%rdx # a[5]
2992 #adox $zero,%rax # of=0
2993 adcx $zero,%rax # cf=0
2995 mulx %r15,%r14,%rbx # a[6]*a[5]
2996 mulx %r8,%r12,%r13 # a[7]*a[5]
2997 mov %r15,%rdx # a[6]
2998 lea 8*8($aptr),$aptr
3005 mulx %r8,%r8,%r14 # a[7]*a[6]
3010 je .Lsqrx8x_outer_break
3012 neg $carry # mov $carry,%cf
3016 adcx 9*8($tptr),%r9 # +=t[9]
3017 adcx 10*8($tptr),%r10 # ...
3018 adcx 11*8($tptr),%r11
3019 adc 12*8($tptr),%r12
3020 adc 13*8($tptr),%r13
3021 adc 14*8($tptr),%r14
3022 adc 15*8($tptr),%r15
3024 lea 2*64($tptr),$tptr
3025 sbb %rax,%rax # mov %cf,$carry
3027 mov -64($aptr),%rdx # a[0]
3028 mov %rax,16+8(%rsp) # offload $carry
3029 mov $tptr,24+8(%rsp)
3031 #lea 8*8($tptr),$tptr # see 2*8*8($tptr) above
3032 xor %eax,%eax # cf=0, of=0
3038 mulx 0*8($aaptr),%rax,%r8 # a[8]*a[i]
3039 adcx %rax,%rbx # +=t[8]
3042 mulx 1*8($aaptr),%rax,%r9 # ...
3046 mulx 2*8($aaptr),%rax,%r10
3050 mulx 3*8($aaptr),%rax,%r11
3054 .byte 0xc4,0x62,0xfb,0xf6,0xa5,0x20,0x00,0x00,0x00 # mulx 4*8($aaptr),%rax,%r12
3058 mulx 5*8($aaptr),%rax,%r13
3062 mulx 6*8($aaptr),%rax,%r14
3063 mov %rbx,($tptr,%rcx,8) # store t[8+i]
3068 .byte 0xc4,0x62,0xfb,0xf6,0xbd,0x38,0x00,0x00,0x00 # mulx 7*8($aaptr),%rax,%r15
3069 mov 8($aptr,%rcx,8),%rdx # a[i]
3071 adox %rbx,%r15 # %rbx is 0, of=0
3072 adcx %rbx,%r15 # cf=0
3078 lea 8*8($aaptr),$aaptr
3080 cmp 8+8(%rsp),$aaptr # done?
3083 sub 16+8(%rsp),%rbx # mov 16(%rsp),%cf
3094 lea 8*8($tptr),$tptr
3096 sbb %rax,%rax # mov %cf,%rax
3097 xor %ebx,%ebx # cf=0, of=0
3098 mov %rax,16+8(%rsp) # offload carry
3104 sub 16+8(%rsp),%rbx # mov 16(%rsp),%cf
3106 mov 24+8(%rsp),$carry # initial $tptr, borrow $carry
3108 mov 0*8($aptr),%rdx # a[8], modulo-scheduled
3116 cmp $carry,$tptr # cf=0, of=0
3117 je .Lsqrx8x_outer_loop
3122 mov 2*8($carry),%r10
3124 mov 3*8($carry),%r11
3126 mov 4*8($carry),%r12
3128 mov 5*8($carry),%r13
3130 mov 6*8($carry),%r14
3132 mov 7*8($carry),%r15
3134 jmp .Lsqrx8x_outer_loop
3137 .Lsqrx8x_outer_break:
3138 mov %r9,9*8($tptr) # t[9]
3139 movq %xmm3,%rcx # -$num
3140 mov %r10,10*8($tptr) # ...
3141 mov %r11,11*8($tptr)
3142 mov %r12,12*8($tptr)
3143 mov %r13,13*8($tptr)
3144 mov %r14,14*8($tptr)
3149 lea 48+8(%rsp),$tptr
3150 mov ($aptr,$i),%rdx # a[0]
3152 mov 8($tptr),$A0[1] # t[1]
3153 xor $A0[0],$A0[0] # t[0], of=0, cf=0
3154 mov 0+8(%rsp),$num # restore $num
3156 mov 16($tptr),$A1[0] # t[2] # prefetch
3157 mov 24($tptr),$A1[1] # t[3] # prefetch
3158 #jmp .Lsqrx4x_shift_n_add # happens to be aligned
3161 .Lsqrx4x_shift_n_add:
3165 .byte 0x48,0x8b,0x94,0x0e,0x08,0x00,0x00,0x00 # mov 8($aptr,$i),%rdx # a[i+1] # prefetch
3166 .byte 0x4c,0x8b,0x97,0x20,0x00,0x00,0x00 # mov 32($tptr),$A0[0] # t[2*i+4] # prefetch
3169 mov 40($tptr),$A0[1] # t[2*i+4+1] # prefetch
3176 mov 16($aptr,$i),%rdx # a[i+2] # prefetch
3177 mov 48($tptr),$A1[0] # t[2*i+6] # prefetch
3180 mov 56($tptr),$A1[1] # t[2*i+6+1] # prefetch
3187 mov 24($aptr,$i),%rdx # a[i+3] # prefetch
3189 mov 64($tptr),$A0[0] # t[2*i+8] # prefetch
3192 mov 72($tptr),$A0[1] # t[2*i+8+1] # prefetch
3199 jrcxz .Lsqrx4x_shift_n_add_break
3200 .byte 0x48,0x8b,0x94,0x0e,0x00,0x00,0x00,0x00 # mov 0($aptr,$i),%rdx # a[i+4] # prefetch
3203 mov 80($tptr),$A1[0] # t[2*i+10] # prefetch
3204 mov 88($tptr),$A1[1] # t[2*i+10+1] # prefetch
3209 jmp .Lsqrx4x_shift_n_add
3212 .Lsqrx4x_shift_n_add_break:
3216 lea 64($tptr),$tptr # end of t[] buffer
3219 ######################################################################
3220 # Montgomery reduction part, "word-by-word" algorithm.
3222 # This new path is inspired by multiple submissions from Intel, by
3223 # Shay Gueron, Vlad Krasnov, Erdinc Ozturk, James Guilford,
3226 my ($nptr,$carry,$m0)=("%rbp","%rsi","%rdx");
3230 __bn_sqrx8x_reduction:
3231 xor %eax,%eax # initial top-most carry bit
3232 mov 32+8(%rsp),%rbx # n0
3233 mov 48+8(%rsp),%rdx # "%r8", 8*0($tptr)
3234 lea -8*8($nptr,$num),%rcx # end of n[]
3235 #lea 48+8(%rsp,$num,2),$tptr # end of t[] buffer
3236 mov %rcx, 0+8(%rsp) # save end of n[]
3237 mov $tptr,8+8(%rsp) # save end of t[]
3239 lea 48+8(%rsp),$tptr # initial t[] window
3240 jmp .Lsqrx8x_reduction_loop
3243 .Lsqrx8x_reduction_loop:
3249 imulq %rbx,%rdx # n0*a[i]
3253 mov %rax,24+8(%rsp) # store top-most carry bit
3255 lea 8*8($tptr),$tptr
3256 xor $carry,$carry # cf=0,of=0
3263 mulx 8*0($nptr),%rax,%r8 # n[0]
3264 adcx %rbx,%rax # discarded
3267 mulx 8*1($nptr),%rbx,%r9 # n[1]
3271 mulx 8*2($nptr),%rbx,%r10
3275 mulx 8*3($nptr),%rbx,%r11
3279 .byte 0xc4,0x62,0xe3,0xf6,0xa5,0x20,0x00,0x00,0x00 # mulx 8*4($nptr),%rbx,%r12
3285 mulx 32+8(%rsp),%rbx,%rdx # %rdx discarded
3287 mov %rax,64+48+8(%rsp,%rcx,8) # put aside n0*a[i]
3289 mulx 8*5($nptr),%rax,%r13
3293 mulx 8*6($nptr),%rax,%r14
3297 mulx 8*7($nptr),%rax,%r15
3300 adox $carry,%r15 # $carry is 0
3301 adcx $carry,%r15 # cf=0
3303 .byte 0x67,0x67,0x67
3307 mov $carry,%rax # xor %rax,%rax
3308 cmp 0+8(%rsp),$nptr # end of n[]?
3309 jae .Lsqrx8x_no_tail
3311 mov 48+8(%rsp),%rdx # pull n0*a[0]
3313 lea 8*8($nptr),$nptr
3316 adcx 8*2($tptr),%r10
3322 lea 8*8($tptr),$tptr
3323 sbb %rax,%rax # top carry
3325 xor $carry,$carry # of=0, cf=0
3332 mulx 8*0($nptr),%rax,%r8
3336 mulx 8*1($nptr),%rax,%r9
3340 mulx 8*2($nptr),%rax,%r10
3344 mulx 8*3($nptr),%rax,%r11
3348 .byte 0xc4,0x62,0xfb,0xf6,0xa5,0x20,0x00,0x00,0x00 # mulx 8*4($nptr),%rax,%r12
3352 mulx 8*5($nptr),%rax,%r13
3356 mulx 8*6($nptr),%rax,%r14
3360 mulx 8*7($nptr),%rax,%r15
3361 mov 72+48+8(%rsp,%rcx,8),%rdx # pull n0*a[i]
3364 mov %rbx,($tptr,%rcx,8) # save result
3366 adcx $carry,%r15 # cf=0
3371 cmp 0+8(%rsp),$nptr # end of n[]?
3372 jae .Lsqrx8x_tail_done # break out of loop
3374 sub 16+8(%rsp),$carry # mov 16(%rsp),%cf
3375 mov 48+8(%rsp),%rdx # pull n0*a[0]
3376 lea 8*8($nptr),$nptr
3385 lea 8*8($tptr),$tptr
3387 sub \$8,%rcx # mov \$-8,%rcx
3389 xor $carry,$carry # of=0, cf=0
3396 add 24+8(%rsp),%r8 # can this overflow?
3406 sub 16+8(%rsp),$carry # mov 16(%rsp),%cf
3407 .Lsqrx8x_no_tail: # %cf is 0 if jumped here
3411 mov 8*7($nptr),$carry
3412 movq %xmm2,$nptr # restore $nptr
3419 adc \$0,%rax # top-most carry
3421 mov 32+8(%rsp),%rbx # n0
3422 mov 8*8($tptr,%rcx),%rdx # modulo-scheduled "%r8"
3424 mov %r8,8*0($tptr) # store top 512 bits
3425 lea 8*8($tptr),%r8 # borrow %r8
3434 lea 8*8($tptr,%rcx),$tptr # start of current t[] window
3435 cmp 8+8(%rsp),%r8 # end of t[]?
3436 jb .Lsqrx8x_reduction_loop
3438 .size bn_sqrx8x_internal,.-bn_sqrx8x_internal
3441 ##############################################################
3442 # Post-condition, 4x unrolled
3445 my ($rptr,$nptr)=("%rdx","%rbp");
3448 __bn_postx4x_internal:
3450 mov %rcx,%r10 # -$num
3451 mov %rcx,%r9 # -$num
3454 #lea 48+8(%rsp,%r9),$tptr
3455 movq %xmm1,$rptr # restore $rptr
3456 movq %xmm1,$aptr # prepare for back-to-back call
3457 dec %r12 # so that after 'not' we get -n[0]
3462 jmp .Lsqrx4x_sub_entry
3472 lea 8*4($nptr),$nptr
3477 neg %r8 # mov %r8,%cf
3483 lea 8*4($tptr),$tptr
3485 sbb %r8,%r8 # mov %cf,%r8
3488 lea 8*4($rptr),$rptr
3493 neg %r9 # restore $num
3496 .size __bn_postx4x_internal,.-__bn_postx4x_internal
3501 my ($inp,$num,$tbl,$idx)=$win64?("%rcx","%edx","%r8", "%r9d") : # Win64 order
3502 ("%rdi","%esi","%rdx","%ecx"); # Unix order
3509 .type bn_get_bits5,\@abi-omnipotent
3521 movzw (%r10,$num,2),%eax
3525 .size bn_get_bits5,.-bn_get_bits5
3528 .type bn_scatter5,\@abi-omnipotent
3532 jz .Lscatter_epilogue
3533 lea ($tbl,$idx,8),$tbl
3543 .size bn_scatter5,.-bn_scatter5
3546 .type bn_gather5,\@abi-omnipotent
3549 .LSEH_begin_bn_gather5: # Win64 thing, but harmless in other cases
3550 # I can't trust assembler to use specific encoding:-(
3551 .byte 0x4c,0x8d,0x14,0x24 #lea (%rsp),%r10
3552 .byte 0x48,0x81,0xec,0x08,0x01,0x00,0x00 #sub $0x108,%rsp
3553 lea .Linc(%rip),%rax
3554 and \$-16,%rsp # shouldn't be formally required
3557 movdqa 0(%rax),%xmm0 # 00000001000000010000000000000000
3558 movdqa 16(%rax),%xmm1 # 00000002000000020000000200000002
3559 lea 128($tbl),%r11 # size optimization
3560 lea 128(%rsp),%rax # size optimization
3562 pshufd \$0,%xmm5,%xmm5 # broadcast $idx
3566 ########################################################################
3567 # calculate mask by comparing 0..31 to $idx and save result to stack
3569 for($i=0;$i<$STRIDE/16;$i+=4) {
3572 pcmpeqd %xmm5,%xmm0 # compare to 1,0
3574 $code.=<<___ if ($i);
3575 movdqa %xmm3,`16*($i-1)-128`(%rax)
3581 pcmpeqd %xmm5,%xmm1 # compare to 3,2
3582 movdqa %xmm0,`16*($i+0)-128`(%rax)
3586 pcmpeqd %xmm5,%xmm2 # compare to 5,4
3587 movdqa %xmm1,`16*($i+1)-128`(%rax)
3591 pcmpeqd %xmm5,%xmm3 # compare to 7,6
3592 movdqa %xmm2,`16*($i+2)-128`(%rax)
3597 movdqa %xmm3,`16*($i-1)-128`(%rax)
3605 for($i=0;$i<$STRIDE/16;$i+=4) {
3607 movdqa `16*($i+0)-128`(%r11),%xmm0
3608 movdqa `16*($i+1)-128`(%r11),%xmm1
3609 movdqa `16*($i+2)-128`(%r11),%xmm2
3610 pand `16*($i+0)-128`(%rax),%xmm0
3611 movdqa `16*($i+3)-128`(%r11),%xmm3
3612 pand `16*($i+1)-128`(%rax),%xmm1
3614 pand `16*($i+2)-128`(%rax),%xmm2
3616 pand `16*($i+3)-128`(%rax),%xmm3
3623 lea $STRIDE(%r11),%r11
3624 pshufd \$0x4e,%xmm4,%xmm0
3626 movq %xmm0,($out) # m0=bp[0]
3633 .LSEH_end_bn_gather5:
3634 .size bn_gather5,.-bn_gather5
3642 .asciz "Montgomery Multiplication with scatter/gather for x86_64, CRYPTOGAMS by <appro\@openssl.org>"
3645 # EXCEPTION_DISPOSITION handler (EXCEPTION_RECORD *rec,ULONG64 frame,
3646 # CONTEXT *context,DISPATCHER_CONTEXT *disp)
3654 .extern __imp_RtlVirtualUnwind
3655 .type mul_handler,\@abi-omnipotent
3669 mov 120($context),%rax # pull context->Rax
3670 mov 248($context),%rbx # pull context->Rip
3672 mov 8($disp),%rsi # disp->ImageBase
3673 mov 56($disp),%r11 # disp->HandlerData
3675 mov 0(%r11),%r10d # HandlerData[0]
3676 lea (%rsi,%r10),%r10 # end of prologue label
3677 cmp %r10,%rbx # context->Rip<end of prologue label
3678 jb .Lcommon_seh_tail
3680 mov 4(%r11),%r10d # HandlerData[1]
3681 lea (%rsi,%r10),%r10 # epilogue label
3682 cmp %r10,%rbx # context->Rip>=epilogue label
3683 jb .Lcommon_pop_regs
3685 mov 152($context),%rax # pull context->Rsp
3687 mov 8(%r11),%r10d # HandlerData[2]
3688 lea (%rsi,%r10),%r10 # epilogue label
3689 cmp %r10,%rbx # context->Rip>=epilogue label
3690 jae .Lcommon_seh_tail
3692 lea .Lmul_epilogue(%rip),%r10
3696 mov 192($context),%r10 # pull $num
3697 mov 8(%rax,%r10,8),%rax # pull saved stack pointer
3699 jmp .Lcommon_pop_regs
3702 mov 40(%rax),%rax # pull saved stack pointer
3710 mov %rbx,144($context) # restore context->Rbx
3711 mov %rbp,160($context) # restore context->Rbp
3712 mov %r12,216($context) # restore context->R12
3713 mov %r13,224($context) # restore context->R13
3714 mov %r14,232($context) # restore context->R14
3715 mov %r15,240($context) # restore context->R15
3720 mov %rax,152($context) # restore context->Rsp
3721 mov %rsi,168($context) # restore context->Rsi
3722 mov %rdi,176($context) # restore context->Rdi
3724 mov 40($disp),%rdi # disp->ContextRecord
3725 mov $context,%rsi # context
3726 mov \$154,%ecx # sizeof(CONTEXT)
3727 .long 0xa548f3fc # cld; rep movsq
3730 xor %rcx,%rcx # arg1, UNW_FLAG_NHANDLER
3731 mov 8(%rsi),%rdx # arg2, disp->ImageBase
3732 mov 0(%rsi),%r8 # arg3, disp->ControlPc
3733 mov 16(%rsi),%r9 # arg4, disp->FunctionEntry
3734 mov 40(%rsi),%r10 # disp->ContextRecord
3735 lea 56(%rsi),%r11 # &disp->HandlerData
3736 lea 24(%rsi),%r12 # &disp->EstablisherFrame
3737 mov %r10,32(%rsp) # arg5
3738 mov %r11,40(%rsp) # arg6
3739 mov %r12,48(%rsp) # arg7
3740 mov %rcx,56(%rsp) # arg8, (NULL)
3741 call *__imp_RtlVirtualUnwind(%rip)
3743 mov \$1,%eax # ExceptionContinueSearch
3755 .size mul_handler,.-mul_handler
3759 .rva .LSEH_begin_bn_mul_mont_gather5
3760 .rva .LSEH_end_bn_mul_mont_gather5
3761 .rva .LSEH_info_bn_mul_mont_gather5
3763 .rva .LSEH_begin_bn_mul4x_mont_gather5
3764 .rva .LSEH_end_bn_mul4x_mont_gather5
3765 .rva .LSEH_info_bn_mul4x_mont_gather5
3767 .rva .LSEH_begin_bn_power5
3768 .rva .LSEH_end_bn_power5
3769 .rva .LSEH_info_bn_power5
3771 .rva .LSEH_begin_bn_from_mont8x
3772 .rva .LSEH_end_bn_from_mont8x
3773 .rva .LSEH_info_bn_from_mont8x
3775 $code.=<<___ if ($addx);
3776 .rva .LSEH_begin_bn_mulx4x_mont_gather5
3777 .rva .LSEH_end_bn_mulx4x_mont_gather5
3778 .rva .LSEH_info_bn_mulx4x_mont_gather5
3780 .rva .LSEH_begin_bn_powerx5
3781 .rva .LSEH_end_bn_powerx5
3782 .rva .LSEH_info_bn_powerx5
3785 .rva .LSEH_begin_bn_gather5
3786 .rva .LSEH_end_bn_gather5
3787 .rva .LSEH_info_bn_gather5
3791 .LSEH_info_bn_mul_mont_gather5:
3794 .rva .Lmul_body,.Lmul_body,.Lmul_epilogue # HandlerData[]
3796 .LSEH_info_bn_mul4x_mont_gather5:
3799 .rva .Lmul4x_prologue,.Lmul4x_body,.Lmul4x_epilogue # HandlerData[]
3801 .LSEH_info_bn_power5:
3804 .rva .Lpower5_prologue,.Lpower5_body,.Lpower5_epilogue # HandlerData[]
3806 .LSEH_info_bn_from_mont8x:
3809 .rva .Lfrom_prologue,.Lfrom_body,.Lfrom_epilogue # HandlerData[]
3811 $code.=<<___ if ($addx);
3813 .LSEH_info_bn_mulx4x_mont_gather5:
3816 .rva .Lmulx4x_prologue,.Lmulx4x_body,.Lmulx4x_epilogue # HandlerData[]
3818 .LSEH_info_bn_powerx5:
3821 .rva .Lpowerx5_prologue,.Lpowerx5_body,.Lpowerx5_epilogue # HandlerData[]
3825 .LSEH_info_bn_gather5:
3826 .byte 0x01,0x0b,0x03,0x0a
3827 .byte 0x0b,0x01,0x21,0x00 # sub rsp,0x108
3828 .byte 0x04,0xa3,0x00,0x00 # lea r10,(rsp)
3833 $code =~ s/\`([^\`]*)\`/eval($1)/gem;