Throw in md5-x86_64 assembler.
[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.71 clocks (9 instructions, 1.58 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         xor     $z,             %r11d           /* z ^ ... */
29         mov     $k_next*4(%rsi),%r10d           /* (NEXT STEP) X[$k_next] */
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 = y' (copy of y for the next step)
41 # Each round2_step() takes about 6.22 clocks (9 instructions, 1.45 IPC)
42 sub round2_step
43 {
44     my ($pos, $dst, $x, $y, $z, $k_next, $T_i, $s) = @_;
45     $code .= " mov      1*4(%rsi),      %r10d           /* (NEXT STEP) X[1] */\n" if ($pos == -1);
46     $code .= " mov      %ecx,           %r11d           /* (NEXT STEP) y' = %ecx */\n" if ($pos == -1);
47     $code .= <<EOF;
48         xor     $x,             %r11d           /* x ^ ... */
49         lea     $T_i($dst,%r10d),$dst           /* Const + dst + ... */
50         and     $z,             %r11d           /* z & ... */
51         xor     $y,             %r11d           /* y ^ ... */
52         mov     $k_next*4(%rsi),%r10d           /* (NEXT STEP) X[$k_next] */
53         add     %r11d,          $dst            /* dst += ... */
54         rol     \$$s,           $dst            /* dst <<< s */
55         mov     $x,             %r11d           /* (NEXT STEP) y' = $x */
56         add     $x,             $dst            /* dst += x */
57 EOF
58 }
59
60 # round3_step() does:
61 #   dst = x + ((dst + H(x,y,z) + X[k] + T_i) <<< s)
62 #   %r10d = X[k_next]
63 #   %r11d = y' (copy of y for the next step)
64 # Each round3_step() takes about 4.26 clocks (8 instructions, 1.88 IPC)
65 sub round3_step
66 {
67     my ($pos, $dst, $x, $y, $z, $k_next, $T_i, $s) = @_;
68     $code .= " mov      5*4(%rsi),      %r10d           /* (NEXT STEP) X[5] */\n" if ($pos == -1);
69     $code .= " mov      %ecx,           %r11d           /* (NEXT STEP) y' = %ecx */\n" if ($pos == -1);
70     $code .= <<EOF;
71         lea     $T_i($dst,%r10d),$dst           /* Const + dst + ... */
72         mov     $k_next*4(%rsi),%r10d           /* (NEXT STEP) X[$k_next] */
73         xor     $z,             %r11d           /* z ^ ... */
74         xor     $x,             %r11d           /* x ^ ... */
75         add     %r11d,          $dst            /* dst += ... */
76         rol     \$$s,           $dst            /* dst <<< s */
77         mov     $x,             %r11d           /* (NEXT STEP) y' = $x */
78         add     $x,             $dst            /* dst += x */
79 EOF
80 }
81
82 # round4_step() does:
83 #   dst = x + ((dst + I(x,y,z) + X[k] + T_i) <<< s)
84 #   %r10d = X[k_next]
85 #   %r11d = not z' (copy of not z for the next step)
86 # Each round4_step() takes about 5.27 clocks (9 instructions, 1.71 IPC)
87 sub round4_step
88 {
89     my ($pos, $dst, $x, $y, $z, $k_next, $T_i, $s) = @_;
90     $code .= " mov      0*4(%rsi),      %r10d           /* (NEXT STEP) X[0] */\n" if ($pos == -1);
91     $code .= " mov      \$0xffffffff,   %r11d\n" if ($pos == -1);
92     $code .= " xor      %edx,           %r11d           /* (NEXT STEP) not z' = not %edx*/\n"
93     if ($pos == -1);
94     $code .= <<EOF;
95         lea     $T_i($dst,%r10d),$dst           /* Const + dst + ... */
96         or      $x,             %r11d           /* x | ... */
97         xor     $y,             %r11d           /* y ^ ... */
98         add     %r11d,          $dst            /* dst += ... */
99         mov     $k_next*4(%rsi),%r10d           /* (NEXT STEP) X[$k_next] */
100         mov     \$0xffffffff,   %r11d
101         rol     \$$s,           $dst            /* dst <<< s */
102         xor     $y,             %r11d           /* (NEXT STEP) not z' = not $y */
103         add     $x,             $dst            /* dst += x */
104 EOF
105 }
106
107 my $output = shift;
108 open STDOUT,">$output" or die "can't open $output: $!";
109
110 $code .= <<EOF;
111 .text
112 .align 16
113
114 .globl md5_block_asm_host_order
115 .type md5_block_asm_host_order,\@function
116 md5_block_asm_host_order:
117         push    %rbp
118         push    %rbx
119         push    %r12
120         push    %r13
121         push    %r14
122         push    %r15
123
124         # rdi = arg #1 (ctx, MD5_CTX pointer)
125         # rsi = arg #2 (ptr, data pointer)
126         # rdx = arg #3 (nbr, number of 16-word blocks to process)
127         mov     %rdi,           %rbp    # rbp = ctx
128         shl     \$6,            %rdx    # rdx = nbr in bytes
129         lea     (%rsi,%rdx),    %rdi    # rdi = end
130         mov     0*4(%rbp),      %eax    # eax = ctx->A
131         mov     1*4(%rbp),      %ebx    # ebx = ctx->B
132         mov     2*4(%rbp),      %ecx    # ecx = ctx->C
133         mov     3*4(%rbp),      %edx    # edx = ctx->D
134         push    %rbp                    # save ctx
135         # end is 'rdi'
136         # ptr is 'rsi'
137         # A is 'eax'
138         # B is 'ebx'
139         # C is 'ecx'
140         # D is 'edx'
141
142         cmp     %rdi,           %rsi            # cmp end with ptr
143         je      1f                              # jmp if ptr == end
144
145         # BEGIN of loop over 16-word blocks
146 2:      # save old values of A, B, C, D
147         mov     %eax,           %r8d
148         mov     %ebx,           %r9d
149         mov     %ecx,           %r14d
150         mov     %edx,           %r15d
151 EOF
152 round1_step(-1,'%eax','%ebx','%ecx','%edx', '1','0xd76aa478', '7');
153 round1_step( 0,'%edx','%eax','%ebx','%ecx', '2','0xe8c7b756','12');
154 round1_step( 0,'%ecx','%edx','%eax','%ebx', '3','0x242070db','17');
155 round1_step( 0,'%ebx','%ecx','%edx','%eax', '4','0xc1bdceee','22');
156 round1_step( 0,'%eax','%ebx','%ecx','%edx', '5','0xf57c0faf', '7');
157 round1_step( 0,'%edx','%eax','%ebx','%ecx', '6','0x4787c62a','12');
158 round1_step( 0,'%ecx','%edx','%eax','%ebx', '7','0xa8304613','17');
159 round1_step( 0,'%ebx','%ecx','%edx','%eax', '8','0xfd469501','22');
160 round1_step( 0,'%eax','%ebx','%ecx','%edx', '9','0x698098d8', '7');
161 round1_step( 0,'%edx','%eax','%ebx','%ecx','10','0x8b44f7af','12');
162 round1_step( 0,'%ecx','%edx','%eax','%ebx','11','0xffff5bb1','17');
163 round1_step( 0,'%ebx','%ecx','%edx','%eax','12','0x895cd7be','22');
164 round1_step( 0,'%eax','%ebx','%ecx','%edx','13','0x6b901122', '7');
165 round1_step( 0,'%edx','%eax','%ebx','%ecx','14','0xfd987193','12');
166 round1_step( 0,'%ecx','%edx','%eax','%ebx','15','0xa679438e','17');
167 round1_step( 1,'%ebx','%ecx','%edx','%eax', '0','0x49b40821','22');
168
169 round2_step(-1,'%eax','%ebx','%ecx','%edx', '6','0xf61e2562', '5');
170 round2_step( 0,'%edx','%eax','%ebx','%ecx','11','0xc040b340', '9');
171 round2_step( 0,'%ecx','%edx','%eax','%ebx', '0','0x265e5a51','14');
172 round2_step( 0,'%ebx','%ecx','%edx','%eax', '5','0xe9b6c7aa','20');
173 round2_step( 0,'%eax','%ebx','%ecx','%edx','10','0xd62f105d', '5');
174 round2_step( 0,'%edx','%eax','%ebx','%ecx','15', '0x2441453', '9');
175 round2_step( 0,'%ecx','%edx','%eax','%ebx', '4','0xd8a1e681','14');
176 round2_step( 0,'%ebx','%ecx','%edx','%eax', '9','0xe7d3fbc8','20');
177 round2_step( 0,'%eax','%ebx','%ecx','%edx','14','0x21e1cde6', '5');
178 round2_step( 0,'%edx','%eax','%ebx','%ecx', '3','0xc33707d6', '9');
179 round2_step( 0,'%ecx','%edx','%eax','%ebx', '8','0xf4d50d87','14');
180 round2_step( 0,'%ebx','%ecx','%edx','%eax','13','0x455a14ed','20');
181 round2_step( 0,'%eax','%ebx','%ecx','%edx', '2','0xa9e3e905', '5');
182 round2_step( 0,'%edx','%eax','%ebx','%ecx', '7','0xfcefa3f8', '9');
183 round2_step( 0,'%ecx','%edx','%eax','%ebx','12','0x676f02d9','14');
184 round2_step( 1,'%ebx','%ecx','%edx','%eax', '0','0x8d2a4c8a','20');
185
186 round3_step(-1,'%eax','%ebx','%ecx','%edx', '8','0xfffa3942', '4');
187 round3_step( 0,'%edx','%eax','%ebx','%ecx','11','0x8771f681','11');
188 round3_step( 0,'%ecx','%edx','%eax','%ebx','14','0x6d9d6122','16');
189 round3_step( 0,'%ebx','%ecx','%edx','%eax', '1','0xfde5380c','23');
190 round3_step( 0,'%eax','%ebx','%ecx','%edx', '4','0xa4beea44', '4');
191 round3_step( 0,'%edx','%eax','%ebx','%ecx', '7','0x4bdecfa9','11');
192 round3_step( 0,'%ecx','%edx','%eax','%ebx','10','0xf6bb4b60','16');
193 round3_step( 0,'%ebx','%ecx','%edx','%eax','13','0xbebfbc70','23');
194 round3_step( 0,'%eax','%ebx','%ecx','%edx', '0','0x289b7ec6', '4');
195 round3_step( 0,'%edx','%eax','%ebx','%ecx', '3','0xeaa127fa','11');
196 round3_step( 0,'%ecx','%edx','%eax','%ebx', '6','0xd4ef3085','16');
197 round3_step( 0,'%ebx','%ecx','%edx','%eax', '9', '0x4881d05','23');
198 round3_step( 0,'%eax','%ebx','%ecx','%edx','12','0xd9d4d039', '4');
199 round3_step( 0,'%edx','%eax','%ebx','%ecx','15','0xe6db99e5','11');
200 round3_step( 0,'%ecx','%edx','%eax','%ebx', '2','0x1fa27cf8','16');
201 round3_step( 1,'%ebx','%ecx','%edx','%eax', '0','0xc4ac5665','23');
202
203 round4_step(-1,'%eax','%ebx','%ecx','%edx', '7','0xf4292244', '6');
204 round4_step( 0,'%edx','%eax','%ebx','%ecx','14','0x432aff97','10');
205 round4_step( 0,'%ecx','%edx','%eax','%ebx', '5','0xab9423a7','15');
206 round4_step( 0,'%ebx','%ecx','%edx','%eax','12','0xfc93a039','21');
207 round4_step( 0,'%eax','%ebx','%ecx','%edx', '3','0x655b59c3', '6');
208 round4_step( 0,'%edx','%eax','%ebx','%ecx','10','0x8f0ccc92','10');
209 round4_step( 0,'%ecx','%edx','%eax','%ebx', '1','0xffeff47d','15');
210 round4_step( 0,'%ebx','%ecx','%edx','%eax', '8','0x85845dd1','21');
211 round4_step( 0,'%eax','%ebx','%ecx','%edx','15','0x6fa87e4f', '6');
212 round4_step( 0,'%edx','%eax','%ebx','%ecx', '6','0xfe2ce6e0','10');
213 round4_step( 0,'%ecx','%edx','%eax','%ebx','13','0xa3014314','15');
214 round4_step( 0,'%ebx','%ecx','%edx','%eax', '4','0x4e0811a1','21');
215 round4_step( 0,'%eax','%ebx','%ecx','%edx','11','0xf7537e82', '6');
216 round4_step( 0,'%edx','%eax','%ebx','%ecx', '2','0xbd3af235','10');
217 round4_step( 0,'%ecx','%edx','%eax','%ebx', '9','0x2ad7d2bb','15');
218 round4_step( 1,'%ebx','%ecx','%edx','%eax', '0','0xeb86d391','21');
219 $code .= <<EOF;
220         # add old values of A, B, C, D
221         add     %r8d,   %eax
222         add     %r9d,   %ebx
223         add     %r14d,  %ecx
224         add     %r15d,  %edx
225
226         # loop control
227         add     \$64,           %rsi            # ptr += 64
228         cmp     %rdi,           %rsi            # cmp end with ptr
229         jb      2b                              # jmp if ptr < end
230         # END of loop over 16-word blocks
231
232 1:      pop     %rbp                            # restore ctx
233         mov     %eax,           0*4(%rbp)       # ctx->A = A
234         mov     %ebx,           1*4(%rbp)       # ctx->B = B
235         mov     %ecx,           2*4(%rbp)       # ctx->C = C
236         mov     %edx,           3*4(%rbp)       # ctx->D = D
237
238         pop     %r15
239         pop     %r14
240         pop     %r13
241         pop     %r12
242         pop     %rbx
243         pop     %rbp
244         ret
245 .L_md5_block_asm_host_order_end:
246 .size md5_block_asm_host_order,.L_md5_block_asm_host_order_end-md5_block_asm_host_order
247 EOF
248
249 print $code;