• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 __FBSDID("$FreeBSD: head/lib/msun/src/s_clogl.c 333577 2018-05-13 09:54:34Z kib $");
29 
30 #include <complex.h>
31 #include <float.h>
32 #ifdef __i386__
33 #include <ieeefp.h>
34 #endif
35 
36 #include "fpmath.h"
37 #include "math.h"
38 #include "math_private.h"
39 
40 #define	MANT_DIG	LDBL_MANT_DIG
41 #define	MAX_EXP		LDBL_MAX_EXP
42 #define	MIN_EXP		LDBL_MIN_EXP
43 
44 static const double
45 ln2_hi = 6.9314718055829871e-1;		/*  0x162e42fefa0000.0p-53 */
46 
47 #if LDBL_MANT_DIG == 64
48 #define	MULT_REDUX	0x1p32		/* exponent MANT_DIG / 2 rounded up */
49 static const double
50 ln2l_lo = 1.6465949582897082e-12;	/*  0x1cf79abc9e3b3a.0p-92 */
51 #elif LDBL_MANT_DIG == 113
52 #define	MULT_REDUX	0x1p57
53 static const long double
54 ln2l_lo = 1.64659495828970812809844307550013433e-12L;	/*  0x1cf79abc9e3b39803f2f6af40f343.0p-152L */
55 #else
56 #error "Unsupported long double format"
57 #endif
58 
59 long double complex
clogl(long double complex z)60 clogl(long double complex z)
61 {
62 	long double ax, ax2h, ax2l, axh, axl, ay, ay2h, ay2l, ayh, ayl;
63 	long double sh, sl, t;
64 	long double x, y, v;
65 	uint16_t hax, hay;
66 	int kx, ky;
67 
68 	ENTERIT(long double complex);
69 
70 	x = creall(z);
71 	y = cimagl(z);
72 	v = atan2l(y, x);
73 
74 	ax = fabsl(x);
75 	ay = fabsl(y);
76 	if (ax < ay) {
77 		t = ax;
78 		ax = ay;
79 		ay = t;
80 	}
81 
82 	GET_LDBL_EXPSIGN(hax, ax);
83 	kx = hax - 16383;
84 	GET_LDBL_EXPSIGN(hay, ay);
85 	ky = hay - 16383;
86 
87 	/* Handle NaNs and Infs using the general formula. */
88 	if (kx == MAX_EXP || ky == MAX_EXP)
89 		RETURNI(CMPLXL(logl(hypotl(x, y)), v));
90 
91 	/* Avoid spurious underflow, and reduce inaccuracies when ax is 1. */
92 	if (ax == 1) {
93 		if (ky < (MIN_EXP - 1) / 2)
94 			RETURNI(CMPLXL((ay / 2) * ay, v));
95 		RETURNI(CMPLXL(log1pl(ay * ay) / 2, v));
96 	}
97 
98 	/* Avoid underflow when ax is not small.  Also handle zero args. */
99 	if (kx - ky > MANT_DIG || ay == 0)
100 		RETURNI(CMPLXL(logl(ax), v));
101 
102 	/* Avoid overflow. */
103 	if (kx >= MAX_EXP - 1)
104 		RETURNI(CMPLXL(logl(hypotl(x * 0x1p-16382L, y * 0x1p-16382L)) +
105 		    (MAX_EXP - 2) * ln2l_lo + (MAX_EXP - 2) * ln2_hi, v));
106 	if (kx >= (MAX_EXP - 1) / 2)
107 		RETURNI(CMPLXL(logl(hypotl(x, y)), v));
108 
109 	/* Reduce inaccuracies and avoid underflow when ax is denormal. */
110 	if (kx <= MIN_EXP - 2)
111 		RETURNI(CMPLXL(logl(hypotl(x * 0x1p16383L, y * 0x1p16383L)) +
112 		    (MIN_EXP - 2) * ln2l_lo + (MIN_EXP - 2) * ln2_hi, v));
113 
114 	/* Avoid remaining underflows (when ax is small but not denormal). */
115 	if (ky < (MIN_EXP - 1) / 2 + MANT_DIG)
116 		RETURNI(CMPLXL(logl(hypotl(x, y)), v));
117 
118 	/* Calculate ax*ax and ay*ay exactly using Dekker's algorithm. */
119 	t = (long double)(ax * (MULT_REDUX + 1));
120 	axh = (long double)(ax - t) + t;
121 	axl = ax - axh;
122 	ax2h = ax * ax;
123 	ax2l = axh * axh - ax2h + 2 * axh * axl + axl * axl;
124 	t = (long double)(ay * (MULT_REDUX + 1));
125 	ayh = (long double)(ay - t) + t;
126 	ayl = ay - ayh;
127 	ay2h = ay * ay;
128 	ay2l = ayh * ayh - ay2h + 2 * ayh * ayl + ayl * ayl;
129 
130 	/*
131 	 * When log(|z|) is far from 1, accuracy in calculating the sum
132 	 * of the squares is not very important since log() reduces
133 	 * inaccuracies.  We depended on this to use the general
134 	 * formula when log(|z|) is very far from 1.  When log(|z|) is
135 	 * moderately far from 1, we go through the extra-precision
136 	 * calculations to reduce branches and gain a little accuracy.
137 	 *
138 	 * When |z| is near 1, we subtract 1 and use log1p() and don't
139 	 * leave it to log() to subtract 1, since we gain at least 1 bit
140 	 * of accuracy in this way.
141 	 *
142 	 * When |z| is very near 1, subtracting 1 can cancel almost
143 	 * 3*MANT_DIG bits.  We arrange that subtracting 1 is exact in
144 	 * doubled precision, and then do the rest of the calculation
145 	 * in sloppy doubled precision.  Although large cancellations
146 	 * often lose lots of accuracy, here the final result is exact
147 	 * in doubled precision if the large calculation occurs (because
148 	 * then it is exact in tripled precision and the cancellation
149 	 * removes enough bits to fit in doubled precision).  Thus the
150 	 * result is accurate in sloppy doubled precision, and the only
151 	 * significant loss of accuracy is when it is summed and passed
152 	 * to log1p().
153 	 */
154 	sh = ax2h;
155 	sl = ay2h;
156 	_2sumF(sh, sl);
157 	if (sh < 0.5 || sh >= 3)
158 		RETURNI(CMPLXL(logl(ay2l + ax2l + sl + sh) / 2, v));
159 	sh -= 1;
160 	_2sum(sh, sl);
161 	_2sum(ax2l, ay2l);
162 	/* Briggs-Kahan algorithm (except we discard the final low term): */
163 	_2sum(sh, ax2l);
164 	_2sum(sl, ay2l);
165 	t = ax2l + sl;
166 	_2sumF(sh, t);
167 	RETURNI(CMPLXL(log1pl(ay2l + t + sh) / 2, v));
168 }
169