2009-02-19 18:54:09 +08:00
|
|
|
/**
|
|
|
|
* This file has no copyright assigned and is placed in the Public Domain.
|
|
|
|
* This file is part of the w64 mingw-runtime package.
|
2010-01-16 04:02:21 +08:00
|
|
|
* No warranty is given; refer to the file DISCLAIMER.PD within this package.
|
2009-02-19 18:54:09 +08:00
|
|
|
*/
|
|
|
|
#include "cephes_mconf.h"
|
|
|
|
|
|
|
|
#ifndef _SET_ERRNO
|
|
|
|
#define _SET_ERRNO(x)
|
|
|
|
#endif
|
2009-08-23 17:30:34 +08:00
|
|
|
float __powif (float x, int nn);
|
2009-02-19 18:54:09 +08:00
|
|
|
|
2009-08-23 17:30:34 +08:00
|
|
|
float __powif (float x, int nn)
|
2009-02-19 18:54:09 +08:00
|
|
|
{
|
2009-08-30 16:35:43 +08:00
|
|
|
int n, e, sign, asign, lx;
|
|
|
|
float w, y, s;
|
2009-02-19 18:54:09 +08:00
|
|
|
|
2009-08-30 16:35:43 +08:00
|
|
|
/* See pow.c for these tests. */
|
|
|
|
if (x == 0.0F)
|
2009-02-19 18:54:09 +08:00
|
|
|
{
|
2009-08-30 16:35:43 +08:00
|
|
|
if (nn == 0)
|
|
|
|
return (1.0F );
|
|
|
|
else if (nn < 0)
|
|
|
|
return (INFINITYF);
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (nn & 1)
|
|
|
|
return (x);
|
|
|
|
else
|
|
|
|
return (0.0);
|
|
|
|
}
|
2009-02-19 18:54:09 +08:00
|
|
|
}
|
|
|
|
|
2009-08-30 16:35:43 +08:00
|
|
|
if (nn == 0)
|
|
|
|
return (1.0);
|
2009-02-19 18:54:09 +08:00
|
|
|
|
2009-08-30 16:35:43 +08:00
|
|
|
if (nn == -1)
|
|
|
|
return (1.0/x);
|
2009-02-19 18:54:09 +08:00
|
|
|
|
2009-08-30 16:35:43 +08:00
|
|
|
if (x < 0.0)
|
2009-02-19 18:54:09 +08:00
|
|
|
{
|
2009-08-30 16:35:43 +08:00
|
|
|
asign = -1;
|
|
|
|
x = -x;
|
2009-02-19 18:54:09 +08:00
|
|
|
}
|
2009-08-30 16:35:43 +08:00
|
|
|
else
|
|
|
|
asign = 0;
|
2009-02-19 18:54:09 +08:00
|
|
|
|
2009-08-30 16:35:43 +08:00
|
|
|
if (nn < 0)
|
2009-02-19 18:54:09 +08:00
|
|
|
{
|
2009-08-30 16:35:43 +08:00
|
|
|
sign = -1;
|
|
|
|
n = -nn;
|
2009-02-19 18:54:09 +08:00
|
|
|
}
|
2009-08-30 16:35:43 +08:00
|
|
|
else
|
2009-02-19 18:54:09 +08:00
|
|
|
{
|
2009-08-30 16:35:43 +08:00
|
|
|
sign = 1;
|
|
|
|
n = nn;
|
2009-02-19 18:54:09 +08:00
|
|
|
}
|
|
|
|
|
2009-08-30 16:35:43 +08:00
|
|
|
/* Even power will be positive. */
|
|
|
|
if ((n & 1) == 0)
|
|
|
|
asign = 0;
|
2009-02-19 18:54:09 +08:00
|
|
|
|
2009-08-30 16:35:43 +08:00
|
|
|
/* Overflow detection */
|
2009-02-19 18:54:09 +08:00
|
|
|
|
2009-08-30 16:35:43 +08:00
|
|
|
/* Calculate approximate logarithm of answer */
|
|
|
|
s = frexpf(x, &lx);
|
|
|
|
e = (lx - 1)*n;
|
|
|
|
if ((e == 0) || (e > 64) || (e < -64))
|
2009-02-19 18:54:09 +08:00
|
|
|
{
|
2009-08-30 16:35:43 +08:00
|
|
|
s = (s - 7.0710678118654752e-1) / (s + 7.0710678118654752e-1);
|
|
|
|
s = (2.9142135623730950 * s - 0.5 + lx) * nn * LOGE2F;
|
2009-02-19 18:54:09 +08:00
|
|
|
}
|
2009-08-30 16:35:43 +08:00
|
|
|
else
|
2009-02-19 18:54:09 +08:00
|
|
|
{
|
2009-08-30 16:35:43 +08:00
|
|
|
s = LOGE2F * e;
|
2009-02-19 18:54:09 +08:00
|
|
|
}
|
|
|
|
|
2009-08-30 16:35:43 +08:00
|
|
|
if (s > MAXLOGF)
|
2009-02-19 18:54:09 +08:00
|
|
|
{
|
2009-08-30 16:35:43 +08:00
|
|
|
mtherr("__powif", OVERFLOW);
|
|
|
|
_SET_ERRNO(ERANGE);
|
|
|
|
y = INFINITYF;
|
|
|
|
goto done;
|
2009-02-19 18:54:09 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
#if DENORMAL
|
2009-08-30 16:35:43 +08:00
|
|
|
if (s < MINLOGF)
|
2009-02-19 18:54:09 +08:00
|
|
|
{
|
2009-08-30 16:35:43 +08:00
|
|
|
y = 0.0;
|
|
|
|
goto done;
|
2009-02-19 18:54:09 +08:00
|
|
|
}
|
|
|
|
|
2009-08-30 16:35:43 +08:00
|
|
|
/* Handle tiny denormal answer, but with less accuracy
|
|
|
|
* since roundoff error in 1.0/x will be amplified.
|
|
|
|
* The precise demarcation should be the gradual underflow threshold.
|
|
|
|
*/
|
|
|
|
if ((s < (-MAXLOGF+2.0)) && (sign < 0))
|
2009-02-19 18:54:09 +08:00
|
|
|
{
|
2009-08-30 16:35:43 +08:00
|
|
|
x = 1.0/x;
|
|
|
|
sign = -sign;
|
2009-02-19 18:54:09 +08:00
|
|
|
}
|
|
|
|
#else
|
2009-08-30 16:35:43 +08:00
|
|
|
/* do not produce denormal answer */
|
|
|
|
if (s < -MAXLOGF)
|
|
|
|
return (0.0);
|
2009-02-19 18:54:09 +08:00
|
|
|
#endif
|
|
|
|
|
2009-08-30 16:35:43 +08:00
|
|
|
/* First bit of the power */
|
|
|
|
if (n & 1)
|
|
|
|
y = x;
|
|
|
|
else
|
|
|
|
y = 1.0;
|
2009-02-19 18:54:09 +08:00
|
|
|
|
2009-08-30 16:35:43 +08:00
|
|
|
w = x;
|
2009-02-19 18:54:09 +08:00
|
|
|
n >>= 1;
|
2009-08-30 16:35:43 +08:00
|
|
|
while (n)
|
|
|
|
{
|
|
|
|
w = w * w; /* arg to the 2-to-the-kth power */
|
|
|
|
if (n & 1) /* if that bit is set, then include in product */
|
|
|
|
y *= w;
|
|
|
|
n >>= 1;
|
2009-02-19 18:54:09 +08:00
|
|
|
}
|
|
|
|
|
2009-08-30 16:35:43 +08:00
|
|
|
if (sign < 0)
|
|
|
|
y = 1.0/y;
|
2009-02-19 18:54:09 +08:00
|
|
|
|
|
|
|
done:
|
|
|
|
|
2009-08-30 16:35:43 +08:00
|
|
|
if (asign)
|
2009-02-19 18:54:09 +08:00
|
|
|
{
|
2009-08-30 16:35:43 +08:00
|
|
|
/* odd power of negative number */
|
|
|
|
if (y == 0.0)
|
|
|
|
y = NEGZEROF;
|
|
|
|
else
|
|
|
|
y = -y;
|
2009-02-19 18:54:09 +08:00
|
|
|
}
|
2009-08-30 16:35:43 +08:00
|
|
|
return (y);
|
2009-02-19 18:54:09 +08:00
|
|
|
}
|
2009-08-30 16:35:43 +08:00
|
|
|
|