-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmultiplication_scs.c
475 lines (366 loc) · 11.8 KB
/
multiplication_scs.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
/** Functions for SCS multiplication operations
@file multiplication_scs.c
@author Defour David David.Defour@ens-lyon.fr
@author Florent de Dinechin Florent.de.Dinechin@ens-lyon.fr
This file is part of the SCS library.
*/
/*
Copyright (C) 2002 David Defour and Florent de Dinechin
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#include "scs.h"
#include "scs_private.h"
#if 0 /* used to help debugging */
void pr(char* s,double d) {
db_number x;
x.d=d;
printf(s);printf(" ");
printf("%8x%8x . 2^%d (%8f %8x %8x) \n",
(x.i[HI_ENDIAN]&0x000FFFFF)+0x00100000,
x.i[LO_ENDIAN],
(x.i[HI_ENDIAN]>>20)-1023,
x.d,
x.i[HI_ENDIAN],
x.i[LO_ENDIAN]);
}
#endif
/***************************************************************/
/* First a few #defines selecting between architectures where integer
multiplication is faster than FP multiplication (the normal case)
and those where it is the opposite (some Sparcs). The latter
doesn't work but might at some point, so the code remains */
#ifdef SCS_USE_FLT_MULT
/* Compute the carry of r1, remove it from r1, and add it to r0. This
doesn't work: how to get a value truncated using only
round-to-nearest FP ? Besides it doesn't work on x86 for at least two
reasons : extended precision FP arithmetic, and a #define below. */
#define SCS_CARRY_PROPAGATE(r1,r0,tmp) \
{tmp=((r1-SCS_FLT_TRUNC_CST)+SCS_FLT_SHIFT_CST)-SCS_FLT_SHIFT_CST; r1-= tmp; r0+=tmp*SCS_RADIX_MONE_DOUBLE;}
typedef double SCS_CONVERSION_MUL;
#else /* SCS_USE_FLT_MULT*/
/* Compute the carry of r1, remove it from r1, and add it to r0 */
#define SCS_CARRY_PROPAGATE(r1,r0,tmp) \
{tmp = r1>>SCS_NB_BITS; r0 += tmp; r1 -= (tmp<<SCS_NB_BITS);}
typedef unsigned long long int SCS_CONVERSION_MUL;
#endif /* SCS_USE_FLT_MULT */
/************************************************************/
/* We have unrolled the loops for SCS_NB_WORDS==4 or 8
We just wish gcc would do it for us ! There are option switches,
but they don't lead to any performance improvement. When they do,
this part of the source code will be removed.
In the meantime, feel free to unroll for other values. */
/***************************/
#if (SCS_NB_WORDS==4)
/***************************/
void scs_mul(scs_ptr result, scs_ptr x, scs_ptr y){
SCS_CONVERSION_MUL val, tmp;
SCS_CONVERSION_MUL r0,r1,r2,r3,r4;
SCS_CONVERSION_MUL x0,x1,x2,x3;
int y0,y1,y2,y3;
R_EXP = X_EXP * Y_EXP;
R_SGN = X_SGN * Y_SGN;
R_IND = X_IND + Y_IND;
/* Partial products computation */
x3=X_HW[3]; y3=Y_HW[3]; x2=X_HW[2]; y2=Y_HW[2];
x1=X_HW[1]; y1=Y_HW[1]; x0=X_HW[0]; y0=Y_HW[0];
r4 = x3*y1 + x2*y2 + x1*y3;
r3 = x3*y0 + x2*y1 + x1*y2 + x0*y3;
r2 = x2*y0 + x1*y1 + x0*y2;
r1 = x1*y0 + x0*y1 ;
r0 = x0*y0;
val= 0;
/* Carry Propagate */
SCS_CARRY_PROPAGATE(r4,r3,tmp)
SCS_CARRY_PROPAGATE(r3,r2,tmp)
SCS_CARRY_PROPAGATE(r2,r1,tmp)
SCS_CARRY_PROPAGATE(r1,r0,tmp)
SCS_CARRY_PROPAGATE(r0,val,tmp)
if(val != 0){
/* shift all the digits ! */
R_HW[0] = val; R_HW[1] = r0; R_HW[2] = r1; R_HW[3] = r2;
R_IND += 1;
}
else {
R_HW[0] = r0; R_HW[1] = r1; R_HW[2] = r2; R_HW[3] = r3;
}
}
void scs_square(scs_ptr result, scs_ptr x){
SCS_CONVERSION_MUL r0,r1,r2,r3,r4;
SCS_CONVERSION_MUL x0,x1,x2,x3;
SCS_CONVERSION_MUL val, tmp;
R_EXP = X_EXP * X_EXP;
R_IND = X_IND + X_IND;
R_SGN = 1;
/*
* Calcul des PP
*/
x3=X_HW[3]; x2=X_HW[2]; x1=X_HW[1]; x0=X_HW[0];
r0 = x0*x0;
r1 = (x0*x1)* 2 ;
r2 = x1*x1 + (x0*x2*2);
r3 = (x1*x2 + x0*x3)* 2;
r4 = x2*x2 + (x1*x3)* 2;
val= 0;
/* Propagation des retenues */
SCS_CARRY_PROPAGATE(r4,r3,tmp)
SCS_CARRY_PROPAGATE(r3,r2,tmp)
SCS_CARRY_PROPAGATE(r2,r1,tmp)
SCS_CARRY_PROPAGATE(r1,r0,tmp)
SCS_CARRY_PROPAGATE(r0,val,tmp)
if(val != 0){
/* shift all the digits ! */
R_HW[0] = val; R_HW[1] = r0; R_HW[2] = r1; R_HW[3] = r2;
R_IND += 1;
}
else {
R_HW[0] = r0; R_HW[1] = r1; R_HW[2] = r2; R_HW[3] = r3;
}
}
/***************************/
#elif (SCS_NB_WORDS==8)
/***************************/
void scs_mul(scs_ptr result, scs_ptr x, scs_ptr y){
SCS_CONVERSION_MUL val, tmp;
SCS_CONVERSION_MUL r0,r1,r2,r3,r4,r5,r6,r7,r8;
SCS_CONVERSION_MUL x0,x1,x2,x3,x4,x5,x6,x7;
int y0,y1,y2,y3,y4,y5,y6,y7;
R_EXP = X_EXP * Y_EXP;
R_SGN = X_SGN * Y_SGN;
R_IND = X_IND + Y_IND;
/* Partial products computation */
x7=X_HW[7]; y7=Y_HW[7]; x6=X_HW[6]; y6=Y_HW[6];
x5=X_HW[5]; y5=Y_HW[5]; x4=X_HW[4]; y4=Y_HW[4];
x3=X_HW[3]; y3=Y_HW[3]; x2=X_HW[2]; y2=Y_HW[2];
x1=X_HW[1]; y1=Y_HW[1]; x0=X_HW[0]; y0=Y_HW[0];
r8 = x7*y1 + x6*y2 + x5*y3 + x4*y4 + x3*y5 + x2*y6 + x1*y7;
r7 = x7*y0 + x6*y1 + x5*y2 + x4*y3 + x3*y4 + x2*y5 + x1*y6 + x0*y7;
r6 = x6*y0 + x5*y1 + x4*y2 + x3*y3 + x2*y4 + x1*y5 + x0*y6;
r5 = x5*y0 + x4*y1 + x3*y2 + x2*y3 + x1*y4 + x0*y5;
r4 = x4*y0 + x3*y1 + x2*y2 + x1*y3 + x0*y4 ;
r3 = x3*y0 + x2*y1 + x1*y2 + x0*y3;
r2 = x2*y0 + x1*y1 + x0*y2;
r1 = x1*y0 + x0*y1 ;
r0 = x0*y0 ;
val= 0;
/* Carry Propagate */
SCS_CARRY_PROPAGATE(r8,r7,tmp)
SCS_CARRY_PROPAGATE(r7,r6,tmp)
SCS_CARRY_PROPAGATE(r6,r5,tmp)
SCS_CARRY_PROPAGATE(r5,r4,tmp)
SCS_CARRY_PROPAGATE(r4,r3,tmp)
SCS_CARRY_PROPAGATE(r3,r2,tmp)
SCS_CARRY_PROPAGATE(r2,r1,tmp)
SCS_CARRY_PROPAGATE(r1,r0,tmp)
SCS_CARRY_PROPAGATE(r0,val,tmp)
if(val != 0){
/* shift all the digits ! */
R_HW[0] = val; R_HW[1] = r0; R_HW[2] = r1; R_HW[3] = r2;
R_HW[4] = r3; R_HW[5] = r4; R_HW[6] = r5; R_HW[7] = r6;
R_IND += 1;
}
else {
R_HW[0] = r0; R_HW[1] = r1; R_HW[2] = r2; R_HW[3] = r3;
R_HW[4] = r4; R_HW[5] = r5; R_HW[6] = r6; R_HW[7] = r7;
}
}
void scs_square(scs_ptr result, scs_ptr x){
SCS_CONVERSION_MUL r0,r1,r2,r3,r4,r5,r6,r7,r8;
SCS_CONVERSION_MUL x0,x1,x2,x3,x4,x5,x6,x7;
SCS_CONVERSION_MUL val, tmp;
R_EXP = X_EXP * X_EXP;
R_IND = X_IND + X_IND;
R_SGN = 1;
/*
* Partial products
*/
x7=X_HW[7]; x6=X_HW[6]; x5=X_HW[5]; x4=X_HW[4];
x3=X_HW[3]; x2=X_HW[2]; x1=X_HW[1]; x0=X_HW[0];
r0 = x0*x0;
r1 = (x0*x1)* 2 ;
r2 = x1*x1 + (x0*x2*2);
r3 = (x1*x2 + x0*x3)* 2;
r4 = x2*x2 + (x1*x3 + x0*x4)* 2;
r5 = (x2*x3 + x1*x4 + x0*x5)* 2;
r6 = x3*x3 + (x2*x4 + x1*x5 + x0*x6)* 2;
r7 = (x3*x4 + x2*x5 + x1*x6 + x0*x7)* 2;
r8 = x4*x4 + (x3*x5 + x2*x6 + x1*x7)* 2;
val= 0;
/* Carry propagation */
SCS_CARRY_PROPAGATE(r8,r7,tmp)
SCS_CARRY_PROPAGATE(r7,r6,tmp)
SCS_CARRY_PROPAGATE(r6,r5,tmp)
SCS_CARRY_PROPAGATE(r5,r4,tmp)
SCS_CARRY_PROPAGATE(r4,r3,tmp)
SCS_CARRY_PROPAGATE(r3,r2,tmp)
SCS_CARRY_PROPAGATE(r2,r1,tmp)
SCS_CARRY_PROPAGATE(r1,r0,tmp)
SCS_CARRY_PROPAGATE(r0,val,tmp)
if(val != 0){
/* shift all the digits ! */
R_HW[0] = val; R_HW[1] = r0; R_HW[2] = r1; R_HW[3] = r2;
R_HW[4] = r3; R_HW[5] = r4; R_HW[6] = r5; R_HW[7] = r6;
R_IND += 1;
}
else {
R_HW[0] = r0; R_HW[1] = r1; R_HW[2] = r2; R_HW[3] = r3;
R_HW[4] = r4; R_HW[5] = r5; R_HW[6] = r6; R_HW[7] = r7;
}
}
/***************************/
#else
/***************************/
/* From there on, the normal, unrolled case */
void scs_mul(scs_ptr result, scs_ptr x, scs_ptr y){
SCS_CONVERSION_MUL RES[SCS_NB_WORDS+1];
SCS_CONVERSION_MUL val, tmp;
int i, j;
R_EXP = X_EXP * Y_EXP;
R_SGN = X_SGN * Y_SGN;
R_IND = X_IND + Y_IND;
for(i=0; i<=SCS_NB_WORDS; i++)
RES[i]=0;
/* Compute only the first half of the partial product. See the
unrolled code for an example of what we compute */
#ifdef SCS_TYPECPU_X86
/* This is the only place where there is assembly code to force 64-bit
arithmetic. Someday gcc will catch up here, too.
*/
{
db_number t;
/* i=0 */
for(j=0; j<(SCS_NB_WORDS); j++) {
__asm__ volatile("mull %3"
: "=a" (t.i[LO_ENDIAN]), "=d" (t.i[HI_ENDIAN])
: "a" (X_HW[0]) , "g" (Y_HW[j]));
RES[j] += t.l;
}
/* i = 1..SCS_NB_WORDS-1 */
for(i=1 ; i<SCS_NB_WORDS; i++){
for(j=0; j<(SCS_NB_WORDS-i); j++){
__asm__ volatile("mull %3"
: "=a" (t.i[LO_ENDIAN]), "=d" (t.i[HI_ENDIAN])
: "a" (X_HW[i]) , "g" (Y_HW[j]));
RES[i+j] += t.l;
}
__asm__ volatile("mull %3"
: "=a" (t.i[LO_ENDIAN]), "=d" (t.i[HI_ENDIAN])
: "a" (X_HW[i]) , "g" (Y_HW[j]));
/* here j==SCS_NB_WORDS-i */
RES[SCS_NB_WORDS] += t.l;
}
}
#else /* other architectures */
/* i=0 */
tmp = X_HW[0];
for(j=0; j<(SCS_NB_WORDS); j++)
RES[j] += tmp * Y_HW[j];
/* i = 1..SCS_NB_WORDS-1 */
for(i=1 ; i<SCS_NB_WORDS; i++){
tmp = X_HW[i];
for(j=0; j<(SCS_NB_WORDS-i); j++)
RES[i+j] += tmp * Y_HW[j];
RES[SCS_NB_WORDS] += tmp * Y_HW[j]; /* here j==SCS_NB_WORDS-i */
}
#endif/* SCS_TYPECPU_X86 */
val = 0;
/* Carry propagate */
for(i=SCS_NB_WORDS; i>0; i--)
SCS_CARRY_PROPAGATE(RES[i],RES[i-1],tmp)
SCS_CARRY_PROPAGATE(RES[0],val,tmp)
/* Store the result */
if(val != 0){
/* shift all the digits ! */
R_HW[0] = val;
for(i=1; i<SCS_NB_WORDS; i++)
R_HW[i] = RES[i-1];
R_IND += 1;
}else {
for(i=0; i<SCS_NB_WORDS; i++)
R_HW[i] = RES[i];
}
}
void scs_square(scs_ptr result, scs_ptr x){
SCS_CONVERSION_MUL RES[SCS_NB_WORDS+1];
SCS_CONVERSION_MUL val, tmp;
int i, j;
R_EXP = X_EXP * X_EXP;
R_SGN = 1;
R_IND = X_IND + X_IND;
/* Set to 0 intermediate register */
for(i=0; i<=SCS_NB_WORDS; i++)
RES[i] = 0;
/* Compute all the double partial products: 2 x_i * x_j, i!=j */
tmp = (SCS_CONVERSION_MUL)X_HW[0];
for(j=1; j<SCS_NB_WORDS; j++)
RES[j] += tmp * X_HW[j];
for(i=1 ; i<(SCS_NB_WORDS+1)/2; i++){
tmp = (SCS_CONVERSION_MUL)X_HW[i];
for(j=i+1; j<(SCS_NB_WORDS-i); j++)
RES[i+j] += tmp * X_HW[j];
RES[SCS_NB_WORDS] += tmp * X_HW[SCS_NB_WORDS-i];
}
/* All these partial products are double */
for(i=0; i<=SCS_NB_WORDS; i++)
RES[i] *=2;
/* Add partial product of the form x_i^2 */
for(i=0, j=0; i<=SCS_NB_WORDS; i+=2, j++){
RES[i] += (SCS_CONVERSION_MUL)X_HW[j] * X_HW[j];
}
val = 0;
/* Carry propagate */
for(i=SCS_NB_WORDS; i>0; i--)
SCS_CARRY_PROPAGATE(RES[i],RES[i-1],tmp)
SCS_CARRY_PROPAGATE(RES[0],val,tmp)
/* Store the result */
if(val != 0){
/* shift all the digits ! */
R_HW[0] = val;
for(i=1; i<SCS_NB_WORDS; i++)
R_HW[i] = RES[i-1];
R_IND += 1;
}else {
for(i=0; i<SCS_NB_WORDS; i++)
R_HW[i] = RES[i];
}
}
/*
* #endif corresponding to the test #if (SCS_NB_WORDS==8)
*/
#endif
/*
Multiply x by an integer val; result is returned in x.
*/
void scs_mul_ui(scs_ptr x, unsigned int val_int){
SCS_CONVERSION_MUL val, tmp, vald, rr;
int i;
if (val_int == 0)
X_EXP = 0;
vald = val_int;
val = 0;
rr = 0;
for(i=(SCS_NB_WORDS-1); i>=0; i--){
val += vald * X_HW[i];
SCS_CARRY_PROPAGATE(val, rr, tmp)
X_HW[i] = val;
val = rr;
rr = 0;
}
if(val != 0){
/* shift all the digits ! */
for(i=(SCS_NB_WORDS-1); i>0; i--)
X_HW[i] = X_HW[i-1];
X_HW[0] = val;
X_IND += 1;
}
return;
}