Edward O'Callaghan | 5583632 | 2009-08-07 20:30:09 +0000 | [diff] [blame^] | 1 | /* ===-- divdc3.c - Implement __divdc3 -------------------------------------=== |
| 2 | * |
| 3 | * The LLVM Compiler Infrastructure |
| 4 | * |
| 5 | * This file is distributed under the University of Illinois Open Source |
| 6 | * License. See LICENSE.TXT for details. |
| 7 | * |
| 8 | * ===----------------------------------------------------------------------=== |
| 9 | * |
| 10 | * This file implements __divdc3 for the compiler_rt library. |
| 11 | * |
| 12 | * ===----------------------------------------------------------------------=== |
| 13 | */ |
Daniel Dunbar | fd08999 | 2009-06-26 16:47:03 +0000 | [diff] [blame] | 14 | |
| 15 | #include "int_lib.h" |
| 16 | #include <math.h> |
| 17 | #include <complex.h> |
| 18 | |
Edward O'Callaghan | 5583632 | 2009-08-07 20:30:09 +0000 | [diff] [blame^] | 19 | /* Returns: the quotient of (a + ib) / (c + id) */ |
Daniel Dunbar | fd08999 | 2009-06-26 16:47:03 +0000 | [diff] [blame] | 20 | |
| 21 | double _Complex |
| 22 | __divdc3(double __a, double __b, double __c, double __d) |
| 23 | { |
| 24 | int __ilogbw = 0; |
| 25 | double __logbw = logb(fmax(fabs(__c), fabs(__d))); |
| 26 | if (isfinite(__logbw)) |
| 27 | { |
| 28 | __ilogbw = (int)__logbw; |
| 29 | __c = scalbn(__c, -__ilogbw); |
| 30 | __d = scalbn(__d, -__ilogbw); |
| 31 | } |
| 32 | double __denom = __c * __c + __d * __d; |
| 33 | double _Complex z; |
| 34 | __real__ z = scalbn((__a * __c + __b * __d) / __denom, -__ilogbw); |
| 35 | __imag__ z = scalbn((__b * __c - __a * __d) / __denom, -__ilogbw); |
| 36 | if (isnan(__real__ z) && isnan(__imag__ z)) |
| 37 | { |
| 38 | if ((__denom == 0.0) && (!isnan(__a) || !isnan(__b))) |
| 39 | { |
| 40 | __real__ z = copysign(INFINITY, __c) * __a; |
| 41 | __imag__ z = copysign(INFINITY, __c) * __b; |
| 42 | } |
| 43 | else if ((isinf(__a) || isinf(__b)) && isfinite(__c) && isfinite(__d)) |
| 44 | { |
| 45 | __a = copysign(isinf(__a) ? 1.0 : 0.0, __a); |
| 46 | __b = copysign(isinf(__b) ? 1.0 : 0.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.0 && isfinite(__a) && isfinite(__b)) |
| 51 | { |
| 52 | __c = copysign(isinf(__c) ? 1.0 : 0.0, __c); |
| 53 | __d = copysign(isinf(__d) ? 1.0 : 0.0, __d); |
| 54 | __real__ z = 0.0 * (__a * __c + __b * __d); |
| 55 | __imag__ z = 0.0 * (__b * __c - __a * __d); |
| 56 | } |
| 57 | } |
| 58 | return z; |
| 59 | } |