3 # ====================================================================
4 # Written by Andy Polyakov <appro@fy.chalmers.se> for the OpenSSL
5 # project. Rights for redistribution and usage in source and binary
6 # forms are granted according to the OpenSSL license.
7 # ====================================================================
9 # 2.22x RC4 tune-up:-) It should be noted though that my hand [as in
10 # "hand-coded assembler"] doesn't stand for the whole improvement
11 # coefficient. It turned out that eliminating RC4_CHAR from config
12 # line results in ~40% improvement (yes, even for C implementation).
13 # Presumably it has everything to do with AMD cache architecture and
14 # RAW or whatever penalties. Once again! The module *requires* config
15 # line *without* RC4_CHAR! As for coding "secret," I bet on partial
16 # register arithmetics. For example instead of 'inc %r8; and $255,%r8'
17 # I simply 'inc %r8b'. Even though optimization manual discourages
18 # to operate on partial registers, it turned out to be the best bet.
19 # At least for AMD... How IA32E would perform remains to be seen...
21 # As was shown by Marc Bevand reordering of couple of load operations
22 # results in even higher performance gain of 3.3x:-) At least on
23 # Opteron... For reference, 1x in this case is RC4_CHAR C-code
24 # compiled with gcc 3.3.2, which performs at ~54MBps per 1GHz clock.
25 # Latter means that if you want to *estimate* what to expect from
26 # *your* CPU, then multiply 54 by 3.3 and clock frequency in GHz.
28 # Intel P4 EM64T core was found to run the AMD64 code really slow...
29 # The only way to achieve comparable performance on P4 is to keep
30 # RC4_CHAR. Kind of ironic, huh? As it's apparently impossible to
31 # compose blended code, which would perform even within 30% marginal
32 # on either AMD and Intel platforms, I implement both cases. See
33 # rc4_skey.c for further details... This applies to 0.9.8 and later.
34 # In 0.9.7 context RC4_CHAR codepath is never engaged and ~70 bytes
35 # of code remain redundant.
39 $win64a=1 if ($output =~ /win64a.[s|asm]/);
41 open STDOUT,">$output" || die "can't open $output: $!";
43 if (defined($win64a)) {
46 $inp="%rsi"; # r8, arg3 moves here
47 $out="%rdi"; # r9, arg4 moves here
62 if (defined($win64a)) {
63 $ret =~ s/\[([\S]+)\+([\S]+)\]/[$2+$1]/g; # [%rN+%rM*4]->[%rM*4+%rN]
64 $ret =~ s/:([^\[]+)\[([^\]]+)\]/:[$2+$1]/g; # :off[ea]->:[ea+off]
66 $ret =~ s/[\+\*]/,/g; # [%rN+%rM*4]->[%rN,%rM,4]
67 $ret =~ s/\[([^\]]+)\]/($1)/g; # [%rN]->(%rN)
72 $code=<<___ if (!defined($win64a));
83 $code=<<___ if (defined($win64a));
100 movl `&PTR("DWORD:-8[$dat]")`,$XX#d
101 movl `&PTR("DWORD:-4[$dat]")`,$YY#d
102 cmpl \$-1,`&PTR("DWORD:256[$dat]")`
109 movl `&PTR("DWORD:[$dat+$XX*4]")`,$TX#d
111 movl `&PTR("DWORD:[$dat+$YY*4]")`,$TY#d
112 movl $TX#d,`&PTR("DWORD:[$dat+$YY*4]")`
113 movl $TY#d,`&PTR("DWORD:[$dat+$XX*4]")`
116 movl `&PTR("DWORD:[$dat+$XX*4]")`,$TX#d
117 movb `&PTR("BYTE:[$dat+$TY*4]")`,%al
119 for ($i=1;$i<=6;$i++) {
123 movl `&PTR("DWORD:[$dat+$YY*4]")`,$TY#d
124 movl $TX#d,`&PTR("DWORD:[$dat+$YY*4]")`
125 movl $TY#d,`&PTR("DWORD:[$dat+$XX*4]")`
128 movl `&PTR("DWORD:[$dat+$XX*4]")`,$TX#d
129 movb `&PTR("BYTE:[$dat+$TY*4]")`,%al
135 movl `&PTR("DWORD:[$dat+$YY*4]")`,$TY#d
136 movl $TX#d,`&PTR("DWORD:[$dat+$YY*4]")`
137 movl $TY#d,`&PTR("DWORD:[$dat+$XX*4]")`
140 movb `&PTR("BYTE:[$dat+$TX*4]")`,%al
145 xor `&PTR("QWORD:-8[$inp]")`,%rax
146 mov %rax,`&PTR("QWORD:-8[$out]")`
153 movl $XX#d,`&PTR("DWORD:-8[$dat]")`
154 movl $YY#d,`&PTR("DWORD:-4[$dat]")`
156 $code.=<<___ if (defined($win64a));
165 movzb `&PTR("BYTE:[$inp]")`,%eax
167 movl `&PTR("DWORD:[$dat+$XX*4]")`,$TX#d
169 movl `&PTR("DWORD:[$dat+$YY*4]")`,$TY#d
170 movl $TX#d,`&PTR("DWORD:[$dat+$YY*4]")`
171 movl $TY#d,`&PTR("DWORD:[$dat+$XX*4]")`
173 movl `&PTR("DWORD:[$dat+$TX*4]")`,$TY#d
176 movb %al,`&PTR("BYTE:[$out]")`
185 movzb `&PTR("BYTE:[$dat+$XX]")`,$TX#d
187 movzb `&PTR("BYTE:[$dat+$YY]")`,$TY#d
188 movb $TX#b,`&PTR("BYTE:[$dat+$YY]")`
189 movb $TY#b,`&PTR("BYTE:[$dat+$XX]")`
191 movzb `&PTR("BYTE:[$dat+$TY]")`,$TY#d
192 xorb `&PTR("BYTE:[$inp]")`,$TY#b
193 movb $TY#b,`&PTR("BYTE:[$out]")`
200 $code.=<<___ if (defined($win64a));
205 $code.=<<___ if (!defined($win64a));
209 $code =~ s/#([bwd])/$1/gm;
210 $code =~ s/\`([^\`]*)\`/eval $1/gem;
212 if (defined($win64a)) {
213 $code =~ s/\.align/ALIGN/gm;
214 $code =~ s/[\$%]//gm;
215 $code =~ s/\.L/\$L/gm;
216 $code =~ s/([\w]+)([\s]+)([\S]+),([\S]+)/$1$2$4,$3/gm;
217 $code =~ s/([QD]*WORD|BYTE):/$1 PTR/gm;
218 $code =~ s/mov[bwlq]/mov/gm;
219 $code =~ s/movzb/movzx/gm;
220 $code =~ s/repret/DB\t0F3h,0C3h/gm;
221 $code =~ s/cmpl/cmp/gm;
222 $code =~ s/xorb/xor/gm;
224 $code =~ s/([QD]*WORD|BYTE)://gm;
225 $code =~ s/repret/.byte\t0xF3,0xC3/gm;