blob: 0f3be9eb68991792e198fe8c5ac11d26c7b6d388 [file] [log] [blame]
Edward O'Callaghan37a6a452009-08-07 20:30:09 +00001/* ===-- muldc3.c - Implement __muldc3 -------------------------------------===
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 __muldc3 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 product of a + ib and c + id */
Daniel Dunbarb3a69012009-06-26 16:47:03 +000020
21double _Complex
22__muldc3(double __a, double __b, double __c, double __d)
23{
24 double __ac = __a * __c;
25 double __bd = __b * __d;
26 double __ad = __a * __d;
27 double __bc = __b * __c;
28 double _Complex z;
29 __real__ z = __ac - __bd;
30 __imag__ z = __ad + __bc;
Daniel Dunbar86030242011-11-16 07:33:00 +000031 if (crt_isnan(__real__ z) && crt_isnan(__imag__ z))
Daniel Dunbarb3a69012009-06-26 16:47:03 +000032 {
33 int __recalc = 0;
Daniel Dunbar86030242011-11-16 07:33:00 +000034 if (crt_isinf(__a) || crt_isinf(__b))
Daniel Dunbarb3a69012009-06-26 16:47:03 +000035 {
Daniel Dunbar86030242011-11-16 07:33:00 +000036 __a = copysign(crt_isinf(__a) ? 1 : 0, __a);
37 __b = copysign(crt_isinf(__b) ? 1 : 0, __b);
38 if (crt_isnan(__c))
Daniel Dunbarb3a69012009-06-26 16:47:03 +000039 __c = copysign(0, __c);
Daniel Dunbar86030242011-11-16 07:33:00 +000040 if (crt_isnan(__d))
Daniel Dunbarb3a69012009-06-26 16:47:03 +000041 __d = copysign(0, __d);
42 __recalc = 1;
43 }
Daniel Dunbar86030242011-11-16 07:33:00 +000044 if (crt_isinf(__c) || crt_isinf(__d))
Daniel Dunbarb3a69012009-06-26 16:47:03 +000045 {
Daniel Dunbar86030242011-11-16 07:33:00 +000046 __c = copysign(crt_isinf(__c) ? 1 : 0, __c);
47 __d = copysign(crt_isinf(__d) ? 1 : 0, __d);
48 if (crt_isnan(__a))
Daniel Dunbarb3a69012009-06-26 16:47:03 +000049 __a = copysign(0, __a);
Daniel Dunbar86030242011-11-16 07:33:00 +000050 if (crt_isnan(__b))
Daniel Dunbarb3a69012009-06-26 16:47:03 +000051 __b = copysign(0, __b);
52 __recalc = 1;
53 }
Daniel Dunbar86030242011-11-16 07:33:00 +000054 if (!__recalc && (crt_isinf(__ac) || crt_isinf(__bd) ||
55 crt_isinf(__ad) || crt_isinf(__bc)))
Daniel Dunbarb3a69012009-06-26 16:47:03 +000056 {
Daniel Dunbar86030242011-11-16 07:33:00 +000057 if (crt_isnan(__a))
Daniel Dunbarb3a69012009-06-26 16:47:03 +000058 __a = copysign(0, __a);
Daniel Dunbar86030242011-11-16 07:33:00 +000059 if (crt_isnan(__b))
Daniel Dunbarb3a69012009-06-26 16:47:03 +000060 __b = copysign(0, __b);
Daniel Dunbar86030242011-11-16 07:33:00 +000061 if (crt_isnan(__c))
Daniel Dunbarb3a69012009-06-26 16:47:03 +000062 __c = copysign(0, __c);
Daniel Dunbar86030242011-11-16 07:33:00 +000063 if (crt_isnan(__d))
Daniel Dunbarb3a69012009-06-26 16:47:03 +000064 __d = copysign(0, __d);
65 __recalc = 1;
66 }
67 if (__recalc)
68 {
69 __real__ z = INFINITY * (__a * __c - __b * __d);
70 __imag__ z = INFINITY * (__a * __d + __b * __c);
71 }
72 }
73 return z;
74}