?login_element?

Subversion Repositories NedoOS

Rev

Blame | Last modification | View Log | Download

  1. /*-------------------------------------------------------------------------
  2.    sincosf.c - Computes sin or cos of a 32-bit float as outlined in [1]
  3.  
  4.    Copyright (C) 2001, 2002, Jesus Calvino-Fraga, jesusc@ieee.org
  5.  
  6.    This library is free software; you can redistribute it and/or modify it
  7.    under the terms of the GNU General Public License as published by the
  8.    Free Software Foundation; either version 2, or (at your option) any
  9.    later version.
  10.  
  11.    This library is distributed in the hope that it will be useful,
  12.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  13.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14.    GNU General Public License for more details.
  15.  
  16.    You should have received a copy of the GNU General Public License
  17.    along with this library; see the file COPYING. If not, write to the
  18.    Free Software Foundation, 51 Franklin Street, Fifth Floor, Boston,
  19.    MA 02110-1301, USA.
  20.  
  21.    As a special exception, if you link this library with other files,
  22.    some of which are compiled with SDCC, to produce an executable,
  23.    this library does not by itself cause the resulting executable to
  24.    be covered by the GNU General Public License. This exception does
  25.    not however invalidate any other reasons why the executable file
  26.    might be covered by the GNU General Public License.
  27. -------------------------------------------------------------------------*/
  28.  
  29. /* [1] William James Cody and W.  M.  Waite.  _Software manual for the
  30.    elementary functions_, Englewood Cliffs, N.J.:Prentice-Hall, 1980. */
  31.  
  32. /* Version 1.0 - Initial release */
  33.  
  34. #include <math.h>
  35. #include <errno.h>
  36. #include <stdbool.h>
  37.  
  38. #define r1      -0.1666665668E+0
  39. #define r2       0.8333025139E-2
  40. #define r3      -0.1980741872E-3
  41. #define r4       0.2601903036E-5
  42.  
  43. /* PI=C1+C2 */
  44. #define C1       3.140625
  45. #define C2       9.676535897E-4
  46.  
  47. /*A reasonable value for YMAX is the int part of PI*B**(t/2)=3.1416*2**(12)*/
  48. #define YMAX     12867.0
  49.  
  50. float sincosf(float x, bool iscos)
  51. {
  52.     float y, f, r, g, XN;
  53.     int N;
  54.     bool sign;
  55.  
  56.     if(iscos)
  57.     {
  58.         y=fabsf(x)+HALF_PI;
  59.         sign=0;
  60.     }
  61.     else
  62.     {
  63.         if(x<0.0)
  64.             { y=-x; sign=1; }
  65.         else
  66.             { y=x; sign=0; }
  67.     }
  68.  
  69.     if(y>YMAX)
  70.     {
  71.         errno=ERANGE;
  72.         return 0.0;
  73.     }
  74.  
  75.     /*Round y/PI to the nearest integer*/
  76.     N=((y*iPI)+0.5); /*y is positive*/
  77.  
  78.     /*If N is odd change sign*/
  79.     if(N&1) sign=!sign;
  80.  
  81.     XN=N;
  82.     /*Cosine required? (is done here to keep accuracy)*/
  83.     if(iscos) XN-=0.5;
  84.  
  85.     y=fabsf(x);
  86.     r=(int)y;
  87.     g=y-r;
  88.     f=((r-XN*C1)+g)-XN*C2;
  89.  
  90.     g=f*f;
  91.     if(g>EPS2) //Used to be if(fabsf(f)>EPS)
  92.     {
  93.         r=(((r4*g+r3)*g+r2)*g+r1)*g;
  94.         f+=f*r;
  95.     }
  96.     return (sign?-f:f);
  97. }
  98.