1570af302Sopenharmony_ci/* origin: OpenBSD /usr/src/lib/libm/src/ld80/s_log1pl.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 * Relative error logarithm 19570af302Sopenharmony_ci * Natural logarithm of 1+x, long double precision 20570af302Sopenharmony_ci * 21570af302Sopenharmony_ci * 22570af302Sopenharmony_ci * SYNOPSIS: 23570af302Sopenharmony_ci * 24570af302Sopenharmony_ci * long double x, y, log1pl(); 25570af302Sopenharmony_ci * 26570af302Sopenharmony_ci * y = log1pl( x ); 27570af302Sopenharmony_ci * 28570af302Sopenharmony_ci * 29570af302Sopenharmony_ci * DESCRIPTION: 30570af302Sopenharmony_ci * 31570af302Sopenharmony_ci * Returns the base e (2.718...) logarithm of 1+x. 32570af302Sopenharmony_ci * 33570af302Sopenharmony_ci * The argument 1+x is separated into its exponent and fractional 34570af302Sopenharmony_ci * parts. If the exponent is between -1 and +1, the logarithm 35570af302Sopenharmony_ci * of the fraction is approximated by 36570af302Sopenharmony_ci * 37570af302Sopenharmony_ci * log(1+x) = x - 0.5 x^2 + x^3 P(x)/Q(x). 38570af302Sopenharmony_ci * 39570af302Sopenharmony_ci * Otherwise, setting z = 2(x-1)/x+1), 40570af302Sopenharmony_ci * 41570af302Sopenharmony_ci * log(x) = z + z^3 P(z)/Q(z). 42570af302Sopenharmony_ci * 43570af302Sopenharmony_ci * 44570af302Sopenharmony_ci * ACCURACY: 45570af302Sopenharmony_ci * 46570af302Sopenharmony_ci * Relative error: 47570af302Sopenharmony_ci * arithmetic domain # trials peak rms 48570af302Sopenharmony_ci * IEEE -1.0, 9.0 100000 8.2e-20 2.5e-20 49570af302Sopenharmony_ci */ 50570af302Sopenharmony_ci 51570af302Sopenharmony_ci#include "libm.h" 52570af302Sopenharmony_ci 53570af302Sopenharmony_ci#if LDBL_MANT_DIG == 53 && LDBL_MAX_EXP == 1024 54570af302Sopenharmony_cilong double log1pl(long double x) 55570af302Sopenharmony_ci{ 56570af302Sopenharmony_ci return log1p(x); 57570af302Sopenharmony_ci} 58570af302Sopenharmony_ci#elif LDBL_MANT_DIG == 64 && LDBL_MAX_EXP == 16384 59570af302Sopenharmony_ci/* Coefficients for log(1+x) = x - x^2 / 2 + x^3 P(x)/Q(x) 60570af302Sopenharmony_ci * 1/sqrt(2) <= x < sqrt(2) 61570af302Sopenharmony_ci * Theoretical peak relative error = 2.32e-20 62570af302Sopenharmony_ci */ 63570af302Sopenharmony_cistatic const long double P[] = { 64570af302Sopenharmony_ci 4.5270000862445199635215E-5L, 65570af302Sopenharmony_ci 4.9854102823193375972212E-1L, 66570af302Sopenharmony_ci 6.5787325942061044846969E0L, 67570af302Sopenharmony_ci 2.9911919328553073277375E1L, 68570af302Sopenharmony_ci 6.0949667980987787057556E1L, 69570af302Sopenharmony_ci 5.7112963590585538103336E1L, 70570af302Sopenharmony_ci 2.0039553499201281259648E1L, 71570af302Sopenharmony_ci}; 72570af302Sopenharmony_cistatic const long double Q[] = { 73570af302Sopenharmony_ci/* 1.0000000000000000000000E0,*/ 74570af302Sopenharmony_ci 1.5062909083469192043167E1L, 75570af302Sopenharmony_ci 8.3047565967967209469434E1L, 76570af302Sopenharmony_ci 2.2176239823732856465394E2L, 77570af302Sopenharmony_ci 3.0909872225312059774938E2L, 78570af302Sopenharmony_ci 2.1642788614495947685003E2L, 79570af302Sopenharmony_ci 6.0118660497603843919306E1L, 80570af302Sopenharmony_ci}; 81570af302Sopenharmony_ci 82570af302Sopenharmony_ci/* Coefficients for log(x) = z + z^3 P(z^2)/Q(z^2), 83570af302Sopenharmony_ci * where z = 2(x-1)/(x+1) 84570af302Sopenharmony_ci * 1/sqrt(2) <= x < sqrt(2) 85570af302Sopenharmony_ci * Theoretical peak relative error = 6.16e-22 86570af302Sopenharmony_ci */ 87570af302Sopenharmony_cistatic const long double R[4] = { 88570af302Sopenharmony_ci 1.9757429581415468984296E-3L, 89570af302Sopenharmony_ci-7.1990767473014147232598E-1L, 90570af302Sopenharmony_ci 1.0777257190312272158094E1L, 91570af302Sopenharmony_ci-3.5717684488096787370998E1L, 92570af302Sopenharmony_ci}; 93570af302Sopenharmony_cistatic const long double S[4] = { 94570af302Sopenharmony_ci/* 1.00000000000000000000E0L,*/ 95570af302Sopenharmony_ci-2.6201045551331104417768E1L, 96570af302Sopenharmony_ci 1.9361891836232102174846E2L, 97570af302Sopenharmony_ci-4.2861221385716144629696E2L, 98570af302Sopenharmony_ci}; 99570af302Sopenharmony_cistatic const long double C1 = 6.9314575195312500000000E-1L; 100570af302Sopenharmony_cistatic const long double C2 = 1.4286068203094172321215E-6L; 101570af302Sopenharmony_ci 102570af302Sopenharmony_ci#define SQRTH 0.70710678118654752440L 103570af302Sopenharmony_ci 104570af302Sopenharmony_cilong double log1pl(long double xm1) 105570af302Sopenharmony_ci{ 106570af302Sopenharmony_ci long double x, y, z; 107570af302Sopenharmony_ci int e; 108570af302Sopenharmony_ci 109570af302Sopenharmony_ci if (isnan(xm1)) 110570af302Sopenharmony_ci return xm1; 111570af302Sopenharmony_ci if (xm1 == INFINITY) 112570af302Sopenharmony_ci return xm1; 113570af302Sopenharmony_ci if (xm1 == 0.0) 114570af302Sopenharmony_ci return xm1; 115570af302Sopenharmony_ci 116570af302Sopenharmony_ci x = xm1 + 1.0; 117570af302Sopenharmony_ci 118570af302Sopenharmony_ci /* Test for domain errors. */ 119570af302Sopenharmony_ci if (x <= 0.0) { 120570af302Sopenharmony_ci if (x == 0.0) 121570af302Sopenharmony_ci return -1/(x*x); /* -inf with divbyzero */ 122570af302Sopenharmony_ci return 0/0.0f; /* nan with invalid */ 123570af302Sopenharmony_ci } 124570af302Sopenharmony_ci 125570af302Sopenharmony_ci /* Separate mantissa from exponent. 126570af302Sopenharmony_ci Use frexp so that denormal numbers will be handled properly. */ 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 if (e > 2 || e < -2) { 132570af302Sopenharmony_ci if (x < SQRTH) { /* 2(2x-1)/(2x+1) */ 133570af302Sopenharmony_ci e -= 1; 134570af302Sopenharmony_ci z = x - 0.5; 135570af302Sopenharmony_ci y = 0.5 * z + 0.5; 136570af302Sopenharmony_ci } else { /* 2 (x-1)/(x+1) */ 137570af302Sopenharmony_ci z = x - 0.5; 138570af302Sopenharmony_ci z -= 0.5; 139570af302Sopenharmony_ci y = 0.5 * x + 0.5; 140570af302Sopenharmony_ci } 141570af302Sopenharmony_ci x = z / y; 142570af302Sopenharmony_ci z = x*x; 143570af302Sopenharmony_ci z = x * (z * __polevll(z, R, 3) / __p1evll(z, S, 3)); 144570af302Sopenharmony_ci z = z + e * C2; 145570af302Sopenharmony_ci z = z + x; 146570af302Sopenharmony_ci z = z + e * C1; 147570af302Sopenharmony_ci return z; 148570af302Sopenharmony_ci } 149570af302Sopenharmony_ci 150570af302Sopenharmony_ci /* logarithm using log(1+x) = x - .5x**2 + x**3 P(x)/Q(x) */ 151570af302Sopenharmony_ci if (x < SQRTH) { 152570af302Sopenharmony_ci e -= 1; 153570af302Sopenharmony_ci if (e != 0) 154570af302Sopenharmony_ci x = 2.0 * x - 1.0; 155570af302Sopenharmony_ci else 156570af302Sopenharmony_ci x = xm1; 157570af302Sopenharmony_ci } else { 158570af302Sopenharmony_ci if (e != 0) 159570af302Sopenharmony_ci x = x - 1.0; 160570af302Sopenharmony_ci else 161570af302Sopenharmony_ci x = xm1; 162570af302Sopenharmony_ci } 163570af302Sopenharmony_ci z = x*x; 164570af302Sopenharmony_ci y = x * (z * __polevll(x, P, 6) / __p1evll(x, Q, 6)); 165570af302Sopenharmony_ci y = y + e * C2; 166570af302Sopenharmony_ci z = y - 0.5 * z; 167570af302Sopenharmony_ci z = z + x; 168570af302Sopenharmony_ci z = z + e * C1; 169570af302Sopenharmony_ci return z; 170570af302Sopenharmony_ci} 171570af302Sopenharmony_ci#elif LDBL_MANT_DIG == 113 && LDBL_MAX_EXP == 16384 172570af302Sopenharmony_ci// TODO: broken implementation to make things compile 173570af302Sopenharmony_cilong double log1pl(long double x) 174570af302Sopenharmony_ci{ 175570af302Sopenharmony_ci return log1p(x); 176570af302Sopenharmony_ci} 177570af302Sopenharmony_ci#endif 178