blob: 03357ae552c3482255593bb130d7c94eaf69a8c7 [file] [log] [blame]
Edward O'Callaghan37a6a452009-08-07 20:30:09 +00001/* ===-- divxc3.c - Implement __divxc3 -------------------------------------===
2 *
3 * The LLVM Compiler Infrastructure
4 *
5 * This file is distributed under the University of Illinois Open Source
6 * License. See LICENSE.TXT for details.
7 *
8 * ===----------------------------------------------------------------------===
9 *
10 * This file implements __divxc3 for the compiler_rt library.
11 *
12 */
Daniel Dunbarb3a69012009-06-26 16:47:03 +000013
14#if !_ARCH_PPC
15
16#include "int_lib.h"
17#include <math.h>
18#include <complex.h>
19
Edward O'Callaghan37a6a452009-08-07 20:30:09 +000020/* Returns: the quotient of (a + ib) / (c + id) */
Daniel Dunbarb3a69012009-06-26 16:47:03 +000021
22long double _Complex
23__divxc3(long double __a, long double __b, long double __c, long double __d)
24{
25 int __ilogbw = 0;
26 long double __logbw = logbl(fmaxl(fabsl(__c), fabsl(__d)));
27 if (isfinite(__logbw))
28 {
29 __ilogbw = (int)__logbw;
30 __c = scalbnl(__c, -__ilogbw);
31 __d = scalbnl(__d, -__ilogbw);
32 }
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))
38 {
39 if ((__denom == 0) && (!isnan(__a) || !isnan(__b)))
40 {
41 __real__ z = copysignl(INFINITY, __c) * __a;
42 __imag__ z = copysignl(INFINITY, __c) * __b;
43 }
44 else if ((isinf(__a) || isinf(__b)) && isfinite(__c) && isfinite(__d))
45 {
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);
50 }
51 else if (isinf(__logbw) && __logbw > 0 && isfinite(__a) && isfinite(__b))
52 {
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);
57 }
58 }
59 return z;
60}
61
62#endif