xref: /openbsd-src/lib/libm/src/ld128/e_sinhl.c (revision 2f2c00629eff6a304ebffb255fc56f4fa7a1833b)
149393c00Smartynas /* @(#)e_sinh.c 5.1 93/09/24 */
249393c00Smartynas /*
349393c00Smartynas  * ====================================================
449393c00Smartynas  * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
549393c00Smartynas  *
649393c00Smartynas  * Developed at SunPro, a Sun Microsystems, Inc. business.
749393c00Smartynas  * Permission to use, copy, modify, and distribute this
849393c00Smartynas  * software is freely granted, provided that this notice
949393c00Smartynas  * is preserved.
1049393c00Smartynas  * ====================================================
1149393c00Smartynas  */
1249393c00Smartynas 
1349393c00Smartynas /*
1449393c00Smartynas  * Copyright (c) 2008 Stephen L. Moshier <steve@moshier.net>
1549393c00Smartynas  *
1649393c00Smartynas  * Permission to use, copy, modify, and distribute this software for any
1749393c00Smartynas  * purpose with or without fee is hereby granted, provided that the above
1849393c00Smartynas  * copyright notice and this permission notice appear in all copies.
1949393c00Smartynas  *
2049393c00Smartynas  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
2149393c00Smartynas  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
2249393c00Smartynas  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
2349393c00Smartynas  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
2449393c00Smartynas  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
2549393c00Smartynas  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
2649393c00Smartynas  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
2749393c00Smartynas  */
2849393c00Smartynas 
2949393c00Smartynas /* sinhl(x)
3049393c00Smartynas  * Method :
3149393c00Smartynas  * mathematically sinh(x) if defined to be (exp(x)-exp(-x))/2
3249393c00Smartynas  *      1. Replace x by |x| (sinhl(-x) = -sinhl(x)).
3349393c00Smartynas  *      2.
3449393c00Smartynas  *                                                   E + E/(E+1)
3549393c00Smartynas  *          0        <= x <= 25     :  sinhl(x) := --------------, E=expm1l(x)
3649393c00Smartynas  *                                                       2
3749393c00Smartynas  *
3849393c00Smartynas  *          25       <= x <= lnovft :  sinhl(x) := expl(x)/2
3949393c00Smartynas  *          lnovft   <= x <= ln2ovft:  sinhl(x) := expl(x/2)/2 * expl(x/2)
4049393c00Smartynas  *          ln2ovft  <  x           :  sinhl(x) := x*shuge (overflow)
4149393c00Smartynas  *
4249393c00Smartynas  * Special cases:
4349393c00Smartynas  *      sinhl(x) is |x| if x is +INF, -INF, or NaN.
4449393c00Smartynas  *      only sinhl(0)=0 is exact for finite x.
4549393c00Smartynas  */
4649393c00Smartynas 
4749393c00Smartynas #include <math.h>
4849393c00Smartynas 
4949393c00Smartynas #include "math_private.h"
5049393c00Smartynas 
5149393c00Smartynas static const long double one = 1.0, shuge = 1.0e4931L,
5249393c00Smartynas ovf_thresh = 1.1357216553474703894801348310092223067821E4L;
5349393c00Smartynas 
5449393c00Smartynas long double
sinhl(long double x)5549393c00Smartynas sinhl(long double x)
5649393c00Smartynas {
5749393c00Smartynas   long double t, w, h;
5849393c00Smartynas   u_int32_t jx, ix;
5949393c00Smartynas   ieee_quad_shape_type u;
6049393c00Smartynas 
6149393c00Smartynas   /* Words of |x|. */
6249393c00Smartynas   u.value = x;
6349393c00Smartynas   jx = u.parts32.mswhi;
6449393c00Smartynas   ix = jx & 0x7fffffff;
6549393c00Smartynas 
6649393c00Smartynas   /* x is INF or NaN */
6749393c00Smartynas   if (ix >= 0x7fff0000)
6849393c00Smartynas     return x + x;
6949393c00Smartynas 
7049393c00Smartynas   h = 0.5;
7149393c00Smartynas   if (jx & 0x80000000)
7249393c00Smartynas     h = -h;
7349393c00Smartynas 
7449393c00Smartynas   /* Absolute value of x.  */
7549393c00Smartynas   u.parts32.mswhi = ix;
7649393c00Smartynas 
7749393c00Smartynas   /* |x| in [0,40], return sign(x)*0.5*(E+E/(E+1))) */
7849393c00Smartynas   if (ix <= 0x40044000)
7949393c00Smartynas     {
8049393c00Smartynas       if (ix < 0x3fc60000) /* |x| < 2^-57 */
8149393c00Smartynas 	if (shuge + x > one)
8249393c00Smartynas 	  return x;		/* sinh(tiny) = tiny with inexact */
8349393c00Smartynas       t = expm1l (u.value);
8449393c00Smartynas       if (ix < 0x3fff0000)
8549393c00Smartynas 	return h * (2.0 * t - t * t / (t + one));
8649393c00Smartynas       return h * (t + t / (t + one));
8749393c00Smartynas     }
8849393c00Smartynas 
8949393c00Smartynas   /* |x| in [40, log(maxdouble)] return 0.5*exp(|x|) */
9049393c00Smartynas   if (ix <= 0x400c62e3) /* 11356.375 */
9149393c00Smartynas     return h * expl (u.value);
9249393c00Smartynas 
9349393c00Smartynas   /* |x| in [log(maxdouble), overflowthreshold]
9449393c00Smartynas      Overflow threshold is log(2 * maxdouble).  */
9549393c00Smartynas   if (u.value <= ovf_thresh)
9649393c00Smartynas     {
9749393c00Smartynas       w = expl (0.5 * u.value);
9849393c00Smartynas       t = h * w;
9949393c00Smartynas       return t * w;
10049393c00Smartynas     }
10149393c00Smartynas 
10249393c00Smartynas   /* |x| > overflowthreshold, sinhl(x) overflow */
10349393c00Smartynas   return x * shuge;
10449393c00Smartynas }
105*2f2c0062Sguenther DEF_STD(sinhl);
106