blob: 88e2e0943ba593d9afa09eebf4abb58984579c74 [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"
Daniel Dunbar86030242011-11-16 07:33:00 +000016#include "int_math.h"
Daniel Dunbarb3a69012009-06-26 16:47:03 +000017#include <math.h>
Daniel Dunbarb3a69012009-06-26 16:47:03 +000018
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)));
Daniel Dunbar86030242011-11-16 07:33:00 +000026 if (crt_isfinite(__logbw))
Daniel Dunbarb3a69012009-06-26 16:47:03 +000027 {
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);
Daniel Dunbar86030242011-11-16 07:33:00 +000036 if (crt_isnan(__real__ z) && crt_isnan(__imag__ z))
Daniel Dunbarb3a69012009-06-26 16:47:03 +000037 {
Daniel Dunbar86030242011-11-16 07:33:00 +000038 if ((__denom == 0) && (!crt_isnan(__a) || !crt_isnan(__b)))
Daniel Dunbarb3a69012009-06-26 16:47:03 +000039 {
40 __real__ z = copysignf(INFINITY, __c) * __a;
41 __imag__ z = copysignf(INFINITY, __c) * __b;
42 }
Daniel Dunbar86030242011-11-16 07:33:00 +000043 else if ((crt_isinf(__a) || crt_isinf(__b)) &&
44 crt_isfinite(__c) && crt_isfinite(__d))
Daniel Dunbarb3a69012009-06-26 16:47:03 +000045 {
Daniel Dunbar86030242011-11-16 07:33:00 +000046 __a = copysignf(crt_isinf(__a) ? 1 : 0, __a);
47 __b = copysignf(crt_isinf(__b) ? 1 : 0, __b);
Daniel Dunbarb3a69012009-06-26 16:47:03 +000048 __real__ z = INFINITY * (__a * __c + __b * __d);
49 __imag__ z = INFINITY * (__b * __c - __a * __d);
50 }
Daniel Dunbar86030242011-11-16 07:33:00 +000051 else if (crt_isinf(__logbw) && __logbw > 0 &&
52 crt_isfinite(__a) && crt_isfinite(__b))
Daniel Dunbarb3a69012009-06-26 16:47:03 +000053 {
Daniel Dunbar86030242011-11-16 07:33:00 +000054 __c = copysignf(crt_isinf(__c) ? 1 : 0, __c);
55 __d = copysignf(crt_isinf(__d) ? 1 : 0, __d);
Daniel Dunbarb3a69012009-06-26 16:47:03 +000056 __real__ z = 0 * (__a * __c + __b * __d);
57 __imag__ z = 0 * (__b * __c - __a * __d);
58 }
59 }
60 return z;
61}