blob: 42a48315e66d1ba465e67955293fc766535a531a [file] [log] [blame]
Edward O'Callaghan55836322009-08-07 20:30:09 +00001/*===-- divsc3.c - Implement __divsc3 -------------------------------------===
2 *
3 * The LLVM Compiler Infrastructure
4 *
Howard Hinnant5b791f62010-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'Callaghan55836322009-08-07 20:30:09 +00007 *
8 * ===----------------------------------------------------------------------===
9 *
10 * This file implements __divsc3 for the compiler_rt library.
11 *
12 *===----------------------------------------------------------------------===
13 */
Daniel Dunbarfd089992009-06-26 16:47:03 +000014
15#include "int_lib.h"
Daniel Dunbarddf18902011-11-16 07:33:00 +000016#include "int_math.h"
Daniel Dunbarfd089992009-06-26 16:47:03 +000017
Edward O'Callaghan55836322009-08-07 20:30:09 +000018/* Returns: the quotient of (a + ib) / (c + id) */
Daniel Dunbarfd089992009-06-26 16:47:03 +000019
Saleem Abdulrasoolb0b7c8a2015-10-07 02:58:07 +000020COMPILER_RT_ABI Fcomplex
Daniel Dunbarfd089992009-06-26 16:47:03 +000021__divsc3(float __a, float __b, float __c, float __d)
22{
23 int __ilogbw = 0;
Daniel Dunbar2139c522011-11-16 07:33:06 +000024 float __logbw = crt_logbf(crt_fmaxf(crt_fabsf(__c), crt_fabsf(__d)));
Daniel Dunbarddf18902011-11-16 07:33:00 +000025 if (crt_isfinite(__logbw))
Daniel Dunbarfd089992009-06-26 16:47:03 +000026 {
27 __ilogbw = (int)__logbw;
Daniel Dunbar2139c522011-11-16 07:33:06 +000028 __c = crt_scalbnf(__c, -__ilogbw);
29 __d = crt_scalbnf(__d, -__ilogbw);
Daniel Dunbarfd089992009-06-26 16:47:03 +000030 }
31 float __denom = __c * __c + __d * __d;
Saleem Abdulrasoolb0b7c8a2015-10-07 02:58:07 +000032 Fcomplex z;
33 COMPLEX_REAL(z) = crt_scalbnf((__a * __c + __b * __d) / __denom, -__ilogbw);
34 COMPLEX_IMAGINARY(z) = crt_scalbnf((__b * __c - __a * __d) / __denom, -__ilogbw);
35 if (crt_isnan(COMPLEX_REAL(z)) && crt_isnan(COMPLEX_IMAGINARY(z)))
Daniel Dunbarfd089992009-06-26 16:47:03 +000036 {
Daniel Dunbarddf18902011-11-16 07:33:00 +000037 if ((__denom == 0) && (!crt_isnan(__a) || !crt_isnan(__b)))
Daniel Dunbarfd089992009-06-26 16:47:03 +000038 {
Saleem Abdulrasoolb0b7c8a2015-10-07 02:58:07 +000039 COMPLEX_REAL(z) = crt_copysignf(CRT_INFINITY, __c) * __a;
40 COMPLEX_IMAGINARY(z) = crt_copysignf(CRT_INFINITY, __c) * __b;
Daniel Dunbarfd089992009-06-26 16:47:03 +000041 }
Daniel Dunbarddf18902011-11-16 07:33:00 +000042 else if ((crt_isinf(__a) || crt_isinf(__b)) &&
43 crt_isfinite(__c) && crt_isfinite(__d))
Daniel Dunbarfd089992009-06-26 16:47:03 +000044 {
Daniel Dunbar2139c522011-11-16 07:33:06 +000045 __a = crt_copysignf(crt_isinf(__a) ? 1 : 0, __a);
46 __b = crt_copysignf(crt_isinf(__b) ? 1 : 0, __b);
Saleem Abdulrasoolb0b7c8a2015-10-07 02:58:07 +000047 COMPLEX_REAL(z) = CRT_INFINITY * (__a * __c + __b * __d);
48 COMPLEX_IMAGINARY(z) = CRT_INFINITY * (__b * __c - __a * __d);
Daniel Dunbarfd089992009-06-26 16:47:03 +000049 }
Daniel Dunbarddf18902011-11-16 07:33:00 +000050 else if (crt_isinf(__logbw) && __logbw > 0 &&
51 crt_isfinite(__a) && crt_isfinite(__b))
Daniel Dunbarfd089992009-06-26 16:47:03 +000052 {
Daniel Dunbar2139c522011-11-16 07:33:06 +000053 __c = crt_copysignf(crt_isinf(__c) ? 1 : 0, __c);
54 __d = crt_copysignf(crt_isinf(__d) ? 1 : 0, __d);
Saleem Abdulrasoolb0b7c8a2015-10-07 02:58:07 +000055 COMPLEX_REAL(z) = 0 * (__a * __c + __b * __d);
56 COMPLEX_IMAGINARY(z) = 0 * (__b * __c - __a * __d);
Daniel Dunbarfd089992009-06-26 16:47:03 +000057 }
58 }
59 return z;
60}