xref: /freebsd/lib/msun/src/s_clog.c (revision 61e21613)
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 <sys/cdefs.h>
28 #include <complex.h>
29 #include <float.h>
30 
31 #include "fpmath.h"
32 #include "math.h"
33 #include "math_private.h"
34 
35 #define	MANT_DIG	DBL_MANT_DIG
36 #define	MAX_EXP		DBL_MAX_EXP
37 #define	MIN_EXP		DBL_MIN_EXP
38 
39 static const double
40 ln2_hi = 6.9314718055829871e-1,		/*  0x162e42fefa0000.0p-53 */
41 ln2_lo = 1.6465949582897082e-12;	/*  0x1cf79abc9e3b3a.0p-92 */
42 
43 double complex
44 clog(double complex z)
45 {
46 	double_t ax, ax2h, ax2l, axh, axl, ay, ay2h, ay2l, ayh, ayl, sh, sl, t;
47 	double x, y, v;
48 	uint32_t hax, hay;
49 	int kx, ky;
50 
51 	x = creal(z);
52 	y = cimag(z);
53 	v = atan2(y, x);
54 
55 	ax = fabs(x);
56 	ay = fabs(y);
57 	if (ax < ay) {
58 		t = ax;
59 		ax = ay;
60 		ay = t;
61 	}
62 
63 	GET_HIGH_WORD(hax, ax);
64 	kx = (hax >> 20) - 1023;
65 	GET_HIGH_WORD(hay, ay);
66 	ky = (hay >> 20) - 1023;
67 
68 	/* Handle NaNs and Infs using the general formula. */
69 	if (kx == MAX_EXP || ky == MAX_EXP)
70 		return (CMPLX(log(hypot(x, y)), v));
71 
72 	/* Avoid spurious underflow, and reduce inaccuracies when ax is 1. */
73 	if (ax == 1) {
74 		if (ky < (MIN_EXP - 1) / 2)
75 			return (CMPLX((ay / 2) * ay, v));
76 		return (CMPLX(log1p(ay * ay) / 2, v));
77 	}
78 
79 	/* Avoid underflow when ax is not small.  Also handle zero args. */
80 	if (kx - ky > MANT_DIG || ay == 0)
81 		return (CMPLX(log(ax), v));
82 
83 	/* Avoid overflow. */
84 	if (kx >= MAX_EXP - 1)
85 		return (CMPLX(log(hypot(x * 0x1p-1022, y * 0x1p-1022)) +
86 		    (MAX_EXP - 2) * ln2_lo + (MAX_EXP - 2) * ln2_hi, v));
87 	if (kx >= (MAX_EXP - 1) / 2)
88 		return (CMPLX(log(hypot(x, y)), v));
89 
90 	/* Reduce inaccuracies and avoid underflow when ax is denormal. */
91 	if (kx <= MIN_EXP - 2)
92 		return (CMPLX(log(hypot(x * 0x1p1023, y * 0x1p1023)) +
93 		    (MIN_EXP - 2) * ln2_lo + (MIN_EXP - 2) * ln2_hi, v));
94 
95 	/* Avoid remaining underflows (when ax is small but not denormal). */
96 	if (ky < (MIN_EXP - 1) / 2 + MANT_DIG)
97 		return (CMPLX(log(hypot(x, y)), v));
98 
99 	/* Calculate ax*ax and ay*ay exactly using Dekker's algorithm. */
100 	t = (double)(ax * (0x1p27 + 1));
101 	axh = (double)(ax - t) + t;
102 	axl = ax - axh;
103 	ax2h = ax * ax;
104 	ax2l = axh * axh - ax2h + 2 * axh * axl + axl * axl;
105 	t = (double)(ay * (0x1p27 + 1));
106 	ayh = (double)(ay - t) + t;
107 	ayl = ay - ayh;
108 	ay2h = ay * ay;
109 	ay2l = ayh * ayh - ay2h + 2 * ayh * ayl + ayl * ayl;
110 
111 	/*
112 	 * When log(|z|) is far from 1, accuracy in calculating the sum
113 	 * of the squares is not very important since log() reduces
114 	 * inaccuracies.  We depended on this to use the general
115 	 * formula when log(|z|) is very far from 1.  When log(|z|) is
116 	 * moderately far from 1, we go through the extra-precision
117 	 * calculations to reduce branches and gain a little accuracy.
118 	 *
119 	 * When |z| is near 1, we subtract 1 and use log1p() and don't
120 	 * leave it to log() to subtract 1, since we gain at least 1 bit
121 	 * of accuracy in this way.
122 	 *
123 	 * When |z| is very near 1, subtracting 1 can cancel almost
124 	 * 3*MANT_DIG bits.  We arrange that subtracting 1 is exact in
125 	 * doubled precision, and then do the rest of the calculation
126 	 * in sloppy doubled precision.  Although large cancellations
127 	 * often lose lots of accuracy, here the final result is exact
128 	 * in doubled precision if the large calculation occurs (because
129 	 * then it is exact in tripled precision and the cancellation
130 	 * removes enough bits to fit in doubled precision).  Thus the
131 	 * result is accurate in sloppy doubled precision, and the only
132 	 * significant loss of accuracy is when it is summed and passed
133 	 * to log1p().
134 	 */
135 	sh = ax2h;
136 	sl = ay2h;
137 	_2sumF(sh, sl);
138 	if (sh < 0.5 || sh >= 3)
139 		return (CMPLX(log(ay2l + ax2l + sl + sh) / 2, v));
140 	sh -= 1;
141 	_2sum(sh, sl);
142 	_2sum(ax2l, ay2l);
143 	/* Briggs-Kahan algorithm (except we discard the final low term): */
144 	_2sum(sh, ax2l);
145 	_2sum(sl, ay2l);
146 	t = ax2l + sl;
147 	_2sumF(sh, t);
148 	return (CMPLX(log1p(ay2l + t + sh) / 2, v));
149 }
150 
151 #if (LDBL_MANT_DIG == 53)
152 __weak_reference(clog, clogl);
153 #endif
154