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.
This commit is contained in:
Rich Felker 2012-12-05 14:12:57 -05:00
parent b088f85582
commit 96b3ea53f9
1 changed files with 29 additions and 14 deletions

View File

@ -85,21 +85,36 @@ int __signbitl(long double);
#define isunordered(x,y) (isnan((x)) ? ((void)(y),1) : isnan((y))) #define isunordered(x,y) (isnan((x)) ? ((void)(y),1) : isnan((y)))
static __inline int __isrel(long double __x, long double __y, int __rel) #define __ISREL_DEF(rel, op, type) \
{ static __inline int __is##rel(type __x, type __y) \
if (isunordered(__x, __y)) return 0; { return !isunordered(__x,__y) && __x op __y; }
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) __ISREL_DEF(lessf, <, float)
#define islessequal(x,y) __isrel((x), (y), -1) __ISREL_DEF(less, <, double)
#define islessgreater(x,y) __isrel((x), (y), 0) __ISREL_DEF(lessl, <, long double)
#define isgreaterequal(x,y) __isrel((x), (y), 1) __ISREL_DEF(lessequalf, <=, float)
#define isgreater(x,y) __isrel((x), (y), 2) __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); double acos(double);
float acosf(float); float acosf(float);