1570af302Sopenharmony_ci/* origin: FreeBSD /usr/src/lib/msun/src/e_log10.c */ 2570af302Sopenharmony_ci/* 3570af302Sopenharmony_ci * ==================================================== 4570af302Sopenharmony_ci * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved. 5570af302Sopenharmony_ci * 6570af302Sopenharmony_ci * Developed at SunSoft, a Sun Microsystems, Inc. business. 7570af302Sopenharmony_ci * Permission to use, copy, modify, and distribute this 8570af302Sopenharmony_ci * software is freely granted, provided that this notice 9570af302Sopenharmony_ci * is preserved. 10570af302Sopenharmony_ci * ==================================================== 11570af302Sopenharmony_ci */ 12570af302Sopenharmony_ci/* 13570af302Sopenharmony_ci * Return the base 10 logarithm of x. See log.c for most comments. 14570af302Sopenharmony_ci * 15570af302Sopenharmony_ci * Reduce x to 2^k (1+f) and calculate r = log(1+f) - f + f*f/2 16570af302Sopenharmony_ci * as in log.c, then combine and scale in extra precision: 17570af302Sopenharmony_ci * log10(x) = (f - f*f/2 + r)/log(10) + k*log10(2) 18570af302Sopenharmony_ci */ 19570af302Sopenharmony_ci 20570af302Sopenharmony_ci#include <math.h> 21570af302Sopenharmony_ci#include <stdint.h> 22570af302Sopenharmony_ci 23570af302Sopenharmony_cistatic const double 24570af302Sopenharmony_ciivln10hi = 4.34294481878168880939e-01, /* 0x3fdbcb7b, 0x15200000 */ 25570af302Sopenharmony_ciivln10lo = 2.50829467116452752298e-11, /* 0x3dbb9438, 0xca9aadd5 */ 26570af302Sopenharmony_cilog10_2hi = 3.01029995663611771306e-01, /* 0x3FD34413, 0x509F6000 */ 27570af302Sopenharmony_cilog10_2lo = 3.69423907715893078616e-13, /* 0x3D59FEF3, 0x11F12B36 */ 28570af302Sopenharmony_ciLg1 = 6.666666666666735130e-01, /* 3FE55555 55555593 */ 29570af302Sopenharmony_ciLg2 = 3.999999999940941908e-01, /* 3FD99999 9997FA04 */ 30570af302Sopenharmony_ciLg3 = 2.857142874366239149e-01, /* 3FD24924 94229359 */ 31570af302Sopenharmony_ciLg4 = 2.222219843214978396e-01, /* 3FCC71C5 1D8E78AF */ 32570af302Sopenharmony_ciLg5 = 1.818357216161805012e-01, /* 3FC74664 96CB03DE */ 33570af302Sopenharmony_ciLg6 = 1.531383769920937332e-01, /* 3FC39A09 D078C69F */ 34570af302Sopenharmony_ciLg7 = 1.479819860511658591e-01; /* 3FC2F112 DF3E5244 */ 35570af302Sopenharmony_ci 36570af302Sopenharmony_cidouble log10(double x) 37570af302Sopenharmony_ci{ 38570af302Sopenharmony_ci union {double f; uint64_t i;} u = {x}; 39570af302Sopenharmony_ci double_t hfsq,f,s,z,R,w,t1,t2,dk,y,hi,lo,val_hi,val_lo; 40570af302Sopenharmony_ci uint32_t hx; 41570af302Sopenharmony_ci int k; 42570af302Sopenharmony_ci 43570af302Sopenharmony_ci hx = u.i>>32; 44570af302Sopenharmony_ci k = 0; 45570af302Sopenharmony_ci if (hx < 0x00100000 || hx>>31) { 46570af302Sopenharmony_ci if (u.i<<1 == 0) 47570af302Sopenharmony_ci return -1/(x*x); /* log(+-0)=-inf */ 48570af302Sopenharmony_ci if (hx>>31) 49570af302Sopenharmony_ci return (x-x)/0.0; /* log(-#) = NaN */ 50570af302Sopenharmony_ci /* subnormal number, scale x up */ 51570af302Sopenharmony_ci k -= 54; 52570af302Sopenharmony_ci x *= 0x1p54; 53570af302Sopenharmony_ci u.f = x; 54570af302Sopenharmony_ci hx = u.i>>32; 55570af302Sopenharmony_ci } else if (hx >= 0x7ff00000) { 56570af302Sopenharmony_ci return x; 57570af302Sopenharmony_ci } else if (hx == 0x3ff00000 && u.i<<32 == 0) 58570af302Sopenharmony_ci return 0; 59570af302Sopenharmony_ci 60570af302Sopenharmony_ci /* reduce x into [sqrt(2)/2, sqrt(2)] */ 61570af302Sopenharmony_ci hx += 0x3ff00000 - 0x3fe6a09e; 62570af302Sopenharmony_ci k += (int)(hx>>20) - 0x3ff; 63570af302Sopenharmony_ci hx = (hx&0x000fffff) + 0x3fe6a09e; 64570af302Sopenharmony_ci u.i = (uint64_t)hx<<32 | (u.i&0xffffffff); 65570af302Sopenharmony_ci x = u.f; 66570af302Sopenharmony_ci 67570af302Sopenharmony_ci f = x - 1.0; 68570af302Sopenharmony_ci hfsq = 0.5*f*f; 69570af302Sopenharmony_ci s = f/(2.0+f); 70570af302Sopenharmony_ci z = s*s; 71570af302Sopenharmony_ci w = z*z; 72570af302Sopenharmony_ci t1 = w*(Lg2+w*(Lg4+w*Lg6)); 73570af302Sopenharmony_ci t2 = z*(Lg1+w*(Lg3+w*(Lg5+w*Lg7))); 74570af302Sopenharmony_ci R = t2 + t1; 75570af302Sopenharmony_ci 76570af302Sopenharmony_ci /* See log2.c for details. */ 77570af302Sopenharmony_ci /* hi+lo = f - hfsq + s*(hfsq+R) ~ log(1+f) */ 78570af302Sopenharmony_ci hi = f - hfsq; 79570af302Sopenharmony_ci u.f = hi; 80570af302Sopenharmony_ci u.i &= (uint64_t)-1<<32; 81570af302Sopenharmony_ci hi = u.f; 82570af302Sopenharmony_ci lo = f - hi - hfsq + s*(hfsq+R); 83570af302Sopenharmony_ci 84570af302Sopenharmony_ci /* val_hi+val_lo ~ log10(1+f) + k*log10(2) */ 85570af302Sopenharmony_ci val_hi = hi*ivln10hi; 86570af302Sopenharmony_ci dk = k; 87570af302Sopenharmony_ci y = dk*log10_2hi; 88570af302Sopenharmony_ci val_lo = dk*log10_2lo + (lo+hi)*ivln10lo + lo*ivln10hi; 89570af302Sopenharmony_ci 90570af302Sopenharmony_ci /* 91570af302Sopenharmony_ci * Extra precision in for adding y is not strictly needed 92570af302Sopenharmony_ci * since there is no very large cancellation near x = sqrt(2) or 93570af302Sopenharmony_ci * x = 1/sqrt(2), but we do it anyway since it costs little on CPUs 94570af302Sopenharmony_ci * with some parallelism and it reduces the error for many args. 95570af302Sopenharmony_ci */ 96570af302Sopenharmony_ci w = y + val_hi; 97570af302Sopenharmony_ci val_lo += (y - w) + val_hi; 98570af302Sopenharmony_ci val_hi = w; 99570af302Sopenharmony_ci 100570af302Sopenharmony_ci return val_lo + val_hi; 101570af302Sopenharmony_ci} 102