From patchwork Mon Jul 24 16:01:55 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jakub Jelinek X-Patchwork-Id: 125077 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:9010:0:b0:3e4:2afc:c1 with SMTP id l16csp1894435vqg; Mon, 24 Jul 2023 09:03:00 -0700 (PDT) X-Google-Smtp-Source: APBJJlGnsGeoSSHO+AL8Ya5eQy1dqvZLmDng3327YVfDLeKNvegN+wYTrLY22iHQZmHWcNnZb7s4 X-Received: by 2002:a17:907:78d9:b0:99b:4e70:d09b with SMTP id kv25-20020a17090778d900b0099b4e70d09bmr10401889ejc.46.1690214580456; Mon, 24 Jul 2023 09:03:00 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1690214580; cv=none; d=google.com; s=arc-20160816; b=BJATDoroy9v1kYQvb8u2dj/akP2j4zvMruXN4+hb5/njKsGuRdoryg1nH+TLHIANeU ykf3XUha/pc5ZDGkLYuCuSKoO+WLApo0fyTh4/4FcwnLgE5llNa0I+Xc+BoC9PFcdyI2 qch3QREAwajJM96tCLzw9AxxBZ9tyB9ehQtoUiDqJckIuDcXj+cc1U+VxWMNjTgtmcb+ TI3HVCNiw7/n8T+8GNOeKMfhIJy/O74/3mdXQcOF7xn5r7x8oEf6MnW8NS7w3sYXlpmM AosU0pRfhv/xqWZzgNPvfqbNIsU+my4W1LI43Mv1iTdMfvM/vFEj2MtMn6wirg4AXdN+ aYjw== 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:mime-version:message-id:subject:cc:to:date :dmarc-filter:delivered-to:dkim-signature:dkim-filter; bh=y9yUkKKl8pBrpcy8HMI4EAdMVEcT1en3+aOITPYOUjs=; fh=yioimZF2NxgM0nRTfXDGjQGyw1m3Drmx+8M5h1V3VIg=; b=aMU5FAvL0dZ1MHScDmbDDwifeAUsX7gMh302RYUPDBKf3tzf3Fd/MyPpXkhVPUODdt oqMyIkCxa7daw5+WjJVP0L+sNx9/Xx+yg3szpPpXSl8lM0DLV5A5Zw7SyUY0AKfaMXoT Qhg4TwNhEZhDnwyppObZTGOFkWhPql3hia1gby4sDAcOPgmeHu9VMFxqjW9siP+K9isx Vd2RzOalPeVHNA51drZK7pqQy/222SOVgQnUZR8x42/otT28BdGj6QgotBY3dpsVsQnU FEQTqQPjfmvcABr2Rb95pt+xz7S/vuNNz92uFtAakVWPjt1ojTwk2dO5mTnRjE/Jm192 jxMQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=QxJab84j; 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 (ip-8-43-85-97.sourceware.org. [8.43.85.97]) by mx.google.com with ESMTPS id v11-20020a1709063bcb00b009895e9b8819si6355771ejf.1051.2023.07.24.09.03.00 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 24 Jul 2023 09:03:00 -0700 (PDT) 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=QxJab84j; 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 1147C3858421 for ; Mon, 24 Jul 2023 16:02:59 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 1147C3858421 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1690214579; bh=y9yUkKKl8pBrpcy8HMI4EAdMVEcT1en3+aOITPYOUjs=; h=Date:To:Cc:Subject:List-Id:List-Unsubscribe:List-Archive: List-Post:List-Help:List-Subscribe:From:Reply-To:From; b=QxJab84jlzeel7pnCXVciGe2mmbSjllr+vqe5b5ZiyGp2xasetFOpj1/ldyp1TNcY +JPmidBreyQdoB9VlFIBtFvNFyOVB9IP2kiNcoS3/5IlJSTICYH6X0fpmcLGqIv9vA H70L5QuzdbYiasBAUHFjjwkTE0ERF8/RD3hLGFdM= 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 096793858C5E for ; Mon, 24 Jul 2023 16:02:15 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 096793858C5E Received: from mimecast-mx02.redhat.com (66.187.233.73 [66.187.233.73]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-503-J5XfxL3aOomHbfNgJCnYMg-1; Mon, 24 Jul 2023 12:01:59 -0400 X-MC-Unique: J5XfxL3aOomHbfNgJCnYMg-1 Received: from smtp.corp.redhat.com (int-mx06.intmail.prod.int.rdu2.redhat.com [10.11.54.6]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 116B638008A7 for ; Mon, 24 Jul 2023 16:01:58 +0000 (UTC) Received: from tucnak.zalov.cz (unknown [10.45.224.18]) by smtp.corp.redhat.com (Postfix) with ESMTPS id C9A472166B25; Mon, 24 Jul 2023 16:01:57 +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 36OG1tvP3987116 (version=TLSv1.3 cipher=TLS_AES_256_GCM_SHA384 bits=256 verify=NOT); Mon, 24 Jul 2023 18:01:56 +0200 Received: (from jakub@localhost) by tucnak.zalov.cz (8.17.1/8.17.1/Submit) id 36OG1tCX3987113; Mon, 24 Jul 2023 18:01:55 +0200 Date: Mon, 24 Jul 2023 18:01:55 +0200 To: Aldy Hernandez , Andrew MacLeod Cc: gcc-patches@gcc.gnu.org Subject: [PATCH] range-op-float: Fix up -frounding-math frange_arithmetic +- handling [PR110755] Message-ID: MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.1 on 10.11.54.6 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Disposition: inline X-Spam-Status: No, score=-3.7 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_H4, RCVD_IN_MSPIKE_WL, SPF_HELO_NONE, SPF_NONE, TXREP, T_SCC_BODY_TEXT_LINE 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: INBOX X-GMAIL-THRID: 1772318443867656189 X-GMAIL-MSGID: 1772318443867656189 Hi! IEEE754 says that x + (-x) and x - x result in +0 in all rounding modes but rounding towards negative infinity, in which case the result is -0 for all finite x. x + x and x - (-x) if it is zero retain sign of x. Now, range_arithmetic implements the normal rounds to even rounding, and as the addition or subtraction in those cases is exact, we don't do any further rounding etc. and e.g. on the testcase below distilled from glibc compute a range [+0, +INF], which is fine for -fno-rounding-math or if we'd have a guarantee that those statements aren't executed with rounding towards negative infinity. I believe it is only +- which has this problematic behavior and I think it is best to deal with it in frange_arithmetic; if we know -frounding-math is on, it is x + (-x) or x - x and we are asked to round to negative infinity (i.e. want low bound rather than high bound), change +0 result to -0. Bootstrapped/regtested on x86_64-linux and i686-linux, ok for trunk and after a while for 13.3? I'm afraid rushing this so late into 13.2... 2023-07-24 Jakub Jelinek PR tree-optimization/110755 * range-op-float.cc (frange_arithmetic): Change +0 result to -0 for PLUS_EXPR or MINUS_EXPR if -frounding-math, inf is negative and it is exact op1 + (-op1) or op1 - op1. * gcc.dg/pr110755.c: New test. Jakub --- gcc/range-op-float.cc.jj 2023-07-23 19:32:20.832434105 +0200 +++ gcc/range-op-float.cc 2023-07-24 09:41:26.231030258 +0200 @@ -324,6 +324,24 @@ frange_arithmetic (enum tree_code code, bool inexact = real_arithmetic (&value, code, &op1, &op2); real_convert (&result, mode, &value); + /* When rounding towards negative infinity, x + (-x) and + x - x is -0 rather than +0 real_arithmetic computes. + So, when we are looking for lower bound (inf is negative), + use -0 rather than +0. */ + if (flag_rounding_math + && (code == PLUS_EXPR || code == MINUS_EXPR) + && !inexact + && real_iszero (&result) + && !real_isneg (&result) + && real_isneg (&inf)) + { + REAL_VALUE_TYPE op2a = op2; + if (code == PLUS_EXPR) + op2a.sign ^= 1; + if (real_isneg (&op1) == real_isneg (&op2a) && real_equal (&op1, &op2a)) + result.sign = 1; + } + // Be extra careful if there may be discrepancies between the // compile and runtime results. bool round = false; --- gcc/testsuite/gcc.dg/pr110755.c.jj 2023-07-21 10:34:05.037251433 +0200 +++ gcc/testsuite/gcc.dg/pr110755.c 2023-07-21 10:35:10.986326816 +0200 @@ -0,0 +1,29 @@ +/* PR tree-optimization/110755 */ +/* { dg-do run } */ +/* { dg-require-effective-target fenv } */ +/* { dg-require-effective-target hard_float } */ +/* { dg-options "-O2 -frounding-math" } */ + +#include + +__attribute__((noipa)) float +foo (float x) +{ + if (x > 0.0) + { + x += 0x1p+23; + x -= 0x1p+23; + x = __builtin_fabsf (x); + } + return x; +} + +int +main () +{ +#ifdef FE_DOWNWARD + fesetround (FE_DOWNWARD); + if (__builtin_signbit (foo (0.5))) + __builtin_abort (); +#endif +}