Bug 96911 - bug with Intrinsic shifta/shiftl/shiftr
Summary: bug with Intrinsic shifta/shiftl/shiftr
Status: RESOLVED INVALID
Alias: None
Product: gcc
Classification: Unclassified
Component: fortran (show other bugs)
Version: 10.1.0
: P3 normal
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2020-09-03 07:28 UTC by zhen.xu
Modified: 2020-09-04 01:20 UTC (History)
2 users (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description zhen.xu 2020-09-03 07:28:05 UTC
Dealing with Intrinsic shifta/shiftl/shiftr, gfortran refuses proper numbers in the code below and throws "Error: Integer too big for its kind". 

The code is checked with ifort.

-------code----------
program test
  print *, shifta(-128_1, 1);
  print *, shifta(-32768_2, 1);
  print *, shifta(-2147483648_4, 1);
  print *, shifta(-9223372036854775808_8, 1);

  print *, shiftl(-128_1, 1);
  print *, shiftl(-32768_2, 1);
  print *, shiftl(-2147483648_4, 1);
  print *, shiftl(-9223372036854775808_8, 1);

  print *, shiftr(-128_1, 1);
  print *, shiftr(-32768_2, 1);
  print *, shiftr(-2147483648_4, 1);
  print *, shiftr(-9223372036854775808_8, 1);

end
----------------------
Comment 1 anlauf 2020-09-03 14:44:28 UTC
Error: Integer too big for its kind at (1). This check can be disabled with the option '-fno-range-check'

Why don't you read what gfortran is telling you, and acting appropriately?
Comment 2 kargl 2020-09-03 16:44:30 UTC
(In reply to zhen.xu from comment #0)
> Dealing with Intrinsic shifta/shiftl/shiftr, gfortran refuses proper numbers
> in the code below and throws "Error: Integer too big for its kind". 
> 
> The code is checked with ifort.
> 
> -------code----------
> program test
>   print *, shifta(-128_1, 1);
>   print *, shifta(-32768_2, 1);
>   print *, shifta(-2147483648_4, 1);
>   print *, shifta(-9223372036854775808_8, 1);
> 
>   print *, shiftl(-128_1, 1);
>   print *, shiftl(-32768_2, 1);
>   print *, shiftl(-2147483648_4, 1);
>   print *, shiftl(-9223372036854775808_8, 1);
> 
>   print *, shiftr(-128_1, 1);
>   print *, shiftr(-32768_2, 1);
>   print *, shiftr(-2147483648_4, 1);
>   print *, shiftr(-9223372036854775808_8, 1);
> 
> end
> ----------------------

There are no negative integer withs gfortran.  -128_1 is a unary minus
operator and 128_1 is a invalid operand. 128_1 would be greater than
huge(1_1).  The only way to get the most "negative integer" is
to do -huge(1_1) - 1 (assuming twos-complement arithmetic applies).
Comment 3 zhen.xu 2020-09-04 00:54:28 UTC
(In reply to anlauf from comment #1)
> Error: Integer too big for its kind at (1). This check can be disabled with
> the option '-fno-range-check'
> 
> Why don't you read what gfortran is telling you, and acting appropriately?


The fact is -128 is surely not too big for integer(1). The below code is compiled by gfortran and can print out -128 properly.
--------------------
program testint
      integer(1) :: x = -128
      print *, x
end
____________________
Comment 4 zhen.xu 2020-09-04 01:03:18 UTC
(In reply to kargl from comment #2)
> (In reply to zhen.xu from comment #0)
> > Dealing with Intrinsic shifta/shiftl/shiftr, gfortran refuses proper numbers
> > in the code below and throws "Error: Integer too big for its kind". 
> > 
> > The code is checked with ifort.
> > 
> > -------code----------
> > program test
> >   print *, shifta(-128_1, 1);
> >   print *, shifta(-32768_2, 1);
> >   print *, shifta(-2147483648_4, 1);
> >   print *, shifta(-9223372036854775808_8, 1);
> > 
> >   print *, shiftl(-128_1, 1);
> >   print *, shiftl(-32768_2, 1);
> >   print *, shiftl(-2147483648_4, 1);
> >   print *, shiftl(-9223372036854775808_8, 1);
> > 
> >   print *, shiftr(-128_1, 1);
> >   print *, shiftr(-32768_2, 1);
> >   print *, shiftr(-2147483648_4, 1);
> >   print *, shiftr(-9223372036854775808_8, 1);
> > 
> > end
> > ----------------------
> 
> There are no negative integer withs gfortran.  -128_1 is a unary minus
> operator and 128_1 is a invalid operand. 128_1 would be greater than
> huge(1_1).  The only way to get the most "negative integer" is
> to do -huge(1_1) - 1 (assuming twos-complement arithmetic applies).

But how can the code below be compiled by gfortran well and print out -128 properly?
--------------------
program testint
      integer(1) :: x = -128
      print *, x
end
____________________
Comment 5 zhen.xu 2020-09-04 01:18:39 UTC
(In reply to kargl from comment #2)
> (In reply to zhen.xu from comment #0)
> > Dealing with Intrinsic shifta/shiftl/shiftr, gfortran refuses proper numbers
> > in the code below and throws "Error: Integer too big for its kind". 
> > 
> > The code is checked with ifort.
> > 
> > -------code----------
> > program test
> >   print *, shifta(-128_1, 1);
> >   print *, shifta(-32768_2, 1);
> >   print *, shifta(-2147483648_4, 1);
> >   print *, shifta(-9223372036854775808_8, 1);
> > 
> >   print *, shiftl(-128_1, 1);
> >   print *, shiftl(-32768_2, 1);
> >   print *, shiftl(-2147483648_4, 1);
> >   print *, shiftl(-9223372036854775808_8, 1);
> > 
> >   print *, shiftr(-128_1, 1);
> >   print *, shiftr(-32768_2, 1);
> >   print *, shiftr(-2147483648_4, 1);
> >   print *, shiftr(-9223372036854775808_8, 1);
> > 
> > end
> > ----------------------
> 
> There are no negative integer withs gfortran.  -128_1 is a unary minus
> operator and 128_1 is a invalid operand. 128_1 would be greater than
> huge(1_1).  The only way to get the most "negative integer" is
> to do -huge(1_1) - 1 (assuming twos-complement arithmetic applie
You are right(In reply to anlauf from comment #1)
> Error: Integer too big for its kind at (1). This check can be disabled with
> the option '-fno-range-check'
> 
> Why don't you read what gfortran is telling you, and acting appropriately?

Sorry, I made a mistake. You are right.
(In reply to anlauf from comment #1)
> Error: Integer too big for its kind at (1). This check can be disabled with
> the option '-fno-range-check'
> 
> Why don't you read what gfortran is telling you, and acting appropriately?

You are right! I made a mistake, please ignore my previous reply.

The right testcase should be below and it throws out the same prompt.
-------------------------------
program testint
  integer(1) :: x = -128_1
  print *, x
end
-------------------------------
Comment 6 zhen.xu 2020-09-04 01:20:23 UTC
(In reply to kargl from comment #2)
> (In reply to zhen.xu from comment #0)
> > Dealing with Intrinsic shifta/shiftl/shiftr, gfortran refuses proper numbers
> > in the code below and throws "Error: Integer too big for its kind". 
> > 
> > The code is checked with ifort.
> > 
> > -------code----------
> > program test
> >   print *, shifta(-128_1, 1);
> >   print *, shifta(-32768_2, 1);
> >   print *, shifta(-2147483648_4, 1);
> >   print *, shifta(-9223372036854775808_8, 1);
> > 
> >   print *, shiftl(-128_1, 1);
> >   print *, shiftl(-32768_2, 1);
> >   print *, shiftl(-2147483648_4, 1);
> >   print *, shiftl(-9223372036854775808_8, 1);
> > 
> >   print *, shiftr(-128_1, 1);
> >   print *, shiftr(-32768_2, 1);
> >   print *, shiftr(-2147483648_4, 1);
> >   print *, shiftr(-9223372036854775808_8, 1);
> > 
> > end
> > ----------------------
> 
> There are no negative integer withs gfortran.  -128_1 is a unary minus
> operator and 128_1 is a invalid operand. 128_1 would be greater than
> huge(1_1).  The only way to get the most "negative integer" is
> to do -huge(1_1) - 1 (assuming twos-complement arithmetic applies).

You are right! I made a mistake, please ignore my previous reply.

The right testcase should be below and it throws out the same prompt.
-------------------------------
program testint
  integer(1) :: x = -128_1
  print *, x
end
-------------------------------