Home | History | Annotate | Line # | Download | only in builtins
      1      1.1  joerg /*===-- divsc3.c - Implement __divsc3 -------------------------------------===
      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 __divsc3 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.1.3  joerg COMPILER_RT_ABI Fcomplex
     21      1.1  joerg __divsc3(float __a, float __b, float __c, float __d)
     22      1.1  joerg {
     23      1.1  joerg     int __ilogbw = 0;
     24      1.1  joerg     float __logbw = crt_logbf(crt_fmaxf(crt_fabsf(__c), crt_fabsf(__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_scalbnf(__c, -__ilogbw);
     29      1.1  joerg         __d = crt_scalbnf(__d, -__ilogbw);
     30      1.1  joerg     }
     31      1.1  joerg     float __denom = __c * __c + __d * __d;
     32  1.1.1.3  joerg     Fcomplex z;
     33  1.1.1.3  joerg     COMPLEX_REAL(z) = crt_scalbnf((__a * __c + __b * __d) / __denom, -__ilogbw);
     34  1.1.1.3  joerg     COMPLEX_IMAGINARY(z) = crt_scalbnf((__b * __c - __a * __d) / __denom, -__ilogbw);
     35  1.1.1.3  joerg     if (crt_isnan(COMPLEX_REAL(z)) && crt_isnan(COMPLEX_IMAGINARY(z)))
     36      1.1  joerg     {
     37      1.1  joerg         if ((__denom == 0) && (!crt_isnan(__a) || !crt_isnan(__b)))
     38      1.1  joerg         {
     39  1.1.1.3  joerg             COMPLEX_REAL(z) = crt_copysignf(CRT_INFINITY, __c) * __a;
     40  1.1.1.3  joerg             COMPLEX_IMAGINARY(z) = crt_copysignf(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_copysignf(crt_isinf(__a) ? 1 : 0, __a);
     46      1.1  joerg             __b = crt_copysignf(crt_isinf(__b) ? 1 : 0, __b);
     47  1.1.1.3  joerg             COMPLEX_REAL(z) = CRT_INFINITY * (__a * __c + __b * __d);
     48  1.1.1.3  joerg             COMPLEX_IMAGINARY(z) = CRT_INFINITY * (__b * __c - __a * __d);
     49      1.1  joerg         }
     50      1.1  joerg         else if (crt_isinf(__logbw) && __logbw > 0 &&
     51      1.1  joerg                  crt_isfinite(__a) && crt_isfinite(__b))
     52      1.1  joerg         {
     53      1.1  joerg             __c = crt_copysignf(crt_isinf(__c) ? 1 : 0, __c);
     54      1.1  joerg             __d = crt_copysignf(crt_isinf(__d) ? 1 : 0, __d);
     55  1.1.1.3  joerg             COMPLEX_REAL(z) = 0 * (__a * __c + __b * __d);
     56  1.1.1.3  joerg             COMPLEX_IMAGINARY(z) = 0 * (__b * __c - __a * __d);
     57      1.1  joerg         }
     58      1.1  joerg     }
     59      1.1  joerg     return z;
     60      1.1  joerg }
     61