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	FLT_MANT_DIG
35#define	MAX_EXP		FLT_MAX_EXP
36#define	MIN_EXP		FLT_MIN_EXP
37
38static const float
39ln2f_hi =  6.9314575195e-1,		/*  0xb17200.0p-24 */
40ln2f_lo =  1.4286067653e-6;		/*  0xbfbe8e.0p-43 */
41
42float complex
43clogf(float complex z)
44{
45	float_t ax, ax2h, ax2l, axh, axl, ay, ay2h, ay2l, ayh, ayl, sh, sl, t;
46	float x, y, v;
47	uint32_t hax, hay;
48	int kx, ky;
49
50	x = crealf(z);
51	y = cimagf(z);
52	v = atan2f(y, x);
53
54	ax = fabsf(x);
55	ay = fabsf(y);
56	if (ax < ay) {
57		t = ax;
58		ax = ay;
59		ay = t;
60	}
61
62	GET_FLOAT_WORD(hax, ax);
63	kx = (hax >> 23) - 127;
64	GET_FLOAT_WORD(hay, ay);
65	ky = (hay >> 23) - 127;
66
67	/* Handle NaNs and Infs using the general formula. */
68	if (kx == MAX_EXP || ky == MAX_EXP)
69		return (CMPLXF(logf(hypotf(x, y)), v));
70
71	/* Avoid spurious underflow, and reduce inaccuracies when ax is 1. */
72	if (hax == 0x3f800000) {
73		if (ky < (MIN_EXP - 1) / 2)
74			return (CMPLXF((ay / 2) * ay, v));
75		return (CMPLXF(log1pf(ay * ay) / 2, v));
76	}
77
78	/* Avoid underflow when ax is not small.  Also handle zero args. */
79	if (kx - ky > MANT_DIG || hay == 0)
80		return (CMPLXF(logf(ax), v));
81
82	/* Avoid overflow. */
83	if (kx >= MAX_EXP - 1)
84		return (CMPLXF(logf(hypotf(x * 0x1p-126F, y * 0x1p-126F)) +
85		    (MAX_EXP - 2) * ln2f_lo + (MAX_EXP - 2) * ln2f_hi, v));
86	if (kx >= (MAX_EXP - 1) / 2)
87		return (CMPLXF(logf(hypotf(x, y)), v));
88
89	/* Reduce inaccuracies and avoid underflow when ax is denormal. */
90	if (kx <= MIN_EXP - 2)
91		return (CMPLXF(logf(hypotf(x * 0x1p127F, y * 0x1p127F)) +
92		    (MIN_EXP - 2) * ln2f_lo + (MIN_EXP - 2) * ln2f_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 (CMPLXF(logf(hypotf(x, y)), v));
97
98	/* Calculate ax*ax and ay*ay exactly using Dekker's algorithm. */
99	t = (float)(ax * (0x1p12F + 1));
100	axh = (float)(ax - t) + t;
101	axl = ax - axh;
102	ax2h = ax * ax;
103	ax2l = axh * axh - ax2h + 2 * axh * axl + axl * axl;
104	t = (float)(ay * (0x1p12F + 1));
105	ayh = (float)(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.5F || sh >= 3)
138		return (CMPLXF(logf(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 (CMPLXF(log1pf(ay2l + t + sh) / 2, v));
148}
149