Import OpenSSL-0.9.8i.
[dragonfly.git] / crypto / openssl-0.9.7e / crypto / bn / asm / co-586.pl
blob5d962cb957d34447b3e7cfc6de632648154f11ed
1 #!/usr/local/bin/perl
3 push(@INC,"perlasm","../../perlasm");
4 require "x86asm.pl";
6 &asm_init($ARGV[0],$0);
8 &bn_mul_comba("bn_mul_comba8",8);
9 &bn_mul_comba("bn_mul_comba4",4);
10 &bn_sqr_comba("bn_sqr_comba8",8);
11 &bn_sqr_comba("bn_sqr_comba4",4);
13 &asm_finish();
15 sub mul_add_c
17 local($a,$ai,$b,$bi,$c0,$c1,$c2,$pos,$i,$na,$nb)=@_;
19 # pos == -1 if eax and edx are pre-loaded, 0 to load from next
20 # words, and 1 if load return value
22 &comment("mul a[$ai]*b[$bi]");
24 # "eax" and "edx" will always be pre-loaded.
25 # &mov("eax",&DWP($ai*4,$a,"",0)) ;
26 # &mov("edx",&DWP($bi*4,$b,"",0));
28 &mul("edx");
29 &add($c0,"eax");
30 &mov("eax",&DWP(($na)*4,$a,"",0)) if $pos == 0; # laod next a
31 &mov("eax",&wparam(0)) if $pos > 0; # load r[]
32 ###
33 &adc($c1,"edx");
34 &mov("edx",&DWP(($nb)*4,$b,"",0)) if $pos == 0; # laod next b
35 &mov("edx",&DWP(($nb)*4,$b,"",0)) if $pos == 1; # laod next b
36 ###
37 &adc($c2,0);
38 # is pos > 1, it means it is the last loop
39 &mov(&DWP($i*4,"eax","",0),$c0) if $pos > 0; # save r[];
40 &mov("eax",&DWP(($na)*4,$a,"",0)) if $pos == 1; # laod next a
43 sub sqr_add_c
45 local($r,$a,$ai,$bi,$c0,$c1,$c2,$pos,$i,$na,$nb)=@_;
47 # pos == -1 if eax and edx are pre-loaded, 0 to load from next
48 # words, and 1 if load return value
50 &comment("sqr a[$ai]*a[$bi]");
52 # "eax" and "edx" will always be pre-loaded.
53 # &mov("eax",&DWP($ai*4,$a,"",0)) ;
54 # &mov("edx",&DWP($bi*4,$b,"",0));
56 if ($ai == $bi)
57 { &mul("eax");}
58 else
59 { &mul("edx");}
60 &add($c0,"eax");
61 &mov("eax",&DWP(($na)*4,$a,"",0)) if $pos == 0; # load next a
62 ###
63 &adc($c1,"edx");
64 &mov("edx",&DWP(($nb)*4,$a,"",0)) if ($pos == 1) && ($na != $nb);
65 ###
66 &adc($c2,0);
67 # is pos > 1, it means it is the last loop
68 &mov(&DWP($i*4,$r,"",0),$c0) if $pos > 0; # save r[];
69 &mov("eax",&DWP(($na)*4,$a,"",0)) if $pos == 1; # load next b
72 sub sqr_add_c2
74 local($r,$a,$ai,$bi,$c0,$c1,$c2,$pos,$i,$na,$nb)=@_;
76 # pos == -1 if eax and edx are pre-loaded, 0 to load from next
77 # words, and 1 if load return value
79 &comment("sqr a[$ai]*a[$bi]");
81 # "eax" and "edx" will always be pre-loaded.
82 # &mov("eax",&DWP($ai*4,$a,"",0)) ;
83 # &mov("edx",&DWP($bi*4,$a,"",0));
85 if ($ai == $bi)
86 { &mul("eax");}
87 else
88 { &mul("edx");}
89 &add("eax","eax");
90 ###
91 &adc("edx","edx");
92 ###
93 &adc($c2,0);
94 &add($c0,"eax");
95 &adc($c1,"edx");
96 &mov("eax",&DWP(($na)*4,$a,"",0)) if $pos == 0; # load next a
97 &mov("eax",&DWP(($na)*4,$a,"",0)) if $pos == 1; # load next b
98 &adc($c2,0);
99 &mov(&DWP($i*4,$r,"",0),$c0) if $pos > 0; # save r[];
100 &mov("edx",&DWP(($nb)*4,$a,"",0)) if ($pos <= 1) && ($na != $nb);
104 sub bn_mul_comba
106 local($name,$num)=@_;
107 local($a,$b,$c0,$c1,$c2);
108 local($i,$as,$ae,$bs,$be,$ai,$bi);
109 local($tot,$end);
111 &function_begin_B($name,"");
113 $c0="ebx";
114 $c1="ecx";
115 $c2="ebp";
116 $a="esi";
117 $b="edi";
119 $as=0;
120 $ae=0;
121 $bs=0;
122 $be=0;
123 $tot=$num+$num-1;
125 &push("esi");
126 &mov($a,&wparam(1));
127 &push("edi");
128 &mov($b,&wparam(2));
129 &push("ebp");
130 &push("ebx");
132 &xor($c0,$c0);
133 &mov("eax",&DWP(0,$a,"",0)); # load the first word
134 &xor($c1,$c1);
135 &mov("edx",&DWP(0,$b,"",0)); # load the first second
137 for ($i=0; $i<$tot; $i++)
139 $ai=$as;
140 $bi=$bs;
141 $end=$be+1;
143 &comment("################## Calculate word $i");
145 for ($j=$bs; $j<$end; $j++)
147 &xor($c2,$c2) if ($j == $bs);
148 if (($j+1) == $end)
150 $v=1;
151 $v=2 if (($i+1) == $tot);
153 else
154 { $v=0; }
155 if (($j+1) != $end)
157 $na=($ai-1);
158 $nb=($bi+1);
160 else
162 $na=$as+($i < ($num-1));
163 $nb=$bs+($i >= ($num-1));
165 #printf STDERR "[$ai,$bi] -> [$na,$nb]\n";
166 &mul_add_c($a,$ai,$b,$bi,$c0,$c1,$c2,$v,$i,$na,$nb);
167 if ($v)
169 &comment("saved r[$i]");
170 # &mov("eax",&wparam(0));
171 # &mov(&DWP($i*4,"eax","",0),$c0);
172 ($c0,$c1,$c2)=($c1,$c2,$c0);
174 $ai--;
175 $bi++;
177 $as++ if ($i < ($num-1));
178 $ae++ if ($i >= ($num-1));
180 $bs++ if ($i >= ($num-1));
181 $be++ if ($i < ($num-1));
183 &comment("save r[$i]");
184 # &mov("eax",&wparam(0));
185 &mov(&DWP($i*4,"eax","",0),$c0);
187 &pop("ebx");
188 &pop("ebp");
189 &pop("edi");
190 &pop("esi");
191 &ret();
192 &function_end_B($name);
195 sub bn_sqr_comba
197 local($name,$num)=@_;
198 local($r,$a,$c0,$c1,$c2)=@_;
199 local($i,$as,$ae,$bs,$be,$ai,$bi);
200 local($b,$tot,$end,$half);
202 &function_begin_B($name,"");
204 $c0="ebx";
205 $c1="ecx";
206 $c2="ebp";
207 $a="esi";
208 $r="edi";
210 &push("esi");
211 &push("edi");
212 &push("ebp");
213 &push("ebx");
214 &mov($r,&wparam(0));
215 &mov($a,&wparam(1));
216 &xor($c0,$c0);
217 &xor($c1,$c1);
218 &mov("eax",&DWP(0,$a,"",0)); # load the first word
220 $as=0;
221 $ae=0;
222 $bs=0;
223 $be=0;
224 $tot=$num+$num-1;
226 for ($i=0; $i<$tot; $i++)
228 $ai=$as;
229 $bi=$bs;
230 $end=$be+1;
232 &comment("############### Calculate word $i");
233 for ($j=$bs; $j<$end; $j++)
235 &xor($c2,$c2) if ($j == $bs);
236 if (($ai-1) < ($bi+1))
238 $v=1;
239 $v=2 if ($i+1) == $tot;
241 else
242 { $v=0; }
243 if (!$v)
245 $na=$ai-1;
246 $nb=$bi+1;
248 else
250 $na=$as+($i < ($num-1));
251 $nb=$bs+($i >= ($num-1));
253 if ($ai == $bi)
255 &sqr_add_c($r,$a,$ai,$bi,
256 $c0,$c1,$c2,$v,$i,$na,$nb);
258 else
260 &sqr_add_c2($r,$a,$ai,$bi,
261 $c0,$c1,$c2,$v,$i,$na,$nb);
263 if ($v)
265 &comment("saved r[$i]");
266 #&mov(&DWP($i*4,$r,"",0),$c0);
267 ($c0,$c1,$c2)=($c1,$c2,$c0);
268 last;
270 $ai--;
271 $bi++;
273 $as++ if ($i < ($num-1));
274 $ae++ if ($i >= ($num-1));
276 $bs++ if ($i >= ($num-1));
277 $be++ if ($i < ($num-1));
279 &mov(&DWP($i*4,$r,"",0),$c0);
280 &pop("ebx");
281 &pop("ebp");
282 &pop("edi");
283 &pop("esi");
284 &ret();
285 &function_end_B($name);