Home | History | Annotate | Line # | Download | only in src
      1 /* from: FreeBSD: head/lib/msun/src/e_coshl.c XXX */
      2 
      3 /*
      4  * ====================================================
      5  * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
      6  *
      7  * Developed at SunPro, a Sun Microsystems, Inc. business.
      8  * Permission to use, copy, modify, and distribute this
      9  * software is freely granted, provided that this notice
     10  * is preserved.
     11  * ====================================================
     12  */
     13 
     14 #include <sys/cdefs.h>
     15 /*
     16  * See e_cosh.c for complete comments.
     17  *
     18  * Converted to long double by Bruce D. Evans.
     19  */
     20 
     21 #include "namespace.h"
     22 #include <float.h>
     23 
     24 __weak_alias(coshl, _coshl)
     25 
     26 #ifdef __HAVE_LONG_DOUBLE
     27 #error SHOULD STOP HERE!!!
     28 
     29 #include "math.h"
     30 #include "math_private.h"
     31 #if LDBL_MANT_DIG == 64
     32 #include "../ld80/k_expl.h"
     33 #elif LDBL_MANT_DIG == 113
     34 #include "../ld128/k_expl.h"
     35 #else
     36 #error "Unsupported long double format"
     37 #endif
     38 
     39 #if LDBL_MAX_EXP != 0x4000
     40 /* We also require the usual expsign encoding. */
     41 #error "Unsupported long double format"
     42 #endif
     43 
     44 #define	BIAS	(LDBL_MAX_EXP - 1)
     45 
     46 static const volatile long double huge = 0x1p10000L, tiny = 0x1p-10000L;
     47 #if LDBL_MANT_DIG == 64
     48 /*
     49  * Domain [-1, 1], range ~[-1.8211e-21, 1.8211e-21]:
     50  * |cosh(x) - c(x)| < 2**-68.8
     51  */
     52 static const union ieee_ext_u
     53 C4u = LD80C(0xaaaaaaaaaaaaac78, -5,  4.16666666666666682297e-2L);
     54 #define	C4	C4u.extu_ld
     55 static const double
     56 C2  =  0.5,
     57 C6  =  1.3888888888888616e-3,		/*  0x16c16c16c16b99.0p-62 */
     58 C8  =  2.4801587301767953e-5,		/*  0x1a01a01a027061.0p-68 */
     59 C10 =  2.7557319163300398e-7,		/*  0x127e4fb6c9b55f.0p-74 */
     60 C12 =  2.0876768371393075e-9,		/*  0x11eed99406a3f4.0p-81 */
     61 C14 =  1.1469537039374480e-11,		/*  0x1938c67cd18c48.0p-89 */
     62 C16 =  4.8473490896852041e-14;		/*  0x1b49c429701e45.0p-97 */
     63 #elif LDBL_MANT_DIG == 113
     64 /*
     65  * Domain [-1, 1], range ~[-2.3194e-37, 2.3194e-37]:
     66  * |cosh(x) - c(x)| < 2**-121.69
     67  */
     68 static const long double
     69 C4  =  4.16666666666666666666666666666666225e-2L,	/*  0x1555555555555555555555555554e.0p-117L */
     70 C6  =  1.38888888888888888888888888889434831e-3L,	/*  0x16c16c16c16c16c16c16c16c1dd7a.0p-122L */
     71 C8  =  2.48015873015873015873015871870962089e-5L,	/*  0x1a01a01a01a01a01a01a017af2756.0p-128L */
     72 C10 =  2.75573192239858906525574318600800201e-7L,	/*  0x127e4fb7789f5c72ef01c8a040640.0p-134L */
     73 C12 =  2.08767569878680989791444691755468269e-9L,	/*  0x11eed8eff8d897b543d0679607399.0p-141L */
     74 C14=  1.14707455977297247387801189650495351e-11L,	/*  0x193974a8c07c9d24ae169a7fa9b54.0p-149L */
     75 C16 =  4.77947733238737883626416876486279985e-14L;	/*  0x1ae7f3e733b814d4e1b90f5727fe4.0p-157L */
     76 static const double
     77 C2  =  0.5,
     78 C18 =  1.5619206968597871e-16,		/*  0x16827863b9900b.0p-105 */
     79 C20 =  4.1103176218528049e-19,		/*  0x1e542ba3d3c269.0p-114 */
     80 C22 =  8.8967926401641701e-22,		/*  0x10ce399542a014.0p-122 */
     81 C24 =  1.6116681626523904e-24,		/*  0x1f2c981d1f0cb7.0p-132 */
     82 C26 =  2.5022374732804632e-27;		/*  0x18c7ecf8b2c4a0.0p-141 */
     83 #else
     84 #error "Unsupported long double format"
     85 #endif /* LDBL_MANT_DIG == 64 */
     86 
     87 /* log(2**16385 - 0.5) rounded up: */
     88 static const float
     89 o_threshold =  1.13572168e4;		/*  0xb174de.0p-10 */
     90 
     91 long double
     92 coshl(long double x)
     93 {
     94 	long double hi,lo,x2;
     95 #if LDBL_MANT_DIG == 113
     96 	double dx2;
     97 #endif
     98 	uint16_t ix;
     99 
    100 	GET_LDBL_EXPSIGN(ix,x);
    101 	ix &= 0x7fff;
    102 
    103     /* x is INF or NaN */
    104 	if(ix>=0x7fff) return x*x;
    105 
    106 	ENTERI();
    107 
    108     /* |x| < 1, return 1 or c(x) */
    109 	if(ix<0x3fff) {
    110 	    if (ix<BIAS-(LDBL_MANT_DIG+1)/2) 	/* |x| < TINY */
    111 		RETURNI(1+tiny);	/* cosh(tiny) = 1(+) with inexact */
    112 	    x2 = x*x;
    113 #if LDBL_MANT_DIG == 64
    114 	    long double x4 = x2*x2;
    115 	    RETURNI(((C16*x2 + C14)*x4 + (C12*x2 + C10))*(x4*x4*x2) +
    116 		((C8*x2 + C6)*x2 + C4)*x4 + C2*x2 + 1);
    117 #elif LDBL_MANT_DIG == 113
    118 	    dx2 = x2;
    119 	    RETURNI((((((((((((C26*dx2 + C24)*dx2 + C22)*dx2 +
    120 		C20)*x2 + C18)*x2 +
    121 		C16)*x2 + C14)*x2 + C12)*x2 + C10)*x2 + C8)*x2 + C6)*x2 +
    122 		C4)*(x2*x2) + C2*x2 + 1);
    123 #endif
    124 	}
    125 
    126     /* |x| in [1, 64), return accurate exp(|x|)/2+1/exp(|x|)/2 */
    127 	if (ix < 0x4005) {
    128 	    k_hexpl(fabsl(x), &hi, &lo);
    129 	    RETURNI(lo + 0.25/(hi + lo) + hi);
    130 	}
    131 
    132     /* |x| in [64, o_threshold], return correctly-overflowing exp(|x|)/2 */
    133 	if (fabsl(x) <= o_threshold)
    134 	    RETURNI(hexpl(fabsl(x)));
    135 
    136     /* |x| > o_threshold, cosh(x) overflow */
    137 	RETURNI(huge*huge);
    138 }
    139 #else
    140 #include "math.h"
    141 
    142 long double
    143 coshl(long double x)
    144 {
    145 	return cosh(x);
    146 }
    147 
    148 #endif
    149