1 2 /* FreeBSD: head/lib/msun/src/e_acos.c 176451 2008-02-22 02:30:36Z das */ 3 /* 4 * ==================================================== 5 * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved. 6 * 7 * Developed at SunSoft, 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 comments in e_acos.c. 17 * Converted to long double by David Schultz <das@FreeBSD.ORG>. 18 */ 19 #include "namespace.h" 20 21 #include <float.h> 22 #include <machine/ieee.h> 23 24 #include "math.h" 25 #include "math_private.h" 26 27 __weak_alias(acosl, _acosl) 28 29 #ifdef __HAVE_LONG_DOUBLE 30 31 #if LDBL_MANT_DIG == 64 32 #include "../ld80/invtrig.h" 33 #elif LDBL_MANT_DIG == 113 34 #include "../ld128/invtrig.h" 35 #else 36 #error "Unsupported long double format" 37 #endif 38 39 #ifdef LDBL_IMPLICIT_NBIT 40 #define LDBL_NBIT 0 41 #endif 42 43 static const long double 44 one= 1.00000000000000000000e+00; 45 46 #ifdef __i386__ 47 /* XXX Work around the fact that gcc truncates long double constants on i386 */ 48 static volatile double 49 pi1 = 3.14159265358979311600e+00, /* 0x1.921fb54442d18p+1 */ 50 pi2 = 1.22514845490862001043e-16; /* 0x1.1a80000000000p-53 */ 51 #define pi ((long double)pi1 + pi2) 52 #else 53 static const long double 54 pi = 3.14159265358979323846264338327950280e+00L; 55 #endif 56 57 long double 58 acosl(long double x) 59 { 60 union ieee_ext_u u; 61 long double z,p,q,r,w,s,c,df; 62 int16_t expsign, expt; 63 u.extu_ld = x; 64 expsign = GET_EXPSIGN(&u); 65 expt = expsign & 0x7fff; 66 if(expt >= BIAS) { /* |x| >= 1 */ 67 if(expt==BIAS && ((u.extu_frach&~LDBL_NBIT)|u.extu_fracl)==0) { 68 if (expsign>0) return 0.0; /* acos(1) = 0 */ 69 else return pi+2.0*pio2_lo; /* acos(-1)= pi */ 70 } 71 return (x-x)/(x-x); /* acos(|x|>1) is NaN */ 72 } 73 if(expt<BIAS-1) { /* |x| < 0.5 */ 74 if(expt<ACOS_CONST) return pio2_hi+pio2_lo;/*x tiny: acosl=pi/2*/ 75 z = x*x; 76 p = P(z); 77 q = Q(z); 78 r = p/q; 79 return pio2_hi - (x - (pio2_lo-x*r)); 80 } else if (expsign<0) { /* x < -0.5 */ 81 z = (one+x)*0.5; 82 p = P(z); 83 q = Q(z); 84 s = sqrtl(z); 85 r = p/q; 86 w = r*s-pio2_lo; 87 return pi - 2.0*(s+w); 88 } else { /* x > 0.5 */ 89 z = (one-x)*0.5; 90 s = sqrtl(z); 91 u.extu_ld = s; 92 u.extu_fracl = 0; 93 df = u.extu_ld; 94 c = (z-df*df)/(s+df); 95 p = P(z); 96 q = Q(z); 97 r = p/q; 98 w = r*s+c; 99 return 2.0*(df+w); 100 } 101 } 102 #else 103 long double 104 acosl(long double x) 105 { 106 return acos(x); 107 } 108 #endif 109