1 /*===-- divsc3.c - Implement __divsc3 -------------------------------------===
3 * The LLVM Compiler Infrastructure
5 * This file is dual licensed under the MIT and the University of Illinois Open
6 * Source Licenses. See LICENSE.TXT for details.
8 * ===----------------------------------------------------------------------===
10 * This file implements __divsc3 for the compiler_rt library.
12 *===----------------------------------------------------------------------===
19 /* Returns: the quotient of (a + ib) / (c + id) */
22 __divsc3(float __a, float __b, float __c, float __d)
25 float __logbw = logbf(fmaxf(fabsf(__c), fabsf(__d)));
26 if (isfinite(__logbw))
28 __ilogbw = (int)__logbw;
29 __c = scalbnf(__c, -__ilogbw);
30 __d = scalbnf(__d, -__ilogbw);
32 float __denom = __c * __c + __d * __d;
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))
38 if ((__denom == 0) && (!isnan(__a) || !isnan(__b)))
40 __real__ z = copysignf(INFINITY, __c) * __a;
41 __imag__ z = copysignf(INFINITY, __c) * __b;
43 else if ((isinf(__a) || isinf(__b)) && isfinite(__c) && isfinite(__d))
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);
50 else if (isinf(__logbw) && __logbw > 0 && isfinite(__a) && isfinite(__b))
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);