xref: /openbsd/lib/libm/src/ld128/s_log1pl.c (revision 2f2c0062)
1*2f2c0062Sguenther /*	$OpenBSD: s_log1pl.c,v 1.2 2016/09/12 19:47:02 guenther Exp $	*/
249393c00Smartynas 
349393c00Smartynas /*
449393c00Smartynas  * Copyright (c) 2008 Stephen L. Moshier <steve@moshier.net>
549393c00Smartynas  *
649393c00Smartynas  * Permission to use, copy, modify, and distribute this software for any
749393c00Smartynas  * purpose with or without fee is hereby granted, provided that the above
849393c00Smartynas  * copyright notice and this permission notice appear in all copies.
949393c00Smartynas  *
1049393c00Smartynas  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
1149393c00Smartynas  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
1249393c00Smartynas  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
1349393c00Smartynas  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
1449393c00Smartynas  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
1549393c00Smartynas  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
1649393c00Smartynas  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
1749393c00Smartynas  */
1849393c00Smartynas 
1949393c00Smartynas /*							log1pl.c
2049393c00Smartynas  *
2149393c00Smartynas  *      Relative error logarithm
2249393c00Smartynas  *	Natural logarithm of 1+x, 128-bit long double precision
2349393c00Smartynas  *
2449393c00Smartynas  *
2549393c00Smartynas  *
2649393c00Smartynas  * SYNOPSIS:
2749393c00Smartynas  *
2849393c00Smartynas  * long double x, y, log1pl();
2949393c00Smartynas  *
3049393c00Smartynas  * y = log1pl( x );
3149393c00Smartynas  *
3249393c00Smartynas  *
3349393c00Smartynas  *
3449393c00Smartynas  * DESCRIPTION:
3549393c00Smartynas  *
3649393c00Smartynas  * Returns the base e (2.718...) logarithm of 1+x.
3749393c00Smartynas  *
3849393c00Smartynas  * The argument 1+x is separated into its exponent and fractional
3949393c00Smartynas  * parts.  If the exponent is between -1 and +1, the logarithm
4049393c00Smartynas  * of the fraction is approximated by
4149393c00Smartynas  *
4249393c00Smartynas  *     log(1+x) = x - 0.5 x^2 + x^3 P(x)/Q(x).
4349393c00Smartynas  *
4449393c00Smartynas  * Otherwise, setting  z = 2(w-1)/(w+1),
4549393c00Smartynas  *
4649393c00Smartynas  *     log(w) = z + z^3 P(z)/Q(z).
4749393c00Smartynas  *
4849393c00Smartynas  *
4949393c00Smartynas  *
5049393c00Smartynas  * ACCURACY:
5149393c00Smartynas  *
5249393c00Smartynas  *                      Relative error:
5349393c00Smartynas  * arithmetic   domain     # trials      peak         rms
5449393c00Smartynas  *    IEEE      -1, 8       100000      1.9e-34     4.3e-35
5549393c00Smartynas  */
5649393c00Smartynas 
5749393c00Smartynas #include <math.h>
5849393c00Smartynas 
5949393c00Smartynas #include "math_private.h"
6049393c00Smartynas 
6149393c00Smartynas /* Coefficients for log(1+x) = x - x^2 / 2 + x^3 P(x)/Q(x)
6249393c00Smartynas  * 1/sqrt(2) <= 1+x < sqrt(2)
6349393c00Smartynas  * Theoretical peak relative error = 5.3e-37,
6449393c00Smartynas  * relative peak error spread = 2.3e-14
6549393c00Smartynas  */
6649393c00Smartynas static const long double
6749393c00Smartynas   P12 = 1.538612243596254322971797716843006400388E-6L,
6849393c00Smartynas   P11 = 4.998469661968096229986658302195402690910E-1L,
6949393c00Smartynas   P10 = 2.321125933898420063925789532045674660756E1L,
7049393c00Smartynas   P9 = 4.114517881637811823002128927449878962058E2L,
7149393c00Smartynas   P8 = 3.824952356185897735160588078446136783779E3L,
7249393c00Smartynas   P7 = 2.128857716871515081352991964243375186031E4L,
7349393c00Smartynas   P6 = 7.594356839258970405033155585486712125861E4L,
7449393c00Smartynas   P5 = 1.797628303815655343403735250238293741397E5L,
7549393c00Smartynas   P4 = 2.854829159639697837788887080758954924001E5L,
7649393c00Smartynas   P3 = 3.007007295140399532324943111654767187848E5L,
7749393c00Smartynas   P2 = 2.014652742082537582487669938141683759923E5L,
7849393c00Smartynas   P1 = 7.771154681358524243729929227226708890930E4L,
7949393c00Smartynas   P0 = 1.313572404063446165910279910527789794488E4L,
8049393c00Smartynas   /* Q12 = 1.000000000000000000000000000000000000000E0L, */
8149393c00Smartynas   Q11 = 4.839208193348159620282142911143429644326E1L,
8249393c00Smartynas   Q10 = 9.104928120962988414618126155557301584078E2L,
8349393c00Smartynas   Q9 = 9.147150349299596453976674231612674085381E3L,
8449393c00Smartynas   Q8 = 5.605842085972455027590989944010492125825E4L,
8549393c00Smartynas   Q7 = 2.248234257620569139969141618556349415120E5L,
8649393c00Smartynas   Q6 = 6.132189329546557743179177159925690841200E5L,
8749393c00Smartynas   Q5 = 1.158019977462989115839826904108208787040E6L,
8849393c00Smartynas   Q4 = 1.514882452993549494932585972882995548426E6L,
8949393c00Smartynas   Q3 = 1.347518538384329112529391120390701166528E6L,
9049393c00Smartynas   Q2 = 7.777690340007566932935753241556479363645E5L,
9149393c00Smartynas   Q1 = 2.626900195321832660448791748036714883242E5L,
9249393c00Smartynas   Q0 = 3.940717212190338497730839731583397586124E4L;
9349393c00Smartynas 
9449393c00Smartynas /* Coefficients for log(x) = z + z^3 P(z^2)/Q(z^2),
9549393c00Smartynas  * where z = 2(x-1)/(x+1)
9649393c00Smartynas  * 1/sqrt(2) <= x < sqrt(2)
9749393c00Smartynas  * Theoretical peak relative error = 1.1e-35,
9849393c00Smartynas  * relative peak error spread 1.1e-9
9949393c00Smartynas  */
10049393c00Smartynas static const long double
10149393c00Smartynas   R5 = -8.828896441624934385266096344596648080902E-1L,
10249393c00Smartynas   R4 = 8.057002716646055371965756206836056074715E1L,
10349393c00Smartynas   R3 = -2.024301798136027039250415126250455056397E3L,
10449393c00Smartynas   R2 = 2.048819892795278657810231591630928516206E4L,
10549393c00Smartynas   R1 = -8.977257995689735303686582344659576526998E4L,
10649393c00Smartynas   R0 = 1.418134209872192732479751274970992665513E5L,
10749393c00Smartynas   /* S6 = 1.000000000000000000000000000000000000000E0L, */
10849393c00Smartynas   S5 = -1.186359407982897997337150403816839480438E2L,
10949393c00Smartynas   S4 = 3.998526750980007367835804959888064681098E3L,
11049393c00Smartynas   S3 = -5.748542087379434595104154610899551484314E4L,
11149393c00Smartynas   S2 = 4.001557694070773974936904547424676279307E5L,
11249393c00Smartynas   S1 = -1.332535117259762928288745111081235577029E6L,
11349393c00Smartynas   S0 = 1.701761051846631278975701529965589676574E6L;
11449393c00Smartynas 
11549393c00Smartynas /* C1 + C2 = ln 2 */
11649393c00Smartynas static const long double C1 = 6.93145751953125E-1L;
11749393c00Smartynas static const long double C2 = 1.428606820309417232121458176568075500134E-6L;
11849393c00Smartynas 
11949393c00Smartynas static const long double sqrth = 0.7071067811865475244008443621048490392848L;
12049393c00Smartynas /* ln (2^16384 * (1 - 2^-113)) */
12149393c00Smartynas static const long double zero = 0.0L;
12249393c00Smartynas 
12349393c00Smartynas long double
log1pl(long double xm1)12449393c00Smartynas log1pl(long double xm1)
12549393c00Smartynas {
12649393c00Smartynas   long double x, y, z, r, s;
12749393c00Smartynas   ieee_quad_shape_type u;
12849393c00Smartynas   int32_t hx;
12949393c00Smartynas   int e;
13049393c00Smartynas 
13149393c00Smartynas   /* Test for NaN or infinity input. */
13249393c00Smartynas   u.value = xm1;
13349393c00Smartynas   hx = u.parts32.mswhi;
13449393c00Smartynas   if (hx >= 0x7fff0000)
13549393c00Smartynas     return xm1;
13649393c00Smartynas 
13749393c00Smartynas   /* log1p(+- 0) = +- 0.  */
13849393c00Smartynas   if (((hx & 0x7fffffff) == 0)
13949393c00Smartynas       && (u.parts32.mswlo | u.parts32.lswhi | u.parts32.lswlo) == 0)
14049393c00Smartynas     return xm1;
14149393c00Smartynas 
14249393c00Smartynas   x = xm1 + 1.0L;
14349393c00Smartynas 
14449393c00Smartynas   /* log1p(-1) = -inf */
14549393c00Smartynas   if (x <= 0.0L)
14649393c00Smartynas     {
14749393c00Smartynas       if (x == 0.0L)
14849393c00Smartynas 	return (-1.0L / (x - x));
14949393c00Smartynas       else
15049393c00Smartynas 	return (zero / (x - x));
15149393c00Smartynas     }
15249393c00Smartynas 
15349393c00Smartynas   /* Separate mantissa from exponent.  */
15449393c00Smartynas 
15549393c00Smartynas   /* Use frexp used so that denormal numbers will be handled properly.  */
15649393c00Smartynas   x = frexpl (x, &e);
15749393c00Smartynas 
15849393c00Smartynas   /* Logarithm using log(x) = z + z^3 P(z^2)/Q(z^2),
15949393c00Smartynas      where z = 2(x-1)/x+1).  */
16049393c00Smartynas   if ((e > 2) || (e < -2))
16149393c00Smartynas     {
16249393c00Smartynas       if (x < sqrth)
16349393c00Smartynas 	{			/* 2( 2x-1 )/( 2x+1 ) */
16449393c00Smartynas 	  e -= 1;
16549393c00Smartynas 	  z = x - 0.5L;
16649393c00Smartynas 	  y = 0.5L * z + 0.5L;
16749393c00Smartynas 	}
16849393c00Smartynas       else
16949393c00Smartynas 	{			/*  2 (x-1)/(x+1)   */
17049393c00Smartynas 	  z = x - 0.5L;
17149393c00Smartynas 	  z -= 0.5L;
17249393c00Smartynas 	  y = 0.5L * x + 0.5L;
17349393c00Smartynas 	}
17449393c00Smartynas       x = z / y;
17549393c00Smartynas       z = x * x;
17649393c00Smartynas       r = ((((R5 * z
17749393c00Smartynas 	      + R4) * z
17849393c00Smartynas 	     + R3) * z
17949393c00Smartynas 	    + R2) * z
18049393c00Smartynas 	   + R1) * z
18149393c00Smartynas 	+ R0;
18249393c00Smartynas       s = (((((z
18349393c00Smartynas 	       + S5) * z
18449393c00Smartynas 	      + S4) * z
18549393c00Smartynas 	     + S3) * z
18649393c00Smartynas 	    + S2) * z
18749393c00Smartynas 	   + S1) * z
18849393c00Smartynas 	+ S0;
18949393c00Smartynas       z = x * (z * r / s);
19049393c00Smartynas       z = z + e * C2;
19149393c00Smartynas       z = z + x;
19249393c00Smartynas       z = z + e * C1;
19349393c00Smartynas       return (z);
19449393c00Smartynas     }
19549393c00Smartynas 
19649393c00Smartynas 
19749393c00Smartynas   /* Logarithm using log(1+x) = x - .5x^2 + x^3 P(x)/Q(x). */
19849393c00Smartynas 
19949393c00Smartynas   if (x < sqrth)
20049393c00Smartynas     {
20149393c00Smartynas       e -= 1;
20249393c00Smartynas       if (e != 0)
20349393c00Smartynas 	x = 2.0L * x - 1.0L;	/*  2x - 1  */
20449393c00Smartynas       else
20549393c00Smartynas 	x = xm1;
20649393c00Smartynas     }
20749393c00Smartynas   else
20849393c00Smartynas     {
20949393c00Smartynas       if (e != 0)
21049393c00Smartynas 	x = x - 1.0L;
21149393c00Smartynas       else
21249393c00Smartynas 	x = xm1;
21349393c00Smartynas     }
21449393c00Smartynas   z = x * x;
21549393c00Smartynas   r = (((((((((((P12 * x
21649393c00Smartynas 		 + P11) * x
21749393c00Smartynas 		+ P10) * x
21849393c00Smartynas 	       + P9) * x
21949393c00Smartynas 	      + P8) * x
22049393c00Smartynas 	     + P7) * x
22149393c00Smartynas 	    + P6) * x
22249393c00Smartynas 	   + P5) * x
22349393c00Smartynas 	  + P4) * x
22449393c00Smartynas 	 + P3) * x
22549393c00Smartynas 	+ P2) * x
22649393c00Smartynas        + P1) * x
22749393c00Smartynas     + P0;
22849393c00Smartynas   s = (((((((((((x
22949393c00Smartynas 		 + Q11) * x
23049393c00Smartynas 		+ Q10) * x
23149393c00Smartynas 	       + Q9) * x
23249393c00Smartynas 	      + Q8) * x
23349393c00Smartynas 	     + Q7) * x
23449393c00Smartynas 	    + Q6) * x
23549393c00Smartynas 	   + Q5) * x
23649393c00Smartynas 	  + Q4) * x
23749393c00Smartynas 	 + Q3) * x
23849393c00Smartynas 	+ Q2) * x
23949393c00Smartynas        + Q1) * x
24049393c00Smartynas     + Q0;
24149393c00Smartynas   y = x * (z * r / s);
24249393c00Smartynas   y = y + e * C2;
24349393c00Smartynas   z = y - 0.5L * z;
24449393c00Smartynas   z = z + x;
24549393c00Smartynas   z = z + e * C1;
24649393c00Smartynas   return (z);
24749393c00Smartynas }
248*2f2c0062Sguenther DEF_STD(log1pl);
249