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