Edward O'Callaghan | 37a6a45 | 2009-08-07 20:30:09 +0000 | [diff] [blame] | 1 | /*===-- divsc3.c - Implement __divsc3 -------------------------------------=== |
| 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 __divsc3 for the compiler_rt library. |
| 11 | * |
| 12 | *===----------------------------------------------------------------------=== |
| 13 | */ |
Daniel Dunbar | b3a6901 | 2009-06-26 16:47:03 +0000 | [diff] [blame] | 14 | |
| 15 | #include "int_lib.h" |
Daniel Dunbar | 8603024 | 2011-11-16 07:33:00 +0000 | [diff] [blame] | 16 | #include "int_math.h" |
Daniel Dunbar | b3a6901 | 2009-06-26 16:47:03 +0000 | [diff] [blame] | 17 | |
Edward O'Callaghan | 37a6a45 | 2009-08-07 20:30:09 +0000 | [diff] [blame] | 18 | /* Returns: the quotient of (a + ib) / (c + id) */ |
Daniel Dunbar | b3a6901 | 2009-06-26 16:47:03 +0000 | [diff] [blame] | 19 | |
| 20 | float _Complex |
| 21 | __divsc3(float __a, float __b, float __c, float __d) |
| 22 | { |
| 23 | int __ilogbw = 0; |
Daniel Dunbar | c25c6d1 | 2011-11-16 07:33:06 +0000 | [diff] [blame] | 24 | float __logbw = crt_logbf(crt_fmaxf(crt_fabsf(__c), crt_fabsf(__d))); |
Daniel Dunbar | 8603024 | 2011-11-16 07:33:00 +0000 | [diff] [blame] | 25 | if (crt_isfinite(__logbw)) |
Daniel Dunbar | b3a6901 | 2009-06-26 16:47:03 +0000 | [diff] [blame] | 26 | { |
| 27 | __ilogbw = (int)__logbw; |
Daniel Dunbar | c25c6d1 | 2011-11-16 07:33:06 +0000 | [diff] [blame] | 28 | __c = crt_scalbnf(__c, -__ilogbw); |
| 29 | __d = crt_scalbnf(__d, -__ilogbw); |
Daniel Dunbar | b3a6901 | 2009-06-26 16:47:03 +0000 | [diff] [blame] | 30 | } |
| 31 | float __denom = __c * __c + __d * __d; |
| 32 | float _Complex z; |
Daniel Dunbar | c25c6d1 | 2011-11-16 07:33:06 +0000 | [diff] [blame] | 33 | __real__ z = crt_scalbnf((__a * __c + __b * __d) / __denom, -__ilogbw); |
| 34 | __imag__ z = crt_scalbnf((__b * __c - __a * __d) / __denom, -__ilogbw); |
Daniel Dunbar | 8603024 | 2011-11-16 07:33:00 +0000 | [diff] [blame] | 35 | if (crt_isnan(__real__ z) && crt_isnan(__imag__ z)) |
Daniel Dunbar | b3a6901 | 2009-06-26 16:47:03 +0000 | [diff] [blame] | 36 | { |
Daniel Dunbar | 8603024 | 2011-11-16 07:33:00 +0000 | [diff] [blame] | 37 | if ((__denom == 0) && (!crt_isnan(__a) || !crt_isnan(__b))) |
Daniel Dunbar | b3a6901 | 2009-06-26 16:47:03 +0000 | [diff] [blame] | 38 | { |
Daniel Dunbar | c25c6d1 | 2011-11-16 07:33:06 +0000 | [diff] [blame] | 39 | __real__ z = crt_copysignf(CRT_INFINITY, __c) * __a; |
| 40 | __imag__ z = crt_copysignf(CRT_INFINITY, __c) * __b; |
Daniel Dunbar | b3a6901 | 2009-06-26 16:47:03 +0000 | [diff] [blame] | 41 | } |
Daniel Dunbar | 8603024 | 2011-11-16 07:33:00 +0000 | [diff] [blame] | 42 | else if ((crt_isinf(__a) || crt_isinf(__b)) && |
| 43 | crt_isfinite(__c) && crt_isfinite(__d)) |
Daniel Dunbar | b3a6901 | 2009-06-26 16:47:03 +0000 | [diff] [blame] | 44 | { |
Daniel Dunbar | c25c6d1 | 2011-11-16 07:33:06 +0000 | [diff] [blame] | 45 | __a = crt_copysignf(crt_isinf(__a) ? 1 : 0, __a); |
| 46 | __b = crt_copysignf(crt_isinf(__b) ? 1 : 0, __b); |
| 47 | __real__ z = CRT_INFINITY * (__a * __c + __b * __d); |
| 48 | __imag__ z = CRT_INFINITY * (__b * __c - __a * __d); |
Daniel Dunbar | b3a6901 | 2009-06-26 16:47:03 +0000 | [diff] [blame] | 49 | } |
Daniel Dunbar | 8603024 | 2011-11-16 07:33:00 +0000 | [diff] [blame] | 50 | else if (crt_isinf(__logbw) && __logbw > 0 && |
| 51 | crt_isfinite(__a) && crt_isfinite(__b)) |
Daniel Dunbar | b3a6901 | 2009-06-26 16:47:03 +0000 | [diff] [blame] | 52 | { |
Daniel Dunbar | c25c6d1 | 2011-11-16 07:33:06 +0000 | [diff] [blame] | 53 | __c = crt_copysignf(crt_isinf(__c) ? 1 : 0, __c); |
| 54 | __d = crt_copysignf(crt_isinf(__d) ? 1 : 0, __d); |
Daniel Dunbar | b3a6901 | 2009-06-26 16:47:03 +0000 | [diff] [blame] | 55 | __real__ z = 0 * (__a * __c + __b * __d); |
| 56 | __imag__ z = 0 * (__b * __c - __a * __d); |
| 57 | } |
| 58 | } |
| 59 | return z; |
| 60 | } |