builtins: Add DFP signaling NaN built-in functions

Add built-in functions __builtin_nansd32, __builtin_nansd64 and
__builtin_nansd128 to return signaling NaNs of decimal floating-point
types, analogous to the functions already present for binary
floating-point types.

This patch, independent of
<https://gcc.gnu.org/pipermail/gcc-patches/2020-October/557136.html>
(pending review), is in preparation for adding the <float.h> macros
for such signaling NaNs that are in C2x, analogous to the macros for
other types that are in that patch.

Bootstrapped with no regressions for x86_64-pc-linux-gnu.  Also ran
the new tests for powerpc64le-linux-gnu to confirm they do work in the
case (hardware DFP) where floating-point exceptions are supported for
DFP.

gcc/
2020-11-06  Joseph Myers  <joseph@codesourcery.com>

	* builtins.def (BUILT_IN_NANSD32, BUILT_IN_NANSD64)
	(BUILT_IN_NANSD128): New built-in functions.
	* fold-const-call.c (fold_const_call): Handle the new built-in
	functions.
	* doc/extend.texi (__builtin_nansd32, __builtin_nansd64)
	(__builtin_nansd128): Document.
	* doc/sourcebuild.texi (Effective-Target Keywords): Document
	fenv_exceptions_dfp.

gcc/testsuite/
2020-11-06  Joseph Myers  <joseph@codesourcery.com>

	* lib/target-supports.exp
	(check_effective_target_fenv_exceptions_dfp): New.
	* gcc.dg/dfp/builtin-snan-1.c, gcc.dg/dfp/builtin-snan-2.c: New
	tests.
This commit is contained in:
Joseph Myers 2020-11-06 21:55:19 +00:00
parent 43d3b7bc07
commit 6c8e4f4d79
7 changed files with 128 additions and 0 deletions

View file

@ -518,6 +518,9 @@ DEF_GCC_BUILTIN (BUILT_IN_NANSF, "nansf", BT_FN_FLOAT_CONST_STRING, ATTR_
DEF_GCC_BUILTIN (BUILT_IN_NANSL, "nansl", BT_FN_LONGDOUBLE_CONST_STRING, ATTR_CONST_NOTHROW_NONNULL)
DEF_GCC_FLOATN_NX_BUILTINS (BUILT_IN_NANS, "nans", NAN_TYPE, ATTR_CONST_NOTHROW_NONNULL)
#undef NAN_TYPE
DEF_GCC_BUILTIN (BUILT_IN_NANSD32, "nansd32", BT_FN_DFLOAT32_CONST_STRING, ATTR_CONST_NOTHROW_NONNULL)
DEF_GCC_BUILTIN (BUILT_IN_NANSD64, "nansd64", BT_FN_DFLOAT64_CONST_STRING, ATTR_CONST_NOTHROW_NONNULL)
DEF_GCC_BUILTIN (BUILT_IN_NANSD128, "nansd128", BT_FN_DFLOAT128_CONST_STRING, ATTR_CONST_NOTHROW_NONNULL)
DEF_C99_BUILTIN (BUILT_IN_NEARBYINT, "nearbyint", BT_FN_DOUBLE_DOUBLE, ATTR_CONST_NOTHROW_LEAF_LIST)
DEF_C99_BUILTIN (BUILT_IN_NEARBYINTF, "nearbyintf", BT_FN_FLOAT_FLOAT, ATTR_CONST_NOTHROW_LEAF_LIST)
DEF_C99_BUILTIN (BUILT_IN_NEARBYINTL, "nearbyintl", BT_FN_LONGDOUBLE_LONGDOUBLE, ATTR_CONST_NOTHROW_LEAF_LIST)

View file

@ -13865,6 +13865,18 @@ to be a signaling NaN@. The @code{nans} function is proposed by
@uref{http://www.open-std.org/jtc1/sc22/wg14/www/docs/n965.htm,,WG14 N965}.
@end deftypefn
@deftypefn {Built-in Function} _Decimal32 __builtin_nansd32 (const char *str)
Similar to @code{__builtin_nans}, except the return type is @code{_Decimal32}.
@end deftypefn
@deftypefn {Built-in Function} _Decimal64 __builtin_nansd64 (const char *str)
Similar to @code{__builtin_nans}, except the return type is @code{_Decimal64}.
@end deftypefn
@deftypefn {Built-in Function} _Decimal128 __builtin_nansd128 (const char *str)
Similar to @code{__builtin_nans}, except the return type is @code{_Decimal128}.
@end deftypefn
@deftypefn {Built-in Function} float __builtin_nansf (const char *str)
Similar to @code{__builtin_nans}, except the return type is @code{float}.
@end deftypefn

View file

@ -2356,6 +2356,11 @@ Target provides @file{fenv.h} include file.
Target supports @file{fenv.h} with all the standard IEEE exceptions
and floating-point exceptions are raised by arithmetic operations.
@item fenv_exceptions_dfp
Target supports @file{fenv.h} with all the standard IEEE exceptions
and floating-point exceptions are raised by arithmetic operations for
decimal floating point.
@item fileio
Target offers such file I/O library functions as @code{fopen},
@code{fclose}, @code{tmpnam}, and @code{remove}. This is a link-time

View file

@ -1300,6 +1300,9 @@ fold_const_call (combined_fn fn, tree type, tree arg)
CASE_CFN_NANS:
CASE_FLT_FN_FLOATN_NX (CFN_BUILT_IN_NANS):
case CFN_BUILT_IN_NANSD32:
case CFN_BUILT_IN_NANSD64:
case CFN_BUILT_IN_NANSD128:
return fold_const_builtin_nan (type, arg, false);
case CFN_REDUC_PLUS:

View file

@ -0,0 +1,23 @@
/* Test __builtin_nansd* functions. Test not requiring runtime
exceptions support. */
/* { dg-do run } */
/* { dg-options "" } */
volatile _Decimal32 d32 = __builtin_nansd32 ("");
volatile _Decimal64 d64 = __builtin_nansd64 ("");
volatile _Decimal128 d128 = __builtin_nansd128 ("");
extern void abort (void);
extern void exit (int);
int
main (void)
{
if (!__builtin_isnan (d32))
abort ();
if (!__builtin_isnan (d64))
abort ();
if (!__builtin_isnan (d128))
abort ();
exit (0);
}

View file

@ -0,0 +1,44 @@
/* Test __builtin_nansd* functions. Test requiring runtime exceptions
support. */
/* { dg-do run } */
/* { dg-require-effective-target fenv_exceptions_dfp } */
/* { dg-options "" } */
#include <fenv.h>
volatile _Decimal32 d32 = __builtin_nansd32 ("");
volatile _Decimal64 d64 = __builtin_nansd64 ("");
volatile _Decimal128 d128 = __builtin_nansd128 ("");
extern void abort (void);
extern void exit (int);
int
main (void)
{
feclearexcept (FE_ALL_EXCEPT);
d32 += d32;
if (!fetestexcept (FE_INVALID))
abort ();
feclearexcept (FE_ALL_EXCEPT);
d32 += d32;
if (fetestexcept (FE_INVALID))
abort ();
feclearexcept (FE_ALL_EXCEPT);
d64 += d64;
if (!fetestexcept (FE_INVALID))
abort ();
feclearexcept (FE_ALL_EXCEPT);
d64 += d64;
if (fetestexcept (FE_INVALID))
abort ();
feclearexcept (FE_ALL_EXCEPT);
d128 += d128;
if (!fetestexcept (FE_INVALID))
abort ();
feclearexcept (FE_ALL_EXCEPT);
d128 += d128;
if (fetestexcept (FE_INVALID))
abort ();
exit (0);
}

View file

@ -9693,6 +9693,44 @@ proc check_effective_target_fenv_exceptions {} {
} [add_options_for_ieee "-std=gnu99"]]
}
# Return 1 if <fenv.h> is available with all the standard IEEE
# exceptions and floating-point exceptions are raised by arithmetic
# operations for decimal floating point. (If the target requires
# special options for "inexact" exceptions, those need to be specified
# in the testcases.)
proc check_effective_target_fenv_exceptions_dfp {} {
return [check_runtime fenv_exceptions_dfp {
#include <fenv.h>
#include <stdlib.h>
#ifndef FE_DIVBYZERO
# error Missing FE_DIVBYZERO
#endif
#ifndef FE_INEXACT
# error Missing FE_INEXACT
#endif
#ifndef FE_INVALID
# error Missing FE_INVALID
#endif
#ifndef FE_OVERFLOW
# error Missing FE_OVERFLOW
#endif
#ifndef FE_UNDERFLOW
# error Missing FE_UNDERFLOW
#endif
volatile _Decimal64 a = 0.0DD, r;
int
main (void)
{
r = a / a;
if (fetestexcept (FE_INVALID))
exit (0);
else
abort ();
}
} [add_options_for_ieee "-std=gnu99"]]
}
# Return 1 if -fexceptions is supported.
proc check_effective_target_exceptions {} {