From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 2071) id 4DE133857C43; Sun, 5 Mar 2023 19:35:47 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 4DE133857C43 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1678044947; bh=LhMxAc4dGX6LUIu+5ybC8VgkTBlOlfqbbd0WSkj/7Gk=; h=From:To:Subject:Date:From; b=jyxcIli0tYFKgJZlALTBN8zhtkoKfP/wBECmiEQDFlj3Oj+3iGSM2x516mZ9H72WE RVNRfwstpz52d9yFcwfTO7N4XVvok0WjjB9m571zeyDoHPbIvwID38l1D/nTNSaasd R6l52iR+eblQT8MltyDmM2USLQHg9yMpkc2JfVFY= MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Content-Type: text/plain; charset="utf-8" From: Harald Anlauf To: gcc-cvs@gcc.gnu.org Subject: [gcc r11-10560] Fortran: fix corner case of IBITS intrinsic [PR108937] X-Act-Checkin: gcc X-Git-Author: Harald Anlauf X-Git-Refname: refs/heads/releases/gcc-11 X-Git-Oldrev: 7db643a17e8c862b4c758fd69d62a45c6de43d38 X-Git-Newrev: adc4c8eb79a75bd0f38a461c299b37c643a1153c Message-Id: <20230305193547.4DE133857C43@sourceware.org> Date: Sun, 5 Mar 2023 19:35:47 +0000 (GMT) List-Id: https://gcc.gnu.org/g:adc4c8eb79a75bd0f38a461c299b37c643a1153c commit r11-10560-gadc4c8eb79a75bd0f38a461c299b37c643a1153c Author: Harald Anlauf Date: Mon Feb 27 21:37:11 2023 +0100 Fortran: fix corner case of IBITS intrinsic [PR108937] gcc/fortran/ChangeLog: PR fortran/108937 * trans-intrinsic.c (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. (cherry picked from commit 6cce953ebec274f1468d5d3a0697cf05bb43b8f6) Diff: --- gcc/fortran/trans-intrinsic.c | 10 ++++++++++ gcc/testsuite/gfortran.dg/ibits_2.f90 | 32 ++++++++++++++++++++++++++++++++ 2 files changed, 42 insertions(+) diff --git a/gcc/fortran/trans-intrinsic.c b/gcc/fortran/trans-intrinsic.c index 313440d7022..05dc2c7c9a6 100644 --- a/gcc/fortran/trans-intrinsic.c +++ b/gcc/fortran/trans-intrinsic.c @@ -6645,6 +6645,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]); @@ -6685,8 +6686,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