Joerg Sonnenberger | 47174a5 | 2015-11-22 19:13:49 +0000 | [diff] [blame] | 1 | /*===-- divtc3.c - Implement __divtc3 -------------------------------------=== |
| 2 | * |
| 3 | * The LLVM Compiler Infrastructure |
| 4 | * |
| 5 | * This file is dual licensed under the MIT and the University of Illinois Open |
| 6 | * Source Licenses. See LICENSE.TXT for details. |
| 7 | * |
| 8 | * ===----------------------------------------------------------------------=== |
| 9 | * |
| 10 | * This file implements __divtc3 for the compiler_rt library. |
| 11 | * |
| 12 | *===----------------------------------------------------------------------=== |
| 13 | */ |
| 14 | |
Jordan Rupprecht | dc48c4f | 2018-09-24 20:39:19 +0000 | [diff] [blame] | 15 | #define QUAD_PRECISION |
| 16 | #include "fp_lib.h" |
Joerg Sonnenberger | 47174a5 | 2015-11-22 19:13:49 +0000 | [diff] [blame] | 17 | #include "int_lib.h" |
| 18 | #include "int_math.h" |
| 19 | |
| 20 | /* Returns: the quotient of (a + ib) / (c + id) */ |
| 21 | |
Reid Kleckner | 3ae87c4 | 2017-04-07 16:54:32 +0000 | [diff] [blame] | 22 | COMPILER_RT_ABI Lcomplex |
Joerg Sonnenberger | 47174a5 | 2015-11-22 19:13:49 +0000 | [diff] [blame] | 23 | __divtc3(long double __a, long double __b, long double __c, long double __d) |
| 24 | { |
| 25 | int __ilogbw = 0; |
Jordan Rupprecht | dc48c4f | 2018-09-24 20:39:19 +0000 | [diff] [blame] | 26 | long double __logbw = |
| 27 | __compiler_rt_logbl(crt_fmaxl(crt_fabsl(__c), crt_fabsl(__d))); |
Joerg Sonnenberger | 47174a5 | 2015-11-22 19:13:49 +0000 | [diff] [blame] | 28 | if (crt_isfinite(__logbw)) |
| 29 | { |
| 30 | __ilogbw = (int)__logbw; |
| 31 | __c = crt_scalbnl(__c, -__ilogbw); |
| 32 | __d = crt_scalbnl(__d, -__ilogbw); |
| 33 | } |
| 34 | long double __denom = __c * __c + __d * __d; |
Reid Kleckner | 3ae87c4 | 2017-04-07 16:54:32 +0000 | [diff] [blame] | 35 | Lcomplex z; |
| 36 | COMPLEX_REAL(z) = crt_scalbnl((__a * __c + __b * __d) / __denom, -__ilogbw); |
| 37 | COMPLEX_IMAGINARY(z) = crt_scalbnl((__b * __c - __a * __d) / __denom, -__ilogbw); |
| 38 | if (crt_isnan(COMPLEX_REAL(z)) && crt_isnan(COMPLEX_IMAGINARY(z))) |
Joerg Sonnenberger | 47174a5 | 2015-11-22 19:13:49 +0000 | [diff] [blame] | 39 | { |
| 40 | if ((__denom == 0.0) && (!crt_isnan(__a) || !crt_isnan(__b))) |
| 41 | { |
Reid Kleckner | 3ae87c4 | 2017-04-07 16:54:32 +0000 | [diff] [blame] | 42 | COMPLEX_REAL(z) = crt_copysignl(CRT_INFINITY, __c) * __a; |
| 43 | COMPLEX_IMAGINARY(z) = crt_copysignl(CRT_INFINITY, __c) * __b; |
Joerg Sonnenberger | 47174a5 | 2015-11-22 19:13:49 +0000 | [diff] [blame] | 44 | } |
| 45 | else if ((crt_isinf(__a) || crt_isinf(__b)) && |
| 46 | crt_isfinite(__c) && crt_isfinite(__d)) |
| 47 | { |
| 48 | __a = crt_copysignl(crt_isinf(__a) ? 1.0 : 0.0, __a); |
| 49 | __b = crt_copysignl(crt_isinf(__b) ? 1.0 : 0.0, __b); |
Reid Kleckner | 3ae87c4 | 2017-04-07 16:54:32 +0000 | [diff] [blame] | 50 | COMPLEX_REAL(z) = CRT_INFINITY * (__a * __c + __b * __d); |
| 51 | COMPLEX_IMAGINARY(z) = CRT_INFINITY * (__b * __c - __a * __d); |
Joerg Sonnenberger | 47174a5 | 2015-11-22 19:13:49 +0000 | [diff] [blame] | 52 | } |
| 53 | else if (crt_isinf(__logbw) && __logbw > 0.0 && |
| 54 | crt_isfinite(__a) && crt_isfinite(__b)) |
| 55 | { |
| 56 | __c = crt_copysignl(crt_isinf(__c) ? 1.0 : 0.0, __c); |
| 57 | __d = crt_copysignl(crt_isinf(__d) ? 1.0 : 0.0, __d); |
Reid Kleckner | 3ae87c4 | 2017-04-07 16:54:32 +0000 | [diff] [blame] | 58 | COMPLEX_REAL(z) = 0.0 * (__a * __c + __b * __d); |
| 59 | COMPLEX_IMAGINARY(z) = 0.0 * (__b * __c - __a * __d); |
Joerg Sonnenberger | 47174a5 | 2015-11-22 19:13:49 +0000 | [diff] [blame] | 60 | } |
| 61 | } |
| 62 | return z; |
| 63 | } |