125c28e83SPiotr Jasiukajtis /* 225c28e83SPiotr Jasiukajtis * CDDL HEADER START 325c28e83SPiotr Jasiukajtis * 425c28e83SPiotr Jasiukajtis * The contents of this file are subject to the terms of the 525c28e83SPiotr Jasiukajtis * Common Development and Distribution License (the "License"). 625c28e83SPiotr Jasiukajtis * You may not use this file except in compliance with the License. 725c28e83SPiotr Jasiukajtis * 825c28e83SPiotr Jasiukajtis * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 925c28e83SPiotr Jasiukajtis * or http://www.opensolaris.org/os/licensing. 1025c28e83SPiotr Jasiukajtis * See the License for the specific language governing permissions 1125c28e83SPiotr Jasiukajtis * and limitations under the License. 1225c28e83SPiotr Jasiukajtis * 1325c28e83SPiotr Jasiukajtis * When distributing Covered Code, include this CDDL HEADER in each 1425c28e83SPiotr Jasiukajtis * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 1525c28e83SPiotr Jasiukajtis * If applicable, add the following below this CDDL HEADER, with the 1625c28e83SPiotr Jasiukajtis * fields enclosed by brackets "[]" replaced with your own identifying 1725c28e83SPiotr Jasiukajtis * information: Portions Copyright [yyyy] [name of copyright owner] 1825c28e83SPiotr Jasiukajtis * 1925c28e83SPiotr Jasiukajtis * CDDL HEADER END 2025c28e83SPiotr Jasiukajtis */ 2125c28e83SPiotr Jasiukajtis 2225c28e83SPiotr Jasiukajtis /* 2325c28e83SPiotr Jasiukajtis * Copyright 2011 Nexenta Systems, Inc. All rights reserved. 2425c28e83SPiotr Jasiukajtis */ 2525c28e83SPiotr Jasiukajtis /* 2625c28e83SPiotr Jasiukajtis * Copyright 2006 Sun Microsystems, Inc. All rights reserved. 2725c28e83SPiotr Jasiukajtis * Use is subject to license terms. 2825c28e83SPiotr Jasiukajtis */ 2925c28e83SPiotr Jasiukajtis 30*ddc0e0b5SRichard Lowe #pragma weak __log2 = log2 3125c28e83SPiotr Jasiukajtis 3225c28e83SPiotr Jasiukajtis /* INDENT OFF */ 3325c28e83SPiotr Jasiukajtis /* 3425c28e83SPiotr Jasiukajtis * log2(x) = log(x)/log2 3525c28e83SPiotr Jasiukajtis * 3625c28e83SPiotr Jasiukajtis * Base on Table look-up algorithm with product polynomial 3725c28e83SPiotr Jasiukajtis * approximation for log(x). 3825c28e83SPiotr Jasiukajtis * 3925c28e83SPiotr Jasiukajtis * By K.C. Ng, Nov 29, 2004 4025c28e83SPiotr Jasiukajtis * 4125c28e83SPiotr Jasiukajtis * (a). For x in [1-0.125, 1+0.125], from log.c we have 4225c28e83SPiotr Jasiukajtis * log(x) = f + ((a1*f^2) * 4325c28e83SPiotr Jasiukajtis * ((a2 + (a3*f)*(a4+f)) + (f^3)*(a5+f))) * 4425c28e83SPiotr Jasiukajtis * (((a6 + f*(a7+f)) + (f^3)*(a8+f)) * 4525c28e83SPiotr Jasiukajtis * ((a9 + (a10*f)*(a11+f)) + (f^3)*(a12+f))) 4625c28e83SPiotr Jasiukajtis * where f = x - 1. 4725c28e83SPiotr Jasiukajtis * (i) modify a1 <- a1 / log2 4825c28e83SPiotr Jasiukajtis * (ii) 1/log2 = 1.4426950408889634... 4925c28e83SPiotr Jasiukajtis * = 1.5 - 0.057304959... (4 bit shift) 5025c28e83SPiotr Jasiukajtis * Let lv = 1.5 - 1/log2, then 5125c28e83SPiotr Jasiukajtis * lv = 0.057304959111036592640075318998107956665325, 5225c28e83SPiotr Jasiukajtis * (iii) f*1.5 is exact because f has 3 trailing zero. 5325c28e83SPiotr Jasiukajtis * (iv) Thus, log2(x) = f*1.5 - (lv*f - PPoly) 5425c28e83SPiotr Jasiukajtis * 5525c28e83SPiotr Jasiukajtis * (b). For 0.09375 <= x < 24 5625c28e83SPiotr Jasiukajtis * Let j = (ix - 0x3fb80000) >> 15. Look up Y[j], 1/Y[j], and log(Y[j]) 5725c28e83SPiotr Jasiukajtis * from _TBL_log.c. Then 5825c28e83SPiotr Jasiukajtis * log2(x) = log2(Y[j]) + log2(1 + (x-Y[j])*(1/Y[j])) 5925c28e83SPiotr Jasiukajtis * = log(Y[j])(1/log2) + log2(1 + s) 6025c28e83SPiotr Jasiukajtis * where 6125c28e83SPiotr Jasiukajtis * s = (x-Y[j])*(1/Y[j]) 6225c28e83SPiotr Jasiukajtis * From log.c, we have log(1+s) = 6325c28e83SPiotr Jasiukajtis * 2 2 2 6425c28e83SPiotr Jasiukajtis * (b s) (b + b s + s ) [b + b s + s (b + s)] (b + b s + s ) 6525c28e83SPiotr Jasiukajtis * 1 2 3 4 5 6 7 8 6625c28e83SPiotr Jasiukajtis * 6725c28e83SPiotr Jasiukajtis * By setting b1 <- b1/log2, we have 6825c28e83SPiotr Jasiukajtis * log2(x) = 1.5 * T - (lv * T - POLY(s)) 6925c28e83SPiotr Jasiukajtis * 7025c28e83SPiotr Jasiukajtis * (c). Otherwise, get "n", the exponent of x, and then normalize x to 7125c28e83SPiotr Jasiukajtis * z in [1,2). Then similar to (b) find a Y[i] that matches z to 5.5 7225c28e83SPiotr Jasiukajtis * significant bits. Then 7325c28e83SPiotr Jasiukajtis * log2(x) = n + log2(z). 7425c28e83SPiotr Jasiukajtis * 7525c28e83SPiotr Jasiukajtis * Special cases: 7625c28e83SPiotr Jasiukajtis * log2(x) is NaN with signal if x < 0 (including -INF) ; 7725c28e83SPiotr Jasiukajtis * log2(+INF) is +INF; log2(0) is -INF with signal; 7825c28e83SPiotr Jasiukajtis * log2(NaN) is that NaN with no signal. 7925c28e83SPiotr Jasiukajtis * 8025c28e83SPiotr Jasiukajtis * Maximum error observed: less than 0.84 ulp 8125c28e83SPiotr Jasiukajtis * 8225c28e83SPiotr Jasiukajtis * Constants: 8325c28e83SPiotr Jasiukajtis * The hexadecimal values are the intended ones for the following constants. 8425c28e83SPiotr Jasiukajtis * The decimal values may be used, provided that the compiler will convert 8525c28e83SPiotr Jasiukajtis * from decimal to binary accurately enough to produce the hexadecimal values 8625c28e83SPiotr Jasiukajtis * shown. 8725c28e83SPiotr Jasiukajtis */ 8825c28e83SPiotr Jasiukajtis /* INDENT ON */ 8925c28e83SPiotr Jasiukajtis 9025c28e83SPiotr Jasiukajtis #include "libm.h" 9125c28e83SPiotr Jasiukajtis #include "libm_protos.h" 9225c28e83SPiotr Jasiukajtis 9325c28e83SPiotr Jasiukajtis extern const double _TBL_log[]; 9425c28e83SPiotr Jasiukajtis 9525c28e83SPiotr Jasiukajtis static const double P[] = { 9625c28e83SPiotr Jasiukajtis /* ONE */ 1.0, 9725c28e83SPiotr Jasiukajtis /* TWO52 */ 4503599627370496.0, 9825c28e83SPiotr Jasiukajtis /* LN10V */ 1.4426950408889634073599246810018920433347, /* 1/log10 */ 9925c28e83SPiotr Jasiukajtis /* ZERO */ 0.0, 10025c28e83SPiotr Jasiukajtis /* A1 */ -9.6809362455249638217841932228967194640116e-02, 10125c28e83SPiotr Jasiukajtis /* A2 */ 1.99628461483039965074226529395673424005508422852e+0000, 10225c28e83SPiotr Jasiukajtis /* A3 */ 2.26812367662950720159642514772713184356689453125e+0000, 10325c28e83SPiotr Jasiukajtis /* A4 */ -9.05030639084976384900471657601883634924888610840e-0001, 10425c28e83SPiotr Jasiukajtis /* A5 */ -1.48275767132434044270894446526654064655303955078e+0000, 10525c28e83SPiotr Jasiukajtis /* A6 */ 1.88158320939722756293122074566781520843505859375e+0000, 10625c28e83SPiotr Jasiukajtis /* A7 */ 1.83309386046986411145098827546462416648864746094e+0000, 10725c28e83SPiotr Jasiukajtis /* A8 */ 1.24847063988317086291601754055591300129890441895e+0000, 10825c28e83SPiotr Jasiukajtis /* A9 */ 1.98372421445537705508854742220137268304824829102e+0000, 10925c28e83SPiotr Jasiukajtis /* A10 */ -3.94711735767898475035764249696512706577777862549e-0001, 11025c28e83SPiotr Jasiukajtis /* A11 */ 3.07890395362954372160402272129431366920471191406e+0000, 11125c28e83SPiotr Jasiukajtis /* A12 */ -9.60099585275022149311041630426188930869102478027e-0001, 11225c28e83SPiotr Jasiukajtis /* B1 */ -1.8039695622547469514898963204616532885451e-01, 11325c28e83SPiotr Jasiukajtis /* B2 */ 1.87161713283355151891381127914642725337613123482e+0000, 11425c28e83SPiotr Jasiukajtis /* B3 */ -1.89082956295731507978530316904652863740921020508e+0000, 11525c28e83SPiotr Jasiukajtis /* B4 */ -2.50562891673640253387134180229622870683670043945e+0000, 11625c28e83SPiotr Jasiukajtis /* B5 */ 1.64822828085258366037635369139024987816810607910e+0000, 11725c28e83SPiotr Jasiukajtis /* B6 */ -1.24409107065868340669112512841820716857910156250e+0000, 11825c28e83SPiotr Jasiukajtis /* B7 */ 1.70534231658220414296067701798165217041969299316e+0000, 11925c28e83SPiotr Jasiukajtis /* B8 */ 1.99196833784655646937267192697618156671524047852e+0000, 12025c28e83SPiotr Jasiukajtis /* LGH */ 1.5, 12125c28e83SPiotr Jasiukajtis /* LGL */ 0.057304959111036592640075318998107956665325, 12225c28e83SPiotr Jasiukajtis }; 12325c28e83SPiotr Jasiukajtis 12425c28e83SPiotr Jasiukajtis #define ONE P[0] 12525c28e83SPiotr Jasiukajtis #define TWO52 P[1] 12625c28e83SPiotr Jasiukajtis #define LN10V P[2] 12725c28e83SPiotr Jasiukajtis #define ZERO P[3] 12825c28e83SPiotr Jasiukajtis #define A1 P[4] 12925c28e83SPiotr Jasiukajtis #define A2 P[5] 13025c28e83SPiotr Jasiukajtis #define A3 P[6] 13125c28e83SPiotr Jasiukajtis #define A4 P[7] 13225c28e83SPiotr Jasiukajtis #define A5 P[8] 13325c28e83SPiotr Jasiukajtis #define A6 P[9] 13425c28e83SPiotr Jasiukajtis #define A7 P[10] 13525c28e83SPiotr Jasiukajtis #define A8 P[11] 13625c28e83SPiotr Jasiukajtis #define A9 P[12] 13725c28e83SPiotr Jasiukajtis #define A10 P[13] 13825c28e83SPiotr Jasiukajtis #define A11 P[14] 13925c28e83SPiotr Jasiukajtis #define A12 P[15] 14025c28e83SPiotr Jasiukajtis #define B1 P[16] 14125c28e83SPiotr Jasiukajtis #define B2 P[17] 14225c28e83SPiotr Jasiukajtis #define B3 P[18] 14325c28e83SPiotr Jasiukajtis #define B4 P[19] 14425c28e83SPiotr Jasiukajtis #define B5 P[20] 14525c28e83SPiotr Jasiukajtis #define B6 P[21] 14625c28e83SPiotr Jasiukajtis #define B7 P[22] 14725c28e83SPiotr Jasiukajtis #define B8 P[23] 14825c28e83SPiotr Jasiukajtis #define LGH P[24] 14925c28e83SPiotr Jasiukajtis #define LGL P[25] 15025c28e83SPiotr Jasiukajtis 15125c28e83SPiotr Jasiukajtis double 15225c28e83SPiotr Jasiukajtis log2(double x) { 15325c28e83SPiotr Jasiukajtis int i, hx, ix, n, lx; 15425c28e83SPiotr Jasiukajtis 15525c28e83SPiotr Jasiukajtis n = 0; 15625c28e83SPiotr Jasiukajtis hx = ((int *) &x)[HIWORD]; ix = hx & 0x7fffffff; 15725c28e83SPiotr Jasiukajtis lx = ((int *) &x)[LOWORD]; 15825c28e83SPiotr Jasiukajtis 15925c28e83SPiotr Jasiukajtis /* subnormal,0,negative,inf,nan */ 16025c28e83SPiotr Jasiukajtis if ((hx + 0x100000) < 0x200000) { 16125c28e83SPiotr Jasiukajtis #if defined(FPADD_TRAPS_INCOMPLETE_ON_NAN) 16225c28e83SPiotr Jasiukajtis if (ix >= 0x7ff80000) /* assumes sparc-like QNaN */ 16325c28e83SPiotr Jasiukajtis return (x); /* for Cheetah when x is QNaN */ 16425c28e83SPiotr Jasiukajtis #endif 16525c28e83SPiotr Jasiukajtis if (((hx << 1) | lx) == 0) /* log(0.0) = -inf */ 16625c28e83SPiotr Jasiukajtis return (A5 / fabs(x)); 16725c28e83SPiotr Jasiukajtis if (hx < 0) { /* x < 0 */ 16825c28e83SPiotr Jasiukajtis if (ix >= 0x7ff00000) 16925c28e83SPiotr Jasiukajtis return (x - x); /* x is -inf or NaN */ 17025c28e83SPiotr Jasiukajtis else 17125c28e83SPiotr Jasiukajtis return (ZERO / (x - x)); 17225c28e83SPiotr Jasiukajtis } 17325c28e83SPiotr Jasiukajtis if (((hx - 0x7ff00000) | lx) == 0) /* log(inf) = inf */ 17425c28e83SPiotr Jasiukajtis return (x); 17525c28e83SPiotr Jasiukajtis if (ix >= 0x7ff00000) /* log(NaN) = NaN */ 17625c28e83SPiotr Jasiukajtis return (x - x); 17725c28e83SPiotr Jasiukajtis x *= TWO52; 17825c28e83SPiotr Jasiukajtis n = -52; 17925c28e83SPiotr Jasiukajtis hx = ((int *) &x)[HIWORD]; ix = hx & 0x7fffffff; 18025c28e83SPiotr Jasiukajtis lx = ((int *) &x)[LOWORD]; 18125c28e83SPiotr Jasiukajtis } 18225c28e83SPiotr Jasiukajtis 18325c28e83SPiotr Jasiukajtis /* 0.09375 (0x3fb80000) <= x < 24 (0x40380000) */ 18425c28e83SPiotr Jasiukajtis i = ix >> 19; 18525c28e83SPiotr Jasiukajtis if (i >= 0x7f7 && i <= 0x806) { 18625c28e83SPiotr Jasiukajtis /* 0.875 <= x < 1.125 */ 18725c28e83SPiotr Jasiukajtis if (ix >= 0x3fec0000 && ix < 0x3ff20000) { 18825c28e83SPiotr Jasiukajtis double s, z, r, w; 18925c28e83SPiotr Jasiukajtis s = x - ONE; z = s * s; r = (A10 * s) * (A11 + s); 19025c28e83SPiotr Jasiukajtis w = z * s; 19125c28e83SPiotr Jasiukajtis if (((ix << 12) | lx) == 0) 19225c28e83SPiotr Jasiukajtis return (z); 19325c28e83SPiotr Jasiukajtis else 19425c28e83SPiotr Jasiukajtis return (LGH * s - (LGL * s - ((A1 * z) * 19525c28e83SPiotr Jasiukajtis ((A2 + (A3 * s) * (A4 + s)) + w * (A5 + s))) * 19625c28e83SPiotr Jasiukajtis (((A6 + s * (A7 + s)) + w * (A8 + s)) * 19725c28e83SPiotr Jasiukajtis ((A9 + r) + w * (A12 + s))))); 19825c28e83SPiotr Jasiukajtis } else { 19925c28e83SPiotr Jasiukajtis double *tb, s; 20025c28e83SPiotr Jasiukajtis i = (ix - 0x3fb80000) >> 15; 20125c28e83SPiotr Jasiukajtis tb = (double *) _TBL_log + (i + i + i); 20225c28e83SPiotr Jasiukajtis if (((ix << 12) | lx) == 0) /* 2's power */ 20325c28e83SPiotr Jasiukajtis return ((double) ((ix >> 20) - 0x3ff)); 20425c28e83SPiotr Jasiukajtis s = (x - tb[0]) * tb[1]; 20525c28e83SPiotr Jasiukajtis return (LGH * tb[2] - (LGL * tb[2] - ((B1 * s) * 20625c28e83SPiotr Jasiukajtis (B2 + s * (B3 + s))) * 20725c28e83SPiotr Jasiukajtis (((B4 + s * B5) + (s * s) * (B6 + s)) * 20825c28e83SPiotr Jasiukajtis (B7 + s * (B8 + s))))); 20925c28e83SPiotr Jasiukajtis } 21025c28e83SPiotr Jasiukajtis } else { 21125c28e83SPiotr Jasiukajtis double *tb, dn, s; 21225c28e83SPiotr Jasiukajtis dn = (double) (n + ((ix >> 20) - 0x3ff)); 21325c28e83SPiotr Jasiukajtis ix <<= 12; 21425c28e83SPiotr Jasiukajtis if ((ix | lx) == 0) 21525c28e83SPiotr Jasiukajtis return (dn); 21625c28e83SPiotr Jasiukajtis i = ((unsigned) ix >> 12) | 0x3ff00000; /* scale x to [1,2) */ 21725c28e83SPiotr Jasiukajtis ((int *) &x)[HIWORD] = i; 21825c28e83SPiotr Jasiukajtis i = (i - 0x3fb80000) >> 15; 21925c28e83SPiotr Jasiukajtis tb = (double *) _TBL_log + (i + i + i); 22025c28e83SPiotr Jasiukajtis s = (x - tb[0]) * tb[1]; 22125c28e83SPiotr Jasiukajtis return (dn + (tb[2] * LN10V + ((B1 * s) * 22225c28e83SPiotr Jasiukajtis (B2 + s * (B3 + s))) * 22325c28e83SPiotr Jasiukajtis (((B4 + s * B5) + (s * s) * (B6 + s)) * 22425c28e83SPiotr Jasiukajtis (B7 + s * (B8 + s))))); 22525c28e83SPiotr Jasiukajtis } 22625c28e83SPiotr Jasiukajtis } 227