1*4887Schin #include "FEATURE/uwin" 2*4887Schin 3*4887Schin #if !_UWIN 4*4887Schin 5*4887Schin void _STUB_log(){} 6*4887Schin 7*4887Schin #else 8*4887Schin 9*4887Schin /* 10*4887Schin * Copyright (c) 1992, 1993 11*4887Schin * The Regents of the University of California. All rights reserved. 12*4887Schin * 13*4887Schin * Redistribution and use in source and binary forms, with or without 14*4887Schin * modification, are permitted provided that the following conditions 15*4887Schin * are met: 16*4887Schin * 1. Redistributions of source code must retain the above copyright 17*4887Schin * notice, this list of conditions and the following disclaimer. 18*4887Schin * 2. Redistributions in binary form must reproduce the above copyright 19*4887Schin * notice, this list of conditions and the following disclaimer in the 20*4887Schin * documentation and/or other materials provided with the distribution. 21*4887Schin * 3. Neither the name of the University nor the names of its contributors 22*4887Schin * may be used to endorse or promote products derived from this software 23*4887Schin * without specific prior written permission. 24*4887Schin * 25*4887Schin * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 26*4887Schin * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 27*4887Schin * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 28*4887Schin * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 29*4887Schin * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 30*4887Schin * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 31*4887Schin * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 32*4887Schin * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 33*4887Schin * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 34*4887Schin * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 35*4887Schin * SUCH DAMAGE. 36*4887Schin */ 37*4887Schin 38*4887Schin #ifndef lint 39*4887Schin static char sccsid[] = "@(#)log.c 8.2 (Berkeley) 11/30/93"; 40*4887Schin #endif /* not lint */ 41*4887Schin 42*4887Schin #include <math.h> 43*4887Schin #include <errno.h> 44*4887Schin 45*4887Schin #include "mathimpl.h" 46*4887Schin 47*4887Schin /* Table-driven natural logarithm. 48*4887Schin * 49*4887Schin * This code was derived, with minor modifications, from: 50*4887Schin * Peter Tang, "Table-Driven Implementation of the 51*4887Schin * Logarithm in IEEE Floating-Point arithmetic." ACM Trans. 52*4887Schin * Math Software, vol 16. no 4, pp 378-400, Dec 1990). 53*4887Schin * 54*4887Schin * Calculates log(2^m*F*(1+f/F)), |f/j| <= 1/256, 55*4887Schin * where F = j/128 for j an integer in [0, 128]. 56*4887Schin * 57*4887Schin * log(2^m) = log2_hi*m + log2_tail*m 58*4887Schin * since m is an integer, the dominant term is exact. 59*4887Schin * m has at most 10 digits (for subnormal numbers), 60*4887Schin * and log2_hi has 11 trailing zero bits. 61*4887Schin * 62*4887Schin * log(F) = logF_hi[j] + logF_lo[j] is in tabular form in log_table.h 63*4887Schin * logF_hi[] + 512 is exact. 64*4887Schin * 65*4887Schin * log(1+f/F) = 2*f/(2*F + f) + 1/12 * (2*f/(2*F + f))**3 + ... 66*4887Schin * the leading term is calculated to extra precision in two 67*4887Schin * parts, the larger of which adds exactly to the dominant 68*4887Schin * m and F terms. 69*4887Schin * There are two cases: 70*4887Schin * 1. when m, j are non-zero (m | j), use absolute 71*4887Schin * precision for the leading term. 72*4887Schin * 2. when m = j = 0, |1-x| < 1/256, and log(x) ~= (x-1). 73*4887Schin * In this case, use a relative precision of 24 bits. 74*4887Schin * (This is done differently in the original paper) 75*4887Schin * 76*4887Schin * Special cases: 77*4887Schin * 0 return signalling -Inf 78*4887Schin * neg return signalling NaN 79*4887Schin * +Inf return +Inf 80*4887Schin */ 81*4887Schin 82*4887Schin #if defined(vax) || defined(tahoe) 83*4887Schin #define _IEEE 0 84*4887Schin #define TRUNC(x) x = (double) (float) (x) 85*4887Schin #else 86*4887Schin #define _IEEE 1 87*4887Schin #define endian (((*(int *) &one)) ? 1 : 0) 88*4887Schin #define TRUNC(x) *(((int *) &x) + endian) &= 0xf8000000 89*4887Schin #define infnan(x) 0.0 90*4887Schin #endif 91*4887Schin 92*4887Schin #define N 128 93*4887Schin 94*4887Schin /* Table of log(Fj) = logF_head[j] + logF_tail[j], for Fj = 1+j/128. 95*4887Schin * Used for generation of extend precision logarithms. 96*4887Schin * The constant 35184372088832 is 2^45, so the divide is exact. 97*4887Schin * It ensures correct reading of logF_head, even for inaccurate 98*4887Schin * decimal-to-binary conversion routines. (Everybody gets the 99*4887Schin * right answer for integers less than 2^53.) 100*4887Schin * Values for log(F) were generated using error < 10^-57 absolute 101*4887Schin * with the bc -l package. 102*4887Schin */ 103*4887Schin static double A1 = .08333333333333178827; 104*4887Schin static double A2 = .01250000000377174923; 105*4887Schin static double A3 = .002232139987919447809; 106*4887Schin static double A4 = .0004348877777076145742; 107*4887Schin 108*4887Schin static double logF_head[N+1] = { 109*4887Schin 0., 110*4887Schin .007782140442060381246, 111*4887Schin .015504186535963526694, 112*4887Schin .023167059281547608406, 113*4887Schin .030771658666765233647, 114*4887Schin .038318864302141264488, 115*4887Schin .045809536031242714670, 116*4887Schin .053244514518837604555, 117*4887Schin .060624621816486978786, 118*4887Schin .067950661908525944454, 119*4887Schin .075223421237524235039, 120*4887Schin .082443669210988446138, 121*4887Schin .089612158689760690322, 122*4887Schin .096729626458454731618, 123*4887Schin .103796793681567578460, 124*4887Schin .110814366340264314203, 125*4887Schin .117783035656430001836, 126*4887Schin .124703478501032805070, 127*4887Schin .131576357788617315236, 128*4887Schin .138402322859292326029, 129*4887Schin .145182009844575077295, 130*4887Schin .151916042025732167530, 131*4887Schin .158605030176659056451, 132*4887Schin .165249572895390883786, 133*4887Schin .171850256926518341060, 134*4887Schin .178407657472689606947, 135*4887Schin .184922338493834104156, 136*4887Schin .191394852999565046047, 137*4887Schin .197825743329758552135, 138*4887Schin .204215541428766300668, 139*4887Schin .210564769107350002741, 140*4887Schin .216873938300523150246, 141*4887Schin .223143551314024080056, 142*4887Schin .229374101064877322642, 143*4887Schin .235566071312860003672, 144*4887Schin .241719936886966024758, 145*4887Schin .247836163904594286577, 146*4887Schin .253915209980732470285, 147*4887Schin .259957524436686071567, 148*4887Schin .265963548496984003577, 149*4887Schin .271933715484010463114, 150*4887Schin .277868451003087102435, 151*4887Schin .283768173130738432519, 152*4887Schin .289633292582948342896, 153*4887Schin .295464212893421063199, 154*4887Schin .301261330578199704177, 155*4887Schin .307025035294827830512, 156*4887Schin .312755710004239517729, 157*4887Schin .318453731118097493890, 158*4887Schin .324119468654316733591, 159*4887Schin .329753286372579168528, 160*4887Schin .335355541920762334484, 161*4887Schin .340926586970454081892, 162*4887Schin .346466767346100823488, 163*4887Schin .351976423156884266063, 164*4887Schin .357455888922231679316, 165*4887Schin .362905493689140712376, 166*4887Schin .368325561158599157352, 167*4887Schin .373716409793814818840, 168*4887Schin .379078352934811846353, 169*4887Schin .384411698910298582632, 170*4887Schin .389716751140440464951, 171*4887Schin .394993808240542421117, 172*4887Schin .400243164127459749579, 173*4887Schin .405465108107819105498, 174*4887Schin .410659924985338875558, 175*4887Schin .415827895143593195825, 176*4887Schin .420969294644237379543, 177*4887Schin .426084395310681429691, 178*4887Schin .431173464818130014464, 179*4887Schin .436236766774527495726, 180*4887Schin .441274560805140936281, 181*4887Schin .446287102628048160113, 182*4887Schin .451274644139630254358, 183*4887Schin .456237433481874177232, 184*4887Schin .461175715122408291790, 185*4887Schin .466089729924533457960, 186*4887Schin .470979715219073113985, 187*4887Schin .475845904869856894947, 188*4887Schin .480688529345570714212, 189*4887Schin .485507815781602403149, 190*4887Schin .490303988045525329653, 191*4887Schin .495077266798034543171, 192*4887Schin .499827869556611403822, 193*4887Schin .504556010751912253908, 194*4887Schin .509261901790523552335, 195*4887Schin .513945751101346104405, 196*4887Schin .518607764208354637958, 197*4887Schin .523248143765158602036, 198*4887Schin .527867089620485785417, 199*4887Schin .532464798869114019908, 200*4887Schin .537041465897345915436, 201*4887Schin .541597282432121573947, 202*4887Schin .546132437597407260909, 203*4887Schin .550647117952394182793, 204*4887Schin .555141507540611200965, 205*4887Schin .559615787935399566777, 206*4887Schin .564070138285387656651, 207*4887Schin .568504735352689749561, 208*4887Schin .572919753562018740922, 209*4887Schin .577315365035246941260, 210*4887Schin .581691739635061821900, 211*4887Schin .586049045003164792433, 212*4887Schin .590387446602107957005, 213*4887Schin .594707107746216934174, 214*4887Schin .599008189645246602594, 215*4887Schin .603290851438941899687, 216*4887Schin .607555250224322662688, 217*4887Schin .611801541106615331955, 218*4887Schin .616029877215623855590, 219*4887Schin .620240409751204424537, 220*4887Schin .624433288012369303032, 221*4887Schin .628608659422752680256, 222*4887Schin .632766669570628437213, 223*4887Schin .636907462236194987781, 224*4887Schin .641031179420679109171, 225*4887Schin .645137961373620782978, 226*4887Schin .649227946625615004450, 227*4887Schin .653301272011958644725, 228*4887Schin .657358072709030238911, 229*4887Schin .661398482245203922502, 230*4887Schin .665422632544505177065, 231*4887Schin .669430653942981734871, 232*4887Schin .673422675212350441142, 233*4887Schin .677398823590920073911, 234*4887Schin .681359224807238206267, 235*4887Schin .685304003098281100392, 236*4887Schin .689233281238557538017, 237*4887Schin .693147180560117703862 238*4887Schin }; 239*4887Schin 240*4887Schin static double logF_tail[N+1] = { 241*4887Schin 0., 242*4887Schin -.00000000000000543229938420049, 243*4887Schin .00000000000000172745674997061, 244*4887Schin -.00000000000001323017818229233, 245*4887Schin -.00000000000001154527628289872, 246*4887Schin -.00000000000000466529469958300, 247*4887Schin .00000000000005148849572685810, 248*4887Schin -.00000000000002532168943117445, 249*4887Schin -.00000000000005213620639136504, 250*4887Schin -.00000000000001819506003016881, 251*4887Schin .00000000000006329065958724544, 252*4887Schin .00000000000008614512936087814, 253*4887Schin -.00000000000007355770219435028, 254*4887Schin .00000000000009638067658552277, 255*4887Schin .00000000000007598636597194141, 256*4887Schin .00000000000002579999128306990, 257*4887Schin -.00000000000004654729747598444, 258*4887Schin -.00000000000007556920687451336, 259*4887Schin .00000000000010195735223708472, 260*4887Schin -.00000000000017319034406422306, 261*4887Schin -.00000000000007718001336828098, 262*4887Schin .00000000000010980754099855238, 263*4887Schin -.00000000000002047235780046195, 264*4887Schin -.00000000000008372091099235912, 265*4887Schin .00000000000014088127937111135, 266*4887Schin .00000000000012869017157588257, 267*4887Schin .00000000000017788850778198106, 268*4887Schin .00000000000006440856150696891, 269*4887Schin .00000000000016132822667240822, 270*4887Schin -.00000000000007540916511956188, 271*4887Schin -.00000000000000036507188831790, 272*4887Schin .00000000000009120937249914984, 273*4887Schin .00000000000018567570959796010, 274*4887Schin -.00000000000003149265065191483, 275*4887Schin -.00000000000009309459495196889, 276*4887Schin .00000000000017914338601329117, 277*4887Schin -.00000000000001302979717330866, 278*4887Schin .00000000000023097385217586939, 279*4887Schin .00000000000023999540484211737, 280*4887Schin .00000000000015393776174455408, 281*4887Schin -.00000000000036870428315837678, 282*4887Schin .00000000000036920375082080089, 283*4887Schin -.00000000000009383417223663699, 284*4887Schin .00000000000009433398189512690, 285*4887Schin .00000000000041481318704258568, 286*4887Schin -.00000000000003792316480209314, 287*4887Schin .00000000000008403156304792424, 288*4887Schin -.00000000000034262934348285429, 289*4887Schin .00000000000043712191957429145, 290*4887Schin -.00000000000010475750058776541, 291*4887Schin -.00000000000011118671389559323, 292*4887Schin .00000000000037549577257259853, 293*4887Schin .00000000000013912841212197565, 294*4887Schin .00000000000010775743037572640, 295*4887Schin .00000000000029391859187648000, 296*4887Schin -.00000000000042790509060060774, 297*4887Schin .00000000000022774076114039555, 298*4887Schin .00000000000010849569622967912, 299*4887Schin -.00000000000023073801945705758, 300*4887Schin .00000000000015761203773969435, 301*4887Schin .00000000000003345710269544082, 302*4887Schin -.00000000000041525158063436123, 303*4887Schin .00000000000032655698896907146, 304*4887Schin -.00000000000044704265010452446, 305*4887Schin .00000000000034527647952039772, 306*4887Schin -.00000000000007048962392109746, 307*4887Schin .00000000000011776978751369214, 308*4887Schin -.00000000000010774341461609578, 309*4887Schin .00000000000021863343293215910, 310*4887Schin .00000000000024132639491333131, 311*4887Schin .00000000000039057462209830700, 312*4887Schin -.00000000000026570679203560751, 313*4887Schin .00000000000037135141919592021, 314*4887Schin -.00000000000017166921336082431, 315*4887Schin -.00000000000028658285157914353, 316*4887Schin -.00000000000023812542263446809, 317*4887Schin .00000000000006576659768580062, 318*4887Schin -.00000000000028210143846181267, 319*4887Schin .00000000000010701931762114254, 320*4887Schin .00000000000018119346366441110, 321*4887Schin .00000000000009840465278232627, 322*4887Schin -.00000000000033149150282752542, 323*4887Schin -.00000000000018302857356041668, 324*4887Schin -.00000000000016207400156744949, 325*4887Schin .00000000000048303314949553201, 326*4887Schin -.00000000000071560553172382115, 327*4887Schin .00000000000088821239518571855, 328*4887Schin -.00000000000030900580513238244, 329*4887Schin -.00000000000061076551972851496, 330*4887Schin .00000000000035659969663347830, 331*4887Schin .00000000000035782396591276383, 332*4887Schin -.00000000000046226087001544578, 333*4887Schin .00000000000062279762917225156, 334*4887Schin .00000000000072838947272065741, 335*4887Schin .00000000000026809646615211673, 336*4887Schin -.00000000000010960825046059278, 337*4887Schin .00000000000002311949383800537, 338*4887Schin -.00000000000058469058005299247, 339*4887Schin -.00000000000002103748251144494, 340*4887Schin -.00000000000023323182945587408, 341*4887Schin -.00000000000042333694288141916, 342*4887Schin -.00000000000043933937969737844, 343*4887Schin .00000000000041341647073835565, 344*4887Schin .00000000000006841763641591466, 345*4887Schin .00000000000047585534004430641, 346*4887Schin .00000000000083679678674757695, 347*4887Schin -.00000000000085763734646658640, 348*4887Schin .00000000000021913281229340092, 349*4887Schin -.00000000000062242842536431148, 350*4887Schin -.00000000000010983594325438430, 351*4887Schin .00000000000065310431377633651, 352*4887Schin -.00000000000047580199021710769, 353*4887Schin -.00000000000037854251265457040, 354*4887Schin .00000000000040939233218678664, 355*4887Schin .00000000000087424383914858291, 356*4887Schin .00000000000025218188456842882, 357*4887Schin -.00000000000003608131360422557, 358*4887Schin -.00000000000050518555924280902, 359*4887Schin .00000000000078699403323355317, 360*4887Schin -.00000000000067020876961949060, 361*4887Schin .00000000000016108575753932458, 362*4887Schin .00000000000058527188436251509, 363*4887Schin -.00000000000035246757297904791, 364*4887Schin -.00000000000018372084495629058, 365*4887Schin .00000000000088606689813494916, 366*4887Schin .00000000000066486268071468700, 367*4887Schin .00000000000063831615170646519, 368*4887Schin .00000000000025144230728376072, 369*4887Schin -.00000000000017239444525614834 370*4887Schin }; 371*4887Schin 372*4887Schin #if !_lib_log 373*4887Schin 374*4887Schin extern double 375*4887Schin #ifdef _ANSI_SOURCE 376*4887Schin log(double x) 377*4887Schin #else 378*4887Schin log(x) double x; 379*4887Schin #endif 380*4887Schin { 381*4887Schin int m, j; 382*4887Schin double F, f, g, q, u, u2, v, zero = 0.0, one = 1.0; 383*4887Schin volatile double u1; 384*4887Schin 385*4887Schin /* Catch special cases */ 386*4887Schin if (x <= 0) 387*4887Schin if (_IEEE && x == zero) /* log(0) = -Inf */ 388*4887Schin return (-one/zero); 389*4887Schin else if (_IEEE) /* log(neg) = NaN */ 390*4887Schin return (zero/zero); 391*4887Schin else if (x == zero) /* NOT REACHED IF _IEEE */ 392*4887Schin return (infnan(-ERANGE)); 393*4887Schin else 394*4887Schin return (infnan(EDOM)); 395*4887Schin else if (!finite(x)) 396*4887Schin if (_IEEE) /* x = NaN, Inf */ 397*4887Schin return (x+x); 398*4887Schin else 399*4887Schin return (infnan(ERANGE)); 400*4887Schin 401*4887Schin /* Argument reduction: 1 <= g < 2; x/2^m = g; */ 402*4887Schin /* y = F*(1 + f/F) for |f| <= 2^-8 */ 403*4887Schin 404*4887Schin m = logb(x); 405*4887Schin g = ldexp(x, -m); 406*4887Schin if (_IEEE && m == -1022) { 407*4887Schin j = logb(g), m += j; 408*4887Schin g = ldexp(g, -j); 409*4887Schin } 410*4887Schin j = N*(g-1) + .5; 411*4887Schin F = (1.0/N) * j + 1; /* F*128 is an integer in [128, 512] */ 412*4887Schin f = g - F; 413*4887Schin 414*4887Schin /* Approximate expansion for log(1+f/F) ~= u + q */ 415*4887Schin g = 1/(2*F+f); 416*4887Schin u = 2*f*g; 417*4887Schin v = u*u; 418*4887Schin q = u*v*(A1 + v*(A2 + v*(A3 + v*A4))); 419*4887Schin 420*4887Schin /* case 1: u1 = u rounded to 2^-43 absolute. Since u < 2^-8, 421*4887Schin * u1 has at most 35 bits, and F*u1 is exact, as F has < 8 bits. 422*4887Schin * It also adds exactly to |m*log2_hi + log_F_head[j] | < 750 423*4887Schin */ 424*4887Schin if (m | j) 425*4887Schin u1 = u + 513, u1 -= 513; 426*4887Schin 427*4887Schin /* case 2: |1-x| < 1/256. The m- and j- dependent terms are zero; 428*4887Schin * u1 = u to 24 bits. 429*4887Schin */ 430*4887Schin else 431*4887Schin u1 = u, TRUNC(u1); 432*4887Schin u2 = (2.0*(f - F*u1) - u1*f) * g; 433*4887Schin /* u1 + u2 = 2f/(2F+f) to extra precision. */ 434*4887Schin 435*4887Schin /* log(x) = log(2^m*F*(1+f/F)) = */ 436*4887Schin /* (m*log2_hi+logF_head[j]+u1) + (m*log2_lo+logF_tail[j]+q); */ 437*4887Schin /* (exact) + (tiny) */ 438*4887Schin 439*4887Schin u1 += m*logF_head[N] + logF_head[j]; /* exact */ 440*4887Schin u2 = (u2 + logF_tail[j]) + q; /* tiny */ 441*4887Schin u2 += logF_tail[N]*m; 442*4887Schin return (u1 + u2); 443*4887Schin } 444*4887Schin 445*4887Schin #endif 446*4887Schin 447*4887Schin /* 448*4887Schin * Extra precision variant, returning struct {double a, b;}; 449*4887Schin * log(x) = a+b to 63 bits, with a is rounded to 26 bits. 450*4887Schin */ 451*4887Schin struct Double 452*4887Schin #ifdef _ANSI_SOURCE 453*4887Schin __log__D(double x) 454*4887Schin #else 455*4887Schin __log__D(x) double x; 456*4887Schin #endif 457*4887Schin { 458*4887Schin int m, j; 459*4887Schin double F, f, g, q, u, v, u2, one = 1.0; 460*4887Schin volatile double u1; 461*4887Schin struct Double r; 462*4887Schin 463*4887Schin /* Argument reduction: 1 <= g < 2; x/2^m = g; */ 464*4887Schin /* y = F*(1 + f/F) for |f| <= 2^-8 */ 465*4887Schin 466*4887Schin m = (int)logb(x); 467*4887Schin g = ldexp(x, -m); 468*4887Schin if (_IEEE && m == -1022) { 469*4887Schin j = (int)logb(g), m += j; 470*4887Schin g = ldexp(g, -j); 471*4887Schin } 472*4887Schin j = (int)(N*(g-1) + .5); 473*4887Schin F = (1.0/N) * j + 1; 474*4887Schin f = g - F; 475*4887Schin 476*4887Schin g = 1/(2*F+f); 477*4887Schin u = 2*f*g; 478*4887Schin v = u*u; 479*4887Schin q = u*v*(A1 + v*(A2 + v*(A3 + v*A4))); 480*4887Schin if (m | j) 481*4887Schin u1 = u + 513, u1 -= 513; 482*4887Schin else 483*4887Schin u1 = u, TRUNC(u1); 484*4887Schin u2 = (2.0*(f - F*u1) - u1*f) * g; 485*4887Schin 486*4887Schin u1 += m*logF_head[N] + logF_head[j]; 487*4887Schin 488*4887Schin u2 += logF_tail[j]; u2 += q; 489*4887Schin u2 += logF_tail[N]*m; 490*4887Schin r.a = u1 + u2; /* Only difference is here */ 491*4887Schin TRUNC(r.a); 492*4887Schin r.b = (u1 - r.a) + u2; 493*4887Schin return (r); 494*4887Schin } 495*4887Schin 496*4887Schin #endif 497