137a6a455466e5b197311771a777ab241e471ed8aEdward O'Callaghan/*===-- divsc3.c - Implement __divsc3 -------------------------------------===
237a6a455466e5b197311771a777ab241e471ed8aEdward O'Callaghan *
337a6a455466e5b197311771a777ab241e471ed8aEdward O'Callaghan *                     The LLVM Compiler Infrastructure
437a6a455466e5b197311771a777ab241e471ed8aEdward O'Callaghan *
59ad441ffec97db647fee3725b3424284fb913e14Howard Hinnant * This file is dual licensed under the MIT and the University of Illinois Open
69ad441ffec97db647fee3725b3424284fb913e14Howard Hinnant * Source Licenses. See LICENSE.TXT for details.
737a6a455466e5b197311771a777ab241e471ed8aEdward O'Callaghan *
837a6a455466e5b197311771a777ab241e471ed8aEdward O'Callaghan * ===----------------------------------------------------------------------===
937a6a455466e5b197311771a777ab241e471ed8aEdward O'Callaghan *
1037a6a455466e5b197311771a777ab241e471ed8aEdward O'Callaghan * This file implements __divsc3 for the compiler_rt library.
1137a6a455466e5b197311771a777ab241e471ed8aEdward O'Callaghan *
1237a6a455466e5b197311771a777ab241e471ed8aEdward O'Callaghan *===----------------------------------------------------------------------===
1337a6a455466e5b197311771a777ab241e471ed8aEdward O'Callaghan */
14b3a6901e66f55b35aa9e01bcb24134e6a65ea004Daniel Dunbar
15b3a6901e66f55b35aa9e01bcb24134e6a65ea004Daniel Dunbar#include "int_lib.h"
168603024a01cfa82ab8dce397f40a2d6f246076e3Daniel Dunbar#include "int_math.h"
17b3a6901e66f55b35aa9e01bcb24134e6a65ea004Daniel Dunbar
1837a6a455466e5b197311771a777ab241e471ed8aEdward O'Callaghan/* Returns: the quotient of (a + ib) / (c + id) */
19b3a6901e66f55b35aa9e01bcb24134e6a65ea004Daniel Dunbar
20799172d60d32feb1acba1a6867f3a9c39a999e5cPirama Arumuga NainarCOMPILER_RT_ABI Fcomplex
21b3a6901e66f55b35aa9e01bcb24134e6a65ea004Daniel Dunbar__divsc3(float __a, float __b, float __c, float __d)
22b3a6901e66f55b35aa9e01bcb24134e6a65ea004Daniel Dunbar{
23b3a6901e66f55b35aa9e01bcb24134e6a65ea004Daniel Dunbar    int __ilogbw = 0;
24c25c6d10b18efa071f0016f31f6f3dd8a8fa6676Daniel Dunbar    float __logbw = crt_logbf(crt_fmaxf(crt_fabsf(__c), crt_fabsf(__d)));
258603024a01cfa82ab8dce397f40a2d6f246076e3Daniel Dunbar    if (crt_isfinite(__logbw))
26b3a6901e66f55b35aa9e01bcb24134e6a65ea004Daniel Dunbar    {
27b3a6901e66f55b35aa9e01bcb24134e6a65ea004Daniel Dunbar        __ilogbw = (int)__logbw;
28c25c6d10b18efa071f0016f31f6f3dd8a8fa6676Daniel Dunbar        __c = crt_scalbnf(__c, -__ilogbw);
29c25c6d10b18efa071f0016f31f6f3dd8a8fa6676Daniel Dunbar        __d = crt_scalbnf(__d, -__ilogbw);
30b3a6901e66f55b35aa9e01bcb24134e6a65ea004Daniel Dunbar    }
31b3a6901e66f55b35aa9e01bcb24134e6a65ea004Daniel Dunbar    float __denom = __c * __c + __d * __d;
32799172d60d32feb1acba1a6867f3a9c39a999e5cPirama Arumuga Nainar    Fcomplex z;
33799172d60d32feb1acba1a6867f3a9c39a999e5cPirama Arumuga Nainar    COMPLEX_REAL(z) = crt_scalbnf((__a * __c + __b * __d) / __denom, -__ilogbw);
34799172d60d32feb1acba1a6867f3a9c39a999e5cPirama Arumuga Nainar    COMPLEX_IMAGINARY(z) = crt_scalbnf((__b * __c - __a * __d) / __denom, -__ilogbw);
35799172d60d32feb1acba1a6867f3a9c39a999e5cPirama Arumuga Nainar    if (crt_isnan(COMPLEX_REAL(z)) && crt_isnan(COMPLEX_IMAGINARY(z)))
36b3a6901e66f55b35aa9e01bcb24134e6a65ea004Daniel Dunbar    {
378603024a01cfa82ab8dce397f40a2d6f246076e3Daniel Dunbar        if ((__denom == 0) && (!crt_isnan(__a) || !crt_isnan(__b)))
38b3a6901e66f55b35aa9e01bcb24134e6a65ea004Daniel Dunbar        {
39799172d60d32feb1acba1a6867f3a9c39a999e5cPirama Arumuga Nainar            COMPLEX_REAL(z) = crt_copysignf(CRT_INFINITY, __c) * __a;
40799172d60d32feb1acba1a6867f3a9c39a999e5cPirama Arumuga Nainar            COMPLEX_IMAGINARY(z) = crt_copysignf(CRT_INFINITY, __c) * __b;
41b3a6901e66f55b35aa9e01bcb24134e6a65ea004Daniel Dunbar        }
428603024a01cfa82ab8dce397f40a2d6f246076e3Daniel Dunbar        else if ((crt_isinf(__a) || crt_isinf(__b)) &&
438603024a01cfa82ab8dce397f40a2d6f246076e3Daniel Dunbar                 crt_isfinite(__c) && crt_isfinite(__d))
44b3a6901e66f55b35aa9e01bcb24134e6a65ea004Daniel Dunbar        {
45c25c6d10b18efa071f0016f31f6f3dd8a8fa6676Daniel Dunbar            __a = crt_copysignf(crt_isinf(__a) ? 1 : 0, __a);
46c25c6d10b18efa071f0016f31f6f3dd8a8fa6676Daniel Dunbar            __b = crt_copysignf(crt_isinf(__b) ? 1 : 0, __b);
47799172d60d32feb1acba1a6867f3a9c39a999e5cPirama Arumuga Nainar            COMPLEX_REAL(z) = CRT_INFINITY * (__a * __c + __b * __d);
48799172d60d32feb1acba1a6867f3a9c39a999e5cPirama Arumuga Nainar            COMPLEX_IMAGINARY(z) = CRT_INFINITY * (__b * __c - __a * __d);
49b3a6901e66f55b35aa9e01bcb24134e6a65ea004Daniel Dunbar        }
508603024a01cfa82ab8dce397f40a2d6f246076e3Daniel Dunbar        else if (crt_isinf(__logbw) && __logbw > 0 &&
518603024a01cfa82ab8dce397f40a2d6f246076e3Daniel Dunbar                 crt_isfinite(__a) && crt_isfinite(__b))
52b3a6901e66f55b35aa9e01bcb24134e6a65ea004Daniel Dunbar        {
53c25c6d10b18efa071f0016f31f6f3dd8a8fa6676Daniel Dunbar            __c = crt_copysignf(crt_isinf(__c) ? 1 : 0, __c);
54c25c6d10b18efa071f0016f31f6f3dd8a8fa6676Daniel Dunbar            __d = crt_copysignf(crt_isinf(__d) ? 1 : 0, __d);
55799172d60d32feb1acba1a6867f3a9c39a999e5cPirama Arumuga Nainar            COMPLEX_REAL(z) = 0 * (__a * __c + __b * __d);
56799172d60d32feb1acba1a6867f3a9c39a999e5cPirama Arumuga Nainar            COMPLEX_IMAGINARY(z) = 0 * (__b * __c - __a * __d);
57b3a6901e66f55b35aa9e01bcb24134e6a65ea004Daniel Dunbar        }
58b3a6901e66f55b35aa9e01bcb24134e6a65ea004Daniel Dunbar    }
59b3a6901e66f55b35aa9e01bcb24134e6a65ea004Daniel Dunbar    return z;
60b3a6901e66f55b35aa9e01bcb24134e6a65ea004Daniel Dunbar}
61