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

Subversion Repositories or1k

[/] [or1k/] [branches/] [newlib/] [newlib/] [newlib/] [libm/] [mathfp/] [s_pow.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_pow.c 1.0 98/08/13 */
3
 
4
/*
5
FUNCTION
6
        <<pow>>, <<powf>>---x to the power y
7
INDEX
8
        pow
9
INDEX
10
        powf
11
 
12
 
13
ANSI_SYNOPSIS
14
        #include <math.h>
15
        double pow(double <[x]>, double <[y]>);
16
        float pow(float <[x]>, float <[y]>);
17
 
18
TRAD_SYNOPSIS
19
        #include <math.h>
20
        double pow(<[x]>, <[y]>);
21
        double <[x]>, <[y]>;
22
 
23
        float pow(<[x]>, <[y]>);
24
        float <[x]>, <[y]>;
25
 
26
DESCRIPTION
27
        <<pow>> and <<powf>> calculate <[x]> raised to the exp1.0nt <[y]>.
28
        @tex
29
        (That is, $x^y$.)
30
        @end tex
31
 
32
RETURNS
33
        On success, <<pow>> and <<powf>> return the value calculated.
34
 
35
        When the argument values would produce overflow, <<pow>>
36
        returns <<HUGE_VAL>> and set <<errno>> to <<ERANGE>>.  If the
37
        argument <[x]> passed to <<pow>> or <<powf>> is a negative
38
        noninteger, and <[y]> is also not an integer, then <<errno>>
39
        is set to <<EDOM>>.  If <[x]> and <[y]> are both 0, then
40
        <<pow>> and <<powf>> return <<1>>.
41
 
42
        You can modify error handling for these functions using <<matherr>>.
43
 
44
PORTABILITY
45
        <<pow>> is ANSI C. <<powf>> is an extension.  */
46
 
47
#include <float.h>
48
#include "fdlibm.h"
49
#include "zmath.h"
50
 
51
#ifndef _DOUBLE_IS_32BITS
52
 
53
double pow (double x, double y)
54
{
55
  double d, t, r = 1.0;
56
  int n, k, sign = 0;
57
  __uint32_t px;
58
 
59
  GET_HIGH_WORD (px, x);
60
 
61
  k = modf (y, &d);
62
  if (k == 0.0)
63
    {
64
      if (modf (ldexp (y, -1), &t))
65
        sign = 0;
66
      else
67
        sign = 1;
68
    }
69
 
70
  if (x == 0.0 && y <= 0.0)
71
    errno = EDOM;
72
 
73
  else if ((t = y * log (fabs (x))) >= BIGX)
74
    {
75
      errno = ERANGE;
76
      if (px & 0x80000000)
77
        {
78
          if (!k)
79
            {
80
              errno = EDOM;
81
              x = 0.0;
82
            }
83
          else if (sign)
84
            x = -z_infinity.d;
85
          else
86
            x =  z_infinity.d;
87
        }
88
 
89
    else
90
      x = z_infinity.d;
91
  }
92
 
93
  else if (t < SMALLX)
94
    {
95
      errno = ERANGE;
96
      x = 0.0;
97
    }
98
 
99
  else
100
    {
101
      if ( k && fabs(d) <= 32767 )
102
        {
103
          n = (int) d;
104
 
105
          if (sign = (n < 0))
106
            n = -n;
107
 
108
          while ( n > 0 )
109
            {
110
              if ((unsigned int) n % 2)
111
                r *= x;
112
              x *= x;
113
              n = (unsigned int) n / 2;
114
            }
115
 
116
          if (sign)
117
            r = 1.0 / r;
118
 
119
          return r;
120
        }
121
 
122
      else
123
        {
124
          if ( px & 0x80000000 )
125
            {
126
              if ( !k )
127
                {
128
                  errno = EDOM;
129
                  return 0.0;
130
                }
131
            }
132
 
133
          x = exp (t);
134
 
135
          if ( sign )
136
            {
137
              px ^= 0x80000000;
138
              SET_HIGH_WORD (x, px);
139
            }
140
        }
141
      }
142
 
143
  return x;
144
}
145
 
146
#endif _DOUBLE_IS_32BITS

powered by: WebSVN 2.1.0

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