1214152Sed/*===-- divsc3.c - Implement __divsc3 -------------------------------------===
2214152Sed *
3214152Sed *                     The LLVM Compiler Infrastructure
4214152Sed *
5222656Sed * This file is dual licensed under the MIT and the University of Illinois Open
6222656Sed * Source Licenses. See LICENSE.TXT for details.
7214152Sed *
8214152Sed * ===----------------------------------------------------------------------===
9214152Sed *
10214152Sed * This file implements __divsc3 for the compiler_rt library.
11214152Sed *
12214152Sed *===----------------------------------------------------------------------===
13214152Sed */
14214152Sed
15214152Sed#include "int_lib.h"
16229135Sed#include "int_math.h"
17214152Sed
18214152Sed/* Returns: the quotient of (a + ib) / (c + id) */
19214152Sed
20214152Sedfloat _Complex
21214152Sed__divsc3(float __a, float __b, float __c, float __d)
22214152Sed{
23214152Sed    int __ilogbw = 0;
24229135Sed    float __logbw = crt_logbf(crt_fmaxf(crt_fabsf(__c), crt_fabsf(__d)));
25229135Sed    if (crt_isfinite(__logbw))
26214152Sed    {
27214152Sed        __ilogbw = (int)__logbw;
28229135Sed        __c = crt_scalbnf(__c, -__ilogbw);
29229135Sed        __d = crt_scalbnf(__d, -__ilogbw);
30214152Sed    }
31214152Sed    float __denom = __c * __c + __d * __d;
32214152Sed    float _Complex z;
33229135Sed    __real__ z = crt_scalbnf((__a * __c + __b * __d) / __denom, -__ilogbw);
34229135Sed    __imag__ z = crt_scalbnf((__b * __c - __a * __d) / __denom, -__ilogbw);
35229135Sed    if (crt_isnan(__real__ z) && crt_isnan(__imag__ z))
36214152Sed    {
37229135Sed        if ((__denom == 0) && (!crt_isnan(__a) || !crt_isnan(__b)))
38214152Sed        {
39229135Sed            __real__ z = crt_copysignf(CRT_INFINITY, __c) * __a;
40229135Sed            __imag__ z = crt_copysignf(CRT_INFINITY, __c) * __b;
41214152Sed        }
42229135Sed        else if ((crt_isinf(__a) || crt_isinf(__b)) &&
43229135Sed                 crt_isfinite(__c) && crt_isfinite(__d))
44214152Sed        {
45229135Sed            __a = crt_copysignf(crt_isinf(__a) ? 1 : 0, __a);
46229135Sed            __b = crt_copysignf(crt_isinf(__b) ? 1 : 0, __b);
47229135Sed            __real__ z = CRT_INFINITY * (__a * __c + __b * __d);
48229135Sed            __imag__ z = CRT_INFINITY * (__b * __c - __a * __d);
49214152Sed        }
50229135Sed        else if (crt_isinf(__logbw) && __logbw > 0 &&
51229135Sed                 crt_isfinite(__a) && crt_isfinite(__b))
52214152Sed        {
53229135Sed            __c = crt_copysignf(crt_isinf(__c) ? 1 : 0, __c);
54229135Sed            __d = crt_copysignf(crt_isinf(__d) ? 1 : 0, __d);
55214152Sed            __real__ z = 0 * (__a * __c + __b * __d);
56214152Sed            __imag__ z = 0 * (__b * __c - __a * __d);
57214152Sed        }
58214152Sed    }
59214152Sed    return z;
60214152Sed}
61