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