blob: 392d6ecacde1ba6fd9df8d585e2fccd07fa30c2a [file] [log] [blame]
Edward O'Callaghan55836322009-08-07 20:30:09 +00001/* ===-- divdc3.c - Implement __divdc3 -------------------------------------===
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 __divdc3 for the compiler_rt library.
11 *
12 * ===----------------------------------------------------------------------===
13 */
Daniel Dunbarfd089992009-06-26 16:47:03 +000014
Jordan Rupprechtdc48c4f2018-09-24 20:39:19 +000015#define DOUBLE_PRECISION
16#include "fp_lib.h"
Daniel Dunbarfd089992009-06-26 16:47:03 +000017#include "int_lib.h"
Daniel Dunbarddf18902011-11-16 07:33:00 +000018#include "int_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
Saleem Abdulrasoolb0b7c8a2015-10-07 02:58:07 +000022COMPILER_RT_ABI Dcomplex
Daniel Dunbarfd089992009-06-26 16:47:03 +000023__divdc3(double __a, double __b, double __c, double __d)
24{
25 int __ilogbw = 0;
Jordan Rupprechtdc48c4f2018-09-24 20:39:19 +000026 double __logbw = __compiler_rt_logb(crt_fmax(crt_fabs(__c), crt_fabs(__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;
Daniel Dunbar2139c522011-11-16 07:33:06 +000030 __c = crt_scalbn(__c, -__ilogbw);
31 __d = crt_scalbn(__d, -__ilogbw);
Daniel Dunbarfd089992009-06-26 16:47:03 +000032 }
33 double __denom = __c * __c + __d * __d;
Saleem Abdulrasoolb0b7c8a2015-10-07 02:58:07 +000034 Dcomplex z;
35 COMPLEX_REAL(z) = crt_scalbn((__a * __c + __b * __d) / __denom, -__ilogbw);
36 COMPLEX_IMAGINARY(z) = crt_scalbn((__b * __c - __a * __d) / __denom, -__ilogbw);
37 if (crt_isnan(COMPLEX_REAL(z)) && crt_isnan(COMPLEX_IMAGINARY(z)))
Daniel Dunbarfd089992009-06-26 16:47:03 +000038 {
Daniel Dunbarddf18902011-11-16 07:33:00 +000039 if ((__denom == 0.0) && (!crt_isnan(__a) || !crt_isnan(__b)))
Daniel Dunbarfd089992009-06-26 16:47:03 +000040 {
Saleem Abdulrasoolb0b7c8a2015-10-07 02:58:07 +000041 COMPLEX_REAL(z) = crt_copysign(CRT_INFINITY, __c) * __a;
42 COMPLEX_IMAGINARY(z) = crt_copysign(CRT_INFINITY, __c) * __b;
Daniel Dunbarfd089992009-06-26 16:47:03 +000043 }
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 Dunbar2139c522011-11-16 07:33:06 +000047 __a = crt_copysign(crt_isinf(__a) ? 1.0 : 0.0, __a);
48 __b = crt_copysign(crt_isinf(__b) ? 1.0 : 0.0, __b);
Saleem Abdulrasoolb0b7c8a2015-10-07 02:58:07 +000049 COMPLEX_REAL(z) = CRT_INFINITY * (__a * __c + __b * __d);
50 COMPLEX_IMAGINARY(z) = CRT_INFINITY * (__b * __c - __a * __d);
Daniel Dunbarfd089992009-06-26 16:47:03 +000051 }
Daniel Dunbarddf18902011-11-16 07:33:00 +000052 else if (crt_isinf(__logbw) && __logbw > 0.0 &&
53 crt_isfinite(__a) && crt_isfinite(__b))
Daniel Dunbarfd089992009-06-26 16:47:03 +000054 {
Daniel Dunbar2139c522011-11-16 07:33:06 +000055 __c = crt_copysign(crt_isinf(__c) ? 1.0 : 0.0, __c);
56 __d = crt_copysign(crt_isinf(__d) ? 1.0 : 0.0, __d);
Saleem Abdulrasoolb0b7c8a2015-10-07 02:58:07 +000057 COMPLEX_REAL(z) = 0.0 * (__a * __c + __b * __d);
58 COMPLEX_IMAGINARY(z) = 0.0 * (__b * __c - __a * __d);
Daniel Dunbarfd089992009-06-26 16:47:03 +000059 }
60 }
61 return z;
62}