]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/compiler-rt/lib/builtins/divsc3.c
Merge llvm, clang, compiler-rt, libc++, libunwind, lld, lldb and openmp
[FreeBSD/FreeBSD.git] / contrib / compiler-rt / lib / builtins / divsc3.c
1 /*===-- divsc3.c - Implement __divsc3 -------------------------------------===
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 __divsc3 for the compiler_rt library.
11  *
12  *===----------------------------------------------------------------------===
13  */
14
15 #define SINGLE_PRECISION
16 #include "fp_lib.h"
17 #include "int_lib.h"
18 #include "int_math.h"
19
20 /* Returns: the quotient of (a + ib) / (c + id) */
21
22 COMPILER_RT_ABI Fcomplex
23 __divsc3(float __a, float __b, float __c, float __d)
24 {
25     int __ilogbw = 0;
26     float __logbw =
27         __compiler_rt_logbf(crt_fmaxf(crt_fabsf(__c), crt_fabsf(__d)));
28     if (crt_isfinite(__logbw))
29     {
30         __ilogbw = (int)__logbw;
31         __c = crt_scalbnf(__c, -__ilogbw);
32         __d = crt_scalbnf(__d, -__ilogbw);
33     }
34     float __denom = __c * __c + __d * __d;
35     Fcomplex z;
36     COMPLEX_REAL(z) = crt_scalbnf((__a * __c + __b * __d) / __denom, -__ilogbw);
37     COMPLEX_IMAGINARY(z) = crt_scalbnf((__b * __c - __a * __d) / __denom, -__ilogbw);
38     if (crt_isnan(COMPLEX_REAL(z)) && crt_isnan(COMPLEX_IMAGINARY(z)))
39     {
40         if ((__denom == 0) && (!crt_isnan(__a) || !crt_isnan(__b)))
41         {
42             COMPLEX_REAL(z) = crt_copysignf(CRT_INFINITY, __c) * __a;
43             COMPLEX_IMAGINARY(z) = crt_copysignf(CRT_INFINITY, __c) * __b;
44         }
45         else if ((crt_isinf(__a) || crt_isinf(__b)) &&
46                  crt_isfinite(__c) && crt_isfinite(__d))
47         {
48             __a = crt_copysignf(crt_isinf(__a) ? 1 : 0, __a);
49             __b = crt_copysignf(crt_isinf(__b) ? 1 : 0, __b);
50             COMPLEX_REAL(z) = CRT_INFINITY * (__a * __c + __b * __d);
51             COMPLEX_IMAGINARY(z) = CRT_INFINITY * (__b * __c - __a * __d);
52         }
53         else if (crt_isinf(__logbw) && __logbw > 0 &&
54                  crt_isfinite(__a) && crt_isfinite(__b))
55         {
56             __c = crt_copysignf(crt_isinf(__c) ? 1 : 0, __c);
57             __d = crt_copysignf(crt_isinf(__d) ? 1 : 0, __d);
58             COMPLEX_REAL(z) = 0 * (__a * __c + __b * __d);
59             COMPLEX_IMAGINARY(z) = 0 * (__b * __c - __a * __d);
60         }
61     }
62     return z;
63 }