2 # Copyright 2011-2018 The OpenSSL Project Authors. All Rights Reserved.
4 # Licensed under the Apache License 2.0 (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...
34 # $output is the last argument if it looks like a file (it has an extension)
35 # $flavour is the first argument if it doesn't look like a file
36 $output = $#ARGV >= 0 && $ARGV[$#ARGV] =~ m|\.\w+$| ? pop : undef;
37 $flavour = $#ARGV >= 0 && $ARGV[0] !~ m|\.| ? shift : undef;
39 $win64=0; $win64=1 if ($flavour =~ /[nm]asm|mingw64/ || $output =~ /\.asm$/);
41 $0 =~ m/(.*[\/\\])[^\/\\]+$/; $dir=$1;
42 ( $xlate="${dir}x86_64-xlate.pl" and -f $xlate ) or
43 ( $xlate="${dir}../../perlasm/x86_64-xlate.pl" and -f $xlate) or
44 die "can't locate x86_64-xlate.pl";
46 open OUT,"| \"$^X\" \"$xlate\" $flavour \"$output\""
47 or die "can't call $xlate: $!";
50 if (`$ENV{CC} -Wa,-v -c -o /dev/null -x assembler /dev/null 2>&1`
51 =~ /GNU assembler version ([2-9]\.[0-9]+)/) {
55 if (!$addx && $win64 && ($flavour =~ /nasm/ || $ENV{ASM} =~ /nasm/) &&
56 `nasm -v 2>&1` =~ /NASM version ([2-9]\.[0-9]+)/) {
60 if (!$addx && $win64 && ($flavour =~ /masm/ || $ENV{ASM} =~ /ml64/) &&
61 `ml64 2>&1` =~ /Version ([0-9]+)\./) {
65 if (!$addx && `$ENV{CC} -v 2>&1` =~ /((?:^clang|LLVM) version|.*based on LLVM) ([3-9])\.([0-9]+)/) {
66 my $ver = $2 + $3/100.0; # 3.1->3.01, 3.10->3.10
70 # int bn_mul_mont_gather5(
71 $rp="%rdi"; # BN_ULONG *rp,
72 $ap="%rsi"; # const BN_ULONG *ap,
73 $bp="%rdx"; # const BN_ULONG *bp,
74 $np="%rcx"; # const BN_ULONG *np,
75 $n0="%r8"; # const BN_ULONG *n0,
76 $num="%r9"; # int num,
77 # int idx); # 0 to 2^5-1, "index" in $bp holding
78 # pre-computed powers of a', interlaced
79 # in such manner that b[0] is $bp[idx],
80 # b[1] is [2^5+idx], etc.
92 .extern OPENSSL_ia32cap_P
94 .globl bn_mul_mont_gather5
95 .type bn_mul_mont_gather5,\@function,6
101 .cfi_def_cfa_register %rax
105 $code.=<<___ if ($addx);
106 mov OPENSSL_ia32cap_P+8(%rip),%r11d
113 movd `($win64?56:8)`(%rsp),%xmm5 # load 7th argument
129 lea -280(%rsp,$num,8),%r10 # future alloca(8*(num+2)+256+8)
130 neg $num # restore $num
131 and \$-1024,%r10 # minimize TLB usage
133 # An OS-agnostic version of __chkstk.
135 # Some OSes (Windows) insist on stack being "wired" to
136 # physical memory in strictly sequential manner, i.e. if stack
137 # allocation spans two pages, then reference to farmost one can
138 # be punishable by SEGV. But page walking can do good even on
139 # other OSes, because it guarantees that villain thread hits
140 # the guard page before it can make damage to innocent one...
147 jmp .Lmul_page_walk_done
154 .Lmul_page_walk_done:
157 mov %rax,8(%rsp,$num,8) # tp[num+1]=%rsp
158 .cfi_cfa_expression %rsp+8,$num,8,mul,plus,deref,+8
161 lea 128($bp),%r12 # reassign $bp (+size optimization)
164 $STRIDE=2**5*8; # 5 is "window size"
165 $N=$STRIDE/4; # should match cache line size
167 movdqa 0(%r10),%xmm0 # 00000001000000010000000000000000
168 movdqa 16(%r10),%xmm1 # 00000002000000020000000200000002
169 lea 24-112(%rsp,$num,8),%r10# place the mask after tp[num+3] (+ICache optimization)
172 pshufd \$0,%xmm5,%xmm5 # broadcast index
176 ########################################################################
177 # calculate mask by comparing 0..31 to index and save result to stack
181 pcmpeqd %xmm5,%xmm0 # compare to 1,0
185 for($k=0;$k<$STRIDE/16-4;$k+=4) {
188 pcmpeqd %xmm5,%xmm1 # compare to 3,2
189 movdqa %xmm0,`16*($k+0)+112`(%r10)
193 pcmpeqd %xmm5,%xmm2 # compare to 5,4
194 movdqa %xmm1,`16*($k+1)+112`(%r10)
198 pcmpeqd %xmm5,%xmm3 # compare to 7,6
199 movdqa %xmm2,`16*($k+2)+112`(%r10)
204 movdqa %xmm3,`16*($k+3)+112`(%r10)
208 $code.=<<___; # last iteration can be optimized
211 movdqa %xmm0,`16*($k+0)+112`(%r10)
216 movdqa %xmm1,`16*($k+1)+112`(%r10)
219 movdqa %xmm2,`16*($k+2)+112`(%r10)
220 pand `16*($k+0)-128`($bp),%xmm0 # while it's still in register
222 pand `16*($k+1)-128`($bp),%xmm1
223 pand `16*($k+2)-128`($bp),%xmm2
224 movdqa %xmm3,`16*($k+3)+112`(%r10)
225 pand `16*($k+3)-128`($bp),%xmm3
229 for($k=0;$k<$STRIDE/16-4;$k+=4) {
231 movdqa `16*($k+0)-128`($bp),%xmm4
232 movdqa `16*($k+1)-128`($bp),%xmm5
233 movdqa `16*($k+2)-128`($bp),%xmm2
234 pand `16*($k+0)+112`(%r10),%xmm4
235 movdqa `16*($k+3)-128`($bp),%xmm3
236 pand `16*($k+1)+112`(%r10),%xmm5
238 pand `16*($k+2)+112`(%r10),%xmm2
240 pand `16*($k+3)+112`(%r10),%xmm3
247 pshufd \$0x4e,%xmm0,%xmm1
250 movq %xmm0,$m0 # m0=bp[0]
252 mov ($n0),$n0 # pull n0[0] value
259 mulq $m0 # ap[0]*bp[0]
263 imulq $lo0,$m1 # "tp[0]"*n0
267 add %rax,$lo0 # discarded
280 add $hi0,$hi1 # np[j]*m1+ap[j]*bp[0]
283 mov $hi1,-16(%rsp,$j,8) # tp[j-1]
287 mulq $m0 # ap[j]*bp[0]
296 jne .L1st # note that upon exit $j==$num, so
297 # they can be used interchangeably
301 add $hi0,$hi1 # np[j]*m1+ap[j]*bp[0]
303 mov $hi1,-16(%rsp,$num,8) # tp[num-1]
310 mov $hi1,-8(%rsp,$num,8)
311 mov %rdx,(%rsp,$num,8) # store upmost overflow bit
317 lea 24+128(%rsp,$num,8),%rdx # where 256-byte mask is (+size optimization)
322 for($k=0;$k<$STRIDE/16;$k+=4) {
324 movdqa `16*($k+0)-128`($bp),%xmm0
325 movdqa `16*($k+1)-128`($bp),%xmm1
326 movdqa `16*($k+2)-128`($bp),%xmm2
327 movdqa `16*($k+3)-128`($bp),%xmm3
328 pand `16*($k+0)-128`(%rdx),%xmm0
329 pand `16*($k+1)-128`(%rdx),%xmm1
331 pand `16*($k+2)-128`(%rdx),%xmm2
333 pand `16*($k+3)-128`(%rdx),%xmm3
340 pshufd \$0x4e,%xmm4,%xmm0
344 mov ($ap),%rax # ap[0]
345 movq %xmm0,$m0 # m0=bp[i]
351 mulq $m0 # ap[0]*bp[i]
352 add %rax,$lo0 # ap[0]*bp[i]+tp[0]
356 imulq $lo0,$m1 # tp[0]*n0
360 add %rax,$lo0 # discarded
363 mov 8(%rsp),$lo0 # tp[1]
374 add $lo0,$hi1 # np[j]*m1+ap[j]*bp[i]+tp[j]
377 mov $hi1,-16(%rsp,$j,8) # tp[j-1]
381 mulq $m0 # ap[j]*bp[i]
385 add $hi0,$lo0 # ap[j]*bp[i]+tp[j]
392 jne .Linner # note that upon exit $j==$num, so
393 # they can be used interchangeably
396 add $lo0,$hi1 # np[j]*m1+ap[j]*bp[i]+tp[j]
397 mov (%rsp,$num,8),$lo0
399 mov $hi1,-16(%rsp,$num,8) # tp[num-1]
405 add $lo0,$hi1 # pull upmost overflow bit
407 mov $hi1,-8(%rsp,$num,8)
408 mov %rdx,(%rsp,$num,8) # store upmost overflow bit
414 xor $i,$i # i=0 and clear CF!
415 mov (%rsp),%rax # tp[0]
416 lea (%rsp),$ap # borrow ap for tp
420 .Lsub: sbb ($np,$i,8),%rax
421 mov %rax,($rp,$i,8) # rp[i]=tp[i]-np[i]
422 mov 8($ap,$i,8),%rax # tp[i+1]
424 dec $j # doesn't affect CF!
427 sbb \$0,%rax # handle upmost overflow bit
433 .Lcopy: # conditional copy
438 mov $i,(%rsp,$i,8) # zap temporary vector
440 mov %rdx,($rp,$i,8) # rp[i]=tp[i]
445 mov 8(%rsp,$num,8),%rsi # restore %rsp
462 .cfi_def_cfa_register %rsp
466 .size bn_mul_mont_gather5,.-bn_mul_mont_gather5
469 my @A=("%r10","%r11");
470 my @N=("%r13","%rdi");
472 .type bn_mul4x_mont_gather5,\@function,6
474 bn_mul4x_mont_gather5:
478 .cfi_def_cfa_register %rax
481 $code.=<<___ if ($addx);
483 cmp \$0x80108,%r11d # check for AD*X+BMI2+BMI1
502 shl \$3,${num}d # convert $num to bytes
503 lea ($num,$num,2),%r10 # 3*$num in bytes
506 ##############################################################
507 # Ensure that stack frame doesn't alias with $rptr+3*$num
508 # modulo 4096, which covers ret[num], am[num] and n[num]
509 # (see bn_exp.c). This is done to allow memory disambiguation
510 # logic do its magic. [Extra [num] is allocated in order
511 # to align with bn_power5's frame, which is cleansed after
512 # completing exponentiation. Extra 256 bytes is for power mask
513 # calculated from 7th argument, the index.]
515 lea -320(%rsp,$num,2),%r11
521 sub %r11,%rbp # align with $rp
522 lea -320(%rbp,$num,2),%rbp # future alloca(frame+2*num*8+256)
527 lea 4096-320(,$num,2),%r10
528 lea -320(%rbp,$num,2),%rbp # future alloca(frame+2*num*8+256)
542 jmp .Lmul4x_page_walk_done
549 .Lmul4x_page_walk_done:
554 .cfi_cfa_expression %rsp+40,deref,+8
559 mov 40(%rsp),%rsi # restore %rsp
576 .cfi_def_cfa_register %rsp
580 .size bn_mul4x_mont_gather5,.-bn_mul4x_mont_gather5
582 .type mul4x_internal,\@abi-omnipotent
585 shl \$5,$num # $num was in bytes
586 movd `($win64?56:8)`(%rax),%xmm5 # load 7th argument, index
588 lea 128(%rdx,$num),%r13 # end of powers table (+size optimization)
589 shr \$5,$num # restore $num
592 $STRIDE=2**5*8; # 5 is "window size"
593 $N=$STRIDE/4; # should match cache line size
596 movdqa 0(%rax),%xmm0 # 00000001000000010000000000000000
597 movdqa 16(%rax),%xmm1 # 00000002000000020000000200000002
598 lea 88-112(%rsp,$num),%r10 # place the mask after tp[num+1] (+ICache optimization)
599 lea 128(%rdx),$bp # size optimization
601 pshufd \$0,%xmm5,%xmm5 # broadcast index
606 ########################################################################
607 # calculate mask by comparing 0..31 to index and save result to stack
611 pcmpeqd %xmm5,%xmm0 # compare to 1,0
615 for($i=0;$i<$STRIDE/16-4;$i+=4) {
618 pcmpeqd %xmm5,%xmm1 # compare to 3,2
619 movdqa %xmm0,`16*($i+0)+112`(%r10)
623 pcmpeqd %xmm5,%xmm2 # compare to 5,4
624 movdqa %xmm1,`16*($i+1)+112`(%r10)
628 pcmpeqd %xmm5,%xmm3 # compare to 7,6
629 movdqa %xmm2,`16*($i+2)+112`(%r10)
634 movdqa %xmm3,`16*($i+3)+112`(%r10)
638 $code.=<<___; # last iteration can be optimized
641 movdqa %xmm0,`16*($i+0)+112`(%r10)
646 movdqa %xmm1,`16*($i+1)+112`(%r10)
649 movdqa %xmm2,`16*($i+2)+112`(%r10)
650 pand `16*($i+0)-128`($bp),%xmm0 # while it's still in register
652 pand `16*($i+1)-128`($bp),%xmm1
653 pand `16*($i+2)-128`($bp),%xmm2
654 movdqa %xmm3,`16*($i+3)+112`(%r10)
655 pand `16*($i+3)-128`($bp),%xmm3
659 for($i=0;$i<$STRIDE/16-4;$i+=4) {
661 movdqa `16*($i+0)-128`($bp),%xmm4
662 movdqa `16*($i+1)-128`($bp),%xmm5
663 movdqa `16*($i+2)-128`($bp),%xmm2
664 pand `16*($i+0)+112`(%r10),%xmm4
665 movdqa `16*($i+3)-128`($bp),%xmm3
666 pand `16*($i+1)+112`(%r10),%xmm5
668 pand `16*($i+2)+112`(%r10),%xmm2
670 pand `16*($i+3)+112`(%r10),%xmm3
677 pshufd \$0x4e,%xmm0,%xmm1
680 movq %xmm0,$m0 # m0=bp[0]
682 mov %r13,16+8(%rsp) # save end of b[num]
683 mov $rp, 56+8(%rsp) # save $rp
685 mov ($n0),$n0 # pull n0[0] value
687 lea ($ap,$num),$ap # end of a[num]
691 mulq $m0 # ap[0]*bp[0]
695 imulq $A[0],$m1 # "tp[0]"*n0
700 add %rax,$A[0] # discarded
713 mov 16($ap,$num),%rax
716 lea 4*8($num),$j # j=4
725 mulq $m0 # ap[j]*bp[0]
736 add $A[0],$N[0] # np[j]*m1+ap[j]*bp[0]
738 mov $N[0],-24($tp) # tp[j-1]
741 mulq $m0 # ap[j]*bp[0]
751 add $A[1],$N[1] # np[j]*m1+ap[j]*bp[0]
753 mov $N[1],-16($tp) # tp[j-1]
756 mulq $m0 # ap[j]*bp[0]
766 add $A[0],$N[0] # np[j]*m1+ap[j]*bp[0]
768 mov $N[0],-8($tp) # tp[j-1]
771 mulq $m0 # ap[j]*bp[0]
781 add $A[1],$N[1] # np[j]*m1+ap[j]*bp[0]
784 mov $N[1],($tp) # tp[j-1]
790 mulq $m0 # ap[j]*bp[0]
801 add $A[0],$N[0] # np[j]*m1+ap[j]*bp[0]
803 mov $N[0],-24($tp) # tp[j-1]
806 mulq $m0 # ap[j]*bp[0]
814 mov ($ap,$num),%rax # ap[0]
816 add $A[1],$N[1] # np[j]*m1+ap[j]*bp[0]
818 mov $N[1],-16($tp) # tp[j-1]
821 lea ($np,$num),$np # rewind $np
832 lea 16+128($tp),%rdx # where 256-byte mask is (+size optimization)
836 for($i=0;$i<$STRIDE/16;$i+=4) {
838 movdqa `16*($i+0)-128`($bp),%xmm0
839 movdqa `16*($i+1)-128`($bp),%xmm1
840 movdqa `16*($i+2)-128`($bp),%xmm2
841 movdqa `16*($i+3)-128`($bp),%xmm3
842 pand `16*($i+0)-128`(%rdx),%xmm0
843 pand `16*($i+1)-128`(%rdx),%xmm1
845 pand `16*($i+2)-128`(%rdx),%xmm2
847 pand `16*($i+3)-128`(%rdx),%xmm3
854 pshufd \$0x4e,%xmm4,%xmm0
857 movq %xmm0,$m0 # m0=bp[i]
861 mulq $m0 # ap[0]*bp[i]
862 add %rax,$A[0] # ap[0]*bp[i]+tp[0]
866 imulq $A[0],$m1 # tp[0]*n0
868 mov $N[1],($tp) # store upmost overflow bit
870 lea ($tp,$num),$tp # rewind $tp
873 add %rax,$A[0] # "$N[0]", discarded
878 mulq $m0 # ap[j]*bp[i]
882 add 8($tp),$A[1] # +tp[1]
888 mov 16($ap,$num),%rax
890 add $A[1],$N[1] # np[j]*m1+ap[j]*bp[i]+tp[j]
891 lea 4*8($num),$j # j=4
899 mulq $m0 # ap[j]*bp[i]
903 add 16($tp),$A[0] # ap[j]*bp[i]+tp[j]
914 mov $N[1],-32($tp) # tp[j-1]
917 mulq $m0 # ap[j]*bp[i]
931 mov $N[0],-24($tp) # tp[j-1]
934 mulq $m0 # ap[j]*bp[i]
938 add ($tp),$A[0] # ap[j]*bp[i]+tp[j]
948 mov $N[1],-16($tp) # tp[j-1]
951 mulq $m0 # ap[j]*bp[i]
966 mov $N[0],-8($tp) # tp[j-1]
972 mulq $m0 # ap[j]*bp[i]
976 add 16($tp),$A[0] # ap[j]*bp[i]+tp[j]
987 mov $N[1],-32($tp) # tp[j-1]
990 mulq $m0 # ap[j]*bp[i]
1001 mov ($ap,$num),%rax # ap[0]
1005 mov $N[0],-24($tp) # tp[j-1]
1008 mov $N[1],-16($tp) # tp[j-1]
1009 lea ($np,$num),$np # rewind $np
1014 add ($tp),$N[0] # pull upmost overflow bit
1015 adc \$0,$N[1] # upmost overflow bit
1024 sub $N[0],$m1 # compare top-most words
1025 adc $j,$j # $j is zero
1027 sub $N[1],%rax # %rax=-$N[1]
1028 lea ($tp,$num),%rbx # tptr in .sqr4x_sub
1030 lea ($np),%rbp # nptr in .sqr4x_sub
1033 mov 56+8(%rsp),%rdi # rptr in .sqr4x_sub
1034 dec %r12 # so that after 'not' we get -n[0]
1039 jmp .Lsqr4x_sub_entry
1042 my @ri=("%rax",$bp,$m0,$m1);
1046 lea ($tp,$num),$tp # rewind $tp
1048 lea ($np,$N[1],8),$np
1049 mov 56+8(%rsp),$rp # restore $rp
1058 sbb 16*0($np),@ri[0]
1060 sbb 16*1($np),@ri[1]
1063 sbb 16*2($np),@ri[2]
1065 sbb 16*3($np),@ri[3]
1079 .size mul4x_internal,.-mul4x_internal
1083 ######################################################################
1085 my $rptr="%rdi"; # BN_ULONG *rptr,
1086 my $aptr="%rsi"; # const BN_ULONG *aptr,
1087 my $bptr="%rdx"; # const void *table,
1088 my $nptr="%rcx"; # const BN_ULONG *nptr,
1089 my $n0 ="%r8"; # const BN_ULONG *n0);
1090 my $num ="%r9"; # int num, has to be divisible by 8
1093 my ($i,$j,$tptr)=("%rbp","%rcx",$rptr);
1094 my @A0=("%r10","%r11");
1095 my @A1=("%r12","%r13");
1096 my ($a0,$a1,$ai)=("%r14","%r15","%rbx");
1100 .type bn_power5,\@function,6
1105 .cfi_def_cfa_register %rax
1107 $code.=<<___ if ($addx);
1108 mov OPENSSL_ia32cap_P+8(%rip),%r11d
1110 cmp \$0x80108,%r11d # check for AD*X+BMI2+BMI1
1128 shl \$3,${num}d # convert $num to bytes
1129 lea ($num,$num,2),%r10d # 3*$num
1133 ##############################################################
1134 # Ensure that stack frame doesn't alias with $rptr+3*$num
1135 # modulo 4096, which covers ret[num], am[num] and n[num]
1136 # (see bn_exp.c). This is done to allow memory disambiguation
1137 # logic do its magic. [Extra 256 bytes is for power mask
1138 # calculated from 7th argument, the index.]
1140 lea -320(%rsp,$num,2),%r11
1146 sub %r11,%rbp # align with $aptr
1147 lea -320(%rbp,$num,2),%rbp # future alloca(frame+2*num*8+256)
1152 lea 4096-320(,$num,2),%r10
1153 lea -320(%rbp,$num,2),%rbp # future alloca(frame+2*num*8+256)
1163 lea (%rbp,%r11),%rsp
1167 jmp .Lpwr_page_walk_done
1170 lea -4096(%rsp),%rsp
1174 .Lpwr_page_walk_done:
1179 ##############################################################
1182 # +0 saved $num, used in reduction section
1183 # +8 &t[2*$num], used in reduction section
1189 mov %rax, 40(%rsp) # save original %rsp
1190 .cfi_cfa_expression %rsp+40,deref,+8
1192 movq $rptr,%xmm1 # save $rptr, used in sqr8x
1193 movq $nptr,%xmm2 # save $nptr
1194 movq %r10, %xmm3 # -$num, used in sqr8x
1197 call __bn_sqr8x_internal
1198 call __bn_post4x_internal
1199 call __bn_sqr8x_internal
1200 call __bn_post4x_internal
1201 call __bn_sqr8x_internal
1202 call __bn_post4x_internal
1203 call __bn_sqr8x_internal
1204 call __bn_post4x_internal
1205 call __bn_sqr8x_internal
1206 call __bn_post4x_internal
1216 mov 40(%rsp),%rsi # restore %rsp
1232 .cfi_def_cfa_register %rsp
1236 .size bn_power5,.-bn_power5
1238 .globl bn_sqr8x_internal
1239 .hidden bn_sqr8x_internal
1240 .type bn_sqr8x_internal,\@abi-omnipotent
1243 __bn_sqr8x_internal:
1244 ##############################################################
1247 # a) multiply-n-add everything but a[i]*a[i];
1248 # b) shift result of a) by 1 to the left and accumulate
1249 # a[i]*a[i] products;
1251 ##############################################################
1317 lea 32(%r10),$i # $i=-($num-32)
1318 lea ($aptr,$num),$aptr # end of a[] buffer, ($aptr,$i)=&ap[2]
1320 mov $num,$j # $j=$num
1322 # comments apply to $num==8 case
1323 mov -32($aptr,$i),$a0 # a[0]
1324 lea 48+8(%rsp,$num,2),$tptr # end of tp[] buffer, &tp[2*$num]
1325 mov -24($aptr,$i),%rax # a[1]
1326 lea -32($tptr,$i),$tptr # end of tp[] window, &tp[2*$num-"$i"]
1327 mov -16($aptr,$i),$ai # a[2]
1331 mov %rax,$A0[0] # a[1]*a[0]
1334 mov $A0[0],-24($tptr,$i) # t[1]
1340 mov $A0[1],-16($tptr,$i) # t[2]
1344 mov -8($aptr,$i),$ai # a[3]
1346 mov %rax,$A1[0] # a[2]*a[1]+t[3]
1352 add %rax,$A0[0] # a[3]*a[0]+a[2]*a[1]+t[3]
1358 mov $A0[0],-8($tptr,$j) # t[3]
1363 mov ($aptr,$j),$ai # a[4]
1365 add %rax,$A1[1] # a[3]*a[1]+t[4]
1371 add %rax,$A0[1] # a[4]*a[0]+a[3]*a[1]+t[4]
1373 mov 8($aptr,$j),$ai # a[5]
1381 add %rax,$A1[0] # a[4]*a[3]+t[5]
1383 mov $A0[1],($tptr,$j) # t[4]
1388 add %rax,$A0[0] # a[5]*a[2]+a[4]*a[3]+t[5]
1390 mov 16($aptr,$j),$ai # a[6]
1397 add %rax,$A1[1] # a[5]*a[3]+t[6]
1399 mov $A0[0],8($tptr,$j) # t[5]
1404 add %rax,$A0[1] # a[6]*a[2]+a[5]*a[3]+t[6]
1406 mov 24($aptr,$j),$ai # a[7]
1414 add %rax,$A1[0] # a[6]*a[5]+t[7]
1416 mov $A0[1],16($tptr,$j) # t[6]
1422 add %rax,$A0[0] # a[7]*a[4]+a[6]*a[5]+t[6]
1428 mov $A0[0],-8($tptr,$j) # t[7]
1440 mov $A1[1],($tptr) # t[8]
1442 mov %rdx,8($tptr) # t[9]
1446 .Lsqr4x_outer: # comments apply to $num==6 case
1447 mov -32($aptr,$i),$a0 # a[0]
1448 lea 48+8(%rsp,$num,2),$tptr # end of tp[] buffer, &tp[2*$num]
1449 mov -24($aptr,$i),%rax # a[1]
1450 lea -32($tptr,$i),$tptr # end of tp[] window, &tp[2*$num-"$i"]
1451 mov -16($aptr,$i),$ai # a[2]
1455 mov -24($tptr,$i),$A0[0] # t[1]
1456 add %rax,$A0[0] # a[1]*a[0]+t[1]
1459 mov $A0[0],-24($tptr,$i) # t[1]
1466 add -16($tptr,$i),$A0[1] # a[2]*a[0]+t[2]
1469 mov $A0[1],-16($tptr,$i) # t[2]
1473 mov -8($aptr,$i),$ai # a[3]
1475 add %rax,$A1[0] # a[2]*a[1]+t[3]
1478 add -8($tptr,$i),$A1[0]
1483 add %rax,$A0[0] # a[3]*a[0]+a[2]*a[1]+t[3]
1489 mov $A0[0],-8($tptr,$i) # t[3]
1496 mov ($aptr,$j),$ai # a[4]
1498 add %rax,$A1[1] # a[3]*a[1]+t[4]
1502 add ($tptr,$j),$A1[1]
1507 add %rax,$A0[1] # a[4]*a[0]+a[3]*a[1]+t[4]
1509 mov 8($aptr,$j),$ai # a[5]
1516 add %rax,$A1[0] # a[4]*a[3]+t[5]
1517 mov $A0[1],($tptr,$j) # t[4]
1521 add 8($tptr,$j),$A1[0]
1526 add %rax,$A0[0] # a[5]*a[2]+a[4]*a[3]+t[5]
1532 mov $A0[0],-8($tptr,$j) # t[5], "preloaded t[1]" below
1544 mov $A1[1],($tptr) # t[6], "preloaded t[2]" below
1546 mov %rdx,8($tptr) # t[7], "preloaded t[3]" below
1551 # comments apply to $num==4 case
1552 mov -32($aptr),$a0 # a[0]
1553 lea 48+8(%rsp,$num,2),$tptr # end of tp[] buffer, &tp[2*$num]
1554 mov -24($aptr),%rax # a[1]
1555 lea -32($tptr,$i),$tptr # end of tp[] window, &tp[2*$num-"$i"]
1556 mov -16($aptr),$ai # a[2]
1560 add %rax,$A0[0] # a[1]*a[0]+t[1], preloaded t[1]
1568 mov $A0[0],-24($tptr) # t[1]
1571 add $A1[1],$A0[1] # a[2]*a[0]+t[2], preloaded t[2]
1572 mov -8($aptr),$ai # a[3]
1576 add %rax,$A1[0] # a[2]*a[1]+t[3], preloaded t[3]
1578 mov $A0[1],-16($tptr) # t[2]
1583 add %rax,$A0[0] # a[3]*a[0]+a[2]*a[1]+t[3]
1589 mov $A0[0],-8($tptr) # t[3]
1593 mov -16($aptr),%rax # a[2]
1598 mov $A1[1],($tptr) # t[4]
1600 mov %rdx,8($tptr) # t[5]
1605 my ($shift,$carry)=($a0,$a1);
1606 my @S=(@A1,$ai,$n0);
1610 sub $num,$i # $i=16-$num
1613 add $A1[0],%rax # t[5]
1615 mov %rax,8($tptr) # t[5]
1616 mov %rdx,16($tptr) # t[6]
1617 mov $carry,24($tptr) # t[7]
1619 mov -16($aptr,$i),%rax # a[0]
1620 lea 48+8(%rsp),$tptr
1621 xor $A0[0],$A0[0] # t[0]
1622 mov 8($tptr),$A0[1] # t[1]
1624 lea ($shift,$A0[0],2),$S[0] # t[2*i]<<1 | shift
1626 lea ($j,$A0[1],2),$S[1] # t[2*i+1]<<1 |
1628 or $A0[0],$S[1] # | t[2*i]>>63
1629 mov 16($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 24($tptr),$A0[1] # t[2*i+2+1] # prefetch
1635 mov -8($aptr,$i),%rax # a[i+1] # prefetch
1639 lea ($shift,$A0[0],2),$S[2] # t[2*i]<<1 | shift
1641 sbb $carry,$carry # mov cf,$carry
1643 lea ($j,$A0[1],2),$S[3] # t[2*i+1]<<1 |
1645 or $A0[0],$S[3] # | t[2*i]>>63
1646 mov 32($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 40($tptr),$A0[1] # t[2*i+2+1] # prefetch
1652 mov 0($aptr,$i),%rax # a[i+1] # prefetch
1657 sbb $carry,$carry # mov cf,$carry
1659 jmp .Lsqr4x_shift_n_add
1662 .Lsqr4x_shift_n_add:
1663 lea ($shift,$A0[0],2),$S[0] # t[2*i]<<1 | shift
1665 lea ($j,$A0[1],2),$S[1] # t[2*i+1]<<1 |
1667 or $A0[0],$S[1] # | t[2*i]>>63
1668 mov -16($tptr),$A0[0] # t[2*i+2] # prefetch
1669 mov $A0[1],$shift # shift=t[2*i+1]>>63
1670 mul %rax # a[i]*a[i]
1671 neg $carry # mov $carry,cf
1672 mov -8($tptr),$A0[1] # t[2*i+2+1] # prefetch
1674 mov -8($aptr,$i),%rax # a[i+1] # prefetch
1675 mov $S[0],-32($tptr)
1678 lea ($shift,$A0[0],2),$S[2] # t[2*i]<<1 | shift
1679 mov $S[1],-24($tptr)
1680 sbb $carry,$carry # mov cf,$carry
1682 lea ($j,$A0[1],2),$S[3] # t[2*i+1]<<1 |
1684 or $A0[0],$S[3] # | t[2*i]>>63
1685 mov 0($tptr),$A0[0] # t[2*i+2] # prefetch
1686 mov $A0[1],$shift # shift=t[2*i+1]>>63
1687 mul %rax # a[i]*a[i]
1688 neg $carry # mov $carry,cf
1689 mov 8($tptr),$A0[1] # t[2*i+2+1] # prefetch
1691 mov 0($aptr,$i),%rax # a[i+1] # prefetch
1692 mov $S[2],-16($tptr)
1695 lea ($shift,$A0[0],2),$S[0] # t[2*i]<<1 | shift
1697 sbb $carry,$carry # mov cf,$carry
1699 lea ($j,$A0[1],2),$S[1] # t[2*i+1]<<1 |
1701 or $A0[0],$S[1] # | t[2*i]>>63
1702 mov 16($tptr),$A0[0] # t[2*i+2] # prefetch
1703 mov $A0[1],$shift # shift=t[2*i+1]>>63
1704 mul %rax # a[i]*a[i]
1705 neg $carry # mov $carry,cf
1706 mov 24($tptr),$A0[1] # t[2*i+2+1] # prefetch
1708 mov 8($aptr,$i),%rax # a[i+1] # prefetch
1712 lea ($shift,$A0[0],2),$S[2] # t[2*i]<<1 | shift
1714 sbb $carry,$carry # mov cf,$carry
1716 lea ($j,$A0[1],2),$S[3] # t[2*i+1]<<1 |
1718 or $A0[0],$S[3] # | t[2*i]>>63
1719 mov 32($tptr),$A0[0] # t[2*i+2] # prefetch
1720 mov $A0[1],$shift # shift=t[2*i+1]>>63
1721 mul %rax # a[i]*a[i]
1722 neg $carry # mov $carry,cf
1723 mov 40($tptr),$A0[1] # t[2*i+2+1] # prefetch
1725 mov 16($aptr,$i),%rax # a[i+1] # prefetch
1729 sbb $carry,$carry # mov cf,$carry
1732 jnz .Lsqr4x_shift_n_add
1734 lea ($shift,$A0[0],2),$S[0] # t[2*i]<<1 | shift
1737 lea ($j,$A0[1],2),$S[1] # t[2*i+1]<<1 |
1739 or $A0[0],$S[1] # | t[2*i]>>63
1740 mov -16($tptr),$A0[0] # t[2*i+2] # prefetch
1741 mov $A0[1],$shift # shift=t[2*i+1]>>63
1742 mul %rax # a[i]*a[i]
1743 neg $carry # mov $carry,cf
1744 mov -8($tptr),$A0[1] # t[2*i+2+1] # prefetch
1746 mov -8($aptr),%rax # a[i+1] # prefetch
1747 mov $S[0],-32($tptr)
1750 lea ($shift,$A0[0],2),$S[2] # t[2*i]<<1|shift
1751 mov $S[1],-24($tptr)
1752 sbb $carry,$carry # mov cf,$carry
1754 lea ($j,$A0[1],2),$S[3] # t[2*i+1]<<1 |
1756 or $A0[0],$S[3] # | t[2*i]>>63
1757 mul %rax # a[i]*a[i]
1758 neg $carry # mov $carry,cf
1761 mov $S[2],-16($tptr)
1765 ######################################################################
1766 # Montgomery reduction part, "word-by-word" algorithm.
1768 # This new path is inspired by multiple submissions from Intel, by
1769 # Shay Gueron, Vlad Krasnov, Erdinc Ozturk, James Guilford,
1772 my ($nptr,$tptr,$carry,$m0)=("%rbp","%rdi","%rsi","%rbx");
1776 __bn_sqr8x_reduction:
1778 lea ($nptr,$num),%rcx # end of n[]
1779 lea 48+8(%rsp,$num,2),%rdx # end of t[] buffer
1781 lea 48+8(%rsp,$num),$tptr # end of initial t[] window
1784 jmp .L8x_reduction_loop
1787 .L8x_reduction_loop:
1788 lea ($tptr,$num),$tptr # start of current t[] window
1798 mov %rax,(%rdx) # store top-most carry bit
1799 lea 8*8($tptr),$tptr
1803 imulq 32+8(%rsp),$m0 # n0*a[0]
1804 mov 8*0($nptr),%rax # n[0]
1811 mov 8*1($nptr),%rax # n[1]
1821 mov $m0,48-8+8(%rsp,%rcx,8) # put aside n0*a[i]
1830 mov 32+8(%rsp),$carry # pull n0, borrow $carry
1838 imulq %r8,$carry # modulo-scheduled
1868 mov $carry,$m0 # n0*a[i]
1870 mov 8*0($nptr),%rax # n[0]
1879 lea 8*8($nptr),$nptr
1881 mov 8+8(%rsp),%rdx # pull end of t[]
1882 cmp 0+8(%rsp),$nptr # end of n[]?
1894 sbb $carry,$carry # top carry
1896 mov 48+56+8(%rsp),$m0 # pull n0*a[0]
1906 mov %r8,($tptr) # save result
1915 lea 8($tptr),$tptr # $tptr++
1960 mov 48-16+8(%rsp,%rcx,8),$m0# pull n0*a[i]
1964 mov 8*0($nptr),%rax # pull n[0]
1971 lea 8*8($nptr),$nptr
1972 mov 8+8(%rsp),%rdx # pull end of t[]
1973 cmp 0+8(%rsp),$nptr # end of n[]?
1974 jae .L8x_tail_done # break out of loop
1976 mov 48+56+8(%rsp),$m0 # pull n0*a[0]
1978 mov 8*0($nptr),%rax # pull n[0]
1987 sbb $carry,$carry # top carry
1995 add (%rdx),%r8 # can this overflow?
2015 adc \$0,%rax # top-most carry
2016 mov -8($nptr),%rcx # np[num-1]
2019 movq %xmm2,$nptr # restore $nptr
2021 mov %r8,8*0($tptr) # store top 512 bits
2023 movq %xmm3,$num # $num is %r9, can't be moved upwards
2030 lea 8*8($tptr),$tptr
2032 cmp %rdx,$tptr # end of t[]?
2033 jb .L8x_reduction_loop
2035 .size bn_sqr8x_internal,.-bn_sqr8x_internal
2038 ##############################################################
2039 # Post-condition, 4x unrolled
2042 my ($tptr,$nptr)=("%rbx","%rbp");
2044 .type __bn_post4x_internal,\@abi-omnipotent
2046 __bn_post4x_internal:
2048 lea (%rdi,$num),$tptr # %rdi was $tptr above
2050 movq %xmm1,$rptr # restore $rptr
2052 movq %xmm1,$aptr # prepare for back-to-back call
2054 dec %r12 # so that after 'not' we get -n[0]
2059 jmp .Lsqr4x_sub_entry
2068 lea 8*4($nptr),$nptr
2078 neg %r10 # mov %r10,%cf
2084 lea 8*4($tptr),$tptr
2086 sbb %r10,%r10 # mov %cf,%r10
2089 lea 8*4($rptr),$rptr
2094 mov $num,%r10 # prepare for back-to-back call
2095 neg $num # restore $num
2097 .size __bn_post4x_internal,.-__bn_post4x_internal
2102 .globl bn_from_montgomery
2103 .type bn_from_montgomery,\@abi-omnipotent
2106 testl \$7,`($win64?"48(%rsp)":"%r9d")`
2110 .size bn_from_montgomery,.-bn_from_montgomery
2112 .type bn_from_mont8x,\@function,6
2118 .cfi_def_cfa_register %rax
2133 shl \$3,${num}d # convert $num to bytes
2134 lea ($num,$num,2),%r10 # 3*$num in bytes
2138 ##############################################################
2139 # Ensure that stack frame doesn't alias with $rptr+3*$num
2140 # modulo 4096, which covers ret[num], am[num] and n[num]
2141 # (see bn_exp.c). The stack is allocated to aligned with
2142 # bn_power5's frame, and as bn_from_montgomery happens to be
2143 # last operation, we use the opportunity to cleanse it.
2145 lea -320(%rsp,$num,2),%r11
2151 sub %r11,%rbp # align with $aptr
2152 lea -320(%rbp,$num,2),%rbp # future alloca(frame+2*$num*8+256)
2157 lea 4096-320(,$num,2),%r10
2158 lea -320(%rbp,$num,2),%rbp # future alloca(frame+2*$num*8+256)
2168 lea (%rbp,%r11),%rsp
2172 jmp .Lfrom_page_walk_done
2175 lea -4096(%rsp),%rsp
2179 .Lfrom_page_walk_done:
2184 ##############################################################
2187 # +0 saved $num, used in reduction section
2188 # +8 &t[2*$num], used in reduction section
2194 mov %rax, 40(%rsp) # save original %rsp
2195 .cfi_cfa_expression %rsp+40,deref,+8
2204 movdqu ($aptr),%xmm1
2205 movdqu 16($aptr),%xmm2
2206 movdqu 32($aptr),%xmm3
2207 movdqa %xmm0,(%rax,$num)
2208 movdqu 48($aptr),%xmm4
2209 movdqa %xmm0,16(%rax,$num)
2210 .byte 0x48,0x8d,0xb6,0x40,0x00,0x00,0x00 # lea 64($aptr),$aptr
2212 movdqa %xmm0,32(%rax,$num)
2213 movdqa %xmm2,16(%rax)
2214 movdqa %xmm0,48(%rax,$num)
2215 movdqa %xmm3,32(%rax)
2216 movdqa %xmm4,48(%rax)
2225 movq %r10, %xmm3 # -num
2227 $code.=<<___ if ($addx);
2228 mov OPENSSL_ia32cap_P+8(%rip),%r11d
2230 cmp \$0x80108,%r11d # check for AD*X+BMI2+BMI1
2233 lea (%rax,$num),$rptr
2234 call __bn_sqrx8x_reduction
2235 call __bn_postx4x_internal
2239 jmp .Lfrom_mont_zero
2245 call __bn_sqr8x_reduction
2246 call __bn_post4x_internal
2250 jmp .Lfrom_mont_zero
2254 mov 40(%rsp),%rsi # restore %rsp
2256 movdqa %xmm0,16*0(%rax)
2257 movdqa %xmm0,16*1(%rax)
2258 movdqa %xmm0,16*2(%rax)
2259 movdqa %xmm0,16*3(%rax)
2262 jnz .Lfrom_mont_zero
2278 .cfi_def_cfa_register %rsp
2282 .size bn_from_mont8x,.-bn_from_mont8x
2288 my $bp="%rdx"; # restore original value
2291 .type bn_mulx4x_mont_gather5,\@function,6
2293 bn_mulx4x_mont_gather5:
2296 .cfi_def_cfa_register %rax
2312 shl \$3,${num}d # convert $num to bytes
2313 lea ($num,$num,2),%r10 # 3*$num in bytes
2317 ##############################################################
2318 # Ensure that stack frame doesn't alias with $rptr+3*$num
2319 # modulo 4096, which covers ret[num], am[num] and n[num]
2320 # (see bn_exp.c). This is done to allow memory disambiguation
2321 # logic do its magic. [Extra [num] is allocated in order
2322 # to align with bn_power5's frame, which is cleansed after
2323 # completing exponentiation. Extra 256 bytes is for power mask
2324 # calculated from 7th argument, the index.]
2326 lea -320(%rsp,$num,2),%r11
2332 sub %r11,%rbp # align with $aptr
2333 lea -320(%rbp,$num,2),%rbp # future alloca(frame+2*$num*8+256)
2337 lea 4096-320(,$num,2),%r10
2338 lea -320(%rbp,$num,2),%rbp # future alloca(frame+2*$num*8+256)
2344 and \$-64,%rbp # ensure alignment
2348 lea (%rbp,%r11),%rsp
2351 ja .Lmulx4x_page_walk
2352 jmp .Lmulx4x_page_walk_done
2355 lea -4096(%rsp),%rsp
2358 ja .Lmulx4x_page_walk
2359 .Lmulx4x_page_walk_done:
2361 ##############################################################
2364 # +8 off-loaded &b[i]
2373 mov $n0, 32(%rsp) # save *n0
2374 mov %rax,40(%rsp) # save original %rsp
2375 .cfi_cfa_expression %rsp+40,deref,+8
2377 call mulx4x_internal
2379 mov 40(%rsp),%rsi # restore %rsp
2396 .cfi_def_cfa_register %rsp
2400 .size bn_mulx4x_mont_gather5,.-bn_mulx4x_mont_gather5
2402 .type mulx4x_internal,\@abi-omnipotent
2405 mov $num,8(%rsp) # save -$num (it was in bytes)
2407 neg $num # restore $num
2409 neg %r10 # restore $num
2410 lea 128($bp,$num),%r13 # end of powers table (+size optimization)
2412 movd `($win64?56:8)`(%rax),%xmm5 # load 7th argument
2414 lea .Linc(%rip),%rax
2415 mov %r13,16+8(%rsp) # end of b[num]
2416 mov $num,24+8(%rsp) # inner counter
2417 mov $rp, 56+8(%rsp) # save $rp
2419 my ($aptr, $bptr, $nptr, $tptr, $mi, $bi, $zero, $num)=
2420 ("%rsi","%rdi","%rcx","%rbx","%r8","%r9","%rbp","%rax");
2422 my $STRIDE=2**5*8; # 5 is "window size"
2423 my $N=$STRIDE/4; # should match cache line size
2425 movdqa 0(%rax),%xmm0 # 00000001000000010000000000000000
2426 movdqa 16(%rax),%xmm1 # 00000002000000020000000200000002
2427 lea 88-112(%rsp,%r10),%r10 # place the mask after tp[num+1] (+ICache optimization)
2428 lea 128($bp),$bptr # size optimization
2430 pshufd \$0,%xmm5,%xmm5 # broadcast index
2435 ########################################################################
2436 # calculate mask by comparing 0..31 to index and save result to stack
2441 pcmpeqd %xmm5,%xmm0 # compare to 1,0
2444 for($i=0;$i<$STRIDE/16-4;$i+=4) {
2447 pcmpeqd %xmm5,%xmm1 # compare to 3,2
2448 movdqa %xmm0,`16*($i+0)+112`(%r10)
2452 pcmpeqd %xmm5,%xmm2 # compare to 5,4
2453 movdqa %xmm1,`16*($i+1)+112`(%r10)
2457 pcmpeqd %xmm5,%xmm3 # compare to 7,6
2458 movdqa %xmm2,`16*($i+2)+112`(%r10)
2463 movdqa %xmm3,`16*($i+3)+112`(%r10)
2467 $code.=<<___; # last iteration can be optimized
2471 movdqa %xmm0,`16*($i+0)+112`(%r10)
2475 movdqa %xmm1,`16*($i+1)+112`(%r10)
2478 movdqa %xmm2,`16*($i+2)+112`(%r10)
2480 pand `16*($i+0)-128`($bptr),%xmm0 # while it's still in register
2481 pand `16*($i+1)-128`($bptr),%xmm1
2482 pand `16*($i+2)-128`($bptr),%xmm2
2483 movdqa %xmm3,`16*($i+3)+112`(%r10)
2484 pand `16*($i+3)-128`($bptr),%xmm3
2488 for($i=0;$i<$STRIDE/16-4;$i+=4) {
2490 movdqa `16*($i+0)-128`($bptr),%xmm4
2491 movdqa `16*($i+1)-128`($bptr),%xmm5
2492 movdqa `16*($i+2)-128`($bptr),%xmm2
2493 pand `16*($i+0)+112`(%r10),%xmm4
2494 movdqa `16*($i+3)-128`($bptr),%xmm3
2495 pand `16*($i+1)+112`(%r10),%xmm5
2497 pand `16*($i+2)+112`(%r10),%xmm2
2499 pand `16*($i+3)+112`(%r10),%xmm3
2506 pshufd \$0x4e,%xmm0,%xmm1
2508 lea $STRIDE($bptr),$bptr
2509 movq %xmm0,%rdx # bp[0]
2510 lea 64+8*4+8(%rsp),$tptr
2513 mulx 0*8($aptr),$mi,%rax # a[0]*b[0]
2514 mulx 1*8($aptr),%r11,%r12 # a[1]*b[0]
2516 mulx 2*8($aptr),%rax,%r13 # ...
2519 mulx 3*8($aptr),%rax,%r14
2522 imulq 32+8(%rsp),$mi # "t[0]"*n0
2523 xor $zero,$zero # cf=0, of=0
2526 mov $bptr,8+8(%rsp) # off-load &b[i]
2528 lea 4*8($aptr),$aptr
2530 adcx $zero,%r14 # cf=0
2532 mulx 0*8($nptr),%rax,%r10
2533 adcx %rax,%r15 # discarded
2535 mulx 1*8($nptr),%rax,%r11
2538 mulx 2*8($nptr),%rax,%r12
2539 mov 24+8(%rsp),$bptr # counter value
2540 mov %r10,-8*4($tptr)
2543 mulx 3*8($nptr),%rax,%r15
2545 mov %r11,-8*3($tptr)
2547 adox $zero,%r15 # of=0
2548 lea 4*8($nptr),$nptr
2549 mov %r12,-8*2($tptr)
2554 adcx $zero,%r15 # cf=0, modulo-scheduled
2555 mulx 0*8($aptr),%r10,%rax # a[4]*b[0]
2557 mulx 1*8($aptr),%r11,%r14 # a[5]*b[0]
2559 mulx 2*8($aptr),%r12,%rax # ...
2561 mulx 3*8($aptr),%r13,%r14
2565 adcx $zero,%r14 # cf=0
2566 lea 4*8($aptr),$aptr
2567 lea 4*8($tptr),$tptr
2570 mulx 0*8($nptr),%rax,%r15
2573 mulx 1*8($nptr),%rax,%r15
2576 mulx 2*8($nptr),%rax,%r15
2577 mov %r10,-5*8($tptr)
2579 mov %r11,-4*8($tptr)
2581 mulx 3*8($nptr),%rax,%r15
2583 mov %r12,-3*8($tptr)
2586 lea 4*8($nptr),$nptr
2587 mov %r13,-2*8($tptr)
2589 dec $bptr # of=0, pass cf
2592 mov 8(%rsp),$num # load -num
2593 adc $zero,%r15 # modulo-scheduled
2594 lea ($aptr,$num),$aptr # rewind $aptr
2596 mov 8+8(%rsp),$bptr # re-load &b[i]
2597 adc $zero,$zero # top-most carry
2598 mov %r14,-1*8($tptr)
2603 lea 16-256($tptr),%r10 # where 256-byte mask is (+density control)
2608 for($i=0;$i<$STRIDE/16;$i+=4) {
2610 movdqa `16*($i+0)-128`($bptr),%xmm0
2611 movdqa `16*($i+1)-128`($bptr),%xmm1
2612 movdqa `16*($i+2)-128`($bptr),%xmm2
2613 pand `16*($i+0)+256`(%r10),%xmm0
2614 movdqa `16*($i+3)-128`($bptr),%xmm3
2615 pand `16*($i+1)+256`(%r10),%xmm1
2617 pand `16*($i+2)+256`(%r10),%xmm2
2619 pand `16*($i+3)+256`(%r10),%xmm3
2626 pshufd \$0x4e,%xmm4,%xmm0
2628 lea $STRIDE($bptr),$bptr
2629 movq %xmm0,%rdx # m0=bp[i]
2631 mov $zero,($tptr) # save top-most carry
2632 lea 4*8($tptr,$num),$tptr # rewind $tptr
2633 mulx 0*8($aptr),$mi,%r11 # a[0]*b[i]
2634 xor $zero,$zero # cf=0, of=0
2636 mulx 1*8($aptr),%r14,%r12 # a[1]*b[i]
2637 adox -4*8($tptr),$mi # +t[0]
2639 mulx 2*8($aptr),%r15,%r13 # ...
2640 adox -3*8($tptr),%r11
2642 mulx 3*8($aptr),%rdx,%r14
2643 adox -2*8($tptr),%r12
2645 lea ($nptr,$num),$nptr # rewind $nptr
2646 lea 4*8($aptr),$aptr
2647 adox -1*8($tptr),%r13
2652 imulq 32+8(%rsp),$mi # "t[0]"*n0
2655 xor $zero,$zero # cf=0, of=0
2656 mov $bptr,8+8(%rsp) # off-load &b[i]
2658 mulx 0*8($nptr),%rax,%r10
2659 adcx %rax,%r15 # discarded
2661 mulx 1*8($nptr),%rax,%r11
2664 mulx 2*8($nptr),%rax,%r12
2667 mulx 3*8($nptr),%rax,%r15
2669 mov 24+8(%rsp),$bptr # counter value
2670 mov %r10,-8*4($tptr)
2672 mov %r11,-8*3($tptr)
2673 adox $zero,%r15 # of=0
2674 mov %r12,-8*2($tptr)
2675 lea 4*8($nptr),$nptr
2680 mulx 0*8($aptr),%r10,%rax # a[4]*b[i]
2681 adcx $zero,%r15 # cf=0, modulo-scheduled
2683 mulx 1*8($aptr),%r11,%r14 # a[5]*b[i]
2684 adcx 0*8($tptr),%r10
2686 mulx 2*8($aptr),%r12,%rax # ...
2687 adcx 1*8($tptr),%r11
2689 mulx 3*8($aptr),%r13,%r14
2691 adcx 2*8($tptr),%r12
2693 adcx 3*8($tptr),%r13
2694 adox $zero,%r14 # of=0
2695 lea 4*8($aptr),$aptr
2696 lea 4*8($tptr),$tptr
2697 adcx $zero,%r14 # cf=0
2700 mulx 0*8($nptr),%rax,%r15
2703 mulx 1*8($nptr),%rax,%r15
2706 mulx 2*8($nptr),%rax,%r15
2707 mov %r10,-5*8($tptr)
2710 mov %r11,-4*8($tptr)
2711 mulx 3*8($nptr),%rax,%r15
2713 lea 4*8($nptr),$nptr
2714 mov %r12,-3*8($tptr)
2717 mov %r13,-2*8($tptr)
2719 dec $bptr # of=0, pass cf
2722 mov 0+8(%rsp),$num # load -num
2723 adc $zero,%r15 # modulo-scheduled
2724 sub 0*8($tptr),$bptr # pull top-most carry to %cf
2725 mov 8+8(%rsp),$bptr # re-load &b[i]
2728 lea ($aptr,$num),$aptr # rewind $aptr
2729 adc $zero,$zero # top-most carry
2730 mov %r14,-1*8($tptr)
2737 mov ($nptr,$num),%r12
2738 lea ($nptr,$num),%rbp # rewind $nptr
2740 lea ($tptr,$num),%rdi # rewind $tptr
2743 sub %r14,%r10 # compare top-most words
2747 sub %r8,%rax # %rax=-%r8
2748 mov 56+8(%rsp),%rdx # restore rp
2749 dec %r12 # so that after 'not' we get -n[0]
2754 jmp .Lsqrx4x_sub_entry # common post-condition
2755 .size mulx4x_internal,.-mulx4x_internal
2758 ######################################################################
2760 my $rptr="%rdi"; # BN_ULONG *rptr,
2761 my $aptr="%rsi"; # const BN_ULONG *aptr,
2762 my $bptr="%rdx"; # const void *table,
2763 my $nptr="%rcx"; # const BN_ULONG *nptr,
2764 my $n0 ="%r8"; # const BN_ULONG *n0);
2765 my $num ="%r9"; # int num, has to be divisible by 8
2768 my ($i,$j,$tptr)=("%rbp","%rcx",$rptr);
2769 my @A0=("%r10","%r11");
2770 my @A1=("%r12","%r13");
2771 my ($a0,$a1,$ai)=("%r14","%r15","%rbx");
2774 .type bn_powerx5,\@function,6
2779 .cfi_def_cfa_register %rax
2795 shl \$3,${num}d # convert $num to bytes
2796 lea ($num,$num,2),%r10 # 3*$num in bytes
2800 ##############################################################
2801 # Ensure that stack frame doesn't alias with $rptr+3*$num
2802 # modulo 4096, which covers ret[num], am[num] and n[num]
2803 # (see bn_exp.c). This is done to allow memory disambiguation
2804 # logic do its magic. [Extra 256 bytes is for power mask
2805 # calculated from 7th argument, the index.]
2807 lea -320(%rsp,$num,2),%r11
2813 sub %r11,%rbp # align with $aptr
2814 lea -320(%rbp,$num,2),%rbp # future alloca(frame+2*$num*8+256)
2819 lea 4096-320(,$num,2),%r10
2820 lea -320(%rbp,$num,2),%rbp # alloca(frame+2*$num*8+256)
2830 lea (%rbp,%r11),%rsp
2834 jmp .Lpwrx_page_walk_done
2837 lea -4096(%rsp),%rsp
2841 .Lpwrx_page_walk_done:
2846 ##############################################################
2849 # +0 saved $num, used in reduction section
2850 # +8 &t[2*$num], used in reduction section
2851 # +16 intermediate carry bit
2852 # +24 top-most carry bit, used in reduction section
2858 movq $rptr,%xmm1 # save $rptr
2859 movq $nptr,%xmm2 # save $nptr
2860 movq %r10, %xmm3 # -$num
2863 mov %rax, 40(%rsp) # save original %rsp
2864 .cfi_cfa_expression %rsp+40,deref,+8
2867 call __bn_sqrx8x_internal
2868 call __bn_postx4x_internal
2869 call __bn_sqrx8x_internal
2870 call __bn_postx4x_internal
2871 call __bn_sqrx8x_internal
2872 call __bn_postx4x_internal
2873 call __bn_sqrx8x_internal
2874 call __bn_postx4x_internal
2875 call __bn_sqrx8x_internal
2876 call __bn_postx4x_internal
2878 mov %r10,$num # -num
2884 call mulx4x_internal
2886 mov 40(%rsp),%rsi # restore %rsp
2903 .cfi_def_cfa_register %rsp
2907 .size bn_powerx5,.-bn_powerx5
2909 .globl bn_sqrx8x_internal
2910 .hidden bn_sqrx8x_internal
2911 .type bn_sqrx8x_internal,\@abi-omnipotent
2914 __bn_sqrx8x_internal:
2916 ##################################################################
2919 # a) multiply-n-add everything but a[i]*a[i];
2920 # b) shift result of a) by 1 to the left and accumulate
2921 # a[i]*a[i] products;
2923 ##################################################################
2924 # 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]
2955 # 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]
2958 my ($zero,$carry)=("%rbp","%rcx");
2961 lea 48+8(%rsp),$tptr
2962 lea ($aptr,$num),$aaptr
2963 mov $num,0+8(%rsp) # save $num
2964 mov $aaptr,8+8(%rsp) # save end of $aptr
2965 jmp .Lsqr8x_zero_start
2968 .byte 0x66,0x66,0x66,0x2e,0x0f,0x1f,0x84,0x00,0x00,0x00,0x00,0x00
2971 movdqa %xmm0,0*8($tptr)
2972 movdqa %xmm0,2*8($tptr)
2973 movdqa %xmm0,4*8($tptr)
2974 movdqa %xmm0,6*8($tptr)
2975 .Lsqr8x_zero_start: # aligned at 32
2976 movdqa %xmm0,8*8($tptr)
2977 movdqa %xmm0,10*8($tptr)
2978 movdqa %xmm0,12*8($tptr)
2979 movdqa %xmm0,14*8($tptr)
2980 lea 16*8($tptr),$tptr
2984 mov 0*8($aptr),%rdx # a[0], modulo-scheduled
2985 #xor %r9,%r9 # t[1], ex-$num, zero already
2992 lea 48+8(%rsp),$tptr
2993 xor $zero,$zero # cf=0, cf=0
2994 jmp .Lsqrx8x_outer_loop
2997 .Lsqrx8x_outer_loop:
2998 mulx 1*8($aptr),%r8,%rax # a[1]*a[0]
2999 adcx %r9,%r8 # a[1]*a[0]+=t[1]
3001 mulx 2*8($aptr),%r9,%rax # a[2]*a[0]
3004 .byte 0xc4,0xe2,0xab,0xf6,0x86,0x18,0x00,0x00,0x00 # mulx 3*8($aptr),%r10,%rax # ...
3007 .byte 0xc4,0xe2,0xa3,0xf6,0x86,0x20,0x00,0x00,0x00 # mulx 4*8($aptr),%r11,%rax
3010 mulx 5*8($aptr),%r12,%rax
3013 mulx 6*8($aptr),%r13,%rax
3016 mulx 7*8($aptr),%r14,%r15
3017 mov 1*8($aptr),%rdx # a[1]
3021 mov %r8,1*8($tptr) # t[1]
3022 mov %r9,2*8($tptr) # t[2]
3023 sbb $carry,$carry # mov %cf,$carry
3024 xor $zero,$zero # cf=0, of=0
3027 mulx 2*8($aptr),%r8,%rbx # a[2]*a[1]
3028 mulx 3*8($aptr),%r9,%rax # a[3]*a[1]
3031 mulx 4*8($aptr),%r10,%rbx # ...
3034 .byte 0xc4,0xe2,0xa3,0xf6,0x86,0x28,0x00,0x00,0x00 # mulx 5*8($aptr),%r11,%rax
3037 .byte 0xc4,0xe2,0x9b,0xf6,0x9e,0x30,0x00,0x00,0x00 # mulx 6*8($aptr),%r12,%rbx
3040 .byte 0xc4,0x62,0x93,0xf6,0xb6,0x38,0x00,0x00,0x00 # mulx 7*8($aptr),%r13,%r14
3041 mov 2*8($aptr),%rdx # a[2]
3045 adox $zero,%r14 # of=0
3046 adcx $zero,%r14 # cf=0
3048 mov %r8,3*8($tptr) # t[3]
3049 mov %r9,4*8($tptr) # t[4]
3051 mulx 3*8($aptr),%r8,%rbx # a[3]*a[2]
3052 mulx 4*8($aptr),%r9,%rax # a[4]*a[2]
3055 mulx 5*8($aptr),%r10,%rbx # ...
3058 .byte 0xc4,0xe2,0xa3,0xf6,0x86,0x30,0x00,0x00,0x00 # mulx 6*8($aptr),%r11,%rax
3061 .byte 0xc4,0x62,0x9b,0xf6,0xae,0x38,0x00,0x00,0x00 # mulx 7*8($aptr),%r12,%r13
3063 mov 3*8($aptr),%rdx # a[3]
3067 mov %r8,5*8($tptr) # t[5]
3068 mov %r9,6*8($tptr) # t[6]
3069 mulx 4*8($aptr),%r8,%rax # a[4]*a[3]
3070 adox $zero,%r13 # of=0
3071 adcx $zero,%r13 # cf=0
3073 mulx 5*8($aptr),%r9,%rbx # a[5]*a[3]
3076 mulx 6*8($aptr),%r10,%rax # ...
3079 mulx 7*8($aptr),%r11,%r12
3080 mov 4*8($aptr),%rdx # a[4]
3081 mov 5*8($aptr),%r14 # a[5]
3084 mov 6*8($aptr),%r15 # a[6]
3086 adox $zero,%r12 # of=0
3087 adcx $zero,%r12 # cf=0
3089 mov %r8,7*8($tptr) # t[7]
3090 mov %r9,8*8($tptr) # t[8]
3092 mulx %r14,%r9,%rax # a[5]*a[4]
3093 mov 7*8($aptr),%r8 # a[7]
3095 mulx %r15,%r10,%rbx # a[6]*a[4]
3098 mulx %r8,%r11,%rax # a[7]*a[4]
3099 mov %r14,%rdx # a[5]
3102 #adox $zero,%rax # of=0
3103 adcx $zero,%rax # cf=0
3105 mulx %r15,%r14,%rbx # a[6]*a[5]
3106 mulx %r8,%r12,%r13 # a[7]*a[5]
3107 mov %r15,%rdx # a[6]
3108 lea 8*8($aptr),$aptr
3115 mulx %r8,%r8,%r14 # a[7]*a[6]
3120 je .Lsqrx8x_outer_break
3122 neg $carry # mov $carry,%cf
3126 adcx 9*8($tptr),%r9 # +=t[9]
3127 adcx 10*8($tptr),%r10 # ...
3128 adcx 11*8($tptr),%r11
3129 adc 12*8($tptr),%r12
3130 adc 13*8($tptr),%r13
3131 adc 14*8($tptr),%r14
3132 adc 15*8($tptr),%r15
3134 lea 2*64($tptr),$tptr
3135 sbb %rax,%rax # mov %cf,$carry
3137 mov -64($aptr),%rdx # a[0]
3138 mov %rax,16+8(%rsp) # offload $carry
3139 mov $tptr,24+8(%rsp)
3141 #lea 8*8($tptr),$tptr # see 2*8*8($tptr) above
3142 xor %eax,%eax # cf=0, of=0
3148 mulx 0*8($aaptr),%rax,%r8 # a[8]*a[i]
3149 adcx %rax,%rbx # +=t[8]
3152 mulx 1*8($aaptr),%rax,%r9 # ...
3156 mulx 2*8($aaptr),%rax,%r10
3160 mulx 3*8($aaptr),%rax,%r11
3164 .byte 0xc4,0x62,0xfb,0xf6,0xa5,0x20,0x00,0x00,0x00 # mulx 4*8($aaptr),%rax,%r12
3168 mulx 5*8($aaptr),%rax,%r13
3172 mulx 6*8($aaptr),%rax,%r14
3173 mov %rbx,($tptr,%rcx,8) # store t[8+i]
3178 .byte 0xc4,0x62,0xfb,0xf6,0xbd,0x38,0x00,0x00,0x00 # mulx 7*8($aaptr),%rax,%r15
3179 mov 8($aptr,%rcx,8),%rdx # a[i]
3181 adox %rbx,%r15 # %rbx is 0, of=0
3182 adcx %rbx,%r15 # cf=0
3188 lea 8*8($aaptr),$aaptr
3190 cmp 8+8(%rsp),$aaptr # done?
3193 sub 16+8(%rsp),%rbx # mov 16(%rsp),%cf
3204 lea 8*8($tptr),$tptr
3206 sbb %rax,%rax # mov %cf,%rax
3207 xor %ebx,%ebx # cf=0, of=0
3208 mov %rax,16+8(%rsp) # offload carry
3214 sub 16+8(%rsp),%rbx # mov 16(%rsp),%cf
3216 mov 24+8(%rsp),$carry # initial $tptr, borrow $carry
3218 mov 0*8($aptr),%rdx # a[8], modulo-scheduled
3226 cmp $carry,$tptr # cf=0, of=0
3227 je .Lsqrx8x_outer_loop
3232 mov 2*8($carry),%r10
3234 mov 3*8($carry),%r11
3236 mov 4*8($carry),%r12
3238 mov 5*8($carry),%r13
3240 mov 6*8($carry),%r14
3242 mov 7*8($carry),%r15
3244 jmp .Lsqrx8x_outer_loop
3247 .Lsqrx8x_outer_break:
3248 mov %r9,9*8($tptr) # t[9]
3249 movq %xmm3,%rcx # -$num
3250 mov %r10,10*8($tptr) # ...
3251 mov %r11,11*8($tptr)
3252 mov %r12,12*8($tptr)
3253 mov %r13,13*8($tptr)
3254 mov %r14,14*8($tptr)
3259 lea 48+8(%rsp),$tptr
3260 mov ($aptr,$i),%rdx # a[0]
3262 mov 8($tptr),$A0[1] # t[1]
3263 xor $A0[0],$A0[0] # t[0], of=0, cf=0
3264 mov 0+8(%rsp),$num # restore $num
3266 mov 16($tptr),$A1[0] # t[2] # prefetch
3267 mov 24($tptr),$A1[1] # t[3] # prefetch
3268 #jmp .Lsqrx4x_shift_n_add # happens to be aligned
3271 .Lsqrx4x_shift_n_add:
3275 .byte 0x48,0x8b,0x94,0x0e,0x08,0x00,0x00,0x00 # mov 8($aptr,$i),%rdx # a[i+1] # prefetch
3276 .byte 0x4c,0x8b,0x97,0x20,0x00,0x00,0x00 # mov 32($tptr),$A0[0] # t[2*i+4] # prefetch
3279 mov 40($tptr),$A0[1] # t[2*i+4+1] # prefetch
3286 mov 16($aptr,$i),%rdx # a[i+2] # prefetch
3287 mov 48($tptr),$A1[0] # t[2*i+6] # prefetch
3290 mov 56($tptr),$A1[1] # t[2*i+6+1] # prefetch
3297 mov 24($aptr,$i),%rdx # a[i+3] # prefetch
3299 mov 64($tptr),$A0[0] # t[2*i+8] # prefetch
3302 mov 72($tptr),$A0[1] # t[2*i+8+1] # prefetch
3309 jrcxz .Lsqrx4x_shift_n_add_break
3310 .byte 0x48,0x8b,0x94,0x0e,0x00,0x00,0x00,0x00 # mov 0($aptr,$i),%rdx # a[i+4] # prefetch
3313 mov 80($tptr),$A1[0] # t[2*i+10] # prefetch
3314 mov 88($tptr),$A1[1] # t[2*i+10+1] # prefetch
3319 jmp .Lsqrx4x_shift_n_add
3322 .Lsqrx4x_shift_n_add_break:
3326 lea 64($tptr),$tptr # end of t[] buffer
3329 ######################################################################
3330 # Montgomery reduction part, "word-by-word" algorithm.
3332 # This new path is inspired by multiple submissions from Intel, by
3333 # Shay Gueron, Vlad Krasnov, Erdinc Ozturk, James Guilford,
3336 my ($nptr,$carry,$m0)=("%rbp","%rsi","%rdx");
3340 __bn_sqrx8x_reduction:
3341 xor %eax,%eax # initial top-most carry bit
3342 mov 32+8(%rsp),%rbx # n0
3343 mov 48+8(%rsp),%rdx # "%r8", 8*0($tptr)
3344 lea -8*8($nptr,$num),%rcx # end of n[]
3345 #lea 48+8(%rsp,$num,2),$tptr # end of t[] buffer
3346 mov %rcx, 0+8(%rsp) # save end of n[]
3347 mov $tptr,8+8(%rsp) # save end of t[]
3349 lea 48+8(%rsp),$tptr # initial t[] window
3350 jmp .Lsqrx8x_reduction_loop
3353 .Lsqrx8x_reduction_loop:
3359 imulq %rbx,%rdx # n0*a[i]
3363 mov %rax,24+8(%rsp) # store top-most carry bit
3365 lea 8*8($tptr),$tptr
3366 xor $carry,$carry # cf=0,of=0
3373 mulx 8*0($nptr),%rax,%r8 # n[0]
3374 adcx %rbx,%rax # discarded
3377 mulx 8*1($nptr),%rbx,%r9 # n[1]
3381 mulx 8*2($nptr),%rbx,%r10
3385 mulx 8*3($nptr),%rbx,%r11
3389 .byte 0xc4,0x62,0xe3,0xf6,0xa5,0x20,0x00,0x00,0x00 # mulx 8*4($nptr),%rbx,%r12
3395 mulx 32+8(%rsp),%rbx,%rdx # %rdx discarded
3397 mov %rax,64+48+8(%rsp,%rcx,8) # put aside n0*a[i]
3399 mulx 8*5($nptr),%rax,%r13
3403 mulx 8*6($nptr),%rax,%r14
3407 mulx 8*7($nptr),%rax,%r15
3410 adox $carry,%r15 # $carry is 0
3411 adcx $carry,%r15 # cf=0
3413 .byte 0x67,0x67,0x67
3417 mov $carry,%rax # xor %rax,%rax
3418 cmp 0+8(%rsp),$nptr # end of n[]?
3419 jae .Lsqrx8x_no_tail
3421 mov 48+8(%rsp),%rdx # pull n0*a[0]
3423 lea 8*8($nptr),$nptr
3426 adcx 8*2($tptr),%r10
3432 lea 8*8($tptr),$tptr
3433 sbb %rax,%rax # top carry
3435 xor $carry,$carry # of=0, cf=0
3442 mulx 8*0($nptr),%rax,%r8
3446 mulx 8*1($nptr),%rax,%r9
3450 mulx 8*2($nptr),%rax,%r10
3454 mulx 8*3($nptr),%rax,%r11
3458 .byte 0xc4,0x62,0xfb,0xf6,0xa5,0x20,0x00,0x00,0x00 # mulx 8*4($nptr),%rax,%r12
3462 mulx 8*5($nptr),%rax,%r13
3466 mulx 8*6($nptr),%rax,%r14
3470 mulx 8*7($nptr),%rax,%r15
3471 mov 72+48+8(%rsp,%rcx,8),%rdx # pull n0*a[i]
3474 mov %rbx,($tptr,%rcx,8) # save result
3476 adcx $carry,%r15 # cf=0
3481 cmp 0+8(%rsp),$nptr # end of n[]?
3482 jae .Lsqrx8x_tail_done # break out of loop
3484 sub 16+8(%rsp),$carry # mov 16(%rsp),%cf
3485 mov 48+8(%rsp),%rdx # pull n0*a[0]
3486 lea 8*8($nptr),$nptr
3495 lea 8*8($tptr),$tptr
3497 sub \$8,%rcx # mov \$-8,%rcx
3499 xor $carry,$carry # of=0, cf=0
3506 add 24+8(%rsp),%r8 # can this overflow?
3516 sub 16+8(%rsp),$carry # mov 16(%rsp),%cf
3517 .Lsqrx8x_no_tail: # %cf is 0 if jumped here
3521 mov 8*7($nptr),$carry
3522 movq %xmm2,$nptr # restore $nptr
3529 adc \$0,%rax # top-most carry
3531 mov 32+8(%rsp),%rbx # n0
3532 mov 8*8($tptr,%rcx),%rdx # modulo-scheduled "%r8"
3534 mov %r8,8*0($tptr) # store top 512 bits
3535 lea 8*8($tptr),%r8 # borrow %r8
3544 lea 8*8($tptr,%rcx),$tptr # start of current t[] window
3545 cmp 8+8(%rsp),%r8 # end of t[]?
3546 jb .Lsqrx8x_reduction_loop
3549 .size bn_sqrx8x_internal,.-bn_sqrx8x_internal
3552 ##############################################################
3553 # Post-condition, 4x unrolled
3556 my ($rptr,$nptr)=("%rdx","%rbp");
3559 __bn_postx4x_internal:
3561 mov %rcx,%r10 # -$num
3562 mov %rcx,%r9 # -$num
3565 #lea 48+8(%rsp,%r9),$tptr
3566 movq %xmm1,$rptr # restore $rptr
3567 movq %xmm1,$aptr # prepare for back-to-back call
3568 dec %r12 # so that after 'not' we get -n[0]
3573 jmp .Lsqrx4x_sub_entry
3583 lea 8*4($nptr),$nptr
3588 neg %r8 # mov %r8,%cf
3594 lea 8*4($tptr),$tptr
3596 sbb %r8,%r8 # mov %cf,%r8
3599 lea 8*4($rptr),$rptr
3604 neg %r9 # restore $num
3607 .size __bn_postx4x_internal,.-__bn_postx4x_internal
3612 my ($inp,$num,$tbl,$idx)=$win64?("%rcx","%edx","%r8", "%r9d") : # Win64 order
3613 ("%rdi","%esi","%rdx","%ecx"); # Unix order
3620 .type bn_get_bits5,\@abi-omnipotent
3632 movzw (%r10,$num,2),%eax
3636 .size bn_get_bits5,.-bn_get_bits5
3639 .type bn_scatter5,\@abi-omnipotent
3643 jz .Lscatter_epilogue
3644 lea ($tbl,$idx,8),$tbl
3654 .size bn_scatter5,.-bn_scatter5
3657 .type bn_gather5,\@abi-omnipotent
3660 .LSEH_begin_bn_gather5: # Win64 thing, but harmless in other cases
3661 # I can't trust assembler to use specific encoding:-(
3662 .byte 0x4c,0x8d,0x14,0x24 #lea (%rsp),%r10
3663 .byte 0x48,0x81,0xec,0x08,0x01,0x00,0x00 #sub $0x108,%rsp
3664 lea .Linc(%rip),%rax
3665 and \$-16,%rsp # shouldn't be formally required
3668 movdqa 0(%rax),%xmm0 # 00000001000000010000000000000000
3669 movdqa 16(%rax),%xmm1 # 00000002000000020000000200000002
3670 lea 128($tbl),%r11 # size optimization
3671 lea 128(%rsp),%rax # size optimization
3673 pshufd \$0,%xmm5,%xmm5 # broadcast $idx
3677 ########################################################################
3678 # calculate mask by comparing 0..31 to $idx and save result to stack
3680 for($i=0;$i<$STRIDE/16;$i+=4) {
3683 pcmpeqd %xmm5,%xmm0 # compare to 1,0
3685 $code.=<<___ if ($i);
3686 movdqa %xmm3,`16*($i-1)-128`(%rax)
3692 pcmpeqd %xmm5,%xmm1 # compare to 3,2
3693 movdqa %xmm0,`16*($i+0)-128`(%rax)
3697 pcmpeqd %xmm5,%xmm2 # compare to 5,4
3698 movdqa %xmm1,`16*($i+1)-128`(%rax)
3702 pcmpeqd %xmm5,%xmm3 # compare to 7,6
3703 movdqa %xmm2,`16*($i+2)-128`(%rax)
3708 movdqa %xmm3,`16*($i-1)-128`(%rax)
3716 for($i=0;$i<$STRIDE/16;$i+=4) {
3718 movdqa `16*($i+0)-128`(%r11),%xmm0
3719 movdqa `16*($i+1)-128`(%r11),%xmm1
3720 movdqa `16*($i+2)-128`(%r11),%xmm2
3721 pand `16*($i+0)-128`(%rax),%xmm0
3722 movdqa `16*($i+3)-128`(%r11),%xmm3
3723 pand `16*($i+1)-128`(%rax),%xmm1
3725 pand `16*($i+2)-128`(%rax),%xmm2
3727 pand `16*($i+3)-128`(%rax),%xmm3
3734 lea $STRIDE(%r11),%r11
3735 pshufd \$0x4e,%xmm4,%xmm0
3737 movq %xmm0,($out) # m0=bp[0]
3744 .LSEH_end_bn_gather5:
3745 .size bn_gather5,.-bn_gather5
3753 .asciz "Montgomery Multiplication with scatter/gather for x86_64, CRYPTOGAMS by <appro\@openssl.org>"
3756 # EXCEPTION_DISPOSITION handler (EXCEPTION_RECORD *rec,ULONG64 frame,
3757 # CONTEXT *context,DISPATCHER_CONTEXT *disp)
3765 .extern __imp_RtlVirtualUnwind
3766 .type mul_handler,\@abi-omnipotent
3780 mov 120($context),%rax # pull context->Rax
3781 mov 248($context),%rbx # pull context->Rip
3783 mov 8($disp),%rsi # disp->ImageBase
3784 mov 56($disp),%r11 # disp->HandlerData
3786 mov 0(%r11),%r10d # HandlerData[0]
3787 lea (%rsi,%r10),%r10 # end of prologue label
3788 cmp %r10,%rbx # context->Rip<end of prologue label
3789 jb .Lcommon_seh_tail
3791 mov 4(%r11),%r10d # HandlerData[1]
3792 lea (%rsi,%r10),%r10 # beginning of body label
3793 cmp %r10,%rbx # context->Rip<body label
3794 jb .Lcommon_pop_regs
3796 mov 152($context),%rax # pull context->Rsp
3798 mov 8(%r11),%r10d # HandlerData[2]
3799 lea (%rsi,%r10),%r10 # epilogue label
3800 cmp %r10,%rbx # context->Rip>=epilogue label
3801 jae .Lcommon_seh_tail
3803 lea .Lmul_epilogue(%rip),%r10
3807 mov 192($context),%r10 # pull $num
3808 mov 8(%rax,%r10,8),%rax # pull saved stack pointer
3810 jmp .Lcommon_pop_regs
3813 mov 40(%rax),%rax # pull saved stack pointer
3821 mov %rbx,144($context) # restore context->Rbx
3822 mov %rbp,160($context) # restore context->Rbp
3823 mov %r12,216($context) # restore context->R12
3824 mov %r13,224($context) # restore context->R13
3825 mov %r14,232($context) # restore context->R14
3826 mov %r15,240($context) # restore context->R15
3831 mov %rax,152($context) # restore context->Rsp
3832 mov %rsi,168($context) # restore context->Rsi
3833 mov %rdi,176($context) # restore context->Rdi
3835 mov 40($disp),%rdi # disp->ContextRecord
3836 mov $context,%rsi # context
3837 mov \$154,%ecx # sizeof(CONTEXT)
3838 .long 0xa548f3fc # cld; rep movsq
3841 xor %rcx,%rcx # arg1, UNW_FLAG_NHANDLER
3842 mov 8(%rsi),%rdx # arg2, disp->ImageBase
3843 mov 0(%rsi),%r8 # arg3, disp->ControlPc
3844 mov 16(%rsi),%r9 # arg4, disp->FunctionEntry
3845 mov 40(%rsi),%r10 # disp->ContextRecord
3846 lea 56(%rsi),%r11 # &disp->HandlerData
3847 lea 24(%rsi),%r12 # &disp->EstablisherFrame
3848 mov %r10,32(%rsp) # arg5
3849 mov %r11,40(%rsp) # arg6
3850 mov %r12,48(%rsp) # arg7
3851 mov %rcx,56(%rsp) # arg8, (NULL)
3852 call *__imp_RtlVirtualUnwind(%rip)
3854 mov \$1,%eax # ExceptionContinueSearch
3866 .size mul_handler,.-mul_handler
3870 .rva .LSEH_begin_bn_mul_mont_gather5
3871 .rva .LSEH_end_bn_mul_mont_gather5
3872 .rva .LSEH_info_bn_mul_mont_gather5
3874 .rva .LSEH_begin_bn_mul4x_mont_gather5
3875 .rva .LSEH_end_bn_mul4x_mont_gather5
3876 .rva .LSEH_info_bn_mul4x_mont_gather5
3878 .rva .LSEH_begin_bn_power5
3879 .rva .LSEH_end_bn_power5
3880 .rva .LSEH_info_bn_power5
3882 .rva .LSEH_begin_bn_from_mont8x
3883 .rva .LSEH_end_bn_from_mont8x
3884 .rva .LSEH_info_bn_from_mont8x
3886 $code.=<<___ if ($addx);
3887 .rva .LSEH_begin_bn_mulx4x_mont_gather5
3888 .rva .LSEH_end_bn_mulx4x_mont_gather5
3889 .rva .LSEH_info_bn_mulx4x_mont_gather5
3891 .rva .LSEH_begin_bn_powerx5
3892 .rva .LSEH_end_bn_powerx5
3893 .rva .LSEH_info_bn_powerx5
3896 .rva .LSEH_begin_bn_gather5
3897 .rva .LSEH_end_bn_gather5
3898 .rva .LSEH_info_bn_gather5
3902 .LSEH_info_bn_mul_mont_gather5:
3905 .rva .Lmul_body,.Lmul_body,.Lmul_epilogue # HandlerData[]
3907 .LSEH_info_bn_mul4x_mont_gather5:
3910 .rva .Lmul4x_prologue,.Lmul4x_body,.Lmul4x_epilogue # HandlerData[]
3912 .LSEH_info_bn_power5:
3915 .rva .Lpower5_prologue,.Lpower5_body,.Lpower5_epilogue # HandlerData[]
3917 .LSEH_info_bn_from_mont8x:
3920 .rva .Lfrom_prologue,.Lfrom_body,.Lfrom_epilogue # HandlerData[]
3922 $code.=<<___ if ($addx);
3924 .LSEH_info_bn_mulx4x_mont_gather5:
3927 .rva .Lmulx4x_prologue,.Lmulx4x_body,.Lmulx4x_epilogue # HandlerData[]
3929 .LSEH_info_bn_powerx5:
3932 .rva .Lpowerx5_prologue,.Lpowerx5_body,.Lpowerx5_epilogue # HandlerData[]
3936 .LSEH_info_bn_gather5:
3937 .byte 0x01,0x0b,0x03,0x0a
3938 .byte 0x0b,0x01,0x21,0x00 # sub rsp,0x108
3939 .byte 0x04,0xa3,0x00,0x00 # lea r10,(rsp)
3944 $code =~ s/\`([^\`]*)\`/eval($1)/gem;