ping^n: C front end change for decimal floating types

Janis Johnson janis187@us.ibm.com
Fri Jun 23 21:21:00 GMT 2006


On Thu, Jun 22, 2006 at 05:52:32PM -0700, Mark Mitchell wrote:
> However, I think that for practical purposes you could also get away with:
> 
>  #define DECIMAL_FLOAT_TYPE_P(TYPE) \
>    (SCALAR_FLOAT_TYPE_P (TYPE) \
>     && DECIMAL_FLOAT_MODE_P (TYPE_MODE (TYPE))
> 
> This still isn't correct in concept, in that front ends really shouldn't
> look at modes and that theoretically the compiler might for some
> miraculous reason decide to put a BFP type in a DFP mode register, but
> in practice I bet it's right enough.  It will fix the problem I
> suggested about single-element structs that get placed into registers.
> 
> I'd be happy enough with the second option, as it will at least give us
> a single chokepoint on any problems that do arise.

Here's what I checked in after further testing:

2006-06-23  Janis Johnson  <janis187@us.ibm.com>

	* tree.h (DECIMAL_FLOAT_TYPE_P): New.
	* c-typeck.c (c_common_type): Disallow operations on decimal float
	types and other float types.
	* convert.c (convert_to_real): Don't ignore conversions involving
	decimal float types.

testsuite:

	* gcc.dg/dfp/usual-arith-conv-bad.c: New test.

Index: gcc/tree.h
===================================================================
--- gcc/tree.h	(revision 114849)
+++ gcc/tree.h	(working copy)
@@ -966,6 +966,11 @@
         || TREE_CODE (TYPE) == VECTOR_TYPE)	\
        && SCALAR_FLOAT_TYPE_P (TREE_TYPE (TYPE))))
 
+/* Nonzero if TYPE represents a decimal floating-point type.  */
+#define DECIMAL_FLOAT_TYPE_P(TYPE)		\
+  (SCALAR_FLOAT_TYPE_P (TYPE)			\
+   && DECIMAL_FLOAT_MODE_P (TYPE_MODE (TYPE)))
+
 /* Nonzero if TYPE represents an aggregate (multi-component) type.
    Keep these checks in ascending code order.  */
 
Index: gcc/c-typeck.c
===================================================================
--- gcc/c-typeck.c	(revision 114849)
+++ gcc/c-typeck.c	(working copy)
@@ -596,6 +596,29 @@
   gcc_assert (code2 == VECTOR_TYPE || code2 == COMPLEX_TYPE
 	      || code2 == REAL_TYPE || code2 == INTEGER_TYPE);
 
+  /* When one operand is a decimal float type, the other operand cannot be
+     a generic float type or a complex type.  We also disallow vector types
+     here.  */
+  if ((DECIMAL_FLOAT_TYPE_P (t1) || DECIMAL_FLOAT_TYPE_P (t2))
+      && !(DECIMAL_FLOAT_TYPE_P (t1) && DECIMAL_FLOAT_TYPE_P (t2)))
+    {
+      if (code1 == VECTOR_TYPE || code2 == VECTOR_TYPE)
+	{
+	  error ("can%'t mix operands of decimal float and vector types");
+	  return error_mark_node;
+	}
+      if (code1 == COMPLEX_TYPE || code2 == COMPLEX_TYPE)
+	{
+	  error ("can%'t mix operands of decimal float and complex types");
+	  return error_mark_node;
+	}
+      if (code1 == REAL_TYPE && code2 == REAL_TYPE)
+	{
+	  error ("can%'t mix operands of decimal float and other float types");
+	  return error_mark_node;
+	}
+    }
+
   /* If one type is a vector type, return that type.  (How the usual
      arithmetic conversions apply to the vector types extension is not
      precisely specified.)  */
Index: gcc/convert.c
===================================================================
--- gcc/convert.c	(revision 114849)
+++ gcc/convert.c	(working copy)
@@ -315,8 +315,12 @@
   switch (TREE_CODE (TREE_TYPE (expr)))
     {
     case REAL_TYPE:
-      return build1 (flag_float_store ? CONVERT_EXPR : NOP_EXPR,
-		     type, expr);
+      /* Ignore the conversion if we don't need to store intermediate
+	 results and neither type is a decimal float.  */
+      return build1 ((flag_float_store
+		     || DECIMAL_FLOAT_TYPE_P (type)
+		     || DECIMAL_FLOAT_TYPE_P (itype))
+		     ? CONVERT_EXPR : NOP_EXPR, type, expr);
 
     case INTEGER_TYPE:
     case ENUMERAL_TYPE:
--- /dev/null	2004-06-24 11:06:20.000000000 -0700
+++ gcc/testsuite/gcc.dg/dfp/usual-arith-conv-bad.c	2006-06-21 14:53:27.000000000 -0700
@@ -0,0 +1,50 @@
+/* { dg-do compile } */
+/* { dg-options "-std=gnu99" } */
+
+/* N1150 5.4: Usual arithmetic conversions.
+   C99 6.3.1.8[1] (New).
+
+   Test arithmetic operators between decimal float types and generic
+   float types, which are not allowed.  */
+
+extern _Decimal32 d32a, d32b;
+extern _Decimal64 d64a, d64b;
+extern _Decimal128 d128a, d128b;
+extern float f;
+extern double d;
+extern long double ld;
+
+extern signed int __attribute__ ((vector_size (16))) vi;
+
+extern _Complex float cf;
+extern _Complex double cd;
+extern _Complex long double cld;
+extern _Complex int ci;
+
+void
+foo (void)
+{
+  /* Mixed operations with decimal and generic float operands.  */
+  d32a = d32b + f;	/* { dg-error "" "error.*mix operands of decimal float" } */
+  d32a = f * d32b;	/* { dg-error "" "error.* mix operands of decimal float" } */
+  d32a *= f;		/* { dg-error "" "error.* mix operands of decimal float" } */
+  f += d32b;		/* { dg-error "" "error.* mix operands of decimal float" } */
+  d64a = d32a + d;	/* { dg-error "" "error.* mix operands of decimal float" } */
+  d64a = d * d128a;	/* { dg-error "" "error.* mix operands of decimal float" } */
+  d64a -= d;		/* { dg-error "" "error.* mix operands of decimal float" } */
+  d128a = ld * d128b;	/* { dg-error "" "error.* mix operands of decimal float" } */
+  d128a = d64b + d;	/* { dg-error "" "error.* mix operands of decimal float" } */
+  d128a *= f;		/* { dg-error "" "error.* mix operands of decimal float" } */
+
+  /* Mixed operations with decimal float and a vector type.  */
+  d64a = d64b + vi;	/* { dg-error "" "error.* mix operands of decimal float" } */
+  d32a *= vi;		/* { dg-error "" "error.* mix operands of decimal float" } */
+  d128a = vi - d128b;	/* { dg-error "" "error.* mix operands of decimal float" } */
+
+  /* Mixed operations with decimal float and Complex types.  */
+  d32a += ci;		/* { dg-error "" "error.* mix operands of decimal float" } */
+  d64a = ci * d32a;	/* { dg-error "" "error.* mix operands of decimal float" } */
+  cd = d64a * cd;	/* { dg-error "" "error.* mix operands of decimal float" } */
+  d128b = cld * d128b;	/* { dg-error "" "error.* mix operands of decimal float" } */
+  d32a = cf * d32b;	/* { dg-error "" "error.* mix operands of decimal float" } */
+}



More information about the Gcc-patches mailing list