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