From patchwork Mon Feb 27 20:54:38 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Harald Anlauf X-Patchwork-Id: 62136 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:5915:0:0:0:0:0 with SMTP id v21csp2640624wrd; Mon, 27 Feb 2023 12:55:43 -0800 (PST) X-Google-Smtp-Source: AK7set96XpkIheScV8VFLXSVjlC+rIf7H8616YUWOiONArcO94vfydTRUTMDBGLnfKh+fG6p9ZKw X-Received: by 2002:a17:906:4d87:b0:878:72d0:2817 with SMTP id s7-20020a1709064d8700b0087872d02817mr38466eju.29.1677531343561; Mon, 27 Feb 2023 12:55:43 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1677531343; cv=none; d=google.com; s=arc-20160816; b=aetZzEHNndSvW8zllJa/Zfr2bUWOwN9mLuQUxA/dJMkTgOxfjf0+8rTRNoAs7BlVBN oYuh3XH5K/q2J6/Bo+1L8HKEtqCfpPjGHbVOoNJJwS8+BtSXHnZwiYzOsCrzaHY/l8f9 H3Vmhgm/HyJ+Xj9AvMGJEDUCb0zx1kB0/Zcmfo4tZ2jaUmODNXMNEPjjZH4yTljaXr9K 2YSpzFmwjOyqa9IKLnZB9XkhitQeMuy2rjLBqv0ir/Sw73uUxl4mOY9PyjdCDiV7s9oa W4UDlFYplvH73kR4JatjdTYI9lPvxlNr+OcAYl03fO3SBsBN82kq/EeAyY6j81JeAv+0 D8nw== 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=ukK7+bQjHvSeRKvNRBfdj1Tf7weuOmaFsYacXX/jIDY=; b=sP3M+KXSIPbak3Dx8pPLfvS7MAs1tHPh5GW53YGEtggBYVHRfTOV1BLzc++umuCXce nVdP7hA7+6wfJyCrvzjkdmJKupYJMYgDEiy4zLM/o9AmmT09mC8PP5oqyJsmJ775DQfV Lh+bT/nkJ4IXKk+PHq5wvCf41SRXwGWn7RaOxw76LuqJJs5lFDwoYG1d0bi78bKT0311 bLB7E59NWNbmFyShNBZYxdA/kvmK26NpXo71Jb7hbXkJm1IlC3Z+VsNwf0c5UxkIL/B9 o3wcurOq6MMuFWxDRox88HHHyCaOAxBrIu4hT/RPRK/U2PkbyWN+oT62MpAo2uU+Na3b SmwQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=NW1a+QH1; 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 v8-20020a170906564800b008cadb41bef7si150432ejr.169.2023.02.27.12.55.43 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 27 Feb 2023 12:55: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=NW1a+QH1; 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 913373850854 for ; Mon, 27 Feb 2023 20:55:25 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 913373850854 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1677531325; bh=ukK7+bQjHvSeRKvNRBfdj1Tf7weuOmaFsYacXX/jIDY=; h=To:Subject:Date:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:From; b=NW1a+QH1/EDLLlGRIPOY8XtKFz93hlilCyWkdxP4WT0yGKRInkcCSeEwXkvAlGV/L wFqV/4umZAmFVilv5Irg4EGPqxs49DeQUo+t/uZvNboAUFbzpQFj6Ooy81oVQlCfcq wL2ZJ2Y4zbl7kvUm8SnCLYpyPq01VPFzQCXOvdhM= 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.17.22]) by sourceware.org (Postfix) with ESMTPS id B693D385842C; Mon, 27 Feb 2023 20:54:39 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org B693D385842C X-UI-Sender-Class: 724b4f7f-cbec-4199-ad4e-598c01a50d3a Received: from [93.207.83.46] ([93.207.83.46]) by web-mail.gmx.net (3c-app-gmx-bap31.server.lan [172.19.172.101]) (via HTTP); Mon, 27 Feb 2023 21:54:38 +0100 MIME-Version: 1.0 Message-ID: To: fortran , gcc-patches Subject: [PATCH] Fortran: fix corner case of IBITS intrinsic [PR108937] Date: Mon, 27 Feb 2023 21:54:38 +0100 Importance: normal Sensitivity: Normal X-Priority: 3 X-Provags-ID: V03:K1:IwBvMgiU97OXLtzvTQFDfA+A3gdkNkbuRQg/lra8CQeBgL4WelDJu6qBueGbkw+C+GBTg ahWvsSswTIZqrCMQpy8XEnUU6ek3eObzt5ZIHUNgyL9kqetGgWdzpVruIic7aRk6zD8lcTV3Trhv cNfqeQyZMcA7atBeK7KX43bpmqrlu0o1MKFsKASLPuvPFRftXTOVUwaBDgjthpJLRBv2jaq/3dIo n13e8/ZxNxOpskfiV7EFRFOUYv9np52LEGquBgCQMpxbQhB7dwoIWxqeXHYgpnMiMlStLGnlbX5S Vc= UI-OutboundReport: notjunk:1;M01:P0:D4CoByGsZjw=;oSlRF2+o9GwP7F/PONPkcjX5Ym8 hd5ZRy/6zDx1dpi1jOuwpp5nj1MzZIcFiE4kesjWI/a0055x/QeYsGzOGygWFzJoi1fceP196 oRsdVARB0XIZN+W3AuQO3s2L8IfehqRp5ZBc/QGQ6Ja/+8xlka5Rz/Q2IikTPETMi1/8FTi5P LgtkBYc8nh3kdxYLiOtv/KS9FSQ0p2yJvSyjDkDzjEP13MwqjWRCsaGscMZyu7zF8zi1SnwLY I71UY6+4krgZ9TcbeBysg+IkPQLL/cfXB1e6Kld/smQeZkwv+S2ZjVcW3HTn/UpNEU0sXQ9Rn KNt4qz3nJQFNa7+Tf1gq/M9VTzb/NLtnQqBCvgg5yLbQDlGO5VaIBArAZJuU3hAZFTIgLMUS5 nLKD7+3hpCMkZW01FtPiqPJNjQegoWsogvfnujc7moYsC5IMAHautEUN3FD+FHYv/1A72ITIO 94IdW7RLja2daIJE9UUxe8360e2O6ng85nlseqNwf1EjGqk3RrIhS94P+SIzX1d8/Dm+FxuaZ QffnMVfMUrrpj7h/lH1xFZzyd1Aff/mxUPe08olGDMZsqRcD7ZdmSGH4eikdgw92hFOEyUQox 1qkOIgAu29E3GYqSMTPrsdSNVj1zYQ7lOQQ1fiIBKBKDGxgMGXlydo8GGh6bQiTK0TvR7mzvk GYzxq3ASKXTM/hitDbcdOM93zWafEVDYACpNyY2LGmKOKMOIE2RDNCbjA1WyyRKa1bKgr5X6z fFWG51rEd9NRcpdlS+h4Anv2zcEIEtl7gZ/DaeesLzIi52jaDAtcHqJn+HlTGqMwzEunNRcA4 /3F3T69HKtra5x/Tr8xrsovA== X-Spam-Status: No, score=-12.8 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?1759019106141302682?= X-GMAIL-MSGID: =?utf-8?q?1759019106141302682?= Dear all, as found by the reporter, the result of the intrinsic IBITS differed from other compilers (e.g. Intel, NAG) for the corner case that the LEN argument was equal to BIT_SIZE(I), which is explicitly allowed by the standard. We actually had an inconsistency for this case between code generated by the frontend and compile-time simplified expressions. The reporter noticed that this is related to a restriction in gcc that requires that shift widths shall be smaller than the bit sizes, and we already special case this for ISHFT. It makes sense to use the same special casing for IBITS. Attached patch fixes this and regtests on x86_64-pc-linux-gnu. OK for mainline? This issue has been there for ages. Shall this be backported or left in release branches as is? Thanks, Harald From 6844c5ecb271e091a8c913903a79eac932cf5f76 Mon Sep 17 00:00:00 2001 From: Harald Anlauf Date: Mon, 27 Feb 2023 21:37:11 +0100 Subject: [PATCH] Fortran: fix corner case of IBITS intrinsic [PR108937] gcc/fortran/ChangeLog: PR fortran/108937 * trans-intrinsic.cc (gfc_conv_intrinsic_ibits): Handle corner case LEN argument of IBITS equal to BITSIZE(I). gcc/testsuite/ChangeLog: PR fortran/108937 * gfortran.dg/ibits_2.f90: New test. --- gcc/fortran/trans-intrinsic.cc | 10 +++++++++ gcc/testsuite/gfortran.dg/ibits_2.f90 | 32 +++++++++++++++++++++++++++ 2 files changed, 42 insertions(+) create mode 100644 gcc/testsuite/gfortran.dg/ibits_2.f90 diff --git a/gcc/fortran/trans-intrinsic.cc b/gcc/fortran/trans-intrinsic.cc index 21eeb12ca89..3cce9c0166e 100644 --- a/gcc/fortran/trans-intrinsic.cc +++ b/gcc/fortran/trans-intrinsic.cc @@ -6638,6 +6638,7 @@ gfc_conv_intrinsic_ibits (gfc_se * se, gfc_expr * expr) tree type; tree tmp; tree mask; + tree num_bits, cond; gfc_conv_intrinsic_function_args (se, expr, args, 3); type = TREE_TYPE (args[0]); @@ -6678,8 +6679,17 @@ gfc_conv_intrinsic_ibits (gfc_se * se, gfc_expr * expr) "in intrinsic IBITS", tmp1, tmp2, nbits); } + /* The Fortran standard allows (shift width) LEN <= BIT_SIZE(I), whereas + gcc requires a shift width < BIT_SIZE(I), so we have to catch this + special case. See also gfc_conv_intrinsic_ishft (). */ + num_bits = build_int_cst (TREE_TYPE (args[2]), TYPE_PRECISION (type)); + mask = build_int_cst (type, -1); mask = fold_build2_loc (input_location, LSHIFT_EXPR, type, mask, args[2]); + cond = fold_build2_loc (input_location, GE_EXPR, logical_type_node, args[2], + num_bits); + mask = fold_build3_loc (input_location, COND_EXPR, type, cond, + build_int_cst (type, 0), mask); mask = fold_build1_loc (input_location, BIT_NOT_EXPR, type, mask); tmp = fold_build2_loc (input_location, RSHIFT_EXPR, type, args[0], args[1]); diff --git a/gcc/testsuite/gfortran.dg/ibits_2.f90 b/gcc/testsuite/gfortran.dg/ibits_2.f90 new file mode 100644 index 00000000000..2af5542d764 --- /dev/null +++ b/gcc/testsuite/gfortran.dg/ibits_2.f90 @@ -0,0 +1,32 @@ +! { dg-do run } +! { dg-additional-options "-fcheck=bits" } +! PR fortran/108937 - Intrinsic IBITS(I,POS,LEN) fails when LEN equals +! to BIT_SIZE(I) +! Contributed by saitofuyuki@jamstec.go.jp + +program test_bits + implicit none + integer, parameter :: KT = kind (1) + integer, parameter :: lbits = bit_size (0_KT) + integer(kind=KT) :: x, y0, y1 + integer(kind=KT) :: p, l + + x = -1 + p = 0 + do l = 0, lbits + y0 = ibits (x, p, l) + y1 = ibits_1(x, p, l) + if (y0 /= y1) then + print *, l, y0, y1 + stop 1+l + end if + end do +contains + elemental integer(kind=KT) function ibits_1(I, POS, LEN) result(n) + !! IBITS(I, POS, LEN) = (I >> POS) & ~((~0) << LEN) + implicit none + integer(kind=KT),intent(in) :: I + integer, intent(in) :: POS, LEN + n = IAND (ISHFT(I, - POS), NOT(ISHFT(-1_KT, LEN))) + end function ibits_1 +end program test_bits -- 2.35.3