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" |
Edward O'Callaghan | 33c1347 | 2009-08-08 04:43:56 +0000 | [diff] [blame] | 7 | |
Daniel Dunbar | 8603024 | 2011-11-16 07:33:00 +0000 | [diff] [blame] | 8 | #define makeFinite(x) { \ |
Daniel Dunbar | c25c6d1 | 2011-11-16 07:33:06 +0000 | [diff] [blame] | 9 | (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] | 10 | (x).s.lo = 0.0; \ |
| 11 | } |
Daniel Dunbar | b3a6901 | 2009-06-26 16:47:03 +0000 | [diff] [blame] | 12 | |
Daniel Dunbar | eaff95c | 2011-11-16 18:56:53 +0000 | [diff] [blame] | 13 | #define zeroNaN(x) { \ |
Daniel Dunbar | 8603024 | 2011-11-16 07:33:00 +0000 | [diff] [blame] | 14 | if (crt_isnan((x).s.hi)) { \ |
Daniel Dunbar | c25c6d1 | 2011-11-16 07:33:06 +0000 | [diff] [blame] | 15 | (x).s.hi = crt_copysign(0.0, (x).s.hi); \ |
Daniel Dunbar | 8603024 | 2011-11-16 07:33:00 +0000 | [diff] [blame] | 16 | (x).s.lo = 0.0; \ |
| 17 | } \ |
| 18 | } |
Daniel Dunbar | b3a6901 | 2009-06-26 16:47:03 +0000 | [diff] [blame] | 19 | |
Daniel Dunbar | b3a6901 | 2009-06-26 16:47:03 +0000 | [diff] [blame] | 20 | long double _Complex |
| 21 | __multc3(long double a, long double b, long double c, long double d) |
| 22 | { |
| 23 | long double ac = __gcc_qmul(a,c); |
| 24 | long double bd = __gcc_qmul(b,d); |
| 25 | long double ad = __gcc_qmul(a,d); |
| 26 | long double bc = __gcc_qmul(b,c); |
| 27 | |
| 28 | DD real = { .ld = __gcc_qsub(ac,bd) }; |
| 29 | DD imag = { .ld = __gcc_qadd(ad,bc) }; |
| 30 | |
Daniel Dunbar | 8603024 | 2011-11-16 07:33:00 +0000 | [diff] [blame] | 31 | if (crt_isnan(real.s.hi) && crt_isnan(imag.s.hi)) |
Daniel Dunbar | b3a6901 | 2009-06-26 16:47:03 +0000 | [diff] [blame] | 32 | { |
| 33 | int recalc = 0; |
| 34 | |
| 35 | DD aDD = { .ld = a }; |
| 36 | DD bDD = { .ld = b }; |
| 37 | DD cDD = { .ld = c }; |
| 38 | DD dDD = { .ld = d }; |
| 39 | |
Daniel Dunbar | 8603024 | 2011-11-16 07:33:00 +0000 | [diff] [blame] | 40 | if (crt_isinf(aDD.s.hi) || crt_isinf(bDD.s.hi)) |
Daniel Dunbar | b3a6901 | 2009-06-26 16:47:03 +0000 | [diff] [blame] | 41 | { |
| 42 | makeFinite(aDD); |
| 43 | makeFinite(bDD); |
| 44 | zeroNaN(cDD); |
| 45 | zeroNaN(dDD); |
| 46 | recalc = 1; |
| 47 | } |
| 48 | |
Daniel Dunbar | 8603024 | 2011-11-16 07:33:00 +0000 | [diff] [blame] | 49 | if (crt_isinf(cDD.s.hi) || crt_isinf(dDD.s.hi)) |
Daniel Dunbar | b3a6901 | 2009-06-26 16:47:03 +0000 | [diff] [blame] | 50 | { |
| 51 | makeFinite(cDD); |
| 52 | makeFinite(dDD); |
| 53 | zeroNaN(aDD); |
| 54 | zeroNaN(bDD); |
| 55 | recalc = 1; |
| 56 | } |
| 57 | |
| 58 | if (!recalc) |
| 59 | { |
| 60 | DD acDD = { .ld = ac }; |
| 61 | DD bdDD = { .ld = bd }; |
| 62 | DD adDD = { .ld = ad }; |
| 63 | DD bcDD = { .ld = bc }; |
| 64 | |
Daniel Dunbar | 8603024 | 2011-11-16 07:33:00 +0000 | [diff] [blame] | 65 | if (crt_isinf(acDD.s.hi) || crt_isinf(bdDD.s.hi) || |
| 66 | crt_isinf(adDD.s.hi) || crt_isinf(bcDD.s.hi)) |
Daniel Dunbar | b3a6901 | 2009-06-26 16:47:03 +0000 | [diff] [blame] | 67 | { |
| 68 | zeroNaN(aDD); |
| 69 | zeroNaN(bDD); |
| 70 | zeroNaN(cDD); |
| 71 | zeroNaN(dDD); |
| 72 | recalc = 1; |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | if (recalc) |
| 77 | { |
Daniel Dunbar | c25c6d1 | 2011-11-16 07:33:06 +0000 | [diff] [blame] | 78 | 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] | 79 | real.s.lo = 0.0; |
Daniel Dunbar | c25c6d1 | 2011-11-16 07:33:06 +0000 | [diff] [blame] | 80 | imag.s.hi = CRT_INFINITY * (aDD.s.hi*dDD.s.hi + bDD.s.hi*cDD.s.hi); |
Edward O'Callaghan | 8bf1e09 | 2009-08-09 18:41:02 +0000 | [diff] [blame] | 81 | imag.s.lo = 0.0; |
Daniel Dunbar | b3a6901 | 2009-06-26 16:47:03 +0000 | [diff] [blame] | 82 | } |
| 83 | } |
| 84 | |
| 85 | long double _Complex z; |
| 86 | __real__ z = real.ld; |
| 87 | __imag__ z = imag.ld; |
| 88 | |
| 89 | return z; |
| 90 | } |