2 # Copyright 2011-2020 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) ([0-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
586 shl \$5,$num # $num was in bytes
587 movd `($win64?56:8)`(%rax),%xmm5 # load 7th argument, index
589 lea 128(%rdx,$num),%r13 # end of powers table (+size optimization)
590 shr \$5,$num # restore $num
593 $STRIDE=2**5*8; # 5 is "window size"
594 $N=$STRIDE/4; # should match cache line size
597 movdqa 0(%rax),%xmm0 # 00000001000000010000000000000000
598 movdqa 16(%rax),%xmm1 # 00000002000000020000000200000002
599 lea 88-112(%rsp,$num),%r10 # place the mask after tp[num+1] (+ICache optimization)
600 lea 128(%rdx),$bp # size optimization
602 pshufd \$0,%xmm5,%xmm5 # broadcast index
607 ########################################################################
608 # calculate mask by comparing 0..31 to index and save result to stack
612 pcmpeqd %xmm5,%xmm0 # compare to 1,0
616 for($i=0;$i<$STRIDE/16-4;$i+=4) {
619 pcmpeqd %xmm5,%xmm1 # compare to 3,2
620 movdqa %xmm0,`16*($i+0)+112`(%r10)
624 pcmpeqd %xmm5,%xmm2 # compare to 5,4
625 movdqa %xmm1,`16*($i+1)+112`(%r10)
629 pcmpeqd %xmm5,%xmm3 # compare to 7,6
630 movdqa %xmm2,`16*($i+2)+112`(%r10)
635 movdqa %xmm3,`16*($i+3)+112`(%r10)
639 $code.=<<___; # last iteration can be optimized
642 movdqa %xmm0,`16*($i+0)+112`(%r10)
647 movdqa %xmm1,`16*($i+1)+112`(%r10)
650 movdqa %xmm2,`16*($i+2)+112`(%r10)
651 pand `16*($i+0)-128`($bp),%xmm0 # while it's still in register
653 pand `16*($i+1)-128`($bp),%xmm1
654 pand `16*($i+2)-128`($bp),%xmm2
655 movdqa %xmm3,`16*($i+3)+112`(%r10)
656 pand `16*($i+3)-128`($bp),%xmm3
660 for($i=0;$i<$STRIDE/16-4;$i+=4) {
662 movdqa `16*($i+0)-128`($bp),%xmm4
663 movdqa `16*($i+1)-128`($bp),%xmm5
664 movdqa `16*($i+2)-128`($bp),%xmm2
665 pand `16*($i+0)+112`(%r10),%xmm4
666 movdqa `16*($i+3)-128`($bp),%xmm3
667 pand `16*($i+1)+112`(%r10),%xmm5
669 pand `16*($i+2)+112`(%r10),%xmm2
671 pand `16*($i+3)+112`(%r10),%xmm3
678 pshufd \$0x4e,%xmm0,%xmm1
681 movq %xmm0,$m0 # m0=bp[0]
683 mov %r13,16+8(%rsp) # save end of b[num]
684 mov $rp, 56+8(%rsp) # save $rp
686 mov ($n0),$n0 # pull n0[0] value
688 lea ($ap,$num),$ap # end of a[num]
692 mulq $m0 # ap[0]*bp[0]
696 imulq $A[0],$m1 # "tp[0]"*n0
701 add %rax,$A[0] # discarded
714 mov 16($ap,$num),%rax
717 lea 4*8($num),$j # j=4
726 mulq $m0 # ap[j]*bp[0]
737 add $A[0],$N[0] # np[j]*m1+ap[j]*bp[0]
739 mov $N[0],-24($tp) # tp[j-1]
742 mulq $m0 # ap[j]*bp[0]
752 add $A[1],$N[1] # np[j]*m1+ap[j]*bp[0]
754 mov $N[1],-16($tp) # tp[j-1]
757 mulq $m0 # ap[j]*bp[0]
767 add $A[0],$N[0] # np[j]*m1+ap[j]*bp[0]
769 mov $N[0],-8($tp) # tp[j-1]
772 mulq $m0 # ap[j]*bp[0]
782 add $A[1],$N[1] # np[j]*m1+ap[j]*bp[0]
785 mov $N[1],($tp) # tp[j-1]
791 mulq $m0 # ap[j]*bp[0]
802 add $A[0],$N[0] # np[j]*m1+ap[j]*bp[0]
804 mov $N[0],-24($tp) # tp[j-1]
807 mulq $m0 # ap[j]*bp[0]
815 mov ($ap,$num),%rax # ap[0]
817 add $A[1],$N[1] # np[j]*m1+ap[j]*bp[0]
819 mov $N[1],-16($tp) # tp[j-1]
822 lea ($np,$num),$np # rewind $np
833 lea 16+128($tp),%rdx # where 256-byte mask is (+size optimization)
837 for($i=0;$i<$STRIDE/16;$i+=4) {
839 movdqa `16*($i+0)-128`($bp),%xmm0
840 movdqa `16*($i+1)-128`($bp),%xmm1
841 movdqa `16*($i+2)-128`($bp),%xmm2
842 movdqa `16*($i+3)-128`($bp),%xmm3
843 pand `16*($i+0)-128`(%rdx),%xmm0
844 pand `16*($i+1)-128`(%rdx),%xmm1
846 pand `16*($i+2)-128`(%rdx),%xmm2
848 pand `16*($i+3)-128`(%rdx),%xmm3
855 pshufd \$0x4e,%xmm4,%xmm0
858 movq %xmm0,$m0 # m0=bp[i]
862 mulq $m0 # ap[0]*bp[i]
863 add %rax,$A[0] # ap[0]*bp[i]+tp[0]
867 imulq $A[0],$m1 # tp[0]*n0
869 mov $N[1],($tp) # store upmost overflow bit
871 lea ($tp,$num),$tp # rewind $tp
874 add %rax,$A[0] # "$N[0]", discarded
879 mulq $m0 # ap[j]*bp[i]
883 add 8($tp),$A[1] # +tp[1]
889 mov 16($ap,$num),%rax
891 add $A[1],$N[1] # np[j]*m1+ap[j]*bp[i]+tp[j]
892 lea 4*8($num),$j # j=4
900 mulq $m0 # ap[j]*bp[i]
904 add 16($tp),$A[0] # ap[j]*bp[i]+tp[j]
915 mov $N[1],-32($tp) # tp[j-1]
918 mulq $m0 # ap[j]*bp[i]
932 mov $N[0],-24($tp) # tp[j-1]
935 mulq $m0 # ap[j]*bp[i]
939 add ($tp),$A[0] # ap[j]*bp[i]+tp[j]
949 mov $N[1],-16($tp) # tp[j-1]
952 mulq $m0 # ap[j]*bp[i]
967 mov $N[0],-8($tp) # tp[j-1]
973 mulq $m0 # ap[j]*bp[i]
977 add 16($tp),$A[0] # ap[j]*bp[i]+tp[j]
988 mov $N[1],-32($tp) # tp[j-1]
991 mulq $m0 # ap[j]*bp[i]
1002 mov ($ap,$num),%rax # ap[0]
1006 mov $N[0],-24($tp) # tp[j-1]
1009 mov $N[1],-16($tp) # tp[j-1]
1010 lea ($np,$num),$np # rewind $np
1015 add ($tp),$N[0] # pull upmost overflow bit
1016 adc \$0,$N[1] # upmost overflow bit
1025 sub $N[0],$m1 # compare top-most words
1026 adc $j,$j # $j is zero
1028 sub $N[1],%rax # %rax=-$N[1]
1029 lea ($tp,$num),%rbx # tptr in .sqr4x_sub
1031 lea ($np),%rbp # nptr in .sqr4x_sub
1034 mov 56+8(%rsp),%rdi # rptr in .sqr4x_sub
1035 dec %r12 # so that after 'not' we get -n[0]
1040 jmp .Lsqr4x_sub_entry
1043 my @ri=("%rax",$bp,$m0,$m1);
1047 lea ($tp,$num),$tp # rewind $tp
1049 lea ($np,$N[1],8),$np
1050 mov 56+8(%rsp),$rp # restore $rp
1059 sbb 16*0($np),@ri[0]
1061 sbb 16*1($np),@ri[1]
1064 sbb 16*2($np),@ri[2]
1066 sbb 16*3($np),@ri[3]
1081 .size mul4x_internal,.-mul4x_internal
1085 ######################################################################
1087 my $rptr="%rdi"; # BN_ULONG *rptr,
1088 my $aptr="%rsi"; # const BN_ULONG *aptr,
1089 my $bptr="%rdx"; # const void *table,
1090 my $nptr="%rcx"; # const BN_ULONG *nptr,
1091 my $n0 ="%r8"; # const BN_ULONG *n0);
1092 my $num ="%r9"; # int num, has to be divisible by 8
1095 my ($i,$j,$tptr)=("%rbp","%rcx",$rptr);
1096 my @A0=("%r10","%r11");
1097 my @A1=("%r12","%r13");
1098 my ($a0,$a1,$ai)=("%r14","%r15","%rbx");
1102 .type bn_power5,\@function,6
1107 .cfi_def_cfa_register %rax
1109 $code.=<<___ if ($addx);
1110 mov OPENSSL_ia32cap_P+8(%rip),%r11d
1112 cmp \$0x80108,%r11d # check for AD*X+BMI2+BMI1
1130 shl \$3,${num}d # convert $num to bytes
1131 lea ($num,$num,2),%r10d # 3*$num
1135 ##############################################################
1136 # Ensure that stack frame doesn't alias with $rptr+3*$num
1137 # modulo 4096, which covers ret[num], am[num] and n[num]
1138 # (see bn_exp.c). This is done to allow memory disambiguation
1139 # logic do its magic. [Extra 256 bytes is for power mask
1140 # calculated from 7th argument, the index.]
1142 lea -320(%rsp,$num,2),%r11
1148 sub %r11,%rbp # align with $aptr
1149 lea -320(%rbp,$num,2),%rbp # future alloca(frame+2*num*8+256)
1154 lea 4096-320(,$num,2),%r10
1155 lea -320(%rbp,$num,2),%rbp # future alloca(frame+2*num*8+256)
1165 lea (%rbp,%r11),%rsp
1169 jmp .Lpwr_page_walk_done
1172 lea -4096(%rsp),%rsp
1176 .Lpwr_page_walk_done:
1181 ##############################################################
1184 # +0 saved $num, used in reduction section
1185 # +8 &t[2*$num], used in reduction section
1191 mov %rax, 40(%rsp) # save original %rsp
1192 .cfi_cfa_expression %rsp+40,deref,+8
1194 movq $rptr,%xmm1 # save $rptr, used in sqr8x
1195 movq $nptr,%xmm2 # save $nptr
1196 movq %r10, %xmm3 # -$num, used in sqr8x
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
1207 call __bn_sqr8x_internal
1208 call __bn_post4x_internal
1218 mov 40(%rsp),%rsi # restore %rsp
1234 .cfi_def_cfa_register %rsp
1238 .size bn_power5,.-bn_power5
1240 .globl bn_sqr8x_internal
1241 .hidden bn_sqr8x_internal
1242 .type bn_sqr8x_internal,\@abi-omnipotent
1245 __bn_sqr8x_internal:
1247 ##############################################################
1250 # a) multiply-n-add everything but a[i]*a[i];
1251 # b) shift result of a) by 1 to the left and accumulate
1252 # a[i]*a[i] products;
1254 ##############################################################
1320 lea 32(%r10),$i # $i=-($num-32)
1321 lea ($aptr,$num),$aptr # end of a[] buffer, ($aptr,$i)=&ap[2]
1323 mov $num,$j # $j=$num
1325 # comments apply to $num==8 case
1326 mov -32($aptr,$i),$a0 # a[0]
1327 lea 48+8(%rsp,$num,2),$tptr # end of tp[] buffer, &tp[2*$num]
1328 mov -24($aptr,$i),%rax # a[1]
1329 lea -32($tptr,$i),$tptr # end of tp[] window, &tp[2*$num-"$i"]
1330 mov -16($aptr,$i),$ai # a[2]
1334 mov %rax,$A0[0] # a[1]*a[0]
1337 mov $A0[0],-24($tptr,$i) # t[1]
1343 mov $A0[1],-16($tptr,$i) # t[2]
1347 mov -8($aptr,$i),$ai # a[3]
1349 mov %rax,$A1[0] # a[2]*a[1]+t[3]
1355 add %rax,$A0[0] # a[3]*a[0]+a[2]*a[1]+t[3]
1361 mov $A0[0],-8($tptr,$j) # t[3]
1366 mov ($aptr,$j),$ai # a[4]
1368 add %rax,$A1[1] # a[3]*a[1]+t[4]
1374 add %rax,$A0[1] # a[4]*a[0]+a[3]*a[1]+t[4]
1376 mov 8($aptr,$j),$ai # a[5]
1384 add %rax,$A1[0] # a[4]*a[3]+t[5]
1386 mov $A0[1],($tptr,$j) # t[4]
1391 add %rax,$A0[0] # a[5]*a[2]+a[4]*a[3]+t[5]
1393 mov 16($aptr,$j),$ai # a[6]
1400 add %rax,$A1[1] # a[5]*a[3]+t[6]
1402 mov $A0[0],8($tptr,$j) # t[5]
1407 add %rax,$A0[1] # a[6]*a[2]+a[5]*a[3]+t[6]
1409 mov 24($aptr,$j),$ai # a[7]
1417 add %rax,$A1[0] # a[6]*a[5]+t[7]
1419 mov $A0[1],16($tptr,$j) # t[6]
1425 add %rax,$A0[0] # a[7]*a[4]+a[6]*a[5]+t[6]
1431 mov $A0[0],-8($tptr,$j) # t[7]
1443 mov $A1[1],($tptr) # t[8]
1445 mov %rdx,8($tptr) # t[9]
1449 .Lsqr4x_outer: # comments apply to $num==6 case
1450 mov -32($aptr,$i),$a0 # a[0]
1451 lea 48+8(%rsp,$num,2),$tptr # end of tp[] buffer, &tp[2*$num]
1452 mov -24($aptr,$i),%rax # a[1]
1453 lea -32($tptr,$i),$tptr # end of tp[] window, &tp[2*$num-"$i"]
1454 mov -16($aptr,$i),$ai # a[2]
1458 mov -24($tptr,$i),$A0[0] # t[1]
1459 add %rax,$A0[0] # a[1]*a[0]+t[1]
1462 mov $A0[0],-24($tptr,$i) # t[1]
1469 add -16($tptr,$i),$A0[1] # a[2]*a[0]+t[2]
1472 mov $A0[1],-16($tptr,$i) # t[2]
1476 mov -8($aptr,$i),$ai # a[3]
1478 add %rax,$A1[0] # a[2]*a[1]+t[3]
1481 add -8($tptr,$i),$A1[0]
1486 add %rax,$A0[0] # a[3]*a[0]+a[2]*a[1]+t[3]
1492 mov $A0[0],-8($tptr,$i) # t[3]
1499 mov ($aptr,$j),$ai # a[4]
1501 add %rax,$A1[1] # a[3]*a[1]+t[4]
1505 add ($tptr,$j),$A1[1]
1510 add %rax,$A0[1] # a[4]*a[0]+a[3]*a[1]+t[4]
1512 mov 8($aptr,$j),$ai # a[5]
1519 add %rax,$A1[0] # a[4]*a[3]+t[5]
1520 mov $A0[1],($tptr,$j) # t[4]
1524 add 8($tptr,$j),$A1[0]
1529 add %rax,$A0[0] # a[5]*a[2]+a[4]*a[3]+t[5]
1535 mov $A0[0],-8($tptr,$j) # t[5], "preloaded t[1]" below
1547 mov $A1[1],($tptr) # t[6], "preloaded t[2]" below
1549 mov %rdx,8($tptr) # t[7], "preloaded t[3]" below
1554 # comments apply to $num==4 case
1555 mov -32($aptr),$a0 # a[0]
1556 lea 48+8(%rsp,$num,2),$tptr # end of tp[] buffer, &tp[2*$num]
1557 mov -24($aptr),%rax # a[1]
1558 lea -32($tptr,$i),$tptr # end of tp[] window, &tp[2*$num-"$i"]
1559 mov -16($aptr),$ai # a[2]
1563 add %rax,$A0[0] # a[1]*a[0]+t[1], preloaded t[1]
1571 mov $A0[0],-24($tptr) # t[1]
1574 add $A1[1],$A0[1] # a[2]*a[0]+t[2], preloaded t[2]
1575 mov -8($aptr),$ai # a[3]
1579 add %rax,$A1[0] # a[2]*a[1]+t[3], preloaded t[3]
1581 mov $A0[1],-16($tptr) # t[2]
1586 add %rax,$A0[0] # a[3]*a[0]+a[2]*a[1]+t[3]
1592 mov $A0[0],-8($tptr) # t[3]
1596 mov -16($aptr),%rax # a[2]
1601 mov $A1[1],($tptr) # t[4]
1603 mov %rdx,8($tptr) # t[5]
1608 my ($shift,$carry)=($a0,$a1);
1609 my @S=(@A1,$ai,$n0);
1613 sub $num,$i # $i=16-$num
1616 add $A1[0],%rax # t[5]
1618 mov %rax,8($tptr) # t[5]
1619 mov %rdx,16($tptr) # t[6]
1620 mov $carry,24($tptr) # t[7]
1622 mov -16($aptr,$i),%rax # a[0]
1623 lea 48+8(%rsp),$tptr
1624 xor $A0[0],$A0[0] # t[0]
1625 mov 8($tptr),$A0[1] # t[1]
1627 lea ($shift,$A0[0],2),$S[0] # t[2*i]<<1 | shift
1629 lea ($j,$A0[1],2),$S[1] # t[2*i+1]<<1 |
1631 or $A0[0],$S[1] # | t[2*i]>>63
1632 mov 16($tptr),$A0[0] # t[2*i+2] # prefetch
1633 mov $A0[1],$shift # shift=t[2*i+1]>>63
1634 mul %rax # a[i]*a[i]
1635 neg $carry # mov $carry,cf
1636 mov 24($tptr),$A0[1] # t[2*i+2+1] # prefetch
1638 mov -8($aptr,$i),%rax # a[i+1] # prefetch
1642 lea ($shift,$A0[0],2),$S[2] # t[2*i]<<1 | shift
1644 sbb $carry,$carry # mov cf,$carry
1646 lea ($j,$A0[1],2),$S[3] # t[2*i+1]<<1 |
1648 or $A0[0],$S[3] # | t[2*i]>>63
1649 mov 32($tptr),$A0[0] # t[2*i+2] # prefetch
1650 mov $A0[1],$shift # shift=t[2*i+1]>>63
1651 mul %rax # a[i]*a[i]
1652 neg $carry # mov $carry,cf
1653 mov 40($tptr),$A0[1] # t[2*i+2+1] # prefetch
1655 mov 0($aptr,$i),%rax # a[i+1] # prefetch
1660 sbb $carry,$carry # mov cf,$carry
1662 jmp .Lsqr4x_shift_n_add
1665 .Lsqr4x_shift_n_add:
1666 lea ($shift,$A0[0],2),$S[0] # t[2*i]<<1 | shift
1668 lea ($j,$A0[1],2),$S[1] # t[2*i+1]<<1 |
1670 or $A0[0],$S[1] # | t[2*i]>>63
1671 mov -16($tptr),$A0[0] # t[2*i+2] # prefetch
1672 mov $A0[1],$shift # shift=t[2*i+1]>>63
1673 mul %rax # a[i]*a[i]
1674 neg $carry # mov $carry,cf
1675 mov -8($tptr),$A0[1] # t[2*i+2+1] # prefetch
1677 mov -8($aptr,$i),%rax # a[i+1] # prefetch
1678 mov $S[0],-32($tptr)
1681 lea ($shift,$A0[0],2),$S[2] # t[2*i]<<1 | shift
1682 mov $S[1],-24($tptr)
1683 sbb $carry,$carry # mov cf,$carry
1685 lea ($j,$A0[1],2),$S[3] # t[2*i+1]<<1 |
1687 or $A0[0],$S[3] # | t[2*i]>>63
1688 mov 0($tptr),$A0[0] # t[2*i+2] # prefetch
1689 mov $A0[1],$shift # shift=t[2*i+1]>>63
1690 mul %rax # a[i]*a[i]
1691 neg $carry # mov $carry,cf
1692 mov 8($tptr),$A0[1] # t[2*i+2+1] # prefetch
1694 mov 0($aptr,$i),%rax # a[i+1] # prefetch
1695 mov $S[2],-16($tptr)
1698 lea ($shift,$A0[0],2),$S[0] # t[2*i]<<1 | shift
1700 sbb $carry,$carry # mov cf,$carry
1702 lea ($j,$A0[1],2),$S[1] # t[2*i+1]<<1 |
1704 or $A0[0],$S[1] # | t[2*i]>>63
1705 mov 16($tptr),$A0[0] # t[2*i+2] # prefetch
1706 mov $A0[1],$shift # shift=t[2*i+1]>>63
1707 mul %rax # a[i]*a[i]
1708 neg $carry # mov $carry,cf
1709 mov 24($tptr),$A0[1] # t[2*i+2+1] # prefetch
1711 mov 8($aptr,$i),%rax # a[i+1] # prefetch
1715 lea ($shift,$A0[0],2),$S[2] # t[2*i]<<1 | shift
1717 sbb $carry,$carry # mov cf,$carry
1719 lea ($j,$A0[1],2),$S[3] # t[2*i+1]<<1 |
1721 or $A0[0],$S[3] # | t[2*i]>>63
1722 mov 32($tptr),$A0[0] # t[2*i+2] # prefetch
1723 mov $A0[1],$shift # shift=t[2*i+1]>>63
1724 mul %rax # a[i]*a[i]
1725 neg $carry # mov $carry,cf
1726 mov 40($tptr),$A0[1] # t[2*i+2+1] # prefetch
1728 mov 16($aptr,$i),%rax # a[i+1] # prefetch
1732 sbb $carry,$carry # mov cf,$carry
1735 jnz .Lsqr4x_shift_n_add
1737 lea ($shift,$A0[0],2),$S[0] # t[2*i]<<1 | shift
1740 lea ($j,$A0[1],2),$S[1] # t[2*i+1]<<1 |
1742 or $A0[0],$S[1] # | t[2*i]>>63
1743 mov -16($tptr),$A0[0] # t[2*i+2] # prefetch
1744 mov $A0[1],$shift # shift=t[2*i+1]>>63
1745 mul %rax # a[i]*a[i]
1746 neg $carry # mov $carry,cf
1747 mov -8($tptr),$A0[1] # t[2*i+2+1] # prefetch
1749 mov -8($aptr),%rax # a[i+1] # prefetch
1750 mov $S[0],-32($tptr)
1753 lea ($shift,$A0[0],2),$S[2] # t[2*i]<<1|shift
1754 mov $S[1],-24($tptr)
1755 sbb $carry,$carry # mov cf,$carry
1757 lea ($j,$A0[1],2),$S[3] # t[2*i+1]<<1 |
1759 or $A0[0],$S[3] # | t[2*i]>>63
1760 mul %rax # a[i]*a[i]
1761 neg $carry # mov $carry,cf
1764 mov $S[2],-16($tptr)
1768 ######################################################################
1769 # Montgomery reduction part, "word-by-word" algorithm.
1771 # This new path is inspired by multiple submissions from Intel, by
1772 # Shay Gueron, Vlad Krasnov, Erdinc Ozturk, James Guilford,
1775 my ($nptr,$tptr,$carry,$m0)=("%rbp","%rdi","%rsi","%rbx");
1779 __bn_sqr8x_reduction:
1781 lea ($nptr,$num),%rcx # end of n[]
1782 lea 48+8(%rsp,$num,2),%rdx # end of t[] buffer
1784 lea 48+8(%rsp,$num),$tptr # end of initial t[] window
1787 jmp .L8x_reduction_loop
1790 .L8x_reduction_loop:
1791 lea ($tptr,$num),$tptr # start of current t[] window
1801 mov %rax,(%rdx) # store top-most carry bit
1802 lea 8*8($tptr),$tptr
1806 imulq 32+8(%rsp),$m0 # n0*a[0]
1807 mov 8*0($nptr),%rax # n[0]
1814 mov 8*1($nptr),%rax # n[1]
1824 mov $m0,48-8+8(%rsp,%rcx,8) # put aside n0*a[i]
1833 mov 32+8(%rsp),$carry # pull n0, borrow $carry
1841 imulq %r8,$carry # modulo-scheduled
1871 mov $carry,$m0 # n0*a[i]
1873 mov 8*0($nptr),%rax # n[0]
1882 lea 8*8($nptr),$nptr
1884 mov 8+8(%rsp),%rdx # pull end of t[]
1885 cmp 0+8(%rsp),$nptr # end of n[]?
1897 sbb $carry,$carry # top carry
1899 mov 48+56+8(%rsp),$m0 # pull n0*a[0]
1909 mov %r8,($tptr) # save result
1918 lea 8($tptr),$tptr # $tptr++
1963 mov 48-16+8(%rsp,%rcx,8),$m0# pull n0*a[i]
1967 mov 8*0($nptr),%rax # pull n[0]
1974 lea 8*8($nptr),$nptr
1975 mov 8+8(%rsp),%rdx # pull end of t[]
1976 cmp 0+8(%rsp),$nptr # end of n[]?
1977 jae .L8x_tail_done # break out of loop
1979 mov 48+56+8(%rsp),$m0 # pull n0*a[0]
1981 mov 8*0($nptr),%rax # pull n[0]
1990 sbb $carry,$carry # top carry
1998 add (%rdx),%r8 # can this overflow?
2018 adc \$0,%rax # top-most carry
2019 mov -8($nptr),%rcx # np[num-1]
2022 movq %xmm2,$nptr # restore $nptr
2024 mov %r8,8*0($tptr) # store top 512 bits
2026 movq %xmm3,$num # $num is %r9, can't be moved upwards
2033 lea 8*8($tptr),$tptr
2035 cmp %rdx,$tptr # end of t[]?
2036 jb .L8x_reduction_loop
2039 .size bn_sqr8x_internal,.-bn_sqr8x_internal
2042 ##############################################################
2043 # Post-condition, 4x unrolled
2046 my ($tptr,$nptr)=("%rbx","%rbp");
2048 .type __bn_post4x_internal,\@abi-omnipotent
2050 __bn_post4x_internal:
2053 lea (%rdi,$num),$tptr # %rdi was $tptr above
2055 movq %xmm1,$rptr # restore $rptr
2057 movq %xmm1,$aptr # prepare for back-to-back call
2059 dec %r12 # so that after 'not' we get -n[0]
2064 jmp .Lsqr4x_sub_entry
2073 lea 8*4($nptr),$nptr
2083 neg %r10 # mov %r10,%cf
2089 lea 8*4($tptr),$tptr
2091 sbb %r10,%r10 # mov %cf,%r10
2094 lea 8*4($rptr),$rptr
2099 mov $num,%r10 # prepare for back-to-back call
2100 neg $num # restore $num
2103 .size __bn_post4x_internal,.-__bn_post4x_internal
2109 my $bp="%rdx"; # restore original value
2112 .type bn_mulx4x_mont_gather5,\@function,6
2114 bn_mulx4x_mont_gather5:
2117 .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). This is done to allow memory disambiguation
2142 # logic do its magic. [Extra [num] is allocated in order
2143 # to align with bn_power5's frame, which is cleansed after
2144 # completing exponentiation. Extra 256 bytes is for power mask
2145 # calculated from 7th argument, the index.]
2147 lea -320(%rsp,$num,2),%r11
2153 sub %r11,%rbp # align with $aptr
2154 lea -320(%rbp,$num,2),%rbp # future alloca(frame+2*$num*8+256)
2158 lea 4096-320(,$num,2),%r10
2159 lea -320(%rbp,$num,2),%rbp # future alloca(frame+2*$num*8+256)
2165 and \$-64,%rbp # ensure alignment
2169 lea (%rbp,%r11),%rsp
2172 ja .Lmulx4x_page_walk
2173 jmp .Lmulx4x_page_walk_done
2176 lea -4096(%rsp),%rsp
2179 ja .Lmulx4x_page_walk
2180 .Lmulx4x_page_walk_done:
2182 ##############################################################
2185 # +8 off-loaded &b[i]
2194 mov $n0, 32(%rsp) # save *n0
2195 mov %rax,40(%rsp) # save original %rsp
2196 .cfi_cfa_expression %rsp+40,deref,+8
2198 call mulx4x_internal
2200 mov 40(%rsp),%rsi # restore %rsp
2217 .cfi_def_cfa_register %rsp
2221 .size bn_mulx4x_mont_gather5,.-bn_mulx4x_mont_gather5
2223 .type mulx4x_internal,\@abi-omnipotent
2227 mov $num,8(%rsp) # save -$num (it was in bytes)
2229 neg $num # restore $num
2231 neg %r10 # restore $num
2232 lea 128($bp,$num),%r13 # end of powers table (+size optimization)
2234 movd `($win64?56:8)`(%rax),%xmm5 # load 7th argument
2236 lea .Linc(%rip),%rax
2237 mov %r13,16+8(%rsp) # end of b[num]
2238 mov $num,24+8(%rsp) # inner counter
2239 mov $rp, 56+8(%rsp) # save $rp
2241 my ($aptr, $bptr, $nptr, $tptr, $mi, $bi, $zero, $num)=
2242 ("%rsi","%rdi","%rcx","%rbx","%r8","%r9","%rbp","%rax");
2244 my $STRIDE=2**5*8; # 5 is "window size"
2245 my $N=$STRIDE/4; # should match cache line size
2247 movdqa 0(%rax),%xmm0 # 00000001000000010000000000000000
2248 movdqa 16(%rax),%xmm1 # 00000002000000020000000200000002
2249 lea 88-112(%rsp,%r10),%r10 # place the mask after tp[num+1] (+ICache optimization)
2250 lea 128($bp),$bptr # size optimization
2252 pshufd \$0,%xmm5,%xmm5 # broadcast index
2257 ########################################################################
2258 # calculate mask by comparing 0..31 to index and save result to stack
2263 pcmpeqd %xmm5,%xmm0 # compare to 1,0
2266 for($i=0;$i<$STRIDE/16-4;$i+=4) {
2269 pcmpeqd %xmm5,%xmm1 # compare to 3,2
2270 movdqa %xmm0,`16*($i+0)+112`(%r10)
2274 pcmpeqd %xmm5,%xmm2 # compare to 5,4
2275 movdqa %xmm1,`16*($i+1)+112`(%r10)
2279 pcmpeqd %xmm5,%xmm3 # compare to 7,6
2280 movdqa %xmm2,`16*($i+2)+112`(%r10)
2285 movdqa %xmm3,`16*($i+3)+112`(%r10)
2289 $code.=<<___; # last iteration can be optimized
2293 movdqa %xmm0,`16*($i+0)+112`(%r10)
2297 movdqa %xmm1,`16*($i+1)+112`(%r10)
2300 movdqa %xmm2,`16*($i+2)+112`(%r10)
2302 pand `16*($i+0)-128`($bptr),%xmm0 # while it's still in register
2303 pand `16*($i+1)-128`($bptr),%xmm1
2304 pand `16*($i+2)-128`($bptr),%xmm2
2305 movdqa %xmm3,`16*($i+3)+112`(%r10)
2306 pand `16*($i+3)-128`($bptr),%xmm3
2310 for($i=0;$i<$STRIDE/16-4;$i+=4) {
2312 movdqa `16*($i+0)-128`($bptr),%xmm4
2313 movdqa `16*($i+1)-128`($bptr),%xmm5
2314 movdqa `16*($i+2)-128`($bptr),%xmm2
2315 pand `16*($i+0)+112`(%r10),%xmm4
2316 movdqa `16*($i+3)-128`($bptr),%xmm3
2317 pand `16*($i+1)+112`(%r10),%xmm5
2319 pand `16*($i+2)+112`(%r10),%xmm2
2321 pand `16*($i+3)+112`(%r10),%xmm3
2328 pshufd \$0x4e,%xmm0,%xmm1
2330 lea $STRIDE($bptr),$bptr
2331 movq %xmm0,%rdx # bp[0]
2332 lea 64+8*4+8(%rsp),$tptr
2335 mulx 0*8($aptr),$mi,%rax # a[0]*b[0]
2336 mulx 1*8($aptr),%r11,%r12 # a[1]*b[0]
2338 mulx 2*8($aptr),%rax,%r13 # ...
2341 mulx 3*8($aptr),%rax,%r14
2344 imulq 32+8(%rsp),$mi # "t[0]"*n0
2345 xor $zero,$zero # cf=0, of=0
2348 mov $bptr,8+8(%rsp) # off-load &b[i]
2350 lea 4*8($aptr),$aptr
2352 adcx $zero,%r14 # cf=0
2354 mulx 0*8($nptr),%rax,%r10
2355 adcx %rax,%r15 # discarded
2357 mulx 1*8($nptr),%rax,%r11
2360 mulx 2*8($nptr),%rax,%r12
2361 mov 24+8(%rsp),$bptr # counter value
2362 mov %r10,-8*4($tptr)
2365 mulx 3*8($nptr),%rax,%r15
2367 mov %r11,-8*3($tptr)
2369 adox $zero,%r15 # of=0
2370 lea 4*8($nptr),$nptr
2371 mov %r12,-8*2($tptr)
2376 adcx $zero,%r15 # cf=0, modulo-scheduled
2377 mulx 0*8($aptr),%r10,%rax # a[4]*b[0]
2379 mulx 1*8($aptr),%r11,%r14 # a[5]*b[0]
2381 mulx 2*8($aptr),%r12,%rax # ...
2383 mulx 3*8($aptr),%r13,%r14
2387 adcx $zero,%r14 # cf=0
2388 lea 4*8($aptr),$aptr
2389 lea 4*8($tptr),$tptr
2392 mulx 0*8($nptr),%rax,%r15
2395 mulx 1*8($nptr),%rax,%r15
2398 mulx 2*8($nptr),%rax,%r15
2399 mov %r10,-5*8($tptr)
2401 mov %r11,-4*8($tptr)
2403 mulx 3*8($nptr),%rax,%r15
2405 mov %r12,-3*8($tptr)
2408 lea 4*8($nptr),$nptr
2409 mov %r13,-2*8($tptr)
2411 dec $bptr # of=0, pass cf
2414 mov 8(%rsp),$num # load -num
2415 adc $zero,%r15 # modulo-scheduled
2416 lea ($aptr,$num),$aptr # rewind $aptr
2418 mov 8+8(%rsp),$bptr # re-load &b[i]
2419 adc $zero,$zero # top-most carry
2420 mov %r14,-1*8($tptr)
2425 lea 16-256($tptr),%r10 # where 256-byte mask is (+density control)
2430 for($i=0;$i<$STRIDE/16;$i+=4) {
2432 movdqa `16*($i+0)-128`($bptr),%xmm0
2433 movdqa `16*($i+1)-128`($bptr),%xmm1
2434 movdqa `16*($i+2)-128`($bptr),%xmm2
2435 pand `16*($i+0)+256`(%r10),%xmm0
2436 movdqa `16*($i+3)-128`($bptr),%xmm3
2437 pand `16*($i+1)+256`(%r10),%xmm1
2439 pand `16*($i+2)+256`(%r10),%xmm2
2441 pand `16*($i+3)+256`(%r10),%xmm3
2448 pshufd \$0x4e,%xmm4,%xmm0
2450 lea $STRIDE($bptr),$bptr
2451 movq %xmm0,%rdx # m0=bp[i]
2453 mov $zero,($tptr) # save top-most carry
2454 lea 4*8($tptr,$num),$tptr # rewind $tptr
2455 mulx 0*8($aptr),$mi,%r11 # a[0]*b[i]
2456 xor $zero,$zero # cf=0, of=0
2458 mulx 1*8($aptr),%r14,%r12 # a[1]*b[i]
2459 adox -4*8($tptr),$mi # +t[0]
2461 mulx 2*8($aptr),%r15,%r13 # ...
2462 adox -3*8($tptr),%r11
2464 mulx 3*8($aptr),%rdx,%r14
2465 adox -2*8($tptr),%r12
2467 lea ($nptr,$num),$nptr # rewind $nptr
2468 lea 4*8($aptr),$aptr
2469 adox -1*8($tptr),%r13
2474 imulq 32+8(%rsp),$mi # "t[0]"*n0
2477 xor $zero,$zero # cf=0, of=0
2478 mov $bptr,8+8(%rsp) # off-load &b[i]
2480 mulx 0*8($nptr),%rax,%r10
2481 adcx %rax,%r15 # discarded
2483 mulx 1*8($nptr),%rax,%r11
2486 mulx 2*8($nptr),%rax,%r12
2489 mulx 3*8($nptr),%rax,%r15
2491 mov 24+8(%rsp),$bptr # counter value
2492 mov %r10,-8*4($tptr)
2494 mov %r11,-8*3($tptr)
2495 adox $zero,%r15 # of=0
2496 mov %r12,-8*2($tptr)
2497 lea 4*8($nptr),$nptr
2502 mulx 0*8($aptr),%r10,%rax # a[4]*b[i]
2503 adcx $zero,%r15 # cf=0, modulo-scheduled
2505 mulx 1*8($aptr),%r11,%r14 # a[5]*b[i]
2506 adcx 0*8($tptr),%r10
2508 mulx 2*8($aptr),%r12,%rax # ...
2509 adcx 1*8($tptr),%r11
2511 mulx 3*8($aptr),%r13,%r14
2513 adcx 2*8($tptr),%r12
2515 adcx 3*8($tptr),%r13
2516 adox $zero,%r14 # of=0
2517 lea 4*8($aptr),$aptr
2518 lea 4*8($tptr),$tptr
2519 adcx $zero,%r14 # cf=0
2522 mulx 0*8($nptr),%rax,%r15
2525 mulx 1*8($nptr),%rax,%r15
2528 mulx 2*8($nptr),%rax,%r15
2529 mov %r10,-5*8($tptr)
2532 mov %r11,-4*8($tptr)
2533 mulx 3*8($nptr),%rax,%r15
2535 lea 4*8($nptr),$nptr
2536 mov %r12,-3*8($tptr)
2539 mov %r13,-2*8($tptr)
2541 dec $bptr # of=0, pass cf
2544 mov 0+8(%rsp),$num # load -num
2545 adc $zero,%r15 # modulo-scheduled
2546 sub 0*8($tptr),$bptr # pull top-most carry to %cf
2547 mov 8+8(%rsp),$bptr # re-load &b[i]
2550 lea ($aptr,$num),$aptr # rewind $aptr
2551 adc $zero,$zero # top-most carry
2552 mov %r14,-1*8($tptr)
2559 mov ($nptr,$num),%r12
2560 lea ($nptr,$num),%rbp # rewind $nptr
2562 lea ($tptr,$num),%rdi # rewind $tptr
2565 sub %r14,%r10 # compare top-most words
2569 sub %r8,%rax # %rax=-%r8
2570 mov 56+8(%rsp),%rdx # restore rp
2571 dec %r12 # so that after 'not' we get -n[0]
2576 jmp .Lsqrx4x_sub_entry # common post-condition
2578 .size mulx4x_internal,.-mulx4x_internal
2581 ######################################################################
2583 my $rptr="%rdi"; # BN_ULONG *rptr,
2584 my $aptr="%rsi"; # const BN_ULONG *aptr,
2585 my $bptr="%rdx"; # const void *table,
2586 my $nptr="%rcx"; # const BN_ULONG *nptr,
2587 my $n0 ="%r8"; # const BN_ULONG *n0);
2588 my $num ="%r9"; # int num, has to be divisible by 8
2591 my ($i,$j,$tptr)=("%rbp","%rcx",$rptr);
2592 my @A0=("%r10","%r11");
2593 my @A1=("%r12","%r13");
2594 my ($a0,$a1,$ai)=("%r14","%r15","%rbx");
2597 .type bn_powerx5,\@function,6
2602 .cfi_def_cfa_register %rax
2618 shl \$3,${num}d # convert $num to bytes
2619 lea ($num,$num,2),%r10 # 3*$num in bytes
2623 ##############################################################
2624 # Ensure that stack frame doesn't alias with $rptr+3*$num
2625 # modulo 4096, which covers ret[num], am[num] and n[num]
2626 # (see bn_exp.c). This is done to allow memory disambiguation
2627 # logic do its magic. [Extra 256 bytes is for power mask
2628 # calculated from 7th argument, the index.]
2630 lea -320(%rsp,$num,2),%r11
2636 sub %r11,%rbp # align with $aptr
2637 lea -320(%rbp,$num,2),%rbp # future alloca(frame+2*$num*8+256)
2642 lea 4096-320(,$num,2),%r10
2643 lea -320(%rbp,$num,2),%rbp # alloca(frame+2*$num*8+256)
2653 lea (%rbp,%r11),%rsp
2657 jmp .Lpwrx_page_walk_done
2660 lea -4096(%rsp),%rsp
2664 .Lpwrx_page_walk_done:
2669 ##############################################################
2672 # +0 saved $num, used in reduction section
2673 # +8 &t[2*$num], used in reduction section
2674 # +16 intermediate carry bit
2675 # +24 top-most carry bit, used in reduction section
2681 movq $rptr,%xmm1 # save $rptr
2682 movq $nptr,%xmm2 # save $nptr
2683 movq %r10, %xmm3 # -$num
2686 mov %rax, 40(%rsp) # save original %rsp
2687 .cfi_cfa_expression %rsp+40,deref,+8
2690 call __bn_sqrx8x_internal
2691 call __bn_postx4x_internal
2692 call __bn_sqrx8x_internal
2693 call __bn_postx4x_internal
2694 call __bn_sqrx8x_internal
2695 call __bn_postx4x_internal
2696 call __bn_sqrx8x_internal
2697 call __bn_postx4x_internal
2698 call __bn_sqrx8x_internal
2699 call __bn_postx4x_internal
2701 mov %r10,$num # -num
2707 call mulx4x_internal
2709 mov 40(%rsp),%rsi # restore %rsp
2726 .cfi_def_cfa_register %rsp
2730 .size bn_powerx5,.-bn_powerx5
2732 .globl bn_sqrx8x_internal
2733 .hidden bn_sqrx8x_internal
2734 .type bn_sqrx8x_internal,\@abi-omnipotent
2737 __bn_sqrx8x_internal:
2739 ##################################################################
2742 # a) multiply-n-add everything but a[i]*a[i];
2743 # b) shift result of a) by 1 to the left and accumulate
2744 # a[i]*a[i] products;
2746 ##################################################################
2747 # 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]
2778 # 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]
2781 my ($zero,$carry)=("%rbp","%rcx");
2784 lea 48+8(%rsp),$tptr
2785 lea ($aptr,$num),$aaptr
2786 mov $num,0+8(%rsp) # save $num
2787 mov $aaptr,8+8(%rsp) # save end of $aptr
2788 jmp .Lsqr8x_zero_start
2791 .byte 0x66,0x66,0x66,0x2e,0x0f,0x1f,0x84,0x00,0x00,0x00,0x00,0x00
2794 movdqa %xmm0,0*8($tptr)
2795 movdqa %xmm0,2*8($tptr)
2796 movdqa %xmm0,4*8($tptr)
2797 movdqa %xmm0,6*8($tptr)
2798 .Lsqr8x_zero_start: # aligned at 32
2799 movdqa %xmm0,8*8($tptr)
2800 movdqa %xmm0,10*8($tptr)
2801 movdqa %xmm0,12*8($tptr)
2802 movdqa %xmm0,14*8($tptr)
2803 lea 16*8($tptr),$tptr
2807 mov 0*8($aptr),%rdx # a[0], modulo-scheduled
2808 #xor %r9,%r9 # t[1], ex-$num, zero already
2815 lea 48+8(%rsp),$tptr
2816 xor $zero,$zero # cf=0, cf=0
2817 jmp .Lsqrx8x_outer_loop
2820 .Lsqrx8x_outer_loop:
2821 mulx 1*8($aptr),%r8,%rax # a[1]*a[0]
2822 adcx %r9,%r8 # a[1]*a[0]+=t[1]
2824 mulx 2*8($aptr),%r9,%rax # a[2]*a[0]
2827 .byte 0xc4,0xe2,0xab,0xf6,0x86,0x18,0x00,0x00,0x00 # mulx 3*8($aptr),%r10,%rax # ...
2830 .byte 0xc4,0xe2,0xa3,0xf6,0x86,0x20,0x00,0x00,0x00 # mulx 4*8($aptr),%r11,%rax
2833 mulx 5*8($aptr),%r12,%rax
2836 mulx 6*8($aptr),%r13,%rax
2839 mulx 7*8($aptr),%r14,%r15
2840 mov 1*8($aptr),%rdx # a[1]
2844 mov %r8,1*8($tptr) # t[1]
2845 mov %r9,2*8($tptr) # t[2]
2846 sbb $carry,$carry # mov %cf,$carry
2847 xor $zero,$zero # cf=0, of=0
2850 mulx 2*8($aptr),%r8,%rbx # a[2]*a[1]
2851 mulx 3*8($aptr),%r9,%rax # a[3]*a[1]
2854 mulx 4*8($aptr),%r10,%rbx # ...
2857 .byte 0xc4,0xe2,0xa3,0xf6,0x86,0x28,0x00,0x00,0x00 # mulx 5*8($aptr),%r11,%rax
2860 .byte 0xc4,0xe2,0x9b,0xf6,0x9e,0x30,0x00,0x00,0x00 # mulx 6*8($aptr),%r12,%rbx
2863 .byte 0xc4,0x62,0x93,0xf6,0xb6,0x38,0x00,0x00,0x00 # mulx 7*8($aptr),%r13,%r14
2864 mov 2*8($aptr),%rdx # a[2]
2868 adox $zero,%r14 # of=0
2869 adcx $zero,%r14 # cf=0
2871 mov %r8,3*8($tptr) # t[3]
2872 mov %r9,4*8($tptr) # t[4]
2874 mulx 3*8($aptr),%r8,%rbx # a[3]*a[2]
2875 mulx 4*8($aptr),%r9,%rax # a[4]*a[2]
2878 mulx 5*8($aptr),%r10,%rbx # ...
2881 .byte 0xc4,0xe2,0xa3,0xf6,0x86,0x30,0x00,0x00,0x00 # mulx 6*8($aptr),%r11,%rax
2884 .byte 0xc4,0x62,0x9b,0xf6,0xae,0x38,0x00,0x00,0x00 # mulx 7*8($aptr),%r12,%r13
2886 mov 3*8($aptr),%rdx # a[3]
2890 mov %r8,5*8($tptr) # t[5]
2891 mov %r9,6*8($tptr) # t[6]
2892 mulx 4*8($aptr),%r8,%rax # a[4]*a[3]
2893 adox $zero,%r13 # of=0
2894 adcx $zero,%r13 # cf=0
2896 mulx 5*8($aptr),%r9,%rbx # a[5]*a[3]
2899 mulx 6*8($aptr),%r10,%rax # ...
2902 mulx 7*8($aptr),%r11,%r12
2903 mov 4*8($aptr),%rdx # a[4]
2904 mov 5*8($aptr),%r14 # a[5]
2907 mov 6*8($aptr),%r15 # a[6]
2909 adox $zero,%r12 # of=0
2910 adcx $zero,%r12 # cf=0
2912 mov %r8,7*8($tptr) # t[7]
2913 mov %r9,8*8($tptr) # t[8]
2915 mulx %r14,%r9,%rax # a[5]*a[4]
2916 mov 7*8($aptr),%r8 # a[7]
2918 mulx %r15,%r10,%rbx # a[6]*a[4]
2921 mulx %r8,%r11,%rax # a[7]*a[4]
2922 mov %r14,%rdx # a[5]
2925 #adox $zero,%rax # of=0
2926 adcx $zero,%rax # cf=0
2928 mulx %r15,%r14,%rbx # a[6]*a[5]
2929 mulx %r8,%r12,%r13 # a[7]*a[5]
2930 mov %r15,%rdx # a[6]
2931 lea 8*8($aptr),$aptr
2938 mulx %r8,%r8,%r14 # a[7]*a[6]
2943 je .Lsqrx8x_outer_break
2945 neg $carry # mov $carry,%cf
2949 adcx 9*8($tptr),%r9 # +=t[9]
2950 adcx 10*8($tptr),%r10 # ...
2951 adcx 11*8($tptr),%r11
2952 adc 12*8($tptr),%r12
2953 adc 13*8($tptr),%r13
2954 adc 14*8($tptr),%r14
2955 adc 15*8($tptr),%r15
2957 lea 2*64($tptr),$tptr
2958 sbb %rax,%rax # mov %cf,$carry
2960 mov -64($aptr),%rdx # a[0]
2961 mov %rax,16+8(%rsp) # offload $carry
2962 mov $tptr,24+8(%rsp)
2964 #lea 8*8($tptr),$tptr # see 2*8*8($tptr) above
2965 xor %eax,%eax # cf=0, of=0
2971 mulx 0*8($aaptr),%rax,%r8 # a[8]*a[i]
2972 adcx %rax,%rbx # +=t[8]
2975 mulx 1*8($aaptr),%rax,%r9 # ...
2979 mulx 2*8($aaptr),%rax,%r10
2983 mulx 3*8($aaptr),%rax,%r11
2987 .byte 0xc4,0x62,0xfb,0xf6,0xa5,0x20,0x00,0x00,0x00 # mulx 4*8($aaptr),%rax,%r12
2991 mulx 5*8($aaptr),%rax,%r13
2995 mulx 6*8($aaptr),%rax,%r14
2996 mov %rbx,($tptr,%rcx,8) # store t[8+i]
3001 .byte 0xc4,0x62,0xfb,0xf6,0xbd,0x38,0x00,0x00,0x00 # mulx 7*8($aaptr),%rax,%r15
3002 mov 8($aptr,%rcx,8),%rdx # a[i]
3004 adox %rbx,%r15 # %rbx is 0, of=0
3005 adcx %rbx,%r15 # cf=0
3011 lea 8*8($aaptr),$aaptr
3013 cmp 8+8(%rsp),$aaptr # done?
3016 sub 16+8(%rsp),%rbx # mov 16(%rsp),%cf
3027 lea 8*8($tptr),$tptr
3029 sbb %rax,%rax # mov %cf,%rax
3030 xor %ebx,%ebx # cf=0, of=0
3031 mov %rax,16+8(%rsp) # offload carry
3037 sub 16+8(%rsp),%rbx # mov 16(%rsp),%cf
3039 mov 24+8(%rsp),$carry # initial $tptr, borrow $carry
3041 mov 0*8($aptr),%rdx # a[8], modulo-scheduled
3049 cmp $carry,$tptr # cf=0, of=0
3050 je .Lsqrx8x_outer_loop
3055 mov 2*8($carry),%r10
3057 mov 3*8($carry),%r11
3059 mov 4*8($carry),%r12
3061 mov 5*8($carry),%r13
3063 mov 6*8($carry),%r14
3065 mov 7*8($carry),%r15
3067 jmp .Lsqrx8x_outer_loop
3070 .Lsqrx8x_outer_break:
3071 mov %r9,9*8($tptr) # t[9]
3072 movq %xmm3,%rcx # -$num
3073 mov %r10,10*8($tptr) # ...
3074 mov %r11,11*8($tptr)
3075 mov %r12,12*8($tptr)
3076 mov %r13,13*8($tptr)
3077 mov %r14,14*8($tptr)
3082 lea 48+8(%rsp),$tptr
3083 mov ($aptr,$i),%rdx # a[0]
3085 mov 8($tptr),$A0[1] # t[1]
3086 xor $A0[0],$A0[0] # t[0], of=0, cf=0
3087 mov 0+8(%rsp),$num # restore $num
3089 mov 16($tptr),$A1[0] # t[2] # prefetch
3090 mov 24($tptr),$A1[1] # t[3] # prefetch
3091 #jmp .Lsqrx4x_shift_n_add # happens to be aligned
3094 .Lsqrx4x_shift_n_add:
3098 .byte 0x48,0x8b,0x94,0x0e,0x08,0x00,0x00,0x00 # mov 8($aptr,$i),%rdx # a[i+1] # prefetch
3099 .byte 0x4c,0x8b,0x97,0x20,0x00,0x00,0x00 # mov 32($tptr),$A0[0] # t[2*i+4] # prefetch
3102 mov 40($tptr),$A0[1] # t[2*i+4+1] # prefetch
3109 mov 16($aptr,$i),%rdx # a[i+2] # prefetch
3110 mov 48($tptr),$A1[0] # t[2*i+6] # prefetch
3113 mov 56($tptr),$A1[1] # t[2*i+6+1] # prefetch
3120 mov 24($aptr,$i),%rdx # a[i+3] # prefetch
3122 mov 64($tptr),$A0[0] # t[2*i+8] # prefetch
3125 mov 72($tptr),$A0[1] # t[2*i+8+1] # prefetch
3132 jrcxz .Lsqrx4x_shift_n_add_break
3133 .byte 0x48,0x8b,0x94,0x0e,0x00,0x00,0x00,0x00 # mov 0($aptr,$i),%rdx # a[i+4] # prefetch
3136 mov 80($tptr),$A1[0] # t[2*i+10] # prefetch
3137 mov 88($tptr),$A1[1] # t[2*i+10+1] # prefetch
3142 jmp .Lsqrx4x_shift_n_add
3145 .Lsqrx4x_shift_n_add_break:
3149 lea 64($tptr),$tptr # end of t[] buffer
3152 ######################################################################
3153 # Montgomery reduction part, "word-by-word" algorithm.
3155 # This new path is inspired by multiple submissions from Intel, by
3156 # Shay Gueron, Vlad Krasnov, Erdinc Ozturk, James Guilford,
3159 my ($nptr,$carry,$m0)=("%rbp","%rsi","%rdx");
3163 __bn_sqrx8x_reduction:
3164 xor %eax,%eax # initial top-most carry bit
3165 mov 32+8(%rsp),%rbx # n0
3166 mov 48+8(%rsp),%rdx # "%r8", 8*0($tptr)
3167 lea -8*8($nptr,$num),%rcx # end of n[]
3168 #lea 48+8(%rsp,$num,2),$tptr # end of t[] buffer
3169 mov %rcx, 0+8(%rsp) # save end of n[]
3170 mov $tptr,8+8(%rsp) # save end of t[]
3172 lea 48+8(%rsp),$tptr # initial t[] window
3173 jmp .Lsqrx8x_reduction_loop
3176 .Lsqrx8x_reduction_loop:
3182 imulq %rbx,%rdx # n0*a[i]
3186 mov %rax,24+8(%rsp) # store top-most carry bit
3188 lea 8*8($tptr),$tptr
3189 xor $carry,$carry # cf=0,of=0
3196 mulx 8*0($nptr),%rax,%r8 # n[0]
3197 adcx %rbx,%rax # discarded
3200 mulx 8*1($nptr),%rbx,%r9 # n[1]
3204 mulx 8*2($nptr),%rbx,%r10
3208 mulx 8*3($nptr),%rbx,%r11
3212 .byte 0xc4,0x62,0xe3,0xf6,0xa5,0x20,0x00,0x00,0x00 # mulx 8*4($nptr),%rbx,%r12
3218 mulx 32+8(%rsp),%rbx,%rdx # %rdx discarded
3220 mov %rax,64+48+8(%rsp,%rcx,8) # put aside n0*a[i]
3222 mulx 8*5($nptr),%rax,%r13
3226 mulx 8*6($nptr),%rax,%r14
3230 mulx 8*7($nptr),%rax,%r15
3233 adox $carry,%r15 # $carry is 0
3234 adcx $carry,%r15 # cf=0
3236 .byte 0x67,0x67,0x67
3240 mov $carry,%rax # xor %rax,%rax
3241 cmp 0+8(%rsp),$nptr # end of n[]?
3242 jae .Lsqrx8x_no_tail
3244 mov 48+8(%rsp),%rdx # pull n0*a[0]
3246 lea 8*8($nptr),$nptr
3249 adcx 8*2($tptr),%r10
3255 lea 8*8($tptr),$tptr
3256 sbb %rax,%rax # top carry
3258 xor $carry,$carry # of=0, cf=0
3265 mulx 8*0($nptr),%rax,%r8
3269 mulx 8*1($nptr),%rax,%r9
3273 mulx 8*2($nptr),%rax,%r10
3277 mulx 8*3($nptr),%rax,%r11
3281 .byte 0xc4,0x62,0xfb,0xf6,0xa5,0x20,0x00,0x00,0x00 # mulx 8*4($nptr),%rax,%r12
3285 mulx 8*5($nptr),%rax,%r13
3289 mulx 8*6($nptr),%rax,%r14
3293 mulx 8*7($nptr),%rax,%r15
3294 mov 72+48+8(%rsp,%rcx,8),%rdx # pull n0*a[i]
3297 mov %rbx,($tptr,%rcx,8) # save result
3299 adcx $carry,%r15 # cf=0
3304 cmp 0+8(%rsp),$nptr # end of n[]?
3305 jae .Lsqrx8x_tail_done # break out of loop
3307 sub 16+8(%rsp),$carry # mov 16(%rsp),%cf
3308 mov 48+8(%rsp),%rdx # pull n0*a[0]
3309 lea 8*8($nptr),$nptr
3318 lea 8*8($tptr),$tptr
3320 sub \$8,%rcx # mov \$-8,%rcx
3322 xor $carry,$carry # of=0, cf=0
3329 add 24+8(%rsp),%r8 # can this overflow?
3339 sub 16+8(%rsp),$carry # mov 16(%rsp),%cf
3340 .Lsqrx8x_no_tail: # %cf is 0 if jumped here
3344 mov 8*7($nptr),$carry
3345 movq %xmm2,$nptr # restore $nptr
3352 adc \$0,%rax # top-most carry
3354 mov 32+8(%rsp),%rbx # n0
3355 mov 8*8($tptr,%rcx),%rdx # modulo-scheduled "%r8"
3357 mov %r8,8*0($tptr) # store top 512 bits
3358 lea 8*8($tptr),%r8 # borrow %r8
3367 lea 8*8($tptr,%rcx),$tptr # start of current t[] window
3368 cmp 8+8(%rsp),%r8 # end of t[]?
3369 jb .Lsqrx8x_reduction_loop
3372 .size bn_sqrx8x_internal,.-bn_sqrx8x_internal
3375 ##############################################################
3376 # Post-condition, 4x unrolled
3379 my ($rptr,$nptr)=("%rdx","%rbp");
3382 __bn_postx4x_internal:
3385 mov %rcx,%r10 # -$num
3386 mov %rcx,%r9 # -$num
3389 #lea 48+8(%rsp,%r9),$tptr
3390 movq %xmm1,$rptr # restore $rptr
3391 movq %xmm1,$aptr # prepare for back-to-back call
3392 dec %r12 # so that after 'not' we get -n[0]
3397 jmp .Lsqrx4x_sub_entry
3407 lea 8*4($nptr),$nptr
3412 neg %r8 # mov %r8,%cf
3418 lea 8*4($tptr),$tptr
3420 sbb %r8,%r8 # mov %cf,%r8
3423 lea 8*4($rptr),$rptr
3428 neg %r9 # restore $num
3432 .size __bn_postx4x_internal,.-__bn_postx4x_internal
3437 my ($inp,$num,$tbl,$idx)=$win64?("%rcx","%edx","%r8", "%r9d") : # Win64 order
3438 ("%rdi","%esi","%rdx","%ecx"); # Unix order
3445 .type bn_get_bits5,\@abi-omnipotent
3458 movzw (%r10,$num,2),%eax
3463 .size bn_get_bits5,.-bn_get_bits5
3466 .type bn_scatter5,\@abi-omnipotent
3471 jz .Lscatter_epilogue
3472 lea ($tbl,$idx,8),$tbl
3483 .size bn_scatter5,.-bn_scatter5
3486 .type bn_gather5,\@abi-omnipotent
3489 .LSEH_begin_bn_gather5: # Win64 thing, but harmless in other cases
3491 # I can't trust assembler to use specific encoding:-(
3492 .byte 0x4c,0x8d,0x14,0x24 #lea (%rsp),%r10
3493 .byte 0x48,0x81,0xec,0x08,0x01,0x00,0x00 #sub $0x108,%rsp
3494 lea .Linc(%rip),%rax
3495 and \$-16,%rsp # shouldn't be formally required
3498 movdqa 0(%rax),%xmm0 # 00000001000000010000000000000000
3499 movdqa 16(%rax),%xmm1 # 00000002000000020000000200000002
3500 lea 128($tbl),%r11 # size optimization
3501 lea 128(%rsp),%rax # size optimization
3503 pshufd \$0,%xmm5,%xmm5 # broadcast $idx
3507 ########################################################################
3508 # calculate mask by comparing 0..31 to $idx and save result to stack
3510 for($i=0;$i<$STRIDE/16;$i+=4) {
3513 pcmpeqd %xmm5,%xmm0 # compare to 1,0
3515 $code.=<<___ if ($i);
3516 movdqa %xmm3,`16*($i-1)-128`(%rax)
3522 pcmpeqd %xmm5,%xmm1 # compare to 3,2
3523 movdqa %xmm0,`16*($i+0)-128`(%rax)
3527 pcmpeqd %xmm5,%xmm2 # compare to 5,4
3528 movdqa %xmm1,`16*($i+1)-128`(%rax)
3532 pcmpeqd %xmm5,%xmm3 # compare to 7,6
3533 movdqa %xmm2,`16*($i+2)-128`(%rax)
3538 movdqa %xmm3,`16*($i-1)-128`(%rax)
3546 for($i=0;$i<$STRIDE/16;$i+=4) {
3548 movdqa `16*($i+0)-128`(%r11),%xmm0
3549 movdqa `16*($i+1)-128`(%r11),%xmm1
3550 movdqa `16*($i+2)-128`(%r11),%xmm2
3551 pand `16*($i+0)-128`(%rax),%xmm0
3552 movdqa `16*($i+3)-128`(%r11),%xmm3
3553 pand `16*($i+1)-128`(%rax),%xmm1
3555 pand `16*($i+2)-128`(%rax),%xmm2
3557 pand `16*($i+3)-128`(%rax),%xmm3
3564 lea $STRIDE(%r11),%r11
3565 pshufd \$0x4e,%xmm4,%xmm0
3567 movq %xmm0,($out) # m0=bp[0]
3574 .LSEH_end_bn_gather5:
3576 .size bn_gather5,.-bn_gather5
3584 .asciz "Montgomery Multiplication with scatter/gather for x86_64, CRYPTOGAMS by <appro\@openssl.org>"
3587 # EXCEPTION_DISPOSITION handler (EXCEPTION_RECORD *rec,ULONG64 frame,
3588 # CONTEXT *context,DISPATCHER_CONTEXT *disp)
3596 .extern __imp_RtlVirtualUnwind
3597 .type mul_handler,\@abi-omnipotent
3611 mov 120($context),%rax # pull context->Rax
3612 mov 248($context),%rbx # pull context->Rip
3614 mov 8($disp),%rsi # disp->ImageBase
3615 mov 56($disp),%r11 # disp->HandlerData
3617 mov 0(%r11),%r10d # HandlerData[0]
3618 lea (%rsi,%r10),%r10 # end of prologue label
3619 cmp %r10,%rbx # context->Rip<end of prologue label
3620 jb .Lcommon_seh_tail
3622 mov 4(%r11),%r10d # HandlerData[1]
3623 lea (%rsi,%r10),%r10 # beginning of body label
3624 cmp %r10,%rbx # context->Rip<body label
3625 jb .Lcommon_pop_regs
3627 mov 152($context),%rax # pull context->Rsp
3629 mov 8(%r11),%r10d # HandlerData[2]
3630 lea (%rsi,%r10),%r10 # epilogue label
3631 cmp %r10,%rbx # context->Rip>=epilogue label
3632 jae .Lcommon_seh_tail
3634 lea .Lmul_epilogue(%rip),%r10
3638 mov 192($context),%r10 # pull $num
3639 mov 8(%rax,%r10,8),%rax # pull saved stack pointer
3641 jmp .Lcommon_pop_regs
3644 mov 40(%rax),%rax # pull saved stack pointer
3652 mov %rbx,144($context) # restore context->Rbx
3653 mov %rbp,160($context) # restore context->Rbp
3654 mov %r12,216($context) # restore context->R12
3655 mov %r13,224($context) # restore context->R13
3656 mov %r14,232($context) # restore context->R14
3657 mov %r15,240($context) # restore context->R15
3662 mov %rax,152($context) # restore context->Rsp
3663 mov %rsi,168($context) # restore context->Rsi
3664 mov %rdi,176($context) # restore context->Rdi
3666 mov 40($disp),%rdi # disp->ContextRecord
3667 mov $context,%rsi # context
3668 mov \$154,%ecx # sizeof(CONTEXT)
3669 .long 0xa548f3fc # cld; rep movsq
3672 xor %rcx,%rcx # arg1, UNW_FLAG_NHANDLER
3673 mov 8(%rsi),%rdx # arg2, disp->ImageBase
3674 mov 0(%rsi),%r8 # arg3, disp->ControlPc
3675 mov 16(%rsi),%r9 # arg4, disp->FunctionEntry
3676 mov 40(%rsi),%r10 # disp->ContextRecord
3677 lea 56(%rsi),%r11 # &disp->HandlerData
3678 lea 24(%rsi),%r12 # &disp->EstablisherFrame
3679 mov %r10,32(%rsp) # arg5
3680 mov %r11,40(%rsp) # arg6
3681 mov %r12,48(%rsp) # arg7
3682 mov %rcx,56(%rsp) # arg8, (NULL)
3683 call *__imp_RtlVirtualUnwind(%rip)
3685 mov \$1,%eax # ExceptionContinueSearch
3697 .size mul_handler,.-mul_handler
3701 .rva .LSEH_begin_bn_mul_mont_gather5
3702 .rva .LSEH_end_bn_mul_mont_gather5
3703 .rva .LSEH_info_bn_mul_mont_gather5
3705 .rva .LSEH_begin_bn_mul4x_mont_gather5
3706 .rva .LSEH_end_bn_mul4x_mont_gather5
3707 .rva .LSEH_info_bn_mul4x_mont_gather5
3709 .rva .LSEH_begin_bn_power5
3710 .rva .LSEH_end_bn_power5
3711 .rva .LSEH_info_bn_power5
3713 $code.=<<___ if ($addx);
3714 .rva .LSEH_begin_bn_mulx4x_mont_gather5
3715 .rva .LSEH_end_bn_mulx4x_mont_gather5
3716 .rva .LSEH_info_bn_mulx4x_mont_gather5
3718 .rva .LSEH_begin_bn_powerx5
3719 .rva .LSEH_end_bn_powerx5
3720 .rva .LSEH_info_bn_powerx5
3723 .rva .LSEH_begin_bn_gather5
3724 .rva .LSEH_end_bn_gather5
3725 .rva .LSEH_info_bn_gather5
3729 .LSEH_info_bn_mul_mont_gather5:
3732 .rva .Lmul_body,.Lmul_body,.Lmul_epilogue # HandlerData[]
3734 .LSEH_info_bn_mul4x_mont_gather5:
3737 .rva .Lmul4x_prologue,.Lmul4x_body,.Lmul4x_epilogue # HandlerData[]
3739 .LSEH_info_bn_power5:
3742 .rva .Lpower5_prologue,.Lpower5_body,.Lpower5_epilogue # HandlerData[]
3744 $code.=<<___ if ($addx);
3746 .LSEH_info_bn_mulx4x_mont_gather5:
3749 .rva .Lmulx4x_prologue,.Lmulx4x_body,.Lmulx4x_epilogue # HandlerData[]
3751 .LSEH_info_bn_powerx5:
3754 .rva .Lpowerx5_prologue,.Lpowerx5_body,.Lpowerx5_epilogue # HandlerData[]
3758 .LSEH_info_bn_gather5:
3759 .byte 0x01,0x0b,0x03,0x0a
3760 .byte 0x0b,0x01,0x21,0x00 # sub rsp,0x108
3761 .byte 0x04,0xa3,0x00,0x00 # lea r10,(rsp)
3766 $code =~ s/\`([^\`]*)\`/eval($1)/gem;
3769 close STDOUT or die "error closing STDOUT: $!";