From patchwork Sun Mar 19 20:44:14 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Harald Anlauf X-Patchwork-Id: 71868 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:604a:0:0:0:0:0 with SMTP id j10csp867261wrt; Sun, 19 Mar 2023 13:45:02 -0700 (PDT) X-Google-Smtp-Source: AK7set/Xc7F0Ig8MT0Cb8XLXhjKk52BVOtEu31Lfc3ONO9Y3Zlntf/RYIoCJ3UwDUUHGT1FYHukk X-Received: by 2002:a17:906:3845:b0:931:fee9:e961 with SMTP id w5-20020a170906384500b00931fee9e961mr6809553ejc.62.1679258702248; Sun, 19 Mar 2023 13:45:02 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1679258702; cv=none; d=google.com; s=arc-20160816; b=YDzgOTbGb7xUSDXAsm39HeJn7V50PX7B8VtlsG5SB+SjkiKZtJKPeQrfe1a8nSny5p h8mCOqkDtGJC3JA3wCVQKqvPkyzRWKhAvPdCc7zIbguolFGTNwbyyDvjl6MFC0aMTXFx zoaLMmX1QApFrjo1XDRPAFFekwszjjxf6W9PTipRxePEJ0M8Uw8umgeTRbd2lmfG0lGk ECuL7yawMZupWYDBmBx0EjonAco/xeiLaV56WIvqp+F2wLBYLHGqI1dbEPHEKb5dvGjB ji4M5ZHNOU4Re+Xru2nddLCEyaLZKHnZsLA41l9EO+ttUgSB8iJ2HhE53vCDWmm1b9lF MKyg== 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:ui-outboundreport :sensitivity:importance:date:subject:to:message-id:mime-version :dmarc-filter:delivered-to:dkim-signature:dkim-filter; bh=jU/vpvzxOOLlxcHiZH32KGZfTLqf6ysjA2ckuekaSoY=; b=mjhPR0moiC5EsGL1ISFFrszGldbRIkvQdj2ncQcKO7bpZQo11v2ZPLVuWm5kAZaQ7t bAKJLpzSDxKcbxWfKlPTCaGTUtfmY0YGbAc3fzsVlrVgjxXG/bWvnJCuE1Lx0nWK1duF IbYll2HVO11pk2v53buOhv2NchiI51f9YR6BIASagBHD86ENjI78hJyZCKfHydKPxB2T DfKs4o3VqwNp8c+cHDSgkPW6pyqpyzJuGctaPRGT1Rc1lqCvvSfTO0hs/7muEaLU13BV JXB+dbaGGxSmOZHPzLMT2h+lv9ruAfv6r4QS1NXStLAbLuvMzIkOnlPzlu/Bz5YyI9Cw Wnaw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=ZRoGiGxc; 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 (server2.sourceware.org. [8.43.85.97]) by mx.google.com with ESMTPS id o12-20020aa7c7cc000000b0049e34ecef4asi8749866eds.5.2023.03.19.13.45.01 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 19 Mar 2023 13:45:02 -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=ZRoGiGxc; 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 2788D385B50D for ; Sun, 19 Mar 2023 20:45:01 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 2788D385B50D DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1679258701; bh=jU/vpvzxOOLlxcHiZH32KGZfTLqf6ysjA2ckuekaSoY=; h=To:Subject:Date:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:From; b=ZRoGiGxcMc1NfxxPuvH+LQ1Brq+idkwsEl+802UrLI0smFaOzDXCdIuDkg8I5EX+r R73Nvuj0KxKzsb8twstj25MJdRNiiA2/sPPdyxTi540Jn85W16E+S9/e4HoV4Ltm7/ 5Kd1F/EGj+SBAtwcHQiRBXAis7NyFRdw8BbgRXsQ= X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from mout.gmx.net (mout.gmx.net [212.227.15.18]) by sourceware.org (Postfix) with ESMTPS id 99D753858D37; Sun, 19 Mar 2023 20:44:15 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 99D753858D37 X-UI-Sender-Class: 724b4f7f-cbec-4199-ad4e-598c01a50d3a Received: from [93.207.90.211] ([93.207.90.211]) by web-mail.gmx.net (3c-app-gmx-bs65.server.lan [172.19.170.209]) (via HTTP); Sun, 19 Mar 2023 21:44:14 +0100 MIME-Version: 1.0 Message-ID: To: fortran , gcc-patches Subject: [PATCH] Fortran: simplification of NEAREST for large argument [PR109186] Date: Sun, 19 Mar 2023 21:44:14 +0100 Importance: normal Sensitivity: Normal X-Priority: 3 X-Provags-ID: V03:K1:Xj10yUNEqLabw3l/RAmz+INLauA7deXbB7wyjj4v44AO2KuN7PZHJVRxTs1M0HYdg8+F0 fNwACiw0u0kgIyfus7beVhBCrDvpnzX06/UsbviiEiS1P7ckGa3bMgfUKGDcmJnA4QvVYWgElucw /OeVFq0/nTY2dXbCEausZbkSqf4JOnYV4avLtN8QRSt67UOdh8fejrinW6p4vnpZx0k69j6LlMtd ZVx6X2aA5RsWIDzcaucEOyx7M5cn1XgYT7AQeu2wXEMI6sXCwnuhPhJ4iXnN8vRVly13T7jGAOfl Mw= UI-OutboundReport: notjunk:1;M01:P0:rUgCnZRwmfs=;qadlRwV/kRiYFaxTxDR70n2CJua QN1sGW5skAqNxs5Y3IYLhidCeiR5spzNkcR+h2XbfhlkIdPkV0WqtFyjAlAYOKJLb1p729slX +vfI3NzVXfi0Uih4dtWxyv/NdT73HUsSM0lMFW/8Oe7VqRtSQYs4N2fNuiT/JvDX5WDYpKy5F oDo1Wz2FrYETd949owriEIOZZmrm34kiEIONzZ3PhhFTmSz9KMEHUX+02C7z0KYjcTtUYUcXL lZFqY3k9RHAlhPN1eYC/mX+zloYKXp5aJazPtBV8Lj7PrDZ2enNr6mAu1eVa403UsRYSRzGAv XGrcLdEbKI4KycILTzmsB3Gs45kWUrdMJbxBE23p2rivFc++NSUbb7cbds0ijQ5A7B4E+EQPs IO9n02Sfkf1iqbkCBbtVHYQu4xpbV+iMEG/86WcsVKQsyIQcQquxDz5inXIRu57CWjIhV9YXH fEWYqEqbdWWBquHRHN2PE4TCmtyTdzjS40DbMwoIYTAsWgChIYNt77pkJqF51wlrni2Bh03S8 8yICvck25V6qbBTOZDgedChlrKZ53uOHilac2ddWLWEk/gRYPqGvgdblePTluRKgYcTfdLenL 3EDD8EGAt8VIf1NlzH7z8Ph2CeJ9PwPXHuz4vcvl+12yqkuUnkDy6lQinX6rBDh+ujOFlelzh ScyzXsIJHZBTPoi9EVgbA9g3tHywMH3XMQDI+ZhenKu8+O95Wwl7ygbelDeSysXP+L9x25m39 fXzE2BELy0ed/kkUtNaPwAmLZ3qzYVjCtO0N9srVrT7PESzwEjwETkkKIV35RikBn8ni3DcRW fLafGMjeMJP+5Z4nIrdQ1q5CDmVGfj65kn6iLHWnxtlVo= X-Spam-Status: No, score=-12.1 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, FREEMAIL_FROM, GIT_PATCH_0, RCVD_IN_DNSWL_LOW, RCVD_IN_MSPIKE_H2, SPF_HELO_NONE, SPF_PASS, 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: Harald Anlauf via Gcc-patches From: Harald Anlauf Reply-To: Harald Anlauf 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?1760830372972928637?= X-GMAIL-MSGID: =?utf-8?q?1760830372972928637?= Dear all, I intend to commit the attached obvious patch within 24h unless there are comments. The issue is an off-by-one error in setting up the maximum exponent of the real kind that is passed to mpfr, so that model numbers between huge(x)/2 and huge(x), when given as an argument to NEAREST(arg,-1.0), are rounded down to huge(x)/2 during compile-time simplification. As no such issue is observed at run-time, the testcase compares the compile-time and run-time results for corner cases. Regtested on x86_64-pc-linux-gnu. As this is sort of a wrong-code issue, I intend to backport to all open branches. (The issue was apparently introduced in r0-84566-gb6f63e898498e6 without noticing, so it is technically a regression.) Thanks, Harald From 9391bd0eeef8e069d9e49f9aa277160b43aaf4f3 Mon Sep 17 00:00:00 2001 From: Harald Anlauf Date: Sun, 19 Mar 2023 21:29:46 +0100 Subject: [PATCH] Fortran: simplification of NEAREST for large argument [PR109186] gcc/fortran/ChangeLog: PR fortran/109186 * simplify.cc (gfc_simplify_nearest): Fix off-by-one error in setting up real kind-specific maximum exponent for mpfr. gcc/testsuite/ChangeLog: PR fortran/109186 * gfortran.dg/nearest_6.f90: New test. --- gcc/fortran/simplify.cc | 2 +- gcc/testsuite/gfortran.dg/nearest_6.f90 | 26 +++++++++++++++++++++++++ 2 files changed, 27 insertions(+), 1 deletion(-) create mode 100644 gcc/testsuite/gfortran.dg/nearest_6.f90 diff --git a/gcc/fortran/simplify.cc b/gcc/fortran/simplify.cc index 20ea38e0007..ecf0e3558df 100644 --- a/gcc/fortran/simplify.cc +++ b/gcc/fortran/simplify.cc @@ -6114,7 +6114,7 @@ gfc_simplify_nearest (gfc_expr *x, gfc_expr *s) kind = gfc_validate_kind (BT_REAL, x->ts.kind, 0); mpfr_set_emin ((mpfr_exp_t) gfc_real_kinds[kind].min_exponent - mpfr_get_prec(result->value.real) + 1); - mpfr_set_emax ((mpfr_exp_t) gfc_real_kinds[kind].max_exponent - 1); + mpfr_set_emax ((mpfr_exp_t) gfc_real_kinds[kind].max_exponent); mpfr_check_range (result->value.real, 0, MPFR_RNDU); if (mpfr_sgn (s->value.real) > 0) diff --git a/gcc/testsuite/gfortran.dg/nearest_6.f90 b/gcc/testsuite/gfortran.dg/nearest_6.f90 new file mode 100644 index 00000000000..00d1ebe618c --- /dev/null +++ b/gcc/testsuite/gfortran.dg/nearest_6.f90 @@ -0,0 +1,26 @@ +! { dg-do run } +! PR fortran/109186 - Verify that NEAREST produces same results at +! compile-time and run-time for corner cases +! Reported by John Harper + +program p + implicit none + integer, parameter :: sp = selected_real_kind (6) + integer, parameter :: dp = selected_real_kind (13) + real(sp), parameter :: x1 = huge (1._sp), t1 = tiny (1._sp) + real(dp), parameter :: x2 = huge (1._dp), t2 = tiny (1._dp) + real(sp), volatile :: y1, z1 + real(dp), volatile :: y2, z2 + y1 = x1 + z1 = nearest (y1, -1._sp) + if (nearest (x1, -1._sp) /= z1) stop 1 + y2 = x2 + z2 = nearest (y2, -1._dp) + if (nearest (x2, -1._dp) /= z2) stop 2 + y1 = t1 + z1 = nearest (y1, 1._sp) + if (nearest (t1, 1._sp) /= z1) stop 3 + y2 = t2 + z2 = nearest (y2, 1._dp) + if (nearest (t2, 1._dp) /= z2) stop 4 +end -- 2.35.3