From patchwork Thu Oct 28 11:32:17 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Richard Biener X-Patchwork-Id: 46738 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 9A82B3857C47 for ; Thu, 28 Oct 2021 11:32:48 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 9A82B3857C47 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1635420768; bh=FqXDBIdaUepguot/xKCU445yAY6bkJOLUi1eW0G0uF0=; h=Date:To:Subject:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:Cc:From; b=P21vkWVAKlzkUb7qsnhVGDR0oHf2xa4t9o2Gm2/b0MYPXrkGBS9y66b3B0qomzGQA mlrKZgVkFLhxfoCKlYu/CH9Rw7OKpAIyrQHeoE0KD9Gb3xkEsBVkTFrX1+8xpEZ4bl jNWsSIb/CAu5f8Fkd8lcXsmf7gIJa2vtt4n+Kd14= X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from smtp-out2.suse.de (smtp-out2.suse.de [195.135.220.29]) by sourceware.org (Postfix) with ESMTPS id 36E793858D35 for ; Thu, 28 Oct 2021 11:32:19 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 36E793858D35 Received: from imap2.suse-dmz.suse.de (imap2.suse-dmz.suse.de [192.168.254.74]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-521) server-digest SHA512) (No client certificate requested) by smtp-out2.suse.de (Postfix) with ESMTPS id 0CD6F1FD3D; Thu, 28 Oct 2021 11:32:18 +0000 (UTC) Received: from imap2.suse-dmz.suse.de (imap2.suse-dmz.suse.de [192.168.254.74]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-521) server-digest SHA512) (No client certificate requested) by imap2.suse-dmz.suse.de (Postfix) with ESMTPS id E100513B88; Thu, 28 Oct 2021 11:32:17 +0000 (UTC) Received: from dovecot-director2.suse.de ([192.168.254.65]) by imap2.suse-dmz.suse.de with ESMTPSA id Eu/KNUGKemE+RwAAMHmgww (envelope-from ); Thu, 28 Oct 2021 11:32:17 +0000 Date: Thu, 28 Oct 2021 13:32:17 +0200 (CEST) To: gcc-patches@gcc.gnu.org Subject: [PATCH] middle-end/84407 - honor -frounding-math for int to float conversion Message-ID: <2qq2o881-1r56-7ooq-37o7-70n599n773r8@fhfr.qr> MIME-Version: 1.0 X-Spam-Status: No, score=-11.9 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.4 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on server2.sourceware.org X-BeenThere: gcc-patches@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: Richard Biener via Gcc-patches From: Richard Biener Reply-To: Richard Biener Cc: jakub@redhat.com, joseph@codesourcery.com Errors-To: gcc-patches-bounces+patchwork=sourceware.org@gcc.gnu.org Sender: "Gcc-patches" This makes us honor -frounding-math for integer to float conversions and avoid constant folding when such conversion is not exact. Bootstrapped and tested on x86_64-unknown-linux-gnu, OK? Thanks, Richard. 2021-10-28 Richard Biener PR middle-end/84407 * fold-const.c (fold_convert_const): Avoid int to float constant folding with -frounding-math and inexact result. * simplify-rtx.c (simplify_const_unary_operation): Likewise. * gcc.dg/torture/fp-uint64-convert-double-1.c: New testcase. --- gcc/fold-const.c | 15 +++- gcc/simplify-rtx.c | 13 ++++ .../torture/fp-uint64-convert-double-1.c | 74 +++++++++++++++++++ 3 files changed, 101 insertions(+), 1 deletion(-) create mode 100644 gcc/testsuite/gcc.dg/torture/fp-uint64-convert-double-1.c diff --git a/gcc/fold-const.c b/gcc/fold-const.c index 18950aeb760..c7daf871125 100644 --- a/gcc/fold-const.c +++ b/gcc/fold-const.c @@ -2290,7 +2290,20 @@ fold_convert_const (enum tree_code code, tree type, tree arg1) else if (TREE_CODE (type) == REAL_TYPE) { if (TREE_CODE (arg1) == INTEGER_CST) - return build_real_from_int_cst (type, arg1); + { + tree res = build_real_from_int_cst (type, arg1); + /* Avoid the folding if flag_rounding_math is on and the + conversion is not exact. */ + if (HONOR_SIGN_DEPENDENT_ROUNDING (type)) + { + bool fail = false; + wide_int w = real_to_integer (&TREE_REAL_CST (res), &fail, + TYPE_PRECISION (TREE_TYPE (arg1))); + if (fail || wi::ne_p (w, wi::to_wide (arg1))) + return NULL_TREE; + } + return res; + } else if (TREE_CODE (arg1) == REAL_CST) return fold_convert_const_real_from_real (type, arg1); else if (TREE_CODE (arg1) == FIXED_CST) diff --git a/gcc/simplify-rtx.c b/gcc/simplify-rtx.c index f38b6d7d31c..a16395befcd 100644 --- a/gcc/simplify-rtx.c +++ b/gcc/simplify-rtx.c @@ -1917,6 +1917,19 @@ simplify_const_unary_operation (enum rtx_code code, machine_mode mode, return 0; d = real_value_truncate (mode, d); + + /* Avoid the folding if flag_rounding_math is on and the + conversion is not exact. */ + if (HONOR_SIGN_DEPENDENT_ROUNDING (mode)) + { + bool fail = false; + wide_int w = real_to_integer (&d, &fail, + GET_MODE_PRECISION + (as_a (op_mode))); + if (fail || wi::ne_p (w, wide_int (rtx_mode_t (op, op_mode)))) + return 0; + } + return const_double_from_real_value (d, mode); } else if (code == UNSIGNED_FLOAT && CONST_SCALAR_INT_P (op)) diff --git a/gcc/testsuite/gcc.dg/torture/fp-uint64-convert-double-1.c b/gcc/testsuite/gcc.dg/torture/fp-uint64-convert-double-1.c new file mode 100644 index 00000000000..b40a16a2257 --- /dev/null +++ b/gcc/testsuite/gcc.dg/torture/fp-uint64-convert-double-1.c @@ -0,0 +1,74 @@ +/* PR84407 */ +/* { dg-do run } */ +/* { dg-require-effective-target fenv } */ +/* { dg-additional-options "-frounding-math" } */ + +#include +#include + +void __attribute__((noipa)) +fooa () +{ +#if __DBL_MANT_DIG__ == 53 +#ifdef FE_TONEAREST + fesetround(FE_TONEAREST); + __UINT64_TYPE__ x = 0x7fffffffffffffff; + double f = x; + if (f != 0x1p+63) + abort (); +#endif +#endif +} + +void __attribute__((noipa)) +foob () +{ +#if __DBL_MANT_DIG__ == 53 +#ifdef FE_DOWNWARD + fesetround(FE_DOWNWARD); + __UINT64_TYPE__ x = 0x7fffffffffffffff; + double f = x; + if (f != 0x1.fffffffffffffp+62) + abort (); +#endif +#endif +} + +void __attribute__((noipa)) +fooc () +{ +#if __DBL_MANT_DIG__ == 53 +#ifdef FE_UPWARD + fesetround(FE_UPWARD); + __UINT64_TYPE__ x = 0x7fffffffffffffff; + double f = x; + if (f != 0x1p+63) + abort (); +#endif +#endif +} + +void __attribute__((noipa)) +food () +{ +#if __DBL_MANT_DIG__ == 53 +#ifdef FE_TOWARDZERO + fesetround(FE_TOWARDZERO); + __UINT64_TYPE__ x = 0x7fffffffffffffff; + double f = x; + if (f != 0x1.fffffffffffffp+62) + abort (); +#endif +#endif +} + + +int +main () +{ + fooa (); + foob (); + fooc (); + food (); + return 0; +}