math: use double_t for temporaries to avoid stores on i386
authorSzabolcs Nagy <nsz@port70.net>
Wed, 15 May 2013 23:08:52 +0000 (23:08 +0000)
committerSzabolcs Nagy <nsz@port70.net>
Wed, 15 May 2013 23:08:52 +0000 (23:08 +0000)
When FLT_EVAL_METHOD!=0 (only i386 with x87 fp) the excess
precision of an expression must be removed in an assignment.
(gcc needs -fexcess-precision=standard or -std=c99 for this)

This is done by extra load/store instructions which adds code
bloat when lot of temporaries are used and it makes the result
less precise in many cases.
Using double_t and float_t avoids these issues on i386 and
it makes no difference on other archs.

For now only a few functions are modified where the excess
precision is clearly beneficial (mostly polynomial evaluations
with temporaries).

object size differences on i386, gcc-4.8:
             old   new
__cosdf.o    123    95
__cos.o      199   169
__sindf.o    131    95
__sin.o      225   203
__tandf.o    207   151
__tan.o      605   499
erff.o      1470  1416
erf.o       1703  1649
j0f.o       1779  1745
j0.o        2308  2274
j1f.o       1602  1568
j1.o        2286  2252
tgamma.o    1431  1424
math/*.o   64164 63635

21 files changed:
src/math/__cos.c
src/math/__cosdf.c
src/math/__log1p.h
src/math/__log1pf.h
src/math/__sin.c
src/math/__sindf.c
src/math/__tan.c
src/math/__tandf.c
src/math/acos.c
src/math/acosf.c
src/math/asin.c
src/math/asinf.c
src/math/atan.c
src/math/atanf.c
src/math/erf.c
src/math/erff.c
src/math/j0.c
src/math/j0f.c
src/math/j1.c
src/math/j1f.c
src/math/tgamma.c

index 8699c1d..46cefb3 100644 (file)
@@ -60,7 +60,7 @@ C6  = -1.13596475577881948265e-11; /* 0xBDA8FAE9, 0xBE8838D4 */
 
 double __cos(double x, double y)
 {
-       double hz,z,r,w;
+       double_t hz,z,r,w;
 
        z  = x*x;
        w  = z*z;
index a65f7f2..2124989 100644 (file)
@@ -25,7 +25,7 @@ C3  =  0x199342e0ee5069.0p-68; /*  0.0000243904487962774090654 */
 
 float __cosdf(double x)
 {
-       double r, w, z;
+       double_t r, w, z;
 
        /* Try to optimize for parallel evaluation as in __tandf.c. */
        z = x*x;
index ec2c77b..5718711 100644 (file)
@@ -81,7 +81,7 @@ Lg7 = 1.479819860511658591e-01; /* 3FC2F112 DF3E5244 */
  */
 static inline double __log1p(double f)
 {
-       double hfsq,s,z,R,w,t1,t2;
+       double_t hfsq,s,z,R,w,t1,t2;
 
        s = f/(2.0+f);
        z = s*s;
index 99492c5..f2fbef2 100644 (file)
@@ -22,7 +22,7 @@ Lg4 = 0xf89e26.0p-26; /* 0.24279078841 */
 
 static inline float __log1pf(float f)
 {
-       float hfsq,s,z,R,w,t1,t2;
+       float_t hfsq,s,z,R,w,t1,t2;
 
        s = f/(2.0f + f);
        z = s*s;
index 9aead04..4030949 100644 (file)
@@ -51,7 +51,7 @@ S6  =  1.58969099521155010221e-10; /* 0x3DE5D93A, 0x5ACFD57C */
 
 double __sin(double x, double y, int iy)
 {
-       double z,r,v,w;
+       double_t z,r,v,w;
 
        z = x*x;
        w = z*z;
index 83c0d7a..8fec2a3 100644 (file)
@@ -25,7 +25,7 @@ S4 =  0x16cd878c3b46a7.0p-71; /*  0.0000027183114939898219064 */
 
 float __sindf(double x)
 {
-       double r, s, w, z;
+       double_t r, s, w, z;
 
        /* Try to optimize for parallel evaluation as in __tandf.c. */
        z = x*x;
index 01e3fe4..fc739f9 100644 (file)
@@ -65,7 +65,7 @@ pio4lo =     3.06161699786838301793e-17; /* 3C81A626, 33145C07 */
 
 double __tan(double x, double y, int iy)
 {
-       double z, r, v, w, s, sign;
+       double_t z, r, v, w, s, sign;
        int32_t ix, hx;
 
        GET_HIGH_WORD(hx,x);
@@ -106,7 +106,8 @@ double __tan(double x, double y, int iy)
                 * -1.0 / (x+r) here
                 */
                /* compute -1.0 / (x+r) accurately */
-               double a, t;
+               double_t a;
+               double z, t;
                z = w;
                SET_LOW_WORD(z,0);
                v = r - (z - x);        /* z+v = r+x */
index 36a8214..3e632fd 100644 (file)
@@ -27,7 +27,7 @@ static const double T[] = {
 
 float __tandf(double x, int iy)
 {
-       double z,r,w,s,t,u;
+       double_t z,r,w,s,t,u;
 
        z = x*x;
        /*
index cd5d06a..ea9c87b 100644 (file)
@@ -51,7 +51,7 @@ qS4 =  7.70381505559019352791e-02; /* 0x3FB3B8C5, 0xB12E9282 */
 
 static double R(double z)
 {
-       double p, q;
+       double_t p, q;
        p = z*(pS0+z*(pS1+z*(pS2+z*(pS3+z*(pS4+z*pS5)))));
        q = 1.0+z*(qS1+z*(qS2+z*(qS3+z*qS4)));
        return p/q;
index 5d7c027..8ee1a71 100644 (file)
@@ -25,7 +25,7 @@ qS1 = -7.0662963390e-01;
 
 static float R(float z)
 {
-       float p, q;
+       float_t p, q;
        p = z*(pS0+z*(pS1+z*pS2));
        q = 1.0f+z*qS1;
        return p/q;
index d61c04b..3e8f99e 100644 (file)
@@ -58,7 +58,7 @@ qS4 =  7.70381505559019352791e-02; /* 0x3FB3B8C5, 0xB12E9282 */
 
 static double R(double z)
 {
-       double p, q;
+       double_t p, q;
        p = z*(pS0+z*(pS1+z*(pS2+z*(pS3+z*(pS4+z*pS5)))));
        q = 1.0+z*(qS1+z*(qS2+z*(qS3+z*qS4)));
        return p/q;
index 462bf04..51fe6c6 100644 (file)
@@ -26,7 +26,7 @@ qS1 = -7.0662963390e-01;
 
 static float R(float z)
 {
-       float p, q;
+       float_t p, q;
        p = z*(pS0+z*(pS1+z*pS2));
        q = 1.0f+z*qS1;
        return p/q;
index 3c9a59f..5a1d33e 100644 (file)
@@ -62,7 +62,7 @@ static const double aT[] = {
 
 double atan(double x)
 {
-       double w,s1,s2,z;
+       double_t w,s1,s2,z;
        uint32_t ix,sign;
        int id;
 
index 4b59509..ac8bfd0 100644 (file)
@@ -40,7 +40,7 @@ static const float aT[] = {
 
 float atanf(float x)
 {
-       float w,s1,s2,z;
+       float_t w,s1,s2,z;
        uint32_t ix,sign;
        int id;
 
index c0fc41d..2f30a29 100644 (file)
@@ -176,7 +176,7 @@ sb7  = -2.24409524465858183362e+01; /* 0xC03670E2, 0x42712D62 */
 
 static double erfc1(double x)
 {
-       double s,P,Q;
+       double_t s,P,Q;
 
        s = fabs(x) - 1;
        P = pa0+s*(pa1+s*(pa2+s*(pa3+s*(pa4+s*(pa5+s*pa6)))));
@@ -186,7 +186,8 @@ static double erfc1(double x)
 
 static double erfc2(uint32_t ix, double x)
 {
-       double s,z,R,S;
+       double_t s,R,S;
+       double z;
 
        if (ix < 0x3ff40000)  /* |x| < 1.25 */
                return erfc1(x);
index e2cfc98..ed5f397 100644 (file)
@@ -86,7 +86,7 @@ sb7  = -2.2440952301e+01; /* 0xc1b38712 */
 
 static float erfc1(float x)
 {
-       float s,P,Q;
+       float_t s,P,Q;
 
        s = fabsf(x) - 1;
        P = pa0+s*(pa1+s*(pa2+s*(pa3+s*(pa4+s*(pa5+s*pa6)))));
@@ -96,7 +96,8 @@ static float erfc1(float x)
 
 static float erfc2(uint32_t ix, float x)
 {
-       float s,z,R,S;
+       float_t s,R,S;
+       float z;
 
        if (ix < 0x3fa00000)  /* |x| < 1.25 */
                return erfc1(x);
index b281e13..d722d94 100644 (file)
@@ -263,7 +263,7 @@ static const double pS2[5] = {
 static double pzero(double x)
 {
        const double *p,*q;
-       double z,r,s;
+       double_t z,r,s;
        uint32_t ix;
 
        GET_HIGH_WORD(ix, x);
@@ -359,7 +359,7 @@ static const double qS2[6] = {
 static double qzero(double x)
 {
        const double *p,*q;
-       double s,r,z;
+       double_t s,r,z;
        uint32_t ix;
 
        GET_HIGH_WORD(ix, x);
index 79bab62..4b0ee3b 100644 (file)
@@ -201,7 +201,7 @@ static const float pS2[5] = {
 static float pzerof(float x)
 {
        const float *p,*q;
-       float z,r,s;
+       float_t z,r,s;
        uint32_t ix;
 
        GET_FLOAT_WORD(ix, x);
@@ -297,7 +297,7 @@ static const float qS2[6] = {
 static float qzerof(float x)
 {
        const float *p,*q;
-       float s,r,z;
+       float_t s,r,z;
        uint32_t ix;
 
        GET_FLOAT_WORD(ix, x);
index ac7bb1e..df724d1 100644 (file)
@@ -250,7 +250,7 @@ static const double ps2[5] = {
 static double pone(double x)
 {
        const double *p,*q;
-       double z,r,s;
+       double_t z,r,s;
        uint32_t ix;
 
        GET_HIGH_WORD(ix, x);
@@ -346,7 +346,7 @@ static const double qs2[6] = {
 static double qone(double x)
 {
        const double *p,*q;
-       double  s,r,z;
+       double_t s,r,z;
        uint32_t ix;
 
        GET_HIGH_WORD(ix, x);
index 5a760f7..6abde34 100644 (file)
@@ -198,7 +198,7 @@ static const float ps2[5] = {
 static float ponef(float x)
 {
        const float *p,*q;
-       float z,r,s;
+       float_t z,r,s;
        uint32_t ix;
 
        GET_FLOAT_WORD(ix, x);
@@ -294,7 +294,7 @@ static const float qs2[6] = {
 static float qonef(float x)
 {
        const float *p,*q;
-       float s,r,z;
+       float_t s,r,z;
        uint32_t ix;
 
        GET_FLOAT_WORD(ix, x);
index a3f203c..691e86a 100644 (file)
@@ -89,7 +89,7 @@ static const double fact[] = {
 /* S(x) rational function for positive x */
 static double S(double x)
 {
-       double num = 0, den = 0;
+       double_t num = 0, den = 0;
        int i;
 
        /* to avoid overflow handle large x differently */