From 3c66839a95f69dfe12db651033e9df9dfbe3c719 Mon Sep 17 00:00:00 2001
From: Francois-Xavier Coudert <fxcoudert@gcc.gnu.org>
Date: Thu, 1 Sep 2022 22:49:49 +0200
Subject: [PATCH] Add __builtin_iseqsig()

iseqsig() is a C2x library function, for signaling floating-point
equality checks.  Provide a GCC-builtin for it, which is folded to
a series of comparisons.

2022-09-01  Francois-Xavier Coudert  <fxcoudert@gcc.gnu.org>

	PR middle-end/77928

gcc/
	* doc/extend.texi: Document iseqsig builtin.
	* builtins.cc (fold_builtin_iseqsig): New function.
	(fold_builtin_2): Handle BUILT_IN_ISEQSIG.
	(is_inexpensive_builtin): Handle BUILT_IN_ISEQSIG.
	* builtins.def (BUILT_IN_ISEQSIG): New built-in.

gcc/c-family/
	* c-common.cc (check_builtin_function_arguments):
	Handle BUILT_IN_ISEQSIG.

gcc/testsuite/
	* gcc.dg/torture/builtin-iseqsig-1.c: New test.
---
 gcc/builtins.cc                               |  41 ++++++
 gcc/builtins.def                              |   1 +
 gcc/c-family/c-common.cc                      |   1 +
 gcc/doc/extend.texi                           |   7 +-
 .../gcc.dg/torture/builtin-iseqsig-1.c        | 117 ++++++++++++++++++
 5 files changed, 164 insertions(+), 3 deletions(-)
 create mode 100644 gcc/testsuite/gcc.dg/torture/builtin-iseqsig-1.c

diff --git a/gcc/builtins.cc b/gcc/builtins.cc
index 5f319b28030..b15af6f1a45 100644
--- a/gcc/builtins.cc
+++ b/gcc/builtins.cc
@@ -171,6 +171,7 @@ static tree fold_builtin_fabs (location_t, tree, tree);
 static tree fold_builtin_abs (location_t, tree, tree);
 static tree fold_builtin_unordered_cmp (location_t, tree, tree, tree, enum tree_code,
 					enum tree_code);
+static tree fold_builtin_iseqsig (location_t, tree, tree);
 static tree fold_builtin_varargs (location_t, tree, tree*, int);
 
 static tree fold_builtin_strpbrk (location_t, tree, tree, tree, tree);
@@ -9400,6 +9401,42 @@ fold_builtin_unordered_cmp (location_t loc, tree fndecl, tree arg0, tree arg1,
 		      fold_build2_loc (loc, code, type, arg0, arg1));
 }
 
+/* Fold a call to __builtin_iseqsig().  ARG0 and ARG1 are the arguments.
+   After choosing the wider floating-point type for the comparison,
+   the code is folded to:
+     SAVE_EXPR<ARG0> >= SAVE_EXPR<ARG1> && SAVE_EXPR<ARG0> <= SAVE_EXPR<ARG1>  */
+
+static tree
+fold_builtin_iseqsig (location_t loc, tree arg0, tree arg1)
+{
+  tree type0, type1;
+  enum tree_code code0, code1;
+  tree cmp1, cmp2, cmp_type = NULL_TREE;
+
+  type0 = TREE_TYPE (arg0);
+  type1 = TREE_TYPE (arg1);
+
+  code0 = TREE_CODE (type0);
+  code1 = TREE_CODE (type1);
+
+  if (code0 == REAL_TYPE && code1 == REAL_TYPE)
+    /* Choose the wider of two real types.  */
+    cmp_type = TYPE_PRECISION (type0) >= TYPE_PRECISION (type1)
+      ? type0 : type1;
+  else if (code0 == REAL_TYPE && code1 == INTEGER_TYPE)
+    cmp_type = type0;
+  else if (code0 == INTEGER_TYPE && code1 == REAL_TYPE)
+    cmp_type = type1;
+
+  arg0 = builtin_save_expr (fold_convert_loc (loc, cmp_type, arg0));
+  arg1 = builtin_save_expr (fold_convert_loc (loc, cmp_type, arg1));
+
+  cmp1 = fold_build2_loc (loc, GE_EXPR, integer_type_node, arg0, arg1);
+  cmp2 = fold_build2_loc (loc, LE_EXPR, integer_type_node, arg0, arg1);
+
+  return fold_build2_loc (loc, TRUTH_AND_EXPR, integer_type_node, cmp1, cmp2);
+}
+
 /* Fold __builtin_{,s,u}{add,sub,mul}{,l,ll}_overflow, either into normal
    arithmetics if it can never overflow, or into internal functions that
    return both result of arithmetics and overflowed boolean flag in
@@ -9787,6 +9824,9 @@ fold_builtin_2 (location_t loc, tree expr, tree fndecl, tree arg0, tree arg1)
 					 arg0, arg1, UNORDERED_EXPR,
 					 NOP_EXPR);
 
+    case BUILT_IN_ISEQSIG:
+      return fold_builtin_iseqsig (loc, arg0, arg1);
+
       /* We do the folding for va_start in the expander.  */
     case BUILT_IN_VA_START:
       break;
@@ -11299,6 +11339,7 @@ is_inexpensive_builtin (tree decl)
       case BUILT_IN_ISLESSEQUAL:
       case BUILT_IN_ISLESSGREATER:
       case BUILT_IN_ISUNORDERED:
+      case BUILT_IN_ISEQSIG:
       case BUILT_IN_VA_ARG_PACK:
       case BUILT_IN_VA_ARG_PACK_LEN:
       case BUILT_IN_VA_COPY:
diff --git a/gcc/builtins.def b/gcc/builtins.def
index f0236316850..8fab9dc3f1b 100644
--- a/gcc/builtins.def
+++ b/gcc/builtins.def
@@ -908,6 +908,7 @@ DEF_GCC_BUILTIN        (BUILT_IN_ISLESS, "isless", BT_FN_INT_VAR, ATTR_CONST_NOT
 DEF_GCC_BUILTIN        (BUILT_IN_ISLESSEQUAL, "islessequal", BT_FN_INT_VAR, ATTR_CONST_NOTHROW_TYPEGENERIC_LEAF)
 DEF_GCC_BUILTIN        (BUILT_IN_ISLESSGREATER, "islessgreater", BT_FN_INT_VAR, ATTR_CONST_NOTHROW_TYPEGENERIC_LEAF)
 DEF_GCC_BUILTIN        (BUILT_IN_ISUNORDERED, "isunordered", BT_FN_INT_VAR, ATTR_CONST_NOTHROW_TYPEGENERIC_LEAF)
+DEF_GCC_BUILTIN        (BUILT_IN_ISEQSIG, "iseqsig", BT_FN_INT_VAR, ATTR_CONST_NOTHROW_TYPEGENERIC_LEAF)
 DEF_GCC_BUILTIN        (BUILT_IN_ISSIGNALING, "issignaling", BT_FN_INT_VAR, ATTR_CONST_NOTHROW_TYPEGENERIC_LEAF)
 DEF_LIB_BUILTIN        (BUILT_IN_LABS, "labs", BT_FN_LONG_LONG, ATTR_CONST_NOTHROW_LEAF_LIST)
 DEF_C99_BUILTIN        (BUILT_IN_LLABS, "llabs", BT_FN_LONGLONG_LONGLONG, ATTR_CONST_NOTHROW_LEAF_LIST)
diff --git a/gcc/c-family/c-common.cc b/gcc/c-family/c-common.cc
index c0f15f4cab1..62d8e54c96d 100644
--- a/gcc/c-family/c-common.cc
+++ b/gcc/c-family/c-common.cc
@@ -6329,6 +6329,7 @@ check_builtin_function_arguments (location_t loc, vec<location_t> arg_loc,
     case BUILT_IN_ISLESSEQUAL:
     case BUILT_IN_ISLESSGREATER:
     case BUILT_IN_ISUNORDERED:
+    case BUILT_IN_ISEQSIG:
       if (builtin_function_validate_nargs (loc, fndecl, nargs, 2))
 	{
 	  enum tree_code code0, code1;
diff --git a/gcc/doc/extend.texi b/gcc/doc/extend.texi
index a5afb467d23..7a02b8c47e8 100644
--- a/gcc/doc/extend.texi
+++ b/gcc/doc/extend.texi
@@ -12995,6 +12995,7 @@ is called and the @var{flag} argument passed to it.
 @findex __builtin_extend_pointer
 @findex __builtin_fpclassify
 @findex __builtin_has_attribute
+@findex __builtin_iseqsig
 @findex __builtin_isfinite
 @findex __builtin_isnormal
 @findex __builtin_isgreater
@@ -13555,9 +13556,9 @@ the same names as the standard macros ( @code{isgreater},
 @code{islessgreater}, and @code{isunordered}) , with @code{__builtin_}
 prefixed.  We intend for a library implementor to be able to simply
 @code{#define} each standard macro to its built-in equivalent.
-In the same fashion, GCC provides @code{fpclassify}, @code{isfinite},
-@code{isinf_sign}, @code{isnormal} and @code{signbit} built-ins used with
-@code{__builtin_} prefixed.  The @code{isinf} and @code{isnan}
+In the same fashion, GCC provides @code{fpclassify}, @code{iseqsig},
+@code{isfinite}, @code{isinf_sign}, @code{isnormal} and @code{signbit} built-ins
+used with @code{__builtin_} prefixed.  The @code{isinf} and @code{isnan}
 built-in functions appear both with and without the @code{__builtin_} prefix.
 With @code{-ffinite-math-only} option the @code{isinf} and @code{isnan}
 built-in functions will always return 0.
diff --git a/gcc/testsuite/gcc.dg/torture/builtin-iseqsig-1.c b/gcc/testsuite/gcc.dg/torture/builtin-iseqsig-1.c
new file mode 100644
index 00000000000..3ffaaf7e15d
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/torture/builtin-iseqsig-1.c
@@ -0,0 +1,117 @@
+/* { dg-do run } */
+/* { dg-add-options ieee } */
+/* { dg-additional-options "-fsignaling-nans" } */
+
+void
+ftrue (float x, float y)
+{
+  if (!__builtin_iseqsig (x, y))
+    __builtin_abort ();
+}
+
+void
+ffalse (float x, float y)
+{
+  if (__builtin_iseqsig (x, y))
+    __builtin_abort ();
+}
+
+void
+dtrue (double x, double y)
+{
+  if (!__builtin_iseqsig (x, y))
+    __builtin_abort ();
+}
+
+void
+dfalse (double x, double y)
+{
+  if (__builtin_iseqsig (x, y))
+    __builtin_abort ();
+}
+
+void
+ltrue (long double x, long double y)
+{
+  if (!__builtin_iseqsig (x, y))
+    __builtin_abort ();
+}
+
+void
+lfalse (long double x, long double y)
+{
+  if (__builtin_iseqsig (x, y))
+    __builtin_abort ();
+}
+
+
+int
+main ()
+{
+  ftrue (0.f, 0.f);
+  ftrue (0.f, -0.f);
+  ffalse (0.f, 1.f);
+  ffalse (-0.f, 1.f);
+  ffalse (0.f, __builtin_inff());
+  ffalse (-0.f, __builtin_inff());
+  ffalse (0.f, __builtin_nanf(""));
+  ffalse (-0.f, __builtin_nanf(""));
+  ftrue (1.f, 1.f);
+  ffalse (1.f, 0.f);
+  ffalse (1.f, -0.f);
+  ffalse (1.f, __builtin_inff());
+  ffalse (1.f, __builtin_nanf(""));
+  ftrue (__builtin_inff(), __builtin_inff());
+  ffalse (__builtin_inff(), __builtin_nanf(""));
+  ffalse (__builtin_nanf(""), __builtin_nanf(""));
+
+  dtrue (0., 0.);
+  dtrue (0., -0.);
+  dfalse (0., 1.);
+  dfalse (-0., 1.);
+  dfalse (0., __builtin_inf());
+  dfalse (-0., __builtin_inf());
+  dfalse (0., __builtin_nan(""));
+  dfalse (-0., __builtin_nan(""));
+  dtrue (1., 1.);
+  dfalse (1., 0.);
+  dfalse (1., -0.);
+  dfalse (1., __builtin_inf());
+  dfalse (1., __builtin_nan(""));
+  dtrue (__builtin_inf(), __builtin_inf());
+  dfalse (__builtin_inf(), __builtin_nan(""));
+  dfalse (__builtin_nan(""), __builtin_nan(""));
+
+  ltrue (0.L, 0.L);
+  ltrue (0.L, -0.L);
+  lfalse (0.L, 1.L);
+  lfalse (-0.L, 1.L);
+  lfalse (0.L, __builtin_infl());
+  lfalse (-0.L, __builtin_infl());
+  lfalse (0.L, __builtin_nanl(""));
+  lfalse (-0.L, __builtin_nanl(""));
+  ltrue (1.L, 1.L);
+  lfalse (1.L, 0.L);
+  lfalse (1.L, -0.L);
+  lfalse (1.L, __builtin_infl());
+  lfalse (1.L, __builtin_nanl(""));
+  ltrue (__builtin_infl(), __builtin_infl());
+  lfalse (__builtin_infl(), __builtin_nanl(""));
+  lfalse (__builtin_nanl(""), __builtin_nanl(""));
+
+  if (!__builtin_iseqsig (0.f, -0.))
+    __builtin_abort ();
+  if (!__builtin_iseqsig (0.f, -0.L))
+    __builtin_abort ();
+  if (!__builtin_iseqsig (0., -0.L))
+    __builtin_abort ();
+
+  if (__builtin_iseqsig (0.f, 1.))
+    __builtin_abort ();
+  if (__builtin_iseqsig (0.f, 1.L))
+    __builtin_abort ();
+  if (__builtin_iseqsig (0., 1.L))
+    __builtin_abort ();
+
+  return 0;
+}
-- 
2.37.0 (Apple Git-136)

