Skip to content

Commit

Permalink
Remove various ABS macros and replace uses with fabs (or in one case …
Browse files Browse the repository at this point in the history
…abs)

which is more efficient on all targets.
  • Loading branch information
Wilco Dijkstra committed May 15, 2015
1 parent fbc68f0 commit 0e9be4d
Show file tree
Hide file tree
Showing 15 changed files with 116 additions and 88 deletions.
24 changes: 24 additions & 0 deletions ChangeLog
Original file line number Diff line number Diff line change
@@ -1,3 +1,27 @@
2015-05-15 Wilco Dijkstra <wdijkstr@arm.com>

* stdio-common/printf_fp.c (___printf_fp): Use abs.
* stdlib/gmp-impl.h (ABS): Remove define. (ABSIZ): Remove.
* sysdeps/ieee754/dbl-64/branred.c (__branred): Use fabs.
* sysdeps/ieee754/dbl-64/dla.h (EADD): Use fabs.
(ESUB): Use fabs. (ADD2): Use fabs. (SUB2): Use fabs.
(ADD2A): Use fabs. (SUB2A): Use fabs.
* sysdeps/ieee754/dbl-64/e_asin.c (__ieee754_asin): Use fabs.
* sysdeps/ieee754/dbl-64/e_log.c (__ieee754_log): Use fabs.
* sysdeps/ieee754/dbl-64/e_pow.c (__ieee754_pow): Use fabs.
(log1): Use fabs. (my_log2): Use fabs.
* sysdeps/ieee754/dbl-64/e_remainder.c (__ieee754_remainder): Use fabs.
* sysdeps/ieee754/dbl-64/mpa.h (ABS): Remove define.
* sysdeps/ieee754/dbl-64/mpatan.c (__mpatan): Use fabs.
* sysdeps/ieee754/dbl-64/mydefs.h (ABS): Remove define.
* sysdeps/ieee754/dbl-64/s_sin.c (__sin): Use fabs.
(__cos): Use fabs. (slow): Use fabs. (slow2): Use fabs.
(sloww): Use fabs. (sloww1): Use fabs. (sloww2): Use fabs.
(bslow1): Use fabs. (bslow2): Use fabs. (cslow2): Use fabs.
(csloww): Use fabs. (csloww1): Use fabs. (csloww2): Use fabs.
* sysdeps/ieee754/dbl-64/sincos32.c (__mpranred): Use fabs.
* sysdeps/x86_64/fpu/multiarch/e_log.c: add math.h include.

2015-05-15 Joseph Myers <joseph@codesourcery.com>

[BZ #18217]
Expand Down
2 changes: 1 addition & 1 deletion stdio-common/printf_fp.c
Original file line number Diff line number Diff line change
Expand Up @@ -449,7 +449,7 @@ ___printf_fp (FILE *fp,
efficient to use variables of the fixed maximum size but because this
would be really big it could lead to memory problems. */
{
mp_size_t bignum_size = ((ABS (p.exponent) + BITS_PER_MP_LIMB - 1)
mp_size_t bignum_size = ((abs (p.exponent) + BITS_PER_MP_LIMB - 1)
/ BITS_PER_MP_LIMB
+ (LDBL_MANT_DIG / BITS_PER_MP_LIMB > 2 ? 8 : 4))
* sizeof (mp_limb_t);
Expand Down
2 changes: 0 additions & 2 deletions stdlib/gmp-impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,6 @@ along with the GNU MP Library; see the file COPYING.LIB. If not, see
#define inline /* Empty */
#endif

#define ABS(x) (x >= 0 ? x : -x)
#ifndef MIN
#define MIN(l,o) ((l) < (o) ? (l) : (o))
#endif
Expand All @@ -74,7 +73,6 @@ along with the GNU MP Library; see the file COPYING.LIB. If not, see

/* Field access macros. */
#define SIZ(x) ((x)->_mp_size)
#define ABSIZ(x) ABS (SIZ (x))
#define PTR(x) ((x)->_mp_d)
#define EXP(x) ((x)->_mp_exp)
#define PREC(x) ((x)->_mp_prec)
Expand Down
3 changes: 2 additions & 1 deletion sysdeps/ieee754/dbl-64/branred.c
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
#include "endian.h"
#include "mydefs.h"
#include "branred.h"
#include <math.h>
#include <math_private.h>

#ifndef SECTION
Expand Down Expand Up @@ -123,7 +124,7 @@ __branred(double x, double *a, double *aa)

sum=sum1+sum2;
b=b1+b2;
bb = (ABS(b1)>ABS(b2))? (b1-b)+b2 : (b2-b)+b1;
bb = (fabs(b1)>fabs(b2))? (b1-b)+b2 : (b2-b)+b1;
if (b > 0.5)
{b-=1.0; sum+=1.0;}
else if (b < -0.5)
Expand Down
26 changes: 14 additions & 12 deletions sysdeps/ieee754/dbl-64/dla.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
* along with this program; if not, see <http://www.gnu.org/licenses/>.
*/

#include <math.h>

/***********************************************************************/
/*MODULE_NAME: dla.h */
/* */
Expand Down Expand Up @@ -44,15 +46,15 @@
/* z+zz = x+y exactly. */

#define EADD(x,y,z,zz) \
z=(x)+(y); zz=(ABS(x)>ABS(y)) ? (((x)-(z))+(y)) : (((y)-(z))+(x));
z=(x)+(y); zz=(fabs(x)>fabs(y)) ? (((x)-(z))+(y)) : (((y)-(z))+(x));


/* Exact subtraction of two single-length floating point numbers, Dekker. */
/* The macro produces a double-length number (z,zz) that satisfies */
/* z+zz = x-y exactly. */

#define ESUB(x,y,z,zz) \
z=(x)-(y); zz=(ABS(x)>ABS(y)) ? (((x)-(z))-(y)) : ((x)-((y)+(z)));
z=(x)-(y); zz=(fabs(x)>fabs(y)) ? (((x)-(z))-(y)) : ((x)-((y)+(z)));


/* Exact multiplication of two single-length floating point numbers, */
Expand Down Expand Up @@ -94,7 +96,7 @@
/* storage variables of type double. */

#define ADD2(x, xx, y, yy, z, zz, r, s) \
r = (x) + (y); s = (ABS (x) > ABS (y)) ? \
r = (x) + (y); s = (fabs (x) > fabs (y)) ? \
(((((x) - r) + (y)) + (yy)) + (xx)) : \
(((((y) - r) + (x)) + (xx)) + (yy)); \
z = r + s; zz = (r - z) + s;
Expand All @@ -107,7 +109,7 @@
/* storage variables of type double. */

#define SUB2(x, xx, y, yy, z, zz, r, s) \
r = (x) - (y); s = (ABS (x) > ABS (y)) ? \
r = (x) - (y); s = (fabs (x) > fabs (y)) ? \
(((((x) - r) - (y)) - (yy)) + (xx)) : \
((((x) - ((y) + r)) + (xx)) - (yy)); \
z = r + s; zz = (r - z) + s;
Expand Down Expand Up @@ -144,16 +146,16 @@

#define ADD2A(x, xx, y, yy, z, zz, r, rr, s, ss, u, uu, w) \
r = (x) + (y); \
if (ABS (x) > ABS (y)) { rr = ((x) - r) + (y); s = (rr + (yy)) + (xx); } \
if (fabs (x) > fabs (y)) { rr = ((x) - r) + (y); s = (rr + (yy)) + (xx); } \
else { rr = ((y) - r) + (x); s = (rr + (xx)) + (yy); } \
if (rr != 0.0) { \
z = r + s; zz = (r - z) + s; } \
else { \
ss = (ABS (xx) > ABS (yy)) ? (((xx) - s) + (yy)) : (((yy) - s) + (xx));\
ss = (fabs (xx) > fabs (yy)) ? (((xx) - s) + (yy)) : (((yy) - s) + (xx));\
u = r + s; \
uu = (ABS (r) > ABS (s)) ? ((r - u) + s) : ((s - u) + r); \
uu = (fabs (r) > fabs (s)) ? ((r - u) + s) : ((s - u) + r); \
w = uu + ss; z = u + w; \
zz = (ABS (u) > ABS (w)) ? ((u - z) + w) : ((w - z) + u); }
zz = (fabs (u) > fabs (w)) ? ((u - z) + w) : ((w - z) + u); }


/* Double-length subtraction, slower but more accurate than SUB2. */
Expand All @@ -165,13 +167,13 @@

#define SUB2A(x, xx, y, yy, z, zz, r, rr, s, ss, u, uu, w) \
r = (x) - (y); \
if (ABS (x) > ABS (y)) { rr = ((x) - r) - (y); s = (rr - (yy)) + (xx); } \
if (fabs (x) > fabs (y)) { rr = ((x) - r) - (y); s = (rr - (yy)) + (xx); } \
else { rr = (x) - ((y) + r); s = (rr + (xx)) - (yy); } \
if (rr != 0.0) { \
z = r + s; zz = (r - z) + s; } \
else { \
ss = (ABS (xx) > ABS (yy)) ? (((xx) - s) - (yy)) : ((xx) - ((yy) + s)); \
ss = (fabs (xx) > fabs (yy)) ? (((xx) - s) - (yy)) : ((xx) - ((yy) + s)); \
u = r + s; \
uu = (ABS (r) > ABS (s)) ? ((r - u) + s) : ((s - u) + r); \
uu = (fabs (r) > fabs (s)) ? ((r - u) + s) : ((s - u) + r); \
w = uu + ss; z = u + w; \
zz = (ABS (u) > ABS (w)) ? ((u - z) + w) : ((w - z) + u); }
zz = (fabs (u) > fabs (w)) ? ((u - z) + w) : ((w - z) + u); }
28 changes: 14 additions & 14 deletions sysdeps/ieee754/dbl-64/e_asin.c
Original file line number Diff line number Diff line change
Expand Up @@ -104,9 +104,9 @@ __ieee754_asin(double x){
__doasin(x,0,w);
if (w[0]==(w[0]+1.00000001*w[1])) return w[0];
else {
y=ABS(x);
res=ABS(w[0]);
res1=ABS(w[0]+1.1*w[1]);
y=fabs(x);
res=fabs(w[0]);
res1=fabs(w[0]+1.1*w[1]);
return (m>0)?__sin32(y,res,res1):-__sin32(y,res,res1);
}
}
Expand Down Expand Up @@ -135,11 +135,11 @@ __ieee754_asin(double x){
res1=res+1.1*cor;
z=0.5*(res1-res);
__dubsin(res,z,w);
z=(w[0]-ABS(x))+w[1];
z=(w[0]-fabs(x))+w[1];
if (z>1.0e-27) return (m>0)?min(res,res1):-min(res,res1);
else if (z<-1.0e-27) return (m>0)?max(res,res1):-max(res,res1);
else {
y=ABS(x);
y=fabs(x);
return (m>0)?__sin32(y,res,res1):-__sin32(y,res,res1);
}
}
Expand Down Expand Up @@ -168,11 +168,11 @@ __ieee754_asin(double x){
res1=res+1.1*cor;
z=0.5*(res1-res);
__dubsin(res,z,w);
z=(w[0]-ABS(x))+w[1];
z=(w[0]-fabs(x))+w[1];
if (z>1.0e-27) return (m>0)?min(res,res1):-min(res,res1);
else if (z<-1.0e-27) return (m>0)?max(res,res1):-max(res,res1);
else {
y=ABS(x);
y=fabs(x);
return (m>0)?__sin32(y,res,res1):-__sin32(y,res,res1);
}
}
Expand Down Expand Up @@ -203,11 +203,11 @@ __ieee754_asin(double x){
y=hp0.x-res;
z=((hp0.x-y)-res)+(hp1.x-z);
__dubcos(y,z,w);
z=(w[0]-ABS(x))+w[1];
z=(w[0]-fabs(x))+w[1];
if (z>1.0e-27) return (m>0)?min(res,res1):-min(res,res1);
else if (z<-1.0e-27) return (m>0)?max(res,res1):-max(res,res1);
else {
y=ABS(x);
y=fabs(x);
return (m>0)?__sin32(y,res,res1):-__sin32(y,res,res1);
}
}
Expand Down Expand Up @@ -241,11 +241,11 @@ __ieee754_asin(double x){
z=y+hp1.x;
y=(y-z)+hp1.x;
__dubcos(z,y,w);
z=(w[0]-ABS(x))+w[1];
z=(w[0]-fabs(x))+w[1];
if (z>1.0e-27) return (m>0)?min(res,res1):-min(res,res1);
else if (z<-1.0e-27) return (m>0)?max(res,res1):-max(res,res1);
else {
y=ABS(x);
y=fabs(x);
return (m>0)?__sin32(y,res,res1):-__sin32(y,res,res1);
}
}
Expand Down Expand Up @@ -280,11 +280,11 @@ __ieee754_asin(double x){
z=y+hp1.x;
y=(y-z)+hp1.x;
__dubcos(z,y,w);
z=(w[0]-ABS(x))+w[1];
z=(w[0]-fabs(x))+w[1];
if (z>1.0e-27) return (m>0)?min(res,res1):-min(res,res1);
else if (z<-1.0e-27) return (m>0)?max(res,res1):-max(res,res1);
else {
y=ABS(x);
y=fabs(x);
return (m>0)?__sin32(y,res,res1):-__sin32(y,res,res1);
}
}
Expand Down Expand Up @@ -318,7 +318,7 @@ __ieee754_asin(double x){
cor = (res1-res)+cor;
if (res==(res+1.0000001*cor)) return (m>0)?res:-res;
else {
y=ABS(x);
y=fabs(x);
res1=res+1.1*cor;
return (m>0)?__sin32(y,res,res1):-__sin32(y,res,res1);
}
Expand Down
3 changes: 2 additions & 1 deletion sysdeps/ieee754/dbl-64/e_log.c
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
#include <dla.h>
#include "mpa.h"
#include "MathLib.h"
#include <math.h>
#include <math_private.h>
#include <stap-probe.h>

Expand Down Expand Up @@ -93,7 +94,7 @@ __ieee754_log (double x)
/* Regular values of x */

w = x - 1;
if (__glibc_likely (ABS (w) > U03))
if (__glibc_likely (fabs (w) > U03))
goto case_03;

/* log (1) is +0 in all rounding modes. */
Expand Down
12 changes: 6 additions & 6 deletions sysdeps/ieee754/dbl-64/e_pow.c
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ __ieee754_pow (double x, double y)

/* Avoid internal underflow for tiny y. The exact value of y does
not matter if |y| <= 2**-64. */
if (ABS (y) < 0x1p-64)
if (fabs (y) < 0x1p-64)
y = y < 0 ? -0x1p-64 : 0x1p-64;
z = log1 (x, &aa, &error); /* x^y =e^(y log (X)) */
t = y * CN;
Expand All @@ -110,7 +110,7 @@ __ieee754_pow (double x, double y)
aa = y2 * a1 + y * a2;
a1 = a + aa;
a2 = (a - a1) + aa;
error = error * ABS (y);
error = error * fabs (y);
t = __exp1 (a1, a2, 1.9e16 * error); /* return -10 or 0 if wasn't computed exactly */
retval = (t > 0) ? t : power1 (x, y);
}
Expand All @@ -127,7 +127,7 @@ __ieee754_pow (double x, double y)
if (((v.i[HIGH_HALF] & 0x7fffffff) == 0x7ff00000 && v.i[LOW_HALF] != 0)
|| (v.i[HIGH_HALF] & 0x7fffffff) > 0x7ff00000) /* NaN */
return y;
if (ABS (y) > 1.0e20)
if (fabs (y) > 1.0e20)
return (y > 0) ? 0 : 1.0 / 0.0;
k = checkint (y);
if (k == -1)
Expand Down Expand Up @@ -232,7 +232,7 @@ power1 (double x, double y)
aa = ((y1 * a1 - a) + y1 * a2 + y2 * a1) + y2 * a2 + aa * y;
a1 = a + aa;
a2 = (a - a1) + aa;
error = error * ABS (y);
error = error * fabs (y);
t = __exp1 (a1, a2, 1.9e16 * error);
return (t >= 0) ? t : __slowpow (x, y, z);
}
Expand Down Expand Up @@ -292,7 +292,7 @@ log1 (double x, double *delta, double *error)
* (r7 + t * r8)))))
- 0.5 * t2 * (t + t1));
res = e1 + e2;
*error = 1.0e-21 * ABS (t);
*error = 1.0e-21 * fabs (t);
*delta = (e1 - res) + e2;
return res;
} /* |x-1| < 1.5*2**-10 */
Expand Down Expand Up @@ -398,7 +398,7 @@ my_log2 (double x, double *delta, double *error)
e2 = ((((t - e1) + z) + zz) + t * t * t
* (ss3 + t * (s4 + t * (s5 + t * (s6 + t * (s7 + t * s8))))));
res = e1 + e2;
*error = 1.0e-25 * ABS (t);
*error = 1.0e-25 * fabs (t);
*delta = (e1 - res) + e2;
return res;
}
Expand Down
15 changes: 8 additions & 7 deletions sysdeps/ieee754/dbl-64/e_remainder.c
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
#include "mydefs.h"
#include "urem.h"
#include "MathLib.h"
#include <math.h>
#include <math_private.h>

/**************************************************************************/
Expand Down Expand Up @@ -66,7 +67,7 @@ __ieee754_remainder (double x, double y)
return (xx != 0) ? xx : ((x > 0) ? ZERO.x : nZERO.x);
else
{
if (ABS (xx) > 0.5 * t.x)
if (fabs (xx) > 0.5 * t.x)
return (z > d) ? xx - t.x : xx + t.x;
else
return xx;
Expand Down Expand Up @@ -98,10 +99,10 @@ __ieee754_remainder (double x, double y)
z = u.x * r.x;
d = (z + big.x) - big.x;
u.x = (u.x - d * w.x) - d * ww.x;
if (ABS (u.x) < 0.5 * t.x)
if (fabs (u.x) < 0.5 * t.x)
return (u.x != 0) ? u.x : ((x > 0) ? ZERO.x : nZERO.x);
else
if (ABS (u.x) > 0.5 * t.x)
if (fabs (u.x) > 0.5 * t.x)
return (d > z) ? u.x + t.x : u.x - t.x;
else
{
Expand All @@ -114,7 +115,7 @@ __ieee754_remainder (double x, double y)
{
if (kx < 0x7fe00000 && ky < 0x7ff00000 && (ky > 0 || t.i[LOW_HALF] != 0))
{
y = ABS (y) * t128.x;
y = fabs (y) * t128.x;
z = __ieee754_remainder (x, y) * t128.x;
z = __ieee754_remainder (z, y) * tm128.x;
return z;
Expand All @@ -124,10 +125,10 @@ __ieee754_remainder (double x, double y)
if ((kx & 0x7ff00000) == 0x7fe00000 && ky < 0x7ff00000 &&
(ky > 0 || t.i[LOW_HALF] != 0))
{
y = ABS (y);
y = fabs (y);
z = 2.0 * __ieee754_remainder (0.5 * x, y);
d = ABS (z);
if (d <= ABS (d - y))
d = fabs (z);
if (d <= fabs (d - y))
return z;
else
return (z > 0) ? z - y : z + y;
Expand Down
2 changes: 0 additions & 2 deletions sysdeps/ieee754/dbl-64/mpa.h
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,6 @@ extern const mp_no __mptwo;
#define EY y->e
#define EZ z->e

#define ABS(x) ((x) < 0 ? -(x) : (x))

#ifndef RADIXI
# define RADIXI 0x1.0p-24 /* 2^-24 */
#endif
Expand Down
3 changes: 2 additions & 1 deletion sysdeps/ieee754/dbl-64/mpatan.c
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@

#include "endian.h"
#include "mpa.h"
#include <math.h>

#ifndef SECTION
# define SECTION
Expand Down Expand Up @@ -65,7 +66,7 @@ __mpatan (mp_no *x, mp_no *y, int p)
else
{
__mp_dbl (x, &dx, p);
dx = ABS (dx);
dx = fabs (dx);
for (m = 6; m > 0; m--)
{
if (dx > __atan_xm[m].d)
Expand Down
Loading

0 comments on commit 0e9be4d

Please sign in to comment.