Quellcode durchsuchen

* x86: Fixed bug where the magic number of an integer division wasn't fully sign-extended, causing incorrect logic within the compiler.

J. Gareth "Curious Kit" Moreton vor 3 Jahren
Ursprung
Commit
32f4931fd2
1 geänderte Dateien mit 10 neuen und 1 gelöschten Zeilen
  1. 10 1
      compiler/cgutils.pas

+ 10 - 1
compiler/cgutils.pas

@@ -408,7 +408,7 @@ uses
 {$r-,q-}
     procedure calc_divconst_magic_signed(N: byte; d: aInt; out magic_m: aInt; out magic_s: byte);
       var
-        p: aInt;
+        p, sign_corrective_shift: aInt;
         ad,anc,delta,q1,r1,q2,r2,t: aWord;
         two_N_minus_1: aWord;
       begin
@@ -441,7 +441,16 @@ uses
             end;
           delta:=ad-r2;
         until not ((q1<delta) or ((q1=delta) and (r1=0)));
+
         magic_m:=q2+1;
+
+        { Sign-extend magic_m to the full size of aint - fixes #39834 }
+        if N < (SizeOf(aint) * 8) then
+          begin
+            sign_corrective_shift := (SizeOf(aint) * 8) - N;
+            magic_m := SarInt64(magic_m shl sign_corrective_shift, sign_corrective_shift);
+          end;
+
         if (d<0) then
           magic_m:=-magic_m;  { resulting magic number }
         magic_s:=p-N;         { resulting shift }