xref: /netbsd-src/external/gpl3/gcc/dist/libquadmath/math/expm1q.c (revision 181254a7b1bdde6873432bffef2d2decc4b5c22f)
1*181254a7Smrg /*							expm1q.c
2*181254a7Smrg  *
3*181254a7Smrg  *	Exponential function, minus 1
4*181254a7Smrg  *      128-bit long double precision
5*181254a7Smrg  *
6*181254a7Smrg  *
7*181254a7Smrg  *
8*181254a7Smrg  * SYNOPSIS:
9*181254a7Smrg  *
10*181254a7Smrg  * long double x, y, expm1q();
11*181254a7Smrg  *
12*181254a7Smrg  * y = expm1q( x );
13*181254a7Smrg  *
14*181254a7Smrg  *
15*181254a7Smrg  *
16*181254a7Smrg  * DESCRIPTION:
17*181254a7Smrg  *
18*181254a7Smrg  * Returns e (2.71828...) raised to the x power, minus one.
19*181254a7Smrg  *
20*181254a7Smrg  * Range reduction is accomplished by separating the argument
21*181254a7Smrg  * into an integer k and fraction f such that
22*181254a7Smrg  *
23*181254a7Smrg  *     x    k  f
24*181254a7Smrg  *    e  = 2  e.
25*181254a7Smrg  *
26*181254a7Smrg  * An expansion x + .5 x^2 + x^3 R(x) approximates exp(f) - 1
27*181254a7Smrg  * in the basic range [-0.5 ln 2, 0.5 ln 2].
28*181254a7Smrg  *
29*181254a7Smrg  *
30*181254a7Smrg  * ACCURACY:
31*181254a7Smrg  *
32*181254a7Smrg  *                      Relative error:
33*181254a7Smrg  * arithmetic   domain     # trials      peak         rms
34*181254a7Smrg  *    IEEE    -79,+MAXLOG    100,000     1.7e-34     4.5e-35
35*181254a7Smrg  *
36*181254a7Smrg  */
37*181254a7Smrg 
38*181254a7Smrg /* Copyright 2001 by Stephen L. Moshier
39*181254a7Smrg 
40*181254a7Smrg     This library is free software; you can redistribute it and/or
41*181254a7Smrg     modify it under the terms of the GNU Lesser General Public
42*181254a7Smrg     License as published by the Free Software Foundation; either
43*181254a7Smrg     version 2.1 of the License, or (at your option) any later version.
44*181254a7Smrg 
45*181254a7Smrg     This library is distributed in the hope that it will be useful,
46*181254a7Smrg     but WITHOUT ANY WARRANTY; without even the implied warranty of
47*181254a7Smrg     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
48*181254a7Smrg     Lesser General Public License for more details.
49*181254a7Smrg 
50*181254a7Smrg     You should have received a copy of the GNU Lesser General Public
51*181254a7Smrg     License along with this library; if not, see
52*181254a7Smrg     <http://www.gnu.org/licenses/>.  */
53*181254a7Smrg 
54*181254a7Smrg #include "quadmath-imp.h"
55*181254a7Smrg 
56*181254a7Smrg /* exp(x) - 1 = x + 0.5 x^2 + x^3 P(x)/Q(x)
57*181254a7Smrg    -.5 ln 2  <  x  <  .5 ln 2
58*181254a7Smrg    Theoretical peak relative error = 8.1e-36  */
59*181254a7Smrg 
60*181254a7Smrg static const __float128
61*181254a7Smrg   P0 = 2.943520915569954073888921213330863757240E8Q,
62*181254a7Smrg   P1 = -5.722847283900608941516165725053359168840E7Q,
63*181254a7Smrg   P2 = 8.944630806357575461578107295909719817253E6Q,
64*181254a7Smrg   P3 = -7.212432713558031519943281748462837065308E5Q,
65*181254a7Smrg   P4 = 4.578962475841642634225390068461943438441E4Q,
66*181254a7Smrg   P5 = -1.716772506388927649032068540558788106762E3Q,
67*181254a7Smrg   P6 = 4.401308817383362136048032038528753151144E1Q,
68*181254a7Smrg   P7 = -4.888737542888633647784737721812546636240E-1Q,
69*181254a7Smrg   Q0 = 1.766112549341972444333352727998584753865E9Q,
70*181254a7Smrg   Q1 = -7.848989743695296475743081255027098295771E8Q,
71*181254a7Smrg   Q2 = 1.615869009634292424463780387327037251069E8Q,
72*181254a7Smrg   Q3 = -2.019684072836541751428967854947019415698E7Q,
73*181254a7Smrg   Q4 = 1.682912729190313538934190635536631941751E6Q,
74*181254a7Smrg   Q5 = -9.615511549171441430850103489315371768998E4Q,
75*181254a7Smrg   Q6 = 3.697714952261803935521187272204485251835E3Q,
76*181254a7Smrg   Q7 = -8.802340681794263968892934703309274564037E1Q,
77*181254a7Smrg   /* Q8 = 1.000000000000000000000000000000000000000E0 */
78*181254a7Smrg /* C1 + C2 = ln 2 */
79*181254a7Smrg 
80*181254a7Smrg   C1 = 6.93145751953125E-1Q,
81*181254a7Smrg   C2 = 1.428606820309417232121458176568075500134E-6Q,
82*181254a7Smrg /* ln 2^-114 */
83*181254a7Smrg   minarg = -7.9018778583833765273564461846232128760607E1Q, big = 1e4932Q;
84*181254a7Smrg 
85*181254a7Smrg 
86*181254a7Smrg __float128
expm1q(__float128 x)87*181254a7Smrg expm1q (__float128 x)
88*181254a7Smrg {
89*181254a7Smrg   __float128 px, qx, xx;
90*181254a7Smrg   int32_t ix, sign;
91*181254a7Smrg   ieee854_float128 u;
92*181254a7Smrg   int k;
93*181254a7Smrg 
94*181254a7Smrg   /* Detect infinity and NaN.  */
95*181254a7Smrg   u.value = x;
96*181254a7Smrg   ix = u.words32.w0;
97*181254a7Smrg   sign = ix & 0x80000000;
98*181254a7Smrg   ix &= 0x7fffffff;
99*181254a7Smrg   if (!sign && ix >= 0x40060000)
100*181254a7Smrg     {
101*181254a7Smrg       /* If num is positive and exp >= 6 use plain exp.  */
102*181254a7Smrg       return expq (x);
103*181254a7Smrg     }
104*181254a7Smrg   if (ix >= 0x7fff0000)
105*181254a7Smrg     {
106*181254a7Smrg       /* Infinity (which must be negative infinity). */
107*181254a7Smrg       if (((ix & 0xffff) | u.words32.w1 | u.words32.w2 | u.words32.w3) == 0)
108*181254a7Smrg 	return -1;
109*181254a7Smrg       /* NaN.  Invalid exception if signaling.  */
110*181254a7Smrg       return x + x;
111*181254a7Smrg     }
112*181254a7Smrg 
113*181254a7Smrg   /* expm1(+- 0) = +- 0.  */
114*181254a7Smrg   if ((ix == 0) && (u.words32.w1 | u.words32.w2 | u.words32.w3) == 0)
115*181254a7Smrg     return x;
116*181254a7Smrg 
117*181254a7Smrg   /* Minimum value.  */
118*181254a7Smrg   if (x < minarg)
119*181254a7Smrg     return (4.0/big - 1);
120*181254a7Smrg 
121*181254a7Smrg   /* Avoid internal underflow when result does not underflow, while
122*181254a7Smrg      ensuring underflow (without returning a zero of the wrong sign)
123*181254a7Smrg      when the result does underflow.  */
124*181254a7Smrg   if (fabsq (x) < 0x1p-113Q)
125*181254a7Smrg     {
126*181254a7Smrg       math_check_force_underflow (x);
127*181254a7Smrg       return x;
128*181254a7Smrg     }
129*181254a7Smrg 
130*181254a7Smrg   /* Express x = ln 2 (k + remainder), remainder not exceeding 1/2. */
131*181254a7Smrg   xx = C1 + C2;			/* ln 2. */
132*181254a7Smrg   px = floorq (0.5 + x / xx);
133*181254a7Smrg   k = px;
134*181254a7Smrg   /* remainder times ln 2 */
135*181254a7Smrg   x -= px * C1;
136*181254a7Smrg   x -= px * C2;
137*181254a7Smrg 
138*181254a7Smrg   /* Approximate exp(remainder ln 2).  */
139*181254a7Smrg   px = (((((((P7 * x
140*181254a7Smrg 	      + P6) * x
141*181254a7Smrg 	     + P5) * x + P4) * x + P3) * x + P2) * x + P1) * x + P0) * x;
142*181254a7Smrg 
143*181254a7Smrg   qx = (((((((x
144*181254a7Smrg 	      + Q7) * x
145*181254a7Smrg 	     + Q6) * x + Q5) * x + Q4) * x + Q3) * x + Q2) * x + Q1) * x + Q0;
146*181254a7Smrg 
147*181254a7Smrg   xx = x * x;
148*181254a7Smrg   qx = x + (0.5 * xx + xx * px / qx);
149*181254a7Smrg 
150*181254a7Smrg   /* exp(x) = exp(k ln 2) exp(remainder ln 2) = 2^k exp(remainder ln 2).
151*181254a7Smrg 
152*181254a7Smrg   We have qx = exp(remainder ln 2) - 1, so
153*181254a7Smrg   exp(x) - 1 = 2^k (qx + 1) - 1
154*181254a7Smrg              = 2^k qx + 2^k - 1.  */
155*181254a7Smrg 
156*181254a7Smrg   px = ldexpq (1, k);
157*181254a7Smrg   x = px * qx + (px - 1.0);
158*181254a7Smrg   return x;
159*181254a7Smrg }
160