From: Rich Felker Date: Wed, 5 Dec 2012 19:12:57 +0000 (-0500) Subject: fix inefficiency of math.h isless, etc. macros X-Git-Url: http://nsz.repo.hu/git/?p=musl;a=commitdiff_plain;h=96b3ea53f9ae365a82fb537d4fdac63c2082cc22 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. --- 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);