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 156