6701bf2755f6e5807ec13c557b71bec5d4637fee
[openssl.git] / crypto / bn / asm / x86_64-mont.pl
1 #!/usr/bin/env perl
2
3 # ====================================================================
4 # Written by Andy Polyakov <appro@fy.chalmers.se> for the OpenSSL
5 # project. The module is, however, dual licensed under OpenSSL and
6 # CRYPTOGAMS licenses depending on where you obtain it. For further
7 # details see http://www.openssl.org/~appro/cryptogams/.
8 # ====================================================================
9
10 # October 2005.
11 #
12 # Montgomery multiplication routine for x86_64. While it gives modest
13 # 9% improvement of rsa4096 sign on Opteron, rsa512 sign runs more
14 # than twice, >2x, as fast. Most common rsa1024 sign is improved by
15 # respectful 50%. It remains to be seen if loop unrolling and
16 # dedicated squaring routine can provide further improvement...
17
18 $output=shift;
19
20 $0 =~ m/(.*[\/\\])[^\/\\]+$/; $dir=$1;
21 ( $xlate="${dir}x86_64-xlate.pl" and -f $xlate ) or
22 ( $xlate="${dir}../../perlasm/x86_64-xlate.pl" and -f $xlate) or
23 die "can't locate x86_64-xlate.pl";
24
25 open STDOUT,"| $^X $xlate $output";
26
27 # int bn_mul_mont(
28 $rp="%rdi";     # BN_ULONG *rp,
29 $ap="%rsi";     # const BN_ULONG *ap,
30 $bp="%rdx";     # const BN_ULONG *bp,
31 $np="%rcx";     # const BN_ULONG *np,
32 $n0="%r8";      # const BN_ULONG *n0,
33 $num="%r9";     # int num);
34 $lo0="%r10";
35 $hi0="%r11";
36 $bp="%r12";     # reassign $bp
37 $hi1="%r13";
38 $i="%r14";
39 $j="%r15";
40 $m0="%rbx";
41 $m1="%rbp";
42
43 $code=<<___;
44 .text
45
46 .globl  bn_mul_mont
47 .type   bn_mul_mont,\@function,6
48 .align  16
49 bn_mul_mont:
50         push    %rbx
51         push    %rbp
52         push    %r12
53         push    %r13
54         push    %r14
55         push    %r15
56
57         lea     2($num),%rax
58         mov     %rsp,%rbp
59         neg     %rax
60         lea     (%rsp,%rax,8),%rsp      # tp=alloca(8*(num+2))
61         and     \$-1024,%rsp            # minimize TLB usage
62
63         mov     %rbp,8(%rsp,$num,8)     # tp[num+1]=%rsp
64         mov     %rdx,$bp                # $bp reassigned, remember?
65
66         mov     ($n0),$n0               # pull n0[0] value
67
68         xor     $i,$i                   # i=0
69         xor     $j,$j                   # j=0
70
71         mov     ($bp),$m0               # m0=bp[0]
72         mov     ($ap),%rax
73         mulq    $m0                     # ap[0]*bp[0]
74         mov     %rax,$lo0
75         mov     %rdx,$hi0
76
77         imulq   $n0,%rax                # "tp[0]"*n0
78         mov     %rax,$m1
79
80         mulq    ($np)                   # np[0]*m1
81         add     $lo0,%rax               # discarded
82         adc     \$0,%rdx
83         mov     %rdx,$hi1
84
85         lea     1($j),$j                # j++
86 .L1st:
87         mov     ($ap,$j,8),%rax
88         mulq    $m0                     # ap[j]*bp[0]
89         add     $hi0,%rax
90         adc     \$0,%rdx
91         mov     %rax,$lo0
92         mov     ($np,$j,8),%rax
93         mov     %rdx,$hi0
94
95         mulq    $m1                     # np[j]*m1
96         add     $hi1,%rax
97         lea     1($j),$j                # j++
98         adc     \$0,%rdx
99         add     $lo0,%rax               # np[j]*m1+ap[j]*bp[0]
100         adc     \$0,%rdx
101         mov     %rax,-16(%rsp,$j,8)     # tp[j-1]
102         cmp     $num,$j
103         mov     %rdx,$hi1
104         jl      .L1st
105
106         xor     %rdx,%rdx
107         add     $hi0,$hi1
108         adc     \$0,%rdx
109         mov     $hi1,-8(%rsp,$num,8)
110         mov     %rdx,(%rsp,$num,8)      # store upmost overflow bit
111
112         lea     1($i),$i                # i++
113 .align  4
114 .Louter:
115         xor     $j,$j                   # j=0
116
117         mov     ($bp,$i,8),$m0          # m0=bp[i]
118         mov     ($ap),%rax              # ap[0]
119         mulq    $m0                     # ap[0]*bp[i]
120         add     (%rsp),%rax             # ap[0]*bp[i]+tp[0]
121         adc     \$0,%rdx
122         mov     %rax,$lo0
123         mov     %rdx,$hi0
124
125         imulq   $n0,%rax                # tp[0]*n0
126         mov     %rax,$m1
127
128         mulq    ($np,$j,8)              # np[0]*m1
129         add     $lo0,%rax               # discarded
130         mov     8(%rsp),$lo0            # tp[1]
131         adc     \$0,%rdx
132         mov     %rdx,$hi1
133
134         lea     1($j),$j                # j++
135 .align  4
136 .Linner:
137         mov     ($ap,$j,8),%rax
138         mulq    $m0                     # ap[j]*bp[i]
139         add     $hi0,%rax
140         adc     \$0,%rdx
141         add     %rax,$lo0               # ap[j]*bp[i]+tp[j]
142         mov     ($np,$j,8),%rax
143         adc     \$0,%rdx
144         mov     %rdx,$hi0
145
146         mulq    $m1                     # np[j]*m1
147         add     $hi1,%rax
148         lea     1($j),$j                # j++
149         adc     \$0,%rdx
150         add     $lo0,%rax               # np[j]*m1+ap[j]*bp[i]+tp[j]
151         adc     \$0,%rdx
152         mov     (%rsp,$j,8),$lo0
153         cmp     $num,$j
154         mov     %rax,-16(%rsp,$j,8)     # tp[j-1]
155         mov     %rdx,$hi1
156         jl      .Linner
157
158         xor     %rdx,%rdx
159         add     $hi0,$hi1
160         adc     \$0,%rdx
161         add     $lo0,$hi1               # pull upmost overflow bit
162         adc     \$0,%rdx
163         mov     $hi1,-8(%rsp,$num,8)
164         mov     %rdx,(%rsp,$num,8)      # store upmost overflow bit
165
166         lea     1($i),$i                # i++
167         cmp     $num,$i
168         jl      .Louter
169
170         mov     -8($np,$num,8),%rax     # np[num-1]
171         lea     (%rsp),$ap              # borrow ap for tp
172         shr     \$62,%rax               # check for boundary condition
173         jz      .Lcopy
174
175         mov     ($ap),%rax              # tp[0]
176         lea     -1($num),$j             # j=num-1
177         xor     $i,$i                   # i=0 and clear CF!
178         jmp     .Lsub
179 .align  16
180 .Lsub:  sbb     ($np,$i,8),%rax
181         mov     %rax,($rp,$i,8)         # rp[i]=tp[i]-np[i]
182         dec     $j                      # doesn't affect CF!
183         mov     8($ap,$i,8),%rax        # tp[i+1]
184         lea     1($i),$i                # i++
185         jge     .Lsub
186
187         sbb     \$0,%rax                # handle upmost overflow bit
188         and     %rax,$ap
189         not     %rax
190         mov     $rp,$np
191         and     %rax,$np
192         lea     -1($num),$j
193         or      $np,$ap                 # ap=borrow?tp:rp
194 .align  16
195 .Lcopy:                                 # copy or in-place refresh
196         mov     ($ap,$j,8),%rax
197         mov     %rax,($rp,$j,8)         # rp[i]=tp[i]
198         mov     $i,(%rsp,$j,8)          # zap temporary vector
199         dec     $j
200         jge     .Lcopy
201         
202         mov     8(%rsp,$num,8),%rsp     # restore %rsp
203         mov     \$1,%rax
204         pop     %r15
205         pop     %r14
206         pop     %r13
207         pop     %r12
208         pop     %rbp
209         pop     %rbx
210         ret
211 .size   bn_mul_mont,.-bn_mul_mont
212 .asciz  "Montgomery Multiplication for x86_64, CRYPTOGAMS by <appro\@openssl.org>"
213 ___
214
215 print $code;
216 close STDOUT;