1 |
786 |
skrzyp |
//===========================================================================
|
2 |
|
|
//
|
3 |
|
|
// e_asin.c
|
4 |
|
|
//
|
5 |
|
|
// Part of the standard mathematical function library
|
6 |
|
|
//
|
7 |
|
|
//===========================================================================
|
8 |
|
|
// ####ECOSGPLCOPYRIGHTBEGIN####
|
9 |
|
|
// -------------------------------------------
|
10 |
|
|
// This file is part of eCos, the Embedded Configurable Operating System.
|
11 |
|
|
// Copyright (C) 1998, 1999, 2000, 2001, 2002 Free Software Foundation, Inc.
|
12 |
|
|
//
|
13 |
|
|
// eCos is free software; you can redistribute it and/or modify it under
|
14 |
|
|
// the terms of the GNU General Public License as published by the Free
|
15 |
|
|
// Software Foundation; either version 2 or (at your option) any later
|
16 |
|
|
// version.
|
17 |
|
|
//
|
18 |
|
|
// eCos is distributed in the hope that it will be useful, but WITHOUT
|
19 |
|
|
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
20 |
|
|
// FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
21 |
|
|
// for more details.
|
22 |
|
|
//
|
23 |
|
|
// You should have received a copy of the GNU General Public License
|
24 |
|
|
// along with eCos; if not, write to the Free Software Foundation, Inc.,
|
25 |
|
|
// 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
26 |
|
|
//
|
27 |
|
|
// As a special exception, if other files instantiate templates or use
|
28 |
|
|
// macros or inline functions from this file, or you compile this file
|
29 |
|
|
// and link it with other works to produce a work based on this file,
|
30 |
|
|
// this file does not by itself cause the resulting work to be covered by
|
31 |
|
|
// the GNU General Public License. However the source code for this file
|
32 |
|
|
// must still be made available in accordance with section (3) of the GNU
|
33 |
|
|
// General Public License v2.
|
34 |
|
|
//
|
35 |
|
|
// This exception does not invalidate any other reasons why a work based
|
36 |
|
|
// on this file might be covered by the GNU General Public License.
|
37 |
|
|
// -------------------------------------------
|
38 |
|
|
// ####ECOSGPLCOPYRIGHTEND####
|
39 |
|
|
//===========================================================================
|
40 |
|
|
//#####DESCRIPTIONBEGIN####
|
41 |
|
|
//
|
42 |
|
|
// Author(s): jlarmour
|
43 |
|
|
// Contributors: jlarmour
|
44 |
|
|
// Date: 1998-02-13
|
45 |
|
|
// Purpose:
|
46 |
|
|
// Description:
|
47 |
|
|
// Usage:
|
48 |
|
|
//
|
49 |
|
|
//####DESCRIPTIONEND####
|
50 |
|
|
//
|
51 |
|
|
//===========================================================================
|
52 |
|
|
|
53 |
|
|
// CONFIGURATION
|
54 |
|
|
|
55 |
|
|
#include <pkgconf/libm.h> // Configuration header
|
56 |
|
|
|
57 |
|
|
// Include the Math library?
|
58 |
|
|
#ifdef CYGPKG_LIBM
|
59 |
|
|
|
60 |
|
|
// Derived from code with the following copyright
|
61 |
|
|
|
62 |
|
|
|
63 |
|
|
/* @(#)e_asin.c 1.3 95/01/18 */
|
64 |
|
|
/*
|
65 |
|
|
* ====================================================
|
66 |
|
|
* Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
|
67 |
|
|
*
|
68 |
|
|
* Developed at SunSoft, a Sun Microsystems, Inc. business.
|
69 |
|
|
* Permission to use, copy, modify, and distribute this
|
70 |
|
|
* software is freely granted, provided that this notice
|
71 |
|
|
* is preserved.
|
72 |
|
|
* ====================================================
|
73 |
|
|
*/
|
74 |
|
|
|
75 |
|
|
/* __ieee754_asin(x)
|
76 |
|
|
* Method :
|
77 |
|
|
* Since asin(x) = x + x^3/6 + x^5*3/40 + x^7*15/336 + ...
|
78 |
|
|
* we approximate asin(x) on [0,0.5] by
|
79 |
|
|
* asin(x) = x + x*x^2*R(x^2)
|
80 |
|
|
* where
|
81 |
|
|
* R(x^2) is a rational approximation of (asin(x)-x)/x^3
|
82 |
|
|
* and its remez error is bounded by
|
83 |
|
|
* |(asin(x)-x)/x^3 - R(x^2)| < 2^(-58.75)
|
84 |
|
|
*
|
85 |
|
|
* For x in [0.5,1]
|
86 |
|
|
* asin(x) = pi/2-2*asin(sqrt((1-x)/2))
|
87 |
|
|
* Let y = (1-x), z = y/2, s := sqrt(z), and pio2_hi+pio2_lo=pi/2;
|
88 |
|
|
* then for x>0.98
|
89 |
|
|
* asin(x) = pi/2 - 2*(s+s*z*R(z))
|
90 |
|
|
* = pio2_hi - (2*(s+s*z*R(z)) - pio2_lo)
|
91 |
|
|
* For x<=0.98, let pio4_hi = pio2_hi/2, then
|
92 |
|
|
* f = hi part of s;
|
93 |
|
|
* c = sqrt(z) - f = (z-f*f)/(s+f) ...f+c=sqrt(z)
|
94 |
|
|
* and
|
95 |
|
|
* asin(x) = pi/2 - 2*(s+s*z*R(z))
|
96 |
|
|
* = pio4_hi+(pio4-2s)-(2s*z*R(z)-pio2_lo)
|
97 |
|
|
* = pio4_hi+(pio4-2f)-(2s*z*R(z)-(pio2_lo+2c))
|
98 |
|
|
*
|
99 |
|
|
* Special cases:
|
100 |
|
|
* if x is NaN, return x itself;
|
101 |
|
|
* if |x|>1, return NaN with invalid signal.
|
102 |
|
|
*
|
103 |
|
|
*/
|
104 |
|
|
|
105 |
|
|
|
106 |
|
|
#include "mathincl/fdlibm.h"
|
107 |
|
|
|
108 |
|
|
static const double
|
109 |
|
|
one = 1.00000000000000000000e+00, /* 0x3FF00000, 0x00000000 */
|
110 |
|
|
huge = 1.000e+300,
|
111 |
|
|
pio2_hi = 1.57079632679489655800e+00, /* 0x3FF921FB, 0x54442D18 */
|
112 |
|
|
pio2_lo = 6.12323399573676603587e-17, /* 0x3C91A626, 0x33145C07 */
|
113 |
|
|
pio4_hi = 7.85398163397448278999e-01, /* 0x3FE921FB, 0x54442D18 */
|
114 |
|
|
/* coefficient for R(x^2) */
|
115 |
|
|
pS0 = 1.66666666666666657415e-01, /* 0x3FC55555, 0x55555555 */
|
116 |
|
|
pS1 = -3.25565818622400915405e-01, /* 0xBFD4D612, 0x03EB6F7D */
|
117 |
|
|
pS2 = 2.01212532134862925881e-01, /* 0x3FC9C155, 0x0E884455 */
|
118 |
|
|
pS3 = -4.00555345006794114027e-02, /* 0xBFA48228, 0xB5688F3B */
|
119 |
|
|
pS4 = 7.91534994289814532176e-04, /* 0x3F49EFE0, 0x7501B288 */
|
120 |
|
|
pS5 = 3.47933107596021167570e-05, /* 0x3F023DE1, 0x0DFDF709 */
|
121 |
|
|
qS1 = -2.40339491173441421878e+00, /* 0xC0033A27, 0x1C8A2D4B */
|
122 |
|
|
qS2 = 2.02094576023350569471e+00, /* 0x40002AE5, 0x9C598AC8 */
|
123 |
|
|
qS3 = -6.88283971605453293030e-01, /* 0xBFE6066C, 0x1B8D0159 */
|
124 |
|
|
qS4 = 7.70381505559019352791e-02; /* 0x3FB3B8C5, 0xB12E9282 */
|
125 |
|
|
|
126 |
|
|
double __ieee754_asin(double x)
|
127 |
|
|
{
|
128 |
|
|
double t,w,p,q,c,r,s;
|
129 |
|
|
int hx,ix;
|
130 |
|
|
|
131 |
|
|
hx = CYG_LIBM_HI(x);
|
132 |
|
|
ix = hx&0x7fffffff;
|
133 |
|
|
if(ix>= 0x3ff00000) { /* |x|>= 1 */
|
134 |
|
|
if(((ix-0x3ff00000)|CYG_LIBM_LO(x))==0)
|
135 |
|
|
/* asin(1)=+-pi/2 with inexact */
|
136 |
|
|
return x*pio2_hi+x*pio2_lo;
|
137 |
|
|
return (x-x)/(x-x); /* asin(|x|>1) is NaN */
|
138 |
|
|
} else if (ix<0x3fe00000) { /* |x|<0.5 */
|
139 |
|
|
if(ix<0x3e400000) { /* if |x| < 2**-27 */
|
140 |
|
|
if(huge+x>one) return x;/* return x with inexact if x!=0*/
|
141 |
|
|
} else {
|
142 |
|
|
t = x*x;
|
143 |
|
|
p = t*(pS0+t*(pS1+t*(pS2+t*(pS3+t*(pS4+t*pS5)))));
|
144 |
|
|
q = one+t*(qS1+t*(qS2+t*(qS3+t*qS4)));
|
145 |
|
|
w = p/q;
|
146 |
|
|
return x+x*w;
|
147 |
|
|
}
|
148 |
|
|
}
|
149 |
|
|
/* 1> |x|>= 0.5 */
|
150 |
|
|
w = one-fabs(x);
|
151 |
|
|
t = w*0.5;
|
152 |
|
|
p = t*(pS0+t*(pS1+t*(pS2+t*(pS3+t*(pS4+t*pS5)))));
|
153 |
|
|
q = one+t*(qS1+t*(qS2+t*(qS3+t*qS4)));
|
154 |
|
|
s = sqrt(t);
|
155 |
|
|
if(ix>=0x3FEF3333) { /* if |x| > 0.975 */
|
156 |
|
|
w = p/q;
|
157 |
|
|
t = pio2_hi-(2.0*(s+s*w)-pio2_lo);
|
158 |
|
|
} else {
|
159 |
|
|
w = s;
|
160 |
|
|
CYG_LIBM_LO(w) = 0;
|
161 |
|
|
c = (t-w*w)/(s+w);
|
162 |
|
|
r = p/q;
|
163 |
|
|
p = 2.0*s*r-(pio2_lo-2.0*c);
|
164 |
|
|
q = pio4_hi-2.0*w;
|
165 |
|
|
t = pio4_hi-(p-q);
|
166 |
|
|
}
|
167 |
|
|
if(hx>0) return t; else return -t;
|
168 |
|
|
}
|
169 |
|
|
|
170 |
|
|
#endif // ifdef CYGPKG_LIBM
|
171 |
|
|
|
172 |
|
|
// EOF e_asin.c
|