From 04606982ac7cb24c531f99b08ce61d394a8e2535 Mon Sep 17 00:00:00 2001 From: Jonas Maebe Date: Fri, 1 Jan 2010 22:50:35 +0000 Subject: [PATCH] * fixed evaluation of "mod" operator for tconstexprint with signed operands (part of mantis #15453) git-svn-id: trunk@14516 - --- .gitattributes | 1 + compiler/constexp.pas | 27 ++++++++++++++++++++------- tests/webtbs/tw15453a.pp | 12 ++++++++++++ 3 files changed, 33 insertions(+), 7 deletions(-) create mode 100644 tests/webtbs/tw15453a.pp diff --git a/.gitattributes b/.gitattributes index 5acaa715c1..bc0fa385c6 100644 --- a/.gitattributes +++ b/.gitattributes @@ -10166,6 +10166,7 @@ tests/webtbs/tw15377.pp svneol=native#text/pascal tests/webtbs/tw1539.pp svneol=native#text/plain tests/webtbs/tw15391.pp svneol=native#text/plain tests/webtbs/tw15415.pp svneol=native#text/plain +tests/webtbs/tw15453a.pp svneol=native#text/plain tests/webtbs/tw1567.pp svneol=native#text/plain tests/webtbs/tw1573.pp svneol=native#text/plain tests/webtbs/tw1592.pp svneol=native#text/plain diff --git a/compiler/constexp.pas b/compiler/constexp.pas index 787c16fd19..075b3879d9 100644 --- a/compiler/constexp.pas +++ b/compiler/constexp.pas @@ -380,7 +380,8 @@ end; operator mod (const a,b:Tconstexprint):Tconstexprint; -var aa,bb:qword; +var aa,bb,r:qword; + sa,sb:boolean; begin if a.overflow or b.overflow then @@ -389,20 +390,32 @@ begin exit; end; result.overflow:=false; - if a.signed then - aa:=qword(a.svalue) + sa:=a.signed and (a.svalue<0); + if sa then + {$Q-} + aa:=qword(-a.svalue) + {$ifdef ena_q}{$Q+}{$endif} else aa:=a.uvalue; - if b.signed then - bb:=qword(b.svalue) + sb:=b.signed and (b.svalue<0); + if sb then + {$Q-} + bb:=qword(-b.svalue) + {$ifdef ena_q}{$Q+}{$endif} else bb:=b.uvalue; if bb=0 then result.overflow:=true else begin - result.signed:=false; - result.uvalue:=aa mod bb; + { the sign of a modulo operation only depends on the sign of the + dividend } + r:=aa mod bb; + result.signed:=sa; + if not sa then + result.uvalue:=r + else + result.svalue:=-int64(r); end; end; diff --git a/tests/webtbs/tw15453a.pp b/tests/webtbs/tw15453a.pp new file mode 100644 index 0000000000..88a1f7c55a --- /dev/null +++ b/tests/webtbs/tw15453a.pp @@ -0,0 +1,12 @@ +uses Math; + +var + Result, Remainder: SmallInt; +begin + Result := -9 div 5; + Remainder := -9 mod 5; + writeln(result,' - ',remainder); + if (result<>-1) or + (remainder<>-4) then + halt(1); +end.