Edward O'Callaghan | 37a6a45 | 2009-08-07 20:30:09 +0000 | [diff] [blame] | 1 | /* ===-- divxc3.c - Implement __divxc3 -------------------------------------=== |
| 2 | * |
| 3 | * The LLVM Compiler Infrastructure |
| 4 | * |
Howard Hinnant | 9ad441f | 2010-11-16 22:13:33 +0000 | [diff] [blame] | 5 | * This file is dual licensed under the MIT and the University of Illinois Open |
| 6 | * Source Licenses. See LICENSE.TXT for details. |
Edward O'Callaghan | 37a6a45 | 2009-08-07 20:30:09 +0000 | [diff] [blame] | 7 | * |
| 8 | * ===----------------------------------------------------------------------=== |
| 9 | * |
| 10 | * This file implements __divxc3 for the compiler_rt library. |
| 11 | * |
| 12 | */ |
Daniel Dunbar | b3a6901 | 2009-06-26 16:47:03 +0000 | [diff] [blame] | 13 | |
| 14 | #if !_ARCH_PPC |
| 15 | |
| 16 | #include "int_lib.h" |
Daniel Dunbar | 8603024 | 2011-11-16 07:33:00 +0000 | [diff] [blame] | 17 | #include "int_math.h" |
Daniel Dunbar | b3a6901 | 2009-06-26 16:47:03 +0000 | [diff] [blame] | 18 | |
Edward O'Callaghan | 37a6a45 | 2009-08-07 20:30:09 +0000 | [diff] [blame] | 19 | /* Returns: the quotient of (a + ib) / (c + id) */ |
Daniel Dunbar | b3a6901 | 2009-06-26 16:47:03 +0000 | [diff] [blame] | 20 | |
| 21 | long double _Complex |
| 22 | __divxc3(long double __a, long double __b, long double __c, long double __d) |
| 23 | { |
| 24 | int __ilogbw = 0; |
Daniel Dunbar | c25c6d1 | 2011-11-16 07:33:06 +0000 | [diff] [blame] | 25 | long double __logbw = crt_logbl(crt_fmaxl(crt_fabsl(__c), crt_fabsl(__d))); |
Daniel Dunbar | 8603024 | 2011-11-16 07:33:00 +0000 | [diff] [blame] | 26 | if (crt_isfinite(__logbw)) |
Daniel Dunbar | b3a6901 | 2009-06-26 16:47:03 +0000 | [diff] [blame] | 27 | { |
| 28 | __ilogbw = (int)__logbw; |
Daniel Dunbar | c25c6d1 | 2011-11-16 07:33:06 +0000 | [diff] [blame] | 29 | __c = crt_scalbnl(__c, -__ilogbw); |
| 30 | __d = crt_scalbnl(__d, -__ilogbw); |
Daniel Dunbar | b3a6901 | 2009-06-26 16:47:03 +0000 | [diff] [blame] | 31 | } |
| 32 | long double __denom = __c * __c + __d * __d; |
| 33 | long double _Complex z; |
Daniel Dunbar | c25c6d1 | 2011-11-16 07:33:06 +0000 | [diff] [blame] | 34 | __real__ z = crt_scalbnl((__a * __c + __b * __d) / __denom, -__ilogbw); |
| 35 | __imag__ z = crt_scalbnl((__b * __c - __a * __d) / __denom, -__ilogbw); |
Daniel Dunbar | 8603024 | 2011-11-16 07:33:00 +0000 | [diff] [blame] | 36 | if (crt_isnan(__real__ z) && crt_isnan(__imag__ z)) |
Daniel Dunbar | b3a6901 | 2009-06-26 16:47:03 +0000 | [diff] [blame] | 37 | { |
Daniel Dunbar | 8603024 | 2011-11-16 07:33:00 +0000 | [diff] [blame] | 38 | if ((__denom == 0) && (!crt_isnan(__a) || !crt_isnan(__b))) |
Daniel Dunbar | b3a6901 | 2009-06-26 16:47:03 +0000 | [diff] [blame] | 39 | { |
Daniel Dunbar | c25c6d1 | 2011-11-16 07:33:06 +0000 | [diff] [blame] | 40 | __real__ z = crt_copysignl(CRT_INFINITY, __c) * __a; |
| 41 | __imag__ z = crt_copysignl(CRT_INFINITY, __c) * __b; |
Daniel Dunbar | b3a6901 | 2009-06-26 16:47:03 +0000 | [diff] [blame] | 42 | } |
Daniel Dunbar | 8603024 | 2011-11-16 07:33:00 +0000 | [diff] [blame] | 43 | else if ((crt_isinf(__a) || crt_isinf(__b)) && |
| 44 | crt_isfinite(__c) && crt_isfinite(__d)) |
Daniel Dunbar | b3a6901 | 2009-06-26 16:47:03 +0000 | [diff] [blame] | 45 | { |
Daniel Dunbar | c25c6d1 | 2011-11-16 07:33:06 +0000 | [diff] [blame] | 46 | __a = crt_copysignl(crt_isinf(__a) ? 1 : 0, __a); |
| 47 | __b = crt_copysignl(crt_isinf(__b) ? 1 : 0, __b); |
| 48 | __real__ z = CRT_INFINITY * (__a * __c + __b * __d); |
| 49 | __imag__ z = CRT_INFINITY * (__b * __c - __a * __d); |
Daniel Dunbar | b3a6901 | 2009-06-26 16:47:03 +0000 | [diff] [blame] | 50 | } |
Daniel Dunbar | 8603024 | 2011-11-16 07:33:00 +0000 | [diff] [blame] | 51 | else if (crt_isinf(__logbw) && __logbw > 0 && |
| 52 | crt_isfinite(__a) && crt_isfinite(__b)) |
Daniel Dunbar | b3a6901 | 2009-06-26 16:47:03 +0000 | [diff] [blame] | 53 | { |
Daniel Dunbar | c25c6d1 | 2011-11-16 07:33:06 +0000 | [diff] [blame] | 54 | __c = crt_copysignl(crt_isinf(__c) ? 1 : 0, __c); |
| 55 | __d = crt_copysignl(crt_isinf(__d) ? 1 : 0, __d); |
Daniel Dunbar | b3a6901 | 2009-06-26 16:47:03 +0000 | [diff] [blame] | 56 | __real__ z = 0 * (__a * __c + __b * __d); |
| 57 | __imag__ z = 0 * (__b * __c - __a * __d); |
| 58 | } |
| 59 | } |
| 60 | return z; |
| 61 | } |
| 62 | |
| 63 | #endif |