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