blob: a05f42902c0e488e94cbb780806977bfc1c9a688 [file] [log] [blame]
Edward O'Callaghan37a6a452009-08-07 20:30:09 +00001/*===-- divsc3.c - Implement __divsc3 -------------------------------------===
2 *
3 * The LLVM Compiler Infrastructure
4 *
Howard Hinnant9ad441f2010-11-16 22:13:33 +00005 * This file is dual licensed under the MIT and the University of Illinois Open
6 * Source Licenses. See LICENSE.TXT for details.
Edward O'Callaghan37a6a452009-08-07 20:30:09 +00007 *
8 * ===----------------------------------------------------------------------===
9 *
10 * This file implements __divsc3 for the compiler_rt library.
11 *
12 *===----------------------------------------------------------------------===
13 */
Daniel Dunbarb3a69012009-06-26 16:47:03 +000014
15#include "int_lib.h"
16#include <math.h>
17#include <complex.h>
18
Edward O'Callaghan37a6a452009-08-07 20:30:09 +000019/* Returns: the quotient of (a + ib) / (c + id) */
Daniel Dunbarb3a69012009-06-26 16:47:03 +000020
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}