Home | History | Annotate | Line # | Download | only in builtins
      1  1.1  joerg /*===-- divtc3.c - Implement __divtc3 -------------------------------------===
      2  1.1  joerg  *
      3  1.1  joerg  *                     The LLVM Compiler Infrastructure
      4  1.1  joerg  *
      5  1.1  joerg  * This file is dual licensed under the MIT and the University of Illinois Open
      6  1.1  joerg  * Source Licenses. See LICENSE.TXT for details.
      7  1.1  joerg  *
      8  1.1  joerg  * ===----------------------------------------------------------------------===
      9  1.1  joerg  *
     10  1.1  joerg  * This file implements __divtc3 for the compiler_rt library.
     11  1.1  joerg  *
     12  1.1  joerg  *===----------------------------------------------------------------------===
     13  1.1  joerg  */
     14  1.1  joerg 
     15  1.1  joerg #include "int_lib.h"
     16  1.1  joerg #include "int_math.h"
     17  1.1  joerg 
     18  1.1  joerg /* Returns: the quotient of (a + ib) / (c + id) */
     19  1.1  joerg 
     20  1.1  joerg COMPILER_RT_ABI long double _Complex
     21  1.1  joerg __divtc3(long double __a, long double __b, long double __c, long double __d)
     22  1.1  joerg {
     23  1.1  joerg     int __ilogbw = 0;
     24  1.1  joerg     long double __logbw = crt_logbl(crt_fmaxl(crt_fabsl(__c), crt_fabsl(__d)));
     25  1.1  joerg     if (crt_isfinite(__logbw))
     26  1.1  joerg     {
     27  1.1  joerg         __ilogbw = (int)__logbw;
     28  1.1  joerg         __c = crt_scalbnl(__c, -__ilogbw);
     29  1.1  joerg         __d = crt_scalbnl(__d, -__ilogbw);
     30  1.1  joerg     }
     31  1.1  joerg     long double __denom = __c * __c + __d * __d;
     32  1.1  joerg     long double _Complex z;
     33  1.1  joerg     __real__ z = crt_scalbnl((__a * __c + __b * __d) / __denom, -__ilogbw);
     34  1.1  joerg     __imag__ z = crt_scalbnl((__b * __c - __a * __d) / __denom, -__ilogbw);
     35  1.1  joerg     if (crt_isnan(__real__ z) && crt_isnan(__imag__ z))
     36  1.1  joerg     {
     37  1.1  joerg         if ((__denom == 0.0) && (!crt_isnan(__a) || !crt_isnan(__b)))
     38  1.1  joerg         {
     39  1.1  joerg             __real__ z = crt_copysignl(CRT_INFINITY, __c) * __a;
     40  1.1  joerg             __imag__ z = crt_copysignl(CRT_INFINITY, __c) * __b;
     41  1.1  joerg         }
     42  1.1  joerg         else if ((crt_isinf(__a) || crt_isinf(__b)) &&
     43  1.1  joerg                  crt_isfinite(__c) && crt_isfinite(__d))
     44  1.1  joerg         {
     45  1.1  joerg             __a = crt_copysignl(crt_isinf(__a) ? 1.0 : 0.0, __a);
     46  1.1  joerg             __b = crt_copysignl(crt_isinf(__b) ? 1.0 : 0.0, __b);
     47  1.1  joerg             __real__ z = CRT_INFINITY * (__a * __c + __b * __d);
     48  1.1  joerg             __imag__ z = CRT_INFINITY * (__b * __c - __a * __d);
     49  1.1  joerg         }
     50  1.1  joerg         else if (crt_isinf(__logbw) && __logbw > 0.0 &&
     51  1.1  joerg                  crt_isfinite(__a) && crt_isfinite(__b))
     52  1.1  joerg         {
     53  1.1  joerg             __c = crt_copysignl(crt_isinf(__c) ? 1.0 : 0.0, __c);
     54  1.1  joerg             __d = crt_copysignl(crt_isinf(__d) ? 1.0 : 0.0, __d);
     55  1.1  joerg             __real__ z = 0.0 * (__a * __c + __b * __d);
     56  1.1  joerg             __imag__ z = 0.0 * (__b * __c - __a * __d);
     57  1.1  joerg         }
     58  1.1  joerg     }
     59  1.1  joerg     return z;
     60  1.1  joerg }
     61