1 /* from: FreeBSD: head/lib/msun/src/e_acosh.c 176451 2008-02-22 02:30:36Z das */ 2 3 /* @(#)e_acosh.c 1.3 95/01/18 */ 4 /* 5 * ==================================================== 6 * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved. 7 * 8 * Developed at SunSoft, a Sun Microsystems, Inc. business. 9 * Permission to use, copy, modify, and distribute this 10 * software is freely granted, provided that this notice 11 * is preserved. 12 * ==================================================== 13 * 14 */ 15 16 #include <sys/cdefs.h> 17 /* 18 * See e_acosh.c for complete comments. 19 * 20 * Converted to long double by David Schultz <das@FreeBSD.ORG> and 21 * Bruce D. Evans. 22 */ 23 24 #include <float.h> 25 #ifdef __i386__ 26 #include <ieeefp.h> 27 #endif 28 29 #include "fpmath.h" 30 #include "math.h" 31 #include "math_private.h" 32 33 /* EXP_LARGE is the threshold above which we use acosh(x) ~= log(2x). */ 34 #if LDBL_MANT_DIG == 64 35 #define EXP_LARGE 34 36 #elif LDBL_MANT_DIG == 113 37 #define EXP_LARGE 58 38 #else 39 #error "Unsupported long double format" 40 #endif 41 42 #if LDBL_MAX_EXP != 0x4000 43 /* We also require the usual expsign encoding. */ 44 #error "Unsupported long double format" 45 #endif 46 47 #define BIAS (LDBL_MAX_EXP - 1) 48 49 static const double 50 one = 1.0; 51 52 #if LDBL_MANT_DIG == 64 53 static const union IEEEl2bits 54 u_ln2 = LD80C(0xb17217f7d1cf79ac, -1, 6.93147180559945309417e-1L); 55 #define ln2 u_ln2.e 56 #elif LDBL_MANT_DIG == 113 57 static const long double 58 ln2 = 6.93147180559945309417232121458176568e-1L; /* 0x162e42fefa39ef35793c7673007e6.0p-113 */ 59 #else 60 #error "Unsupported long double format" 61 #endif 62 63 long double 64 acoshl(long double x) 65 { 66 long double t; 67 int16_t hx; 68 69 ENTERI(); 70 GET_LDBL_EXPSIGN(hx, x); 71 if (hx < 0x3fff) { /* x < 1, or misnormal */ 72 RETURNI((x-x)/(x-x)); 73 } else if (hx >= BIAS + EXP_LARGE) { /* x >= LARGE */ 74 if (hx >= 0x7fff) { /* x is inf, NaN or misnormal */ 75 RETURNI(x+x); 76 } else 77 RETURNI(logl(x)+ln2); /* acosh(huge)=log(2x), or misnormal */ 78 } else if (hx == 0x3fff && x == 1) { 79 RETURNI(0.0); /* acosh(1) = 0 */ 80 } else if (hx >= 0x4000) { /* LARGE > x >= 2, or misnormal */ 81 t=x*x; 82 RETURNI(logl(2.0*x-one/(x+sqrtl(t-one)))); 83 } else { /* 1<x<2 */ 84 t = x-one; 85 RETURNI(log1pl(t+sqrtl(2.0*t+t*t))); 86 } 87 } 88