Update.
[platform/upstream/glibc.git] / sysdeps / libm-ieee754 / e_acos.c
1 /* @(#)e_acos.c 5.1 93/09/24 */
2 /*
3  * ====================================================
4  * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
5  *
6  * Developed at SunPro, a Sun Microsystems, Inc. business.
7  * Permission to use, copy, modify, and distribute this
8  * software is freely granted, provided that this notice
9  * is preserved.
10  * ====================================================
11  */
12 /* Modified by Naohiko Shimizu/Tokai University, Japan 1997/08/25,
13    for performance improvement on pipelined processors.
14  */
15
16 #if defined(LIBM_SCCS) && !defined(lint)
17 static char rcsid[] = "$NetBSD: e_acos.c,v 1.9 1995/05/12 04:57:13 jtc Exp $";
18 #endif
19
20 /* __ieee754_acos(x)
21  * Method :
22  *      acos(x)  = pi/2 - asin(x)
23  *      acos(-x) = pi/2 + asin(x)
24  * For |x|<=0.5
25  *      acos(x) = pi/2 - (x + x*x^2*R(x^2))     (see asin.c)
26  * For x>0.5
27  *      acos(x) = pi/2 - (pi/2 - 2asin(sqrt((1-x)/2)))
28  *              = 2asin(sqrt((1-x)/2))
29  *              = 2s + 2s*z*R(z)        ...z=(1-x)/2, s=sqrt(z)
30  *              = 2f + (2c + 2s*z*R(z))
31  *     where f=hi part of s, and c = (z-f*f)/(s+f) is the correction term
32  *     for f so that f+c ~ sqrt(z).
33  * For x<-0.5
34  *      acos(x) = pi - 2asin(sqrt((1-|x|)/2))
35  *              = pi - 0.5*(s+s*z*R(z)), where z=(1-|x|)/2,s=sqrt(z)
36  *
37  * Special cases:
38  *      if x is NaN, return x itself;
39  *      if |x|>1, return NaN with invalid signal.
40  *
41  * Function needed: __ieee754_sqrt
42  */
43
44 #include "math.h"
45 #include "math_private.h"
46 #define one qS[0]
47
48 #ifdef __STDC__
49 static const double
50 #else
51 static double
52 #endif
53 pi =  3.14159265358979311600e+00, /* 0x400921FB, 0x54442D18 */
54 pio2_hi =  1.57079632679489655800e+00, /* 0x3FF921FB, 0x54442D18 */
55 pio2_lo =  6.12323399573676603587e-17, /* 0x3C91A626, 0x33145C07 */
56 pS[] =  {1.66666666666666657415e-01, /* 0x3FC55555, 0x55555555 */
57  -3.25565818622400915405e-01, /* 0xBFD4D612, 0x03EB6F7D */
58   2.01212532134862925881e-01, /* 0x3FC9C155, 0x0E884455 */
59  -4.00555345006794114027e-02, /* 0xBFA48228, 0xB5688F3B */
60   7.91534994289814532176e-04, /* 0x3F49EFE0, 0x7501B288 */
61   3.47933107596021167570e-05}, /* 0x3F023DE1, 0x0DFDF709 */
62 qS[] ={1.0, -2.40339491173441421878e+00, /* 0xC0033A27, 0x1C8A2D4B */
63   2.02094576023350569471e+00, /* 0x40002AE5, 0x9C598AC8 */
64  -6.88283971605453293030e-01, /* 0xBFE6066C, 0x1B8D0159 */
65   7.70381505559019352791e-02}; /* 0x3FB3B8C5, 0xB12E9282 */
66
67 #ifdef __STDC__
68         double __ieee754_acos(double x)
69 #else
70         double __ieee754_acos(x)
71         double x;
72 #endif
73 {
74         double z,p,q,r,w,s,c,df,p1,p2,p3,q1,q2,z2,z4,z6;
75         int32_t hx,ix;
76         GET_HIGH_WORD(hx,x);
77         ix = hx&0x7fffffff;
78         if(ix>=0x3ff00000) {    /* |x| >= 1 */
79             u_int32_t lx;
80             GET_LOW_WORD(lx,x);
81             if(((ix-0x3ff00000)|lx)==0) {       /* |x|==1 */
82                 if(hx>0) return 0.0;            /* acos(1) = 0  */
83                 else return pi+2.0*pio2_lo;     /* acos(-1)= pi */
84             }
85             return (x-x)/(x-x);         /* acos(|x|>1) is NaN */
86         }
87         if(ix<0x3fe00000) {     /* |x| < 0.5 */
88             if(ix<=0x3c600000) return pio2_hi+pio2_lo;/*if|x|<2**-57*/
89             z = x*x;
90 #ifdef DO_NOT_USE_THIS
91             p = z*(pS0+z*(pS1+z*(pS2+z*(pS3+z*(pS4+z*pS5)))));
92             q = one+z*(qS1+z*(qS2+z*(qS3+z*qS4)));
93 #else
94             p1 = z*pS[0]; z2=z*z;
95             p2 = pS[1]+z*pS[2]; z4=z2*z2;
96             p3 = pS[3]+z*pS[4]; z6=z4*z2;
97             q1 = one+z*qS[1];
98             q2 = qS[2]+z*qS[3];
99             p = p1 + z2*p2 + z4*p3 + z6*pS[5];
100             q = q1 + z2*q2 + z4*qS[4];
101 #endif
102             r = p/q;
103             return pio2_hi - (x - (pio2_lo-x*r));
104         } else  if (hx<0) {             /* x < -0.5 */
105             z = (one+x)*0.5;
106 #ifdef DO_NOT_USE_THIS
107             p = z*(pS0+z*(pS1+z*(pS2+z*(pS3+z*(pS4+z*pS5)))));
108             q = one+z*(qS1+z*(qS2+z*(qS3+z*qS4)));
109 #else
110             p1 = z*pS[0]; z2=z*z;
111             p2 = pS[1]+z*pS[2]; z4=z2*z2;
112             p3 = pS[3]+z*pS[4]; z6=z4*z2;
113             q1 = one+z*qS[1];
114             q2 = qS[2]+z*qS[3];
115             p = p1 + z2*p2 + z4*p3 + z6*pS[5];
116             q = q1 + z2*q2 + z4*qS[4];
117 #endif
118             s = __ieee754_sqrt(z);
119             r = p/q;
120             w = r*s-pio2_lo;
121             return pi - 2.0*(s+w);
122         } else {                        /* x > 0.5 */
123             z = (one-x)*0.5;
124             s = __ieee754_sqrt(z);
125             df = s;
126             SET_LOW_WORD(df,0);
127             c  = (z-df*df)/(s+df);
128 #ifdef DO_NOT_USE_THIS
129             p = z*(pS0+z*(pS1+z*(pS2+z*(pS3+z*(pS4+z*pS5)))));
130             q = one+z*(qS1+z*(qS2+z*(qS3+z*qS4)));
131 #else
132             p1 = z*pS[0]; z2=z*z;
133             p2 = pS[1]+z*pS[2]; z4=z2*z2;
134             p3 = pS[3]+z*pS[4]; z6=z4*z2;
135             q1 = one+z*qS[1];
136             q2 = qS[2]+z*qS[3];
137             p = p1 + z2*p2 + z4*p3 + z6*pS[5];
138             q = q1 + z2*q2 + z4*qS[4];
139 #endif
140             r = p/q;
141             w = r*s+c;
142             return 2.0*(df+w);
143         }
144 }