Final touches to rc4/asm/rc4-596.pl, +52% better performance on AMD core.
[openssl.git] / crypto / rc4 / asm / rc4-586.pl
1 #!/usr/local/bin/perl
2
3 # At some point it became apparent that the original SSLeay RC4
4 # assembler implementation performs suboptimal on latest IA-32
5 # microarchitectures. After re-tuning performance has changed as
6 # following:
7 #
8 # Pentium       +0%
9 # Pentium III   +17%
10 # AMD           +52%(*)
11 # P4            +180%(**)
12 #
13 # (*)   This number is actually a trade-off:-) It's possible to
14 #       achieve +72%, but at the cost of -48% off PIII performance.
15 #       In other words code performing further 13% faster on AMD
16 #       would perform almost 2 times slower on Intel PIII...
17 #       For reference! This code delivers ~80% of rc4-amd64.pl
18 #       performance on same Opteron machine.
19 # (**)  This number requires compressed key schedule set up by
20 #       RC4_set_key, see commentary section in rc4_skey.c for
21 #       further details.
22 #                                       <appro@fy.chalmers.se>
23
24 push(@INC,"perlasm","../../perlasm");
25 require "x86asm.pl";
26
27 &asm_init($ARGV[0],"rc4-586.pl");
28
29 $x="eax";
30 $y="ebx";
31 $tx="ecx";
32 $ty="edx";
33 $in="esi";
34 $out="edi";
35 $d="ebp";
36
37 &RC4("RC4");
38
39 &asm_finish();
40
41 sub RC4_loop
42         {
43         local($n,$p,$char)=@_;
44
45         &comment("Round $n");
46
47         if ($char)
48                 {
49                 if ($p >= 0)
50                         {
51                          &mov($ty,      &swtmp(2));
52                         &cmp($ty,       $in);
53                          &jbe(&label("finished"));
54                         &inc($in);
55                         }
56                 else
57                         {
58                         &add($ty,       8);
59                          &inc($in);
60                         &cmp($ty,       $in);
61                          &jb(&label("finished"));
62                         &mov(&swtmp(2), $ty);
63                         }
64                 }
65         # Moved out
66         # &mov( $tx,            &DWP(0,$d,$x,4)) if $p < 0;
67
68         &add(   &LB($y),        &LB($tx));
69          &inc(  &LB($x));                       # NEXT ROUND
70         &mov(   $ty,            &DWP(0,$d,$y,4));
71          # XXX
72         &mov(   &DWP(-4,$d,$x,4),$ty);                  # AGI
73          &add(  $ty,            $tx);
74         &mov(   &DWP(0,$d,$y,4),$tx);
75          &and(  $ty,            0xff);
76         &mov(   $tx,            &DWP(0,$d,$x,4)) if $p < 1; # NEXT ROUND
77          &mov(  $ty,            &DWP(0,$d,$ty,4));
78
79         if (!$char)
80                 {
81                 #moved up into last round
82                 if ($p >= 1)
83                         {
84                         &add(   $out,   8)
85                         }
86                 &movb(  &BP($n,"esp","",0),     &LB($ty));
87                 }
88         else
89                 {
90                 # Note in+=8 has occured
91                 &movb(  &HB($ty),       &BP(-1,$in,"",0));
92                  # XXX
93                 &xorb(&LB($ty),         &HB($ty));
94                  # XXX
95                 &movb(&BP($n,$out,"",0),&LB($ty));
96                 }
97         }
98
99
100 sub RC4
101         {
102         local($name)=@_;
103
104         &function_begin_B($name,"");
105
106         &mov($ty,&wparam(1));           # len
107         &cmp($ty,0);
108         &jne(&label("proceed"));
109         &ret();
110         &set_label("proceed");
111
112         &comment("");
113
114         &push("ebp");
115          &push("ebx");
116         &push("esi");
117          &xor(  $x,     $x);            # avoid partial register stalls
118         &push("edi");
119          &xor(  $y,     $y);            # avoid partial register stalls
120         &mov(   $d,     &wparam(0));    # key
121          &mov(  $in,    &wparam(2));
122
123         &movb(  &LB($x),        &BP(0,$d,"",1));
124          &movb( &LB($y),        &BP(4,$d,"",1));
125
126         &mov(   $out,   &wparam(3));
127          &inc(  &LB($x));
128
129         &stack_push(3); # 3 temp variables
130          &add(  $d,     8);
131
132         # detect compressed schedule, see commentary section in rc4_skey.c...
133         &cmp(&DWP(256,$d),-1);
134         &je(&label("RC4_CHAR"));
135
136          &lea(  $ty,    &DWP(-8,$ty,$in));
137
138         # check for 0 length input
139
140          &mov(  &swtmp(2),      $ty);   # this is now address to exit at
141         &mov(   $tx,    &DWP(0,$d,$x,4));
142
143          &cmp(  $ty,    $in);
144         &jb(    &label("end")); # less than 8 bytes
145
146         &set_label("start");
147
148         # filling DELAY SLOT
149         &add(   $in,    8);
150
151         &RC4_loop(0,-1,0);
152         &RC4_loop(1,0,0);
153         &RC4_loop(2,0,0);
154         &RC4_loop(3,0,0);
155         &RC4_loop(4,0,0);
156         &RC4_loop(5,0,0);
157         &RC4_loop(6,0,0);
158         &RC4_loop(7,1,0);
159         
160         &comment("apply the cipher text");
161         # xor the cipher data with input
162
163         #&add(  $out,   8); #moved up into last round
164
165         &mov(   $tx,    &swtmp(0));
166          &mov(  $ty,    &DWP(-8,$in,"",0));
167         &xor(   $tx,    $ty);
168          &mov(  $ty,    &DWP(-4,$in,"",0)); 
169         &mov(   &DWP(-8,$out,"",0),     $tx);
170          &mov(  $tx,    &swtmp(1));
171         &xor(   $tx,    $ty);
172          &mov(  $ty,    &swtmp(2));     # load end ptr;
173         &mov(   &DWP(-4,$out,"",0),     $tx);
174          &mov(  $tx,            &DWP(0,$d,$x,4));
175         &cmp($in,       $ty);
176          &jbe(&label("start"));
177
178         &set_label("end");
179
180         # There is quite a bit of extra crap in RC4_loop() for this
181         # first round
182         &RC4_loop(0,-1,1);
183         &RC4_loop(1,0,1);
184         &RC4_loop(2,0,1);
185         &RC4_loop(3,0,1);
186         &RC4_loop(4,0,1);
187         &RC4_loop(5,0,1);
188         &RC4_loop(6,1,1);
189
190         &jmp(&label("finished"));
191
192         &align(16);
193         # this is essentially Intel P4 specific codepath, see rc4_skey.c...
194         &set_label("RC4_CHAR");
195
196         &lea    ($ty,&DWP(0,$in,$ty));
197         &mov    (&swtmp(2),$ty);
198
199         # strangely enough unrolled loop performs over 20% slower...
200         &set_label("RC4_CHAR_loop");
201                 &movz   ($tx,&BP(0,$d,$x));
202                 &add    (&LB($y),&LB($tx));
203                 &movz   ($ty,&BP(0,$d,$y));
204                 &movb   (&BP(0,$d,$y),&LB($tx));
205                 &movb   (&BP(0,$d,$x),&LB($ty));
206                 &add    (&LB($ty),&LB($tx));
207                 &movz   ($ty,&BP(0,$d,$ty));
208                 &xorb   (&LB($ty),&BP(0,$in));
209                 &movb   (&BP(0,$out),&LB($ty));
210                 &inc    (&LB($x));
211                 &inc    ($in);
212                 &inc    ($out);
213                 &cmp    ($in,&swtmp(2));
214         &jb     (&label("RC4_CHAR_loop"));
215
216         &set_label("finished");
217         &dec(   $x);
218          &stack_pop(3);
219         &movb(  &BP(-4,$d,"",0),&LB($y));
220          &movb( &BP(-8,$d,"",0),&LB($x));
221
222         &function_end($name);
223         }
224