diff mbox series

c: Handle C23 floating constant {d,D}{32,64,128} suffixes like {df,dd,dl}

Message ID ZzIis43/LKZ+AvFL@tucnak
State New
Headers show
Series c: Handle C23 floating constant {d,D}{32,64,128} suffixes like {df,dd,dl} | expand

Commit Message

Jakub Jelinek Nov. 11, 2024, 3:28 p.m. UTC
Hi!

C23 roughly says that {d,D}{32,64,128} floating point constant suffixes
are alternate spellings of {df,dd,dl} suffixes in annex H.

So, the following patch allows that alternate spelling.
Or is it intentional it isn't enabled and we need to do everything in
there first before trying to define __STDC_IEC_60559_DFP__?
Like add support for _Decimal32x and _Decimal64x types (including
the d32x and d64x suffixes) etc.

2024-11-11  Jakub Jelinek  <jakub@redhat.com>

libcpp/
	* expr.cc (interpret_float_suffix): Handle d32 and D32 suffixes
	for C like df, d64 and D64 like dd and d128 and D128 like
	dl.
gcc/c-family/
	* c-lex.cc (interpret_float): Subtract 3 or 4 from copylen
	rather than 2 if last character of CPP_N_DFLOAT is a digit.
gcc/testsuite/
	* gcc.dg/dfp/c11-constants-3.c: New test.
	* gcc.dg/dfp/c11-constants-4.c: New test.
	* gcc.dg/dfp/c23-constants-3.c: New test.
	* gcc.dg/dfp/c23-constants-4.c: New test.


	Jakub

Comments

Joseph Myers Nov. 12, 2024, 1:16 a.m. UTC | #1
On Mon, 11 Nov 2024, Jakub Jelinek wrote:

> Hi!
> 
> C23 roughly says that {d,D}{32,64,128} floating point constant suffixes
> are alternate spellings of {df,dd,dl} suffixes in annex H.
> 
> So, the following patch allows that alternate spelling.

This is OK.

> Or is it intentional it isn't enabled and we need to do everything in
> there first before trying to define __STDC_IEC_60559_DFP__?
> Like add support for _Decimal32x and _Decimal64x types (including
> the d32x and d64x suffixes) etc.

It's reasonable to add support incrementally rather than needing to add 
everything at once.

There is no such type as _Decimal32x, just _Decimal64x (required in Annex 
H) and _Decimal128x (optional).

__STDC_IEC_60559_DFP__ is one of those predefined macros for a combination 
of language and library features (i.e. one for stdc-predef.h, given both 
language and library support - and libdfp, if that's what you use for DFP 
features (see also 
<https://sourceware.org/pipermail/libc-alpha/2019-September/106579.html>), 
is a long way from supporting all of the features more recent than TR 
24732:2009).  It does not require Annex H features unless 
__STDC_IEC_60559_TYPES__ is defined as well.
diff mbox series

Patch

--- libcpp/expr.cc.jj	2024-11-11 13:42:33.704372129 +0100
+++ libcpp/expr.cc	2024-11-11 14:41:49.366463196 +0100
@@ -99,9 +99,9 @@  interpret_float_suffix (cpp_reader *pfil
   /* The following decimal float suffixes, from TR 24732:2009, TS
      18661-2:2015 and C23, are supported:
 
-     df, DF - _Decimal32.
-     dd, DD - _Decimal64.
-     dl, DL - _Decimal128.
+     df, DF, d32, D32 - _Decimal32.
+     dd, DD, d64, D64 - _Decimal64.
+     dl, DL, d128, D128 - _Decimal128.
 
      The dN and DN suffixes for _DecimalN, and dNx and DNx for
      _DecimalNx, defined in TS 18661-3:2015, are not supported.
@@ -253,7 +253,18 @@  interpret_float_suffix (cpp_reader *pfil
 	      break;
 	    }
 	  return 0;
-	case 'd': case 'D': d++; break;
+	case 'd': case 'D':
+	  if (!CPP_OPTION (pfile, cplusplus) && orig_s == s && len > 1)
+	    {
+	      if (s[1] == '3' && s[2] == '2' && len == 2)
+		return CPP_N_DFLOAT | CPP_N_SMALL;
+	      if (s[1] == '6' && s[2] == '4' && len == 2)
+		return CPP_N_DFLOAT | CPP_N_MEDIUM;
+	      if (s[1] == '1' && s[2] == '2' && len == 3 && s[3] == '8')
+		return CPP_N_DFLOAT | CPP_N_LARGE;
+	    }
+	  d++;
+	  break;
 	case 'l': case 'L': l++; break;
 	case 'w': case 'W': w++; break;
 	case 'q': case 'Q': q++; break;
--- gcc/c-family/c-lex.cc.jj	2024-11-09 11:41:24.218838261 +0100
+++ gcc/c-family/c-lex.cc	2024-11-11 16:14:28.760344200 +0100
@@ -1347,7 +1347,12 @@  interpret_float (const cpp_token *token,
   if (flags & CPP_N_USERDEF)
     copylen -= strlen (suffix);
   else if (flags & CPP_N_DFLOAT)
-    copylen -= 2;
+    {
+      if (ISDIGIT (token->val.str.text[copylen - 1]))
+	copylen -= (flags & CPP_N_LARGE) ? 4 : 3;
+      else
+	copylen -= 2;
+    }
   else
     {
       if ((flags & CPP_N_WIDTH) != CPP_N_MEDIUM)
--- gcc/testsuite/gcc.dg/dfp/c11-constants-3.c.jj	2024-11-11 15:44:30.842603023 +0100
+++ gcc/testsuite/gcc.dg/dfp/c11-constants-3.c	2024-11-11 15:45:39.028645039 +0100
@@ -0,0 +1,13 @@ 
+/* Test that DFP constants are diagnosed in C11 mode: -pedantic.  */
+/* { dg-do compile } */
+/* { dg-options "-std=c11 -pedantic" } */
+
+int a = (int) 1.1D32; /* { dg-warning "C23 feature" } */
+int b = (int) 2.d32; /* { dg-warning "C23 feature" } */
+int c = (int) .33D64; /* { dg-warning "C23 feature" } */
+int d = (int) 2e1d64; /* { dg-warning "C23 feature" } */
+int e = (int) .3e2D128; /* { dg-warning "C23 feature" } */
+int f = (int) 4.5e3d128; /* { dg-warning "C23 feature" } */
+int g = (int) 5.e0D32; /* { dg-warning "C23 feature" } */
+int h = (int) 1e+2d32; /* { dg-warning "C23 feature" } */
+int i = (int) 1000e-3D128; /* { dg-warning "C23 feature" } */
--- gcc/testsuite/gcc.dg/dfp/c11-constants-4.c.jj	2024-11-11 15:44:34.358553625 +0100
+++ gcc/testsuite/gcc.dg/dfp/c11-constants-4.c	2024-11-11 15:46:05.975266448 +0100
@@ -0,0 +1,13 @@ 
+/* Test that DFP constants are diagnosed in C11 mode: -pedantic-errors.  */
+/* { dg-do compile } */
+/* { dg-options "-std=c11 -pedantic-errors" } */
+
+int a = (int) 1.1D32; /* { dg-error "C23 feature" } */
+int b = (int) 2.d32; /* { dg-error "C23 feature" } */
+int c = (int) .33D64; /* { dg-error "C23 feature" } */
+int d = (int) 2e1d64; /* { dg-error "C23 feature" } */
+int e = (int) .3e2D128; /* { dg-error "C23 feature" } */
+int f = (int) 4.5e3d128; /* { dg-error "C23 feature" } */
+int g = (int) 5.e0D32; /* { dg-error "C23 feature" } */
+int h = (int) 1e+2d32; /* { dg-error "C23 feature" } */
+int i = (int) 1000e-3D128; /* { dg-error "C23 feature" } */
--- gcc/testsuite/gcc.dg/dfp/c23-constants-3.c.jj	2024-11-11 15:44:44.668408776 +0100
+++ gcc/testsuite/gcc.dg/dfp/c23-constants-3.c	2024-11-11 16:17:27.690830558 +0100
@@ -0,0 +1,39 @@ 
+/* Test that DFP constants are accepted in C23 mode.  */
+/* { dg-do run } */
+/* { dg-options "-std=c23 -pedantic-errors" } */
+
+int a = (int) 1.1D32;
+int b = (int) 2.d32;
+int c = (int) .33D64;
+int d = (int) 2e1d64;
+int e = (int) .3e2D128;
+int f = (int) 4.5e3d128;
+int g = (int) 5.e0D32;
+int h = (int) 1e+2d32;
+int i = (int) 1000e-3D128;
+
+#define expr_has_type(e, t) _Generic (e, default : 0, t : 1)
+static_assert (expr_has_type (1.1D32, _Decimal32));
+static_assert (expr_has_type (2.d32, _Decimal32));
+static_assert (expr_has_type (.33D64, _Decimal64));
+static_assert (expr_has_type (2e1d64, _Decimal64));
+static_assert (expr_has_type (.3e2D128, _Decimal128));
+static_assert (expr_has_type (4.5e3d128, _Decimal128));
+static_assert (expr_has_type (5.e0D32, _Decimal32));
+static_assert (expr_has_type (1e+2d32, _Decimal32));
+static_assert (expr_has_type (1000e-3D128, _Decimal128));
+
+int
+main ()
+{
+  if (1.1D32 != 1.1df
+      || 2.d32 != 2.df
+      || .33D64 != .33dd
+      || 2e1d64 != 2e1dd
+      || .3e2D128 != .3e2dl
+      || 4.5e3d128 != 4.5e3dl
+      || 5.e0D32 != 5.e0df
+      || 1e+2d32 != 1e+2df
+      || 1000e-3D128 != 1000e-3dl)
+    __builtin_abort ();
+}
--- gcc/testsuite/gcc.dg/dfp/c23-constants-4.c.jj	2024-11-11 15:44:48.295357830 +0100
+++ gcc/testsuite/gcc.dg/dfp/c23-constants-4.c	2024-11-11 15:51:48.030460722 +0100
@@ -0,0 +1,13 @@ 
+/* Test that DFP constants are accepted in C23 mode: compat warnings.  */
+/* { dg-do compile } */
+/* { dg-options "-std=c23 -Wc11-c23-compat" } */
+
+int a = (int) 1.1D32; /* { dg-warning "C23 feature" } */
+int b = (int) 2.d32; /* { dg-warning "C23 feature" } */
+int c = (int) .33D64; /* { dg-warning "C23 feature" } */
+int d = (int) 2e1d64; /* { dg-warning "C23 feature" } */
+int e = (int) .3e2D128; /* { dg-warning "C23 feature" } */
+int f = (int) 4.5e3d128; /* { dg-warning "C23 feature" } */
+int g = (int) 5.e0D32; /* { dg-warning "C23 feature" } */
+int h = (int) 1e+2d32; /* { dg-warning "C23 feature" } */
+int i = (int) 1000e-3D128; /* { dg-warning "C23 feature" } */