1570af302Sopenharmony_ci/* origin: OpenBSD /usr/src/lib/libm/src/ld80/e_log2l.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 * Base 2 logarithm, long double precision 19570af302Sopenharmony_ci * 20570af302Sopenharmony_ci * 21570af302Sopenharmony_ci * SYNOPSIS: 22570af302Sopenharmony_ci * 23570af302Sopenharmony_ci * long double x, y, log2l(); 24570af302Sopenharmony_ci * 25570af302Sopenharmony_ci * y = log2l( x ); 26570af302Sopenharmony_ci * 27570af302Sopenharmony_ci * 28570af302Sopenharmony_ci * DESCRIPTION: 29570af302Sopenharmony_ci * 30570af302Sopenharmony_ci * Returns the base 2 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 (natural) 34570af302Sopenharmony_ci * logarithm 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) = 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 30000 9.8e-20 2.7e-20 48570af302Sopenharmony_ci * IEEE exp(+-10000) 70000 5.4e-20 2.3e-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 log2l(long double x) 59570af302Sopenharmony_ci{ 60570af302Sopenharmony_ci return log2(x); 61570af302Sopenharmony_ci} 62570af302Sopenharmony_ci#elif LDBL_MANT_DIG == 64 && LDBL_MAX_EXP == 16384 63570af302Sopenharmony_ci/* Coefficients for ln(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 = 6.2e-22 66570af302Sopenharmony_ci */ 67570af302Sopenharmony_cistatic const long double P[] = { 68570af302Sopenharmony_ci 4.9962495940332550844739E-1L, 69570af302Sopenharmony_ci 1.0767376367209449010438E1L, 70570af302Sopenharmony_ci 7.7671073698359539859595E1L, 71570af302Sopenharmony_ci 2.5620629828144409632571E2L, 72570af302Sopenharmony_ci 4.2401812743503691187826E2L, 73570af302Sopenharmony_ci 3.4258224542413922935104E2L, 74570af302Sopenharmony_ci 1.0747524399916215149070E2L, 75570af302Sopenharmony_ci}; 76570af302Sopenharmony_cistatic const long double Q[] = { 77570af302Sopenharmony_ci/* 1.0000000000000000000000E0,*/ 78570af302Sopenharmony_ci 2.3479774160285863271658E1L, 79570af302Sopenharmony_ci 1.9444210022760132894510E2L, 80570af302Sopenharmony_ci 7.7952888181207260646090E2L, 81570af302Sopenharmony_ci 1.6911722418503949084863E3L, 82570af302Sopenharmony_ci 2.0307734695595183428202E3L, 83570af302Sopenharmony_ci 1.2695660352705325274404E3L, 84570af302Sopenharmony_ci 3.2242573199748645407652E2L, 85570af302Sopenharmony_ci}; 86570af302Sopenharmony_ci 87570af302Sopenharmony_ci/* Coefficients for log(x) = z + z^3 P(z^2)/Q(z^2), 88570af302Sopenharmony_ci * where z = 2(x-1)/(x+1) 89570af302Sopenharmony_ci * 1/sqrt(2) <= x < sqrt(2) 90570af302Sopenharmony_ci * Theoretical peak relative error = 6.16e-22 91570af302Sopenharmony_ci */ 92570af302Sopenharmony_cistatic const long double R[4] = { 93570af302Sopenharmony_ci 1.9757429581415468984296E-3L, 94570af302Sopenharmony_ci-7.1990767473014147232598E-1L, 95570af302Sopenharmony_ci 1.0777257190312272158094E1L, 96570af302Sopenharmony_ci-3.5717684488096787370998E1L, 97570af302Sopenharmony_ci}; 98570af302Sopenharmony_cistatic const long double S[4] = { 99570af302Sopenharmony_ci/* 1.00000000000000000000E0L,*/ 100570af302Sopenharmony_ci-2.6201045551331104417768E1L, 101570af302Sopenharmony_ci 1.9361891836232102174846E2L, 102570af302Sopenharmony_ci-4.2861221385716144629696E2L, 103570af302Sopenharmony_ci}; 104570af302Sopenharmony_ci/* log2(e) - 1 */ 105570af302Sopenharmony_ci#define LOG2EA 4.4269504088896340735992e-1L 106570af302Sopenharmony_ci 107570af302Sopenharmony_ci#define SQRTH 0.70710678118654752440L 108570af302Sopenharmony_ci 109570af302Sopenharmony_cilong double log2l(long double x) 110570af302Sopenharmony_ci{ 111570af302Sopenharmony_ci long double y, z; 112570af302Sopenharmony_ci int e; 113570af302Sopenharmony_ci 114570af302Sopenharmony_ci if (isnan(x)) 115570af302Sopenharmony_ci return x; 116570af302Sopenharmony_ci if (x == INFINITY) 117570af302Sopenharmony_ci return x; 118570af302Sopenharmony_ci if (x <= 0.0) { 119570af302Sopenharmony_ci if (x == 0.0) 120570af302Sopenharmony_ci return -1/(x*x); /* -inf with divbyzero */ 121570af302Sopenharmony_ci return 0/0.0f; /* nan with invalid */ 122570af302Sopenharmony_ci } 123570af302Sopenharmony_ci 124570af302Sopenharmony_ci /* separate mantissa from exponent */ 125570af302Sopenharmony_ci /* Note, frexp is used so that denormal numbers 126570af302Sopenharmony_ci * will be handled properly. 127570af302Sopenharmony_ci */ 128570af302Sopenharmony_ci x = frexpl(x, &e); 129570af302Sopenharmony_ci 130570af302Sopenharmony_ci /* logarithm using log(x) = z + z**3 P(z)/Q(z), 131570af302Sopenharmony_ci * where z = 2(x-1)/x+1) 132570af302Sopenharmony_ci */ 133570af302Sopenharmony_ci if (e > 2 || e < -2) { 134570af302Sopenharmony_ci if (x < SQRTH) { /* 2(2x-1)/(2x+1) */ 135570af302Sopenharmony_ci e -= 1; 136570af302Sopenharmony_ci z = x - 0.5; 137570af302Sopenharmony_ci y = 0.5 * z + 0.5; 138570af302Sopenharmony_ci } else { /* 2 (x-1)/(x+1) */ 139570af302Sopenharmony_ci z = x - 0.5; 140570af302Sopenharmony_ci z -= 0.5; 141570af302Sopenharmony_ci y = 0.5 * x + 0.5; 142570af302Sopenharmony_ci } 143570af302Sopenharmony_ci x = z / y; 144570af302Sopenharmony_ci z = x*x; 145570af302Sopenharmony_ci y = x * (z * __polevll(z, R, 3) / __p1evll(z, S, 3)); 146570af302Sopenharmony_ci goto done; 147570af302Sopenharmony_ci } 148570af302Sopenharmony_ci 149570af302Sopenharmony_ci /* logarithm using log(1+x) = x - .5x**2 + x**3 P(x)/Q(x) */ 150570af302Sopenharmony_ci if (x < SQRTH) { 151570af302Sopenharmony_ci e -= 1; 152570af302Sopenharmony_ci x = 2.0*x - 1.0; 153570af302Sopenharmony_ci } else { 154570af302Sopenharmony_ci x = x - 1.0; 155570af302Sopenharmony_ci } 156570af302Sopenharmony_ci z = x*x; 157570af302Sopenharmony_ci y = x * (z * __polevll(x, P, 6) / __p1evll(x, Q, 7)); 158570af302Sopenharmony_ci y = y - 0.5*z; 159570af302Sopenharmony_ci 160570af302Sopenharmony_cidone: 161570af302Sopenharmony_ci /* Multiply log of fraction by log2(e) 162570af302Sopenharmony_ci * and base 2 exponent by 1 163570af302Sopenharmony_ci * 164570af302Sopenharmony_ci * ***CAUTION*** 165570af302Sopenharmony_ci * 166570af302Sopenharmony_ci * This sequence of operations is critical and it may 167570af302Sopenharmony_ci * be horribly defeated by some compiler optimizers. 168570af302Sopenharmony_ci */ 169570af302Sopenharmony_ci z = y * LOG2EA; 170570af302Sopenharmony_ci z += x * LOG2EA; 171570af302Sopenharmony_ci z += y; 172570af302Sopenharmony_ci z += x; 173570af302Sopenharmony_ci z += e; 174570af302Sopenharmony_ci return z; 175570af302Sopenharmony_ci} 176570af302Sopenharmony_ci#elif LDBL_MANT_DIG == 113 && LDBL_MAX_EXP == 16384 177570af302Sopenharmony_ci// TODO: broken implementation to make things compile 178570af302Sopenharmony_cilong double log2l(long double x) 179570af302Sopenharmony_ci{ 180570af302Sopenharmony_ci return log2(x); 181570af302Sopenharmony_ci} 182570af302Sopenharmony_ci#endif 183