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

This commit is contained in:
J. Gareth "Curious Kit" Moreton 2022-08-18 07:30:58 +01:00 committed by FPK
parent 9ad1995c6d
commit 32f4931fd2

View File

@ -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 }