diff options
author | Rich Felker <dalias@aerifal.cx> | 2012-12-05 14:12:57 -0500 |
---|---|---|
committer | Rich Felker <dalias@aerifal.cx> | 2012-12-05 14:12:57 -0500 |
commit | 96b3ea53f9ae365a82fb537d4fdac63c2082cc22 (patch) | |
tree | ba9ec1722ce133321a917423c8d97a8bc61fe377 /include/math.h | |
parent | b088f85582d8787911ef48449574bed4131b44a2 (diff) | |
download | musl-96b3ea53f9ae365a82fb537d4fdac63c2082cc22.tar.gz musl-96b3ea53f9ae365a82fb537d4fdac63c2082cc22.tar.bz2 musl-96b3ea53f9ae365a82fb537d4fdac63c2082cc22.tar.xz musl-96b3ea53f9ae365a82fb537d4fdac63c2082cc22.zip |
fix inefficiency of math.h isless, etc. macros
previously, everything was going through an intermediate conversion to
long double, which caused the extern __fpclassifyl function to get
invoked, preventing virtually all optimizations of these operations.
with the new code, tests on constant float or double arguments compile
to a constant 0 or 1, and tests on non-constant expressions are
efficient. I may later add support for __builtin versions on compilers
that support them.
Diffstat (limited to 'include/math.h')
-rw-r--r-- | include/math.h | 45 |
1 files changed, 30 insertions, 15 deletions
diff --git a/include/math.h b/include/math.h index 90691401..b44738d7 100644 --- a/include/math.h +++ b/include/math.h @@ -85,21 +85,36 @@ int __signbitl(long double); #define isunordered(x,y) (isnan((x)) ? ((void)(y),1) : isnan((y))) -static __inline int __isrel(long double __x, long double __y, int __rel) -{ - if (isunordered(__x, __y)) return 0; - if (__rel==-2) return __x < __y; - if (__rel==2) return __x > __y; - if (__rel==-1) return __x <= __y; - if (__rel==1) return __x >= __y; - return __x != __y; -} - -#define isless(x,y) __isrel((x), (y), -2) -#define islessequal(x,y) __isrel((x), (y), -1) -#define islessgreater(x,y) __isrel((x), (y), 0) -#define isgreaterequal(x,y) __isrel((x), (y), 1) -#define isgreater(x,y) __isrel((x), (y), 2) +#define __ISREL_DEF(rel, op, type) \ +static __inline int __is##rel(type __x, type __y) \ +{ return !isunordered(__x,__y) && __x op __y; } + +__ISREL_DEF(lessf, <, float) +__ISREL_DEF(less, <, double) +__ISREL_DEF(lessl, <, long double) +__ISREL_DEF(lessequalf, <=, float) +__ISREL_DEF(lessequal, <=, double) +__ISREL_DEF(lessequall, <=, long double) +__ISREL_DEF(lessgreaterf, !=, float) +__ISREL_DEF(lessgreater, !=, double) +__ISREL_DEF(lessgreaterl, !=, long double) +__ISREL_DEF(greaterf, >, float) +__ISREL_DEF(greater, >, double) +__ISREL_DEF(greaterl, >, long double) +__ISREL_DEF(greaterequalf, >=, float) +__ISREL_DEF(greaterequal, >=, double) +__ISREL_DEF(greaterequall, >=, long double) + +#define __tg_pred_2(x, y, p) ( \ + sizeof((x)+(y)) == sizeof(float) ? p##f(x, y) : \ + sizeof((x)+(y)) == sizeof(double) ? p(x, y) : \ + p##l(x, y) ) + +#define isless(x, y) __tg_pred_2(x, y, __isless) +#define islessequal(x, y) __tg_pred_2(x, y, __islessequal) +#define islessgreater(x, y) __tg_pred_2(x, y, __islessgreater) +#define isgreater(x, y) __tg_pred_2(x, y, __isgreater) +#define isgreaterequal(x, y) __tg_pred_2(x, y, __isgreaterequal) double acos(double); float acosf(float); |