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

Subversion Repositories or1k

[/] [or1k/] [branches/] [newlib/] [newlib/] [newlib/] [libm/] [mathfp/] [sf_atangent.c] - Blame information for rev 56

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

Line No. Rev Author Line
1 56 joel
 
2
/* @(#)z_atangentf.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
 * Arctangent
11
 *
12
 * Input:
13
 *   x - floating point value
14
 *
15
 * Output:
16
 *   arctangent of x
17
 *
18
 * Description:
19
 *   This routine calculates arctangents.
20
 *
21
 *****************************************************************/
22
 
23
#include <float.h>
24
#include "fdlibm.h"
25
#include "zmath.h"
26
 
27
static const float ROOT3 = 1.732050807;
28
static const float a[] = { 0.0, 0.523598775, 1.570796326,
29
                     1.047197551 };
30
static const float q[] = { 0.1412500740e+1 };
31
static const float p[] = { -0.4708325141, -0.5090958253e-1 };
32
 
33
float
34
_DEFUN (atangentf, (float, float, float, int),
35
        float x _AND
36
        float v _AND
37
        float u _AND
38
        int arctan2)
39
{
40
  float f, g, R, P, Q, A, res;
41
  int N;
42
  int branch = 0;
43
  int expv, expu;
44
 
45
  /* Preparation for calculating arctan2. */
46
  if (arctan2)
47
    {
48
      if (u == 0.0)
49
        if (v == 0.0)
50
          {
51
            errno = ERANGE;
52
            return (z_notanum_f.f);
53
          }
54
        else
55
          {
56
            branch = 1;
57
            res = __PI_OVER_TWO;
58
          }
59
 
60
      if (!branch)
61
        {
62
          /* Get the exponent values of the inputs. */
63
          g = frexpf (v, &expv);
64
          g = frexpf (u, &expu);
65
 
66
          /* See if a divide will overflow. */
67
          if ((expv - expu) > DBL_MAX_EXP + 1022)
68
            {
69
               branch = 1;
70
               res = __PI_OVER_TWO;
71
            }
72
 
73
          /* Also check for underflow. */
74
          else if ((expv - expu) < - DBL_MIN_EXP - 1022)
75
            {
76
               branch = 2;
77
               res = 0.0;
78
            }
79
         }
80
    }
81
 
82
  if (!branch)
83
    {
84
      if (arctan2)
85
        f = fabsf (v / u);
86
      else
87
        f = fabsf (x);
88
 
89
      if (f > 1.0)
90
        {
91
          f = 1.0 / f;
92
          N = 2;
93
        }
94
      else
95
        N = 0;
96
 
97
      if (f > (2.0 - ROOT3))
98
        {
99
          A = ROOT3 - 1.0;
100
          f = (((A * f - 0.5) - 0.5) + f) / (ROOT3 + f);
101
          N++;
102
        }
103
 
104
      /* Check for values that are too small. */
105
      if (-z_rooteps_f < f && f < z_rooteps_f)
106
        res = f;
107
 
108
      /* Calculate the Taylor series. */
109
      else
110
        {
111
          g = f * f;
112
          P = (p[1] * g + p[0]) * g;
113
          Q = g + q[0];
114
          R = P / Q;
115
 
116
          res = f + f * R;
117
        }
118
 
119
      if (N > 1)
120
        res = -res;
121
 
122
      res += a[N];
123
    }
124
 
125
  if (arctan2 && branch)
126
    {
127
      if (u < 0.0 || branch == 2)
128
        res = __PI - res;
129
      if (v < 0.0 || branch == 1)
130
        res = -res;
131
    }
132
  else if (x < 0.0)
133
    {
134
      res = -res;
135
    }
136
 
137
  return (res);
138
}

powered by: WebSVN 2.1.0

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