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 '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...
23 $win64a=1 if ($output =~ /win64a.[s|asm]/);
25 open STDOUT,">$output" || die "can't open $output: $!";
27 if (defined($win64a)) {
30 $inp="%rsi"; # r8, arg3 moves here
31 $out="%rdi"; # r9, arg4 moves here
46 if (defined($win64a)) {
47 $ret =~ s/\[([\S]+)\+([\S]+)\]/[$2+$1]/g; # [%rN+%rM*4]->[%rM*4+%rN]
48 $ret =~ s/:([^\[]+)\[([^\]]+)\]/:[$2+$1]/g; # :off[ea]->:[ea+off]
50 $ret =~ s/[\+\*]/,/g; # [%rN+%rM*4]->[%rN,%rM,4]
51 $ret =~ s/\[([^\]]+)\]/($1)/g; # [%rN]->(%rN)
56 $code=<<___ if (!defined($win64a));
67 $code=<<___ if (defined($win64a));
84 movl `&PTR("DWORD:-8[$dat]")`,$XX#d
85 movl `&PTR("DWORD:-4[$dat]")`,$YY#d
90 movq `&PTR("QWORD:[$inp]")`,%rax
93 movl `&PTR("DWORD:[$dat+$XX*4]")`,$TX#d
95 movl `&PTR("DWORD:[$dat+$YY*4]")`,$TY#d
96 movl $TX#d,`&PTR("DWORD:[$dat+$YY*4]")`
97 movl $TY#d,`&PTR("DWORD:[$dat+$XX*4]")`
100 movl `&PTR("DWORD:[$dat+$TX*4]")`,$TY#d
103 for ($i=1;$i<=6;$i++) {
105 movl `&PTR("DWORD:[$dat+$XX*4]")`,$TX#d
107 movl `&PTR("DWORD:[$dat+$YY*4]")`,$TY#d
108 movl $TX#d,`&PTR("DWORD:[$dat+$YY*4]")`
109 movl $TY#d,`&PTR("DWORD:[$dat+$XX*4]")`
111 movl `&PTR("DWORD:[$dat+$TX*4]")`,$TY#d
118 movl `&PTR("DWORD:[$dat+$XX*4]")`,$TX#d
120 movl `&PTR("DWORD:[$dat+$YY*4]")`,$TY#d
121 movl $TX#d,`&PTR("DWORD:[$dat+$YY*4]")`
122 movl $TY#d,`&PTR("DWORD:[$dat+$XX*4]")`
126 movl `&PTR("DWORD:[$dat+$TX*4]")`,$TY#d
131 mov %rax,`&PTR("QWORD:-8[$out]")`
138 movl $XX#d,`&PTR("DWORD:-8[$dat]")`
139 movl $YY#d,`&PTR("DWORD:-4[$dat]")`
141 $code.=<<___ if (defined($win64a));
150 movzb `&PTR("BYTE:[$inp]")`,%eax
152 movl `&PTR("DWORD:[$dat+$XX*4]")`,$TX#d
154 movl `&PTR("DWORD:[$dat+$YY*4]")`,$TY#d
155 movl $TX#d,`&PTR("DWORD:[$dat+$YY*4]")`
156 movl $TY#d,`&PTR("DWORD:[$dat+$XX*4]")`
158 movl `&PTR("DWORD:[$dat+$TX*4]")`,$TY#d
161 movb %al,`&PTR("BYTE:[$out]")`
167 $code.=<<___ if (defined($win64a));
172 $code.=<<___ if (!defined($win64a));
176 $code =~ s/#([bwd])/$1/gm;
177 $code =~ s/\`([^\`]*)\`/eval $1/gem;
179 if (defined($win64a)) {
180 $code =~ s/\.align/ALIGN/gm;
181 $code =~ s/[\$%]//gm;
182 $code =~ s/\.L/\$L/gm;
183 $code =~ s/([\w]+)([\s]+)([\S]+),([\S]+)/$1$2$4,$3/gm;
184 $code =~ s/([QD]*WORD|BYTE):/$1 PTR/gm;
185 $code =~ s/mov[bwlq]/mov/gm;
186 $code =~ s/movzb/movzx/gm;
187 $code =~ s/repret/DB\t0F3h,0C3h/gm;
189 $code =~ s/([QD]*WORD|BYTE)://gm;
190 $code =~ s/repret/.byte\t0xF3,0xC3/gm;