https://gcc.gnu.org/bugzilla/show_bug.cgi?id=123959
Bug ID: 123959
Summary: Bit shift operators fail with variable as shift
argument
Product: gcc
Version: 16.0
Status: UNCONFIRMED
Severity: normal
Priority: P3
Component: algol68
Assignee: algol68 at gcc dot gnu.org
Reporter: jpl.algol68 at gmail dot com
Target Milestone: ---
Bit shifting by a literal constant works, but shifting by same amount using a
variable fails (gives erroneous result). Furthermore, re-running the
executables will give different results.
There are two examples:
1) This first one uses 2**31 to set the msb (I suppose this could produce
undefined behavior since the result would not be a valid int32. Nevertheless
there is no run-time error.
2) This second one uses 2**30 to produce a valid int32.
Both of this examples fail in the same manner.
-------File first_bit.a68 --------------------------------------------------
access Transput
begin
proc printbits = (int x) void:
begin
bits b := Bin(x);
for i from 32 by -1 to 1 do
( i Mod 4 = 0 | puts((" ")));
( b TEST 32 | puts(("1")) | puts(("0")));
b := b Shl 1
od
end;
int first_bit = 2**31;
int myshift = 29;
int works := 0;
int fails := 0;
puts(("first_bit: " + whole(first_bit, 20) + " "));
printbits(first_bit);
puts(("'n"));
works := Abs(Bin(first_bit) Shr 29);
puts(("shifting by literal 29'n"));
puts((" works: "));
printbits(works);
puts(("'n"));
puts(("shifting by variable with value of 29'n"));
puts(("myshift: " + whole(myshift, 5) + "'n"));
fails := Abs(Bin(first_bit) Shr myshift);
puts((" fails: "));
printbits(fails);
puts(("'n"))
end
-------File second_bit.a68 --------------------------------------------------
access Transput
begin
proc printbits = (int x) void:
begin
bits b := Bin(x);
for i from 32 by -1 to 1 do
( i Mod 4 = 0 | puts((" ")));
( b TEST 32 | puts(("1")) | puts(("0")));
b := b Shl 1
od
end;
int second_bit = 2**30;
int myshift = 29;
int works := 0;
int fails := 0;
puts(("second_bit: " + whole(second_bit, 20) + " "));
printbits(second_bit);
puts(("'n"));
works := Abs(Bin(second_bit) Shr 29);
puts(("shifting by literal 29'n"));
puts((" works: "));
printbits(works);
puts(("'n"));
puts(("shifting by variable with value of 29'n"));
puts(("myshift: " + whole(myshift, 5) + "'n"));
fails := Abs(Bin(second_bit) Shr myshift);
puts((" fails: "));
printbits(fails);
puts(("'n"))
end