1/*===-- divsc3.c - Implement __divsc3 -------------------------------------===
2 *
3 *                     The LLVM Compiler Infrastructure
4 *
5 * This file is distributed under the University of Illinois Open Source
6 * License. See LICENSE.TXT for details.
7 *
8 * ===----------------------------------------------------------------------===
9 *
10 * This file implements __divsc3 for the compiler_rt library.
11 *
12 *===----------------------------------------------------------------------===
13 */
14
15#include "int_lib.h"
16#include <math.h>
17#include <complex.h>
18
19/* Returns: the quotient of (a + ib) / (c + id) */
20
21float _Complex
22__divsc3(float __a, float __b, float __c, float __d)
23{
24    int __ilogbw = 0;
25    float __logbw = logbf(fmaxf(fabsf(__c), fabsf(__d)));
26    if (isfinite(__logbw))
27    {
28        __ilogbw = (int)__logbw;
29        __c = scalbnf(__c, -__ilogbw);
30        __d = scalbnf(__d, -__ilogbw);
31    }
32    float __denom = __c * __c + __d * __d;
33    float _Complex z;
34    __real__ z = scalbnf((__a * __c + __b * __d) / __denom, -__ilogbw);
35    __imag__ z = scalbnf((__b * __c - __a * __d) / __denom, -__ilogbw);
36    if (isnan(__real__ z) && isnan(__imag__ z))
37    {
38        if ((__denom == 0) && (!isnan(__a) || !isnan(__b)))
39        {
40            __real__ z = copysignf(INFINITY, __c) * __a;
41            __imag__ z = copysignf(INFINITY, __c) * __b;
42        }
43        else if ((isinf(__a) || isinf(__b)) && isfinite(__c) && isfinite(__d))
44        {
45            __a = copysignf(isinf(__a) ? 1 : 0, __a);
46            __b = copysignf(isinf(__b) ? 1 : 0, __b);
47            __real__ z = INFINITY * (__a * __c + __b * __d);
48            __imag__ z = INFINITY * (__b * __c - __a * __d);
49        }
50        else if (isinf(__logbw) && __logbw > 0 && isfinite(__a) && isfinite(__b))
51        {
52            __c = copysignf(isinf(__c) ? 1 : 0, __c);
53            __d = copysignf(isinf(__d) ? 1 : 0, __d);
54            __real__ z = 0 * (__a * __c + __b * __d);
55            __imag__ z = 0 * (__b * __c - __a * __d);
56        }
57    }
58    return z;
59}
60