1*0c0288a2SKonstantin Belousov /*-
2*0c0288a2SKonstantin Belousov * Copyright (c) 2013 Bruce D. Evans
3*0c0288a2SKonstantin Belousov * All rights reserved.
4*0c0288a2SKonstantin Belousov *
5*0c0288a2SKonstantin Belousov * Redistribution and use in source and binary forms, with or without
6*0c0288a2SKonstantin Belousov * modification, are permitted provided that the following conditions
7*0c0288a2SKonstantin Belousov * are met:
8*0c0288a2SKonstantin Belousov * 1. Redistributions of source code must retain the above copyright
9*0c0288a2SKonstantin Belousov * notice unmodified, this list of conditions, and the following
10*0c0288a2SKonstantin Belousov * disclaimer.
11*0c0288a2SKonstantin Belousov * 2. Redistributions in binary form must reproduce the above copyright
12*0c0288a2SKonstantin Belousov * notice, this list of conditions and the following disclaimer in the
13*0c0288a2SKonstantin Belousov * documentation and/or other materials provided with the distribution.
14*0c0288a2SKonstantin Belousov *
15*0c0288a2SKonstantin Belousov * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16*0c0288a2SKonstantin Belousov * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17*0c0288a2SKonstantin Belousov * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18*0c0288a2SKonstantin Belousov * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19*0c0288a2SKonstantin Belousov * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20*0c0288a2SKonstantin Belousov * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21*0c0288a2SKonstantin Belousov * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22*0c0288a2SKonstantin Belousov * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23*0c0288a2SKonstantin Belousov * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24*0c0288a2SKonstantin Belousov * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25*0c0288a2SKonstantin Belousov */
26*0c0288a2SKonstantin Belousov
27*0c0288a2SKonstantin Belousov #include <complex.h>
28*0c0288a2SKonstantin Belousov #include <float.h>
29*0c0288a2SKonstantin Belousov
30*0c0288a2SKonstantin Belousov #include "fpmath.h"
31*0c0288a2SKonstantin Belousov #include "math.h"
32*0c0288a2SKonstantin Belousov #include "math_private.h"
33*0c0288a2SKonstantin Belousov
34*0c0288a2SKonstantin Belousov #define MANT_DIG DBL_MANT_DIG
35*0c0288a2SKonstantin Belousov #define MAX_EXP DBL_MAX_EXP
36*0c0288a2SKonstantin Belousov #define MIN_EXP DBL_MIN_EXP
37*0c0288a2SKonstantin Belousov
38*0c0288a2SKonstantin Belousov static const double
39*0c0288a2SKonstantin Belousov ln2_hi = 6.9314718055829871e-1, /* 0x162e42fefa0000.0p-53 */
40*0c0288a2SKonstantin Belousov ln2_lo = 1.6465949582897082e-12; /* 0x1cf79abc9e3b3a.0p-92 */
41*0c0288a2SKonstantin Belousov
42*0c0288a2SKonstantin Belousov double complex
clog(double complex z)43*0c0288a2SKonstantin Belousov clog(double complex z)
44*0c0288a2SKonstantin Belousov {
45*0c0288a2SKonstantin Belousov double_t ax, ax2h, ax2l, axh, axl, ay, ay2h, ay2l, ayh, ayl, sh, sl, t;
46*0c0288a2SKonstantin Belousov double x, y, v;
47*0c0288a2SKonstantin Belousov uint32_t hax, hay;
48*0c0288a2SKonstantin Belousov int kx, ky;
49*0c0288a2SKonstantin Belousov
50*0c0288a2SKonstantin Belousov x = creal(z);
51*0c0288a2SKonstantin Belousov y = cimag(z);
52*0c0288a2SKonstantin Belousov v = atan2(y, x);
53*0c0288a2SKonstantin Belousov
54*0c0288a2SKonstantin Belousov ax = fabs(x);
55*0c0288a2SKonstantin Belousov ay = fabs(y);
56*0c0288a2SKonstantin Belousov if (ax < ay) {
57*0c0288a2SKonstantin Belousov t = ax;
58*0c0288a2SKonstantin Belousov ax = ay;
59*0c0288a2SKonstantin Belousov ay = t;
60*0c0288a2SKonstantin Belousov }
61*0c0288a2SKonstantin Belousov
62*0c0288a2SKonstantin Belousov GET_HIGH_WORD(hax, ax);
63*0c0288a2SKonstantin Belousov kx = (hax >> 20) - 1023;
64*0c0288a2SKonstantin Belousov GET_HIGH_WORD(hay, ay);
65*0c0288a2SKonstantin Belousov ky = (hay >> 20) - 1023;
66*0c0288a2SKonstantin Belousov
67*0c0288a2SKonstantin Belousov /* Handle NaNs and Infs using the general formula. */
68*0c0288a2SKonstantin Belousov if (kx == MAX_EXP || ky == MAX_EXP)
69*0c0288a2SKonstantin Belousov return (CMPLX(log(hypot(x, y)), v));
70*0c0288a2SKonstantin Belousov
71*0c0288a2SKonstantin Belousov /* Avoid spurious underflow, and reduce inaccuracies when ax is 1. */
72*0c0288a2SKonstantin Belousov if (ax == 1) {
73*0c0288a2SKonstantin Belousov if (ky < (MIN_EXP - 1) / 2)
74*0c0288a2SKonstantin Belousov return (CMPLX((ay / 2) * ay, v));
75*0c0288a2SKonstantin Belousov return (CMPLX(log1p(ay * ay) / 2, v));
76*0c0288a2SKonstantin Belousov }
77*0c0288a2SKonstantin Belousov
78*0c0288a2SKonstantin Belousov /* Avoid underflow when ax is not small. Also handle zero args. */
79*0c0288a2SKonstantin Belousov if (kx - ky > MANT_DIG || ay == 0)
80*0c0288a2SKonstantin Belousov return (CMPLX(log(ax), v));
81*0c0288a2SKonstantin Belousov
82*0c0288a2SKonstantin Belousov /* Avoid overflow. */
83*0c0288a2SKonstantin Belousov if (kx >= MAX_EXP - 1)
84*0c0288a2SKonstantin Belousov return (CMPLX(log(hypot(x * 0x1p-1022, y * 0x1p-1022)) +
85*0c0288a2SKonstantin Belousov (MAX_EXP - 2) * ln2_lo + (MAX_EXP - 2) * ln2_hi, v));
86*0c0288a2SKonstantin Belousov if (kx >= (MAX_EXP - 1) / 2)
87*0c0288a2SKonstantin Belousov return (CMPLX(log(hypot(x, y)), v));
88*0c0288a2SKonstantin Belousov
89*0c0288a2SKonstantin Belousov /* Reduce inaccuracies and avoid underflow when ax is denormal. */
90*0c0288a2SKonstantin Belousov if (kx <= MIN_EXP - 2)
91*0c0288a2SKonstantin Belousov return (CMPLX(log(hypot(x * 0x1p1023, y * 0x1p1023)) +
92*0c0288a2SKonstantin Belousov (MIN_EXP - 2) * ln2_lo + (MIN_EXP - 2) * ln2_hi, v));
93*0c0288a2SKonstantin Belousov
94*0c0288a2SKonstantin Belousov /* Avoid remaining underflows (when ax is small but not denormal). */
95*0c0288a2SKonstantin Belousov if (ky < (MIN_EXP - 1) / 2 + MANT_DIG)
96*0c0288a2SKonstantin Belousov return (CMPLX(log(hypot(x, y)), v));
97*0c0288a2SKonstantin Belousov
98*0c0288a2SKonstantin Belousov /* Calculate ax*ax and ay*ay exactly using Dekker's algorithm. */
99*0c0288a2SKonstantin Belousov t = (double)(ax * (0x1p27 + 1));
100*0c0288a2SKonstantin Belousov axh = (double)(ax - t) + t;
101*0c0288a2SKonstantin Belousov axl = ax - axh;
102*0c0288a2SKonstantin Belousov ax2h = ax * ax;
103*0c0288a2SKonstantin Belousov ax2l = axh * axh - ax2h + 2 * axh * axl + axl * axl;
104*0c0288a2SKonstantin Belousov t = (double)(ay * (0x1p27 + 1));
105*0c0288a2SKonstantin Belousov ayh = (double)(ay - t) + t;
106*0c0288a2SKonstantin Belousov ayl = ay - ayh;
107*0c0288a2SKonstantin Belousov ay2h = ay * ay;
108*0c0288a2SKonstantin Belousov ay2l = ayh * ayh - ay2h + 2 * ayh * ayl + ayl * ayl;
109*0c0288a2SKonstantin Belousov
110*0c0288a2SKonstantin Belousov /*
111*0c0288a2SKonstantin Belousov * When log(|z|) is far from 1, accuracy in calculating the sum
112*0c0288a2SKonstantin Belousov * of the squares is not very important since log() reduces
113*0c0288a2SKonstantin Belousov * inaccuracies. We depended on this to use the general
114*0c0288a2SKonstantin Belousov * formula when log(|z|) is very far from 1. When log(|z|) is
115*0c0288a2SKonstantin Belousov * moderately far from 1, we go through the extra-precision
116*0c0288a2SKonstantin Belousov * calculations to reduce branches and gain a little accuracy.
117*0c0288a2SKonstantin Belousov *
118*0c0288a2SKonstantin Belousov * When |z| is near 1, we subtract 1 and use log1p() and don't
119*0c0288a2SKonstantin Belousov * leave it to log() to subtract 1, since we gain at least 1 bit
120*0c0288a2SKonstantin Belousov * of accuracy in this way.
121*0c0288a2SKonstantin Belousov *
122*0c0288a2SKonstantin Belousov * When |z| is very near 1, subtracting 1 can cancel almost
123*0c0288a2SKonstantin Belousov * 3*MANT_DIG bits. We arrange that subtracting 1 is exact in
124*0c0288a2SKonstantin Belousov * doubled precision, and then do the rest of the calculation
125*0c0288a2SKonstantin Belousov * in sloppy doubled precision. Although large cancellations
126*0c0288a2SKonstantin Belousov * often lose lots of accuracy, here the final result is exact
127*0c0288a2SKonstantin Belousov * in doubled precision if the large calculation occurs (because
128*0c0288a2SKonstantin Belousov * then it is exact in tripled precision and the cancellation
129*0c0288a2SKonstantin Belousov * removes enough bits to fit in doubled precision). Thus the
130*0c0288a2SKonstantin Belousov * result is accurate in sloppy doubled precision, and the only
131*0c0288a2SKonstantin Belousov * significant loss of accuracy is when it is summed and passed
132*0c0288a2SKonstantin Belousov * to log1p().
133*0c0288a2SKonstantin Belousov */
134*0c0288a2SKonstantin Belousov sh = ax2h;
135*0c0288a2SKonstantin Belousov sl = ay2h;
136*0c0288a2SKonstantin Belousov _2sumF(sh, sl);
137*0c0288a2SKonstantin Belousov if (sh < 0.5 || sh >= 3)
138*0c0288a2SKonstantin Belousov return (CMPLX(log(ay2l + ax2l + sl + sh) / 2, v));
139*0c0288a2SKonstantin Belousov sh -= 1;
140*0c0288a2SKonstantin Belousov _2sum(sh, sl);
141*0c0288a2SKonstantin Belousov _2sum(ax2l, ay2l);
142*0c0288a2SKonstantin Belousov /* Briggs-Kahan algorithm (except we discard the final low term): */
143*0c0288a2SKonstantin Belousov _2sum(sh, ax2l);
144*0c0288a2SKonstantin Belousov _2sum(sl, ay2l);
145*0c0288a2SKonstantin Belousov t = ax2l + sl;
146*0c0288a2SKonstantin Belousov _2sumF(sh, t);
147*0c0288a2SKonstantin Belousov return (CMPLX(log1p(ay2l + t + sh) / 2, v));
148*0c0288a2SKonstantin Belousov }
149*0c0288a2SKonstantin Belousov
150*0c0288a2SKonstantin Belousov #if (LDBL_MANT_DIG == 53)
151*0c0288a2SKonstantin Belousov __weak_reference(clog, clogl);
152*0c0288a2SKonstantin Belousov #endif
153