|
| 1 | +/* |
| 2 | + Copyright (C) 2001, 2002 Jesus Calvino-Fraga, jesusc@ieee.org |
| 3 | +
|
| 4 | + This library is free software; you can redistribute it and/or |
| 5 | + modify it under the terms of the GNU Lesser General Public |
| 6 | + License as published by the Free Software Foundation; either |
| 7 | + version 2.1 of the License, or (at your option) any later version. |
| 8 | +
|
| 9 | + This library is distributed in the hope that it will be useful, |
| 10 | + but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 12 | + Lesser General Public License for more details. |
| 13 | +
|
| 14 | + You should have received a copy of the GNU Lesser General Public |
| 15 | + License along with this library; if not, write to the Free Software |
| 16 | + Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 17 | +*/ |
| 18 | + |
| 19 | +union float_long |
| 20 | +{ |
| 21 | + float f; |
| 22 | + long l; |
| 23 | +}; |
| 24 | + |
| 25 | +#define HALF_PI 1.5707963268 |
| 26 | +#define iPI 0.3183098862 |
| 27 | +#define EPS2 59.6046E-9 |
| 28 | + |
| 29 | +/* fabsf: Returns the absolute value of a 32-bit float. */ |
| 30 | +static float fabsf(const float x) |
| 31 | +{ |
| 32 | + union float_long fl; |
| 33 | + |
| 34 | + fl.f = x; |
| 35 | + fl.l &= 0x7fffffff; |
| 36 | + return fl.f; |
| 37 | +} |
| 38 | + |
| 39 | +/* |
| 40 | + sincosf: Computes sin or cos of a 32-bit float as outlined in |
| 41 | + [1] William James Cody and W. M. Waite. _Software manual for the |
| 42 | + elementary functions_, Englewood Cliffs, N.J.:Prentice-Hall, 1980. |
| 43 | +*/ |
| 44 | +#define r1 (-0.1666665668E+0) |
| 45 | +#define r2 (0.8333025139E-2) |
| 46 | +#define r3 (-0.1980741872E-3) |
| 47 | +#define r4 (0.2601903036E-5) |
| 48 | + |
| 49 | +/* PI=C1+C2 */ |
| 50 | +#define C1 3.140625 |
| 51 | +#define C2 9.676535897E-4 |
| 52 | + |
| 53 | +/*A reasonable value for YMAX is the int part of PI*B**(t/2)=3.1416*2**(12)*/ |
| 54 | +#define YMAX 12867.0 |
| 55 | + |
| 56 | +static float sincosf(float x, int iscos) |
| 57 | +{ |
| 58 | + float y, f, r, g, XN; |
| 59 | + int N; |
| 60 | + char sign; |
| 61 | + |
| 62 | + if(iscos) |
| 63 | + { |
| 64 | + y=fabsf(x)+HALF_PI; |
| 65 | + sign=0; |
| 66 | + } |
| 67 | + else |
| 68 | + { |
| 69 | + if(x<0.0) |
| 70 | + { y=-x; sign=1; } |
| 71 | + else |
| 72 | + { y=x; sign=0; } |
| 73 | + } |
| 74 | + |
| 75 | + if(y>YMAX) |
| 76 | + { |
| 77 | + //errno=ERANGE; |
| 78 | + return 0.0; |
| 79 | + } |
| 80 | + |
| 81 | + /*Round y/PI to the nearest integer*/ |
| 82 | + N=((y*iPI)+0.5); /*y is positive*/ |
| 83 | + |
| 84 | + /*If N is odd change sign*/ |
| 85 | + if(N&1) sign=!sign; |
| 86 | + |
| 87 | + XN=N; |
| 88 | + /*Cosine required? (is done here to keep accuracy)*/ |
| 89 | + if(iscos) XN-=0.5; |
| 90 | + |
| 91 | + y=fabsf(x); |
| 92 | + r=(int)y; |
| 93 | + g=y-r; |
| 94 | + f=((r-XN*C1)+g)-XN*C2; |
| 95 | + |
| 96 | + g=f*f; |
| 97 | + if(g>EPS2) //Used to be if(fabsf(f)>EPS) |
| 98 | + { |
| 99 | + r=(((r4*g+r3)*g+r2)*g+r1)*g; |
| 100 | + f+=f*r; |
| 101 | + } |
| 102 | + return (sign?-f:f); |
| 103 | +} |
| 104 | + |
| 105 | +float sinf(float x) |
| 106 | +{ |
| 107 | + if (x==0.0) return 0.0; |
| 108 | + return sincosf(x, 0); |
| 109 | +} |
| 110 | + |
| 111 | +float cosf(float x) |
| 112 | +{ |
| 113 | + if (x==0.0) return 1.0; |
| 114 | + return sincosf(x, 1); |
| 115 | +} |
0 commit comments