Edward O'Callaghan | 33c1347 | 2009-08-08 04:43:56 +0000 | [diff] [blame] | 1 | /* This file is distributed under the University of Illinois Open Source |
| 2 | * License. See LICENSE.TXT for details. |
| 3 | */ |
Daniel Dunbar | b3a6901 | 2009-06-26 16:47:03 +0000 | [diff] [blame] | 4 | |
| 5 | #include "DD.h" |
Daniel Dunbar | 8603024 | 2011-11-16 07:33:00 +0000 | [diff] [blame] | 6 | #include "../int_math.h" |
Daniel Dunbar | b3a6901 | 2009-06-26 16:47:03 +0000 | [diff] [blame] | 7 | |
Daniel Dunbar | c25c6d1 | 2011-11-16 07:33:06 +0000 | [diff] [blame] | 8 | #if !defined(CRT_INFINITY) && defined(HUGE_VAL) |
| 9 | #define CRT_INFINITY HUGE_VAL |
| 10 | #endif /* CRT_INFINITY */ |
Edward O'Callaghan | 33c1347 | 2009-08-08 04:43:56 +0000 | [diff] [blame] | 11 | |
Daniel Dunbar | 8603024 | 2011-11-16 07:33:00 +0000 | [diff] [blame] | 12 | #define makeFinite(x) { \ |
Daniel Dunbar | c25c6d1 | 2011-11-16 07:33:06 +0000 | [diff] [blame] | 13 | (x).s.hi = crt_copysign(crt_isinf((x).s.hi) ? 1.0 : 0.0, (x).s.hi); \ |
Daniel Dunbar | 8603024 | 2011-11-16 07:33:00 +0000 | [diff] [blame] | 14 | (x).s.lo = 0.0; \ |
| 15 | } |
Daniel Dunbar | b3a6901 | 2009-06-26 16:47:03 +0000 | [diff] [blame] | 16 | |
| 17 | long double __gcc_qadd(long double, long double); |
| 18 | long double __gcc_qsub(long double, long double); |
| 19 | long double __gcc_qmul(long double, long double); |
| 20 | long double __gcc_qdiv(long double, long double); |
| 21 | |
| 22 | long double _Complex |
| 23 | __divtc3(long double a, long double b, long double c, long double d) |
| 24 | { |
| 25 | DD cDD = { .ld = c }; |
| 26 | DD dDD = { .ld = d }; |
| 27 | |
| 28 | int ilogbw = 0; |
Daniel Dunbar | c25c6d1 | 2011-11-16 07:33:06 +0000 | [diff] [blame] | 29 | const double logbw = crt_logb(crt_fmax(crt_fabs(cDD.s.hi), crt_fabs(dDD.s.hi) )); |
Daniel Dunbar | b3a6901 | 2009-06-26 16:47:03 +0000 | [diff] [blame] | 30 | |
Daniel Dunbar | 8603024 | 2011-11-16 07:33:00 +0000 | [diff] [blame] | 31 | if (crt_isfinite(logbw)) |
Daniel Dunbar | b3a6901 | 2009-06-26 16:47:03 +0000 | [diff] [blame] | 32 | { |
| 33 | ilogbw = (int)logbw; |
| 34 | |
Daniel Dunbar | c25c6d1 | 2011-11-16 07:33:06 +0000 | [diff] [blame] | 35 | cDD.s.hi = crt_scalbn(cDD.s.hi, -ilogbw); |
| 36 | cDD.s.lo = crt_scalbn(cDD.s.lo, -ilogbw); |
| 37 | dDD.s.hi = crt_scalbn(dDD.s.hi, -ilogbw); |
| 38 | dDD.s.lo = crt_scalbn(dDD.s.lo, -ilogbw); |
Daniel Dunbar | b3a6901 | 2009-06-26 16:47:03 +0000 | [diff] [blame] | 39 | } |
| 40 | |
| 41 | const long double denom = __gcc_qadd(__gcc_qmul(cDD.ld, cDD.ld), __gcc_qmul(dDD.ld, dDD.ld)); |
| 42 | const long double realNumerator = __gcc_qadd(__gcc_qmul(a,cDD.ld), __gcc_qmul(b,dDD.ld)); |
| 43 | const long double imagNumerator = __gcc_qsub(__gcc_qmul(b,cDD.ld), __gcc_qmul(a,dDD.ld)); |
| 44 | |
| 45 | DD real = { .ld = __gcc_qdiv(realNumerator, denom) }; |
| 46 | DD imag = { .ld = __gcc_qdiv(imagNumerator, denom) }; |
| 47 | |
Daniel Dunbar | c25c6d1 | 2011-11-16 07:33:06 +0000 | [diff] [blame] | 48 | real.s.hi = crt_scalbn(real.s.hi, -ilogbw); |
| 49 | real.s.lo = crt_scalbn(real.s.lo, -ilogbw); |
| 50 | imag.s.hi = crt_scalbn(imag.s.hi, -ilogbw); |
| 51 | imag.s.lo = crt_scalbn(imag.s.lo, -ilogbw); |
Daniel Dunbar | b3a6901 | 2009-06-26 16:47:03 +0000 | [diff] [blame] | 52 | |
Daniel Dunbar | 8603024 | 2011-11-16 07:33:00 +0000 | [diff] [blame] | 53 | if (crt_isnan(real.s.hi) && crt_isnan(imag.s.hi)) |
Daniel Dunbar | b3a6901 | 2009-06-26 16:47:03 +0000 | [diff] [blame] | 54 | { |
| 55 | DD aDD = { .ld = a }; |
| 56 | DD bDD = { .ld = b }; |
| 57 | DD rDD = { .ld = denom }; |
| 58 | |
Daniel Dunbar | 8603024 | 2011-11-16 07:33:00 +0000 | [diff] [blame] | 59 | if ((rDD.s.hi == 0.0) && (!crt_isnan(aDD.s.hi) || |
| 60 | !crt_isnan(bDD.s.hi))) |
Daniel Dunbar | b3a6901 | 2009-06-26 16:47:03 +0000 | [diff] [blame] | 61 | { |
Daniel Dunbar | c25c6d1 | 2011-11-16 07:33:06 +0000 | [diff] [blame] | 62 | real.s.hi = crt_copysign(CRT_INFINITY,cDD.s.hi) * aDD.s.hi; |
Edward O'Callaghan | 8bf1e09 | 2009-08-09 18:41:02 +0000 | [diff] [blame] | 63 | real.s.lo = 0.0; |
Daniel Dunbar | c25c6d1 | 2011-11-16 07:33:06 +0000 | [diff] [blame] | 64 | imag.s.hi = crt_copysign(CRT_INFINITY,cDD.s.hi) * bDD.s.hi; |
Edward O'Callaghan | 8bf1e09 | 2009-08-09 18:41:02 +0000 | [diff] [blame] | 65 | imag.s.lo = 0.0; |
Daniel Dunbar | b3a6901 | 2009-06-26 16:47:03 +0000 | [diff] [blame] | 66 | } |
| 67 | |
Daniel Dunbar | 8603024 | 2011-11-16 07:33:00 +0000 | [diff] [blame] | 68 | else if ((crt_isinf(aDD.s.hi) || crt_isinf(bDD.s.hi)) && |
| 69 | crt_isfinite(cDD.s.hi) && crt_isfinite(dDD.s.hi)) |
Daniel Dunbar | b3a6901 | 2009-06-26 16:47:03 +0000 | [diff] [blame] | 70 | { |
| 71 | makeFinite(aDD); |
| 72 | makeFinite(bDD); |
Daniel Dunbar | c25c6d1 | 2011-11-16 07:33:06 +0000 | [diff] [blame] | 73 | real.s.hi = CRT_INFINITY * (aDD.s.hi*cDD.s.hi + bDD.s.hi*dDD.s.hi); |
Edward O'Callaghan | 8bf1e09 | 2009-08-09 18:41:02 +0000 | [diff] [blame] | 74 | real.s.lo = 0.0; |
Daniel Dunbar | c25c6d1 | 2011-11-16 07:33:06 +0000 | [diff] [blame] | 75 | imag.s.hi = CRT_INFINITY * (bDD.s.hi*cDD.s.hi - aDD.s.hi*dDD.s.hi); |
Edward O'Callaghan | 8bf1e09 | 2009-08-09 18:41:02 +0000 | [diff] [blame] | 76 | imag.s.lo = 0.0; |
Daniel Dunbar | b3a6901 | 2009-06-26 16:47:03 +0000 | [diff] [blame] | 77 | } |
| 78 | |
Daniel Dunbar | 8603024 | 2011-11-16 07:33:00 +0000 | [diff] [blame] | 79 | else if ((crt_isinf(cDD.s.hi) || crt_isinf(dDD.s.hi)) && |
| 80 | crt_isfinite(aDD.s.hi) && crt_isfinite(bDD.s.hi)) |
Daniel Dunbar | b3a6901 | 2009-06-26 16:47:03 +0000 | [diff] [blame] | 81 | { |
| 82 | makeFinite(cDD); |
| 83 | makeFinite(dDD); |
Daniel Dunbar | c25c6d1 | 2011-11-16 07:33:06 +0000 | [diff] [blame] | 84 | real.s.hi = crt_copysign(0.0,(aDD.s.hi*cDD.s.hi + bDD.s.hi*dDD.s.hi)); |
Edward O'Callaghan | 8bf1e09 | 2009-08-09 18:41:02 +0000 | [diff] [blame] | 85 | real.s.lo = 0.0; |
Daniel Dunbar | c25c6d1 | 2011-11-16 07:33:06 +0000 | [diff] [blame] | 86 | imag.s.hi = crt_copysign(0.0,(bDD.s.hi*cDD.s.hi - aDD.s.hi*dDD.s.hi)); |
Edward O'Callaghan | 8bf1e09 | 2009-08-09 18:41:02 +0000 | [diff] [blame] | 87 | imag.s.lo = 0.0; |
Daniel Dunbar | b3a6901 | 2009-06-26 16:47:03 +0000 | [diff] [blame] | 88 | } |
| 89 | } |
| 90 | |
| 91 | long double _Complex z; |
| 92 | __real__ z = real.ld; |
| 93 | __imag__ z = imag.ld; |
| 94 | |
| 95 | return z; |
| 96 | } |