pawn-lang / compiler

Pawn compiler for SA-MP with bug fixes and new features - runs on Windows, Linux, macOS
Other
301 stars 71 forks source link

Incorrect division of integers in special cases #688

Open MuthaX opened 2 years ago

MuthaX commented 2 years ago

Issue description:

I don’t know exactly how it is related is for the compiler or not. If you divide one INTEGER number by another INTEGER, then in some cases you can get strange results. This issue appears only at runtime. The code (with const values) that was optimised at compile-time works perfectly.

If you divide some positive integer N (>0xFFFF) by any integer M from range (represented in hex as) [0x0x7FFF3BB7 - 0x7FFFFFF] you will get result = 1 (expected 0 if +N<M and 1 otherwise). Dividing N by M=[0x0 - 0x7FFF3BB6] works as expected. Dividing N by M=0x8000000 will give you result = 1 (expected 0). Dividing N by any integer from range M=[(-N-1) - (cellmin+1)] will give you result = -1 (expected 0).

Minimal complete verifiable example (MCVE):

new N, M, Res;
N=100500; M=0x7FFF3BB6; Res=N/M; // resulted as 0, expected 0. OK.
N=100500; M=0x7FFF3BB7; Res=N/M; // resulted as -1, expected 0.
N=100500; M=0x7FFFFFFF; Res=N/M; // resulted as -1, expected 0.
N=100500;M=0x80000000; Res=N/M; // resulted as 1, expected 0.
N=100;M=-128; Res=N/M; // resulted as -1, expected 0.
The next examples works as expected (cause of right optimisation of compiler).
Res=100500/0x7FFF3BB7;
Res=100500/0x7FFF3BB6;
Res=100500/0x7FFFFFFF;
Res=100500/0x7FFF3BB7;
Res=100500/0x80000000;
Res=100/-128;

Workspace Information:

stale[bot] commented 2 years ago

This issue has been automatically marked as stale because it has not had recent activity.