This is the mail archive of the gcc-patches@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: [PATCH]: Add some folding of builtins fmin/fmax


On Sat, 18 Nov 2006, Roger Sayle wrote:

> I'm sure you've already got it queued, but you can now easily add
> fmin(x,NaN) -> x and fmin(NaN,y) -> y to your new fold_builtin_fmin_fmax.
> Roger

Well I hadn't actually planned on it, but here you go. :-)
Bootstrapped on sparc-sun-solaris2.10, no regressions.

Okay for mainline?

		Thanks,
		--Kaveh


2006-11-23  Kaveh R. Ghazi  <ghazi@caip.rutgers.edu>

	* builtins.c (fold_builtin_fmin_fmax): Handle NaN arguments.

testsuite:
	* gcc.dg/torture/builtin-minmax-1.c: Test NaN in fmin/fmax.

diff -rup orig/egcc-SVN20061118/gcc/builtins.c egcc-SVN20061118/gcc/builtins.c
--- orig/egcc-SVN20061118/gcc/builtins.c	2006-11-18 15:52:31.000000000 -0500
+++ egcc-SVN20061118/gcc/builtins.c	2006-11-18 16:23:42.423868408 -0500
@@ -8742,6 +8742,13 @@ fold_builtin_fmin_fmax (tree arglist, tr
       if (res)
 	return res;

+      /* If either argument is NaN, return the other one.  */
+      if (TREE_CODE (arg0) == REAL_CST && real_isnan (&TREE_REAL_CST (arg0)))
+	return fold_convert (type, arg1);
+      else
+	if (TREE_CODE (arg1) == REAL_CST && real_isnan (&TREE_REAL_CST (arg1)))
+	  return fold_convert (type, arg0);
+
       /* Transform fmin/fmax(x,x) -> x.  */
       if (operand_equal_p (arg0, arg1, OEP_PURE_SAME))
 	return omit_one_operand (type, arg0, arg1);
diff -rup orig/egcc-SVN20061118/gcc/testsuite/gcc.dg/torture/builtin-minmax-1.c egcc-SVN20061118/gcc/testsuite/gcc.dg/torture/builtin-minmax-1.c
--- orig/egcc-SVN20061118/gcc/testsuite/gcc.dg/torture/builtin-minmax-1.c	2006-11-18 15:52:22.000000000 -0500
+++ egcc-SVN20061118/gcc/testsuite/gcc.dg/torture/builtin-minmax-1.c	2006-11-18 16:32:58.334504662 -0500
@@ -74,6 +74,22 @@ extern int pure(int) __attribute__ ((__p
     link_error(__LINE__); \
   } while (0)

+/* Test that FUNC(NaN,x) == x.  We cast to (long) so "!=" folds.  */
+#define TEST_NAN(FUNC) do { \
+  if ((long)FUNC##f(__builtin_nanf(""),xf) != (long)xf) \
+    link_error(__LINE__); \
+  if ((long)FUNC##f(xf,__builtin_nanf("")) != (long)xf) \
+    link_error(__LINE__); \
+  if ((long)FUNC(__builtin_nan(""),x) != (long)x) \
+    link_error(__LINE__); \
+  if ((long)FUNC(x,__builtin_nan("")) != (long)x) \
+    link_error(__LINE__); \
+  if ((long)FUNC##l(__builtin_nanl(""),xl) != (long)xl) \
+    link_error(__LINE__); \
+  if ((long)FUNC##l(xl,__builtin_nanl("")) != (long)xl) \
+    link_error(__LINE__); \
+  } while (0)
+
 void foo (float xf, double x, long double xl,
 	  float yf, double y, long double yl,
 	  int i, int j)
@@ -91,6 +107,9 @@ void foo (float xf, double x, long doubl

   TEST_NONNEG(fmin);
   TEST_NONNEG(fmax);
+
+  TEST_NAN(fmin);
+  TEST_NAN(fmax);
 }

 int main()


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]