1 /* ===-- divxc3.c - Implement __divxc3 -------------------------------------===
3 * The LLVM Compiler Infrastructure
5 * This file is dual licensed under the MIT and the University of Illinois Open
6 * Source Licenses. See LICENSE.TXT for details.
8 * ===----------------------------------------------------------------------===
10 * This file implements __divxc3 for the compiler_rt library.
20 /* Returns: the quotient of (a + ib) / (c + id) */
23 __divxc3(long double __a, long double __b, long double __c, long double __d)
26 long double __logbw = logbl(fmaxl(fabsl(__c), fabsl(__d)));
27 if (isfinite(__logbw))
29 __ilogbw = (int)__logbw;
30 __c = scalbnl(__c, -__ilogbw);
31 __d = scalbnl(__d, -__ilogbw);
33 long double __denom = __c * __c + __d * __d;
34 long double _Complex z;
35 __real__ z = scalbnl((__a * __c + __b * __d) / __denom, -__ilogbw);
36 __imag__ z = scalbnl((__b * __c - __a * __d) / __denom, -__ilogbw);
37 if (isnan(__real__ z) && isnan(__imag__ z))
39 if ((__denom == 0) && (!isnan(__a) || !isnan(__b)))
41 __real__ z = copysignl(INFINITY, __c) * __a;
42 __imag__ z = copysignl(INFINITY, __c) * __b;
44 else if ((isinf(__a) || isinf(__b)) && isfinite(__c) && isfinite(__d))
46 __a = copysignl(isinf(__a) ? 1 : 0, __a);
47 __b = copysignl(isinf(__b) ? 1 : 0, __b);
48 __real__ z = INFINITY * (__a * __c + __b * __d);
49 __imag__ z = INFINITY * (__b * __c - __a * __d);
51 else if (isinf(__logbw) && __logbw > 0 && isfinite(__a) && isfinite(__b))
53 __c = copysignl(isinf(__c) ? 1 : 0, __c);
54 __d = copysignl(isinf(__d) ? 1 : 0, __d);
55 __real__ z = 0 * (__a * __c + __b * __d);
56 __imag__ z = 0 * (__b * __c - __a * __d);