typecast+SHR can generate wrong code on x86-32.
Original Reporter info from Mantis: runewalsh
-
Reporter name:
Original Reporter info from Mantis: runewalsh
- Reporter name:
Description:
SHR‘ing a typecasted value sometimes tricks the compiler into ignoring that typecast, see below.
I blundered into this bug SOLELY on x86-32 and with optimization level 1, x86-64 or any other optimization level (including 0) work cleanly, doing either MOVZX m8 or AND $ff.
Steps to reproduce:
function Get8bShr1_CORRECT(n: SizeUint): SizeUint;
begin
result := uint8(n) shr 1;
end;
function Get8bShr1_BUGGY_x86_32(n: SizeUint): SizeUint;
begin
result := n;
result := uint8(result) shr 1;
end;
begin
writeln('Correct: ', HexStr(Get8bShr1_CORRECT($AAAAAA), bitsizeof(SizeUint) div 4));
writeln('Wrong: ', HexStr(Get8bShr1_BUGGY_x86_32($AAAAAA), bitsizeof(SizeUint) div 4));
end.
Additional information:
Correct: 00000055
Wrong: 00555555
Same thing with uint16.
Mantis conversion info:
- Mantis ID: 37254
- Build: 63406
- Platform: x86-32
- Version: 3.3.1
- Fixed in version: 3.3.1
- Fixed in revision: 45713 (#d41f4c7c)