Only free if it ain't NULL.
[openssl.git] / crypto / bn / bn_comba.c
1 /* crypto/bn/bn_comba.c */
2 #include <stdio.h>
3 #include "bn_lcl.h"
4 /* Auto generated from crypto/bn/comba.pl
5  */
6
7 #undef bn_mul_comba8
8 #undef bn_mul_comba4
9 #undef bn_sqr_comba8
10 #undef bn_sqr_comba4
11
12 #ifdef BN_LLONG
13 #define mul_add_c(a,b,c0,c1,c2) \
14         t=(BN_ULLONG)a*b; \
15         t1=(BN_ULONG)Lw(t); \
16         t2=(BN_ULONG)Hw(t); \
17         c0=(c0+t1)&BN_MASK2; if ((c0) < t1) t2++; \
18         c1=(c1+t2)&BN_MASK2; if ((c1) < t2) c2++;
19
20 #define mul_add_c2(a,b,c0,c1,c2) \
21         t=(BN_ULLONG)a*b; \
22         tt=(t+t)&BN_MASK; \
23         if (tt < t) c2++; \
24         t1=(BN_ULONG)Lw(tt); \
25         t2=(BN_ULONG)Hw(tt); \
26         c0=(c0+t1)&BN_MASK2;  \
27         if ((c0 < t1) && (((++t2)&BN_MASK2) == 0)) c2++; \
28         c1=(c1+t2)&BN_MASK2; if ((c1) < t2) c2++;
29
30 #define sqr_add_c(a,i,c0,c1,c2) \
31         t=(BN_ULLONG)a[i]*a[i]; \
32         t1=(BN_ULONG)Lw(t); \
33         t2=(BN_ULONG)Hw(t); \
34         c0=(c0+t1)&BN_MASK2; if ((c0) < t1) t2++; \
35         c1=(c1+t2)&BN_MASK2; if ((c1) < t2) c2++;
36
37 #define sqr_add_c2(a,i,j,c0,c1,c2) \
38         mul_add_c2((a)[i],(a)[j],c0,c1,c2)
39 #else
40 #define mul_add_c(a,b,c0,c1,c2) \
41         t1=LBITS(a); t2=HBITS(a); \
42         bl=LBITS(b); bh=HBITS(b); \
43         mul64(t1,t2,bl,bh); \
44         c0=(c0+t1)&BN_MASK2; if ((c0) < t1) t2++; \
45         c1=(c1+t2)&BN_MASK2; if ((c1) < t2) c2++;
46
47 #define mul_add_c2(a,b,c0,c1,c2) \
48         t1=LBITS(a); t2=HBITS(a); \
49         bl=LBITS(b); bh=HBITS(b); \
50         mul64(t1,t2,bl,bh); \
51         if (t2 & BN_TBIT) c2++; \
52         t2=(t2+t2)&BN_MASK2; \
53         if (t1 & BN_TBIT) t2++; \
54         t1=(t1+t1)&BN_MASK2; \
55         c0=(c0+t1)&BN_MASK2;  \
56         if ((c0 < t1) && (((++t2)&BN_MASK2) == 0)) c2++; \
57         c1=(c1+t2)&BN_MASK2; if ((c1) < t2) c2++;
58
59 #define sqr_add_c(a,i,c0,c1,c2) \
60         sqr64(t1,t2,(a)[i]); \
61         c0=(c0+t1)&BN_MASK2; if ((c0) < t1) t2++; \
62         c1=(c1+t2)&BN_MASK2; if ((c1) < t2) c2++;
63
64 #define sqr_add_c2(a,i,j,c0,c1,c2) \
65         mul_add_c2((a)[i],(a)[j],c0,c1,c2)
66 #endif
67
68 void bn_mul_comba88(BN_ULONG *r,BN_ULONG *a,BN_ULONG *b);
69 void bn_mul_comba44(BN_ULONG *r,BN_ULONG *a,BN_ULONG *b);
70 void bn_sqr_comba88(BN_ULONG *r,BN_ULONG *a);
71 void bn_sqr_comba44(BN_ULONG *r,BN_ULONG *a);
72
73 void bn_mul_comba88(r,a,b)
74 BN_ULONG *r,*a,*b;
75         {
76 #ifdef BN_LLONG
77         BN_ULLONG t;
78 #else
79         BN_ULONG bl,bh;
80 #endif
81         BN_ULONG t1,t2;
82         BN_ULONG c1,c2,c3;
83
84         c1=0;
85         c2=0;
86         c3=0;
87         mul_add_c(a[0],b[0],c1,c2,c3);
88         r[0]=c1;
89         c1=0;
90         mul_add_c(a[0],b[1],c2,c3,c1);
91         mul_add_c(a[1],b[0],c2,c3,c1);
92         r[1]=c2;
93         c2=0;
94         mul_add_c(a[2],b[0],c3,c1,c2);
95         mul_add_c(a[1],b[1],c3,c1,c2);
96         mul_add_c(a[0],b[2],c3,c1,c2);
97         r[2]=c3;
98         c3=0;
99         mul_add_c(a[0],b[3],c1,c2,c3);
100         mul_add_c(a[1],b[2],c1,c2,c3);
101         mul_add_c(a[2],b[1],c1,c2,c3);
102         mul_add_c(a[3],b[0],c1,c2,c3);
103         r[3]=c1;
104         c1=0;
105         mul_add_c(a[4],b[0],c2,c3,c1);
106         mul_add_c(a[3],b[1],c2,c3,c1);
107         mul_add_c(a[2],b[2],c2,c3,c1);
108         mul_add_c(a[1],b[3],c2,c3,c1);
109         mul_add_c(a[0],b[4],c2,c3,c1);
110         r[4]=c2;
111         c2=0;
112         mul_add_c(a[0],b[5],c3,c1,c2);
113         mul_add_c(a[1],b[4],c3,c1,c2);
114         mul_add_c(a[2],b[3],c3,c1,c2);
115         mul_add_c(a[3],b[2],c3,c1,c2);
116         mul_add_c(a[4],b[1],c3,c1,c2);
117         mul_add_c(a[5],b[0],c3,c1,c2);
118         r[5]=c3;
119         c3=0;
120         mul_add_c(a[6],b[0],c1,c2,c3);
121         mul_add_c(a[5],b[1],c1,c2,c3);
122         mul_add_c(a[4],b[2],c1,c2,c3);
123         mul_add_c(a[3],b[3],c1,c2,c3);
124         mul_add_c(a[2],b[4],c1,c2,c3);
125         mul_add_c(a[1],b[5],c1,c2,c3);
126         mul_add_c(a[0],b[6],c1,c2,c3);
127         r[6]=c1;
128         c1=0;
129         mul_add_c(a[0],b[7],c2,c3,c1);
130         mul_add_c(a[1],b[6],c2,c3,c1);
131         mul_add_c(a[2],b[5],c2,c3,c1);
132         mul_add_c(a[3],b[4],c2,c3,c1);
133         mul_add_c(a[4],b[3],c2,c3,c1);
134         mul_add_c(a[5],b[2],c2,c3,c1);
135         mul_add_c(a[6],b[1],c2,c3,c1);
136         mul_add_c(a[7],b[0],c2,c3,c1);
137         r[7]=c2;
138         c2=0;
139         mul_add_c(a[7],b[1],c3,c1,c2);
140         mul_add_c(a[6],b[2],c3,c1,c2);
141         mul_add_c(a[5],b[3],c3,c1,c2);
142         mul_add_c(a[4],b[4],c3,c1,c2);
143         mul_add_c(a[3],b[5],c3,c1,c2);
144         mul_add_c(a[2],b[6],c3,c1,c2);
145         mul_add_c(a[1],b[7],c3,c1,c2);
146         r[8]=c3;
147         c3=0;
148         mul_add_c(a[2],b[7],c1,c2,c3);
149         mul_add_c(a[3],b[6],c1,c2,c3);
150         mul_add_c(a[4],b[5],c1,c2,c3);
151         mul_add_c(a[5],b[4],c1,c2,c3);
152         mul_add_c(a[6],b[3],c1,c2,c3);
153         mul_add_c(a[7],b[2],c1,c2,c3);
154         r[9]=c1;
155         c1=0;
156         mul_add_c(a[7],b[3],c2,c3,c1);
157         mul_add_c(a[6],b[4],c2,c3,c1);
158         mul_add_c(a[5],b[5],c2,c3,c1);
159         mul_add_c(a[4],b[6],c2,c3,c1);
160         mul_add_c(a[3],b[7],c2,c3,c1);
161         r[10]=c2;
162         c2=0;
163         mul_add_c(a[4],b[7],c3,c1,c2);
164         mul_add_c(a[5],b[6],c3,c1,c2);
165         mul_add_c(a[6],b[5],c3,c1,c2);
166         mul_add_c(a[7],b[4],c3,c1,c2);
167         r[11]=c3;
168         c3=0;
169         mul_add_c(a[7],b[5],c1,c2,c3);
170         mul_add_c(a[6],b[6],c1,c2,c3);
171         mul_add_c(a[5],b[7],c1,c2,c3);
172         r[12]=c1;
173         c1=0;
174         mul_add_c(a[6],b[7],c2,c3,c1);
175         mul_add_c(a[7],b[6],c2,c3,c1);
176         r[13]=c2;
177         c2=0;
178         mul_add_c(a[7],b[7],c3,c1,c2);
179         r[14]=c3;
180         r[15]=c1;
181         }
182
183 void bn_mul_comba44(r,a,b)
184 BN_ULONG *r,*a,*b;
185         {
186 #ifdef BN_LLONG
187         BN_ULLONG t;
188 #else
189         BN_ULONG bl,bh;
190 #endif
191         BN_ULONG t1,t2;
192         BN_ULONG c1,c2,c3;
193
194         c1=0;
195         c2=0;
196         c3=0;
197         mul_add_c(a[0],b[0],c1,c2,c3);
198         r[0]=c1;
199         c1=0;
200         mul_add_c(a[0],b[1],c2,c3,c1);
201         mul_add_c(a[1],b[0],c2,c3,c1);
202         r[1]=c2;
203         c2=0;
204         mul_add_c(a[2],b[0],c3,c1,c2);
205         mul_add_c(a[1],b[1],c3,c1,c2);
206         mul_add_c(a[0],b[2],c3,c1,c2);
207         r[2]=c3;
208         c3=0;
209         mul_add_c(a[0],b[3],c1,c2,c3);
210         mul_add_c(a[1],b[2],c1,c2,c3);
211         mul_add_c(a[2],b[1],c1,c2,c3);
212         mul_add_c(a[3],b[0],c1,c2,c3);
213         r[3]=c1;
214         c1=0;
215         mul_add_c(a[3],b[1],c2,c3,c1);
216         mul_add_c(a[2],b[2],c2,c3,c1);
217         mul_add_c(a[1],b[3],c2,c3,c1);
218         r[4]=c2;
219         c2=0;
220         mul_add_c(a[2],b[3],c3,c1,c2);
221         mul_add_c(a[3],b[2],c3,c1,c2);
222         r[5]=c3;
223         c3=0;
224         mul_add_c(a[3],b[3],c1,c2,c3);
225         r[6]=c1;
226         r[7]=c2;
227         }
228
229 void bn_sqr_comba88(r,a)
230 BN_ULONG *r,*a;
231         {
232 #ifdef BN_LLONG
233         BN_ULLONG t,tt;
234 #else
235         BN_ULONG bl,bh;
236 #endif
237         BN_ULONG t1,t2;
238         BN_ULONG c1,c2,c3;
239
240         c1=0;
241         c2=0;
242         c3=0;
243         sqr_add_c(a,0,c1,c2,c3);
244         r[0]=c1;
245         c1=0;
246         sqr_add_c2(a,1,0,c2,c3,c1);
247         r[1]=c2;
248         c2=0;
249         sqr_add_c(a,1,c3,c1,c2);
250         sqr_add_c2(a,2,0,c3,c1,c2);
251         r[2]=c3;
252         c3=0;
253         sqr_add_c2(a,3,0,c1,c2,c3);
254         sqr_add_c2(a,2,1,c1,c2,c3);
255         r[3]=c1;
256         c1=0;
257         sqr_add_c(a,2,c2,c3,c1);
258         sqr_add_c2(a,3,1,c2,c3,c1);
259         sqr_add_c2(a,4,0,c2,c3,c1);
260         r[4]=c2;
261         c2=0;
262         sqr_add_c2(a,5,0,c3,c1,c2);
263         sqr_add_c2(a,4,1,c3,c1,c2);
264         sqr_add_c2(a,3,2,c3,c1,c2);
265         r[5]=c3;
266         c3=0;
267         sqr_add_c(a,3,c1,c2,c3);
268         sqr_add_c2(a,4,2,c1,c2,c3);
269         sqr_add_c2(a,5,1,c1,c2,c3);
270         sqr_add_c2(a,6,0,c1,c2,c3);
271         r[6]=c1;
272         c1=0;
273         sqr_add_c2(a,7,0,c2,c3,c1);
274         sqr_add_c2(a,6,1,c2,c3,c1);
275         sqr_add_c2(a,5,2,c2,c3,c1);
276         sqr_add_c2(a,4,3,c2,c3,c1);
277         r[7]=c2;
278         c2=0;
279         sqr_add_c(a,4,c3,c1,c2);
280         sqr_add_c2(a,5,3,c3,c1,c2);
281         sqr_add_c2(a,6,2,c3,c1,c2);
282         sqr_add_c2(a,7,1,c3,c1,c2);
283         r[8]=c3;
284         c3=0;
285         sqr_add_c2(a,7,2,c1,c2,c3);
286         sqr_add_c2(a,6,3,c1,c2,c3);
287         sqr_add_c2(a,5,4,c1,c2,c3);
288         r[9]=c1;
289         c1=0;
290         sqr_add_c(a,5,c2,c3,c1);
291         sqr_add_c2(a,6,4,c2,c3,c1);
292         sqr_add_c2(a,7,3,c2,c3,c1);
293         r[10]=c2;
294         c2=0;
295         sqr_add_c2(a,7,4,c3,c1,c2);
296         sqr_add_c2(a,6,5,c3,c1,c2);
297         r[11]=c3;
298         c3=0;
299         sqr_add_c(a,6,c1,c2,c3);
300         sqr_add_c2(a,7,5,c1,c2,c3);
301         r[12]=c1;
302         c1=0;
303         sqr_add_c2(a,7,6,c2,c3,c1);
304         r[13]=c2;
305         c2=0;
306         sqr_add_c(a,7,c3,c1,c2);
307         r[14]=c3;
308         r[15]=c1;
309         }
310
311 void bn_sqr_comba44(r,a)
312 BN_ULONG *r,*a;
313         {
314 #ifdef BN_LLONG
315         BN_ULLONG t,tt;
316 #else
317         BN_ULONG bl,bh;
318 #endif
319         BN_ULONG t1,t2;
320         BN_ULONG c1,c2,c3;
321
322         c1=0;
323         c2=0;
324         c3=0;
325         sqr_add_c(a,0,c1,c2,c3);
326         r[0]=c1;
327         c1=0;
328         sqr_add_c2(a,1,0,c2,c3,c1);
329         r[1]=c2;
330         c2=0;
331         sqr_add_c(a,1,c3,c1,c2);
332         sqr_add_c2(a,2,0,c3,c1,c2);
333         r[2]=c3;
334         c3=0;
335         sqr_add_c2(a,3,0,c1,c2,c3);
336         sqr_add_c2(a,2,1,c1,c2,c3);
337         r[3]=c1;
338         c1=0;
339         sqr_add_c(a,2,c2,c3,c1);
340         sqr_add_c2(a,3,1,c2,c3,c1);
341         r[4]=c2;
342         c2=0;
343         sqr_add_c2(a,3,2,c3,c1,c2);
344         r[5]=c3;
345         c3=0;
346         sqr_add_c(a,3,c1,c2,c3);
347         r[6]=c1;
348         r[7]=c2;
349         }