• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
2 // See https://llvm.org/LICENSE.txt for license information.
3 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
4 
5 #include "../int_math.h"
6 #include "DD.h"
7 // Use DOUBLE_PRECISION because the soft-fp method we use is logb (on the upper
8 // half of the long doubles), even though this file defines complex division for
9 // 128-bit floats.
10 #define DOUBLE_PRECISION
11 #include "../fp_lib.h"
12 
13 #if !defined(CRT_INFINITY) && defined(HUGE_VAL)
14 #define CRT_INFINITY HUGE_VAL
15 #endif // CRT_INFINITY
16 
17 #define makeFinite(x)                                                          \
18   {                                                                            \
19     (x).s.hi = crt_copysign(crt_isinf((x).s.hi) ? 1.0 : 0.0, (x).s.hi);        \
20     (x).s.lo = 0.0;                                                            \
21   }
22 
__divtc3(long double a,long double b,long double c,long double d)23 long double _Complex __divtc3(long double a, long double b, long double c,
24                               long double d) {
25   DD cDD = {.ld = c};
26   DD dDD = {.ld = d};
27 
28   int ilogbw = 0;
29   const double logbw =
30       __compiler_rt_logb(crt_fmax(crt_fabs(cDD.s.hi), crt_fabs(dDD.s.hi)));
31 
32   if (crt_isfinite(logbw)) {
33     ilogbw = (int)logbw;
34 
35     cDD.s.hi = crt_scalbn(cDD.s.hi, -ilogbw);
36     cDD.s.lo = crt_scalbn(cDD.s.lo, -ilogbw);
37     dDD.s.hi = crt_scalbn(dDD.s.hi, -ilogbw);
38     dDD.s.lo = crt_scalbn(dDD.s.lo, -ilogbw);
39   }
40 
41   const long double denom =
42       __gcc_qadd(__gcc_qmul(cDD.ld, cDD.ld), __gcc_qmul(dDD.ld, dDD.ld));
43   const long double realNumerator =
44       __gcc_qadd(__gcc_qmul(a, cDD.ld), __gcc_qmul(b, dDD.ld));
45   const long double imagNumerator =
46       __gcc_qsub(__gcc_qmul(b, cDD.ld), __gcc_qmul(a, dDD.ld));
47 
48   DD real = {.ld = __gcc_qdiv(realNumerator, denom)};
49   DD imag = {.ld = __gcc_qdiv(imagNumerator, denom)};
50 
51   real.s.hi = crt_scalbn(real.s.hi, -ilogbw);
52   real.s.lo = crt_scalbn(real.s.lo, -ilogbw);
53   imag.s.hi = crt_scalbn(imag.s.hi, -ilogbw);
54   imag.s.lo = crt_scalbn(imag.s.lo, -ilogbw);
55 
56   if (crt_isnan(real.s.hi) && crt_isnan(imag.s.hi)) {
57     DD aDD = {.ld = a};
58     DD bDD = {.ld = b};
59     DD rDD = {.ld = denom};
60 
61     if ((rDD.s.hi == 0.0) && (!crt_isnan(aDD.s.hi) || !crt_isnan(bDD.s.hi))) {
62       real.s.hi = crt_copysign(CRT_INFINITY, cDD.s.hi) * aDD.s.hi;
63       real.s.lo = 0.0;
64       imag.s.hi = crt_copysign(CRT_INFINITY, cDD.s.hi) * bDD.s.hi;
65       imag.s.lo = 0.0;
66     }
67 
68     else if ((crt_isinf(aDD.s.hi) || crt_isinf(bDD.s.hi)) &&
69              crt_isfinite(cDD.s.hi) && crt_isfinite(dDD.s.hi)) {
70       makeFinite(aDD);
71       makeFinite(bDD);
72       real.s.hi = CRT_INFINITY * (aDD.s.hi * cDD.s.hi + bDD.s.hi * dDD.s.hi);
73       real.s.lo = 0.0;
74       imag.s.hi = CRT_INFINITY * (bDD.s.hi * cDD.s.hi - aDD.s.hi * dDD.s.hi);
75       imag.s.lo = 0.0;
76     }
77 
78     else if ((crt_isinf(cDD.s.hi) || crt_isinf(dDD.s.hi)) &&
79              crt_isfinite(aDD.s.hi) && crt_isfinite(bDD.s.hi)) {
80       makeFinite(cDD);
81       makeFinite(dDD);
82       real.s.hi =
83           crt_copysign(0.0, (aDD.s.hi * cDD.s.hi + bDD.s.hi * dDD.s.hi));
84       real.s.lo = 0.0;
85       imag.s.hi =
86           crt_copysign(0.0, (bDD.s.hi * cDD.s.hi - aDD.s.hi * dDD.s.hi));
87       imag.s.lo = 0.0;
88     }
89   }
90 
91   long double _Complex z;
92   __real__ z = real.ld;
93   __imag__ z = imag.ld;
94 
95   return z;
96 }
97