1570af302Sopenharmony_ci/* origin: OpenBSD /usr/src/lib/libm/src/ld80/e_logl.c */ 2570af302Sopenharmony_ci/* 3570af302Sopenharmony_ci * Copyright (c) 2008 Stephen L. Moshier <steve@moshier.net> 4570af302Sopenharmony_ci * 5570af302Sopenharmony_ci * Permission to use, copy, modify, and distribute this software for any 6570af302Sopenharmony_ci * purpose with or without fee is hereby granted, provided that the above 7570af302Sopenharmony_ci * copyright notice and this permission notice appear in all copies. 8570af302Sopenharmony_ci * 9570af302Sopenharmony_ci * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 10570af302Sopenharmony_ci * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 11570af302Sopenharmony_ci * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 12570af302Sopenharmony_ci * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 13570af302Sopenharmony_ci * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 14570af302Sopenharmony_ci * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 15570af302Sopenharmony_ci * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 16570af302Sopenharmony_ci */ 17570af302Sopenharmony_ci/* 18570af302Sopenharmony_ci * Natural logarithm, long double precision 19570af302Sopenharmony_ci * 20570af302Sopenharmony_ci * 21570af302Sopenharmony_ci * SYNOPSIS: 22570af302Sopenharmony_ci * 23570af302Sopenharmony_ci * long double x, y, logl(); 24570af302Sopenharmony_ci * 25570af302Sopenharmony_ci * y = logl( x ); 26570af302Sopenharmony_ci * 27570af302Sopenharmony_ci * 28570af302Sopenharmony_ci * DESCRIPTION: 29570af302Sopenharmony_ci * 30570af302Sopenharmony_ci * Returns the base e (2.718...) logarithm of x. 31570af302Sopenharmony_ci * 32570af302Sopenharmony_ci * The argument is separated into its exponent and fractional 33570af302Sopenharmony_ci * parts. If the exponent is between -1 and +1, the logarithm 34570af302Sopenharmony_ci * of the fraction is approximated by 35570af302Sopenharmony_ci * 36570af302Sopenharmony_ci * log(1+x) = x - 0.5 x**2 + x**3 P(x)/Q(x). 37570af302Sopenharmony_ci * 38570af302Sopenharmony_ci * Otherwise, setting z = 2(x-1)/(x+1), 39570af302Sopenharmony_ci * 40570af302Sopenharmony_ci * log(x) = log(1+z/2) - log(1-z/2) = z + z**3 P(z)/Q(z). 41570af302Sopenharmony_ci * 42570af302Sopenharmony_ci * 43570af302Sopenharmony_ci * ACCURACY: 44570af302Sopenharmony_ci * 45570af302Sopenharmony_ci * Relative error: 46570af302Sopenharmony_ci * arithmetic domain # trials peak rms 47570af302Sopenharmony_ci * IEEE 0.5, 2.0 150000 8.71e-20 2.75e-20 48570af302Sopenharmony_ci * IEEE exp(+-10000) 100000 5.39e-20 2.34e-20 49570af302Sopenharmony_ci * 50570af302Sopenharmony_ci * In the tests over the interval exp(+-10000), the logarithms 51570af302Sopenharmony_ci * of the random arguments were uniformly distributed over 52570af302Sopenharmony_ci * [-10000, +10000]. 53570af302Sopenharmony_ci */ 54570af302Sopenharmony_ci 55570af302Sopenharmony_ci#include "libm.h" 56570af302Sopenharmony_ci 57570af302Sopenharmony_ci#if LDBL_MANT_DIG == 53 && LDBL_MAX_EXP == 1024 58570af302Sopenharmony_cilong double logl(long double x) 59570af302Sopenharmony_ci{ 60570af302Sopenharmony_ci return log(x); 61570af302Sopenharmony_ci} 62570af302Sopenharmony_ci#elif LDBL_MANT_DIG == 64 && LDBL_MAX_EXP == 16384 63570af302Sopenharmony_ci/* Coefficients for log(1+x) = x - x**2/2 + x**3 P(x)/Q(x) 64570af302Sopenharmony_ci * 1/sqrt(2) <= x < sqrt(2) 65570af302Sopenharmony_ci * Theoretical peak relative error = 2.32e-20 66570af302Sopenharmony_ci */ 67570af302Sopenharmony_cistatic const long double P[] = { 68570af302Sopenharmony_ci 4.5270000862445199635215E-5L, 69570af302Sopenharmony_ci 4.9854102823193375972212E-1L, 70570af302Sopenharmony_ci 6.5787325942061044846969E0L, 71570af302Sopenharmony_ci 2.9911919328553073277375E1L, 72570af302Sopenharmony_ci 6.0949667980987787057556E1L, 73570af302Sopenharmony_ci 5.7112963590585538103336E1L, 74570af302Sopenharmony_ci 2.0039553499201281259648E1L, 75570af302Sopenharmony_ci}; 76570af302Sopenharmony_cistatic const long double Q[] = { 77570af302Sopenharmony_ci/* 1.0000000000000000000000E0,*/ 78570af302Sopenharmony_ci 1.5062909083469192043167E1L, 79570af302Sopenharmony_ci 8.3047565967967209469434E1L, 80570af302Sopenharmony_ci 2.2176239823732856465394E2L, 81570af302Sopenharmony_ci 3.0909872225312059774938E2L, 82570af302Sopenharmony_ci 2.1642788614495947685003E2L, 83570af302Sopenharmony_ci 6.0118660497603843919306E1L, 84570af302Sopenharmony_ci}; 85570af302Sopenharmony_ci 86570af302Sopenharmony_ci/* Coefficients for log(x) = z + z^3 P(z^2)/Q(z^2), 87570af302Sopenharmony_ci * where z = 2(x-1)/(x+1) 88570af302Sopenharmony_ci * 1/sqrt(2) <= x < sqrt(2) 89570af302Sopenharmony_ci * Theoretical peak relative error = 6.16e-22 90570af302Sopenharmony_ci */ 91570af302Sopenharmony_cistatic const long double R[4] = { 92570af302Sopenharmony_ci 1.9757429581415468984296E-3L, 93570af302Sopenharmony_ci-7.1990767473014147232598E-1L, 94570af302Sopenharmony_ci 1.0777257190312272158094E1L, 95570af302Sopenharmony_ci-3.5717684488096787370998E1L, 96570af302Sopenharmony_ci}; 97570af302Sopenharmony_cistatic const long double S[4] = { 98570af302Sopenharmony_ci/* 1.00000000000000000000E0L,*/ 99570af302Sopenharmony_ci-2.6201045551331104417768E1L, 100570af302Sopenharmony_ci 1.9361891836232102174846E2L, 101570af302Sopenharmony_ci-4.2861221385716144629696E2L, 102570af302Sopenharmony_ci}; 103570af302Sopenharmony_cistatic const long double C1 = 6.9314575195312500000000E-1L; 104570af302Sopenharmony_cistatic const long double C2 = 1.4286068203094172321215E-6L; 105570af302Sopenharmony_ci 106570af302Sopenharmony_ci#define SQRTH 0.70710678118654752440L 107570af302Sopenharmony_ci 108570af302Sopenharmony_cilong double logl(long double x) 109570af302Sopenharmony_ci{ 110570af302Sopenharmony_ci long double y, z; 111570af302Sopenharmony_ci int e; 112570af302Sopenharmony_ci 113570af302Sopenharmony_ci if (isnan(x)) 114570af302Sopenharmony_ci return x; 115570af302Sopenharmony_ci if (x == INFINITY) 116570af302Sopenharmony_ci return x; 117570af302Sopenharmony_ci if (x <= 0.0) { 118570af302Sopenharmony_ci if (x == 0.0) 119570af302Sopenharmony_ci return -1/(x*x); /* -inf with divbyzero */ 120570af302Sopenharmony_ci return 0/0.0f; /* nan with invalid */ 121570af302Sopenharmony_ci } 122570af302Sopenharmony_ci 123570af302Sopenharmony_ci /* separate mantissa from exponent */ 124570af302Sopenharmony_ci /* Note, frexp is used so that denormal numbers 125570af302Sopenharmony_ci * will be handled properly. 126570af302Sopenharmony_ci */ 127570af302Sopenharmony_ci x = frexpl(x, &e); 128570af302Sopenharmony_ci 129570af302Sopenharmony_ci /* logarithm using log(x) = z + z**3 P(z)/Q(z), 130570af302Sopenharmony_ci * where z = 2(x-1)/(x+1) 131570af302Sopenharmony_ci */ 132570af302Sopenharmony_ci if (e > 2 || e < -2) { 133570af302Sopenharmony_ci if (x < SQRTH) { /* 2(2x-1)/(2x+1) */ 134570af302Sopenharmony_ci e -= 1; 135570af302Sopenharmony_ci z = x - 0.5; 136570af302Sopenharmony_ci y = 0.5 * z + 0.5; 137570af302Sopenharmony_ci } else { /* 2 (x-1)/(x+1) */ 138570af302Sopenharmony_ci z = x - 0.5; 139570af302Sopenharmony_ci z -= 0.5; 140570af302Sopenharmony_ci y = 0.5 * x + 0.5; 141570af302Sopenharmony_ci } 142570af302Sopenharmony_ci x = z / y; 143570af302Sopenharmony_ci z = x*x; 144570af302Sopenharmony_ci z = x * (z * __polevll(z, R, 3) / __p1evll(z, S, 3)); 145570af302Sopenharmony_ci z = z + e * C2; 146570af302Sopenharmony_ci z = z + x; 147570af302Sopenharmony_ci z = z + e * C1; 148570af302Sopenharmony_ci return z; 149570af302Sopenharmony_ci } 150570af302Sopenharmony_ci 151570af302Sopenharmony_ci /* logarithm using log(1+x) = x - .5x**2 + x**3 P(x)/Q(x) */ 152570af302Sopenharmony_ci if (x < SQRTH) { 153570af302Sopenharmony_ci e -= 1; 154570af302Sopenharmony_ci x = 2.0*x - 1.0; 155570af302Sopenharmony_ci } else { 156570af302Sopenharmony_ci x = x - 1.0; 157570af302Sopenharmony_ci } 158570af302Sopenharmony_ci z = x*x; 159570af302Sopenharmony_ci y = x * (z * __polevll(x, P, 6) / __p1evll(x, Q, 6)); 160570af302Sopenharmony_ci y = y + e * C2; 161570af302Sopenharmony_ci z = y - 0.5*z; 162570af302Sopenharmony_ci /* Note, the sum of above terms does not exceed x/4, 163570af302Sopenharmony_ci * so it contributes at most about 1/4 lsb to the error. 164570af302Sopenharmony_ci */ 165570af302Sopenharmony_ci z = z + x; 166570af302Sopenharmony_ci z = z + e * C1; /* This sum has an error of 1/2 lsb. */ 167570af302Sopenharmony_ci return z; 168570af302Sopenharmony_ci} 169570af302Sopenharmony_ci#elif LDBL_MANT_DIG == 113 && LDBL_MAX_EXP == 16384 170570af302Sopenharmony_ci// TODO: broken implementation to make things compile 171570af302Sopenharmony_cilong double logl(long double x) 172570af302Sopenharmony_ci{ 173570af302Sopenharmony_ci return log(x); 174570af302Sopenharmony_ci} 175570af302Sopenharmony_ci#endif 176