about summary refs log tree commit diff
path: root/sysdeps/powerpc/q_addsub.c
blob: e4ef6d8165a4ad92d3a353c5b49b6da3e3b85a2b (plain) (blame)
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
/* Add or subtract two 128-bit floating point values.  C prototype.
   Copyright (C) 1997 Free Software Foundation, Inc.
   This file is part of the GNU C Library.

   The GNU C Library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Library General Public License as
   published by the Free Software Foundation; either version 2 of the
   License, or (at your option) any later version.

   The GNU C 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
   Library General Public License for more details.

   You should have received a copy of the GNU Library General Public
   License along with the GNU C Library; see the file COPYING.LIB.  If not,
   write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
   Boston, MA 02111-1307, USA.  */

#include <quad_float.h>

/* Add 'a' to 'b' and put the result in 'result', but treat a[0]=axx,
   b[0]=bxx.  bxx differs from b[0] only in the high bit, similarly axx.  */
/* Exceptions to raise:
   - Invalid (SNaN)
   - Invalid (Inf-Inf)
   - Overflow
   - Underflow
   - Inexact
   */

/* Handle cases where exponent of a or b is maximum.  */
static void
handle_max_exponent(unsigned result[4],
		    const unsigned a[4], const unsigned b[4],
		    const unsigned axx,  /* Treat as a[0].  */
		    const unsigned bxx,  /* Treat as b[0].  */
		    const unsigned ax,   /* axx >> 16 & 0x7fff.  */
		    const unsigned bx)   /* bxx >> 16 & 0x7fff.  */
{
  int ax_ismax, bx_ismax;
  unsigned a1,a2,a3, b1,b2,b3;
  int a_zeromant, b_zeromant;

  ax_ismax = ax == 0x7fff;
  bx_ismax = bx == 0x7fff;

  assert(ax_ismax || bx_ismax);

  a1 = a[1]; a2 = a[2]; a3 = a[3];
  b1 = b[1]; b2 = b[2]; b3 = b[3];
  
  a_zeromant = (axx & 0xffff  |  a1 | a2 | a3) == 0;
  b_zeromant = (bxx & 0xffff  |  b1 | b2 | b3) == 0;
  
  /* Deal with SNaNs.  */
  if (   ax_ismax && !a_zeromant && (axx & 0x8000) == 0
      || bx_ismax && !b_zeromant && (bxx & 0x8000) == 0)
    {
      set_fpscr_bit(FPSCR_VXSNAN);
      axx |= 0x8000; /* Demote the SNaN to a QNaN (whichever of */
      bxx |= 0x8000; /* a or b it was).  */
    }
  /* Deal with Inf-Inf.  */
  else if (a_zeromant && b_zeromant && (axx ^ bxx) == 0x80000000)
    {
      set_fpscr_bit(FPSCR_VXISI);
      bxx |= 0x8000; /* Return an appropriate QNaN.  */
    }
  
  /* Return the lexicographically larger of a or b, ignoring the sign
     bits.  */
  if ((axx & 0x7fffffff) > (bxx & 0x7fffffff)) goto return_a;
  else if ((axx & 0x7fffffff) < (bxx & 0x7fffffff)) goto return_b;
  else if (a1 > b1) goto return_a;
  else if (a1 < b1) goto return_b;
  else if (a2 > b2) goto return_a;
  else if (a2 < b2) goto return_b;
  else if (a3 > b3) goto return_a;  /* I've clearly been writing too */
  else if (a3 < b3) goto return_b;  /* much Fortran...  */
  
  /* If they are equal except for the sign bits, return 'b'.  */
  
return_b:
  result[0] = bxx; result[1] = b1; result[2] = b2; result[3] = b3;
  return;
    
return_a:
  result[0] = axx; result[1] = a1; result[2] = a2; result[3] = a3;
  return;
}

/* Renormalise and output a FP number.  */
static void
renormalise_value(unsigned result[4],
		  const unsigned axx,
		  unsigned ax,
		  unsigned r0,
		  unsigned r1,
		  unsigned r2,
		  unsigned r3)
{
  int rshift;
  if (r0 != 0 || cntlzw(a1) < 16 || 32 > ax-1)
    {
      rshift = cntlzw(r0)-15 + (-(cntlzw(r0) >> 5) & cntlzw(a1));
      assert(rshift < 32);
      if (rshift > ax-1)
	{
	  ax--;
	  rshift = ax;
	}

      result[0] = (axx & 0x80000000
		   | ax-rshift << 16
		   | r0 << rshift & 0xffff
		   | a1 >> 32-rshift & 0xffff);
      result[1] = a1 << rshift | a2 >> 32-rshift;
      result[2] = a2 << rshift | a3 >> 32-rshift;
      result[3] = a3 << rshift;
      return;
    }
  result[3] = 0;
  /* Special case for zero.  */
  if (a1 == 0 && a2 == 0 && a3 == 0)
    {
      result[0] = axx & 0x80000000;
      result[1] = result[2] = 0;
      return;
    }
  while (a1 != 0 && cntlzw(a2) >= 16 && 64 <= ax-1)
    {
      ax -= 32;
      a1 = a2; a2 = a3; a3 = 0;
    }
  rshift = cntlzw(a1)-15 + (-(cntlzw(a1) >> 5) & cntlzw(a2));
  assert(rshift < 32);
  if (rshift > ax-1-32)
    {
      ax--;
      rshift = ax-32;
    }
  
  result[0] = (axx & 0x80000000
	       | ax-rshift-32 << 16
	       | a1 << rshift & 0xffff
	       | a2 >> 32-rshift & 0xffff);
  result[1] = a2 << rshift | a3 >> 32-rshift;
  result[2] = a3 << rshift;
  return;
}

/* Handle the case where one or both numbers are denormalised or zero. 
   This case almost never happens, so we don't slow the main code
   with it.  */
static void
handle_min_exponent(unsigned result[4],
		    const unsigned a[4], const unsigned b[4],
		    const unsigned axx,  /* Treat as a[0].  */
		    const unsigned bxx,  /* Treat as b[0].  */
		    const unsigned ax,   /* axx >> 16 & 0x7fff.  */
		    const unsigned bx)   /* bxx >> 16 & 0x7fff.  */
{
  int ax_denorm, bx_denorm;
  unsigned a1,a2,a3, b1,b2,b3;
  int a_zeromant, b_zeromant;

  ax_denorm = ax == 0;
  bx_denorm = bx == 0;

  assert(ax_denorm || bx_denorm);

  a1 = a[1]; a2 = a[2]; a3 = a[3];
  b1 = b[1]; b2 = b[2]; b3 = b[3];
  

}

/* Add a+b+cin modulo 2^32, put result in 'r' and carry in 'cout'.  */
#define addc(r,cout,a,b,cin) \
  do { \
    unsigned long long addc_tmp = (a)+(b)+(cin);
    (cout) = addc_tmp >> 32;
    (r) = addc_tmp;
  }

/* Calculate a+~b+cin modulo 2^32, put result in 'r' and carry in 'cout'.  */
#define subc(r,cout,a,b,cin) \
  do { \
    unsigned long long addc_tmp = (a)-(b)+(cin)-1;
    (cout) = addc_tmp >> 63;
    (r) = addc_tmp;
  }

/* Handle the case where both exponents are the same.  This requires quite
   a different algorithm than the general case.  */
static void
handle_equal_exponents(unsigned result[4],
		       const unsigned a[4], const unsigned b[4],
		       const unsigned axx,  /* Treat as a[0].  */
		       const unsigned bxx,  /* Treat as b[0].  */
		       unsigned ax)         /* [ab]xx >> 16 & 0x7fff.  */
{
  unsigned a1,a2,a3, b1,b2,b3;
  int roundmode;
  unsigned carry, r0;

  a1 = a[1]; a2 = a[2]; a3 = a[3];
  b1 = b[1]; b2 = b[2]; b3 = b[3];

  if ((int)(axx ^ bxx) >= 0)
    {
      int roundmode;

      /* Adding.  */
      roundmode = fegetround();
  
      /* What about overflow?  */
      if (ax == 0x7ffe)
	{
	  /* Oh no!  Too big!  */
	  /* Result:
	     rounding result
	     -------- ------
	     nearest  return Inf with sign of a,b
	     zero     return nearest possible non-Inf value with
	              sign of a,b
	     +Inf     return +Inf if a,b>0, otherwise return
	              value just before -Inf.
	     -Inf     return +Inf if a,b>0, otherwise return
	              value just before -Inf.
	   */
	  set_fpscr_bit(FPSCR_OX);
	  /* Overflow always produces inexact result.  */
	  set_fpscr_bit(FPSCR_XX);

	  if (   roundmode == FE_TONEAREST
	      || roundmode == FE_UPWARD && (int)axx >= 0
	      || roundmode == FE_DOWNWARD && (int)axx < 0)
	    {
	      result[3] = result[2] = result[1] = 0;
	      result[0] = axx & 0xffff0000 | 0x7fff0000;
	    }
	  else
	    {
	      result[3] = result[2] = result[1] = 0xffffffff;
	      result[0] = axx & 0xfffe0000 | 0x7ffeffff;
	    }
	  return;
	}

      /* We need to worry about rounding/inexact here.  Do it like this: */
      if (a3 + b3  &  1)
	{
	  /* Need to round.  Upwards?  */
	  set_fpscr_bit(FPSCR_XX);
	  carry = (   roundmode == FE_NEAREST && (a3 + b3  &  2) != 0
		   || roundmode == FE_UPWARD && (int)axx >= 0
		   || roundmode == FE_DOWNWARD && (int)axx < 0);
	}
      else
	carry = 0; /* Result will be exact.  */

      /* Perform the addition.  */
      addc(a3,carry,a3,b3,carry);
      addc(a2,carry,a2,b2,carry);
      addc(a1,carry,a1,b1,carry);
      r0 = (axx & 0xffff) + (bxx & 0xffff) + carry;

      /* Shift right by 1.  */
      result[3] = a3 >> 1 | a2 << 31;
      result[2] = a2 >> 1 | a1 << 31;
      result[1] = a1 >> 1 | r0 << 31;
      /* Exponent of result is exponent of inputs plus 1.  
         Sign of result is common sign of inputs.  */
      result[0] = r0 >> 1 & 0xffff  |  axx + 0x10000 & 0xffff0000;
    }
  else
    {
      /* Subtracting.  */
      
      /* Perform the subtraction, a-b.  */
      subc(a3,carry,a3,b3,0);
      subc(a2,carry,a2,b2,carry);
      subc(a1,carry,a1,b1,carry);
      subc(r0,carry,a0&0xffff,b0&0xffff,carry);
      
      /* Maybe we should have calculated b-a... */
      if (carry)
	{
	  subc(a3,carry,0,a3,0);
	  subc(a2,carry,0,a2,carry);
	  subc(a1,carry,0,a1,carry);
	  subc(r0,carry,0,r0,carry);
	  axx ^= 0x80000000;
	}
      
      renormalise_value(result, axx, ax, r0, a1, a2, a3);
    }
}


static void
add(unsigned result[4], const unsigned a[4], const unsigned b[4],
    unsigned axx, unsigned bxx)
{
  int ax, bx, diff, carry;
  unsigned a0,a1,a2,a3, b0,b1,b2,b3,b4, sdiff;

  ax = axx >> 16  &  0x7fff;
  bx = bxx >> 16  &  0x7fff;

  /* Deal with NaNs and Inf.  */
  if (ax == 0x7fff || bx == 0x7fff)
    {
      handle_max_exponent(result, a, b, axx, bxx, ax, bx);
      return;
    }
  /* Deal with denorms and zero.  */
  if (ax == 0 || bx == 0)
    {
      handle_min_exponent(result, a, b, axx, bxx, ax, bx);
      return;
    }
  /* Finally, one special case, when both exponents are equal.  */
  if (ax == bx)
    {
      handle_equal_exponents(result, a, b, axx, bxx, ax);
      return;
    }

  sdiff = axx ^ bxx;
  /* Swap a and b if b has a larger magnitude than a, so that a will have
     the larger magnitude.  */
  if (ax < bx)
    {
      const unsigned *t;
      t = b; b = a; a = t;
      diff = bx - ax;
      ax = bx;
      axx = bxx;
    }
  else
    diff = ax - bx;

  a0 = a[0] & 0xffff | 0x10000; a1 = a[1]; a2 = a[2]; a3 = a[3];
  b0 = b[0] & 0xffff | 0x10000; b1 = b[1]; b2 = b[2]; b3 = b[3];
  if (diff < 32)
    {
      b4 = b3 << 32-diff;
      b3 = b3 >> diff | b2 << 32-biff;
      b2 = b2 >> diff | b1 << 32-diff;
      b1 = b1 >> diff | b0 << 32-diff;
      b0 = b0 >> diff;
    }
  else if (diff < 64)
    {
      diff -= 32;
      b4 = b3 & 1 | b3 >> (diff == 32) | b2 << 32-biff;
      b3 = b2 >> diff | b1 << 32-diff;
      b2 = b1 >> diff | b0 << 32-diff;
      b1 = b0 >> diff;
      b0 = 0;
    }
  else if (diff < 96)
    {
      b4 = b2 | b3 | b1 << 32-diff;
      b3 = b1 >> diff | b0 << 32-diff;
      b2 = b0 >> diff;
      b1 = b0 = 0;
    }
  else if (diff < 128)
    {
      b4 = b1 | b2 | b3 | b0 << 32-diff;
      b3 = b0 >> diff;
      b2 = b1 = b0 = 0;
    }
  else
    {
      b4 = b0|b1|b2|b3;
      b3 = b2 = b1 = b0 = 0;
    }

  /* Now, two cases: one for addition, one for subtraction.  */
  if ((int)sdiff >= 0)
    {
      /* Addition.  */

      /* 

      /* Perform the addition.  */
      addc(a3,carry,a3,b3,0);
      addc(a2,carry,a2,b2,carry);
      addc(a1,carry,a1,b1,carry);
      addc(a0,carry,a0,b0,carry);

      

      if (a0 & 0x20000)
	{
	  /* Need to renormalise by shifting right.  */
	  /* Shift right by 1.  */
	  b4 = b4 | a3 << 31;
	  a3 = a3 >> 1 | a2 << 31;
	  a2 = a2 >> 1 | a1 << 31;
	  result[1] = a1 >> 1 | r0 << 31;
	  /* Exponent of result is exponent of inputs plus 1.  
	     Sign of result is common sign of inputs.  */
	  result[0] = r0 >> 1 & 0xffff  |  axx + 0x10000 & 0xffff0000;
	}
      

    }
  else
    {
      /* Subtraction.  */
      
    }
}

/* Add the absolute values of two 128-bit floating point values,
   give the result the sign of one of them.  The only exception this
   can raise is for SNaN.  */
static void
aadd(unsigned result[4], const unsigned a[4], const unsigned b[4])
{
  unsigned ax, bx, xd;
  const unsigned *sml;
  unsigned t0,t1,t2,t3,tx, s0,s1,s2,s3,s4, carry;
  int rmode, xdelta, shift;

  ax = a[0] >> 16 & 0x7fff;
  bx = b[0] >> 16 & 0x7fff;
  
  /* Deal with .  */
  if (ax == 0x7fff)
    {
      t0 = a[0]; t1 = a[1]; t2 = a[2]; t3 = a[3];
      /* Check for SNaN.  */
      if ((t0 & 0x8000) == 0
	  && (t0 & 0x7fff  |  t1  |  t2  |  t3) != 0)
	set_fpscr_bit(FPSCR_VXSNAN);
      /* Return b.  */
      result[0] = t0; result[1] = t1; result[2] = t2; result[3] = t3;
      return;
    }
  /* Deal with b==Inf or b==NaN. */
  if (bx == 0x7fff)
    {
      t0 = b[0]; t1 = b[1]; t2 = b[2]; t3 = b[3];
      /* Check for SNaN.  */
      if ((t0 & 0x8000) == 0
	  && (t0 & 0x7fff  |  t1  |  t2  |  t3) != 0)
	set_fpscr_bit(FPSCR_VXSNAN);
      /* Return b.  */
      result[0] = t0; result[1] = t1; result[2] = t2; result[3] = t3;
      return;
    }

  /* Choose the larger of the two to be 't', and the smaller to be 's'.  */
  if (ax > bx)
    {
      t0 = a[0] & 0xffff  |  (ax != 0) << 16;
      t1 = a[1]; t2 = a[2]; t3 = a[3]; tx = ax;
      s0 = b[0] & 0xffff  |  (bx != 0) << 16;
      s1 = b[1]; s2 = b[2]; s3 = b[3];
      xd = ax-bx;
    }
  else
    {
      t0 = b[0] & 0xffff  |  (bx != 0) << 16;
      t1 = b[1]; t2 = b[2]; t3 = b[3]; tx = bx;
      s0 = a[0] & 0xffff  |  (ax != 0) << 16;
      s1 = a[1]; s2 = a[2]; s3 = a[3];
      sml = a;
      xd = bx-ax;
    }

  /* Shift 's2' right by 'xd' bits. */
  switch (xd >> 5)
    {
    case 0:
      s4 = 0;
      break;
    case 1:
      s4 = s3; s3 = s2; s2 = s1; s1 = s0; s0 = 0;
      break;
    case 2:
      s4 = s2  |  s3 != 0;
      s3 = s1; s2 = s0; s1 = s0 = 0;
      break;
    case 3:
      s4 = s1  |  (s3|s2) != 0;
      s3 = s0; s2 = s1 = s0 = 0;
      break;
    default:
      s4 = s0  |  (s3|s2|s1) != 0;
      s3 = s2 = s1 = s0 = 0;
    }
  xd = xd & 0x1f;
  if (xd != 0)
    {
      s4 = s4 >> xd  |  (s4 << 32-xd) != 0  |  s3 << 32-xd;
      s3 = s3 >> xd  |  s2 << 32-xd;
      s2 = s2 >> xd  |  s1 << 32-xd;
      s1 = s1 >> xd  |  s0 << 32-xd;
      s0 = s0 >> xd;
    }

  /* Do the addition.  */
#define addc(r,cout,a,b,cin) \
  do { \
    unsigned long long addc_tmp = (a)+(b)+(cin);
    (cout) = addc_tmp >> 32;
    (r) = addc_tmp;
  }
  addc(t3,carry,t3,s3,0);
  addc(t2,carry,t2,s2,carry);
  addc(t1,carry,t1,s1,carry);
  t0 = t0 + s0 + carry;
  
  /* Renormalise.  */
  xdelta = 15-cntlzw(t0);
  if (tx + xdelta <= 0x7fff)
    shift = xdelta;
  else
    {
    }
}

/* Add two 128-bit floating point values.  */
void
__q_add(unsigned result[4], const unsigned a[4], const unsigned b[4])
{
  if ((a[0] ^ b[0]) >= 0)
    aadd(result, a, b);
  else
    asubtract(result, a, b);
}

/* Subtract two 128-bit floating point values.  */
void
__q_sub(unsigned result[4], const unsigned a[4], const unsigned b[4])
{
  if ((a[0] ^ b[0]) < 0)
    aadd(result, a, b);
  else
    asubtract(result, a, b);
}