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_clogl.c 334654 2018-06-05 13:46:18Z kib $");
29333577Skib
30333577Skib#include <complex.h>
31333577Skib#include <float.h>
32333577Skib#ifdef __i386__
33333577Skib#include <ieeefp.h>
34333577Skib#endif
35333577Skib
36333577Skib#include "fpmath.h"
37333577Skib#include "math.h"
38333577Skib#include "math_private.h"
39333577Skib
40333577Skib#define	MANT_DIG	LDBL_MANT_DIG
41333577Skib#define	MAX_EXP		LDBL_MAX_EXP
42333577Skib#define	MIN_EXP		LDBL_MIN_EXP
43333577Skib
44333577Skibstatic const double
45333577Skibln2_hi = 6.9314718055829871e-1;		/*  0x162e42fefa0000.0p-53 */
46333577Skib
47333577Skib#if LDBL_MANT_DIG == 64
48333577Skib#define	MULT_REDUX	0x1p32		/* exponent MANT_DIG / 2 rounded up */
49333577Skibstatic const double
50333577Skibln2l_lo = 1.6465949582897082e-12;	/*  0x1cf79abc9e3b3a.0p-92 */
51333577Skib#elif LDBL_MANT_DIG == 113
52333577Skib#define	MULT_REDUX	0x1p57
53333577Skibstatic const long double
54333577Skibln2l_lo = 1.64659495828970812809844307550013433e-12L;	/*  0x1cf79abc9e3b39803f2f6af40f343.0p-152L */
55333577Skib#else
56333577Skib#error "Unsupported long double format"
57333577Skib#endif
58333577Skib
59333577Skiblong double complex
60333577Skibclogl(long double complex z)
61333577Skib{
62333577Skib	long double ax, ax2h, ax2l, axh, axl, ay, ay2h, ay2l, ayh, ayl;
63333577Skib	long double sh, sl, t;
64333577Skib	long double x, y, v;
65333577Skib	uint16_t hax, hay;
66333577Skib	int kx, ky;
67333577Skib
68333577Skib	ENTERIT(long double complex);
69333577Skib
70333577Skib	x = creall(z);
71333577Skib	y = cimagl(z);
72333577Skib	v = atan2l(y, x);
73333577Skib
74333577Skib	ax = fabsl(x);
75333577Skib	ay = fabsl(y);
76333577Skib	if (ax < ay) {
77333577Skib		t = ax;
78333577Skib		ax = ay;
79333577Skib		ay = t;
80333577Skib	}
81333577Skib
82333577Skib	GET_LDBL_EXPSIGN(hax, ax);
83333577Skib	kx = hax - 16383;
84333577Skib	GET_LDBL_EXPSIGN(hay, ay);
85333577Skib	ky = hay - 16383;
86333577Skib
87333577Skib	/* Handle NaNs and Infs using the general formula. */
88333577Skib	if (kx == MAX_EXP || ky == MAX_EXP)
89333577Skib		RETURNI(CMPLXL(logl(hypotl(x, y)), v));
90333577Skib
91333577Skib	/* Avoid spurious underflow, and reduce inaccuracies when ax is 1. */
92333577Skib	if (ax == 1) {
93333577Skib		if (ky < (MIN_EXP - 1) / 2)
94333577Skib			RETURNI(CMPLXL((ay / 2) * ay, v));
95333577Skib		RETURNI(CMPLXL(log1pl(ay * ay) / 2, v));
96333577Skib	}
97333577Skib
98333577Skib	/* Avoid underflow when ax is not small.  Also handle zero args. */
99333577Skib	if (kx - ky > MANT_DIG || ay == 0)
100333577Skib		RETURNI(CMPLXL(logl(ax), v));
101333577Skib
102333577Skib	/* Avoid overflow. */
103333577Skib	if (kx >= MAX_EXP - 1)
104333577Skib		RETURNI(CMPLXL(logl(hypotl(x * 0x1p-16382L, y * 0x1p-16382L)) +
105333577Skib		    (MAX_EXP - 2) * ln2l_lo + (MAX_EXP - 2) * ln2_hi, v));
106333577Skib	if (kx >= (MAX_EXP - 1) / 2)
107333577Skib		RETURNI(CMPLXL(logl(hypotl(x, y)), v));
108333577Skib
109333577Skib	/* Reduce inaccuracies and avoid underflow when ax is denormal. */
110333577Skib	if (kx <= MIN_EXP - 2)
111333577Skib		RETURNI(CMPLXL(logl(hypotl(x * 0x1p16383L, y * 0x1p16383L)) +
112333577Skib		    (MIN_EXP - 2) * ln2l_lo + (MIN_EXP - 2) * ln2_hi, v));
113333577Skib
114333577Skib	/* Avoid remaining underflows (when ax is small but not denormal). */
115333577Skib	if (ky < (MIN_EXP - 1) / 2 + MANT_DIG)
116333577Skib		RETURNI(CMPLXL(logl(hypotl(x, y)), v));
117333577Skib
118333577Skib	/* Calculate ax*ax and ay*ay exactly using Dekker's algorithm. */
119333577Skib	t = (long double)(ax * (MULT_REDUX + 1));
120333577Skib	axh = (long double)(ax - t) + t;
121333577Skib	axl = ax - axh;
122333577Skib	ax2h = ax * ax;
123333577Skib	ax2l = axh * axh - ax2h + 2 * axh * axl + axl * axl;
124333577Skib	t = (long double)(ay * (MULT_REDUX + 1));
125333577Skib	ayh = (long double)(ay - t) + t;
126333577Skib	ayl = ay - ayh;
127333577Skib	ay2h = ay * ay;
128333577Skib	ay2l = ayh * ayh - ay2h + 2 * ayh * ayl + ayl * ayl;
129333577Skib
130333577Skib	/*
131333577Skib	 * When log(|z|) is far from 1, accuracy in calculating the sum
132333577Skib	 * of the squares is not very important since log() reduces
133333577Skib	 * inaccuracies.  We depended on this to use the general
134333577Skib	 * formula when log(|z|) is very far from 1.  When log(|z|) is
135333577Skib	 * moderately far from 1, we go through the extra-precision
136333577Skib	 * calculations to reduce branches and gain a little accuracy.
137333577Skib	 *
138333577Skib	 * When |z| is near 1, we subtract 1 and use log1p() and don't
139333577Skib	 * leave it to log() to subtract 1, since we gain at least 1 bit
140333577Skib	 * of accuracy in this way.
141333577Skib	 *
142333577Skib	 * When |z| is very near 1, subtracting 1 can cancel almost
143333577Skib	 * 3*MANT_DIG bits.  We arrange that subtracting 1 is exact in
144333577Skib	 * doubled precision, and then do the rest of the calculation
145333577Skib	 * in sloppy doubled precision.  Although large cancellations
146333577Skib	 * often lose lots of accuracy, here the final result is exact
147333577Skib	 * in doubled precision if the large calculation occurs (because
148333577Skib	 * then it is exact in tripled precision and the cancellation
149333577Skib	 * removes enough bits to fit in doubled precision).  Thus the
150333577Skib	 * result is accurate in sloppy doubled precision, and the only
151333577Skib	 * significant loss of accuracy is when it is summed and passed
152333577Skib	 * to log1p().
153333577Skib	 */
154333577Skib	sh = ax2h;
155333577Skib	sl = ay2h;
156333577Skib	_2sumF(sh, sl);
157333577Skib	if (sh < 0.5 || sh >= 3)
158333577Skib		RETURNI(CMPLXL(logl(ay2l + ax2l + sl + sh) / 2, v));
159333577Skib	sh -= 1;
160333577Skib	_2sum(sh, sl);
161333577Skib	_2sum(ax2l, ay2l);
162333577Skib	/* Briggs-Kahan algorithm (except we discard the final low term): */
163333577Skib	_2sum(sh, ax2l);
164333577Skib	_2sum(sl, ay2l);
165333577Skib	t = ax2l + sl;
166333577Skib	_2sumF(sh, t);
167333577Skib	RETURNI(CMPLXL(log1pl(ay2l + t + sh) / 2, v));
168333577Skib}
169