c-family: Add more predefined macros for math flags

Library code, especially in headers, sometimes needs to know how the
compiler interprets / optimizes floating-point types and operations.
This information can be used for additional optimizations or for
ensuring correctness. This change makes -freciprocal-math,
-fno-signed-zeros, -fno-trapping-math, -fassociative-math, and
-frounding-math report their state via corresponding pre-defined macros.

Signed-off-by: Matthias Kretz <m.kretz@gsi.de>

gcc/testsuite/ChangeLog:

	* gcc.dg/associative-math-1.c: New test.
	* gcc.dg/associative-math-2.c: New test.
	* gcc.dg/no-signed-zeros-1.c: New test.
	* gcc.dg/no-signed-zeros-2.c: New test.
	* gcc.dg/no-trapping-math-1.c: New test.
	* gcc.dg/no-trapping-math-2.c: New test.
	* gcc.dg/reciprocal-math-1.c: New test.
	* gcc.dg/reciprocal-math-2.c: New test.
	* gcc.dg/rounding-math-1.c: New test.
	* gcc.dg/rounding-math-2.c: New test.

gcc/c-family/ChangeLog:

	* c-cppbuiltin.c (c_cpp_builtins_optimize_pragma): Define or
	undefine __RECIPROCAL_MATH__, __NO_SIGNED_ZEROS__,
	__NO_TRAPPING_MATH__, __ASSOCIATIVE_MATH__, and
	__ROUNDING_MATH__ according to the new optimization flags.

gcc/ChangeLog:

	* cppbuiltin.c (define_builtin_macros_for_compilation_flags):
	Define __RECIPROCAL_MATH__, __NO_SIGNED_ZEROS__,
	__NO_TRAPPING_MATH__, __ASSOCIATIVE_MATH__, and
	__ROUNDING_MATH__ according to their corresponding flags.
	* doc/cpp.texi: Document __RECIPROCAL_MATH__,
	__NO_SIGNED_ZEROS__, __NO_TRAPPING_MATH__, __ASSOCIATIVE_MATH__,
	and __ROUNDING_MATH__.
This commit is contained in:
Matthias Kretz 2021-06-30 10:41:33 +02:00
parent 71a4bdada5
commit 1949d7540a
13 changed files with 223 additions and 0 deletions

View File

@ -628,6 +628,31 @@ c_cpp_builtins_optimize_pragma (cpp_reader *pfile, tree prev_tree,
cpp_undef (pfile, "__FINITE_MATH_ONLY__");
cpp_define_unused (pfile, "__FINITE_MATH_ONLY__=0");
}
if (!prev->x_flag_reciprocal_math && cur->x_flag_reciprocal_math)
cpp_define_unused (pfile, "__RECIPROCAL_MATH__");
else if (prev->x_flag_reciprocal_math && !cur->x_flag_reciprocal_math)
cpp_undef (pfile, "__RECIPROCAL_MATH__");
if (!prev->x_flag_signed_zeros && cur->x_flag_signed_zeros)
cpp_undef (pfile, "__NO_SIGNED_ZEROS__");
else if (prev->x_flag_signed_zeros && !cur->x_flag_signed_zeros)
cpp_define_unused (pfile, "__NO_SIGNED_ZEROS__");
if (!prev->x_flag_trapping_math && cur->x_flag_trapping_math)
cpp_undef (pfile, "__NO_TRAPPING_MATH__");
else if (prev->x_flag_trapping_math && !cur->x_flag_trapping_math)
cpp_define_unused (pfile, "__NO_TRAPPING_MATH__");
if (!prev->x_flag_associative_math && cur->x_flag_associative_math)
cpp_define_unused (pfile, "__ASSOCIATIVE_MATH__");
else if (prev->x_flag_associative_math && !cur->x_flag_associative_math)
cpp_undef (pfile, "__ASSOCIATIVE_MATH__");
if (!prev->x_flag_rounding_math && cur->x_flag_rounding_math)
cpp_define_unused (pfile, "__ROUNDING_MATH__");
else if (prev->x_flag_rounding_math && !cur->x_flag_rounding_math)
cpp_undef (pfile, "__ROUNDING_MATH__");
}

View File

@ -110,6 +110,16 @@ define_builtin_macros_for_compilation_flags (cpp_reader *pfile)
cpp_define (pfile, "__SUPPORT_SNAN__");
if (!flag_errno_math)
cpp_define (pfile, "__NO_MATH_ERRNO__");
if (flag_reciprocal_math)
cpp_define (pfile, "__RECIPROCAL_MATH__");
if (!flag_signed_zeros)
cpp_define (pfile, "__NO_SIGNED_ZEROS__");
if (!flag_trapping_math)
cpp_define (pfile, "__NO_TRAPPING_MATH__");
if (flag_associative_math)
cpp_define (pfile, "__ASSOCIATIVE_MATH__");
if (flag_rounding_math)
cpp_define (pfile, "__ROUNDING_MATH__");
cpp_define_formatted (pfile, "__FINITE_MATH_ONLY__=%d",
flag_finite_math_only);

View File

@ -2462,6 +2462,24 @@ features are supported by GCC.
This macro is defined if @option{-fno-math-errno} is used, or enabled
by another option such as @option{-ffast-math} or by default.
@item __RECIPROCAL_MATH__
This macro is defined if @option{-freciprocal-math} is used, or enabled
by another option such as @option{-ffast-math} or by default.
@item __NO_SIGNED_ZEROS__
This macro is defined if @option{-fno-signed-zeros} is used, or enabled
by another option such as @option{-ffast-math} or by default.
@item __NO_TRAPPING_MATH__
This macro is defined if @option{-fno-trapping-math} is used.
@item __ASSOCIATIVE_MATH__
This macro is defined if @option{-fassociative-math} is used, or enabled
by another option such as @option{-ffast-math} or by default.
@item __ROUNDING_MATH__
This macro is defined if @option{-frounding-math} is used.
@item __GNUC_EXECUTION_CHARSET_NAME
@itemx __GNUC_WIDE_EXECUTION_CHARSET_NAME
These macros are defined to expand to a narrow string literal of

View File

@ -0,0 +1,17 @@
/* Test __ASSOCIATIVE_MATH__ is defined with -fassociative-math. */
/* { dg-do compile } */
/* { dg-options "-fassociative-math -fno-signed-zeros -fno-trapping-math" } */
#ifndef __ASSOCIATIVE_MATH__
#error "__ASSOCIATIVE_MATH__ not defined"
#endif
#pragma GCC optimize "-fno-associative-math"
#ifdef __ASSOCIATIVE_MATH__
#error "__ASSOCIATIVE_MATH__ defined"
#endif
#pragma GCC optimize "-fassociative-math"
#ifndef __ASSOCIATIVE_MATH__
#error "__ASSOCIATIVE_MATH__ not defined"
#endif

View File

@ -0,0 +1,17 @@
/* Test __ASSOCIATIVE_MATH__ is not defined with -fno-associative-math. */
/* { dg-do compile } */
/* { dg-options "-fno-associative-math" } */
#ifdef __ASSOCIATIVE_MATH__
#error "__ASSOCIATIVE_MATH__ defined"
#endif
#pragma GCC optimize "-fassociative-math"
#ifndef __ASSOCIATIVE_MATH__
#error "__ASSOCIATIVE_MATH__ not defined"
#endif
#pragma GCC optimize "-fno-associative-math"
#ifdef __ASSOCIATIVE_MATH__
#error "__ASSOCIATIVE_MATH__ defined"
#endif

View File

@ -0,0 +1,17 @@
/* Test __NO_SIGNED_ZEROS__ is defined with -fno-signed-zeros. */
/* { dg-do compile } */
/* { dg-options "-fno-signed-zeros" } */
#ifndef __NO_SIGNED_ZEROS__
#error "__NO_SIGNED_ZEROS__ not defined"
#endif
#pragma GCC optimize "-fsigned-zeros"
#ifdef __NO_SIGNED_ZEROS__
#error "__NO_SIGNED_ZEROS__ defined"
#endif
#pragma GCC optimize "-fno-signed-zeros"
#ifndef __NO_SIGNED_ZEROS__
#error "__NO_SIGNED_ZEROS__ not defined"
#endif

View File

@ -0,0 +1,17 @@
/* Test __NO_SIGNED_ZEROS__ is not defined with -fsigned-zeros. */
/* { dg-do compile } */
/* { dg-options "-fsigned-zeros" } */
#ifdef __NO_SIGNED_ZEROS__
#error "__NO_SIGNED_ZEROS__ defined"
#endif
#pragma GCC optimize "-fno-signed-zeros"
#ifndef __NO_SIGNED_ZEROS__
#error "__NO_SIGNED_ZEROS__ not defined"
#endif
#pragma GCC optimize "-fsigned-zeros"
#ifdef __NO_SIGNED_ZEROS__
#error "__NO_SIGNED_ZEROS__ defined"
#endif

View File

@ -0,0 +1,17 @@
/* Test __NO_TRAPPING_MATH__ is defined with -fno-trapping-math. */
/* { dg-do compile } */
/* { dg-options "-fno-trapping-math" } */
#ifndef __NO_TRAPPING_MATH__
#error "__NO_TRAPPING_MATH__ not defined"
#endif
#pragma GCC optimize "-ftrapping-math"
#ifdef __NO_TRAPPING_MATH__
#error "__NO_TRAPPING_MATH__ defined"
#endif
#pragma GCC optimize "-fno-trapping-math"
#ifndef __NO_TRAPPING_MATH__
#error "__NO_TRAPPING_MATH__ not defined"
#endif

View File

@ -0,0 +1,17 @@
/* Test __NO_TRAPPING_MATH__ is not defined with -ftrapping-math. */
/* { dg-do compile } */
/* { dg-options "-ftrapping-math" } */
#ifdef __NO_TRAPPING_MATH__
#error "__NO_TRAPPING_MATH__ defined"
#endif
#pragma GCC optimize "-fno-trapping-math"
#ifndef __NO_TRAPPING_MATH__
#error "__NO_TRAPPING_MATH__ not defined"
#endif
#pragma GCC optimize "-ftrapping-math"
#ifdef __NO_TRAPPING_MATH__
#error "__NO_TRAPPING_MATH__ defined"
#endif

View File

@ -0,0 +1,17 @@
/* Test __RECIPROCAL_MATH__ is defined with -freciprocal-math. */
/* { dg-do compile } */
/* { dg-options "-freciprocal-math" } */
#ifndef __RECIPROCAL_MATH__
#error "__RECIPROCAL_MATH__ not defined"
#endif
#pragma GCC optimize "-fno-reciprocal-math"
#ifdef __RECIPROCAL_MATH__
#error "__RECIPROCAL_MATH__ defined"
#endif
#pragma GCC optimize "-freciprocal-math"
#ifndef __RECIPROCAL_MATH__
#error "__RECIPROCAL_MATH__ not defined"
#endif

View File

@ -0,0 +1,17 @@
/* Test __RECIPROCAL_MATH__ is not defined with -fno-reciprocal-math. */
/* { dg-do compile } */
/* { dg-options "-fno-reciprocal-math" } */
#ifdef __RECIPROCAL_MATH__
#error "__RECIPROCAL_MATH__ defined"
#endif
#pragma GCC optimize "-freciprocal-math"
#ifndef __RECIPROCAL_MATH__
#error "__RECIPROCAL_MATH__ not defined"
#endif
#pragma GCC optimize "-fno-reciprocal-math"
#ifdef __RECIPROCAL_MATH__
#error "__RECIPROCAL_MATH__ defined"
#endif

View File

@ -0,0 +1,17 @@
/* Test __ROUNDING_MATH__ is defined with -frounding-math. */
/* { dg-do compile } */
/* { dg-options "-frounding-math" } */
#ifndef __ROUNDING_MATH__
#error "__ROUNDING_MATH__ not defined"
#endif
#pragma GCC optimize "-fno-rounding-math"
#ifdef __ROUNDING_MATH__
#error "__ROUNDING_MATH__ defined"
#endif
#pragma GCC optimize "-frounding-math"
#ifndef __ROUNDING_MATH__
#error "__ROUNDING_MATH__ not defined"
#endif

View File

@ -0,0 +1,17 @@
/* Test __ROUNDING_MATH__ is not defined with -fno-rounding-math. */
/* { dg-do compile } */
/* { dg-options "-fno-rounding-math" } */
#ifdef __ROUNDING_MATH__
#error "__ROUNDING_MATH__ defined"
#endif
#pragma GCC optimize "-frounding-math"
#ifndef __ROUNDING_MATH__
#error "__ROUNDING_MATH__ not defined"
#endif
#pragma GCC optimize "-fno-rounding-math"
#ifdef __ROUNDING_MATH__
#error "__ROUNDING_MATH__ defined"
#endif