From patchwork Wed Dec 7 15:49:49 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jakub Jelinek X-Patchwork-Id: 30900 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:adf:f944:0:0:0:0:0 with SMTP id q4csp260043wrr; Wed, 7 Dec 2022 07:50:43 -0800 (PST) X-Google-Smtp-Source: AA0mqf7cG+TLuNttZlMZXUSMazzahfftcwjqDzsW18lLZrY1CdaRX8MyDHjEMKZFYZhoO9fg0vG6 X-Received: by 2002:a17:906:d963:b0:7c1:98f:be57 with SMTP id rp3-20020a170906d96300b007c1098fbe57mr6998102ejb.97.1670428243172; Wed, 07 Dec 2022 07:50:43 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1670428243; cv=none; d=google.com; s=arc-20160816; b=lPq1DP2txDg/bVt0X1XuvL8OkGXF9iPIUavBsguOy6VRw9CiFQQApVY2djLmBKhBb/ R1M71NWsyZciqCY70DZROgKbwk2yiFcGK16eZpNp6ZNJ0Q1tXI8+ioxQjPPLlaYSCjq9 paAZFzpUsrFHBWha5VJ9HwlSZ90XU/SucnmXBXsQnb7GJHqAGtBuVX40Ae/YR6x3aa5W 0twJMe3ERbxiJidDJqTBSbLYDHiGFwZFnAxk9hjlufgGtdDdH1FzHyMy0mLHAIcIraDm MtvOGkUhfH4Xjfrkgl7rLyBo14M8rxltCjzIslaL0WrbdZYAUcD/ecUuMjXe1du73r05 L9fg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:reply-to:from:list-subscribe:list-help:list-post :list-archive:list-unsubscribe:list-id:precedence :content-disposition:in-reply-to:mime-version:references:message-id :subject:cc:to:date:dmarc-filter:delivered-to:dkim-signature :dkim-filter; bh=GdaP7Itnf3w1aX3bGN6x+l9HgsWtU+D/jYcOX/ETaF4=; b=hb9pCs9Gu89DkfczYcqKdQQ+fTmjxI5BnzaC1J6FmHpT3TmgWl1eEghfD0S0E/thC8 NY9SwAcjpNyVkELOUXoBvi3q3euFzTdLqpmeQtaCjl6xEXyIm4nD2lRxhRBte+5sfjXE 8HyCSt7HS7tBljmf/Nck5Pd/3V7zojiHvRS+rAllb6qqQKcDp2CFUk1P79a2Ho7/K5aO 04z2gbJtDgWHuIf6H3P9xYdwEGlZqp0p2JljOXrNB8FR7PBd/YIiAdBHAkAOw22C0SYP o7TntjCC6Fjs+uwjg5VImecnlMqIJI37qSkwtke2W8Pet4LnIrm/T8ektZ0h0VOTuLFk LLpg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=ioGdSq5+; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=gnu.org Received: from sourceware.org (ip-8-43-85-97.sourceware.org. [8.43.85.97]) by mx.google.com with ESMTPS id j27-20020a170906279b00b007c0bd0edd72si9813984ejc.163.2022.12.07.07.50.43 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 07 Dec 2022 07:50:43 -0800 (PST) Received-SPF: pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 as permitted sender) client-ip=8.43.85.97; Authentication-Results: mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=ioGdSq5+; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=gnu.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 30923390CE89 for ; Wed, 7 Dec 2022 15:50:42 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 30923390CE89 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1670428242; bh=GdaP7Itnf3w1aX3bGN6x+l9HgsWtU+D/jYcOX/ETaF4=; h=Date:To:Cc:Subject:References:In-Reply-To:List-Id: List-Unsubscribe:List-Archive:List-Post:List-Help:List-Subscribe: From:Reply-To:From; b=ioGdSq5+G1fq6m623+NHSZBRoK0fpUQSmM5JOASfIihH0/IKEGvVNI5hfVBhjnxb6 vF37TnZ1tBlr7Xz6N1f+RpaFdefvqAypM3DXcuAz9VGyZ6EqmY/sw3oREjsRKwkT/I l4+snaXrgzWz9fjJBTAdI/ZcuoSz5bwKGbYhqcyI= X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by sourceware.org (Postfix) with ESMTPS id 026203833A0E for ; Wed, 7 Dec 2022 15:49:57 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 026203833A0E Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-589-C-gR0eEpPsKYYgKsqDwC0A-1; Wed, 07 Dec 2022 10:49:56 -0500 X-MC-Unique: C-gR0eEpPsKYYgKsqDwC0A-1 Received: from smtp.corp.redhat.com (int-mx10.intmail.prod.int.rdu2.redhat.com [10.11.54.10]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 34B77811E81 for ; Wed, 7 Dec 2022 15:49:56 +0000 (UTC) Received: from tucnak.zalov.cz (unknown [10.39.195.114]) by smtp.corp.redhat.com (Postfix) with ESMTPS id E8039492B04; Wed, 7 Dec 2022 15:49:55 +0000 (UTC) Received: from tucnak.zalov.cz (localhost [127.0.0.1]) by tucnak.zalov.cz (8.17.1/8.17.1) with ESMTPS id 2B7FnpU5098220 (version=TLSv1.3 cipher=TLS_AES_256_GCM_SHA384 bits=256 verify=NOT); Wed, 7 Dec 2022 16:49:51 +0100 Received: (from jakub@localhost) by tucnak.zalov.cz (8.17.1/8.17.1/Submit) id 2B7Fnot4098219; Wed, 7 Dec 2022 16:49:50 +0100 Date: Wed, 7 Dec 2022 16:49:49 +0100 To: Aldy Hernandez Cc: gcc-patches@gcc.gnu.org, andrew MacLeod Subject: [PATCH] range-op-float, v2: Fix up frange_arithmetic [PR107967] Message-ID: References: MIME-Version: 1.0 In-Reply-To: X-Scanned-By: MIMEDefang 3.1 on 10.11.54.10 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Disposition: inline X-Spam-Status: No, score=-3.5 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H2, SPF_HELO_NONE, SPF_NONE, TXREP autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) 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: Jakub Jelinek via Gcc-patches From: Jakub Jelinek Reply-To: Jakub Jelinek Errors-To: gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org Sender: "Gcc-patches" X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1751570965529833951?= X-GMAIL-MSGID: =?utf-8?q?1751570965529833951?= On Wed, Dec 07, 2022 at 04:26:14PM +0100, Aldy Hernandez wrote: > This chunk... > > ...is quite similar to this one. Could you abstract this? It differs in various small details, plus comment content. Anyway, here it is reworked such that those various small details are based on whether inf is negative or positive in a single piece of code. Is this ok if it passes bootstrap/regtest? 2022-12-07 Jakub Jelinek PR tree-optimization/107967 * range-op-float.cc (frange_arithmetic): Fix a thinko - if inf is negative, use nextafter if !real_less (&result, &value) rather than if real_less (&result, &value). If result is +-INF while value is finite and -fno-rounding-math, don't do rounding if !inexact or if result is significantly above max representable value or below min representable value. * gcc.dg/pr107967-1.c: New test. * gcc.dg/pr107967-2.c: New test. * gcc.dg/pr107967-3.c: New test. Jakub --- gcc/range-op-float.cc.jj 2022-12-07 16:37:05.285143250 +0100 +++ gcc/range-op-float.cc 2022-12-07 16:41:58.500928517 +0100 @@ -287,9 +287,42 @@ frange_arithmetic (enum tree_code code, // Be extra careful if there may be discrepancies between the // compile and runtime results. - if ((mode_composite || (real_isneg (&inf) ? real_less (&result, &value) - : !real_less (&value, &result))) - && (inexact || !real_identical (&result, &value))) + bool round = false; + if (mode_composite) + round = true; + else + { + bool low = real_isneg (&inf); + round = (low ? !real_less (&result, &value) + : !real_less (&value, &result)); + if (real_isinf (&result, !low) + && !real_isinf (&value) + && !flag_rounding_math) + { + // Use just [+INF, +INF] rather than [MAX, +INF] + // even if value is larger than MAX and rounds to + // nearest to +INF. Similarly just [-INF, -INF] + // rather than [-INF, +MAX] even if value is smaller + // than -MAX and rounds to nearest to -INF. + // Unless INEXACT is true, in that case we need some + // extra buffer. + if (!inexact) + round = false; + else + { + REAL_VALUE_TYPE tmp = result, tmp2; + frange_nextafter (mode, tmp, inf); + // TMP is at this point the maximum representable + // number. + real_arithmetic (&tmp2, MINUS_EXPR, &value, &tmp); + if (real_isneg (&tmp2) != low + && (REAL_EXP (&tmp2) - REAL_EXP (&tmp) + >= 2 - REAL_MODE_FORMAT (mode)->p)) + round = false; + } + } + } + if (round && (inexact || !real_identical (&result, &value))) { if (mode_composite) { --- gcc/testsuite/gcc.dg/pr107967-1.c.jj 2022-12-07 16:38:07.519248686 +0100 +++ gcc/testsuite/gcc.dg/pr107967-1.c 2022-12-07 16:38:07.519248686 +0100 @@ -0,0 +1,35 @@ +/* PR tree-optimization/107967 */ +/* { dg-do compile { target float64 } } */ +/* { dg-options "-O2 -frounding-math -fno-trapping-math -fdump-tree-optimized" } */ +/* { dg-add-options float64 } */ +/* { dg-final { scan-tree-dump-not "return\[ \t]\*-?Inf;" "optimized" } } */ + +_Float64 +foo (void) +{ + const _Float64 huge = 1.0e+300f64; + return huge * huge; +} + +_Float64 +bar (void) +{ + const _Float64 huge = 1.0e+300f64; + return huge * -huge; +} + +_Float64 +baz (void) +{ + const _Float64 a = 0x1.fffffffffffffp+1023f64; + const _Float64 b = 0x1.fffffffffffffp+970f64; + return a + b; +} + +_Float64 +qux (void) +{ + const _Float64 a = 0x1.fffffffffffffp+1023f64; + const _Float64 b = 0x1.fffffffffffffp+969f64; + return a + b; +} --- gcc/testsuite/gcc.dg/pr107967-2.c.jj 2022-12-07 16:38:07.519248686 +0100 +++ gcc/testsuite/gcc.dg/pr107967-2.c 2022-12-07 16:38:07.519248686 +0100 @@ -0,0 +1,35 @@ +/* PR tree-optimization/107967 */ +/* { dg-do compile { target float64 } } */ +/* { dg-options "-O2 -fno-rounding-math -fno-trapping-math -fdump-tree-optimized" } */ +/* { dg-add-options float64 } */ +/* { dg-final { scan-tree-dump-times "return\[ \t]\*-?Inf;" 3 "optimized" } } */ + +_Float64 +foo (void) +{ + const _Float64 huge = 1.0e+300f64; + return huge * huge; +} + +_Float64 +bar (void) +{ + const _Float64 huge = 1.0e+300f64; + return huge * -huge; +} + +_Float64 +baz (void) +{ + const _Float64 a = 0x1.fffffffffffffp+1023f64; + const _Float64 b = 0x1.fffffffffffffp+970f64; + return a + b; +} + +_Float64 +qux (void) +{ + const _Float64 a = 0x1.fffffffffffffp+1023f64; + const _Float64 b = 0x1.fffffffffffffp+969f64; + return a + b; +} --- gcc/testsuite/gcc.dg/pr107967-3.c.jj 2022-12-07 16:38:07.519248686 +0100 +++ gcc/testsuite/gcc.dg/pr107967-3.c 2022-12-07 16:38:07.519248686 +0100 @@ -0,0 +1,53 @@ +/* PR tree-optimization/107967 */ +/* { dg-do compile { target float64 } } */ +/* { dg-options "-O2 -fno-rounding-math -fno-trapping-math -fdump-tree-optimized" } */ +/* { dg-add-options float64 } */ +/* { dg-final { scan-tree-dump-times "return\[ \t]\*-?Inf;" 3 "optimized" } } */ + +_Float64 +foo (_Float64 x) +{ + if (x >= 1.0e+300f64) + ; + else + __builtin_unreachable (); + return x * x; +} + +_Float64 +bar (_Float64 x) +{ + if (x >= 1.0e+300f64) + ; + else + __builtin_unreachable (); + return x * -x; +} + +_Float64 +baz (_Float64 a, _Float64 b) +{ + if (a >= 0x1.fffffffffffffp+1023f64) + ; + else + __builtin_unreachable (); + if (b >= 0x1.p+972f64) + ; + else + __builtin_unreachable (); + return a + b; +} + +_Float64 +qux (_Float64 a, _Float64 b) +{ + if (a >= 0x1.fffffffffffffp+1023f64) + ; + else + __builtin_unreachable (); + if (b >= 0x1.fffffffffffffp+969f64) + ; + else + __builtin_unreachable (); + return a + b; +}