1/*-
2 * Copyright (c) 2013 Bruce D. Evans
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 *    notice unmodified, this list of conditions, and the following
10 *    disclaimer.
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.
14 *
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.
25 */
26
27#include <complex.h>
28#include <float.h>
29
30#include "fpmath.h"
31#include "math.h"
32#include "math_private.h"
33
34#define	MANT_DIG	DBL_MANT_DIG
35#define	MAX_EXP		DBL_MAX_EXP
36#define	MIN_EXP		DBL_MIN_EXP
37
38static const double
39ln2_hi = 6.9314718055829871e-1,		/*  0x162e42fefa0000.0p-53 */
40ln2_lo = 1.6465949582897082e-12;	/*  0x1cf79abc9e3b3a.0p-92 */
41
42double complex
43clog(double complex z)
44{
45	double_t ax, ax2h, ax2l, axh, axl, ay, ay2h, ay2l, ayh, ayl, sh, sl, t;
46	double x, y, v;
47	uint32_t hax, hay;
48	int kx, ky;
49
50	x = creal(z);
51	y = cimag(z);
52	v = atan2(y, x);
53
54	ax = fabs(x);
55	ay = fabs(y);
56	if (ax < ay) {
57		t = ax;
58		ax = ay;
59		ay = t;
60	}
61
62	GET_HIGH_WORD(hax, ax);
63	kx = (hax >> 20) - 1023;
64	GET_HIGH_WORD(hay, ay);
65	ky = (hay >> 20) - 1023;
66
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));
70
71	/* Avoid spurious underflow, and reduce inaccuracies when ax is 1. */
72	if (ax == 1) {
73		if (ky < (MIN_EXP - 1) / 2)
74			return (CMPLX((ay / 2) * ay, v));
75		return (CMPLX(log1p(ay * ay) / 2, v));
76	}
77
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));
81
82	/* Avoid overflow. */
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));
88
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));
93
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));
97
98	/* Calculate ax*ax and ay*ay exactly using Dekker's algorithm. */
99	t = (double)(ax * (0x1p27 + 1));
100	axh = (double)(ax - t) + t;
101	axl = ax - axh;
102	ax2h = ax * ax;
103	ax2l = axh * axh - ax2h + 2 * axh * axl + axl * axl;
104	t = (double)(ay * (0x1p27 + 1));
105	ayh = (double)(ay - t) + t;
106	ayl = ay - ayh;
107	ay2h = ay * ay;
108	ay2l = ayh * ayh - ay2h + 2 * ayh * ayl + ayl * ayl;
109
110	/*
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.
117	 *
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.
121	 *
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
132	 * to log1p().
133	 */
134	sh = ax2h;
135	sl = ay2h;
136	_2sumF(sh, sl);
137	if (sh < 0.5 || sh >= 3)
138		return (CMPLX(log(ay2l + ax2l + sl + sh) / 2, v));
139	sh -= 1;
140	_2sum(sh, sl);
141	_2sum(ax2l, ay2l);
142	/* Briggs-Kahan algorithm (except we discard the final low term): */
143	_2sum(sh, ax2l);
144	_2sum(sl, ay2l);
145	t = ax2l + sl;
146	_2sumF(sh, t);
147	return (CMPLX(log1p(ay2l + t + sh) / 2, v));
148}
149
150#if (LDBL_MANT_DIG == 53)
151__weak_reference(clog, clogl);
152#endif
153