Possible bug with range-checking option lazarus / 64-FPC development ---------------------------------------------------------------------- ad1mt · 2024-03-07 09:26 I think I've found a bug with the range-checking option. If you run the following program twice, once with range-checking off, and once with range-checking on, you will see a discrepency in the results. Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i program bitshift_demo;uses strutils;vari_16u           :uint16;i_16u_shift   :uint16;i_16s           :int16;i_16s_shift     :int16;begini_16s:= 32767;writeln('i_16s = ',IntToBin(i_16s,16),' ',i_16s);write('i_16s shl 1 = ');try i_16s_shift:= (i_16s shl 1); except; end;writeln(IntToBin(i_16s_shift,16),' ',i_16s_shift);writeln;i_16u:= 65535;writeln('i_16u = ',IntToBin(i_16u,16),' ',i_16u);write('i_16u shl 1 = ');try i_16u_shift:= (i_16u shl 1); except; end;writeln(IntToBin(i_16u_shift,16),' ',i_16u_shift);writeln;end.The problem does not arise with smaller integer values. The problem does not affect 32-bit and 64-bit integers. What is interesting is that if range-checking is set in the source code with inline directives, everything works correctly, as in the following program: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i program bitshift_demo_R;uses strutils;vari_16u           :uint16;i_16u_shift   :uint16;i_16s           :int16;i_16s_shift     :int16;begin{$R-}i_16s:= 32767;writeln('i_16s = ',IntToBin(i_16s,16),' ',i_16s);write('i_16s shl 1 = ');try i_16s_shift:= (i_16s shl 1); except; end;writeln(IntToBin(i_16s_shift,16),' ',i_16s_shift);writeln;{$R+}i_16s:= 32767;writeln('i_16s = ',IntToBin(i_16s,16),' ',i_16s);write('i_16s shl 1 = ');try i_16s_shift:= (i_16s shl 1); except; end;writeln(IntToBin(i_16s_shift,16),' ',i_16s_shift);writeln;end.So it might not be a problem with the compiler itself, but in the way it is being called by the Lazarus IDE? ---------------------------------------------------------------------- Leledumbo · 2024-03-07 09:35 I'm not sure whether to categorize it a bug or not. Basically with range checking on, the except block does execute and raises ERangeError, which you deliberately ignore, but may do something to the assigned value. Maybe just report it, let the core devs decide. ---------------------------------------------------------------------- bytebites · 2024-03-07 09:52 without range check i_16s = 0111111111111111 32767 i_16s shl 1 = 1111111111111110 -2 i_16s = 0111111111111111 32767 i_16s shl 1 = 1111111111111110 -2 with range check i_16s = 0111111111111111 32767 i_16s shl 1 = 1111111111111110 -2 i_16s = 0111111111111111 32767 i_16s shl 1 = 1111111111111110 -2 ---------------------------------------------------------------------- ad1mt · 2024-03-08 08:32 Probably best to close this... I can not reproduce the problem consistently. ---------------------------------------------------------------------- ad1mt · 2024-03-10 10:27 The problems I've encountered are inconsistencies in behaviour, depending on whether range-checking was switched on, and how it was switched on. These are my latest tests that reproduce the inconsistency in a consistent way ( ): This code works as I would expect: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i program bitshift_demo_4C;uses strutils;varv_int16,v_int16_shift   :int16;beginwriteln('{$R-}');{$R-}v_int16:= 32767;writeln('v_int16 = ',IntToBin(v_int16,16),' ',v_int16);write('v_int16 shl 1 = ');try v_int16_shift:= (v_int16 shl 1); except; end;writeln(IntToBin(v_int16_shift,16),' ',v_int16_shift);writeln;writeln('{$R+}');{$R+}v_int16:= 32767;writeln('v_int16 = ',IntToBin(v_int16,16),' ',v_int16);write('v_int16 shl 1 = ');try v_int16_shift:= (v_int16 shl 1); except; end;writeln(IntToBin(v_int16_shift,16),' ',v_int16_shift);writeln;end.The output from the above code is: Code: Text  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i {$R-}v_int16 = 0111111111111111 32767v_int16 shl 1 = 1111111111111110 -2 {$R+}v_int16 = 0111111111111111 32767v_int16 shl 1 = 1111111111111110 -2However, this code shows the problem, as I see it: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i program bitshift_demo_4D;uses strutils;varv_int16,v_int16_shift   :int16;beginwriteln('{$R+}');{$R+}v_int16:= 32767;writeln('v_int16 = ',IntToBin(v_int16,16),' ',v_int16);write('v_int16 shl 1 = ');try v_int16_shift:= (v_int16 shl 1); except; end;writeln(IntToBin(v_int16_shift,16),' ',v_int16_shift);writeln;writeln('{$R-}');{$R-}v_int16:= 32767;writeln('v_int16 = ',IntToBin(v_int16,16),' ',v_int16);write('v_int16 shl 1 = ');try v_int16_shift:= (v_int16 shl 1); except; end;writeln(IntToBin(v_int16_shift,16),' ',v_int16_shift);writeln;end.The output from the second test is: Code: Text  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i {$R+}v_int16 = 0111111111111111 32767v_int16 shl 1 = 0000000000000000 0 {$R-}v_int16 = 0111111111111111 32767v_int16 shl 1 = 1111111111111110 -2In the first test, the behaviour of the left shift is correct, when range-checking is first switched off and then switched on. In the second test, the behaviour of the left shift is different, when range-checking is first switched on and then switched off. At the point where the behaviour become inconsistent, the state of range-checking is the same, i.e. switched on. One of these cases has to be incorrect, they cannot both be correct. If you are trying to write robust code, this is a big problem. But it also indicates a bug, which might have other side-effects that I have not yet discovered. ---------------------------------------------------------------------- tetrastes · 2024-03-10 17:29 As @Leledumbo noted, your code ignores Range Check Exception (to see it, compile with debug information and run from Lazarus), and Writeln(v_int16_shift) simply outputs the last value of v_int16_shift before shl operation, which in the first example is -2, and in the second one is initialized by 0 by default. To better understand this, add for example Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i v_int16_shift := 5;after lines 9 and 17. The output will be Code: Text  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i {$R-}v_int16 = 0111111111111111 32767v_int16 shl 1 = 1111111111111110 -2 {$R+}v_int16 = 0111111111111111 32767v_int16 shl 1 = 0000000000000101 5 and Code: Text  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i {$R+}v_int16 = 0111111111111111 32767v_int16 shl 1 = 0000000000000101 5 {$R-}v_int16 = 0111111111111111 32767v_int16 shl 1 = 1111111111111110 -2  Also you may remove try ... except and see what you'll get. ---------------------------------------------------------------------- TRon · 2024-03-10 19:34 Quote from: ad1mt on March 07, 2024, 10:26:09 amI think I've found a bug with the range-checking option. Same answer as in this thread in particular this post. What in particular about the contents of these links is not clear to/for you ? (and that is besides the fact that the presented code manipulates the standard default behaviour of the compiler).