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