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
+2 -2
View File
@@ -13,11 +13,11 @@
/* 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
+29 -10
View File
@@ -12,6 +12,7 @@
*/ */
/* 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)
@@ -52,27 +53,43 @@
#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); hx = __HI (x);
ix = hx & 0x7fffffff; ix = hx & 0x7fffffff;
if(ix>=0x3ff00000) { /* |x| >= 1 */ if (ix >= 0x3ff00000) /* |x| >= 1 */
if(((ix-0x3ff00000)|__LO(x))==0) { /* |x|==1 */ {
if(hx>0) return 0.0; /* acos(1) = 0 */ if (((ix - 0x3ff00000) | __LO (x)) == 0) /* |x| == 1 */
else return pi+2.0*pio2_lo; /* acos(-1)= pi */ {
if (hx > 0) /* acos(1) = 0 */
{
return 0.0;
}
else /* acos(-1) = pi */
{
return pi + 2.0 * pio2_lo;
}
} }
return (x - x) / (x - x); /* acos(|x|>1) is NaN */ return (x - x) / (x - x); /* acos(|x|>1) is NaN */
} }
if(ix<0x3fe00000) { /* |x| < 0.5 */ if (ix < 0x3fe00000) /* |x| < 0.5 */
if(ix<=0x3c600000) return pio2_hi+pio2_lo;/*if|x|<2**-57*/ {
if (ix <= 0x3c600000) /* if |x| < 2**-57 */
{
return pio2_hi + pio2_lo;
}
z = x * x; z = x * x;
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;
return pio2_hi - (x - (pio2_lo - x * r)); return pio2_hi - (x - (pio2_lo - x * r));
} else if (hx<0) { /* x < -0.5 */ }
else if (hx < 0) /* x < -0.5 */
{
z = (one + x) * 0.5; z = (one + x) * 0.5;
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)));
@@ -80,7 +97,9 @@ double acos(double x)
r = p / q; r = p / q;
w = r * s - pio2_lo; w = r * s - pio2_lo;
return pi - 2.0 * (s + w); return pi - 2.0 * (s + w);
} else { /* x > 0.5 */ }
else /* x > 0.5 */
{
z = (one - x) * 0.5; z = (one - x) * 0.5;
s = sqrt (z); s = sqrt (z);
df = s; df = s;
@@ -92,4 +111,4 @@ double acos(double x)
w = r * s + c; w = r * s + c;
return 2.0 * (df + w); return 2.0 * (df + w);
} }
} } /* acos */
+35 -13
View File
@@ -12,6 +12,7 @@
*/ */
/* 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
@@ -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 */
@@ -61,22 +60,35 @@
#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); hx = __HI (x);
ix = hx & 0x7fffffff; ix = hx & 0x7fffffff;
if(ix>= 0x3ff00000) { /* |x|>= 1 */ if (ix >= 0x3ff00000) /* |x| >= 1 */
if(((ix-0x3ff00000)|__LO(x))==0) {
/* asin(1)=+-pi/2 with inexact */ if (((ix - 0x3ff00000) | __LO (x)) == 0) /* asin(1) = +-pi/2 with inexact */
{
return x * pio2_hi + x * pio2_lo; return x * pio2_hi + x * pio2_lo;
}
return (x - x) / (x - x); /* asin(|x|>1) is NaN */ return (x - x) / (x - x); /* asin(|x|>1) is NaN */
} else if (ix<0x3fe00000) { /* |x|<0.5 */ }
if(ix<0x3e400000) { /* if |x| < 2**-27 */ else if (ix < 0x3fe00000) /* |x| < 0.5 */
if(huge+x>one) return x;/* return x with inexact if x!=0*/ {
} else if (ix < 0x3e400000) /* if |x| < 2**-27 */
{
if (huge + x > one) /* return x with inexact if x != 0 */
{
return x;
}
}
else
{
t = x * x; t = x * x;
}
p = t * (pS0 + t * (pS1 + t * (pS2 + t * (pS3 + t * (pS4 + t * pS5))))); p = t * (pS0 + t * (pS1 + t * (pS2 + t * (pS3 + t * (pS4 + t * pS5)))));
q = one + t * (qS1 + t * (qS2 + t * (qS3 + t * qS4))); q = one + t * (qS1 + t * (qS2 + t * (qS3 + t * qS4)));
w = p / q; w = p / q;
@@ -88,10 +100,13 @@ double asin(double x)
p = t * (pS0 + t * (pS1 + t * (pS2 + t * (pS3 + t * (pS4 + t * pS5))))); p = t * (pS0 + t * (pS1 + t * (pS2 + t * (pS3 + t * (pS4 + t * pS5)))));
q = one + t * (qS1 + t * (qS2 + t * (qS3 + t * qS4))); q = one + t * (qS1 + t * (qS2 + t * (qS3 + t * qS4)));
s = sqrt (t); s = sqrt (t);
if(ix>=0x3FEF3333) { /* if |x| > 0.975 */ if (ix >= 0x3FEF3333) /* if |x| > 0.975 */
{
w = p / q; w = p / q;
t = pio2_hi - (2.0 * (s + s * w) - pio2_lo); t = pio2_hi - (2.0 * (s + s * w) - pio2_lo);
} else { }
else
{
w = s; w = s;
__LO (w) = 0; __LO (w) = 0;
c = (t - w * w) / (s + w); c = (t - w * w) / (s + w);
@@ -100,5 +115,12 @@ double asin(double x)
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 */
+65 -28
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 */ return x + x; /* NaN */
if(hx>0) return atanhi[3]+atanlo[3]; }
else return -atanhi[3]-atanlo[3]; if (hx > 0)
} if (ix < 0x3fdc0000) { /* |x| < 0.4375 */ {
if (ix < 0x3e200000) { /* |x| < 2^-29 */ return atanhi[3] + atanlo[3];
if(huge+x>one) return x; /* raise inexact */ }
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 { }
else
{
x = fabs (x); x = fabs (x);
if (ix < 0x3ff30000) { /* |x| < 1.1875 */ if (ix < 0x3ff30000) /* |x| < 1.1875 */
if (ix < 0x3fe60000) { /* 7/16 <=|x|<11/16 */ {
id = 0; x = (2.0*x-one)/(2.0+x); if (ix < 0x3fe60000) /* 7/16 <= |x| < 11/16 */
} else { /* 11/16<=|x|< 19/16 */ {
id = 1; x = (x-one)/(x+one); 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;
}
} }
} 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 { {
return x - x * (s1 + s2);
}
else
{
z = atanhi[id] - ((x * (s1 + s2) - atanlo[id]) - x); z = atanhi[id] - ((x * (s1 + s2) - atanlo[id]) - x);
return (hx < 0) ? -z : z; return (hx < 0) ? -z : z;
} }
} } /* atan */
+113 -38
View File
@@ -9,10 +9,10 @@
* 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):
@@ -20,7 +20,6 @@
* 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);
ix = hx & 0x7fffffff;
lx = __LO (x); lx = __LO (x);
hy = __HI(y); iy = hy&0x7fffffff; hy = __HI (y);
iy = hy & 0x7fffffff;
ly = __LO (y); ly = __LO (y);
if(((ix|((lx|-lx)>>31))>0x7ff00000)|| if (((ix | ((lx | -lx) >> 31)) > 0x7ff00000) || ((iy | ((ly | -ly) >> 31)) > 0x7ff00000)) /* x or y is NaN */
((iy|((ly|-ly)>>31))>0x7ff00000)) /* x or y is NaN */ {
return x + y; return x + y;
if((hx-0x3ff00000|lx)==0) return atan(y); /* x=1.0 */ }
if ((hx - 0x3ff00000 | lx) == 0) /* x = 1.0 */
{
return atan (y);
}
m = ((hy >> 31) & 1) | ((hx >> 30) & 2); /* 2 * sign(x) + sign(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)
{
return (hy < 0) ? -pi_o_2 - tiny : pi_o_2 + tiny;
}
/* compute y / x */ /* compute y / x */
k = (iy - ix) >> 20; k = (iy - ix) >> 20;
if(k > 60) z=pi_o_2+0.5*pi_lo; /* |y/x| > 2**60 */ if (k > 60) /* |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 */ z = pi_o_2 + 0.5 * pi_lo;
switch (m) { }
case 0: return z ; /* atan(+,+) */ else if (hx < 0 && k < -60) /* |y| / x < -2**60 */
case 1: __HI(z) ^= 0x80000000; {
return z ; /* atan(-,+) */ z = 0.0;
case 2: return pi-(z-pi_lo);/* atan(+,-) */ }
default: /* case 3 */ else /* safe to do y / x */
return (z-pi_lo)-pi;/* atan(-,-) */ {
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 */
+70 -24
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,39 +25,84 @@
#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); i0 = __HI (x);
i1 = __LO (x); i1 = __LO (x);
j0 = ((i0 >> 20) & 0x7ff) - 0x3ff; j0 = ((i0 >> 20) & 0x7ff) - 0x3ff;
if(j0<20) { if (j0 < 20)
if(j0<0) { /* raise inexact if x != 0 */ {
if(huge+x>0.0) {/* return 0*sign(x) if |x|<1 */ if (j0 < 0) /* raise inexact if x != 0 */
if(i0<0) {i0=0x80000000;i1=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)
{
i0 = 0x3ff00000;
i1 = 0;
}
}
}
else
{
i = (0x000fffff) >> j0; i = (0x000fffff) >> j0;
if(((i0&i)|i1)==0) return x; /* x is integral */ if (((i0 & i) | i1) == 0) /* x is integral */
if(huge+x>0.0) { /* raise inexact flag */ {
if(i0>0) i0 += (0x00100000)>>j0; return x;
i0 &= (~i); i1=0; }
if (huge + x > 0.0) /* raise inexact flag */
{
if (i0 > 0)
{
i0 += (0x00100000) >> j0;
}
i0 &= (~i);
i1 = 0;
} }
} }
} else if (j0>51) { }
if(j0==0x400) return x+x; /* inf or NaN */ else if (j0 > 51)
else return x; /* x is integral */ {
} else { if (j0 == 0x400) /* inf or NaN */
{
return x + x;
}
else /* x is integral */
{
return x;
}
}
else
{
i = ((unsigned) (0xffffffff)) >> (j0 - 20); i = ((unsigned) (0xffffffff)) >> (j0 - 20);
if((i1&i)==0) return x; /* x is integral */ if ((i1 & i) == 0) /* x is integral */
if(huge+x>0.0) { /* raise inexact flag */ {
if(i0>0) { return x;
if(j0==20) i0+=1; }
else { if (huge + x > 0.0) /* raise inexact flag */
{
if (i0 > 0)
{
if (j0 == 20)
{
i0 += 1;
}
else
{
j = i1 + (1 << (52 - j0)); j = i1 + (1 << (52 - j0));
if(j<i1) i0+=1; /* got a carry */ if (j < i1) /* got a carry */
{
i0 += 1;
}
i1 = j; i1 = j;
} }
} }
@@ -66,4 +112,4 @@ double ceil(double x)
__HI (x) = i0; __HI (x) = i0;
__LO (x) = i1; __LO (x) = i1;
return x; return x;
} } /* ceil */
+4 -5
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 */
+73 -28
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,7 +103,8 @@ 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;
@@ -105,21 +115,40 @@ double exp(double x) /* default IEEE double exp */
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 { {
hi = x - ln2HI[xsb];
lo = ln2LO[xsb];
k = 1 - xsb - xsb;
}
else
{
k = (int) (invln2 * x + halF[xsb]); 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 */
@@ -127,21 +156,37 @@ double exp(double x) /* default IEEE double exp */
} }
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);
}
else
{
y = one - ((lo - (x * c) / (2.0 - c)) - hi);
}
if (k >= -1021)
{
__HI (y) += (k << 20); /* add k to y's exponent */ __HI (y) += (k << 20); /* add k to y's exponent */
return y; return y;
} else { }
else
{
__HI (y) += ((k + 1000) << 20); /* add k to y's exponent */ __HI (y) += ((k + 1000) << 20); /* add k to y's exponent */
return y * twom1000; return y * twom1000;
} }
} } /* exp */
+4 -4
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 */
+5 -4
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); hx = __HI (x);
return (unsigned) ((hx & 0x7fffffff) - 0x7ff00000) >> 31; return (unsigned) ((hx & 0x7fffffff) - 0x7ff00000) >> 31;
} } /* finite */
+68 -24
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,40 +25,83 @@
#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); i0 = __HI (x);
i1 = __LO (x); i1 = __LO (x);
j0 = ((i0 >> 20) & 0x7ff) - 0x3ff; j0 = ((i0 >> 20) & 0x7ff) - 0x3ff;
if(j0<20) { if (j0 < 20)
if(j0<0) { /* raise inexact if x != 0 */ {
if(huge+x>0.0) {/* return 0*sign(x) if |x|<1 */ if (j0 < 0) /* raise inexact if x != 0 */
if(i0>=0) {i0=i1=0;} {
if (huge + x > 0.0) /* return 0 * sign(x) if |x| < 1 */
{
if (i0 >= 0)
{
i0 = i1 = 0;
}
else if (((i0 & 0x7fffffff) | i1) != 0) else if (((i0 & 0x7fffffff) | i1) != 0)
{ i0=0xbff00000;i1=0;} {
i0 = 0xbff00000;
i1 = 0;
} }
} else { }
}
else
{
i = (0x000fffff) >> j0; i = (0x000fffff) >> j0;
if(((i0&i)|i1)==0) return x; /* x is integral */ if (((i0 & i) | i1) == 0) /* x is integral */
if(huge+x>0.0) { /* raise inexact flag */ {
if(i0<0) i0 += (0x00100000)>>j0; return x;
i0 &= (~i); i1=0; }
if (huge + x > 0.0) /* raise inexact flag */
{
if (i0 < 0)
{
i0 += (0x00100000) >> j0;
}
i0 &= (~i);
i1 = 0;
} }
} }
} else if (j0>51) { }
if(j0==0x400) return x+x; /* inf or NaN */ else if (j0 > 51)
else return x; /* x is integral */ {
} else { if (j0 == 0x400) /* inf or NaN */
{
return x + x;
}
else /* x is integral */
{
return x;
}
}
else
{
i = ((unsigned) (0xffffffff)) >> (j0 - 20); i = ((unsigned) (0xffffffff)) >> (j0 - 20);
if((i1&i)==0) return x; /* x is integral */ if ((i1 & i) == 0) /* x is integral */
if(huge+x>0.0) { /* raise inexact flag */ {
if(i0<0) { return x;
if(j0==20) i0+=1; }
else { if (huge + x > 0.0) /* raise inexact flag */
{
if (i0 < 0)
{
if (j0 == 20)
{
i0 += 1;
}
else
{
j = i1 + (1 << (52 - j0)); j = i1 + (1 << (52 - j0));
if(j<i1) i0 +=1 ; /* got a carry */ if (j < i1) /* got a carry */
{
i0 += 1;
}
i1 = j; i1 = j;
} }
} }
@@ -67,4 +111,4 @@ double floor(double x)
__HI (x) = i0; __HI (x) = i0;
__LO (x) = i1; __LO (x) = i1;
return x; return x;
} } /* floor */
+129 -44
View File
@@ -11,20 +11,20 @@
* ==================================================== * ====================================================
*/ */
/* /* 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;
@@ -40,52 +40,100 @@ double fmod(double x, double 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); return (x * y) / (x * y);
if(hx<=hy) { }
if((hx<hy)||(lx<ly)) return x; /* |x|<|y| return x */ if (hx <= hy)
if(lx==ly) {
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); hx = 0x00100000 | (0x000fffff & hx);
else { /* subnormal x, shift x to normal */ }
else /* subnormal x, shift x to normal */
{
n = -1022 - ix; n = -1022 - ix;
if(n<=31) { if (n <= 31)
{
hx = (hx << n) | (lx >> (32 - n)); hx = (hx << n) | (lx >> (32 - n));
lx <<= n; lx <<= n;
} else { }
else
{
hx = lx << (n - 32); hx = lx << (n - 32);
lx = 0; lx = 0;
} }
} }
if (iy >= -1022) if (iy >= -1022)
{
hy = 0x00100000 | (0x000fffff & hy); hy = 0x00100000 | (0x000fffff & hy);
else { /* subnormal y, shift y to normal */ }
else /* subnormal y, shift y to normal */
{
n = -1022 - iy; n = -1022 - iy;
if(n<=31) { if (n <= 31)
{
hy = (hy << n) | (ly >> (32 - n)); hy = (hy << n) | (ly >> (32 - n));
ly <<= n; ly <<= n;
} else { }
else
{
hy = ly << (n - 32); hy = ly << (n - 32);
ly = 0; ly = 0;
} }
@@ -93,42 +141,79 @@ double fmod(double x, double y)
/* 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 (lx < ly)
{
hz -= 1;
}
if (hz < 0)
{
hx = hx + hx + (lx >> 31);
lx = lx + lx;
}
else
{
if ((hz | lz) == 0) /* return sign(x) * 0 */ if ((hz | lz) == 0) /* return sign(x) * 0 */
{
return Zero[(unsigned) sx >> 31]; return Zero[(unsigned) sx >> 31];
hx = hz+hz+(lz>>31); lx = lz+lz; }
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]; return Zero[(unsigned) sx >> 31];
while(hx<0x00100000) { /* normalize x */ }
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)); hx = ((hx - 0x00100000) | ((iy + 1023) << 20));
__HI (x) = hx | sx; __HI (x) = hx | sx;
__LO (x) = lx; __LO (x) = lx;
} else { /* subnormal output */ }
else /* subnormal output */
{
n = -1022 - iy; n = -1022 - iy;
if(n<=20) { if (n <= 20)
{
lx = (lx >> n) | ((unsigned) hx << (32 - n)); lx = (lx >> n) | ((unsigned) hx << (32 - n));
hx >>= n; hx >>= n;
} else if (n<=31) { }
lx = (hx<<(32-n))|(lx>>n); hx = sx; else if (n <= 31)
} else { {
lx = hx>>(n-32); hx = sx; lx = (hx << (32 - n)) | (lx >> n);
hx = sx;
}
else
{
lx = hx >> (n - 32);
hx = sx;
} }
__HI (x) = hx | sx; __HI (x) = hx | sx;
__LO (x) = lx; __LO (x) = lx;
x *= one; /* create necessary signal */ x *= one; /* create necessary signal */
} }
return x; /* exact output */ return x; /* exact output */
} } /* fmod */
+5 -4
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); hx = (__HI (x) & 0x7fffffff);
lx = __LO (x); lx = __LO (x);
hx |= (unsigned) (lx | (-lx)) >> 31; hx |= (unsigned) (lx | (-lx)) >> 31;
hx = 0x7ff00000 - hx; hx = 0x7ff00000 - hx;
return ((unsigned) (hx)) >> 31; return ((unsigned) (hx)) >> 31;
} } /* isnan */
+60 -16
View File
@@ -76,7 +76,8 @@
#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;
@@ -86,26 +87,54 @@ double log(double 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;
}
if (hx < 0) /* log(-#) = NaN */
{
return (x - x) / zero;
}
k -= 54;
x *= two54; /* subnormal number, scale up x */
hx = __HI (x); /* high word of x */ hx = __HI (x); /* high word of x */
} }
if (hx >= 0x7ff00000) return x+x; if (hx >= 0x7ff00000)
{
return x + x;
}
k += (hx >> 20) - 1023; 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)
{
if (k == 0)
{
return zero;
}
else
{
dk = (double) k;
return dk * ln2_hi + dk * ln2_lo;
}
}
R = f * f * (0.5 - 0.33333333333333333 * f); 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 f - R;
}
else
{
dk = (double) k;
return dk * ln2_hi - ((R - dk * ln2_lo) - f);
}
} }
s = f / (2.0 + f); s = f / (2.0 + f);
dk = (double) k; dk = (double) k;
@@ -117,12 +146,27 @@ double log(double x)
t2 = z * (Lg1 + w * (Lg3 + w * (Lg5 + w * Lg7))); 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; hfsq = 0.5 * f * f;
if(k==0) return f-(hfsq-s*(hfsq+R)); else if (k == 0)
{
return f - (hfsq - s * (hfsq + R));
}
else
{
return dk * ln2_hi - ((hfsq - (s * (hfsq + R) + dk * ln2_lo)) - f); return dk * ln2_hi - ((hfsq - (s * (hfsq + R) + dk * ln2_lo)) - f);
} else { }
if(k==0) return f-s*(f-R); else }
else
{
if (k == 0)
{
return f - s * (f - R);
}
else
{
return dk * ln2_hi - ((s * (f - R) - dk * ln2_lo) - f); return dk * ln2_hi - ((s * (f - R) - dk * ln2_lo) - f);
} }
} }
} /* log */
+189 -60
View File
@@ -57,11 +57,22 @@
#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
@@ -91,7 +102,8 @@ dp_l[] = { 0.0, 1.35003920212974897128e-08,}; /* 0x3E4CFDEB, 0x43CFD006 */
#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;
@@ -99,18 +111,26 @@ double pow(double x, double y)
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,52 +138,98 @@ 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 */
{
yisint = 2;
}
else if (iy >= 0x3ff00000)
{
k = (iy >> 20) - 0x3ff; /* exponent */ k = (iy >> 20) - 0x3ff; /* exponent */
if(k>20) { if (k > 20)
{
j = ly >> (52 - k); j = ly >> (52 - k);
if((j<<(52-k))==ly) yisint = 2-(j&1); if ((j << (52 - k)) == ly)
} else if(ly==0) { {
yisint = 2 - (j & 1);
}
}
else if (ly == 0)
{
j = iy >> (20 - k); j = iy >> (20 - k);
if((j<<(20-k))==iy) yisint = 2-(j&1); 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 */ {
if (((ix - 0x3ff00000) | lx) == 0) /* inf**+-1 is NaN */
{
return y - y;
}
else if (ix >= 0x3ff00000) /* (|x|>1)**+-inf = inf,0 */ else if (ix >= 0x3ff00000) /* (|x|>1)**+-inf = inf,0 */
{
return (hy >= 0) ? y : zero; 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 (hy < 0)
{
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 */ if (hx >= 0) /* x >= +0 */
{
return sqrt (x); 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){ {
if (ix == 0x7ff00000 || ix == 0 || ix == 0x3ff00000)
{
z = ax; /* x is +-0,+-inf,+-1 */ z = ax; /* x is +-0,+-inf,+-1 */
if(hy<0) z = one/z; /* z = (1/|x|) */ if (hy < 0)
if(hx<0) { {
if(((ix-0x3ff00000)|yisint)==0) { z = one / z; /* z = (1 / |x|) */
}
if (hx < 0)
{
if (((ix - 0x3ff00000) | yisint) == 0)
{
z = (z - z) / (z - z); /* (-1)**non-int is NaN */ z = (z - z) / (z - z); /* (-1)**non-int is NaN */
} else if(yisint==1) }
else if (yisint == 1)
{
z = -z; /* (x<0)**odd = -(|x|**odd) */ z = -z; /* (x<0)**odd = -(|x|**odd) */
} }
}
return z; return z;
} }
} }
@@ -171,20 +237,40 @@ double pow(double x, double y)
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; {
return (hy < 0) ? s * huge * huge : s * tiny * tiny;
}
if (ix > 0x3ff00000)
{
return (hy > 0) ? s * huge * huge : s * tiny * tiny;
}
/* now |1 - x| is tiny <= 2**-20, suffice to compute /* now |1 - x| is tiny <= 2**-20, suffice to compute
log(x) by x - x^2 / 2 + x^3 / 3 - x^4 / 4 */ log(x) by x - x^2 / 2 + x^3 / 3 - x^4 / 4 */
t = ax - one; /* t has 20 trailing zeros */ t = ax - one; /* t has 20 trailing zeros */
@@ -194,19 +280,37 @@ double pow(double x, double y)
t1 = u + v; t1 = u + v;
__LO (t1) = 0; __LO (t1) = 0;
t2 = v - (t1 - u); t2 = v - (t1 - u);
} else { }
else
{
double ss, s2, s_h, s_l, t_h, t_l; 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); } {
ax *= two53;
n -= 53;
ix = __HI (ax);
}
n += ((ix) >> 20) - 0x3ff; n += ((ix) >> 20) - 0x3ff;
j = ix & 0x000fffff; 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;
}
else if (j < 0xBB67A) /* |x| < sqrt(3) */
{
k = 1;
}
else
{
k = 0;
n += 1;
ix -= 0x00100000;
}
__HI (ax) = ix; __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) */
@@ -231,13 +335,13 @@ double pow(double x, double y)
/* 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;
@@ -252,17 +356,32 @@ double pow(double x, double y)
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 */ if (((j - 0x40900000) | i) != 0) /* if z > 1024 */
{
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 */ 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 */ if (((j - 0xc090cc00) | i) != 0) /* z < -1075 */
{
return s * tiny * tiny; /* underflow */ return s * tiny * tiny; /* underflow */
else { }
if(p_l<=z-p_h) return s*tiny*tiny; /* underflow */ else
{
if (p_l <= z - p_h)
{
return s * tiny * tiny; /* underflow */
}
} }
} }
/* /*
@@ -271,13 +390,17 @@ double pow(double x, double y)
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)); n = j + (0x00100000 >> (k + 1));
k = ((n & 0x7fffffff) >> 20) - 0x3ff; /* new k for n */ 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;
@@ -292,7 +415,13 @@ double pow(double x, double y)
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 */
+35 -12
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); hx = __HI (x);
lx = __LO (x); lx = __LO (x);
k = (hx & 0x7ff00000) >> 20; /* extract exponent */ k = (hx & 0x7ff00000) >> 20; /* extract exponent */
if (k==0) { /* 0 or subnormal x */ if (k == 0) /* 0 or subnormal x */
if ((lx|(hx&0x7fffffff))==0) return x; /* +-0 */ {
if ((lx | (hx & 0x7fffffff)) == 0) /* +-0 */
{
return x;
}
x *= two54; x *= two54;
hx = __HI (x); hx = __HI (x);
k = ((hx & 0x7ff00000) >> 20) - 54; k = ((hx & 0x7ff00000) >> 20) - 54;
if (n< -50000) return tiny*x; /*underflow*/ if (n < -50000) /*underflow */
{
return tiny * x;
}
}
if (k == 0x7ff) /* NaN or Inf */
{
return x + x;
} }
if (k==0x7ff) return x+x; /* NaN or Inf */
k = k + n; k = k + n;
if (k > 0x7fe) return huge*copysign(huge,x); /* overflow */ if (k > 0x7fe) /* overflow */
{
return huge * copysign (huge, x);
}
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 */ if (n > 50000) /* in case integer overflow in n + k */
{
return huge * copysign (huge, x); /*overflow */ return huge * copysign (huge, x); /*overflow */
else return tiny*copysign(tiny,x); /*underflow*/ }
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 */
+69 -29
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
@@ -78,7 +80,6 @@
* 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,7 +87,8 @@
#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;
@@ -97,31 +99,44 @@ double sqrt(double 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++) ix0<<=1;
m -= i - 1; m -= i - 1;
ix0 |= (ix1 >> (32 - i)); 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;
} }
@@ -133,9 +148,11 @@ double sqrt(double x)
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; t = s0 + r;
if(t<=ix0) { if (t <= ix0)
{
s0 = t + r; s0 = t + r;
ix0 -= t; ix0 -= t;
q += r; q += r;
@@ -146,14 +163,22 @@ double sqrt(double x)
} }
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; s1 = t1 + r;
if(((t1&sign)==sign)&&(s1&sign)==0) s0 += 1; 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;
} }
@@ -163,26 +188,42 @@ double sqrt(double x)
} }
/* 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 */ z = one - tiny; /* trigger inexact flag */
if (z>=one) { if (z >= one)
{
z = one + tiny; z = one + tiny;
if (q1==(unsigned)0xffffffff) { q1=0; q += 1;} if (q1 == (unsigned) 0xffffffff)
else if (z>one) { {
if (q1==(unsigned)0xfffffffe) q+=1; q1 = 0;
q += 1;
}
else if (z > one)
{
if (q1 == (unsigned) 0xfffffffe)
{
q += 1;
}
q1 += 2; q1 += 2;
} else }
else
{
q1 += (q1 & 1); q1 += (q1 & 1);
} }
} }
}
ix0 = (q >> 1) + 0x3fe00000; ix0 = (q >> 1) + 0x3fe00000;
ix1 = q1 >> 1; ix1 = q1 >> 1;
if ((q&1)==1) ix1 |= sign; if ((q & 1) == 1)
{
ix1 |= sign;
}
ix0 += (m << 20); ix0 += (m << 20);
__HI (z) = ix0; __HI (z) = ix0;
__LO (z) = ix1; __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).
*/ */
+370 -141
View File
@@ -26,8 +26,7 @@
#define two24 1.67772160000000000000e+07 /* 0x41700000, 0x00000000 */ #define two24 1.67772160000000000000e+07 /* 0x41700000, 0x00000000 */
#define twon24 5.96046447753906250000e-08 /* 0x3E700000, 0x00000000 */ #define twon24 5.96046447753906250000e-08 /* 0x3E700000, 0x00000000 */
/* /* __kernel_rem_pio2(x,y,e0,nx,prec)
* __kernel_rem_pio2(x,y,e0,nx,prec)
* double x[],y[]; int e0,nx,prec; * double x[],y[]; int e0,nx,prec;
* *
* __kernel_rem_pio2 return the last three digits of N with * __kernel_rem_pio2 return the last three digits of N with
@@ -56,7 +55,6 @@
* x[i] = floor(z) * x[i] = floor(z)
* z = (z-x[i])*2**24 * z = (z-x[i])*2**24
* *
*
* y[] ouput result in an array of double precision numbers. * y[] ouput result in an array of double precision numbers.
* The dimension of y[] is: * The dimension of y[] is:
* 24-bit precision 1 * 24-bit precision 1
@@ -85,11 +83,9 @@
* External function: * External function:
* double scalbn(), floor(); * double scalbn(), floor();
* *
*
* Here is the description of some local variables: * Here is the description of some local variables:
* *
* ipio2[] * ipio2[] integer array, contains the (24*i)-th to (24*i+23)-th
* integer array, contains the (24*i)-th to (24*i+23)-th
* bit of 2/pi after binary point. The corresponding * bit of 2/pi after binary point. The corresponding
* floating value is * floating value is
* *
@@ -130,10 +126,8 @@
* *
* ih integer. If >0 it indicates q[] is >= 0.5, hence * ih integer. If >0 it indicates q[] is >= 0.5, hence
* it also indicates the *sign* of the result. * it also indicates the *sign* of the result.
*
*/ */
/* /*
* Constants: * Constants:
* The hexadecimal values are the intended ones for the following * The hexadecimal values are the intended ones for the following
@@ -142,9 +136,14 @@
* to produce the hexadecimal values shown. * to produce the hexadecimal values shown.
*/ */
static const int init_jk[] = {2,3,4,6}; /* initial value for jk */ /* initial value for jk */
static const int init_jk[] =
{
2, 3, 4, 6
};
static const double PIo2[] = { static const double PIo2[] =
{
1.57079625129699707031e+00, /* 0x3FF921FB, 0x40000000 */ 1.57079625129699707031e+00, /* 0x3FF921FB, 0x40000000 */
7.54978941586159635335e-08, /* 0x3E74442D, 0x00000000 */ 7.54978941586159635335e-08, /* 0x3E74442D, 0x00000000 */
5.39030252995776476554e-15, /* 0x3CF84698, 0x80000000 */ 5.39030252995776476554e-15, /* 0x3CF84698, 0x80000000 */
@@ -158,7 +157,8 @@ static const double PIo2[] = {
/* /*
* Table of constants for 2/pi, 396 Hex digits (476 decimal) of 2/pi * Table of constants for 2/pi, 396 Hex digits (476 decimal) of 2/pi
*/ */
static const int ipio2[] = { static const int ipio2[] =
{
0xA2F983, 0x6E4E44, 0x1529FC, 0x2757D1, 0xF534DD, 0xC0DB62, 0xA2F983, 0x6E4E44, 0x1529FC, 0x2757D1, 0xF534DD, 0xC0DB62,
0x95993C, 0x439041, 0xFE5163, 0xABDEBB, 0xC561B7, 0x246E3A, 0x95993C, 0x439041, 0xFE5163, 0xABDEBB, 0xC561B7, 0x246E3A,
0x424DD2, 0xE00649, 0x2EEA09, 0xD1921C, 0xFE1DEB, 0x1CB129, 0x424DD2, 0xE00649, 0x2EEA09, 0xD1921C, 0xFE1DEB, 0x1CB129,
@@ -172,7 +172,8 @@ static const int ipio2[] = {
0x4D7327, 0x310606, 0x1556CA, 0x73A8C9, 0x60E27B, 0xC08C6B, 0x4D7327, 0x310606, 0x1556CA, 0x73A8C9, 0x60E27B, 0xC08C6B,
}; };
static int __kernel_rem_pio2(double *x, double *y, int e0, int nx, int prec) static int
__kernel_rem_pio2 (double *x, double *y, int e0, int nx, int prec)
{ {
int jz, jx, jv, jp, jk, carry, n, iq[20], i, j, k, m, q0, ih; int jz, jx, jv, jp, jk, carry, n, iq[20], i, j, k, m, q0, ih;
double z, fw, f[20], fq[20], q[20]; double z, fw, f[20], fq[20], q[20];
@@ -183,22 +184,36 @@ static int __kernel_rem_pio2(double *x, double *y, int e0, int nx, int prec)
/* determine jx, jv, q0, note that 3 > q0 */ /* determine jx, jv, q0, note that 3 > q0 */
jx = nx - 1; jx = nx - 1;
jv = (e0-3)/24; if(jv<0) jv=0; jv = (e0 - 3) / 24;
if (jv < 0)
{
jv = 0;
}
q0 = e0 - 24 * (jv + 1); q0 = e0 - 24 * (jv + 1);
/* set up f[0] to f[jx + jk] where f[jx + jk] = ipio2[jv + jk] */ /* set up f[0] to f[jx + jk] where f[jx + jk] = ipio2[jv + jk] */
j = jv-jx; m = jx+jk; j = jv - jx;
for(i=0;i<=m;i++,j++) f[i] = (j<0)? zero : (double) ipio2[j]; m = jx + jk;
for (i = 0; i <= m; i++, j++)
{
f[i] = (j < 0) ? zero : (double) ipio2[j];
}
/* compute q[0], q[1], ... q[jk] */ /* compute q[0], q[1], ... q[jk] */
for (i=0;i<=jk;i++) { for (i = 0; i <= jk; i++)
for(j=0,fw=0.0;j<=jx;j++) fw += x[j]*f[jx+i-j]; q[i] = fw; {
for (j = 0, fw = 0.0; j <= jx; j++)
{
fw += x[j] * f[jx + i - j];
}
q[i] = fw;
} }
jz = jk; jz = jk;
recompute: recompute:
/* distill q[] into iq[] reversingly */ /* distill q[] into iq[] reversingly */
for(i=0,j=jz,z=q[jz];j>0;i++,j--) { for (i = 0, j = jz, z = q[jz]; j > 0; i++, j--)
{
fw = (double) ((int) (twon24 * z)); fw = (double) ((int) (twon24 * z));
iq[i] = (int) (z - two24 * fw); iq[i] = (int) (z - two24 * fw);
z = q[j - 1] + fw; z = q[j - 1] + fw;
@@ -210,48 +225,89 @@ recompute:
n = (int) z; n = (int) z;
z -= (double) n; z -= (double) n;
ih = 0; ih = 0;
if(q0>0) { /* need iq[jz-1] to determine n */ if (q0 > 0) /* need iq[jz - 1] to determine n */
i = (iq[jz-1]>>(24-q0)); n += i; {
i = (iq[jz - 1] >> (24 - q0));
n += i;
iq[jz - 1] -= i << (24 - q0); iq[jz - 1] -= i << (24 - q0);
ih = iq[jz - 1] >> (23 - q0); ih = iq[jz - 1] >> (23 - q0);
} }
else if(q0==0) ih = iq[jz-1]>>23; else if (q0 == 0)
else if(z>=0.5) ih=2; {
ih = iq[jz - 1] >> 23;
}
else if (z >= 0.5)
{
ih = 2;
}
if(ih>0) { /* q > 0.5 */ if (ih > 0) /* q > 0.5 */
n += 1; carry = 0; {
for(i=0;i<jz ;i++) { /* compute 1-q */ n += 1;
carry = 0;
for (i = 0; i < jz; i++) /* compute 1 - q */
{
j = iq[i]; j = iq[i];
if(carry==0) { if (carry == 0)
if(j!=0) { {
carry = 1; iq[i] = 0x1000000- j; if (j != 0)
{
carry = 1;
iq[i] = 0x1000000 - j;
} }
} else iq[i] = 0xffffff - j;
} }
if(q0>0) { /* rare case: chance is 1 in 12 */ else
switch(q0) { {
iq[i] = 0xffffff - j;
}
}
if (q0 > 0) /* rare case: chance is 1 in 12 */
{
switch (q0)
{
case 1: case 1:
iq[jz-1] &= 0x7fffff; break; {
iq[jz - 1] &= 0x7fffff;
break;
}
case 2: case 2:
iq[jz-1] &= 0x3fffff; break; {
iq[jz - 1] &= 0x3fffff;
break;
} }
} }
if(ih==2) { }
if (ih == 2)
{
z = one - z; z = one - z;
if(carry!=0) z -= scalbn(one,q0); if (carry != 0)
{
z -= scalbn (one, q0);
}
} }
} }
/* check if recomputation is needed */ /* check if recomputation is needed */
if(z==zero) { if (z == zero)
{
j = 0; j = 0;
for (i=jz-1;i>=jk;i--) j |= iq[i]; for (i = jz - 1; i >= jk; i--)
if(j==0) { /* need recomputation */ {
for(k=1;iq[jk-k]==0;k++); /* k = no. of terms needed */ j |= iq[i];
}
if (j == 0) /* need recomputation */
{
for (k = 1; iq[jk - k] == 0; k++) /* k = no. of terms needed */
{
}
for(i=jz+1;i<=jz+k;i++) { /* add q[jz+1] to q[jz+k] */ for (i = jz + 1; i <= jz + k; i++) /* add q[jz + 1] to q[jz + k] */
{
f[jx + i] = (double) ipio2[jv + i]; f[jx + i] = (double) ipio2[jv + i];
for(j=0,fw=0.0;j<=jx;j++) fw += x[j]*f[jx+i-j]; for (j = 0, fw = 0.0; j <= jx; j++)
{
fw += x[j] * f[jx + i - j];
}
q[i] = fw; q[i] = fw;
} }
jz += k; jz += k;
@@ -260,75 +316,123 @@ recompute:
} }
/* chop off zero terms */ /* chop off zero terms */
if(z==0.0) { if (z == 0.0)
jz -= 1; q0 -= 24; {
while(iq[jz]==0) { jz--; q0-=24;} jz -= 1;
} else { /* break z into 24-bit if necessary */ q0 -= 24;
while (iq[jz] == 0)
{
jz--;
q0 -= 24;
}
}
else
{ /* break z into 24-bit if necessary */
z = scalbn (z, -q0); z = scalbn (z, -q0);
if(z>=two24) { if (z >= two24)
{
fw = (double) ((int) (twon24 * z)); fw = (double) ((int) (twon24 * z));
iq[jz] = (int) (z - two24 * fw); iq[jz] = (int) (z - two24 * fw);
jz += 1; q0 += 24; jz += 1;
q0 += 24;
iq[jz] = (int) fw; iq[jz] = (int) fw;
} else iq[jz] = (int) z ; }
else
{
iq[jz] = (int) z;
}
} }
/* convert integer "bit" chunk to floating-point value */ /* convert integer "bit" chunk to floating-point value */
fw = scalbn (one, q0); fw = scalbn (one, q0);
for(i=jz;i>=0;i--) { for (i = jz; i >= 0; i--)
q[i] = fw*(double)iq[i]; fw*=twon24; {
q[i] = fw * (double) iq[i];
fw *= twon24;
} }
/* compute PIo2[0, ..., jp] * q[jz, ..., 0] */ /* compute PIo2[0, ..., jp] * q[jz, ..., 0] */
for(i=jz;i>=0;i--) { for (i = jz; i >= 0; i--)
for(fw=0.0,k=0;k<=jp&&k<=jz-i;k++) fw += PIo2[k]*q[i+k]; {
for (fw = 0.0, k = 0; k <= jp && k <= jz - i; k++)
{
fw += PIo2[k] * q[i + k];
}
fq[jz - i] = fw; fq[jz - i] = fw;
} }
/* compress fq[] into y[] */ /* compress fq[] into y[] */
switch(prec) { switch (prec)
{
case 0: case 0:
{
fw = 0.0; fw = 0.0;
for (i=jz;i>=0;i--) fw += fq[i]; for (i = jz; i >= 0; i--)
{
fw += fq[i];
}
y[0] = (ih == 0) ? fw : -fw; y[0] = (ih == 0) ? fw : -fw;
break; break;
}
case 1: case 1:
case 2: case 2:
{
fw = 0.0; fw = 0.0;
for (i=jz;i>=0;i--) fw += fq[i]; for (i = jz; i >= 0; i--)
{
fw += fq[i];
}
y[0] = (ih == 0) ? fw : -fw; y[0] = (ih == 0) ? fw : -fw;
fw = fq[0] - fw; fw = fq[0] - fw;
for (i=1;i<=jz;i++) fw += fq[i]; for (i = 1; i <= jz; i++)
{
fw += fq[i];
}
y[1] = (ih == 0) ? fw : -fw; y[1] = (ih == 0) ? fw : -fw;
break; break;
}
case 3: /* painful */ case 3: /* painful */
for (i=jz;i>0;i--) { {
for (i = jz; i > 0; i--)
{
fw = fq[i - 1] + fq[i]; fw = fq[i - 1] + fq[i];
fq[i] += fq[i - 1] - fw; fq[i] += fq[i - 1] - fw;
fq[i - 1] = fw; fq[i - 1] = fw;
} }
for (i=jz;i>1;i--) { for (i = jz; i > 1; i--)
{
fw = fq[i - 1] + fq[i]; fw = fq[i - 1] + fq[i];
fq[i] += fq[i - 1] - fw; fq[i] += fq[i - 1] - fw;
fq[i - 1] = fw; fq[i - 1] = fw;
} }
for (fw=0.0,i=jz;i>=2;i--) fw += fq[i]; for (fw = 0.0, i = jz; i >= 2; i--)
if(ih==0) { {
y[0] = fq[0]; y[1] = fq[1]; y[2] = fw; fw += fq[i];
} else { }
y[0] = -fq[0]; y[1] = -fq[1]; y[2] = -fw; if (ih == 0)
{
y[0] = fq[0];
y[1] = fq[1];
y[2] = fw;
}
else
{
y[0] = -fq[0];
y[1] = -fq[1];
y[2] = -fw;
}
} }
} }
return n & 7; return n & 7;
} } /* __kernel_rem_pio2 */
/* __ieee754_rem_pio2(x,y) /* __ieee754_rem_pio2(x,y)
*
* return the remainder of x rem pi/2 in y[0]+y[1] * return the remainder of x rem pi/2 in y[0]+y[1]
* use __kernel_rem_pio2() * use __kernel_rem_pio2()
*/ */
static const int npio2_hw[] = { static const int npio2_hw[] =
{
0x3FF921FB, 0x400921FB, 0x4012D97C, 0x401921FB, 0x401F6A7A, 0x4022D97C, 0x3FF921FB, 0x400921FB, 0x4012D97C, 0x401921FB, 0x401F6A7A, 0x4022D97C,
0x4025FDBB, 0x402921FB, 0x402C463A, 0x402F6A7A, 0x4031475C, 0x4032D97C, 0x4025FDBB, 0x402921FB, 0x402C463A, 0x402F6A7A, 0x4031475C, 0x4032D97C,
0x40346B9C, 0x4035FDBB, 0x40378FDB, 0x403921FB, 0x403AB41B, 0x403C463A, 0x40346B9C, 0x4035FDBB, 0x40378FDB, 0x403921FB, 0x403AB41B, 0x403C463A,
@@ -354,7 +458,8 @@ static const int npio2_hw[] = {
#define pio2_3 2.02226624871116645580e-21 /* 0x3BA3198A, 0x2E000000 */ #define pio2_3 2.02226624871116645580e-21 /* 0x3BA3198A, 0x2E000000 */
#define pio2_3t 8.47842766036889956997e-32 /* 0x397B839A, 0x252049C1 */ #define pio2_3t 8.47842766036889956997e-32 /* 0x397B839A, 0x252049C1 */
static int __ieee754_rem_pio2(double x, double *y) static int
__ieee754_rem_pio2 (double x, double *y)
{ {
double z, w, t, r, fn; double z, w, t, r, fn;
double tx[3]; double tx[3];
@@ -363,25 +468,39 @@ static int __ieee754_rem_pio2(double x, double *y)
hx = __HI (x); /* high word of x */ hx = __HI (x); /* high word of x */
ix = hx & 0x7fffffff; ix = hx & 0x7fffffff;
if (ix <= 0x3fe921fb) /* |x| ~<= pi/4 , no need for reduction */ if (ix <= 0x3fe921fb) /* |x| ~<= pi/4 , no need for reduction */
{y[0] = x; y[1] = 0; return 0;} {
if(ix<0x4002d97c) { /* |x| < 3pi/4, special case with n=+-1 */ y[0] = x;
if(hx>0) { y[1] = 0;
return 0;
}
if (ix < 0x4002d97c) /* |x| < 3pi/4, special case with n = +-1 */
{
if (hx > 0)
{
z = x - pio2_1; z = x - pio2_1;
if(ix!=0x3ff921fb) { /* 33+53 bit pi is good enough */ if (ix != 0x3ff921fb) /* 33 + 53 bit pi is good enough */
{
y[0] = z - pio2_1t; y[0] = z - pio2_1t;
y[1] = (z - y[0]) - pio2_1t; y[1] = (z - y[0]) - pio2_1t;
} else { /* near pi/2, use 33+33+53 bit pi */ }
else /* near pi/2, use 33 + 33 + 53 bit pi */
{
z -= pio2_2; z -= pio2_2;
y[0] = z - pio2_2t; y[0] = z - pio2_2t;
y[1] = (z - y[0]) - pio2_2t; y[1] = (z - y[0]) - pio2_2t;
} }
return 1; return 1;
} else { /* negative x */ }
else /* negative x */
{
z = x + pio2_1; z = x + pio2_1;
if(ix!=0x3ff921fb) { /* 33+53 bit pi is good enough */ if (ix != 0x3ff921fb) /* 33 + 53 bit pi is good enough */
{
y[0] = z + pio2_1t; y[0] = z + pio2_1t;
y[1] = (z - y[0]) + pio2_1t; y[1] = (z - y[0]) + pio2_1t;
} else { /* near pi/2, use 33+33+53 bit pi */ }
else /* near pi/2, use 33 + 33 + 53 bit pi */
{
z += pio2_2; z += pio2_2;
y[0] = z + pio2_2t; y[0] = z + pio2_2t;
y[1] = (z - y[0]) + pio2_2t; y[1] = (z - y[0]) + pio2_2t;
@@ -389,27 +508,33 @@ static int __ieee754_rem_pio2(double x, double *y)
return -1; return -1;
} }
} }
if(ix<=0x413921fb) { /* |x| ~<= 2^19*(pi/2), medium size */ if (ix <= 0x413921fb) /* |x| ~<= 2^19 * (pi/2), medium size */
{
t = fabs (x); t = fabs (x);
n = (int) (t * invpio2 + half); n = (int) (t * invpio2 + half);
fn = (double) n; fn = (double) n;
r = t - fn * pio2_1; r = t - fn * pio2_1;
w = fn * pio2_1t; /* 1st round good to 85 bit */ w = fn * pio2_1t; /* 1st round good to 85 bit */
if(n<32&&ix!=npio2_hw[n-1]) { if (n < 32 && ix != npio2_hw[n - 1])
{
y[0] = r - w; /* quick check no cancellation */ y[0] = r - w; /* quick check no cancellation */
} else { }
else
{
j = ix >> 20; j = ix >> 20;
y[0] = r - w; y[0] = r - w;
i = j - (((__HI (y[0])) >> 20) & 0x7ff); i = j - (((__HI (y[0])) >> 20) & 0x7ff);
if(i>16) { /* 2nd iteration needed, good to 118 */ if (i > 16) /* 2nd iteration needed, good to 118 */
{
t = r; t = r;
w = fn * pio2_2; w = fn * pio2_2;
r = t - w; r = t - w;
w = fn * pio2_2t - ((t - r) - w); w = fn * pio2_2t - ((t - r) - w);
y[0] = r - w; y[0] = r - w;
i = j - (((__HI (y[0])) >> 20) & 0x7ff); i = j - (((__HI (y[0])) >> 20) & 0x7ff);
if(i>49) { /* 3rd iteration need, 151 bits acc */ if (i > 49) /* 3rd iteration need, 151 bits acc, will cover all possible cases */
t = r; /* will cover all possible cases */ {
t = r;
w = fn * pio2_3; w = fn * pio2_3;
r = t - w; r = t - w;
w = fn * pio2_3t - ((t - r) - w); w = fn * pio2_3t - ((t - r) - w);
@@ -418,30 +543,49 @@ static int __ieee754_rem_pio2(double x, double *y)
} }
} }
y[1] = (r - y[0]) - w; y[1] = (r - y[0]) - w;
if(hx<0) {y[0] = -y[0]; y[1] = -y[1]; return -n;} if (hx < 0)
else return n; {
y[0] = -y[0];
y[1] = -y[1];
return -n;
}
else
{
return n;
}
} }
/* /*
* all other (large) arguments * all other (large) arguments
*/ */
if(ix>=0x7ff00000) { /* x is inf or NaN */ if (ix >= 0x7ff00000) /* x is inf or NaN */
y[0]=y[1]=x-x; return 0; {
y[0] = y[1] = x - x;
return 0;
} }
/* set z = scalbn(|x|, ilogb(x) - 23) */ /* set z = scalbn(|x|, ilogb(x) - 23) */
__LO (z) = __LO (x); __LO (z) = __LO (x);
e0 = (ix >> 20) - 1046; /* e0 = ilogb(z) - 23; */ e0 = (ix >> 20) - 1046; /* e0 = ilogb(z) - 23; */
__HI (z) = ix - (e0 << 20); __HI (z) = ix - (e0 << 20);
for(i=0;i<2;i++) { for (i = 0; i < 2; i++)
{
tx[i] = (double) ((int) (z)); tx[i] = (double) ((int) (z));
z = (z - tx[i]) * two24; z = (z - tx[i]) * two24;
} }
tx[2] = z; tx[2] = z;
nx = 3; nx = 3;
while(tx[nx-1]==zero) nx--; /* skip zero term */ while (tx[nx - 1] == zero) /* skip zero term */
n = __kernel_rem_pio2(tx,y,e0,nx,2); {
if(hx<0) {y[0] = -y[0]; y[1] = -y[1]; return -n;} nx--;
return n;
} }
n = __kernel_rem_pio2 (tx, y, e0, nx, 2);
if (hx < 0)
{
y[0] = -y[0];
y[1] = -y[1];
return -n;
}
return n;
} /* __ieee754_rem_pio2 */
/* __kernel_sin( x, y, iy) /* __kernel_sin( x, y, iy)
* kernel sin function on [-pi/4, pi/4], pi/4 ~ 0.7854 * kernel sin function on [-pi/4, pi/4], pi/4 ~ 0.7854
@@ -478,19 +622,32 @@ static int __ieee754_rem_pio2(double x, double *y)
#define S5 -2.50507602534068634195e-08 /* 0xBE5AE5E6, 0x8A2B9CEB */ #define S5 -2.50507602534068634195e-08 /* 0xBE5AE5E6, 0x8A2B9CEB */
#define S6 1.58969099521155010221e-10 /* 0x3DE5D93A, 0x5ACFD57C */ #define S6 1.58969099521155010221e-10 /* 0x3DE5D93A, 0x5ACFD57C */
static double __kernel_sin(double x, double y, int iy) static double
__kernel_sin (double x, double y, int iy)
{ {
double z, r, v; double z, r, v;
int ix; int ix;
ix = __HI (x) & 0x7fffffff; /* high word of x */ ix = __HI (x) & 0x7fffffff; /* high word of x */
if (ix < 0x3e400000) /* |x| < 2**-27 */ if (ix < 0x3e400000) /* |x| < 2**-27 */
{if((int)x==0) return x;} /* generate inexact */ {
if ((int) x == 0)
{
return x; /* generate inexact */
}
}
z = x * x; z = x * x;
v = z * x; v = z * x;
r = S2 + z * (S3 + z * (S4 + z * (S5 + z * S6))); r = S2 + z * (S3 + z * (S4 + z * (S5 + z * S6)));
if(iy==0) return x+v*(S1+z*r); if (iy == 0)
else return x-((z*(half*y-v*r)-y)-v*S1); {
return x + v * (S1 + z * r);
} }
else
{
return x - ((z * (half * y - v * r) - y) - v * S1);
}
} /* __kernel_sin */
/* /*
* __kernel_cos( x, y ) * __kernel_cos( x, y )
@@ -534,22 +691,34 @@ static double __kernel_sin(double x, double y, int iy)
#define C5 2.08757232129817482790e-09 /* 0x3E21EE9E, 0xBDB4B1C4 */ #define C5 2.08757232129817482790e-09 /* 0x3E21EE9E, 0xBDB4B1C4 */
#define C6 -1.13596475577881948265e-11 /* 0xBDA8FAE9, 0xBE8838D4 */ #define C6 -1.13596475577881948265e-11 /* 0xBDA8FAE9, 0xBE8838D4 */
static double __kernel_cos(double x, double y) static double
__kernel_cos (double x, double y)
{ {
double a, hz, z, r, qx; double a, hz, z, r, qx;
int ix; int ix;
ix = __HI (x) & 0x7fffffff; /* ix = |x|'s high word */ ix = __HI (x) & 0x7fffffff; /* ix = |x|'s high word */
if(ix<0x3e400000) { /* if x < 2**27 */ if (ix < 0x3e400000) /* if x < 2**27 */
if(((int)x)==0) return one; /* generate inexact */ {
if (((int) x) == 0)
{
return one; /* generate inexact */
}
} }
z = x * x; z = x * x;
r = z * (C1 + z * (C2 + z * (C3 + z * (C4 + z * (C5 + z * C6))))); r = z * (C1 + z * (C2 + z * (C3 + z * (C4 + z * (C5 + z * C6)))));
if (ix < 0x3FD33333) /* if |x| < 0.3 */ if (ix < 0x3FD33333) /* if |x| < 0.3 */
{
return one - (0.5 * z - (z * r - x * y)); return one - (0.5 * z - (z * r - x * y));
else { }
if(ix > 0x3fe90000) { /* x > 0.78125 */ else
{
if (ix > 0x3fe90000) /* x > 0.78125 */
{
qx = 0.28125; qx = 0.28125;
} else { }
else
{
__HI (qx) = ix - 0x00200000; /* x / 4 */ __HI (qx) = ix - 0x00200000; /* x / 4 */
__LO (qx) = 0; __LO (qx) = 0;
} }
@@ -557,7 +726,7 @@ static double __kernel_cos(double x, double y)
a = one - qx; a = one - qx;
return a - (hz - (z * r - x * y)); return a - (hz - (z * r - x * y));
} }
} } /* __kernel_cos */
/* __kernel_tan( x, y, k ) /* __kernel_tan( x, y, k )
* kernel tan function on [-pi/4, pi/4], pi/4 ~ 0.7854 * kernel tan function on [-pi/4, pi/4], pi/4 ~ 0.7854
@@ -608,21 +777,30 @@ static double __kernel_cos(double x, double y)
#define pio4 7.85398163397448278999e-01 /* 3FE921FB, 54442D18 */ #define pio4 7.85398163397448278999e-01 /* 3FE921FB, 54442D18 */
#define pio4lo 3.06161699786838301793e-17 /* 3C81A626, 33145C07 */ #define pio4lo 3.06161699786838301793e-17 /* 3C81A626, 33145C07 */
static double __kernel_tan(double x, double y, int iy) static double
__kernel_tan (double x, double y, int iy)
{ {
double z, r, v, w, s; double z, r, v, w, s;
int ix, hx; int ix, hx;
hx = __HI (x); /* high word of x */ hx = __HI (x); /* high word of x */
ix = hx & 0x7fffffff; /* high word of |x| */ ix = hx & 0x7fffffff; /* high word of |x| */
if (ix < 0x3e300000) { /* x < 2**-28 */ if (ix < 0x3e300000) /* x < 2**-28 */
if ((int) x == 0) { /* generate inexact */ {
if ((int) x == 0) /* generate inexact */
{
if (((ix | __LO (x)) | (iy + 1)) == 0) if (((ix | __LO (x)) | (iy + 1)) == 0)
{
return one / fabs (x); return one / fabs (x);
else { }
else
{
if (iy == 1) if (iy == 1)
{
return x; return x;
else { /* compute -1 / (x+y) carefully */ }
else /* compute -1 / (x + y) carefully */
{
double a, t; double a, t;
z = w = x + y; z = w = x + y;
@@ -636,8 +814,10 @@ static double __kernel_tan(double x, double y, int iy)
} }
} }
} }
if (ix >= 0x3FE59428) { /* |x| >= 0.6744 */ if (ix >= 0x3FE59428) /* |x| >= 0.6744 */
if (hx < 0) { {
if (hx < 0)
{
x = -x; x = -x;
y = -y; y = -y;
} }
@@ -653,28 +833,30 @@ static double __kernel_tan(double x, double y, int iy)
* x^5 (T[1] + x^4 * T[3] + ... + x^20 * T[11]) + * x^5 (T[1] + x^4 * T[3] + ... + x^20 * T[11]) +
* x^5 (x^2 * (T[2] + x^4 * T[4] + ... + x^22 * [T12])) * x^5 (x^2 * (T[2] + x^4 * T[4] + ... + x^22 * [T12]))
*/ */
r = T1 + w * (T3 + w * (T5 + w * (T7 + w * (T9 + r = T1 + w * (T3 + w * (T5 + w * (T7 + w * (T9 + w * T11))));
w * T11)))); v = z * (T2 + w * (T4 + w * (T6 + w * (T8 + w * (T10 + w * T12)))));
v = z * (T2 + w * (T4 + w * (T6 + w * (T8 + w * (T10 +
w * T12)))));
s = z * x; s = z * x;
r = y + z * (s * (r + v) + y); r = y + z * (s * (r + v) + y);
r += T0 * s; r += T0 * s;
w = x + r; w = x + r;
if (ix >= 0x3FE59428) { if (ix >= 0x3FE59428)
{
v = (double) iy; v = (double) iy;
return (double) (1 - ((hx >> 30) & 2)) * return (double) (1 - ((hx >> 30) & 2)) * (v - 2.0 * (x - (w * w / (w + v) - r)));
(v - 2.0 * (x - (w * w / (w + v) - r)));
} }
if (iy == 1) if (iy == 1)
{
return w; return w;
else { }
else
{
/* /*
* if allow error up to 2 ulp, simply return * if allow error up to 2 ulp, simply return
* -1.0 / (x + r) here * -1.0 / (x + r) here
*/ */
/* compute -1.0 / (x + r) accurately */ /* compute -1.0 / (x + r) accurately */
double a, t; double a, t;
z = w; z = w;
__LO (z) = 0; __LO (z) = 0;
v = r - (z - x); /* z + v = r + x */ v = r - (z - x); /* z + v = r + x */
@@ -683,9 +865,9 @@ static double __kernel_tan(double x, double y, int iy)
s = 1.0 + t * z; s = 1.0 + t * z;
return t + a * (s + t * v); return t + a * (s + t * v);
} }
} } /* __kernel_tan */
/* Method. /* Method:
* Let S,C and T denote the sin, cos and tan respectively on * Let S,C and T denote the sin, cos and tan respectively on
* [-PI/4, +PI/4]. Reduce the argument x to y1+y2 = x-k*pi/2 * [-PI/4, +PI/4]. Reduce the argument x to y1+y2 = x-k*pi/2
* in [-pi/4 , +pi/4], and let n = k mod 4. * in [-pi/4 , +pi/4], and let n = k mod 4.
@@ -716,7 +898,8 @@ static double __kernel_tan(double x, double y, int iy)
* __kernel_cos ... cose function on [-pi/4,pi/4] * __kernel_cos ... cose function on [-pi/4,pi/4]
* __ieee754_rem_pio2 ... argument reduction routine * __ieee754_rem_pio2 ... argument reduction routine
*/ */
double sin(double x) double
sin (double x)
{ {
double y[2], z = 0.0; double y[2], z = 0.0;
int n, ix; int n, ix;
@@ -726,23 +909,42 @@ double sin(double x)
/* |x| ~< pi/4 */ /* |x| ~< pi/4 */
ix &= 0x7fffffff; ix &= 0x7fffffff;
if(ix <= 0x3fe921fb) return __kernel_sin(x,z,0); if (ix <= 0x3fe921fb)
{
return __kernel_sin (x, z, 0);
}
/* sin(Inf or NaN) is NaN */ /* sin(Inf or NaN) is NaN */
else if (ix>=0x7ff00000) return x-x; else if (ix >= 0x7ff00000)
{
return x - x;
}
/* argument reduction needed */ /* argument reduction needed */
else { else
{
n = __ieee754_rem_pio2 (x, y); n = __ieee754_rem_pio2 (x, y);
switch(n&3) { switch (n & 3)
case 0: return __kernel_sin(y[0],y[1],1); {
case 1: return __kernel_cos(y[0],y[1]); case 0:
case 2: return -__kernel_sin(y[0],y[1],1); {
return __kernel_sin (y[0], y[1], 1);
}
case 1:
{
return __kernel_cos (y[0], y[1]);
}
case 2:
{
return -__kernel_sin (y[0], y[1], 1);
}
default: default:
{
return -__kernel_cos (y[0], y[1]); return -__kernel_cos (y[0], y[1]);
} }
} }
} }
} /* sin */
/* cos(x) /* cos(x)
* Return cosine function of x. * Return cosine function of x.
@@ -753,7 +955,8 @@ double sin(double x)
* __ieee754_rem_pio2 ... argument reduction routine * __ieee754_rem_pio2 ... argument reduction routine
*/ */
double cos(double x) double
cos (double x)
{ {
double y[2], z = 0.0; double y[2], z = 0.0;
int n, ix; int n, ix;
@@ -763,23 +966,42 @@ double cos(double x)
/* |x| ~< pi/4 */ /* |x| ~< pi/4 */
ix &= 0x7fffffff; ix &= 0x7fffffff;
if(ix <= 0x3fe921fb) return __kernel_cos(x,z); if (ix <= 0x3fe921fb)
{
return __kernel_cos (x, z);
}
/* cos(Inf or NaN) is NaN */ /* cos(Inf or NaN) is NaN */
else if (ix>=0x7ff00000) return x-x; else if (ix >= 0x7ff00000)
{
return x - x;
}
/* argument reduction needed */ /* argument reduction needed */
else { else
{
n = __ieee754_rem_pio2 (x, y); n = __ieee754_rem_pio2 (x, y);
switch(n&3) { switch (n & 3)
case 0: return __kernel_cos(y[0],y[1]); {
case 1: return -__kernel_sin(y[0],y[1],1); case 0:
case 2: return -__kernel_cos(y[0],y[1]); {
return __kernel_cos (y[0], y[1]);
}
case 1:
{
return -__kernel_sin (y[0], y[1], 1);
}
case 2:
{
return -__kernel_cos (y[0], y[1]);
}
default: default:
{
return __kernel_sin (y[0], y[1], 1); return __kernel_sin (y[0], y[1], 1);
} }
} }
} }
} /* cos */
/* tan(x) /* tan(x)
* Return tangent function of x. * Return tangent function of x.
@@ -789,7 +1011,8 @@ double cos(double x)
* __ieee754_rem_pio2 ... argument reduction routine * __ieee754_rem_pio2 ... argument reduction routine
*/ */
double tan(double x) double
tan (double x)
{ {
double y[2], z = 0.0; double y[2], z = 0.0;
int n, ix; int n, ix;
@@ -799,15 +1022,21 @@ double tan(double x)
/* |x| ~< pi/4 */ /* |x| ~< pi/4 */
ix &= 0x7fffffff; ix &= 0x7fffffff;
if(ix <= 0x3fe921fb) return __kernel_tan(x,z,1); if (ix <= 0x3fe921fb)
{
return __kernel_tan (x, z, 1);
}
/* tan(Inf or NaN) is NaN */ /* tan(Inf or NaN) is NaN */
else if (ix>=0x7ff00000) return x-x; /* NaN */ else if (ix >= 0x7ff00000)
{
return x - x; /* NaN */
}
/* argument reduction needed */ /* argument reduction needed */
else { else
{
n = __ieee754_rem_pio2 (x, y); n = __ieee754_rem_pio2 (x, y);
return __kernel_tan(y[0],y[1],1-((n&1)<<1)); /* 1 -- n even return __kernel_tan (y[0], y[1], 1 - ((n & 1) << 1)); /* 1 -- n even, -1 -- n odd */
-1 -- n odd */
}
} }
} /* tan */