View | Details | Raw Unified | Return to bug 216863 | Differences between
and this patch

Collapse All | Expand All

(-)b/include/complex.h (+4 lines)
Lines 101-106 float complex cexpf(float complex); Link Here
101
double		cimag(double complex) __pure2;
101
double		cimag(double complex) __pure2;
102
float		cimagf(float complex) __pure2;
102
float		cimagf(float complex) __pure2;
103
long double	cimagl(long double complex) __pure2;
103
long double	cimagl(long double complex) __pure2;
104
double complex	clog(double complex);
105
float complex	clogf(float complex);
106
long double complex
107
		clogl(long double complex);
104
double complex	conj(double complex) __pure2;
108
double complex	conj(double complex) __pure2;
105
float complex	conjf(float complex) __pure2;
109
float complex	conjf(float complex) __pure2;
106
long double complex
110
long double complex
(-)b/lib/libc/softfloat/bits64/softfloat-macros (-1 / +1 lines)
Lines 157-163 INLINE void Link Here
157
        z0 = a0>>count;
157
        z0 = a0>>count;
158
    }
158
    }
159
    else {
159
    else {
160
        z1 = ( count < 64 ) ? ( a0>>( count & 63 ) ) : 0;
160
        z1 = ( count < 128 ) ? ( a0>>( count & 63 ) ) : 0;
161
        z0 = 0;
161
        z0 = 0;
162
    }
162
    }
163
    *z1Ptr = z1;
163
    *z1Ptr = z1;
(-)b/lib/msun/Makefile (-2 / +4 lines)
Lines 57-63 COMMON_SRCS= b_exp.c b_log.c b_tgamma.c \ Link Here
57
	k_cos.c k_cosf.c k_exp.c k_expf.c k_rem_pio2.c k_sin.c k_sinf.c \
57
	k_cos.c k_cosf.c k_exp.c k_expf.c k_rem_pio2.c k_sin.c k_sinf.c \
58
	k_tan.c k_tanf.c \
58
	k_tan.c k_tanf.c \
59
	s_asinh.c s_asinhf.c s_atan.c s_atanf.c s_carg.c s_cargf.c s_cargl.c \
59
	s_asinh.c s_asinhf.c s_atan.c s_atanf.c s_carg.c s_cargf.c s_cargl.c \
60
	s_cbrt.c s_cbrtf.c s_ceil.c s_ceilf.c \
60
	s_cbrt.c s_cbrtf.c s_ceil.c s_ceilf.c s_clog.c s_clogf.c s_clogl.c \
61
	s_copysign.c s_copysignf.c s_cos.c s_cosf.c \
61
	s_copysign.c s_copysignf.c s_cos.c s_cosf.c \
62
	s_csqrt.c s_csqrtf.c s_erf.c s_erff.c \
62
	s_csqrt.c s_csqrtf.c s_erf.c s_erff.c \
63
	s_exp2.c s_exp2f.c s_expm1.c s_expm1f.c s_fabsf.c s_fdim.c \
63
	s_exp2.c s_exp2f.c s_expm1.c s_expm1f.c s_fabsf.c s_fdim.c \
Lines 133-139 INCS+= fenv.h math.h Link Here
133
133
134
MAN=	acos.3 acosh.3 asin.3 asinh.3 atan.3 atan2.3 atanh.3 \
134
MAN=	acos.3 acosh.3 asin.3 asinh.3 atan.3 atan2.3 atanh.3 \
135
	ceil.3 cacos.3 ccos.3 ccosh.3 cexp.3 \
135
	ceil.3 cacos.3 ccos.3 ccosh.3 cexp.3 \
136
	cimag.3 copysign.3 cos.3 cosh.3 csqrt.3 erf.3 exp.3 fabs.3 fdim.3 \
136
	cimag.3 clog.3 copysign.3 cos.3 cosh.3 csqrt.3 erf.3 \
137
	exp.3 fabs.3 fdim.3 \
137
	feclearexcept.3 feenableexcept.3 fegetenv.3 \
138
	feclearexcept.3 feenableexcept.3 fegetenv.3 \
138
	fegetround.3 fenv.3 floor.3 \
139
	fegetround.3 fenv.3 floor.3 \
139
	fma.3 fmax.3 fmod.3 hypot.3 ieee.3 ieee_test.3 ilogb.3 j0.3 \
140
	fma.3 fmax.3 fmod.3 hypot.3 ieee.3 ieee_test.3 ilogb.3 j0.3 \
Lines 166-171 MLINKS+=cimag.3 cimagf.3 cimag.3 cimagl.3 \ Link Here
166
	cimag.3 conj.3 cimag.3 conjf.3 cimag.3 conjl.3 \
167
	cimag.3 conj.3 cimag.3 conjf.3 cimag.3 conjl.3 \
167
	cimag.3 cproj.3 cimag.3 cprojf.3 cimag.3 cprojl.3 \
168
	cimag.3 cproj.3 cimag.3 cprojf.3 cimag.3 cprojl.3 \
168
	cimag.3 creal.3 cimag.3 crealf.3 cimag.3 creall.3
169
	cimag.3 creal.3 cimag.3 crealf.3 cimag.3 creall.3
170
MLINKS+=clog.3 clogf.3 clog.3 clogl.3
169
MLINKS+=copysign.3 copysignf.3 copysign.3 copysignl.3
171
MLINKS+=copysign.3 copysignf.3 copysign.3 copysignl.3
170
MLINKS+=cos.3 cosf.3 cos.3 cosl.3
172
MLINKS+=cos.3 cosf.3 cos.3 cosl.3
171
MLINKS+=cosh.3 coshf.3 cosh.3 coshl.3
173
MLINKS+=cosh.3 coshf.3 cosh.3 coshl.3
(-)b/lib/msun/Symbol.map (+3 lines)
Lines 294-299 FBSD_1.5 { Link Here
294
	casinl;
294
	casinl;
295
	catanl;
295
	catanl;
296
	catanhl;
296
	catanhl;
297
	clog;
298
	clogf;
299
	clogl;
297
	sincos;
300
	sincos;
298
	sincosf;
301
	sincosf;
299
	sincosl;
302
	sincosl;
(-)b/lib/msun/man/clog.3 (+103 lines)
Added Link Here
1
.\" Copyright (c) 2017 Steven G. Kargl <kargl@FreeBSD.org>
2
.\" All rights reserved.
3
.\"
4
.\" Redistribution and use in source and binary forms, with or without
5
.\" modification, are permitted provided that the following conditions
6
.\" are met:
7
.\" 1. Redistributions of source code must retain the above copyright
8
.\"    notice, this list of conditions and the following disclaimer.
9
.\" 2. Redistributions in binary form must reproduce the above copyright
10
.\"    notice, this list of conditions and the following disclaimer in the
11
.\"    documentation and/or other materials provided with the distribution.
12
.\"
13
.\" THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
14
.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15
.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
16
.\" ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
17
.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
18
.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
19
.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
20
.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
21
.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
22
.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
23
.\" SUCH DAMAGE.
24
.\"
25
.\" $FreeBSD$
26
.\"
27
.Dd February 13, 2017
28
.Dt CLOG 3
29
.Os
30
.Sh NAME
31
.Nm clog ,
32
.Nm clogf ,
33
and
34
.Nm clogl
35
.Nd complex natural logrithm functions
36
.Sh LIBRARY
37
.Lb libm
38
.Sh SYNOPSIS
39
.In complex.h
40
.Ft double complex
41
.Fn clog "double complex z"
42
.Ft float complex
43
.Fn clogf "float complex z"
44
.Ft long double complex
45
.Fn clogl "long double complex z"
46
.Sh DESCRIPTION
47
The
48
.Fn clog ,
49
.Fn clogf ,
50
and 
51
.Fn clogl
52
functions compute the complex natural logrithm of
53
.Fa z .
54
with a branch cut along the negative real axis .
55
.Sh RETURN VALUES
56
The
57
.Fn clog
58
function returns the complex natural logarithm value, in the
59
range of a strip mathematically unbounded along the real axis and in
60
the interval [-I* \*(Pi , +I* \*(Pi ] along the imaginary axis.
61
The function satisfies the relationship: 
62
.Fo clog
63
.Fn conj "z" Fc
64
=
65
.Fo conj
66
.Fn clog "z" Fc .
67
.Pp
68
69
.\" Table is formatted for an 80-column xterm.
70
.Bl -column ".Sy +\*(If + I*\*(Na" ".Sy Return value" ".Sy Divide-by-zero exception"
71
.It Sy Argument          Ta Sy Return value Ta Sy Comment
72
.It -0 + I*0             Ta -\*(If + I*\*(Pi    Ta Divide-by-zero exception
73
.It                      Ta                     Ta raised
74
.It +0 + I*0             Ta -\*(If + I*0        Ta Divide by zero exception
75
.It                      Ta                     Ta raised
76
.It x + I*\*(If          Ta +\*(If + I*\*(Pi/2  Ta For finite x
77
.It x + I*\*(Na          Ta  \*(Na + I*\*(Na    Ta Optionally raises invalid
78
.It                      Ta                     Ta floating-point exception
79
.It                      Ta                     Ta for finite x
80
.It -\*(If + I*y         Ta +\*(If + I*\*(Pi    Ta For finite positive-signed y
81
.It +\*(If + I*y         Ta +\*(If + I*0        Ta For finite positive-signed y
82
.It -\*(If + I*\*(If     Ta +\*(If + I*3\*(Pi/4
83
.It +\*(If + I*\*(If     Ta +\*(If + I*\*(Pi/4
84
.It \*(Pm\*(If + I*\*(Na Ta +\*(If + I*\*(Na
85
.It \*(Na + I*y          Ta \*(Na + I*\*(Na    Ta Optionally raises invalid
86
.It                      Ta                    Ta floating-point exception
87
.It                      Ta                    Ta for finite y
88
.It \*(Na + I*\*(If      Ta +\*(If + I*\*(Na
89
.It \*(Na + I*\*(Na      Ta \*(Na + I*\*(Na 
90
.El
91
92
.Sh SEE ALSO
93
.Xr complex 3 ,
94
.Xr log 3 ,
95
.Xr math 3
96
.Sh STANDARDS
97
The
98
.Fn clog ,
99
.Fn cexpf ,
100
and
101
.Fn clogl
102
functions conform to
103
.St -isoC-99 .
(-)b/lib/msun/man/complex.3 (-4 / +6 lines)
Lines 24-30 Link Here
24
.\"
24
.\"
25
.\" $FreeBSD$
25
.\" $FreeBSD$
26
.\"
26
.\"
27
.Dd October 17, 2011
27
.Dd February 13, 2017
28
.Dt COMPLEX 3
28
.Dt COMPLEX 3
29
.Os
29
.Os
30
.Sh NAME
30
.Sh NAME
Lines 77-82 csqrt complex square root Link Here
77
.Cl
77
.Cl
78
cexp	exponential base e
78
cexp	exponential base e
79
.El
79
.El
80
.Ss Natural logrithm Function
81
.Cl
82
clog	natural logrithm
83
.El
80
.\" Section 7.3.9 of ISO C99 standard
84
.\" Section 7.3.9 of ISO C99 standard
81
.Ss Manipulation Functions
85
.Ss Manipulation Functions
82
.Cl
86
.Cl
Lines 117-124 The Link Here
117
functions described here conform to
121
functions described here conform to
118
.St -isoC-99 .
122
.St -isoC-99 .
119
.Sh BUGS
123
.Sh BUGS
120
The logarithmic functions
124
The power functions
121
.Fn clog
122
and the power functions
123
.Fn cpow
125
.Fn cpow
124
are not implemented.
126
are not implemented.
(-)b/lib/msun/src/math_private.h (-2 / +4 lines)
Lines 294-301 do { \ Link Here
294
294
295
/* Support switching the mode to FP_PE if necessary. */
295
/* Support switching the mode to FP_PE if necessary. */
296
#if defined(__i386__) && !defined(NO_FPSETPREC)
296
#if defined(__i386__) && !defined(NO_FPSETPREC)
297
#define	ENTERI()				\
297
#define	ENTERI() ENTERIT(long double)
298
	long double __retval;			\
298
#define	ENTERIT(returntype)			\
299
	returntype __retval;			\
299
	fp_prec_t __oprec;			\
300
	fp_prec_t __oprec;			\
300
						\
301
						\
301
	if ((__oprec = fpgetprec()) != FP_PE)	\
302
	if ((__oprec = fpgetprec()) != FP_PE)	\
Lines 318-323 do { \ Link Here
318
} while (0)
319
} while (0)
319
#else
320
#else
320
#define	ENTERI()
321
#define	ENTERI()
322
#define	ENTERIT(x)
321
#define	RETURNI(x)	RETURNF(x)
323
#define	RETURNI(x)	RETURNF(x)
322
#define	ENTERV()
324
#define	ENTERV()
323
#define	RETURNV()	return
325
#define	RETURNV()	return
(-)b/lib/msun/src/s_clog.c (+155 lines)
Added Link Here
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	DBL_MANT_DIG
38
#define	MAX_EXP		DBL_MAX_EXP
39
#define	MIN_EXP		DBL_MIN_EXP
40
41
static const double
42
ln2_hi = 6.9314718055829871e-1,		/*  0x162e42fefa0000.0p-53 */
43
ln2_lo = 1.6465949582897082e-12;	/*  0x1cf79abc9e3b3a.0p-92 */
44
45
double complex
46
clog(double complex z)
47
{
48
	double_t ax, ax2h, ax2l, axh, axl, ay, ay2h, ay2l, ayh, ayl, sh, sl, t;
49
	double x, y, v;
50
	uint32_t hax, hay;
51
	int kx, ky;
52
53
	x = creal(z);
54
	y = cimag(z);
55
	v = atan2(y, x);
56
57
	ax = fabs(x);
58
	ay = fabs(y);
59
	if (ax < ay) {
60
		t = ax;
61
		ax = ay;
62
		ay = t;
63
	}
64
65
	GET_HIGH_WORD(hax, ax);
66
	kx = (hax >> 20) - 1023;
67
	GET_HIGH_WORD(hay, ay);
68
	ky = (hay >> 20) - 1023;
69
70
	/* Handle NaNs and Infs using the general formula. */
71
	if (kx == MAX_EXP || ky == MAX_EXP)
72
		return (CMPLX(log(hypot(x, y)), v));
73
74
	/* Avoid spurious underflow, and reduce inaccuracies when ax is 1. */
75
	if (ax == 1) {
76
		if (ky < (MIN_EXP - 1) / 2)
77
			return (CMPLX((ay / 2) * ay, v));
78
		return (CMPLX(log1p(ay * ay) / 2, v));
79
	}
80
81
	/* Avoid underflow when ax is not small.  Also handle zero args. */
82
	if (kx - ky > MANT_DIG || ay == 0)
83
		return (CMPLX(log(ax), v));
84
85
	/* Avoid overflow. */
86
	if (kx >= MAX_EXP - 1)
87
		return (CMPLX(log(hypot(x * 0x1p-1022, y * 0x1p-1022)) +
88
		    (MAX_EXP - 2) * ln2_lo + (MAX_EXP - 2) * ln2_hi, v));
89
	if (kx >= (MAX_EXP - 1) / 2)
90
		return (CMPLX(log(hypot(x, y)), v));
91
92
	/* Reduce inaccuracies and avoid underflow when ax is denormal. */
93
	if (kx <= MIN_EXP - 2)
94
		return (CMPLX(log(hypot(x * 0x1p1023, y * 0x1p1023)) +
95
		    (MIN_EXP - 2) * ln2_lo + (MIN_EXP - 2) * ln2_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 (CMPLX(log(hypot(x, y)), v));
100
101
	/* Calculate ax*ax and ay*ay exactly using Dekker's algorithm. */
102
	t = (double)(ax * (0x1p27 + 1));
103
	axh = (double)(ax - t) + t;
104
	axl = ax - axh;
105
	ax2h = ax * ax;
106
	ax2l = axh * axh - ax2h + 2 * axh * axl + axl * axl;
107
	t = (double)(ay * (0x1p27 + 1));
108
	ayh = (double)(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.5 || sh >= 3)
141
		return (CMPLX(log(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 (CMPLX(log1p(ay2l + t + sh) / 2, v));
151
}
152
153
#if (LDBL_MANT_DIG == 53)
154
__weak_reference(clog, clogl);
155
#endif
(-)b/lib/msun/src/s_clogf.c (+151 lines)
Added Link Here
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
}
(-)b/lib/msun/src/s_clogl.c (+168 lines)
Added Link Here
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
#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
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
}

Return to bug 216863