1*0c0288a2SKonstantin Belousov /*- 2*0c0288a2SKonstantin Belousov * Copyright (c) 2013 Bruce D. Evans 3*0c0288a2SKonstantin Belousov * All rights reserved. 4*0c0288a2SKonstantin Belousov * 5*0c0288a2SKonstantin Belousov * Redistribution and use in source and binary forms, with or without 6*0c0288a2SKonstantin Belousov * modification, are permitted provided that the following conditions 7*0c0288a2SKonstantin Belousov * are met: 8*0c0288a2SKonstantin Belousov * 1. Redistributions of source code must retain the above copyright 9*0c0288a2SKonstantin Belousov * notice unmodified, this list of conditions, and the following 10*0c0288a2SKonstantin Belousov * disclaimer. 11*0c0288a2SKonstantin Belousov * 2. Redistributions in binary form must reproduce the above copyright 12*0c0288a2SKonstantin Belousov * notice, this list of conditions and the following disclaimer in the 13*0c0288a2SKonstantin Belousov * documentation and/or other materials provided with the distribution. 14*0c0288a2SKonstantin Belousov * 15*0c0288a2SKonstantin Belousov * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 16*0c0288a2SKonstantin Belousov * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 17*0c0288a2SKonstantin Belousov * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 18*0c0288a2SKonstantin Belousov * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 19*0c0288a2SKonstantin Belousov * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 20*0c0288a2SKonstantin Belousov * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 21*0c0288a2SKonstantin Belousov * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 22*0c0288a2SKonstantin Belousov * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 23*0c0288a2SKonstantin Belousov * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 24*0c0288a2SKonstantin Belousov * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 25*0c0288a2SKonstantin Belousov */ 26*0c0288a2SKonstantin Belousov 27*0c0288a2SKonstantin Belousov #include <sys/cdefs.h> 28*0c0288a2SKonstantin Belousov __FBSDID("$FreeBSD$"); 29*0c0288a2SKonstantin Belousov 30*0c0288a2SKonstantin Belousov #include <complex.h> 31*0c0288a2SKonstantin Belousov #include <float.h> 32*0c0288a2SKonstantin Belousov 33*0c0288a2SKonstantin Belousov #include "fpmath.h" 34*0c0288a2SKonstantin Belousov #include "math.h" 35*0c0288a2SKonstantin Belousov #include "math_private.h" 36*0c0288a2SKonstantin Belousov 37*0c0288a2SKonstantin Belousov #define MANT_DIG DBL_MANT_DIG 38*0c0288a2SKonstantin Belousov #define MAX_EXP DBL_MAX_EXP 39*0c0288a2SKonstantin Belousov #define MIN_EXP DBL_MIN_EXP 40*0c0288a2SKonstantin Belousov 41*0c0288a2SKonstantin Belousov static const double 42*0c0288a2SKonstantin Belousov ln2_hi = 6.9314718055829871e-1, /* 0x162e42fefa0000.0p-53 */ 43*0c0288a2SKonstantin Belousov ln2_lo = 1.6465949582897082e-12; /* 0x1cf79abc9e3b3a.0p-92 */ 44*0c0288a2SKonstantin Belousov 45*0c0288a2SKonstantin Belousov double complex 46*0c0288a2SKonstantin Belousov clog(double complex z) 47*0c0288a2SKonstantin Belousov { 48*0c0288a2SKonstantin Belousov double_t ax, ax2h, ax2l, axh, axl, ay, ay2h, ay2l, ayh, ayl, sh, sl, t; 49*0c0288a2SKonstantin Belousov double x, y, v; 50*0c0288a2SKonstantin Belousov uint32_t hax, hay; 51*0c0288a2SKonstantin Belousov int kx, ky; 52*0c0288a2SKonstantin Belousov 53*0c0288a2SKonstantin Belousov x = creal(z); 54*0c0288a2SKonstantin Belousov y = cimag(z); 55*0c0288a2SKonstantin Belousov v = atan2(y, x); 56*0c0288a2SKonstantin Belousov 57*0c0288a2SKonstantin Belousov ax = fabs(x); 58*0c0288a2SKonstantin Belousov ay = fabs(y); 59*0c0288a2SKonstantin Belousov if (ax < ay) { 60*0c0288a2SKonstantin Belousov t = ax; 61*0c0288a2SKonstantin Belousov ax = ay; 62*0c0288a2SKonstantin Belousov ay = t; 63*0c0288a2SKonstantin Belousov } 64*0c0288a2SKonstantin Belousov 65*0c0288a2SKonstantin Belousov GET_HIGH_WORD(hax, ax); 66*0c0288a2SKonstantin Belousov kx = (hax >> 20) - 1023; 67*0c0288a2SKonstantin Belousov GET_HIGH_WORD(hay, ay); 68*0c0288a2SKonstantin Belousov ky = (hay >> 20) - 1023; 69*0c0288a2SKonstantin Belousov 70*0c0288a2SKonstantin Belousov /* Handle NaNs and Infs using the general formula. */ 71*0c0288a2SKonstantin Belousov if (kx == MAX_EXP || ky == MAX_EXP) 72*0c0288a2SKonstantin Belousov return (CMPLX(log(hypot(x, y)), v)); 73*0c0288a2SKonstantin Belousov 74*0c0288a2SKonstantin Belousov /* Avoid spurious underflow, and reduce inaccuracies when ax is 1. */ 75*0c0288a2SKonstantin Belousov if (ax == 1) { 76*0c0288a2SKonstantin Belousov if (ky < (MIN_EXP - 1) / 2) 77*0c0288a2SKonstantin Belousov return (CMPLX((ay / 2) * ay, v)); 78*0c0288a2SKonstantin Belousov return (CMPLX(log1p(ay * ay) / 2, v)); 79*0c0288a2SKonstantin Belousov } 80*0c0288a2SKonstantin Belousov 81*0c0288a2SKonstantin Belousov /* Avoid underflow when ax is not small. Also handle zero args. */ 82*0c0288a2SKonstantin Belousov if (kx - ky > MANT_DIG || ay == 0) 83*0c0288a2SKonstantin Belousov return (CMPLX(log(ax), v)); 84*0c0288a2SKonstantin Belousov 85*0c0288a2SKonstantin Belousov /* Avoid overflow. */ 86*0c0288a2SKonstantin Belousov if (kx >= MAX_EXP - 1) 87*0c0288a2SKonstantin Belousov return (CMPLX(log(hypot(x * 0x1p-1022, y * 0x1p-1022)) + 88*0c0288a2SKonstantin Belousov (MAX_EXP - 2) * ln2_lo + (MAX_EXP - 2) * ln2_hi, v)); 89*0c0288a2SKonstantin Belousov if (kx >= (MAX_EXP - 1) / 2) 90*0c0288a2SKonstantin Belousov return (CMPLX(log(hypot(x, y)), v)); 91*0c0288a2SKonstantin Belousov 92*0c0288a2SKonstantin Belousov /* Reduce inaccuracies and avoid underflow when ax is denormal. */ 93*0c0288a2SKonstantin Belousov if (kx <= MIN_EXP - 2) 94*0c0288a2SKonstantin Belousov return (CMPLX(log(hypot(x * 0x1p1023, y * 0x1p1023)) + 95*0c0288a2SKonstantin Belousov (MIN_EXP - 2) * ln2_lo + (MIN_EXP - 2) * ln2_hi, v)); 96*0c0288a2SKonstantin Belousov 97*0c0288a2SKonstantin Belousov /* Avoid remaining underflows (when ax is small but not denormal). */ 98*0c0288a2SKonstantin Belousov if (ky < (MIN_EXP - 1) / 2 + MANT_DIG) 99*0c0288a2SKonstantin Belousov return (CMPLX(log(hypot(x, y)), v)); 100*0c0288a2SKonstantin Belousov 101*0c0288a2SKonstantin Belousov /* Calculate ax*ax and ay*ay exactly using Dekker's algorithm. */ 102*0c0288a2SKonstantin Belousov t = (double)(ax * (0x1p27 + 1)); 103*0c0288a2SKonstantin Belousov axh = (double)(ax - t) + t; 104*0c0288a2SKonstantin Belousov axl = ax - axh; 105*0c0288a2SKonstantin Belousov ax2h = ax * ax; 106*0c0288a2SKonstantin Belousov ax2l = axh * axh - ax2h + 2 * axh * axl + axl * axl; 107*0c0288a2SKonstantin Belousov t = (double)(ay * (0x1p27 + 1)); 108*0c0288a2SKonstantin Belousov ayh = (double)(ay - t) + t; 109*0c0288a2SKonstantin Belousov ayl = ay - ayh; 110*0c0288a2SKonstantin Belousov ay2h = ay * ay; 111*0c0288a2SKonstantin Belousov ay2l = ayh * ayh - ay2h + 2 * ayh * ayl + ayl * ayl; 112*0c0288a2SKonstantin Belousov 113*0c0288a2SKonstantin Belousov /* 114*0c0288a2SKonstantin Belousov * When log(|z|) is far from 1, accuracy in calculating the sum 115*0c0288a2SKonstantin Belousov * of the squares is not very important since log() reduces 116*0c0288a2SKonstantin Belousov * inaccuracies. We depended on this to use the general 117*0c0288a2SKonstantin Belousov * formula when log(|z|) is very far from 1. When log(|z|) is 118*0c0288a2SKonstantin Belousov * moderately far from 1, we go through the extra-precision 119*0c0288a2SKonstantin Belousov * calculations to reduce branches and gain a little accuracy. 120*0c0288a2SKonstantin Belousov * 121*0c0288a2SKonstantin Belousov * When |z| is near 1, we subtract 1 and use log1p() and don't 122*0c0288a2SKonstantin Belousov * leave it to log() to subtract 1, since we gain at least 1 bit 123*0c0288a2SKonstantin Belousov * of accuracy in this way. 124*0c0288a2SKonstantin Belousov * 125*0c0288a2SKonstantin Belousov * When |z| is very near 1, subtracting 1 can cancel almost 126*0c0288a2SKonstantin Belousov * 3*MANT_DIG bits. We arrange that subtracting 1 is exact in 127*0c0288a2SKonstantin Belousov * doubled precision, and then do the rest of the calculation 128*0c0288a2SKonstantin Belousov * in sloppy doubled precision. Although large cancellations 129*0c0288a2SKonstantin Belousov * often lose lots of accuracy, here the final result is exact 130*0c0288a2SKonstantin Belousov * in doubled precision if the large calculation occurs (because 131*0c0288a2SKonstantin Belousov * then it is exact in tripled precision and the cancellation 132*0c0288a2SKonstantin Belousov * removes enough bits to fit in doubled precision). Thus the 133*0c0288a2SKonstantin Belousov * result is accurate in sloppy doubled precision, and the only 134*0c0288a2SKonstantin Belousov * significant loss of accuracy is when it is summed and passed 135*0c0288a2SKonstantin Belousov * to log1p(). 136*0c0288a2SKonstantin Belousov */ 137*0c0288a2SKonstantin Belousov sh = ax2h; 138*0c0288a2SKonstantin Belousov sl = ay2h; 139*0c0288a2SKonstantin Belousov _2sumF(sh, sl); 140*0c0288a2SKonstantin Belousov if (sh < 0.5 || sh >= 3) 141*0c0288a2SKonstantin Belousov return (CMPLX(log(ay2l + ax2l + sl + sh) / 2, v)); 142*0c0288a2SKonstantin Belousov sh -= 1; 143*0c0288a2SKonstantin Belousov _2sum(sh, sl); 144*0c0288a2SKonstantin Belousov _2sum(ax2l, ay2l); 145*0c0288a2SKonstantin Belousov /* Briggs-Kahan algorithm (except we discard the final low term): */ 146*0c0288a2SKonstantin Belousov _2sum(sh, ax2l); 147*0c0288a2SKonstantin Belousov _2sum(sl, ay2l); 148*0c0288a2SKonstantin Belousov t = ax2l + sl; 149*0c0288a2SKonstantin Belousov _2sumF(sh, t); 150*0c0288a2SKonstantin Belousov return (CMPLX(log1p(ay2l + t + sh) / 2, v)); 151*0c0288a2SKonstantin Belousov } 152*0c0288a2SKonstantin Belousov 153*0c0288a2SKonstantin Belousov #if (LDBL_MANT_DIG == 53) 154*0c0288a2SKonstantin Belousov __weak_reference(clog, clogl); 155*0c0288a2SKonstantin Belousov #endif 156