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