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

Subversion Repositories or1k

[/] [or1k/] [trunk/] [newlib/] [newlib/] [libm/] [math/] [w_pow.c] - Blame information for rev 39

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

Line No. Rev Author Line
1 39 lampret
 
2
 
3
/* @(#)w_pow.c 5.2 93/10/01 */
4
/*
5
 * ====================================================
6
 * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
7
 *
8
 * Developed at SunPro, a Sun Microsystems, Inc. business.
9
 * Permission to use, copy, modify, and distribute this
10
 * software is freely granted, provided that this notice
11
 * is preserved.
12
 * ====================================================
13
 */
14
 
15
/*
16
FUNCTION
17
        <<pow>>, <<powf>>---x to the power y
18
INDEX
19
        pow
20
INDEX
21
        powf
22
 
23
 
24
ANSI_SYNOPSIS
25
        #include <math.h>
26
        double pow(double <[x]>, double <[y]>);
27
        float pow(float <[x]>, float <[y]>);
28
 
29
TRAD_SYNOPSIS
30
        #include <math.h>
31
        double pow(<[x]>, <[y]>);
32
        double <[x]>, <[y]>;
33
 
34
        float pow(<[x]>, <[y]>);
35
        float <[x]>, <[y]>;
36
 
37
DESCRIPTION
38
        <<pow>> and <<powf>> calculate <[x]> raised to the exp1.0nt <[y]>.
39
        @tex
40
        (That is, $x^y$.)
41
        @end tex
42
 
43
RETURNS
44
        On success, <<pow>> and <<powf>> return the value calculated.
45
 
46
        When the argument values would produce overflow, <<pow>>
47
        returns <<HUGE_VAL>> and set <<errno>> to <<ERANGE>>.  If the
48
        argument <[x]> passed to <<pow>> or <<powf>> is a negative
49
        noninteger, and <[y]> is also not an integer, then <<errno>>
50
        is set to <<EDOM>>.  If <[x]> and <[y]> are both 0, then
51
        <<pow>> and <<powf>> return <<1>>.
52
 
53
        You can modify error handling for these functions using <<matherr>>.
54
 
55
PORTABILITY
56
        <<pow>> is ANSI C. <<powf>> is an extension.  */
57
 
58
/*
59
 * wrapper pow(x,y) return x**y
60
 */
61
 
62
#include "fdlibm.h"
63
#include <errno.h>
64
 
65
#ifndef _DOUBLE_IS_32BITS
66
 
67
#ifdef __STDC__
68
        double pow(double x, double y)  /* wrapper pow */
69
#else
70
        double pow(x,y)                 /* wrapper pow */
71
        double x,y;
72
#endif
73
{
74
#ifdef _IEEE_LIBM
75
        return  __ieee754_pow(x,y);
76
#else
77
        double z;
78
#ifndef HUGE_VAL 
79
#define HUGE_VAL inf
80
        double inf = 0.0;
81
 
82
        SET_HIGH_WORD(inf,0x7ff00000);  /* set inf to infinite */
83
#endif
84
        struct exception exc;
85
        z=__ieee754_pow(x,y);
86
        if(_LIB_VERSION == _IEEE_|| isnan(y)) return z;
87
        if(isnan(x)) {
88
            if(y==0.0) {
89
                /* pow(NaN,0.0) */
90
                /* error only if _LIB_VERSION == _SVID_ & _XOPEN_ */
91
                exc.type = DOMAIN;
92
                exc.name = "pow";
93
                exc.retval = x;
94
                if (_LIB_VERSION == _IEEE_ ||
95
                    _LIB_VERSION == _POSIX_) exc.retval = 1.0;
96
                else if (!matherr(&exc)) {
97
                        errno = EDOM;
98
                }
99
                if (exc.err != 0)
100
                   errno = exc.err;
101
                return exc.retval;
102
            } else
103
                return z;
104
        }
105
        if(x==0.0){
106
            if(y==0.0) {
107
                /* pow(0.0,0.0) */
108
                /* error only if _LIB_VERSION == _SVID_ */
109
                exc.type = DOMAIN;
110
                exc.name = "pow";
111
                exc.retval = 0.0;
112
                if (_LIB_VERSION != _SVID_) exc.retval = 1.0;
113
                else if (!matherr(&exc)) {
114
                        errno = EDOM;
115
                }
116
                if (exc.err != 0)
117
                   errno = exc.err;
118
                return exc.retval;
119
            }
120
            if(finite(y)&&y<0.0) {
121
                /* 0**neg */
122
                exc.type = DOMAIN;
123
                exc.name = "pow";
124
                if (_LIB_VERSION == _SVID_)
125
                  exc.retval = 0.0;
126
                else
127
                  exc.retval = -HUGE_VAL;
128
                if (_LIB_VERSION == _POSIX_)
129
                  errno = EDOM;
130
                else if (!matherr(&exc)) {
131
                  errno = EDOM;
132
                }
133
                if (exc.err != 0)
134
                   errno = exc.err;
135
                return exc.retval;
136
            }
137
            return z;
138
        }
139
        if(!finite(z)) {
140
            if(finite(x)&&finite(y)) {
141
                if(isnan(z)) {
142
                    /* neg**non-integral */
143
                    exc.type = DOMAIN;
144
                    exc.name = "pow";
145
                    if (_LIB_VERSION == _SVID_)
146
                        exc.retval = 0.0;
147
                    else
148
                        exc.retval = 0.0/0.0;   /* X/Open allow NaN */
149
                    if (_LIB_VERSION == _POSIX_)
150
                        errno = EDOM;
151
                    else if (!matherr(&exc)) {
152
                        errno = EDOM;
153
                    }
154
                    if (exc.err != 0)
155
                        errno = exc.err;
156
                    return exc.retval;
157
                } else {
158
                    /* pow(x,y) overflow */
159
                    exc.type = OVERFLOW;
160
                    exc.name = "pow";
161
                    if (_LIB_VERSION == _SVID_) {
162
                       exc.retval = HUGE;
163
                       y *= 0.5;
164
                       if(x<0.0&&rint(y)!=y) exc.retval = -HUGE;
165
                    } else {
166
                       exc.retval = HUGE_VAL;
167
                       y *= 0.5;
168
                       if(x<0.0&&rint(y)!=y) exc.retval = -HUGE_VAL;
169
                    }
170
                    if (_LIB_VERSION == _POSIX_)
171
                        errno = ERANGE;
172
                    else if (!matherr(&exc)) {
173
                        errno = ERANGE;
174
                    }
175
                    if (exc.err != 0)
176
                        errno = exc.err;
177
                    return exc.retval;
178
                }
179
            }
180
        }
181
        if(z==0.0&&finite(x)&&finite(y)) {
182
            /* pow(x,y) underflow */
183
            exc.type = UNDERFLOW;
184
            exc.name = "pow";
185
            exc.retval =  0.0;
186
            if (_LIB_VERSION == _POSIX_)
187
                errno = ERANGE;
188
            else if (!matherr(&exc)) {
189
                errno = ERANGE;
190
            }
191
            if (exc.err != 0)
192
                errno = exc.err;
193
            return exc.retval;
194
        }
195
        return z;
196
#endif
197
}
198
 
199
#endif /* defined(_DOUBLE_IS_32BITS) */
200
 
201
 
202
 
203
 
204
 
205
 
206
 
207
 
208
 
209
 
210
 
211
 
212
 
213
 

powered by: WebSVN 2.1.0

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