This is the mail archive of the
gcc-patches@gcc.gnu.org
mailing list for the GCC project.
[PATCH] middle-end: provide generic "isnormal" transformation
- From: "Kaveh R. GHAZI" <ghazi at caip dot rutgers dot edu>
- To: gcc-patches at gcc dot gnu dot org
- Date: Tue, 19 Jun 2007 00:40:56 -0400 (EDT)
- Subject: [PATCH] middle-end: provide generic "isnormal" transformation
This patch enables a generic transformation for builtin isnormal as
suggested by rth in PR target/30652. It can be overridden by a more
efficient optab.
Tested in conjunction with the two previous (isinf/isfinite) patches on
sparc-sun-solaris2.10, no regressions and the new cases pass.
(I will add docs for the new isnormal builtin once I get feedback on what
DEF_* macro to declare it with.)
Okay for mainline?
Thanks,
--Kaveh
2007-06-18 Kaveh R. Ghazi <ghazi@caip.rutgers.edu>
PR target/30652
* builtins.c (expand_builtin_interclass_mathfn): Provide a generic
transformation for builtin ISNORMAL.
(expand_builtin): Handle BUILT_IN_ISNORMAL.
* builtins.def (BUILT_IN_ISNORMAL): New.
testsuite:
* gcc.dg/pr28796-2.c: Add more cases.
diff -rup orig/egcc-SVN20070617/gcc/builtins.c egcc-SVN20070617/gcc/builtins.c
--- orig/egcc-SVN20070617/gcc/builtins.c 2007-06-18 10:52:07.435166987 -0400
+++ egcc-SVN20070617/gcc/builtins.c 2007-06-18 11:15:26.037178670 -0400
@@ -2226,6 +2226,7 @@ expand_builtin_interclass_mathfn (tree e
errno_set = true; builtin_optab = ilogb_optab; break;
CASE_FLT_FN (BUILT_IN_ISINF):
builtin_optab = isinf_optab; break;
+ case BUILT_IN_ISNORMAL:
case BUILT_IN_ISFINITE:
CASE_FLT_FN (BUILT_IN_FINITE):
/* These builtins have no optabs (yet). */
@@ -2312,6 +2313,28 @@ expand_builtin_interclass_mathfn (tree e
build_real (type, r));
return expand_expr (result, target, VOIDmode, EXPAND_NORMAL);
}
+ case BUILT_IN_ISNORMAL:
+ {
+ /* isnormal(x) -> isgreaterequal(fabs(x),DBL_MIN) &
+ islessequal(fabs(x),DBL_MAX). */
+ tree const isle_fn = built_in_decls[BUILT_IN_ISLESSEQUAL];
+ tree const isge_fn = built_in_decls[BUILT_IN_ISGREATEREQUAL];
+ tree const type = TREE_TYPE (arg);
+ REAL_VALUE_TYPE rmax, rmin;
+ char buf[128];
+
+ get_max_float (REAL_MODE_FORMAT (mode), buf, sizeof (buf));
+ real_from_string (&rmax, buf);
+ sprintf (buf, "0x1p%d", REAL_MODE_FORMAT (mode)->emin - 1);
+ real_from_string (&rmin, buf);
+ arg = builtin_save_expr (fold_build1 (ABS_EXPR, type, arg));
+ result = build_call_expr (isle_fn, 2, arg,
+ build_real (type, rmax));
+ result = fold_build2 (BIT_AND_EXPR, integer_type_node, result,
+ build_call_expr (isge_fn, 2, arg,
+ build_real (type, rmin)));
+ return expand_expr (result, target, VOIDmode, EXPAND_NORMAL);
+ }
default:
break;
}
@@ -6103,6 +6126,7 @@ expand_builtin (tree exp, rtx target, rt
CASE_FLT_FN (BUILT_IN_ISINF):
CASE_FLT_FN (BUILT_IN_FINITE):
case BUILT_IN_ISFINITE:
+ case BUILT_IN_ISNORMAL:
target = expand_builtin_interclass_mathfn (exp, target, subtarget);
if (target)
return target;
diff -rup orig/egcc-SVN20070617/gcc/builtins.def egcc-SVN20070617/gcc/builtins.def
--- orig/egcc-SVN20070617/gcc/builtins.def 2007-06-18 10:52:07.438180577 -0400
+++ egcc-SVN20070617/gcc/builtins.def 2007-06-18 11:03:33.978692385 -0400
@@ -666,6 +666,7 @@ DEF_EXT_LIB_BUILTIN (BUILT_IN_ISNANL,
DEF_EXT_LIB_BUILTIN (BUILT_IN_ISNAND32, "isnand32", BT_FN_INT_DFLOAT32, ATTR_CONST_NOTHROW_LIST)
DEF_EXT_LIB_BUILTIN (BUILT_IN_ISNAND64, "isnand64", BT_FN_INT_DFLOAT64, ATTR_CONST_NOTHROW_LIST)
DEF_EXT_LIB_BUILTIN (BUILT_IN_ISNAND128, "isnand128", BT_FN_INT_DFLOAT128, ATTR_CONST_NOTHROW_LIST)
+DEF_GCC_BUILTIN (BUILT_IN_ISNORMAL, "isnormal", BT_FN_INT_VAR, ATTR_CONST_NOTHROW_LIST)
DEF_GCC_BUILTIN (BUILT_IN_ISGREATER, "isgreater", BT_FN_INT_VAR, ATTR_CONST_NOTHROW_LIST)
DEF_GCC_BUILTIN (BUILT_IN_ISGREATEREQUAL, "isgreaterequal", BT_FN_INT_VAR, ATTR_CONST_NOTHROW_LIST)
DEF_GCC_BUILTIN (BUILT_IN_ISLESS, "isless", BT_FN_INT_VAR, ATTR_CONST_NOTHROW_LIST)
diff -rup orig/egcc-SVN20070617/gcc/testsuite/gcc.dg/pr28796-2.c egcc-SVN20070617/gcc/testsuite/gcc.dg/pr28796-2.c
--- orig/egcc-SVN20070617/gcc/testsuite/gcc.dg/pr28796-2.c 2007-06-18 10:52:07.440445390 -0400
+++ egcc-SVN20070617/gcc/testsuite/gcc.dg/pr28796-2.c 2007-06-18 11:23:49.708621620 -0400
@@ -6,7 +6,8 @@ extern void abort (void);
void __attribute__ ((__noinline__))
foo_1 (float f, double d, long double ld,
- int res_unord, int res_isnan, int res_isinf, int res_isfin)
+ int res_unord, int res_isnan, int res_isinf,
+ int res_isfin, int res_isnorm)
{
if (__builtin_isunordered (f, 0) != res_unord)
abort ();
@@ -43,6 +44,13 @@ foo_1 (float f, double d, long double ld
if (__builtin_isinfl (ld) != res_isinf)
abort ();
+ if (__builtin_isnormal (f) != res_isnorm)
+ abort ();
+ if (__builtin_isnormal (d) != res_isnorm)
+ abort ();
+ if (__builtin_isnormal (ld) != res_isnorm)
+ abort ();
+
if (__builtin_isfinite (f) != res_isfin)
abort ();
if (__builtin_isfinite (d) != res_isfin)
@@ -64,10 +72,12 @@ foo_1 (float f, double d, long double ld
void __attribute__ ((__noinline__))
foo (float f, double d, long double ld,
- int res_unord, int res_isnan, int res_isinf, int res_isfin)
+ int res_unord, int res_isnan, int res_isinf,
+ int res_isfin, int res_isnorm)
{
- foo_1 (f, d, ld, res_unord, res_isnan, res_isinf, res_isfin);
- foo_1 (-f, -d, -ld, res_unord, res_isnan, res_isinf, res_isfin);
+ foo_1 (f, d, ld, res_unord, res_isnan, res_isinf, res_isfin, res_isnorm);
+ /* Try all values negative as well. */
+ foo_1 (-f, -d, -ld, res_unord, res_isnan, res_isinf, res_isfin, res_isnorm);
}
int main()
@@ -77,19 +87,28 @@ int main()
long double ld;
f = __builtin_nanf(""); d = __builtin_nan(""); ld = __builtin_nanl("");
- foo(f, d, ld, /*unord=*/ 1, /*isnan=*/ 1, /*isinf=*/ 0, /*isfin=*/ 0);
+ foo(f, d, ld, /*unord=*/ 1, /*isnan=*/ 1, /*isinf=*/ 0, /*isfin=*/ 0, /*isnorm=*/ 0);
f = __builtin_inff(); d = __builtin_inf(); ld = __builtin_infl();
- foo(f, d, ld, /*unord=*/ 0, /*isnan=*/ 0, /*isinf=*/ 1, /*isfin=*/ 0);
+ foo(f, d, ld, /*unord=*/ 0, /*isnan=*/ 0, /*isinf=*/ 1, /*isfin=*/ 0, /*isnorm=*/ 0);
f = 0; d = 0; ld = 0;
- foo(f, d, ld, /*unord=*/ 0, /*isnan=*/ 0, /*isinf=*/ 0, /*isfin=*/ 1);
+ foo(f, d, ld, /*unord=*/ 0, /*isnan=*/ 0, /*isinf=*/ 0, /*isfin=*/ 1, /*isnorm=*/ 0);
+
+ f = 1; d = 1; ld = 1;
+ foo(f, d, ld, /*unord=*/ 0, /*isnan=*/ 0, /*isinf=*/ 0, /*isfin=*/ 1, /*isnorm=*/ 1);
f = __FLT_MIN__; d = __DBL_MIN__; ld = __LDBL_MIN__;
- foo(f, d, ld, /*unord=*/ 0, /*isnan=*/ 0, /*isinf=*/ 0, /*isfin=*/ 1);
+ foo(f, d, ld, /*unord=*/ 0, /*isnan=*/ 0, /*isinf=*/ 0, /*isfin=*/ 1, /*isnorm=*/ 1);
+
+ f = __FLT_MIN__/2; d = __DBL_MIN__/2; ld = __LDBL_MIN__/2;
+ foo(f, d, ld, /*unord=*/ 0, /*isnan=*/ 0, /*isinf=*/ 0, /*isfin=*/ 1, /*isnorm=*/ 0);
f = __FLT_MAX__; d = __DBL_MAX__; ld = __LDBL_MAX__;
- foo(f, d, ld, /*unord=*/ 0, /*isnan=*/ 0, /*isinf=*/ 0, /*isfin=*/ 1);
+ foo(f, d, ld, /*unord=*/ 0, /*isnan=*/ 0, /*isinf=*/ 0, /*isfin=*/ 1, /*isnorm=*/ 1);
+
+ f = __FLT_MAX__*2; d = __DBL_MAX__*2; ld = __LDBL_MAX__*2;
+ foo(f, d, ld, /*unord=*/ 0, /*isnan=*/ 0, /*isinf=*/ 1, /*isfin=*/ 0, /*isnorm=*/ 0);
return 0;
}