blob: 32364d62018de226733a47fd7a1e8be2c950b50f [file] [log] [blame]
Edward O'Callaghan55836322009-08-07 20:30:09 +00001/* ===-- divxc3.c - Implement __divxc3 -------------------------------------===
2 *
3 * The LLVM Compiler Infrastructure
4 *
Howard Hinnant5b791f62010-11-16 22:13:33 +00005 * This file is dual licensed under the MIT and the University of Illinois Open
6 * Source Licenses. See LICENSE.TXT for details.
Edward O'Callaghan55836322009-08-07 20:30:09 +00007 *
8 * ===----------------------------------------------------------------------===
9 *
10 * This file implements __divxc3 for the compiler_rt library.
11 *
12 */
Daniel Dunbarfd089992009-06-26 16:47:03 +000013
14#if !_ARCH_PPC
15
16#include "int_lib.h"
17#include <math.h>
Daniel Dunbarfd089992009-06-26 16:47:03 +000018
Edward O'Callaghan55836322009-08-07 20:30:09 +000019/* Returns: the quotient of (a + ib) / (c + id) */
Daniel Dunbarfd089992009-06-26 16:47:03 +000020
21long double _Complex
22__divxc3(long double __a, long double __b, long double __c, long double __d)
23{
24 int __ilogbw = 0;
25 long double __logbw = logbl(fmaxl(fabsl(__c), fabsl(__d)));
26 if (isfinite(__logbw))
27 {
28 __ilogbw = (int)__logbw;
29 __c = scalbnl(__c, -__ilogbw);
30 __d = scalbnl(__d, -__ilogbw);
31 }
32 long double __denom = __c * __c + __d * __d;
33 long double _Complex z;
34 __real__ z = scalbnl((__a * __c + __b * __d) / __denom, -__ilogbw);
35 __imag__ z = scalbnl((__b * __c - __a * __d) / __denom, -__ilogbw);
36 if (isnan(__real__ z) && isnan(__imag__ z))
37 {
38 if ((__denom == 0) && (!isnan(__a) || !isnan(__b)))
39 {
40 __real__ z = copysignl(INFINITY, __c) * __a;
41 __imag__ z = copysignl(INFINITY, __c) * __b;
42 }
43 else if ((isinf(__a) || isinf(__b)) && isfinite(__c) && isfinite(__d))
44 {
45 __a = copysignl(isinf(__a) ? 1 : 0, __a);
46 __b = copysignl(isinf(__b) ? 1 : 0, __b);
47 __real__ z = INFINITY * (__a * __c + __b * __d);
48 __imag__ z = INFINITY * (__b * __c - __a * __d);
49 }
50 else if (isinf(__logbw) && __logbw > 0 && isfinite(__a) && isfinite(__b))
51 {
52 __c = copysignl(isinf(__c) ? 1 : 0, __c);
53 __d = copysignl(isinf(__d) ? 1 : 0, __d);
54 __real__ z = 0 * (__a * __c + __b * __d);
55 __imag__ z = 0 * (__b * __c - __a * __d);
56 }
57 }
58 return z;
59}
60
61#endif