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