xref: /openbsd-src/lib/libm/src/ld128/s_expm1l.c (revision 2f2c00629eff6a304ebffb255fc56f4fa7a1833b)
1*2f2c0062Sguenther /*	$OpenBSD: s_expm1l.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 /*							expm1l.c
2049393c00Smartynas  *
2149393c00Smartynas  *	Exponential function, minus 1
2249393c00Smartynas  *      128-bit long double precision
2349393c00Smartynas  *
2449393c00Smartynas  *
2549393c00Smartynas  *
2649393c00Smartynas  * SYNOPSIS:
2749393c00Smartynas  *
2849393c00Smartynas  * long double x, y, expm1l();
2949393c00Smartynas  *
3049393c00Smartynas  * y = expm1l( x );
3149393c00Smartynas  *
3249393c00Smartynas  *
3349393c00Smartynas  *
3449393c00Smartynas  * DESCRIPTION:
3549393c00Smartynas  *
3649393c00Smartynas  * Returns e (2.71828...) raised to the x power, minus one.
3749393c00Smartynas  *
3849393c00Smartynas  * Range reduction is accomplished by separating the argument
3949393c00Smartynas  * into an integer k and fraction f such that
4049393c00Smartynas  *
4149393c00Smartynas  *     x    k  f
4249393c00Smartynas  *    e  = 2  e.
4349393c00Smartynas  *
4449393c00Smartynas  * An expansion x + .5 x^2 + x^3 R(x) approximates exp(f) - 1
4549393c00Smartynas  * in the basic range [-0.5 ln 2, 0.5 ln 2].
4649393c00Smartynas  *
4749393c00Smartynas  *
4849393c00Smartynas  * ACCURACY:
4949393c00Smartynas  *
5049393c00Smartynas  *                      Relative error:
5149393c00Smartynas  * arithmetic   domain     # trials      peak         rms
5249393c00Smartynas  *    IEEE    -79,+MAXLOG    100,000     1.7e-34     4.5e-35
5349393c00Smartynas  *
5449393c00Smartynas  */
5549393c00Smartynas 
5649393c00Smartynas #include <errno.h>
5749393c00Smartynas #include <math.h>
5849393c00Smartynas 
5949393c00Smartynas #include "math_private.h"
6049393c00Smartynas 
6149393c00Smartynas /* exp(x) - 1 = x + 0.5 x^2 + x^3 P(x)/Q(x)
6249393c00Smartynas    -.5 ln 2  <  x  <  .5 ln 2
6349393c00Smartynas    Theoretical peak relative error = 8.1e-36  */
6449393c00Smartynas 
6549393c00Smartynas static const long double
6649393c00Smartynas   P0 = 2.943520915569954073888921213330863757240E8L,
6749393c00Smartynas   P1 = -5.722847283900608941516165725053359168840E7L,
6849393c00Smartynas   P2 = 8.944630806357575461578107295909719817253E6L,
6949393c00Smartynas   P3 = -7.212432713558031519943281748462837065308E5L,
7049393c00Smartynas   P4 = 4.578962475841642634225390068461943438441E4L,
7149393c00Smartynas   P5 = -1.716772506388927649032068540558788106762E3L,
7249393c00Smartynas   P6 = 4.401308817383362136048032038528753151144E1L,
7349393c00Smartynas   P7 = -4.888737542888633647784737721812546636240E-1L,
7449393c00Smartynas   Q0 = 1.766112549341972444333352727998584753865E9L,
7549393c00Smartynas   Q1 = -7.848989743695296475743081255027098295771E8L,
7649393c00Smartynas   Q2 = 1.615869009634292424463780387327037251069E8L,
7749393c00Smartynas   Q3 = -2.019684072836541751428967854947019415698E7L,
7849393c00Smartynas   Q4 = 1.682912729190313538934190635536631941751E6L,
7949393c00Smartynas   Q5 = -9.615511549171441430850103489315371768998E4L,
8049393c00Smartynas   Q6 = 3.697714952261803935521187272204485251835E3L,
8149393c00Smartynas   Q7 = -8.802340681794263968892934703309274564037E1L,
8249393c00Smartynas   /* Q8 = 1.000000000000000000000000000000000000000E0 */
8349393c00Smartynas /* C1 + C2 = ln 2 */
8449393c00Smartynas 
8549393c00Smartynas   C1 = 6.93145751953125E-1L,
8649393c00Smartynas   C2 = 1.428606820309417232121458176568075500134E-6L,
8749393c00Smartynas /* ln (2^16384 * (1 - 2^-113)) */
8849393c00Smartynas   maxlog = 1.1356523406294143949491931077970764891253E4L,
8949393c00Smartynas /* ln 2^-114 */
9049393c00Smartynas   minarg = -7.9018778583833765273564461846232128760607E1L, big = 1e4932L;
9149393c00Smartynas 
9249393c00Smartynas 
9349393c00Smartynas long double
expm1l(long double x)9449393c00Smartynas expm1l(long double x)
9549393c00Smartynas {
9649393c00Smartynas   long double px, qx, xx;
9749393c00Smartynas   int32_t ix, sign;
9849393c00Smartynas   ieee_quad_shape_type u;
9949393c00Smartynas   int k;
10049393c00Smartynas 
10149393c00Smartynas   /* Detect infinity and NaN.  */
10249393c00Smartynas   u.value = x;
10349393c00Smartynas   ix = u.parts32.mswhi;
10449393c00Smartynas   sign = ix & 0x80000000;
10549393c00Smartynas   ix &= 0x7fffffff;
10649393c00Smartynas   if (ix >= 0x7fff0000)
10749393c00Smartynas     {
10849393c00Smartynas       /* Infinity. */
10949393c00Smartynas       if (((ix & 0xffff) | u.parts32.mswlo | u.parts32.lswhi |
11049393c00Smartynas 	u.parts32.lswlo) == 0)
11149393c00Smartynas 	{
11249393c00Smartynas 	  if (sign)
11349393c00Smartynas 	    return -1.0L;
11449393c00Smartynas 	  else
11549393c00Smartynas 	    return x;
11649393c00Smartynas 	}
11749393c00Smartynas       /* NaN. No invalid exception. */
11849393c00Smartynas       return x;
11949393c00Smartynas     }
12049393c00Smartynas 
12149393c00Smartynas   /* expm1(+- 0) = +- 0.  */
12249393c00Smartynas   if ((ix == 0) && (u.parts32.mswlo | u.parts32.lswhi | u.parts32.lswlo) == 0)
12349393c00Smartynas     return x;
12449393c00Smartynas 
12549393c00Smartynas   /* Overflow.  */
12649393c00Smartynas   if (x > maxlog)
12749393c00Smartynas       return (big * big);
12849393c00Smartynas 
12949393c00Smartynas   /* Minimum value.  */
13049393c00Smartynas   if (x < minarg)
13149393c00Smartynas     return (4.0/big - 1.0L);
13249393c00Smartynas 
13349393c00Smartynas   /* Express x = ln 2 (k + remainder), remainder not exceeding 1/2. */
13449393c00Smartynas   xx = C1 + C2;			/* ln 2. */
13549393c00Smartynas   px = floorl (0.5 + x / xx);
13649393c00Smartynas   k = px;
13749393c00Smartynas   /* remainder times ln 2 */
13849393c00Smartynas   x -= px * C1;
13949393c00Smartynas   x -= px * C2;
14049393c00Smartynas 
14149393c00Smartynas   /* Approximate exp(remainder ln 2).  */
14249393c00Smartynas   px = (((((((P7 * x
14349393c00Smartynas 	      + P6) * x
14449393c00Smartynas 	     + P5) * x + P4) * x + P3) * x + P2) * x + P1) * x + P0) * x;
14549393c00Smartynas 
14649393c00Smartynas   qx = (((((((x
14749393c00Smartynas 	      + Q7) * x
14849393c00Smartynas 	     + Q6) * x + Q5) * x + Q4) * x + Q3) * x + Q2) * x + Q1) * x + Q0;
14949393c00Smartynas 
15049393c00Smartynas   xx = x * x;
15149393c00Smartynas   qx = x + (0.5 * xx + xx * px / qx);
15249393c00Smartynas 
15349393c00Smartynas   /* exp(x) = exp(k ln 2) exp(remainder ln 2) = 2^k exp(remainder ln 2).
15449393c00Smartynas 
15549393c00Smartynas   We have qx = exp(remainder ln 2) - 1, so
15649393c00Smartynas   exp(x) - 1 = 2^k (qx + 1) - 1
15749393c00Smartynas 	     = 2^k qx + 2^k - 1.  */
15849393c00Smartynas 
15949393c00Smartynas   px = ldexpl (1.0L, k);
16049393c00Smartynas   x = px * qx + (px - 1.0);
16149393c00Smartynas   return x;
16249393c00Smartynas }
163*2f2c0062Sguenther DEF_STD(expm1l);
164