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