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