On Wed, Oct 27, 2021 at 03:20:29PM +0200, Richard Biener via Gcc-patches wrote:
> The following honors -frounding-math when converting a FP constant
> to another FP type.
>
> Bootstrapped and tested on x86_64-unknown-linux-gnu, OK?
>
> I wonder what a good way to test this in a portable way, the bugreport
> unfortunately didn't contain something executable and I don't see
> much -frounding-math test coverage to copy from.
E.g. following tests call fesetround, use fenv effective target etc.:
torture/fp-int-convert-float128-timode-3.c: fesetround (FE_TOWARDZERO);
torture/fp-int-convert-timode-2.c: fesetround (FE_DOWNWARD);
torture/fp-int-convert-timode-3.c: fesetround (FE_UPWARD);
torture/fp-int-convert-timode-4.c: fesetround (FE_TOWARDZERO);
And the test can just hardcode one or more common float/double etc.
configurations, checked using
__{FLT,DBL}_{DIG,MANT_DIG,RADIX,MIN_EXP,MAX_EXP}__ etc. macros.
Say just test double to float conversions of some specific values assuming
float is IEEE754 single precicion and double is IEEE754 double precision
in all the 4 rounding modes.
> 2021-10-27 Richard Biener <[email protected]>
>
> PR middle-end/57245
> * fold-const.c (fold_convert_const_real_from_real): Honor
> -frounding-math if the conversion is not exact.
> ---
> gcc/fold-const.c | 6 ++++++
> 1 file changed, 6 insertions(+)
>
> diff --git a/gcc/fold-const.c b/gcc/fold-const.c
> index ff23f12f33c..c7aebf9cc7e 100644
> --- a/gcc/fold-const.c
> +++ b/gcc/fold-const.c
> @@ -2139,6 +2139,12 @@ fold_convert_const_real_from_real (tree type,
> const_tree arg1)
> && REAL_VALUE_ISSIGNALING_NAN (TREE_REAL_CST (arg1)))
> return NULL_TREE;
>
> + /* With flag_rounding_math we shuld respect the current rounding mode
s/shuld/should/
> + unless the conversion is exact. */
> + if (HONOR_SIGN_DEPENDENT_ROUNDING (arg1)
> + && !exact_real_truncate (TYPE_MODE (type), &TREE_REAL_CST (arg1)))
> + return NULL_TREE;
> +
> real_convert (&value, TYPE_MODE (type), &TREE_REAL_CST (arg1));
> t = build_real (type, value);
Jakub