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) ([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
2108 .globl bn_from_montgomery
2109 .type bn_from_montgomery
,\
@abi-omnipotent
2113 testl \
$7,`($win64?"48(%rsp)":"%r9d")`
2118 .size bn_from_montgomery
,.-bn_from_montgomery
2120 .type bn_from_mont8x
,\
@function,6
2126 .cfi_def_cfa_register
%rax
2141 shl \
$3,${num
}d
# convert $num to bytes
2142 lea
($num,$num,2),%r10 # 3*$num in bytes
2146 ##############################################################
2147 # Ensure that stack frame doesn't alias with $rptr+3*$num
2148 # modulo 4096, which covers ret[num], am[num] and n[num]
2149 # (see bn_exp.c). The stack is allocated to aligned with
2150 # bn_power5's frame, and as bn_from_montgomery happens to be
2151 # last operation, we use the opportunity to cleanse it.
2153 lea
-320(%rsp,$num,2),%r11
2159 sub %r11,%rbp # align with $aptr
2160 lea
-320(%rbp,$num,2),%rbp # future alloca(frame+2*$num*8+256)
2165 lea
4096-320(,$num,2),%r10
2166 lea
-320(%rbp,$num,2),%rbp # future alloca(frame+2*$num*8+256)
2176 lea
(%rbp,%r11),%rsp
2180 jmp
.Lfrom_page_walk_done
2183 lea
-4096(%rsp),%rsp
2187 .Lfrom_page_walk_done
:
2192 ##############################################################
2195 # +0 saved $num, used in reduction section
2196 # +8 &t[2*$num], used in reduction section
2202 mov
%rax, 40(%rsp) # save original %rsp
2203 .cfi_cfa_expression
%rsp+40,deref
,+8
2212 movdqu
($aptr),%xmm1
2213 movdqu
16($aptr),%xmm2
2214 movdqu
32($aptr),%xmm3
2215 movdqa
%xmm0,(%rax,$num)
2216 movdqu
48($aptr),%xmm4
2217 movdqa
%xmm0,16(%rax,$num)
2218 .byte
0x48,0x8d,0xb6,0x40,0x00,0x00,0x00 # lea 64($aptr),$aptr
2220 movdqa
%xmm0,32(%rax,$num)
2221 movdqa
%xmm2,16(%rax)
2222 movdqa
%xmm0,48(%rax,$num)
2223 movdqa
%xmm3,32(%rax)
2224 movdqa
%xmm4,48(%rax)
2233 movq
%r10, %xmm3 # -num
2235 $code.=<<___
if ($addx);
2236 mov OPENSSL_ia32cap_P
+8(%rip),%r11d
2238 cmp \
$0x80108,%r11d # check for AD*X+BMI2+BMI1
2241 lea
(%rax,$num),$rptr
2242 call __bn_sqrx8x_reduction
2243 call __bn_postx4x_internal
2247 jmp
.Lfrom_mont_zero
2253 call __bn_sqr8x_reduction
2254 call __bn_post4x_internal
2258 jmp
.Lfrom_mont_zero
2262 mov
40(%rsp),%rsi # restore %rsp
2264 movdqa
%xmm0,16*0(%rax)
2265 movdqa
%xmm0,16*1(%rax)
2266 movdqa
%xmm0,16*2(%rax)
2267 movdqa
%xmm0,16*3(%rax)
2270 jnz
.Lfrom_mont_zero
2286 .cfi_def_cfa_register
%rsp
2290 .size bn_from_mont8x
,.-bn_from_mont8x
2296 my $bp="%rdx"; # restore original value
2299 .type bn_mulx4x_mont_gather5
,\
@function,6
2301 bn_mulx4x_mont_gather5
:
2304 .cfi_def_cfa_register
%rax
2320 shl \
$3,${num
}d
# convert $num to bytes
2321 lea
($num,$num,2),%r10 # 3*$num in bytes
2325 ##############################################################
2326 # Ensure that stack frame doesn't alias with $rptr+3*$num
2327 # modulo 4096, which covers ret[num], am[num] and n[num]
2328 # (see bn_exp.c). This is done to allow memory disambiguation
2329 # logic do its magic. [Extra [num] is allocated in order
2330 # to align with bn_power5's frame, which is cleansed after
2331 # completing exponentiation. Extra 256 bytes is for power mask
2332 # calculated from 7th argument, the index.]
2334 lea
-320(%rsp,$num,2),%r11
2340 sub %r11,%rbp # align with $aptr
2341 lea
-320(%rbp,$num,2),%rbp # future alloca(frame+2*$num*8+256)
2345 lea
4096-320(,$num,2),%r10
2346 lea
-320(%rbp,$num,2),%rbp # future alloca(frame+2*$num*8+256)
2352 and \
$-64,%rbp # ensure alignment
2356 lea
(%rbp,%r11),%rsp
2359 ja
.Lmulx4x_page_walk
2360 jmp
.Lmulx4x_page_walk_done
2363 lea
-4096(%rsp),%rsp
2366 ja
.Lmulx4x_page_walk
2367 .Lmulx4x_page_walk_done
:
2369 ##############################################################
2372 # +8 off-loaded &b[i]
2381 mov
$n0, 32(%rsp) # save *n0
2382 mov
%rax,40(%rsp) # save original %rsp
2383 .cfi_cfa_expression
%rsp+40,deref
,+8
2385 call mulx4x_internal
2387 mov
40(%rsp),%rsi # restore %rsp
2404 .cfi_def_cfa_register
%rsp
2408 .size bn_mulx4x_mont_gather5
,.-bn_mulx4x_mont_gather5
2410 .type mulx4x_internal
,\
@abi-omnipotent
2414 mov
$num,8(%rsp) # save -$num (it was in bytes)
2416 neg
$num # restore $num
2418 neg
%r10 # restore $num
2419 lea
128($bp,$num),%r13 # end of powers table (+size optimization)
2421 movd
`($win64?56:8)`(%rax),%xmm5 # load 7th argument
2423 lea
.Linc
(%rip),%rax
2424 mov
%r13,16+8(%rsp) # end of b[num]
2425 mov
$num,24+8(%rsp) # inner counter
2426 mov
$rp, 56+8(%rsp) # save $rp
2428 my ($aptr, $bptr, $nptr, $tptr, $mi, $bi, $zero, $num)=
2429 ("%rsi","%rdi","%rcx","%rbx","%r8","%r9","%rbp","%rax");
2431 my $STRIDE=2**5*8; # 5 is "window size"
2432 my $N=$STRIDE/4; # should match cache line size
2434 movdqa
0(%rax),%xmm0 # 00000001000000010000000000000000
2435 movdqa
16(%rax),%xmm1 # 00000002000000020000000200000002
2436 lea
88-112(%rsp,%r10),%r10 # place the mask after tp[num+1] (+ICache optimization)
2437 lea
128($bp),$bptr # size optimization
2439 pshufd \
$0,%xmm5,%xmm5 # broadcast index
2444 ########################################################################
2445 # calculate mask by comparing 0..31 to index and save result to stack
2450 pcmpeqd
%xmm5,%xmm0 # compare to 1,0
2453 for($i=0;$i<$STRIDE/16-4;$i+=4) {
2456 pcmpeqd
%xmm5,%xmm1 # compare to 3,2
2457 movdqa
%xmm0,`16*($i+0)+112`(%r10)
2461 pcmpeqd
%xmm5,%xmm2 # compare to 5,4
2462 movdqa
%xmm1,`16*($i+1)+112`(%r10)
2466 pcmpeqd
%xmm5,%xmm3 # compare to 7,6
2467 movdqa
%xmm2,`16*($i+2)+112`(%r10)
2472 movdqa
%xmm3,`16*($i+3)+112`(%r10)
2476 $code.=<<___
; # last iteration can be optimized
2480 movdqa
%xmm0,`16*($i+0)+112`(%r10)
2484 movdqa
%xmm1,`16*($i+1)+112`(%r10)
2487 movdqa
%xmm2,`16*($i+2)+112`(%r10)
2489 pand
`16*($i+0)-128`($bptr),%xmm0 # while it's still in register
2490 pand
`16*($i+1)-128`($bptr),%xmm1
2491 pand
`16*($i+2)-128`($bptr),%xmm2
2492 movdqa
%xmm3,`16*($i+3)+112`(%r10)
2493 pand
`16*($i+3)-128`($bptr),%xmm3
2497 for($i=0;$i<$STRIDE/16-4;$i+=4) {
2499 movdqa
`16*($i+0)-128`($bptr),%xmm4
2500 movdqa
`16*($i+1)-128`($bptr),%xmm5
2501 movdqa
`16*($i+2)-128`($bptr),%xmm2
2502 pand
`16*($i+0)+112`(%r10),%xmm4
2503 movdqa
`16*($i+3)-128`($bptr),%xmm3
2504 pand
`16*($i+1)+112`(%r10),%xmm5
2506 pand
`16*($i+2)+112`(%r10),%xmm2
2508 pand
`16*($i+3)+112`(%r10),%xmm3
2515 pshufd \
$0x4e,%xmm0,%xmm1
2517 lea
$STRIDE($bptr),$bptr
2518 movq
%xmm0,%rdx # bp[0]
2519 lea
64+8*4+8(%rsp),$tptr
2522 mulx
0*8($aptr),$mi,%rax # a[0]*b[0]
2523 mulx
1*8($aptr),%r11,%r12 # a[1]*b[0]
2525 mulx
2*8($aptr),%rax,%r13 # ...
2528 mulx
3*8($aptr),%rax,%r14
2531 imulq
32+8(%rsp),$mi # "t[0]"*n0
2532 xor $zero,$zero # cf=0, of=0
2535 mov
$bptr,8+8(%rsp) # off-load &b[i]
2537 lea
4*8($aptr),$aptr
2539 adcx
$zero,%r14 # cf=0
2541 mulx
0*8($nptr),%rax,%r10
2542 adcx
%rax,%r15 # discarded
2544 mulx
1*8($nptr),%rax,%r11
2547 mulx
2*8($nptr),%rax,%r12
2548 mov
24+8(%rsp),$bptr # counter value
2549 mov
%r10,-8*4($tptr)
2552 mulx
3*8($nptr),%rax,%r15
2554 mov
%r11,-8*3($tptr)
2556 adox
$zero,%r15 # of=0
2557 lea
4*8($nptr),$nptr
2558 mov
%r12,-8*2($tptr)
2563 adcx
$zero,%r15 # cf=0, modulo-scheduled
2564 mulx
0*8($aptr),%r10,%rax # a[4]*b[0]
2566 mulx
1*8($aptr),%r11,%r14 # a[5]*b[0]
2568 mulx
2*8($aptr),%r12,%rax # ...
2570 mulx
3*8($aptr),%r13,%r14
2574 adcx
$zero,%r14 # cf=0
2575 lea
4*8($aptr),$aptr
2576 lea
4*8($tptr),$tptr
2579 mulx
0*8($nptr),%rax,%r15
2582 mulx
1*8($nptr),%rax,%r15
2585 mulx
2*8($nptr),%rax,%r15
2586 mov
%r10,-5*8($tptr)
2588 mov
%r11,-4*8($tptr)
2590 mulx
3*8($nptr),%rax,%r15
2592 mov
%r12,-3*8($tptr)
2595 lea
4*8($nptr),$nptr
2596 mov
%r13,-2*8($tptr)
2598 dec
$bptr # of=0, pass cf
2601 mov
8(%rsp),$num # load -num
2602 adc
$zero,%r15 # modulo-scheduled
2603 lea
($aptr,$num),$aptr # rewind $aptr
2605 mov
8+8(%rsp),$bptr # re-load &b[i]
2606 adc
$zero,$zero # top-most carry
2607 mov
%r14,-1*8($tptr)
2612 lea
16-256($tptr),%r10 # where 256-byte mask is (+density control)
2617 for($i=0;$i<$STRIDE/16;$i+=4) {
2619 movdqa
`16*($i+0)-128`($bptr),%xmm0
2620 movdqa
`16*($i+1)-128`($bptr),%xmm1
2621 movdqa
`16*($i+2)-128`($bptr),%xmm2
2622 pand
`16*($i+0)+256`(%r10),%xmm0
2623 movdqa
`16*($i+3)-128`($bptr),%xmm3
2624 pand
`16*($i+1)+256`(%r10),%xmm1
2626 pand
`16*($i+2)+256`(%r10),%xmm2
2628 pand
`16*($i+3)+256`(%r10),%xmm3
2635 pshufd \
$0x4e,%xmm4,%xmm0
2637 lea
$STRIDE($bptr),$bptr
2638 movq
%xmm0,%rdx # m0=bp[i]
2640 mov
$zero,($tptr) # save top-most carry
2641 lea
4*8($tptr,$num),$tptr # rewind $tptr
2642 mulx
0*8($aptr),$mi,%r11 # a[0]*b[i]
2643 xor $zero,$zero # cf=0, of=0
2645 mulx
1*8($aptr),%r14,%r12 # a[1]*b[i]
2646 adox
-4*8($tptr),$mi # +t[0]
2648 mulx
2*8($aptr),%r15,%r13 # ...
2649 adox
-3*8($tptr),%r11
2651 mulx
3*8($aptr),%rdx,%r14
2652 adox
-2*8($tptr),%r12
2654 lea
($nptr,$num),$nptr # rewind $nptr
2655 lea
4*8($aptr),$aptr
2656 adox
-1*8($tptr),%r13
2661 imulq
32+8(%rsp),$mi # "t[0]"*n0
2664 xor $zero,$zero # cf=0, of=0
2665 mov
$bptr,8+8(%rsp) # off-load &b[i]
2667 mulx
0*8($nptr),%rax,%r10
2668 adcx
%rax,%r15 # discarded
2670 mulx
1*8($nptr),%rax,%r11
2673 mulx
2*8($nptr),%rax,%r12
2676 mulx
3*8($nptr),%rax,%r15
2678 mov
24+8(%rsp),$bptr # counter value
2679 mov
%r10,-8*4($tptr)
2681 mov
%r11,-8*3($tptr)
2682 adox
$zero,%r15 # of=0
2683 mov
%r12,-8*2($tptr)
2684 lea
4*8($nptr),$nptr
2689 mulx
0*8($aptr),%r10,%rax # a[4]*b[i]
2690 adcx
$zero,%r15 # cf=0, modulo-scheduled
2692 mulx
1*8($aptr),%r11,%r14 # a[5]*b[i]
2693 adcx
0*8($tptr),%r10
2695 mulx
2*8($aptr),%r12,%rax # ...
2696 adcx
1*8($tptr),%r11
2698 mulx
3*8($aptr),%r13,%r14
2700 adcx
2*8($tptr),%r12
2702 adcx
3*8($tptr),%r13
2703 adox
$zero,%r14 # of=0
2704 lea
4*8($aptr),$aptr
2705 lea
4*8($tptr),$tptr
2706 adcx
$zero,%r14 # cf=0
2709 mulx
0*8($nptr),%rax,%r15
2712 mulx
1*8($nptr),%rax,%r15
2715 mulx
2*8($nptr),%rax,%r15
2716 mov
%r10,-5*8($tptr)
2719 mov
%r11,-4*8($tptr)
2720 mulx
3*8($nptr),%rax,%r15
2722 lea
4*8($nptr),$nptr
2723 mov
%r12,-3*8($tptr)
2726 mov
%r13,-2*8($tptr)
2728 dec
$bptr # of=0, pass cf
2731 mov
0+8(%rsp),$num # load -num
2732 adc
$zero,%r15 # modulo-scheduled
2733 sub 0*8($tptr),$bptr # pull top-most carry to %cf
2734 mov
8+8(%rsp),$bptr # re-load &b[i]
2737 lea
($aptr,$num),$aptr # rewind $aptr
2738 adc
$zero,$zero # top-most carry
2739 mov
%r14,-1*8($tptr)
2746 mov
($nptr,$num),%r12
2747 lea
($nptr,$num),%rbp # rewind $nptr
2749 lea
($tptr,$num),%rdi # rewind $tptr
2752 sub %r14,%r10 # compare top-most words
2756 sub %r8,%rax # %rax=-%r8
2757 mov
56+8(%rsp),%rdx # restore rp
2758 dec
%r12 # so that after 'not' we get -n[0]
2763 jmp
.Lsqrx4x_sub_entry
# common post-condition
2765 .size mulx4x_internal
,.-mulx4x_internal
2768 ######################################################################
2770 my $rptr="%rdi"; # BN_ULONG *rptr,
2771 my $aptr="%rsi"; # const BN_ULONG *aptr,
2772 my $bptr="%rdx"; # const void *table,
2773 my $nptr="%rcx"; # const BN_ULONG *nptr,
2774 my $n0 ="%r8"; # const BN_ULONG *n0);
2775 my $num ="%r9"; # int num, has to be divisible by 8
2778 my ($i,$j,$tptr)=("%rbp","%rcx",$rptr);
2779 my @A0=("%r10","%r11");
2780 my @A1=("%r12","%r13");
2781 my ($a0,$a1,$ai)=("%r14","%r15","%rbx");
2784 .type bn_powerx5
,\
@function,6
2789 .cfi_def_cfa_register
%rax
2805 shl \
$3,${num
}d
# convert $num to bytes
2806 lea
($num,$num,2),%r10 # 3*$num in bytes
2810 ##############################################################
2811 # Ensure that stack frame doesn't alias with $rptr+3*$num
2812 # modulo 4096, which covers ret[num], am[num] and n[num]
2813 # (see bn_exp.c). This is done to allow memory disambiguation
2814 # logic do its magic. [Extra 256 bytes is for power mask
2815 # calculated from 7th argument, the index.]
2817 lea
-320(%rsp,$num,2),%r11
2823 sub %r11,%rbp # align with $aptr
2824 lea
-320(%rbp,$num,2),%rbp # future alloca(frame+2*$num*8+256)
2829 lea
4096-320(,$num,2),%r10
2830 lea
-320(%rbp,$num,2),%rbp # alloca(frame+2*$num*8+256)
2840 lea
(%rbp,%r11),%rsp
2844 jmp
.Lpwrx_page_walk_done
2847 lea
-4096(%rsp),%rsp
2851 .Lpwrx_page_walk_done
:
2856 ##############################################################
2859 # +0 saved $num, used in reduction section
2860 # +8 &t[2*$num], used in reduction section
2861 # +16 intermediate carry bit
2862 # +24 top-most carry bit, used in reduction section
2868 movq
$rptr,%xmm1 # save $rptr
2869 movq
$nptr,%xmm2 # save $nptr
2870 movq
%r10, %xmm3 # -$num
2873 mov
%rax, 40(%rsp) # save original %rsp
2874 .cfi_cfa_expression
%rsp+40,deref
,+8
2877 call __bn_sqrx8x_internal
2878 call __bn_postx4x_internal
2879 call __bn_sqrx8x_internal
2880 call __bn_postx4x_internal
2881 call __bn_sqrx8x_internal
2882 call __bn_postx4x_internal
2883 call __bn_sqrx8x_internal
2884 call __bn_postx4x_internal
2885 call __bn_sqrx8x_internal
2886 call __bn_postx4x_internal
2888 mov
%r10,$num # -num
2894 call mulx4x_internal
2896 mov
40(%rsp),%rsi # restore %rsp
2913 .cfi_def_cfa_register
%rsp
2917 .size bn_powerx5
,.-bn_powerx5
2919 .globl bn_sqrx8x_internal
2920 .hidden bn_sqrx8x_internal
2921 .type bn_sqrx8x_internal
,\
@abi-omnipotent
2924 __bn_sqrx8x_internal
:
2926 ##################################################################
2929 # a) multiply-n-add everything but a[i]*a[i];
2930 # b) shift result of a) by 1 to the left and accumulate
2931 # a[i]*a[i] products;
2933 ##################################################################
2934 # 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]
2965 # 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]
2968 my ($zero,$carry)=("%rbp","%rcx");
2971 lea
48+8(%rsp),$tptr
2972 lea
($aptr,$num),$aaptr
2973 mov
$num,0+8(%rsp) # save $num
2974 mov
$aaptr,8+8(%rsp) # save end of $aptr
2975 jmp
.Lsqr8x_zero_start
2978 .byte
0x66,0x66,0x66,0x2e,0x0f,0x1f,0x84,0x00,0x00,0x00,0x00,0x00
2981 movdqa
%xmm0,0*8($tptr)
2982 movdqa
%xmm0,2*8($tptr)
2983 movdqa
%xmm0,4*8($tptr)
2984 movdqa
%xmm0,6*8($tptr)
2985 .Lsqr8x_zero_start
: # aligned at 32
2986 movdqa
%xmm0,8*8($tptr)
2987 movdqa
%xmm0,10*8($tptr)
2988 movdqa
%xmm0,12*8($tptr)
2989 movdqa
%xmm0,14*8($tptr)
2990 lea
16*8($tptr),$tptr
2994 mov
0*8($aptr),%rdx # a[0], modulo-scheduled
2995 #xor %r9,%r9 # t[1], ex-$num, zero already
3002 lea
48+8(%rsp),$tptr
3003 xor $zero,$zero # cf=0, cf=0
3004 jmp
.Lsqrx8x_outer_loop
3007 .Lsqrx8x_outer_loop
:
3008 mulx
1*8($aptr),%r8,%rax # a[1]*a[0]
3009 adcx
%r9,%r8 # a[1]*a[0]+=t[1]
3011 mulx
2*8($aptr),%r9,%rax # a[2]*a[0]
3014 .byte
0xc4,0xe2,0xab,0xf6,0x86,0x18,0x00,0x00,0x00 # mulx 3*8($aptr),%r10,%rax # ...
3017 .byte
0xc4,0xe2,0xa3,0xf6,0x86,0x20,0x00,0x00,0x00 # mulx 4*8($aptr),%r11,%rax
3020 mulx
5*8($aptr),%r12,%rax
3023 mulx
6*8($aptr),%r13,%rax
3026 mulx
7*8($aptr),%r14,%r15
3027 mov
1*8($aptr),%rdx # a[1]
3031 mov
%r8,1*8($tptr) # t[1]
3032 mov
%r9,2*8($tptr) # t[2]
3033 sbb
$carry,$carry # mov %cf,$carry
3034 xor $zero,$zero # cf=0, of=0
3037 mulx
2*8($aptr),%r8,%rbx # a[2]*a[1]
3038 mulx
3*8($aptr),%r9,%rax # a[3]*a[1]
3041 mulx
4*8($aptr),%r10,%rbx # ...
3044 .byte
0xc4,0xe2,0xa3,0xf6,0x86,0x28,0x00,0x00,0x00 # mulx 5*8($aptr),%r11,%rax
3047 .byte
0xc4,0xe2,0x9b,0xf6,0x9e,0x30,0x00,0x00,0x00 # mulx 6*8($aptr),%r12,%rbx
3050 .byte
0xc4,0x62,0x93,0xf6,0xb6,0x38,0x00,0x00,0x00 # mulx 7*8($aptr),%r13,%r14
3051 mov
2*8($aptr),%rdx # a[2]
3055 adox
$zero,%r14 # of=0
3056 adcx
$zero,%r14 # cf=0
3058 mov
%r8,3*8($tptr) # t[3]
3059 mov
%r9,4*8($tptr) # t[4]
3061 mulx
3*8($aptr),%r8,%rbx # a[3]*a[2]
3062 mulx
4*8($aptr),%r9,%rax # a[4]*a[2]
3065 mulx
5*8($aptr),%r10,%rbx # ...
3068 .byte
0xc4,0xe2,0xa3,0xf6,0x86,0x30,0x00,0x00,0x00 # mulx 6*8($aptr),%r11,%rax
3071 .byte
0xc4,0x62,0x9b,0xf6,0xae,0x38,0x00,0x00,0x00 # mulx 7*8($aptr),%r12,%r13
3073 mov
3*8($aptr),%rdx # a[3]
3077 mov
%r8,5*8($tptr) # t[5]
3078 mov
%r9,6*8($tptr) # t[6]
3079 mulx
4*8($aptr),%r8,%rax # a[4]*a[3]
3080 adox
$zero,%r13 # of=0
3081 adcx
$zero,%r13 # cf=0
3083 mulx
5*8($aptr),%r9,%rbx # a[5]*a[3]
3086 mulx
6*8($aptr),%r10,%rax # ...
3089 mulx
7*8($aptr),%r11,%r12
3090 mov
4*8($aptr),%rdx # a[4]
3091 mov
5*8($aptr),%r14 # a[5]
3094 mov
6*8($aptr),%r15 # a[6]
3096 adox
$zero,%r12 # of=0
3097 adcx
$zero,%r12 # cf=0
3099 mov
%r8,7*8($tptr) # t[7]
3100 mov
%r9,8*8($tptr) # t[8]
3102 mulx
%r14,%r9,%rax # a[5]*a[4]
3103 mov
7*8($aptr),%r8 # a[7]
3105 mulx
%r15,%r10,%rbx # a[6]*a[4]
3108 mulx
%r8,%r11,%rax # a[7]*a[4]
3109 mov
%r14,%rdx # a[5]
3112 #adox $zero,%rax # of=0
3113 adcx
$zero,%rax # cf=0
3115 mulx
%r15,%r14,%rbx # a[6]*a[5]
3116 mulx
%r8,%r12,%r13 # a[7]*a[5]
3117 mov
%r15,%rdx # a[6]
3118 lea
8*8($aptr),$aptr
3125 mulx
%r8,%r8,%r14 # a[7]*a[6]
3130 je
.Lsqrx8x_outer_break
3132 neg
$carry # mov $carry,%cf
3136 adcx
9*8($tptr),%r9 # +=t[9]
3137 adcx
10*8($tptr),%r10 # ...
3138 adcx
11*8($tptr),%r11
3139 adc
12*8($tptr),%r12
3140 adc
13*8($tptr),%r13
3141 adc
14*8($tptr),%r14
3142 adc
15*8($tptr),%r15
3144 lea
2*64($tptr),$tptr
3145 sbb
%rax,%rax # mov %cf,$carry
3147 mov
-64($aptr),%rdx # a[0]
3148 mov
%rax,16+8(%rsp) # offload $carry
3149 mov
$tptr,24+8(%rsp)
3151 #lea 8*8($tptr),$tptr # see 2*8*8($tptr) above
3152 xor %eax,%eax # cf=0, of=0
3158 mulx
0*8($aaptr),%rax,%r8 # a[8]*a[i]
3159 adcx
%rax,%rbx # +=t[8]
3162 mulx
1*8($aaptr),%rax,%r9 # ...
3166 mulx
2*8($aaptr),%rax,%r10
3170 mulx
3*8($aaptr),%rax,%r11
3174 .byte
0xc4,0x62,0xfb,0xf6,0xa5,0x20,0x00,0x00,0x00 # mulx 4*8($aaptr),%rax,%r12
3178 mulx
5*8($aaptr),%rax,%r13
3182 mulx
6*8($aaptr),%rax,%r14
3183 mov
%rbx,($tptr,%rcx,8) # store t[8+i]
3188 .byte
0xc4,0x62,0xfb,0xf6,0xbd,0x38,0x00,0x00,0x00 # mulx 7*8($aaptr),%rax,%r15
3189 mov
8($aptr,%rcx,8),%rdx # a[i]
3191 adox
%rbx,%r15 # %rbx is 0, of=0
3192 adcx
%rbx,%r15 # cf=0
3198 lea
8*8($aaptr),$aaptr
3200 cmp 8+8(%rsp),$aaptr # done?
3203 sub 16+8(%rsp),%rbx # mov 16(%rsp),%cf
3214 lea
8*8($tptr),$tptr
3216 sbb
%rax,%rax # mov %cf,%rax
3217 xor %ebx,%ebx # cf=0, of=0
3218 mov
%rax,16+8(%rsp) # offload carry
3224 sub 16+8(%rsp),%rbx # mov 16(%rsp),%cf
3226 mov
24+8(%rsp),$carry # initial $tptr, borrow $carry
3228 mov
0*8($aptr),%rdx # a[8], modulo-scheduled
3236 cmp $carry,$tptr # cf=0, of=0
3237 je
.Lsqrx8x_outer_loop
3242 mov
2*8($carry),%r10
3244 mov
3*8($carry),%r11
3246 mov
4*8($carry),%r12
3248 mov
5*8($carry),%r13
3250 mov
6*8($carry),%r14
3252 mov
7*8($carry),%r15
3254 jmp
.Lsqrx8x_outer_loop
3257 .Lsqrx8x_outer_break
:
3258 mov
%r9,9*8($tptr) # t[9]
3259 movq
%xmm3,%rcx # -$num
3260 mov
%r10,10*8($tptr) # ...
3261 mov
%r11,11*8($tptr)
3262 mov
%r12,12*8($tptr)
3263 mov
%r13,13*8($tptr)
3264 mov
%r14,14*8($tptr)
3269 lea
48+8(%rsp),$tptr
3270 mov
($aptr,$i),%rdx # a[0]
3272 mov
8($tptr),$A0[1] # t[1]
3273 xor $A0[0],$A0[0] # t[0], of=0, cf=0
3274 mov
0+8(%rsp),$num # restore $num
3276 mov
16($tptr),$A1[0] # t[2] # prefetch
3277 mov
24($tptr),$A1[1] # t[3] # prefetch
3278 #jmp .Lsqrx4x_shift_n_add # happens to be aligned
3281 .Lsqrx4x_shift_n_add
:
3285 .byte
0x48,0x8b,0x94,0x0e,0x08,0x00,0x00,0x00 # mov 8($aptr,$i),%rdx # a[i+1] # prefetch
3286 .byte
0x4c,0x8b,0x97,0x20,0x00,0x00,0x00 # mov 32($tptr),$A0[0] # t[2*i+4] # prefetch
3289 mov
40($tptr),$A0[1] # t[2*i+4+1] # prefetch
3296 mov
16($aptr,$i),%rdx # a[i+2] # prefetch
3297 mov
48($tptr),$A1[0] # t[2*i+6] # prefetch
3300 mov
56($tptr),$A1[1] # t[2*i+6+1] # prefetch
3307 mov
24($aptr,$i),%rdx # a[i+3] # prefetch
3309 mov
64($tptr),$A0[0] # t[2*i+8] # prefetch
3312 mov
72($tptr),$A0[1] # t[2*i+8+1] # prefetch
3319 jrcxz
.Lsqrx4x_shift_n_add_break
3320 .byte
0x48,0x8b,0x94,0x0e,0x00,0x00,0x00,0x00 # mov 0($aptr,$i),%rdx # a[i+4] # prefetch
3323 mov
80($tptr),$A1[0] # t[2*i+10] # prefetch
3324 mov
88($tptr),$A1[1] # t[2*i+10+1] # prefetch
3329 jmp
.Lsqrx4x_shift_n_add
3332 .Lsqrx4x_shift_n_add_break
:
3336 lea
64($tptr),$tptr # end of t[] buffer
3339 ######################################################################
3340 # Montgomery reduction part, "word-by-word" algorithm.
3342 # This new path is inspired by multiple submissions from Intel, by
3343 # Shay Gueron, Vlad Krasnov, Erdinc Ozturk, James Guilford,
3346 my ($nptr,$carry,$m0)=("%rbp","%rsi","%rdx");
3350 __bn_sqrx8x_reduction
:
3351 xor %eax,%eax # initial top-most carry bit
3352 mov
32+8(%rsp),%rbx # n0
3353 mov
48+8(%rsp),%rdx # "%r8", 8*0($tptr)
3354 lea
-8*8($nptr,$num),%rcx # end of n[]
3355 #lea 48+8(%rsp,$num,2),$tptr # end of t[] buffer
3356 mov
%rcx, 0+8(%rsp) # save end of n[]
3357 mov
$tptr,8+8(%rsp) # save end of t[]
3359 lea
48+8(%rsp),$tptr # initial t[] window
3360 jmp
.Lsqrx8x_reduction_loop
3363 .Lsqrx8x_reduction_loop
:
3369 imulq
%rbx,%rdx # n0*a[i]
3373 mov
%rax,24+8(%rsp) # store top-most carry bit
3375 lea
8*8($tptr),$tptr
3376 xor $carry,$carry # cf=0,of=0
3383 mulx
8*0($nptr),%rax,%r8 # n[0]
3384 adcx
%rbx,%rax # discarded
3387 mulx
8*1($nptr),%rbx,%r9 # n[1]
3391 mulx
8*2($nptr),%rbx,%r10
3395 mulx
8*3($nptr),%rbx,%r11
3399 .byte
0xc4,0x62,0xe3,0xf6,0xa5,0x20,0x00,0x00,0x00 # mulx 8*4($nptr),%rbx,%r12
3405 mulx
32+8(%rsp),%rbx,%rdx # %rdx discarded
3407 mov
%rax,64+48+8(%rsp,%rcx,8) # put aside n0*a[i]
3409 mulx
8*5($nptr),%rax,%r13
3413 mulx
8*6($nptr),%rax,%r14
3417 mulx
8*7($nptr),%rax,%r15
3420 adox
$carry,%r15 # $carry is 0
3421 adcx
$carry,%r15 # cf=0
3423 .byte
0x67,0x67,0x67
3427 mov
$carry,%rax # xor %rax,%rax
3428 cmp 0+8(%rsp),$nptr # end of n[]?
3429 jae
.Lsqrx8x_no_tail
3431 mov
48+8(%rsp),%rdx # pull n0*a[0]
3433 lea
8*8($nptr),$nptr
3436 adcx
8*2($tptr),%r10
3442 lea
8*8($tptr),$tptr
3443 sbb
%rax,%rax # top carry
3445 xor $carry,$carry # of=0, cf=0
3452 mulx
8*0($nptr),%rax,%r8
3456 mulx
8*1($nptr),%rax,%r9
3460 mulx
8*2($nptr),%rax,%r10
3464 mulx
8*3($nptr),%rax,%r11
3468 .byte
0xc4,0x62,0xfb,0xf6,0xa5,0x20,0x00,0x00,0x00 # mulx 8*4($nptr),%rax,%r12
3472 mulx
8*5($nptr),%rax,%r13
3476 mulx
8*6($nptr),%rax,%r14
3480 mulx
8*7($nptr),%rax,%r15
3481 mov
72+48+8(%rsp,%rcx,8),%rdx # pull n0*a[i]
3484 mov
%rbx,($tptr,%rcx,8) # save result
3486 adcx
$carry,%r15 # cf=0
3491 cmp 0+8(%rsp),$nptr # end of n[]?
3492 jae
.Lsqrx8x_tail_done
# break out of loop
3494 sub 16+8(%rsp),$carry # mov 16(%rsp),%cf
3495 mov
48+8(%rsp),%rdx # pull n0*a[0]
3496 lea
8*8($nptr),$nptr
3505 lea
8*8($tptr),$tptr
3507 sub \
$8,%rcx # mov \$-8,%rcx
3509 xor $carry,$carry # of=0, cf=0
3516 add
24+8(%rsp),%r8 # can this overflow?
3526 sub 16+8(%rsp),$carry # mov 16(%rsp),%cf
3527 .Lsqrx8x_no_tail
: # %cf is 0 if jumped here
3531 mov
8*7($nptr),$carry
3532 movq
%xmm2,$nptr # restore $nptr
3539 adc \
$0,%rax # top-most carry
3541 mov
32+8(%rsp),%rbx # n0
3542 mov
8*8($tptr,%rcx),%rdx # modulo-scheduled "%r8"
3544 mov
%r8,8*0($tptr) # store top 512 bits
3545 lea
8*8($tptr),%r8 # borrow %r8
3554 lea
8*8($tptr,%rcx),$tptr # start of current t[] window
3555 cmp 8+8(%rsp),%r8 # end of t[]?
3556 jb
.Lsqrx8x_reduction_loop
3559 .size bn_sqrx8x_internal
,.-bn_sqrx8x_internal
3562 ##############################################################
3563 # Post-condition, 4x unrolled
3566 my ($rptr,$nptr)=("%rdx","%rbp");
3569 __bn_postx4x_internal
:
3572 mov
%rcx,%r10 # -$num
3573 mov
%rcx,%r9 # -$num
3576 #lea 48+8(%rsp,%r9),$tptr
3577 movq
%xmm1,$rptr # restore $rptr
3578 movq
%xmm1,$aptr # prepare for back-to-back call
3579 dec
%r12 # so that after 'not' we get -n[0]
3584 jmp
.Lsqrx4x_sub_entry
3594 lea
8*4($nptr),$nptr
3599 neg
%r8 # mov %r8,%cf
3605 lea
8*4($tptr),$tptr
3607 sbb
%r8,%r8 # mov %cf,%r8
3610 lea
8*4($rptr),$rptr
3615 neg
%r9 # restore $num
3619 .size __bn_postx4x_internal
,.-__bn_postx4x_internal
3624 my ($inp,$num,$tbl,$idx)=$win64?
("%rcx","%edx","%r8", "%r9d") : # Win64 order
3625 ("%rdi","%esi","%rdx","%ecx"); # Unix order
3632 .type bn_get_bits5
,\
@abi-omnipotent
3645 movzw
(%r10,$num,2),%eax
3650 .size bn_get_bits5
,.-bn_get_bits5
3653 .type bn_scatter5
,\
@abi-omnipotent
3658 jz
.Lscatter_epilogue
3659 lea
($tbl,$idx,8),$tbl
3670 .size bn_scatter5
,.-bn_scatter5
3673 .type bn_gather5
,\
@abi-omnipotent
3676 .LSEH_begin_bn_gather5
: # Win64 thing, but harmless in other cases
3678 # I can't trust assembler to use specific encoding:-(
3679 .byte
0x4c,0x8d,0x14,0x24 #lea (%rsp),%r10
3680 .byte
0x48,0x81,0xec,0x08,0x01,0x00,0x00 #sub $0x108,%rsp
3681 lea
.Linc
(%rip),%rax
3682 and \
$-16,%rsp # shouldn't be formally required
3685 movdqa
0(%rax),%xmm0 # 00000001000000010000000000000000
3686 movdqa
16(%rax),%xmm1 # 00000002000000020000000200000002
3687 lea
128($tbl),%r11 # size optimization
3688 lea
128(%rsp),%rax # size optimization
3690 pshufd \
$0,%xmm5,%xmm5 # broadcast $idx
3694 ########################################################################
3695 # calculate mask by comparing 0..31 to $idx and save result to stack
3697 for($i=0;$i<$STRIDE/16;$i+=4) {
3700 pcmpeqd
%xmm5,%xmm0 # compare to 1,0
3702 $code.=<<___
if ($i);
3703 movdqa
%xmm3,`16*($i-1)-128`(%rax)
3709 pcmpeqd
%xmm5,%xmm1 # compare to 3,2
3710 movdqa
%xmm0,`16*($i+0)-128`(%rax)
3714 pcmpeqd
%xmm5,%xmm2 # compare to 5,4
3715 movdqa
%xmm1,`16*($i+1)-128`(%rax)
3719 pcmpeqd
%xmm5,%xmm3 # compare to 7,6
3720 movdqa
%xmm2,`16*($i+2)-128`(%rax)
3725 movdqa
%xmm3,`16*($i-1)-128`(%rax)
3733 for($i=0;$i<$STRIDE/16;$i+=4) {
3735 movdqa
`16*($i+0)-128`(%r11),%xmm0
3736 movdqa
`16*($i+1)-128`(%r11),%xmm1
3737 movdqa
`16*($i+2)-128`(%r11),%xmm2
3738 pand
`16*($i+0)-128`(%rax),%xmm0
3739 movdqa
`16*($i+3)-128`(%r11),%xmm3
3740 pand
`16*($i+1)-128`(%rax),%xmm1
3742 pand
`16*($i+2)-128`(%rax),%xmm2
3744 pand
`16*($i+3)-128`(%rax),%xmm3
3751 lea
$STRIDE(%r11),%r11
3752 pshufd \
$0x4e,%xmm4,%xmm0
3754 movq
%xmm0,($out) # m0=bp[0]
3761 .LSEH_end_bn_gather5
:
3763 .size bn_gather5
,.-bn_gather5
3771 .asciz
"Montgomery Multiplication with scatter/gather for x86_64, CRYPTOGAMS by <appro\@openssl.org>"
3774 # EXCEPTION_DISPOSITION handler (EXCEPTION_RECORD *rec,ULONG64 frame,
3775 # CONTEXT *context,DISPATCHER_CONTEXT *disp)
3783 .extern __imp_RtlVirtualUnwind
3784 .type mul_handler
,\
@abi-omnipotent
3798 mov
120($context),%rax # pull context->Rax
3799 mov
248($context),%rbx # pull context->Rip
3801 mov
8($disp),%rsi # disp->ImageBase
3802 mov
56($disp),%r11 # disp->HandlerData
3804 mov
0(%r11),%r10d # HandlerData[0]
3805 lea
(%rsi,%r10),%r10 # end of prologue label
3806 cmp %r10,%rbx # context->Rip<end of prologue label
3807 jb
.Lcommon_seh_tail
3809 mov
4(%r11),%r10d # HandlerData[1]
3810 lea
(%rsi,%r10),%r10 # beginning of body label
3811 cmp %r10,%rbx # context->Rip<body label
3812 jb
.Lcommon_pop_regs
3814 mov
152($context),%rax # pull context->Rsp
3816 mov
8(%r11),%r10d # HandlerData[2]
3817 lea
(%rsi,%r10),%r10 # epilogue label
3818 cmp %r10,%rbx # context->Rip>=epilogue label
3819 jae
.Lcommon_seh_tail
3821 lea
.Lmul_epilogue
(%rip),%r10
3825 mov
192($context),%r10 # pull $num
3826 mov
8(%rax,%r10,8),%rax # pull saved stack pointer
3828 jmp
.Lcommon_pop_regs
3831 mov
40(%rax),%rax # pull saved stack pointer
3839 mov
%rbx,144($context) # restore context->Rbx
3840 mov
%rbp,160($context) # restore context->Rbp
3841 mov
%r12,216($context) # restore context->R12
3842 mov
%r13,224($context) # restore context->R13
3843 mov
%r14,232($context) # restore context->R14
3844 mov
%r15,240($context) # restore context->R15
3849 mov
%rax,152($context) # restore context->Rsp
3850 mov
%rsi,168($context) # restore context->Rsi
3851 mov
%rdi,176($context) # restore context->Rdi
3853 mov
40($disp),%rdi # disp->ContextRecord
3854 mov
$context,%rsi # context
3855 mov \
$154,%ecx # sizeof(CONTEXT)
3856 .long
0xa548f3fc # cld; rep movsq
3859 xor %rcx,%rcx # arg1, UNW_FLAG_NHANDLER
3860 mov
8(%rsi),%rdx # arg2, disp->ImageBase
3861 mov
0(%rsi),%r8 # arg3, disp->ControlPc
3862 mov
16(%rsi),%r9 # arg4, disp->FunctionEntry
3863 mov
40(%rsi),%r10 # disp->ContextRecord
3864 lea
56(%rsi),%r11 # &disp->HandlerData
3865 lea
24(%rsi),%r12 # &disp->EstablisherFrame
3866 mov
%r10,32(%rsp) # arg5
3867 mov
%r11,40(%rsp) # arg6
3868 mov
%r12,48(%rsp) # arg7
3869 mov
%rcx,56(%rsp) # arg8, (NULL)
3870 call
*__imp_RtlVirtualUnwind
(%rip)
3872 mov \
$1,%eax # ExceptionContinueSearch
3884 .size mul_handler
,.-mul_handler
3888 .rva
.LSEH_begin_bn_mul_mont_gather5
3889 .rva
.LSEH_end_bn_mul_mont_gather5
3890 .rva
.LSEH_info_bn_mul_mont_gather5
3892 .rva
.LSEH_begin_bn_mul4x_mont_gather5
3893 .rva
.LSEH_end_bn_mul4x_mont_gather5
3894 .rva
.LSEH_info_bn_mul4x_mont_gather5
3896 .rva
.LSEH_begin_bn_power5
3897 .rva
.LSEH_end_bn_power5
3898 .rva
.LSEH_info_bn_power5
3900 .rva
.LSEH_begin_bn_from_mont8x
3901 .rva
.LSEH_end_bn_from_mont8x
3902 .rva
.LSEH_info_bn_from_mont8x
3904 $code.=<<___
if ($addx);
3905 .rva
.LSEH_begin_bn_mulx4x_mont_gather5
3906 .rva
.LSEH_end_bn_mulx4x_mont_gather5
3907 .rva
.LSEH_info_bn_mulx4x_mont_gather5
3909 .rva
.LSEH_begin_bn_powerx5
3910 .rva
.LSEH_end_bn_powerx5
3911 .rva
.LSEH_info_bn_powerx5
3914 .rva
.LSEH_begin_bn_gather5
3915 .rva
.LSEH_end_bn_gather5
3916 .rva
.LSEH_info_bn_gather5
3920 .LSEH_info_bn_mul_mont_gather5
:
3923 .rva
.Lmul_body
,.Lmul_body
,.Lmul_epilogue
# HandlerData[]
3925 .LSEH_info_bn_mul4x_mont_gather5
:
3928 .rva
.Lmul4x_prologue
,.Lmul4x_body
,.Lmul4x_epilogue
# HandlerData[]
3930 .LSEH_info_bn_power5
:
3933 .rva
.Lpower5_prologue
,.Lpower5_body
,.Lpower5_epilogue
# HandlerData[]
3935 .LSEH_info_bn_from_mont8x
:
3938 .rva
.Lfrom_prologue
,.Lfrom_body
,.Lfrom_epilogue
# HandlerData[]
3940 $code.=<<___
if ($addx);
3942 .LSEH_info_bn_mulx4x_mont_gather5
:
3945 .rva
.Lmulx4x_prologue
,.Lmulx4x_body
,.Lmulx4x_epilogue
# HandlerData[]
3947 .LSEH_info_bn_powerx5
:
3950 .rva
.Lpowerx5_prologue
,.Lpowerx5_body
,.Lpowerx5_epilogue
# HandlerData[]
3954 .LSEH_info_bn_gather5
:
3955 .byte
0x01,0x0b,0x03,0x0a
3956 .byte
0x0b,0x01,0x21,0x00 # sub rsp,0x108
3957 .byte
0x04,0xa3,0x00,0x00 # lea r10,(rsp)
3962 $code =~ s/\`([^\`]*)\`/eval($1)/gem;
3965 close STDOUT
or die "error closing STDOUT: $!";