1333577Skib/*-
2333577Skib * Copyright (c) 2013 Bruce D. Evans
3333577Skib * All rights reserved.
4333577Skib *
5333577Skib * Redistribution and use in source and binary forms, with or without
6333577Skib * modification, are permitted provided that the following conditions
7333577Skib * are met:
8333577Skib * 1. Redistributions of source code must retain the above copyright
9333577Skib *    notice unmodified, this list of conditions, and the following
10333577Skib *    disclaimer.
11333577Skib * 2. Redistributions in binary form must reproduce the above copyright
12333577Skib *    notice, this list of conditions and the following disclaimer in the
13333577Skib *    documentation and/or other materials provided with the distribution.
14333577Skib *
15333577Skib * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16333577Skib * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17333577Skib * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18333577Skib * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19333577Skib * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20333577Skib * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21333577Skib * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22333577Skib * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23333577Skib * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24333577Skib * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25333577Skib */
26333577Skib
27333577Skib#include <sys/cdefs.h>
28333577Skib__FBSDID("$FreeBSD: stable/11/lib/msun/src/s_clog.c 334654 2018-06-05 13:46:18Z kib $");
29333577Skib
30333577Skib#include <complex.h>
31333577Skib#include <float.h>
32333577Skib
33333577Skib#include "fpmath.h"
34333577Skib#include "math.h"
35333577Skib#include "math_private.h"
36333577Skib
37333577Skib#define	MANT_DIG	DBL_MANT_DIG
38333577Skib#define	MAX_EXP		DBL_MAX_EXP
39333577Skib#define	MIN_EXP		DBL_MIN_EXP
40333577Skib
41333577Skibstatic const double
42333577Skibln2_hi = 6.9314718055829871e-1,		/*  0x162e42fefa0000.0p-53 */
43333577Skibln2_lo = 1.6465949582897082e-12;	/*  0x1cf79abc9e3b3a.0p-92 */
44333577Skib
45333577Skibdouble complex
46333577Skibclog(double complex z)
47333577Skib{
48333577Skib	double_t ax, ax2h, ax2l, axh, axl, ay, ay2h, ay2l, ayh, ayl, sh, sl, t;
49333577Skib	double x, y, v;
50333577Skib	uint32_t hax, hay;
51333577Skib	int kx, ky;
52333577Skib
53333577Skib	x = creal(z);
54333577Skib	y = cimag(z);
55333577Skib	v = atan2(y, x);
56333577Skib
57333577Skib	ax = fabs(x);
58333577Skib	ay = fabs(y);
59333577Skib	if (ax < ay) {
60333577Skib		t = ax;
61333577Skib		ax = ay;
62333577Skib		ay = t;
63333577Skib	}
64333577Skib
65333577Skib	GET_HIGH_WORD(hax, ax);
66333577Skib	kx = (hax >> 20) - 1023;
67333577Skib	GET_HIGH_WORD(hay, ay);
68333577Skib	ky = (hay >> 20) - 1023;
69333577Skib
70333577Skib	/* Handle NaNs and Infs using the general formula. */
71333577Skib	if (kx == MAX_EXP || ky == MAX_EXP)
72333577Skib		return (CMPLX(log(hypot(x, y)), v));
73333577Skib
74333577Skib	/* Avoid spurious underflow, and reduce inaccuracies when ax is 1. */
75333577Skib	if (ax == 1) {
76333577Skib		if (ky < (MIN_EXP - 1) / 2)
77333577Skib			return (CMPLX((ay / 2) * ay, v));
78333577Skib		return (CMPLX(log1p(ay * ay) / 2, v));
79333577Skib	}
80333577Skib
81333577Skib	/* Avoid underflow when ax is not small.  Also handle zero args. */
82333577Skib	if (kx - ky > MANT_DIG || ay == 0)
83333577Skib		return (CMPLX(log(ax), v));
84333577Skib
85333577Skib	/* Avoid overflow. */
86333577Skib	if (kx >= MAX_EXP - 1)
87333577Skib		return (CMPLX(log(hypot(x * 0x1p-1022, y * 0x1p-1022)) +
88333577Skib		    (MAX_EXP - 2) * ln2_lo + (MAX_EXP - 2) * ln2_hi, v));
89333577Skib	if (kx >= (MAX_EXP - 1) / 2)
90333577Skib		return (CMPLX(log(hypot(x, y)), v));
91333577Skib
92333577Skib	/* Reduce inaccuracies and avoid underflow when ax is denormal. */
93333577Skib	if (kx <= MIN_EXP - 2)
94333577Skib		return (CMPLX(log(hypot(x * 0x1p1023, y * 0x1p1023)) +
95333577Skib		    (MIN_EXP - 2) * ln2_lo + (MIN_EXP - 2) * ln2_hi, v));
96333577Skib
97333577Skib	/* Avoid remaining underflows (when ax is small but not denormal). */
98333577Skib	if (ky < (MIN_EXP - 1) / 2 + MANT_DIG)
99333577Skib		return (CMPLX(log(hypot(x, y)), v));
100333577Skib
101333577Skib	/* Calculate ax*ax and ay*ay exactly using Dekker's algorithm. */
102333577Skib	t = (double)(ax * (0x1p27 + 1));
103333577Skib	axh = (double)(ax - t) + t;
104333577Skib	axl = ax - axh;
105333577Skib	ax2h = ax * ax;
106333577Skib	ax2l = axh * axh - ax2h + 2 * axh * axl + axl * axl;
107333577Skib	t = (double)(ay * (0x1p27 + 1));
108333577Skib	ayh = (double)(ay - t) + t;
109333577Skib	ayl = ay - ayh;
110333577Skib	ay2h = ay * ay;
111333577Skib	ay2l = ayh * ayh - ay2h + 2 * ayh * ayl + ayl * ayl;
112333577Skib
113333577Skib	/*
114333577Skib	 * When log(|z|) is far from 1, accuracy in calculating the sum
115333577Skib	 * of the squares is not very important since log() reduces
116333577Skib	 * inaccuracies.  We depended on this to use the general
117333577Skib	 * formula when log(|z|) is very far from 1.  When log(|z|) is
118333577Skib	 * moderately far from 1, we go through the extra-precision
119333577Skib	 * calculations to reduce branches and gain a little accuracy.
120333577Skib	 *
121333577Skib	 * When |z| is near 1, we subtract 1 and use log1p() and don't
122333577Skib	 * leave it to log() to subtract 1, since we gain at least 1 bit
123333577Skib	 * of accuracy in this way.
124333577Skib	 *
125333577Skib	 * When |z| is very near 1, subtracting 1 can cancel almost
126333577Skib	 * 3*MANT_DIG bits.  We arrange that subtracting 1 is exact in
127333577Skib	 * doubled precision, and then do the rest of the calculation
128333577Skib	 * in sloppy doubled precision.  Although large cancellations
129333577Skib	 * often lose lots of accuracy, here the final result is exact
130333577Skib	 * in doubled precision if the large calculation occurs (because
131333577Skib	 * then it is exact in tripled precision and the cancellation
132333577Skib	 * removes enough bits to fit in doubled precision).  Thus the
133333577Skib	 * result is accurate in sloppy doubled precision, and the only
134333577Skib	 * significant loss of accuracy is when it is summed and passed
135333577Skib	 * to log1p().
136333577Skib	 */
137333577Skib	sh = ax2h;
138333577Skib	sl = ay2h;
139333577Skib	_2sumF(sh, sl);
140333577Skib	if (sh < 0.5 || sh >= 3)
141333577Skib		return (CMPLX(log(ay2l + ax2l + sl + sh) / 2, v));
142333577Skib	sh -= 1;
143333577Skib	_2sum(sh, sl);
144333577Skib	_2sum(ax2l, ay2l);
145333577Skib	/* Briggs-Kahan algorithm (except we discard the final low term): */
146333577Skib	_2sum(sh, ax2l);
147333577Skib	_2sum(sl, ay2l);
148333577Skib	t = ax2l + sl;
149333577Skib	_2sumF(sh, t);
150333577Skib	return (CMPLX(log1p(ay2l + t + sh) / 2, v));
151333577Skib}
152333577Skib
153333577Skib#if (LDBL_MANT_DIG == 53)
154333577Skib__weak_reference(clog, clogl);
155333577Skib#endif
156