blob: 66111db5cce8df2b744733b3fc3a98f757fca468 [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"
Daniel Dunbarddf18902011-11-16 07:33:00 +000017#include "int_math.h"
Daniel Dunbarfd089992009-06-26 16:47:03 +000018#include <math.h>
Daniel Dunbarfd089992009-06-26 16:47:03 +000019
Edward O'Callaghan55836322009-08-07 20:30:09 +000020/* Returns: the quotient of (a + ib) / (c + id) */
Daniel Dunbarfd089992009-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)));
Daniel Dunbarddf18902011-11-16 07:33:00 +000027 if (crt_isfinite(__logbw))
Daniel Dunbarfd089992009-06-26 16:47:03 +000028 {
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);
Daniel Dunbarddf18902011-11-16 07:33:00 +000037 if (crt_isnan(__real__ z) && crt_isnan(__imag__ z))
Daniel Dunbarfd089992009-06-26 16:47:03 +000038 {
Daniel Dunbarddf18902011-11-16 07:33:00 +000039 if ((__denom == 0) && (!crt_isnan(__a) || !crt_isnan(__b)))
Daniel Dunbarfd089992009-06-26 16:47:03 +000040 {
41 __real__ z = copysignl(INFINITY, __c) * __a;
42 __imag__ z = copysignl(INFINITY, __c) * __b;
43 }
Daniel Dunbarddf18902011-11-16 07:33:00 +000044 else if ((crt_isinf(__a) || crt_isinf(__b)) &&
45 crt_isfinite(__c) && crt_isfinite(__d))
Daniel Dunbarfd089992009-06-26 16:47:03 +000046 {
Daniel Dunbarddf18902011-11-16 07:33:00 +000047 __a = copysignl(crt_isinf(__a) ? 1 : 0, __a);
48 __b = copysignl(crt_isinf(__b) ? 1 : 0, __b);
Daniel Dunbarfd089992009-06-26 16:47:03 +000049 __real__ z = INFINITY * (__a * __c + __b * __d);
50 __imag__ z = INFINITY * (__b * __c - __a * __d);
51 }
Daniel Dunbarddf18902011-11-16 07:33:00 +000052 else if (crt_isinf(__logbw) && __logbw > 0 &&
53 crt_isfinite(__a) && crt_isfinite(__b))
Daniel Dunbarfd089992009-06-26 16:47:03 +000054 {
Daniel Dunbarddf18902011-11-16 07:33:00 +000055 __c = copysignl(crt_isinf(__c) ? 1 : 0, __c);
56 __d = copysignl(crt_isinf(__d) ? 1 : 0, __d);
Daniel Dunbarfd089992009-06-26 16:47:03 +000057 __real__ z = 0 * (__a * __c + __b * __d);
58 __imag__ z = 0 * (__b * __c - __a * __d);
59 }
60 }
61 return z;
62}
63
64#endif