-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaddition_scs.c
655 lines (545 loc) · 16.4 KB
/
addition_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
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
/** Functions for SCS addition and subtraction
@file addition_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.
Many functions come in two versions, selected by a @#if.
The reason is that we designed scslib library for internal use with
SCS_NB_WORDS==8, so we provide a version with manual optimizations for
this case.
These optimisations include loop unrolling, and sometimes replacing
temporary arrays of size 8 with 8 variables, which is more efficient
on all modern processors with many (renaming) registers.
Using gcc3.2 with the most aggressive optimization options for this
purpose (-funroll-loops -foptimize-register-move -frerun-loop-opt
-frerun-cse-after-loop) is still much slower. At some point in the
future, gcc should catch up with unrolling since our loops are so
simple, however the replacement of small arrays with variables is
not something we are aware of in the literature about compiler
optimization.
*/
/*
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"
/**
This function copies a result into another. There is an unrolled
version for the case SCS_NB_WORDS==8.
*/
#ifdef LINUX_INLINE
void inline scs_set(scs_ptr result, scs_ptr x){
#else
void scs_set(scs_ptr result, scs_ptr x){
#endif
/* unsigned int i;*/
#if (SCS_NB_WORDS==8)
R_HW[0] = X_HW[0]; R_HW[1] = X_HW[1];
R_HW[2] = X_HW[2]; R_HW[3] = X_HW[3];
R_HW[4] = X_HW[4]; R_HW[5] = X_HW[5];
R_HW[6] = X_HW[6]; R_HW[7] = X_HW[7];
#else
for(i=0; i<SCS_NB_WORDS; i++)
R_HW[i] = X_HW[i];
#endif
R_EXP = X_EXP;
R_IND = X_IND;
R_SGN = X_SGN;
}
/** renormalize a SCS number.
This function removes the carry from each digit, and also shifts the
digits in case of a cancellation (so that if result != 0 then its
first digit is non-zero)
@warning THIS FUNCTION HAS NEVER BEEN PROPERLY TESTED and is
currently unused in the library: instead, specific renormalisation
steps are fused within the code of the operations which require it.
*/
#ifdef LINUX_INLINE
void inline scs_renorm(scs_ptr result){
#else
void scs_renorm(scs_ptr result){
#endif
unsigned int c;
int i, j, k;
/*
* Carry propagate
*/
for(i=SCS_NB_WORDS-1; i>0; i--){
c = R_HW[i] & ~SCS_MASK_RADIX;
R_HW[i-1] += c >> SCS_NB_BITS;
R_HW[i] = R_HW[i] & SCS_MASK_RADIX;
}
if (R_HW[0] >= SCS_RADIX){
/* Carry out! Need to shift digits */
c = R_HW[0] & ~SCS_MASK_RADIX;
c = c >> SCS_NB_BITS;
for(i=SCS_NB_WORDS-1; i>1; i--)
R_HW[i] = R_HW[i-1];
R_HW[1] = R_HW[0] & SCS_MASK_RADIX;
R_HW[0] = c;
R_IND += 1;
}else{
/* Was there a cancellation ? */
if (R_HW[0] == 0){
k = 1;
while ((R_HW[k] == 0) && (k <= SCS_NB_WORDS))
k++;
R_IND -= k;
for(j=k, i=0; j<SCS_NB_WORDS; j++, i++)
R_HW[i] = R_HW[j];
for( ; i<SCS_NB_WORDS; i++)
R_HW[i] = 0;
}
}
}
/** Renormalization without cancellation check.
This renormalization step is especially designed for the addition of
several numbers with the same sign. In this case, you know that there
has been no cancellation, which allows simpler renormalisation.
*/
#ifdef LINUX_INLINE
void inline scs_renorm_no_cancel_check(scs_ptr result){
#else
void scs_renorm_no_cancel_check(scs_ptr result){
#endif
unsigned int carry, c0;
/* int i;*/
/* Carry propagate */
#if (SCS_NB_WORDS==8)
carry = R_HW[7] >> SCS_NB_BITS;
R_HW[6] += carry; R_HW[7] = R_HW[7] & SCS_MASK_RADIX;
carry = R_HW[6] >> SCS_NB_BITS;
R_HW[5] += carry; R_HW[6] = R_HW[6] & SCS_MASK_RADIX;
carry = R_HW[5] >> SCS_NB_BITS;
R_HW[4] += carry; R_HW[5] = R_HW[5] & SCS_MASK_RADIX;
carry = R_HW[4] >> SCS_NB_BITS;
R_HW[3] += carry; R_HW[4] = R_HW[4] & SCS_MASK_RADIX;
carry = R_HW[3] >> SCS_NB_BITS;
R_HW[2] += carry; R_HW[3] = R_HW[3] & SCS_MASK_RADIX;
carry = R_HW[2] >> SCS_NB_BITS;
R_HW[1] += carry; R_HW[2] = R_HW[2] & SCS_MASK_RADIX;
carry = R_HW[1] >> SCS_NB_BITS;
R_HW[0] += carry; R_HW[1] = R_HW[1] & SCS_MASK_RADIX;
#else
for(i=(SCS_NB_WORDS-1);i>0;i--){
carry = R_HW[i] >> SCS_NB_BITS;
R_HW[i-1] += carry;
R_HW[i] = R_HW[i] & SCS_MASK_RADIX;
}
#endif
if (R_HW[0] >= SCS_RADIX){
/* Carry out ! Need to shift digits */
c0 = R_HW[0] >> SCS_NB_BITS;
#if (SCS_NB_WORDS==8)
R_HW[7] = R_HW[6]; R_HW[6] = R_HW[5];
R_HW[5] = R_HW[4]; R_HW[4] = R_HW[3];
R_HW[3] = R_HW[2]; R_HW[2] = R_HW[1];
#else
for(i=(SCS_NB_WORDS-1); i>1; i--)
R_HW[i] = R_HW[i-1];
#endif
R_HW[1] = R_HW[0] & SCS_MASK_RADIX;
R_HW[0] = c0;
R_IND += 1;
}
return;
}
/* addition without renormalisation.
Add two scs number x and y, the result is put into "result".
Assumes x.sign == y.sign x.index > y.index.
The result is not normalized.
*/
void do_add_no_renorm(scs_ptr result, scs_ptr x, scs_ptr y){
unsigned int RES[SCS_NB_WORDS];
unsigned int i, j, Diff;
if (x->exception.i[HI_ENDIAN]==0){scs_set(result, y); return; }
if (y->exception.i[HI_ENDIAN]==0){scs_set(result, x); return; }
for (i=0; i<SCS_NB_WORDS; i++)
RES[i] = X_HW[i];
Diff = X_IND - Y_IND;
R_EXP = X_EXP + Y_EXP - 1;
R_IND = X_IND;
R_SGN = X_SGN;
for (i=Diff, j=0; i<SCS_NB_WORDS; i++, j++)
RES[i] += Y_HW[j];
for (i=0; i<SCS_NB_WORDS; i++)
R_HW[i] = RES[i];
return;
}
/*
* Addition without renormalization. Assumes that x.sign == y.sign.
*/
#ifdef LINUX_INLINE
void inline scs_add_no_renorm(scs_ptr result, scs_ptr x, scs_ptr y)
#else
void scs_add_no_renorm(scs_ptr result, scs_ptr x, scs_ptr y)
#endif
{
if (X_IND >= Y_IND)
do_add_no_renorm(result,x,y);
else
do_add_no_renorm(result,y,x);
return;
}
/* The function that does the work in case of an addition
do_add is the function that does the addition of two SCS numbers,
assuming that x.sign == y.sign, X_IND > Y_IND, x and y both
non-zero.
*/
void do_add(scs_ptr result, scs_ptr x, scs_ptr y)
{
#if (SCS_NB_WORDS==8) /* in this case we unroll all the loops */
int carry, Diff;
int r0,r1,r2,r3,r4,r5,r6,r7;
Diff = X_IND - Y_IND;
R_EXP = X_EXP + Y_EXP - 1;
R_IND = X_IND;
R_SGN = X_SGN;
#if 0
if(Diff<4)
if(Diff<2)
if(Diff==0)
{
// case 0:
r0 = X_HW[0] + Y_HW[0]; r1 = X_HW[1] + Y_HW[1];
r2 = X_HW[2] + Y_HW[2]; r3 = X_HW[3] + Y_HW[3];
r4 = X_HW[4] + Y_HW[4]; r5 = X_HW[5] + Y_HW[5];
r6 = X_HW[6] + Y_HW[6]; r7 = X_HW[7] + Y_HW[7];
}
else {
// case 1:
r0 = X_HW[0]; r1 = X_HW[1] + Y_HW[0];
r2 = X_HW[2] + Y_HW[1]; r3 = X_HW[3] + Y_HW[2];
r4 = X_HW[4] + Y_HW[3]; r5 = X_HW[5] + Y_HW[4];
r6 = X_HW[6] + Y_HW[5]; r7 = X_HW[7] + Y_HW[6];
}
else if(Diff==2)
{
//case 2:
r0 = X_HW[0]; r1 = X_HW[1];
r2 = X_HW[2] + Y_HW[0]; r3 = X_HW[3] + Y_HW[1];
r4 = X_HW[4] + Y_HW[2]; r5 = X_HW[5] + Y_HW[3];
r6 = X_HW[6] + Y_HW[4]; r7 = X_HW[7] + Y_HW[5];
}
else
{
// case 3:
r0 = X_HW[0]; r1 = X_HW[1];
r2 = X_HW[2]; r3 = X_HW[3] + Y_HW[0];
r4 = X_HW[4] + Y_HW[1]; r5 = X_HW[5] + Y_HW[2];
r6 = X_HW[6] + Y_HW[3]; r7 = X_HW[7] + Y_HW[4];
}
else if(Diff<6)
if(Diff==4)
{
// case 4:
r0 = X_HW[0]; r1 = X_HW[1];
r2 = X_HW[2]; r3 = X_HW[3];
r4 = X_HW[4] + Y_HW[0]; r5 = X_HW[5] + Y_HW[1];
r6 = X_HW[6] + Y_HW[2]; r7 = X_HW[7] + Y_HW[3];
}
else {
// case 5:
r0 = X_HW[0]; r1 = X_HW[1];
r2 = X_HW[2]; r3 = X_HW[3];
r4 = X_HW[4]; r5 = X_HW[5] + Y_HW[0];
r6 = X_HW[6] + Y_HW[1]; r7 = X_HW[7] + Y_HW[2];
}
else if(Diff<8)
if(Diff==6)
{
// case 6:
r0 = X_HW[0]; r1 = X_HW[1];
r2 = X_HW[2]; r3 = X_HW[3];
r4 = X_HW[4]; r5 = X_HW[5];
r6 = X_HW[6] + Y_HW[0]; r7 = X_HW[7] + Y_HW[1];
}
else {
// case 7:
r0 = X_HW[0]; r1 = X_HW[1];
r2 = X_HW[2]; r3 = X_HW[3];
r4 = X_HW[4]; r5 = X_HW[5];
r6 = X_HW[6]; r7 = X_HW[7] + Y_HW[0];
}
else
{
/* Diff >= 8*/
R_HW[0] = X_HW[0]; R_HW[1] = X_HW[1];
R_HW[2] = X_HW[2]; R_HW[3] = X_HW[3];
R_HW[4] = X_HW[4]; R_HW[5] = X_HW[5];
R_HW[6] = X_HW[6]; R_HW[7] = X_HW[7];
return;
}
#else
switch (Diff){
case 0:
r0 = X_HW[0] + Y_HW[0]; r1 = X_HW[1] + Y_HW[1];
r2 = X_HW[2] + Y_HW[2]; r3 = X_HW[3] + Y_HW[3];
r4 = X_HW[4] + Y_HW[4]; r5 = X_HW[5] + Y_HW[5];
r6 = X_HW[6] + Y_HW[6]; r7 = X_HW[7] + Y_HW[7]; break;
case 1:
r0 = X_HW[0]; r1 = X_HW[1] + Y_HW[0];
r2 = X_HW[2] + Y_HW[1]; r3 = X_HW[3] + Y_HW[2];
r4 = X_HW[4] + Y_HW[3]; r5 = X_HW[5] + Y_HW[4];
r6 = X_HW[6] + Y_HW[5]; r7 = X_HW[7] + Y_HW[6]; break;
case 2:
r0 = X_HW[0]; r1 = X_HW[1];
r2 = X_HW[2] + Y_HW[0]; r3 = X_HW[3] + Y_HW[1];
r4 = X_HW[4] + Y_HW[2]; r5 = X_HW[5] + Y_HW[3];
r6 = X_HW[6] + Y_HW[4]; r7 = X_HW[7] + Y_HW[5]; break;
case 3:
r0 = X_HW[0]; r1 = X_HW[1];
r2 = X_HW[2]; r3 = X_HW[3] + Y_HW[0];
r4 = X_HW[4] + Y_HW[1]; r5 = X_HW[5] + Y_HW[2];
r6 = X_HW[6] + Y_HW[3]; r7 = X_HW[7] + Y_HW[4]; break;
case 4:
r0 = X_HW[0]; r1 = X_HW[1];
r2 = X_HW[2]; r3 = X_HW[3];
r4 = X_HW[4] + Y_HW[0]; r5 = X_HW[5] + Y_HW[1];
r6 = X_HW[6] + Y_HW[2]; r7 = X_HW[7] + Y_HW[3]; break;
case 5:
r0 = X_HW[0]; r1 = X_HW[1];
r2 = X_HW[2]; r3 = X_HW[3];
r4 = X_HW[4]; r5 = X_HW[5] + Y_HW[0];
r6 = X_HW[6] + Y_HW[1]; r7 = X_HW[7] + Y_HW[2]; break;
case 6:
r0 = X_HW[0]; r1 = X_HW[1];
r2 = X_HW[2]; r3 = X_HW[3];
r4 = X_HW[4]; r5 = X_HW[5];
r6 = X_HW[6] + Y_HW[0]; r7 = X_HW[7] + Y_HW[1]; break;
case 7:
r0 = X_HW[0]; r1 = X_HW[1];
r2 = X_HW[2]; r3 = X_HW[3];
r4 = X_HW[4]; r5 = X_HW[5];
r6 = X_HW[6]; r7 = X_HW[7] + Y_HW[0]; break;
default:
/* Diff >= 8*/
R_HW[0] = X_HW[0]; R_HW[1] = X_HW[1];
R_HW[2] = X_HW[2]; R_HW[3] = X_HW[3];
R_HW[4] = X_HW[4]; R_HW[5] = X_HW[5];
R_HW[6] = X_HW[6]; R_HW[7] = X_HW[7]; return;
}
#endif
/* Carry propagation */
carry = r7 >> SCS_NB_BITS; r6 += carry; r7 = r7 & SCS_MASK_RADIX;
carry = r6 >> SCS_NB_BITS; r5 += carry; r6 = r6 & SCS_MASK_RADIX;
carry = r5 >> SCS_NB_BITS; r4 += carry; r5 = r5 & SCS_MASK_RADIX;
carry = r4 >> SCS_NB_BITS; r3 += carry; r4 = r4 & SCS_MASK_RADIX;
carry = r3 >> SCS_NB_BITS; r2 += carry; r3 = r3 & SCS_MASK_RADIX;
carry = r2 >> SCS_NB_BITS; r1 += carry; r2 = r2 & SCS_MASK_RADIX;
carry = r1 >> SCS_NB_BITS; r0 += carry; r1 = r1 & SCS_MASK_RADIX;
carry = r0 >> SCS_NB_BITS;
if (carry){
R_HW[7] = r6; R_HW[6] = r5; R_HW[5] = r4; R_HW[4] = r3;
R_HW[3] = r2; R_HW[2] = r1; R_HW[1] = r0 & SCS_MASK_RADIX;
R_HW[0] = 1 ;
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;
}
return;
#else /* #if SCS_NB_WORDS==8*/
/* This generic version is still written in such a way that
it is unrollable at compile time
*/
int i,j, s, carry, Diff;
int res[SCS_NB_WORDS];
Diff = X_IND - Y_IND;
R_EXP = X_EXP + Y_EXP - 1;
R_IND = X_IND;
R_SGN = X_SGN;
/* The easy case */
if(Diff >= SCS_NB_WORDS){
scs_set(result, x); return;
}
/* 0 <= Diff <= (SCS_NB_WORDS-1) */
carry=0;
for(i=(SCS_NB_WORDS-1), j=((SCS_NB_WORDS-1)-Diff); i>=0 ; i--,j--){
if (j>=0)
s = X_HW[i] + Y_HW[j] + carry;
else
s = X_HW[i] + carry;
carry = s >> SCS_NB_BITS;
res[i] = s & SCS_MASK_RADIX;
}
if (carry){
/* Carry out ! Need to shift digits */
for(i=(SCS_NB_WORDS-1); i>=1; i--)
R_HW[i] = res[i-1];
R_HW[0] = 1 ;
R_IND += 1;
}
else {
for(i=0; i<SCS_NB_WORDS; i++)
R_HW[i] = res[i];
}
return;
#endif /* #if SCS_NB_WORDS==8*/
} /* do_add*/
/*
* Compare only the mantissa of x and y without the index or sign field
* Return :
* - 1 if x > y
* - (-1) if x < y
* - (0) if x = y
*/
#ifdef LINUX_INLINE
int inline scs_cmp_mant(scs_ptr x, scs_ptr y){
#else
int scs_cmp_mant(scs_ptr x, scs_ptr y){
#endif
int i;
/* Tried to unroll this loop, it doesn't work */
for(i=0; i< SCS_NB_WORDS; i++){
if (X_HW[i] == Y_HW[i]) continue;
else if (X_HW[i] > Y_HW[i]) return 1;
else return -1;}
return 0;
}
/*/////////////////////////////////////////////////////////////////
/////////////////////// SUBTRACTION //////////////////////////////
//////////////////////////////////////////////////////////////////
// This procedure assumes :
// - X_IND >= Y_IND
// - X_SIGN != Y_SIGN
// neither x or y is zero
// and result = x - y
*/
void do_sub(scs_ptr result, scs_ptr x, scs_ptr y){
int s, carry;
int Diff, i, j, cp;
int res[SCS_NB_WORDS];
R_EXP = X_EXP + Y_EXP - 1;
Diff = X_IND - Y_IND;
R_IND = X_IND;
/* The easy case */
if(Diff >= SCS_NB_WORDS){
scs_set(result, x); return;
}
else {
/* 0 <= Diff <= (SCS_NB_WORDS-1) */
carry = 0;
if(Diff==0) {
cp = scs_cmp_mant(x, y);
if (cp == 0) {
/* Yet another easy case: result = 0 */
scs_zero(result);
return;
}
else { /* cp <> 0 */
if (cp > 0){
/* x > y */
R_SGN = X_SGN;
for(i=(SCS_NB_WORDS-1); i>=0 ;i--){
s = X_HW[i] - Y_HW[i] - carry;
carry = (s&SCS_RADIX)>>SCS_NB_BITS;
res[i] = (s&SCS_RADIX) + s;
}
}
else { /* cp < 0 */
/* x < y (change of sign) */
R_SGN = - X_SGN;
for(i=(SCS_NB_WORDS-1); i>=0 ;i--){
s = - X_HW[i] + Y_HW[i] - carry;
carry = (s&SCS_RADIX)>>SCS_NB_BITS;
res[i] = (s&SCS_RADIX) + s;
}
}
}
}
else {
/* 1<=Diff<(SCS_NB_WORDS-1) Digits of x and y overlap but the
* sign will be that of x */
R_SGN = X_SGN;
for(i=(SCS_NB_WORDS-1), j=((SCS_NB_WORDS-1)-Diff); i>=0 ;i--,j--){
if(j>=0)
s = X_HW[i] - Y_HW[j] - carry;
else
s = X_HW[i] - carry;
carry = (s&SCS_RADIX)>>SCS_NB_BITS;
res[i] = (s&SCS_RADIX) + s;
}
}
/* check for cancellations */
i=0;
while ((res[i]==0) && (i < SCS_NB_WORDS)) i++;
if(i>0) { /* cancellation, shift result*/
R_IND -= i;
for(j=0; i<SCS_NB_WORDS; i++,j++) R_HW[j] = res[i];
for( ; j<SCS_NB_WORDS; j++) R_HW[j] = 0;
}
else {
for(i=0; i<SCS_NB_WORDS; i++)
R_HW[i] = res[i];
}
}
return;
}
/** SCS addition (result is a normalised SCS number).
*/
#ifdef LINUX_INLINE
void inline scs_add(scs_ptr result, scs_ptr x, scs_ptr y)
#else
void scs_add(scs_ptr result, scs_ptr x, scs_ptr y)
#endif
{
if (x->exception.i[HI_ENDIAN]==0){scs_set(result, y); return; }
if (y->exception.i[HI_ENDIAN]==0){scs_set(result, x); return; }
if (X_SGN == Y_SGN){
if(X_IND >= Y_IND)
do_add(result,x,y);
else
do_add(result,y,x);
}else {
if(X_IND>=Y_IND){
do_sub(result,x,y);
}else {
do_sub(result,y,x);
}
} return;
}
/** SCS subtraction (result is a normalised SCS number).
The arguments x, y and result may point to the same memory
location.
*/
#ifdef LINUX_INLINE
void inline scs_sub(scs_ptr result, scs_ptr x, scs_ptr y)
#else
void scs_sub(scs_ptr result, scs_ptr x, scs_ptr y)
#endif
{
if (x->exception.i[HI_ENDIAN]==0)
{ scs_set(result, y); R_SGN = -R_SGN; return; }
if (y->exception.i[HI_ENDIAN]==0)
{ scs_set(result, x); return; }
if (X_SGN == Y_SGN) {
/* Same sign, so it's a sub */
if(X_IND>=Y_IND)
do_sub(result,x,y);
else{
do_sub(result,y,x);
R_SGN = -R_SGN;
}
}else {
if(X_IND>=Y_IND)
do_add(result,x,y);
else{
do_add(result,y,x);
R_SGN = -R_SGN;
}
}
return;
}