Re-style fdlibm to conform to jerry guidelines

* First re-style was done automatically by indent to minimize the
  chance of errors during rewrite.

* Manual changes were applied to non-critical places only (comments
  and spaces):
  * Replaced all tabs with spaces.
  * Fixed tab stops in formulae in function comments.
    (Note: ASCII art for math formulae (especially for super- and
    subscripts) is a terrible idea.)
  * Unified the style of function comments.
  * Moved some in-code comments to their right places, which indent
    couldn't handle.
  * Added spaces to formulae of in-code comments to make them more
    readable.
  * Added braces mandated by jerry style guidelines.
  * Added parentheses to multiline #ifdef.

JerryScript-DCO-1.0-Signed-off-by: Akos Kiss akiss@inf.u-szeged.hu
This commit is contained in:
Akos Kiss
2016-03-17 10:42:00 +01:00
parent b39474c746
commit 8dd5186a0d
19 changed files with 2726 additions and 1887 deletions
+15 -15
View File
@@ -52,29 +52,29 @@ extern "C"
#define M_2_SQRTPI 1.1283791670955125738961589031215452 #define M_2_SQRTPI 1.1283791670955125738961589031215452
// Trigonometric functions // Trigonometric functions
double cos(double); double cos (double);
double sin(double); double sin (double);
double tan(double); double tan (double);
double acos(double); double acos (double);
double asin(double); double asin (double);
double atan(double); double atan (double);
double atan2(double, double); double atan2 (double, double);
// Exponential and logarithmic functions // Exponential and logarithmic functions
double exp(double); double exp (double);
double log(double); double log (double);
// Power functions // Power functions
double pow(double, double); double pow (double, double);
double sqrt(double); double sqrt (double);
// Rounding and remainder functions // Rounding and remainder functions
double ceil(double); double ceil (double);
double floor(double); double floor (double);
// Other functions // Other functions
double fabs(double); double fabs (double);
double fmod(double, double); double fmod (double, double);
#ifdef __cplusplus #ifdef __cplusplus
} }
+10 -10
View File
@@ -13,27 +13,27 @@
/* Sometimes it's necessary to define __LITTLE_ENDIAN explicitly /* Sometimes it's necessary to define __LITTLE_ENDIAN explicitly
but these catch some common cases. */ but these catch some common cases. */
#if defined(i386) || defined(__i386) || defined(__i386__) || \ #if (defined (i386) || defined (__i386) || defined (__i386__) || \
defined(i486) || defined(__i486) || defined(__i486__) || \ defined (i486) || defined (__i486) || defined (__i486__) || \
defined(intel) || defined(x86) || defined(i86pc) || \ defined (intel) || defined (x86) || defined (i86pc) || \
defined(__alpha) || defined(__osf__) || \ defined (__alpha) || defined (__osf__) || \
defined(__x86_64__) || defined(__arm__) defined (__x86_64__) || defined (__arm__))
#define __LITTLE_ENDIAN #define __LITTLE_ENDIAN
#endif #endif
#ifdef __LITTLE_ENDIAN #ifdef __LITTLE_ENDIAN
#define __HI(x) *(1+(int*)&x) #define __HI(x) *(1 + (int *) &x)
#define __LO(x) *(int*)&x #define __LO(x) *(int *) &x
#else #else
#define __HI(x) *(int*)&x #define __HI(x) *(int *) &x
#define __LO(x) *(1+(int*)&x) #define __LO(x) *(1 + (int *) &x)
#endif #endif
/* /*
* ANSI/POSIX * ANSI/POSIX
*/ */
#define MAXFLOAT ((float)3.40282346638528860e+38) #define MAXFLOAT ((float) 3.40282346638528860e+38)
#define HUGE MAXFLOAT #define HUGE MAXFLOAT
+56 -37
View File
@@ -12,7 +12,8 @@
*/ */
/* acos(x) /* acos(x)
* Method : *
* Method:
* acos(x) = pi/2 - asin(x) * acos(x) = pi/2 - asin(x)
* acos(-x) = pi/2 + asin(x) * acos(-x) = pi/2 + asin(x)
* For |x|<=0.5 * For |x|<=0.5
@@ -52,44 +53,62 @@
#define qS3 -6.88283971605453293030e-01 /* 0xBFE6066C, 0x1B8D0159 */ #define qS3 -6.88283971605453293030e-01 /* 0xBFE6066C, 0x1B8D0159 */
#define qS4 7.70381505559019352791e-02 /* 0x3FB3B8C5, 0xB12E9282 */ #define qS4 7.70381505559019352791e-02 /* 0x3FB3B8C5, 0xB12E9282 */
double acos(double x) double
acos (double x)
{ {
double z,p,q,r,w,s,c,df; double z, p, q, r, w, s, c, df;
int hx,ix; int hx, ix;
hx = __HI(x);
ix = hx&0x7fffffff; hx = __HI (x);
if(ix>=0x3ff00000) { /* |x| >= 1 */ ix = hx & 0x7fffffff;
if(((ix-0x3ff00000)|__LO(x))==0) { /* |x|==1 */ if (ix >= 0x3ff00000) /* |x| >= 1 */
if(hx>0) return 0.0; /* acos(1) = 0 */ {
else return pi+2.0*pio2_lo; /* acos(-1)= pi */ if (((ix - 0x3ff00000) | __LO (x)) == 0) /* |x| == 1 */
{
if (hx > 0) /* acos(1) = 0 */
{
return 0.0;
} }
return (x-x)/(x-x); /* acos(|x|>1) is NaN */ else /* acos(-1) = pi */
{
return pi + 2.0 * pio2_lo;
} }
if(ix<0x3fe00000) { /* |x| < 0.5 */ }
if(ix<=0x3c600000) return pio2_hi+pio2_lo;/*if|x|<2**-57*/ return (x - x) / (x - x); /* acos(|x|>1) is NaN */
z = x*x; }
p = z*(pS0+z*(pS1+z*(pS2+z*(pS3+z*(pS4+z*pS5))))); if (ix < 0x3fe00000) /* |x| < 0.5 */
q = one+z*(qS1+z*(qS2+z*(qS3+z*qS4))); {
r = p/q; if (ix <= 0x3c600000) /* if |x| < 2**-57 */
return pio2_hi - (x - (pio2_lo-x*r)); {
} else if (hx<0) { /* x < -0.5 */ return pio2_hi + pio2_lo;
z = (one+x)*0.5; }
p = z*(pS0+z*(pS1+z*(pS2+z*(pS3+z*(pS4+z*pS5))))); z = x * x;
q = one+z*(qS1+z*(qS2+z*(qS3+z*qS4))); p = z * (pS0 + z * (pS1 + z * (pS2 + z * (pS3 + z * (pS4 + z * pS5)))));
s = sqrt(z); q = one + z * (qS1 + z * (qS2 + z * (qS3 + z * qS4)));
r = p/q; r = p / q;
w = r*s-pio2_lo; return pio2_hi - (x - (pio2_lo - x * r));
return pi - 2.0*(s+w); }
} else { /* x > 0.5 */ else if (hx < 0) /* x < -0.5 */
z = (one-x)*0.5; {
s = sqrt(z); z = (one + x) * 0.5;
p = z * (pS0 + z * (pS1 + z * (pS2 + z * (pS3 + z * (pS4 + z * pS5)))));
q = one + z * (qS1 + z * (qS2 + z * (qS3 + z * qS4)));
s = sqrt (z);
r = p / q;
w = r * s - pio2_lo;
return pi - 2.0 * (s + w);
}
else /* x > 0.5 */
{
z = (one - x) * 0.5;
s = sqrt (z);
df = s; df = s;
__LO(df) = 0; __LO (df) = 0;
c = (z-df*df)/(s+df); c = (z - df * df) / (s + df);
p = z*(pS0+z*(pS1+z*(pS2+z*(pS3+z*(pS4+z*pS5))))); p = z * (pS0 + z * (pS1 + z * (pS2 + z * (pS3 + z * (pS4 + z * pS5)))));
q = one+z*(qS1+z*(qS2+z*(qS3+z*qS4))); q = one + z * (qS1 + z * (qS2 + z * (qS3 + z * qS4)));
r = p/q; r = p / q;
w = r*s+c; w = r * s + c;
return 2.0*(df+w); return 2.0 * (df + w);
} }
} } /* acos */
+63 -41
View File
@@ -12,7 +12,8 @@
*/ */
/* asin(x) /* asin(x)
* Method : *
* Method:
* Since asin(x) = x + x^3/6 + x^5*3/40 + x^7*15/336 + ... * Since asin(x) = x + x^3/6 + x^5*3/40 + x^7*15/336 + ...
* we approximate asin(x) on [0,0.5] by * we approximate asin(x) on [0,0.5] by
* asin(x) = x + x*x^2*R(x^2) * asin(x) = x + x*x^2*R(x^2)
@@ -38,10 +39,8 @@
* Special cases: * Special cases:
* if x is NaN, return x itself; * if x is NaN, return x itself;
* if |x|>1, return NaN with invalid signal. * if |x|>1, return NaN with invalid signal.
*
*/ */
#include "fdlibm.h" #include "fdlibm.h"
#define one 1.00000000000000000000e+00 /* 0x3FF00000, 0x00000000 */ #define one 1.00000000000000000000e+00 /* 0x3FF00000, 0x00000000 */
@@ -49,7 +48,7 @@
#define pio2_hi 1.57079632679489655800e+00 /* 0x3FF921FB, 0x54442D18 */ #define pio2_hi 1.57079632679489655800e+00 /* 0x3FF921FB, 0x54442D18 */
#define pio2_lo 6.12323399573676603587e-17 /* 0x3C91A626, 0x33145C07 */ #define pio2_lo 6.12323399573676603587e-17 /* 0x3C91A626, 0x33145C07 */
#define pio4_hi 7.85398163397448278999e-01 /* 0x3FE921FB, 0x54442D18 */ #define pio4_hi 7.85398163397448278999e-01 /* 0x3FE921FB, 0x54442D18 */
/* coefficient for R(x^2) */ /* coefficient for R(x^2) */
#define pS0 1.66666666666666657415e-01 /* 0x3FC55555, 0x55555555 */ #define pS0 1.66666666666666657415e-01 /* 0x3FC55555, 0x55555555 */
#define pS1 -3.25565818622400915405e-01 /* 0xBFD4D612, 0x03EB6F7D */ #define pS1 -3.25565818622400915405e-01 /* 0xBFD4D612, 0x03EB6F7D */
#define pS2 2.01212532134862925881e-01 /* 0x3FC9C155, 0x0E884455 */ #define pS2 2.01212532134862925881e-01 /* 0x3FC9C155, 0x0E884455 */
@@ -61,44 +60,67 @@
#define qS3 -6.88283971605453293030e-01 /* 0xBFE6066C, 0x1B8D0159 */ #define qS3 -6.88283971605453293030e-01 /* 0xBFE6066C, 0x1B8D0159 */
#define qS4 7.70381505559019352791e-02 /* 0x3FB3B8C5, 0xB12E9282 */ #define qS4 7.70381505559019352791e-02 /* 0x3FB3B8C5, 0xB12E9282 */
double asin(double x) double
asin (double x)
{ {
double t = 0,w,p,q,c,r,s; double t = 0, w, p, q, c, r, s;
int hx,ix; int hx, ix;
hx = __HI(x);
ix = hx&0x7fffffff; hx = __HI (x);
if(ix>= 0x3ff00000) { /* |x|>= 1 */ ix = hx & 0x7fffffff;
if(((ix-0x3ff00000)|__LO(x))==0) if (ix >= 0x3ff00000) /* |x| >= 1 */
/* asin(1)=+-pi/2 with inexact */ {
return x*pio2_hi+x*pio2_lo; if (((ix - 0x3ff00000) | __LO (x)) == 0) /* asin(1) = +-pi/2 with inexact */
return (x-x)/(x-x); /* asin(|x|>1) is NaN */ {
} else if (ix<0x3fe00000) { /* |x|<0.5 */ return x * pio2_hi + x * pio2_lo;
if(ix<0x3e400000) { /* if |x| < 2**-27 */
if(huge+x>one) return x;/* return x with inexact if x!=0*/
} else
t = x*x;
p = t*(pS0+t*(pS1+t*(pS2+t*(pS3+t*(pS4+t*pS5)))));
q = one+t*(qS1+t*(qS2+t*(qS3+t*qS4)));
w = p/q;
return x+x*w;
} }
/* 1> |x|>= 0.5 */ return (x - x) / (x - x); /* asin(|x|>1) is NaN */
w = one-fabs(x); }
t = w*0.5; else if (ix < 0x3fe00000) /* |x| < 0.5 */
p = t*(pS0+t*(pS1+t*(pS2+t*(pS3+t*(pS4+t*pS5))))); {
q = one+t*(qS1+t*(qS2+t*(qS3+t*qS4))); if (ix < 0x3e400000) /* if |x| < 2**-27 */
s = sqrt(t); {
if(ix>=0x3FEF3333) { /* if |x| > 0.975 */ if (huge + x > one) /* return x with inexact if x != 0 */
w = p/q; {
t = pio2_hi-(2.0*(s+s*w)-pio2_lo); return x;
} else { }
}
else
{
t = x * x;
}
p = t * (pS0 + t * (pS1 + t * (pS2 + t * (pS3 + t * (pS4 + t * pS5)))));
q = one + t * (qS1 + t * (qS2 + t * (qS3 + t * qS4)));
w = p / q;
return x + x * w;
}
/* 1 > |x| >= 0.5 */
w = one - fabs (x);
t = w * 0.5;
p = t * (pS0 + t * (pS1 + t * (pS2 + t * (pS3 + t * (pS4 + t * pS5)))));
q = one + t * (qS1 + t * (qS2 + t * (qS3 + t * qS4)));
s = sqrt (t);
if (ix >= 0x3FEF3333) /* if |x| > 0.975 */
{
w = p / q;
t = pio2_hi - (2.0 * (s + s * w) - pio2_lo);
}
else
{
w = s; w = s;
__LO(w) = 0; __LO (w) = 0;
c = (t-w*w)/(s+w); c = (t - w * w) / (s + w);
r = p/q; r = p / q;
p = 2.0*s*r-(pio2_lo-2.0*c); p = 2.0 * s * r - (pio2_lo - 2.0 * c);
q = pio4_hi-2.0*w; q = pio4_hi - 2.0 * w;
t = pio4_hi-(p-q); t = pio4_hi - (p - q);
} }
if(hx>0) return t; else return -t; if (hx > 0)
} {
return t;
}
else
{
return -t;
}
} /* asin */
+78 -41
View File
@@ -9,11 +9,11 @@
* software is freely granted, provided that this notice * software is freely granted, provided that this notice
* is preserved. * is preserved.
* ==================================================== * ====================================================
*
*/ */
/* atan(x) /* atan(x)
* Method *
* Method:
* 1. Reduce x to positive by atan(x) = -atan(-x). * 1. Reduce x to positive by atan(x) = -atan(-x).
* 2. According to the integer k=4t+0.25 chopped, t=x, the argument * 2. According to the integer k=4t+0.25 chopped, t=x, the argument
* is further reduced to one of the following intervals and the * is further reduced to one of the following intervals and the
@@ -34,14 +34,16 @@
#include "fdlibm.h" #include "fdlibm.h"
static const double atanhi[] = { static const double atanhi[] =
{
4.63647609000806093515e-01, /* atan(0.5)hi 0x3FDDAC67, 0x0561BB4F */ 4.63647609000806093515e-01, /* atan(0.5)hi 0x3FDDAC67, 0x0561BB4F */
7.85398163397448278999e-01, /* atan(1.0)hi 0x3FE921FB, 0x54442D18 */ 7.85398163397448278999e-01, /* atan(1.0)hi 0x3FE921FB, 0x54442D18 */
9.82793723247329054082e-01, /* atan(1.5)hi 0x3FEF730B, 0xD281F69B */ 9.82793723247329054082e-01, /* atan(1.5)hi 0x3FEF730B, 0xD281F69B */
1.57079632679489655800e+00, /* atan(inf)hi 0x3FF921FB, 0x54442D18 */ 1.57079632679489655800e+00, /* atan(inf)hi 0x3FF921FB, 0x54442D18 */
}; };
static const double atanlo[] = { static const double atanlo[] =
{
2.26987774529616870924e-17, /* atan(0.5)lo 0x3C7A2B7F, 0x222F65E2 */ 2.26987774529616870924e-17, /* atan(0.5)lo 0x3C7A2B7F, 0x222F65E2 */
3.06161699786838301793e-17, /* atan(1.0)lo 0x3C81A626, 0x33145C07 */ 3.06161699786838301793e-17, /* atan(1.0)lo 0x3C81A626, 0x33145C07 */
1.39033110312309984516e-17, /* atan(1.5)lo 0x3C700788, 0x7AF0CBBD */ 1.39033110312309984516e-17, /* atan(1.5)lo 0x3C700788, 0x7AF0CBBD */
@@ -63,48 +65,83 @@ static const double atanlo[] = {
#define one 1.0 #define one 1.0
#define huge 1.0e300 #define huge 1.0e300
double atan(double x) double
atan (double x)
{ {
double w,s1,s2,z; double w, s1, s2, z;
int ix,hx,id; int ix, hx, id;
hx = __HI(x); hx = __HI (x);
ix = hx&0x7fffffff; ix = hx & 0x7fffffff;
if(ix>=0x44100000) { /* if |x| >= 2^66 */ if (ix >= 0x44100000) /* if |x| >= 2^66 */
if(ix>0x7ff00000|| {
(ix==0x7ff00000&&(__LO(x)!=0))) if (ix > 0x7ff00000 || (ix == 0x7ff00000 && (__LO (x) != 0)))
return x+x; /* NaN */ {
if(hx>0) return atanhi[3]+atanlo[3]; return x + x; /* NaN */
else return -atanhi[3]-atanlo[3]; }
} if (ix < 0x3fdc0000) { /* |x| < 0.4375 */ if (hx > 0)
if (ix < 0x3e200000) { /* |x| < 2^-29 */ {
if(huge+x>one) return x; /* raise inexact */ return atanhi[3] + atanlo[3];
}
else
{
return -atanhi[3] - atanlo[3];
}
}
if (ix < 0x3fdc0000) /* |x| < 0.4375 */
{
if (ix < 0x3e200000) /* |x| < 2^-29 */
{
if (huge + x > one) /* raise inexact */
{
return x;
}
} }
id = -1; id = -1;
} else {
x = fabs(x);
if (ix < 0x3ff30000) { /* |x| < 1.1875 */
if (ix < 0x3fe60000) { /* 7/16 <=|x|<11/16 */
id = 0; x = (2.0*x-one)/(2.0+x);
} else { /* 11/16<=|x|< 19/16 */
id = 1; x = (x-one)/(x+one);
} }
} else { else
if (ix < 0x40038000) { /* |x| < 2.4375 */ {
id = 2; x = (x-1.5)/(one+1.5*x); x = fabs (x);
} else { /* 2.4375 <= |x| < 2^66 */ if (ix < 0x3ff30000) /* |x| < 1.1875 */
id = 3; x = -1.0/x; {
if (ix < 0x3fe60000) /* 7/16 <= |x| < 11/16 */
{
id = 0;
x = (2.0 * x - one) / (2.0 + x);
}
else /* 11/16 <= |x| < 19/16 */
{
id = 1;
x = (x - one) / (x + one);
}
}
else
{
if (ix < 0x40038000) /* |x| < 2.4375 */
{
id = 2;
x = (x - 1.5) / (one + 1.5 * x);
}
else /* 2.4375 <= |x| < 2^66 */
{
id = 3;
x = -1.0 / x;
}
}
} }
}}
/* end of argument reduction */ /* end of argument reduction */
z = x*x; z = x * x;
w = z*z; w = z * z;
/* break sum from i=0 to 10 aT[i]z**(i+1) into odd and even poly */ /* break sum from i=0 to 10 aT[i] z**(i+1) into odd and even poly */
s1 = z*(aT0+w*(aT2+w*(aT4+w*(aT6+w*(aT8+w*aT10))))); s1 = z * (aT0 + w * (aT2 + w * (aT4 + w * (aT6 + w * (aT8 + w * aT10)))));
s2 = w*(aT1+w*(aT3+w*(aT5+w*(aT7+w*aT9)))); s2 = w * (aT1 + w * (aT3 + w * (aT5 + w * (aT7 + w * aT9))));
if (id<0) return x - x*(s1+s2); if (id < 0)
else { {
z = atanhi[id] - ((x*(s1+s2) - atanlo[id]) - x); return x - x * (s1 + s2);
return (hx<0)? -z:z;
} }
} else
{
z = atanhi[id] - ((x * (s1 + s2) - atanlo[id]) - x);
return (hx < 0) ? -z : z;
}
} /* atan */
+124 -49
View File
@@ -9,18 +9,17 @@
* software is freely granted, provided that this notice * software is freely granted, provided that this notice
* is preserved. * is preserved.
* ==================================================== * ====================================================
*
*/ */
/* atan2(y,x) /* atan2(y,x)
* Method : *
* Method:
* 1. Reduce y to positive by atan2(y,x)=-atan2(-y,x). * 1. Reduce y to positive by atan2(y,x)=-atan2(-y,x).
* 2. Reduce x to positive by (if x and y are unexceptional): * 2. Reduce x to positive by (if x and y are unexceptional):
* ARG (x+iy) = arctan(y/x) ... if x > 0, * ARG (x+iy) = arctan(y/x) ... if x > 0,
* ARG (x+iy) = pi - arctan[y/(-x)] ... if x < 0, * ARG (x+iy) = pi - arctan[y/(-x)] ... if x < 0,
* *
* Special cases: * Special cases:
*
* ATAN2((anything), NaN ) is NaN; * ATAN2((anything), NaN ) is NaN;
* ATAN2(NAN , (anything) ) is NaN; * ATAN2(NAN , (anything) ) is NaN;
* ATAN2(+-0, +(anything but NaN)) is +-0 ; * ATAN2(+-0, +(anything but NaN)) is +-0 ;
@@ -48,66 +47,142 @@
#define pi 3.1415926535897931160E+00 /* 0x400921FB, 0x54442D18 */ #define pi 3.1415926535897931160E+00 /* 0x400921FB, 0x54442D18 */
#define pi_lo 1.2246467991473531772E-16 /* 0x3CA1A626, 0x33145C07 */ #define pi_lo 1.2246467991473531772E-16 /* 0x3CA1A626, 0x33145C07 */
double atan2(double y, double x) double
atan2 (double y, double x)
{ {
double z; double z;
int k,m,hx,hy,ix,iy; int k, m, hx, hy, ix, iy;
unsigned lx,ly; unsigned lx, ly;
hx = __HI(x); ix = hx&0x7fffffff; hx = __HI (x);
lx = __LO(x); ix = hx & 0x7fffffff;
hy = __HI(y); iy = hy&0x7fffffff; lx = __LO (x);
ly = __LO(y); hy = __HI (y);
if(((ix|((lx|-lx)>>31))>0x7ff00000)|| iy = hy & 0x7fffffff;
((iy|((ly|-ly)>>31))>0x7ff00000)) /* x or y is NaN */ ly = __LO (y);
return x+y; if (((ix | ((lx | -lx) >> 31)) > 0x7ff00000) || ((iy | ((ly | -ly) >> 31)) > 0x7ff00000)) /* x or y is NaN */
if((hx-0x3ff00000|lx)==0) return atan(y); /* x=1.0 */ {
m = ((hy>>31)&1)|((hx>>30)&2); /* 2*sign(x)+sign(y) */ return x + y;
}
if ((hx - 0x3ff00000 | lx) == 0) /* x = 1.0 */
{
return atan (y);
}
m = ((hy >> 31) & 1) | ((hx >> 30) & 2); /* 2 * sign(x) + sign(y) */
/* when y = 0 */ /* when y = 0 */
if((iy|ly)==0) { if ((iy | ly) == 0)
switch(m) { {
switch (m)
{
case 0: case 0:
case 1: return y; /* atan(+-0,+anything)=+-0 */ case 1:
case 2: return pi+tiny;/* atan(+0,-anything) = pi */ {
case 3: return -pi-tiny;/* atan(-0,-anything) =-pi */ return y; /* atan(+-0,+anything) = +-0 */
}
case 2:
{
return pi + tiny; /* atan(+0,-anything) = pi */
}
case 3:
{
return -pi - tiny; /* atan(-0,-anything) = -pi */
}
} }
} }
/* when x = 0 */ /* when x = 0 */
if((ix|lx)==0) return (hy<0)? -pi_o_2-tiny: pi_o_2+tiny; if ((ix | lx) == 0)
{
return (hy < 0) ? -pi_o_2 - tiny : pi_o_2 + tiny;
}
/* when x is INF */ /* when x is INF */
if(ix==0x7ff00000) { if (ix == 0x7ff00000)
if(iy==0x7ff00000) { {
switch(m) { if (iy == 0x7ff00000)
case 0: return pi_o_4+tiny;/* atan(+INF,+INF) */ {
case 1: return -pi_o_4-tiny;/* atan(-INF,+INF) */ switch (m)
case 2: return 3.0*pi_o_4+tiny;/*atan(+INF,-INF)*/ {
case 3: return -3.0*pi_o_4-tiny;/*atan(-INF,-INF)*/ case 0: /* atan(+INF,+INF) */
{
return pi_o_4 + tiny;
}
case 1: /* atan(-INF,+INF) */
{
return -pi_o_4 - tiny;
}
case 2: /* atan(+INF,-INF) */
{
return 3.0 * pi_o_4 + tiny;
}
case 3: /* atan(-INF,-INF) */
{
return -3.0 * pi_o_4 - tiny;
}
}
}
else
{
switch (m)
{
case 0: /* atan(+...,+INF) */
{
return zero;
}
case 1: /* atan(-...,+INF) */
{
return -zero;
}
case 2: /* atan(+...,-INF) */
{
return pi + tiny;
}
case 3: /* atan(-...,-INF) */
{
return -pi - tiny;
} }
} else {
switch(m) {
case 0: return zero ; /* atan(+...,+INF) */
case 1: return -zero ; /* atan(-...,+INF) */
case 2: return pi+tiny ; /* atan(+...,-INF) */
case 3: return -pi-tiny ; /* atan(-...,-INF) */
} }
} }
} }
/* when y is INF */ /* when y is INF */
if(iy==0x7ff00000) return (hy<0)? -pi_o_2-tiny: pi_o_2+tiny; if (iy == 0x7ff00000)
{
/* compute y/x */ return (hy < 0) ? -pi_o_2 - tiny : pi_o_2 + tiny;
k = (iy-ix)>>20;
if(k > 60) z=pi_o_2+0.5*pi_lo; /* |y/x| > 2**60 */
else if(hx<0&&k<-60) z=0.0; /* |y|/x < -2**60 */
else z=atan(fabs(y/x)); /* safe to do y/x */
switch (m) {
case 0: return z ; /* atan(+,+) */
case 1: __HI(z) ^= 0x80000000;
return z ; /* atan(-,+) */
case 2: return pi-(z-pi_lo);/* atan(+,-) */
default: /* case 3 */
return (z-pi_lo)-pi;/* atan(-,-) */
} }
}
/* compute y / x */
k = (iy - ix) >> 20;
if (k > 60) /* |y / x| > 2**60 */
{
z = pi_o_2 + 0.5 * pi_lo;
}
else if (hx < 0 && k < -60) /* |y| / x < -2**60 */
{
z = 0.0;
}
else /* safe to do y / x */
{
z = atan (fabs (y / x));
}
switch (m)
{
case 0: /* atan(+,+) */
{
return z;
}
case 1: /* atan(-,+) */
{
__HI (z) ^= 0x80000000;
return z;
}
case 2: /* atan(+,-) */
{
return pi - (z - pi_lo);
}
/* case 3: */
default: /* atan(-,-) */
{
return (z - pi_lo) - pi;
}
}
} /* atan2 */
+80 -34
View File
@@ -11,11 +11,12 @@
* ==================================================== * ====================================================
*/ */
/* /* ceil(x)
* ceil(x)
* Return x rounded toward -inf to integral value * Return x rounded toward -inf to integral value
*
* Method: * Method:
* Bit twiddling. * Bit twiddling.
*
* Exception: * Exception:
* Inexact flag raised if x not equal to ceil(x). * Inexact flag raised if x not equal to ceil(x).
*/ */
@@ -24,46 +25,91 @@
#define huge 1.0e300 #define huge 1.0e300
double ceil(double x) double
ceil (double x)
{ {
int i0,i1,j0; int i0, i1, j0;
unsigned i,j; unsigned i, j;
i0 = __HI(x);
i1 = __LO(x); i0 = __HI (x);
j0 = ((i0>>20)&0x7ff)-0x3ff; i1 = __LO (x);
if(j0<20) { j0 = ((i0 >> 20) & 0x7ff) - 0x3ff;
if(j0<0) { /* raise inexact if x != 0 */ if (j0 < 20)
if(huge+x>0.0) {/* return 0*sign(x) if |x|<1 */ {
if(i0<0) {i0=0x80000000;i1=0;} if (j0 < 0) /* raise inexact if x != 0 */
else if((i0|i1)!=0) { i0=0x3ff00000;i1=0;} {
if (huge + x > 0.0) /* return 0 * sign(x) if |x| < 1 */
{
if (i0 < 0)
{
i0 = 0x80000000;
i1 = 0;
} }
} else { else if ((i0 | i1) != 0)
i = (0x000fffff)>>j0; {
if(((i0&i)|i1)==0) return x; /* x is integral */ i0 = 0x3ff00000;
if(huge+x>0.0) { /* raise inexact flag */ i1 = 0;
if(i0>0) i0 += (0x00100000)>>j0;
i0 &= (~i); i1=0;
} }
} }
} else if (j0>51) { }
if(j0==0x400) return x+x; /* inf or NaN */ else
else return x; /* x is integral */ {
} else { i = (0x000fffff) >> j0;
i = ((unsigned)(0xffffffff))>>(j0-20); if (((i0 & i) | i1) == 0) /* x is integral */
if((i1&i)==0) return x; /* x is integral */ {
if(huge+x>0.0) { /* raise inexact flag */ return x;
if(i0>0) { }
if(j0==20) i0+=1; if (huge + x > 0.0) /* raise inexact flag */
else { {
j = i1 + (1<<(52-j0)); if (i0 > 0)
if(j<i1) i0+=1; /* got a carry */ {
i0 += (0x00100000) >> j0;
}
i0 &= (~i);
i1 = 0;
}
}
}
else if (j0 > 51)
{
if (j0 == 0x400) /* inf or NaN */
{
return x + x;
}
else /* x is integral */
{
return x;
}
}
else
{
i = ((unsigned) (0xffffffff)) >> (j0 - 20);
if ((i1 & i) == 0) /* x is integral */
{
return x;
}
if (huge + x > 0.0) /* raise inexact flag */
{
if (i0 > 0)
{
if (j0 == 20)
{
i0 += 1;
}
else
{
j = i1 + (1 << (52 - j0));
if (j < i1) /* got a carry */
{
i0 += 1;
}
i1 = j; i1 = j;
} }
} }
i1 &= (~i); i1 &= (~i);
} }
} }
__HI(x) = i0; __HI (x) = i0;
__LO(x) = i1; __LO (x) = i1;
return x; return x;
} } /* ceil */
+5 -6
View File
@@ -11,16 +11,15 @@
* ==================================================== * ====================================================
*/ */
/* /* copysign(x,y) returns a value with the magnitude of x and
* copysign(double x, double y)
* copysign(x,y) returns a value with the magnitude of x and
* with the sign bit of y. * with the sign bit of y.
*/ */
#include "fdlibm.h" #include "fdlibm.h"
double copysign(double x, double y) double
copysign (double x, double y)
{ {
__HI(x) = (__HI(x)&0x7fffffff)|(__HI(y)&0x80000000); __HI (x) = (__HI (x) & 0x7fffffff) | (__HI (y) & 0x80000000);
return x; return x;
} } /* copysign */
+86 -41
View File
@@ -13,7 +13,7 @@
/* exp(x) /* exp(x)
* Returns the exponential of x. * Returns the exponential of x.
* *
* Method * Method:
* 1. Argument reduction: * 1. Argument reduction:
* Reduce x to an r so that |r| <= 0.5*ln2 ~ 0.34658. * Reduce x to an r so that |r| <= 0.5*ln2 ~ 0.34658.
* Given x, find r and integer k such that * Given x, find r and integer k such that
@@ -61,7 +61,7 @@
* according to an error analysis, the error is always less than * according to an error analysis, the error is always less than
* 1 ulp (unit in the last place). * 1 ulp (unit in the last place).
* *
* Misc. info. * Misc. info:
* For IEEE double * For IEEE double
* if x > 7.09782712893383973096e+02 then exp(x) overflow * if x > 7.09782712893383973096e+02 then exp(x) overflow
* if x < -7.45133219101941108420e+02 then exp(x) underflow * if x < -7.45133219101941108420e+02 then exp(x) underflow
@@ -75,12 +75,21 @@
#include "fdlibm.h" #include "fdlibm.h"
static const double static const double halF[2] =
halF[2] = {0.5,-0.5,}, {
ln2HI[2] = { 6.93147180369123816490e-01, /* 0x3fe62e42, 0xfee00000 */ 0.5,
-6.93147180369123816490e-01,}, /* 0xbfe62e42, 0xfee00000 */ -0.5,
ln2LO[2] = { 1.90821492927058770002e-10, /* 0x3dea39ef, 0x35793c76 */ };
-1.90821492927058770002e-10,}; /* 0xbdea39ef, 0x35793c76 */ static const double ln2HI[2] =
{
6.93147180369123816490e-01, /* 0x3fe62e42, 0xfee00000 */
-6.93147180369123816490e-01, /* 0xbfe62e42, 0xfee00000 */
};
static const double ln2LO[2] =
{
1.90821492927058770002e-10, /* 0x3dea39ef, 0x35793c76 */
-1.90821492927058770002e-10, /* 0xbdea39ef, 0x35793c76 */
};
#define one 1.0 #define one 1.0
#define huge 1.0e+300 #define huge 1.0e+300
@@ -94,54 +103,90 @@ ln2LO[2] = { 1.90821492927058770002e-10, /* 0x3dea39ef, 0x35793c76 */
#define P4 -1.65339022054652515390e-06 /* 0xBEBBBD41, 0xC5D26BF1 */ #define P4 -1.65339022054652515390e-06 /* 0xBEBBBD41, 0xC5D26BF1 */
#define P5 4.13813679705723846039e-08 /* 0x3E663769, 0x72BEA4D0 */ #define P5 4.13813679705723846039e-08 /* 0x3E663769, 0x72BEA4D0 */
double exp(double x) /* default IEEE double exp */ double
exp (double x) /* default IEEE double exp */
{ {
double y,hi,lo,c,t; double y, hi, lo, c, t;
int k = 0,xsb; int k = 0, xsb;
unsigned hx; unsigned hx;
hx = __HI(x); /* high word of x */ hx = __HI (x); /* high word of x */
xsb = (hx>>31)&1; /* sign bit of x */ xsb = (hx >> 31) & 1; /* sign bit of x */
hx &= 0x7fffffff; /* high word of |x| */ hx &= 0x7fffffff; /* high word of |x| */
/* filter out non-finite argument */ /* filter out non-finite argument */
if(hx >= 0x40862E42) { /* if |x|>=709.78... */ if (hx >= 0x40862E42) /* if |x| >= 709.78... */
if(hx>=0x7ff00000) { {
if(((hx&0xfffff)|__LO(x))!=0) if (hx >= 0x7ff00000)
return x+x; /* NaN */ {
else return (xsb==0)? x:0.0; /* exp(+-inf)={inf,0} */ if (((hx & 0xfffff) | __LO (x)) != 0) /* NaN */
{
return x + x;
}
else /* exp(+-inf) = {inf,0} */
{
return (xsb == 0) ? x : 0.0;
}
}
if (x > o_threshold) /* overflow */
{
return huge * huge;
}
if (x < u_threshold) /* underflow */
{
return twom1000 * twom1000;
} }
if(x > o_threshold) return huge*huge; /* overflow */
if(x < u_threshold) return twom1000*twom1000; /* underflow */
} }
/* argument reduction */ /* argument reduction */
if(hx > 0x3fd62e42) { /* if |x| > 0.5 ln2 */ if (hx > 0x3fd62e42) /* if |x| > 0.5 ln2 */
if(hx < 0x3FF0A2B2) { /* and |x| < 1.5 ln2 */ {
hi = x-ln2HI[xsb]; lo=ln2LO[xsb]; k = 1-xsb-xsb; if (hx < 0x3FF0A2B2) /* and |x| < 1.5 ln2 */
} else { {
k = (int)(invln2*x+halF[xsb]); hi = x - ln2HI[xsb];
lo = ln2LO[xsb];
k = 1 - xsb - xsb;
}
else
{
k = (int) (invln2 * x + halF[xsb]);
t = k; t = k;
hi = x - t*ln2HI[0]; /* t*ln2HI is exact here */ hi = x - t * ln2HI[0]; /* t * ln2HI is exact here */
lo = t*ln2LO[0]; lo = t * ln2LO[0];
} }
x = hi - lo; x = hi - lo;
} }
else if(hx < 0x3e300000) { /* when |x|<2**-28 */ else if (hx < 0x3e300000) /* when |x| < 2**-28 */
if(huge+x>one) return one+x;/* trigger inexact */ {
if (huge + x > one) /* trigger inexact */
{
return one + x;
}
}
else
{
k = 0;
} }
else k = 0;
/* x is now in primary range */ /* x is now in primary range */
t = x*x; t = x * x;
c = x - t*(P1+t*(P2+t*(P3+t*(P4+t*P5)))); c = x - t * (P1 + t * (P2 + t * (P3 + t * (P4 + t * P5))));
if(k==0) return one-((x*c)/(c-2.0)-x); if (k == 0)
else y = one-((lo-(x*c)/(2.0-c))-hi); {
if(k >= -1021) { return one - ((x * c) / (c - 2.0) - x);
__HI(y) += (k<<20); /* add k to y's exponent */
return y;
} else {
__HI(y) += ((k+1000)<<20);/* add k to y's exponent */
return y*twom1000;
} }
} else
{
y = one - ((lo - (x * c) / (2.0 - c)) - hi);
}
if (k >= -1021)
{
__HI (y) += (k << 20); /* add k to y's exponent */
return y;
}
else
{
__HI (y) += ((k + 1000) << 20); /* add k to y's exponent */
return y * twom1000;
}
} /* exp */
+5 -5
View File
@@ -11,14 +11,14 @@
* ==================================================== * ====================================================
*/ */
/* /* fabs(x) returns the absolute value of x.
* fabs(x) returns the absolute value of x.
*/ */
#include "fdlibm.h" #include "fdlibm.h"
double fabs(double x) double
fabs (double x)
{ {
__HI(x) &= 0x7fffffff; __HI (x) &= 0x7fffffff;
return x; return x;
} } /* fabs */
+7 -6
View File
@@ -11,16 +11,17 @@
* ==================================================== * ====================================================
*/ */
/* /* finite(x) returns 1 is x is finite, else 0;
* finite(x) returns 1 is x is finite, else 0;
* no branching! * no branching!
*/ */
#include "fdlibm.h" #include "fdlibm.h"
int finite(double x) int
finite (double x)
{ {
int hx; int hx;
hx = __HI(x);
return (unsigned)((hx&0x7fffffff)-0x7ff00000)>>31; hx = __HI (x);
} return (unsigned) ((hx & 0x7fffffff) - 0x7ff00000) >> 31;
} /* finite */
+80 -36
View File
@@ -11,11 +11,12 @@
* ==================================================== * ====================================================
*/ */
/* /* floor(x)
* floor(x)
* Return x rounded toward -inf to integral value * Return x rounded toward -inf to integral value
*
* Method: * Method:
* Bit twiddling. * Bit twiddling.
*
* Exception: * Exception:
* Inexact flag raised if x not equal to floor(x). * Inexact flag raised if x not equal to floor(x).
*/ */
@@ -24,47 +25,90 @@
#define huge 1.0e300 #define huge 1.0e300
double floor(double x) double
floor (double x)
{ {
int i0,i1,j0; int i0, i1, j0;
unsigned i,j; unsigned i, j;
i0 = __HI(x);
i1 = __LO(x); i0 = __HI (x);
j0 = ((i0>>20)&0x7ff)-0x3ff; i1 = __LO (x);
if(j0<20) { j0 = ((i0 >> 20) & 0x7ff) - 0x3ff;
if(j0<0) { /* raise inexact if x != 0 */ if (j0 < 20)
if(huge+x>0.0) {/* return 0*sign(x) if |x|<1 */ {
if(i0>=0) {i0=i1=0;} if (j0 < 0) /* raise inexact if x != 0 */
else if(((i0&0x7fffffff)|i1)!=0) {
{ i0=0xbff00000;i1=0;} if (huge + x > 0.0) /* return 0 * sign(x) if |x| < 1 */
{
if (i0 >= 0)
{
i0 = i1 = 0;
} }
} else { else if (((i0 & 0x7fffffff) | i1) != 0)
i = (0x000fffff)>>j0; {
if(((i0&i)|i1)==0) return x; /* x is integral */ i0 = 0xbff00000;
if(huge+x>0.0) { /* raise inexact flag */ i1 = 0;
if(i0<0) i0 += (0x00100000)>>j0;
i0 &= (~i); i1=0;
} }
} }
} else if (j0>51) { }
if(j0==0x400) return x+x; /* inf or NaN */ else
else return x; /* x is integral */ {
} else { i = (0x000fffff) >> j0;
i = ((unsigned)(0xffffffff))>>(j0-20); if (((i0 & i) | i1) == 0) /* x is integral */
if((i1&i)==0) return x; /* x is integral */ {
if(huge+x>0.0) { /* raise inexact flag */ return x;
if(i0<0) { }
if(j0==20) i0+=1; if (huge + x > 0.0) /* raise inexact flag */
else { {
j = i1+(1<<(52-j0)); if (i0 < 0)
if(j<i1) i0 +=1 ; /* got a carry */ {
i1=j; i0 += (0x00100000) >> j0;
}
i0 &= (~i);
i1 = 0;
}
}
}
else if (j0 > 51)
{
if (j0 == 0x400) /* inf or NaN */
{
return x + x;
}
else /* x is integral */
{
return x;
}
}
else
{
i = ((unsigned) (0xffffffff)) >> (j0 - 20);
if ((i1 & i) == 0) /* x is integral */
{
return x;
}
if (huge + x > 0.0) /* raise inexact flag */
{
if (i0 < 0)
{
if (j0 == 20)
{
i0 += 1;
}
else
{
j = i1 + (1 << (52 - j0));
if (j < i1) /* got a carry */
{
i0 += 1;
}
i1 = j;
} }
} }
i1 &= (~i); i1 &= (~i);
} }
} }
__HI(x) = i0; __HI (x) = i0;
__LO(x) = i1; __LO (x) = i1;
return x; return x;
} } /* floor */
+162 -77
View File
@@ -11,124 +11,209 @@
* ==================================================== * ====================================================
*/ */
/* /* fmod(x,y)
* fmod(x,y)
* Return x mod y in exact arithmetic * Return x mod y in exact arithmetic
*
* Method: shift and subtract * Method: shift and subtract
*/ */
#include "fdlibm.h" #include "fdlibm.h"
static const double static const double Zero[] = { 0.0, -0.0, };
Zero[] = {0.0, -0.0,};
#define one 1.0 #define one 1.0
double fmod(double x, double y) double
fmod (double x, double y)
{ {
int n,hx,hy,hz,ix,iy,sx,i; int n, hx, hy, hz, ix, iy, sx, i;
unsigned lx,ly,lz; unsigned lx, ly, lz;
hx = __HI(x); /* high word of x */ hx = __HI (x); /* high word of x */
lx = __LO(x); /* low word of x */ lx = __LO (x); /* low word of x */
hy = __HI(y); /* high word of y */ hy = __HI (y); /* high word of y */
ly = __LO(y); /* low word of y */ ly = __LO (y); /* low word of y */
sx = hx&0x80000000; /* sign of x */ sx = hx & 0x80000000; /* sign of x */
hx ^=sx; /* |x| */ hx ^= sx; /* |x| */
hy &= 0x7fffffff; /* |y| */ hy &= 0x7fffffff; /* |y| */
/* purge off exception values */ /* purge off exception values */
if((hy|ly)==0||(hx>=0x7ff00000)|| /* y=0,or x not finite */ if ((hy | ly) == 0 || (hx >= 0x7ff00000) || /* y = 0, or x not finite */
((hy|((ly|-ly)>>31))>0x7ff00000)) /* or y is NaN */ ((hy | ((ly | -ly) >> 31)) > 0x7ff00000)) /* or y is NaN */
return (x*y)/(x*y); {
if(hx<=hy) { return (x * y) / (x * y);
if((hx<hy)||(lx<ly)) return x; /* |x|<|y| return x */ }
if(lx==ly) if (hx <= hy)
return Zero[(unsigned)sx>>31]; /* |x|=|y| return x*0*/ {
if ((hx < hy) || (lx < ly)) /* |x| < |y| return x */
{
return x;
}
if (lx == ly) /* |x| = |y| return x * 0 */
{
return Zero[(unsigned) sx >> 31];
}
} }
/* determine ix = ilogb(x) */ /* determine ix = ilogb(x) */
if(hx<0x00100000) { /* subnormal x */ if (hx < 0x00100000) /* subnormal x */
if(hx==0) { {
for (ix = -1043, i=lx; i>0; i<<=1) ix -=1; if (hx == 0)
} else { {
for (ix = -1022,i=(hx<<11); i>0; i<<=1) ix -=1; for (ix = -1043, i = lx; i > 0; i <<= 1)
{
ix -= 1;
}
}
else
{
for (ix = -1022, i = (hx << 11); i > 0; i <<= 1)
{
ix -= 1;
}
}
}
else
{
ix = (hx >> 20) - 1023;
} }
} else ix = (hx>>20)-1023;
/* determine iy = ilogb(y) */ /* determine iy = ilogb(y) */
if(hy<0x00100000) { /* subnormal y */ if (hy < 0x00100000) /* subnormal y */
if(hy==0) { {
for (iy = -1043, i=ly; i>0; i<<=1) iy -=1; if (hy == 0)
} else { {
for (iy = -1022,i=(hy<<11); i>0; i<<=1) iy -=1; for (iy = -1043, i = ly; i > 0; i <<= 1)
{
iy -= 1;
}
}
else
{
for (iy = -1022, i = (hy << 11); i > 0; i <<= 1)
{
iy -= 1;
}
}
}
else
{
iy = (hy >> 20) - 1023;
} }
} else iy = (hy>>20)-1023;
/* set up {hx,lx}, {hy,ly} and align y to x */ /* set up {hx,lx}, {hy,ly} and align y to x */
if(ix >= -1022) if (ix >= -1022)
hx = 0x00100000|(0x000fffff&hx); {
else { /* subnormal x, shift x to normal */ hx = 0x00100000 | (0x000fffff & hx);
n = -1022-ix; }
if(n<=31) { else /* subnormal x, shift x to normal */
hx = (hx<<n)|(lx>>(32-n)); {
n = -1022 - ix;
if (n <= 31)
{
hx = (hx << n) | (lx >> (32 - n));
lx <<= n; lx <<= n;
} else { }
hx = lx<<(n-32); else
{
hx = lx << (n - 32);
lx = 0; lx = 0;
} }
} }
if(iy >= -1022) if (iy >= -1022)
hy = 0x00100000|(0x000fffff&hy); {
else { /* subnormal y, shift y to normal */ hy = 0x00100000 | (0x000fffff & hy);
n = -1022-iy; }
if(n<=31) { else /* subnormal y, shift y to normal */
hy = (hy<<n)|(ly>>(32-n)); {
n = -1022 - iy;
if (n <= 31)
{
hy = (hy << n) | (ly >> (32 - n));
ly <<= n; ly <<= n;
} else { }
hy = ly<<(n-32); else
{
hy = ly << (n - 32);
ly = 0; ly = 0;
} }
} }
/* fix point fmod */ /* fix point fmod */
n = ix - iy; n = ix - iy;
while(n--) { while (n--)
hz=hx-hy;lz=lx-ly; if(lx<ly) hz -= 1; {
if(hz<0){hx = hx+hx+(lx>>31); lx = lx+lx;} hz = hx - hy;
else { lz = lx - ly;
if((hz|lz)==0) /* return sign(x)*0 */ if (lx < ly)
return Zero[(unsigned)sx>>31]; {
hx = hz+hz+(lz>>31); lx = lz+lz; hz -= 1;
}
if (hz < 0)
{
hx = hx + hx + (lx >> 31);
lx = lx + lx;
}
else
{
if ((hz | lz) == 0) /* return sign(x) * 0 */
{
return Zero[(unsigned) sx >> 31];
}
hx = hz + hz + (lz >> 31);
lx = lz + lz;
} }
} }
hz=hx-hy;lz=lx-ly; if(lx<ly) hz -= 1; hz = hx - hy;
if(hz>=0) {hx=hz;lx=lz;} lz = lx - ly;
if (lx < ly)
{
hz -= 1;
}
if (hz >= 0)
{
hx = hz;
lx = lz;
}
/* convert back to floating value and restore the sign */ /* convert back to floating value and restore the sign */
if((hx|lx)==0) /* return sign(x)*0 */ if ((hx | lx) == 0) /* return sign(x) * 0 */
return Zero[(unsigned)sx>>31]; {
while(hx<0x00100000) { /* normalize x */ return Zero[(unsigned) sx >> 31];
hx = hx+hx+(lx>>31); lx = lx+lx; }
while (hx < 0x00100000) /* normalize x */
{
hx = hx + hx + (lx >> 31);
lx = lx + lx;
iy -= 1; iy -= 1;
} }
if(iy>= -1022) { /* normalize output */ if (iy >= -1022) /* normalize output */
hx = ((hx-0x00100000)|((iy+1023)<<20)); {
__HI(x) = hx|sx; hx = ((hx - 0x00100000) | ((iy + 1023) << 20));
__LO(x) = lx; __HI (x) = hx | sx;
} else { /* subnormal output */ __LO (x) = lx;
n = -1022 - iy;
if(n<=20) {
lx = (lx>>n)|((unsigned)hx<<(32-n));
hx >>= n;
} else if (n<=31) {
lx = (hx<<(32-n))|(lx>>n); hx = sx;
} else {
lx = hx>>(n-32); hx = sx;
} }
__HI(x) = hx|sx; else /* subnormal output */
__LO(x) = lx; {
n = -1022 - iy;
if (n <= 20)
{
lx = (lx >> n) | ((unsigned) hx << (32 - n));
hx >>= n;
}
else if (n <= 31)
{
lx = (hx << (32 - n)) | (lx >> n);
hx = sx;
}
else
{
lx = hx >> (n - 32);
hx = sx;
}
__HI (x) = hx | sx;
__LO (x) = lx;
x *= one; /* create necessary signal */ x *= one; /* create necessary signal */
} }
return x; /* exact output */ return x; /* exact output */
} } /* fmod */
+10 -9
View File
@@ -11,19 +11,20 @@
* ==================================================== * ====================================================
*/ */
/* /* isnan(x) returns 1 is x is nan, else 0;
* isnan(x) returns 1 is x is nan, else 0;
* no branching! * no branching!
*/ */
#include "fdlibm.h" #include "fdlibm.h"
int isnan(double x) int
isnan (double x)
{ {
int hx,lx; int hx, lx;
hx = (__HI(x)&0x7fffffff);
lx = __LO(x); hx = (__HI (x) & 0x7fffffff);
hx |= (unsigned)(lx|(-lx))>>31; lx = __LO (x);
hx |= (unsigned) (lx | (-lx)) >> 31;
hx = 0x7ff00000 - hx; hx = 0x7ff00000 - hx;
return ((unsigned)(hx))>>31; return ((unsigned) (hx)) >> 31;
} } /* isnan */
+85 -41
View File
@@ -76,53 +76,97 @@
#define Lg6 1.531383769920937332e-01 /* 3FC39A09 D078C69F */ #define Lg6 1.531383769920937332e-01 /* 3FC39A09 D078C69F */
#define Lg7 1.479819860511658591e-01 /* 3FC2F112 DF3E5244 */ #define Lg7 1.479819860511658591e-01 /* 3FC2F112 DF3E5244 */
double log(double x) double
log (double x)
{ {
double hfsq,f,s,z,R,w,t1,t2,dk; double hfsq, f, s, z, R, w, t1, t2, dk;
int k,hx,i,j; int k, hx, i, j;
unsigned lx; unsigned lx;
hx = __HI(x); /* high word of x */ hx = __HI (x); /* high word of x */
lx = __LO(x); /* low word of x */ lx = __LO (x); /* low word of x */
k=0; k = 0;
if (hx < 0x00100000) { /* x < 2**-1022 */ if (hx < 0x00100000) /* x < 2**-1022 */
if (((hx&0x7fffffff)|lx)==0) {
return -two54/zero; /* log(+-0)=-inf */ if (((hx & 0x7fffffff) | lx) == 0) /* log(+-0) = -inf */
if (hx<0) return (x-x)/zero; /* log(-#) = NaN */ {
k -= 54; x *= two54; /* subnormal number, scale up x */ return -two54 / zero;
hx = __HI(x); /* high word of x */
} }
if (hx >= 0x7ff00000) return x+x; if (hx < 0) /* log(-#) = NaN */
k += (hx>>20)-1023; {
return (x - x) / zero;
}
k -= 54;
x *= two54; /* subnormal number, scale up x */
hx = __HI (x); /* high word of x */
}
if (hx >= 0x7ff00000)
{
return x + x;
}
k += (hx >> 20) - 1023;
hx &= 0x000fffff; hx &= 0x000fffff;
i = (hx+0x95f64)&0x100000; i = (hx + 0x95f64) & 0x100000;
__HI(x) = hx|(i^0x3ff00000); /* normalize x or x/2 */ __HI (x) = hx | (i ^ 0x3ff00000); /* normalize x or x / 2 */
k += (i>>20); k += (i >> 20);
f = x-1.0; f = x - 1.0;
if((0x000fffff&(2+hx))<3) { /* |f| < 2**-20 */ if ((0x000fffff & (2 + hx)) < 3) /* |f| < 2**-20 */
if(f==zero) if(k==0) return zero; else {dk=(double)k; {
return dk*ln2_hi+dk*ln2_lo;} if (f == zero)
R = f*f*(0.5-0.33333333333333333*f); {
if(k==0) return f-R; else {dk=(double)k; if (k == 0)
return dk*ln2_hi-((R-dk*ln2_lo)-f);} {
return zero;
} }
s = f/(2.0+f); else
dk = (double)k; {
z = s*s; dk = (double) k;
i = hx-0x6147a; return dk * ln2_hi + dk * ln2_lo;
w = z*z; }
j = 0x6b851-hx; }
t1= w*(Lg2+w*(Lg4+w*Lg6)); R = f * f * (0.5 - 0.33333333333333333 * f);
t2= z*(Lg1+w*(Lg3+w*(Lg5+w*Lg7))); if (k == 0)
{
return f - R;
}
else
{
dk = (double) k;
return dk * ln2_hi - ((R - dk * ln2_lo) - f);
}
}
s = f / (2.0 + f);
dk = (double) k;
z = s * s;
i = hx - 0x6147a;
w = z * z;
j = 0x6b851 - hx;
t1 = w * (Lg2 + w * (Lg4 + w * Lg6));
t2 = z * (Lg1 + w * (Lg3 + w * (Lg5 + w * Lg7)));
i |= j; i |= j;
R = t2+t1; R = t2 + t1;
if(i>0) { if (i > 0)
hfsq=0.5*f*f; {
if(k==0) return f-(hfsq-s*(hfsq+R)); else hfsq = 0.5 * f * f;
return dk*ln2_hi-((hfsq-(s*(hfsq+R)+dk*ln2_lo))-f); if (k == 0)
} else { {
if(k==0) return f-s*(f-R); else return f - (hfsq - s * (hfsq + R));
return dk*ln2_hi-((s*(f-R)-dk*ln2_lo)-f);
} }
} else
{
return dk * ln2_hi - ((hfsq - (s * (hfsq + R) + dk * ln2_lo)) - f);
}
}
else
{
if (k == 0)
{
return f - s * (f - R);
}
else
{
return dk * ln2_hi - ((s * (f - R) - dk * ln2_lo) - f);
}
}
} /* log */
+291 -162
View File
@@ -48,7 +48,7 @@
* always returns the correct integer provided it is * always returns the correct integer provided it is
* representable. * representable.
* *
* Constants : * Constants:
* The hexadecimal values are the intended ones for the following * The hexadecimal values are the intended ones for the following
* constants. The decimal values may be used, provided that the * constants. The decimal values may be used, provided that the
* compiler will convert from decimal to binary accurately enough * compiler will convert from decimal to binary accurately enough
@@ -57,18 +57,29 @@
#include "fdlibm.h" #include "fdlibm.h"
static const double static const double one = 1.0;
one = 1.0, static const double bp[] =
bp[] = {1.0, 1.5,}, {
dp_h[] = { 0.0, 5.84962487220764160156e-01,}, /* 0x3FE2B803, 0x40000000 */ 1.0,
dp_l[] = { 0.0, 1.35003920212974897128e-08,}; /* 0x3E4CFDEB, 0x43CFD006 */ 1.5,
};
static const double dp_h[] =
{
0.0,
5.84962487220764160156e-01, /* 0x3FE2B803, 0x40000000 */
};
static const double dp_l[] =
{
0.0,
1.35003920212974897128e-08, /* 0x3E4CFDEB, 0x43CFD006 */
};
#define zero 0.0 #define zero 0.0
#define two 2.0 #define two 2.0
#define two53 9007199254740992.0 /* 0x43400000, 0x00000000 */ #define two53 9007199254740992.0 /* 0x43400000, 0x00000000 */
#define huge 1.0e300 #define huge 1.0e300
#define tiny 1.0e-300 #define tiny 1.0e-300
/* poly coefs for (3/2)*(log(x)-2s-2/3*s**3 */ /* poly coefs for (3/2) * (log(x) - 2s - 2/3 * s**3 */
#define L1 5.99999999999994648725e-01 /* 0x3FE33333, 0x33333303 */ #define L1 5.99999999999994648725e-01 /* 0x3FE33333, 0x33333303 */
#define L2 4.28571428578550184252e-01 /* 0x3FDB6DB6, 0xDB6FABFF */ #define L2 4.28571428578550184252e-01 /* 0x3FDB6DB6, 0xDB6FABFF */
#define L3 3.33333329818377432918e-01 /* 0x3FD55555, 0x518F264D */ #define L3 3.33333329818377432918e-01 /* 0x3FD55555, 0x518F264D */
@@ -84,33 +95,42 @@ dp_l[] = { 0.0, 1.35003920212974897128e-08,}; /* 0x3E4CFDEB, 0x43CFD006 */
#define lg2_h 6.93147182464599609375e-01 /* 0x3FE62E43, 0x00000000 */ #define lg2_h 6.93147182464599609375e-01 /* 0x3FE62E43, 0x00000000 */
#define lg2_l -1.90465429995776804525e-09 /* 0xBE205C61, 0x0CA86C39 */ #define lg2_l -1.90465429995776804525e-09 /* 0xBE205C61, 0x0CA86C39 */
#define ovt 8.0085662595372944372e-0017 /* -(1024-log2(ovfl+.5ulp)) */ #define ovt 8.0085662595372944372e-0017 /* -(1024-log2(ovfl+.5ulp)) */
#define cp 9.61796693925975554329e-01 /* 0x3FEEC709, 0xDC3A03FD =2/(3ln2) */ #define cp 9.61796693925975554329e-01 /* 0x3FEEC709, 0xDC3A03FD = 2 / (3 ln2) */
#define cp_h 9.61796700954437255859e-01 /* 0x3FEEC709, 0xE0000000 =(float)cp */ #define cp_h 9.61796700954437255859e-01 /* 0x3FEEC709, 0xE0000000 = (float) cp */
#define cp_l -7.02846165095275826516e-09 /* 0xBE3E2FE0, 0x145B01F5 =tail of cp_h*/ #define cp_l -7.02846165095275826516e-09 /* 0xBE3E2FE0, 0x145B01F5 = tail of cp_h */
#define ivln2 1.44269504088896338700e+00 /* 0x3FF71547, 0x652B82FE =1/ln2 */ #define ivln2 1.44269504088896338700e+00 /* 0x3FF71547, 0x652B82FE = 1 / ln2 */
#define ivln2_h 1.44269502162933349609e+00 /* 0x3FF71547, 0x60000000 =24b 1/ln2*/ #define ivln2_h 1.44269502162933349609e+00 /* 0x3FF71547, 0x60000000 = 24b 1 / ln2 */
#define ivln2_l 1.92596299112661746887e-08 /* 0x3E54AE0B, 0xF85DDF44 =1/ln2 tail*/ #define ivln2_l 1.92596299112661746887e-08 /* 0x3E54AE0B, 0xF85DDF44 = 1 / ln2 tail */
double pow(double x, double y) double
pow (double x, double y)
{ {
double z,ax,z_h,z_l,p_h,p_l; double z, ax, z_h, z_l, p_h, p_l;
double y1,t1,t2,r,s,t,u,v,w; double y1, t1, t2, r, s, t, u, v, w;
int i0,i1,i,j,k,yisint,n; int i0, i1, i, j, k, yisint, n;
int hx,hy,ix,iy; int hx, hy, ix, iy;
unsigned lx,ly; unsigned lx, ly;
i0 = ((*(int*)&one)>>29)^1; i1=1-i0; i0 = ((*(int *) &one) >> 29) ^ 1;
hx = __HI(x); lx = __LO(x); i1 = 1 - i0;
hy = __HI(y); ly = __LO(y); hx = __HI (x);
ix = hx&0x7fffffff; iy = hy&0x7fffffff; lx = __LO (x);
hy = __HI (y);
ly = __LO (y);
ix = hx & 0x7fffffff;
iy = hy & 0x7fffffff;
/* y==zero: x**0 = 1 */ /* y == zero: x**0 = 1 */
if((iy|ly)==0) return one; if ((iy | ly) == 0)
{
return one;
}
/* +-NaN return x+y */ /* +-NaN return x + y */
if(ix > 0x7ff00000 || ((ix==0x7ff00000)&&(lx!=0)) || if (ix > 0x7ff00000 || ((ix == 0x7ff00000) && (lx != 0)) || iy > 0x7ff00000 || ((iy == 0x7ff00000) && (ly != 0)))
iy > 0x7ff00000 || ((iy==0x7ff00000)&&(ly!=0))) {
return x+y; return x + y;
}
/* determine if y is an odd int when x < 0 /* determine if y is an odd int when x < 0
* yisint = 0 ... y is not an integer * yisint = 0 ... y is not an integer
@@ -118,181 +138,290 @@ double pow(double x, double y)
* yisint = 2 ... y is an even int * yisint = 2 ... y is an even int
*/ */
yisint = 0; yisint = 0;
if(hx<0) { if (hx < 0)
if(iy>=0x43400000) yisint = 2; /* even integer y */ {
else if(iy>=0x3ff00000) { if (iy >= 0x43400000) /* even integer y */
k = (iy>>20)-0x3ff; /* exponent */ {
if(k>20) { yisint = 2;
j = ly>>(52-k); }
if((j<<(52-k))==ly) yisint = 2-(j&1); else if (iy >= 0x3ff00000)
} else if(ly==0) { {
j = iy>>(20-k); k = (iy >> 20) - 0x3ff; /* exponent */
if((j<<(20-k))==iy) yisint = 2-(j&1); if (k > 20)
{
j = ly >> (52 - k);
if ((j << (52 - k)) == ly)
{
yisint = 2 - (j & 1);
}
}
else if (ly == 0)
{
j = iy >> (20 - k);
if ((j << (20 - k)) == iy)
{
yisint = 2 - (j & 1);
}
} }
} }
} }
/* special value of y */ /* special value of y */
if(ly==0) { if (ly == 0)
if (iy==0x7ff00000) { /* y is +-inf */ {
if(((ix-0x3ff00000)|lx)==0) if (iy == 0x7ff00000) /* y is +-inf */
return y - y; /* inf**+-1 is NaN */ {
else if (ix >= 0x3ff00000)/* (|x|>1)**+-inf = inf,0 */ if (((ix - 0x3ff00000) | lx) == 0) /* inf**+-1 is NaN */
return (hy>=0)? y: zero; {
return y - y;
}
else if (ix >= 0x3ff00000) /* (|x|>1)**+-inf = inf,0 */
{
return (hy >= 0) ? y : zero;
}
else /* (|x|<1)**-,+inf = inf,0 */ else /* (|x|<1)**-,+inf = inf,0 */
return (hy<0)?-y: zero; {
return (hy < 0) ? -y : zero;
} }
if(iy==0x3ff00000) { /* y is +-1 */
if(hy<0) return one/x; else return x;
} }
if(hy==0x40000000) return x*x; /* y is 2 */ if (iy == 0x3ff00000) /* y is +-1 */
if(hy==0x3fe00000) { /* y is 0.5 */ {
if(hx>=0) /* x >= +0 */ if (hy < 0)
return sqrt(x); {
return one / x;
}
else
{
return x;
}
}
if (hy == 0x40000000) /* y is 2 */
{
return x * x;
}
if (hy == 0x3fe00000) /* y is 0.5 */
{
if (hx >= 0) /* x >= +0 */
{
return sqrt (x);
}
} }
} }
ax = fabs(x); ax = fabs (x);
/* special value of x */ /* special value of x */
if(lx==0) { if (lx == 0)
if(ix==0x7ff00000||ix==0||ix==0x3ff00000){ {
z = ax; /*x is +-0,+-inf,+-1*/ if (ix == 0x7ff00000 || ix == 0 || ix == 0x3ff00000)
if(hy<0) z = one/z; /* z = (1/|x|) */ {
if(hx<0) { z = ax; /* x is +-0,+-inf,+-1 */
if(((ix-0x3ff00000)|yisint)==0) { if (hy < 0)
z = (z-z)/(z-z); /* (-1)**non-int is NaN */ {
} else if(yisint==1) z = one / z; /* z = (1 / |x|) */
}
if (hx < 0)
{
if (((ix - 0x3ff00000) | yisint) == 0)
{
z = (z - z) / (z - z); /* (-1)**non-int is NaN */
}
else if (yisint == 1)
{
z = -z; /* (x<0)**odd = -(|x|**odd) */ z = -z; /* (x<0)**odd = -(|x|**odd) */
} }
}
return z; return z;
} }
} }
n = (hx>>31)+1; n = (hx >> 31) + 1;
/* (x<0)**(non-int) is NaN */ /* (x<0)**(non-int) is NaN */
if((n|yisint)==0) return (x-x)/(x-x); if ((n | yisint) == 0)
{
return (x - x) / (x - x);
}
s = one; /* s (sign of result -ve**odd) = -1 else = 1 */ s = one; /* s (sign of result -ve**odd) = -1 else = 1 */
if((n|(yisint-1))==0) s = -one;/* (-ve)**(odd int) */ if ((n | (yisint - 1)) == 0)
{
s = -one; /* (-ve)**(odd int) */
}
/* |y| is huge */ /* |y| is huge */
if(iy>0x41e00000) { /* if |y| > 2**31 */ if (iy > 0x41e00000) /* if |y| > 2**31 */
if(iy>0x43f00000){ /* if |y| > 2**64, must o/uflow */ {
if(ix<=0x3fefffff) return (hy<0)? huge*huge:tiny*tiny; if (iy > 0x43f00000) /* if |y| > 2**64, must o/uflow */
if(ix>=0x3ff00000) return (hy>0)? huge*huge:tiny*tiny; {
if (ix <= 0x3fefffff)
{
return (hy < 0) ? huge * huge : tiny * tiny;
}
if (ix >= 0x3ff00000)
{
return (hy > 0) ? huge * huge : tiny * tiny;
}
} }
/* over/underflow if x is not close to one */ /* over/underflow if x is not close to one */
if(ix<0x3fefffff) return (hy<0)? s*huge*huge:s*tiny*tiny; if (ix < 0x3fefffff)
if(ix>0x3ff00000) return (hy>0)? s*huge*huge:s*tiny*tiny; {
/* now |1-x| is tiny <= 2**-20, suffice to compute return (hy < 0) ? s * huge * huge : s * tiny * tiny;
log(x) by x-x^2/2+x^3/3-x^4/4 */ }
t = ax-one; /* t has 20 trailing zeros */ if (ix > 0x3ff00000)
w = (t*t)*(0.5-t*(0.3333333333333333333333-t*0.25)); {
u = ivln2_h*t; /* ivln2_h has 21 sig. bits */ return (hy > 0) ? s * huge * huge : s * tiny * tiny;
v = t*ivln2_l-w*ivln2; }
t1 = u+v; /* now |1 - x| is tiny <= 2**-20, suffice to compute
__LO(t1) = 0; log(x) by x - x^2 / 2 + x^3 / 3 - x^4 / 4 */
t2 = v-(t1-u); t = ax - one; /* t has 20 trailing zeros */
} else { w = (t * t) * (0.5 - t * (0.3333333333333333333333 - t * 0.25));
double ss,s2,s_h,s_l,t_h,t_l; u = ivln2_h * t; /* ivln2_h has 21 sig. bits */
v = t * ivln2_l - w * ivln2;
t1 = u + v;
__LO (t1) = 0;
t2 = v - (t1 - u);
}
else
{
double ss, s2, s_h, s_l, t_h, t_l;
n = 0; n = 0;
/* take care subnormal number */ /* take care subnormal number */
if(ix<0x00100000) if (ix < 0x00100000)
{ax *= two53; n -= 53; ix = __HI(ax); } {
n += ((ix)>>20)-0x3ff; ax *= two53;
j = ix&0x000fffff; n -= 53;
ix = __HI (ax);
}
n += ((ix) >> 20) - 0x3ff;
j = ix & 0x000fffff;
/* determine interval */ /* determine interval */
ix = j|0x3ff00000; /* normalize ix */ ix = j | 0x3ff00000; /* normalize ix */
if(j<=0x3988E) k=0; /* |x|<sqrt(3/2) */ if (j <= 0x3988E) /* |x| < sqrt(3/2) */
else if(j<0xBB67A) k=1; /* |x|<sqrt(3) */ {
else {k=0;n+=1;ix -= 0x00100000;} k = 0;
__HI(ax) = ix; }
else if (j < 0xBB67A) /* |x| < sqrt(3) */
{
k = 1;
}
else
{
k = 0;
n += 1;
ix -= 0x00100000;
}
__HI (ax) = ix;
/* compute ss = s_h+s_l = (x-1)/(x+1) or (x-1.5)/(x+1.5) */ /* compute ss = s_h + s_l = (x - 1) / (x + 1) or (x - 1.5) / (x + 1.5) */
u = ax-bp[k]; /* bp[0]=1.0, bp[1]=1.5 */ u = ax - bp[k]; /* bp[0] = 1.0, bp[1] = 1.5 */
v = one/(ax+bp[k]); v = one / (ax + bp[k]);
ss = u*v; ss = u * v;
s_h = ss; s_h = ss;
__LO(s_h) = 0; __LO (s_h) = 0;
/* t_h=ax+bp[k] High */ /* t_h = ax + bp[k] High */
t_h = zero; t_h = zero;
__HI(t_h)=((ix>>1)|0x20000000)+0x00080000+(k<<18); __HI (t_h) = ((ix >> 1) | 0x20000000) + 0x00080000 + (k << 18);
t_l = ax - (t_h-bp[k]); t_l = ax - (t_h - bp[k]);
s_l = v*((u-s_h*t_h)-s_h*t_l); s_l = v * ((u - s_h * t_h) - s_h * t_l);
/* compute log(ax) */ /* compute log(ax) */
s2 = ss*ss; s2 = ss * ss;
r = s2*s2*(L1+s2*(L2+s2*(L3+s2*(L4+s2*(L5+s2*L6))))); r = s2 * s2 * (L1 + s2 * (L2 + s2 * (L3 + s2 * (L4 + s2 * (L5 + s2 * L6)))));
r += s_l*(s_h+ss); r += s_l * (s_h + ss);
s2 = s_h*s_h; s2 = s_h * s_h;
t_h = 3.0+s2+r; t_h = 3.0 + s2 + r;
__LO(t_h) = 0; __LO (t_h) = 0;
t_l = r-((t_h-3.0)-s2); t_l = r - ((t_h - 3.0) - s2);
/* u+v = ss*(1+...) */ /* u + v = ss * (1 + ...) */
u = s_h*t_h; u = s_h * t_h;
v = s_l*t_h+t_l*ss; v = s_l * t_h + t_l * ss;
/* 2/(3log2)*(ss+...) */ /* 2 / (3 * log2) * (ss + ...) */
p_h = u+v; p_h = u + v;
__LO(p_h) = 0; __LO (p_h) = 0;
p_l = v-(p_h-u); p_l = v - (p_h - u);
z_h = cp_h*p_h; /* cp_h+cp_l = 2/(3*log2) */ z_h = cp_h * p_h; /* cp_h + cp_l = 2 / (3 * log2) */
z_l = cp_l*p_h+p_l*cp+dp_l[k]; z_l = cp_l * p_h + p_l * cp + dp_l[k];
/* log2(ax) = (ss+..)*2/(3*log2) = n + dp_h + z_h + z_l */ /* log2(ax) = (ss + ...) * 2 / (3 * log2) = n + dp_h + z_h + z_l */
t = (double)n; t = (double) n;
t1 = (((z_h+z_l)+dp_h[k])+t); t1 = (((z_h + z_l) + dp_h[k]) + t);
__LO(t1) = 0; __LO (t1) = 0;
t2 = z_l-(((t1-t)-dp_h[k])-z_h); t2 = z_l - (((t1 - t) - dp_h[k]) - z_h);
} }
/* split up y into y1+y2 and compute (y1+y2)*(t1+t2) */ /* split up y into y1 + y2 and compute (y1 + y2) * (t1 + t2) */
y1 = y; y1 = y;
__LO(y1) = 0; __LO (y1) = 0;
p_l = (y-y1)*t1+y*t2; p_l = (y - y1) * t1 + y * t2;
p_h = y1*t1; p_h = y1 * t1;
z = p_l+p_h; z = p_l + p_h;
j = __HI(z); j = __HI (z);
i = __LO(z); i = __LO (z);
if (j>=0x40900000) { /* z >= 1024 */ if (j >= 0x40900000) /* z >= 1024 */
if(((j-0x40900000)|i)!=0) /* if z > 1024 */ {
return s*huge*huge; /* overflow */ if (((j - 0x40900000) | i) != 0) /* if z > 1024 */
else { {
if(p_l+ovt>z-p_h) return s*huge*huge; /* overflow */ return s * huge * huge; /* overflow */
}
else
{
if (p_l + ovt > z - p_h)
{
return s * huge * huge; /* overflow */
}
}
}
else if ((j & 0x7fffffff) >= 0x4090cc00) /* z <= -1075 */
{
if (((j - 0xc090cc00) | i) != 0) /* z < -1075 */
{
return s * tiny * tiny; /* underflow */
}
else
{
if (p_l <= z - p_h)
{
return s * tiny * tiny; /* underflow */
} }
} else if((j&0x7fffffff)>=0x4090cc00 ) { /* z <= -1075 */
if(((j-0xc090cc00)|i)!=0) /* z < -1075 */
return s*tiny*tiny; /* underflow */
else {
if(p_l<=z-p_h) return s*tiny*tiny; /* underflow */
} }
} }
/* /*
* compute 2**(p_h+p_l) * compute 2**(p_h + p_l)
*/ */
i = j&0x7fffffff; i = j & 0x7fffffff;
k = (i>>20)-0x3ff; k = (i >> 20) - 0x3ff;
n = 0; n = 0;
if(i>0x3fe00000) { /* if |z| > 0.5, set n = [z+0.5] */ if (i > 0x3fe00000) /* if |z| > 0.5, set n = [z + 0.5] */
n = j+(0x00100000>>(k+1)); {
k = ((n&0x7fffffff)>>20)-0x3ff; /* new k for n */ n = j + (0x00100000 >> (k + 1));
k = ((n & 0x7fffffff) >> 20) - 0x3ff; /* new k for n */
t = zero; t = zero;
__HI(t) = (n&~(0x000fffff>>k)); __HI (t) = (n & ~(0x000fffff >> k));
n = ((n&0x000fffff)|0x00100000)>>(20-k); n = ((n & 0x000fffff) | 0x00100000) >> (20 - k);
if(j<0) n = -n; if (j < 0)
{
n = -n;
}
p_h -= t; p_h -= t;
} }
t = p_l+p_h; t = p_l + p_h;
__LO(t) = 0; __LO (t) = 0;
u = t*lg2_h; u = t * lg2_h;
v = (p_l-(t-p_h))*lg2+t*lg2_l; v = (p_l - (t - p_h)) * lg2 + t * lg2_l;
z = u+v; z = u + v;
w = v-(z-u); w = v - (z - u);
t = z*z; t = z * z;
t1 = z - t*(P1+t*(P2+t*(P3+t*(P4+t*P5)))); t1 = z - t * (P1 + t * (P2 + t * (P3 + t * (P4 + t * P5))));
r = (z*t1)/(t1-two)-(w+z*w); r = (z * t1) / (t1 - two) - (w + z * w);
z = one-(r-z); z = one - (r - z);
j = __HI(z); j = __HI (z);
j += (n<<20); j += (n << 20);
if((j>>20)<=0) z = scalbn(z,n); /* subnormal output */ if ((j >> 20) <= 0) /* subnormal output */
else __HI(z) += (n<<20); {
return s*z; z = scalbn (z, n);
} }
else
{
__HI (z) += (n << 20);
}
return s * z;
} /* pow */
+47 -24
View File
@@ -11,9 +11,7 @@
* ==================================================== * ====================================================
*/ */
/* /* scalbn(x,n) returns x* 2**n computed by exponent
* scalbn (double x, int n)
* scalbn(x,n) returns x* 2**n computed by exponent
* manipulation rather than by actually performing an * manipulation rather than by actually performing an
* exponentiation or a multiplication. * exponentiation or a multiplication.
*/ */
@@ -25,29 +23,54 @@
#define huge 1.0e+300 #define huge 1.0e+300
#define tiny 1.0e-300 #define tiny 1.0e-300
double scalbn (double x, int n) double
scalbn (double x, int n)
{ {
int k,hx,lx; int k, hx, lx;
hx = __HI(x);
lx = __LO(x); hx = __HI (x);
k = (hx&0x7ff00000)>>20; /* extract exponent */ lx = __LO (x);
if (k==0) { /* 0 or subnormal x */ k = (hx & 0x7ff00000) >> 20; /* extract exponent */
if ((lx|(hx&0x7fffffff))==0) return x; /* +-0 */ if (k == 0) /* 0 or subnormal x */
x *= two54; {
hx = __HI(x); if ((lx | (hx & 0x7fffffff)) == 0) /* +-0 */
k = ((hx&0x7ff00000)>>20) - 54; {
if (n< -50000) return tiny*x; /*underflow*/ return x;
}
x *= two54;
hx = __HI (x);
k = ((hx & 0x7ff00000) >> 20) - 54;
if (n < -50000) /*underflow */
{
return tiny * x;
}
}
if (k == 0x7ff) /* NaN or Inf */
{
return x + x;
}
k = k + n;
if (k > 0x7fe) /* overflow */
{
return huge * copysign (huge, x);
} }
if (k==0x7ff) return x+x; /* NaN or Inf */
k = k+n;
if (k > 0x7fe) return huge*copysign(huge,x); /* overflow */
if (k > 0) /* normal result */ if (k > 0) /* normal result */
{__HI(x) = (hx&0x800fffff)|(k<<20); return x;} {
__HI (x) = (hx & 0x800fffff) | (k << 20);
return x;
}
if (k <= -54) if (k <= -54)
if (n > 50000) /* in case integer overflow in n+k */ {
return huge*copysign(huge,x); /*overflow*/ if (n > 50000) /* in case integer overflow in n + k */
else return tiny*copysign(tiny,x); /*underflow*/ {
return huge * copysign (huge, x); /*overflow */
}
else
{
return tiny * copysign (tiny, x); /*underflow */
}
}
k += 54; /* subnormal result */ k += 54; /* subnormal result */
__HI(x) = (hx&0x800fffff)|(k<<20); __HI (x) = (hx & 0x800fffff) | (k << 20);
return x*twom54; return x * twom54;
} } /* scalbn */
+99 -59
View File
@@ -13,9 +13,11 @@
/* sqrt(x) /* sqrt(x)
* Return correctly rounded sqrt. * Return correctly rounded sqrt.
*
* ------------------------------------------ * ------------------------------------------
* | Use the hardware sqrt if you have one | * | Use the hardware sqrt if you have one |
* ------------------------------------------ * ------------------------------------------
*
* Method: * Method:
* Bit by bit method using integer arithmetic. (Slow, but portable) * Bit by bit method using integer arithmetic. (Slow, but portable)
* 1. Normalization * 1. Normalization
@@ -77,8 +79,7 @@
* sqrt(-ve) = NaN ... with invalid signal * sqrt(-ve) = NaN ... with invalid signal
* sqrt(NaN) = NaN ... with invalid signal for signaling NaN * sqrt(NaN) = NaN ... with invalid signal for signaling NaN
* *
* Other methods : see the appended file at the end of the program below. * Other methods: see the appended file at the end of the program below.
*---------------
*/ */
#include "fdlibm.h" #include "fdlibm.h"
@@ -86,103 +87,143 @@
#define one 1.0 #define one 1.0
#define tiny 1.0e-300 #define tiny 1.0e-300
double sqrt(double x) double
sqrt (double x)
{ {
double z; double z;
int sign = (int)0x80000000; int sign = (int) 0x80000000;
unsigned r,t1,s1,ix1,q1; unsigned r, t1, s1, ix1, q1;
int ix0,s0,q,m,t,i; int ix0, s0, q, m, t, i;
ix0 = __HI(x); /* high word of x */ ix0 = __HI (x); /* high word of x */
ix1 = __LO(x); /* low word of x */ ix1 = __LO (x); /* low word of x */
/* take care of Inf and NaN */ /* take care of Inf and NaN */
if((ix0&0x7ff00000)==0x7ff00000) { if ((ix0 & 0x7ff00000) == 0x7ff00000)
return x*x+x; /* sqrt(NaN)=NaN, sqrt(+inf)=+inf {
sqrt(-inf)=sNaN */ return x * x + x; /* sqrt(NaN) = NaN, sqrt(+inf) = +inf, sqrt(-inf) = sNaN */
} }
/* take care of zero */ /* take care of zero */
if(ix0<=0) { if (ix0 <= 0)
if(((ix0&(~sign))|ix1)==0) return x;/* sqrt(+-0) = +-0 */ {
else if(ix0<0) if (((ix0 & (~sign)) | ix1) == 0) /* sqrt(+-0) = +-0 */
return (x-x)/(x-x); /* sqrt(-ve) = sNaN */ {
return x;
}
else if (ix0 < 0) /* sqrt(-ve) = sNaN */
{
return (x - x) / (x - x);
}
} }
/* normalize x */ /* normalize x */
m = (ix0>>20); m = (ix0 >> 20);
if(m==0) { /* subnormal x */ if (m == 0) /* subnormal x */
while(ix0==0) { {
while (ix0 == 0)
{
m -= 21; m -= 21;
ix0 |= (ix1>>11); ix1 <<= 21; ix0 |= (ix1 >> 11);
ix1 <<= 21;
} }
for(i=0;(ix0&0x00100000)==0;i++) ix0<<=1; for (i = 0; (ix0 & 0x00100000) == 0; i++)
m -= i-1; {
ix0 |= (ix1>>(32-i)); ix0 <<= 1;
}
m -= i - 1;
ix0 |= (ix1 >> (32 - i));
ix1 <<= i; ix1 <<= i;
} }
m -= 1023; /* unbias exponent */ m -= 1023; /* unbias exponent */
ix0 = (ix0&0x000fffff)|0x00100000; ix0 = (ix0 & 0x000fffff) | 0x00100000;
if(m&1){ /* odd m, double x to make it even */ if (m & 1) /* odd m, double x to make it even */
ix0 += ix0 + ((ix1&sign)>>31); {
ix0 += ix0 + ((ix1 & sign) >> 31);
ix1 += ix1; ix1 += ix1;
} }
m >>= 1; /* m = [m/2] */ m >>= 1; /* m = [m / 2] */
/* generate sqrt(x) bit by bit */ /* generate sqrt(x) bit by bit */
ix0 += ix0 + ((ix1&sign)>>31); ix0 += ix0 + ((ix1 & sign) >> 31);
ix1 += ix1; ix1 += ix1;
q = q1 = s0 = s1 = 0; /* [q,q1] = sqrt(x) */ q = q1 = s0 = s1 = 0; /* [q,q1] = sqrt(x) */
r = 0x00200000; /* r = moving bit from right to left */ r = 0x00200000; /* r = moving bit from right to left */
while(r!=0) { while (r != 0)
t = s0+r; {
if(t<=ix0) { t = s0 + r;
s0 = t+r; if (t <= ix0)
{
s0 = t + r;
ix0 -= t; ix0 -= t;
q += r; q += r;
} }
ix0 += ix0 + ((ix1&sign)>>31); ix0 += ix0 + ((ix1 & sign) >> 31);
ix1 += ix1; ix1 += ix1;
r>>=1; r >>= 1;
} }
r = sign; r = sign;
while(r!=0) { while (r != 0)
t1 = s1+r; {
t1 = s1 + r;
t = s0; t = s0;
if((t<ix0)||((t==ix0)&&(t1<=ix1))) { if ((t < ix0) || ((t == ix0) && (t1 <= ix1)))
s1 = t1+r; {
if(((t1&sign)==sign)&&(s1&sign)==0) s0 += 1; s1 = t1 + r;
if (((t1 & sign) == sign) && (s1 & sign) == 0)
{
s0 += 1;
}
ix0 -= t; ix0 -= t;
if (ix1 < t1) ix0 -= 1; if (ix1 < t1)
{
ix0 -= 1;
}
ix1 -= t1; ix1 -= t1;
q1 += r; q1 += r;
} }
ix0 += ix0 + ((ix1&sign)>>31); ix0 += ix0 + ((ix1 & sign) >> 31);
ix1 += ix1; ix1 += ix1;
r>>=1; r >>= 1;
} }
/* use floating add to find out rounding direction */ /* use floating add to find out rounding direction */
if((ix0|ix1)!=0) { if ((ix0 | ix1) != 0)
z = one-tiny; /* trigger inexact flag */ {
if (z>=one) { z = one - tiny; /* trigger inexact flag */
z = one+tiny; if (z >= one)
if (q1==(unsigned)0xffffffff) { q1=0; q += 1;} {
else if (z>one) { z = one + tiny;
if (q1==(unsigned)0xfffffffe) q+=1; if (q1 == (unsigned) 0xffffffff)
q1+=2; {
} else q1 = 0;
q1 += (q1&1); q += 1;
}
else if (z > one)
{
if (q1 == (unsigned) 0xfffffffe)
{
q += 1;
}
q1 += 2;
}
else
{
q1 += (q1 & 1);
} }
} }
ix0 = (q>>1)+0x3fe00000; }
ix1 = q1>>1; ix0 = (q >> 1) + 0x3fe00000;
if ((q&1)==1) ix1 |= sign; ix1 = q1 >> 1;
ix0 += (m <<20); if ((q & 1) == 1)
__HI(z) = ix0; {
__LO(z) = ix1; ix1 |= sign;
}
ix0 += (m << 20);
__HI (z) = ix0;
__LO (z) = ix1;
return z; return z;
} } /* sqrt */
/* /*
Other methods (use floating-point arithmetic) Other methods (use floating-point arithmetic)
@@ -438,5 +479,4 @@ B. sqrt(x) by Reciproot Iteration
------------------------------------------------- -------------------------------------------------
(4) Special cases (see (4) of Section A). (4) Special cases (see (4) of Section A).
*/ */
+502 -273
View File
File diff suppressed because it is too large Load Diff