Import of old SSLeay release: SSLeay 0.9.1b (unreleased)
[openssl.git] / crypto / bn / comba.pl
1 #!/usr/local/bin/perl
2
3 $num=8;
4 $num2=8/2;
5
6 print <<"EOF";
7 /* crypto/bn/bn_comba.c */
8 #include <stdio.h>
9 #include "bn_lcl.h"
10 /* Auto generated from crypto/bn/comba.pl
11  */
12
13 #undef bn_mul_comba8
14 #undef bn_mul_comba4
15 #undef bn_sqr_comba8
16 #undef bn_sqr_comba4
17
18 #ifdef BN_LLONG
19 #define mul_add_c(a,b,c0,c1,c2) \\
20         t=(BN_ULLONG)a*b; \\
21         t1=(BN_ULONG)Lw(t); \\
22         t2=(BN_ULONG)Hw(t); \\
23         c0=(c0+t1)&BN_MASK2; if ((c0) < t1) t2++; \\
24         c1=(c1+t2)&BN_MASK2; if ((c1) < t2) c2++;
25
26 #define mul_add_c2(a,b,c0,c1,c2) \\
27         t=(BN_ULLONG)a*b; \\
28         tt=(t+t)&BN_MASK; \\
29         if (tt < t) c2++; \\
30         t1=(BN_ULONG)Lw(tt); \\
31         t2=(BN_ULONG)Hw(tt); \\
32         c0=(c0+t1)&BN_MASK2;  \\
33         if ((c0 < t1) && (((++t2)&BN_MASK2) == 0)) c2++; \\
34         c1=(c1+t2)&BN_MASK2; if ((c1) < t2) c2++;
35
36 #define sqr_add_c(a,i,c0,c1,c2) \\
37         t=(BN_ULLONG)a[i]*a[i]; \\
38         t1=(BN_ULONG)Lw(t); \\
39         t2=(BN_ULONG)Hw(t); \\
40         c0=(c0+t1)&BN_MASK2; if ((c0) < t1) t2++; \\
41         c1=(c1+t2)&BN_MASK2; if ((c1) < t2) c2++;
42
43 #define sqr_add_c2(a,i,j,c0,c1,c2) \\
44         mul_add_c2((a)[i],(a)[j],c0,c1,c2)
45 #else
46 #define mul_add_c(a,b,c0,c1,c2) \\
47         t1=LBITS(a); t2=HBITS(a); \\
48         bl=LBITS(b); bh=HBITS(b); \\
49         mul64(t1,t2,bl,bh); \\
50         c0=(c0+t1)&BN_MASK2; if ((c0) < t1) t2++; \\
51         c1=(c1+t2)&BN_MASK2; if ((c1) < t2) c2++;
52
53 #define mul_add_c2(a,b,c0,c1,c2) \\
54         t1=LBITS(a); t2=HBITS(a); \\
55         bl=LBITS(b); bh=HBITS(b); \\
56         mul64(t1,t2,bl,bh); \\
57         if (t2 & BN_TBIT) c2++; \\
58         t2=(t2+t2)&BN_MASK2; \\
59         if (t1 & BN_TBIT) t2++; \\
60         t1=(t1+t1)&BN_MASK2; \\
61         c0=(c0+t1)&BN_MASK2;  \\
62         if ((c0 < t1) && (((++t2)&BN_MASK2) == 0)) c2++; \\
63         c1=(c1+t2)&BN_MASK2; if ((c1) < t2) c2++;
64
65 #define sqr_add_c(a,i,c0,c1,c2) \\
66         sqr64(t1,t2,(a)[i]); \\
67         c0=(c0+t1)&BN_MASK2; if ((c0) < t1) t2++; \\
68         c1=(c1+t2)&BN_MASK2; if ((c1) < t2) c2++;
69
70 #define sqr_add_c2(a,i,j,c0,c1,c2) \\
71         mul_add_c2((a)[i],(a)[j],c0,c1,c2)
72 #endif
73
74 void bn_mul_comba${num}(r,a,b)
75 BN_ULONG *r,*a,*b;
76         {
77 #ifdef BN_LLONG
78         BN_ULLONG t;
79 #else
80         BN_ULONG bl,bh;
81 #endif
82         BN_ULONG t1,t2;
83         BN_ULONG c1,c2,c3;
84
85 EOF
86 $ret=&combas_mul("r","a","b",$num,"c1","c2","c3");
87 printf <<"EOF";
88         }
89
90 void bn_mul_comba${num2}(r,a,b)
91 BN_ULONG *r,*a,*b;
92         {
93 #ifdef BN_LLONG
94         BN_ULLONG t;
95 #else
96         BN_ULONG bl,bh;
97 #endif
98         BN_ULONG t1,t2;
99         BN_ULONG c1,c2,c3;
100
101 EOF
102 $ret=&combas_mul("r","a","b",$num2,"c1","c2","c3");
103 printf <<"EOF";
104         }
105
106 void bn_sqr_comba${num}(r,a)
107 BN_ULONG *r,*a;
108         {
109 #ifdef BN_LLONG
110         BN_ULLONG t,tt;
111 #else
112         BN_ULONG bl,bh;
113 #endif
114         BN_ULONG t1,t2;
115         BN_ULONG c1,c2,c3;
116
117 EOF
118 $ret=&combas_sqr("r","a",$num,"c1","c2","c3");
119 printf <<"EOF";
120         }
121
122 void bn_sqr_comba${num2}(r,a)
123 BN_ULONG *r,*a;
124         {
125 #ifdef BN_LLONG
126         BN_ULLONG t,tt;
127 #else
128         BN_ULONG bl,bh;
129 #endif
130         BN_ULONG t1,t2;
131         BN_ULONG c1,c2,c3;
132
133 EOF
134 $ret=&combas_sqr("r","a",$num2,"c1","c2","c3");
135 printf <<"EOF";
136         }
137 EOF
138
139 sub bn_str
140         {
141         local($var,$val)=@_;
142         print "\t$var=$val;\n";
143         }
144
145 sub bn_ary
146         {
147         local($var,$idx)=@_;
148         return("${var}[$idx]");
149         }
150
151 sub bn_clr
152         {
153         local($var)=@_;
154
155         print "\t$var=0;\n";
156         }
157
158 sub bn_mad
159         {
160         local($a,$b,$c0,$c1,$c2,$num)=@_;
161
162         if ($num == 2)
163                 { printf("\tmul_add_c2($a,$b,$c0,$c1,$c2);\n"); }
164         else
165                 { printf("\tmul_add_c($a,$b,$c0,$c1,$c2);\n"); }
166         }
167
168 sub bn_sad
169         {
170         local($a,$i,$j,$c0,$c1,$c2,$num)=@_;
171
172         if ($num == 2)
173                 { printf("\tsqr_add_c2($a,$i,$j,$c0,$c1,$c2);\n"); }
174         else
175                 { printf("\tsqr_add_c($a,$i,$c0,$c1,$c2);\n"); }
176         }
177
178 sub combas_mul
179         {
180         local($r,$a,$b,$num,$c0,$c1,$c2)=@_;
181         local($i,$as,$ae,$bs,$be,$ai,$bi);
182         local($tot,$end);
183
184         $as=0;
185         $ae=0;
186         $bs=0;
187         $be=0;
188         $tot=$num+$num-1;
189         &bn_clr($c0);
190         &bn_clr($c1);
191         for ($i=0; $i<$tot; $i++)
192                 {
193                 $ai=$as;
194                 $bi=$bs;
195                 $end=$be+1;
196                 @numa=@numb=();
197
198 #print "($as $ae) ($bs $be) $bs -> $end [$i $num]\n";
199                 for ($j=$bs; $j<$end; $j++)
200                         {
201                         push(@numa,$ai);
202                         push(@numb,$bi);
203                         $ai--;
204                         $bi++;
205                         }
206
207                 if ($i & 1)
208                         {
209                         @numa=reverse(@numa);
210                         @numb=reverse(@numb);
211                         }
212
213                 &bn_clr($c2);
214                 for ($j=0; $j<=$#numa; $j++)
215                         {
216                         &bn_mad(&bn_ary($a,$numa[$j]),
217                                 &bn_ary($b,$numb[$j]),$c0,$c1,$c2,1);
218                         }
219                 &bn_str(&bn_ary($r,$i),$c0);
220                 ($c0,$c1,$c2)=($c1,$c2,$c0);
221
222                 $as++ if ($i < ($num-1));
223                 $ae++ if ($i >= ($num-1));
224
225                 $bs++ if ($i >= ($num-1));
226                 $be++ if ($i < ($num-1));
227                 }
228         &bn_str(&bn_ary($r,$i),$c0);
229         }
230
231 sub combas_sqr
232         {
233         local($r,$a,$num,$c0,$c1,$c2)=@_;
234         local($i,$as,$ae,$bs,$be,$ai,$bi);
235         local($b,$tot,$end,$half);
236
237         $b=$a;
238         $as=0;
239         $ae=0;
240         $bs=0;
241         $be=0;
242         $tot=$num+$num-1;
243         &bn_clr($c0);
244         &bn_clr($c1);
245         for ($i=0; $i<$tot; $i++)
246                 {
247                 $ai=$as;
248                 $bi=$bs;
249                 $end=$be+1;
250                 @numa=@numb=();
251
252 #print "($as $ae) ($bs $be) $bs -> $end [$i $num]\n";
253                 for ($j=$bs; $j<$end; $j++)
254                         {
255                         push(@numa,$ai);
256                         push(@numb,$bi);
257                         $ai--;
258                         $bi++;
259                         last if ($ai < $bi);
260                         }
261                 if (!($i & 1))
262                         {
263                         @numa=reverse(@numa);
264                         @numb=reverse(@numb);
265                         }
266
267                 &bn_clr($c2);
268                 for ($j=0; $j <= $#numa; $j++)
269                         {
270                         if ($numa[$j] == $numb[$j])
271                                 {&bn_sad($a,$numa[$j],$numb[$j],$c0,$c1,$c2,1);}
272                         else
273                                 {&bn_sad($a,$numa[$j],$numb[$j],$c0,$c1,$c2,2);}
274                         }
275                 &bn_str(&bn_ary($r,$i),$c0);
276                 ($c0,$c1,$c2)=($c1,$c2,$c0);
277
278                 $as++ if ($i < ($num-1));
279                 $ae++ if ($i >= ($num-1));
280
281                 $bs++ if ($i >= ($num-1));
282                 $be++ if ($i < ($num-1));
283                 }
284         &bn_str(&bn_ary($r,$i),$c0);
285         }