OpenCores
URL https://opencores.org/ocsvn/openrisc/openrisc/trunk

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [gnu-old/] [newlib-1.17.0/] [newlib/] [libm/] [mathfp/] [s_sine.c] - Blame information for rev 825

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 148 jeremybenn
 
2
/* @(#)z_sine.c 1.0 98/08/13 */
3
/******************************************************************
4
 * The following routines are coded directly from the algorithms
5
 * and coefficients given in "Software Manual for the Elementary
6
 * Functions" by William J. Cody, Jr. and William Waite, Prentice
7
 * Hall, 1980.
8
 ******************************************************************/
9
 
10
/*
11
FUNCTION
12
        <<sin>>, <<cos>>, <<sine>>, <<sinf>>, <<cosf>>, <<sinef>>---sine or cosine
13
INDEX
14
sin
15
INDEX
16
sinf
17
INDEX
18
cos
19
INDEX
20
cosf
21
ANSI_SYNOPSIS
22
        #include <math.h>
23
        double sin(double <[x]>);
24
        float  sinf(float <[x]>);
25
        double cos(double <[x]>);
26
        float cosf(float <[x]>);
27
 
28
TRAD_SYNOPSIS
29
        #include <math.h>
30
        double sin(<[x]>)
31
        double <[x]>;
32
        float  sinf(<[x]>)
33
        float <[x]>;
34
 
35
        double cos(<[x]>)
36
        double <[x]>;
37
        float cosf(<[x]>)
38
        float <[x]>;
39
 
40
DESCRIPTION
41
        <<sin>> and <<cos>> compute (respectively) the sine and cosine
42
        of the argument <[x]>.  Angles are specified in radians.
43
RETURNS
44
        The sine or cosine of <[x]> is returned.
45
 
46
PORTABILITY
47
        <<sin>> and <<cos>> are ANSI C.
48
        <<sinf>> and <<cosf>> are extensions.
49
 
50
QUICKREF
51
        sin ansi pure
52
        sinf - pure
53
*/
54
 
55
/******************************************************************
56
 * sine
57
 *
58
 * Input:
59
 *   x - floating point value
60
 *   cosine - indicates cosine value
61
 *
62
 * Output:
63
 *   Sine of x.
64
 *
65
 * Description:
66
 *   This routine calculates sines and cosines.
67
 *
68
 *****************************************************************/
69
 
70
#include "fdlibm.h"
71
#include "zmath.h"
72
 
73
#ifndef _DOUBLE_IS_32BITS
74
 
75
static const double HALF_PI = 1.57079632679489661923;
76
static const double ONE_OVER_PI = 0.31830988618379067154;
77
static const double r[] = { -0.16666666666666665052,
78
                             0.83333333333331650314e-02,
79
                            -0.19841269841201840457e-03,
80
                             0.27557319210152756119e-05,
81
                            -0.25052106798274584544e-07,
82
                             0.16058936490371589114e-09,
83
                            -0.76429178068910467734e-12,
84
                             0.27204790957888846175e-14 };
85
 
86
double
87
_DEFUN (sine, (double, int),
88
        double x _AND
89
        int cosine)
90
{
91
  int sgn, N;
92
  double y, XN, g, R, res;
93
  double YMAX = 210828714.0;
94
 
95
  switch (numtest (x))
96
    {
97
      case NAN:
98
        errno = EDOM;
99
        return (x);
100
      case INF:
101
        errno = EDOM;
102
        return (z_notanum.d);
103
    }
104
 
105
  /* Use sin and cos properties to ease computations. */
106
  if (cosine)
107
    {
108
      sgn = 1;
109
      y = fabs (x) + HALF_PI;
110
    }
111
  else
112
    {
113
      if (x < 0.0)
114
        {
115
          sgn = -1;
116
          y = -x;
117
        }
118
      else
119
        {
120
          sgn = 1;
121
          y = x;
122
        }
123
    }
124
 
125
  /* Check for values of y that will overflow here. */
126
  if (y > YMAX)
127
    {
128
      errno = ERANGE;
129
      return (x);
130
    }
131
 
132
  /* Calculate the exponent. */
133
  if (y < 0.0)
134
    N = (int) (y * ONE_OVER_PI - 0.5);
135
  else
136
    N = (int) (y * ONE_OVER_PI + 0.5);
137
  XN = (double) N;
138
 
139
  if (N & 1)
140
    sgn = -sgn;
141
 
142
  if (cosine)
143
    XN -= 0.5;
144
 
145
  y = fabs (x) - XN * __PI;
146
 
147
  if (-z_rooteps < y && y < z_rooteps)
148
    res = y;
149
 
150
  else
151
    {
152
      g = y * y;
153
 
154
      /* Calculate the Taylor series. */
155
      R = (((((((r[6] * g + r[5]) * g + r[4]) * g + r[3]) * g + r[2]) * g + r[1]) * g + r[0]) * g);
156
 
157
      /* Finally, compute the result. */
158
      res = y + y * R;
159
    }
160
 
161
  res *= sgn;
162
 
163
  return (res);
164
}
165
 
166
#endif /* _DOUBLE_IS_32BITS */

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.