bc46f1938728137f97869a19849f1bf67e69da8a
[openssl.git] / crypto / md5 / asm / md5-x86_64.pl
1 #!/usr/bin/perl -w
2 #
3 # MD5 optimized for AMD64.
4 #
5 # Author: Marc Bevand <bevand_m (at) epita.fr>
6 # Licence: I hereby disclaim the copyright on this code and place it
7 # in the public domain.
8 #
9
10 use strict;
11
12 my $code;
13
14 # round1_step() does:
15 #   dst = x + ((dst + F(x,y,z) + X[k] + T_i) <<< s)
16 #   %r10d = X[k_next]
17 #   %r11d = z' (copy of z for the next step)
18 # Each round1_step() takes about 5.3 clocks (9 instructions, 1.7 IPC)
19 sub round1_step
20 {
21     my ($pos, $dst, $x, $y, $z, $k_next, $T_i, $s) = @_;
22     $code .= " mov      0*4(%rsi),      %r10d           /* (NEXT STEP) X[0] */\n" if ($pos == -1);
23     $code .= " mov      %edx,           %r11d           /* (NEXT STEP) z' = %edx */\n" if ($pos == -1);
24     $code .= <<EOF;
25         xor     $y,             %r11d           /* y ^ ... */
26         lea     $T_i($dst,%r10d),$dst           /* Const + dst + ... */
27         and     $x,             %r11d           /* x & ... */
28         mov     $k_next*4(%rsi),%r10d           /* (NEXT STEP) X[$k_next] */
29         xor     $z,             %r11d           /* z ^ ... */
30         add     %r11d,          $dst            /* dst += ... */
31         rol     \$$s,           $dst            /* dst <<< s */
32         mov     $y,             %r11d           /* (NEXT STEP) z' = $y */
33         add     $x,             $dst            /* dst += x */
34 EOF
35 }
36
37 # round2_step() does:
38 #   dst = x + ((dst + G(x,y,z) + X[k] + T_i) <<< s)
39 #   %r10d = X[k_next]
40 #   %r11d = z' (copy of z for the next step)
41 #   %r12d = z' (copy of z for the next step)
42 # Each round2_step() takes about 5.4 clocks (11 instructions, 2.0 IPC)
43 sub round2_step
44 {
45     my ($pos, $dst, $x, $y, $z, $k_next, $T_i, $s) = @_;
46     $code .= " mov      %edx,           %r11d           /* (NEXT STEP) z' = %edx */\n" if ($pos == -1);
47     $code .= " mov      %edx,           %r12d           /* (NEXT STEP) z' = %edx */\n" if ($pos == -1);
48     $code .= <<EOF;
49         not     %r11d                           /* not z */
50         lea     $T_i($dst,%r10d),$dst           /* Const + dst + ... */
51         and     $x,             %r12d           /* x & z */
52         and     $y,             %r11d           /* y & (not z) */
53         mov     $k_next*4(%rsi),%r10d           /* (NEXT STEP) X[$k_next] */
54         or      %r11d,          %r12d           /* (y & (not z)) | (x & z) */
55         mov     $y,             %r11d           /* (NEXT STEP) z' = $y */
56         add     %r12d,          $dst            /* dst += ... */
57         mov     $y,             %r12d           /* (NEXT STEP) z' = $y */
58         rol     \$$s,           $dst            /* dst <<< s */
59         add     $x,             $dst            /* dst += x */
60 EOF
61 }
62
63 # round3_step() does:
64 #   dst = x + ((dst + H(x,y,z) + X[k] + T_i) <<< s)
65 #   %r10d = X[k_next]
66 #   %r11d = y' (copy of y for the next step)
67 # Each round3_step() takes about 4.2 clocks (8 instructions, 1.9 IPC)
68 sub round3_step
69 {
70     my ($pos, $dst, $x, $y, $z, $k_next, $T_i, $s) = @_;
71     $code .= " mov      %ecx,           %r11d           /* (NEXT STEP) y' = %ecx */\n" if ($pos == -1);
72     $code .= <<EOF;
73         lea     $T_i($dst,%r10d),$dst           /* Const + dst + ... */
74         xor     $z,             %r11d           /* z ^ ... */
75         mov     $k_next*4(%rsi),%r10d           /* (NEXT STEP) X[$k_next] */
76         xor     $x,             %r11d           /* x ^ ... */
77         add     %r11d,          $dst            /* dst += ... */
78         rol     \$$s,           $dst            /* dst <<< s */
79         mov     $x,             %r11d           /* (NEXT STEP) y' = $x */
80         add     $x,             $dst            /* dst += x */
81 EOF
82 }
83
84 # round4_step() does:
85 #   dst = x + ((dst + I(x,y,z) + X[k] + T_i) <<< s)
86 #   %r10d = X[k_next]
87 #   %r11d = not z' (copy of not z for the next step)
88 # Each round4_step() takes about 5.2 clocks (9 instructions, 1.7 IPC)
89 sub round4_step
90 {
91     my ($pos, $dst, $x, $y, $z, $k_next, $T_i, $s) = @_;
92     $code .= " mov      \$0xffffffff,   %r11d\n" if ($pos == -1);
93     $code .= " xor      %edx,           %r11d           /* (NEXT STEP) not z' = not %edx*/\n"
94     if ($pos == -1);
95     $code .= <<EOF;
96         lea     $T_i($dst,%r10d),$dst           /* Const + dst + ... */
97         or      $x,             %r11d           /* x | ... */
98         mov     $k_next*4(%rsi),%r10d           /* (NEXT STEP) X[$k_next] */
99         xor     $y,             %r11d           /* y ^ ... */
100         add     %r11d,          $dst            /* dst += ... */
101         mov     \$0xffffffff,   %r11d
102         rol     \$$s,           $dst            /* dst <<< s */
103         xor     $y,             %r11d           /* (NEXT STEP) not z' = not $y */
104         add     $x,             $dst            /* dst += x */
105 EOF
106 }
107
108 my $flavour = shift;
109 my $output  = shift;
110 if ($flavour =~ /\./) { $output = $flavour; undef $flavour; }
111
112 my $win64=0; $win64=1 if ($flavour =~ /[nm]asm|mingw64/ || $output =~ /\.asm$/);
113
114 $0 =~ m/(.*[\/\\])[^\/\\]+$/; my $dir=$1; my $xlate;
115 ( $xlate="${dir}x86_64-xlate.pl" and -f $xlate ) or
116 ( $xlate="${dir}../../perlasm/x86_64-xlate.pl" and -f $xlate) or
117 die "can't locate x86_64-xlate.pl";
118
119 no warnings qw(uninitialized);
120 open STDOUT,"| $^X $xlate $flavour $output";
121
122 $code .= <<EOF;
123 .text
124 .align 16
125
126 .globl md5_block_asm_data_order
127 .type md5_block_asm_data_order,\@function,3
128 md5_block_asm_data_order:
129         push    %rbp
130         push    %rbx
131         push    %r12
132         push    %r14
133         push    %r15
134 .Lprologue:
135
136         # rdi = arg #1 (ctx, MD5_CTX pointer)
137         # rsi = arg #2 (ptr, data pointer)
138         # rdx = arg #3 (nbr, number of 16-word blocks to process)
139         mov     %rdi,           %rbp    # rbp = ctx
140         shl     \$6,            %rdx    # rdx = nbr in bytes
141         lea     (%rsi,%rdx),    %rdi    # rdi = end
142         mov     0*4(%rbp),      %eax    # eax = ctx->A
143         mov     1*4(%rbp),      %ebx    # ebx = ctx->B
144         mov     2*4(%rbp),      %ecx    # ecx = ctx->C
145         mov     3*4(%rbp),      %edx    # edx = ctx->D
146         # end is 'rdi'
147         # ptr is 'rsi'
148         # A is 'eax'
149         # B is 'ebx'
150         # C is 'ecx'
151         # D is 'edx'
152
153         cmp     %rdi,           %rsi            # cmp end with ptr
154         je      .Lend                           # jmp if ptr == end
155
156         # BEGIN of loop over 16-word blocks
157 .Lloop: # save old values of A, B, C, D
158         mov     %eax,           %r8d
159         mov     %ebx,           %r9d
160         mov     %ecx,           %r14d
161         mov     %edx,           %r15d
162 EOF
163 round1_step(-1,'%eax','%ebx','%ecx','%edx', '1','0xd76aa478', '7');
164 round1_step( 0,'%edx','%eax','%ebx','%ecx', '2','0xe8c7b756','12');
165 round1_step( 0,'%ecx','%edx','%eax','%ebx', '3','0x242070db','17');
166 round1_step( 0,'%ebx','%ecx','%edx','%eax', '4','0xc1bdceee','22');
167 round1_step( 0,'%eax','%ebx','%ecx','%edx', '5','0xf57c0faf', '7');
168 round1_step( 0,'%edx','%eax','%ebx','%ecx', '6','0x4787c62a','12');
169 round1_step( 0,'%ecx','%edx','%eax','%ebx', '7','0xa8304613','17');
170 round1_step( 0,'%ebx','%ecx','%edx','%eax', '8','0xfd469501','22');
171 round1_step( 0,'%eax','%ebx','%ecx','%edx', '9','0x698098d8', '7');
172 round1_step( 0,'%edx','%eax','%ebx','%ecx','10','0x8b44f7af','12');
173 round1_step( 0,'%ecx','%edx','%eax','%ebx','11','0xffff5bb1','17');
174 round1_step( 0,'%ebx','%ecx','%edx','%eax','12','0x895cd7be','22');
175 round1_step( 0,'%eax','%ebx','%ecx','%edx','13','0x6b901122', '7');
176 round1_step( 0,'%edx','%eax','%ebx','%ecx','14','0xfd987193','12');
177 round1_step( 0,'%ecx','%edx','%eax','%ebx','15','0xa679438e','17');
178 round1_step( 1,'%ebx','%ecx','%edx','%eax', '1','0x49b40821','22');
179
180 round2_step(-1,'%eax','%ebx','%ecx','%edx', '6','0xf61e2562', '5');
181 round2_step( 0,'%edx','%eax','%ebx','%ecx','11','0xc040b340', '9');
182 round2_step( 0,'%ecx','%edx','%eax','%ebx', '0','0x265e5a51','14');
183 round2_step( 0,'%ebx','%ecx','%edx','%eax', '5','0xe9b6c7aa','20');
184 round2_step( 0,'%eax','%ebx','%ecx','%edx','10','0xd62f105d', '5');
185 round2_step( 0,'%edx','%eax','%ebx','%ecx','15', '0x2441453', '9');
186 round2_step( 0,'%ecx','%edx','%eax','%ebx', '4','0xd8a1e681','14');
187 round2_step( 0,'%ebx','%ecx','%edx','%eax', '9','0xe7d3fbc8','20');
188 round2_step( 0,'%eax','%ebx','%ecx','%edx','14','0x21e1cde6', '5');
189 round2_step( 0,'%edx','%eax','%ebx','%ecx', '3','0xc33707d6', '9');
190 round2_step( 0,'%ecx','%edx','%eax','%ebx', '8','0xf4d50d87','14');
191 round2_step( 0,'%ebx','%ecx','%edx','%eax','13','0x455a14ed','20');
192 round2_step( 0,'%eax','%ebx','%ecx','%edx', '2','0xa9e3e905', '5');
193 round2_step( 0,'%edx','%eax','%ebx','%ecx', '7','0xfcefa3f8', '9');
194 round2_step( 0,'%ecx','%edx','%eax','%ebx','12','0x676f02d9','14');
195 round2_step( 1,'%ebx','%ecx','%edx','%eax', '5','0x8d2a4c8a','20');
196
197 round3_step(-1,'%eax','%ebx','%ecx','%edx', '8','0xfffa3942', '4');
198 round3_step( 0,'%edx','%eax','%ebx','%ecx','11','0x8771f681','11');
199 round3_step( 0,'%ecx','%edx','%eax','%ebx','14','0x6d9d6122','16');
200 round3_step( 0,'%ebx','%ecx','%edx','%eax', '1','0xfde5380c','23');
201 round3_step( 0,'%eax','%ebx','%ecx','%edx', '4','0xa4beea44', '4');
202 round3_step( 0,'%edx','%eax','%ebx','%ecx', '7','0x4bdecfa9','11');
203 round3_step( 0,'%ecx','%edx','%eax','%ebx','10','0xf6bb4b60','16');
204 round3_step( 0,'%ebx','%ecx','%edx','%eax','13','0xbebfbc70','23');
205 round3_step( 0,'%eax','%ebx','%ecx','%edx', '0','0x289b7ec6', '4');
206 round3_step( 0,'%edx','%eax','%ebx','%ecx', '3','0xeaa127fa','11');
207 round3_step( 0,'%ecx','%edx','%eax','%ebx', '6','0xd4ef3085','16');
208 round3_step( 0,'%ebx','%ecx','%edx','%eax', '9', '0x4881d05','23');
209 round3_step( 0,'%eax','%ebx','%ecx','%edx','12','0xd9d4d039', '4');
210 round3_step( 0,'%edx','%eax','%ebx','%ecx','15','0xe6db99e5','11');
211 round3_step( 0,'%ecx','%edx','%eax','%ebx', '2','0x1fa27cf8','16');
212 round3_step( 1,'%ebx','%ecx','%edx','%eax', '0','0xc4ac5665','23');
213
214 round4_step(-1,'%eax','%ebx','%ecx','%edx', '7','0xf4292244', '6');
215 round4_step( 0,'%edx','%eax','%ebx','%ecx','14','0x432aff97','10');
216 round4_step( 0,'%ecx','%edx','%eax','%ebx', '5','0xab9423a7','15');
217 round4_step( 0,'%ebx','%ecx','%edx','%eax','12','0xfc93a039','21');
218 round4_step( 0,'%eax','%ebx','%ecx','%edx', '3','0x655b59c3', '6');
219 round4_step( 0,'%edx','%eax','%ebx','%ecx','10','0x8f0ccc92','10');
220 round4_step( 0,'%ecx','%edx','%eax','%ebx', '1','0xffeff47d','15');
221 round4_step( 0,'%ebx','%ecx','%edx','%eax', '8','0x85845dd1','21');
222 round4_step( 0,'%eax','%ebx','%ecx','%edx','15','0x6fa87e4f', '6');
223 round4_step( 0,'%edx','%eax','%ebx','%ecx', '6','0xfe2ce6e0','10');
224 round4_step( 0,'%ecx','%edx','%eax','%ebx','13','0xa3014314','15');
225 round4_step( 0,'%ebx','%ecx','%edx','%eax', '4','0x4e0811a1','21');
226 round4_step( 0,'%eax','%ebx','%ecx','%edx','11','0xf7537e82', '6');
227 round4_step( 0,'%edx','%eax','%ebx','%ecx', '2','0xbd3af235','10');
228 round4_step( 0,'%ecx','%edx','%eax','%ebx', '9','0x2ad7d2bb','15');
229 round4_step( 1,'%ebx','%ecx','%edx','%eax', '0','0xeb86d391','21');
230 $code .= <<EOF;
231         # add old values of A, B, C, D
232         add     %r8d,   %eax
233         add     %r9d,   %ebx
234         add     %r14d,  %ecx
235         add     %r15d,  %edx
236
237         # loop control
238         add     \$64,           %rsi            # ptr += 64
239         cmp     %rdi,           %rsi            # cmp end with ptr
240         jb      .Lloop                          # jmp if ptr < end
241         # END of loop over 16-word blocks
242
243 .Lend:
244         mov     %eax,           0*4(%rbp)       # ctx->A = A
245         mov     %ebx,           1*4(%rbp)       # ctx->B = B
246         mov     %ecx,           2*4(%rbp)       # ctx->C = C
247         mov     %edx,           3*4(%rbp)       # ctx->D = D
248
249         mov     (%rsp),%r15
250         mov     8(%rsp),%r14
251         mov     16(%rsp),%r12
252         mov     24(%rsp),%rbx
253         mov     32(%rsp),%rbp
254         add     \$40,%rsp
255 .Lepilogue:
256         ret
257 .size md5_block_asm_data_order,.-md5_block_asm_data_order
258 EOF
259
260 # EXCEPTION_DISPOSITION handler (EXCEPTION_RECORD *rec,ULONG64 frame,
261 #               CONTEXT *context,DISPATCHER_CONTEXT *disp)
262 if ($win64) {
263 my $rec="%rcx";
264 my $frame="%rdx";
265 my $context="%r8";
266 my $disp="%r9";
267
268 $code.=<<___;
269 .extern __imp_RtlVirtualUnwind
270 .type   se_handler,\@abi-omnipotent
271 .align  16
272 se_handler:
273         push    %rsi
274         push    %rdi
275         push    %rbx
276         push    %rbp
277         push    %r12
278         push    %r13
279         push    %r14
280         push    %r15
281         pushfq
282         sub     \$64,%rsp
283
284         mov     120($context),%rax      # pull context->Rax
285         mov     248($context),%rbx      # pull context->Rip
286
287         lea     .Lprologue(%rip),%r10
288         cmp     %r10,%rbx               # context->Rip<.Lprologue
289         jb      .Lin_prologue
290
291         mov     152($context),%rax      # pull context->Rsp
292
293         lea     .Lepilogue(%rip),%r10
294         cmp     %r10,%rbx               # context->Rip>=.Lepilogue
295         jae     .Lin_prologue
296
297         lea     40(%rax),%rax
298
299         mov     -8(%rax),%rbp
300         mov     -16(%rax),%rbx
301         mov     -24(%rax),%r12
302         mov     -32(%rax),%r14
303         mov     -40(%rax),%r15
304         mov     %rbx,144($context)      # restore context->Rbx
305         mov     %rbp,160($context)      # restore context->Rbp
306         mov     %r12,216($context)      # restore context->R12
307         mov     %r14,232($context)      # restore context->R14
308         mov     %r15,240($context)      # restore context->R15
309
310 .Lin_prologue:
311         mov     8(%rax),%rdi
312         mov     16(%rax),%rsi
313         mov     %rax,152($context)      # restore context->Rsp
314         mov     %rsi,168($context)      # restore context->Rsi
315         mov     %rdi,176($context)      # restore context->Rdi
316
317         mov     40($disp),%rdi          # disp->ContextRecord
318         mov     $context,%rsi           # context
319         mov     \$154,%ecx              # sizeof(CONTEXT)
320         .long   0xa548f3fc              # cld; rep movsq
321
322         mov     $disp,%rsi
323         xor     %rcx,%rcx               # arg1, UNW_FLAG_NHANDLER
324         mov     8(%rsi),%rdx            # arg2, disp->ImageBase
325         mov     0(%rsi),%r8             # arg3, disp->ControlPc
326         mov     16(%rsi),%r9            # arg4, disp->FunctionEntry
327         mov     40(%rsi),%r10           # disp->ContextRecord
328         lea     56(%rsi),%r11           # &disp->HandlerData
329         lea     24(%rsi),%r12           # &disp->EstablisherFrame
330         mov     %r10,32(%rsp)           # arg5
331         mov     %r11,40(%rsp)           # arg6
332         mov     %r12,48(%rsp)           # arg7
333         mov     %rcx,56(%rsp)           # arg8, (NULL)
334         call    *__imp_RtlVirtualUnwind(%rip)
335
336         mov     \$1,%eax                # ExceptionContinueSearch
337         add     \$64,%rsp
338         popfq
339         pop     %r15
340         pop     %r14
341         pop     %r13
342         pop     %r12
343         pop     %rbp
344         pop     %rbx
345         pop     %rdi
346         pop     %rsi
347         ret
348 .size   se_handler,.-se_handler
349
350 .section        .pdata
351 .align  4
352         .rva    .LSEH_begin_md5_block_asm_data_order
353         .rva    .LSEH_end_md5_block_asm_data_order
354         .rva    .LSEH_info_md5_block_asm_data_order
355
356 .section        .xdata
357 .align  8
358 .LSEH_info_md5_block_asm_data_order:
359         .byte   9,0,0,0
360         .rva    se_handler
361 ___
362 }
363
364 print $code;
365
366 close STDOUT;